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