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 #include "precompiled_sw.hxx"
23 
24 #include "PageSizeControl.hxx"
25 #include "PagePropertyPanel.hxx"
26 #include "PagePropertyPanel.hrc"
27 
28 #include <cmdid.h>
29 #include <swtypes.hxx>
30 
31 #include <svx/sidebar/ValueSetWithTextControl.hxx>
32 
33 #include <tools/inetmime.hxx>
34 #include <editeng/paperinf.hxx>
35 #include <sfx2/bindings.hxx>
36 #include <sfx2/dispatch.hxx>
37 
38 
39 namespace sw { namespace sidebar {
40 
PageSizeControl(Window * pParent,PagePropertyPanel & rPanel,const Paper ePaper,const sal_Bool bLandscape,const FieldUnit eFUnit)41 PageSizeControl::PageSizeControl(
42     Window* pParent,
43     PagePropertyPanel& rPanel,
44     const Paper ePaper,
45     const sal_Bool bLandscape,
46     const FieldUnit eFUnit )
47     : ::svx::sidebar::PopupControl( pParent, SW_RES(RID_POPUP_SWPAGE_SIZE) )
48     , mpSizeValueSet( new ::svx::sidebar::ValueSetWithTextControl( ::svx::sidebar::ValueSetWithTextControl::TEXT_TEXT, this, SW_RES(VS_SIZE) ) )
49     , maMoreButton( this, SW_RES(CB_SIZE_MORE) )
50     , maWidthHeightField( this, SW_RES(FLD_WIDTH_HEIGHT) )
51     , mePaper( ePaper )
52     , maPaperList()
53     , mrPagePropPanel(rPanel)
54 {
55     maWidthHeightField.Hide();
56     SetFieldUnit( maWidthHeightField, eFUnit );
57 
58     maPaperList.push_back( PAPER_A3 );
59     maPaperList.push_back( PAPER_A4 );
60     maPaperList.push_back( PAPER_A5 );
61     maPaperList.push_back( PAPER_B4_ISO );
62     maPaperList.push_back( PAPER_B5_ISO );
63     maPaperList.push_back( PAPER_ENV_C5 );
64     maPaperList.push_back( PAPER_LETTER );
65     maPaperList.push_back( PAPER_LEGAL );
66 
67     mpSizeValueSet->SetStyle( mpSizeValueSet->GetStyle() | WB_3DLOOK | WB_NO_DIRECTSELECT );
68     mpSizeValueSet->SetColor( GetSettings().GetStyleSettings().GetMenuColor() );
69 
70     sal_uInt16 nSelectedItem = 0;
71     {
72         XubString aMetricStr;
73         {
74             const XubString aText = maWidthHeightField.GetText();
75             for (short i = aText.Len() - 1; i >= 0; i--)
76             {
77                 xub_Unicode c = aText.GetChar(i);
78                 if ( INetMIME::isAlpha(c) || (c == '\'') || (c == '\"') || (c == '%') )
79                 {
80                     aMetricStr.Insert(c, 0);
81                 }
82                 else
83                 {
84                     if (aMetricStr.Len())
85                     {
86                         break;
87                     }
88                 }
89             }
90         }
91 
92         const LocaleDataWrapper& localeDataWrapper = maWidthHeightField.GetLocaleDataWrapper();
93         String WidthStr;
94         String HeightStr;
95         String ItemText2;
96         for ( ::std::vector< Paper >::size_type nPaperIdx = 0;
97               nPaperIdx < maPaperList.size();
98               ++nPaperIdx )
99         {
100             Size aPaperSize = SvxPaperInfo::GetPaperSize( maPaperList[ nPaperIdx ] );
101             if ( bLandscape )
102             {
103                 Swap( aPaperSize );
104             }
105             maWidthHeightField.SetValue( maWidthHeightField.Normalize( aPaperSize.Width() ), FUNIT_TWIP );
106             WidthStr = localeDataWrapper.getNum(
107                 maWidthHeightField.GetValue(),
108                 maWidthHeightField.GetDecimalDigits(),
109                 maWidthHeightField.IsUseThousandSep(),
110                 maWidthHeightField.IsShowTrailingZeros() );
111 
112             maWidthHeightField.SetValue( maWidthHeightField.Normalize( aPaperSize.Height() ), FUNIT_TWIP);
113             HeightStr = localeDataWrapper.getNum(
114                 maWidthHeightField.GetValue(),
115                 maWidthHeightField.GetDecimalDigits(),
116                 maWidthHeightField.IsUseThousandSep(),
117                 maWidthHeightField.IsShowTrailingZeros() );
118 
119             ItemText2 = WidthStr;
120             ItemText2 += String::CreateFromAscii(" x ");
121             ItemText2 += HeightStr;
122             ItemText2 += String::CreateFromAscii(" ");
123             ItemText2 += aMetricStr;
124 
125             mpSizeValueSet->AddItem(
126                 SvxPaperInfo::GetName( maPaperList[ nPaperIdx ] ),
127                 ItemText2,
128                 0 );
129 
130             if ( maPaperList[ nPaperIdx ] == mePaper )
131             {
132                 nSelectedItem = nPaperIdx + 1;
133             }
134         }
135     }
136 
137     mpSizeValueSet->SetNoSelection();
138     mpSizeValueSet->SetSelectHdl( LINK(this, PageSizeControl,ImplSizeHdl ) );
139     mpSizeValueSet->Show();
140 
141     mpSizeValueSet->SelectItem( nSelectedItem );
142     mpSizeValueSet->Format();
143     mpSizeValueSet->StartSelection();
144 
145     maMoreButton.SetClickHdl( LINK( this, PageSizeControl, MoreButtonClickHdl_Impl ) );
146     maMoreButton.GrabFocus();
147 
148     FreeResource();
149 }
150 
151 
~PageSizeControl(void)152 PageSizeControl::~PageSizeControl(void)
153 {
154     delete mpSizeValueSet;
155 }
156 
157 
IMPL_LINK(PageSizeControl,ImplSizeHdl,void *,pControl)158 IMPL_LINK(PageSizeControl, ImplSizeHdl, void *, pControl)
159 {
160     mpSizeValueSet->SetNoSelection();
161     if ( pControl == mpSizeValueSet )
162     {
163         const sal_uInt16 nSelectedPaper = mpSizeValueSet->GetSelectItemId();
164         const Paper ePaper = maPaperList[nSelectedPaper - 1];
165         if ( ePaper != mePaper )
166         {
167             mePaper = ePaper;
168             mrPagePropPanel.ExecuteSizeChange( mePaper );
169         }
170     }
171 
172     mrPagePropPanel.ClosePageSizePopup();
173     return 0;
174 }
175 
IMPL_LINK(PageSizeControl,MoreButtonClickHdl_Impl,void *,EMPTYARG)176 IMPL_LINK(PageSizeControl, MoreButtonClickHdl_Impl, void *, EMPTYARG)
177 {
178     mrPagePropPanel.GetBindings()->GetDispatcher()->Execute( FN_FORMAT_PAGE_SETTING_DLG, SFX_CALLMODE_ASYNCHRON );
179 
180     mrPagePropPanel.ClosePageSizePopup();
181     return 0;
182 }
183 
184 
185 } } // end of namespace sw::sidebar
186 
187