xref: /aoo42x/main/svx/source/xoutdev/xtabdash.cxx (revision 81e4f59a)
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 aSize(pVirDev->PixelToLogic(Size(
121             bUseDoubleSize ? getUiBitmapWidth() * 5 : getUiBitmapWidth() * 5 / 2,
122             bUseDoubleSize ? getUiBitmapHeight() * 2 : getUiBitmapHeight())));
123 		pVirDev->SetOutputSize(aSize);
124         pVirDev->SetDrawMode(rStyleSettings.GetHighContrastMode()
125             ? DRAWMODE_SETTINGSLINE | DRAWMODE_SETTINGSFILL | DRAWMODE_SETTINGSTEXT | DRAWMODE_SETTINGSGRADIENT
126             : DRAWMODE_DEFAULT);
127         pVirDev->SetBackground(rStyleSettings.GetFieldColor());
128 
129         SdrModel* pSdrModel = new SdrModel();
130 		OSL_ENSURE(0 != pSdrModel, "XDashList: no SdrModel created!" );
131 	    pSdrModel->GetItemPool().FreezeIdRanges();
132 
133         const Rectangle aBackgroundSize(aZero, aSize);
134         SdrObject* pBackgroundObject = new SdrRectObj(aBackgroundSize);
135 		OSL_ENSURE(0 != pBackgroundObject, "XDashList: no BackgroundObject created!" );
136     	pBackgroundObject->SetModel(pSdrModel);
137         pBackgroundObject->SetMergedItem(XFillStyleItem(XFILL_SOLID));
138         pBackgroundObject->SetMergedItem(XLineStyleItem(XLINE_NONE));
139         pBackgroundObject->SetMergedItem(XFillColorItem(String(), rStyleSettings.GetFieldColor()));
140 
141         const sal_uInt32 nHalfHeight(aSize.Height() / 2);
142         const basegfx::B2DPoint aStart(0, nHalfHeight);
143         const basegfx::B2DPoint aEnd(aSize.Width(), nHalfHeight);
144 	    basegfx::B2DPolygon aPolygon;
145 	    aPolygon.append(aStart);
146 	    aPolygon.append(aEnd);
147 	    SdrObject* pLineObject = new SdrPathObj(OBJ_LINE, basegfx::B2DPolyPolygon(aPolygon));
148 		OSL_ENSURE(0 != pLineObject, "XDashList: no LineObject created!" );
149     	pLineObject->SetModel(pSdrModel);
150         pLineObject->SetMergedItem(XLineStyleItem(XLINE_DASH));
151         pLineObject->SetMergedItem(XLineColorItem(String(), rStyleSettings.GetFieldTextColor()));
152 		const Size aLineWidth(pVirDev->PixelToLogic(Size(getUiBitmapLineWidth(), 0)));
153         pLineObject->SetMergedItem(XLineWidthItem(bUseDoubleSize ? aLineWidth.getWidth() * 2 : aLineWidth.getWidth()));
154         mpData = new impXDashList(pVirDev, pSdrModel, pBackgroundObject, pLineObject);
155 		OSL_ENSURE(0 != mpData, "XDashList: data creation went wrong!" );
156     }
157 }
158 
159 void XDashList::impDestroy()
160 {
161     if(mpData)
162     {
163         delete mpData;
164         mpData = 0;
165     }
166 }
167 
168 XDashList::XDashList(const String& rPath, XOutdevItemPool* pInPool )
169 :   XPropertyList(rPath, pInPool ),
170     mpData(0),
171     maBitmapSolidLine(),
172     maStringSolidLine(),
173     maStringNoLine()
174 {
175 }
176 
177 XDashList::~XDashList()
178 {
179     impDestroy();
180 }
181 
182 XDashEntry* XDashList::Replace(XDashEntry* pEntry, long nIndex )
183 {
184 	return (XDashEntry*) XPropertyList::Replace(pEntry, nIndex);
185 }
186 
187 XDashEntry* XDashList::Remove(long nIndex)
188 {
189 	return (XDashEntry*) XPropertyList::Remove(nIndex, 0);
190 }
191 
192 XDashEntry* XDashList::GetDash(long nIndex) const
193 {
194 	return (XDashEntry*) XPropertyList::Get(nIndex, 0);
195 }
196 
197 sal_Bool XDashList::Load()
198 {
199 	if( mbListDirty )
200 	{
201 		mbListDirty = false;
202 
203 		INetURLObject aURL( maPath );
204 
205 		if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
206 		{
207 			DBG_ASSERT( !maPath.Len(), "invalid URL" );
208 			return sal_False;
209 		}
210 
211 		aURL.Append( maName );
212 
213 		if( !aURL.getExtension().getLength() )
214 			aURL.setExtension( rtl::OUString( pszExtDash, 3 ) );
215 
216 		uno::Reference< container::XNameContainer > xTable( SvxUnoXDashTable_createInstance( this ), uno::UNO_QUERY );
217 		return SvxXMLXTableImport::load( aURL.GetMainURL( INetURLObject::NO_DECODE ), xTable );
218 	}
219 	return( sal_False );
220 }
221 
222 sal_Bool XDashList::Save()
223 {
224 	INetURLObject aURL( maPath );
225 
226 	if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
227 	{
228 		DBG_ASSERT( !maPath.Len(), "invalid URL" );
229 		return sal_False;
230 	}
231 
232 	aURL.Append( maName );
233 
234 	if( !aURL.getExtension().getLength() )
235 		aURL.setExtension( rtl::OUString( pszExtDash, 3 ) );
236 
237 	uno::Reference< container::XNameContainer > xTable( SvxUnoXDashTable_createInstance( this ), uno::UNO_QUERY );
238 	return SvxXMLXTableExportComponent::save( aURL.GetMainURL( INetURLObject::NO_DECODE ), xTable );
239 }
240 
241 sal_Bool XDashList::Create()
242 {
243 	XubString aStr( SVX_RES( RID_SVXSTR_LINESTYLE ) );
244 	xub_StrLen nLen;
245 
246 	aStr.AppendAscii(" 1");
247 	nLen = aStr.Len() - 1;
248 	Insert(new XDashEntry(XDash(XDASH_RECT,1, 50,1, 50, 50),aStr));
249 	aStr.SetChar(nLen, sal_Unicode('2'));
250 	Insert(new XDashEntry(XDash(XDASH_RECT,1,500,1,500,500),aStr));
251 	aStr.SetChar(nLen, sal_Unicode('3'));
252 	Insert(new XDashEntry(XDash(XDASH_RECT,2, 50,3,250,120),aStr));
253 
254 	return( sal_True );
255 }
256 
257 Bitmap XDashList::ImpCreateBitmapForXDash(const XDash* pDash)
258 {
259     impCreate();
260     VirtualDevice* pVD = mpData->getVirtualDevice();
261     SdrObject* pLine = mpData->getLineObject();
262 
263     if(pDash)
264     {
265         pLine->SetMergedItem(XLineStyleItem(XLINE_DASH));
266         pLine->SetMergedItem(XLineDashItem(String(), *pDash));
267     }
268     else
269     {
270         pLine->SetMergedItem(XLineStyleItem(XLINE_SOLID));
271     }
272 
273     sdr::contact::SdrObjectVector aObjectVector;
274 	aObjectVector.push_back(mpData->getBackgroundObject());
275 	aObjectVector.push_back(pLine);
276 	sdr::contact::ObjectContactOfObjListPainter aPainter(*pVD, aObjectVector, 0);
277 	sdr::contact::DisplayInfo aDisplayInfo;
278 
279     pVD->Erase();
280 	aPainter.ProcessDisplay(aDisplayInfo);
281 
282     const Point aZero(0, 0);
283     Bitmap aResult(pVD->GetBitmap(aZero, pVD->GetOutputSize()));
284 
285     if(bUseDoubleSize)
286     {
287         const Size aCurrentSize(aResult.GetSizePixel());
288 
289         aResult.Scale(Size(aCurrentSize.Width() / 2, aCurrentSize.Height() / 2), BMP_SCALE_FASTESTINTERPOLATE);
290     }
291 
292     return aResult;
293 }
294 
295 Bitmap XDashList::CreateBitmapForUI( long nIndex )
296 {
297     const XDash& rDash = GetDash(nIndex)->GetDash();
298 
299     return ImpCreateBitmapForXDash(&rDash);
300 }
301 
302 Bitmap XDashList::GetBitmapForUISolidLine() const
303 {
304     if(maBitmapSolidLine.IsEmpty())
305     {
306         const_cast< XDashList* >(this)->maBitmapSolidLine = const_cast< XDashList* >(this)->ImpCreateBitmapForXDash(0);
307     }
308 
309     return maBitmapSolidLine;
310 }
311 
312 String XDashList::GetStringForUiSolidLine() const
313 {
314     if(!maStringSolidLine.Len())
315     {
316         const_cast< XDashList* >(this)->maStringSolidLine = String(ResId(RID_SVXSTR_SOLID, DIALOG_MGR()));
317     }
318 
319     return maStringSolidLine;
320 }
321 
322 String XDashList::GetStringForUiNoLine() const
323 {
324     if(!maStringNoLine.Len())
325     {
326         // formally was RID_SVXSTR_INVISIBLE, but tomake equal
327         // everywhere, use RID_SVXSTR_NONE
328         const_cast< XDashList* >(this)->maStringNoLine = String(ResId(RID_SVXSTR_NONE, DIALOG_MGR()));
329     }
330 
331     return maStringNoLine;
332 }
333 
334 //////////////////////////////////////////////////////////////////////////////
335 // eof
336