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