Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / svx / source / xoutdev / xtable.cxx
blob9d18cc08fdb679872461206d101944202016ec06
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <svx/XPropertyTable.hxx>
31 #include "xmlxtexp.hxx"
32 #include "xmlxtimp.hxx"
33 #include <tools/urlobj.hxx>
34 #include <svx/xtable.hxx>
35 #include <svx/xpool.hxx>
37 using namespace com::sun::star;
39 // Helper for other sub-classes to have easy-to-read constructors
40 Color RGB_Color( ColorData nColorName )
42 Color aColor( nColorName );
43 Color aRGBColor( aColor.GetRed(), aColor.GetGreen(), aColor.GetBlue() );
44 return aRGBColor;
47 // static int count = 0;
49 XPropertyList::XPropertyList(
50 XPropertyListType type,
51 const String& rPath,
52 XOutdevItemPool* pInPool
53 ) : eType ( type ),
54 aName ( RTL_CONSTASCII_USTRINGPARAM( "standard" ) ),
55 aPath ( rPath ),
56 pXPool ( pInPool ),
57 pBmpList ( NULL ),
58 bListDirty ( true ),
59 bBitmapsDirty ( true ),
60 bOwnPool ( false ),
61 bEmbedInDocument( false )
63 if( !pXPool )
65 bOwnPool = true;
66 pXPool = new XOutdevItemPool;
67 DBG_ASSERT( pXPool, "XOutPool konnte nicht erzeugt werden!" );
69 // fprintf (stderr, "Create type %d count %d\n", (int)eType, count++);
72 XPropertyList::~XPropertyList()
74 // fprintf (stderr, "Destroy type %d count %d\n", (int)eType, --count);
75 for( size_t i = 0, n = aList.size(); i < n; ++i )
76 delete aList[ i ];
78 aList.clear();
80 if( pBmpList )
82 for ( size_t i = 0, n = pBmpList->size(); i < n; ++i ) {
83 delete (*pBmpList)[ i ];
85 pBmpList->clear();
86 delete pBmpList;
87 pBmpList = NULL;
90 if( bOwnPool && pXPool )
91 SfxItemPool::Free(pXPool);
94 long XPropertyList::Count() const
96 if( bListDirty )
98 if( !( (XPropertyList*) this )->Load() )
99 ( (XPropertyList*) this )->Create();
101 return( aList.size() );
104 XPropertyEntry* XPropertyList::Get( long nIndex, sal_uInt16 /*nDummy*/) const
106 if( bListDirty )
108 if( !( (XPropertyList*) this )->Load() )
109 ( (XPropertyList*) this )->Create();
111 return ( (size_t)nIndex < aList.size() ) ? aList[ nIndex ] : NULL;
114 long XPropertyList::Get(const XubString& rName)
116 if( bListDirty )
118 if( !Load() )
119 Create();
122 for( long i = 0, n = aList.size(); i < n; ++i ) {
123 if ( aList[ i ]->GetName() == rName ) {
124 return i;
127 return -1;
130 Bitmap* XPropertyList::GetBitmap( long nIndex ) const
132 if( pBmpList )
134 if( bBitmapsDirty )
136 ( (XPropertyList*) this )->bBitmapsDirty = false;
137 ( (XPropertyList*) this )->CreateBitmapsForUI();
139 if( (size_t)nIndex < pBmpList->size() )
140 return (*pBmpList)[ nIndex ];
142 return NULL;
145 void XPropertyList::Insert( XPropertyEntry* pEntry, long nIndex )
147 if ( (size_t)nIndex < aList.size() ) {
148 aList.insert( aList.begin() + nIndex, pEntry );
149 } else {
150 aList.push_back( pEntry );
153 if( pBmpList && !bBitmapsDirty )
155 Bitmap* pBmp = CreateBitmapForUI(
156 (size_t)nIndex < aList.size() ? nIndex : aList.size() - 1
158 if ( (size_t)nIndex < pBmpList->size() ) {
159 pBmpList->insert( pBmpList->begin() + nIndex, pBmp );
160 } else {
161 pBmpList->push_back( pBmp );
166 XPropertyEntry* XPropertyList::Replace( XPropertyEntry* pEntry, long nIndex )
168 XPropertyEntry* pOldEntry = (size_t)nIndex < aList.size() ? aList[ nIndex ] : NULL;
169 if ( pOldEntry ) {
170 aList[ nIndex ] = pEntry;
173 if( pBmpList && !bBitmapsDirty )
175 Bitmap* pBmp = CreateBitmapForUI( (sal_uIntPtr) nIndex );
176 if ( (size_t)nIndex < pBmpList->size() )
178 delete (*pBmpList)[ nIndex ];
179 (*pBmpList)[ nIndex ] = pBmp;
181 else {
182 pBmpList->push_back( pBmp );
185 return pOldEntry;
188 XPropertyEntry* XPropertyList::Remove( long nIndex )
190 if( pBmpList && !bBitmapsDirty )
192 if ( (size_t)nIndex < pBmpList->size() )
194 delete (*pBmpList)[ nIndex ];
195 pBmpList->erase( pBmpList->begin() + nIndex );
199 XPropertyEntry* pEntry = NULL;
200 if ( (size_t)nIndex < aList.size() ) {
201 pEntry = aList[ nIndex ];
202 aList.erase( aList.begin() + nIndex );
204 return pEntry;
207 void XPropertyList::SetName( const String& rString )
209 if(rString.Len())
211 aName = rString;
215 bool XPropertyList::Load()
217 if( bListDirty )
219 bListDirty = false;
221 INetURLObject aURL( aPath );
223 if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
225 DBG_ASSERT( !aPath.Len(), "invalid URL" );
226 return false;
229 aURL.Append( aName );
231 if( aURL.getExtension().isEmpty() )
232 aURL.setExtension( GetDefaultExt() );
234 return SvxXMLXTableImport::load( aURL.GetMainURL( INetURLObject::NO_DECODE ),
235 uno::Reference < embed::XStorage >(),
236 createInstance(), NULL );
239 return false;
242 bool XPropertyList::LoadFrom( const uno::Reference < embed::XStorage > &xStorage,
243 const rtl::OUString &rURL )
245 if( !bListDirty )
246 return false;
247 bListDirty = false;
248 return SvxXMLXTableImport::load( rURL, xStorage, createInstance(), &bEmbedInDocument );
251 bool XPropertyList::Save()
253 INetURLObject aURL( aPath );
255 if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
257 DBG_ASSERT( !aPath.Len(), "invalid URL" );
258 return false;
261 aURL.Append( aName );
263 if( aURL.getExtension().isEmpty() )
264 aURL.setExtension( GetDefaultExt() );
266 return SvxXMLXTableExportComponent::save( aURL.GetMainURL( INetURLObject::NO_DECODE ),
267 createInstance(),
268 uno::Reference< embed::XStorage >(), NULL );
271 bool XPropertyList::SaveTo( const uno::Reference< embed::XStorage > &xStorage,
272 const rtl::OUString &rURL, rtl::OUString *pOptName )
274 return SvxXMLXTableExportComponent::save( rURL, createInstance(), xStorage, pOptName );
277 XPropertyListRef XPropertyList::CreatePropertyList( XPropertyListType t,
278 const String& rPath,
279 XOutdevItemPool* pXPool )
281 XPropertyListRef pRet;
283 #define MAP(e,c) \
284 case e: pRet = XPropertyListRef (new c( rPath, pXPool ) ); break
285 switch (t) {
286 MAP( XCOLOR_LIST, XColorList );
287 MAP( XLINE_END_LIST, XLineEndList );
288 MAP( XDASH_LIST, XDashList );
289 MAP( XHATCH_LIST, XHatchList );
290 MAP( XGRADIENT_LIST, XGradientList );
291 MAP( XBITMAP_LIST, XBitmapList );
292 default:
293 OSL_FAIL("unknown xproperty type");
294 break;
296 #undef MAP
297 OSL_ASSERT( !pRet.is() || pRet->eType == t );
299 return pRet;
302 XPropertyListRef
303 XPropertyList::CreatePropertyListFromURL( XPropertyListType t,
304 const rtl::OUString & rURLStr,
305 XOutdevItemPool* pXPool )
307 INetURLObject aURL( rURLStr );
308 INetURLObject aPathURL( aURL );
310 aPathURL.removeSegment();
311 aPathURL.removeFinalSlash();
313 XPropertyListRef pList = XPropertyList::CreatePropertyList(
314 t, aPathURL.GetMainURL( INetURLObject::NO_DECODE ), pXPool );
315 pList->SetName( aURL.getName() );
317 return pList;
320 // catch people being silly with ref counting ...
322 void* XPropertyList::operator new (size_t nCount)
324 return rtl_allocateMemory( nCount );
327 void XPropertyList::operator delete(void *pPtr)
329 return rtl_freeMemory( pPtr );
332 static struct {
333 XPropertyListType t;
334 const char *pExt;
335 } pExtnMap[] = {
336 { XCOLOR_LIST, "soc" },
337 { XLINE_END_LIST, "soe" },
338 { XDASH_LIST, "sod" },
339 { XHATCH_LIST, "soh" },
340 { XGRADIENT_LIST, "sog" },
341 { XBITMAP_LIST, "sob" }
344 rtl::OUString XPropertyList::GetDefaultExt( XPropertyListType t )
346 for (size_t i = 0; i < SAL_N_ELEMENTS (pExtnMap); i++)
348 if( pExtnMap[i].t == t )
349 return rtl::OUString::createFromAscii( pExtnMap[ i ].pExt );
351 return rtl::OUString();
354 rtl::OUString XPropertyList::GetDefaultExtFilter( XPropertyListType t )
356 rtl::OUString aFilter( RTL_CONSTASCII_USTRINGPARAM( "*." ) );
357 return aFilter + GetDefaultExt( t );
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */