xref: /aoo41x/main/svx/source/xoutdev/xtabdash.cxx (revision 35726d9d)
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 sal_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 sal_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 	return( sal_False );
106 }
107 
108 sal_Bool XDashList::Save()
109 {
110 	INetURLObject aURL( maPath );
111 
112 	if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
113 	{
114 		OSL_ENSURE( !maPath.Len(), "invalid URL" );
115 		return sal_False;
116 	}
117 
118 	aURL.Append( maName );
119 
120 	if( !aURL.getExtension().getLength() )
121 		aURL.setExtension( rtl::OUString( pszExtDash, 3 ) );
122 
123 	uno::Reference< container::XNameContainer > xTable( SvxUnoXDashTable_createInstance( this ), uno::UNO_QUERY );
124 	return SvxXMLXTableExportComponent::save( aURL.GetMainURL( INetURLObject::NO_DECODE ), xTable );
125 }
126 
127 sal_Bool XDashList::Create()
128 {
129 	XubString aStr( SVX_RES( RID_SVXSTR_LINESTYLE ) );
130 	xub_StrLen nLen;
131 
132 	aStr.AppendAscii(" 1");
133 	nLen = aStr.Len() - 1;
134 	Insert(new XDashEntry(XDash(XDASH_RECT,1, 50,1, 50, 50),aStr));
135 	aStr.SetChar(nLen, sal_Unicode('2'));
136 	Insert(new XDashEntry(XDash(XDASH_RECT,1,500,1,500,500),aStr));
137 	aStr.SetChar(nLen, sal_Unicode('3'));
138 	Insert(new XDashEntry(XDash(XDASH_RECT,2, 50,3,250,120),aStr));
139 
140 	return( sal_True );
141 }
142 
143 Bitmap XDashList::ImpCreateBitmapForXDash(const XDash* pDash)
144 {
145     const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
146     const Size& rSize = rStyleSettings.GetListBoxPreviewDefaultPixelSize();
147     const sal_uInt32 nFactor(2);
148     const Size aSize((rSize.Width() * 5 * 2) / 2, rSize.Height() * nFactor);
149 
150     // prepare polygon geometry for line
151     basegfx::B2DPolygon aLine;
152 
153     aLine.append(basegfx::B2DPoint(0.0, aSize.Height() / 2.0));
154     aLine.append(basegfx::B2DPoint(aSize.Width(), aSize.Height() / 2.0));
155 
156     // prepare LineAttribute
157     const basegfx::BColor aLineColor(rStyleSettings.GetFieldTextColor().getBColor());
158     const double fLineWidth(rStyleSettings.GetListBoxPreviewDefaultLineWidth() * (nFactor * 1.1));
159     const drawinglayer::attribute::LineAttribute aLineAttribute(
160         aLineColor,
161         fLineWidth);
162 
163     // prepare StrokeAttribute
164     ::std::vector< double > aDotDashArray;
165     double fFullDotDashLen(0.0);
166 
167     if(pDash && (pDash->GetDots() || pDash->GetDashes()))
168     {
169         const basegfx::B2DHomMatrix aScaleMatrix(OutputDevice::LogicToLogic(MAP_100TH_MM, MAP_PIXEL));
170         const basegfx::B2DVector aScaleVector(aScaleMatrix * basegfx::B2DVector(1.0, 0.0));
171         const double fScaleValue(aScaleVector.getLength() * (nFactor * (1.4 / 2.0)));
172         const double fLineWidthInUnits(fLineWidth / fScaleValue);
173 
174         fFullDotDashLen = pDash->CreateDotDashArray(aDotDashArray, fLineWidthInUnits);
175 
176         if(!aDotDashArray.empty())
177         {
178             for(sal_uInt32 a(0); a < aDotDashArray.size(); a++)
179             {
180                 aDotDashArray[a] *= fScaleValue;
181             }
182 
183             fFullDotDashLen *= fScaleValue;
184         }
185     }
186 
187     const drawinglayer::attribute::StrokeAttribute aStrokeAttribute(
188         aDotDashArray,
189         fFullDotDashLen);
190 
191     // cerate LinePrimitive
192     const drawinglayer::primitive2d::Primitive2DReference aLinePrimitive(
193         new drawinglayer::primitive2d::PolygonStrokePrimitive2D(
194             aLine,
195             aLineAttribute,
196             aStrokeAttribute));
197 
198     // prepare VirtualDevice
199     VirtualDevice aVirtualDevice;
200     const drawinglayer::geometry::ViewInformation2D aNewViewInformation2D;
201 
202     aVirtualDevice.SetOutputSizePixel(aSize);
203     aVirtualDevice.SetDrawMode(rStyleSettings.GetHighContrastMode()
204         ? DRAWMODE_SETTINGSLINE | DRAWMODE_SETTINGSFILL | DRAWMODE_SETTINGSTEXT | DRAWMODE_SETTINGSGRADIENT
205         : DRAWMODE_DEFAULT);
206 
207     if(rStyleSettings.GetUIPreviewUsesCheckeredBackground())
208     {
209         const Point aNull(0, 0);
210         static const sal_uInt32 nLen(8 * nFactor);
211         static const Color aW(COL_WHITE);
212         static const Color aG(0xef, 0xef, 0xef);
213 
214         aVirtualDevice.DrawCheckered(aNull, aSize, nLen, aW, aG);
215     }
216     else
217     {
218         aVirtualDevice.SetBackground(rStyleSettings.GetFieldColor());
219         aVirtualDevice.Erase();
220     }
221 
222     // create processor and draw primitives
223     drawinglayer::processor2d::BaseProcessor2D* pProcessor2D = drawinglayer::processor2d::createPixelProcessor2DFromOutputDevice(
224         aVirtualDevice,
225         aNewViewInformation2D);
226 
227     if(pProcessor2D)
228     {
229         const drawinglayer::primitive2d::Primitive2DSequence aSequence(&aLinePrimitive, 1);
230 
231         pProcessor2D->process(aSequence);
232         delete pProcessor2D;
233     }
234 
235     // get result bitmap and scale
236     Bitmap aRetval(aVirtualDevice.GetBitmap(Point(0, 0), aVirtualDevice.GetOutputSizePixel()));
237 
238     if(1 != nFactor)
239     {
240         aRetval.Scale(Size((rSize.Width() * 5) / 2, rSize.Height()), BMP_SCALE_FASTESTINTERPOLATE);
241     }
242 
243     return aRetval;
244 }
245 
246 Bitmap XDashList::CreateBitmapForUI( long nIndex )
247 {
248     Bitmap aRetval;
249     OSL_ENSURE(nIndex < Count(), "OOps, global values missing (!)");
250 
251     if(nIndex < Count())
252     {
253         const XDash& rDash = GetDash(nIndex)->GetDash();
254 
255         aRetval = ImpCreateBitmapForXDash(&rDash);
256     }
257 
258     return aRetval;
259 }
260 
261 Bitmap XDashList::GetBitmapForUISolidLine() const
262 {
263     if(maBitmapSolidLine.IsEmpty())
264     {
265         const_cast< XDashList* >(this)->maBitmapSolidLine = const_cast< XDashList* >(this)->ImpCreateBitmapForXDash(0);
266     }
267 
268     return maBitmapSolidLine;
269 }
270 
271 String XDashList::GetStringForUiSolidLine() const
272 {
273     if(!maStringSolidLine.Len())
274     {
275         const_cast< XDashList* >(this)->maStringSolidLine = String(ResId(RID_SVXSTR_SOLID, DIALOG_MGR()));
276     }
277 
278     return maStringSolidLine;
279 }
280 
281 String XDashList::GetStringForUiNoLine() const
282 {
283     if(!maStringNoLine.Len())
284     {
285         // formally was RID_SVXSTR_INVISIBLE, but tomake equal
286         // everywhere, use RID_SVXSTR_NONE
287         const_cast< XDashList* >(this)->maStringNoLine = String(ResId(RID_SVXSTR_NONE, DIALOG_MGR()));
288     }
289 
290     return maStringNoLine;
291 }
292 
293 //////////////////////////////////////////////////////////////////////////////
294 // eof
295