xref: /aoo42x/main/toolkit/workben/layout/zoom.cxx (revision cdf0e10c)
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 #if !TEST_LAYOUT
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_svx.hxx"
31 #endif /* !TEST_LAYOUT */
32 
33 #ifdef SVX_DLLIMPLEMENTATION
34 #undef SVX_DLLIMPLEMENTATION
35 #endif
36 
37 // include ---------------------------------------------------------------
38 
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>
47 
48 #define _SVX_ZOOM_CXX
49 
50 #include <svx/dialogs.hrc>
51 #if !TEST_LAYOUT
52 #include "zoom.hrc"
53 #endif /* !TEST_LAYOUT */
54 
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
61 {
62     void SetType( char const* s )
63     {
64         fputs( s, stderr );
65     }
66 
67     void SetValue( int n )
68     {
69         fprintf( stderr, "Zoom factor: %d\n", int( n ) );
70     }
71 };
72 #define SfxPoolItem LAYOUT_SfxPoolItem
73 class LAYOUT_SfxPoolItem
74 {
75 };
76 struct SvxViewLayoutItem : public LAYOUT_SfxPoolItem
77 {
78     bool IsBookMode()
79     {
80         return false;
81     }
82     USHORT GetValue()
83     {
84         return 0;
85     }
86 
87     void SetValue( int n )
88     {
89         fprintf( stderr, "Zoom factor: %d\n", int( n ) );
90     }
91     void SetBookMode( bool )
92     {
93     }
94 #if 0 // needed for real SfxPoolItem
95     int operator==( const SfxPoolItem& ) const
96     {
97         return 0;
98     }
99     SfxPoolItem* Clone( SfxItemPool* ) const
100     {
101         return 0;
102     }
103 #endif
104 };
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"
111 
112 #include <layout/layout-pre.hxx>
113 
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 */
120 
121 // static ----------------------------------------------------------------
122 
123 static USHORT pRanges[] =
124 {
125 	SID_ATTR_ZOOM,
126 	SID_ATTR_ZOOM,
127 	0
128 };
129 
130 #define SPECIAL_FACTOR	((USHORT)0xFFFF)
131 
132 // class SvxZoomDialog ---------------------------------------------------
133 
134 USHORT SvxZoomDialog::GetFactor() const
135 {
136     if ( a100Btn.IsChecked() )
137 		return 100;
138     if ( aUserBtn.IsChecked() )
139 		return (USHORT)aUserEdit.GetValue();
140 	else
141 		return SPECIAL_FACTOR;
142 }
143 
144 // -----------------------------------------------------------------------
145 
146 void SvxZoomDialog::SetFactor( USHORT nNewFactor, USHORT nBtnId )
147 {
148 #if TEST_LAYOUT
149     printf ("\t\t set factor %d\n", (int)nNewFactor);
150 #endif /* !TEST_LAYOUT */
151 	aUserEdit.Disable();
152 
153 	if ( !nBtnId )
154 	{
155         if ( nNewFactor == 100 )
156 		{
157 			a100Btn.Check();
158 			a100Btn.GrabFocus();
159 		}
160         else
161 		{
162 			aUserBtn.Check();
163 			aUserEdit.Enable();
164 			aUserEdit.SetValue( (long)nNewFactor );
165 			aUserEdit.GrabFocus();
166 		}
167 	}
168 	else
169 	{
170 		aUserEdit.SetValue( (long)nNewFactor );
171 
172 		if ( ZOOMBTN_OPTIMAL == nBtnId )
173 		{
174 			aOptimalBtn.Check();
175 			aOptimalBtn.GrabFocus();
176 		}
177 		else if ( ZOOMBTN_PAGEWIDTH == nBtnId )
178 		{
179 			aPageWidthBtn.Check();
180 			aPageWidthBtn.GrabFocus();
181 		}
182 		else if ( ZOOMBTN_WHOLEPAGE == nBtnId )
183 		{
184 			aWholePageBtn.Check();
185 			aWholePageBtn.GrabFocus();
186 		}
187 	}
188 }
189 
190 // -----------------------------------------------------------------------
191 
192 void SvxZoomDialog::SetButtonText( USHORT nBtnId, const String& rNewTxt )
193 {
194 	switch ( nBtnId )
195 	{
196 		case ZOOMBTN_OPTIMAL: // Optimal-Button
197 			aOptimalBtn.SetText( rNewTxt );
198 			break;
199 
200 		case ZOOMBTN_PAGEWIDTH: // Seitenbreite-Button
201 			aPageWidthBtn.SetText( rNewTxt );
202 			break;
203 
204 		case ZOOMBTN_WHOLEPAGE: // Ganze Seite-Button
205 			aWholePageBtn.SetText( rNewTxt );
206 			break;
207 
208 		default:
209 			DBG_ERROR( "wrong button number" );
210 	}
211 }
212 
213 // -----------------------------------------------------------------------
214 
215 void SvxZoomDialog::HideButton( USHORT nBtnId )
216 {
217 	switch ( nBtnId )
218 	{
219 		case ZOOMBTN_OPTIMAL: // Optimal-Button
220 			aOptimalBtn.Hide();
221 			break;
222 
223 		case ZOOMBTN_PAGEWIDTH: // Seitenbreite-Button
224 			aPageWidthBtn.Hide();
225 			break;
226 
227 		case ZOOMBTN_WHOLEPAGE: // Ganze Seite-Button
228 			aWholePageBtn.Hide();
229 			break;
230 
231 		default:
232 			DBG_ERROR( "Falsche Button-Nummer!!!" );
233 	}
234 }
235 
236 // -----------------------------------------------------------------------
237 
238 void SvxZoomDialog::SetLimits( USHORT nMin, USHORT nMax )
239 {
240 	DBG_ASSERT( nMin < nMax, "invalid limits" );
241     aUserEdit.SetMin( nMin );
242 	aUserEdit.SetFirst( nMin );
243     aUserEdit.SetMax( nMax );
244 	aUserEdit.SetLast( nMax );
245 }
246 
247 // -----------------------------------------------------------------------
248 
249 void SvxZoomDialog::SetSpinSize( USHORT nNewSpin )
250 {
251 	aUserEdit.SetSpinSize( nNewSpin );
252 }
253 
254 // -----------------------------------------------------------------------
255 
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 */
261 
262 	SfxModalDialog( pParent, SVX_RES( RID_SVXDLG_ZOOM ) ),
263 
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 ) ),
271 
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 ) ),
278 
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 ) ),
283 
284 #if !TEST_LAYOUT
285 	rSet		( rCoreSet ),
286 #endif /* !TEST_LAYOUT */
287 	pOutSet     ( NULL ),
288 	bModified	( FALSE )
289 
290 {
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 );
300 
301     Link aViewLayoutLink = LINK( this, SvxZoomDialog, ViewLayoutUserHdl );
302     aAutomaticBtn.SetClickHdl( aViewLayoutLink );
303     aSingleBtn.SetClickHdl( aViewLayoutLink );
304     aColumnsBtn.SetClickHdl( aViewLayoutLink );
305 
306     Link aViewLayoutSpinLink = LINK( this, SvxZoomDialog, ViewLayoutSpinHdl );
307     aColumnsEdit.SetModifyHdl( aViewLayoutSpinLink );
308 
309     Link aViewLayoutCheckLink = LINK( this, SvxZoomDialog, ViewLayoutCheckHdl );
310     aBookModeChk.SetClickHdl( aViewLayoutCheckLink );
311 
312     aOKBtn.SetClickHdl( LINK( this, SvxZoomDialog, OKHdl ) );
313 	aUserEdit.SetModifyHdl( LINK( this, SvxZoomDialog, SpinHdl ) );
314 
315 	// Default-Werte
316 	USHORT nValue = 100;
317 	USHORT nMin = 10;
318 	USHORT nMax = 1000;
319 
320 #if !TEST_LAYOUT
321 	// ggf. erst den alten Wert besorgen
322 	const SfxUInt16Item* pOldUserItem = 0;
323 	SfxObjectShell* pSh = SfxObjectShell::Current();
324 
325 	if ( pSh )
326 		pOldUserItem = (const SfxUInt16Item*)pSh->GetItem( SID_ATTR_ZOOM_USER );
327 
328 	if ( pOldUserItem )
329 		nValue = pOldUserItem->GetValue();
330 
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 );
342 
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 ) );
348 
349 	if ( rItem.ISA(SvxZoomItem) )
350 	{
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;
356 
357 		switch ( eType )
358 		{
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;
372 		}
373 
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 );
383 	}
384 	else
385 	{
386         const USHORT nZoom = ( (const SfxUInt16Item&)rItem ).GetValue();
387         SetFactor( nZoom );
388 	}
389 
390     const SfxPoolItem* pViewLayoutItem = 0;
391     if ( SFX_ITEM_SET == rSet.GetItemState( SID_ATTR_VIEWLAYOUT, FALSE, &pViewLayoutItem ) )
392     {
393         const USHORT nColumns = static_cast<const SvxViewLayoutItem*>(pViewLayoutItem)->GetValue();
394         const bool bBookMode  = static_cast<const SvxViewLayoutItem*>(pViewLayoutItem)->IsBookMode();
395 
396         if ( 0 == nColumns )
397         {
398             aAutomaticBtn.Check();
399             aColumnsEdit.SetValue( 2 );
400             aColumnsEdit.Disable();
401             aBookModeChk.Disable();
402         }
403         else if ( 1 == nColumns)
404         {
405             aSingleBtn.Check();
406             aColumnsEdit.SetValue( 2 );
407             aColumnsEdit.Disable();
408             aBookModeChk.Disable();
409         }
410         else
411         {
412             aColumnsBtn.Check();
413             if ( !bBookMode )
414             {
415                 aColumnsEdit.SetValue( nColumns );
416                 if ( 0 != nColumns % 2 )
417                     aBookModeChk.Disable();
418             }
419             else
420             {
421                 aColumnsEdit.SetValue( nColumns );
422                 aBookModeChk.Check();
423             }
424         }
425     }
426     else
427     {
428         // hide view layout related controls:
429         aViewLayoutFl.Disable();
430         aAutomaticBtn.Disable();
431         aSingleBtn.Disable();
432         aColumnsBtn.Disable();
433         aColumnsEdit.Disable();
434         aBookModeChk.Disable();
435     }
436 #endif /* !TEST_LAYOUT */
437 
438 	FreeResource();
439 }
440 
441 // -----------------------------------------------------------------------
442 
443 SvxZoomDialog::~SvxZoomDialog()
444 {
445 	delete pOutSet;
446 	pOutSet = 0;
447 }
448 
449 // -----------------------------------------------------------------------
450 
451 USHORT* SvxZoomDialog::GetRanges()
452 {
453 	return pRanges;
454 }
455 
456 // -----------------------------------------------------------------------
457 
458 IMPL_LINK( SvxZoomDialog, UserHdl, RadioButton *, pBtn )
459 {
460 #if TEST_LAYOUT
461     fprintf( stderr, "SvxZoomDialog::UserHdl\n" );
462 #endif /* TEST_LAYOUT */
463 	bModified |= TRUE;
464 
465 	if ( pBtn == &aUserBtn )
466 	{
467 		aUserEdit.Enable();
468 		aUserEdit.GrabFocus();
469 	}
470 	else
471 		aUserEdit.Disable();
472 	return 0;
473 }
474 
475 // -----------------------------------------------------------------------
476 
477 IMPL_LINK( SvxZoomDialog, SpinHdl, MetricField *, EMPTYARG )
478 {
479 	if ( !aUserBtn.IsChecked() )
480 		return 0;
481 	bModified |= TRUE;
482 	return 0;
483 }
484 
485 // -----------------------------------------------------------------------
486 
487 IMPL_LINK( SvxZoomDialog, ViewLayoutUserHdl, RadioButton *, pBtn )
488 {
489     bModified |= TRUE;
490 
491     if ( pBtn == &aAutomaticBtn )
492     {
493         aColumnsEdit.Disable();
494         aBookModeChk.Disable();
495     }
496     else if ( pBtn == &aSingleBtn )
497     {
498         aColumnsEdit.Disable();
499         aBookModeChk.Disable();
500     }
501     else if ( pBtn == &aColumnsBtn )
502     {
503         aColumnsEdit.Enable();
504         aColumnsEdit.GrabFocus();
505         if ( 0 == aColumnsEdit.GetValue() % 2 )
506             aBookModeChk.Enable();
507     }
508     else
509     {
510         DBG_ERROR( "Wrong Button" );
511         return 0;
512     }
513 
514     return 0;
515 }
516 
517 // -----------------------------------------------------------------------
518 
519 IMPL_LINK( SvxZoomDialog, ViewLayoutSpinHdl, MetricField *, pEdt )
520 {
521     if ( pEdt == &aColumnsEdit && !aColumnsBtn.IsChecked() )
522         return 0;
523 
524     if ( 0 == aColumnsEdit.GetValue() % 2 )
525         aBookModeChk.Enable();
526     else
527     {
528         aBookModeChk.Check( sal_False );
529         aBookModeChk.Disable();
530     }
531 
532     bModified |= TRUE;
533 
534     return 0;
535 }
536 
537 // -----------------------------------------------------------------------
538 
539 IMPL_LINK( SvxZoomDialog, ViewLayoutCheckHdl, CheckBox *, pChk )
540 {
541     if ( pChk == &aBookModeChk && !aColumnsBtn.IsChecked() )
542         return 0;
543 
544     bModified |= TRUE;
545 
546     return 0;
547 }
548 
549 // -----------------------------------------------------------------------
550 
551 IMPL_LINK( SvxZoomDialog, OKHdl, Button *, pBtn )
552 {
553 	if ( bModified || &aOKBtn != pBtn )
554 	{
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 */
562 
563 		if ( &aOKBtn == pBtn )
564 		{
565 			USHORT nFactor = GetFactor();
566 
567 			if ( SPECIAL_FACTOR == nFactor )
568 			{
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 );
575 			}
576 			else
577                 aZoomItem.SetValue( nFactor );
578 
579             if ( aAutomaticBtn.IsChecked() )
580             {
581                 aViewLayoutItem.SetValue( 0 );
582                 aViewLayoutItem.SetBookMode( false );
583             }
584             if ( aSingleBtn.IsChecked() )
585             {
586                 aViewLayoutItem.SetValue( 1 );
587                 aViewLayoutItem.SetBookMode( false );
588             }
589             else if ( aColumnsBtn.IsChecked() )
590             {
591                 aViewLayoutItem.SetValue( static_cast<USHORT>(aColumnsEdit.GetValue()) );
592                 aViewLayoutItem.SetBookMode( aBookModeChk.IsChecked() );
593             }
594         }
595 		else
596 		{
597 			DBG_ERROR( "Wrong Button" );
598 			return 0;
599 		}
600 #if !TEST_LAYOUT
601 		pOutSet = new SfxItemSet( rSet );
602         pOutSet->Put( aZoomItem );
603 
604         // don't set attribute in case the whole viewlayout stuff is disabled:
605         if ( aViewLayoutFl.IsEnabled() )
606             pOutSet->Put( aViewLayoutItem );
607 
608 		// Wert aus dem UserEdit "uber den Dialog hinaus merken
609 		SfxObjectShell* pSh = SfxObjectShell::Current();
610 
611 		if ( pSh )
612 			pSh->PutItem( SfxUInt16Item( SID_ATTR_ZOOM_USER,
613 										 (UINT16)aUserEdit.GetValue() ) );
614 #endif /* !TEST_LAYOUT */
615 		EndDialog( RET_OK );
616 	}
617 	else
618 		EndDialog( RET_CANCEL );
619 	return 0;
620 }
621 
622 
623