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