Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / sidebar / NumberFormatPropertyPanel.cxx
blob59c8d050a6299cb8af82e33ac99fd5433b76e791
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sfx2/sidebar/ResourceDefinitions.hrc>
21 #include <sfx2/sidebar/Theme.hxx>
22 #include <sfx2/sidebar/ControlFactory.hxx>
23 #include <NumberFormatPropertyPanel.hxx>
24 #include "sc.hrc"
25 #include "scresid.hxx"
26 #include <sfx2/bindings.hxx>
27 #include <sfx2/dispatch.hxx>
28 #include <sfx2/imagemgr.hxx>
29 #include <vcl/fixed.hxx>
30 #include <vcl/lstbox.hxx>
31 #include <vcl/field.hxx>
32 #include <vcl/toolbox.hxx>
33 #include <svl/intitem.hxx>
34 #include <svl/stritem.hxx>
36 using namespace css;
37 using namespace cssu;
38 using ::sfx2::sidebar::Theme;
40 const char UNO_NUMERICFIELD[] = ".uno:NumericField";
41 const char UNO_NUMBERFORMATPERCENT[] = ".uno:NumberFormatPercent";
42 const char UNO_NUMBERFORMATCURRENCY[] = ".uno:NumberFormatCurrency";
43 const char UNO_NUMBERFORMATDATE[] = ".uno:NumberFormatDate";
44 const char UNO_INSERTFIXEDTEXT[] = ".uno:InsertFixedText";
46 //////////////////////////////////////////////////////////////////////////////
47 // namespace open
49 namespace sc { namespace sidebar {
51 //////////////////////////////////////////////////////////////////////////////
53 NumberFormatPropertyPanel::NumberFormatPropertyPanel(
54 Window* pParent,
55 const cssu::Reference<css::frame::XFrame>& rxFrame,
56 SfxBindings* pBindings)
57 : PanelLayout(pParent,"NumberFormatPropertyPanel", "modules/scalc/ui/sidebarnumberformat.ui", rxFrame),
58 maNumFormatControl(SID_NUMBER_TYPE_FORMAT, *pBindings, *this),
60 // Caution! SID_NUMBER_FORMAT is reworked in symphony code, may be needed (!) If
61 // yes, grep for it in SC and symphony (!)
62 maFormatControl(SID_NUMBER_FORMAT, *pBindings, *this),
64 mnCategorySelected(0),
65 mxFrame(rxFrame),
66 maContext(),
67 mpBindings(pBindings)
69 get(mpLbCategory, "category");
70 get(mpTBCategory, "numberformat");
71 get(mpEdDecimals, "decimalplaces");
72 get(mpEdLeadZeroes, "leadingzeroes");
73 get(mpBtnNegRed, "negativenumbersred");
74 get(mpBtnThousand, "thousandseparator");
76 Initialize();
79 //////////////////////////////////////////////////////////////////////////////
81 NumberFormatPropertyPanel::~NumberFormatPropertyPanel()
85 //////////////////////////////////////////////////////////////////////////////
87 void NumberFormatPropertyPanel::Initialize()
89 Link aLink = LINK(this, NumberFormatPropertyPanel, NumFormatSelectHdl);
90 mpLbCategory->SetSelectHdl ( aLink );
91 mpLbCategory->SelectEntryPos(0);
92 mpLbCategory->SetAccessibleName(OUString( "Category")); //wj acc
93 mpLbCategory->SetDropDownLineCount(mpLbCategory->GetEntryCount());
95 aLink = LINK(this, NumberFormatPropertyPanel, NumFormatHdl);
96 mpTBCategory->SetSelectHdl ( aLink );
98 aLink = LINK(this, NumberFormatPropertyPanel, NumFormatValueHdl);
100 mpEdDecimals->SetModifyHdl( aLink );
101 mpEdLeadZeroes->SetModifyHdl( aLink );
102 mpEdDecimals->SetAccessibleName(OUString( "Decimal Places")); //wj acc
103 mpEdLeadZeroes->SetAccessibleName(OUString( "Leading Zeroes")); //wj acc
104 mpBtnNegRed->SetClickHdl( aLink );
105 mpBtnThousand->SetClickHdl( aLink );
107 mpTBCategory->SetAccessibleRelationLabeledBy(mpTBCategory);
110 //////////////////////////////////////////////////////////////////////////////
112 IMPL_LINK( NumberFormatPropertyPanel, NumFormatHdl, ToolBox*, pBox )
114 const OUString aCommand(pBox->GetItemCommand(pBox->GetCurItemId()));
115 sal_uInt16 nId = 0;
117 if(aCommand == UNO_NUMERICFIELD)
118 nId = 1;
119 else if(aCommand == UNO_NUMBERFORMATPERCENT)
120 nId = 2;
121 else if(aCommand == UNO_NUMBERFORMATCURRENCY)
122 nId = 3;
123 else if(aCommand == UNO_NUMBERFORMATDATE)
124 nId = 4;
125 else if(aCommand == UNO_INSERTFIXEDTEXT)
126 nId = 9;
128 if( nId != mnCategorySelected )
130 SfxUInt16Item aItem( SID_NUMBER_TYPE_FORMAT, nId );
131 GetBindings()->GetDispatcher()->Execute(SID_NUMBER_TYPE_FORMAT, SFX_CALLMODE_RECORD, &aItem, 0L);
133 return 0L;
136 //////////////////////////////////////////////////////////////////////////////
138 IMPL_LINK( NumberFormatPropertyPanel, NumFormatSelectHdl, ListBox*, pBox )
140 sal_uInt16 nVal = pBox->GetSelectEntryPos();
141 if( nVal != mnCategorySelected )
143 SfxUInt16Item aItem( SID_NUMBER_TYPE_FORMAT, nVal );
144 GetBindings()->GetDispatcher()->Execute(SID_NUMBER_TYPE_FORMAT, SFX_CALLMODE_RECORD, &aItem, 0L);
145 mnCategorySelected = nVal;
147 return 0L;
150 //////////////////////////////////////////////////////////////////////////////
152 IMPL_LINK( NumberFormatPropertyPanel, NumFormatValueHdl, void*, EMPTYARG )
154 OUString aFormat;
155 OUString sBreak = ",";
156 bool bThousand = mpBtnThousand->IsEnabled()
157 && mpBtnThousand->IsChecked();
158 bool bNegRed = mpBtnNegRed->IsEnabled()
159 && mpBtnNegRed->IsChecked();
160 sal_uInt16 nPrecision = (mpEdDecimals->IsEnabled())
161 ? (sal_uInt16)mpEdDecimals->GetValue()
162 : (sal_uInt16)0;
163 sal_uInt16 nLeadZeroes = (mpEdLeadZeroes->IsEnabled())
164 ? (sal_uInt16)mpEdLeadZeroes->GetValue()
165 : (sal_uInt16)0;
167 OUString sThousand = OUString::number(static_cast<sal_Int32>(bThousand));
168 OUString sNegRed = OUString::number(static_cast<sal_Int32>(bNegRed));
169 OUString sPrecision = OUString::number(nPrecision);
170 OUString sLeadZeroes = OUString::number(nLeadZeroes);
172 aFormat += sThousand;
173 aFormat += sBreak;
174 aFormat += sNegRed;
175 aFormat += sBreak;
176 aFormat += sPrecision;
177 aFormat += sBreak;
178 aFormat += sLeadZeroes;
179 aFormat += sBreak;
181 SfxStringItem aItem( SID_NUMBER_FORMAT, aFormat );
182 GetBindings()->GetDispatcher()->Execute(SID_NUMBER_FORMAT, SFX_CALLMODE_RECORD, &aItem, 0L);
183 return 0L;
186 //////////////////////////////////////////////////////////////////////////////
188 NumberFormatPropertyPanel* NumberFormatPropertyPanel::Create (
189 Window* pParent,
190 const cssu::Reference<css::frame::XFrame>& rxFrame,
191 SfxBindings* pBindings)
193 if (pParent == NULL)
194 throw lang::IllegalArgumentException("no parent Window given to NumberFormatPropertyPanel::Create", NULL, 0);
195 if ( ! rxFrame.is())
196 throw lang::IllegalArgumentException("no XFrame given to NumberFormatPropertyPanel::Create", NULL, 1);
197 if (pBindings == NULL)
198 throw lang::IllegalArgumentException("no SfxBindings given to NumberFormatPropertyPanel::Create", NULL, 2);
200 return new NumberFormatPropertyPanel(
201 pParent,
202 rxFrame,
203 pBindings);
206 //////////////////////////////////////////////////////////////////////////////
208 void NumberFormatPropertyPanel::DataChanged(
209 const DataChangedEvent& rEvent)
211 (void)rEvent;
214 //////////////////////////////////////////////////////////////////////////////
216 void NumberFormatPropertyPanel::HandleContextChange(
217 const ::sfx2::sidebar::EnumContext aContext)
219 if(maContext == aContext)
221 // Nothing to do.
222 return;
225 maContext = aContext;
229 // todo
232 //////////////////////////////////////////////////////////////////////////////
234 void NumberFormatPropertyPanel::NotifyItemUpdate(
235 sal_uInt16 nSID,
236 SfxItemState eState,
237 const SfxPoolItem* pState,
238 const bool bIsEnabled)
240 (void)bIsEnabled;
242 switch(nSID)
244 case SID_NUMBER_TYPE_FORMAT:
246 if( eState >= SFX_ITEM_AVAILABLE)
248 const SfxInt16Item* pItem = (const SfxInt16Item*)pState;
249 sal_uInt16 nVal = pItem->GetValue();
250 mnCategorySelected = nVal;
251 mpLbCategory->SelectEntryPos(nVal);
252 if( nVal < 4 )
254 mpBtnThousand->Enable();
255 mpBtnNegRed->Enable();
256 mpEdDecimals->Enable();
257 mpEdLeadZeroes->Enable();
259 else
261 mpBtnThousand->Disable();
262 mpBtnNegRed->Disable();
263 mpEdDecimals->Disable();
264 mpEdLeadZeroes->Disable();
267 else
269 mpLbCategory->SetNoSelection();
270 mnCategorySelected = 0;
271 mpBtnThousand->Disable();
272 mpBtnNegRed->Disable();
273 mpEdDecimals->Disable();
274 mpEdLeadZeroes->Disable();
277 break;
278 case SID_NUMBER_FORMAT:
280 bool bThousand = 0;
281 bool bNegRed = 0;
282 sal_uInt16 nPrecision = 0;
283 sal_uInt16 nLeadZeroes = 0;
284 if( eState >= SFX_ITEM_AVAILABLE)
286 const SfxStringItem* pItem = (const SfxStringItem*)pState;
287 OUString aCode = pItem->GetValue();
288 /* if(aCode.Equals(String::CreateFromAscii("General")))
290 mnCategorySelected = 0;
291 mpLbCategory->SelectEntryPos(0);
292 mpBtnThousand->Check(0);
293 mpBtnNegRed->Check(0);
294 mpEdDecimals->SetValue(0);
295 mpEdLeadZeroes->SetValue(1);
296 break;
298 else if( mpLbCategory->GetSelectEntryPos() == 0 )
300 mnCategorySelected = 1;
301 mpLbCategory->SelectEntryPos(1);
303 sal_uInt16 aLen = aCode.getLength();
304 OUString* sFormat = new OUString[4];
305 OUString sTmpStr = "";
306 sal_uInt16 nCount = 0;
307 sal_uInt16 nStrCount = 0;
308 while( nCount < aLen )
310 sal_Unicode cChar = aCode[nCount];
311 if(cChar == ',')
313 sFormat[nStrCount] = sTmpStr;
314 sTmpStr = "";
315 nStrCount++;
317 else
319 sTmpStr += OUString(cChar);
321 nCount++;
323 bThousand = sFormat[0].toInt32();
324 bNegRed = sFormat[1].toInt32();
325 nPrecision = (sal_uInt16)sFormat[2].toInt32();
326 nLeadZeroes = (sal_uInt16)sFormat[3].toInt32();
327 delete[] sFormat;
329 else
331 bThousand = 0;
332 bNegRed = 0;
333 nPrecision = 0;
334 nLeadZeroes = 1;
336 mpBtnThousand->Check(bThousand);
337 mpBtnNegRed->Check(bNegRed);
338 mpEdDecimals->SetValue(nPrecision);
339 mpEdLeadZeroes->SetValue(nLeadZeroes);
341 default:
346 //////////////////////////////////////////////////////////////////////////////
348 SfxBindings* NumberFormatPropertyPanel::GetBindings()
350 return mpBindings;
353 //////////////////////////////////////////////////////////////////////////////
354 // namespace close
356 }} // end of namespace ::sc::sidebar
358 //////////////////////////////////////////////////////////////////////////////
359 // eof
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */