xref: /aoo41x/main/svx/source/xoutdev/xattrbmp.cxx (revision 70d3707a)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_svx.hxx"
24 
25 #include <com/sun/star/awt/XBitmap.hpp>
26 #include <com/sun/star/graphic/XGraphic.hpp>
27 #include <tools/stream.hxx>
28 #include <vcl/window.hxx>
29 #include <vcl/virdev.hxx>
30 #include <vcl/bitmapex.hxx>
31 #include <toolkit/unohlp.hxx>
32 #include <svl/style.hxx>
33 #include <editeng/memberids.hrc>
34 #include <svx/dialogs.hrc>
35 #include "svx/xattr.hxx"
36 #include <svx/xtable.hxx>
37 #include <svx/xdef.hxx>
38 #include <svx/unomid.hxx>
39 #include <editeng/unoprnms.hxx>
40 #include <svx/unoapi.hxx>
41 #include <svx/svdmodel.hxx>
42 #include <com/sun/star/beans/PropertyValue.hpp>
43 #include <vcl/salbtype.hxx>
44 #include <vcl/bmpacc.hxx>
45 
46 using namespace ::com::sun::star;
47 
48 // -----------------------
49 // class XFillBitmapItem
50 // -----------------------
51 TYPEINIT1_AUTOFACTORY(XFillBitmapItem, NameOrIndex);
52 
53 //////////////////////////////////////////////////////////////////////////////
54 
55 XFillBitmapItem::XFillBitmapItem(long nIndex, const GraphicObject& rGraphicObject)
56 :   NameOrIndex(XATTR_FILLBITMAP, nIndex),
57 	maGraphicObject(rGraphicObject)
58 {
59 }
60 
61 //////////////////////////////////////////////////////////////////////////////
62 
63 XFillBitmapItem::XFillBitmapItem(const XubString& rName, const GraphicObject& rGraphicObject)
64 :   NameOrIndex(XATTR_FILLBITMAP, rName),
65 	maGraphicObject(rGraphicObject)
66 {
67 }
68 
69 //////////////////////////////////////////////////////////////////////////////
70 
71 XFillBitmapItem::XFillBitmapItem(const XFillBitmapItem& rItem)
72 :   NameOrIndex(rItem),
73 	maGraphicObject(rItem.maGraphicObject)
74 {
75 }
76 
77 //////////////////////////////////////////////////////////////////////////////
78 
79 Bitmap createHistorical8x8FromArray(const sal_uInt16* pArray, Color aColorPix, Color aColorBack)
80 {
81     BitmapPalette aPalette(2);
82 
83     aPalette[0] = BitmapColor(aColorBack);
84     aPalette[1] = BitmapColor(aColorPix);
85 
86     Bitmap aBitmap(Size(8, 8), 1, &aPalette);
87 	BitmapWriteAccess* pContent = aBitmap.AcquireWriteAccess();
88 
89     if(pContent)
90     {
91         for(sal_uInt16 a(0); a < 8; a++)
92         {
93             for(sal_uInt16 b(0); b < 8; b++)
94             {
95                 if(pArray[(a * 8) + b])
96                 {
97                     pContent->SetPixel(b, a, sal_uInt8(1));
98                 }
99                 else
100                 {
101                     pContent->SetPixel(b, a, sal_uInt8(0));
102                 }
103             }
104         }
105 
106         aBitmap.ReleaseAccess(pContent);
107     }
108 
109     return aBitmap;
110 }
111 
112 //////////////////////////////////////////////////////////////////////////////
113 
114 bool SVX_DLLPUBLIC isHistorical8x8(const BitmapEx& rBitmapEx, BitmapColor& o_rBack, BitmapColor& o_rFront)
115 {
116     if(!rBitmapEx.IsTransparent())
117     {
118         Bitmap aBitmap(rBitmapEx.GetBitmap());
119 
120         if(8 == aBitmap.GetSizePixel().Width() && 8 == aBitmap.GetSizePixel().Height())
121         {
122             if(2 == aBitmap.GetColorCount())
123             {
124                 BitmapReadAccess* pRead = aBitmap.AcquireReadAccess();
125 
126                 if(pRead)
127                 {
128                     if(pRead->HasPalette() && 2 == pRead->GetPaletteEntryCount())
129                     {
130                         const BitmapPalette& rPalette = pRead->GetPalette();
131 
132                         o_rBack = rPalette[1];
133                         o_rFront = rPalette[0];
134 
135                         return true;
136                     }
137                 }
138             }
139         }
140     }
141 
142     return false;
143 }
144 
145 //////////////////////////////////////////////////////////////////////////////
146 
147 XFillBitmapItem::XFillBitmapItem(SvStream& rIn, sal_uInt16 nVer)
148 :   NameOrIndex(XATTR_FILLBITMAP, rIn)
149 {
150 	if (!IsIndex())
151 	{
152 	    if(0 == nVer)
153 	    {
154 		    // Behandlung der alten Bitmaps
155 		    Bitmap aBmp;
156 
157 		    rIn >> aBmp;
158             maGraphicObject = Graphic(aBmp);
159 	    }
160 	    else if(1 == nVer)
161 	    {
162             enum XBitmapType
163             {
164                 XBITMAP_IMPORT,
165                 XBITMAP_8X8
166             };
167 
168             sal_Int16 iTmp;
169 
170             rIn >> iTmp; // former XBitmapStyle
171 		    rIn >> iTmp; // former XBitmapType
172 
173 		    if(XBITMAP_IMPORT == iTmp)
174 		    {
175 			    Bitmap aBmp;
176 
177                 rIn >> aBmp;
178                 maGraphicObject = Graphic(aBmp);
179 		    }
180 		    else if(XBITMAP_8X8 == iTmp)
181 		    {
182                 sal_uInt16 aArray[64];
183 
184 			    for(sal_uInt16 i(0); i < 64; i++)
185                 {
186 				    rIn >> aArray[i];
187                 }
188 
189 			    Color aColorPix;
190 			    Color aColorBack;
191 
192                 rIn >> aColorPix;
193 			    rIn >> aColorBack;
194 
195                 const Bitmap aBitmap(createHistorical8x8FromArray(aArray, aColorPix, aColorBack));
196 
197                 maGraphicObject = Graphic(aBitmap);
198             }
199 	    }
200         else if(2 == nVer)
201         {
202 		    BitmapEx aBmpEx;
203 
204 		    rIn >> aBmpEx;
205             maGraphicObject = Graphic(aBmpEx);
206         }
207     }
208 }
209 
210 //////////////////////////////////////////////////////////////////////////////
211 
212 XFillBitmapItem::XFillBitmapItem(SfxItemPool* /*pPool*/, const GraphicObject& rGraphicObject)
213 : 	NameOrIndex( XATTR_FILLBITMAP, -1),
214 	maGraphicObject(rGraphicObject)
215 {
216 }
217 
218 //////////////////////////////////////////////////////////////////////////////
219 
220 XFillBitmapItem::XFillBitmapItem(SfxItemPool* /*pPool*/)
221 :   NameOrIndex(XATTR_FILLBITMAP, -1),
222     maGraphicObject()
223 {
224 }
225 
226 //////////////////////////////////////////////////////////////////////////////
227 
228 SfxPoolItem* XFillBitmapItem::Clone(SfxItemPool* /*pPool*/) const
229 {
230 	return new XFillBitmapItem(*this);
231 }
232 
233 //////////////////////////////////////////////////////////////////////////////
234 
235 int XFillBitmapItem::operator==(const SfxPoolItem& rItem) const
236 {
237 	return (NameOrIndex::operator==(rItem)
238         && maGraphicObject == ((const XFillBitmapItem&)rItem).maGraphicObject);
239 }
240 
241 //////////////////////////////////////////////////////////////////////////////
242 
243 SfxPoolItem* XFillBitmapItem::Create(SvStream& rIn, sal_uInt16 nVer) const
244 {
245 	return new XFillBitmapItem( rIn, nVer );
246 }
247 
248 //////////////////////////////////////////////////////////////////////////////
249 
250 SvStream& XFillBitmapItem::Store( SvStream& rOut, sal_uInt16 nItemVersion ) const
251 {
252 	NameOrIndex::Store(rOut, nItemVersion);
253 
254 	if(!IsIndex())
255 	{
256         rOut << maGraphicObject.GetGraphic().GetBitmapEx();
257 	}
258 
259 	return rOut;
260 }
261 
262 //////////////////////////////////////////////////////////////////////////////
263 
264 const GraphicObject& XFillBitmapItem::GetGraphicObject() const
265 {
266     return maGraphicObject;
267 }
268 
269 //////////////////////////////////////////////////////////////////////////////
270 
271 void XFillBitmapItem::SetGraphicObject(const GraphicObject& rGraphicObject)
272 {
273     maGraphicObject = rGraphicObject;
274 }
275 
276 //////////////////////////////////////////////////////////////////////////////
277 
278 sal_uInt16 XFillBitmapItem::GetVersion(sal_uInt16 /*nFileFormatVersion*/) const
279 {
280 	// version three
281 	return(2);
282 }
283 
284 //////////////////////////////////////////////////////////////////////////////
285 
286 SfxItemPresentation XFillBitmapItem::GetPresentation(
287 	SfxItemPresentation ePres,
288 	SfxMapUnit /*eCoreUnit*/,
289 	SfxMapUnit /*ePresUnit*/,
290     XubString& rText,
291     const IntlWrapper*) const
292 {
293 	switch (ePres)
294 	{
295 		case SFX_ITEM_PRESENTATION_NONE:
296 			rText.Erase();
297 			return ePres;
298 		case SFX_ITEM_PRESENTATION_NAMELESS:
299 		case SFX_ITEM_PRESENTATION_COMPLETE:
300 			rText += GetName();
301 			return ePres;
302 		default:
303 			return SFX_ITEM_PRESENTATION_NONE;
304 	}
305 }
306 
307 //////////////////////////////////////////////////////////////////////////////
308 
309 sal_Bool XFillBitmapItem::QueryValue(::com::sun::star::uno::Any& rVal, sal_uInt8 nMemberId) const
310 {
311     nMemberId &= ~CONVERT_TWIPS;
312 
313     // needed for MID_NAME
314     ::rtl::OUString aApiName;
315     // needed for complete item (MID 0)
316     ::rtl::OUString aInternalName;
317 
318     ::rtl::OUString aURL;
319     ::com::sun::star::uno::Reference< ::com::sun::star::awt::XBitmap > xBmp;
320 
321 	if( nMemberId == MID_NAME )
322 	{
323  		SvxUnogetApiNameForItem( Which(), GetName(), aApiName );
324 	}
325     else if( nMemberId == 0  )
326     {
327         aInternalName = GetName();
328     }
329 
330     if( nMemberId == MID_GRAFURL ||
331         nMemberId == 0 )
332 	{
333         aURL = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(UNO_NAME_GRAPHOBJ_URLPREFIX));
334 		aURL += ::rtl::OUString::createFromAscii(GetGraphicObject().GetUniqueID().GetBuffer() );
335 	}
336     if( nMemberId == MID_BITMAP ||
337         nMemberId == 0  )
338 	{
339 		xBmp.set(VCLUnoHelper::CreateBitmap(GetGraphicObject().GetGraphic().GetBitmapEx()));
340 	}
341 
342     if( nMemberId == MID_NAME )
343 		rVal <<= aApiName;
344 	else if( nMemberId == MID_GRAFURL )
345 		rVal <<= aURL;
346 	else if( nMemberId == MID_BITMAP )
347 		rVal <<= xBmp;
348     else
349     {
350         // member-id 0 => complete item (e.g. for toolbars)
351         DBG_ASSERT( nMemberId == 0, "invalid member-id" );
352         uno::Sequence< beans::PropertyValue > aPropSeq( 3 );
353 
354         aPropSeq[0].Name  = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ));
355         aPropSeq[0].Value = uno::makeAny( aInternalName );
356         aPropSeq[1].Name  = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FillBitmapURL" ));
357         aPropSeq[1].Value = uno::makeAny( aURL );
358         aPropSeq[2].Name  = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Bitmap" ));
359         aPropSeq[2].Value = uno::makeAny( xBmp );
360 
361         rVal <<= aPropSeq;
362     }
363 
364 	return sal_True;
365 }
366 
367 //////////////////////////////////////////////////////////////////////////////
368 
369 sal_Bool XFillBitmapItem::PutValue( const ::com::sun::star::uno::Any& rVal, sal_uInt8 nMemberId )
370 {
371     nMemberId &= ~CONVERT_TWIPS;
372 
373     ::rtl::OUString aName;
374     ::rtl::OUString aURL;
375     ::com::sun::star::uno::Reference< ::com::sun::star::awt::XBitmap > xBmp;
376     ::com::sun::star::uno::Reference< ::com::sun::star::graphic::XGraphic > xGraphic;
377 
378     bool bSetName   = false;
379     bool bSetURL    = false;
380     bool bSetBitmap = false;
381 
382     if( nMemberId == MID_NAME )
383 		bSetName = (rVal >>= aName);
384     else if( nMemberId == MID_GRAFURL )
385 		bSetURL = (rVal >>= aURL);
386 	else if( nMemberId == MID_BITMAP )
387 	{
388         bSetBitmap = (rVal >>= xBmp);
389 		if ( !bSetBitmap )
390 			bSetBitmap = (rVal >>= xGraphic );
391 	}
392     else
393     {
394         DBG_ASSERT( nMemberId == 0, "invalid member-id" );
395         uno::Sequence< beans::PropertyValue >   aPropSeq;
396         if( rVal >>= aPropSeq )
397         {
398             for ( sal_Int32 n = 0; n < aPropSeq.getLength(); n++ )
399             {
400                 if( aPropSeq[n].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Name" )))
401                     bSetName = (aPropSeq[n].Value >>= aName);
402                 else if( aPropSeq[n].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "FillBitmapURL" )))
403                     bSetURL = (aPropSeq[n].Value >>= aURL);
404                 else if( aPropSeq[n].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Bitmap" )))
405                     bSetBitmap = (aPropSeq[n].Value >>= xBmp);
406             }
407         }
408     }
409 
410     if( bSetName )
411     {
412         SetName( aName );
413     }
414     if( bSetURL )
415     {
416         maGraphicObject  = GraphicObject::CreateGraphicObjectFromURL(aURL);
417     }
418     if( bSetBitmap )
419     {
420 		if(xBmp.is())
421 		{
422             maGraphicObject = Graphic(VCLUnoHelper::GetBitmap(xBmp));
423 		}
424 		else if(xGraphic.is())
425 		{
426 			maGraphicObject = Graphic(xGraphic);
427 		}
428     }
429 
430 	return (bSetName || bSetURL || bSetBitmap);
431 }
432 
433 //////////////////////////////////////////////////////////////////////////////
434 
435 sal_Bool XFillBitmapItem::CompareValueFunc( const NameOrIndex* p1, const NameOrIndex* p2 )
436 {
437     const GraphicObject& aGraphicObjectA(((XFillBitmapItem*)p1)->GetGraphicObject());
438     const GraphicObject& aGraphicObjectB(((XFillBitmapItem*)p2)->GetGraphicObject());
439 
440 	return aGraphicObjectA == aGraphicObjectB;
441 }
442 
443 //////////////////////////////////////////////////////////////////////////////
444 
445 XFillBitmapItem* XFillBitmapItem::checkForUniqueItem( SdrModel* pModel ) const
446 {
447 	if( pModel )
448 	{
449 		const String aUniqueName = NameOrIndex::CheckNamedItem(	this,
450 																XATTR_FILLBITMAP,
451 																&pModel->GetItemPool(),
452 																pModel->GetStyleSheetPool() ? &pModel->GetStyleSheetPool()->GetPool() : NULL,
453 																XFillBitmapItem::CompareValueFunc,
454 																RID_SVXSTR_BMP21,
455 																pModel->GetBitmapList() );
456 
457 		// if the given name is not valid, replace it!
458 		if( aUniqueName != GetName() )
459 		{
460 			return new XFillBitmapItem(aUniqueName, maGraphicObject);
461 		}
462 	}
463 
464 	return (XFillBitmapItem*)this;
465 }
466 
467 //////////////////////////////////////////////////////////////////////////////
468 // eof
469