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 <svl/eitem.hxx>
21 #include <svl/stritem.hxx>
22 #include <sfx2/app.hxx>
23 #include <sfx2/module.hxx>
24 #include <sfx2/sfxsids.hrc>
25 #include <dialmgr.hxx>
26 #include <svx/dlgutil.hxx>
27 #include <editeng/sizeitem.hxx>
28 #include <editeng/brushitem.hxx>
29 #include <grfpage.hxx>
30 #include <svx/grfcrop.hxx>
31 #include <rtl/ustring.hxx>
33 #include <tools/fract.hxx>
34 #include <svx/dialogs.hrc>
35 #include <vcl/builderfactory.hxx>
36 #include <vcl/settings.hxx>
37 #include <boost/scoped_ptr.hpp>
39 #define CM_1_TO_TWIP 567
40 #define TWIP_TO_INCH 1440
43 static inline long lcl_GetValue( MetricField
& rMetric
, FieldUnit eUnit
)
45 return static_cast<long>(rMetric
.Denormalize( rMetric
.GetValue( eUnit
)));
48 /*--------------------------------------------------------------------
49 description: crop graphic
50 --------------------------------------------------------------------*/
52 SvxGrfCropPage::SvxGrfCropPage ( vcl::Window
*pParent
, const SfxItemSet
&rSet
)
53 : SfxTabPage(pParent
, "CropPage", "cui/ui/croppage.ui", &rSet
)
61 get(m_pCropFrame
, "cropframe");
62 get(m_pScaleFrame
, "scaleframe");
63 get(m_pSizeFrame
, "sizeframe");
64 get(m_pOrigSizeGrid
, "origsizegrid");
65 get(m_pZoomConstRB
, "keepscale");
66 get(m_pSizeConstRB
, "keepsize");
67 get(m_pOrigSizeFT
, "origsizeft");
68 get(m_pOrigSizePB
, "origsize");
69 get(m_pLeftMF
, "left");
70 get(m_pRightMF
, "right");
72 get(m_pBottomMF
, "bottom");
73 get(m_pWidthZoomMF
, "widthzoom");
74 get(m_pHeightZoomMF
, "heightzoom");
75 get(m_pWidthMF
, "width");
76 get(m_pHeightMF
, "height");
77 get(m_pExampleWN
, "preview");
81 // set the correct metric
82 const FieldUnit eMetric
= GetModuleFieldUnit( rSet
);
84 SetFieldUnit( *m_pWidthMF
, eMetric
);
85 SetFieldUnit( *m_pHeightMF
, eMetric
);
86 SetFieldUnit( *m_pLeftMF
, eMetric
);
87 SetFieldUnit( *m_pRightMF
, eMetric
);
88 SetFieldUnit( *m_pTopMF
, eMetric
);
89 SetFieldUnit( *m_pBottomMF
, eMetric
);
91 Link
<> aLk
= LINK(this, SvxGrfCropPage
, SizeHdl
);
92 m_pWidthMF
->SetModifyHdl( aLk
);
93 m_pHeightMF
->SetModifyHdl( aLk
);
95 aLk
= LINK(this, SvxGrfCropPage
, ZoomHdl
);
96 m_pWidthZoomMF
->SetModifyHdl( aLk
);
97 m_pHeightZoomMF
->SetModifyHdl( aLk
);
99 aLk
= LINK(this, SvxGrfCropPage
, CropHdl
);
100 m_pLeftMF
->SetDownHdl( aLk
);
101 m_pRightMF
->SetDownHdl( aLk
);
102 m_pTopMF
->SetDownHdl( aLk
);
103 m_pBottomMF
->SetDownHdl( aLk
);
104 m_pLeftMF
->SetUpHdl( aLk
);
105 m_pRightMF
->SetUpHdl( aLk
);
106 m_pTopMF
->SetUpHdl( aLk
);
107 m_pBottomMF
->SetUpHdl( aLk
);
109 aLk
= LINK(this, SvxGrfCropPage
, CropModifyHdl
);
110 m_pLeftMF
->SetModifyHdl( aLk
);
111 m_pRightMF
->SetModifyHdl( aLk
);
112 m_pTopMF
->SetModifyHdl( aLk
);
113 m_pBottomMF
->SetModifyHdl( aLk
);
115 aLk
= LINK(this, SvxGrfCropPage
, CropLoseFocusHdl
);
116 m_pLeftMF
->SetLoseFocusHdl( aLk
);
117 m_pRightMF
->SetLoseFocusHdl( aLk
);
118 m_pTopMF
->SetLoseFocusHdl( aLk
);
119 m_pBottomMF
->SetLoseFocusHdl( aLk
);
121 aLk
= LINK(this, SvxGrfCropPage
, OrigSizeHdl
);
122 m_pOrigSizePB
->SetClickHdl( aLk
);
124 aTimer
.SetTimeoutHdl(LINK(this, SvxGrfCropPage
, Timeout
));
125 aTimer
.SetTimeout( 1500 );
128 SvxGrfCropPage::~SvxGrfCropPage()
133 void SvxGrfCropPage::dispose()
136 m_pCropFrame
.clear();
137 m_pZoomConstRB
.clear();
138 m_pSizeConstRB
.clear();
143 m_pScaleFrame
.clear();
144 m_pWidthZoomMF
.clear();
145 m_pHeightZoomMF
.clear();
146 m_pSizeFrame
.clear();
149 m_pOrigSizeGrid
.clear();
150 m_pOrigSizeFT
.clear();
151 m_pOrigSizePB
.clear();
152 m_pExampleWN
.clear();
153 pLastCropField
.clear();
154 SfxTabPage::dispose();
157 VclPtr
<SfxTabPage
> SvxGrfCropPage::Create(vcl::Window
*pParent
, const SfxItemSet
*rSet
)
159 return VclPtr
<SvxGrfCropPage
>::Create( pParent
, *rSet
);
162 void SvxGrfCropPage::Reset( const SfxItemSet
*rSet
)
164 const SfxPoolItem
* pItem
;
165 const SfxItemPool
& rPool
= *rSet
->GetPool();
167 if(SfxItemState::SET
== rSet
->GetItemState( rPool
.GetWhich(
168 SID_ATTR_GRAF_KEEP_ZOOM
), true, &pItem
))
170 if( static_cast<const SfxBoolItem
*>(pItem
)->GetValue() )
171 m_pZoomConstRB
->Check();
173 m_pSizeConstRB
->Check();
174 m_pZoomConstRB
->SaveValue();
177 sal_uInt16 nW
= rPool
.GetWhich( SID_ATTR_GRAF_CROP
);
178 if( SfxItemState::SET
== rSet
->GetItemState( nW
, true, &pItem
))
180 FieldUnit eUnit
= MapToFieldUnit( rSet
->GetPool()->GetMetric( nW
));
182 const SvxGrfCrop
* pCrop
= static_cast<const SvxGrfCrop
*>(pItem
);
184 m_pExampleWN
->SetLeft( pCrop
->GetLeft());
185 m_pExampleWN
->SetRight( pCrop
->GetRight());
186 m_pExampleWN
->SetTop( pCrop
->GetTop());
187 m_pExampleWN
->SetBottom( pCrop
->GetBottom());
189 m_pLeftMF
->SetValue( m_pLeftMF
->Normalize( pCrop
->GetLeft()), eUnit
);
190 m_pRightMF
->SetValue( m_pRightMF
->Normalize( pCrop
->GetRight()), eUnit
);
191 m_pTopMF
->SetValue( m_pTopMF
->Normalize( pCrop
->GetTop()), eUnit
);
192 m_pBottomMF
->SetValue( m_pBottomMF
->Normalize( pCrop
->GetBottom()), eUnit
);
196 m_pLeftMF
->SetValue( 0 );
197 m_pRightMF
->SetValue( 0 );
198 m_pTopMF
->SetValue( 0 );
199 m_pBottomMF
->SetValue( 0 );
202 nW
= rPool
.GetWhich( SID_ATTR_PAGE_SIZE
);
203 if ( SfxItemState::SET
== rSet
->GetItemState( nW
, false, &pItem
) )
205 // orientation and size from the PageItem
206 FieldUnit eUnit
= MapToFieldUnit( rSet
->GetPool()->GetMetric( nW
));
208 aPageSize
= static_cast<const SvxSizeItem
*>(pItem
)->GetSize();
210 sal_Int64 nTmp
= m_pHeightMF
->Normalize(aPageSize
.Height());
211 m_pHeightMF
->SetMax( nTmp
, eUnit
);
212 nTmp
= m_pWidthMF
->Normalize(aPageSize
.Width());
213 m_pWidthMF
->SetMax( nTmp
, eUnit
);
214 nTmp
= m_pWidthMF
->Normalize( 23 );
215 m_pHeightMF
->SetMin( nTmp
, eUnit
);
216 m_pWidthMF
->SetMin( nTmp
, eUnit
);
220 aPageSize
= OutputDevice::LogicToLogic(
221 Size( CM_1_TO_TWIP
, CM_1_TO_TWIP
),
223 MapMode( (MapUnit
)rSet
->GetPool()->GetMetric( nW
) ) );
227 if( SfxItemState::SET
== rSet
->GetItemState( SID_ATTR_GRAF_GRAPHIC
, false, &pItem
) )
230 SfxStringItem
const * it
= static_cast<SfxStringItem
const *>(
231 rSet
->GetItem(SID_REFERER
));
233 referer
= it
->GetValue();
235 const Graphic
* pGrf
= static_cast<const SvxBrushItem
*>(pItem
)->GetGraphic(referer
);
238 aOrigSize
= GetGrfOrigSize( *pGrf
);
239 if (pGrf
->GetType() == GRAPHIC_BITMAP
&& aOrigSize
.Width() && aOrigSize
.Height())
241 Bitmap aBitmap
= pGrf
->GetBitmap();
242 aOrigPixelSize
= aBitmap
.GetSizePixel();
245 if( aOrigSize
.Width() && aOrigSize
.Height() )
248 m_pExampleWN
->SetGraphic( *pGrf
);
249 m_pExampleWN
->SetFrameSize( aOrigSize
);
252 if( !static_cast<const SvxBrushItem
*>(pItem
)->GetGraphicLink().isEmpty() )
253 aGraphicName
= static_cast<const SvxBrushItem
*>(pItem
)->GetGraphicLink();
258 GraphicHasChanged( bFound
);
260 ActivatePage( *rSet
);
264 bool SvxGrfCropPage::FillItemSet(SfxItemSet
*rSet
)
266 const SfxItemPool
& rPool
= *rSet
->GetPool();
267 bool bModified
= false;
268 if( m_pWidthMF
->IsValueChangedFromSaved() ||
269 m_pHeightMF
->IsValueChangedFromSaved() )
271 sal_uInt16 nW
= rPool
.GetWhich( SID_ATTR_GRAF_FRMSIZE
);
272 FieldUnit eUnit
= MapToFieldUnit( rSet
->GetPool()->GetMetric( nW
));
274 SvxSizeItem
aSz( nW
);
276 // size could already have been set from another page
278 const SfxItemSet
* pExSet
= GetTabDialog() ? GetTabDialog()->GetExampleSet() : NULL
;
279 const SfxPoolItem
* pItem
= 0;
280 if( pExSet
&& SfxItemState::SET
==
281 pExSet
->GetItemState( nW
, false, &pItem
) )
282 aSz
= *static_cast<const SvxSizeItem
*>(pItem
);
284 aSz
= static_cast<const SvxSizeItem
&>(GetItemSet().Get( nW
));
286 Size
aTmpSz( aSz
.GetSize() );
287 if( m_pWidthMF
->IsValueChangedFromSaved() )
288 aTmpSz
.Width() = lcl_GetValue( *m_pWidthMF
, eUnit
);
289 if( m_pHeightMF
->IsValueChangedFromSaved() )
290 aTmpSz
.Height() = lcl_GetValue( *m_pHeightMF
, eUnit
);
291 aSz
.SetSize( aTmpSz
);
292 m_pWidthMF
->SaveValue();
293 m_pHeightMF
->SaveValue();
295 bModified
|= 0 != rSet
->Put( aSz
);
299 bModified
|= 0 != rSet
->Put( SvxSizeItem( rPool
.GetWhich(
300 SID_ATTR_GRAF_FRMSIZE_PERCENT
), Size( 0, 0 )) );
303 if( m_pLeftMF
->IsModified() || m_pRightMF
->IsModified() ||
304 m_pTopMF
->IsModified() || m_pBottomMF
->IsModified() )
306 sal_uInt16 nW
= rPool
.GetWhich( SID_ATTR_GRAF_CROP
);
307 FieldUnit eUnit
= MapToFieldUnit( rSet
->GetPool()->GetMetric( nW
));
308 boost::scoped_ptr
<SvxGrfCrop
> pNew(static_cast<SvxGrfCrop
*>(rSet
->Get( nW
).Clone()));
310 pNew
->SetLeft( lcl_GetValue( *m_pLeftMF
, eUnit
) );
311 pNew
->SetRight( lcl_GetValue( *m_pRightMF
, eUnit
) );
312 pNew
->SetTop( lcl_GetValue( *m_pTopMF
, eUnit
) );
313 pNew
->SetBottom( lcl_GetValue( *m_pBottomMF
, eUnit
) );
314 bModified
|= 0 != rSet
->Put( *pNew
);
317 if( m_pZoomConstRB
->IsValueChangedFromSaved() )
319 bModified
|= 0 != rSet
->Put( SfxBoolItem( rPool
.GetWhich(
320 SID_ATTR_GRAF_KEEP_ZOOM
), m_pZoomConstRB
->IsChecked() ) );
323 bInitialized
= false;
328 void SvxGrfCropPage::ActivatePage(const SfxItemSet
& rSet
)
331 SfxItemPool
* pPool
= GetItemSet().GetPool();
332 DBG_ASSERT( pPool
, "Wo ist der Pool" );
335 bSetOrigSize
= false;
339 const SfxPoolItem
* pItem
;
340 if( SfxItemState::SET
== rSet
.GetItemState( SID_ATTR_GRAF_FRMSIZE
, false, &pItem
) )
341 aSize
= static_cast<const SvxSizeItem
*>(pItem
)->GetSize();
343 nOldWidth
= aSize
.Width();
344 nOldHeight
= aSize
.Height();
346 sal_Int64 nWidth
= m_pWidthMF
->Normalize(nOldWidth
);
347 sal_Int64 nHeight
= m_pHeightMF
->Normalize(nOldHeight
);
349 if (nWidth
!= m_pWidthMF
->GetValue(FUNIT_TWIP
))
353 // value was changed by wrap-tabpage and has to
354 // be set with modify-flag
355 m_pWidthMF
->SetUserValue(nWidth
, FUNIT_TWIP
);
358 m_pWidthMF
->SetValue(nWidth
, FUNIT_TWIP
);
360 m_pWidthMF
->SaveValue();
362 if (nHeight
!= m_pHeightMF
->GetValue(FUNIT_TWIP
))
366 // value was changed by wrap-tabpage and has to
367 // be set with modify-flag
368 m_pHeightMF
->SetUserValue(nHeight
, FUNIT_TWIP
);
371 m_pHeightMF
->SetValue(nHeight
, FUNIT_TWIP
);
373 m_pHeightMF
->SaveValue();
376 if( SfxItemState::SET
== rSet
.GetItemState( SID_ATTR_GRAF_GRAPHIC
, false, &pItem
) )
378 const SvxBrushItem
& rBrush
= *static_cast<const SvxBrushItem
*>(pItem
);
379 if( !rBrush
.GetGraphicLink().isEmpty() &&
380 aGraphicName
!= rBrush
.GetGraphicLink() )
381 aGraphicName
= rBrush
.GetGraphicLink();
384 SfxStringItem
const * it
= static_cast<SfxStringItem
const *>(
385 rSet
.GetItem(SID_REFERER
));
387 referer
= it
->GetValue();
389 const Graphic
* pGrf
= rBrush
.GetGraphic(referer
);
392 m_pExampleWN
->SetGraphic( *pGrf
);
393 aOrigSize
= GetGrfOrigSize( *pGrf
);
394 if (pGrf
->GetType() == GRAPHIC_BITMAP
&& aOrigSize
.Width() > 1 && aOrigSize
.Height() > 1) {
395 Bitmap aBitmap
= pGrf
->GetBitmap();
396 aOrigPixelSize
= aBitmap
.GetSizePixel();
398 m_pExampleWN
->SetFrameSize(aOrigSize
);
399 GraphicHasChanged( aOrigSize
.Width() && aOrigSize
.Height() );
403 GraphicHasChanged( false );
409 SfxTabPage::sfxpg
SvxGrfCropPage::DeactivatePage(SfxItemSet
*_pSet
)
412 FillItemSet( _pSet
);
416 /*--------------------------------------------------------------------
417 description: scale changed, adjust size
418 --------------------------------------------------------------------*/
420 IMPL_LINK( SvxGrfCropPage
, ZoomHdl
, MetricField
*, pField
)
422 SfxItemPool
* pPool
= GetItemSet().GetPool();
423 DBG_ASSERT( pPool
, "Wo ist der Pool" );
424 FieldUnit eUnit
= MapToFieldUnit( pPool
->GetMetric( pPool
->GetWhich(
425 SID_ATTR_GRAF_CROP
) ) );
427 if( pField
== m_pWidthZoomMF
)
429 long nLRBorders
= lcl_GetValue(*m_pLeftMF
, eUnit
)
430 +lcl_GetValue(*m_pRightMF
, eUnit
);
431 m_pWidthMF
->SetValue( m_pWidthMF
->Normalize(
432 ((aOrigSize
.Width() - nLRBorders
) * pField
->GetValue())/100L),
437 long nULBorders
= lcl_GetValue(*m_pTopMF
, eUnit
)
438 +lcl_GetValue(*m_pBottomMF
, eUnit
);
439 m_pHeightMF
->SetValue( m_pHeightMF
->Normalize(
440 ((aOrigSize
.Height() - nULBorders
) * pField
->GetValue())/100L) ,
447 /*--------------------------------------------------------------------
448 description: change size, adjust scale
449 --------------------------------------------------------------------*/
451 IMPL_LINK( SvxGrfCropPage
, SizeHdl
, MetricField
*, pField
)
453 SfxItemPool
* pPool
= GetItemSet().GetPool();
454 DBG_ASSERT( pPool
, "Wo ist der Pool" );
455 FieldUnit eUnit
= MapToFieldUnit( pPool
->GetMetric( pPool
->GetWhich(
456 SID_ATTR_GRAF_CROP
) ) );
458 Size
aSize( lcl_GetValue(*m_pWidthMF
, eUnit
),
459 lcl_GetValue(*m_pHeightMF
, eUnit
) );
461 if(pField
== m_pWidthMF
)
463 long nWidth
= aOrigSize
.Width() -
464 ( lcl_GetValue(*m_pLeftMF
, eUnit
) +
465 lcl_GetValue(*m_pRightMF
, eUnit
) );
468 sal_uInt16 nZoom
= (sal_uInt16
)( aSize
.Width() * 100L / nWidth
);
469 m_pWidthZoomMF
->SetValue(nZoom
);
473 long nHeight
= aOrigSize
.Height() -
474 ( lcl_GetValue(*m_pTopMF
, eUnit
) +
475 lcl_GetValue(*m_pBottomMF
, eUnit
));
478 sal_uInt16 nZoom
= (sal_uInt16
)( aSize
.Height() * 100L/ nHeight
);
479 m_pHeightZoomMF
->SetValue(nZoom
);
485 /*--------------------------------------------------------------------
486 description: evaluate border
487 --------------------------------------------------------------------*/
489 IMPL_LINK( SvxGrfCropPage
, CropHdl
, const MetricField
*, pField
)
491 SfxItemPool
* pPool
= GetItemSet().GetPool();
492 DBG_ASSERT( pPool
, "Wo ist der Pool" );
493 FieldUnit eUnit
= MapToFieldUnit( pPool
->GetMetric( pPool
->GetWhich(
494 SID_ATTR_GRAF_CROP
) ) );
496 bool bZoom
= m_pZoomConstRB
->IsChecked();
497 if( pField
== m_pLeftMF
|| pField
== m_pRightMF
)
499 long nLeft
= lcl_GetValue( *m_pLeftMF
, eUnit
);
500 long nRight
= lcl_GetValue( *m_pRightMF
, eUnit
);
501 long nWidthZoom
= static_cast<long>(m_pWidthZoomMF
->GetValue());
502 if(bZoom
&& ( ( ( aOrigSize
.Width() - (nLeft
+ nRight
)) * nWidthZoom
)
503 / 100 >= aPageSize
.Width() ) )
505 if(pField
== m_pLeftMF
)
507 nLeft
= aOrigSize
.Width() -
508 ( aPageSize
.Width() * 100 / nWidthZoom
+ nRight
);
509 m_pLeftMF
->SetValue( m_pLeftMF
->Normalize( nLeft
), eUnit
);
513 nRight
= aOrigSize
.Width() -
514 ( aPageSize
.Width() * 100 / nWidthZoom
+ nLeft
);
515 m_pRightMF
->SetValue( m_pRightMF
->Normalize( nRight
), eUnit
);
518 if (AllSettings::GetLayoutRTL())
520 m_pExampleWN
->SetLeft(nRight
);
521 m_pExampleWN
->SetRight(nLeft
);
525 m_pExampleWN
->SetLeft(nLeft
);
526 m_pExampleWN
->SetRight(nRight
);
530 // scale stays, recompute width
531 ZoomHdl(m_pWidthZoomMF
);
536 long nTop
= lcl_GetValue( *m_pTopMF
, eUnit
);
537 long nBottom
= lcl_GetValue( *m_pBottomMF
, eUnit
);
538 long nHeightZoom
= static_cast<long>(m_pHeightZoomMF
->GetValue());
539 if(bZoom
&& ( ( ( aOrigSize
.Height() - (nTop
+ nBottom
)) * nHeightZoom
)
540 / 100 >= aPageSize
.Height()))
542 if(pField
== m_pTopMF
)
544 nTop
= aOrigSize
.Height() -
545 ( aPageSize
.Height() * 100 / nHeightZoom
+ nBottom
);
546 m_pTopMF
->SetValue( m_pWidthMF
->Normalize( nTop
), eUnit
);
550 nBottom
= aOrigSize
.Height() -
551 ( aPageSize
.Height() * 100 / nHeightZoom
+ nTop
);
552 m_pBottomMF
->SetValue( m_pWidthMF
->Normalize( nBottom
), eUnit
);
555 m_pExampleWN
->SetTop( nTop
);
556 m_pExampleWN
->SetBottom( nBottom
);
559 // scale stays, recompute height
560 ZoomHdl(m_pHeightZoomMF
);
563 m_pExampleWN
->Invalidate();
564 // size and border changed -> recompute scale
570 /*--------------------------------------------------------------------
571 description: set original size
572 --------------------------------------------------------------------*/
574 IMPL_LINK_NOARG(SvxGrfCropPage
, OrigSizeHdl
)
576 SfxItemPool
* pPool
= GetItemSet().GetPool();
577 DBG_ASSERT( pPool
, "Wo ist der Pool" );
578 FieldUnit eUnit
= MapToFieldUnit( pPool
->GetMetric( pPool
->GetWhich(
579 SID_ATTR_GRAF_CROP
) ) );
581 long nWidth
= aOrigSize
.Width() -
582 lcl_GetValue( *m_pLeftMF
, eUnit
) -
583 lcl_GetValue( *m_pRightMF
, eUnit
);
584 m_pWidthMF
->SetValue( m_pWidthMF
->Normalize( nWidth
), eUnit
);
585 long nHeight
= aOrigSize
.Height() -
586 lcl_GetValue( *m_pTopMF
, eUnit
) -
587 lcl_GetValue( *m_pBottomMF
, eUnit
);
588 m_pHeightMF
->SetValue( m_pHeightMF
->Normalize( nHeight
), eUnit
);
589 m_pWidthZoomMF
->SetValue(100);
590 m_pHeightZoomMF
->SetValue(100);
594 /*--------------------------------------------------------------------
595 description: compute scale
596 --------------------------------------------------------------------*/
598 void SvxGrfCropPage::CalcZoom()
600 SfxItemPool
* pPool
= GetItemSet().GetPool();
601 DBG_ASSERT( pPool
, "Wo ist der Pool" );
602 FieldUnit eUnit
= MapToFieldUnit( pPool
->GetMetric( pPool
->GetWhich(
603 SID_ATTR_GRAF_CROP
) ) );
605 long nWidth
= lcl_GetValue( *m_pWidthMF
, eUnit
);
606 long nHeight
= lcl_GetValue( *m_pHeightMF
, eUnit
);
607 long nLRBorders
= lcl_GetValue( *m_pLeftMF
, eUnit
) +
608 lcl_GetValue( *m_pRightMF
, eUnit
);
609 long nULBorders
= lcl_GetValue( *m_pTopMF
, eUnit
) +
610 lcl_GetValue( *m_pBottomMF
, eUnit
);
611 sal_uInt16 nZoom
= 0;
613 if( (nDen
= aOrigSize
.Width() - nLRBorders
) > 0)
614 nZoom
= (sal_uInt16
)((( nWidth
* 1000L / nDen
)+5)/10);
615 m_pWidthZoomMF
->SetValue(nZoom
);
616 if( (nDen
= aOrigSize
.Height() - nULBorders
) > 0)
617 nZoom
= (sal_uInt16
)((( nHeight
* 1000L / nDen
)+5)/10);
620 m_pHeightZoomMF
->SetValue(nZoom
);
623 /*--------------------------------------------------------------------
624 description: set minimum/maximum values for the margins
625 --------------------------------------------------------------------*/
627 void SvxGrfCropPage::CalcMinMaxBorder()
629 SfxItemPool
* pPool
= GetItemSet().GetPool();
630 DBG_ASSERT( pPool
, "Wo ist der Pool" );
631 FieldUnit eUnit
= MapToFieldUnit( pPool
->GetMetric( pPool
->GetWhich(
632 SID_ATTR_GRAF_CROP
) ) );
633 long nR
= lcl_GetValue(*m_pRightMF
, eUnit
);
634 long nMinWidth
= (aOrigSize
.Width() * 10) /11;
635 long nMin
= nMinWidth
- (nR
>= 0 ? nR
: 0);
636 m_pLeftMF
->SetMax( m_pLeftMF
->Normalize(nMin
), eUnit
);
638 long nL
= lcl_GetValue(*m_pLeftMF
, eUnit
);
639 nMin
= nMinWidth
- (nL
>= 0 ? nL
: 0);
640 m_pRightMF
->SetMax( m_pRightMF
->Normalize(nMin
), eUnit
);
642 long nUp
= lcl_GetValue( *m_pTopMF
, eUnit
);
643 long nMinHeight
= (aOrigSize
.Height() * 10) /11;
644 nMin
= nMinHeight
- (nUp
>= 0 ? nUp
: 0);
645 m_pBottomMF
->SetMax( m_pBottomMF
->Normalize(nMin
), eUnit
);
647 long nLow
= lcl_GetValue(*m_pBottomMF
, eUnit
);
648 nMin
= nMinHeight
- (nLow
>= 0 ? nLow
: 0);
649 m_pTopMF
->SetMax( m_pTopMF
->Normalize(nMin
), eUnit
);
651 /*--------------------------------------------------------------------
652 description: set spinsize to 1/20 of the original size,
653 fill FixedText with the original size
654 --------------------------------------------------------------------*/
656 void SvxGrfCropPage::GraphicHasChanged( bool bFound
)
660 SfxItemPool
* pPool
= GetItemSet().GetPool();
661 DBG_ASSERT( pPool
, "Wo ist der Pool" );
662 FieldUnit eUnit
= MapToFieldUnit( pPool
->GetMetric( pPool
->GetWhich(
663 SID_ATTR_GRAF_CROP
) ));
665 sal_Int64 nSpin
= m_pLeftMF
->Normalize(aOrigSize
.Width()) / 20;
666 nSpin
= MetricField::ConvertValue( nSpin
, aOrigSize
.Width(), 0,
667 eUnit
, m_pLeftMF
->GetUnit());
669 // if the margin is too big, it is set to 1/3 on both pages
670 long nR
= lcl_GetValue( *m_pRightMF
, eUnit
);
671 long nL
= lcl_GetValue( *m_pLeftMF
, eUnit
);
672 if((nL
+ nR
) < - aOrigSize
.Width())
674 long nVal
= aOrigSize
.Width() / -3;
675 m_pRightMF
->SetValue( m_pRightMF
->Normalize( nVal
), eUnit
);
676 m_pLeftMF
->SetValue( m_pLeftMF
->Normalize( nVal
), eUnit
);
677 m_pExampleWN
->SetLeft(nVal
);
678 m_pExampleWN
->SetRight(nVal
);
680 long nUp
= lcl_GetValue(*m_pTopMF
, eUnit
);
681 long nLow
= lcl_GetValue(*m_pBottomMF
, eUnit
);
682 if((nUp
+ nLow
) < - aOrigSize
.Height())
684 long nVal
= aOrigSize
.Height() / -3;
685 m_pTopMF
->SetValue( m_pTopMF
->Normalize( nVal
), eUnit
);
686 m_pBottomMF
->SetValue( m_pBottomMF
->Normalize( nVal
), eUnit
);
687 m_pExampleWN
->SetTop(nVal
);
688 m_pExampleWN
->SetBottom(nVal
);
691 m_pLeftMF
->SetSpinSize(nSpin
);
692 m_pRightMF
->SetSpinSize(nSpin
);
693 nSpin
= m_pTopMF
->Normalize(aOrigSize
.Height()) / 20;
694 nSpin
= MetricField::ConvertValue( nSpin
, aOrigSize
.Width(), 0,
695 eUnit
, m_pLeftMF
->GetUnit() );
696 m_pTopMF
->SetSpinSize(nSpin
);
697 m_pBottomMF
->SetSpinSize(nSpin
);
699 // display original size
700 const FieldUnit eMetric
= GetModuleFieldUnit( GetItemSet() );
702 ScopedVclPtrInstance
< MetricField
> aFld(this, WB_HIDE
);
703 SetFieldUnit( *aFld
.get(), eMetric
);
704 aFld
->SetDecimalDigits( m_pWidthMF
->GetDecimalDigits() );
705 aFld
->SetMax( LONG_MAX
- 1 );
707 aFld
->SetValue( aFld
->Normalize( aOrigSize
.Width() ), eUnit
);
708 OUString sTemp
= aFld
->GetText();
709 aFld
->SetValue( aFld
->Normalize( aOrigSize
.Height() ), eUnit
);
710 // multiplication sign (U+00D7)
711 sTemp
+= OUString( sal_Unicode (0x00D7) );
712 sTemp
+= aFld
->GetText();
714 if ( aOrigPixelSize
.Width() && aOrigPixelSize
.Height() ) {
715 sal_Int32 ax
= sal_Int32(floor((float)aOrigPixelSize
.Width() /
716 ((float)aOrigSize
.Width()/TWIP_TO_INCH
)+0.5));
717 sal_Int32 ay
= sal_Int32(floor((float)aOrigPixelSize
.Height() /
718 ((float)aOrigSize
.Height()/TWIP_TO_INCH
)+0.5));
720 sTemp
+= CUI_RESSTR( RID_SVXSTR_PPI
);
721 OUString sPPI
= OUString::number(ax
);
722 if (abs(ax
- ay
) > 1) {
723 sPPI
+= OUString( sal_Unicode (0x00D7) );
724 sPPI
+= OUString::number(ay
);
726 sTemp
= sTemp
.replaceAll("%1", sPPI
);
728 m_pOrigSizeFT
->SetText( sTemp
);
731 m_pCropFrame
->Enable(bFound
);
732 m_pScaleFrame
->Enable(bFound
);
733 m_pSizeFrame
->Enable(bFound
);
734 m_pOrigSizeGrid
->Enable(bFound
);
735 m_pZoomConstRB
->Enable(bFound
);
738 IMPL_LINK_NOARG_TYPED(SvxGrfCropPage
, Timeout
, Timer
*, void)
740 DBG_ASSERT(pLastCropField
,"Timeout ohne Feld?");
741 CropHdl(pLastCropField
);
746 IMPL_LINK( SvxGrfCropPage
, CropLoseFocusHdl
, MetricField
*, pField
)
755 IMPL_LINK( SvxGrfCropPage
, CropModifyHdl
, MetricField
*, pField
)
758 pLastCropField
= pField
;
762 Size
SvxGrfCropPage::GetGrfOrigSize( const Graphic
& rGrf
) const
764 const MapMode
aMapTwip( MAP_TWIP
);
765 Size
aSize( rGrf
.GetPrefSize() );
766 if( MAP_PIXEL
== rGrf
.GetPrefMapMode().GetMapUnit() )
767 aSize
= PixelToLogic( aSize
, aMapTwip
);
769 aSize
= OutputDevice::LogicToLogic( aSize
,
770 rGrf
.GetPrefMapMode(), aMapTwip
);
774 /*****************************************************************/
776 SvxCropExample::SvxCropExample( vcl::Window
* pPar
, WinBits nStyle
)
777 : Window( pPar
, nStyle
)
778 , aFrameSize( OutputDevice::LogicToLogic(
779 Size( CM_1_TO_TWIP
/ 2, CM_1_TO_TWIP
/ 2 ),
780 MapMode( MAP_TWIP
), GetMapMode() ))
784 SetBorderStyle( WindowBorderStyle::MONO
);
787 Size
SvxCropExample::GetOptimalSize() const
789 return LogicToPixel(Size(78, 78), MAP_APPFONT
);
792 VCL_BUILDER_DECL_FACTORY(SvxCropExample
)
794 WinBits nWinStyle
= 0;
795 OString sBorder
= VclBuilder::extractCustomProperty(rMap
);
796 if (!sBorder
.isEmpty())
797 nWinStyle
|= WB_BORDER
;
798 rRet
= VclPtr
<SvxCropExample
>::Create(pParent
, nWinStyle
);
801 void SvxCropExample::Paint(vcl::RenderContext
& rRenderContext
, const Rectangle
&)
803 Size
aWinSize(rRenderContext
.PixelToLogic(GetOutputSizePixel()));
804 rRenderContext
.SetLineColor();
805 rRenderContext
.SetFillColor(rRenderContext
.GetSettings().GetStyleSettings().GetWindowColor());
806 rRenderContext
.SetRasterOp(ROP_OVERPAINT
);
807 rRenderContext
.DrawRect(Rectangle(Point(), aWinSize
));
809 rRenderContext
.SetLineColor(Color(COL_WHITE
));
810 Rectangle
aRect(Point((aWinSize
.Width() - aFrameSize
.Width())/2,
811 (aWinSize
.Height() - aFrameSize
.Height())/2),
813 aGrf
.Draw(&rRenderContext
, aRect
.TopLeft(), aRect
.GetSize());
816 aSz
= rRenderContext
.PixelToLogic(aSz
);
817 rRenderContext
.SetFillColor(Color(COL_TRANSPARENT
));
818 rRenderContext
.SetRasterOp(ROP_INVERT
);
819 aRect
.Left() += aTopLeft
.Y();
820 aRect
.Top() += aTopLeft
.X();
821 aRect
.Right() -= aBottomRight
.Y();
822 aRect
.Bottom() -= aBottomRight
.X();
823 rRenderContext
.DrawRect(aRect
);
826 void SvxCropExample::Resize()
828 SetFrameSize(aFrameSize
);
831 void SvxCropExample::SetFrameSize( const Size
& rSz
)
834 if(!aFrameSize
.Width())
835 aFrameSize
.Width() = 1;
836 if(!aFrameSize
.Height())
837 aFrameSize
.Height() = 1;
838 Size
aWinSize( GetOutputSizePixel() );
839 Fraction
aXScale( aWinSize
.Width() * 4, aFrameSize
.Width() * 5 );
840 Fraction
aYScale( aWinSize
.Height() * 4, aFrameSize
.Height() * 5 );
842 if( aYScale
< aXScale
)
845 MapMode
aMapMode( GetMapMode() );
847 aMapMode
.SetScaleX( aXScale
);
848 aMapMode
.SetScaleY( aXScale
);
850 SetMapMode( aMapMode
);
854 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */