xref: /trunk/main/sc/source/ui/sidebar/NumberFormatPropertyPanel.cxx (revision ca62e2c2083b5d0995f1245bad6c2edfb455fbec)
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     const bool bIsEnabled)
272 {
273     (void)bIsEnabled;
274 
275     switch(nSID)
276     {
277     case SID_NUMBER_TYPE_FORMAT:
278         {
279             if( eState >= SFX_ITEM_AVAILABLE)
280             {
281                 const SfxInt16Item* pItem = (const SfxInt16Item*)pState;
282                 sal_uInt16 nVal = pItem->GetValue();
283                 mnCategorySelected = nVal;
284                 mpLbCategory->SelectEntryPos(nVal);
285                 if( nVal < 4 )
286                 {
287                     mpBtnThousand->Enable();
288                     mpBtnNegRed->Enable();
289                     mpEdDecimals->Enable();
290                     mpEdLeadZeroes->Enable();
291                 }
292                 else
293                 {
294                     mpBtnThousand->Disable();
295                     mpBtnNegRed->Disable();
296                     mpEdDecimals->Disable();
297                     mpEdLeadZeroes->Disable();
298                 }
299             }
300             else
301             {
302                 mpLbCategory->SetNoSelection();
303                 mnCategorySelected = 0;
304                 mpBtnThousand->Disable();
305                 mpBtnNegRed->Disable();
306                 mpEdDecimals->Disable();
307                 mpEdLeadZeroes->Disable();
308             }
309         }
310         break;
311     case SID_NUMBER_FORMAT:
312         {
313             bool          bThousand     =    0;
314             bool          bNegRed       =    0;
315             sal_uInt16        nPrecision    =    0;
316             sal_uInt16        nLeadZeroes   =    0;
317             if( eState >= SFX_ITEM_AVAILABLE)
318             {
319                 const SfxStringItem* pItem = (const SfxStringItem*)pState;
320                 String aCode = pItem->GetValue();
321             /*  if(aCode.Equals(String::CreateFromAscii("General")))
322                 {
323                     mnCategorySelected = 0;
324                     mpLbCategory->SelectEntryPos(0);
325                     mpBtnThousand->Check(0);
326                     mpBtnNegRed->Check(0);
327                     mpEdDecimals->SetValue(0);
328                     mpEdLeadZeroes->SetValue(1);
329                     break;
330                 }
331                 else if( mpLbCategory->GetSelectEntryPos() == 0 )
332                 {
333                     mnCategorySelected = 1;
334                     mpLbCategory->SelectEntryPos(1);
335                 }*/
336                 sal_uInt16 aLen = aCode.Len();
337                 String* sFormat = new String[4];
338                 String  sTmpStr = String::CreateFromAscii("");
339                 sal_uInt16 nCount = 0;
340                 sal_uInt16 nStrCount = 0;
341                 while( nCount < aLen )
342                 {
343                     sal_Unicode cChar = aCode.GetChar(nCount);
344                     if(cChar == sal_Unicode(','))
345                     {
346                         sFormat[nStrCount] = sTmpStr;
347                         sTmpStr = String::CreateFromAscii("");
348                         nStrCount++;
349                     }
350                     else
351                     {
352                         sTmpStr += cChar;
353                     }
354                     nCount++;
355                 }
356                 bThousand   =    sFormat[0].ToInt32();
357                 bNegRed     =    sFormat[1].ToInt32();
358                 nPrecision  =    (sal_uInt16)sFormat[2].ToInt32();
359                 nLeadZeroes =    (sal_uInt16)sFormat[3].ToInt32();
360                 delete[] sFormat;
361             }
362             else
363             {
364                 bThousand   =    0;
365                 bNegRed     =    0;
366                 nPrecision  =    0;
367                 nLeadZeroes =    1;
368             }
369             mpBtnThousand->Check(bThousand);
370             mpBtnNegRed->Check(bNegRed);
371             mpEdDecimals->SetValue(nPrecision);
372             mpEdLeadZeroes->SetValue(nLeadZeroes);
373         }
374     default:
375         ;
376     }
377 }
378 
379 //////////////////////////////////////////////////////////////////////////////
380 
381 SfxBindings* NumberFormatPropertyPanel::GetBindings()
382 {
383     return mpBindings;
384 }
385 
386 //////////////////////////////////////////////////////////////////////////////
387 // namespace close
388 
389 }} // end of namespace ::sc::sidebar
390 
391 //////////////////////////////////////////////////////////////////////////////
392 // eof
393