Bump version to 4.1-6
[LibreOffice.git] / editeng / source / items / bulitem.cxx
blobc8de24a1d3f9a41c2b9d65b77003ed8f560e10da
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 #include <editeng/bulletitem.hxx>
24 #include <editeng/editrids.hrc>
26 #include <tools/tenccvt.hxx>
28 #define BULITEM_VERSION ((sal_uInt16)2)
30 // -----------------------------------------------------------------------
32 TYPEINIT1(SvxBulletItem,SfxPoolItem);
34 // -----------------------------------------------------------------------
36 void SvxBulletItem::StoreFont( SvStream& rStream, const Font& rFont )
38 sal_uInt16 nTemp;
40 rStream << rFont.GetColor();
41 nTemp = (sal_uInt16)rFont.GetFamily(); rStream << nTemp;
43 nTemp = (sal_uInt16)GetSOStoreTextEncoding((rtl_TextEncoding)rFont.GetCharSet());
44 rStream << nTemp;
46 nTemp = (sal_uInt16)rFont.GetPitch(); rStream << nTemp;
47 nTemp = (sal_uInt16)rFont.GetAlign(); rStream << nTemp;
48 nTemp = (sal_uInt16)rFont.GetWeight(); rStream << nTemp;
49 nTemp = (sal_uInt16)rFont.GetUnderline(); rStream << nTemp;
50 nTemp = (sal_uInt16)rFont.GetStrikeout(); rStream << nTemp;
51 nTemp = (sal_uInt16)rFont.GetItalic(); rStream << nTemp;
53 // UNICODE: rStream << rFont.GetName();
54 rStream.WriteUniOrByteString(rFont.GetName(), rStream.GetStreamCharSet());
56 rStream << rFont.IsOutline();
57 rStream << rFont.IsShadow();
58 rStream << rFont.IsTransparent();
61 // -----------------------------------------------------------------------
63 Font SvxBulletItem::CreateFont( SvStream& rStream, sal_uInt16 nVer )
65 Font aFont;
66 Color aColor;
67 rStream >> aColor; aFont.SetColor( aColor );
68 sal_uInt16 nTemp;
69 rStream >> nTemp; aFont.SetFamily((FontFamily)nTemp);
71 rStream >> nTemp;
72 nTemp = (sal_uInt16)GetSOLoadTextEncoding((rtl_TextEncoding)nTemp);
73 aFont.SetCharSet((rtl_TextEncoding)nTemp);
75 rStream >> nTemp; aFont.SetPitch((FontPitch)nTemp);
76 rStream >> nTemp; aFont.SetAlign((FontAlign)nTemp);
77 rStream >> nTemp; aFont.SetWeight((FontWeight)nTemp);
78 rStream >> nTemp; aFont.SetUnderline((FontUnderline)nTemp);
79 rStream >> nTemp; aFont.SetStrikeout((FontStrikeout)nTemp);
80 rStream >> nTemp; aFont.SetItalic((FontItalic)nTemp);
82 // UNICODE: rStream >> aName; aFont.SetName( aName );
83 String aName = rStream.ReadUniOrByteString(rStream.GetStreamCharSet());
84 aFont.SetName( aName );
86 if( nVer == 1 )
88 //#fdo39428 SvStream no longer supports operator>>(long&)
89 sal_Int32 nHeight(0), nWidth(0);
90 rStream >> nHeight; rStream >> nWidth; Size aSize( nWidth, nHeight );
91 aFont.SetSize( aSize );
94 sal_Bool bTemp;
95 rStream >> bTemp; aFont.SetOutline( bTemp );
96 rStream >> bTemp; aFont.SetShadow( bTemp );
97 rStream >> bTemp; aFont.SetTransparent( bTemp );
98 return aFont;
102 // -----------------------------------------------------------------------
104 SvxBulletItem::SvxBulletItem( sal_uInt16 _nWhich ) : SfxPoolItem( _nWhich )
106 SetDefaultFont_Impl();
107 SetDefaults_Impl();
108 nValidMask = 0xFFFF;
111 // -----------------------------------------------------------------------
113 SvxBulletItem::SvxBulletItem( SvStream& rStrm, sal_uInt16 _nWhich ) :
114 SfxPoolItem( _nWhich ),
115 pGraphicObject( NULL )
117 rStrm >> nStyle;
119 if( nStyle != BS_BMP )
120 aFont = CreateFont( rStrm, BULITEM_VERSION );
121 else
123 // Safe Load with Test on empty Bitmap
124 Bitmap aBmp;
125 const sal_uInt32 nOldPos = rStrm.Tell();
126 // Ignore Errorcode when reading Bitmap,
127 // see comment in SvxBulletItem::Store()
128 sal_Bool bOldError = rStrm.GetError() ? sal_True : sal_False;
129 rStrm >> aBmp;
130 if ( !bOldError && rStrm.GetError() )
132 rStrm.ResetError();
135 if( aBmp.IsEmpty() )
137 rStrm.Seek( nOldPos );
138 nStyle = BS_NONE;
140 else
141 pGraphicObject = new GraphicObject( aBmp );
144 //#fdo39428 SvStream no longer supports operator>>(long&)
145 sal_Int32 nTmp(0);
146 rStrm >> nTmp; nWidth = nTmp;
147 rStrm >> nStart;
148 rStrm >> nJustify;
150 char cTmpSymbol;
151 rStrm >> cTmpSymbol;
152 //convert single byte to unicode
153 cSymbol = OUString(&cTmpSymbol, 1, aFont.GetCharSet()).toChar();
155 rStrm >> nScale;
157 // UNICODE: rStrm >> aPrevText;
158 aPrevText = rStrm.ReadUniOrByteString(rStrm.GetStreamCharSet());
160 // UNICODE: rStrm >> aFollowText;
161 aFollowText = rStrm.ReadUniOrByteString(rStrm.GetStreamCharSet());
163 nValidMask = 0xFFFF;
166 // -----------------------------------------------------------------------
168 SvxBulletItem::SvxBulletItem( const SvxBulletItem& rItem) : SfxPoolItem( rItem )
170 aFont = rItem.aFont;
171 pGraphicObject = ( rItem.pGraphicObject ? new GraphicObject( *rItem.pGraphicObject ) : NULL );
172 aPrevText = rItem.aPrevText;
173 aFollowText = rItem.aFollowText;
174 nStart = rItem.nStart;
175 nStyle = rItem.nStyle;
176 nWidth = rItem.nWidth;
177 nScale = rItem.nScale;
178 cSymbol = rItem.cSymbol;
179 nJustify = rItem.nJustify;
180 nValidMask = rItem.nValidMask;
183 // -----------------------------------------------------------------------
185 SvxBulletItem::~SvxBulletItem()
187 if( pGraphicObject )
188 delete pGraphicObject;
191 // -----------------------------------------------------------------------
193 SfxPoolItem* SvxBulletItem::Clone( SfxItemPool * /*pPool*/ ) const
195 return new SvxBulletItem( *this );
198 // -----------------------------------------------------------------------
200 SfxPoolItem* SvxBulletItem::Create( SvStream& rStrm, sal_uInt16 /*nVersion*/ ) const
202 return new SvxBulletItem( rStrm, Which() );
205 // -----------------------------------------------------------------------
207 void SvxBulletItem::SetDefaultFont_Impl()
209 aFont = OutputDevice::GetDefaultFont( DEFAULTFONT_FIXED, LANGUAGE_SYSTEM, 0 );
210 aFont.SetAlign( ALIGN_BOTTOM);
211 aFont.SetTransparent( sal_True );
214 // -----------------------------------------------------------------------
216 void SvxBulletItem::SetDefaults_Impl()
218 pGraphicObject = NULL;
219 nWidth = 1200; // 1.2cm
220 nStart = 1;
221 nStyle = BS_123;
222 nJustify = BJ_HLEFT | BJ_VCENTER;
223 cSymbol = sal_Unicode(' ');
224 nScale = 75;
227 // -----------------------------------------------------------------------
229 sal_uInt16 SvxBulletItem::GetVersion( sal_uInt16 /*nVersion*/ ) const
231 return BULITEM_VERSION;
234 // -----------------------------------------------------------------------
236 void SvxBulletItem::CopyValidProperties( const SvxBulletItem& rCopyFrom )
238 Font _aFont = GetFont();
239 Font aNewFont = rCopyFrom.GetFont();
240 if ( rCopyFrom.IsValid( VALID_FONTNAME ) )
242 _aFont.SetName( aNewFont.GetName() );
243 _aFont.SetFamily( aNewFont.GetFamily() );
244 _aFont.SetStyleName( aNewFont.GetStyleName() );
246 if ( rCopyFrom.IsValid( VALID_FONTCOLOR ) )
247 _aFont.SetColor( aNewFont.GetColor() );
248 if ( rCopyFrom.IsValid( VALID_SYMBOL ) )
249 SetSymbol( rCopyFrom.GetSymbol() );
250 if ( rCopyFrom.IsValid( VALID_BITMAP ) )
251 SetGraphicObject( rCopyFrom.GetGraphicObject() );
252 if ( rCopyFrom.IsValid( VALID_SCALE ) )
253 SetScale( rCopyFrom.GetScale() );
254 if ( rCopyFrom.IsValid( VALID_START ) )
255 SetStart( rCopyFrom.GetStart() );
256 if ( rCopyFrom.IsValid( VALID_STYLE ) )
257 SetStyle( rCopyFrom.GetStyle() );
258 if ( rCopyFrom.IsValid( VALID_PREVTEXT ) )
259 SetPrevText( rCopyFrom.GetPrevText() );
260 if ( rCopyFrom.IsValid( VALID_FOLLOWTEXT ) )
261 SetFollowText( rCopyFrom.GetFollowText() );
263 SetFont( _aFont );
267 // -----------------------------------------------------------------------
269 int SvxBulletItem::operator==( const SfxPoolItem& rItem ) const
271 DBG_ASSERT(rItem.ISA(SvxBulletItem),"operator==Types not matching");
272 const SvxBulletItem& rBullet = (const SvxBulletItem&)rItem;
273 // Compare with ValidMask, otherwise no put possible in a AttrSet if the
274 // item differs only in terms of the ValidMask from an existing one.
275 if( nValidMask != rBullet.nValidMask ||
276 nStyle != rBullet.nStyle ||
277 nScale != rBullet.nScale ||
278 nJustify != rBullet.nJustify ||
279 nWidth != rBullet.nWidth ||
280 nStart != rBullet.nStart ||
281 cSymbol != rBullet.cSymbol ||
282 aPrevText != rBullet.aPrevText ||
283 aFollowText != rBullet.aFollowText )
284 return 0;
286 if( ( nStyle != BS_BMP ) && ( aFont != rBullet.aFont ) )
287 return 0;
289 if( nStyle == BS_BMP )
291 if( ( pGraphicObject && !rBullet.pGraphicObject ) || ( !pGraphicObject && rBullet.pGraphicObject ) )
292 return 0;
294 if( ( pGraphicObject && rBullet.pGraphicObject ) &&
295 ( ( *pGraphicObject != *rBullet.pGraphicObject ) ||
296 ( pGraphicObject->GetPrefSize() != rBullet.pGraphicObject->GetPrefSize() ) ) )
298 return 0;
302 return 1;
305 // -----------------------------------------------------------------------
307 SvStream& SvxBulletItem::Store( SvStream& rStrm, sal_uInt16 /*nItemVersion*/ ) const
309 // Correction for empty bitmap
310 if( ( nStyle == BS_BMP ) &&
311 ( !pGraphicObject || ( GRAPHIC_NONE == pGraphicObject->GetType() ) || ( GRAPHIC_DEFAULT == pGraphicObject->GetType() ) ) )
313 if( pGraphicObject )
315 delete( const_cast< SvxBulletItem* >( this )->pGraphicObject );
316 const_cast< SvxBulletItem* >( this )->pGraphicObject = NULL;
319 const_cast< SvxBulletItem* >( this )->nStyle = BS_NONE;
322 rStrm << nStyle;
324 if( nStyle != BS_BMP )
325 StoreFont( rStrm, aFont );
326 else
328 sal_uLong _nStart = rStrm.Tell();
330 // Small preliminary estimate of the size ...
331 sal_uInt16 nFac = ( rStrm.GetCompressMode() != COMPRESSMODE_NONE ) ? 3 : 1;
332 const Bitmap aBmp( pGraphicObject->GetGraphic().GetBitmap() );
333 sal_uLong nBytes = aBmp.GetSizeBytes();
334 if ( nBytes < sal_uLong(0xFF00*nFac) )
335 rStrm << aBmp;
337 sal_uLong nEnd = rStrm.Tell();
338 // Item can not write with an overhead more than 64K or SfxMultiRecord
339 // will crash. Then prefer to forego on the bitmap, it is only
340 // important for the outliner and only for <= 5.0.
341 // When reading, the stream-operator makes note of the bitmap and the
342 // fact that there is none. This is now the case how it works with
343 // large bitmap created from another file format, which do not occupy a
344 // 64K chunk, but if a bitmap > 64K is used, the SvxNumBulletItem will
345 // have problem loading it, but does not crash.
347 if ( (nEnd-_nStart) > 0xFF00 )
348 rStrm.Seek( _nStart );
350 rStrm << static_cast<sal_Int32>(nWidth);
351 rStrm << nStart;
352 rStrm << nJustify;
353 rStrm << OUStringToOString(OUString(cSymbol), aFont.GetCharSet()).toChar();
354 rStrm << nScale;
356 // UNICODE: rStrm << aPrevText;
357 rStrm.WriteUniOrByteString(aPrevText, rStrm.GetStreamCharSet());
359 // UNICODE: rStrm << aFollowText;
360 rStrm.WriteUniOrByteString(aFollowText, rStrm.GetStreamCharSet());
362 return rStrm;
365 //------------------------------------------------------------------------
367 XubString SvxBulletItem::GetFullText() const
369 XubString aStr( aPrevText );
370 aStr += cSymbol;
371 aStr += aFollowText;
372 return aStr;
375 //------------------------------------------------------------------------
377 SfxItemPresentation SvxBulletItem::GetPresentation
379 SfxItemPresentation ePres,
380 SfxMapUnit /*eCoreUnit*/,
381 SfxMapUnit /*ePresUnit*/,
382 OUString& rText, const IntlWrapper *
383 ) const
385 SfxItemPresentation eRet = SFX_ITEM_PRESENTATION_NONE;
386 switch ( ePres )
388 case SFX_ITEM_PRESENTATION_NONE:
389 rText = OUString();
390 eRet = SFX_ITEM_PRESENTATION_NONE;
391 break;
393 case SFX_ITEM_PRESENTATION_NAMELESS:
394 case SFX_ITEM_PRESENTATION_COMPLETE:
395 rText = GetFullText();
396 eRet = SFX_ITEM_PRESENTATION_COMPLETE;
397 break;
398 default: ; //prevent warning
400 return eRet;
403 //------------------------------------------------------------------------
405 const GraphicObject& SvxBulletItem::GetGraphicObject() const
407 if( pGraphicObject )
408 return *pGraphicObject;
409 else
411 static const GraphicObject aDefaultObject;
412 return aDefaultObject;
416 //------------------------------------------------------------------------
418 void SvxBulletItem::SetGraphicObject( const GraphicObject& rGraphicObject )
420 if( ( GRAPHIC_NONE == rGraphicObject.GetType() ) || ( GRAPHIC_DEFAULT == rGraphicObject.GetType() ) )
422 if( pGraphicObject )
424 delete pGraphicObject;
425 pGraphicObject = NULL;
428 else
430 delete pGraphicObject;
431 pGraphicObject = new GraphicObject( rGraphicObject );
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */