Update ooo320-m1
[ooovba.git] / sd / source / ui / dlg / vectdlg.cxx
blobc9149d642f44667db503da8fb6277fa94d89f8d3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vectdlg.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #ifdef SD_DLLIMPLEMENTATION
35 #undef SD_DLLIMPLEMENTATION
36 #endif
37 #include <vcl/vclenum.hxx>
38 #include <vcl/wrkwin.hxx>
40 #include "strings.hrc"
41 #include "sdresid.hxx"
42 #include "DrawDocShell.hxx"
43 #include "sdmod.hxx"
44 #include "sdiocmpt.hxx"
45 #include "DrawDocShell.hxx"
46 #include "vectdlg.hxx"
47 #include "vectdlg.hrc"
48 #include <tools/config.hxx>
49 #ifndef _SV_OCTREE_HXX //autogen
50 #include <vcl/octree.hxx>
51 #endif
52 #include <vcl/bmpacc.hxx>
53 #include <vcl/msgbox.hxx>
54 #include <vcl/metaact.hxx>
56 // -----------
57 // - Defines -
58 // -----------
60 #define VECTORIZE_MAX_EXTENT 512
62 // ------------------
63 // - SdVectorizeDlg -
64 // ------------------
66 SdVectorizeDlg::SdVectorizeDlg(
67 Window* pParent, const Bitmap& rBmp, ::sd::DrawDocShell* pDocShell ) :
68 ModalDialog ( pParent, SdResId( DLG_VECTORIZE ) ),
69 mpDocSh ( pDocShell ),
70 aGrpSettings ( this, SdResId( GRP_SETTINGS ) ),
71 aFtLayers ( this, SdResId( FT_LAYERS ) ),
72 aNmLayers ( this, SdResId( NM_LAYERS ) ),
73 aFtReduce ( this, SdResId( FT_REDUCE ) ),
74 aMtReduce ( this, SdResId( MT_REDUCE ) ),
75 aFtFillHoles ( this, SdResId( FT_FILLHOLES ) ),
76 aMtFillHoles ( this, SdResId( MT_FILLHOLES ) ),
77 aCbFillHoles ( this, SdResId( CB_FILLHOLES ) ),
78 aFtOriginal ( this, SdResId( FT_ORIGINAL ) ),
79 aBmpWin ( this, SdResId( CTL_BMP ) ),
80 aFtVectorized ( this, SdResId( FT_VECTORIZED ) ),
81 aMtfWin ( this, SdResId( CTL_WMF ) ),
82 aGrpPrgs ( this, SdResId( GRP_PRGS ) ),
83 aPrgs ( this, SdResId( WND_PRGS ) ),
84 aBtnOK ( this, SdResId( BTN_OK ) ),
85 aBtnCancel ( this, SdResId( BTN_CANCEL ) ),
86 aBtnHelp ( this, SdResId( BTN_HELP ) ),
87 aBtnPreview ( this, SdResId( BTN_PREVIEW ) ),
88 aBmp ( rBmp )
90 FreeResource();
92 aBtnPreview.SetClickHdl( LINK( this, SdVectorizeDlg, ClickPreviewHdl ) );
93 aBtnOK.SetClickHdl( LINK( this, SdVectorizeDlg, ClickOKHdl ) );
94 aNmLayers.SetModifyHdl( LINK( this, SdVectorizeDlg, ModifyHdl ) );
95 aMtReduce.SetModifyHdl( LINK( this, SdVectorizeDlg, ModifyHdl ) );
96 aMtFillHoles.SetModifyHdl( LINK( this, SdVectorizeDlg, ModifyHdl ) );
97 aCbFillHoles.SetToggleHdl( LINK( this, SdVectorizeDlg, ToggleHdl ) );
99 // disable 3D border
100 aBmpWin.SetBorderStyle(WINDOW_BORDER_MONO);
101 aMtfWin.SetBorderStyle(WINDOW_BORDER_MONO);
103 LoadSettings();
104 InitPreviewBmp();
107 // -----------------------------------------------------------------------------
109 SdVectorizeDlg::~SdVectorizeDlg()
113 // -----------------------------------------------------------------------------
115 Rectangle SdVectorizeDlg::GetRect( const Size& rDispSize, const Size& rBmpSize ) const
117 Rectangle aRect;
119 if( rBmpSize.Width() && rBmpSize.Height() && rDispSize.Width() && rDispSize.Height() )
121 Size aBmpSize( rBmpSize );
122 const double fGrfWH = (double) aBmpSize.Width() / aBmpSize.Height();
123 const double fWinWH = (double) rDispSize.Width() / rDispSize.Height();
125 if( fGrfWH < fWinWH )
127 aBmpSize.Width() = (long) ( rDispSize.Height() * fGrfWH );
128 aBmpSize.Height()= rDispSize.Height();
130 else
132 aBmpSize.Width() = rDispSize.Width();
133 aBmpSize.Height()= (long) ( rDispSize.Width() / fGrfWH);
136 const Point aBmpPos( ( rDispSize.Width() - aBmpSize.Width() ) >> 1,
137 ( rDispSize.Height() - aBmpSize.Height() ) >> 1 );
139 aRect = Rectangle( aBmpPos, aBmpSize );
142 return aRect;
145 // -----------------------------------------------------------------------------
147 void SdVectorizeDlg::InitPreviewBmp()
149 const Rectangle aRect( GetRect( aBmpWin.GetSizePixel(), aBmp.GetSizePixel() ) );
151 aPreviewBmp = aBmp;
152 aPreviewBmp.Scale( aRect.GetSize() );
153 aBmpWin.SetGraphic( aPreviewBmp );
156 // -----------------------------------------------------------------------------
158 Bitmap SdVectorizeDlg::GetPreparedBitmap( Bitmap& rBmp, Fraction& rScale )
160 Bitmap aNew( rBmp );
161 const Size aSizePix( aNew.GetSizePixel() );
163 if( aSizePix.Width() > VECTORIZE_MAX_EXTENT || aSizePix.Height() > VECTORIZE_MAX_EXTENT )
165 const Rectangle aRect( GetRect( Size( VECTORIZE_MAX_EXTENT, VECTORIZE_MAX_EXTENT ), aSizePix ) );
166 rScale = Fraction( aSizePix.Width(), aRect.GetWidth() );
167 aNew.Scale( aRect.GetSize() );
169 else
170 rScale = Fraction( 1, 1 );
172 aNew.ReduceColors( (USHORT) aNmLayers.GetValue(), BMP_REDUCE_SIMPLE );
174 return aNew;
177 // -----------------------------------------------------------------------------
179 void SdVectorizeDlg::Calculate( Bitmap& rBmp, GDIMetaFile& rMtf )
181 mpDocSh->SetWaitCursor( TRUE );
182 aPrgs.SetValue( 0 );
184 Fraction aScale;
185 Bitmap aTmp( GetPreparedBitmap( rBmp, aScale ) );
187 if( !!aTmp )
189 const Link aPrgsHdl( LINK( this, SdVectorizeDlg, ProgressHdl ) );
190 aTmp.Vectorize( rMtf, (BYTE) aMtReduce.GetValue(), BMP_VECTORIZE_OUTER | BMP_VECTORIZE_REDUCE_EDGES, &aPrgsHdl );
192 if( aCbFillHoles.IsChecked() )
194 GDIMetaFile aNewMtf;
195 BitmapReadAccess* pRAcc = aTmp.AcquireReadAccess();
197 if( pRAcc )
199 const long nWidth = pRAcc->Width();
200 const long nHeight = pRAcc->Height();
201 const long nTileX = static_cast<long>(aMtFillHoles.GetValue());
202 const long nTileY = static_cast<long>(aMtFillHoles.GetValue());
203 const long nCountX = nWidth / nTileX;
204 const long nCountY = nHeight / nTileY;
205 const long nRestX = nWidth % nTileX;
206 const long nRestY = nHeight % nTileY;
208 MapMode aMap( rMtf.GetPrefMapMode() );
209 aNewMtf.SetPrefSize( rMtf.GetPrefSize() );
210 aNewMtf.SetPrefMapMode( aMap );
212 for( long nTY = 0; nTY < nCountY; nTY++ )
214 const long nY = nTY * nTileY;
216 for( long nTX = 0; nTX < nCountX; nTX++ )
217 AddTile( pRAcc, aNewMtf, nTX * nTileX, nTY * nTileY, nTileX, nTileY );
219 if( nRestX )
220 AddTile( pRAcc, aNewMtf, nCountX * nTileX, nY, nRestX, nTileY );
223 if( nRestY )
225 const long nY = nCountY * nTileY;
227 for( long nTX = 0; nTX < nCountX; nTX++ )
228 AddTile( pRAcc, aNewMtf, nTX * nTileX, nY, nTileX, nRestY );
230 if( nRestX )
231 AddTile( pRAcc, aNewMtf, nCountX * nTileX, nCountY * nTileY, nRestX, nRestY );
235 aTmp.ReleaseAccess( pRAcc );
237 for( ULONG n = 0UL, nCount = rMtf.GetActionCount(); n < nCount; n++ )
238 aNewMtf.AddAction( rMtf.GetAction( n )->Clone() );
240 aMap.SetScaleX( aMap.GetScaleX() * aScale );
241 aMap.SetScaleY( aMap.GetScaleY() * aScale );
242 aNewMtf.SetPrefMapMode( aMap );
243 rMtf = aNewMtf;
248 aPrgs.SetValue( 0 );
249 mpDocSh->SetWaitCursor( FALSE );
252 // -----------------------------------------------------------------------------
254 void SdVectorizeDlg::AddTile( BitmapReadAccess* pRAcc, GDIMetaFile& rMtf,
255 long nPosX, long nPosY, long nWidth, long nHeight )
257 ULONG nSumR = 0UL, nSumG = 0UL, nSumB = 0UL;
258 const long nRight = nPosX + nWidth - 1L;
259 const long nBottom = nPosY + nHeight - 1L;
260 const double fMult = 1.0 / ( nWidth * nHeight );
262 for( long nY = nPosY; nY <= nBottom; nY++ )
264 for( long nX = nPosX; nX <= nRight; nX++ )
266 const BitmapColor aPixel( pRAcc->GetColor( nY, nX ) );
268 nSumR += aPixel.GetRed();
269 nSumG += aPixel.GetGreen();
270 nSumB += aPixel.GetBlue();
274 const Color aColor( (BYTE) FRound( nSumR * fMult ),
275 (BYTE) FRound( nSumG * fMult ),
276 (BYTE) FRound( nSumB * fMult ) );
278 Rectangle aRect( Point( nPosX, nPosY ), Size( nWidth + 1, nHeight + 1 ) );
279 const Size& rMaxSize = rMtf.GetPrefSize();
281 aRect = PixelToLogic( aRect, rMtf.GetPrefMapMode() );
283 if( aRect.Right() > ( rMaxSize.Width() - 1L ) )
284 aRect.Right() = rMaxSize.Width() - 1L;
286 if( aRect.Bottom() > ( rMaxSize.Height() - 1L ) )
287 aRect.Bottom() = rMaxSize.Height() - 1L;
289 rMtf.AddAction( new MetaLineColorAction( aColor, TRUE ) );
290 rMtf.AddAction( new MetaFillColorAction( aColor, TRUE ) );
291 rMtf.AddAction( new MetaRectAction( aRect ) );
294 // -----------------------------------------------------------------------------
296 IMPL_LINK( SdVectorizeDlg, ProgressHdl, void*, pData )
298 aPrgs.SetValue( (USHORT)(ULONG) pData );
299 return 0L;
302 // -----------------------------------------------------------------------------
304 IMPL_LINK( SdVectorizeDlg, ClickPreviewHdl, PushButton*, EMPTYARG )
306 Calculate( aBmp, aMtf );
307 aMtfWin.SetGraphic( aMtf );
308 aBtnPreview.Disable();
310 return 0L;
313 // -----------------------------------------------------------------------------
315 IMPL_LINK( SdVectorizeDlg, ClickOKHdl, OKButton*, EMPTYARG )
317 if( aBtnPreview.IsEnabled() )
318 Calculate( aBmp, aMtf );
320 SaveSettings();
321 EndDialog( RET_OK );
323 return 0L;
326 // -----------------------------------------------------------------------------
328 IMPL_LINK( SdVectorizeDlg, ToggleHdl, CheckBox*, pCb )
330 if( pCb->IsChecked() )
332 aFtFillHoles.Enable();
333 aMtFillHoles.Enable();
335 else
337 aFtFillHoles.Disable();
338 aMtFillHoles.Disable();
341 ModifyHdl( NULL );
343 return 0L;
346 // -----------------------------------------------------------------------------
348 IMPL_LINK( SdVectorizeDlg, ModifyHdl, void*, EMPTYARG )
350 aBtnPreview.Enable();
351 return 0L;
354 // -----------------------------------------------------------------------------
356 void SdVectorizeDlg::LoadSettings()
358 SvStorageStreamRef xIStm( SD_MOD()->GetOptionStream(
359 UniString::CreateFromAscii(
360 RTL_CONSTASCII_STRINGPARAM( SD_OPTION_VECTORIZE ) ),
361 SD_OPTION_LOAD ) );
362 UINT16 nLayers;
363 UINT16 nReduce;
364 UINT16 nFillHoles;
365 BOOL bFillHoles;
367 if( xIStm.Is() )
369 SdIOCompat aCompat( *xIStm, STREAM_READ );
370 *xIStm >> nLayers >> nReduce >> nFillHoles >> bFillHoles;
372 else
374 nLayers = 8;
375 nReduce = 0;
376 nFillHoles = 32;
377 bFillHoles = FALSE;
380 aNmLayers.SetValue( nLayers );
381 aMtReduce.SetValue( nReduce );
382 aMtFillHoles.SetValue( nFillHoles );
383 aCbFillHoles.Check( bFillHoles );
385 ToggleHdl( &aCbFillHoles );
388 // -----------------------------------------------------------------------------
390 void SdVectorizeDlg::SaveSettings() const
392 SvStorageStreamRef xOStm( SD_MOD()->GetOptionStream(
393 UniString::CreateFromAscii(
394 RTL_CONSTASCII_STRINGPARAM( SD_OPTION_VECTORIZE ) ),
395 SD_OPTION_STORE ) );
397 if( xOStm.Is() )
399 SdIOCompat aCompat( *xOStm, STREAM_WRITE, 1 );
400 *xOStm << (UINT16) aNmLayers.GetValue() << (UINT16) aMtReduce.GetValue();
401 *xOStm << (UINT16) aMtFillHoles.GetValue() << aCbFillHoles.IsChecked();