1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "gridcontrol.hxx"
22 #include "grideventforwarder.hxx"
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/view/SelectionType.hpp>
26 #include <com/sun/star/awt/grid/XGridDataModel.hpp>
27 #include <com/sun/star/awt/grid/XMutableGridDataModel.hpp>
28 #include <com/sun/star/awt/grid/DefaultGridDataModel.hpp>
29 #include <com/sun/star/awt/grid/SortableGridDataModel.hpp>
30 #include <com/sun/star/awt/grid/XGridColumnModel.hpp>
31 #include <toolkit/helper/unopropertyarrayhelper.hxx>
32 #include <toolkit/helper/property.hxx>
33 #include <com/sun/star/awt/XVclWindowPeer.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <tools/diagnose_ex.h>
36 #include <tools/color.hxx>
38 using namespace ::com::sun::star
;
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::awt
;
41 using namespace ::com::sun::star::awt::grid
;
42 using namespace ::com::sun::star::lang
;
43 using namespace ::com::sun::star::beans
;
44 using namespace ::com::sun::star::container
;
45 using namespace ::com::sun::star::view
;
46 using namespace ::com::sun::star::util
;
50 //======================================================================================================================
52 //======================================================================================================================
55 Reference
< XGridDataModel
> lcl_getDefaultDataModel_throw( ::comphelper::ComponentContext
const & i_context
)
57 Reference
< XMutableGridDataModel
> const xDelegatorModel( DefaultGridDataModel::create( i_context
.getUNOContext() ), UNO_QUERY_THROW
);
58 Reference
< XGridDataModel
> const xDataModel( SortableGridDataModel::create( i_context
.getUNOContext(), xDelegatorModel
), UNO_QUERY_THROW
);
62 Reference
< XGridColumnModel
> lcl_getDefaultColumnModel_throw( ::comphelper::ComponentContext
const & i_context
)
64 Reference
< XGridColumnModel
> const xColumnModel( i_context
.createComponent( "com.sun.star.awt.grid.DefaultGridColumnModel" ), UNO_QUERY_THROW
);
69 //----------------------------------------------------------------------------------------------------------------------
70 UnoGridModel::UnoGridModel( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
)
71 :UnoControlModel( rxContext
)
73 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR
);
74 ImplRegisterProperty( BASEPROPERTY_BORDER
);
75 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR
);
76 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL
);
77 ImplRegisterProperty( BASEPROPERTY_ENABLED
);
78 ImplRegisterProperty( BASEPROPERTY_FILLCOLOR
);
79 ImplRegisterProperty( BASEPROPERTY_HELPTEXT
);
80 ImplRegisterProperty( BASEPROPERTY_HELPURL
);
81 ImplRegisterProperty( BASEPROPERTY_PRINTABLE
);
82 ImplRegisterProperty( BASEPROPERTY_SIZEABLE
); // resizable
83 ImplRegisterProperty( BASEPROPERTY_HSCROLL
);
84 ImplRegisterProperty( BASEPROPERTY_VSCROLL
);
85 ImplRegisterProperty( BASEPROPERTY_TABSTOP
);
86 ImplRegisterProperty( BASEPROPERTY_GRID_SHOWROWHEADER
);
87 ImplRegisterProperty( BASEPROPERTY_ROW_HEADER_WIDTH
);
88 ImplRegisterProperty( BASEPROPERTY_GRID_SHOWCOLUMNHEADER
);
89 ImplRegisterProperty( BASEPROPERTY_COLUMN_HEADER_HEIGHT
);
90 ImplRegisterProperty( BASEPROPERTY_ROW_HEIGHT
);
91 ImplRegisterProperty( BASEPROPERTY_GRID_DATAMODEL
, makeAny( lcl_getDefaultDataModel_throw( m_xContext
) ) );
92 ImplRegisterProperty( BASEPROPERTY_GRID_COLUMNMODEL
, makeAny( lcl_getDefaultColumnModel_throw( m_xContext
) ) );
93 ImplRegisterProperty( BASEPROPERTY_GRID_SELECTIONMODE
);
94 ImplRegisterProperty( BASEPROPERTY_FONTRELIEF
);
95 ImplRegisterProperty( BASEPROPERTY_FONTEMPHASISMARK
);
96 ImplRegisterProperty( BASEPROPERTY_FONTDESCRIPTOR
);
97 ImplRegisterProperty( BASEPROPERTY_TEXTCOLOR
);
98 ImplRegisterProperty( BASEPROPERTY_TEXTLINECOLOR
);
99 ImplRegisterProperty( BASEPROPERTY_USE_GRID_LINES
);
100 ImplRegisterProperty( BASEPROPERTY_GRID_LINE_COLOR
);
101 ImplRegisterProperty( BASEPROPERTY_GRID_HEADER_BACKGROUND
);
102 ImplRegisterProperty( BASEPROPERTY_GRID_HEADER_TEXT_COLOR
);
103 ImplRegisterProperty( BASEPROPERTY_GRID_ROW_BACKGROUND_COLORS
);
104 ImplRegisterProperty( BASEPROPERTY_ACTIVE_SEL_BACKGROUND_COLOR
);
105 ImplRegisterProperty( BASEPROPERTY_INACTIVE_SEL_BACKGROUND_COLOR
);
106 ImplRegisterProperty( BASEPROPERTY_ACTIVE_SEL_TEXT_COLOR
);
107 ImplRegisterProperty( BASEPROPERTY_INACTIVE_SEL_TEXT_COLOR
);
108 ImplRegisterProperty( BASEPROPERTY_VERTICALALIGN
);
111 //----------------------------------------------------------------------------------------------------------------------
112 UnoGridModel::UnoGridModel( const UnoGridModel
& rModel
)
113 :UnoControlModel( rModel
)
115 osl_atomic_increment( &m_refCount
);
117 Reference
< XGridDataModel
> xDataModel
;
118 // clone the data model
119 const Reference
< XFastPropertySet
> xCloneSource( &const_cast< UnoGridModel
& >( rModel
) );
122 const Reference
< XCloneable
> xCloneable( xCloneSource
->getFastPropertyValue( BASEPROPERTY_GRID_DATAMODEL
), UNO_QUERY_THROW
);
123 xDataModel
.set( xCloneable
->createClone(), UNO_QUERY_THROW
);
125 catch( const Exception
& )
127 DBG_UNHANDLED_EXCEPTION();
129 if ( !xDataModel
.is() )
130 xDataModel
= lcl_getDefaultDataModel_throw( m_xContext
);
131 UnoControlModel::setFastPropertyValue_NoBroadcast( BASEPROPERTY_GRID_DATAMODEL
, makeAny( xDataModel
) );
132 // do *not* use setFastPropertyValue here: The UnoControlModel ctor did a simple copy of all property values,
133 // so before this call here, we share our data model with the own of the clone source. setFastPropertyValue,
134 // then, disposes the old data model - which means the data model which in fact belongs to the clone source.
135 // so, call the UnoControlModel's impl-method for setting the value.
137 // clone the column model
138 Reference
< XGridColumnModel
> xColumnModel
;
141 const Reference
< XCloneable
> xCloneable( xCloneSource
->getFastPropertyValue( BASEPROPERTY_GRID_COLUMNMODEL
), UNO_QUERY_THROW
);
142 xColumnModel
.set( xCloneable
->createClone(), UNO_QUERY_THROW
);
144 catch( const Exception
& )
146 DBG_UNHANDLED_EXCEPTION();
148 if ( !xColumnModel
.is() )
149 xColumnModel
= lcl_getDefaultColumnModel_throw( m_xContext
);
150 UnoControlModel::setFastPropertyValue_NoBroadcast( BASEPROPERTY_GRID_COLUMNMODEL
, makeAny( xColumnModel
) );
151 // same comment as above: do not use our own setPropertyValue here.
153 osl_atomic_decrement( &m_refCount
);
156 //----------------------------------------------------------------------------------------------------------------------
157 UnoControlModel
* UnoGridModel::Clone() const
159 return new UnoGridModel( *this );
162 //----------------------------------------------------------------------------------------------------------------------
165 void lcl_dispose_nothrow( const Any
& i_component
)
169 const Reference
< XComponent
> xComponent( i_component
, UNO_QUERY_THROW
);
170 xComponent
->dispose();
172 catch( const Exception
& )
174 DBG_UNHANDLED_EXCEPTION();
179 //----------------------------------------------------------------------------------------------------------------------
180 void SAL_CALL
UnoGridModel::dispose( ) throw(RuntimeException
)
182 lcl_dispose_nothrow( getFastPropertyValue( BASEPROPERTY_GRID_COLUMNMODEL
) );
183 lcl_dispose_nothrow( getFastPropertyValue( BASEPROPERTY_GRID_DATAMODEL
) );
185 UnoControlModel::dispose();
188 //----------------------------------------------------------------------------------------------------------------------
189 void SAL_CALL
UnoGridModel::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
, const Any
& rValue
) throw (Exception
)
192 if ( ( nHandle
== BASEPROPERTY_GRID_COLUMNMODEL
) || ( nHandle
== BASEPROPERTY_GRID_DATAMODEL
) )
194 aOldSubModel
= getFastPropertyValue( nHandle
);
195 if ( aOldSubModel
== rValue
)
197 OSL_ENSURE( false, "UnoGridModel::setFastPropertyValue_NoBroadcast: setting the same value, again!" );
198 // shouldn't this have been caught by convertFastPropertyValue?
199 aOldSubModel
.clear();
203 UnoControlModel::setFastPropertyValue_NoBroadcast( nHandle
, rValue
);
205 if ( aOldSubModel
.hasValue() )
206 lcl_dispose_nothrow( aOldSubModel
);
209 //----------------------------------------------------------------------------------------------------------------------
210 OUString
UnoGridModel::getServiceName() throw(RuntimeException
)
212 return OUString::createFromAscii( szServiceName_GridControlModel
);
215 //----------------------------------------------------------------------------------------------------------------------
216 Any
UnoGridModel::ImplGetDefaultValue( sal_uInt16 nPropId
) const
220 case BASEPROPERTY_DEFAULTCONTROL
:
221 return uno::makeAny( OUString::createFromAscii( szServiceName_GridControl
) );
222 case BASEPROPERTY_GRID_SELECTIONMODE
:
223 return uno::makeAny( SelectionType(1) );
224 case BASEPROPERTY_GRID_SHOWROWHEADER
:
225 case BASEPROPERTY_USE_GRID_LINES
:
226 return uno::makeAny( (sal_Bool
)sal_False
);
227 case BASEPROPERTY_ROW_HEADER_WIDTH
:
228 return uno::makeAny( sal_Int32( 10 ) );
229 case BASEPROPERTY_GRID_SHOWCOLUMNHEADER
:
230 return uno::makeAny( (sal_Bool
)sal_True
);
231 case BASEPROPERTY_COLUMN_HEADER_HEIGHT
:
232 case BASEPROPERTY_ROW_HEIGHT
:
233 case BASEPROPERTY_GRID_HEADER_BACKGROUND
:
234 case BASEPROPERTY_GRID_HEADER_TEXT_COLOR
:
235 case BASEPROPERTY_GRID_LINE_COLOR
:
236 case BASEPROPERTY_GRID_ROW_BACKGROUND_COLORS
:
237 case BASEPROPERTY_ACTIVE_SEL_BACKGROUND_COLOR
:
238 case BASEPROPERTY_INACTIVE_SEL_BACKGROUND_COLOR
:
239 case BASEPROPERTY_ACTIVE_SEL_TEXT_COLOR
:
240 case BASEPROPERTY_INACTIVE_SEL_TEXT_COLOR
:
243 return UnoControlModel::ImplGetDefaultValue( nPropId
);
248 //----------------------------------------------------------------------------------------------------------------------
249 ::cppu::IPropertyArrayHelper
& UnoGridModel::getInfoHelper()
251 static UnoPropertyArrayHelper
* pHelper
= NULL
;
254 Sequence
<sal_Int32
> aIDs
= ImplGetPropertyIds();
255 pHelper
= new UnoPropertyArrayHelper( aIDs
);
260 //----------------------------------------------------------------------------------------------------------------------
262 Reference
< XPropertySetInfo
> UnoGridModel::getPropertySetInfo( ) throw(RuntimeException
)
264 static Reference
< XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
269 //======================================================================================================================
271 //======================================================================================================================
272 UnoGridControl::UnoGridControl()
273 :UnoGridControl_Base()
274 ,m_aSelectionListeners( *this )
275 ,m_pEventForwarder( new GridEventForwarder( *this ) )
279 //----------------------------------------------------------------------------------------------------------------------
280 UnoGridControl::~UnoGridControl()
284 //----------------------------------------------------------------------------------------------------------------------
285 OUString
UnoGridControl::GetComponentServiceName()
287 return OUString("Grid");
290 //----------------------------------------------------------------------------------------------------------------------
291 void SAL_CALL
UnoGridControl::dispose( ) throw(RuntimeException
)
293 lang::EventObject aEvt
;
294 aEvt
.Source
= (::cppu::OWeakObject
*)this;
295 m_aSelectionListeners
.disposeAndClear( aEvt
);
296 UnoControl::dispose();
299 //----------------------------------------------------------------------------------------------------------------------
300 void SAL_CALL
UnoGridControl::createPeer( const uno::Reference
< awt::XToolkit
> & rxToolkit
, const uno::Reference
< awt::XWindowPeer
> & rParentPeer
) throw(uno::RuntimeException
)
302 UnoControlBase::createPeer( rxToolkit
, rParentPeer
);
304 const Reference
< XGridRowSelection
> xGrid( getPeer(), UNO_QUERY_THROW
);
305 xGrid
->addSelectionListener( &m_aSelectionListeners
);
308 //----------------------------------------------------------------------------------------------------------------------
311 void lcl_setEventForwarding( const Reference
< XControlModel
>& i_gridControlModel
, const ::boost::scoped_ptr
< GridEventForwarder
>& i_listener
,
314 const Reference
< XPropertySet
> xModelProps( i_gridControlModel
, UNO_QUERY
);
315 if ( !xModelProps
.is() )
320 Reference
< XContainer
> const xColModel(
321 xModelProps
->getPropertyValue( OUString( "ColumnModel" ) ),
324 xColModel
->addContainerListener( i_listener
.get() );
326 xColModel
->removeContainerListener( i_listener
.get() );
328 Reference
< XGridDataModel
> const xDataModel(
329 xModelProps
->getPropertyValue( OUString( "GridDataModel" ) ),
332 Reference
< XMutableGridDataModel
> const xMutableDataModel( xDataModel
, UNO_QUERY
);
333 if ( xMutableDataModel
.is() )
336 xMutableDataModel
->addGridDataListener( i_listener
.get() );
338 xMutableDataModel
->removeGridDataListener( i_listener
.get() );
341 catch( const Exception
& )
343 DBG_UNHANDLED_EXCEPTION();
348 //----------------------------------------------------------------------------------------------------------------------
349 sal_Bool SAL_CALL
UnoGridControl::setModel( const Reference
< XControlModel
>& i_model
) throw(RuntimeException
)
351 lcl_setEventForwarding( getModel(), m_pEventForwarder
, false );
352 if ( !UnoGridControl_Base::setModel( i_model
) )
354 lcl_setEventForwarding( getModel(), m_pEventForwarder
, true );
358 //----------------------------------------------------------------------------------------------------------------------
359 ::sal_Int32
UnoGridControl::getRowAtPoint(::sal_Int32 x
, ::sal_Int32 y
) throw (::com::sun::star::uno::RuntimeException
)
361 Reference
< XGridControl
> const xGrid ( getPeer(), UNO_QUERY_THROW
);
362 return xGrid
->getRowAtPoint( x
, y
);
365 //----------------------------------------------------------------------------------------------------------------------
366 ::sal_Int32
UnoGridControl::getColumnAtPoint(::sal_Int32 x
, ::sal_Int32 y
) throw (::com::sun::star::uno::RuntimeException
)
368 Reference
< XGridControl
> const xGrid ( getPeer(), UNO_QUERY_THROW
);
369 return xGrid
->getColumnAtPoint( x
, y
);
372 //----------------------------------------------------------------------------------------------------------------------
373 ::sal_Int32 SAL_CALL
UnoGridControl::getCurrentColumn( ) throw (RuntimeException
)
375 Reference
< XGridControl
> const xGrid ( getPeer(), UNO_QUERY_THROW
);
376 return xGrid
->getCurrentColumn();
379 //----------------------------------------------------------------------------------------------------------------------
380 ::sal_Int32 SAL_CALL
UnoGridControl::getCurrentRow( ) throw (RuntimeException
)
382 Reference
< XGridControl
> const xGrid ( getPeer(), UNO_QUERY_THROW
);
383 return xGrid
->getCurrentRow();
386 //----------------------------------------------------------------------------------------------------------------------
387 void SAL_CALL
UnoGridControl::goToCell( ::sal_Int32 i_columnIndex
, ::sal_Int32 i_rowIndex
) throw (RuntimeException
, IndexOutOfBoundsException
, VetoException
)
389 Reference
< XGridControl
> const xGrid ( getPeer(), UNO_QUERY_THROW
);
390 xGrid
->goToCell( i_columnIndex
, i_rowIndex
);
393 //----------------------------------------------------------------------------------------------------------------------
394 void SAL_CALL
UnoGridControl::selectRow( ::sal_Int32 i_rowIndex
) throw (RuntimeException
, IndexOutOfBoundsException
)
396 Reference
< XGridRowSelection
>( getPeer(), UNO_QUERY_THROW
)->selectRow( i_rowIndex
);
399 //----------------------------------------------------------------------------------------------------------------------
400 void SAL_CALL
UnoGridControl::selectAllRows() throw (::com::sun::star::uno::RuntimeException
)
402 Reference
< XGridRowSelection
>( getPeer(), UNO_QUERY_THROW
)->selectAllRows();
405 //----------------------------------------------------------------------------------------------------------------------
406 void SAL_CALL
UnoGridControl::deselectRow( ::sal_Int32 i_rowIndex
) throw (RuntimeException
, IndexOutOfBoundsException
)
408 Reference
< XGridRowSelection
>( getPeer(), UNO_QUERY_THROW
)->deselectRow( i_rowIndex
);
411 //----------------------------------------------------------------------------------------------------------------------
412 void SAL_CALL
UnoGridControl::deselectAllRows() throw (::com::sun::star::uno::RuntimeException
)
414 Reference
< XGridRowSelection
>( getPeer(), UNO_QUERY_THROW
)->deselectAllRows();
417 //----------------------------------------------------------------------------------------------------------------------
418 ::com::sun::star::uno::Sequence
< ::sal_Int32
> SAL_CALL
UnoGridControl::getSelectedRows() throw (::com::sun::star::uno::RuntimeException
)
420 return Reference
< XGridRowSelection
>( getPeer(), UNO_QUERY_THROW
)->getSelectedRows();
423 //----------------------------------------------------------------------------------------------------------------------
424 ::sal_Bool SAL_CALL
UnoGridControl::hasSelectedRows() throw (::com::sun::star::uno::RuntimeException
)
426 return Reference
< XGridRowSelection
>( getPeer(), UNO_QUERY_THROW
)->hasSelectedRows();
429 //----------------------------------------------------------------------------------------------------------------------
430 ::sal_Bool SAL_CALL
UnoGridControl::isRowSelected(::sal_Int32 index
) throw (::com::sun::star::uno::RuntimeException
)
432 return Reference
< XGridRowSelection
>( getPeer(), UNO_QUERY_THROW
)->isRowSelected( index
);
435 //----------------------------------------------------------------------------------------------------------------------
436 void SAL_CALL
UnoGridControl::addSelectionListener(const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::grid::XGridSelectionListener
> & listener
) throw (::com::sun::star::uno::RuntimeException
)
438 m_aSelectionListeners
.addInterface( listener
);
441 //----------------------------------------------------------------------------------------------------------------------
442 void SAL_CALL
UnoGridControl::removeSelectionListener(const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::grid::XGridSelectionListener
> & listener
) throw (::com::sun::star::uno::RuntimeException
)
444 m_aSelectionListeners
.removeInterface( listener
);
449 Reference
< XInterface
> SAL_CALL
GridControl_CreateInstance( const Reference
< XMultiServiceFactory
>& )
451 return Reference
< XInterface
>( ( ::cppu::OWeakObject
* ) new ::toolkit::UnoGridControl() );
454 Reference
< XInterface
> SAL_CALL
GridControlModel_CreateInstance( const Reference
< XMultiServiceFactory
>& i_factory
)
456 return Reference
< XInterface
>( ( ::cppu::OWeakObject
* ) new ::toolkit::UnoGridModel( comphelper::getComponentContext(i_factory
) ) );
459 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */