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