xref: /AOO41X/main/cui/source/tabpages/grfpage.cxx (revision 2ee96f1cdb99d49425d866b1ec4c5567f37285e6)
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 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cui.hxx"
26 
27 #include <tools/shl.hxx>
28 #include <svl/eitem.hxx>
29 #include <sfx2/app.hxx>
30 #include <sfx2/module.hxx>
31 #include <sfx2/sfxsids.hrc>
32 #include <dialmgr.hxx>
33 #include <svx/dlgutil.hxx>
34 #include <editeng/sizeitem.hxx>
35 #include <editeng/brshitem.hxx>
36 #include <grfpage.hxx>
37 #include <svx/grfcrop.hxx>
38 #include <grfpage.hrc>
39 #include <cuires.hrc>
40 #include <svx/dialogs.hrc> // for RID_SVXPAGE_GRFCROP
41 
42 #define CM_1_TO_TWIP        567
43 
44 
lcl_GetValue(MetricField & rMetric,FieldUnit eUnit)45 inline long lcl_GetValue( MetricField& rMetric, FieldUnit eUnit )
46 {
47     return static_cast<long>(rMetric.Denormalize( rMetric.GetValue( eUnit )));
48 }
49 
50 /*--------------------------------------------------------------------
51     Beschreibung: Grafik zuschneiden
52  --------------------------------------------------------------------*/
53 
SvxGrfCropPage(Window * pParent,const SfxItemSet & rSet)54 SvxGrfCropPage::SvxGrfCropPage ( Window *pParent, const SfxItemSet &rSet )
55     : SfxTabPage( pParent,  CUI_RES( RID_SVXPAGE_GRFCROP ), rSet ),
56     aCropFL(        this, CUI_RES( FL_CROP    )),
57     aZoomConstRB(   this, CUI_RES( RB_ZOOMCONST)),
58     aSizeConstRB(   this, CUI_RES( RB_SIZECONST)),
59     aLeftFT(        this, CUI_RES( FT_LEFT    )),
60     aLeftMF(        this, CUI_RES( MF_LEFT    )),
61     aRightFT(       this, CUI_RES( FT_RIGHT   )),
62     aRightMF(       this, CUI_RES( MF_RIGHT   )),
63     aTopFT(         this, CUI_RES( FT_TOP     )),
64     aTopMF(         this, CUI_RES( MF_TOP     )),
65     aBottomFT(      this, CUI_RES( FT_BOTTOM  )),
66     aBottomMF(      this, CUI_RES( MF_BOTTOM  )),
67     aZoomFL(        this, CUI_RES( FL_ZOOM    )),
68     aWidthZoomFT(   this, CUI_RES( FT_WIDTHZOOM )),
69     aWidthZoomMF(   this, CUI_RES( MF_WIDTHZOOM )),
70     aHeightZoomFT(  this, CUI_RES( FT_HEIGHTZOOM)),
71     aHeightZoomMF(  this, CUI_RES( MF_HEIGHTZOOM)),
72     aSizeFL(        this, CUI_RES( FL_SIZE    )),
73     aWidthFT(       this, CUI_RES( FT_WIDTH   )),
74     aWidthMF(       this, CUI_RES( MF_WIDTH   )),
75     aHeightFT(      this, CUI_RES( FT_HEIGHT  )),
76     aHeightMF(      this, CUI_RES( MF_HEIGHT  )),
77     aOrigSizeFT(    this, CUI_RES(FT_ORIG_SIZE)),
78     aOrigSizePB(    this, CUI_RES( PB_ORGSIZE )),
79     aExampleWN(     this, CUI_RES( WN_BSP     )),
80     pLastCropField(0),
81     bInitialized(sal_False),
82     bSetOrigSize(sal_False)
83 {
84     FreeResource();
85 
86     SetExchangeSupport();
87 
88     // set the correct metric
89     const FieldUnit eMetric = GetModuleFieldUnit( rSet );
90 
91     SetFieldUnit( aWidthMF, eMetric );
92     SetFieldUnit( aHeightMF, eMetric );
93     SetFieldUnit( aLeftMF, eMetric );
94     SetFieldUnit( aRightMF, eMetric );
95     SetFieldUnit( aTopMF , eMetric );
96     SetFieldUnit( aBottomMF, eMetric );
97 
98     Link aLk = LINK(this, SvxGrfCropPage, SizeHdl);
99     aWidthMF.SetModifyHdl( aLk );
100     aHeightMF.SetModifyHdl( aLk );
101 
102     aLk = LINK(this, SvxGrfCropPage, ZoomHdl);
103     aWidthZoomMF.SetModifyHdl( aLk );
104     aHeightZoomMF.SetModifyHdl( aLk );
105 
106     aLk = LINK(this, SvxGrfCropPage, CropHdl);
107     aLeftMF.SetDownHdl( aLk );
108     aRightMF.SetDownHdl( aLk );
109     aTopMF.SetDownHdl( aLk );
110     aBottomMF.SetDownHdl( aLk );
111     aLeftMF.SetUpHdl( aLk );
112     aRightMF.SetUpHdl( aLk );
113     aTopMF.SetUpHdl( aLk );
114     aBottomMF.SetUpHdl( aLk );
115 
116     aLk = LINK(this, SvxGrfCropPage, CropModifyHdl);
117     aLeftMF.SetModifyHdl( aLk );
118     aRightMF.SetModifyHdl( aLk );
119     aTopMF.SetModifyHdl( aLk );
120     aBottomMF.SetModifyHdl( aLk );
121 
122     aLk = LINK(this, SvxGrfCropPage, CropLoseFocusHdl);
123     aLeftMF.SetLoseFocusHdl( aLk );
124     aRightMF.SetLoseFocusHdl( aLk );
125     aTopMF.SetLoseFocusHdl( aLk );
126     aBottomMF.SetLoseFocusHdl( aLk );
127 
128     aLk = LINK(this, SvxGrfCropPage, OrigSizeHdl);
129     aOrigSizePB.SetClickHdl( aLk );
130 
131     aTimer.SetTimeoutHdl(LINK(this, SvxGrfCropPage, Timeout));
132     aTimer.SetTimeout( 1500 );
133 
134     aOrigSizePB.SetAccessibleRelationLabeledBy( &aOrigSizeFT );
135 }
136 
137 /*--------------------------------------------------------------------
138     Beschreibung:
139  --------------------------------------------------------------------*/
140 
~SvxGrfCropPage()141 SvxGrfCropPage::~SvxGrfCropPage()
142 {
143     aTimer.Stop();
144 }
145 
146 /*--------------------------------------------------------------------
147     Beschreibung:
148  --------------------------------------------------------------------*/
149 
Create(Window * pParent,const SfxItemSet & rSet)150 SfxTabPage* SvxGrfCropPage::Create(Window *pParent, const SfxItemSet &rSet)
151 {
152     return new SvxGrfCropPage( pParent, rSet );
153 }
154 /*--------------------------------------------------------------------
155     Beschreibung:
156  --------------------------------------------------------------------*/
157 
Reset(const SfxItemSet & rSet)158 void SvxGrfCropPage::Reset( const SfxItemSet &rSet )
159 {
160     const SfxPoolItem* pItem;
161     const SfxItemPool& rPool = *rSet.GetPool();
162 
163     if(SFX_ITEM_SET == rSet.GetItemState( rPool.GetWhich(
164                                     SID_ATTR_GRAF_KEEP_ZOOM ), sal_True, &pItem ))
165     {
166         if( ((const SfxBoolItem*)pItem)->GetValue() )
167             aZoomConstRB.Check();
168         else
169             aSizeConstRB.Check();
170         aZoomConstRB.SaveValue();
171     }
172 
173     sal_uInt16 nW = rPool.GetWhich( SID_ATTR_GRAF_CROP );
174     if( SFX_ITEM_SET == rSet.GetItemState( nW, sal_True, &pItem))
175     {
176         FieldUnit eUnit = MapToFieldUnit( rSet.GetPool()->GetMetric( nW ));
177 
178         SvxGrfCrop* pCrop =  (SvxGrfCrop*)pItem;
179 
180         aExampleWN.SetLeft(     pCrop->GetLeft());
181         aExampleWN.SetRight(    pCrop->GetRight());
182         aExampleWN.SetTop(      pCrop->GetTop());
183         aExampleWN.SetBottom(   pCrop->GetBottom());
184 
185         aLeftMF.SetValue( aLeftMF.Normalize( pCrop->GetLeft()), eUnit );
186         aRightMF.SetValue( aRightMF.Normalize( pCrop->GetRight()), eUnit );
187         aTopMF.SetValue( aTopMF.Normalize( pCrop->GetTop()), eUnit );
188         aBottomMF.SetValue( aBottomMF.Normalize( pCrop->GetBottom()), eUnit );
189     }
190     else
191     {
192         aLeftMF.SetValue( 0 );
193         aRightMF.SetValue( 0 );
194         aTopMF.SetValue( 0 );
195         aBottomMF.SetValue( 0 );
196     }
197 
198     nW = rPool.GetWhich( SID_ATTR_PAGE_SIZE );
199     if ( SFX_ITEM_SET == rSet.GetItemState( nW, sal_False, &pItem ) )
200     {
201         // Orientation und Size aus dem PageItem
202         FieldUnit eUnit = MapToFieldUnit( rSet.GetPool()->GetMetric( nW ));
203 
204         aPageSize = ((const SvxSizeItem*)pItem)->GetSize();
205 
206         sal_Int64 nTmp = aHeightMF.Normalize(aPageSize.Height());
207         aHeightMF.SetMax( nTmp, eUnit );
208         nTmp = aWidthMF.Normalize(aPageSize.Width());
209         aWidthMF.SetMax( nTmp, eUnit );
210         nTmp = aWidthMF.Normalize( 23 );
211         aHeightMF.SetMin( nTmp, eUnit );
212         aWidthMF.SetMin( nTmp, eUnit );
213     }
214     else
215     {
216         aPageSize = OutputDevice::LogicToLogic(
217                         Size( CM_1_TO_TWIP,  CM_1_TO_TWIP ),
218                         MapMode( MAP_TWIP ),
219                         MapMode( (MapUnit)rSet.GetPool()->GetMetric( nW ) ) );
220     }
221 
222     sal_Bool bFound = sal_False;
223     if( SFX_ITEM_SET == rSet.GetItemState( SID_ATTR_GRAF_GRAPHIC, sal_False, &pItem ) )
224     {
225         const Graphic* pGrf = ((SvxBrushItem*)pItem)->GetGraphic();
226         if( pGrf )
227             aOrigSize = GetGrfOrigSize( *pGrf );
228 
229         if( aOrigSize.Width() && aOrigSize.Height() )
230         {
231             CalcMinMaxBorder();
232             aExampleWN.SetGraphic( *pGrf );
233             aExampleWN.SetFrameSize( aOrigSize );
234 
235             bFound = sal_True;
236             if( ((SvxBrushItem*)pItem)->GetGraphicLink() )
237                 aGraphicName = *((SvxBrushItem*)pItem)->GetGraphicLink();
238         }
239     }
240 
241     GraphicHasChanged( bFound );
242     bReset = sal_True;
243     ActivatePage( rSet );
244     bReset = sal_False;
245 }
246 
247 /*--------------------------------------------------------------------
248     Beschreibung:
249  --------------------------------------------------------------------*/
250 
FillItemSet(SfxItemSet & rSet)251 sal_Bool SvxGrfCropPage::FillItemSet(SfxItemSet &rSet)
252 {
253     const SfxItemPool& rPool = *rSet.GetPool();
254     sal_Bool bModified = sal_False;
255     if( aWidthMF.GetSavedValue() != aWidthMF.GetText() ||
256         aHeightMF.GetSavedValue() != aHeightMF.GetText() )
257     {
258         sal_uInt16 nW = rPool.GetWhich( SID_ATTR_GRAF_FRMSIZE );
259         FieldUnit eUnit = MapToFieldUnit( rSet.GetPool()->GetMetric( nW ));
260 
261         SvxSizeItem aSz( nW );
262 
263         // die Groesse koennte schon von einer anderen Page gesetzt worden sein
264         // #44204#
265         const SfxItemSet* pExSet = GetTabDialog() ? GetTabDialog()->GetExampleSet() : NULL;
266         const SfxPoolItem* pItem = 0;
267         if( pExSet && SFX_ITEM_SET ==
268                 pExSet->GetItemState( nW, sal_False, &pItem ) )
269             aSz = *(const SvxSizeItem*)pItem;
270         else
271             aSz = (const SvxSizeItem&)GetItemSet().Get( nW );
272 
273         Size aTmpSz( aSz.GetSize() );
274         if( aWidthMF.GetText() != aWidthMF.GetSavedValue() )
275             aTmpSz.Width() = lcl_GetValue( aWidthMF, eUnit );
276         if( aHeightMF.GetText() != aHeightMF.GetSavedValue() )
277             aTmpSz.Height() = lcl_GetValue( aHeightMF, eUnit );
278         aSz.SetSize( aTmpSz );
279         aWidthMF.SaveValue();
280         aHeightMF.SaveValue();
281 
282         bModified |= 0 != rSet.Put( aSz );
283 
284         if( bSetOrigSize )
285         {
286             bModified |= 0 != rSet.Put( SvxSizeItem( rPool.GetWhich(
287                         SID_ATTR_GRAF_FRMSIZE_PERCENT ), Size( 0, 0 )) );
288         }
289     }
290     if( aLeftMF.IsModified() || aRightMF.IsModified() ||
291         aTopMF.IsModified()  || aBottomMF.IsModified() )
292     {
293         sal_uInt16 nW = rPool.GetWhich( SID_ATTR_GRAF_CROP );
294         FieldUnit eUnit = MapToFieldUnit( rSet.GetPool()->GetMetric( nW ));
295         SvxGrfCrop* pNew = (SvxGrfCrop*)rSet.Get( nW ).Clone();
296 
297         pNew->SetLeft( lcl_GetValue( aLeftMF, eUnit ) );
298         pNew->SetRight( lcl_GetValue( aRightMF, eUnit ) );
299         pNew->SetTop( lcl_GetValue( aTopMF, eUnit ) );
300         pNew->SetBottom( lcl_GetValue( aBottomMF, eUnit ) );
301         bModified |= 0 != rSet.Put( *pNew );
302         delete pNew;
303     }
304 
305     if( aZoomConstRB.GetSavedValue() != aZoomConstRB.IsChecked() )
306     {
307         bModified |= 0 != rSet.Put( SfxBoolItem( rPool.GetWhich(
308                     SID_ATTR_GRAF_KEEP_ZOOM), aZoomConstRB.IsChecked() ) );
309     }
310 
311     bInitialized = sal_False;
312 
313     return bModified;
314 }
315 
316 /*--------------------------------------------------------------------
317     Beschreibung:
318  --------------------------------------------------------------------*/
319 
ActivatePage(const SfxItemSet & rSet)320 void SvxGrfCropPage::ActivatePage(const SfxItemSet& rSet)
321 {
322 #ifdef DBG_UTIL
323     SfxItemPool* pPool = GetItemSet().GetPool();
324     DBG_ASSERT( pPool, "Wo ist der Pool" );
325 #endif
326 
327     bSetOrigSize = sal_False;
328 
329     // Size
330     Size aSize;
331     const SfxPoolItem* pItem;
332     if( SFX_ITEM_SET == rSet.GetItemState( SID_ATTR_GRAF_FRMSIZE, sal_False, &pItem ) )
333         aSize = ((const SvxSizeItem*)pItem)->GetSize();
334 
335     nOldWidth = aSize.Width();
336     nOldHeight = aSize.Height();
337 
338     sal_Int64 nWidth = aWidthMF.Normalize(nOldWidth);
339     sal_Int64 nHeight = aHeightMF.Normalize(nOldHeight);
340 
341     if (nWidth != aWidthMF.GetValue(FUNIT_TWIP))
342     {
343         if(!bReset)
344         {
345             // Wert wurde von Umlauf-Tabpage geaendert und muss
346             // mit Modify-Flag gesetzt werden
347             aWidthMF.SetUserValue(nWidth, FUNIT_TWIP);
348         }
349         else
350             aWidthMF.SetValue(nWidth, FUNIT_TWIP);
351     }
352     aWidthMF.SaveValue();
353 
354     if (nHeight != aHeightMF.GetValue(FUNIT_TWIP))
355     {
356         if (!bReset)
357         {
358             // Wert wurde von Umlauf-Tabpage geaendert und muss
359             // mit Modify-Flag gesetzt werden
360             aHeightMF.SetUserValue(nHeight, FUNIT_TWIP);
361         }
362         else
363             aHeightMF.SetValue(nHeight, FUNIT_TWIP);
364     }
365     aHeightMF.SaveValue();
366     bInitialized = sal_True;
367 
368     if( SFX_ITEM_SET == rSet.GetItemState( SID_ATTR_GRAF_GRAPHIC, sal_False, &pItem ) )
369     {
370         const SvxBrushItem& rBrush = *(SvxBrushItem*)pItem;
371         if( rBrush.GetGraphicLink() &&
372             aGraphicName != *rBrush.GetGraphicLink() )
373             aGraphicName = *rBrush.GetGraphicLink();
374 
375         const Graphic* pGrf = rBrush.GetGraphic();
376         if( pGrf )
377         {
378             aExampleWN.SetGraphic( *pGrf );
379             aOrigSize = GetGrfOrigSize( *pGrf );
380             aExampleWN.SetFrameSize(aOrigSize);
381             GraphicHasChanged( aOrigSize.Width() && aOrigSize.Height() );
382             CalcMinMaxBorder();
383         }
384         else
385             GraphicHasChanged( sal_False );
386     }
387 
388     CalcZoom();
389 }
390 
391 /*--------------------------------------------------------------------
392     Beschreibung:
393  --------------------------------------------------------------------*/
394 
DeactivatePage(SfxItemSet * _pSet)395 int SvxGrfCropPage::DeactivatePage(SfxItemSet *_pSet)
396 {
397     if ( _pSet )
398         FillItemSet( *_pSet );
399     return sal_True;
400 }
401 
402 /*--------------------------------------------------------------------
403     Beschreibung: Massstab geaendert, Groesse anpassen
404  --------------------------------------------------------------------*/
405 
IMPL_LINK(SvxGrfCropPage,ZoomHdl,MetricField *,pField)406 IMPL_LINK( SvxGrfCropPage, ZoomHdl, MetricField *, pField )
407 {
408     SfxItemPool* pPool = GetItemSet().GetPool();
409     DBG_ASSERT( pPool, "Wo ist der Pool" );
410     FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
411                                                     SID_ATTR_GRAF_CROP ) ) );
412 
413     if( pField == &aWidthZoomMF )
414     {
415         long nLRBorders = lcl_GetValue(aLeftMF, eUnit)
416                          +lcl_GetValue(aRightMF, eUnit);
417         aWidthMF.SetValue( aWidthMF.Normalize(
418             ((aOrigSize.Width() - nLRBorders) * pField->GetValue())/100L),
419             eUnit);
420     }
421     else
422     {
423         long nULBorders = lcl_GetValue(aTopMF, eUnit)
424                          +lcl_GetValue(aBottomMF, eUnit);
425         aHeightMF.SetValue( aHeightMF.Normalize(
426             ((aOrigSize.Height() - nULBorders ) * pField->GetValue())/100L) ,
427             eUnit );
428     }
429 
430     return 0;
431 }
432 
433 /*--------------------------------------------------------------------
434     Beschreibung: Groesse aendern, Massstab anpassen
435  --------------------------------------------------------------------*/
436 
IMPL_LINK(SvxGrfCropPage,SizeHdl,MetricField *,pField)437 IMPL_LINK( SvxGrfCropPage, SizeHdl, MetricField *, pField )
438 {
439     SfxItemPool* pPool = GetItemSet().GetPool();
440     DBG_ASSERT( pPool, "Wo ist der Pool" );
441     FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
442                                                     SID_ATTR_GRAF_CROP ) ) );
443 
444     Size aSize( lcl_GetValue(aWidthMF, eUnit),
445                 lcl_GetValue(aHeightMF, eUnit) );
446 
447     if(pField == &aWidthMF)
448     {
449         long nWidth = aOrigSize.Width() -
450                 ( lcl_GetValue(aLeftMF, eUnit) +
451                   lcl_GetValue(aRightMF, eUnit) );
452         if(!nWidth)
453             nWidth++;
454         sal_uInt16 nZoom = (sal_uInt16)( aSize.Width() * 100L / nWidth);
455         aWidthZoomMF.SetValue(nZoom);
456     }
457     else
458     {
459         long nHeight = aOrigSize.Height() -
460                 ( lcl_GetValue(aTopMF, eUnit) +
461                   lcl_GetValue(aBottomMF, eUnit));
462         if(!nHeight)
463             nHeight++;
464         sal_uInt16 nZoom = (sal_uInt16)( aSize.Height() * 100L/ nHeight);
465         aHeightZoomMF.SetValue(nZoom);
466     }
467 
468     return 0;
469 }
470 
471 /*--------------------------------------------------------------------
472     Beschreibung: Raender auswerten
473  --------------------------------------------------------------------*/
474 
IMPL_LINK(SvxGrfCropPage,CropHdl,const MetricField *,pField)475 IMPL_LINK( SvxGrfCropPage, CropHdl, const MetricField *, pField )
476 {
477     SfxItemPool* pPool = GetItemSet().GetPool();
478     DBG_ASSERT( pPool, "Wo ist der Pool" );
479     FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
480                                                     SID_ATTR_GRAF_CROP ) ) );
481 
482     sal_Bool bZoom = aZoomConstRB.IsChecked();
483     if( pField == &aLeftMF || pField == &aRightMF )
484     {
485         long nLeft = lcl_GetValue( aLeftMF, eUnit );
486         long nRight = lcl_GetValue( aRightMF, eUnit );
487         long nWidthZoom = static_cast<long>(aWidthZoomMF.GetValue());
488         if(bZoom && ( ( ( aOrigSize.Width() - (nLeft + nRight )) * nWidthZoom )
489                             / 100 >= aPageSize.Width() ) )
490         {
491             if(pField == &aLeftMF)
492             {
493 //              nLeft = aPageSize.Width() -
494 //                  ((nRight + aOrigSize.Width()) * nWidthZoom) / 100;
495                 nLeft = aOrigSize.Width() -
496                             ( aPageSize.Width() * 100 / nWidthZoom + nRight );
497                 aLeftMF.SetValue( aLeftMF.Normalize( nLeft ), eUnit );
498             }
499             else
500             {
501 //              nRight = aPageSize.Width() -
502 //                  ((nLeft - aOrigSize.Width()) * nWidthZoom) / 100;
503                 nRight = aOrigSize.Width() -
504                             ( aPageSize.Width() * 100 / nWidthZoom + nLeft );
505                 aRightMF.SetValue( aRightMF.Normalize( nRight ), eUnit );
506             }
507         }
508         aExampleWN.SetLeft(nLeft);
509         aExampleWN.SetRight(nRight);
510         if(bZoom)
511         {
512             //Massstab bleibt -> Breite neu berechnen
513             ZoomHdl(&aWidthZoomMF);
514         }
515     }
516     else
517     {
518         long nTop = lcl_GetValue( aTopMF, eUnit );
519         long nBottom = lcl_GetValue( aBottomMF, eUnit );
520         long nHeightZoom = static_cast<long>(aHeightZoomMF.GetValue());
521         if(bZoom && ( ( ( aOrigSize.Height() - (nTop + nBottom )) * nHeightZoom)
522                                             / 100 >= aPageSize.Height()))
523         {
524             if(pField == &aTopMF)
525             {
526 //              nTop = aPageSize.Height() -
527 //                  ((aOrigSize.Height() - nBottom) * nHeightZoom)/ 100;
528                 nTop = aOrigSize.Height() -
529                             ( aPageSize.Height() * 100 / nHeightZoom + nBottom);
530                 aTopMF.SetValue( aWidthMF.Normalize( nTop ), eUnit );
531             }
532             else
533             {
534 //              nBottom = aPageSize.Height() -
535 //                  ((aOrigSize.Height() - nTop)*nHeightZoom) / 100;
536                 nBottom = aOrigSize.Height() -
537                             ( aPageSize.Height() * 100 / nHeightZoom + nTop);
538                 aBottomMF.SetValue( aWidthMF.Normalize( nBottom ), eUnit );
539             }
540         }
541         aExampleWN.SetTop( nTop );
542         aExampleWN.SetBottom( nBottom );
543         if(bZoom)
544         {
545             //Massstab bleibt -> Hoehe neu berechnen
546             ZoomHdl(&aHeightZoomMF);
547         }
548     }
549     aExampleWN.Invalidate();
550     //Groesse und Raender veraendert -> Massstab neu berechnen
551     if(!bZoom)
552         CalcZoom();
553     CalcMinMaxBorder();
554     return 0;
555 }
556 /*--------------------------------------------------------------------
557     Beschreibung: Originalgroesse einstellen
558  --------------------------------------------------------------------*/
559 
IMPL_LINK(SvxGrfCropPage,OrigSizeHdl,PushButton *,EMPTYARG)560 IMPL_LINK( SvxGrfCropPage, OrigSizeHdl, PushButton *, EMPTYARG )
561 {
562     SfxItemPool* pPool = GetItemSet().GetPool();
563     DBG_ASSERT( pPool, "Wo ist der Pool" );
564     FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
565                                                     SID_ATTR_GRAF_CROP ) ) );
566 
567     long nWidth = aOrigSize.Width() -
568         lcl_GetValue( aLeftMF, eUnit ) -
569         lcl_GetValue( aRightMF, eUnit );
570     aWidthMF.SetValue( aWidthMF.Normalize( nWidth ), eUnit );
571     long nHeight = aOrigSize.Height() -
572         lcl_GetValue( aTopMF, eUnit ) -
573         lcl_GetValue( aBottomMF, eUnit );
574     aHeightMF.SetValue( aHeightMF.Normalize( nHeight ), eUnit );
575     aWidthZoomMF.SetValue(100);
576     aHeightZoomMF.SetValue(100);
577     bSetOrigSize = sal_True;
578     return 0;
579 }
580 /*--------------------------------------------------------------------
581     Beschreibung: Massstab berechnen
582  --------------------------------------------------------------------*/
583 
CalcZoom()584 void SvxGrfCropPage::CalcZoom()
585 {
586     SfxItemPool* pPool = GetItemSet().GetPool();
587     DBG_ASSERT( pPool, "Wo ist der Pool" );
588     FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
589                                                     SID_ATTR_GRAF_CROP ) ) );
590 
591     long nWidth = lcl_GetValue( aWidthMF, eUnit );
592     long nHeight = lcl_GetValue( aHeightMF, eUnit );
593     long nLRBorders = lcl_GetValue( aLeftMF, eUnit ) +
594                       lcl_GetValue( aRightMF, eUnit );
595     long nULBorders = lcl_GetValue( aTopMF, eUnit ) +
596                       lcl_GetValue( aBottomMF, eUnit );
597     sal_uInt16 nZoom = 0;
598     long nDen;
599     if( (nDen = aOrigSize.Width() - nLRBorders) > 0)
600         nZoom = (sal_uInt16)((( nWidth  * 1000L / nDen )+5)/10);
601     aWidthZoomMF.SetValue(nZoom);
602     if( (nDen = aOrigSize.Height() - nULBorders) > 0)
603         nZoom = (sal_uInt16)((( nHeight * 1000L / nDen )+5)/10);
604     else
605         nZoom = 0;
606     aHeightZoomMF.SetValue(nZoom);
607 }
608 
609 /*--------------------------------------------------------------------
610     Beschreibung: Minimal-/Maximalwerte fuer die Raender setzen
611  --------------------------------------------------------------------*/
612 
CalcMinMaxBorder()613 void SvxGrfCropPage::CalcMinMaxBorder()
614 {
615     SfxItemPool* pPool = GetItemSet().GetPool();
616     DBG_ASSERT( pPool, "Wo ist der Pool" );
617     FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
618                                                     SID_ATTR_GRAF_CROP ) ) );
619     long nR = lcl_GetValue(aRightMF, eUnit );
620     long nMinWidth = (aOrigSize.Width() * 10) /11;
621     long nMin = nMinWidth - (nR >= 0 ? nR : 0);
622     aLeftMF.SetMax( aLeftMF.Normalize(nMin), eUnit );
623 
624     long nL = lcl_GetValue(aLeftMF, eUnit );
625     nMin = nMinWidth - (nL >= 0 ? nL : 0);
626     aRightMF.SetMax( aRightMF.Normalize(nMin), eUnit );
627 
628     // Zoom nicht unter 2%
629 /*  nMin = (aOrigSize.Width() * 102) /100;
630     aLeftMF.SetMax(aPageSize.Width() - nR - nMin);
631     aRightMF.SetMax(aPageSize.Width() - nL - nMin);
632 */
633     long nUp  = lcl_GetValue( aTopMF, eUnit );
634     long nMinHeight = (aOrigSize.Height() * 10) /11;
635     nMin = nMinHeight - (nUp >= 0 ? nUp : 0);
636     aBottomMF.SetMax( aBottomMF.Normalize(nMin), eUnit );
637 
638     long nLow = lcl_GetValue(aBottomMF, eUnit );
639     nMin = nMinHeight - (nLow >= 0 ? nLow : 0);
640     aTopMF.SetMax( aTopMF.Normalize(nMin), eUnit );
641 
642     // Zoom nicht unter 2%
643 /*  nMin = (aOrigSize.Height() * 102) /100;
644     aTopMF.SetMax(aPageSize.Height() - nLow - nMin);
645     aBottomMF.SetMax(aPageSize.Height() - nUp - nMin);*/
646 }
647 /*--------------------------------------------------------------------
648     Beschreibung:   Spinsize auf 1/20 der Originalgroesse setzen,
649                     FixedText mit der Originalgroesse fuellen
650  --------------------------------------------------------------------*/
651 
GraphicHasChanged(sal_Bool bFound)652 void SvxGrfCropPage::GraphicHasChanged( sal_Bool bFound )
653 {
654     if( bFound )
655     {
656         SfxItemPool* pPool = GetItemSet().GetPool();
657         DBG_ASSERT( pPool, "Wo ist der Pool" );
658         FieldUnit eUnit = MapToFieldUnit( pPool->GetMetric( pPool->GetWhich(
659                                                     SID_ATTR_GRAF_CROP ) ));
660 
661         sal_Int64 nSpin = aLeftMF.Normalize(aOrigSize.Width()) / 20;
662         nSpin = MetricField::ConvertValue( nSpin, aOrigSize.Width(), 0,
663                                             eUnit, aLeftMF.GetUnit());
664 
665         // Ist der Rand zu gross, wird er auf beiden Seiten auf 1/3 eingestellt.
666         long nR = lcl_GetValue( aRightMF, eUnit );
667         long nL = lcl_GetValue( aLeftMF, eUnit );
668         if((nL + nR) < - aOrigSize.Width())
669         {
670             long nVal = aOrigSize.Width() / -3;
671             aRightMF.SetValue( aRightMF.Normalize( nVal ), eUnit );
672             aLeftMF.SetValue( aLeftMF.Normalize( nVal ), eUnit );
673             aExampleWN.SetLeft(nVal);
674             aExampleWN.SetRight(nVal);
675         }
676         long nUp  = lcl_GetValue(aTopMF, eUnit );
677         long nLow = lcl_GetValue(aBottomMF, eUnit );
678         if((nUp + nLow) < - aOrigSize.Height())
679         {
680             long nVal = aOrigSize.Height() / -3;
681             aTopMF.SetValue( aTopMF.Normalize( nVal ), eUnit );
682             aBottomMF.SetValue( aBottomMF.Normalize( nVal ), eUnit );
683             aExampleWN.SetTop(nVal);
684             aExampleWN.SetBottom(nVal);
685         }
686 
687         aLeftMF.SetSpinSize(nSpin);
688         aRightMF.SetSpinSize(nSpin);
689         nSpin = aTopMF.Normalize(aOrigSize.Height()) / 20;
690         nSpin = MetricField::ConvertValue( nSpin, aOrigSize.Width(), 0,
691                                             eUnit, aLeftMF.GetUnit() );
692         aTopMF.SetSpinSize(nSpin);
693         aBottomMF.SetSpinSize(nSpin);
694 
695         //Originalgroesse anzeigen
696         const FieldUnit eMetric = GetModuleFieldUnit( GetItemSet() );
697 
698         MetricField aFld(this, WB_HIDE);
699         SetFieldUnit( aFld, eMetric );
700         aFld.SetDecimalDigits( aWidthMF.GetDecimalDigits() );
701         aFld.SetMax( LONG_MAX - 1 );
702 
703         aFld.SetValue( aFld.Normalize( aOrigSize.Width() ), eUnit );
704         String sTemp = aFld.GetText();
705         aFld.SetValue( aFld.Normalize( aOrigSize.Height() ), eUnit );
706         sTemp += UniString::CreateFromAscii(" x ");
707         sTemp += aFld.GetText();
708         aOrigSizeFT.SetText(sTemp);
709     }
710     aLeftFT         .Enable(bFound);
711     aLeftMF         .Enable(bFound);
712     aRightFT        .Enable(bFound);
713     aRightMF        .Enable(bFound);
714     aTopFT          .Enable(bFound);
715     aTopMF          .Enable(bFound);
716     aBottomFT       .Enable(bFound);
717     aBottomMF       .Enable(bFound);
718     aSizeConstRB    .Enable(bFound);
719     aZoomConstRB    .Enable(bFound);
720     aWidthFT        .Enable(bFound);
721     aWidthMF        .Enable(bFound);
722     aHeightFT       .Enable(bFound);
723     aHeightMF       .Enable(bFound);
724     aWidthZoomFT    .Enable(bFound);
725     aWidthZoomMF    .Enable(bFound);
726     aHeightZoomFT   .Enable(bFound);
727     aHeightZoomMF   .Enable(bFound);
728     aExampleWN      .Enable(bFound);
729     aOrigSizePB     .Enable(bFound);
730     aOrigSizeFT     .Enable(bFound);
731 }
732 
IMPL_LINK(SvxGrfCropPage,Timeout,Timer *,EMPTYARG)733 IMPL_LINK( SvxGrfCropPage, Timeout, Timer *, EMPTYARG )
734 {
735     DBG_ASSERT(pLastCropField,"Timeout ohne Feld?");
736     CropHdl(pLastCropField);
737     pLastCropField = 0;
738     return 0;
739 }
740 
741 
IMPL_LINK(SvxGrfCropPage,CropLoseFocusHdl,MetricField *,pField)742 IMPL_LINK( SvxGrfCropPage, CropLoseFocusHdl, MetricField*, pField )
743 {
744     aTimer.Stop();
745     CropHdl(pField);
746     pLastCropField = 0;
747     return 0;
748 }
749 
750 
IMPL_LINK(SvxGrfCropPage,CropModifyHdl,MetricField *,pField)751 IMPL_LINK( SvxGrfCropPage, CropModifyHdl, MetricField *, pField )
752 {
753     aTimer.Start();
754     pLastCropField = pField;
755     return 0;
756 }
757 
GetGrfOrigSize(const Graphic & rGrf) const758 Size SvxGrfCropPage::GetGrfOrigSize( const Graphic& rGrf ) const
759 {
760     const MapMode aMapTwip( MAP_TWIP );
761     Size aSize( rGrf.GetPrefSize() );
762     if( MAP_PIXEL == rGrf.GetPrefMapMode().GetMapUnit() )
763         aSize = PixelToLogic( aSize, aMapTwip );
764     else
765         aSize = OutputDevice::LogicToLogic( aSize,
766                                         rGrf.GetPrefMapMode(), aMapTwip );
767     return aSize;
768 }
769 
770 /*****************************************************************/
771 
SvxCropExample(Window * pPar,const ResId & rResId)772 SvxGrfCropPage::SvxCropExample::SvxCropExample( Window* pPar,
773                                                 const ResId& rResId )
774     : Window( pPar, rResId ),
775     aFrameSize( OutputDevice::LogicToLogic(
776                             Size( CM_1_TO_TWIP / 2, CM_1_TO_TWIP / 2 ),
777                             MapMode( MAP_TWIP ), GetMapMode() )),
778     aTopLeft(0,0), aBottomRight(0,0)
779 {
780     SetBorderStyle( WINDOW_BORDER_MONO );
781 }
782 
Paint(const Rectangle &)783 void SvxGrfCropPage::SvxCropExample::Paint( const Rectangle& )
784 {
785     Size aWinSize( PixelToLogic(GetOutputSizePixel() ));
786     SetLineColor();
787     SetFillColor( GetSettings().GetStyleSettings().GetWindowColor() );
788     SetRasterOp( ROP_OVERPAINT );
789     DrawRect( Rectangle( Point(), aWinSize ) );
790 
791     SetLineColor( Color( COL_WHITE ) );
792     Rectangle aRect(Point((aWinSize.Width() - aFrameSize.Width())/2,
793                           (aWinSize.Height() - aFrameSize.Height())/2),
794                           aFrameSize );
795     aGrf.Draw( this,  aRect.TopLeft(), aRect.GetSize() );
796 
797     Size aSz( 2, 0 );
798     aSz = PixelToLogic( aSz );
799     SetFillColor( Color( COL_TRANSPARENT ) );
800     SetRasterOp( ROP_INVERT );
801     aRect.Left()    += aTopLeft.Y();
802     aRect.Top()     += aTopLeft.X();
803     aRect.Right()   -= aBottomRight.Y();
804     aRect.Bottom()  -= aBottomRight.X();
805     DrawRect( aRect );
806 }
807 
SetFrameSize(const Size & rSz)808 void SvxGrfCropPage::SvxCropExample::SetFrameSize( const Size& rSz )
809 {
810     aFrameSize = rSz;
811     if(!aFrameSize.Width())
812         aFrameSize.Width() = 1;
813     if(!aFrameSize.Height())
814         aFrameSize.Height() = 1;
815     Size aWinSize( GetOutputSizePixel() );
816     Fraction aXScale( aWinSize.Width() * 4, aFrameSize.Width() * 5 );
817     Fraction aYScale( aWinSize.Height() * 4, aFrameSize.Height() * 5 );
818 
819     if( aYScale < aXScale )
820         aXScale = aYScale;
821 
822     MapMode aMapMode( GetMapMode() );
823 
824     aMapMode.SetScaleX( aXScale );
825     aMapMode.SetScaleY( aXScale );
826 
827     SetMapMode( aMapMode );
828     Invalidate();
829 }
830 
831 
832 
833 
834