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