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 9 : Reference< XGridDataModel > lcl_getDefaultDataModel_throw( const Reference<XComponentContext> & i_context )
59 : {
60 9 : Reference< XMutableGridDataModel > const xDelegatorModel( DefaultGridDataModel::create( i_context ), UNO_QUERY_THROW );
61 9 : Reference< XGridDataModel > const xDataModel( SortableGridDataModel::create( i_context, xDelegatorModel ), UNO_QUERY_THROW );
62 9 : return xDataModel;
63 : }
64 :
65 9 : Reference< XGridColumnModel > lcl_getDefaultColumnModel_throw( const Reference<XComponentContext> & i_context )
66 : {
67 9 : Reference< XGridColumnModel > const xColumnModel = DefaultGridColumnModel::create( i_context );
68 9 : return xColumnModel;
69 : }
70 : }
71 :
72 :
73 9 : UnoGridModel::UnoGridModel( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext )
74 9 : :UnoControlModel( rxContext )
75 : {
76 9 : ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
77 9 : ImplRegisterProperty( BASEPROPERTY_BORDER );
78 9 : ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
79 9 : ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
80 9 : ImplRegisterProperty( BASEPROPERTY_ENABLED );
81 9 : ImplRegisterProperty( BASEPROPERTY_FILLCOLOR );
82 9 : ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
83 9 : ImplRegisterProperty( BASEPROPERTY_HELPURL );
84 9 : ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
85 9 : ImplRegisterProperty( BASEPROPERTY_SIZEABLE ); // resizable
86 9 : ImplRegisterProperty( BASEPROPERTY_HSCROLL );
87 9 : ImplRegisterProperty( BASEPROPERTY_VSCROLL );
88 9 : ImplRegisterProperty( BASEPROPERTY_TABSTOP );
89 9 : ImplRegisterProperty( BASEPROPERTY_GRID_SHOWROWHEADER );
90 9 : ImplRegisterProperty( BASEPROPERTY_ROW_HEADER_WIDTH );
91 9 : ImplRegisterProperty( BASEPROPERTY_GRID_SHOWCOLUMNHEADER );
92 9 : ImplRegisterProperty( BASEPROPERTY_COLUMN_HEADER_HEIGHT );
93 9 : ImplRegisterProperty( BASEPROPERTY_ROW_HEIGHT );
94 9 : ImplRegisterProperty( BASEPROPERTY_GRID_DATAMODEL, makeAny( lcl_getDefaultDataModel_throw( m_xContext ) ) );
95 9 : ImplRegisterProperty( BASEPROPERTY_GRID_COLUMNMODEL, makeAny( lcl_getDefaultColumnModel_throw( m_xContext ) ) );
96 9 : ImplRegisterProperty( BASEPROPERTY_GRID_SELECTIONMODE );
97 9 : ImplRegisterProperty( BASEPROPERTY_FONTRELIEF );
98 9 : ImplRegisterProperty( BASEPROPERTY_FONTEMPHASISMARK );
99 9 : ImplRegisterProperty( BASEPROPERTY_FONTDESCRIPTOR );
100 9 : ImplRegisterProperty( BASEPROPERTY_TEXTCOLOR );
101 9 : ImplRegisterProperty( BASEPROPERTY_TEXTLINECOLOR );
102 9 : ImplRegisterProperty( BASEPROPERTY_USE_GRID_LINES );
103 9 : ImplRegisterProperty( BASEPROPERTY_GRID_LINE_COLOR );
104 9 : ImplRegisterProperty( BASEPROPERTY_GRID_HEADER_BACKGROUND );
105 9 : ImplRegisterProperty( BASEPROPERTY_GRID_HEADER_TEXT_COLOR );
106 9 : ImplRegisterProperty( BASEPROPERTY_GRID_ROW_BACKGROUND_COLORS );
107 9 : ImplRegisterProperty( BASEPROPERTY_ACTIVE_SEL_BACKGROUND_COLOR );
108 9 : ImplRegisterProperty( BASEPROPERTY_INACTIVE_SEL_BACKGROUND_COLOR );
109 9 : ImplRegisterProperty( BASEPROPERTY_ACTIVE_SEL_TEXT_COLOR );
110 9 : ImplRegisterProperty( BASEPROPERTY_INACTIVE_SEL_TEXT_COLOR );
111 9 : ImplRegisterProperty( BASEPROPERTY_VERTICALALIGN );
112 9 : }
113 :
114 :
115 1 : UnoGridModel::UnoGridModel( const UnoGridModel& rModel )
116 1 : :UnoControlModel( rModel )
117 : {
118 1 : osl_atomic_increment( &m_refCount );
119 : {
120 1 : Reference< XGridDataModel > xDataModel;
121 : // clone the data model
122 2 : const Reference< XFastPropertySet > xCloneSource( &const_cast< UnoGridModel& >( rModel ) );
123 : try
124 : {
125 1 : const Reference< XCloneable > xCloneable( xCloneSource->getFastPropertyValue( BASEPROPERTY_GRID_DATAMODEL ), UNO_QUERY_THROW );
126 1 : xDataModel.set( xCloneable->createClone(), UNO_QUERY_THROW );
127 : }
128 0 : catch( const Exception& )
129 : {
130 : DBG_UNHANDLED_EXCEPTION();
131 : }
132 1 : if ( !xDataModel.is() )
133 0 : xDataModel = lcl_getDefaultDataModel_throw( m_xContext );
134 1 : 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 2 : Reference< XGridColumnModel > xColumnModel;
142 : try
143 : {
144 1 : const Reference< XCloneable > xCloneable( xCloneSource->getFastPropertyValue( BASEPROPERTY_GRID_COLUMNMODEL ), UNO_QUERY_THROW );
145 1 : xColumnModel.set( xCloneable->createClone(), UNO_QUERY_THROW );
146 : }
147 0 : catch( const Exception& )
148 : {
149 : DBG_UNHANDLED_EXCEPTION();
150 : }
151 1 : if ( !xColumnModel.is() )
152 0 : xColumnModel = lcl_getDefaultColumnModel_throw( m_xContext );
153 2 : UnoControlModel::setFastPropertyValue_NoBroadcast( BASEPROPERTY_GRID_COLUMNMODEL, makeAny( xColumnModel ) );
154 : // same comment as above: do not use our own setPropertyValue here.
155 : }
156 1 : osl_atomic_decrement( &m_refCount );
157 1 : }
158 :
159 :
160 1 : UnoControlModel* UnoGridModel::Clone() const
161 : {
162 1 : return new UnoGridModel( *this );
163 : }
164 :
165 :
166 : namespace
167 : {
168 10 : void lcl_dispose_nothrow( const Any& i_component )
169 : {
170 : try
171 : {
172 10 : const Reference< XComponent > xComponent( i_component, UNO_QUERY_THROW );
173 10 : xComponent->dispose();
174 : }
175 0 : catch( const Exception& )
176 : {
177 : DBG_UNHANDLED_EXCEPTION();
178 : }
179 10 : }
180 : }
181 :
182 :
183 4 : void SAL_CALL UnoGridModel::dispose( ) throw(RuntimeException, std::exception)
184 : {
185 4 : lcl_dispose_nothrow( getFastPropertyValue( BASEPROPERTY_GRID_COLUMNMODEL ) );
186 4 : lcl_dispose_nothrow( getFastPropertyValue( BASEPROPERTY_GRID_DATAMODEL ) );
187 :
188 4 : UnoControlModel::dispose();
189 4 : }
190 :
191 :
192 2 : void SAL_CALL UnoGridModel::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw (Exception, std::exception)
193 : {
194 2 : Any aOldSubModel;
195 2 : if ( ( nHandle == BASEPROPERTY_GRID_COLUMNMODEL ) || ( nHandle == BASEPROPERTY_GRID_DATAMODEL ) )
196 : {
197 2 : aOldSubModel = getFastPropertyValue( nHandle );
198 2 : 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 2 : UnoControlModel::setFastPropertyValue_NoBroadcast( nHandle, rValue );
207 :
208 2 : if ( aOldSubModel.hasValue() )
209 2 : lcl_dispose_nothrow( aOldSubModel );
210 2 : }
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 342 : Any UnoGridModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
220 : {
221 342 : switch( nPropId )
222 : {
223 : case BASEPROPERTY_DEFAULTCONTROL:
224 9 : return uno::makeAny( OUString("com.sun.star.awt.grid.UnoControlGrid") );
225 : case BASEPROPERTY_GRID_SELECTIONMODE:
226 9 : return uno::makeAny( SelectionType(1) );
227 : case BASEPROPERTY_GRID_SHOWROWHEADER:
228 : case BASEPROPERTY_USE_GRID_LINES:
229 18 : return uno::makeAny( false );
230 : case BASEPROPERTY_ROW_HEADER_WIDTH:
231 9 : return uno::makeAny( sal_Int32( 10 ) );
232 : case BASEPROPERTY_GRID_SHOWCOLUMNHEADER:
233 9 : 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 90 : return Any();
245 : default:
246 198 : return UnoControlModel::ImplGetDefaultValue( nPropId );
247 : }
248 :
249 : }
250 :
251 :
252 119 : ::cppu::IPropertyArrayHelper& UnoGridModel::getInfoHelper()
253 : {
254 : static UnoPropertyArrayHelper* pHelper = NULL;
255 119 : if ( !pHelper )
256 : {
257 3 : Sequence<sal_Int32> aIDs = ImplGetPropertyIds();
258 3 : pHelper = new UnoPropertyArrayHelper( aIDs );
259 : }
260 119 : return *pHelper;
261 : }
262 :
263 :
264 : // XMultiPropertySet
265 1 : Reference< XPropertySetInfo > UnoGridModel::getPropertySetInfo( ) throw(RuntimeException, std::exception)
266 : {
267 1 : static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
268 1 : 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 1 : OUString UnoGridControl::GetComponentServiceName()
289 : {
290 1 : 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 = static_cast<cppu::OWeakObject*>(this);
298 2 : m_aSelectionListeners.disposeAndClear( aEvt );
299 2 : UnoControl::dispose();
300 2 : }
301 :
302 :
303 1 : void SAL_CALL UnoGridControl::createPeer( const uno::Reference< awt::XToolkit > & rxToolkit, const uno::Reference< awt::XWindowPeer > & rParentPeer ) throw(uno::RuntimeException, std::exception)
304 : {
305 1 : UnoControlBase::createPeer( rxToolkit, rParentPeer );
306 :
307 1 : const Reference< XGridRowSelection > xGrid( getPeer(), UNO_QUERY_THROW );
308 1 : xGrid->addSelectionListener( &m_aSelectionListeners );
309 1 : }
310 :
311 :
312 : namespace
313 : {
314 4 : void lcl_setEventForwarding( const Reference< XControlModel >& i_gridControlModel, const ::boost::scoped_ptr< toolkit::GridEventForwarder >& i_listener,
315 : bool const i_add )
316 : {
317 4 : const Reference< XPropertySet > xModelProps( i_gridControlModel, UNO_QUERY );
318 4 : if ( !xModelProps.is() )
319 6 : return;
320 :
321 : try
322 : {
323 : Reference< XContainer > const xColModel(
324 2 : xModelProps->getPropertyValue("ColumnModel"),
325 2 : UNO_QUERY_THROW );
326 2 : if ( i_add )
327 1 : xColModel->addContainerListener( i_listener.get() );
328 : else
329 1 : xColModel->removeContainerListener( i_listener.get() );
330 :
331 : Reference< XGridDataModel > const xDataModel(
332 2 : xModelProps->getPropertyValue("GridDataModel"),
333 : UNO_QUERY_THROW
334 4 : );
335 4 : Reference< XMutableGridDataModel > const xMutableDataModel( xDataModel, UNO_QUERY );
336 2 : if ( xMutableDataModel.is() )
337 : {
338 2 : if ( i_add )
339 1 : xMutableDataModel->addGridDataListener( i_listener.get() );
340 : else
341 1 : xMutableDataModel->removeGridDataListener( i_listener.get() );
342 2 : }
343 : }
344 0 : catch( const Exception& )
345 : {
346 : DBG_UNHANDLED_EXCEPTION();
347 2 : }
348 : }
349 : }
350 :
351 :
352 3 : sal_Bool SAL_CALL UnoGridControl::setModel( const Reference< XControlModel >& i_model ) throw(RuntimeException, std::exception)
353 : {
354 3 : lcl_setEventForwarding( getModel(), m_pEventForwarder, false );
355 3 : if ( !UnoGridControl_Base::setModel( i_model ) )
356 2 : return sal_False;
357 1 : lcl_setEventForwarding( getModel(), m_pEventForwarder, true );
358 1 : 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 3 : ::sal_Int32 SAL_CALL UnoGridControl::getCurrentColumn( ) throw (RuntimeException, std::exception)
377 : {
378 3 : Reference< XGridControl > const xGrid ( getPeer(), UNO_QUERY_THROW );
379 3 : return xGrid->getCurrentColumn();
380 : }
381 :
382 :
383 3 : ::sal_Int32 SAL_CALL UnoGridControl::getCurrentRow( ) throw (RuntimeException, std::exception)
384 : {
385 3 : Reference< XGridControl > const xGrid ( getPeer(), UNO_QUERY_THROW );
386 3 : return xGrid->getCurrentRow();
387 : }
388 :
389 :
390 2 : void SAL_CALL UnoGridControl::goToCell( ::sal_Int32 i_columnIndex, ::sal_Int32 i_rowIndex ) throw (RuntimeException, IndexOutOfBoundsException, VetoException, std::exception)
391 : {
392 2 : Reference< XGridControl > const xGrid ( getPeer(), UNO_QUERY_THROW );
393 2 : xGrid->goToCell( i_columnIndex, i_rowIndex );
394 2 : }
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 7 : stardiv_Toolkit_GridControlModel_get_implementation(
462 : css::uno::XComponentContext *context,
463 : css::uno::Sequence<css::uno::Any> const &)
464 : {
465 7 : return cppu::acquire(new toolkit::UnoGridModel(context));
466 : }
467 :
468 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|