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