1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <vcl/builderfactory.hxx>
21 #include <vcl/msgbox.hxx>
22 #include <sfx2/viewfrm.hxx>
23 #include <sfx2/viewsh.hxx>
24 #include <sfx2/objsh.hxx>
25 #include <sfx2/request.hxx>
26 #include <dialmgr.hxx>
27 #include "cuigrfflt.hxx"
29 #include <svx/dialogs.hrc>
31 GraphicPreviewWindow::GraphicPreviewWindow(vcl::Window
* pParent
,
33 : Control(pParent
, nStyle
)
40 VCL_BUILDER_DECL_FACTORY(GraphicPreviewWindow
)
42 WinBits nWinBits
= WB_TABSTOP
;
44 OString sBorder
= VclBuilder::extractCustomProperty(rMap
);
45 if (!sBorder
.isEmpty())
46 nWinBits
|= WB_BORDER
;
48 rRet
= VclPtr
<GraphicPreviewWindow
>::Create(pParent
, nWinBits
);
51 Size
GraphicPreviewWindow::GetOptimalSize() const
53 return LogicToPixel(Size(81, 73), MAP_APPFONT
);
56 void GraphicPreviewWindow::Paint(vcl::RenderContext
& rRenderContext
, const Rectangle
& rRect
)
58 Control::Paint(rRenderContext
, rRect
);
60 const Size
aOutputSize(GetOutputSizePixel());
62 if (maPreview
.IsAnimated())
64 const Size
aGraphicSize(rRenderContext
.LogicToPixel(maPreview
.GetPrefSize(), maPreview
.GetPrefMapMode()));
65 const Point
aGraphicPosition((aOutputSize
.Width() - aGraphicSize
.Width() ) >> 1,
66 (aOutputSize
.Height() - aGraphicSize
.Height() ) >> 1);
67 maPreview
.StartAnimation(&rRenderContext
, aGraphicPosition
, aGraphicSize
);
71 const Size
aGraphicSize(maPreview
.GetSizePixel());
72 const Point
aGraphicPosition((aOutputSize
.Width() - aGraphicSize
.Width()) >> 1,
73 (aOutputSize
.Height() - aGraphicSize
.Height()) >> 1);
74 maPreview
.Draw(&rRenderContext
, aGraphicPosition
, aGraphicSize
);
78 void GraphicPreviewWindow::SetPreview(const Graphic
& rGraphic
)
84 void GraphicPreviewWindow::ScaleImageToFit()
89 maScaledOrig
= *mpOrigGraphic
;
91 const Size
aPreviewSize( GetOutputSizePixel() );
92 Size
aSizePixel(LogicToPixel(mpOrigGraphic
->GetPrefSize(),
93 mpOrigGraphic
->GetPrefMapMode()));
94 Size
aGrfSize(aSizePixel
);
96 if( mpOrigGraphic
->GetType() == GRAPHIC_BITMAP
&&
97 aPreviewSize
.Width() && aPreviewSize
.Height() &&
98 aGrfSize
.Width() && aGrfSize
.Height() )
100 const double fGrfWH
= (double) aGrfSize
.Width() / aGrfSize
.Height();
101 const double fPreWH
= (double) aPreviewSize
.Width() / aPreviewSize
.Height();
103 if( fGrfWH
< fPreWH
)
105 aGrfSize
.Width() = (long) ( aPreviewSize
.Height() * fGrfWH
);
106 aGrfSize
.Height() = aPreviewSize
.Height();
110 aGrfSize
.Width() = aPreviewSize
.Width();
111 aGrfSize
.Height() = (long) ( aPreviewSize
.Width() / fGrfWH
);
114 mfScaleX
= (double) aGrfSize
.Width() / aSizePixel
.Width();
115 mfScaleY
= (double) aGrfSize
.Height() / aSizePixel
.Height();
117 if( !mpOrigGraphic
->IsAnimated() )
119 BitmapEx
aBmpEx( mpOrigGraphic
->GetBitmapEx() );
121 if( aBmpEx
.Scale( aGrfSize
, BmpScaleFlag::Default
) )
122 maScaledOrig
= aBmpEx
;
126 maModifyHdl
.Call(this);
129 void GraphicPreviewWindow::Resize()
135 GraphicFilterDialog::GraphicFilterDialog(vcl::Window
* pParent
,
136 const OUString
& rID
, const OUString
& rUIXMLDescription
,
137 const Graphic
& rGraphic
)
138 : ModalDialog(pParent
, rID
, rUIXMLDescription
)
139 , maModifyHdl(LINK( this, GraphicFilterDialog
, ImplModifyHdl
))
140 , maSizePixel(LogicToPixel(rGraphic
.GetPrefSize(),
141 rGraphic
.GetPrefMapMode()))
143 bIsBitmap
= rGraphic
.GetType() == GRAPHIC_BITMAP
;
145 maTimer
.SetTimeoutHdl( LINK( this, GraphicFilterDialog
, ImplPreviewTimeoutHdl
) );
146 maTimer
.SetTimeout( 5 );
148 get(mpPreview
, "preview");
149 mpPreview
->init(&rGraphic
, maModifyHdl
);
152 GraphicFilterDialog::~GraphicFilterDialog()
157 void GraphicFilterDialog::dispose()
160 ModalDialog::dispose();
164 IMPL_LINK_NOARG_TYPED(GraphicFilterDialog
, ImplPreviewTimeoutHdl
, Timer
*, void)
167 mpPreview
->SetPreview(GetFilteredGraphic(mpPreview
->GetScaledOriginal(),
168 mpPreview
->GetScaleX(), mpPreview
->GetScaleY()));
173 IMPL_LINK_NOARG(GraphicFilterDialog
, ImplModifyHdl
)
188 GraphicFilterMosaic::GraphicFilterMosaic( vcl::Window
* pParent
, const Graphic
& rGraphic
,
189 sal_uInt16 nTileWidth
, sal_uInt16 nTileHeight
, bool bEnhanceEdges
)
190 : GraphicFilterDialog(pParent
, "MosaicDialog",
191 "cui/ui/mosaicdialog.ui", rGraphic
)
193 get(mpMtrWidth
, "width");
194 get(mpMtrHeight
, "height");
195 get(mpCbxEdges
, "edges");
197 mpMtrWidth
->SetValue( nTileWidth
);
198 mpMtrWidth
->SetLast( GetGraphicSizePixel().Width() );
199 mpMtrWidth
->SetModifyHdl( GetModifyHdl() );
201 mpMtrHeight
->SetValue( nTileHeight
);
202 mpMtrHeight
->SetLast( GetGraphicSizePixel().Height() );
203 mpMtrHeight
->SetModifyHdl( GetModifyHdl() );
205 mpCbxEdges
->Check( bEnhanceEdges
);
206 mpCbxEdges
->SetToggleHdl( GetModifyHdl() );
208 mpMtrWidth
->GrabFocus();
211 GraphicFilterMosaic::~GraphicFilterMosaic()
216 void GraphicFilterMosaic::dispose()
221 GraphicFilterDialog::dispose();
224 Graphic
GraphicFilterMosaic::GetFilteredGraphic( const Graphic
& rGraphic
,
225 double fScaleX
, double fScaleY
)
228 const Size
aSize( std::max( FRound( GetTileWidth() * fScaleX
), 1L ),
229 std::max( FRound( GetTileHeight() * fScaleY
), 1L ) );
230 BmpFilterParam
aParam( aSize
);
232 if( rGraphic
.IsAnimated() )
234 Animation
aAnim( rGraphic
.GetAnimation() );
236 if( aAnim
.Filter( BMP_FILTER_MOSAIC
, &aParam
) )
238 if( IsEnhanceEdges() )
239 aAnim
.Filter( BMP_FILTER_SHARPEN
);
246 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
248 if( aBmpEx
.Filter( BMP_FILTER_MOSAIC
, &aParam
) )
250 if( IsEnhanceEdges() )
251 (void)aBmpEx
.Filter( BMP_FILTER_SHARPEN
);
261 // - GraphicFilterSmooth -
264 GraphicFilterSmooth::GraphicFilterSmooth( vcl::Window
* pParent
, const Graphic
& rGraphic
, double nRadius
)
265 : GraphicFilterDialog(pParent
, "SmoothDialog",
266 "cui/ui/smoothdialog.ui", rGraphic
)
268 get(mpMtrRadius
, "radius");
270 mpMtrRadius
->SetValue( nRadius
* 10 );
271 mpMtrRadius
->SetModifyHdl( GetModifyHdl() );
272 mpMtrRadius
->GrabFocus();
275 GraphicFilterSmooth::~GraphicFilterSmooth()
280 void GraphicFilterSmooth::dispose()
283 GraphicFilterDialog::dispose();
287 Graphic
GraphicFilterSmooth::GetFilteredGraphic( const Graphic
& rGraphic
, double /*fScaleX*/, double /*fScaleY*/ )
290 BmpFilterParam
aParam( GetRadius() );
292 if( rGraphic
.IsAnimated() )
294 Animation
aAnim( rGraphic
.GetAnimation() );
296 if( aAnim
.Filter( BMP_FILTER_SMOOTH
, &aParam
) )
303 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
305 if( aBmpEx
.Filter( BMP_FILTER_SMOOTH
, &aParam
) )
315 // - GraphicFilterSolarize -
318 GraphicFilterSolarize::GraphicFilterSolarize( vcl::Window
* pParent
, const Graphic
& rGraphic
,
319 sal_uInt8 cGreyThreshold
, bool bInvert
)
320 : GraphicFilterDialog(pParent
, "SolarizeDialog",
321 "cui/ui/solarizedialog.ui", rGraphic
)
323 get(mpMtrThreshold
, "value");
324 get(mpCbxInvert
, "invert");
326 mpMtrThreshold
->SetValue( FRound( cGreyThreshold
/ 2.55 ) );
327 mpMtrThreshold
->SetModifyHdl( GetModifyHdl() );
329 mpCbxInvert
->Check( bInvert
);
330 mpCbxInvert
->SetToggleHdl( GetModifyHdl() );
333 GraphicFilterSolarize::~GraphicFilterSolarize()
338 void GraphicFilterSolarize::dispose()
340 mpMtrThreshold
.clear();
342 GraphicFilterDialog::dispose();
346 Graphic
GraphicFilterSolarize::GetFilteredGraphic( const Graphic
& rGraphic
,
347 double /*fScaleX*/, double /*fScaleY*/ )
350 BmpFilterParam
aParam( GetGreyThreshold() );
352 if( rGraphic
.IsAnimated() )
354 Animation
aAnim( rGraphic
.GetAnimation() );
356 if( aAnim
.Filter( BMP_FILTER_SOLARIZE
, &aParam
) )
366 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
368 if( aBmpEx
.Filter( BMP_FILTER_SOLARIZE
, &aParam
) )
381 // - GraphicFilterSepia -
384 GraphicFilterSepia::GraphicFilterSepia( vcl::Window
* pParent
, const Graphic
& rGraphic
,
385 sal_uInt16 nSepiaPercent
)
386 : GraphicFilterDialog(pParent
, "AgingDialog",
387 "cui/ui/agingdialog.ui", rGraphic
)
389 get(mpMtrSepia
, "value");
391 mpMtrSepia
->SetValue( nSepiaPercent
);
392 mpMtrSepia
->SetModifyHdl( GetModifyHdl() );
395 GraphicFilterSepia::~GraphicFilterSepia()
400 void GraphicFilterSepia::dispose()
403 GraphicFilterDialog::dispose();
407 Graphic
GraphicFilterSepia::GetFilteredGraphic( const Graphic
& rGraphic
,
408 double /*fScaleX*/, double /*fScaleY*/ )
411 BmpFilterParam
aParam( GetSepiaPercent() );
413 if( rGraphic
.IsAnimated() )
415 Animation
aAnim( rGraphic
.GetAnimation() );
417 if( aAnim
.Filter( BMP_FILTER_SEPIA
, &aParam
) )
422 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
424 if( aBmpEx
.Filter( BMP_FILTER_SEPIA
, &aParam
) )
432 // - GraphicFilterPoster -
435 GraphicFilterPoster::GraphicFilterPoster(vcl::Window
* pParent
, const Graphic
& rGraphic
,
436 sal_uInt16 nPosterCount
)
437 : GraphicFilterDialog(pParent
, "PosterDialog",
438 "cui/ui/posterdialog.ui", rGraphic
)
440 get(mpNumPoster
, "value");
442 mpNumPoster
->SetFirst( 2 );
443 mpNumPoster
->SetLast( rGraphic
.GetBitmapEx().GetBitCount() );
444 mpNumPoster
->SetValue( nPosterCount
);
445 mpNumPoster
->SetModifyHdl( GetModifyHdl() );
448 GraphicFilterPoster::~GraphicFilterPoster()
453 void GraphicFilterPoster::dispose()
456 GraphicFilterDialog::dispose();
460 Graphic
GraphicFilterPoster::GetFilteredGraphic( const Graphic
& rGraphic
,
461 double /*fScaleX*/, double /*fScaleY*/ )
464 const sal_uInt16 nPosterCount
= GetPosterColorCount();
466 if( rGraphic
.IsAnimated() )
468 Animation
aAnim( rGraphic
.GetAnimation() );
470 if( aAnim
.ReduceColors( nPosterCount
, BMP_REDUCE_POPULAR
) )
475 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
477 if( aBmpEx
.ReduceColors( nPosterCount
, BMP_REDUCE_POPULAR
) )
485 // - GraphicFilterEmboss -
488 void EmbossControl::MouseButtonDown( const MouseEvent
& rEvt
)
490 const RECT_POINT eOldRP
= GetActualRP();
492 SvxRectCtl::MouseButtonDown( rEvt
);
494 if( GetActualRP() != eOldRP
)
495 maModifyHdl
.Call( this );
498 Size
EmbossControl::GetOptimalSize() const
500 return LogicToPixel(Size(77, 60), MAP_APPFONT
);
503 VCL_BUILDER_FACTORY(EmbossControl
)
505 GraphicFilterEmboss::GraphicFilterEmboss(vcl::Window
* pParent
,
506 const Graphic
& rGraphic
, RECT_POINT eLightSource
)
507 : GraphicFilterDialog (pParent
, "EmbossDialog",
508 "cui/ui/embossdialog.ui", rGraphic
)
510 get(mpCtlLight
, "lightsource");
511 mpCtlLight
->SetActualRP(eLightSource
);
512 mpCtlLight
->SetModifyHdl( GetModifyHdl() );
513 mpCtlLight
->GrabFocus();
516 GraphicFilterEmboss::~GraphicFilterEmboss()
521 void GraphicFilterEmboss::dispose()
524 GraphicFilterDialog::dispose();
528 Graphic
GraphicFilterEmboss::GetFilteredGraphic( const Graphic
& rGraphic
,
529 double /*fScaleX*/, double /*fScaleY*/ )
532 sal_uInt16 nAzim
, nElev
;
534 switch( mpCtlLight
->GetActualRP() )
536 default: OSL_FAIL("svx::GraphicFilterEmboss::GetFilteredGraphic(), unknown Reference Point!" );
538 case( RP_LT
): nAzim
= 4500, nElev
= 4500; break;
539 case( RP_MT
): nAzim
= 9000, nElev
= 4500; break;
540 case( RP_RT
): nAzim
= 13500, nElev
= 4500; break;
541 case( RP_LM
): nAzim
= 0, nElev
= 4500; break;
542 case( RP_MM
): nAzim
= 0, nElev
= 9000; break;
543 case( RP_RM
): nAzim
= 18000, nElev
= 4500; break;
544 case( RP_LB
): nAzim
= 31500, nElev
= 4500; break;
545 case( RP_MB
): nAzim
= 27000, nElev
= 4500; break;
546 case( RP_RB
): nAzim
= 22500, nElev
= 4500; break;
549 BmpFilterParam
aParam( nAzim
, nElev
);
551 if( rGraphic
.IsAnimated() )
553 Animation
aAnim( rGraphic
.GetAnimation() );
555 if( aAnim
.Filter( BMP_FILTER_EMBOSS_GREY
, &aParam
) )
560 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
562 if( aBmpEx
.Filter( BMP_FILTER_EMBOSS_GREY
, &aParam
) )
569 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */