xref: /aoo41x/main/toolkit/workben/layout/zoom.cxx (revision b0724fc6)
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 #if !TEST_LAYOUT
25 // MARKER(update_precomp.py): autogen include statement, do not remove
26 #include "precompiled_svx.hxx"
27 #endif /* !TEST_LAYOUT */
28 
29 #ifdef SVX_DLLIMPLEMENTATION
30 #undef SVX_DLLIMPLEMENTATION
31 #endif
32 
33 // include ---------------------------------------------------------------
34 
35 #if TEST_LAYOUT
36 #include <cstdio>
37 #endif /* TEST_LAYOUT */
38 #include <tools/shl.hxx>
39 #include <svtools/itemset.hxx>
40 #include <svtools/itempool.hxx>
41 #include <sfx2/objsh.hxx>
42 #include <vcl/msgbox.hxx>
43 
44 #define _SVX_ZOOM_CXX
45 
46 #include <svx/dialogs.hrc>
47 #if !TEST_LAYOUT
48 #include "zoom.hrc"
49 #endif /* !TEST_LAYOUT */
50 
51 #include "zoom.hxx"
52 #if TEST_LAYOUT
53 #define SVX_ZOOM_OPTIMAL "Zoom optimal"
54 #define SVX_ZOOM_PAGEWIDTH "Zoom page width"
55 #define SVX_ZOOM_WHOLEPAGE "Zoom whole page"
56 struct SvxZoomItem
57 {
SetTypeSvxZoomItem58     void SetType( char const* s )
59     {
60         fputs( s, stderr );
61     }
62 
SetValueSvxZoomItem63     void SetValue( int n )
64     {
65         fprintf( stderr, "Zoom factor: %d\n", int( n ) );
66     }
67 };
68 #define SfxPoolItem LAYOUT_SfxPoolItem
69 class LAYOUT_SfxPoolItem
70 {
71 };
72 struct SvxViewLayoutItem : public LAYOUT_SfxPoolItem
73 {
IsBookModeSvxViewLayoutItem74     bool IsBookMode()
75     {
76         return false;
77     }
GetValueSvxViewLayoutItem78     USHORT GetValue()
79     {
80         return 0;
81     }
82 
SetValueSvxViewLayoutItem83     void SetValue( int n )
84     {
85         fprintf( stderr, "Zoom factor: %d\n", int( n ) );
86     }
SetBookModeSvxViewLayoutItem87     void SetBookMode( bool )
88     {
89     }
90 #if 0 // needed for real SfxPoolItem
91     int operator==( const SfxPoolItem& ) const
92     {
93         return 0;
94     }
95     SfxPoolItem* Clone( SfxItemPool* ) const
96     {
97         return 0;
98     }
99 #endif
100 };
101 #else /* !TEST_LAYOUT */
102 #include <svx/zoomitem.hxx>
103 #include <svx/viewlayoutitem.hxx>
104 #include <svx/dialmgr.hxx>
105 #endif /* !TEST_LAYOUT */
106 #include "zoom_def.hxx"
107 
108 #include <layout/layout-pre.hxx>
109 
110 #if ENABLE_LAYOUT
111 #undef SVX_RES
112 #define SVX_RES(x) #x
113 #undef SfxModalDialog
114 #define SfxModalDialog( parent, id ) Dialog( parent, "zoom.xml", id )
115 #endif /* ENABLE_LAYOUT */
116 
117 // static ----------------------------------------------------------------
118 
119 static USHORT pRanges[] =
120 {
121 	SID_ATTR_ZOOM,
122 	SID_ATTR_ZOOM,
123 	0
124 };
125 
126 #define SPECIAL_FACTOR	((USHORT)0xFFFF)
127 
128 // class SvxZoomDialog ---------------------------------------------------
129 
GetFactor() const130 USHORT SvxZoomDialog::GetFactor() const
131 {
132     if ( a100Btn.IsChecked() )
133 		return 100;
134     if ( aUserBtn.IsChecked() )
135 		return (USHORT)aUserEdit.GetValue();
136 	else
137 		return SPECIAL_FACTOR;
138 }
139 
140 // -----------------------------------------------------------------------
141 
SetFactor(USHORT nNewFactor,USHORT nBtnId)142 void SvxZoomDialog::SetFactor( USHORT nNewFactor, USHORT nBtnId )
143 {
144 #if TEST_LAYOUT
145     printf ("\t\t set factor %d\n", (int)nNewFactor);
146 #endif /* !TEST_LAYOUT */
147 	aUserEdit.Disable();
148 
149 	if ( !nBtnId )
150 	{
151         if ( nNewFactor == 100 )
152 		{
153 			a100Btn.Check();
154 			a100Btn.GrabFocus();
155 		}
156         else
157 		{
158 			aUserBtn.Check();
159 			aUserEdit.Enable();
160 			aUserEdit.SetValue( (long)nNewFactor );
161 			aUserEdit.GrabFocus();
162 		}
163 	}
164 	else
165 	{
166 		aUserEdit.SetValue( (long)nNewFactor );
167 
168 		if ( ZOOMBTN_OPTIMAL == nBtnId )
169 		{
170 			aOptimalBtn.Check();
171 			aOptimalBtn.GrabFocus();
172 		}
173 		else if ( ZOOMBTN_PAGEWIDTH == nBtnId )
174 		{
175 			aPageWidthBtn.Check();
176 			aPageWidthBtn.GrabFocus();
177 		}
178 		else if ( ZOOMBTN_WHOLEPAGE == nBtnId )
179 		{
180 			aWholePageBtn.Check();
181 			aWholePageBtn.GrabFocus();
182 		}
183 	}
184 }
185 
186 // -----------------------------------------------------------------------
187 
SetButtonText(USHORT nBtnId,const String & rNewTxt)188 void SvxZoomDialog::SetButtonText( USHORT nBtnId, const String& rNewTxt )
189 {
190 	switch ( nBtnId )
191 	{
192 		case ZOOMBTN_OPTIMAL: // Optimal-Button
193 			aOptimalBtn.SetText( rNewTxt );
194 			break;
195 
196 		case ZOOMBTN_PAGEWIDTH: // Seitenbreite-Button
197 			aPageWidthBtn.SetText( rNewTxt );
198 			break;
199 
200 		case ZOOMBTN_WHOLEPAGE: // Ganze Seite-Button
201 			aWholePageBtn.SetText( rNewTxt );
202 			break;
203 
204 		default:
205 			DBG_ERROR( "wrong button number" );
206 	}
207 }
208 
209 // -----------------------------------------------------------------------
210 
HideButton(USHORT nBtnId)211 void SvxZoomDialog::HideButton( USHORT nBtnId )
212 {
213 	switch ( nBtnId )
214 	{
215 		case ZOOMBTN_OPTIMAL: // Optimal-Button
216 			aOptimalBtn.Hide();
217 			break;
218 
219 		case ZOOMBTN_PAGEWIDTH: // Seitenbreite-Button
220 			aPageWidthBtn.Hide();
221 			break;
222 
223 		case ZOOMBTN_WHOLEPAGE: // Ganze Seite-Button
224 			aWholePageBtn.Hide();
225 			break;
226 
227 		default:
228 			DBG_ERROR( "Falsche Button-Nummer!!!" );
229 	}
230 }
231 
232 // -----------------------------------------------------------------------
233 
SetLimits(USHORT nMin,USHORT nMax)234 void SvxZoomDialog::SetLimits( USHORT nMin, USHORT nMax )
235 {
236 	DBG_ASSERT( nMin < nMax, "invalid limits" );
237     aUserEdit.SetMin( nMin );
238 	aUserEdit.SetFirst( nMin );
239     aUserEdit.SetMax( nMax );
240 	aUserEdit.SetLast( nMax );
241 }
242 
243 // -----------------------------------------------------------------------
244 
SetSpinSize(USHORT nNewSpin)245 void SvxZoomDialog::SetSpinSize( USHORT nNewSpin )
246 {
247 	aUserEdit.SetSpinSize( nNewSpin );
248 }
249 
250 // -----------------------------------------------------------------------
251 
252 #if TEST_LAYOUT
SvxZoomDialog(Window * pParent)253 SvxZoomDialog::SvxZoomDialog( Window* pParent ) :
254 #else /* !TEST_LAYOUT */
255 SvxZoomDialog::SvxZoomDialog( Window* pParent, const SfxItemSet& rCoreSet ) :
256 #endif /* !TEST_LAYOUT */
257 
258 	SfxModalDialog( pParent, SVX_RES( RID_SVXDLG_ZOOM ) ),
259 
260     aZoomFl         ( this, SVX_RES( FL_ZOOM ) ),
261     aOptimalBtn     ( this, SVX_RES( BTN_OPTIMAL ) ),
262     aWholePageBtn   ( this, SVX_RES( BTN_WHOLE_PAGE ) ),
263     aPageWidthBtn   ( this, SVX_RES( BTN_PAGE_WIDTH ) ),
264     a100Btn         ( this, SVX_RES( BTN_100 ) ),
265     aUserBtn        ( this, SVX_RES( BTN_USER ) ),
266 	aUserEdit       ( this, SVX_RES( ED_USER ) ),
267 
268     aViewLayoutFl   ( this, SVX_RES( FL_VIEWLAYOUT ) ),
269     aAutomaticBtn   ( this, SVX_RES( BTN_AUTOMATIC ) ),
270     aSingleBtn      ( this, SVX_RES( BTN_SINGLE ) ),
271     aColumnsBtn     ( this, SVX_RES( BTN_COLUMNS ) ),
272     aColumnsEdit    ( this, SVX_RES( ED_COLUMNS ) ),
273     aBookModeChk    ( this, SVX_RES( CHK_BOOK ) ),
274 
275     aBottomFl       ( this, SVX_RES( FL_BOTTOM ) ),
276     aOKBtn          ( this, SVX_RES( BTN_ZOOM_OK ) ),
277 	aCancelBtn      ( this, SVX_RES( BTN_ZOOM_CANCEL ) ),
278 	aHelpBtn	    ( this, SVX_RES( BTN_ZOOM_HELP ) ),
279 
280 #if !TEST_LAYOUT
281 	rSet		( rCoreSet ),
282 #endif /* !TEST_LAYOUT */
283 	pOutSet     ( NULL ),
284 	bModified	( FALSE )
285 
286 {
287 #if ENABLE_LAYOUT
288     SetHelpId (SID_ATTR_ZOOM);
289 #endif /* ENABLE_LAYOUT */
290 	Link aLink = LINK( this, SvxZoomDialog, UserHdl );
291     a100Btn.SetClickHdl( aLink );
292     aOptimalBtn.SetClickHdl( aLink );
293 	aPageWidthBtn.SetClickHdl( aLink );
294 	aWholePageBtn.SetClickHdl( aLink );
295 	aUserBtn.SetClickHdl( aLink );
296 
297     Link aViewLayoutLink = LINK( this, SvxZoomDialog, ViewLayoutUserHdl );
298     aAutomaticBtn.SetClickHdl( aViewLayoutLink );
299     aSingleBtn.SetClickHdl( aViewLayoutLink );
300     aColumnsBtn.SetClickHdl( aViewLayoutLink );
301 
302     Link aViewLayoutSpinLink = LINK( this, SvxZoomDialog, ViewLayoutSpinHdl );
303     aColumnsEdit.SetModifyHdl( aViewLayoutSpinLink );
304 
305     Link aViewLayoutCheckLink = LINK( this, SvxZoomDialog, ViewLayoutCheckHdl );
306     aBookModeChk.SetClickHdl( aViewLayoutCheckLink );
307 
308     aOKBtn.SetClickHdl( LINK( this, SvxZoomDialog, OKHdl ) );
309 	aUserEdit.SetModifyHdl( LINK( this, SvxZoomDialog, SpinHdl ) );
310 
311 	// Default-Werte
312 	USHORT nValue = 100;
313 	USHORT nMin = 10;
314 	USHORT nMax = 1000;
315 
316 #if !TEST_LAYOUT
317 	// ggf. erst den alten Wert besorgen
318 	const SfxUInt16Item* pOldUserItem = 0;
319 	SfxObjectShell* pSh = SfxObjectShell::Current();
320 
321 	if ( pSh )
322 		pOldUserItem = (const SfxUInt16Item*)pSh->GetItem( SID_ATTR_ZOOM_USER );
323 
324 	if ( pOldUserItem )
325 		nValue = pOldUserItem->GetValue();
326 
327 	// UserEdit initialisieren
328 	if ( nMin > nValue )
329 		nMin = nValue;
330 	if ( nMax < nValue )
331 		nMax = nValue;
332 #endif /* !TEST_LAYOUT */
333     aUserEdit.SetMin( nMin );
334 	aUserEdit.SetFirst( nMin );
335     aUserEdit.SetMax( nMax );
336 	aUserEdit.SetLast( nMax );
337 	aUserEdit.SetValue( nValue );
338 
339 #if TEST_LAYOUT
340     USHORT nZoom = 100;
341     SetFactor( nZoom );
342 #else /* !TEST_LAYOUT */
343 	const SfxPoolItem& rItem = rSet.Get( rSet.GetPool()->GetWhich( SID_ATTR_ZOOM ) );
344 
345 	if ( rItem.ISA(SvxZoomItem) )
346 	{
347 		const SvxZoomItem& rZoomItem = (const SvxZoomItem&)rItem;
348         const USHORT nZoom = rZoomItem.GetValue();
349         const SvxZoomType eType = rZoomItem.GetType();
350         const USHORT nValSet = rZoomItem.GetValueSet();
351 		USHORT nBtnId = 0;
352 
353 		switch ( eType )
354 		{
355 			case SVX_ZOOM_OPTIMAL:
356 				nBtnId = ZOOMBTN_OPTIMAL;
357 				break;
358 			case SVX_ZOOM_PAGEWIDTH:
359 				nBtnId = ZOOMBTN_PAGEWIDTH;
360 				break;
361 			case SVX_ZOOM_WHOLEPAGE:
362 				nBtnId = ZOOMBTN_WHOLEPAGE;
363 				break;
364 			case SVX_ZOOM_PERCENT:
365 				break;
366             case SVX_ZOOM_PAGEWIDTH_NOBORDER:
367                 break;
368 		}
369 
370         if ( !(SVX_ZOOM_ENABLE_100 & nValSet) )
371 			a100Btn.Disable();
372         if ( !(SVX_ZOOM_ENABLE_OPTIMAL & nValSet) )
373 			aOptimalBtn.Disable();
374 		if ( !(SVX_ZOOM_ENABLE_PAGEWIDTH & nValSet) )
375 			aPageWidthBtn.Disable();
376 		if ( !(SVX_ZOOM_ENABLE_WHOLEPAGE & nValSet) )
377 			aWholePageBtn.Disable();
378         SetFactor( nZoom, nBtnId );
379 	}
380 	else
381 	{
382         const USHORT nZoom = ( (const SfxUInt16Item&)rItem ).GetValue();
383         SetFactor( nZoom );
384 	}
385 
386     const SfxPoolItem* pViewLayoutItem = 0;
387     if ( SFX_ITEM_SET == rSet.GetItemState( SID_ATTR_VIEWLAYOUT, FALSE, &pViewLayoutItem ) )
388     {
389         const USHORT nColumns = static_cast<const SvxViewLayoutItem*>(pViewLayoutItem)->GetValue();
390         const bool bBookMode  = static_cast<const SvxViewLayoutItem*>(pViewLayoutItem)->IsBookMode();
391 
392         if ( 0 == nColumns )
393         {
394             aAutomaticBtn.Check();
395             aColumnsEdit.SetValue( 2 );
396             aColumnsEdit.Disable();
397             aBookModeChk.Disable();
398         }
399         else if ( 1 == nColumns)
400         {
401             aSingleBtn.Check();
402             aColumnsEdit.SetValue( 2 );
403             aColumnsEdit.Disable();
404             aBookModeChk.Disable();
405         }
406         else
407         {
408             aColumnsBtn.Check();
409             if ( !bBookMode )
410             {
411                 aColumnsEdit.SetValue( nColumns );
412                 if ( 0 != nColumns % 2 )
413                     aBookModeChk.Disable();
414             }
415             else
416             {
417                 aColumnsEdit.SetValue( nColumns );
418                 aBookModeChk.Check();
419             }
420         }
421     }
422     else
423     {
424         // hide view layout related controls:
425         aViewLayoutFl.Disable();
426         aAutomaticBtn.Disable();
427         aSingleBtn.Disable();
428         aColumnsBtn.Disable();
429         aColumnsEdit.Disable();
430         aBookModeChk.Disable();
431     }
432 #endif /* !TEST_LAYOUT */
433 
434 	FreeResource();
435 }
436 
437 // -----------------------------------------------------------------------
438 
~SvxZoomDialog()439 SvxZoomDialog::~SvxZoomDialog()
440 {
441 	delete pOutSet;
442 	pOutSet = 0;
443 }
444 
445 // -----------------------------------------------------------------------
446 
GetRanges()447 USHORT* SvxZoomDialog::GetRanges()
448 {
449 	return pRanges;
450 }
451 
452 // -----------------------------------------------------------------------
453 
IMPL_LINK(SvxZoomDialog,UserHdl,RadioButton *,pBtn)454 IMPL_LINK( SvxZoomDialog, UserHdl, RadioButton *, pBtn )
455 {
456 #if TEST_LAYOUT
457     fprintf( stderr, "SvxZoomDialog::UserHdl\n" );
458 #endif /* TEST_LAYOUT */
459 	bModified |= TRUE;
460 
461 	if ( pBtn == &aUserBtn )
462 	{
463 		aUserEdit.Enable();
464 		aUserEdit.GrabFocus();
465 	}
466 	else
467 		aUserEdit.Disable();
468 	return 0;
469 }
470 
471 // -----------------------------------------------------------------------
472 
IMPL_LINK(SvxZoomDialog,SpinHdl,MetricField *,EMPTYARG)473 IMPL_LINK( SvxZoomDialog, SpinHdl, MetricField *, EMPTYARG )
474 {
475 	if ( !aUserBtn.IsChecked() )
476 		return 0;
477 	bModified |= TRUE;
478 	return 0;
479 }
480 
481 // -----------------------------------------------------------------------
482 
IMPL_LINK(SvxZoomDialog,ViewLayoutUserHdl,RadioButton *,pBtn)483 IMPL_LINK( SvxZoomDialog, ViewLayoutUserHdl, RadioButton *, pBtn )
484 {
485     bModified |= TRUE;
486 
487     if ( pBtn == &aAutomaticBtn )
488     {
489         aColumnsEdit.Disable();
490         aBookModeChk.Disable();
491     }
492     else if ( pBtn == &aSingleBtn )
493     {
494         aColumnsEdit.Disable();
495         aBookModeChk.Disable();
496     }
497     else if ( pBtn == &aColumnsBtn )
498     {
499         aColumnsEdit.Enable();
500         aColumnsEdit.GrabFocus();
501         if ( 0 == aColumnsEdit.GetValue() % 2 )
502             aBookModeChk.Enable();
503     }
504     else
505     {
506         DBG_ERROR( "Wrong Button" );
507         return 0;
508     }
509 
510     return 0;
511 }
512 
513 // -----------------------------------------------------------------------
514 
IMPL_LINK(SvxZoomDialog,ViewLayoutSpinHdl,MetricField *,pEdt)515 IMPL_LINK( SvxZoomDialog, ViewLayoutSpinHdl, MetricField *, pEdt )
516 {
517     if ( pEdt == &aColumnsEdit && !aColumnsBtn.IsChecked() )
518         return 0;
519 
520     if ( 0 == aColumnsEdit.GetValue() % 2 )
521         aBookModeChk.Enable();
522     else
523     {
524         aBookModeChk.Check( sal_False );
525         aBookModeChk.Disable();
526     }
527 
528     bModified |= TRUE;
529 
530     return 0;
531 }
532 
533 // -----------------------------------------------------------------------
534 
IMPL_LINK(SvxZoomDialog,ViewLayoutCheckHdl,CheckBox *,pChk)535 IMPL_LINK( SvxZoomDialog, ViewLayoutCheckHdl, CheckBox *, pChk )
536 {
537     if ( pChk == &aBookModeChk && !aColumnsBtn.IsChecked() )
538         return 0;
539 
540     bModified |= TRUE;
541 
542     return 0;
543 }
544 
545 // -----------------------------------------------------------------------
546 
IMPL_LINK(SvxZoomDialog,OKHdl,Button *,pBtn)547 IMPL_LINK( SvxZoomDialog, OKHdl, Button *, pBtn )
548 {
549 	if ( bModified || &aOKBtn != pBtn )
550 	{
551 #if TEST_LAYOUT
552 		SvxZoomItem aZoomItem;
553 		SvxViewLayoutItem aViewLayoutItem;
554 #else /* !TEST_LAYOUT */
555         SvxZoomItem aZoomItem( SVX_ZOOM_PERCENT, 0, rSet.GetPool()->GetWhich( SID_ATTR_ZOOM ) );
556         SvxViewLayoutItem aViewLayoutItem( 0, false, rSet.GetPool()->GetWhich( SID_ATTR_VIEWLAYOUT ) );
557 #endif /* !TEST_LAYOUT */
558 
559 		if ( &aOKBtn == pBtn )
560 		{
561 			USHORT nFactor = GetFactor();
562 
563 			if ( SPECIAL_FACTOR == nFactor )
564 			{
565 				if ( aOptimalBtn.IsChecked() )
566                     aZoomItem.SetType( SVX_ZOOM_OPTIMAL );
567 				else if ( aPageWidthBtn.IsChecked() )
568                     aZoomItem.SetType( SVX_ZOOM_PAGEWIDTH );
569 				else if ( aWholePageBtn.IsChecked() )
570                     aZoomItem.SetType( SVX_ZOOM_WHOLEPAGE );
571 			}
572 			else
573                 aZoomItem.SetValue( nFactor );
574 
575             if ( aAutomaticBtn.IsChecked() )
576             {
577                 aViewLayoutItem.SetValue( 0 );
578                 aViewLayoutItem.SetBookMode( false );
579             }
580             if ( aSingleBtn.IsChecked() )
581             {
582                 aViewLayoutItem.SetValue( 1 );
583                 aViewLayoutItem.SetBookMode( false );
584             }
585             else if ( aColumnsBtn.IsChecked() )
586             {
587                 aViewLayoutItem.SetValue( static_cast<USHORT>(aColumnsEdit.GetValue()) );
588                 aViewLayoutItem.SetBookMode( aBookModeChk.IsChecked() );
589             }
590         }
591 		else
592 		{
593 			DBG_ERROR( "Wrong Button" );
594 			return 0;
595 		}
596 #if !TEST_LAYOUT
597 		pOutSet = new SfxItemSet( rSet );
598         pOutSet->Put( aZoomItem );
599 
600         // don't set attribute in case the whole viewlayout stuff is disabled:
601         if ( aViewLayoutFl.IsEnabled() )
602             pOutSet->Put( aViewLayoutItem );
603 
604 		// Wert aus dem UserEdit "uber den Dialog hinaus merken
605 		SfxObjectShell* pSh = SfxObjectShell::Current();
606 
607 		if ( pSh )
608 			pSh->PutItem( SfxUInt16Item( SID_ATTR_ZOOM_USER,
609 										 (UINT16)aUserEdit.GetValue() ) );
610 #endif /* !TEST_LAYOUT */
611 		EndDialog( RET_OK );
612 	}
613 	else
614 		EndDialog( RET_CANCEL );
615 	return 0;
616 }
617 
618 
619