xref: /aoo42x/main/svx/source/xoutdev/xtabdash.cxx (revision a68b38df)
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 #include <vcl/svapp.hxx>
40 
41 #include <tools/urlobj.hxx>
42 #include <vcl/virdev.hxx>
43 #include <vcl/window.hxx>
44 #include <svl/itemset.hxx>
45 #include <sfx2/docfile.hxx>
46 #include <svx/dialogs.hrc>
47 #include <svx/dialmgr.hxx>
48 #include <svx/xtable.hxx>
49 #include <svx/xpool.hxx>
50 #include <svx/xlineit0.hxx>
51 #include <svx/xlnclit.hxx>
52 #include <svx/xlnwtit.hxx>
53 #include <svx/xlndsit.hxx>
54 #include <svx/xflclit.hxx>
55 
56 #include <svx/svdorect.hxx>
57 #include <svx/svdopath.hxx>
58 #include <svx/svdmodel.hxx>
59 #include <svx/sdr/contact/objectcontactofobjlistpainter.hxx>
60 #include <svx/sdr/contact/displayinfo.hxx>
61 #include <basegfx/polygon/b2dpolygon.hxx>
62 
63 using namespace com::sun::star;
64 using namespace rtl;
65 
66 #define GLOBALOVERFLOW
67 
68 sal_Unicode const pszExtDash[] 	= {'s','o','d'};
69 char const aChckDash[]  = { 0x04, 0x00, 'S','O','D','L'};	// < 5.2
70 char const aChckDash0[] = { 0x04, 0x00, 'S','O','D','0'};	// = 5.2
71 char const aChckXML[]   = { '<', '?', 'x', 'm', 'l' };		// = 6.0
72 
73 // ----------------
74 // class XDashList
75 // ----------------
76 
77 class impXDashList
78 {
79 private:
80 	VirtualDevice*          mpVirtualDevice;
81 	SdrModel*				mpSdrModel;
82 	SdrObject*			    mpBackgroundObject;
83 	SdrObject*			    mpLineObject;
84 
85 public:
86     impXDashList(VirtualDevice* pV, SdrModel* pM, SdrObject* pB, SdrObject* pL)
87     :   mpVirtualDevice(pV),
88         mpSdrModel(pM),
89         mpBackgroundObject(pB),
90         mpLineObject(pL)
91     {}
92 
93     ~impXDashList()
94     {
95         delete mpVirtualDevice;
96         SdrObject::Free(mpBackgroundObject);
97         SdrObject::Free(mpLineObject);
98         delete mpSdrModel;
99     }
100 
101     VirtualDevice* getVirtualDevice() const { return mpVirtualDevice; }
102     SdrObject* getBackgroundObject() const { return mpBackgroundObject; }
103     SdrObject* getLineObject() const { return mpLineObject; }
104 };
105 
106 // to avoid rendering trouble (e.g. vcl renderer) and to get better AAed quality,
107 // use double prerender size
108 static bool bUseDoubleSize = true;
109 
110 void XDashList::impCreate()
111 {
112     if(!mpData)
113     {
114     	const Point aZero(0, 0);
115 		const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
116 
117         VirtualDevice* pVirDev = new VirtualDevice;
118 		OSL_ENSURE(0 != pVirDev, "XDashList: no VirtualDevice created!" );
119 		pVirDev->SetMapMode(MAP_100TH_MM);
120         const Size& rSize = rStyleSettings.GetListBoxPreviewDefaultPixelSize();
121 		const Size aSize(pVirDev->PixelToLogic(Size(
122             bUseDoubleSize ? rSize.Width() * 5 : rSize.Width() * 5 / 2,
123             bUseDoubleSize ? rSize.Height() * 2 : rSize.Height())));
124 		pVirDev->SetOutputSize(aSize);
125         pVirDev->SetDrawMode(rStyleSettings.GetHighContrastMode()
126             ? DRAWMODE_SETTINGSLINE | DRAWMODE_SETTINGSFILL | DRAWMODE_SETTINGSTEXT | DRAWMODE_SETTINGSGRADIENT
127             : DRAWMODE_DEFAULT);
128         pVirDev->SetBackground(rStyleSettings.GetFieldColor());
129 
130         SdrModel* pSdrModel = new SdrModel();
131 		OSL_ENSURE(0 != pSdrModel, "XDashList: no SdrModel created!" );
132 	    pSdrModel->GetItemPool().FreezeIdRanges();
133 
134         const Rectangle aBackgroundSize(aZero, aSize);
135         SdrObject* pBackgroundObject = new SdrRectObj(aBackgroundSize);
136 		OSL_ENSURE(0 != pBackgroundObject, "XDashList: no BackgroundObject created!" );
137     	pBackgroundObject->SetModel(pSdrModel);
138         pBackgroundObject->SetMergedItem(XFillStyleItem(XFILL_SOLID));
139         pBackgroundObject->SetMergedItem(XLineStyleItem(XLINE_NONE));
140         pBackgroundObject->SetMergedItem(XFillColorItem(String(), rStyleSettings.GetFieldColor()));
141 
142         const sal_uInt32 nHalfHeight(aSize.Height() / 2);
143         const basegfx::B2DPoint aStart(0, nHalfHeight);
144         const basegfx::B2DPoint aEnd(aSize.Width(), nHalfHeight);
145 	    basegfx::B2DPolygon aPolygon;
146 	    aPolygon.append(aStart);
147 	    aPolygon.append(aEnd);
148 	    SdrObject* pLineObject = new SdrPathObj(OBJ_LINE, basegfx::B2DPolyPolygon(aPolygon));
149 		OSL_ENSURE(0 != pLineObject, "XDashList: no LineObject created!" );
150     	pLineObject->SetModel(pSdrModel);
151         pLineObject->SetMergedItem(XLineStyleItem(XLINE_DASH));
152         pLineObject->SetMergedItem(XLineColorItem(String(), rStyleSettings.GetFieldTextColor()));
153 		const Size aLineWidth(pVirDev->PixelToLogic(Size(rStyleSettings.GetListBoxPreviewDefaultLineWidth(), 0)));
154         pLineObject->SetMergedItem(XLineWidthItem(bUseDoubleSize ? aLineWidth.getWidth() * 2 : aLineWidth.getWidth()));
155         mpData = new impXDashList(pVirDev, pSdrModel, pBackgroundObject, pLineObject);
156 		OSL_ENSURE(0 != mpData, "XDashList: data creation went wrong!" );
157     }
158 }
159 
160 void XDashList::impDestroy()
161 {
162     if(mpData)
163     {
164         delete mpData;
165         mpData = 0;
166     }
167 }
168 
169 XDashList::XDashList(const String& rPath, XOutdevItemPool* pInPool )
170 :   XPropertyList(rPath, pInPool ),
171     mpData(0),
172     maBitmapSolidLine(),
173     maStringSolidLine(),
174     maStringNoLine()
175 {
176 }
177 
178 XDashList::~XDashList()
179 {
180     impDestroy();
181 }
182 
183 XDashEntry* XDashList::Replace(XDashEntry* pEntry, long nIndex )
184 {
185 	return (XDashEntry*) XPropertyList::Replace(pEntry, nIndex);
186 }
187 
188 XDashEntry* XDashList::Remove(long nIndex)
189 {
190 	return (XDashEntry*) XPropertyList::Remove(nIndex, 0);
191 }
192 
193 XDashEntry* XDashList::GetDash(long nIndex) const
194 {
195 	return (XDashEntry*) XPropertyList::Get(nIndex, 0);
196 }
197 
198 sal_Bool XDashList::Load()
199 {
200 	if( mbListDirty )
201 	{
202 		mbListDirty = false;
203 
204 		INetURLObject aURL( maPath );
205 
206 		if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
207 		{
208 			DBG_ASSERT( !maPath.Len(), "invalid URL" );
209 			return sal_False;
210 		}
211 
212 		aURL.Append( maName );
213 
214 		if( !aURL.getExtension().getLength() )
215 			aURL.setExtension( rtl::OUString( pszExtDash, 3 ) );
216 
217 		uno::Reference< container::XNameContainer > xTable( SvxUnoXDashTable_createInstance( this ), uno::UNO_QUERY );
218 		return SvxXMLXTableImport::load( aURL.GetMainURL( INetURLObject::NO_DECODE ), xTable );
219 	}
220 	return( sal_False );
221 }
222 
223 sal_Bool XDashList::Save()
224 {
225 	INetURLObject aURL( maPath );
226 
227 	if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
228 	{
229 		DBG_ASSERT( !maPath.Len(), "invalid URL" );
230 		return sal_False;
231 	}
232 
233 	aURL.Append( maName );
234 
235 	if( !aURL.getExtension().getLength() )
236 		aURL.setExtension( rtl::OUString( pszExtDash, 3 ) );
237 
238 	uno::Reference< container::XNameContainer > xTable( SvxUnoXDashTable_createInstance( this ), uno::UNO_QUERY );
239 	return SvxXMLXTableExportComponent::save( aURL.GetMainURL( INetURLObject::NO_DECODE ), xTable );
240 }
241 
242 sal_Bool XDashList::Create()
243 {
244 	XubString aStr( SVX_RES( RID_SVXSTR_LINESTYLE ) );
245 	xub_StrLen nLen;
246 
247 	aStr.AppendAscii(" 1");
248 	nLen = aStr.Len() - 1;
249 	Insert(new XDashEntry(XDash(XDASH_RECT,1, 50,1, 50, 50),aStr));
250 	aStr.SetChar(nLen, sal_Unicode('2'));
251 	Insert(new XDashEntry(XDash(XDASH_RECT,1,500,1,500,500),aStr));
252 	aStr.SetChar(nLen, sal_Unicode('3'));
253 	Insert(new XDashEntry(XDash(XDASH_RECT,2, 50,3,250,120),aStr));
254 
255 	return( sal_True );
256 }
257 
258 Bitmap XDashList::ImpCreateBitmapForXDash(const XDash* pDash)
259 {
260     impCreate();
261     VirtualDevice* pVD = mpData->getVirtualDevice();
262     SdrObject* pLine = mpData->getLineObject();
263 
264     if(pDash)
265     {
266         pLine->SetMergedItem(XLineStyleItem(XLINE_DASH));
267         pLine->SetMergedItem(XLineDashItem(String(), *pDash));
268     }
269     else
270     {
271         pLine->SetMergedItem(XLineStyleItem(XLINE_SOLID));
272     }
273 
274     sdr::contact::SdrObjectVector aObjectVector;
275 	aObjectVector.push_back(mpData->getBackgroundObject());
276 	aObjectVector.push_back(pLine);
277 	sdr::contact::ObjectContactOfObjListPainter aPainter(*pVD, aObjectVector, 0);
278 	sdr::contact::DisplayInfo aDisplayInfo;
279 
280     pVD->Erase();
281 	aPainter.ProcessDisplay(aDisplayInfo);
282 
283     const Point aZero(0, 0);
284     Bitmap aResult(pVD->GetBitmap(aZero, pVD->GetOutputSize()));
285 
286     if(bUseDoubleSize)
287     {
288         const Size aCurrentSize(aResult.GetSizePixel());
289 
290         aResult.Scale(Size(aCurrentSize.Width() / 2, aCurrentSize.Height() / 2), BMP_SCALE_FASTESTINTERPOLATE);
291     }
292 
293     return aResult;
294 }
295 
296 Bitmap XDashList::CreateBitmapForUI( long nIndex )
297 {
298     const XDash& rDash = GetDash(nIndex)->GetDash();
299 
300     return ImpCreateBitmapForXDash(&rDash);
301 }
302 
303 Bitmap XDashList::GetBitmapForUISolidLine() const
304 {
305     if(maBitmapSolidLine.IsEmpty())
306     {
307         const_cast< XDashList* >(this)->maBitmapSolidLine = const_cast< XDashList* >(this)->ImpCreateBitmapForXDash(0);
308     }
309 
310     return maBitmapSolidLine;
311 }
312 
313 String XDashList::GetStringForUiSolidLine() const
314 {
315     if(!maStringSolidLine.Len())
316     {
317         const_cast< XDashList* >(this)->maStringSolidLine = String(ResId(RID_SVXSTR_SOLID, DIALOG_MGR()));
318     }
319 
320     return maStringSolidLine;
321 }
322 
323 String XDashList::GetStringForUiNoLine() const
324 {
325     if(!maStringNoLine.Len())
326     {
327         // formally was RID_SVXSTR_INVISIBLE, but tomake equal
328         // everywhere, use RID_SVXSTR_NONE
329         const_cast< XDashList* >(this)->maStringNoLine = String(ResId(RID_SVXSTR_NONE, DIALOG_MGR()));
330     }
331 
332     return maStringNoLine;
333 }
334 
335 //////////////////////////////////////////////////////////////////////////////
336 // eof
337