xref: /trunk/main/cui/source/dialogs/zoom.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cui.hxx"
30 
31 // include ---------------------------------------------------------------
32 
33 #include <tools/shl.hxx>
34 #include <svl/itemset.hxx>
35 #include <svl/itempool.hxx>
36 #include <sfx2/objsh.hxx>
37 #include <vcl/msgbox.hxx>
38 
39 #define _SVX_ZOOM_CXX
40 
41 #include <cuires.hrc>
42 #include "zoom.hrc"
43 
44 #include "zoom.hxx"
45 #include <svx/zoomitem.hxx>
46 #include <svx/viewlayoutitem.hxx>
47 #include <dialmgr.hxx>
48 #include <svx/zoom_def.hxx>
49 #include <svx/dialogs.hrc>  // RID_SVXDLG_ZOOM
50 #include <layout/layout-pre.hxx>
51 
52 #if ENABLE_LAYOUT
53 #undef CUI_RES
54 #define CUI_RES(x) #x
55 #undef SfxModalDialog
56 #define SfxModalDialog( parent, id ) Dialog( parent, "zoom.xml", id )
57 #endif /* ENABLE_LAYOUT */
58 
59 // static ----------------------------------------------------------------
60 
61 #define SPECIAL_FACTOR  ((sal_uInt16)0xFFFF)
62 
63 // class SvxZoomDialog ---------------------------------------------------
64 
65 sal_uInt16 SvxZoomDialog::GetFactor() const
66 {
67     if ( a100Btn.IsChecked() )
68         return 100;
69     if ( aUserBtn.IsChecked() )
70         return (sal_uInt16)aUserEdit.GetValue();
71     else
72         return SPECIAL_FACTOR;
73 }
74 
75 // -----------------------------------------------------------------------
76 
77 void SvxZoomDialog::SetFactor( sal_uInt16 nNewFactor, sal_uInt16 nBtnId )
78 {
79     aUserEdit.Disable();
80 
81     if ( !nBtnId )
82     {
83         if ( nNewFactor == 100 )
84         {
85             a100Btn.Check();
86             a100Btn.GrabFocus();
87         }
88         else
89         {
90             aUserBtn.Check();
91             aUserEdit.Enable();
92             aUserEdit.SetValue( (long)nNewFactor );
93             aUserEdit.GrabFocus();
94         }
95     }
96     else
97     {
98         aUserEdit.SetValue( (long)nNewFactor );
99 
100         if ( ZOOMBTN_OPTIMAL == nBtnId )
101         {
102             aOptimalBtn.Check();
103             aOptimalBtn.GrabFocus();
104         }
105         else if ( ZOOMBTN_PAGEWIDTH == nBtnId )
106         {
107             aPageWidthBtn.Check();
108             aPageWidthBtn.GrabFocus();
109         }
110         else if ( ZOOMBTN_WHOLEPAGE == nBtnId )
111         {
112             aWholePageBtn.Check();
113             aWholePageBtn.GrabFocus();
114         }
115     }
116 }
117 
118 // -----------------------------------------------------------------------
119 
120 void SvxZoomDialog::HideButton( sal_uInt16 nBtnId )
121 {
122     switch ( nBtnId )
123     {
124         case ZOOMBTN_OPTIMAL: // Optimal-Button
125             aOptimalBtn.Hide();
126             break;
127 
128         case ZOOMBTN_PAGEWIDTH: // Seitenbreite-Button
129             aPageWidthBtn.Hide();
130             break;
131 
132         case ZOOMBTN_WHOLEPAGE: // Ganze Seite-Button
133             aWholePageBtn.Hide();
134             break;
135 
136         default:
137             DBG_ERROR( "Falsche Button-Nummer!!!" );
138     }
139 }
140 
141 // -----------------------------------------------------------------------
142 
143 void SvxZoomDialog::SetLimits( sal_uInt16 nMin, sal_uInt16 nMax )
144 {
145     DBG_ASSERT( nMin < nMax, "invalid limits" );
146     aUserEdit.SetMin( nMin );
147     aUserEdit.SetFirst( nMin );
148     aUserEdit.SetMax( nMax );
149     aUserEdit.SetLast( nMax );
150 }
151 
152 // -----------------------------------------------------------------------
153 
154 SvxZoomDialog::SvxZoomDialog( Window* pParent, const SfxItemSet& rCoreSet ) :
155 
156     SfxModalDialog( pParent, CUI_RES( RID_SVXDLG_ZOOM ) ),
157 
158     aZoomFl         ( this, CUI_RES( FL_ZOOM ) ),
159     aOptimalBtn     ( this, CUI_RES( BTN_OPTIMAL ) ),
160     aWholePageBtn   ( this, CUI_RES( BTN_WHOLE_PAGE ) ),
161     aPageWidthBtn   ( this, CUI_RES( BTN_PAGE_WIDTH ) ),
162     a100Btn         ( this, CUI_RES( BTN_100 ) ),
163     aUserBtn        ( this, CUI_RES( BTN_USER ) ),
164     aUserEdit       ( this, CUI_RES( ED_USER ) ),
165 
166     aViewLayoutFl   ( this, CUI_RES( FL_VIEWLAYOUT ) ),
167     aAutomaticBtn   ( this, CUI_RES( BTN_AUTOMATIC ) ),
168     aSingleBtn      ( this, CUI_RES( BTN_SINGLE ) ),
169     aColumnsBtn     ( this, CUI_RES( BTN_COLUMNS ) ),
170     aColumnsEdit    ( this, CUI_RES( ED_COLUMNS ) ),
171     aBookModeChk    ( this, CUI_RES( CHK_BOOK ) ),
172 
173     aBottomFl       ( this, CUI_RES( FL_BOTTOM ) ),
174     aOKBtn          ( this, CUI_RES( BTN_ZOOM_OK ) ),
175     aCancelBtn      ( this, CUI_RES( BTN_ZOOM_CANCEL ) ),
176     aHelpBtn        ( this, CUI_RES( BTN_ZOOM_HELP ) ),
177 
178     rSet        ( rCoreSet ),
179     pOutSet     ( NULL ),
180     bModified   ( sal_False )
181 
182 {
183 #if ENABLE_LAYOUT
184     SetHelpId (".uno:Zoom");
185 #endif /* ENABLE_LAYOUT */
186     Link aLink = LINK( this, SvxZoomDialog, UserHdl );
187     a100Btn.SetClickHdl( aLink );
188     aOptimalBtn.SetClickHdl( aLink );
189     aPageWidthBtn.SetClickHdl( aLink );
190     aWholePageBtn.SetClickHdl( aLink );
191     aUserBtn.SetClickHdl( aLink );
192 
193     Link aViewLayoutLink = LINK( this, SvxZoomDialog, ViewLayoutUserHdl );
194     aAutomaticBtn.SetClickHdl( aViewLayoutLink );
195     aSingleBtn.SetClickHdl( aViewLayoutLink );
196     aColumnsBtn.SetClickHdl( aViewLayoutLink );
197 
198     Link aViewLayoutSpinLink = LINK( this, SvxZoomDialog, ViewLayoutSpinHdl );
199     aColumnsEdit.SetModifyHdl( aViewLayoutSpinLink );
200 
201     Link aViewLayoutCheckLink = LINK( this, SvxZoomDialog, ViewLayoutCheckHdl );
202     aBookModeChk.SetClickHdl( aViewLayoutCheckLink );
203 
204     aOKBtn.SetClickHdl( LINK( this, SvxZoomDialog, OKHdl ) );
205     aUserEdit.SetModifyHdl( LINK( this, SvxZoomDialog, SpinHdl ) );
206 
207     // Default-Werte
208     sal_uInt16 nValue = 100;
209     sal_uInt16 nMin = 10;
210     sal_uInt16 nMax = 1000;
211 
212     // ggf. erst den alten Wert besorgen
213     const SfxUInt16Item* pOldUserItem = 0;
214     SfxObjectShell* pSh = SfxObjectShell::Current();
215 
216     if ( pSh )
217         pOldUserItem = (const SfxUInt16Item*)pSh->GetItem( SID_ATTR_ZOOM_USER );
218 
219     if ( pOldUserItem )
220         nValue = pOldUserItem->GetValue();
221 
222     // UserEdit initialisieren
223     if ( nMin > nValue )
224         nMin = nValue;
225     if ( nMax < nValue )
226         nMax = nValue;
227     aUserEdit.SetMin( nMin );
228     aUserEdit.SetFirst( nMin );
229     aUserEdit.SetMax( nMax );
230     aUserEdit.SetLast( nMax );
231     aUserEdit.SetValue( nValue );
232 
233     aUserEdit.SetAccessibleRelationLabeledBy( &aUserBtn );
234     aUserEdit.SetAccessibleName(aUserBtn.GetText());
235     aColumnsEdit.SetAccessibleRelationLabeledBy(&aColumnsBtn);
236     aColumnsEdit.SetAccessibleName(aColumnsBtn.GetText());
237     aColumnsEdit.SetAccessibleRelationMemberOf(&aColumnsBtn);
238     aBookModeChk.SetAccessibleRelationLabeledBy(&aColumnsBtn);
239     aBookModeChk.SetAccessibleRelationMemberOf(&aColumnsBtn);
240 
241     const SfxPoolItem& rItem = rSet.Get( rSet.GetPool()->GetWhich( SID_ATTR_ZOOM ) );
242 
243     if ( rItem.ISA(SvxZoomItem) )
244     {
245         const SvxZoomItem& rZoomItem = (const SvxZoomItem&)rItem;
246         const sal_uInt16 nZoom = rZoomItem.GetValue();
247         const SvxZoomType eType = rZoomItem.GetType();
248         const sal_uInt16 nValSet = rZoomItem.GetValueSet();
249         sal_uInt16 nBtnId = 0;
250 
251         switch ( eType )
252         {
253             case SVX_ZOOM_OPTIMAL:
254                 nBtnId = ZOOMBTN_OPTIMAL;
255                 break;
256             case SVX_ZOOM_PAGEWIDTH:
257                 nBtnId = ZOOMBTN_PAGEWIDTH;
258                 break;
259             case SVX_ZOOM_WHOLEPAGE:
260                 nBtnId = ZOOMBTN_WHOLEPAGE;
261                 break;
262             case SVX_ZOOM_PERCENT:
263                 break;
264             case SVX_ZOOM_PAGEWIDTH_NOBORDER:
265                 break;
266         }
267 
268         if ( !(SVX_ZOOM_ENABLE_100 & nValSet) )
269             a100Btn.Disable();
270         if ( !(SVX_ZOOM_ENABLE_OPTIMAL & nValSet) )
271             aOptimalBtn.Disable();
272         if ( !(SVX_ZOOM_ENABLE_PAGEWIDTH & nValSet) )
273             aPageWidthBtn.Disable();
274         if ( !(SVX_ZOOM_ENABLE_WHOLEPAGE & nValSet) )
275             aWholePageBtn.Disable();
276         SetFactor( nZoom, nBtnId );
277     }
278     else
279     {
280         const sal_uInt16 nZoom = ( (const SfxUInt16Item&)rItem ).GetValue();
281         SetFactor( nZoom );
282     }
283 
284     const SfxPoolItem* pViewLayoutItem = 0;
285     if ( SFX_ITEM_SET == rSet.GetItemState( SID_ATTR_VIEWLAYOUT, sal_False, &pViewLayoutItem ) )
286     {
287         const sal_uInt16 nColumns = static_cast<const SvxViewLayoutItem*>(pViewLayoutItem)->GetValue();
288         const bool bBookMode  = static_cast<const SvxViewLayoutItem*>(pViewLayoutItem)->IsBookMode();
289 
290         if ( 0 == nColumns )
291         {
292             aAutomaticBtn.Check();
293             aColumnsEdit.SetValue( 2 );
294             aColumnsEdit.Disable();
295             aBookModeChk.Disable();
296         }
297         else if ( 1 == nColumns)
298         {
299             aSingleBtn.Check();
300             aColumnsEdit.SetValue( 2 );
301             aColumnsEdit.Disable();
302             aBookModeChk.Disable();
303         }
304         else
305         {
306             aColumnsBtn.Check();
307             if ( !bBookMode )
308             {
309                 aColumnsEdit.SetValue( nColumns );
310                 if ( 0 != nColumns % 2 )
311                     aBookModeChk.Disable();
312             }
313             else
314             {
315                 aColumnsEdit.SetValue( nColumns );
316                 aBookModeChk.Check();
317             }
318         }
319     }
320     else
321     {
322         // hide view layout related controls:
323         aViewLayoutFl.Disable();
324         aAutomaticBtn.Disable();
325         aSingleBtn.Disable();
326         aColumnsBtn.Disable();
327         aColumnsEdit.Disable();
328         aBookModeChk.Disable();
329     }
330 
331     FreeResource();
332 }
333 
334 // -----------------------------------------------------------------------
335 
336 SvxZoomDialog::~SvxZoomDialog()
337 {
338     delete pOutSet;
339     pOutSet = 0;
340 }
341 
342 // -----------------------------------------------------------------------
343 
344 IMPL_LINK( SvxZoomDialog, UserHdl, RadioButton *, pBtn )
345 {
346     bModified |= sal_True;
347 
348     if ( pBtn == &aUserBtn )
349     {
350         aUserEdit.Enable();
351         aUserEdit.GrabFocus();
352     }
353     else
354         aUserEdit.Disable();
355     return 0;
356 }
357 
358 // -----------------------------------------------------------------------
359 
360 IMPL_LINK( SvxZoomDialog, SpinHdl, MetricField *, EMPTYARG )
361 {
362     if ( !aUserBtn.IsChecked() )
363         return 0;
364     bModified |= sal_True;
365     return 0;
366 }
367 
368 // -----------------------------------------------------------------------
369 
370 IMPL_LINK( SvxZoomDialog, ViewLayoutUserHdl, RadioButton *, pBtn )
371 {
372     bModified |= sal_True;
373 
374     if ( pBtn == &aAutomaticBtn )
375     {
376         aColumnsEdit.Disable();
377         aBookModeChk.Disable();
378     }
379     else if ( pBtn == &aSingleBtn )
380     {
381         aColumnsEdit.Disable();
382         aBookModeChk.Disable();
383     }
384     else if ( pBtn == &aColumnsBtn )
385     {
386         aColumnsEdit.Enable();
387         aColumnsEdit.GrabFocus();
388         if ( 0 == aColumnsEdit.GetValue() % 2 )
389             aBookModeChk.Enable();
390     }
391     else
392     {
393         DBG_ERROR( "Wrong Button" );
394         return 0;
395     }
396 
397     return 0;
398 }
399 
400 // -----------------------------------------------------------------------
401 
402 IMPL_LINK( SvxZoomDialog, ViewLayoutSpinHdl, MetricField *, pEdt )
403 {
404     if ( pEdt == &aColumnsEdit && !aColumnsBtn.IsChecked() )
405         return 0;
406 
407     if ( 0 == aColumnsEdit.GetValue() % 2 )
408         aBookModeChk.Enable();
409     else
410     {
411         aBookModeChk.Check( sal_False );
412         aBookModeChk.Disable();
413     }
414 
415     bModified |= sal_True;
416 
417     return 0;
418 }
419 
420 // -----------------------------------------------------------------------
421 
422 IMPL_LINK( SvxZoomDialog, ViewLayoutCheckHdl, CheckBox *, pChk )
423 {
424     if ( pChk == &aBookModeChk && !aColumnsBtn.IsChecked() )
425         return 0;
426 
427     bModified |= sal_True;
428 
429     return 0;
430 }
431 
432 // -----------------------------------------------------------------------
433 
434 IMPL_LINK( SvxZoomDialog, OKHdl, Button *, pBtn )
435 {
436     if ( bModified || &aOKBtn != pBtn )
437     {
438         SvxZoomItem aZoomItem( SVX_ZOOM_PERCENT, 0, rSet.GetPool()->GetWhich( SID_ATTR_ZOOM ) );
439         SvxViewLayoutItem aViewLayoutItem( 0, false, rSet.GetPool()->GetWhich( SID_ATTR_VIEWLAYOUT ) );
440 
441         if ( &aOKBtn == pBtn )
442         {
443             sal_uInt16 nFactor = GetFactor();
444 
445             if ( SPECIAL_FACTOR == nFactor )
446             {
447                 if ( aOptimalBtn.IsChecked() )
448                     aZoomItem.SetType( SVX_ZOOM_OPTIMAL );
449                 else if ( aPageWidthBtn.IsChecked() )
450                     aZoomItem.SetType( SVX_ZOOM_PAGEWIDTH );
451                 else if ( aWholePageBtn.IsChecked() )
452                     aZoomItem.SetType( SVX_ZOOM_WHOLEPAGE );
453             }
454             else
455                 aZoomItem.SetValue( nFactor );
456 
457             if ( aAutomaticBtn.IsChecked() )
458             {
459                 aViewLayoutItem.SetValue( 0 );
460                 aViewLayoutItem.SetBookMode( false );
461             }
462             if ( aSingleBtn.IsChecked() )
463             {
464                 aViewLayoutItem.SetValue( 1 );
465                 aViewLayoutItem.SetBookMode( false );
466             }
467             else if ( aColumnsBtn.IsChecked() )
468             {
469                 aViewLayoutItem.SetValue( static_cast<sal_uInt16>(aColumnsEdit.GetValue()) );
470                 aViewLayoutItem.SetBookMode( aBookModeChk.IsChecked() );
471             }
472         }
473         else
474         {
475             DBG_ERROR( "Wrong Button" );
476             return 0;
477         }
478         pOutSet = new SfxItemSet( rSet );
479         pOutSet->Put( aZoomItem );
480 
481         // don't set attribute in case the whole viewlayout stuff is disabled:
482         if ( aViewLayoutFl.IsEnabled() )
483             pOutSet->Put( aViewLayoutItem );
484 
485         // Wert aus dem UserEdit "uber den Dialog hinaus merken
486         SfxObjectShell* pSh = SfxObjectShell::Current();
487 
488         if ( pSh )
489             pSh->PutItem( SfxUInt16Item( SID_ATTR_ZOOM_USER,
490                                          (sal_uInt16)aUserEdit.GetValue() ) );
491         EndDialog( RET_OK );
492     }
493     else
494         EndDialog( RET_CANCEL );
495     return 0;
496 }
497 
498 
499