Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / editeng / source / items / bulitem.cxx
blob0a433ecfa49e86222770338a3669dde5c88dc14a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <tools/stream.hxx>
21 #include <vcl/outdev.hxx>
23 #define _SVX_BULITEM_CXX
25 #include <editeng/bulitem.hxx>
26 #include <editeng/editrids.hrc>
28 #include <tools/tenccvt.hxx>
30 #define BULITEM_VERSION ((sal_uInt16)2)
32 // -----------------------------------------------------------------------
34 TYPEINIT1(SvxBulletItem,SfxPoolItem);
36 // -----------------------------------------------------------------------
38 void SvxBulletItem::StoreFont( SvStream& rStream, const Font& rFont )
40 sal_uInt16 nTemp;
42 rStream << rFont.GetColor();
43 nTemp = (sal_uInt16)rFont.GetFamily(); rStream << nTemp;
45 nTemp = (sal_uInt16)GetSOStoreTextEncoding((rtl_TextEncoding)rFont.GetCharSet());
46 rStream << nTemp;
48 nTemp = (sal_uInt16)rFont.GetPitch(); rStream << nTemp;
49 nTemp = (sal_uInt16)rFont.GetAlign(); rStream << nTemp;
50 nTemp = (sal_uInt16)rFont.GetWeight(); rStream << nTemp;
51 nTemp = (sal_uInt16)rFont.GetUnderline(); rStream << nTemp;
52 nTemp = (sal_uInt16)rFont.GetStrikeout(); rStream << nTemp;
53 nTemp = (sal_uInt16)rFont.GetItalic(); rStream << nTemp;
55 // UNICODE: rStream << rFont.GetName();
56 rStream.WriteUniOrByteString(rFont.GetName(), rStream.GetStreamCharSet());
58 rStream << rFont.IsOutline();
59 rStream << rFont.IsShadow();
60 rStream << rFont.IsTransparent();
63 // -----------------------------------------------------------------------
65 Font SvxBulletItem::CreateFont( SvStream& rStream, sal_uInt16 nVer )
67 Font aFont;
68 Color aColor;
69 rStream >> aColor; aFont.SetColor( aColor );
70 sal_uInt16 nTemp;
71 rStream >> nTemp; aFont.SetFamily((FontFamily)nTemp);
73 rStream >> nTemp;
74 nTemp = (sal_uInt16)GetSOLoadTextEncoding((rtl_TextEncoding)nTemp);
75 aFont.SetCharSet((rtl_TextEncoding)nTemp);
77 rStream >> nTemp; aFont.SetPitch((FontPitch)nTemp);
78 rStream >> nTemp; aFont.SetAlign((FontAlign)nTemp);
79 rStream >> nTemp; aFont.SetWeight((FontWeight)nTemp);
80 rStream >> nTemp; aFont.SetUnderline((FontUnderline)nTemp);
81 rStream >> nTemp; aFont.SetStrikeout((FontStrikeout)nTemp);
82 rStream >> nTemp; aFont.SetItalic((FontItalic)nTemp);
84 // UNICODE: rStream >> aName; aFont.SetName( aName );
85 String aName = rStream.ReadUniOrByteString(rStream.GetStreamCharSet());
86 aFont.SetName( aName );
88 if( nVer == 1 )
90 //#fdo39428 SvStream no longer supports operator>>(long&)
91 sal_Int32 nHeight(0), nWidth(0);
92 rStream >> nHeight; rStream >> nWidth; Size aSize( nWidth, nHeight );
93 aFont.SetSize( aSize );
96 sal_Bool bTemp;
97 rStream >> bTemp; aFont.SetOutline( bTemp );
98 rStream >> bTemp; aFont.SetShadow( bTemp );
99 rStream >> bTemp; aFont.SetTransparent( bTemp );
100 return aFont;
104 // -----------------------------------------------------------------------
106 SvxBulletItem::SvxBulletItem( sal_uInt16 _nWhich ) : SfxPoolItem( _nWhich )
108 SetDefaultFont_Impl();
109 SetDefaults_Impl();
110 nValidMask = 0xFFFF;
113 // -----------------------------------------------------------------------
115 SvxBulletItem::SvxBulletItem( SvStream& rStrm, sal_uInt16 _nWhich ) :
116 SfxPoolItem( _nWhich ),
117 pGraphicObject( NULL )
119 rStrm >> nStyle;
121 if( nStyle != BS_BMP )
122 aFont = CreateFont( rStrm, BULITEM_VERSION );
123 else
125 // Safe Load with Test on empty Bitmap
126 Bitmap aBmp;
127 const sal_uInt32 nOldPos = rStrm.Tell();
128 // Ignore Errorcode when reading Bitmap,
129 // see comment in SvxBulletItem::Store()
130 sal_Bool bOldError = rStrm.GetError() ? sal_True : sal_False;
131 rStrm >> aBmp;
132 if ( !bOldError && rStrm.GetError() )
134 rStrm.ResetError();
137 if( aBmp.IsEmpty() )
139 rStrm.Seek( nOldPos );
140 nStyle = BS_NONE;
142 else
143 pGraphicObject = new GraphicObject( aBmp );
146 //#fdo39428 SvStream no longer supports operator>>(long&)
147 sal_Int32 nTmp(0);
148 rStrm >> nTmp; nWidth = nTmp;
149 rStrm >> nStart;
150 rStrm >> nJustify;
152 char cTmpSymbol;
153 rStrm >> cTmpSymbol;
154 //convert single byte to unicode
155 cSymbol = rtl::OUString(&cTmpSymbol, 1, aFont.GetCharSet()).toChar();
157 rStrm >> nScale;
159 // UNICODE: rStrm >> aPrevText;
160 aPrevText = rStrm.ReadUniOrByteString(rStrm.GetStreamCharSet());
162 // UNICODE: rStrm >> aFollowText;
163 aFollowText = rStrm.ReadUniOrByteString(rStrm.GetStreamCharSet());
165 nValidMask = 0xFFFF;
168 // -----------------------------------------------------------------------
170 SvxBulletItem::SvxBulletItem( const SvxBulletItem& rItem) : SfxPoolItem( rItem )
172 aFont = rItem.aFont;
173 pGraphicObject = ( rItem.pGraphicObject ? new GraphicObject( *rItem.pGraphicObject ) : NULL );
174 aPrevText = rItem.aPrevText;
175 aFollowText = rItem.aFollowText;
176 nStart = rItem.nStart;
177 nStyle = rItem.nStyle;
178 nWidth = rItem.nWidth;
179 nScale = rItem.nScale;
180 cSymbol = rItem.cSymbol;
181 nJustify = rItem.nJustify;
182 nValidMask = rItem.nValidMask;
185 // -----------------------------------------------------------------------
187 SvxBulletItem::~SvxBulletItem()
189 if( pGraphicObject )
190 delete pGraphicObject;
193 // -----------------------------------------------------------------------
195 SfxPoolItem* SvxBulletItem::Clone( SfxItemPool * /*pPool*/ ) const
197 return new SvxBulletItem( *this );
200 // -----------------------------------------------------------------------
202 SfxPoolItem* SvxBulletItem::Create( SvStream& rStrm, sal_uInt16 /*nVersion*/ ) const
204 return new SvxBulletItem( rStrm, Which() );
207 // -----------------------------------------------------------------------
209 void SvxBulletItem::SetDefaultFont_Impl()
211 aFont = OutputDevice::GetDefaultFont( DEFAULTFONT_FIXED, LANGUAGE_SYSTEM, 0 );
212 aFont.SetAlign( ALIGN_BOTTOM);
213 aFont.SetTransparent( sal_True );
216 // -----------------------------------------------------------------------
218 void SvxBulletItem::SetDefaults_Impl()
220 pGraphicObject = NULL;
221 nWidth = 1200; // 1.2cm
222 nStart = 1;
223 nStyle = BS_123;
224 nJustify = BJ_HLEFT | BJ_VCENTER;
225 cSymbol = sal_Unicode(' ');
226 nScale = 75;
229 // -----------------------------------------------------------------------
231 sal_uInt16 SvxBulletItem::GetVersion( sal_uInt16 /*nVersion*/ ) const
233 return BULITEM_VERSION;
236 // -----------------------------------------------------------------------
238 void SvxBulletItem::CopyValidProperties( const SvxBulletItem& rCopyFrom )
240 Font _aFont = GetFont();
241 Font aNewFont = rCopyFrom.GetFont();
242 if ( rCopyFrom.IsValid( VALID_FONTNAME ) )
244 _aFont.SetName( aNewFont.GetName() );
245 _aFont.SetFamily( aNewFont.GetFamily() );
246 _aFont.SetStyleName( aNewFont.GetStyleName() );
248 if ( rCopyFrom.IsValid( VALID_FONTCOLOR ) )
249 _aFont.SetColor( aNewFont.GetColor() );
250 if ( rCopyFrom.IsValid( VALID_SYMBOL ) )
251 SetSymbol( rCopyFrom.GetSymbol() );
252 if ( rCopyFrom.IsValid( VALID_BITMAP ) )
253 SetGraphicObject( rCopyFrom.GetGraphicObject() );
254 if ( rCopyFrom.IsValid( VALID_SCALE ) )
255 SetScale( rCopyFrom.GetScale() );
256 if ( rCopyFrom.IsValid( VALID_START ) )
257 SetStart( rCopyFrom.GetStart() );
258 if ( rCopyFrom.IsValid( VALID_STYLE ) )
259 SetStyle( rCopyFrom.GetStyle() );
260 if ( rCopyFrom.IsValid( VALID_PREVTEXT ) )
261 SetPrevText( rCopyFrom.GetPrevText() );
262 if ( rCopyFrom.IsValid( VALID_FOLLOWTEXT ) )
263 SetFollowText( rCopyFrom.GetFollowText() );
265 SetFont( _aFont );
269 // -----------------------------------------------------------------------
271 int SvxBulletItem::operator==( const SfxPoolItem& rItem ) const
273 DBG_ASSERT(rItem.ISA(SvxBulletItem),"operator==Types not matching");
274 const SvxBulletItem& rBullet = (const SvxBulletItem&)rItem;
275 // Compare with ValidMask, otherwise no put possible in a AttrSet if the
276 // item differs only in terms of the ValidMask from an existing one.
277 if( nValidMask != rBullet.nValidMask ||
278 nStyle != rBullet.nStyle ||
279 nScale != rBullet.nScale ||
280 nJustify != rBullet.nJustify ||
281 nWidth != rBullet.nWidth ||
282 nStart != rBullet.nStart ||
283 cSymbol != rBullet.cSymbol ||
284 aPrevText != rBullet.aPrevText ||
285 aFollowText != rBullet.aFollowText )
286 return 0;
288 if( ( nStyle != BS_BMP ) && ( aFont != rBullet.aFont ) )
289 return 0;
291 if( nStyle == BS_BMP )
293 if( ( pGraphicObject && !rBullet.pGraphicObject ) || ( !pGraphicObject && rBullet.pGraphicObject ) )
294 return 0;
296 if( ( pGraphicObject && rBullet.pGraphicObject ) &&
297 ( ( *pGraphicObject != *rBullet.pGraphicObject ) ||
298 ( pGraphicObject->GetPrefSize() != rBullet.pGraphicObject->GetPrefSize() ) ) )
300 return 0;
304 return 1;
307 // -----------------------------------------------------------------------
309 SvStream& SvxBulletItem::Store( SvStream& rStrm, sal_uInt16 /*nItemVersion*/ ) const
311 // Correction for empty bitmap
312 if( ( nStyle == BS_BMP ) &&
313 ( !pGraphicObject || ( GRAPHIC_NONE == pGraphicObject->GetType() ) || ( GRAPHIC_DEFAULT == pGraphicObject->GetType() ) ) )
315 if( pGraphicObject )
317 delete( const_cast< SvxBulletItem* >( this )->pGraphicObject );
318 const_cast< SvxBulletItem* >( this )->pGraphicObject = NULL;
321 const_cast< SvxBulletItem* >( this )->nStyle = BS_NONE;
324 rStrm << nStyle;
326 if( nStyle != BS_BMP )
327 StoreFont( rStrm, aFont );
328 else
330 sal_uLong _nStart = rStrm.Tell();
332 // Small preliminary estimate of the size ...
333 sal_uInt16 nFac = ( rStrm.GetCompressMode() != COMPRESSMODE_NONE ) ? 3 : 1;
334 const Bitmap aBmp( pGraphicObject->GetGraphic().GetBitmap() );
335 sal_uLong nBytes = aBmp.GetSizeBytes();
336 if ( nBytes < sal_uLong(0xFF00*nFac) )
337 rStrm << aBmp;
339 sal_uLong nEnd = rStrm.Tell();
340 // Item can not write with an overhead more than 64K or SfxMultiRecord
341 // will crash. Then prefer to forego on the bitmap, it is only
342 // important for the outliner and only for <= 5.0.
343 // When reading, the stream-operator makes note of the bitmap and the
344 // fact that there is none. This is now the case how it works with
345 // large bitmap created from another file format, which do not occupy a
346 // 64K chunk, but if a bitmap > 64K is used, the SvxNumBulletItem will
347 // have problem loading it, but does not crash.
349 if ( (nEnd-_nStart) > 0xFF00 )
350 rStrm.Seek( _nStart );
352 rStrm << static_cast<sal_Int32>(nWidth);
353 rStrm << nStart;
354 rStrm << nJustify;
355 rStrm << rtl::OUStringToOString(rtl::OUString(cSymbol), aFont.GetCharSet()).toChar();
356 rStrm << nScale;
358 // UNICODE: rStrm << aPrevText;
359 rStrm.WriteUniOrByteString(aPrevText, rStrm.GetStreamCharSet());
361 // UNICODE: rStrm << aFollowText;
362 rStrm.WriteUniOrByteString(aFollowText, rStrm.GetStreamCharSet());
364 return rStrm;
367 //------------------------------------------------------------------------
369 XubString SvxBulletItem::GetFullText() const
371 XubString aStr( aPrevText );
372 aStr += cSymbol;
373 aStr += aFollowText;
374 return aStr;
377 //------------------------------------------------------------------------
379 SfxItemPresentation SvxBulletItem::GetPresentation
381 SfxItemPresentation ePres,
382 SfxMapUnit /*eCoreUnit*/,
383 SfxMapUnit /*ePresUnit*/,
384 XubString& rText, const IntlWrapper *
385 ) const
387 SfxItemPresentation eRet = SFX_ITEM_PRESENTATION_NONE;
388 switch ( ePres )
390 case SFX_ITEM_PRESENTATION_NONE:
391 rText.Erase();
392 eRet = SFX_ITEM_PRESENTATION_NONE;
393 break;
395 case SFX_ITEM_PRESENTATION_NAMELESS:
396 case SFX_ITEM_PRESENTATION_COMPLETE:
397 rText = GetFullText();
398 eRet = SFX_ITEM_PRESENTATION_COMPLETE;
399 break;
400 default: ; //prevent warning
402 return eRet;
405 //------------------------------------------------------------------------
407 const GraphicObject& SvxBulletItem::GetGraphicObject() const
409 if( pGraphicObject )
410 return *pGraphicObject;
411 else
413 static const GraphicObject aDefaultObject;
414 return aDefaultObject;
418 //------------------------------------------------------------------------
420 void SvxBulletItem::SetGraphicObject( const GraphicObject& rGraphicObject )
422 if( ( GRAPHIC_NONE == rGraphicObject.GetType() ) || ( GRAPHIC_DEFAULT == rGraphicObject.GetType() ) )
424 if( pGraphicObject )
426 delete pGraphicObject;
427 pGraphicObject = NULL;
430 else
432 delete pGraphicObject;
433 pGraphicObject = new GraphicObject( rGraphicObject );
437 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */