1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/awt/XBitmap.hpp>
21 #include <com/sun/star/graphic/XGraphic.hpp>
22 #include <tools/stream.hxx>
23 #include <vcl/window.hxx>
24 #include <vcl/virdev.hxx>
25 #include <vcl/bitmapex.hxx>
26 #include <toolkit/unohlp.hxx>
27 #include <svl/style.hxx>
28 #include <editeng/memberids.hrc>
29 #include <svx/dialogs.hrc>
30 #include "svx/xattr.hxx"
31 #include <svx/xtable.hxx>
32 #include <svx/xdef.hxx>
33 #include <svx/unomid.hxx>
34 #include <editeng/unoprnms.hxx>
35 #include <svx/unoapi.hxx>
36 #include <svx/svdmodel.hxx>
37 #include <svx/xbitmap.hxx>
38 #include <com/sun/star/beans/PropertyValue.hpp>
39 #include <vcl/salbtype.hxx>
40 #include <vcl/bmpacc.hxx>
42 using namespace ::com::sun::star
;
44 /*************************************************************************
46 |* XOBitmap::XOBitmap( Bitmap aBitmap )
48 *************************************************************************/
50 XOBitmap::XOBitmap( const Bitmap
& rBmp
) :
51 eType ( XBITMAP_IMPORT
),
52 aGraphicObject ( rBmp
),
54 bGraphicDirty ( sal_False
)
58 /*************************************************************************
60 |* XOBitmap::XOBitmap( const XOBitmap& rXBmp )
62 *************************************************************************/
64 XOBitmap::XOBitmap( const XOBitmap
& rXBmp
) :
68 aGraphicObject
= rXBmp
.aGraphicObject
;
69 aArraySize
= rXBmp
.aArraySize
;
70 aPixelColor
= rXBmp
.aPixelColor
;
71 aBckgrColor
= rXBmp
.aBckgrColor
;
72 bGraphicDirty
= rXBmp
.bGraphicDirty
;
74 if( rXBmp
.pPixelArray
)
76 if( eType
== XBITMAP_8X8
)
78 pPixelArray
= new sal_uInt16
[ 64 ];
80 for( sal_uInt16 i
= 0; i
< 64; i
++ )
81 *( pPixelArray
+ i
) = *( rXBmp
.pPixelArray
+ i
);
86 /*************************************************************************
88 |* XOBitmap::XOBitmap( Bitmap aBitmap )
90 *************************************************************************/
94 delete [] pPixelArray
;
97 /*************************************************************************
99 |* XOBitmap& XOBitmap::operator=( const XOBitmap& rXBmp )
101 *************************************************************************/
103 XOBitmap
& XOBitmap::operator=( const XOBitmap
& rXBmp
)
106 aGraphicObject
= rXBmp
.aGraphicObject
;
107 aArraySize
= rXBmp
.aArraySize
;
108 aPixelColor
= rXBmp
.aPixelColor
;
109 aBckgrColor
= rXBmp
.aBckgrColor
;
110 bGraphicDirty
= rXBmp
.bGraphicDirty
;
112 if( rXBmp
.pPixelArray
)
114 if( eType
== XBITMAP_8X8
)
116 pPixelArray
= new sal_uInt16
[ 64 ];
118 for( sal_uInt16 i
= 0; i
< 64; i
++ )
119 *( pPixelArray
+ i
) = *( rXBmp
.pPixelArray
+ i
);
125 /*************************************************************************
127 |* int XOBitmap::operator==( const XOBitmap& rXOBitmap ) const
129 *************************************************************************/
131 int XOBitmap::operator==( const XOBitmap
& rXOBitmap
) const
133 if( eType
!= rXOBitmap
.eType
||
134 aGraphicObject
!= rXOBitmap
.aGraphicObject
||
135 aArraySize
!= rXOBitmap
.aArraySize
||
136 aPixelColor
!= rXOBitmap
.aPixelColor
||
137 aBckgrColor
!= rXOBitmap
.aBckgrColor
||
138 bGraphicDirty
!= rXOBitmap
.bGraphicDirty
)
143 if( pPixelArray
&& rXOBitmap
.pPixelArray
)
145 sal_uInt16 nCount
= (sal_uInt16
) ( aArraySize
.Width() * aArraySize
.Height() );
146 for( sal_uInt16 i
= 0; i
< nCount
; i
++ )
148 if( *( pPixelArray
+ i
) != *( rXOBitmap
.pPixelArray
+ i
) )
155 /*************************************************************************
157 |* Bitmap XOBitmap::GetBitmap()
159 *************************************************************************/
161 Bitmap
XOBitmap::GetBitmap() const
163 return GetGraphicObject().GetGraphic().GetBitmap();
166 /*************************************************************************
168 |* Bitmap XOBitmap::GetGraphicObject()
170 *************************************************************************/
172 const GraphicObject
& XOBitmap::GetGraphicObject() const
175 ( (XOBitmap
*) this )->Array2Bitmap();
177 return aGraphicObject
;
180 /*************************************************************************
182 |* void XOBitmap::Bitmap2Array()
184 |* Beschreibung Umwandlung der Bitmap in Array, Hinter- u.
187 *************************************************************************/
189 void XOBitmap::Bitmap2Array()
192 bool bPixelColor
= false;
193 const Bitmap
aBitmap( GetBitmap() );
194 const sal_uInt16 nLines
= 8; // von Type abhaengig
197 pPixelArray
= new sal_uInt16
[ nLines
* nLines
];
199 aVD
.SetOutputSizePixel( aBitmap
.GetSizePixel() );
200 aVD
.DrawBitmap( Point(), aBitmap
);
201 aPixelColor
= aBckgrColor
= aVD
.GetPixel( Point() );
203 // Aufbau des Arrays und Ermittlung der Vorder-, bzw.
205 for( sal_uInt16 i
= 0; i
< nLines
; i
++ )
207 for( sal_uInt16 j
= 0; j
< nLines
; j
++ )
209 if ( aVD
.GetPixel( Point( j
, i
) ) == aBckgrColor
)
210 *( pPixelArray
+ j
+ i
* nLines
) = 0;
213 *( pPixelArray
+ j
+ i
* nLines
) = 1;
216 aPixelColor
= aVD
.GetPixel( Point( j
, i
) );
224 /*************************************************************************
226 |* void XOBitmap::Array2Bitmap()
228 |* Beschreibung Umwandlung des Arrays, Hinter- u.
229 |* Vordergrundfarbe in eine Bitmap
231 *************************************************************************/
233 void XOBitmap::Array2Bitmap()
236 sal_uInt16 nLines
= 8; // von Type abhaengig
241 aVD
.SetOutputSizePixel( Size( nLines
, nLines
) );
244 for( sal_uInt16 i
= 0; i
< nLines
; i
++ )
246 for( sal_uInt16 j
= 0; j
< nLines
; j
++ )
248 if( *( pPixelArray
+ j
+ i
* nLines
) == 0 )
249 aVD
.DrawPixel( Point( j
, i
), aBckgrColor
);
251 aVD
.DrawPixel( Point( j
, i
), aPixelColor
);
255 aGraphicObject
= GraphicObject( aVD
.GetBitmap( Point(), Size( nLines
, nLines
) ) );
256 bGraphicDirty
= sal_False
;
259 // -----------------------
260 // class XFillBitmapItem
261 // -----------------------
262 TYPEINIT1_AUTOFACTORY(XFillBitmapItem
, NameOrIndex
);
264 //////////////////////////////////////////////////////////////////////////////
266 XFillBitmapItem::XFillBitmapItem(const XubString
& rName
, const GraphicObject
& rGraphicObject
)
267 : NameOrIndex(XATTR_FILLBITMAP
, rName
),
268 maGraphicObject(rGraphicObject
)
272 //////////////////////////////////////////////////////////////////////////////
274 XFillBitmapItem::XFillBitmapItem(const XFillBitmapItem
& rItem
)
275 : NameOrIndex(rItem
),
276 maGraphicObject(rItem
.maGraphicObject
)
280 //////////////////////////////////////////////////////////////////////////////
282 Bitmap
createHistorical8x8FromArray(const sal_uInt16
* pArray
, Color aColorPix
, Color aColorBack
)
284 BitmapPalette
aPalette(2);
286 aPalette
[0] = BitmapColor(aColorBack
);
287 aPalette
[1] = BitmapColor(aColorPix
);
289 Bitmap
aBitmap(Size(8, 8), 1, &aPalette
);
290 BitmapWriteAccess
* pContent
= aBitmap
.AcquireWriteAccess();
294 for(sal_uInt16
a(0); a
< 8; a
++)
296 for(sal_uInt16
b(0); b
< 8; b
++)
298 if(pArray
[(a
* 8) + b
])
300 pContent
->SetPixelIndex(b
, a
, 1);
304 pContent
->SetPixelIndex(b
, a
, 0);
309 aBitmap
.ReleaseAccess(pContent
);
315 //////////////////////////////////////////////////////////////////////////////
317 bool SVX_DLLPUBLIC
isHistorical8x8(const BitmapEx
& rBitmapEx
, BitmapColor
& o_rBack
, BitmapColor
& o_rFront
)
319 if(!rBitmapEx
.IsTransparent())
321 Bitmap
aBitmap(rBitmapEx
.GetBitmap());
323 if(8 == aBitmap
.GetSizePixel().Width() && 8 == aBitmap
.GetSizePixel().Height())
325 if(2 == aBitmap
.GetColorCount())
327 BitmapReadAccess
* pRead
= aBitmap
.AcquireReadAccess();
331 if(pRead
->HasPalette() && 2 == pRead
->GetPaletteEntryCount())
333 const BitmapPalette
& rPalette
= pRead
->GetPalette();
335 o_rBack
= rPalette
[1];
336 o_rFront
= rPalette
[0];
348 //////////////////////////////////////////////////////////////////////////////
350 XFillBitmapItem::XFillBitmapItem(SvStream
& rIn
, sal_uInt16 nVer
)
351 : NameOrIndex(XATTR_FILLBITMAP
, rIn
)
357 // Behandlung der alten Bitmaps
361 maGraphicObject
= Graphic(aBmp
);
367 rIn
>> iTmp
; // former XBitmapStyle
368 rIn
>> iTmp
; // former XBitmapType
370 if(XBITMAP_IMPORT
== iTmp
)
375 maGraphicObject
= Graphic(aBmp
);
377 else if(XBITMAP_8X8
== iTmp
)
379 sal_uInt16 aArray
[64];
381 for(sal_uInt16
i(0); i
< 64; i
++)
392 const Bitmap
aBitmap(createHistorical8x8FromArray(aArray
, aColorPix
, aColorBack
));
394 maGraphicObject
= Graphic(aBitmap
);
402 maGraphicObject
= Graphic(aBmpEx
);
407 //////////////////////////////////////////////////////////////////////////////
409 XFillBitmapItem::XFillBitmapItem(SfxItemPool
* /*pPool*/, const GraphicObject
& rGraphicObject
)
410 : NameOrIndex( XATTR_FILLBITMAP
, -1),
411 maGraphicObject(rGraphicObject
)
415 //////////////////////////////////////////////////////////////////////////////
417 SfxPoolItem
* XFillBitmapItem::Clone(SfxItemPool
* /*pPool*/) const
419 return new XFillBitmapItem(*this);
422 //////////////////////////////////////////////////////////////////////////////
424 int XFillBitmapItem::operator==(const SfxPoolItem
& rItem
) const
426 return (NameOrIndex::operator==(rItem
)
427 && maGraphicObject
== ((const XFillBitmapItem
&)rItem
).maGraphicObject
);
430 //////////////////////////////////////////////////////////////////////////////
432 SfxPoolItem
* XFillBitmapItem::Create(SvStream
& rIn
, sal_uInt16 nVer
) const
434 return new XFillBitmapItem( rIn
, nVer
);
437 //////////////////////////////////////////////////////////////////////////////
439 SvStream
& XFillBitmapItem::Store( SvStream
& rOut
, sal_uInt16 nItemVersion
) const
441 NameOrIndex::Store(rOut
, nItemVersion
);
445 rOut
<< maGraphicObject
.GetGraphic().GetBitmapEx();
451 //////////////////////////////////////////////////////////////////////////////
453 const GraphicObject
& XFillBitmapItem::GetGraphicObject() const
455 return maGraphicObject
;
458 //////////////////////////////////////////////////////////////////////////////
460 void XFillBitmapItem::SetGraphicObject(const GraphicObject
& rGraphicObject
)
462 maGraphicObject
= rGraphicObject
;
465 //////////////////////////////////////////////////////////////////////////////
467 sal_uInt16
XFillBitmapItem::GetVersion(sal_uInt16
/*nFileFormatVersion*/) const
473 //////////////////////////////////////////////////////////////////////////////
475 SfxItemPresentation
XFillBitmapItem::GetPresentation(
476 SfxItemPresentation ePres
,
477 SfxMapUnit
/*eCoreUnit*/,
478 SfxMapUnit
/*ePresUnit*/,
480 const IntlWrapper
*) const
484 case SFX_ITEM_PRESENTATION_NONE
:
487 case SFX_ITEM_PRESENTATION_NAMELESS
:
488 case SFX_ITEM_PRESENTATION_COMPLETE
:
492 return SFX_ITEM_PRESENTATION_NONE
;
496 //////////////////////////////////////////////////////////////////////////////
498 bool XFillBitmapItem::QueryValue(::com::sun::star::uno::Any
& rVal
, sal_uInt8 nMemberId
) const
500 nMemberId
&= ~CONVERT_TWIPS
;
502 // needed for MID_NAME
504 // needed for complete item (MID 0)
505 OUString aInternalName
;
508 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XBitmap
> xBmp
;
510 if( nMemberId
== MID_NAME
)
512 aApiName
= SvxUnogetApiNameForItem(Which(), GetName());
514 else if( nMemberId
== 0 )
516 aInternalName
= GetName();
519 if( nMemberId
== MID_GRAFURL
||
523 UNO_NAME_GRAPHOBJ_URLPREFIX
);
524 aURL
+= OStringToOUString(
525 GetGraphicObject().GetUniqueID(),
526 RTL_TEXTENCODING_ASCII_US
);
528 if( nMemberId
== MID_BITMAP
||
531 xBmp
.set(VCLUnoHelper::CreateBitmap(GetGraphicObject().GetGraphic().GetBitmapEx()));
534 if( nMemberId
== MID_NAME
)
536 else if( nMemberId
== MID_GRAFURL
)
538 else if( nMemberId
== MID_BITMAP
)
542 // member-id 0 => complete item (e.g. for toolbars)
543 DBG_ASSERT( nMemberId
== 0, "invalid member-id" );
544 uno::Sequence
< beans::PropertyValue
> aPropSeq( 3 );
546 aPropSeq
[0].Name
= OUString( "Name" );
547 aPropSeq
[0].Value
= uno::makeAny( aInternalName
);
548 aPropSeq
[1].Name
= OUString( "FillBitmapURL" );
549 aPropSeq
[1].Value
= uno::makeAny( aURL
);
550 aPropSeq
[2].Name
= OUString( "Bitmap" );
551 aPropSeq
[2].Value
= uno::makeAny( xBmp
);
559 //////////////////////////////////////////////////////////////////////////////
561 bool XFillBitmapItem::PutValue( const ::com::sun::star::uno::Any
& rVal
, sal_uInt8 nMemberId
)
563 nMemberId
&= ~CONVERT_TWIPS
;
567 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XBitmap
> xBmp
;
568 ::com::sun::star::uno::Reference
< ::com::sun::star::graphic::XGraphic
> xGraphic
;
570 bool bSetName
= false;
571 bool bSetURL
= false;
572 bool bSetBitmap
= false;
574 if( nMemberId
== MID_NAME
)
575 bSetName
= (rVal
>>= aName
);
576 else if( nMemberId
== MID_GRAFURL
)
577 bSetURL
= (rVal
>>= aURL
);
578 else if( nMemberId
== MID_BITMAP
)
580 bSetBitmap
= (rVal
>>= xBmp
);
582 bSetBitmap
= (rVal
>>= xGraphic
);
586 DBG_ASSERT( nMemberId
== 0, "invalid member-id" );
587 uno::Sequence
< beans::PropertyValue
> aPropSeq
;
588 if( rVal
>>= aPropSeq
)
590 for ( sal_Int32 n
= 0; n
< aPropSeq
.getLength(); n
++ )
592 if ( aPropSeq
[n
].Name
== "Name" )
593 bSetName
= (aPropSeq
[n
].Value
>>= aName
);
594 else if ( aPropSeq
[n
].Name
== "FillBitmapURL" )
595 bSetURL
= (aPropSeq
[n
].Value
>>= aURL
);
596 else if ( aPropSeq
[n
].Name
== "Bitmap" )
597 bSetBitmap
= (aPropSeq
[n
].Value
>>= xBmp
);
608 GraphicObject aGraphicObject
= GraphicObject::CreateGraphicObjectFromURL(aURL
);
609 if( aGraphicObject
.GetType() != GRAPHIC_NONE
)
610 maGraphicObject
= aGraphicObject
;
616 maGraphicObject
= Graphic(VCLUnoHelper::GetBitmap(xBmp
));
618 else if(xGraphic
.is())
620 maGraphicObject
= Graphic(xGraphic
);
624 return (bSetName
|| bSetURL
|| bSetBitmap
);
627 //////////////////////////////////////////////////////////////////////////////
629 sal_Bool
XFillBitmapItem::CompareValueFunc( const NameOrIndex
* p1
, const NameOrIndex
* p2
)
631 const GraphicObject
& aGraphicObjectA(((XFillBitmapItem
*)p1
)->GetGraphicObject());
632 const GraphicObject
& aGraphicObjectB(((XFillBitmapItem
*)p2
)->GetGraphicObject());
634 return aGraphicObjectA
== aGraphicObjectB
;
637 //////////////////////////////////////////////////////////////////////////////
639 XFillBitmapItem
* XFillBitmapItem::checkForUniqueItem( SdrModel
* pModel
) const
643 const String aUniqueName
= NameOrIndex::CheckNamedItem(
644 this, XATTR_FILLBITMAP
, &pModel
->GetItemPool(),
645 pModel
->GetStyleSheetPool() ? &pModel
->GetStyleSheetPool()->GetPool() : NULL
,
646 XFillBitmapItem::CompareValueFunc
, RID_SVXSTR_BMP21
,
647 pModel
->GetPropertyList( XBITMAP_LIST
) );
649 // if the given name is not valid, replace it!
650 if( aUniqueName
!= GetName() )
652 return new XFillBitmapItem(aUniqueName
, maGraphicObject
);
656 return (XFillBitmapItem
*)this;
659 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */