Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "gridcontrol.hxx"
22 : #include "grideventforwarder.hxx"
23 :
24 : #include <com/sun/star/uno/XComponentContext.hpp>
25 : #include <com/sun/star/view/SelectionType.hpp>
26 : #include <com/sun/star/awt/grid/XGridControl.hpp>
27 : #include <com/sun/star/awt/grid/XGridDataModel.hpp>
28 : #include <com/sun/star/awt/grid/XGridRowSelection.hpp>
29 : #include <com/sun/star/awt/grid/XMutableGridDataModel.hpp>
30 : #include <com/sun/star/awt/grid/DefaultGridDataModel.hpp>
31 : #include <com/sun/star/awt/grid/SortableGridDataModel.hpp>
32 : #include <com/sun/star/awt/grid/DefaultGridColumnModel.hpp>
33 : #include <toolkit/helper/property.hxx>
34 : #include <tools/diagnose_ex.h>
35 : #include <tools/color.hxx>
36 : #include <toolkit/controls/unocontrolbase.hxx>
37 : #include <toolkit/controls/unocontrolmodel.hxx>
38 : #include <toolkit/helper/listenermultiplexer.hxx>
39 :
40 : #include <boost/scoped_ptr.hpp>
41 :
42 : #include "helper/unopropertyarrayhelper.hxx"
43 :
44 : using namespace ::com::sun::star;
45 : using namespace ::com::sun::star::uno;
46 : using namespace ::com::sun::star::awt;
47 : using namespace ::com::sun::star::awt::grid;
48 : using namespace ::com::sun::star::lang;
49 : using namespace ::com::sun::star::beans;
50 : using namespace ::com::sun::star::container;
51 : using namespace ::com::sun::star::view;
52 : using namespace ::com::sun::star::util;
53 :
54 : namespace toolkit {
55 :
56 : namespace
57 : {
58 16 : Reference< XGridDataModel > lcl_getDefaultDataModel_throw( const Reference<XComponentContext> & i_context )
59 : {
60 16 : Reference< XMutableGridDataModel > const xDelegatorModel( DefaultGridDataModel::create( i_context ), UNO_QUERY_THROW );
61 16 : Reference< XGridDataModel > const xDataModel( SortableGridDataModel::create( i_context, xDelegatorModel ), UNO_QUERY_THROW );
62 16 : return xDataModel;
63 : }
64 :
65 16 : Reference< XGridColumnModel > lcl_getDefaultColumnModel_throw( const Reference<XComponentContext> & i_context )
66 : {
67 16 : Reference< XGridColumnModel > const xColumnModel = DefaultGridColumnModel::create( i_context );
68 16 : return xColumnModel;
69 : }
70 : }
71 :
72 :
73 16 : UnoGridModel::UnoGridModel( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext )
74 16 : :UnoControlModel( rxContext )
75 : {
76 16 : ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
77 16 : ImplRegisterProperty( BASEPROPERTY_BORDER );
78 16 : ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
79 16 : ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
80 16 : ImplRegisterProperty( BASEPROPERTY_ENABLED );
81 16 : ImplRegisterProperty( BASEPROPERTY_FILLCOLOR );
82 16 : ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
83 16 : ImplRegisterProperty( BASEPROPERTY_HELPURL );
84 16 : ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
85 16 : ImplRegisterProperty( BASEPROPERTY_SIZEABLE ); // resizable
86 16 : ImplRegisterProperty( BASEPROPERTY_HSCROLL );
87 16 : ImplRegisterProperty( BASEPROPERTY_VSCROLL );
88 16 : ImplRegisterProperty( BASEPROPERTY_TABSTOP );
89 16 : ImplRegisterProperty( BASEPROPERTY_GRID_SHOWROWHEADER );
90 16 : ImplRegisterProperty( BASEPROPERTY_ROW_HEADER_WIDTH );
91 16 : ImplRegisterProperty( BASEPROPERTY_GRID_SHOWCOLUMNHEADER );
92 16 : ImplRegisterProperty( BASEPROPERTY_COLUMN_HEADER_HEIGHT );
93 16 : ImplRegisterProperty( BASEPROPERTY_ROW_HEIGHT );
94 16 : ImplRegisterProperty( BASEPROPERTY_GRID_DATAMODEL, makeAny( lcl_getDefaultDataModel_throw( m_xContext ) ) );
95 16 : ImplRegisterProperty( BASEPROPERTY_GRID_COLUMNMODEL, makeAny( lcl_getDefaultColumnModel_throw( m_xContext ) ) );
96 16 : ImplRegisterProperty( BASEPROPERTY_GRID_SELECTIONMODE );
97 16 : ImplRegisterProperty( BASEPROPERTY_FONTRELIEF );
98 16 : ImplRegisterProperty( BASEPROPERTY_FONTEMPHASISMARK );
99 16 : ImplRegisterProperty( BASEPROPERTY_FONTDESCRIPTOR );
100 16 : ImplRegisterProperty( BASEPROPERTY_TEXTCOLOR );
101 16 : ImplRegisterProperty( BASEPROPERTY_TEXTLINECOLOR );
102 16 : ImplRegisterProperty( BASEPROPERTY_USE_GRID_LINES );
103 16 : ImplRegisterProperty( BASEPROPERTY_GRID_LINE_COLOR );
104 16 : ImplRegisterProperty( BASEPROPERTY_GRID_HEADER_BACKGROUND );
105 16 : ImplRegisterProperty( BASEPROPERTY_GRID_HEADER_TEXT_COLOR );
106 16 : ImplRegisterProperty( BASEPROPERTY_GRID_ROW_BACKGROUND_COLORS );
107 16 : ImplRegisterProperty( BASEPROPERTY_ACTIVE_SEL_BACKGROUND_COLOR );
108 16 : ImplRegisterProperty( BASEPROPERTY_INACTIVE_SEL_BACKGROUND_COLOR );
109 16 : ImplRegisterProperty( BASEPROPERTY_ACTIVE_SEL_TEXT_COLOR );
110 16 : ImplRegisterProperty( BASEPROPERTY_INACTIVE_SEL_TEXT_COLOR );
111 16 : ImplRegisterProperty( BASEPROPERTY_VERTICALALIGN );
112 16 : }
113 :
114 :
115 2 : UnoGridModel::UnoGridModel( const UnoGridModel& rModel )
116 2 : :UnoControlModel( rModel )
117 : {
118 2 : osl_atomic_increment( &m_refCount );
119 : {
120 2 : Reference< XGridDataModel > xDataModel;
121 : // clone the data model
122 4 : const Reference< XFastPropertySet > xCloneSource( &const_cast< UnoGridModel& >( rModel ) );
123 : try
124 : {
125 2 : const Reference< XCloneable > xCloneable( xCloneSource->getFastPropertyValue( BASEPROPERTY_GRID_DATAMODEL ), UNO_QUERY_THROW );
126 2 : xDataModel.set( xCloneable->createClone(), UNO_QUERY_THROW );
127 : }
128 0 : catch( const Exception& )
129 : {
130 : DBG_UNHANDLED_EXCEPTION();
131 : }
132 2 : if ( !xDataModel.is() )
133 0 : xDataModel = lcl_getDefaultDataModel_throw( m_xContext );
134 2 : UnoControlModel::setFastPropertyValue_NoBroadcast( BASEPROPERTY_GRID_DATAMODEL, makeAny( xDataModel ) );
135 : // do *not* use setFastPropertyValue here: The UnoControlModel ctor did a simple copy of all property values,
136 : // so before this call here, we share our data model with the own of the clone source. setFastPropertyValue,
137 : // then, disposes the old data model - which means the data model which in fact belongs to the clone source.
138 : // so, call the UnoControlModel's impl-method for setting the value.
139 :
140 : // clone the column model
141 4 : Reference< XGridColumnModel > xColumnModel;
142 : try
143 : {
144 2 : const Reference< XCloneable > xCloneable( xCloneSource->getFastPropertyValue( BASEPROPERTY_GRID_COLUMNMODEL ), UNO_QUERY_THROW );
145 2 : xColumnModel.set( xCloneable->createClone(), UNO_QUERY_THROW );
146 : }
147 0 : catch( const Exception& )
148 : {
149 : DBG_UNHANDLED_EXCEPTION();
150 : }
151 2 : if ( !xColumnModel.is() )
152 0 : xColumnModel = lcl_getDefaultColumnModel_throw( m_xContext );
153 4 : UnoControlModel::setFastPropertyValue_NoBroadcast( BASEPROPERTY_GRID_COLUMNMODEL, makeAny( xColumnModel ) );
154 : // same comment as above: do not use our own setPropertyValue here.
155 : }
156 2 : osl_atomic_decrement( &m_refCount );
157 2 : }
158 :
159 :
160 2 : UnoControlModel* UnoGridModel::Clone() const
161 : {
162 2 : return new UnoGridModel( *this );
163 : }
164 :
165 :
166 : namespace
167 : {
168 16 : void lcl_dispose_nothrow( const Any& i_component )
169 : {
170 : try
171 : {
172 16 : const Reference< XComponent > xComponent( i_component, UNO_QUERY_THROW );
173 16 : xComponent->dispose();
174 : }
175 0 : catch( const Exception& )
176 : {
177 : DBG_UNHANDLED_EXCEPTION();
178 : }
179 16 : }
180 : }
181 :
182 :
183 6 : void SAL_CALL UnoGridModel::dispose( ) throw(RuntimeException, std::exception)
184 : {
185 6 : lcl_dispose_nothrow( getFastPropertyValue( BASEPROPERTY_GRID_COLUMNMODEL ) );
186 6 : lcl_dispose_nothrow( getFastPropertyValue( BASEPROPERTY_GRID_DATAMODEL ) );
187 :
188 6 : UnoControlModel::dispose();
189 6 : }
190 :
191 :
192 4 : void SAL_CALL UnoGridModel::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw (Exception, std::exception)
193 : {
194 4 : Any aOldSubModel;
195 4 : if ( ( nHandle == BASEPROPERTY_GRID_COLUMNMODEL ) || ( nHandle == BASEPROPERTY_GRID_DATAMODEL ) )
196 : {
197 4 : aOldSubModel = getFastPropertyValue( nHandle );
198 4 : if ( aOldSubModel == rValue )
199 : {
200 : OSL_ENSURE( false, "UnoGridModel::setFastPropertyValue_NoBroadcast: setting the same value, again!" );
201 : // shouldn't this have been caught by convertFastPropertyValue?
202 0 : aOldSubModel.clear();
203 : }
204 : }
205 :
206 4 : UnoControlModel::setFastPropertyValue_NoBroadcast( nHandle, rValue );
207 :
208 4 : if ( aOldSubModel.hasValue() )
209 4 : lcl_dispose_nothrow( aOldSubModel );
210 4 : }
211 :
212 :
213 0 : OUString UnoGridModel::getServiceName() throw(RuntimeException, std::exception)
214 : {
215 0 : return OUString("com.sun.star.awt.grid.UnoControlGridModel");
216 : }
217 :
218 :
219 608 : Any UnoGridModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
220 : {
221 608 : switch( nPropId )
222 : {
223 : case BASEPROPERTY_DEFAULTCONTROL:
224 16 : return uno::makeAny( OUString("com.sun.star.awt.grid.UnoControlGrid") );
225 : case BASEPROPERTY_GRID_SELECTIONMODE:
226 16 : return uno::makeAny( SelectionType(1) );
227 : case BASEPROPERTY_GRID_SHOWROWHEADER:
228 : case BASEPROPERTY_USE_GRID_LINES:
229 32 : return uno::makeAny( false );
230 : case BASEPROPERTY_ROW_HEADER_WIDTH:
231 16 : return uno::makeAny( sal_Int32( 10 ) );
232 : case BASEPROPERTY_GRID_SHOWCOLUMNHEADER:
233 16 : return uno::makeAny( true );
234 : case BASEPROPERTY_COLUMN_HEADER_HEIGHT:
235 : case BASEPROPERTY_ROW_HEIGHT:
236 : case BASEPROPERTY_GRID_HEADER_BACKGROUND:
237 : case BASEPROPERTY_GRID_HEADER_TEXT_COLOR:
238 : case BASEPROPERTY_GRID_LINE_COLOR:
239 : case BASEPROPERTY_GRID_ROW_BACKGROUND_COLORS:
240 : case BASEPROPERTY_ACTIVE_SEL_BACKGROUND_COLOR:
241 : case BASEPROPERTY_INACTIVE_SEL_BACKGROUND_COLOR:
242 : case BASEPROPERTY_ACTIVE_SEL_TEXT_COLOR:
243 : case BASEPROPERTY_INACTIVE_SEL_TEXT_COLOR:
244 160 : return Any();
245 : default:
246 352 : return UnoControlModel::ImplGetDefaultValue( nPropId );
247 : }
248 :
249 : }
250 :
251 :
252 234 : ::cppu::IPropertyArrayHelper& UnoGridModel::getInfoHelper()
253 : {
254 : static UnoPropertyArrayHelper* pHelper = NULL;
255 234 : if ( !pHelper )
256 : {
257 4 : Sequence<sal_Int32> aIDs = ImplGetPropertyIds();
258 4 : pHelper = new UnoPropertyArrayHelper( aIDs );
259 : }
260 234 : return *pHelper;
261 : }
262 :
263 :
264 : // XMultiPropertySet
265 2 : Reference< XPropertySetInfo > UnoGridModel::getPropertySetInfo( ) throw(RuntimeException, std::exception)
266 : {
267 2 : static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
268 2 : return xInfo;
269 : }
270 :
271 :
272 :
273 : //= UnoGridControl
274 :
275 2 : UnoGridControl::UnoGridControl()
276 : :UnoGridControl_Base()
277 : ,m_aSelectionListeners( *this )
278 2 : ,m_pEventForwarder( new toolkit::GridEventForwarder( *this ) )
279 : {
280 2 : }
281 :
282 :
283 4 : UnoGridControl::~UnoGridControl()
284 : {
285 4 : }
286 :
287 :
288 2 : OUString UnoGridControl::GetComponentServiceName()
289 : {
290 2 : return OUString("Grid");
291 : }
292 :
293 :
294 2 : void SAL_CALL UnoGridControl::dispose( ) throw(RuntimeException, std::exception)
295 : {
296 2 : lang::EventObject aEvt;
297 2 : aEvt.Source = (::cppu::OWeakObject*)this;
298 2 : m_aSelectionListeners.disposeAndClear( aEvt );
299 2 : UnoControl::dispose();
300 2 : }
301 :
302 :
303 2 : void SAL_CALL UnoGridControl::createPeer( const uno::Reference< awt::XToolkit > & rxToolkit, const uno::Reference< awt::XWindowPeer > & rParentPeer ) throw(uno::RuntimeException, std::exception)
304 : {
305 2 : UnoControlBase::createPeer( rxToolkit, rParentPeer );
306 :
307 2 : const Reference< XGridRowSelection > xGrid( getPeer(), UNO_QUERY_THROW );
308 2 : xGrid->addSelectionListener( &m_aSelectionListeners );
309 2 : }
310 :
311 :
312 : namespace
313 : {
314 6 : void lcl_setEventForwarding( const Reference< XControlModel >& i_gridControlModel, const ::boost::scoped_ptr< toolkit::GridEventForwarder >& i_listener,
315 : bool const i_add )
316 : {
317 6 : const Reference< XPropertySet > xModelProps( i_gridControlModel, UNO_QUERY );
318 6 : if ( !xModelProps.is() )
319 8 : return;
320 :
321 : try
322 : {
323 : Reference< XContainer > const xColModel(
324 4 : xModelProps->getPropertyValue("ColumnModel"),
325 4 : UNO_QUERY_THROW );
326 4 : if ( i_add )
327 2 : xColModel->addContainerListener( i_listener.get() );
328 : else
329 2 : xColModel->removeContainerListener( i_listener.get() );
330 :
331 : Reference< XGridDataModel > const xDataModel(
332 4 : xModelProps->getPropertyValue("GridDataModel"),
333 : UNO_QUERY_THROW
334 8 : );
335 8 : Reference< XMutableGridDataModel > const xMutableDataModel( xDataModel, UNO_QUERY );
336 4 : if ( xMutableDataModel.is() )
337 : {
338 4 : if ( i_add )
339 2 : xMutableDataModel->addGridDataListener( i_listener.get() );
340 : else
341 2 : xMutableDataModel->removeGridDataListener( i_listener.get() );
342 4 : }
343 : }
344 0 : catch( const Exception& )
345 : {
346 : DBG_UNHANDLED_EXCEPTION();
347 4 : }
348 : }
349 : }
350 :
351 :
352 4 : sal_Bool SAL_CALL UnoGridControl::setModel( const Reference< XControlModel >& i_model ) throw(RuntimeException, std::exception)
353 : {
354 4 : lcl_setEventForwarding( getModel(), m_pEventForwarder, false );
355 4 : if ( !UnoGridControl_Base::setModel( i_model ) )
356 2 : return sal_False;
357 2 : lcl_setEventForwarding( getModel(), m_pEventForwarder, true );
358 2 : return sal_True;
359 : }
360 :
361 :
362 0 : ::sal_Int32 UnoGridControl::getRowAtPoint(::sal_Int32 x, ::sal_Int32 y) throw (::com::sun::star::uno::RuntimeException, std::exception)
363 : {
364 0 : Reference< XGridControl > const xGrid ( getPeer(), UNO_QUERY_THROW );
365 0 : return xGrid->getRowAtPoint( x, y );
366 : }
367 :
368 :
369 0 : ::sal_Int32 UnoGridControl::getColumnAtPoint(::sal_Int32 x, ::sal_Int32 y) throw (::com::sun::star::uno::RuntimeException, std::exception)
370 : {
371 0 : Reference< XGridControl > const xGrid ( getPeer(), UNO_QUERY_THROW );
372 0 : return xGrid->getColumnAtPoint( x, y );
373 : }
374 :
375 :
376 6 : ::sal_Int32 SAL_CALL UnoGridControl::getCurrentColumn( ) throw (RuntimeException, std::exception)
377 : {
378 6 : Reference< XGridControl > const xGrid ( getPeer(), UNO_QUERY_THROW );
379 6 : return xGrid->getCurrentColumn();
380 : }
381 :
382 :
383 6 : ::sal_Int32 SAL_CALL UnoGridControl::getCurrentRow( ) throw (RuntimeException, std::exception)
384 : {
385 6 : Reference< XGridControl > const xGrid ( getPeer(), UNO_QUERY_THROW );
386 6 : return xGrid->getCurrentRow();
387 : }
388 :
389 :
390 4 : void SAL_CALL UnoGridControl::goToCell( ::sal_Int32 i_columnIndex, ::sal_Int32 i_rowIndex ) throw (RuntimeException, IndexOutOfBoundsException, VetoException, std::exception)
391 : {
392 4 : Reference< XGridControl > const xGrid ( getPeer(), UNO_QUERY_THROW );
393 4 : xGrid->goToCell( i_columnIndex, i_rowIndex );
394 4 : }
395 :
396 :
397 0 : void SAL_CALL UnoGridControl::selectRow( ::sal_Int32 i_rowIndex ) throw (RuntimeException, IndexOutOfBoundsException, std::exception )
398 : {
399 0 : Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->selectRow( i_rowIndex );
400 0 : }
401 :
402 :
403 0 : void SAL_CALL UnoGridControl::selectAllRows() throw (::com::sun::star::uno::RuntimeException, std::exception)
404 : {
405 0 : Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->selectAllRows();
406 0 : }
407 :
408 :
409 0 : void SAL_CALL UnoGridControl::deselectRow( ::sal_Int32 i_rowIndex ) throw (RuntimeException, IndexOutOfBoundsException, std::exception )
410 : {
411 0 : Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->deselectRow( i_rowIndex );
412 0 : }
413 :
414 :
415 0 : void SAL_CALL UnoGridControl::deselectAllRows() throw (::com::sun::star::uno::RuntimeException, std::exception)
416 : {
417 0 : Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->deselectAllRows();
418 0 : }
419 :
420 :
421 0 : ::com::sun::star::uno::Sequence< ::sal_Int32 > SAL_CALL UnoGridControl::getSelectedRows() throw (::com::sun::star::uno::RuntimeException, std::exception)
422 : {
423 0 : return Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->getSelectedRows();
424 : }
425 :
426 :
427 0 : sal_Bool SAL_CALL UnoGridControl::hasSelectedRows() throw (::com::sun::star::uno::RuntimeException, std::exception)
428 : {
429 0 : return Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->hasSelectedRows();
430 : }
431 :
432 :
433 0 : sal_Bool SAL_CALL UnoGridControl::isRowSelected(::sal_Int32 index) throw (::com::sun::star::uno::RuntimeException, std::exception)
434 : {
435 0 : return Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->isRowSelected( index );
436 : }
437 :
438 :
439 0 : void SAL_CALL UnoGridControl::addSelectionListener(const ::com::sun::star::uno::Reference< ::com::sun::star::awt::grid::XGridSelectionListener > & listener) throw (::com::sun::star::uno::RuntimeException, std::exception)
440 : {
441 0 : m_aSelectionListeners.addInterface( listener );
442 0 : }
443 :
444 :
445 0 : void SAL_CALL UnoGridControl::removeSelectionListener(const ::com::sun::star::uno::Reference< ::com::sun::star::awt::grid::XGridSelectionListener > & listener) throw (::com::sun::star::uno::RuntimeException, std::exception)
446 : {
447 0 : m_aSelectionListeners.removeInterface( listener );
448 0 : }
449 :
450 : }
451 :
452 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
453 2 : stardiv_Toolkit_GridControl_get_implementation(
454 : css::uno::XComponentContext *,
455 : css::uno::Sequence<css::uno::Any> const &)
456 : {
457 2 : return cppu::acquire(new toolkit::UnoGridControl());
458 : }
459 :
460 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
461 12 : stardiv_Toolkit_GridControlModel_get_implementation(
462 : css::uno::XComponentContext *context,
463 : css::uno::Sequence<css::uno::Any> const &)
464 : {
465 12 : return cppu::acquire(new toolkit::UnoGridModel(context));
466 : }
467 :
468 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|