merged tag ooo/DEV300_m102
[LibreOffice.git] / toolkit / workben / layout / zoom.cxx
blobbf27ad3feaf6b1821dfda8d67af68f5fd7c978f0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 #if !TEST_LAYOUT
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_svx.hxx"
31 #endif /* !TEST_LAYOUT */
33 #ifdef SVX_DLLIMPLEMENTATION
34 #undef SVX_DLLIMPLEMENTATION
35 #endif
37 // include ---------------------------------------------------------------
39 #if TEST_LAYOUT
40 #include <cstdio>
41 #endif /* TEST_LAYOUT */
42 #include <tools/shl.hxx>
43 #include <svtools/itemset.hxx>
44 #include <svtools/itempool.hxx>
45 #include <sfx2/objsh.hxx>
46 #include <vcl/msgbox.hxx>
48 #define _SVX_ZOOM_CXX
50 #include <svx/dialogs.hrc>
51 #if !TEST_LAYOUT
52 #include "zoom.hrc"
53 #endif /* !TEST_LAYOUT */
55 #include "zoom.hxx"
56 #if TEST_LAYOUT
57 #define SVX_ZOOM_OPTIMAL "Zoom optimal"
58 #define SVX_ZOOM_PAGEWIDTH "Zoom page width"
59 #define SVX_ZOOM_WHOLEPAGE "Zoom whole page"
60 struct SvxZoomItem
62 void SetType( char const* s )
64 fputs( s, stderr );
67 void SetValue( int n )
69 fprintf( stderr, "Zoom factor: %d\n", int( n ) );
72 #define SfxPoolItem LAYOUT_SfxPoolItem
73 class LAYOUT_SfxPoolItem
76 struct SvxViewLayoutItem : public LAYOUT_SfxPoolItem
78 bool IsBookMode()
80 return false;
82 USHORT GetValue()
84 return 0;
87 void SetValue( int n )
89 fprintf( stderr, "Zoom factor: %d\n", int( n ) );
91 void SetBookMode( bool )
94 #if 0 // needed for real SfxPoolItem
95 int operator==( const SfxPoolItem& ) const
97 return 0;
99 SfxPoolItem* Clone( SfxItemPool* ) const
101 return 0;
103 #endif
105 #else /* !TEST_LAYOUT */
106 #include <svx/zoomitem.hxx>
107 #include <svx/viewlayoutitem.hxx>
108 #include <svx/dialmgr.hxx>
109 #endif /* !TEST_LAYOUT */
110 #include "zoom_def.hxx"
112 #include <layout/layout-pre.hxx>
114 #if ENABLE_LAYOUT
115 #undef SVX_RES
116 #define SVX_RES(x) #x
117 #undef SfxModalDialog
118 #define SfxModalDialog( parent, id ) Dialog( parent, "zoom.xml", id )
119 #endif /* ENABLE_LAYOUT */
121 // static ----------------------------------------------------------------
123 static USHORT pRanges[] =
125 SID_ATTR_ZOOM,
126 SID_ATTR_ZOOM,
130 #define SPECIAL_FACTOR ((USHORT)0xFFFF)
132 // class SvxZoomDialog ---------------------------------------------------
134 USHORT SvxZoomDialog::GetFactor() const
136 if ( a100Btn.IsChecked() )
137 return 100;
138 if ( aUserBtn.IsChecked() )
139 return (USHORT)aUserEdit.GetValue();
140 else
141 return SPECIAL_FACTOR;
144 // -----------------------------------------------------------------------
146 void SvxZoomDialog::SetFactor( USHORT nNewFactor, USHORT nBtnId )
148 #if TEST_LAYOUT
149 printf ("\t\t set factor %d\n", (int)nNewFactor);
150 #endif /* !TEST_LAYOUT */
151 aUserEdit.Disable();
153 if ( !nBtnId )
155 if ( nNewFactor == 100 )
157 a100Btn.Check();
158 a100Btn.GrabFocus();
160 else
162 aUserBtn.Check();
163 aUserEdit.Enable();
164 aUserEdit.SetValue( (long)nNewFactor );
165 aUserEdit.GrabFocus();
168 else
170 aUserEdit.SetValue( (long)nNewFactor );
172 if ( ZOOMBTN_OPTIMAL == nBtnId )
174 aOptimalBtn.Check();
175 aOptimalBtn.GrabFocus();
177 else if ( ZOOMBTN_PAGEWIDTH == nBtnId )
179 aPageWidthBtn.Check();
180 aPageWidthBtn.GrabFocus();
182 else if ( ZOOMBTN_WHOLEPAGE == nBtnId )
184 aWholePageBtn.Check();
185 aWholePageBtn.GrabFocus();
190 // -----------------------------------------------------------------------
192 void SvxZoomDialog::SetButtonText( USHORT nBtnId, const String& rNewTxt )
194 switch ( nBtnId )
196 case ZOOMBTN_OPTIMAL: // Optimal-Button
197 aOptimalBtn.SetText( rNewTxt );
198 break;
200 case ZOOMBTN_PAGEWIDTH: // Seitenbreite-Button
201 aPageWidthBtn.SetText( rNewTxt );
202 break;
204 case ZOOMBTN_WHOLEPAGE: // Ganze Seite-Button
205 aWholePageBtn.SetText( rNewTxt );
206 break;
208 default:
209 DBG_ERROR( "wrong button number" );
213 // -----------------------------------------------------------------------
215 void SvxZoomDialog::HideButton( USHORT nBtnId )
217 switch ( nBtnId )
219 case ZOOMBTN_OPTIMAL: // Optimal-Button
220 aOptimalBtn.Hide();
221 break;
223 case ZOOMBTN_PAGEWIDTH: // Seitenbreite-Button
224 aPageWidthBtn.Hide();
225 break;
227 case ZOOMBTN_WHOLEPAGE: // Ganze Seite-Button
228 aWholePageBtn.Hide();
229 break;
231 default:
232 DBG_ERROR( "Falsche Button-Nummer!!!" );
236 // -----------------------------------------------------------------------
238 void SvxZoomDialog::SetLimits( USHORT nMin, USHORT nMax )
240 DBG_ASSERT( nMin < nMax, "invalid limits" );
241 aUserEdit.SetMin( nMin );
242 aUserEdit.SetFirst( nMin );
243 aUserEdit.SetMax( nMax );
244 aUserEdit.SetLast( nMax );
247 // -----------------------------------------------------------------------
249 void SvxZoomDialog::SetSpinSize( USHORT nNewSpin )
251 aUserEdit.SetSpinSize( nNewSpin );
254 // -----------------------------------------------------------------------
256 #if TEST_LAYOUT
257 SvxZoomDialog::SvxZoomDialog( Window* pParent ) :
258 #else /* !TEST_LAYOUT */
259 SvxZoomDialog::SvxZoomDialog( Window* pParent, const SfxItemSet& rCoreSet ) :
260 #endif /* !TEST_LAYOUT */
262 SfxModalDialog( pParent, SVX_RES( RID_SVXDLG_ZOOM ) ),
264 aZoomFl ( this, SVX_RES( FL_ZOOM ) ),
265 aOptimalBtn ( this, SVX_RES( BTN_OPTIMAL ) ),
266 aWholePageBtn ( this, SVX_RES( BTN_WHOLE_PAGE ) ),
267 aPageWidthBtn ( this, SVX_RES( BTN_PAGE_WIDTH ) ),
268 a100Btn ( this, SVX_RES( BTN_100 ) ),
269 aUserBtn ( this, SVX_RES( BTN_USER ) ),
270 aUserEdit ( this, SVX_RES( ED_USER ) ),
272 aViewLayoutFl ( this, SVX_RES( FL_VIEWLAYOUT ) ),
273 aAutomaticBtn ( this, SVX_RES( BTN_AUTOMATIC ) ),
274 aSingleBtn ( this, SVX_RES( BTN_SINGLE ) ),
275 aColumnsBtn ( this, SVX_RES( BTN_COLUMNS ) ),
276 aColumnsEdit ( this, SVX_RES( ED_COLUMNS ) ),
277 aBookModeChk ( this, SVX_RES( CHK_BOOK ) ),
279 aBottomFl ( this, SVX_RES( FL_BOTTOM ) ),
280 aOKBtn ( this, SVX_RES( BTN_ZOOM_OK ) ),
281 aCancelBtn ( this, SVX_RES( BTN_ZOOM_CANCEL ) ),
282 aHelpBtn ( this, SVX_RES( BTN_ZOOM_HELP ) ),
284 #if !TEST_LAYOUT
285 rSet ( rCoreSet ),
286 #endif /* !TEST_LAYOUT */
287 pOutSet ( NULL ),
288 bModified ( FALSE )
291 #if ENABLE_LAYOUT
292 SetHelpId (SID_ATTR_ZOOM);
293 #endif /* ENABLE_LAYOUT */
294 Link aLink = LINK( this, SvxZoomDialog, UserHdl );
295 a100Btn.SetClickHdl( aLink );
296 aOptimalBtn.SetClickHdl( aLink );
297 aPageWidthBtn.SetClickHdl( aLink );
298 aWholePageBtn.SetClickHdl( aLink );
299 aUserBtn.SetClickHdl( aLink );
301 Link aViewLayoutLink = LINK( this, SvxZoomDialog, ViewLayoutUserHdl );
302 aAutomaticBtn.SetClickHdl( aViewLayoutLink );
303 aSingleBtn.SetClickHdl( aViewLayoutLink );
304 aColumnsBtn.SetClickHdl( aViewLayoutLink );
306 Link aViewLayoutSpinLink = LINK( this, SvxZoomDialog, ViewLayoutSpinHdl );
307 aColumnsEdit.SetModifyHdl( aViewLayoutSpinLink );
309 Link aViewLayoutCheckLink = LINK( this, SvxZoomDialog, ViewLayoutCheckHdl );
310 aBookModeChk.SetClickHdl( aViewLayoutCheckLink );
312 aOKBtn.SetClickHdl( LINK( this, SvxZoomDialog, OKHdl ) );
313 aUserEdit.SetModifyHdl( LINK( this, SvxZoomDialog, SpinHdl ) );
315 // Default-Werte
316 USHORT nValue = 100;
317 USHORT nMin = 10;
318 USHORT nMax = 1000;
320 #if !TEST_LAYOUT
321 // ggf. erst den alten Wert besorgen
322 const SfxUInt16Item* pOldUserItem = 0;
323 SfxObjectShell* pSh = SfxObjectShell::Current();
325 if ( pSh )
326 pOldUserItem = (const SfxUInt16Item*)pSh->GetItem( SID_ATTR_ZOOM_USER );
328 if ( pOldUserItem )
329 nValue = pOldUserItem->GetValue();
331 // UserEdit initialisieren
332 if ( nMin > nValue )
333 nMin = nValue;
334 if ( nMax < nValue )
335 nMax = nValue;
336 #endif /* !TEST_LAYOUT */
337 aUserEdit.SetMin( nMin );
338 aUserEdit.SetFirst( nMin );
339 aUserEdit.SetMax( nMax );
340 aUserEdit.SetLast( nMax );
341 aUserEdit.SetValue( nValue );
343 #if TEST_LAYOUT
344 USHORT nZoom = 100;
345 SetFactor( nZoom );
346 #else /* !TEST_LAYOUT */
347 const SfxPoolItem& rItem = rSet.Get( rSet.GetPool()->GetWhich( SID_ATTR_ZOOM ) );
349 if ( rItem.ISA(SvxZoomItem) )
351 const SvxZoomItem& rZoomItem = (const SvxZoomItem&)rItem;
352 const USHORT nZoom = rZoomItem.GetValue();
353 const SvxZoomType eType = rZoomItem.GetType();
354 const USHORT nValSet = rZoomItem.GetValueSet();
355 USHORT nBtnId = 0;
357 switch ( eType )
359 case SVX_ZOOM_OPTIMAL:
360 nBtnId = ZOOMBTN_OPTIMAL;
361 break;
362 case SVX_ZOOM_PAGEWIDTH:
363 nBtnId = ZOOMBTN_PAGEWIDTH;
364 break;
365 case SVX_ZOOM_WHOLEPAGE:
366 nBtnId = ZOOMBTN_WHOLEPAGE;
367 break;
368 case SVX_ZOOM_PERCENT:
369 break;
370 case SVX_ZOOM_PAGEWIDTH_NOBORDER:
371 break;
374 if ( !(SVX_ZOOM_ENABLE_100 & nValSet) )
375 a100Btn.Disable();
376 if ( !(SVX_ZOOM_ENABLE_OPTIMAL & nValSet) )
377 aOptimalBtn.Disable();
378 if ( !(SVX_ZOOM_ENABLE_PAGEWIDTH & nValSet) )
379 aPageWidthBtn.Disable();
380 if ( !(SVX_ZOOM_ENABLE_WHOLEPAGE & nValSet) )
381 aWholePageBtn.Disable();
382 SetFactor( nZoom, nBtnId );
384 else
386 const USHORT nZoom = ( (const SfxUInt16Item&)rItem ).GetValue();
387 SetFactor( nZoom );
390 const SfxPoolItem* pViewLayoutItem = 0;
391 if ( SFX_ITEM_SET == rSet.GetItemState( SID_ATTR_VIEWLAYOUT, FALSE, &pViewLayoutItem ) )
393 const USHORT nColumns = static_cast<const SvxViewLayoutItem*>(pViewLayoutItem)->GetValue();
394 const bool bBookMode = static_cast<const SvxViewLayoutItem*>(pViewLayoutItem)->IsBookMode();
396 if ( 0 == nColumns )
398 aAutomaticBtn.Check();
399 aColumnsEdit.SetValue( 2 );
400 aColumnsEdit.Disable();
401 aBookModeChk.Disable();
403 else if ( 1 == nColumns)
405 aSingleBtn.Check();
406 aColumnsEdit.SetValue( 2 );
407 aColumnsEdit.Disable();
408 aBookModeChk.Disable();
410 else
412 aColumnsBtn.Check();
413 if ( !bBookMode )
415 aColumnsEdit.SetValue( nColumns );
416 if ( 0 != nColumns % 2 )
417 aBookModeChk.Disable();
419 else
421 aColumnsEdit.SetValue( nColumns );
422 aBookModeChk.Check();
426 else
428 // hide view layout related controls:
429 aViewLayoutFl.Disable();
430 aAutomaticBtn.Disable();
431 aSingleBtn.Disable();
432 aColumnsBtn.Disable();
433 aColumnsEdit.Disable();
434 aBookModeChk.Disable();
436 #endif /* !TEST_LAYOUT */
438 FreeResource();
441 // -----------------------------------------------------------------------
443 SvxZoomDialog::~SvxZoomDialog()
445 delete pOutSet;
446 pOutSet = 0;
449 // -----------------------------------------------------------------------
451 USHORT* SvxZoomDialog::GetRanges()
453 return pRanges;
456 // -----------------------------------------------------------------------
458 IMPL_LINK( SvxZoomDialog, UserHdl, RadioButton *, pBtn )
460 #if TEST_LAYOUT
461 fprintf( stderr, "SvxZoomDialog::UserHdl\n" );
462 #endif /* TEST_LAYOUT */
463 bModified |= TRUE;
465 if ( pBtn == &aUserBtn )
467 aUserEdit.Enable();
468 aUserEdit.GrabFocus();
470 else
471 aUserEdit.Disable();
472 return 0;
475 // -----------------------------------------------------------------------
477 IMPL_LINK( SvxZoomDialog, SpinHdl, MetricField *, EMPTYARG )
479 if ( !aUserBtn.IsChecked() )
480 return 0;
481 bModified |= TRUE;
482 return 0;
485 // -----------------------------------------------------------------------
487 IMPL_LINK( SvxZoomDialog, ViewLayoutUserHdl, RadioButton *, pBtn )
489 bModified |= TRUE;
491 if ( pBtn == &aAutomaticBtn )
493 aColumnsEdit.Disable();
494 aBookModeChk.Disable();
496 else if ( pBtn == &aSingleBtn )
498 aColumnsEdit.Disable();
499 aBookModeChk.Disable();
501 else if ( pBtn == &aColumnsBtn )
503 aColumnsEdit.Enable();
504 aColumnsEdit.GrabFocus();
505 if ( 0 == aColumnsEdit.GetValue() % 2 )
506 aBookModeChk.Enable();
508 else
510 DBG_ERROR( "Wrong Button" );
511 return 0;
514 return 0;
517 // -----------------------------------------------------------------------
519 IMPL_LINK( SvxZoomDialog, ViewLayoutSpinHdl, MetricField *, pEdt )
521 if ( pEdt == &aColumnsEdit && !aColumnsBtn.IsChecked() )
522 return 0;
524 if ( 0 == aColumnsEdit.GetValue() % 2 )
525 aBookModeChk.Enable();
526 else
528 aBookModeChk.Check( sal_False );
529 aBookModeChk.Disable();
532 bModified |= TRUE;
534 return 0;
537 // -----------------------------------------------------------------------
539 IMPL_LINK( SvxZoomDialog, ViewLayoutCheckHdl, CheckBox *, pChk )
541 if ( pChk == &aBookModeChk && !aColumnsBtn.IsChecked() )
542 return 0;
544 bModified |= TRUE;
546 return 0;
549 // -----------------------------------------------------------------------
551 IMPL_LINK( SvxZoomDialog, OKHdl, Button *, pBtn )
553 if ( bModified || &aOKBtn != pBtn )
555 #if TEST_LAYOUT
556 SvxZoomItem aZoomItem;
557 SvxViewLayoutItem aViewLayoutItem;
558 #else /* !TEST_LAYOUT */
559 SvxZoomItem aZoomItem( SVX_ZOOM_PERCENT, 0, rSet.GetPool()->GetWhich( SID_ATTR_ZOOM ) );
560 SvxViewLayoutItem aViewLayoutItem( 0, false, rSet.GetPool()->GetWhich( SID_ATTR_VIEWLAYOUT ) );
561 #endif /* !TEST_LAYOUT */
563 if ( &aOKBtn == pBtn )
565 USHORT nFactor = GetFactor();
567 if ( SPECIAL_FACTOR == nFactor )
569 if ( aOptimalBtn.IsChecked() )
570 aZoomItem.SetType( SVX_ZOOM_OPTIMAL );
571 else if ( aPageWidthBtn.IsChecked() )
572 aZoomItem.SetType( SVX_ZOOM_PAGEWIDTH );
573 else if ( aWholePageBtn.IsChecked() )
574 aZoomItem.SetType( SVX_ZOOM_WHOLEPAGE );
576 else
577 aZoomItem.SetValue( nFactor );
579 if ( aAutomaticBtn.IsChecked() )
581 aViewLayoutItem.SetValue( 0 );
582 aViewLayoutItem.SetBookMode( false );
584 if ( aSingleBtn.IsChecked() )
586 aViewLayoutItem.SetValue( 1 );
587 aViewLayoutItem.SetBookMode( false );
589 else if ( aColumnsBtn.IsChecked() )
591 aViewLayoutItem.SetValue( static_cast<USHORT>(aColumnsEdit.GetValue()) );
592 aViewLayoutItem.SetBookMode( aBookModeChk.IsChecked() );
595 else
597 DBG_ERROR( "Wrong Button" );
598 return 0;
600 #if !TEST_LAYOUT
601 pOutSet = new SfxItemSet( rSet );
602 pOutSet->Put( aZoomItem );
604 // don't set attribute in case the whole viewlayout stuff is disabled:
605 if ( aViewLayoutFl.IsEnabled() )
606 pOutSet->Put( aViewLayoutItem );
608 // Wert aus dem UserEdit "uber den Dialog hinaus merken
609 SfxObjectShell* pSh = SfxObjectShell::Current();
611 if ( pSh )
612 pSh->PutItem( SfxUInt16Item( SID_ATTR_ZOOM_USER,
613 (UINT16)aUserEdit.GetValue() ) );
614 #endif /* !TEST_LAYOUT */
615 EndDialog( RET_OK );
617 else
618 EndDialog( RET_CANCEL );
619 return 0;