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 : #include <com/sun/star/io/XMarkableStream.hpp>
21 : #include <com/sun/star/uno/XComponentContext.hpp>
22 :
23 : #include <toolkit/controls/stdtabcontrollermodel.hxx>
24 : #include <toolkit/helper/macros.hxx>
25 : #include <toolkit/helper/servicenames.hxx>
26 : #include <toolkit/helper/property.hxx>
27 : #include <cppuhelper/supportsservice.hxx>
28 : #include <cppuhelper/typeprovider.hxx>
29 : #include <cppuhelper/queryinterface.hxx>
30 : #include <rtl/uuid.h>
31 :
32 : #include <tools/debug.hxx>
33 :
34 : #define UNOCONTROL_STREAMVERSION (short)2
35 :
36 :
37 : // class UnoControlModelEntryList
38 :
39 2 : UnoControlModelEntryList::UnoControlModelEntryList()
40 : {
41 2 : }
42 :
43 4 : UnoControlModelEntryList::~UnoControlModelEntryList()
44 : {
45 2 : Reset();
46 2 : }
47 :
48 2 : void UnoControlModelEntryList::Reset()
49 : {
50 4 : for ( size_t n = maList.size(); n; )
51 0 : DestroyEntry( --n );
52 2 : }
53 :
54 0 : void UnoControlModelEntryList::DestroyEntry( size_t nEntry )
55 : {
56 0 : UnoControlModelEntryListBase::iterator it = maList.begin();
57 0 : ::std::advance( it, nEntry );
58 :
59 0 : if ( (*it)->bGroup )
60 0 : delete (*it)->pGroup;
61 : else
62 0 : delete (*it)->pxControl;
63 :
64 0 : delete *it;
65 0 : maList.erase( it );
66 0 : }
67 :
68 0 : size_t UnoControlModelEntryList::size() const {
69 0 : return maList.size();
70 : }
71 :
72 0 : UnoControlModelEntry* UnoControlModelEntryList::operator[]( size_t i ) const {
73 0 : return ( i < maList.size() ) ? maList[ i ] : NULL;
74 : }
75 :
76 0 : void UnoControlModelEntryList::push_back( UnoControlModelEntry* item ) {
77 0 : maList.push_back( item );
78 0 : }
79 :
80 0 : void UnoControlModelEntryList::insert( size_t i, UnoControlModelEntry* item ) {
81 0 : if ( i < maList.size() ) {
82 0 : UnoControlModelEntryListBase::iterator it = maList.begin();
83 0 : ::std::advance( it, i );
84 0 : maList.insert( it, item );
85 : } else {
86 0 : maList.push_back( item );
87 : }
88 0 : }
89 :
90 :
91 :
92 : // class StdTabControllerModel
93 :
94 2 : StdTabControllerModel::StdTabControllerModel()
95 : {
96 2 : mbGroupControl = true;
97 2 : }
98 :
99 4 : StdTabControllerModel::~StdTabControllerModel()
100 : {
101 4 : }
102 :
103 0 : sal_uInt32 StdTabControllerModel::ImplGetControlCount( const UnoControlModelEntryList& rList ) const
104 : {
105 0 : sal_uInt32 nCount = 0;
106 0 : size_t nEntries = rList.size();
107 0 : for ( size_t n = 0; n < nEntries; n++ )
108 : {
109 0 : UnoControlModelEntry* pEntry = rList[ n ];
110 0 : if ( pEntry->bGroup )
111 0 : nCount += ImplGetControlCount( *pEntry->pGroup );
112 : else
113 0 : nCount++;
114 : }
115 0 : return nCount;
116 : }
117 :
118 0 : void StdTabControllerModel::ImplGetControlModels( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > ** ppRefs, const UnoControlModelEntryList& rList ) const
119 : {
120 0 : size_t nEntries = rList.size();
121 0 : for ( size_t n = 0; n < nEntries; n++ )
122 : {
123 0 : UnoControlModelEntry* pEntry = rList[ n ];
124 0 : if ( pEntry->bGroup )
125 0 : ImplGetControlModels( ppRefs, *pEntry->pGroup );
126 : else
127 : {
128 0 : **ppRefs = *pEntry->pxControl;
129 0 : (*ppRefs)++;
130 : }
131 : }
132 0 : }
133 :
134 0 : void StdTabControllerModel::ImplSetControlModels( UnoControlModelEntryList& rList, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > >& Controls )
135 : {
136 0 : const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > * pRefs = Controls.getConstArray();
137 0 : sal_uInt32 nControls = Controls.getLength();
138 0 : for ( sal_uInt32 n = 0; n < nControls; n++ )
139 : {
140 0 : UnoControlModelEntry* pNewEntry = new UnoControlModelEntry;
141 0 : pNewEntry->bGroup = false;
142 0 : pNewEntry->pxControl = new ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > ;
143 0 : *pNewEntry->pxControl = pRefs[n];
144 0 : rList.push_back( pNewEntry );
145 : }
146 0 : }
147 :
148 0 : sal_uInt32 StdTabControllerModel::ImplGetControlPos( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel >& rCtrl, const UnoControlModelEntryList& rList )
149 : {
150 0 : for ( size_t n = rList.size(); n; )
151 : {
152 0 : UnoControlModelEntry* pEntry = rList[ --n ];
153 0 : if ( !pEntry->bGroup && ( *pEntry->pxControl == rCtrl ) )
154 0 : return n;
155 : }
156 0 : return CONTROLPOS_NOTFOUND;
157 : }
158 :
159 0 : void ImplWriteControls( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XObjectOutputStream > & OutStream, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > >& rCtrls )
160 : {
161 0 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XMarkableStream > xMark( OutStream, ::com::sun::star::uno::UNO_QUERY );
162 : DBG_ASSERT( xMark.is(), "write: no XMarkableStream!" );
163 :
164 0 : sal_uInt32 nStoredControls = 0;
165 0 : sal_Int32 nDataBeginMark = xMark->createMark();
166 :
167 0 : OutStream->writeLong( 0L ); // DataLen
168 0 : OutStream->writeLong( 0L ); // nStoredControls
169 :
170 0 : sal_uInt32 nCtrls = rCtrls.getLength();
171 0 : for ( sal_uInt32 n = 0; n < nCtrls; n++ )
172 : {
173 0 : const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > xI = rCtrls.getConstArray()[n];
174 0 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XPersistObject > xPO( xI, ::com::sun::star::uno::UNO_QUERY );
175 : DBG_ASSERT( xPO.is(), "write: Control doesn't support XPersistObject" );
176 0 : if ( xPO.is() )
177 : {
178 0 : OutStream->writeObject( xPO );
179 0 : nStoredControls++;
180 : }
181 0 : }
182 0 : sal_Int32 nDataLen = xMark->offsetToMark( nDataBeginMark );
183 0 : xMark->jumpToMark( nDataBeginMark );
184 0 : OutStream->writeLong( nDataLen );
185 0 : OutStream->writeLong( nStoredControls );
186 0 : xMark->jumpToFurthest();
187 0 : xMark->deleteMark(nDataBeginMark);
188 0 : }
189 :
190 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > > ImplReadControls( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XObjectInputStream > & InStream )
191 : {
192 0 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XMarkableStream > xMark( InStream, ::com::sun::star::uno::UNO_QUERY );
193 : DBG_ASSERT( xMark.is(), "write: no XMarkableStream!" );
194 :
195 0 : sal_Int32 nDataBeginMark = xMark->createMark();
196 :
197 0 : sal_Int32 nDataLen = InStream->readLong();
198 0 : sal_uInt32 nCtrls = InStream->readLong();
199 :
200 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > > aSeq( nCtrls );
201 0 : for ( sal_uInt32 n = 0; n < nCtrls; n++ )
202 : {
203 0 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XPersistObject > xObj = InStream->readObject();
204 0 : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > xI( xObj, ::com::sun::star::uno::UNO_QUERY );
205 0 : aSeq.getArray()[n] = xI;
206 0 : }
207 :
208 : // Skip remainder if more data exists than this version recognizes
209 0 : xMark->jumpToMark( nDataBeginMark );
210 0 : InStream->skipBytes( nDataLen );
211 0 : xMark->deleteMark(nDataBeginMark);
212 0 : return aSeq;
213 : }
214 :
215 :
216 : // ::com::sun::star::uno::XInterface
217 7 : ::com::sun::star::uno::Any StdTabControllerModel::queryAggregation( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException, std::exception)
218 : {
219 : ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType,
220 : (static_cast< ::com::sun::star::awt::XTabControllerModel* >(this)),
221 : (static_cast< ::com::sun::star::lang::XServiceInfo* >(this)),
222 : (static_cast< ::com::sun::star::io::XPersistObject* >(this)),
223 7 : (static_cast< ::com::sun::star::lang::XTypeProvider* >(this)) );
224 7 : return (aRet.hasValue() ? aRet : OWeakAggObject::queryAggregation( rType ));
225 : }
226 :
227 : // ::com::sun::star::lang::XTypeProvider
228 0 : IMPL_XTYPEPROVIDER_START( StdTabControllerModel )
229 0 : cppu::UnoType<com::sun::star::awt::XTabControllerModel>::get(),
230 0 : cppu::UnoType<com::sun::star::lang::XServiceInfo>::get(),
231 0 : cppu::UnoType<com::sun::star::io::XPersistObject>::get()
232 0 : IMPL_XTYPEPROVIDER_END
233 :
234 0 : sal_Bool StdTabControllerModel::getGroupControl( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
235 : {
236 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
237 :
238 0 : return mbGroupControl;
239 : }
240 :
241 0 : void StdTabControllerModel::setGroupControl( sal_Bool GroupControl ) throw(::com::sun::star::uno::RuntimeException, std::exception)
242 : {
243 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
244 :
245 0 : mbGroupControl = GroupControl;
246 0 : }
247 :
248 0 : void StdTabControllerModel::setControlModels( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > >& Controls ) throw(::com::sun::star::uno::RuntimeException, std::exception)
249 : {
250 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
251 :
252 0 : maControls.Reset();
253 0 : ImplSetControlModels( maControls, Controls );
254 0 : }
255 :
256 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > > StdTabControllerModel::getControlModels( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
257 : {
258 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
259 :
260 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > > aSeq( ImplGetControlCount( maControls ) );
261 0 : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > * pRefs = aSeq.getArray();
262 0 : ImplGetControlModels( &pRefs, maControls );
263 0 : return aSeq;
264 : }
265 :
266 0 : void StdTabControllerModel::setGroup( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > >& Group, const OUString& GroupName ) throw(::com::sun::star::uno::RuntimeException, std::exception)
267 : {
268 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
269 :
270 : // The controls might occur as a flat list and will be grouped.
271 : // Nested groups are not possible.
272 : // The first element of a group determines its position.
273 0 : UnoControlModelEntry* pNewEntry = new UnoControlModelEntry;
274 0 : pNewEntry->bGroup = true;
275 0 : pNewEntry->pGroup = new UnoControlModelEntryList;
276 0 : pNewEntry->pGroup->SetName( GroupName );
277 0 : ImplSetControlModels( *pNewEntry->pGroup, Group );
278 :
279 0 : bool bInserted = false;
280 0 : size_t nElements = pNewEntry->pGroup->size();
281 0 : for ( size_t n = 0; n < nElements; n++ )
282 : {
283 0 : UnoControlModelEntry* pEntry = (*pNewEntry->pGroup)[ n ];
284 0 : if ( !pEntry->bGroup )
285 : {
286 0 : sal_uInt32 nPos = ImplGetControlPos( *pEntry->pxControl, maControls );
287 : // At the beginning, all Controls should be in a flattened list
288 : DBG_ASSERT( nPos != CONTROLPOS_NOTFOUND, "setGroup - Element not found" );
289 0 : if ( nPos != CONTROLPOS_NOTFOUND )
290 : {
291 0 : maControls.DestroyEntry( nPos );
292 0 : if ( !bInserted )
293 : {
294 0 : maControls.insert( nPos, pNewEntry );
295 0 : bInserted = true;
296 : }
297 : }
298 : }
299 : }
300 0 : if ( !bInserted )
301 0 : maControls.push_back( pNewEntry );
302 0 : }
303 :
304 0 : sal_Int32 StdTabControllerModel::getGroupCount( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
305 : {
306 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
307 :
308 : // Start with only one group layer, even though Model and Impl-methods
309 : // work recursively, this is not presented to the outside.
310 :
311 0 : sal_Int32 nGroups = 0;
312 0 : size_t nEntries = maControls.size();
313 0 : for ( size_t n = 0; n < nEntries; n++ )
314 : {
315 0 : UnoControlModelEntry* pEntry = maControls[ n ];
316 0 : if ( pEntry->bGroup )
317 0 : nGroups++;
318 : }
319 0 : return nGroups;
320 : }
321 :
322 0 : void StdTabControllerModel::getGroup( sal_Int32 nGroup, ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > >& rGroup, OUString& rName ) throw(::com::sun::star::uno::RuntimeException, std::exception)
323 : {
324 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
325 :
326 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > > aSeq;
327 0 : sal_uInt32 nG = 0;
328 0 : size_t nEntries = maControls.size();
329 0 : for ( size_t n = 0; n < nEntries; n++ )
330 : {
331 0 : UnoControlModelEntry* pEntry = maControls[ n ];
332 0 : if ( pEntry->bGroup )
333 : {
334 0 : if ( nG == (sal_uInt32)nGroup )
335 : {
336 0 : sal_uInt32 nCount = ImplGetControlCount( *pEntry->pGroup );
337 0 : aSeq = ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > >( nCount );
338 0 : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > * pRefs = aSeq.getArray();
339 0 : ImplGetControlModels( &pRefs, *pEntry->pGroup );
340 0 : rName = pEntry->pGroup->GetName();
341 0 : break;
342 : }
343 0 : nG++;
344 : }
345 : }
346 0 : rGroup = aSeq;
347 0 : }
348 :
349 0 : void StdTabControllerModel::getGroupByName( const OUString& rName, ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > >& rGroup ) throw(::com::sun::star::uno::RuntimeException, std::exception)
350 : {
351 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
352 :
353 0 : sal_uInt32 nGroup = 0;
354 0 : size_t nEntries = maControls.size();
355 0 : for ( size_t n = 0; n < nEntries; n++ )
356 : {
357 0 : UnoControlModelEntry* pEntry = maControls[ n ];
358 0 : if ( pEntry->bGroup )
359 : {
360 0 : if ( pEntry->pGroup->GetName() == rName )
361 : {
362 0 : OUString Dummy;
363 0 : getGroup( nGroup, rGroup, Dummy );
364 0 : break;
365 : }
366 0 : nGroup++;
367 : }
368 0 : }
369 0 : }
370 :
371 :
372 : // ::com::sun::star::io::XPersistObject
373 0 : OUString StdTabControllerModel::getServiceName( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
374 : {
375 0 : return OUString::createFromAscii( szServiceName_TabControllerModel );
376 : }
377 :
378 0 : void StdTabControllerModel::write( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XObjectOutputStream >& OutStream ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
379 : {
380 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
381 :
382 0 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XMarkableStream > xMark( OutStream, ::com::sun::star::uno::UNO_QUERY );
383 : DBG_ASSERT( xMark.is(), "write: no XMarkableStream!" );
384 :
385 0 : OutStream->writeShort( UNOCONTROL_STREAMVERSION );
386 :
387 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > > aCtrls = getControlModels();
388 0 : ImplWriteControls( OutStream, aCtrls );
389 :
390 0 : sal_uInt32 nGroups = getGroupCount();
391 0 : OutStream->writeLong( nGroups );
392 0 : for ( sal_uInt32 n = 0; n < nGroups; n++ )
393 : {
394 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > > aGroupCtrls;
395 0 : OUString aGroupName;
396 0 : getGroup( n, aGroupCtrls, aGroupName );
397 0 : OutStream->writeUTF( aGroupName );
398 0 : ImplWriteControls( OutStream, aGroupCtrls );
399 0 : }
400 0 : }
401 :
402 0 : void StdTabControllerModel::read( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XObjectInputStream >& InStream ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
403 : {
404 0 : ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
405 :
406 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > > aSeq = ImplReadControls( InStream );
407 0 : setControlModels( aSeq );
408 :
409 0 : sal_uInt32 nGroups = InStream->readLong();
410 0 : for ( sal_uInt32 n = 0; n < nGroups; n++ )
411 : {
412 0 : OUString aGroupName = InStream->readUTF();
413 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel > > aCtrlSeq = ImplReadControls( InStream );
414 0 : setGroup( aCtrlSeq, aGroupName );
415 0 : }
416 0 : }
417 :
418 2 : OUString StdTabControllerModel::getImplementationName()
419 : throw (css::uno::RuntimeException, std::exception)
420 : {
421 2 : return OUString("stardiv.Toolkit.StdTabControllerModel");
422 : }
423 :
424 0 : sal_Bool StdTabControllerModel::supportsService(OUString const & ServiceName)
425 : throw (css::uno::RuntimeException, std::exception)
426 : {
427 0 : return cppu::supportsService(this, ServiceName);
428 : }
429 :
430 1 : css::uno::Sequence<OUString> StdTabControllerModel::getSupportedServiceNames()
431 : throw (css::uno::RuntimeException, std::exception)
432 : {
433 : return css::uno::Sequence<OUString>{
434 : OUString::createFromAscii(szServiceName2_TabControllerModel),
435 1 : "stardiv.vcl.controlmodel.TabController"};
436 : }
437 :
438 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
439 2 : stardiv_Toolkit_StdTabControllerModel_get_implementation(
440 : css::uno::XComponentContext *,
441 : css::uno::Sequence<css::uno::Any> const &)
442 : {
443 2 : return cppu::acquire(new StdTabControllerModel());
444 : }
445 :
446 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|