LibreOffice Module cui (master)  1
grfpage.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <memory>
21 #include <svl/eitem.hxx>
22 #include <svl/stritem.hxx>
23 #include <sfx2/app.hxx>
24 #include <sfx2/module.hxx>
25 #include <sfx2/sfxsids.hrc>
26 #include <dialmgr.hxx>
27 #include <svx/dlgutil.hxx>
28 #include <editeng/sizeitem.hxx>
29 #include <editeng/brushitem.hxx>
30 #include <grfpage.hxx>
31 #include <svx/grfcrop.hxx>
32 #include <rtl/ustring.hxx>
33 #include <tools/debug.hxx>
34 #include <tools/fract.hxx>
35 #include <svx/dialogs.hrc>
36 #include <svx/svxids.hrc>
37 #include <strings.hrc>
38 #include <vcl/field.hxx>
39 #include <vcl/settings.hxx>
40 #include <svtools/unitconv.hxx>
41 
42 #define CM_1_TO_TWIP 567
43 #define TWIP_TO_INCH 1440
44 
45 
46 static int lcl_GetValue(const weld::MetricSpinButton& rMetric, FieldUnit eUnit)
47 {
48  return rMetric.denormalize(rMetric.get_value(eUnit));
49 }
50 
51 /*--------------------------------------------------------------------
52  description: crop graphic
53  --------------------------------------------------------------------*/
54 
56  : SfxTabPage(pParent, "cui/ui/croppage.ui", "CropPage", &rSet)
57  , nOldWidth(0)
58  , nOldHeight(0)
59  , bSetOrigSize(false)
60  , m_xCropFrame(m_xBuilder->weld_widget("cropframe"))
61  , m_xZoomConstRB(m_xBuilder->weld_radio_button("keepscale"))
62  , m_xSizeConstRB(m_xBuilder->weld_radio_button("keepsize"))
63  , m_xLeftMF(m_xBuilder->weld_metric_spin_button("left", FieldUnit::CM))
64  , m_xRightMF(m_xBuilder->weld_metric_spin_button("right", FieldUnit::CM))
65  , m_xTopMF(m_xBuilder->weld_metric_spin_button("top", FieldUnit::CM))
66  , m_xBottomMF(m_xBuilder->weld_metric_spin_button("bottom", FieldUnit::CM))
67  , m_xScaleFrame(m_xBuilder->weld_widget("scaleframe"))
68  , m_xWidthZoomMF(m_xBuilder->weld_metric_spin_button("widthzoom", FieldUnit::PERCENT))
69  , m_xHeightZoomMF(m_xBuilder->weld_metric_spin_button("heightzoom", FieldUnit::PERCENT))
70  , m_xSizeFrame(m_xBuilder->weld_widget("sizeframe"))
71  , m_xWidthMF(m_xBuilder->weld_metric_spin_button("width", FieldUnit::CM))
72  , m_xHeightMF(m_xBuilder->weld_metric_spin_button("height", FieldUnit::CM))
73  , m_xOrigSizeGrid(m_xBuilder->weld_widget("origsizegrid"))
74  , m_xOrigSizeFT(m_xBuilder->weld_label("origsizeft"))
75  , m_xOrigSizePB(m_xBuilder->weld_button("origsize"))
76  , m_xExampleWN(new weld::CustomWeld(*m_xBuilder, "preview", m_aExampleWN))
77 {
79 
80  // set the correct metric
81  const FieldUnit eMetric = GetModuleFieldUnit( rSet );
82 
83  SetFieldUnit( *m_xWidthMF, eMetric );
84  SetFieldUnit( *m_xHeightMF, eMetric );
85  SetFieldUnit( *m_xLeftMF, eMetric );
86  SetFieldUnit( *m_xRightMF, eMetric );
87  SetFieldUnit( *m_xTopMF , eMetric );
88  SetFieldUnit( *m_xBottomMF, eMetric );
89 
91  m_xWidthMF->connect_value_changed( aLk );
92  m_xHeightMF->connect_value_changed( aLk );
93 
94  aLk = LINK(this, SvxGrfCropPage, ZoomHdl);
95  m_xWidthZoomMF->connect_value_changed( aLk );
96  m_xHeightZoomMF->connect_value_changed( aLk );
97 
98  aLk = LINK(this, SvxGrfCropPage, CropModifyHdl);
99  m_xLeftMF->connect_value_changed( aLk );
100  m_xRightMF->connect_value_changed( aLk );
101  m_xTopMF->connect_value_changed( aLk );
102  m_xBottomMF->connect_value_changed( aLk );
103 
104  m_xOrigSizePB->connect_clicked(LINK(this, SvxGrfCropPage, OrigSizeHdl));
105 }
106 
108 {
109  disposeOnce();
110 }
111 
113 {
114  m_xExampleWN.reset();
116 }
117 
119 {
120  return VclPtr<SvxGrfCropPage>::Create(pParent, *rSet);
121 }
122 
124 {
125  const SfxPoolItem* pItem;
126  const SfxItemPool& rPool = *rSet->GetPool();
127 
128  if(SfxItemState::SET == rSet->GetItemState( rPool.GetWhich(
129  SID_ATTR_GRAF_KEEP_ZOOM ), true, &pItem ))
130  {
131  if( static_cast<const SfxBoolItem*>(pItem)->GetValue() )
132  m_xZoomConstRB->set_active(true);
133  else
134  m_xSizeConstRB->set_active(true);
135  m_xZoomConstRB->save_state();
136  }
137 
138  sal_uInt16 nW = rPool.GetWhich( SID_ATTR_GRAF_CROP );
139  if( SfxItemState::SET == rSet->GetItemState( nW, true, &pItem))
140  {
141  FieldUnit eUnit = MapToFieldUnit( rSet->GetPool()->GetMetric( nW ));
142 
143  const SvxGrfCrop* pCrop = static_cast<const SvxGrfCrop*>(pItem);
144 
145  m_aExampleWN.SetLeft(pCrop->GetLeft());
146  m_aExampleWN.SetRight(pCrop->GetRight());
147  m_aExampleWN.SetTop(pCrop->GetTop());
148  m_aExampleWN.SetBottom(pCrop->GetBottom());
149 
150  m_xLeftMF->set_value( m_xLeftMF->normalize( pCrop->GetLeft()), eUnit );
151  m_xRightMF->set_value( m_xRightMF->normalize( pCrop->GetRight()), eUnit );
152  m_xTopMF->set_value( m_xTopMF->normalize( pCrop->GetTop()), eUnit );
153  m_xBottomMF->set_value( m_xBottomMF->normalize( pCrop->GetBottom()), eUnit );
154  }
155  else
156  {
157  m_xLeftMF->set_value(0, FieldUnit::NONE);
158  m_xRightMF->set_value(0, FieldUnit::NONE);
159  m_xTopMF->set_value(0, FieldUnit::NONE);
160  m_xBottomMF->set_value(0, FieldUnit::NONE);
161  }
162 
163  m_xLeftMF->save_value();
164  m_xRightMF->save_value();
165  m_xTopMF->save_value();
166  m_xBottomMF->save_value();
167 
168  nW = rPool.GetWhich( SID_ATTR_PAGE_SIZE );
169  if ( SfxItemState::SET == rSet->GetItemState( nW, false, &pItem ) )
170  {
171  // orientation and size from the PageItem
172  FieldUnit eUnit = MapToFieldUnit( rSet->GetPool()->GetMetric( nW ));
173 
174  aPageSize = static_cast<const SvxSizeItem*>(pItem)->GetSize();
175 
176  auto nMin = m_xWidthMF->normalize( 23 );
177  auto nMax = m_xHeightMF->normalize(aPageSize.Height());
178  m_xHeightMF->set_range(nMin, nMax, eUnit);
179  nMax = m_xWidthMF->normalize(aPageSize.Width());
180  m_xWidthMF->set_range(nMin, nMax, eUnit);
181  }
182  else
183  {
186  MapMode( MapUnit::MapTwip ),
187  MapMode( rSet->GetPool()->GetMetric( nW ) ) );
188  }
189 
190  bool bFound = false;
191  if( SfxItemState::SET == rSet->GetItemState( SID_ATTR_GRAF_GRAPHIC, false, &pItem ) )
192  {
193  OUString referer;
194  SfxStringItem const * it = static_cast<SfxStringItem const *>(
195  rSet->GetItem(SID_REFERER));
196  if (it != nullptr) {
197  referer = it->GetValue();
198  }
199  const Graphic* pGrf = static_cast<const SvxBrushItem*>(pItem)->GetGraphic(referer);
200  if( pGrf )
201  {
202  aOrigSize = GetGrfOrigSize( *pGrf );
203  if (pGrf->GetType() == GraphicType::Bitmap && aOrigSize.Width() && aOrigSize.Height())
204  {
205  aOrigPixelSize = pGrf->GetSizePixel();
206  }
207 
208  if( aOrigSize.Width() && aOrigSize.Height() )
209  {
211  m_aExampleWN.SetGraphic( *pGrf );
213 
214  bFound = true;
215  if( !static_cast<const SvxBrushItem*>(pItem)->GetGraphicLink().isEmpty() )
216  aGraphicName = static_cast<const SvxBrushItem*>(pItem)->GetGraphicLink();
217  }
218  }
219  }
220 
221  GraphicHasChanged( bFound );
222  ActivatePage( *rSet );
223 }
224 
226 {
227  const SfxItemPool& rPool = *rSet->GetPool();
228  bool bModified = false;
229  if( m_xWidthMF->get_value_changed_from_saved() ||
230  m_xHeightMF->get_value_changed_from_saved() )
231  {
232  sal_uInt16 nW = rPool.GetWhich( SID_ATTR_GRAF_FRMSIZE );
233  FieldUnit eUnit = MapToFieldUnit( rSet->GetPool()->GetMetric( nW ));
234 
235  std::shared_ptr<SvxSizeItem> aSz(std::make_shared<SvxSizeItem>(nW));
236 
237  // size could already have been set from another page
238  const SfxItemSet* pExSet = GetDialogExampleSet();
239  const SfxPoolItem* pItem = nullptr;
240  if( pExSet && SfxItemState::SET ==pExSet->GetItemState( nW, false, &pItem ) )
241  {
242  aSz.reset(static_cast< SvxSizeItem*>(pItem->Clone()));
243  }
244  else
245  {
246  aSz.reset(static_cast< SvxSizeItem*>(GetItemSet().Get(nW).Clone()));
247  }
248 
249  Size aTmpSz( aSz->GetSize() );
250  if( m_xWidthMF->get_value_changed_from_saved() )
251  aTmpSz.setWidth( lcl_GetValue( *m_xWidthMF, eUnit ) );
252  if( m_xHeightMF->get_value_changed_from_saved() )
253  aTmpSz.setHeight( lcl_GetValue( *m_xHeightMF, eUnit ) );
254  aSz->SetSize( aTmpSz );
255  m_xWidthMF->save_value();
256  m_xHeightMF->save_value();
257 
258  bModified |= nullptr != rSet->Put( *aSz );
259 
260  if( bSetOrigSize )
261  {
262  bModified |= nullptr != rSet->Put( SvxSizeItem( rPool.GetWhich(
263  SID_ATTR_GRAF_FRMSIZE_PERCENT ), Size( 0, 0 )) );
264  }
265  }
266  if( m_xLeftMF->get_value_changed_from_saved() || m_xRightMF->get_value_changed_from_saved() ||
267  m_xTopMF->get_value_changed_from_saved() || m_xBottomMF->get_value_changed_from_saved() )
268  {
269  sal_uInt16 nW = rPool.GetWhich( SID_ATTR_GRAF_CROP );
270  FieldUnit eUnit = MapToFieldUnit( rSet->GetPool()->GetMetric( nW ));
271  std::unique_ptr<SvxGrfCrop> pNew(static_cast<SvxGrfCrop*>(rSet->Get( nW ).Clone()));
272 
273  pNew->SetLeft( lcl_GetValue( *m_xLeftMF, eUnit ) );
274  pNew->SetRight( lcl_GetValue( *m_xRightMF, eUnit ) );
275  pNew->SetTop( lcl_GetValue( *m_xTopMF, eUnit ) );
276  pNew->SetBottom( lcl_GetValue( *m_xBottomMF, eUnit ) );
277  bModified |= nullptr != rSet->Put( *pNew );
278  }
279 
280  if( m_xZoomConstRB->get_state_changed_from_saved() )
281  {
282  bModified |= nullptr != rSet->Put( SfxBoolItem( rPool.GetWhich(
283  SID_ATTR_GRAF_KEEP_ZOOM), m_xZoomConstRB->get_active() ) );
284  }
285 
286  return bModified;
287 }
288 
290 {
291 #ifdef DBG_UTIL
292  SfxItemPool* pPool = GetItemSet().GetPool();
293  DBG_ASSERT( pPool, "Where is the pool?" );
294 #endif
295 
296  bSetOrigSize = false;
297 
298  // Size
299  Size aSize;
300  const SfxPoolItem* pItem;
301  if( SfxItemState::SET == rSet.GetItemState( SID_ATTR_GRAF_FRMSIZE, false, &pItem ) )
302  aSize = static_cast<const SvxSizeItem*>(pItem)->GetSize();
303 
304  nOldWidth = aSize.Width();
305  nOldHeight = aSize.Height();
306 
307  auto nWidth = m_xWidthMF->normalize(nOldWidth);
308  auto nHeight = m_xHeightMF->normalize(nOldHeight);
309 
310  if (nWidth != m_xWidthMF->get_value(FieldUnit::TWIP))
311  m_xWidthMF->set_value(nWidth, FieldUnit::TWIP);
312  m_xWidthMF->save_value();
313 
314  if (nHeight != m_xHeightMF->get_value(FieldUnit::TWIP))
315  m_xHeightMF->set_value(nHeight, FieldUnit::TWIP);
316  m_xHeightMF->save_value();
317 
318  if( SfxItemState::SET == rSet.GetItemState( SID_ATTR_GRAF_GRAPHIC, false, &pItem ) )
319  {
320  const SvxBrushItem& rBrush = *static_cast<const SvxBrushItem*>(pItem);
321  if( !rBrush.GetGraphicLink().isEmpty() &&
322  aGraphicName != rBrush.GetGraphicLink() )
323  aGraphicName = rBrush.GetGraphicLink();
324 
325  OUString referer;
326  SfxStringItem const * it = static_cast<SfxStringItem const *>(
327  rSet.GetItem(SID_REFERER));
328  if (it != nullptr) {
329  referer = it->GetValue();
330  }
331  const Graphic* pGrf = rBrush.GetGraphic(referer);
332  if( pGrf )
333  {
334  m_aExampleWN.SetGraphic( *pGrf );
335  aOrigSize = GetGrfOrigSize( *pGrf );
336  if (pGrf->GetType() == GraphicType::Bitmap && aOrigSize.Width() > 1 && aOrigSize.Height() > 1) {
337  aOrigPixelSize = pGrf->GetSizePixel();
338  }
342  }
343  else
344  GraphicHasChanged( false );
345  }
346 
347  CalcZoom();
348 }
349 
351 {
352  if ( _pSet )
353  FillItemSet( _pSet );
354  return DeactivateRC::LeavePage;
355 }
356 
357 /*--------------------------------------------------------------------
358  description: scale changed, adjust size
359  --------------------------------------------------------------------*/
360 
362 {
363  SfxItemPool* pPool = GetItemSet().GetPool();
364  DBG_ASSERT( pPool, "Where is the pool?" );
365  FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
366  SID_ATTR_GRAF_CROP ) ) );
367 
368  if (&rField == m_xWidthZoomMF.get())
369  {
370  long nLRBorders = lcl_GetValue(*m_xLeftMF, eUnit)
371  +lcl_GetValue(*m_xRightMF, eUnit);
372  m_xWidthMF->set_value( m_xWidthMF->normalize(
373  ((aOrigSize.Width() - nLRBorders) * rField.get_value(FieldUnit::NONE))/100),
374  eUnit);
375  }
376  else
377  {
378  long nULBorders = lcl_GetValue(*m_xTopMF, eUnit)
379  +lcl_GetValue(*m_xBottomMF, eUnit);
380  m_xHeightMF->set_value( m_xHeightMF->normalize(
381  ((aOrigSize.Height() - nULBorders ) * rField.get_value(FieldUnit::NONE))/100) ,
382  eUnit );
383  }
384 }
385 
386 /*--------------------------------------------------------------------
387  description: change size, adjust scale
388  --------------------------------------------------------------------*/
389 
391 {
392  SfxItemPool* pPool = GetItemSet().GetPool();
393  DBG_ASSERT( pPool, "Where is the pool?" );
394  FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
395  SID_ATTR_GRAF_CROP ) ) );
396 
397  Size aSize( lcl_GetValue(*m_xWidthMF, eUnit),
398  lcl_GetValue(*m_xHeightMF, eUnit) );
399 
400  if(&rField == m_xWidthMF.get())
401  {
402  long nWidth = aOrigSize.Width() -
403  ( lcl_GetValue(*m_xLeftMF, eUnit) +
404  lcl_GetValue(*m_xRightMF, eUnit) );
405  if(!nWidth)
406  nWidth++;
407  sal_uInt16 nZoom = static_cast<sal_uInt16>( aSize.Width() * 100 / nWidth);
408  m_xWidthZoomMF->set_value(nZoom, FieldUnit::NONE);
409  }
410  else
411  {
412  long nHeight = aOrigSize.Height() -
413  ( lcl_GetValue(*m_xTopMF, eUnit) +
414  lcl_GetValue(*m_xBottomMF, eUnit));
415  if(!nHeight)
416  nHeight++;
417  sal_uInt16 nZoom = static_cast<sal_uInt16>( aSize.Height() * 100 / nHeight);
418  m_xHeightZoomMF->set_value(nZoom, FieldUnit::NONE);
419  }
420 }
421 
422 /*--------------------------------------------------------------------
423  description: evaluate border
424  --------------------------------------------------------------------*/
425 
426 IMPL_LINK( SvxGrfCropPage, CropModifyHdl, weld::MetricSpinButton&, rField, void )
427 {
428  SfxItemPool* pPool = GetItemSet().GetPool();
429  DBG_ASSERT( pPool, "Where is the pool?" );
430  FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
431  SID_ATTR_GRAF_CROP ) ) );
432 
433  bool bZoom = m_xZoomConstRB->get_active();
434  if (&rField == m_xLeftMF.get() || &rField == m_xRightMF.get())
435  {
436  long nLeft = lcl_GetValue( *m_xLeftMF, eUnit );
437  long nRight = lcl_GetValue( *m_xRightMF, eUnit );
438  long nWidthZoom = static_cast<long>(m_xWidthZoomMF->get_value(FieldUnit::NONE));
439  if (bZoom && nWidthZoom != 0 && ( ( ( aOrigSize.Width() - (nLeft + nRight )) * nWidthZoom )
440  / 100 >= aPageSize.Width() ) )
441  {
442  if (&rField == m_xLeftMF.get())
443  {
444  nLeft = aOrigSize.Width() -
445  ( aPageSize.Width() * 100 / nWidthZoom + nRight );
446  m_xLeftMF->set_value( m_xLeftMF->normalize( nLeft ), eUnit );
447  }
448  else
449  {
450  nRight = aOrigSize.Width() -
451  ( aPageSize.Width() * 100 / nWidthZoom + nLeft );
452  m_xRightMF->set_value( m_xRightMF->normalize( nRight ), eUnit );
453  }
454  }
456  {
457  m_aExampleWN.SetLeft(nRight);
458  m_aExampleWN.SetRight(nLeft);
459  }
460  else
461  {
462  m_aExampleWN.SetLeft(nLeft);
463  m_aExampleWN.SetRight(nRight);
464  }
465  if(bZoom)
466  {
467  // scale stays, recompute width
468  ZoomHdl(*m_xWidthZoomMF);
469  }
470  }
471  else
472  {
473  long nTop = lcl_GetValue( *m_xTopMF, eUnit );
474  long nBottom = lcl_GetValue( *m_xBottomMF, eUnit );
475  long nHeightZoom = static_cast<long>(m_xHeightZoomMF->get_value(FieldUnit::NONE));
476  if(bZoom && ( ( ( aOrigSize.Height() - (nTop + nBottom )) * nHeightZoom)
477  / 100 >= aPageSize.Height()))
478  {
479  assert(nHeightZoom && "div-by-zero");
480  if(&rField == m_xTopMF.get())
481  {
482  nTop = aOrigSize.Height() -
483  ( aPageSize.Height() * 100 / nHeightZoom + nBottom);
484  m_xTopMF->set_value( m_xWidthMF->normalize( nTop ), eUnit );
485  }
486  else
487  {
488  nBottom = aOrigSize.Height() -
489  ( aPageSize.Height() * 100 / nHeightZoom + nTop);
490  m_xBottomMF->set_value( m_xWidthMF->normalize( nBottom ), eUnit );
491  }
492  }
493  m_aExampleWN.SetTop( nTop );
494  m_aExampleWN.SetBottom( nBottom );
495  if(bZoom)
496  {
497  // scale stays, recompute height
498  ZoomHdl(*m_xHeightZoomMF);
499  }
500  }
501  m_aExampleWN.Invalidate();
502  // size and border changed -> recompute scale
503  if(!bZoom)
504  CalcZoom();
505  CalcMinMaxBorder();
506 }
507 /*--------------------------------------------------------------------
508  description: set original size
509  --------------------------------------------------------------------*/
510 
512 {
513  SfxItemPool* pPool = GetItemSet().GetPool();
514  DBG_ASSERT( pPool, "Where is the pool?" );
515  FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
516  SID_ATTR_GRAF_CROP ) ) );
517 
518  long nWidth = aOrigSize.Width() -
519  lcl_GetValue( *m_xLeftMF, eUnit ) -
520  lcl_GetValue( *m_xRightMF, eUnit );
521  m_xWidthMF->set_value( m_xWidthMF->normalize( nWidth ), eUnit );
522  long nHeight = aOrigSize.Height() -
523  lcl_GetValue( *m_xTopMF, eUnit ) -
524  lcl_GetValue( *m_xBottomMF, eUnit );
525  m_xHeightMF->set_value( m_xHeightMF->normalize( nHeight ), eUnit );
526  m_xWidthZoomMF->set_value(100, FieldUnit::NONE);
527  m_xHeightZoomMF->set_value(100, FieldUnit::NONE);
528  bSetOrigSize = true;
529 }
530 /*--------------------------------------------------------------------
531  description: compute scale
532  --------------------------------------------------------------------*/
533 
535 {
536  SfxItemPool* pPool = GetItemSet().GetPool();
537  DBG_ASSERT( pPool, "Where is the pool?" );
538  FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
539  SID_ATTR_GRAF_CROP ) ) );
540 
541  long nWidth = lcl_GetValue( *m_xWidthMF, eUnit );
542  long nHeight = lcl_GetValue( *m_xHeightMF, eUnit );
543  long nLRBorders = lcl_GetValue( *m_xLeftMF, eUnit ) +
544  lcl_GetValue( *m_xRightMF, eUnit );
545  long nULBorders = lcl_GetValue( *m_xTopMF, eUnit ) +
546  lcl_GetValue( *m_xBottomMF, eUnit );
547  sal_uInt16 nZoom = 0;
548  long nDen;
549  if( (nDen = aOrigSize.Width() - nLRBorders) > 0)
550  nZoom = static_cast<sal_uInt16>((( nWidth * 1000 / nDen )+5)/10);
551  m_xWidthZoomMF->set_value(nZoom, FieldUnit::NONE);
552  if( (nDen = aOrigSize.Height() - nULBorders) > 0)
553  nZoom = static_cast<sal_uInt16>((( nHeight * 1000 / nDen )+5)/10);
554  else
555  nZoom = 0;
556  m_xHeightZoomMF->set_value(nZoom, FieldUnit::NONE);
557 }
558 
559 /*--------------------------------------------------------------------
560  description: set minimum/maximum values for the margins
561  --------------------------------------------------------------------*/
562 
564 {
565  SfxItemPool* pPool = GetItemSet().GetPool();
566  DBG_ASSERT( pPool, "Where is the pool?" );
567  FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
568  SID_ATTR_GRAF_CROP ) ) );
569  long nR = lcl_GetValue(*m_xRightMF, eUnit );
570  long nMinWidth = (aOrigSize.Width() * 10) /11;
571  long nMin = nMinWidth - (nR >= 0 ? nR : 0);
572  m_xLeftMF->set_max( m_xLeftMF->normalize(nMin), eUnit );
573 
574  long nL = lcl_GetValue(*m_xLeftMF, eUnit );
575  nMin = nMinWidth - (nL >= 0 ? nL : 0);
576  m_xRightMF->set_max( m_xRightMF->normalize(nMin), eUnit );
577 
578  long nUp = lcl_GetValue( *m_xTopMF, eUnit );
579  long nMinHeight = (aOrigSize.Height() * 10) /11;
580  nMin = nMinHeight - (nUp >= 0 ? nUp : 0);
581  m_xBottomMF->set_max( m_xBottomMF->normalize(nMin), eUnit );
582 
583  long nLow = lcl_GetValue(*m_xBottomMF, eUnit );
584  nMin = nMinHeight - (nLow >= 0 ? nLow : 0);
585  m_xTopMF->set_max( m_xTopMF->normalize(nMin), eUnit );
586 }
587 /*--------------------------------------------------------------------
588  description: set spinsize to 1/20 of the original size,
589  fill FixedText with the original size
590  --------------------------------------------------------------------*/
591 
593 {
594  if( bFound )
595  {
596  SfxItemPool* pPool = GetItemSet().GetPool();
597  DBG_ASSERT( pPool, "Where is the pool?" );
598  FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
599  SID_ATTR_GRAF_CROP ) ));
600 
601  sal_Int64 nSpin = m_xLeftMF->normalize(aOrigSize.Width()) / 20;
602  nSpin = MetricField::ConvertValue( nSpin, aOrigSize.Width(), 0,
603  eUnit, m_xLeftMF->get_unit());
604 
605  // if the margin is too big, it is set to 1/3 on both pages
606  long nR = lcl_GetValue( *m_xRightMF, eUnit );
607  long nL = lcl_GetValue( *m_xLeftMF, eUnit );
608  if((nL + nR) < - aOrigSize.Width())
609  {
610  long nVal = aOrigSize.Width() / -3;
611  m_xRightMF->set_value( m_xRightMF->normalize( nVal ), eUnit );
612  m_xLeftMF->set_value( m_xLeftMF->normalize( nVal ), eUnit );
613  m_aExampleWN.SetLeft(nVal);
614  m_aExampleWN.SetRight(nVal);
615  }
616  long nUp = lcl_GetValue(*m_xTopMF, eUnit );
617  long nLow = lcl_GetValue(*m_xBottomMF, eUnit );
618  if((nUp + nLow) < - aOrigSize.Height())
619  {
620  long nVal = aOrigSize.Height() / -3;
621  m_xTopMF->set_value( m_xTopMF->normalize( nVal ), eUnit );
622  m_xBottomMF->set_value( m_xBottomMF->normalize( nVal ), eUnit );
623  m_aExampleWN.SetTop(nVal);
624  m_aExampleWN.SetBottom(nVal);
625  }
626 
627  m_xLeftMF->set_increments(nSpin, nSpin * 10, FieldUnit::NONE);
628  m_xRightMF->set_increments(nSpin, nSpin * 10, FieldUnit::NONE);
629  nSpin = m_xTopMF->normalize(aOrigSize.Height()) / 20;
630  nSpin = MetricField::ConvertValue( nSpin, aOrigSize.Width(), 0,
631  eUnit, m_xLeftMF->get_unit() );
632  m_xTopMF->set_increments(nSpin, nSpin * 10, FieldUnit::NONE);
633  m_xBottomMF->set_increments(nSpin, nSpin * 10, FieldUnit::NONE);
634 
635  // display original size
636  const FieldUnit eMetric = GetModuleFieldUnit( GetItemSet() );
637 
639  SetFieldUnit( *aFld, eMetric );
640  aFld->SetDecimalDigits(m_xWidthMF->get_digits());
641  aFld->SetMax( LONG_MAX - 1 );
642 
643  aFld->SetValue( aFld->Normalize( aOrigSize.Width() ), eUnit );
644  OUString sTemp = aFld->GetText();
645  aFld->SetValue( aFld->Normalize( aOrigSize.Height() ), eUnit );
646  // multiplication sign (U+00D7)
647  sTemp += OUStringLiteral1(0x00D7) + aFld->GetText();
648 
649  if ( aOrigPixelSize.Width() && aOrigPixelSize.Height() ) {
650  sal_Int32 ax = sal_Int32(floor(static_cast<float>(aOrigPixelSize.Width()) /
651  (static_cast<float>(aOrigSize.Width())/TWIP_TO_INCH)+0.5));
652  sal_Int32 ay = sal_Int32(floor(static_cast<float>(aOrigPixelSize.Height()) /
653  (static_cast<float>(aOrigSize.Height())/TWIP_TO_INCH)+0.5));
654  sTemp += " ";
655  sTemp += CuiResId( RID_SVXSTR_PPI );
656  OUString sPPI = OUString::number(ax);
657  if (abs(ax - ay) > 1) {
658  sPPI += OUStringLiteral1(0x00D7) + OUString::number(ay);
659  }
660  sTemp = sTemp.replaceAll("%1", sPPI);
661  }
662  m_xOrigSizeFT->set_label(sTemp);
663  }
664 
665  m_xCropFrame->set_sensitive(bFound);
666  m_xScaleFrame->set_sensitive(bFound);
667  m_xSizeFrame->set_sensitive(bFound);
668  m_xOrigSizeGrid->set_sensitive(bFound);
669  m_xZoomConstRB->set_sensitive(bFound);
670 }
671 
673 {
674  const MapMode aMapTwip( MapUnit::MapTwip );
675  Size aSize( rGrf.GetPrefSize() );
676  if( MapUnit::MapPixel == rGrf.GetPrefMapMode().GetMapUnit() )
677  aSize = PixelToLogic( aSize, aMapTwip );
678  else
679  aSize = OutputDevice::LogicToLogic( aSize,
680  rGrf.GetPrefMapMode(), aMapTwip );
681  return aSize;
682 }
683 
684 /*****************************************************************/
685 
687  : m_aTopLeft(0, 0)
688  , m_aBottomRight(0, 0)
689 {
690 }
691 
693 {
694  CustomWidgetController::SetDrawingArea(pDrawingArea);
695  OutputDevice& rDevice = pDrawingArea->get_ref_device();
696  Size aSize(rDevice.LogicToPixel(Size(78, 78), MapMode(MapUnit::MapAppFont)));
697  pDrawingArea->set_size_request(aSize.Width(), aSize.Height());
698 
699  m_aMapMode = rDevice.GetMapMode();
701  Size(CM_1_TO_TWIP / 2, CM_1_TO_TWIP / 2),
702  MapMode(MapUnit::MapTwip), m_aMapMode);
703 }
704 
705 void SvxCropExample::Paint(vcl::RenderContext& rRenderContext, const ::tools::Rectangle&)
706 {
707  rRenderContext.Push(PushFlags::MAPMODE | PushFlags::RASTEROP);
708  rRenderContext.SetMapMode(m_aMapMode);
709 
710  Size aWinSize(rRenderContext.PixelToLogic(GetOutputSizePixel()));
711  rRenderContext.SetLineColor();
712  rRenderContext.SetFillColor(rRenderContext.GetSettings().GetStyleSettings().GetWindowColor());
713  rRenderContext.DrawRect(::tools::Rectangle(Point(), aWinSize));
714 
715  rRenderContext.SetLineColor(COL_WHITE);
716  ::tools::Rectangle aRect(Point((aWinSize.Width() - m_aFrameSize.Width())/2,
717  (aWinSize.Height() - m_aFrameSize.Height())/2),
718  m_aFrameSize);
719  m_aGrf.Draw(&rRenderContext, aRect.TopLeft(), aRect.GetSize());
720 
721  rRenderContext.SetFillColor(COL_TRANSPARENT);
722  rRenderContext.SetRasterOp(RasterOp::Invert);
723  aRect.AdjustLeft(m_aTopLeft.Y() );
724  aRect.AdjustTop(m_aTopLeft.X() );
725  aRect.AdjustRight(-m_aBottomRight.Y());
726  aRect.AdjustBottom(-m_aBottomRight.X());
727  rRenderContext.DrawRect(aRect);
728 
729  rRenderContext.Pop();
730 }
731 
733 {
735 }
736 
738 {
739  m_aFrameSize = rSz;
740  if (!m_aFrameSize.Width())
741  m_aFrameSize.setWidth( 1 );
742  if (!m_aFrameSize.Height())
743  m_aFrameSize.setHeight( 1 );
744  Size aWinSize( GetOutputSizePixel() );
745  Fraction aXScale( aWinSize.Width() * 4, m_aFrameSize.Width() * 5 );
746  Fraction aYScale( aWinSize.Height() * 4, m_aFrameSize.Height() * 5 );
747 
748  if( aYScale < aXScale )
749  aXScale = aYScale;
750 
751  m_aMapMode.SetScaleX(aXScale);
752  m_aMapMode.SetScaleY(aXScale);
753 
754  Invalidate();
755 }
756 
757 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
void SetFieldUnit(weld::MetricSpinButton &rField, FieldUnit eUnit, bool bAll)
Point m_aTopLeft
Definition: grfpage.hxx:31
virtual DeactivateRC DeactivatePage(SfxItemSet *pSet) override
Definition: grfpage.cxx:350
FieldUnit
Graphic m_aGrf
Definition: grfpage.hxx:32
MapMode m_aMapMode
Definition: grfpage.hxx:29
Point LogicToLogic(const Point &rPtSource, const MapMode *pMapModeSource, const MapMode *pMapModeDest) const
Size GetSizePixel(const OutputDevice *pRefDevice=nullptr) const
Size aOrigPixelSize
Definition: grfpage.hxx:58
long Height() const
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const =0
virtual void ActivatePage(const SfxItemSet &rSet) override
Definition: grfpage.cxx:289
IMPL_LINK(SvxGrfCropPage, ZoomHdl, weld::MetricSpinButton &, rField, void)
Definition: grfpage.cxx:361
void SetTop(long nVal)
Definition: grfpage.hxx:41
static VclPtr< SfxTabPage > Create(TabPageParent pParent, const SfxItemSet *rSet)
Definition: grfpage.cxx:118
const StyleSettings & GetStyleSettings() const
std::unique_ptr< weld::MetricSpinButton > m_xLeftMF
Definition: grfpage.hxx:69
virtual void Paint(vcl::RenderContext &rRenderContext, const ::tools::Rectangle &rRect) override
Definition: grfpage.cxx:705
const MapMode & GetMapMode() const
void SetLeft(long nVal)
Definition: grfpage.hxx:43
void SetMax(sal_Int64 nNewMax, FieldUnit eInUnit)
virtual void SetDrawingArea(weld::DrawingArea *pDrawingArea) override
Definition: grfpage.cxx:692
std::unique_ptr< weld::MetricSpinButton > m_xTopMF
Definition: grfpage.hxx:71
std::unique_ptr< weld::Widget > m_xScaleFrame
Definition: grfpage.hxx:74
void SetMapMode()
bool bSetOrigSize
Definition: grfpage.hxx:62
Size const & GetOutputSizePixel() const
FieldUnit GetModuleFieldUnit(const SfxItemSet &rSet)
virtual MapUnit GetMetric(sal_uInt16 nWhich) const
constexpr::Color COL_TRANSPARENT(0xFF, 0xFF, 0xFF, 0xFF)
#define TWIP_TO_INCH
Definition: grfpage.cxx:43
std::unique_ptr< weld::Widget > m_xCropFrame
Definition: grfpage.hxx:66
long nOldWidth
Definition: grfpage.hxx:60
std::unique_ptr< weld::MetricSpinButton > m_xBottomMF
Definition: grfpage.hxx:72
virtual bool FillItemSet(SfxItemSet *rSet) override
Definition: grfpage.cxx:225
void SetExchangeSupport()
Size aPageSize
Definition: grfpage.hxx:59
Size m_aFrameSize
Definition: grfpage.hxx:30
std::unique_ptr< weld::MetricSpinButton > m_xRightMF
Definition: grfpage.hxx:70
const Graphic * GetGraphic(OUString const &referer=OUString()) const
virtual OutputDevice & get_ref_device()=0
void CalcZoom()
Definition: grfpage.cxx:534
virtual ~SvxGrfCropPage() override
Definition: grfpage.cxx:107
virtual void dispose() override
std::unique_ptr< weld::MetricSpinButton > m_xHeightZoomMF
Definition: grfpage.hxx:76
WinBits const WB_HIDE
void DrawRect(const tools::Rectangle &rRect)
const SfxItemSet & GetItemSet() const
std::unique_ptr< weld::CustomWeld > m_xExampleWN
Definition: grfpage.hxx:87
void SetLineColor()
void SetScaleX(const Fraction &rScaleX)
void Draw(OutputDevice *pOutDev, const Point &rDestPt) const
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
#define DBG_ASSERT(sCon, aError)
Reference< XAnimationNode > Clone(const Reference< XAnimationNode > &xSourceNode, const SdPage *pSource, const SdPage *pTarget)
GraphicType GetType() const
Size GetGrfOrigSize(const Graphic &) const
Definition: grfpage.cxx:672
Size aOrigSize
Definition: grfpage.hxx:57
void CalcMinMaxBorder()
Definition: grfpage.cxx:563
std::unique_ptr< weld::Button > m_xOrigSizePB
Definition: grfpage.hxx:84
sal_Int64 Normalize(sal_Int64 nValue) const
const OUString & GetValue() const
virtual OUString GetText() const override
void SetFillColor()
const SfxItemSet * GetItemSet(const SfxPoolItem &rAttr)
static bool GetLayoutRTL()
void SetScaleY(const Fraction &rScaleY)
const AllSettings & GetSettings() const
MapUnit GetMapUnit() const
SfxItemPool * GetPool() const
long X() const
const SfxItemSet * GetDialogExampleSet() const
OUString CuiResId(const char *pKey)
Definition: cuiresmgr.cxx:23
const long LONG_MAX
SvxGrfCropPage(TabPageParent pParent, const SfxItemSet &rSet)
Definition: grfpage.cxx:55
#define CM_1_TO_TWIP
Definition: grfpage.cxx:42
void SetFrameSize(const Size &rSz)
Definition: grfpage.cxx:737
Point PixelToLogic(const Point &rDevicePt) const
Point LogicToPixel(const Point &rLogicPt) const
Size GetPrefSize() const
std::unique_ptr< weld::MetricSpinButton > m_xHeightMF
Definition: grfpage.hxx:80
std::unique_ptr< weld::Widget > m_xSizeFrame
Definition: grfpage.hxx:78
const OUString & GetGraphicLink() const
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
virtual void dispose() override
Definition: grfpage.cxx:112
static int lcl_GetValue(const weld::MetricSpinButton &rMetric, FieldUnit eUnit)
Definition: grfpage.cxx:46
std::unique_ptr< weld::MetricSpinButton > m_xWidthZoomMF
Definition: grfpage.hxx:75
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
sal_uInt16 GetWhich(sal_uInt16 nSlot, bool bDeep=true) const
MapMode GetPrefMapMode() const
static sal_Int64 ConvertValue(sal_Int64 nValue, sal_Int64 mnBaseValue, sal_uInt16 nDecDigits, FieldUnit eInUnit, FieldUnit eOutUnit)
std::unique_ptr< weld::RadioButton > m_xSizeConstRB
Definition: grfpage.hxx:68
long nOldHeight
Definition: grfpage.hxx:61
SvxCropExample m_aExampleWN
Definition: grfpage.hxx:64
void SetRasterOp(RasterOp eRasterOp)
virtual void Resize() override
Definition: grfpage.cxx:732
static VclPtr< reference_type > Create(Arg &&...arg)
OUString aGraphicName
Definition: grfpage.hxx:56
const OUString PERCENT("Percent")
void SetDecimalDigits(sal_uInt16 nDigits)
const Color & GetWindowColor() const
constexpr::Color COL_WHITE(0xFF, 0xFF, 0xFF)
std::unique_ptr< weld::RadioButton > m_xZoomConstRB
Definition: grfpage.hxx:67
virtual void SetValue(sal_Int64 nNewValue, FieldUnit eInUnit)
void SetRight(long nVal)
Definition: grfpage.hxx:44
DeactivateRC
FieldUnit MapToFieldUnit(const MapUnit eUnit)
virtual void Reset(const SfxItemSet *rSet) override
Definition: grfpage.cxx:123
int get_value(FieldUnit eDestUnit) const
SVX_DLLPUBLIC MSO_SPT Get(const OUString &)
std::unique_ptr< weld::MetricSpinButton > m_xWidthMF
Definition: grfpage.hxx:79
std::unique_ptr< weld::Label > m_xOrigSizeFT
Definition: grfpage.hxx:83
Point m_aBottomRight
Definition: grfpage.hxx:31
void GraphicHasChanged(bool bFound)
Definition: grfpage.cxx:592
virtual void set_size_request(int nWidth, int nHeight)=0
void SetGraphic(const Graphic &rGrf)
Definition: grfpage.hxx:46
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
void Push(PushFlags nFlags=PushFlags::ALL)
std::unique_ptr< weld::Widget > m_xOrigSizeGrid
Definition: grfpage.hxx:82
void setWidth(long nWidth)
IMPL_LINK_NOARG(SvxGrfCropPage, OrigSizeHdl, weld::Button &, void)
Definition: grfpage.cxx:511
int denormalize(int nValue) const
void SetBottom(long nVal)
Definition: grfpage.hxx:42
long Y() const
void setHeight(long nHeight)