LibreOffice Module toolkit (master) 1
roadmapcontrol.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
23#include <helper/property.hxx>
24#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
25#include <com/sun/star/uno/XComponentContext.hpp>
26
28
29namespace toolkit
30{
31
32
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::awt;
35 using namespace ::com::sun::star::lang;
36 using namespace ::com::sun::star::beans;
37 using namespace ::com::sun::star::container;
38
39
40// helper
41
42
44{ // throwing is expensive (in terms of code size), thus we hope the compiler does not inline this...
45 throw IllegalArgumentException();
46}
47
49{ // throwing is expensive (in terms of code size), thus we hope the compiler does not inline this...
50 throw IndexOutOfBoundsException();
51}
52
53
54 // = UnoControlRoadmapModel
55
56
57 UnoControlRoadmapModel::UnoControlRoadmapModel( const Reference< XComponentContext >& i_factory )
58 :UnoControlRoadmapModel_Base( i_factory )
59 ,maContainerListeners( *this )
60 {
61 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
62 ImplRegisterProperty( BASEPROPERTY_BORDER );
63 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
64 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
65 ImplRegisterProperty( BASEPROPERTY_FONTDESCRIPTOR );
66 ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
67 ImplRegisterProperty( BASEPROPERTY_HELPURL );
68 ImplRegisterProperty( BASEPROPERTY_IMAGEURL );
69 ImplRegisterProperty( BASEPROPERTY_GRAPHIC );
70 ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
71 ImplRegisterProperty( BASEPROPERTY_COMPLETE );
72 ImplRegisterProperty( BASEPROPERTY_ACTIVATED );
73 ImplRegisterProperty( BASEPROPERTY_CURRENTITEMID );
74 ImplRegisterProperty( BASEPROPERTY_TABSTOP );
75 ImplRegisterProperty( BASEPROPERTY_TEXT );
76 }
77
78
79 OUString UnoControlRoadmapModel::getServiceName()
80 {
81 return "stardiv.vcl.controlmodel.Roadmap";
82 }
83
84 OUString UnoControlRoadmapModel::getImplementationName()
85 {
86 return "stardiv.Toolkit.UnoControlRoadmapModel";
87 }
88
89 css::uno::Sequence<OUString>
90 UnoControlRoadmapModel::getSupportedServiceNames()
91 {
92 auto s(UnoControlRoadmapModel_Base::getSupportedServiceNames());
93 s.realloc(s.getLength() + 2);
94 auto ps = s.getArray();
95 ps[s.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmapModel";
96 ps[s.getLength() - 1] = "stardiv.vcl.controlmodel.Roadmap";
97 return s;
98 }
99
100 Any UnoControlRoadmapModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
101 {
102 Any aReturn;
103 switch (nPropId)
104 {
106 aReturn <<= true;
107 break;
109 aReturn <<= true;
110 break;
112 aReturn <<= sal_Int16(-1);
113 break;
115 break;
117 aReturn <<= sal_Int16(2); // No Border
118 break;
120 aReturn <<= OUString( "stardiv.vcl.control.Roadmap" );
121 break;
122 default : aReturn = UnoControlRoadmapModel_Base::ImplGetDefaultValue( nPropId ); break;
123 }
124
125 return aReturn;
126 }
127
128
129 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstance( )
130 {
131 rtl::Reference<ORoadmapEntry> pRoadmapItem = new ORoadmapEntry();
132 Reference< XInterface > xNewRoadmapItem = static_cast<cppu::OWeakObject*>(pRoadmapItem.get());
133 return xNewRoadmapItem;
134 }
135
136
137 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstanceWithArguments( const Sequence< Any >& /*aArguments*/ )
138 {
139 // Todo: implementation of the arguments handling
140 rtl::Reference<ORoadmapEntry> pRoadmapItem = new ORoadmapEntry();
141 Reference< XInterface > xNewRoadmapItem = static_cast<cppu::OWeakObject*>(pRoadmapItem.get());
142 return xNewRoadmapItem;
143 }
144
145
147
148
149 css::uno::Any SAL_CALL UnoControlRoadmapModel::queryAggregation( const css::uno::Type & rType )
150 {
151 Any aRet = UnoControlRoadmapModel_Base::queryAggregation( rType );
152 if ( !aRet.hasValue() )
153 aRet = UnoControlRoadmapModel_IBase::queryInterface( rType );
154 return aRet;
155 }
156
157
158 ::cppu::IPropertyArrayHelper& UnoControlRoadmapModel::getInfoHelper()
159 {
160 static UnoPropertyArrayHelper aHelper( ImplGetPropertyIds() );
161 return aHelper;
162 }
163
164
165 // beans::XMultiPropertySet
166
167 Reference< XPropertySetInfo > UnoControlRoadmapModel::getPropertySetInfo( )
168 {
169 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
170 return xInfo;
171 }
172
173
174 sal_Int32 SAL_CALL UnoControlRoadmapModel::getCount()
175 {
176 return maRoadmapItems.size();
177 }
178
179 Any SAL_CALL UnoControlRoadmapModel::getByIndex( sal_Int32 Index )
180 {
181 if ((Index < 0) || ( o3tl::make_unsigned(Index) >= maRoadmapItems.size()))
183 Any aAny( maRoadmapItems.at( Index ) );
184 return aAny;
185 }
186
187
188 void UnoControlRoadmapModel::MakeRMItemValidation( sal_Int32 Index, const Reference< XInterface >& xRoadmapItem )
189 {
190 if (( Index < 0 ) || (o3tl::make_unsigned(Index) > maRoadmapItems.size()) )
192 if ( !xRoadmapItem.is() )
194 Reference< XServiceInfo > xServiceInfo( xRoadmapItem, UNO_QUERY );
195 bool bIsRoadmapItem = xServiceInfo->supportsService("com.sun.star.awt.RoadmapItem");
196 if ( !bIsRoadmapItem )
198 }
199
200
201 void UnoControlRoadmapModel::SetRMItemDefaultProperties( const Reference< XInterface >& xRoadmapItem)
202 {
203 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
204 Reference< XPropertySet > xProps( xRoadmapItem, UNO_QUERY );
205 if ( xProps.is() )
206 {
207 sal_Int32 LocID = 0;
208 Any aValue = xPropertySet->getPropertyValue("ID");
209 aValue >>= LocID;
210 if (LocID < 0) // index may not be smaller than zero
211 {
212 xPropertySet->setPropertyValue("ID", Any(GetUniqueID()) );
213 }
214 }
215 }
216
217
218// The performance of this method could certainly be improved.
219// As long as only vectors with up to 10 elements are
220// involved it should be sufficient
221 sal_Int32 UnoControlRoadmapModel::GetUniqueID()
222 {
223 Any aAny;
224 bool bIncrement = true;
225 sal_Int32 CurID = 0;
226 sal_Int32 n_CurItemID = 0;
227 Reference< XInterface > CurRoadmapItem;
228 while ( bIncrement )
229 {
230 bIncrement = false;
231 for ( const auto& rRoadmapItem : maRoadmapItems )
232 {
233 CurRoadmapItem = rRoadmapItem;
234 Reference< XPropertySet > xPropertySet( CurRoadmapItem, UNO_QUERY );
235 aAny = xPropertySet->getPropertyValue("ID");
236 aAny >>= n_CurItemID;
237 if (n_CurItemID == CurID)
238 {
239 bIncrement = true;
240 CurID++;
241 break;
242 }
243 }
244 }
245 return CurID;
246 }
247
248
249 ContainerEvent UnoControlRoadmapModel::GetContainerEvent(sal_Int32 Index, const Reference< XInterface >& xRoadmapItem)
250 {
251 ContainerEvent aEvent;
252 aEvent.Source = *this;
253 aEvent.Element <<= xRoadmapItem;
254 aEvent.Accessor <<= Index;
255 return aEvent;
256 }
257
258
259 sal_Int16 UnoControlRoadmapModel::GetCurrentItemID( const Reference< XPropertySet >& xPropertySet )
260 {
261 Any aAny = xPropertySet->getPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ) );
262 sal_Int16 n_CurrentItemID = 0;
263 aAny >>= n_CurrentItemID;
264 return n_CurrentItemID;
265 }
266
267
268 void SAL_CALL UnoControlRoadmapModel::insertByIndex( const sal_Int32 Index, const Any& Element)
269 {
270 if ( ( Index >= ( static_cast<sal_Int32>(maRoadmapItems.size()) + 1 ) ) || (Index < 0))
272 Reference< XInterface > xRoadmapItem;
273 Element >>= xRoadmapItem;
274 MakeRMItemValidation( Index, xRoadmapItem);
275 SetRMItemDefaultProperties( xRoadmapItem );
276 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem);
277 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
278 maContainerListeners.elementInserted( aEvent );
279 Reference< XPropertySet > xPropertySet( this );
280 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
281 if ( Index <= n_CurrentItemID )
282 {
283 Any aAny(static_cast<sal_Int16>( n_CurrentItemID + 1 ) );
284 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
285 }
286 }
287
288
289 void SAL_CALL UnoControlRoadmapModel::removeByIndex( sal_Int32 Index)
290 {
291 if ((Index < 0) || ( o3tl::make_unsigned(Index) > maRoadmapItems.size()))
293 Reference< XInterface > xRoadmapItem;
294 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
295 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
296 maContainerListeners.elementRemoved( aEvent );
297 Reference< XPropertySet > xPropertySet( this );
298 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
299 Any aAny;
300 if ( Index > n_CurrentItemID )
301 return;
302
303 if ( n_CurrentItemID >= static_cast<sal_Int32>(maRoadmapItems.size()) )
304 {
305 n_CurrentItemID = sal::static_int_cast< sal_Int16 >(
306 maRoadmapItems.size()-1);
307 if ( n_CurrentItemID < 0 )
308 return;
309 aAny <<= n_CurrentItemID;
310 }
311 else if (Index == n_CurrentItemID)
312 aAny <<= sal_Int16(-1);
313 else if( Index < n_CurrentItemID)
314 aAny <<= static_cast<sal_Int16>( n_CurrentItemID - 1 );
315 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
316 }
317
318
319 void SAL_CALL UnoControlRoadmapModel::replaceByIndex( const sal_Int32 Index, const Any& Element)
320 {
321 Reference< XInterface > xRoadmapItem;
322 Element >>= xRoadmapItem;
323 MakeRMItemValidation( Index, xRoadmapItem);
324 SetRMItemDefaultProperties( xRoadmapItem );
325 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
326 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem); //push_back( xRoadmapItem );
327 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
328 maContainerListeners.elementReplaced( aEvent );
329 }
330
331
332 Type SAL_CALL UnoControlRoadmapModel::getElementType()
333 {
335 return aType;
336 }
337
338
339 sal_Bool SAL_CALL UnoControlRoadmapModel::hasElements()
340 {
341 return !maRoadmapItems.empty();
342 }
343
344
345 void SAL_CALL UnoControlRoadmapModel::addContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener )
346 {
347 maContainerListeners.addInterface( xListener );
348 }
349
350 void SAL_CALL UnoControlRoadmapModel::removeContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener )
351 {
352 maContainerListeners.removeInterface( xListener );
353 }
354
355
356 // = UnoRoadmapControl
357
358
359 UnoRoadmapControl::UnoRoadmapControl()
360 :maItemListeners( *this )
361 {
362 }
363
365
366css::uno::Any UnoRoadmapControl::queryAggregation(css::uno::Type const & aType) {
368 if (!ret.hasValue()) {
370 }
371 return ret;
372}
373
374
375sal_Bool SAL_CALL UnoRoadmapControl::setModel(const Reference< XControlModel >& _rModel)
376 {
377 Reference< XContainer > xC( getModel(), UNO_QUERY );
378 if ( xC.is() )
379 xC->removeContainerListener( this );
380
381 bool bReturn = UnoControlBase::setModel( _rModel );
382
383 xC.set(getModel(), css::uno::UNO_QUERY);
384 if ( xC.is() )
385 xC->addContainerListener( this );
386
387 return bReturn;
388 }
389
390
392 {
393 return "Roadmap";
394 }
395
396
398 {
399 EventObject aEvt;
400 aEvt.Source = static_cast<cppu::OWeakObject*>(this);
401 maItemListeners.disposeAndClear( aEvt );
403 }
404
405
406void UnoRoadmapControl::elementInserted( const ContainerEvent& rEvent )
407{
408 Reference< XInterface > xRoadmapItem;
409 rEvent.Element >>= xRoadmapItem;
410 Reference< XPropertySet > xRoadmapPropertySet( xRoadmapItem, UNO_QUERY );
411 if ( xRoadmapPropertySet.is() )
412 xRoadmapPropertySet->addPropertyChangeListener( OUString(), this );
413
414 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
415 if ( xPeer.is() )
416 {
417 xPeer->elementInserted( rEvent );
418 Reference < XPropertySet > xPropertySet( xPeer, UNO_QUERY );
419 if ( xPropertySet.is() )
420 xPropertySet->addPropertyChangeListener( OUString(), this );
421 }
422}
423
424
425void UnoRoadmapControl::elementRemoved( const ContainerEvent& rEvent )
426{
427 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
428 if ( xPeer.is() )
429 xPeer->elementRemoved( rEvent );
430 Reference< XInterface > xRoadmapItem;
431 rEvent.Element >>= xRoadmapItem;
432 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
433 if ( xPropertySet.is() )
434 xPropertySet->removePropertyChangeListener( OUString(), this );
435}
436
437
438void UnoRoadmapControl::elementReplaced( const ContainerEvent& rEvent )
439{
440 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
441 if ( xPeer.is() )
442 xPeer->elementReplaced( rEvent );
443}
444
445
446void SAL_CALL UnoRoadmapControl::itemStateChanged( const ItemEvent& rEvent )
447{
448 sal_Int16 CurItemIndex = sal::static_int_cast< sal_Int16 >(rEvent.ItemId);
449 Reference< XControlModel > xModel = getModel( );
450 Reference< XPropertySet > xPropertySet( xModel, UNO_QUERY );
451 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), Any(CurItemIndex) );
452 if ( maItemListeners.getLength() )
453 maItemListeners.itemStateChanged( rEvent );
454}
455
456
457void SAL_CALL UnoRoadmapControl::addItemListener( const Reference< XItemListener >& l )
458{
459 maItemListeners.addInterface( l );
460 if( getPeer().is() && maItemListeners.getLength() == 1 )
461 {
462 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
463 xRoadmap->addItemListener( this );
464 }
465}
466
467
468void SAL_CALL UnoRoadmapControl::removeItemListener( const Reference< XItemListener >& l )
469{
470 if( getPeer().is() && maItemListeners.getLength() == 1 )
471 {
472 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
473 xRoadmap->removeItemListener( this );
474 }
475
476 maItemListeners.removeInterface( l );
477}
478
479
480void SAL_CALL UnoRoadmapControl::propertyChange( const PropertyChangeEvent& evt )
481{
482 Reference< XPropertyChangeListener > xPeer(getPeer(), UNO_QUERY);
483 if ( xPeer.is() )
484 xPeer->propertyChange( evt );
485}
486
488{
489 return "stardiv.Toolkit.UnoRoadmapControl";
490}
491
493{
495 s.realloc(s.getLength() + 2);
496 auto ps = s.getArray();
497 ps[s.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmap";
498 ps[s.getLength() - 1] = "stardiv.vcl.control.Roadmap";
499 return s;
500}
501
502}
503
504extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
506 css::uno::XComponentContext *context,
507 css::uno::Sequence<css::uno::Any> const &)
508{
509 return cppu::acquire(new toolkit::UnoControlRoadmapModel(context));
510}
511
512extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
514 css::uno::XComponentContext *,
515 css::uno::Sequence<css::uno::Any> const &)
516{
517 return cppu::acquire(new toolkit::UnoRoadmapControl());
518}
519
520/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
AnyEventRef aEvent
css::uno::Reference< css::awt::XControlModel > SAL_CALL getModel() override
void SAL_CALL dispose() override
Definition: unocontrol.cxx:346
sal_Bool SAL_CALL setModel(const css::uno::Reference< css::awt::XControlModel > &Model) override
css::uno::Reference< css::awt::XWindowPeer > SAL_CALL getPeer() override
css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const &rType) SAL_OVERRIDE
css::uno::Type const & get()
virtual css::uno::Any SAL_CALL queryAggregation(css::uno::Type const &rType) SAL_OVERRIDE
UnoControlRoadmapModel(const css::uno::Reference< css::uno::XComponentContext > &i_factory)
virtual void SAL_CALL addItemListener(const css::uno::Reference< css::awt::XItemListener > &l) override
void SAL_CALL dispose() override
virtual void SAL_CALL removeItemListener(const css::uno::Reference< css::awt::XItemListener > &l) override
OUString GetComponentServiceName() const override
void SAL_CALL elementReplaced(const css::container::ContainerEvent &rEvent) override
OUString SAL_CALL getImplementationName() override
sal_Bool SAL_CALL setModel(const css::uno::Reference< css::awt::XControlModel > &Model) override
virtual void SAL_CALL propertyChange(const css::beans::PropertyChangeEvent &evt) override
void SAL_CALL elementInserted(const css::container::ContainerEvent &rEvent) override
void SAL_CALL elementRemoved(const css::container::ContainerEvent &rEvent) override
virtual void SAL_CALL itemStateChanged(const css::awt::ItemEvent &rEvent) override
ItemListenerMultiplexer maItemListeners
css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
static void lcl_throwIllegalArgumentException()
Any aHelper
Type
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
GraphicControlModel UnoControlRoadmapModel_Base
static void lcl_throwIndexOutOfBoundsException()
static void lcl_throwIllegalArgumentException()
const OUString & GetPropertyName(sal_uInt16 nPropertyId)
Definition: property.cxx:295
#define BASEPROPERTY_COMPLETE
Definition: property.hxx:139
#define BASEPROPERTY_BORDER
Definition: property.hxx:39
#define BASEPROPERTY_CURRENTITEMID
Definition: property.hxx:140
#define BASEPROPERTY_TEXT
Definition: property.hxx:34
#define BASEPROPERTY_HELPURL
Definition: property.hxx:91
#define BASEPROPERTY_ACTIVATED
Definition: property.hxx:138
#define BASEPROPERTY_IMAGEURL
Definition: property.hxx:75
#define BASEPROPERTY_BACKGROUNDCOLOR
Definition: property.hxx:35
#define BASEPROPERTY_TABSTOP
Definition: property.hxx:47
#define BASEPROPERTY_DEFAULTCONTROL
Definition: property.hxx:52
#define BASEPROPERTY_GRAPHIC
Definition: property.hxx:150
#define BASEPROPERTY_BORDERCOLOR
Definition: property.hxx:145
#define BASEPROPERTY_FONTDESCRIPTOR
Definition: property.hxx:41
#define BASEPROPERTY_HELPTEXT
Definition: property.hxx:106
#define BASEPROPERTY_PRINTABLE
Definition: property.hxx:78
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * stardiv_Toolkit_UnoControlRoadmapModel_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * stardiv_Toolkit_UnoRoadmapControl_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
IMPLEMENT_FORWARD_XTYPEPROVIDER2(ORoadmapEntry, ORoadmapEntry_Base, ::comphelper::OPropertyContainer)
Reference< XModel > xModel
unsigned char sal_Bool