xref: /aoo42x/main/svx/source/xoutdev/xtabhtch.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 <vcl/svapp.hxx>
30 #include "xmlxtexp.hxx"
31 #include "xmlxtimp.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/fillhatchattribute.hxx>
38 #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
39 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
40 #include <drawinglayer/processor2d/processor2dtools.hxx>
41 #include <basegfx/polygon/b2dpolygontools.hxx>
42 
43 using namespace ::com::sun::star;
44 using namespace ::rtl;
45 
46 sal_Unicode const pszExtHatch[]	 = {'s','o','h'};
47 //char const aChckHatch[]  = { 0x04, 0x00, 'S','O','H','L'};	// < 5.2
48 //char const aChckHatch0[] = { 0x04, 0x00, 'S','O','H','0'};	// = 5.2
49 //char const aChckXML[]    = { '<', '?', 'x', 'm', 'l' };		// = 6.0
50 
51 // -----------------
52 // class XHatchList
53 // -----------------
54 
55 XHatchList::XHatchList(const String& rPath )
56 :   XPropertyList(rPath)
57 {
58 }
59 
60 XHatchList::~XHatchList()
61 {
62 }
63 
64 XHatchEntry* XHatchList::Replace(XHatchEntry* pEntry, long nIndex )
65 {
66 	return (XHatchEntry*) XPropertyList::Replace(pEntry, nIndex);
67 }
68 
69 XHatchEntry* XHatchList::Remove(long nIndex)
70 {
71 	return (XHatchEntry*) XPropertyList::Remove(nIndex);
72 }
73 
74 XHatchEntry* XHatchList::GetHatch(long nIndex) const
75 {
76 	return (XHatchEntry*) XPropertyList::Get(nIndex);
77 }
78 
79 sal_Bool XHatchList::Load()
80 {
81 	if( mbListDirty )
82 	{
83 		mbListDirty = false;
84 
85 		INetURLObject aURL( maPath );
86 
87 		if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
88 		{
89 			OSL_ENSURE( !maPath.Len(), "invalid URL" );
90 			return sal_False;
91 		}
92 
93 		aURL.Append( maName );
94 
95 		if( !aURL.getExtension().getLength() )
96 			aURL.setExtension( rtl::OUString( pszExtHatch, 3 ) );
97 
98 		uno::Reference< container::XNameContainer > xTable( SvxUnoXHatchTable_createInstance( this ), uno::UNO_QUERY );
99 		return SvxXMLXTableImport::load( aURL.GetMainURL( INetURLObject::NO_DECODE ), xTable );
100 	}
101 	return( sal_False );
102 }
103 
104 sal_Bool XHatchList::Save()
105 {
106 	INetURLObject aURL( maPath );
107 
108 	if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
109 	{
110 		OSL_ENSURE( !maPath.Len(), "invalid URL" );
111 		return sal_False;
112 	}
113 
114 	aURL.Append( maName );
115 
116 	if( !aURL.getExtension().getLength() )
117 		aURL.setExtension( rtl::OUString( pszExtHatch, 3 ) );
118 
119 	uno::Reference< container::XNameContainer > xTable( SvxUnoXHatchTable_createInstance( this ), uno::UNO_QUERY );
120 	return SvxXMLXTableExportComponent::save( aURL.GetMainURL( INetURLObject::NO_DECODE ), xTable );
121 }
122 
123 sal_Bool XHatchList::Create()
124 {
125 	XubString aStr( SVX_RES( RID_SVXSTR_HATCH ) );
126 	xub_StrLen nLen;
127 
128 	aStr.AppendAscii(" 1");
129 	nLen = aStr.Len() - 1;
130 	Insert(new XHatchEntry(XHatch(RGB_Color(COL_BLACK),XHATCH_SINGLE,100,  0),aStr));
131 	aStr.SetChar(nLen, sal_Unicode('2'));
132 	Insert(new XHatchEntry(XHatch(RGB_Color(COL_RED  ),XHATCH_DOUBLE, 80,450),aStr));
133 	aStr.SetChar(nLen, sal_Unicode('3'));
134 	Insert(new XHatchEntry(XHatch(RGB_Color(COL_BLUE ),XHATCH_TRIPLE,120,  0),aStr));
135 
136 	return( sal_True );
137 }
138 
139 Bitmap XHatchList::CreateBitmapForUI( long nIndex )
140 {
141     Bitmap aRetval;
142     OSL_ENSURE(nIndex < Count(), "OOps, access out of range (!)");
143 
144     if(nIndex < Count())
145     {
146         const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
147         const Size& rSize = rStyleSettings.GetListBoxPreviewDefaultPixelSize();
148 
149         // prepare polygon geometry for rectangle
150         const basegfx::B2DPolygon aRectangle(
151             basegfx::tools::createPolygonFromRect(
152                 basegfx::B2DRange(0.0, 0.0, rSize.Width(), rSize.Height())));
153 
154         const XHatch& rHatch = GetHatch(nIndex)->GetHatch();
155         drawinglayer::attribute::HatchStyle aHatchStyle(drawinglayer::attribute::HATCHSTYLE_TRIPLE);
156 
157         switch(rHatch.GetHatchStyle())
158         {
159             case XHATCH_SINGLE :
160             {
161                 aHatchStyle = drawinglayer::attribute::HATCHSTYLE_SINGLE;
162                 break;
163             }
164             case XHATCH_DOUBLE :
165             {
166                 aHatchStyle = drawinglayer::attribute::HATCHSTYLE_DOUBLE;
167                 break;
168             }
169             default :
170             {
171                 aHatchStyle = drawinglayer::attribute::HATCHSTYLE_TRIPLE; // XHATCH_TRIPLE
172                 break;
173             }
174         }
175 
176         const basegfx::B2DHomMatrix aScaleMatrix(OutputDevice::LogicToLogic(MAP_100TH_MM, MAP_PIXEL));
177         const basegfx::B2DVector aScaleVector(aScaleMatrix * basegfx::B2DVector(1.0, 0.0));
178         const double fScaleValue(aScaleVector.getLength());
179 
180         const drawinglayer::attribute::FillHatchAttribute aFillHatch(
181             aHatchStyle,
182             (double)rHatch.GetDistance() * fScaleValue,
183             (double)rHatch.GetAngle() * F_PI1800,
184             rHatch.GetColor().getBColor(),
185             3, // same default as VCL, a minimum of three discrete units (pixels) offset
186             false);
187 
188         const basegfx::BColor aBlack(0.0, 0.0, 0.0);
189         const drawinglayer::primitive2d::Primitive2DReference aHatchPrimitive(
190             new drawinglayer::primitive2d::PolyPolygonHatchPrimitive2D(
191                 basegfx::B2DPolyPolygon(aRectangle),
192                 aBlack,
193                 aFillHatch));
194 
195         const drawinglayer::primitive2d::Primitive2DReference aBlackRectanglePrimitive(
196             new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(
197                 aRectangle,
198                 aBlack));
199 
200         // prepare VirtualDevice
201         VirtualDevice aVirtualDevice;
202         const drawinglayer::geometry::ViewInformation2D aNewViewInformation2D;
203 
204         aVirtualDevice.SetOutputSizePixel(rSize);
205         aVirtualDevice.SetDrawMode(rStyleSettings.GetHighContrastMode()
206             ? DRAWMODE_SETTINGSLINE | DRAWMODE_SETTINGSFILL | DRAWMODE_SETTINGSTEXT | DRAWMODE_SETTINGSGRADIENT
207             : DRAWMODE_DEFAULT);
208 
209         if(rStyleSettings.GetUIPreviewUsesCheckeredBackground())
210         {
211             const Point aNull(0, 0);
212             static const sal_uInt32 nLen(8);
213             static const Color aW(COL_WHITE);
214             static const Color aG(0xef, 0xef, 0xef);
215 
216             aVirtualDevice.DrawCheckered(aNull, rSize, nLen, aW, aG);
217         }
218         else
219         {
220             aVirtualDevice.SetBackground(rStyleSettings.GetFieldColor());
221             aVirtualDevice.Erase();
222         }
223 
224         // create processor and draw primitives
225         drawinglayer::processor2d::BaseProcessor2D* pProcessor2D = drawinglayer::processor2d::createPixelProcessor2DFromOutputDevice(
226             aVirtualDevice,
227             aNewViewInformation2D);
228 
229         if(pProcessor2D)
230         {
231             drawinglayer::primitive2d::Primitive2DSequence aSequence(2);
232 
233             aSequence[0] = aHatchPrimitive;
234             aSequence[1] = aBlackRectanglePrimitive;
235 
236             pProcessor2D->process(aSequence);
237             delete pProcessor2D;
238         }
239 
240         // get result bitmap and scale
241         aRetval = aVirtualDevice.GetBitmap(Point(0, 0), aVirtualDevice.GetOutputSizePixel());
242     }
243 
244     return aRetval;
245 }
246 
247 //////////////////////////////////////////////////////////////////////////////
248 // eof
249