xref: /aoo41x/main/svx/source/xoutdev/xtabgrdt.cxx (revision f6e50924)
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 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 // include ---------------------------------------------------------------
28 
29 #ifndef SVX_LIGHT
30 
31 #include <com/sun/star/container/XNameContainer.hpp>
32 #include "svx/XPropertyTable.hxx"
33 #include <unotools/ucbstreamhelper.hxx>
34 
35 #include "xmlxtexp.hxx"
36 #include "xmlxtimp.hxx"
37 
38 #endif
39 
40 #include <tools/urlobj.hxx>
41 #include <vcl/virdev.hxx>
42 #include <svl/itemset.hxx>
43 #include <sfx2/docfile.hxx>
44 #include <svx/dialogs.hrc>
45 #include <svx/dialmgr.hxx>
46 #include <svx/xtable.hxx>
47 #include <svx/xpool.hxx>
48 #include <svx/xfillit0.hxx>
49 #include <svx/xflgrit.hxx>
50 
51 #include <svx/svdorect.hxx>
52 #include <svx/svdmodel.hxx>
53 #include <svx/sdr/contact/objectcontactofobjlistpainter.hxx>
54 #include <svx/sdr/contact/displayinfo.hxx>
55 #include <vcl/svapp.hxx>
56 #include <svx/xlnclit.hxx>
57 #include <svx/xgrscit.hxx>
58 
59 #define GLOBALOVERFLOW
60 
61 using namespace com::sun::star;
62 using namespace rtl;
63 
64 sal_Unicode const pszExtGradient[]	= {'s','o','g'};
65 
66 char const aChckGradient[]  = { 0x04, 0x00, 'S','O','G','L'};	// < 5.2
67 char const aChckGradient0[] = { 0x04, 0x00, 'S','O','G','0'};	// = 5.2
68 char const aChckXML[]       = { '<', '?', 'x', 'm', 'l' };		// = 6.0
69 
70 // ---------------------
71 // class XGradientTable
72 // ---------------------
73 
74 /*************************************************************************
75 |*
76 |* XGradientTable::XGradientTable()
77 |*
78 *************************************************************************/
79 
80 XGradientTable::XGradientTable( const String& rPath,
81 							XOutdevItemPool* pInPool,
82 							sal_uInt16 nInitSize, sal_uInt16 nReSize ) :
83 				XPropertyTable( rPath, pInPool, nInitSize, nReSize)
84 {
85 	pBmpTable = new Table( nInitSize, nReSize );
86 }
87 
88 /************************************************************************/
89 
90 XGradientTable::~XGradientTable()
91 {
92 }
93 
94 /************************************************************************/
95 
96 XGradientEntry* XGradientTable::Replace(long nIndex, XGradientEntry* pEntry )
97 {
98 	return (XGradientEntry*) XPropertyTable::Replace(nIndex, pEntry);
99 }
100 
101 /************************************************************************/
102 
103 XGradientEntry* XGradientTable::Remove(long nIndex)
104 {
105 	return (XGradientEntry*) XPropertyTable::Remove(nIndex, 0);
106 }
107 
108 /************************************************************************/
109 
110 XGradientEntry* XGradientTable::GetGradient(long nIndex) const
111 {
112 	return (XGradientEntry*) XPropertyTable::Get(nIndex, 0);
113 }
114 
115 /************************************************************************/
116 
117 sal_Bool XGradientTable::Load()
118 {
119 	return( sal_False );
120 }
121 
122 /************************************************************************/
123 
124 sal_Bool XGradientTable::Save()
125 {
126 	return( sal_False );
127 }
128 
129 /************************************************************************/
130 
131 sal_Bool XGradientTable::Create()
132 {
133 	return( sal_False );
134 }
135 
136 /************************************************************************/
137 
138 sal_Bool XGradientTable::CreateBitmapsForUI()
139 {
140 	return( sal_False );
141 }
142 
143 /************************************************************************/
144 
145 Bitmap* XGradientTable::CreateBitmapForUI( long /*nIndex*/, sal_Bool /*bDelete*/)
146 {
147 	return( NULL );
148 }
149 
150 // --------------------
151 // class XGradientList
152 // --------------------
153 
154 class impXGradientList
155 {
156 private:
157 	VirtualDevice*          mpVirtualDevice;
158 	SdrModel*				mpSdrModel;
159 	SdrObject*			    mpBackgroundObject;
160 
161 public:
162     impXGradientList(VirtualDevice* pV, SdrModel* pM, SdrObject* pB)
163     :   mpVirtualDevice(pV),
164         mpSdrModel(pM),
165         mpBackgroundObject(pB)
166     {}
167 
168     ~impXGradientList()
169     {
170         delete mpVirtualDevice;
171         SdrObject::Free(mpBackgroundObject);
172         delete mpSdrModel;
173     }
174 
175     VirtualDevice* getVirtualDevice() const { return mpVirtualDevice; }
176     SdrObject* getBackgroundObject() const { return mpBackgroundObject; }
177 };
178 
179 void XGradientList::impCreate()
180 {
181     if(!mpData)
182     {
183     	const Point aZero(0, 0);
184 		const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
185 
186         VirtualDevice* pVirDev = new VirtualDevice;
187 		OSL_ENSURE(0 != pVirDev, "XGradientList: no VirtualDevice created!" );
188 		pVirDev->SetMapMode(MAP_100TH_MM);
189 		const Size aSize(pVirDev->PixelToLogic(Size(BITMAP_WIDTH, BITMAP_HEIGHT)));
190 		pVirDev->SetOutputSize(aSize);
191         pVirDev->SetDrawMode(rStyleSettings.GetHighContrastMode()
192             ? DRAWMODE_SETTINGSLINE | DRAWMODE_SETTINGSFILL | DRAWMODE_SETTINGSTEXT | DRAWMODE_SETTINGSGRADIENT
193             : DRAWMODE_DEFAULT);
194 
195 	    SdrModel* pSdrModel = new SdrModel();
196 		OSL_ENSURE(0 != pSdrModel, "XGradientList: no SdrModel created!" );
197 	    pSdrModel->GetItemPool().FreezeIdRanges();
198 
199         const Size aSinglePixel(pVirDev->PixelToLogic(Size(1, 1)));
200         const Rectangle aBackgroundSize(aZero, Size(aSize.getWidth() - aSinglePixel.getWidth(), aSize.getHeight() - aSinglePixel.getHeight()));
201         SdrObject* pBackgroundObject = new SdrRectObj(aBackgroundSize);
202 		OSL_ENSURE(0 != pBackgroundObject, "XGradientList: no BackgroundObject created!" );
203     	pBackgroundObject->SetModel(pSdrModel);
204         pBackgroundObject->SetMergedItem(XFillStyleItem(XFILL_GRADIENT));
205         pBackgroundObject->SetMergedItem(XLineStyleItem(XLINE_SOLID));
206         pBackgroundObject->SetMergedItem(XLineColorItem(String(), Color(COL_BLACK)));
207         pBackgroundObject->SetMergedItem(XGradientStepCountItem(sal_uInt16((BITMAP_WIDTH + BITMAP_HEIGHT) / 3)));
208 
209         mpData = new impXGradientList(pVirDev, pSdrModel, pBackgroundObject);
210 		OSL_ENSURE(0 != mpData, "XGradientList: data creation went wrong!" );
211     }
212 }
213 
214 void XGradientList::impDestroy()
215 {
216     if(mpData)
217     {
218         delete mpData;
219         mpData = 0;
220     }
221 }
222 
223 XGradientList::XGradientList( const String& rPath, XOutdevItemPool* pInPool, sal_uInt16 nInitSize, sal_uInt16 nReSize)
224 :   XPropertyList(rPath, pInPool, nInitSize, nReSize),
225     mpData(0)
226 {
227 	pBmpList = new List(nInitSize, nReSize);
228 }
229 
230 XGradientList::~XGradientList()
231 {
232     if(mpData)
233     {
234         delete mpData;
235         mpData = 0;
236     }
237 }
238 
239 XGradientEntry* XGradientList::Replace(XGradientEntry* pEntry, long nIndex )
240 {
241 	return( (XGradientEntry*) XPropertyList::Replace( pEntry, nIndex ) );
242 }
243 
244 XGradientEntry* XGradientList::Remove(long nIndex)
245 {
246 	return( (XGradientEntry*) XPropertyList::Remove( nIndex, 0 ) );
247 }
248 
249 XGradientEntry* XGradientList::GetGradient(long nIndex) const
250 {
251 	return( (XGradientEntry*) XPropertyList::Get( nIndex, 0 ) );
252 }
253 
254 sal_Bool XGradientList::Load()
255 {
256 	if( bListDirty )
257 	{
258 		bListDirty = sal_False;
259 
260 		INetURLObject aURL( aPath );
261 
262 		if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
263 		{
264 			DBG_ASSERT( !aPath.Len(), "invalid URL" );
265 			return sal_False;
266 		}
267 
268 		aURL.Append( aName );
269 
270 		if( !aURL.getExtension().getLength() )
271 			aURL.setExtension( rtl::OUString( pszExtGradient, 3 ) );
272 
273 		uno::Reference< container::XNameContainer > xTable( SvxUnoXGradientTable_createInstance( this ), uno::UNO_QUERY );
274 		return SvxXMLXTableImport::load( aURL.GetMainURL( INetURLObject::NO_DECODE ), xTable );
275 
276 	}
277 	return( sal_False );
278 }
279 
280 sal_Bool XGradientList::Save()
281 {
282 	INetURLObject aURL( aPath );
283 
284 	if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
285 	{
286 		DBG_ASSERT( !aPath.Len(), "invalid URL" );
287 		return sal_False;
288 	}
289 
290 	aURL.Append( aName );
291 
292 	if( !aURL.getExtension().getLength() )
293 		aURL.setExtension( rtl::OUString( pszExtGradient, 3 ) );
294 
295 	uno::Reference< container::XNameContainer > xTable( SvxUnoXGradientTable_createInstance( this ), uno::UNO_QUERY );
296 	return SvxXMLXTableExportComponent::save( aURL.GetMainURL( INetURLObject::NO_DECODE ), xTable );
297 }
298 
299 sal_Bool XGradientList::Create()
300 {
301 	XubString aStr( SVX_RES( RID_SVXSTR_GRADIENT ) );
302 	xub_StrLen nLen;
303 
304 	aStr.AppendAscii(" 1");
305 	nLen = aStr.Len() - 1;
306 	Insert(new XGradientEntry(XGradient(RGB_Color(COL_BLACK  ),RGB_Color(COL_WHITE	),XGRAD_LINEAR	  ,    0,10,10, 0,100,100),aStr));
307 	aStr.SetChar(nLen, sal_Unicode('2'));
308 	Insert(new XGradientEntry(XGradient(RGB_Color(COL_BLUE	 ),RGB_Color(COL_RED	),XGRAD_AXIAL	  ,  300,20,20,10,100,100),aStr));
309 	aStr.SetChar(nLen, sal_Unicode('3'));
310 	Insert(new XGradientEntry(XGradient(RGB_Color(COL_RED	 ),RGB_Color(COL_YELLOW ),XGRAD_RADIAL	  ,  600,30,30,20,100,100),aStr));
311 	aStr.SetChar(nLen, sal_Unicode('4'));
312 	Insert(new XGradientEntry(XGradient(RGB_Color(COL_YELLOW ),RGB_Color(COL_GREEN	),XGRAD_ELLIPTICAL,  900,40,40,30,100,100),aStr));
313 	aStr.SetChar(nLen, sal_Unicode('5'));
314 	Insert(new XGradientEntry(XGradient(RGB_Color(COL_GREEN  ),RGB_Color(COL_MAGENTA),XGRAD_SQUARE	  , 1200,50,50,40,100,100),aStr));
315 	aStr.SetChar(nLen, sal_Unicode('6'));
316 	Insert(new XGradientEntry(XGradient(RGB_Color(COL_MAGENTA),RGB_Color(COL_YELLOW ),XGRAD_RECT	  , 1900,60,60,50,100,100),aStr));
317 
318 	return( sal_True );
319 }
320 
321 sal_Bool XGradientList::CreateBitmapsForUI()
322 {
323     impCreate();
324 
325     for( long i = 0; i < Count(); i++)
326 	{
327 		Bitmap* pBmp = CreateBitmapForUI( i, sal_False );
328 		DBG_ASSERT( pBmp, "XGradientList: Bitmap(UI) konnte nicht erzeugt werden!" );
329 
330 		if( pBmp )
331 			pBmpList->Insert( pBmp, i );
332 	}
333 
334     impDestroy();
335 
336 	return( sal_False );
337 }
338 
339 Bitmap* XGradientList::CreateBitmapForUI( long nIndex, sal_Bool bDelete )
340 {
341     impCreate();
342     VirtualDevice* pVD = mpData->getVirtualDevice();
343     SdrObject* pBackgroundObject = mpData->getBackgroundObject();
344 
345 	const SfxItemSet& rItemSet = pBackgroundObject->GetMergedItemSet();
346     pBackgroundObject->SetMergedItem(XFillStyleItem(XFILL_GRADIENT));
347     pBackgroundObject->SetMergedItem(XFillGradientItem(rItemSet.GetPool(), GetGradient(nIndex)->GetGradient()));
348 
349     sdr::contact::SdrObjectVector aObjectVector;
350 	aObjectVector.push_back(pBackgroundObject);
351 	sdr::contact::ObjectContactOfObjListPainter aPainter(*pVD, aObjectVector, 0);
352 	sdr::contact::DisplayInfo aDisplayInfo;
353 
354 	aPainter.ProcessDisplay(aDisplayInfo);
355 
356     const Point aZero(0, 0);
357 	Bitmap* pBitmap = new Bitmap(pVD->GetBitmap(aZero, pVD->GetOutputSize()));
358 
359 	if(bDelete)
360 	{
361         impDestroy();
362 	}
363 
364 	return pBitmap;
365 }
366 
367 //////////////////////////////////////////////////////////////////////////////
368 // eof
369