1 /************************************************************************
2 *
3 * Licensed Materials - Property of IBM.
4 * (C) Copyright IBM Corporation 2003, 2012.  All Rights Reserved.
5 * U.S. Government Users Restricted Rights:
6 * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
7 *
8 ************************************************************************/
9 
10 #include "precompiled_sw.hxx"
11 
12 #include "PagePropertyPanel.hxx"
13 #include "PagePropertyPanel.hrc"
14 
15 #include "PropertyPanel.hrc"
16 
17 #include <svx/sidebar/PopupContainer.hxx>
18 #include "PageOrientationControl.hxx"
19 #include "PageMarginControl.hxx"
20 #include "PageSizeControl.hxx"
21 #include "PageColumnControl.hxx"
22 
23 #include <swtypes.hxx>
24 #include <cmdid.h>
25 #include <errhdl.hxx>
26 
27 #include <svl/intitem.hxx>
28 #include <editeng/sizeitem.hxx>
29 #include <editeng/paperinf.hxx>
30 #include <svx/svxids.hrc>
31 #include <svx/dlgutil.hxx>
32 #include <svx/rulritem.hxx>
33 
34 #include <sfx2/sidebar/ControlFactory.hxx>
35 #include <sfx2/dispatch.hxx>
36 #include <sfx2/bindings.hxx>
37 #include <sfx2/viewsh.hxx>
38 #include <sfx2/objsh.hxx>
39 
40 #include <boost/bind.hpp>
41 
42 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
43 
44 #define SetFldVal(rField, lValue) (rField).SetValue((rField).Normalize(lValue), FUNIT_TWIP)
45 
46 
47 namespace sw { namespace sidebar {
48 
49 PagePropertyPanel* PagePropertyPanel::Create (
50     Window* pParent,
51     const cssu::Reference<css::frame::XFrame>& rxFrame,
52     SfxBindings* pBindings)
53 {
54     if (pParent == NULL)
55         throw ::com::sun::star::lang::IllegalArgumentException(A2S("no parent Window given to PagePropertyPanel::Create"), NULL, 0);
56     if ( ! rxFrame.is())
57         throw ::com::sun::star::lang::IllegalArgumentException(A2S("no XFrame given to PagePropertyPanel::Create"), NULL, 1);
58     if (pBindings == NULL)
59         throw ::com::sun::star::lang::IllegalArgumentException(A2S("no SfxBindings given to PagePropertyPanel::Create"), NULL, 2);
60 
61     return new PagePropertyPanel(
62         pParent,
63         rxFrame,
64         pBindings);
65 }
66 
67 PagePropertyPanel::PagePropertyPanel(
68             Window* pParent,
69             const cssu::Reference<css::frame::XFrame>& rxFrame,
70             SfxBindings* pBindings)
71     : Control(pParent, SW_RES(RID_PROPERTYPANEL_SWPAGE))
72     , mxFrame(rxFrame)
73     , maContext()
74     , mpBindings(pBindings)
75     // visible controls
76     , maFtOrientation( this, SW_RES(FT_ORIENTATION) )
77     , mpToolBoxOrientationBackground( ::sfx2::sidebar::ControlFactory::CreateToolBoxBackground(this) )
78     , mpToolBoxOrientation( ::sfx2::sidebar::ControlFactory::CreateToolBox( mpToolBoxOrientationBackground.get(), SW_RES(TB_ORIENTATION)) )
79     , maFtMargin( this, SW_RES(FT_MARGIN) )
80     , mpToolBoxMarginBackground( ::sfx2::sidebar::ControlFactory::CreateToolBoxBackground(this) )
81     , mpToolBoxMargin( ::sfx2::sidebar::ControlFactory::CreateToolBox( mpToolBoxMarginBackground.get(), SW_RES(TB_MARGIN)) )
82     , maFtSize( this, SW_RES(FT_SIZE) )
83     , mpToolBoxSizeBackground( ::sfx2::sidebar::ControlFactory::CreateToolBoxBackground(this) )
84     , mpToolBoxSize( ::sfx2::sidebar::ControlFactory::CreateToolBox( mpToolBoxSizeBackground.get(), SW_RES(TB_SIZE)) )
85     , maFtColumn( this, SW_RES(FT_COLUMN) )
86     , mpToolBoxColumnBackground( ::sfx2::sidebar::ControlFactory::CreateToolBoxBackground(this) )
87     , mpToolBoxColumn( ::sfx2::sidebar::ControlFactory::CreateToolBox( mpToolBoxColumnBackground.get(), SW_RES(TB_COLUMN)) )
88     // image resources
89     , maImgSize					(NULL)
90     , maImgSize_L					(NULL)
91     , mImgPortrait				(SW_RES(IMG_PAGE_PORTRAIT))
92     , mImgLandscape				(SW_RES(IMG_PAGE_LANDSCAPE))
93     , mImgNarrow					(SW_RES(IMG_PAGE_NARROW))
94     , mImgNormal					(SW_RES(IMG_PAGE_NORMAL))
95     , mImgWide					(SW_RES(IMG_PAGE_WIDE))
96     , mImgMirrored				(SW_RES(IMG_PAGE_MIRRORED))
97     , mImgMarginCustom			(SW_RES(IMG_PAGE_MARGIN_CUSTOM))
98     , mImgNarrow_L				(SW_RES(IMG_PAGE_NARROW_L))
99     , mImgNormal_L				(SW_RES(IMG_PAGE_NORMAL_L))
100     , mImgWide_L					(SW_RES(IMG_PAGE_WIDE_L))
101     , mImgMirrored_L				(SW_RES(IMG_PAGE_MIRRORED_L))
102     , mImgMarginCustom_L			(SW_RES(IMG_PAGE_MARGIN_CUSTOM_L))
103     , mImgA3						(SW_RES(IMG_PAGE_A3))
104     , mImgA4						(SW_RES(IMG_PAGE_A4))
105     , mImgA5						(SW_RES(IMG_PAGE_A5))
106     , mImgB4						(SW_RES(IMG_PAGE_B4))
107     , mImgB5						(SW_RES(IMG_PAGE_B5))
108     , mImgC5						(SW_RES(IMG_PAGE_C5))
109     , mImgLetter					(SW_RES(IMG_PAGE_LETTER))
110     , mImgLegal					(SW_RES(IMG_PAGE_LEGAL))
111     , mImgSizeNone				(SW_RES(IMG_PAGE_SIZE_NONE))
112     , mImgA3_L					(SW_RES(IMG_PAGE_A3_L))
113     , mImgA4_L					(SW_RES(IMG_PAGE_A4_L))
114     , mImgA5_L					(SW_RES(IMG_PAGE_A5_L))
115     , mImgB4_L					(SW_RES(IMG_PAGE_B4_L))
116     , mImgB5_L					(SW_RES(IMG_PAGE_B5_L))
117     , mImgC5_L					(SW_RES(IMG_PAGE_C5_L))
118     , mImgLetter_L				(SW_RES(IMG_PAGE_LETTER_L))
119     , mImgLegal_L					(SW_RES(IMG_PAGE_LEGAL_L))
120     , mImgSizeNone_L				(SW_RES(IMG_PAGE_SIZE_NONE_L))
121     , mImgColumn1					(SW_RES(IMG_PAGE_COLUMN_1))
122     , mImgColumn2					(SW_RES(IMG_PAGE_COLUMN_2))
123     , mImgColumn3					(SW_RES(IMG_PAGE_COLUMN_3))
124     , mImgLeft					(SW_RES(IMG_PAGE_COLUMN_LEFT))
125     , mImgRight					(SW_RES(IMG_PAGE_COLUMN_RIGHT))
126     , mImgColumnNone				(SW_RES(IMG_PAGE_COLUMN_NONE))
127     , mImgColumn1_L				(SW_RES(IMG_PAGE_COLUMN_1_L))
128     , mImgColumn2_L				(SW_RES(IMG_PAGE_COLUMN_2_L))
129     , mImgColumn3_L				(SW_RES(IMG_PAGE_COLUMN_3_L))
130     , mImgLeft_L					(SW_RES(IMG_PAGE_COLUMN_LEFT_L))
131     , mImgRight_L					(SW_RES(IMG_PAGE_COLUMN_RIGHT_L))
132     , mImgColumnNone_L			(SW_RES(IMG_PAGE_COLUMN_NONE_L))
133 
134     , mpPageItem( new SvxPageItem(SID_ATTR_PAGE) )
135     , mpPageLRMarginItem( new SvxLongLRSpaceItem( 0, 0, SID_ATTR_LONG_LRSPACE ) )
136     , mpPageULMarginItem( new SvxLongULSpaceItem( 0, 0, SID_ATTR_LONG_ULSPACE ) )
137     , mpPageSizeItem( new SvxSizeItem(SID_ATTR_PAGE_SIZE) )
138     , mePaper( PAPER_USER )
139     , mpPageColumnTypeItem( new SfxInt16Item(SID_ATTR_SWPAGE_COLUMN) )
140 
141     , meFUnit()
142     , meUnit()
143 
144     , m_aSwPagePgULControl		(SID_ATTR_LONG_ULSPACE, *pBindings, *this)
145     , m_aSwPagePgLRControl		(SID_ATTR_LONG_LRSPACE, *pBindings, *this)
146     , m_aSwPagePgSizeControl		(SID_ATTR_PAGE_SIZE, *pBindings, *this)
147     , m_aSwPagePgControl			(SID_ATTR_PAGE, *pBindings, *this)
148     , m_aSwPageColControl			(SID_ATTR_SWPAGE_COLUMN, *pBindings, *this)
149     , m_aSwPagePgMetricControl	(SID_ATTR_METRIC, *pBindings, *this)
150 
151     , mpOrientationPopup()
152     , mpMarginPopup()
153     , mpSizePopup()
154     , mpColumnPopup()
155 
156     , mbInvalidateSIDAttrPageOnSIDAttrPageSizeNotify( false )
157 {
158     Initialize();
159     mbInvalidateSIDAttrPageOnSIDAttrPageSizeNotify = true;
160     FreeResource();
161 }
162 
163 PagePropertyPanel::~PagePropertyPanel()
164 {
165     delete[] maImgSize;
166     delete[] maImgSize_L;
167 
168     // destroy the toolbox windows.
169     mpToolBoxOrientation.reset();
170     mpToolBoxMargin.reset();
171     mpToolBoxSize.reset();
172     mpToolBoxColumn.reset();
173 
174     // destroy the background windows of the toolboxes.
175     mpToolBoxOrientationBackground.reset();
176     mpToolBoxMarginBackground.reset();
177     mpToolBoxSizeBackground.reset();
178     mpToolBoxColumnBackground.reset();
179 }
180 
181 void PagePropertyPanel::Initialize()
182 {
183     // popup for page orientation
184     Link aLink = LINK( this, PagePropertyPanel, ClickOrientationHdl );
185     mpToolBoxOrientation->SetDropdownClickHdl( aLink );
186     mpToolBoxOrientation->SetSelectHdl( aLink );
187     mpToolBoxOrientation->SetItemImage( TBI_ORIENTATION, mImgPortrait);
188     mpToolBoxOrientation->SetItemBits( TBI_ORIENTATION, mpToolBoxOrientation->GetItemBits( TBI_ORIENTATION ) | TIB_DROPDOWNONLY );
189     mpToolBoxOrientation->SetQuickHelpText(TBI_ORIENTATION,String(SW_RES(STR_QHELP_TB_ORIENTATION)));
190     mpToolBoxOrientation->SetOutputSizePixel( mpToolBoxOrientation->CalcWindowSizePixel() );
191     mpToolBoxOrientation->SetBackground( Wallpaper() );
192     mpToolBoxOrientation->SetPaintTransparent( sal_True );
193 
194     // popup for page margins
195     aLink = LINK( this, PagePropertyPanel, ClickMarginHdl );
196     mpToolBoxMargin->SetDropdownClickHdl( aLink );
197     mpToolBoxMargin->SetSelectHdl( aLink );
198     mpToolBoxMargin->SetItemImage(TBI_MARGIN, mImgNormal);
199     mpToolBoxMargin->SetItemBits( TBI_MARGIN, mpToolBoxMargin->GetItemBits( TBI_MARGIN ) | TIB_DROPDOWNONLY );
200     mpToolBoxMargin->SetQuickHelpText(TBI_MARGIN,String(SW_RES(STR_QHELP_TB_MARGIN)));
201     mpToolBoxMargin->SetOutputSizePixel( mpToolBoxMargin->CalcWindowSizePixel() );
202     mpToolBoxMargin->SetBackground(Wallpaper());
203     mpToolBoxMargin->SetPaintTransparent( sal_True );
204 
205     // popup for page size
206     aLink = LINK( this, PagePropertyPanel, ClickSizeHdl );
207     mpToolBoxSize->SetDropdownClickHdl( aLink );
208     mpToolBoxSize->SetSelectHdl( aLink );
209     mpToolBoxSize->SetItemImage(TBI_SIZE, mImgLetter);
210     mpToolBoxSize->SetItemBits( TBI_SIZE, mpToolBoxSize->GetItemBits( TBI_SIZE ) | TIB_DROPDOWNONLY );
211     mpToolBoxSize->SetQuickHelpText(TBI_SIZE,String(SW_RES(STR_QHELP_TB_SIZE)));
212     mpToolBoxSize->SetOutputSizePixel( mpToolBoxSize->CalcWindowSizePixel() );
213     mpToolBoxSize->SetBackground(Wallpaper());
214     mpToolBoxSize->SetPaintTransparent( sal_True );
215     maImgSize = new Image[8];
216     maImgSize[0] = mImgA3;
217     maImgSize[1] = mImgA4;
218     maImgSize[2] = mImgA5;
219     maImgSize[3] = mImgB4;
220     maImgSize[4] = mImgB5;
221     maImgSize[5] = mImgC5;
222     maImgSize[6] = mImgLetter;
223     maImgSize[7] = mImgLegal;
224     maImgSize_L = new Image[8];
225     maImgSize_L[0] = mImgA3_L;
226     maImgSize_L[1] = mImgA4_L;
227     maImgSize_L[2] = mImgA5_L;
228     maImgSize_L[3] = mImgB4_L;
229     maImgSize_L[4] = mImgB5_L;
230     maImgSize_L[5] = mImgC5_L;
231     maImgSize_L[6] = mImgLetter_L;
232     maImgSize_L[7] = mImgLegal_L;
233 
234     // popup for page column property
235     aLink = LINK( this, PagePropertyPanel, ClickColumnHdl );
236     mpToolBoxColumn->SetDropdownClickHdl( aLink );
237     mpToolBoxColumn->SetSelectHdl( aLink );
238     mpToolBoxColumn->SetItemImage(TBI_COLUMN, mImgColumn1);
239     mpToolBoxColumn->SetItemBits( TBI_COLUMN, mpToolBoxColumn->GetItemBits( TBI_COLUMN ) | TIB_DROPDOWNONLY );
240     mpToolBoxColumn->SetQuickHelpText(TBI_COLUMN,String(SW_RES(STR_QHELP_TB_COLUMN)));
241     mpToolBoxColumn->SetOutputSizePixel( mpToolBoxColumn->CalcWindowSizePixel() );
242     mpToolBoxColumn->SetBackground(Wallpaper());
243     mpToolBoxColumn->SetPaintTransparent( sal_True );
244 
245     meFUnit = GetModuleFieldUnit();
246     meUnit  = m_aSwPagePgSizeControl.GetCoreMetric();
247 
248     // 'pull' for page style's attribute values
249     mpBindings->Update( SID_ATTR_LONG_LRSPACE );
250     mpBindings->Update( SID_ATTR_LONG_ULSPACE );
251     mpBindings->Update( SID_ATTR_PAGE );
252     mpBindings->Update( SID_ATTR_PAGE_SIZE );
253 }
254 
255 
256 void PagePropertyPanel::DataChanged( const DataChangedEvent& rEvent )
257 {
258     // currently nothing special to do
259 
260     // forward to base class
261     Control::DataChanged( rEvent );
262 }
263 
264 
265 void PagePropertyPanel::HandleContextChange( const ::sfx2::sidebar::EnumContext aContext)
266 {
267     if (maContext == aContext)
268     {
269         // nothing to do.
270         return;
271     }
272 
273     maContext = aContext;
274 
275     // TODO: reactions on certain context switches
276 }
277 
278 
279 void PagePropertyPanel::ShowPageStyleFormatDialog( void )
280 {
281     mpBindings->GetDispatcher()->Execute( FN_FORMAT_PAGE_DLG, SFX_CALLMODE_ASYNCHRON );
282 }
283 
284 
285 
286 
287 ::svx::sidebar::PopupControl* PagePropertyPanel::CreatePageOrientationControl( ::svx::sidebar::PopupContainer* pParent )
288 {
289     return new PageOrientationControl( pParent, *this , mpPageItem->IsLandscape() );
290 }
291 
292 
293 IMPL_LINK( PagePropertyPanel, ClickOrientationHdl, ToolBox*, pToolBox )
294 {
295     mpOrientationPopup.reset(
296         new ::svx::sidebar::Popup(
297             this,
298             ::boost::bind(&PagePropertyPanel::CreatePageOrientationControl, this, _1),
299             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Page orientation")) ) );
300     mpOrientationPopup->Show( *pToolBox );
301 
302     return 0L;
303 }
304 
305 
306 void PagePropertyPanel::ExecuteOrientationChange( const sal_Bool bLandscape )
307 {
308     {
309         // set new page orientation
310         mpPageItem->SetLandscape( bLandscape );
311 
312         // swap the width and height of the page size
313         mpPageSizeItem->SetSize( Size( mpPageSizeItem->GetSize().Height(), mpPageSizeItem->GetSize().Width() ) );
314 
315         // apply changed attributes
316         GetBindings()->GetDispatcher()->Execute( SID_ATTR_PAGE_SIZE, SFX_CALLMODE_RECORD, mpPageSizeItem.get(), mpPageItem.get(), 0L );
317     }
318 
319     // check, if margin values still fit to the changed page size.
320     // if not, adjust margin values
321     {
322         const long nML = mpPageLRMarginItem->GetLeft();
323         const long nMR = mpPageLRMarginItem->GetRight();
324         const long nTmpPW = nML + nMR + MINBODY;
325 
326         const long nPW  = mpPageSizeItem->GetSize().Width();
327 
328         if ( nTmpPW > nPW )
329         {
330             if ( nML <= nMR )
331             {
332                 ExecuteMarginLRChange( mpPageLRMarginItem->GetLeft(), nMR - (nTmpPW - nPW ) );
333             }
334             else
335             {
336                 ExecuteMarginLRChange( nML - (nTmpPW - nPW ), mpPageLRMarginItem->GetRight() );
337             }
338         }
339 
340         const long nMT = mpPageULMarginItem->GetUpper();
341         const long nMB = mpPageULMarginItem->GetLower();
342         const long nTmpPH = nMT + nMB + MINBODY;
343 
344         const long nPH  = mpPageSizeItem->GetSize().Height();
345 
346         if ( nTmpPH > nPH )
347         {
348             if ( nMT <= nMB )
349             {
350                 ExecuteMarginULChange( mpPageULMarginItem->GetUpper(), nMB - ( nTmpPH - nPH ) );
351             }
352             else
353             {
354                 ExecuteMarginULChange( nMT - ( nTmpPH - nPH ), mpPageULMarginItem->GetLower() );
355             }
356         }
357     }
358 }
359 
360 
361 void PagePropertyPanel::ClosePageOrientationPopup()
362 {
363     mpOrientationPopup->Hide();
364 }
365 
366 
367 
368 
369 ::svx::sidebar::PopupControl* PagePropertyPanel::CreatePageMarginControl( ::svx::sidebar::PopupContainer* pParent )
370 {
371     return new PageMarginControl(
372         pParent,
373         *this,
374         *mpPageLRMarginItem.get(),
375         *mpPageULMarginItem.get(),
376         mpPageItem->GetPageUsage() == SVX_PAGE_MIRROR,
377         mpPageSizeItem->GetSize(),
378         mpPageItem->IsLandscape(),
379         meFUnit,
380         meUnit );
381 }
382 
383 
384 void PagePropertyPanel::ExecuteMarginLRChange(
385     const long nPageLeftMargin,
386     const long nPageRightMargin )
387 {
388     mpPageLRMarginItem->SetLeft( nPageLeftMargin );
389     mpPageLRMarginItem->SetRight( nPageRightMargin );
390     GetBindings()->GetDispatcher()->Execute( SID_ATTR_LONG_LRSPACE, SFX_CALLMODE_RECORD, mpPageLRMarginItem.get(),  0L );
391 }
392 
393 void PagePropertyPanel::ExecuteMarginULChange(
394     const long nPageTopMargin,
395     const long nPageBottomMargin )
396 {
397     mpPageULMarginItem->SetUpper( nPageTopMargin );
398     mpPageULMarginItem->SetLower( nPageBottomMargin );
399     GetBindings()->GetDispatcher()->Execute( SID_ATTR_LONG_ULSPACE, SFX_CALLMODE_RECORD, mpPageULMarginItem.get(),  0L );
400 }
401 
402 
403 void PagePropertyPanel::ExecutePageLayoutChange( const bool bMirrored )
404 {
405     mpPageItem->SetPageUsage( bMirrored ? SVX_PAGE_MIRROR : SVX_PAGE_ALL );
406     GetBindings()->GetDispatcher()->Execute( SID_ATTR_PAGE, SFX_CALLMODE_RECORD, mpPageItem.get(),  0L );
407 }
408 
409 
410 IMPL_LINK( PagePropertyPanel, ClickMarginHdl, ToolBox*, pToolBox )
411 {
412     mpMarginPopup.reset(
413         new ::svx::sidebar::Popup(
414             this,
415             ::boost::bind(&PagePropertyPanel::CreatePageMarginControl, this, _1),
416             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Page margins")) ) );
417     mpMarginPopup->Show( *pToolBox );
418 
419     return 0L;
420 }
421 
422 
423 void PagePropertyPanel::ClosePageMarginPopup()
424 {
425     mpMarginPopup->Hide();
426 }
427 
428 
429 
430 
431 ::svx::sidebar::PopupControl* PagePropertyPanel::CreatePageSizeControl( ::svx::sidebar::PopupContainer* pParent )
432 {
433     return new PageSizeControl(
434         pParent,
435         *this,
436         mePaper,
437         mpPageItem->IsLandscape(),
438         meFUnit );
439 }
440 
441 
442 void PagePropertyPanel::ExecuteSizeChange( const Paper ePaper )
443 {
444     Size aPageSize = SvxPaperInfo::GetPaperSize( ePaper, (MapUnit)(meUnit) );
445     if ( mpPageItem->IsLandscape() )
446     {
447         Swap( aPageSize );
448     }
449     mpPageSizeItem->SetSize( aPageSize );
450 
451     mpBindings->GetDispatcher()->Execute(SID_ATTR_PAGE_SIZE, SFX_CALLMODE_RECORD, mpPageSizeItem.get(),  0L );
452 }
453 
454 
455 IMPL_LINK( PagePropertyPanel, ClickSizeHdl, ToolBox*, pToolBox )
456 {
457     mpSizePopup.reset(
458         new ::svx::sidebar::Popup(
459             this,
460             ::boost::bind(&PagePropertyPanel::CreatePageSizeControl, this, _1),
461             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Page size")) ) );
462     mpSizePopup->Show( *pToolBox );
463 
464     return 0L;
465 }
466 
467 
468 void PagePropertyPanel::ClosePageSizePopup()
469 {
470     mpSizePopup->Hide();
471 }
472 
473 
474 
475 
476 ::svx::sidebar::PopupControl* PagePropertyPanel::CreatePageColumnControl( ::svx::sidebar::PopupContainer* pParent )
477 {
478     return new PageColumnControl(
479         pParent,
480         *this,
481         mpPageColumnTypeItem->GetValue(),
482         mpPageItem->IsLandscape() );
483 }
484 
485 
486 void PagePropertyPanel::ExecuteColumnChange( const sal_uInt16 nColumnType )
487 {
488     mpPageColumnTypeItem->SetValue( nColumnType );
489     mpBindings->GetDispatcher()->Execute(SID_ATTR_SWPAGE_COLUMN, SFX_CALLMODE_RECORD, mpPageColumnTypeItem.get(),  0L );
490 }
491 
492 
493 IMPL_LINK( PagePropertyPanel, ClickColumnHdl, ToolBox*, pToolBox )
494 {
495     mpColumnPopup.reset(
496         new ::svx::sidebar::Popup(
497             this,
498             ::boost::bind(&PagePropertyPanel::CreatePageColumnControl, this, _1),
499             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Page columns")) ) );
500     mpColumnPopup->Show( *pToolBox );
501 
502     return 0L;
503 }
504 
505 
506 void PagePropertyPanel::ClosePageColumnPopup()
507 {
508     mpColumnPopup->Hide();
509 }
510 
511 
512 
513 
514 void PagePropertyPanel::NotifyItemUpdate(
515     const sal_uInt16 nSId,
516     const SfxItemState eState,
517     const SfxPoolItem* pState)
518 {
519     switch( nSId )
520     {
521     case SID_ATTR_SWPAGE_COLUMN:
522         {
523             if ( eState >= SFX_ITEM_AVAILABLE &&
524                  pState && pState->ISA(SfxInt16Item) )
525             {
526                 mpPageColumnTypeItem.reset( static_cast<SfxInt16Item*>(pState->Clone()) );
527                 ChangeColumnImage( mpPageColumnTypeItem->GetValue() );
528             }
529         }
530         break;
531     case SID_ATTR_LONG_LRSPACE:
532         if ( eState >= SFX_ITEM_AVAILABLE &&
533              pState && pState->ISA(SvxLongLRSpaceItem) )
534         {
535             mpPageLRMarginItem.reset( static_cast<SvxLongLRSpaceItem*>(pState->Clone()) );
536             ChangeMarginImage();
537         }
538         break;
539 
540     case SID_ATTR_LONG_ULSPACE:
541         if ( eState >= SFX_ITEM_AVAILABLE &&
542              pState && pState->ISA(SvxLongULSpaceItem) )
543         {
544             mpPageULMarginItem.reset( static_cast<SvxLongULSpaceItem*>(pState->Clone()) );
545             ChangeMarginImage();
546         }
547         break;
548 
549     case SID_ATTR_PAGE:
550         if ( eState >= SFX_ITEM_AVAILABLE &&
551              pState && pState->ISA(SvxPageItem) )
552         {
553             mpPageItem.reset( static_cast<SvxPageItem*>(pState->Clone()) );
554             if ( mpPageItem->IsLandscape() )
555             {
556                 mpToolBoxOrientation->SetItemImage(TBI_ORIENTATION, mImgLandscape);
557             }
558             else
559             {
560                 mpToolBoxOrientation->SetItemImage(TBI_ORIENTATION, mImgPortrait);
561             }
562             ChangeMarginImage();
563             ChangeSizeImage();
564             ChangeColumnImage( mpPageColumnTypeItem->GetValue() );
565         }
566         break;
567 
568     case SID_ATTR_PAGE_SIZE:
569         if ( mbInvalidateSIDAttrPageOnSIDAttrPageSizeNotify )
570         {
571             mpBindings->Invalidate( SID_ATTR_PAGE, sal_True, sal_False );
572         }
573         if ( eState >= SFX_ITEM_AVAILABLE &&
574              pState && pState->ISA(SvxSizeItem) )
575         {
576             mpPageSizeItem.reset( static_cast<SvxSizeItem*>(pState->Clone()) );
577             ChangeSizeImage();
578         }
579         break;
580     case SID_ATTR_METRIC:
581         MetricState( eState, pState );
582         break;
583     }
584 }
585 
586 
587 void PagePropertyPanel::MetricState( SfxItemState eState, const SfxPoolItem* pState )
588 {
589     meFUnit = FUNIT_NONE;
590     if ( pState && eState >= SFX_ITEM_DEFAULT )
591     {
592         meFUnit = (FieldUnit)( (const SfxUInt16Item*)pState )->GetValue();
593     }
594     else
595     {
596         SfxViewFrame* pFrame = SfxViewFrame::Current();
597         SfxObjectShell* pSh = NULL;
598         if ( pFrame )
599             pSh = pFrame->GetObjectShell();
600         if ( pSh )
601         {
602             SfxModule* pModule = pSh->GetModule();
603             if ( pModule )
604             {
605                 const SfxPoolItem* pItem = pModule->GetItem( SID_ATTR_METRIC );
606                 if ( pItem )
607                     meFUnit = (FieldUnit)( (SfxUInt16Item*)pItem )->GetValue();
608             }
609             else
610             {
611                 DBG_ERRORFILE( "<PagePropertyPanel::MetricState(..)>: no module found" );
612             }
613         }
614     }
615 }
616 
617 
618 
619 
620 void PagePropertyPanel::ChangeMarginImage()
621 {
622     if ( mpPageLRMarginItem.get() == 0 ||
623          mpPageULMarginItem.get() == 0 ||
624          mpPageItem.get() == 0 )
625     {
626         return;
627     }
628 
629     const long cTolerance = 5;
630 
631     if( abs(mpPageLRMarginItem->GetLeft() - SWPAGE_NARROW_VALUE) <= cTolerance &&
632         abs(mpPageLRMarginItem->GetRight() - SWPAGE_NARROW_VALUE) <= cTolerance &&
633         abs(mpPageULMarginItem->GetUpper() - SWPAGE_NARROW_VALUE) <= cTolerance &&
634         abs(mpPageULMarginItem->GetLower() - SWPAGE_NARROW_VALUE) <= cTolerance &&
635         mpPageItem->GetPageUsage() != SVX_PAGE_MIRROR )
636         mpToolBoxMargin->SetItemImage( TBI_MARGIN, mpPageItem->IsLandscape() ? mImgNarrow_L : mImgNarrow );
637 
638     else if( abs(mpPageLRMarginItem->GetLeft() - SWPAGE_NORMAL_VALUE) <= cTolerance &&
639         abs(mpPageLRMarginItem->GetRight() - SWPAGE_NORMAL_VALUE) <= cTolerance &&
640         abs(mpPageULMarginItem->GetUpper() - SWPAGE_NORMAL_VALUE) <= cTolerance &&
641         abs(mpPageULMarginItem->GetLower() - SWPAGE_NORMAL_VALUE) <= cTolerance &&
642         mpPageItem->GetPageUsage() != SVX_PAGE_MIRROR )
643         mpToolBoxMargin->SetItemImage(TBI_MARGIN, mpPageItem->IsLandscape() ? mImgNormal_L : mImgNormal );
644 
645     else if( abs(mpPageLRMarginItem->GetLeft() - SWPAGE_WIDE_VALUE2) <= cTolerance &&
646         abs(mpPageLRMarginItem->GetRight() - SWPAGE_WIDE_VALUE2) <= cTolerance &&
647         abs(mpPageULMarginItem->GetUpper() - SWPAGE_WIDE_VALUE1) <= cTolerance &&
648         abs(mpPageULMarginItem->GetLower() - SWPAGE_WIDE_VALUE1) <= cTolerance &&
649         mpPageItem->GetPageUsage() != SVX_PAGE_MIRROR )
650         mpToolBoxMargin->SetItemImage(TBI_MARGIN, mpPageItem->IsLandscape() ? mImgWide_L : mImgWide );
651 
652     else if( abs(mpPageLRMarginItem->GetLeft() - SWPAGE_WIDE_VALUE3) <= cTolerance &&
653         abs(mpPageLRMarginItem->GetRight() - SWPAGE_WIDE_VALUE1) <= cTolerance &&
654         abs(mpPageULMarginItem->GetUpper() - SWPAGE_WIDE_VALUE1) <= cTolerance &&
655         abs(mpPageULMarginItem->GetLower() - SWPAGE_WIDE_VALUE1) <= cTolerance &&
656         mpPageItem->GetPageUsage() == SVX_PAGE_MIRROR )
657         mpToolBoxMargin->SetItemImage(TBI_MARGIN, mpPageItem->IsLandscape() ? mImgMirrored_L : mImgMirrored );
658 
659     else
660         mpToolBoxMargin->SetItemImage(TBI_MARGIN, mpPageItem->IsLandscape() ? mImgMarginCustom_L : mImgMarginCustom );
661 }
662 
663 
664 void PagePropertyPanel::ChangeSizeImage()
665 {
666     if ( mpPageSizeItem.get() == 0 ||
667          mpPageItem.get() == 0 )
668     {
669         return;
670     }
671 
672     Size aTmpPaperSize = mpPageSizeItem->GetSize();
673     if ( mpPageItem->IsLandscape() )
674     {
675         Swap( aTmpPaperSize ); // Swap(..) defined in editeng/paperinf.hxx
676     }
677 
678     mePaper = SvxPaperInfo::GetSvxPaper( aTmpPaperSize, static_cast<MapUnit>(meUnit), sal_True );
679 
680     sal_uInt16 nImageIdx = 0;
681     switch ( mePaper )
682     {
683     case PAPER_A3:
684         nImageIdx = 1;
685         break;
686     case PAPER_A4:
687         nImageIdx = 2;
688         break;
689     case PAPER_A5:
690         nImageIdx = 3;
691         break;
692     case PAPER_B4_ISO:
693         nImageIdx = 4;
694         break;
695     case PAPER_B5_ISO:
696         nImageIdx = 5;
697         break;
698     case PAPER_ENV_C5:
699         nImageIdx = 6;
700         break;
701     case PAPER_LETTER:
702         nImageIdx = 7;
703         break;
704     case PAPER_LEGAL:
705         nImageIdx = 8;
706         break;
707     default:
708         nImageIdx = 0;
709         mePaper = PAPER_USER;
710         break;
711     }
712 
713     if ( nImageIdx == 0 )
714     {
715         mpToolBoxSize->SetItemImage( TBI_SIZE,
716                                      ( mpPageItem->IsLandscape() ? mImgSizeNone_L : mImgSizeNone  ) );
717     }
718     else
719     {
720         mpToolBoxSize->SetItemImage( TBI_SIZE,
721                                      ( mpPageItem->IsLandscape() ? maImgSize_L[nImageIdx-1] : maImgSize[nImageIdx-1] ) );
722     }
723 }
724 
725 
726 void PagePropertyPanel::ChangeColumnImage( const sal_uInt16 nColumnType )
727 {
728     if ( mpPageItem.get() == 0 )
729     {
730         return;
731     }
732 
733     if ( !mpPageItem->IsLandscape() )
734     {
735         switch( nColumnType )
736         {
737         case 1:
738             mpToolBoxColumn->SetItemImage(TBI_COLUMN, mImgColumn1);
739             break;
740         case 2:
741             mpToolBoxColumn->SetItemImage(TBI_COLUMN, mImgColumn2);
742             break;
743         case 3:
744             mpToolBoxColumn->SetItemImage(TBI_COLUMN, mImgColumn3);
745             break;
746         case 4:
747             mpToolBoxColumn->SetItemImage(TBI_COLUMN, mImgLeft);
748             break;
749         case 5:
750             mpToolBoxColumn->SetItemImage(TBI_COLUMN, mImgRight);
751             break;
752         default:
753             mpToolBoxColumn->SetItemImage(TBI_COLUMN, mImgColumnNone);
754         }
755     }
756     else
757     {
758         switch( nColumnType )
759         {
760         case 1:
761             mpToolBoxColumn->SetItemImage(TBI_COLUMN, mImgColumn1_L);
762             break;
763         case 2:
764             mpToolBoxColumn->SetItemImage(TBI_COLUMN, mImgColumn2_L);
765             break;
766         case 3:
767             mpToolBoxColumn->SetItemImage(TBI_COLUMN, mImgColumn3_L);
768             break;
769         case 4:
770             mpToolBoxColumn->SetItemImage(TBI_COLUMN, mImgLeft_L);
771             break;
772         case 5:
773             mpToolBoxColumn->SetItemImage(TBI_COLUMN, mImgRight_L);
774             break;
775         default:
776             mpToolBoxColumn->SetItemImage(TBI_COLUMN, mImgColumnNone_L);
777         }
778     }
779 }
780 
781 } } // end of namespace ::sw::sidebar
782