xref: /aoo42x/main/svx/source/xoutdev/xtabdash.cxx (revision 3e407bd8)
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 ---------------------------------------------------------------
26 #include <com/sun/star/container/XNameContainer.hpp>
27 #include "svx/XPropertyTable.hxx"
28 #include <unotools/ucbstreamhelper.hxx>
29 #include "xmlxtexp.hxx"
30 #include "xmlxtimp.hxx"
31 #include <vcl/svapp.hxx>
32 #include <tools/urlobj.hxx>
33 #include <vcl/virdev.hxx>
34 #include <svx/dialogs.hrc>
35 #include <svx/dialmgr.hxx>
36 #include <svx/xtable.hxx>
37 #include <drawinglayer/attribute/lineattribute.hxx>
38 #include <drawinglayer/attribute/strokeattribute.hxx>
39 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
40 #include <drawinglayer/processor2d/processor2dtools.hxx>
41 
42 using namespace com::sun::star;
43 using namespace rtl;
44 
45 #define GLOBALOVERFLOW
46 
47 sal_Unicode const pszExtDash[] 	= {'s','o','d'};
48 //char const aChckDash[]  = { 0x04, 0x00, 'S','O','D','L'};	// < 5.2
49 //char const aChckDash0[] = { 0x04, 0x00, 'S','O','D','0'};	// = 5.2
50 //char const aChckXML[]   = { '<', '?', 'x', 'm', 'l' };		// = 6.0
51 
52 // ----------------
53 // class XDashList
54 // ----------------
55 
56 XDashList::XDashList(const String& rPath )
57 :   XPropertyList(rPath),
58     maBitmapSolidLine(),
59     maStringSolidLine(),
60     maStringNoLine()
61 {
62 }
63 
64 XDashList::~XDashList()
65 {
66 }
67 
68 XDashEntry* XDashList::Replace(XDashEntry* pEntry, long nIndex )
69 {
70 	return (XDashEntry*) XPropertyList::Replace(pEntry, nIndex);
71 }
72 
73 XDashEntry* XDashList::Remove(long nIndex)
74 {
75 	return (XDashEntry*) XPropertyList::Remove(nIndex);
76 }
77 
78 XDashEntry* XDashList::GetDash(long nIndex) const
79 {
80 	return (XDashEntry*) XPropertyList::Get(nIndex);
81 }
82 
83 bool XDashList::Load()
84 {
85 	if( mbListDirty )
86 	{
87 		mbListDirty = false;
88 
89 		INetURLObject aURL( maPath );
90 
91 		if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
92 		{
93 			OSL_ENSURE( !maPath.Len(), "invalid URL" );
94 			return false;
95 		}
96 
97 		aURL.Append( maName );
98 
99 		if( !aURL.getExtension().getLength() )
100 			aURL.setExtension( rtl::OUString( pszExtDash, 3 ) );
101 
102 		uno::Reference< container::XNameContainer > xTable( SvxUnoXDashTable_createInstance( this ), uno::UNO_QUERY );
103 		return SvxXMLXTableImport::load( aURL.GetMainURL( INetURLObject::NO_DECODE ), xTable );
104 	}
105 
106     return false;
107 }
108 
109 bool XDashList::Save()
110 {
111 	INetURLObject aURL( maPath );
112 
113 	if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
114 	{
115 		OSL_ENSURE( !maPath.Len(), "invalid URL" );
116 		return false;
117 	}
118 
119 	aURL.Append( maName );
120 
121 	if( !aURL.getExtension().getLength() )
122 		aURL.setExtension( rtl::OUString( pszExtDash, 3 ) );
123 
124 	uno::Reference< container::XNameContainer > xTable( SvxUnoXDashTable_createInstance( this ), uno::UNO_QUERY );
125 	return SvxXMLXTableExportComponent::save( aURL.GetMainURL( INetURLObject::NO_DECODE ), xTable );
126 }
127 
128 bool XDashList::Create()
129 {
130 	XubString aStr( SVX_RES( RID_SVXSTR_LINESTYLE ) );
131 	xub_StrLen nLen;
132 
133 	aStr.AppendAscii(" 1");
134 	nLen = aStr.Len() - 1;
135 	Insert(new XDashEntry(XDash(XDASH_RECT,1, 50,1, 50, 50),aStr));
136 	aStr.SetChar(nLen, sal_Unicode('2'));
137 	Insert(new XDashEntry(XDash(XDASH_RECT,1,500,1,500,500),aStr));
138 	aStr.SetChar(nLen, sal_Unicode('3'));
139 	Insert(new XDashEntry(XDash(XDASH_RECT,2, 50,3,250,120),aStr));
140 
141 	return true;
142 }
143 
144 Bitmap XDashList::ImpCreateBitmapForXDash(const XDash* pDash)
145 {
146     const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
147     const Size& rSize = rStyleSettings.GetListBoxPreviewDefaultPixelSize();
148     const sal_uInt32 nFactor(2);
149     const Size aSize((rSize.Width() * 5 * 2) / 2, rSize.Height() * nFactor);
150 
151     // prepare polygon geometry for line
152     basegfx::B2DPolygon aLine;
153 
154     aLine.append(basegfx::B2DPoint(0.0, aSize.Height() / 2.0));
155     aLine.append(basegfx::B2DPoint(aSize.Width(), aSize.Height() / 2.0));
156 
157     // prepare LineAttribute
158     const basegfx::BColor aLineColor(rStyleSettings.GetFieldTextColor().getBColor());
159     const double fLineWidth(rStyleSettings.GetListBoxPreviewDefaultLineWidth() * (nFactor * 1.1));
160     const drawinglayer::attribute::LineAttribute aLineAttribute(
161         aLineColor,
162         fLineWidth);
163 
164     // prepare StrokeAttribute
165     ::std::vector< double > aDotDashArray;
166     double fFullDotDashLen(0.0);
167 
168     if(pDash && (pDash->GetDots() || pDash->GetDashes()))
169     {
170         const basegfx::B2DHomMatrix aScaleMatrix(OutputDevice::LogicToLogic(MAP_100TH_MM, MAP_PIXEL));
171         const basegfx::B2DVector aScaleVector(aScaleMatrix * basegfx::B2DVector(1.0, 0.0));
172         const double fScaleValue(aScaleVector.getLength() * (nFactor * (1.4 / 2.0)));
173         const double fLineWidthInUnits(fLineWidth / fScaleValue);
174 
175         fFullDotDashLen = pDash->CreateDotDashArray(aDotDashArray, fLineWidthInUnits);
176 
177         if(!aDotDashArray.empty())
178         {
179             for(sal_uInt32 a(0); a < aDotDashArray.size(); a++)
180             {
181                 aDotDashArray[a] *= fScaleValue;
182             }
183 
184             fFullDotDashLen *= fScaleValue;
185         }
186     }
187 
188     const drawinglayer::attribute::StrokeAttribute aStrokeAttribute(
189         aDotDashArray,
190         fFullDotDashLen);
191 
192     // cerate LinePrimitive
193     const drawinglayer::primitive2d::Primitive2DReference aLinePrimitive(
194         new drawinglayer::primitive2d::PolygonStrokePrimitive2D(
195             aLine,
196             aLineAttribute,
197             aStrokeAttribute));
198 
199     // prepare VirtualDevice
200     VirtualDevice aVirtualDevice;
201     const drawinglayer::geometry::ViewInformation2D aNewViewInformation2D;
202 
203     aVirtualDevice.SetOutputSizePixel(aSize);
204     aVirtualDevice.SetDrawMode(rStyleSettings.GetHighContrastMode()
205         ? DRAWMODE_SETTINGSLINE | DRAWMODE_SETTINGSFILL | DRAWMODE_SETTINGSTEXT | DRAWMODE_SETTINGSGRADIENT
206         : DRAWMODE_DEFAULT);
207 
208     if(rStyleSettings.GetUIPreviewUsesCheckeredBackground())
209     {
210         const Point aNull(0, 0);
211         static const sal_uInt32 nLen(8 * nFactor);
212         static const Color aW(COL_WHITE);
213         static const Color aG(0xef, 0xef, 0xef);
214 
215         aVirtualDevice.DrawCheckered(aNull, aSize, nLen, aW, aG);
216     }
217     else
218     {
219         aVirtualDevice.SetBackground(rStyleSettings.GetFieldColor());
220         aVirtualDevice.Erase();
221     }
222 
223     // create processor and draw primitives
224     drawinglayer::processor2d::BaseProcessor2D* pProcessor2D = drawinglayer::processor2d::createPixelProcessor2DFromOutputDevice(
225         aVirtualDevice,
226         aNewViewInformation2D);
227 
228     if(pProcessor2D)
229     {
230         const drawinglayer::primitive2d::Primitive2DSequence aSequence(&aLinePrimitive, 1);
231 
232         pProcessor2D->process(aSequence);
233         delete pProcessor2D;
234     }
235 
236     // get result bitmap and scale
237     Bitmap aRetval(aVirtualDevice.GetBitmap(Point(0, 0), aVirtualDevice.GetOutputSizePixel()));
238 
239     if(1 != nFactor)
240     {
241         aRetval.Scale(Size((rSize.Width() * 5) / 2, rSize.Height()), BMP_SCALE_FASTESTINTERPOLATE);
242     }
243 
244     return aRetval;
245 }
246 
247 Bitmap XDashList::CreateBitmapForUI( long nIndex )
248 {
249     Bitmap aRetval;
250     OSL_ENSURE(nIndex < Count(), "OOps, global values missing (!)");
251 
252     if(nIndex < Count())
253     {
254         const XDash& rDash = GetDash(nIndex)->GetDash();
255 
256         aRetval = ImpCreateBitmapForXDash(&rDash);
257     }
258 
259     return aRetval;
260 }
261 
262 Bitmap XDashList::GetBitmapForUISolidLine() const
263 {
264     if(maBitmapSolidLine.IsEmpty())
265     {
266         const_cast< XDashList* >(this)->maBitmapSolidLine = const_cast< XDashList* >(this)->ImpCreateBitmapForXDash(0);
267     }
268 
269     return maBitmapSolidLine;
270 }
271 
272 String XDashList::GetStringForUiSolidLine() const
273 {
274     if(!maStringSolidLine.Len())
275     {
276         const_cast< XDashList* >(this)->maStringSolidLine = String(ResId(RID_SVXSTR_SOLID, DIALOG_MGR()));
277     }
278 
279     return maStringSolidLine;
280 }
281 
282 String XDashList::GetStringForUiNoLine() const
283 {
284     if(!maStringNoLine.Len())
285     {
286         // formally was RID_SVXSTR_INVISIBLE, but tomake equal
287         // everywhere, use RID_SVXSTR_NONE
288         const_cast< XDashList* >(this)->maStringNoLine = String(ResId(RID_SVXSTR_NONE, DIALOG_MGR()));
289     }
290 
291     return maStringNoLine;
292 }
293 
294 //////////////////////////////////////////////////////////////////////////////
295 // eof
296