xref: /AOO41X/main/sc/source/ui/sidebar/NumberFormatPropertyPanel.cxx (revision bfaba8a2bfbeecb976bed59dc67e51a3365db0fb)
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_sc.hxx"
23 
24 #include <sfx2/sidebar/propertypanel.hrc>
25 #include <sfx2/sidebar/Theme.hxx>
26 #include <sfx2/sidebar/ControlFactory.hxx>
27 #include <NumberFormatPropertyPanel.hxx>
28 #include <NumberFormatPropertyPanel.hrc>
29 #include "sc.hrc"
30 #include "scresid.hxx"
31 #include <sfx2/bindings.hxx>
32 #include <sfx2/dispatch.hxx>
33 #include <vcl/fixed.hxx>
34 #include <vcl/lstbox.hxx>
35 #include <vcl/field.hxx>
36 #include <vcl/toolbox.hxx>
37 #include <svl/intitem.hxx>
38 #include <svl/stritem.hxx>
39 
40 using namespace css;
41 using namespace cssu;
42 
43 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
44 
45 //////////////////////////////////////////////////////////////////////////////
46 // namespace open
47 
48 namespace sc { namespace sidebar {
49 
50 //////////////////////////////////////////////////////////////////////////////
51 
52 NumberFormatPropertyPanel::NumberFormatPropertyPanel(
53     Window* pParent,
54     const cssu::Reference<css::frame::XFrame>& rxFrame,
55     SfxBindings* pBindings)
56 :   Control(
57         pParent,
58         ScResId(RID_PROPERTYPANEL_SC_NUMBERFORMAT)),
59     mpFtCategory(new FixedText(this, ScResId(FT_CATEGORY))),
60     mpLbCategory(new ListBox(this, ScResId(LB_CATEGORY))),
61     mpTBCategoryBackground(sfx2::sidebar::ControlFactory::CreateToolBoxBackground(this)),
62     mpTBCategory(sfx2::sidebar::ControlFactory::CreateToolBox(mpTBCategoryBackground.get(), ScResId(TBX_CATEGORY))),
63     mpFtDecimals(new FixedText(this, ScResId(FT_DECIMALS))),
64     mpEdDecimals(new NumericField(this, ScResId(ED_DECIMALS))),
65     mpFtLeadZeroes(new FixedText(this, ScResId(FT_LEADZEROES))),
66     mpEdLeadZeroes(new NumericField(this, ScResId(ED_LEADZEROES))),
67     mpBtnNegRed(new CheckBox(this, ScResId(BTN_NEGRED))),
68     mpBtnThousand(new CheckBox(this, ScResId(BTN_THOUSAND))),
69     maNumFormatControl(SID_NUMBER_TYPE_FORMAT, *pBindings, *this),
70 
71     // Caution! SID_NUMBER_FORMAT is reworked in symphony code, may be needed (!) If
72     // yes, grep for it in SC and symphony (!)
73     maFormatControl(SID_NUMBER_FORMAT, *pBindings, *this),
74 
75     maImgNumber(ScResId(IMG_NUMBER)),
76     maImgPercent(ScResId(IMG_PERCENT)),
77     maImgCurrency(ScResId(IMG_CURRENCY)),
78     maImgDate(ScResId(IMG_DATE)),
79     maImgText(ScResId(IMG_TEXT)),
80     mnCategorySelected(0),
81     mxFrame(rxFrame),
82     maContext(),
83     mpBindings(pBindings)
84 {
85     Initialize();
86     FreeResource();
87 }
88 
89 //////////////////////////////////////////////////////////////////////////////
90 
91 NumberFormatPropertyPanel::~NumberFormatPropertyPanel()
92 {
93     // Destroy the toolboxes, then their background windows.
94     mpTBCategory.reset();
95     mpTBCategoryBackground.reset();
96 }
97 
98 //////////////////////////////////////////////////////////////////////////////
99 
100 void NumberFormatPropertyPanel::Initialize()
101 {
102 	Link aLink = LINK(this, NumberFormatPropertyPanel, NumFormatSelectHdl);
103 	mpLbCategory->SetSelectHdl ( aLink );
104 	mpLbCategory->SelectEntryPos(0);
105 	mpLbCategory->SetAccessibleName(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Category")));		//wj acc
106     mpLbCategory->SetDropDownLineCount(mpLbCategory->GetEntryCount());
107 
108 	mpTBCategory->SetItemImage(ID_NUMBER, maImgNumber);
109 	mpTBCategory->SetItemImage(ID_PERCENT, maImgPercent);
110 	mpTBCategory->SetItemImage(ID_CURRENCY, maImgCurrency);
111 	mpTBCategory->SetItemImage(ID_DATE, maImgDate);
112 	mpTBCategory->SetItemImage(ID_TEXT, maImgText);
113 	Size aTbxSize( mpTBCategory->CalcWindowSizePixel() );
114 	mpTBCategory->SetOutputSizePixel( aTbxSize );
115 	mpTBCategory->SetBackground(Wallpaper());
116 	mpTBCategory->SetPaintTransparent(true);
117 	aLink = LINK(this, NumberFormatPropertyPanel, NumFormatHdl);
118 	mpTBCategory->SetSelectHdl ( aLink );
119 
120 	aLink = LINK(this, NumberFormatPropertyPanel, NumFormatValueHdl);
121 
122 	mpEdDecimals->SetModifyHdl( aLink );
123 	mpEdLeadZeroes->SetModifyHdl( aLink );
124 	mpEdDecimals->SetAccessibleName(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Decimal Places")));		//wj acc
125 	mpEdLeadZeroes->SetAccessibleName(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Leading Zeroes")));		//wj acc
126 	mpBtnNegRed->SetClickHdl( aLink );
127 	mpBtnThousand->SetClickHdl( aLink );
128 
129     mpLbCategory->SetAccessibleRelationLabeledBy(mpFtCategory.get());
130 	mpTBCategory->SetAccessibleRelationLabeledBy(mpTBCategory.get());
131 	mpEdDecimals->SetAccessibleRelationLabeledBy(mpFtDecimals.get());
132 	mpEdLeadZeroes->SetAccessibleRelationLabeledBy(mpFtLeadZeroes.get());
133 }
134 
135 //////////////////////////////////////////////////////////////////////////////
136 
137 IMPL_LINK( NumberFormatPropertyPanel, NumFormatHdl, ToolBox*, pBox )
138 {
139 	sal_uInt16 nVal = pBox->GetCurItemId();
140 	sal_uInt16 nId = 0;
141 	switch(nVal)
142 	{
143 	case ID_NUMBER:
144 		nId = 1;
145 		break;
146 	case ID_PERCENT:
147 		nId = 2;
148 		break;
149 	case ID_CURRENCY:
150 		nId = 3;
151 		break;
152 	case ID_DATE:
153 		nId = 4;
154 		break;
155 	case ID_TEXT:
156 		nId = 9;
157 		break;
158 	default:
159 		;
160 	}
161 	if( nId != mnCategorySelected )
162 	{
163 		SfxUInt16Item aItem( SID_NUMBER_TYPE_FORMAT,  nId );
164 		GetBindings()->GetDispatcher()->Execute(SID_NUMBER_TYPE_FORMAT, SFX_CALLMODE_RECORD, &aItem, 0L);
165 	}
166 	return 0L;
167 }
168 
169 //////////////////////////////////////////////////////////////////////////////
170 
171 IMPL_LINK( NumberFormatPropertyPanel, NumFormatSelectHdl, ListBox*, pBox )
172 {
173 	sal_uInt16 nVal = pBox->GetSelectEntryPos();
174 	if( nVal != mnCategorySelected )
175 	{
176 		SfxUInt16Item aItem( SID_NUMBER_TYPE_FORMAT,  nVal );
177 		GetBindings()->GetDispatcher()->Execute(SID_NUMBER_TYPE_FORMAT, SFX_CALLMODE_RECORD, &aItem, 0L);
178 		mnCategorySelected = nVal;
179 	}
180 	return 0L;
181 }
182 
183 //////////////////////////////////////////////////////////////////////////////
184 
185 IMPL_LINK( NumberFormatPropertyPanel, NumFormatValueHdl, void*, EMPTYARG )
186 {
187 	String		  aFormat;
188 	String		  sBreak = String::CreateFromAscii(",");
189 	bool		  bThousand 	=    mpBtnThousand->IsEnabled()
190 		&& mpBtnThousand->IsChecked();
191 	bool		  bNegRed		=    mpBtnNegRed->IsEnabled()
192 		&& mpBtnNegRed->IsChecked();
193 	sal_uInt16		  nPrecision	= (mpEdDecimals->IsEnabled())
194 		? (sal_uInt16)mpEdDecimals->GetValue()
195 		: (sal_uInt16)0;
196 	sal_uInt16		  nLeadZeroes	= (mpEdLeadZeroes->IsEnabled())
197 		? (sal_uInt16)mpEdLeadZeroes->GetValue()
198 		: (sal_uInt16)0;
199 
200 	String sThousand = String::CreateFromInt32(bThousand);
201 	String sNegRed = String::CreateFromInt32(bNegRed);
202 	String sPrecision = String::CreateFromInt32(nPrecision);
203 	String sLeadZeroes = String::CreateFromInt32(nLeadZeroes);
204 
205 	aFormat += sThousand;
206 	aFormat += sBreak;
207 	aFormat += sNegRed;
208 	aFormat += sBreak;
209 	aFormat += sPrecision;
210 	aFormat += sBreak;
211 	aFormat += sLeadZeroes;
212 	aFormat += sBreak;
213 
214 	SfxStringItem aItem( SID_NUMBER_FORMAT,  aFormat );
215 	GetBindings()->GetDispatcher()->Execute(SID_NUMBER_FORMAT, SFX_CALLMODE_RECORD, &aItem, 0L);
216 	return 0L;
217 }
218 
219 //////////////////////////////////////////////////////////////////////////////
220 
221 NumberFormatPropertyPanel* NumberFormatPropertyPanel::Create (
222     Window* pParent,
223     const cssu::Reference<css::frame::XFrame>& rxFrame,
224     SfxBindings* pBindings)
225 {
226     if (pParent == NULL)
227         throw lang::IllegalArgumentException(A2S("no parent Window given to NumberFormatPropertyPanel::Create"), NULL, 0);
228     if ( ! rxFrame.is())
229         throw lang::IllegalArgumentException(A2S("no XFrame given to NumberFormatPropertyPanel::Create"), NULL, 1);
230     if (pBindings == NULL)
231         throw lang::IllegalArgumentException(A2S("no SfxBindings given to NumberFormatPropertyPanel::Create"), NULL, 2);
232 
233     return new NumberFormatPropertyPanel(
234         pParent,
235         rxFrame,
236         pBindings);
237 }
238 
239 //////////////////////////////////////////////////////////////////////////////
240 
241 void NumberFormatPropertyPanel::DataChanged(
242     const DataChangedEvent& rEvent)
243 {
244     (void)rEvent;
245 }
246 
247 //////////////////////////////////////////////////////////////////////////////
248 
249 void NumberFormatPropertyPanel::HandleContextChange(
250     const ::sfx2::sidebar::EnumContext aContext)
251 {
252     if(maContext == aContext)
253     {
254         // Nothing to do.
255         return;
256     }
257 
258     maContext = aContext;
259 
260 
261 
262     // todo
263 }
264 
265 //////////////////////////////////////////////////////////////////////////////
266 
267 void NumberFormatPropertyPanel::NotifyItemUpdate(
268     sal_uInt16 nSID,
269     SfxItemState eState,
270     const SfxPoolItem* pState)
271 {
272 	switch(nSID)
273 	{
274 	case SID_NUMBER_TYPE_FORMAT:
275 		{
276 			if( eState >= SFX_ITEM_AVAILABLE)
277 			{
278 				const SfxInt16Item* pItem = (const SfxInt16Item*)pState;
279 				sal_uInt16 nVal = pItem->GetValue();
280 				mnCategorySelected = nVal;
281 				mpLbCategory->SelectEntryPos(nVal);
282 				if( nVal < 4 )
283 				{
284 					mpBtnThousand->Enable();
285 					mpBtnNegRed->Enable();
286 					mpEdDecimals->Enable();
287 					mpEdLeadZeroes->Enable();
288 				}
289 				else
290 				{
291 					mpBtnThousand->Disable();
292 					mpBtnNegRed->Disable();
293 					mpEdDecimals->Disable();
294 					mpEdLeadZeroes->Disable();
295 				}
296 			}
297 			else
298 			{
299 				mpLbCategory->SetNoSelection();
300 				mnCategorySelected = 0;
301 				mpBtnThousand->Disable();
302 				mpBtnNegRed->Disable();
303 				mpEdDecimals->Disable();
304 				mpEdLeadZeroes->Disable();
305 			}
306 		}
307 		break;
308 	case SID_NUMBER_FORMAT:
309 		{
310 			bool		  bThousand 	=    0;
311 			bool		  bNegRed		=    0;
312 			sal_uInt16		  nPrecision	=	 0;
313 			sal_uInt16		  nLeadZeroes	=    0;
314 			if( eState >= SFX_ITEM_AVAILABLE)
315 			{
316 				const SfxStringItem* pItem = (const SfxStringItem*)pState;
317 				String aCode = pItem->GetValue();
318 			/*	if(aCode.Equals(String::CreateFromAscii("General")))
319 				{
320 					mnCategorySelected = 0;
321 					mpLbCategory->SelectEntryPos(0);
322 					mpBtnThousand->Check(0);
323 					mpBtnNegRed->Check(0);
324 					mpEdDecimals->SetValue(0);
325 					mpEdLeadZeroes->SetValue(1);
326 					break;
327 				}
328 				else if( mpLbCategory->GetSelectEntryPos() == 0 )
329 				{
330 					mnCategorySelected = 1;
331 					mpLbCategory->SelectEntryPos(1);
332 				}*/
333 				sal_uInt16 aLen = aCode.Len();
334 				String* sFormat = new String[4];
335 				String  sTmpStr = String::CreateFromAscii("");
336 				sal_uInt16 nCount = 0;
337 				sal_uInt16 nStrCount = 0;
338 				while( nCount < aLen )
339 				{
340 					sal_Unicode cChar = aCode.GetChar(nCount);
341 					if(cChar == sal_Unicode(','))
342 					{
343 						sFormat[nStrCount] = sTmpStr;
344 						sTmpStr = String::CreateFromAscii("");
345 						nStrCount++;
346 					}
347 					else
348 					{
349 						sTmpStr += cChar;
350 					}
351 					nCount++;
352 				}
353 				bThousand 	=    sFormat[0].ToInt32();
354 				bNegRed		=    sFormat[1].ToInt32();
355 				nPrecision	=	 (sal_uInt16)sFormat[2].ToInt32();
356 				nLeadZeroes	=    (sal_uInt16)sFormat[3].ToInt32();
357 				delete[] sFormat;
358 			}
359 			else
360 			{
361 				bThousand 	=    0;
362 				bNegRed		=    0;
363 				nPrecision	=	 0;
364 				nLeadZeroes	=    1;
365 			}
366 			mpBtnThousand->Check(bThousand);
367 			mpBtnNegRed->Check(bNegRed);
368 			mpEdDecimals->SetValue(nPrecision);
369 			mpEdLeadZeroes->SetValue(nLeadZeroes);
370 		}
371 	default:
372 		;
373 	}
374 }
375 
376 //////////////////////////////////////////////////////////////////////////////
377 
378 SfxBindings* NumberFormatPropertyPanel::GetBindings()
379 {
380     return mpBindings;
381 }
382 
383 //////////////////////////////////////////////////////////////////////////////
384 // namespace close
385 
386 }} // end of namespace ::sc::sidebar
387 
388 //////////////////////////////////////////////////////////////////////////////
389 // eof
390