LibreOffice Module toolkit (master) 1
gridcontrol.cxx
Go to the documentation of this file.
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"
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 <helper/property.hxx>
38
39#include <memory>
40
42
43using namespace ::com::sun::star;
44using namespace ::com::sun::star::uno;
45using namespace ::com::sun::star::awt;
46using namespace ::com::sun::star::awt::grid;
47using namespace ::com::sun::star::lang;
48using namespace ::com::sun::star::beans;
49using namespace ::com::sun::star::container;
50using namespace ::com::sun::star::view;
51using namespace ::com::sun::star::util;
52
53namespace toolkit {
54
55namespace
56{
57 Reference< XGridDataModel > lcl_getDefaultDataModel_throw( const Reference<XComponentContext> & i_context )
58 {
59 Reference< XMutableGridDataModel > const xDelegatorModel( DefaultGridDataModel::create( i_context ), UNO_SET_THROW );
60 Reference< XGridDataModel > const xDataModel( SortableGridDataModel::create( i_context, xDelegatorModel ), UNO_QUERY_THROW );
61 return xDataModel;
62 }
63
64 Reference< XGridColumnModel > lcl_getDefaultColumnModel_throw( const Reference<XComponentContext> & i_context )
65 {
66 Reference< XGridColumnModel > const xColumnModel = DefaultGridColumnModel::create( i_context );
67 return xColumnModel;
68 }
69}
70
71
72UnoGridModel::UnoGridModel( const css::uno::Reference< css::uno::XComponentContext >& rxContext )
73 :UnoControlModel( rxContext )
74{
93 ImplRegisterProperty( BASEPROPERTY_GRID_DATAMODEL, Any( lcl_getDefaultDataModel_throw( m_xContext ) ) );
94 ImplRegisterProperty( BASEPROPERTY_GRID_COLUMNMODEL, Any( lcl_getDefaultColumnModel_throw( m_xContext ) ) );
111}
112
113
115 :UnoControlModel( rModel )
116{
117 osl_atomic_increment( &m_refCount );
118 {
119 Reference< XGridDataModel > xDataModel;
120 // clone the data model
121 const Reference< XFastPropertySet > xCloneSource( &const_cast< UnoGridModel& >( rModel ) );
122 try
123 {
124 const Reference< XCloneable > xCloneable( xCloneSource->getFastPropertyValue( BASEPROPERTY_GRID_DATAMODEL ), UNO_QUERY_THROW );
125 xDataModel.set( xCloneable->createClone(), UNO_QUERY_THROW );
126 }
127 catch( const Exception& )
128 {
129 DBG_UNHANDLED_EXCEPTION("toolkit.controls");
130 }
131 if ( !xDataModel.is() )
132 xDataModel = lcl_getDefaultDataModel_throw( m_xContext );
133 std::unique_lock aGuard(m_aMutex);
135 // do *not* use setFastPropertyValue here: The UnoControlModel ctor made 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 Reference< XGridColumnModel > xColumnModel;
142 try
143 {
144 const Reference< XCloneable > xCloneable( xCloneSource->getFastPropertyValue( BASEPROPERTY_GRID_COLUMNMODEL ), UNO_QUERY_THROW );
145 xColumnModel.set( xCloneable->createClone(), UNO_QUERY_THROW );
146 }
147 catch( const Exception& )
148 {
149 DBG_UNHANDLED_EXCEPTION("toolkit.controls");
150 }
151 if ( !xColumnModel.is() )
152 xColumnModel = lcl_getDefaultColumnModel_throw( m_xContext );
154 // same comment as above: do not use our own setPropertyValue here.
155 }
156 osl_atomic_decrement( &m_refCount );
157}
158
159
161{
162 return new UnoGridModel( *this );
163}
164
165
166namespace
167{
168 void lcl_dispose_nothrow( const Any& i_component )
169 {
170 try
171 {
172 const Reference< XComponent > xComponent( i_component, UNO_QUERY_THROW );
173 xComponent->dispose();
174 }
175 catch( const Exception& )
176 {
177 DBG_UNHANDLED_EXCEPTION("toolkit.controls");
178 }
179 }
180}
181
182
183void SAL_CALL UnoGridModel::dispose( )
184{
185 lcl_dispose_nothrow( getFastPropertyValue( BASEPROPERTY_GRID_COLUMNMODEL ) );
186 lcl_dispose_nothrow( getFastPropertyValue( BASEPROPERTY_GRID_DATAMODEL ) );
187
189}
190
191
192void UnoGridModel::setFastPropertyValue_NoBroadcast( std::unique_lock<std::mutex>& rGuard, sal_Int32 nHandle, const Any& rValue )
193{
194 Any aOldSubModel;
196 {
197 getFastPropertyValue( rGuard, aOldSubModel, nHandle );
198 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 aOldSubModel.clear();
203 }
204 }
205
207
208 if ( aOldSubModel.hasValue() )
209 lcl_dispose_nothrow( aOldSubModel );
210}
211
212
214{
215 return "com.sun.star.awt.grid.UnoControlGridModel";
216}
217
218
219Any UnoGridModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
220{
221 switch( nPropId )
222 {
224 return uno::Any( OUString("com.sun.star.awt.grid.UnoControlGrid") );
226 return uno::Any( SelectionType(1) );
229 return uno::Any( false );
231 return uno::Any( sal_Int32( 10 ) );
233 return uno::Any( true );
244 return Any();
245 default:
246 return UnoControlModel::ImplGetDefaultValue( nPropId );
247 }
248
249}
250
251
253{
255 return aHelper;
256}
257
258
259// XMultiPropertySet
260Reference< XPropertySetInfo > UnoGridModel::getPropertySetInfo( )
261{
262 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
263 return xInfo;
264}
265
266
267//= UnoGridControl
268
270 :m_aSelectionListeners( *this )
271 ,m_pEventForwarder( new toolkit::GridEventForwarder( *this ) )
272{
273}
274
275
277{
278}
279
280
282{
283 return "Grid";
284}
285
286
288{
289 lang::EventObject aEvt;
290 aEvt.Source = static_cast<cppu::OWeakObject*>(this);
291 m_aSelectionListeners.disposeAndClear( aEvt );
293}
294
295
296void SAL_CALL UnoGridControl::createPeer( const uno::Reference< awt::XToolkit > & rxToolkit, const uno::Reference< awt::XWindowPeer > & rParentPeer )
297{
298 UnoControlBase::createPeer( rxToolkit, rParentPeer );
299
300 const Reference< XGridRowSelection > xGrid( getPeer(), UNO_QUERY_THROW );
301 xGrid->addSelectionListener( &m_aSelectionListeners );
302}
303
304
305namespace
306{
307 void lcl_setEventForwarding( const Reference< XControlModel >& i_gridControlModel, const std::unique_ptr< toolkit::GridEventForwarder >& i_listener,
308 bool const i_add )
309 {
310 const Reference< XPropertySet > xModelProps( i_gridControlModel, UNO_QUERY );
311 if ( !xModelProps.is() )
312 return;
313
314 try
315 {
316 Reference< XContainer > const xColModel(
317 xModelProps->getPropertyValue("ColumnModel"),
318 UNO_QUERY_THROW );
319 if ( i_add )
320 xColModel->addContainerListener( i_listener.get() );
321 else
322 xColModel->removeContainerListener( i_listener.get() );
323
324 Reference< XGridDataModel > const xDataModel(
325 xModelProps->getPropertyValue("GridDataModel"),
326 UNO_QUERY_THROW
327 );
328 Reference< XMutableGridDataModel > const xMutableDataModel( xDataModel, UNO_QUERY );
329 if ( xMutableDataModel.is() )
330 {
331 if ( i_add )
332 xMutableDataModel->addGridDataListener( i_listener.get() );
333 else
334 xMutableDataModel->removeGridDataListener( i_listener.get() );
335 }
336 }
337 catch( const Exception& )
338 {
339 DBG_UNHANDLED_EXCEPTION("toolkit.controls");
340 }
341 }
342}
343
344
345sal_Bool SAL_CALL UnoGridControl::setModel( const Reference< XControlModel >& i_model )
346{
347 lcl_setEventForwarding( getModel(), m_pEventForwarder, false );
348 if ( !UnoGridControl_Base::setModel( i_model ) )
349 return false;
350 lcl_setEventForwarding( getModel(), m_pEventForwarder, true );
351 return true;
352}
353
354
355::sal_Int32 UnoGridControl::getRowAtPoint(::sal_Int32 x, ::sal_Int32 y)
356{
357 Reference< XGridControl > const xGrid ( getPeer(), UNO_QUERY_THROW );
358 return xGrid->getRowAtPoint( x, y );
359}
360
361
362::sal_Int32 UnoGridControl::getColumnAtPoint(::sal_Int32 x, ::sal_Int32 y)
363{
364 Reference< XGridControl > const xGrid ( getPeer(), UNO_QUERY_THROW );
365 return xGrid->getColumnAtPoint( x, y );
366}
367
368
369::sal_Int32 SAL_CALL UnoGridControl::getCurrentColumn( )
370{
371 Reference< XGridControl > const xGrid ( getPeer(), UNO_QUERY_THROW );
372 return xGrid->getCurrentColumn();
373}
374
375
376::sal_Int32 SAL_CALL UnoGridControl::getCurrentRow( )
377{
378 Reference< XGridControl > const xGrid ( getPeer(), UNO_QUERY_THROW );
379 return xGrid->getCurrentRow();
380}
381
382
383void SAL_CALL UnoGridControl::goToCell( ::sal_Int32 i_columnIndex, ::sal_Int32 i_rowIndex )
384{
385 Reference< XGridControl > const xGrid ( getPeer(), UNO_QUERY_THROW );
386 xGrid->goToCell( i_columnIndex, i_rowIndex );
387}
388
389
390void SAL_CALL UnoGridControl::selectRow( ::sal_Int32 i_rowIndex )
391{
392 Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->selectRow( i_rowIndex );
393}
394
395
397{
398 Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->selectAllRows();
399}
400
401
402void SAL_CALL UnoGridControl::deselectRow( ::sal_Int32 i_rowIndex )
403{
404 Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->deselectRow( i_rowIndex );
405}
406
407
409{
410 Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->deselectAllRows();
411}
412
413
414css::uno::Sequence< ::sal_Int32 > SAL_CALL UnoGridControl::getSelectedRows()
415{
416 return Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->getSelectedRows();
417}
418
419
421{
422 return Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->hasSelectedRows();
423}
424
425
426sal_Bool SAL_CALL UnoGridControl::isRowSelected(::sal_Int32 index)
427{
428 return Reference< XGridRowSelection >( getPeer(), UNO_QUERY_THROW )->isRowSelected( index );
429}
430
431
432void SAL_CALL UnoGridControl::addSelectionListener(const css::uno::Reference< css::awt::grid::XGridSelectionListener > & listener)
433{
434 m_aSelectionListeners.addInterface( listener );
435}
436
437
438void SAL_CALL UnoGridControl::removeSelectionListener(const css::uno::Reference< css::awt::grid::XGridSelectionListener > & listener)
439{
440 m_aSelectionListeners.removeInterface( listener );
441}
442
443}
444
445extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
447 css::uno::XComponentContext *,
448 css::uno::Sequence<css::uno::Any> const &)
449{
450 return cppu::acquire(new toolkit::UnoGridControl());
451}
452
453extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
455 css::uno::XComponentContext *context,
456 css::uno::Sequence<css::uno::Any> const &)
457{
458 return cppu::acquire(new toolkit::UnoGridModel(context));
459}
460
461/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< sal_Int32 > ImplGetPropertyIds() const
void ImplRegisterProperty(sal_uInt16 nPropType)
void SAL_CALL dispose() override
void setFastPropertyValue_NoBroadcast(std::unique_lock< std::mutex > &rGuard, sal_Int32 nHandle, const css::uno::Any &rValue) override
virtual css::uno::Any ImplGetDefaultValue(sal_uInt16 nPropId) const
void getFastPropertyValue(std::unique_lock< std::mutex > &rGuard, css::uno::Any &rValue, sal_Int32 nHandle) const override
css::uno::Reference< css::uno::XComponentContext > m_xContext
void SAL_CALL dispose() override
Definition: unocontrol.cxx:346
void SAL_CALL createPeer(const css::uno::Reference< css::awt::XToolkit > &Toolkit, const css::uno::Reference< css::awt::XWindowPeer > &Parent) override
static css::uno::Reference< css::beans::XPropertySetInfo > createPropertySetInfo(cppu::IPropertyArrayHelper &rProperties)
oslInterlockedCount m_refCount
virtual sal_Bool SAL_CALL hasSelectedRows() override
virtual ::sal_Int32 SAL_CALL getCurrentRow() override
virtual void SAL_CALL deselectAllRows() override
virtual sal_Bool SAL_CALL isRowSelected(::sal_Int32 index) override
std::unique_ptr< GridEventForwarder > m_pEventForwarder
OUString GetComponentServiceName() const override
virtual void SAL_CALL goToCell(::sal_Int32 i_columnIndex, ::sal_Int32 i_rowIndex) override
virtual void SAL_CALL selectRow(::sal_Int32 i_rowIndex) override
virtual ::sal_Int32 SAL_CALL getColumnAtPoint(::sal_Int32 x, ::sal_Int32 y) override
virtual ~UnoGridControl() override
virtual void SAL_CALL selectAllRows() override
css::uno::Reference< css::awt::XWindowPeer > SAL_CALL getPeer() override
virtual ::sal_Int32 SAL_CALL getCurrentColumn() override
virtual css::uno::Sequence< ::sal_Int32 > SAL_CALL getSelectedRows() override
virtual void SAL_CALL addSelectionListener(const css::uno::Reference< css::awt::grid::XGridSelectionListener > &listener) override
virtual ::sal_Int32 SAL_CALL getRowAtPoint(::sal_Int32 x, ::sal_Int32 y) override
void SAL_CALL createPeer(const css::uno::Reference< css::awt::XToolkit > &Toolkit, const css::uno::Reference< css::awt::XWindowPeer > &Parent) override
virtual void SAL_CALL deselectRow(::sal_Int32 i_rowIndex) override
SelectionListenerMultiplexer m_aSelectionListeners
void SAL_CALL dispose() override
virtual void SAL_CALL removeSelectionListener(const css::uno::Reference< css::awt::grid::XGridSelectionListener > &listener) override
sal_Bool SAL_CALL setModel(const css::uno::Reference< css::awt::XControlModel > &rxModel) override
::cppu::IPropertyArrayHelper & getInfoHelper() override
css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
rtl::Reference< UnoControlModel > Clone() const override
css::uno::Any ImplGetDefaultValue(sal_uInt16 nPropId) const override
void SAL_CALL dispose() override
void setFastPropertyValue_NoBroadcast(std::unique_lock< std::mutex > &rGuard, sal_Int32 nHandle, const css::uno::Any &rValue) override
OUString SAL_CALL getServiceName() override
UnoGridModel(const css::uno::Reference< css::uno::XComponentContext > &i_factory)
Definition: gridcontrol.cxx:72
#define DBG_UNHANDLED_EXCEPTION(...)
float y
float x
Any aHelper
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * stardiv_Toolkit_GridControl_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * stardiv_Toolkit_GridControlModel_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
@ Exception
index
#define BASEPROPERTY_USE_GRID_LINES
Definition: property.hxx:196
#define BASEPROPERTY_GRID_COLUMNMODEL
Definition: property.hxx:178
#define BASEPROPERTY_TEXTLINECOLOR
Definition: property.hxx:118
#define BASEPROPERTY_GRID_SHOWROWHEADER
Definition: property.hxx:175
#define BASEPROPERTY_GRID_DATAMODEL
Definition: property.hxx:177
#define BASEPROPERTY_BORDER
Definition: property.hxx:39
#define BASEPROPERTY_HSCROLL
Definition: property.hxx:45
#define BASEPROPERTY_FILLCOLOR
Definition: property.hxx:36
#define BASEPROPERTY_ACTIVE_SEL_TEXT_COLOR
Definition: property.hxx:203
#define BASEPROPERTY_GRID_LINE_COLOR
Definition: property.hxx:187
#define BASEPROPERTY_GRID_HEADER_TEXT_COLOR
Definition: property.hxx:185
#define BASEPROPERTY_HELPURL
Definition: property.hxx:91
#define BASEPROPERTY_ROW_HEADER_WIDTH
Definition: property.hxx:194
#define BASEPROPERTY_GRID_SHOWCOLUMNHEADER
Definition: property.hxx:176
#define BASEPROPERTY_BACKGROUNDCOLOR
Definition: property.hxx:35
#define BASEPROPERTY_TABSTOP
Definition: property.hxx:47
#define BASEPROPERTY_FONTRELIEF
Definition: property.hxx:116
#define BASEPROPERTY_ENABLED
Definition: property.hxx:77
#define BASEPROPERTY_GRID_HEADER_BACKGROUND
Definition: property.hxx:184
#define BASEPROPERTY_DEFAULTCONTROL
Definition: property.hxx:52
#define BASEPROPERTY_VERTICALALIGN
Definition: property.hxx:148
#define BASEPROPERTY_GRID_ROW_BACKGROUND_COLORS
Definition: property.hxx:186
#define BASEPROPERTY_BORDERCOLOR
Definition: property.hxx:145
#define BASEPROPERTY_FONTDESCRIPTOR
Definition: property.hxx:41
#define BASEPROPERTY_VSCROLL
Definition: property.hxx:46
#define BASEPROPERTY_ACTIVE_SEL_BACKGROUND_COLOR
Definition: property.hxx:201
#define BASEPROPERTY_HELPTEXT
Definition: property.hxx:106
#define BASEPROPERTY_COLUMN_HEADER_HEIGHT
Definition: property.hxx:195
#define BASEPROPERTY_ROW_HEIGHT
Definition: property.hxx:164
#define BASEPROPERTY_SIZEABLE
Definition: property.hxx:105
#define BASEPROPERTY_INACTIVE_SEL_BACKGROUND_COLOR
Definition: property.hxx:202
#define BASEPROPERTY_FONTEMPHASISMARK
Definition: property.hxx:117
#define BASEPROPERTY_GRID_SELECTIONMODE
Definition: property.hxx:179
#define BASEPROPERTY_INACTIVE_SEL_TEXT_COLOR
Definition: property.hxx:204
#define BASEPROPERTY_TEXTCOLOR
Definition: property.hxx:37
#define BASEPROPERTY_PRINTABLE
Definition: property.hxx:78
sal_Int32 nHandle
unsigned char sal_Bool
SelectionType