xref: /trunk/main/sc/source/ui/sidebar/NumberFormatPropertyPanel.cxx (revision 4e8031e0a9bc16df402a00dc593d120d34289fa7)
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 
107     mpTBCategory->SetItemImage(ID_NUMBER, maImgNumber);
108     mpTBCategory->SetItemImage(ID_PERCENT, maImgPercent);
109     mpTBCategory->SetItemImage(ID_CURRENCY, maImgCurrency);
110     mpTBCategory->SetItemImage(ID_DATE, maImgDate);
111     mpTBCategory->SetItemImage(ID_TEXT, maImgText);
112     Size aTbxSize( mpTBCategory->CalcWindowSizePixel() );
113     mpTBCategory->SetOutputSizePixel( aTbxSize );
114     mpTBCategory->SetBackground(Wallpaper());
115     mpTBCategory->SetPaintTransparent(true);
116     aLink = LINK(this, NumberFormatPropertyPanel, NumFormatHdl);
117     mpTBCategory->SetSelectHdl ( aLink );
118 
119     aLink = LINK(this, NumberFormatPropertyPanel, NumFormatValueHdl);
120 
121     mpEdDecimals->SetModifyHdl( aLink );
122     mpEdLeadZeroes->SetModifyHdl( aLink );
123     mpEdDecimals->SetAccessibleName(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Decimal Places")));       //wj acc
124     mpEdLeadZeroes->SetAccessibleName(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Leading Zeroes")));     //wj acc
125     mpBtnNegRed->SetClickHdl( aLink );
126     mpBtnThousand->SetClickHdl( aLink );
127 
128     mpLbCategory->SetAccessibleRelationLabeledBy(mpFtCategory.get());
129     mpTBCategory->SetAccessibleRelationLabeledBy(mpTBCategory.get());
130     mpEdDecimals->SetAccessibleRelationLabeledBy(mpFtDecimals.get());
131     mpEdLeadZeroes->SetAccessibleRelationLabeledBy(mpFtLeadZeroes.get());
132 }
133 
134 //////////////////////////////////////////////////////////////////////////////
135 
136 IMPL_LINK( NumberFormatPropertyPanel, NumFormatHdl, ToolBox*, pBox )
137 {
138     sal_uInt16 nVal = pBox->GetCurItemId();
139     sal_uInt16 nId = 0;
140     switch(nVal)
141     {
142     case ID_NUMBER:
143         nId = 1;
144         break;
145     case ID_PERCENT:
146         nId = 2;
147         break;
148     case ID_CURRENCY:
149         nId = 3;
150         break;
151     case ID_DATE:
152         nId = 4;
153         break;
154     case ID_TEXT:
155         nId = 9;
156         break;
157     default:
158         ;
159     }
160     if( nId != mnCategorySelected )
161     {
162         SfxUInt16Item aItem( SID_NUMBER_TYPE_FORMAT,  nId );
163         GetBindings()->GetDispatcher()->Execute(SID_NUMBER_TYPE_FORMAT, SFX_CALLMODE_RECORD, &aItem, 0L);
164     }
165     return 0L;
166 }
167 
168 //////////////////////////////////////////////////////////////////////////////
169 
170 IMPL_LINK( NumberFormatPropertyPanel, NumFormatSelectHdl, ListBox*, pBox )
171 {
172     sal_uInt16 nVal = pBox->GetSelectEntryPos();
173     if( nVal != mnCategorySelected )
174     {
175         SfxUInt16Item aItem( SID_NUMBER_TYPE_FORMAT,  nVal );
176         GetBindings()->GetDispatcher()->Execute(SID_NUMBER_TYPE_FORMAT, SFX_CALLMODE_RECORD, &aItem, 0L);
177         mnCategorySelected = nVal;
178     }
179     return 0L;
180 }
181 
182 //////////////////////////////////////////////////////////////////////////////
183 
184 IMPL_LINK( NumberFormatPropertyPanel, NumFormatValueHdl, void*, EMPTYARG )
185 {
186     String        aFormat;
187     String        sBreak = String::CreateFromAscii(",");
188     bool          bThousand     =    mpBtnThousand->IsEnabled()
189         && mpBtnThousand->IsChecked();
190     bool          bNegRed       =    mpBtnNegRed->IsEnabled()
191         && mpBtnNegRed->IsChecked();
192     sal_uInt16        nPrecision    = (mpEdDecimals->IsEnabled())
193         ? (sal_uInt16)mpEdDecimals->GetValue()
194         : (sal_uInt16)0;
195     sal_uInt16        nLeadZeroes   = (mpEdLeadZeroes->IsEnabled())
196         ? (sal_uInt16)mpEdLeadZeroes->GetValue()
197         : (sal_uInt16)0;
198 
199     String sThousand = String::CreateFromInt32(bThousand);
200     String sNegRed = String::CreateFromInt32(bNegRed);
201     String sPrecision = String::CreateFromInt32(nPrecision);
202     String sLeadZeroes = String::CreateFromInt32(nLeadZeroes);
203 
204     aFormat += sThousand;
205     aFormat += sBreak;
206     aFormat += sNegRed;
207     aFormat += sBreak;
208     aFormat += sPrecision;
209     aFormat += sBreak;
210     aFormat += sLeadZeroes;
211     aFormat += sBreak;
212 
213     SfxStringItem aItem( SID_NUMBER_FORMAT,  aFormat );
214     GetBindings()->GetDispatcher()->Execute(SID_NUMBER_FORMAT, SFX_CALLMODE_RECORD, &aItem, 0L);
215     return 0L;
216 }
217 
218 //////////////////////////////////////////////////////////////////////////////
219 
220 NumberFormatPropertyPanel* NumberFormatPropertyPanel::Create (
221     Window* pParent,
222     const cssu::Reference<css::frame::XFrame>& rxFrame,
223     SfxBindings* pBindings)
224 {
225     if (pParent == NULL)
226         throw lang::IllegalArgumentException(A2S("no parent Window given to NumberFormatPropertyPanel::Create"), NULL, 0);
227     if ( ! rxFrame.is())
228         throw lang::IllegalArgumentException(A2S("no XFrame given to NumberFormatPropertyPanel::Create"), NULL, 1);
229     if (pBindings == NULL)
230         throw lang::IllegalArgumentException(A2S("no SfxBindings given to NumberFormatPropertyPanel::Create"), NULL, 2);
231 
232     return new NumberFormatPropertyPanel(
233         pParent,
234         rxFrame,
235         pBindings);
236 }
237 
238 //////////////////////////////////////////////////////////////////////////////
239 
240 void NumberFormatPropertyPanel::DataChanged(
241     const DataChangedEvent& rEvent)
242 {
243     (void)rEvent;
244 }
245 
246 //////////////////////////////////////////////////////////////////////////////
247 
248 void NumberFormatPropertyPanel::HandleContextChange(
249     const ::sfx2::sidebar::EnumContext aContext)
250 {
251     if(maContext == aContext)
252     {
253         // Nothing to do.
254         return;
255     }
256 
257     maContext = aContext;
258 
259 
260 
261     // todo
262 }
263 
264 //////////////////////////////////////////////////////////////////////////////
265 
266 void NumberFormatPropertyPanel::NotifyItemUpdate(
267     sal_uInt16 nSID,
268     SfxItemState eState,
269     const SfxPoolItem* pState)
270 {
271     switch(nSID)
272     {
273     case SID_NUMBER_TYPE_FORMAT:
274         {
275             if( eState >= SFX_ITEM_AVAILABLE)
276             {
277                 const SfxInt16Item* pItem = (const SfxInt16Item*)pState;
278                 sal_uInt16 nVal = pItem->GetValue();
279                 mnCategorySelected = nVal;
280                 mpLbCategory->SelectEntryPos(nVal);
281                 if( nVal < 4 )
282                 {
283                     mpBtnThousand->Enable();
284                     mpBtnNegRed->Enable();
285                     mpEdDecimals->Enable();
286                     mpEdLeadZeroes->Enable();
287                 }
288                 else
289                 {
290                     mpBtnThousand->Disable();
291                     mpBtnNegRed->Disable();
292                     mpEdDecimals->Disable();
293                     mpEdLeadZeroes->Disable();
294                 }
295             }
296             else
297             {
298                 mpLbCategory->SetNoSelection();
299                 mnCategorySelected = 0;
300                 mpBtnThousand->Disable();
301                 mpBtnNegRed->Disable();
302                 mpEdDecimals->Disable();
303                 mpEdLeadZeroes->Disable();
304             }
305         }
306         break;
307     case SID_NUMBER_FORMAT:
308         {
309             bool          bThousand     =    0;
310             bool          bNegRed       =    0;
311             sal_uInt16        nPrecision    =    0;
312             sal_uInt16        nLeadZeroes   =    0;
313             if( eState >= SFX_ITEM_AVAILABLE)
314             {
315                 const SfxStringItem* pItem = (const SfxStringItem*)pState;
316                 String aCode = pItem->GetValue();
317             /*  if(aCode.Equals(String::CreateFromAscii("General")))
318                 {
319                     mnCategorySelected = 0;
320                     mpLbCategory->SelectEntryPos(0);
321                     mpBtnThousand->Check(0);
322                     mpBtnNegRed->Check(0);
323                     mpEdDecimals->SetValue(0);
324                     mpEdLeadZeroes->SetValue(1);
325                     break;
326                 }
327                 else if( mpLbCategory->GetSelectEntryPos() == 0 )
328                 {
329                     mnCategorySelected = 1;
330                     mpLbCategory->SelectEntryPos(1);
331                 }*/
332                 sal_uInt16 aLen = aCode.Len();
333                 String* sFormat = new String[4];
334                 String  sTmpStr = String::CreateFromAscii("");
335                 sal_uInt16 nCount = 0;
336                 sal_uInt16 nStrCount = 0;
337                 while( nCount < aLen )
338                 {
339                     sal_Unicode cChar = aCode.GetChar(nCount);
340                     if(cChar == sal_Unicode(','))
341                     {
342                         sFormat[nStrCount] = sTmpStr;
343                         sTmpStr = String::CreateFromAscii("");
344                         nStrCount++;
345                     }
346                     else
347                     {
348                         sTmpStr += cChar;
349                     }
350                     nCount++;
351                 }
352                 bThousand   =    sFormat[0].ToInt32();
353                 bNegRed     =    sFormat[1].ToInt32();
354                 nPrecision  =    (sal_uInt16)sFormat[2].ToInt32();
355                 nLeadZeroes =    (sal_uInt16)sFormat[3].ToInt32();
356                 delete[] sFormat;
357             }
358             else
359             {
360                 bThousand   =    0;
361                 bNegRed     =    0;
362                 nPrecision  =    0;
363                 nLeadZeroes =    1;
364             }
365             mpBtnThousand->Check(bThousand);
366             mpBtnNegRed->Check(bNegRed);
367             mpEdDecimals->SetValue(nPrecision);
368             mpEdLeadZeroes->SetValue(nLeadZeroes);
369         }
370     default:
371         ;
372     }
373 }
374 
375 //////////////////////////////////////////////////////////////////////////////
376 
377 SfxBindings* NumberFormatPropertyPanel::GetBindings()
378 {
379     return mpBindings;
380 }
381 
382 //////////////////////////////////////////////////////////////////////////////
383 // namespace close
384 
385 }} // end of namespace ::sc::sidebar
386 
387 //////////////////////////////////////////////////////////////////////////////
388 // eof
389