1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: stdtabcontrollermodel.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_toolkit.hxx"
33 #include <com/sun/star/io/XMarkableStream.hpp>
35 #include <toolkit/controls/stdtabcontrollermodel.hxx>
36 #include <toolkit/helper/macros.hxx>
37 #include <toolkit/helper/servicenames.hxx>
38 #include <toolkit/helper/property.hxx>
39 #include <cppuhelper/typeprovider.hxx>
40 #include <rtl/memory.h>
43 #include <tools/debug.hxx>
45 #define UNOCONTROL_STREAMVERSION (short)2
47 // ----------------------------------------------------
48 // class UnoControlModelEntryList
49 // ----------------------------------------------------
50 UnoControlModelEntryList::UnoControlModelEntryList()
54 UnoControlModelEntryList::~UnoControlModelEntryList()
59 void UnoControlModelEntryList::Reset()
61 for ( sal_uInt32 n
= Count(); n
; )
65 void UnoControlModelEntryList::DestroyEntry( sal_uInt32 nEntry
)
67 UnoControlModelEntry
* pEntry
= GetObject( nEntry
);
70 delete pEntry
->pGroup
;
72 delete pEntry
->pxControl
;
78 // ----------------------------------------------------
79 // class StdTabControllerModel
80 // ----------------------------------------------------
81 StdTabControllerModel::StdTabControllerModel()
83 mbGroupControl
= sal_True
;
86 StdTabControllerModel::~StdTabControllerModel()
90 sal_uInt32
StdTabControllerModel::ImplGetControlCount( const UnoControlModelEntryList
& rList
) const
92 sal_uInt32 nCount
= 0;
93 sal_uInt32 nEntries
= rList
.Count();
94 for ( sal_uInt32 n
= 0; n
< nEntries
; n
++ )
96 UnoControlModelEntry
* pEntry
= rList
.GetObject( n
);
98 nCount
+= ImplGetControlCount( *pEntry
->pGroup
);
105 void StdTabControllerModel::ImplGetControlModels( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> ** ppRefs
, const UnoControlModelEntryList
& rList
) const
107 sal_uInt32 nEntries
= rList
.Count();
108 for ( sal_uInt32 n
= 0; n
< nEntries
; n
++ )
110 UnoControlModelEntry
* pEntry
= rList
.GetObject( n
);
111 if ( pEntry
->bGroup
)
112 ImplGetControlModels( ppRefs
, *pEntry
->pGroup
);
115 **ppRefs
= *pEntry
->pxControl
;
121 void StdTabControllerModel::ImplSetControlModels( UnoControlModelEntryList
& rList
, const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> >& Controls
) const
123 const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> * pRefs
= Controls
.getConstArray();
124 sal_uInt32 nControls
= Controls
.getLength();
125 for ( sal_uInt32 n
= 0; n
< nControls
; n
++ )
127 UnoControlModelEntry
* pNewEntry
= new UnoControlModelEntry
;
128 pNewEntry
->bGroup
= sal_False
;
129 pNewEntry
->pxControl
= new ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> ;
130 *pNewEntry
->pxControl
= pRefs
[n
];
131 rList
.Insert( pNewEntry
, LIST_APPEND
);
135 sal_uInt32
StdTabControllerModel::ImplGetControlPos( const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> xCtrl
, const UnoControlModelEntryList
& rList
) const
137 for ( sal_uInt32 n
= rList
.Count(); n
; )
139 UnoControlModelEntry
* pEntry
= rList
.GetObject( --n
);
140 if ( !pEntry
->bGroup
&& ( *pEntry
->pxControl
== xCtrl
) )
143 return CONTROLPOS_NOTFOUND
;
146 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
)
148 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XMarkableStream
> xMark( OutStream
, ::com::sun::star::uno::UNO_QUERY
);
149 DBG_ASSERT( xMark
.is(), "write: no XMarkableStream!" );
151 sal_uInt32 nStoredControls
= 0;
152 sal_Int32 nDataBeginMark
= xMark
->createMark();
154 OutStream
->writeLong( 0L ); // DataLen
155 OutStream
->writeLong( 0L ); // nStoredControls
157 sal_uInt32 nCtrls
= rCtrls
.getLength();
158 for ( sal_uInt32 n
= 0; n
< nCtrls
; n
++ )
160 const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> xI
= rCtrls
.getConstArray()[n
];
161 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XPersistObject
> xPO( xI
, ::com::sun::star::uno::UNO_QUERY
);
162 DBG_ASSERT( xPO
.is(), "write: Control doesn't support XPersistObject" );
165 OutStream
->writeObject( xPO
);
169 sal_Int32 nDataLen
= xMark
->offsetToMark( nDataBeginMark
);
170 xMark
->jumpToMark( nDataBeginMark
);
171 OutStream
->writeLong( nDataLen
);
172 OutStream
->writeLong( nStoredControls
);
173 xMark
->jumpToFurthest();
174 xMark
->deleteMark(nDataBeginMark
);
177 ::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
)
179 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XMarkableStream
> xMark( InStream
, ::com::sun::star::uno::UNO_QUERY
);
180 DBG_ASSERT( xMark
.is(), "write: no XMarkableStream!" );
182 sal_Int32 nDataBeginMark
= xMark
->createMark();
184 sal_Int32 nDataLen
= InStream
->readLong();
185 sal_uInt32 nCtrls
= InStream
->readLong();
187 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> > aSeq( nCtrls
);
188 for ( sal_uInt32 n
= 0; n
< nCtrls
; n
++ )
190 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XPersistObject
> xObj
= InStream
->readObject();
191 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> xI( xObj
, ::com::sun::star::uno::UNO_QUERY
);
192 aSeq
.getArray()[n
] = xI
;
195 // Falls bereits mehr drinsteht als diese Version kennt:
196 xMark
->jumpToMark( nDataBeginMark
);
197 InStream
->skipBytes( nDataLen
);
198 xMark
->deleteMark(nDataBeginMark
);
203 // ::com::sun::star::uno::XInterface
204 ::com::sun::star::uno::Any
StdTabControllerModel::queryAggregation( const ::com::sun::star::uno::Type
& rType
) throw(::com::sun::star::uno::RuntimeException
)
206 ::com::sun::star::uno::Any aRet
= ::cppu::queryInterface( rType
,
207 SAL_STATIC_CAST( ::com::sun::star::awt::XTabControllerModel
*, this ),
208 SAL_STATIC_CAST( ::com::sun::star::lang::XServiceInfo
*, this ),
209 SAL_STATIC_CAST( ::com::sun::star::io::XPersistObject
*, this ),
210 SAL_STATIC_CAST( ::com::sun::star::lang::XTypeProvider
*, this ) );
211 return (aRet
.hasValue() ? aRet
: OWeakAggObject::queryAggregation( rType
));
214 // ::com::sun::star::lang::XTypeProvider
215 IMPL_XTYPEPROVIDER_START( StdTabControllerModel
)
216 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XTabControllerModel
>* ) NULL
),
217 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XServiceInfo
>* ) NULL
),
218 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::io::XPersistObject
>* ) NULL
)
219 IMPL_XTYPEPROVIDER_END
221 sal_Bool
StdTabControllerModel::getGroupControl( ) throw(::com::sun::star::uno::RuntimeException
)
223 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
225 return mbGroupControl
;
228 void StdTabControllerModel::setGroupControl( sal_Bool GroupControl
) throw(::com::sun::star::uno::RuntimeException
)
230 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
232 mbGroupControl
= GroupControl
;
235 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
)
237 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
240 ImplSetControlModels( maControls
, Controls
);
243 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> > StdTabControllerModel::getControlModels( ) throw(::com::sun::star::uno::RuntimeException
)
245 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
247 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> > aSeq( ImplGetControlCount( maControls
) );
248 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> * pRefs
= aSeq
.getArray();
249 ImplGetControlModels( &pRefs
, maControls
);
253 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
)
255 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
257 // Die Controls stehen eventuel flach in der Liste und werden jetzt gruppiert.
258 // Verschachtelte Gruppen sind erstmal nicht moeglich...
259 // Das erste Element der Gruppe welches auch schon in der flachen Liste
260 // stand bestimmt die Position der Gruppe.
262 UnoControlModelEntry
* pNewEntry
= new UnoControlModelEntry
;
263 pNewEntry
->bGroup
= sal_True
;
264 pNewEntry
->pGroup
= new UnoControlModelEntryList
;
265 pNewEntry
->pGroup
->SetName( GroupName
);
266 ImplSetControlModels( *pNewEntry
->pGroup
, Group
);
268 sal_Bool bInserted
= sal_False
;
269 sal_uInt32 nElements
= pNewEntry
->pGroup
->Count();
270 for ( sal_uInt32 n
= 0; n
< nElements
; n
++ )
272 UnoControlModelEntry
* pEntry
= pNewEntry
->pGroup
->GetObject( n
);
273 if ( !pEntry
->bGroup
)
275 sal_uInt32 nPos
= ImplGetControlPos( *pEntry
->pxControl
, maControls
);
276 // Eigentlich sollten alle Controls vorher in der flachen Liste stehen
277 DBG_ASSERT( nPos
!= CONTROLPOS_NOTFOUND
, "setGroup - Element not found" );
278 if ( nPos
!= CONTROLPOS_NOTFOUND
)
280 maControls
.DestroyEntry( nPos
);
283 maControls
.Insert( pNewEntry
, nPos
);
284 bInserted
= sal_True
;
290 maControls
.Insert( pNewEntry
, LIST_APPEND
);
293 sal_Int32
StdTabControllerModel::getGroupCount( ) throw(::com::sun::star::uno::RuntimeException
)
295 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
297 // erstmal nur eine Ebene...
298 // Das Model und die Impl-Methoden arbeiten zwar rekursiv, aber das wird
299 // erstmal nich nach aussen gegeben.
301 sal_Int32 nGroups
= 0;
302 sal_uInt32 nEntries
= maControls
.Count();
303 for ( sal_uInt32 n
= 0; n
< nEntries
; n
++ )
305 UnoControlModelEntry
* pEntry
= maControls
.GetObject( n
);
306 if ( pEntry
->bGroup
)
312 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
)
314 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
316 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> > aSeq
;
318 sal_uInt32 nEntries
= maControls
.Count();
319 for ( sal_uInt32 n
= 0; n
< nEntries
; n
++ )
321 UnoControlModelEntry
* pEntry
= maControls
.GetObject( n
);
322 if ( pEntry
->bGroup
)
324 if ( nG
== (sal_uInt32
)nGroup
)
326 sal_uInt32 nCount
= ImplGetControlCount( *pEntry
->pGroup
);
327 aSeq
= ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> >( nCount
);
328 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> * pRefs
= aSeq
.getArray();
329 ImplGetControlModels( &pRefs
, *pEntry
->pGroup
);
330 rName
= pEntry
->pGroup
->GetName();
339 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
)
341 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
343 sal_uInt32 nGroup
= 0;
344 sal_uInt32 nEntries
= maControls
.Count();
345 for ( sal_uInt32 n
= 0; n
< nEntries
; n
++ )
347 UnoControlModelEntry
* pEntry
= maControls
.GetObject( n
);
348 if ( pEntry
->bGroup
)
350 if ( pEntry
->pGroup
->GetName() == rName
)
352 ::rtl::OUString Dummy
;
353 getGroup( nGroup
, rGroup
, Dummy
);
362 // ::com::sun::star::io::XPersistObject
363 ::rtl::OUString
StdTabControllerModel::getServiceName( ) throw(::com::sun::star::uno::RuntimeException
)
365 return ::rtl::OUString::createFromAscii( szServiceName_TabControllerModel
);
368 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
)
370 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
372 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XMarkableStream
> xMark( OutStream
, ::com::sun::star::uno::UNO_QUERY
);
373 DBG_ASSERT( xMark
.is(), "write: no XMarkableStream!" );
375 OutStream
->writeShort( UNOCONTROL_STREAMVERSION
);
377 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> > aCtrls
= getControlModels();
378 ImplWriteControls( OutStream
, aCtrls
);
380 sal_uInt32 nGroups
= getGroupCount();
381 OutStream
->writeLong( nGroups
);
382 for ( sal_uInt32 n
= 0; n
< nGroups
; n
++ )
384 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> > aGroupCtrls
;
385 ::rtl::OUString aGroupName
;
386 getGroup( n
, aGroupCtrls
, aGroupName
);
387 OutStream
->writeUTF( aGroupName
);
388 ImplWriteControls( OutStream
, aGroupCtrls
);
392 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
)
394 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
396 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> > aSeq
= ImplReadControls( InStream
);
397 setControlModels( aSeq
);
399 sal_uInt32 nGroups
= InStream
->readLong();
400 for ( sal_uInt32 n
= 0; n
< nGroups
; n
++ )
402 ::rtl::OUString aGroupName
= InStream
->readUTF();
403 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XControlModel
> > aCtrlSeq
= ImplReadControls( InStream
);
404 setGroup( aCtrlSeq
, aGroupName
);