bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / utlui / numfmtlb.cxx
blob32bf99a1ce399858d630568284b01ef7ea1686fc
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 <hintids.hxx>
21 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 #include <comphelper/processfactory.hxx>
23 #include <editeng/unolingu.hxx>
24 #include <unotools/localedatawrapper.hxx>
25 #include <i18nlangtag/lang.h>
26 #include <svl/zformat.hxx>
27 #include <svl/eitem.hxx>
28 #include <svx/svxids.hrc>
29 #include <svx/numinf.hxx>
30 #include <vcl/msgbox.hxx>
31 #include <svx/flagsdef.hxx>
32 #include <svl/itemset.hxx>
33 #include <docsh.hxx>
34 #include <swtypes.hxx>
35 #include <swmodule.hxx>
36 #include <view.hxx>
37 #include <wrtsh.hxx>
38 #include <numfmtlb.hxx>
39 #include <utlui.hrc>
40 #include "swabstdlg.hxx"
41 #include "dialog.hrc"
42 #include <unomid.h>
43 #include <sfx2/viewfrm.hxx>
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::lang;
49 // STATIC DATA
51 /**
52 * Description:
53 * nFormatType: Display the formats of this Type
54 * nDefFmt: Select this format and possibly insert it
57 NumFormatListBox::NumFormatListBox( Window* pWin, const ResId& rResId,
58 short nFormatType, sal_uLong nDefFmt,
59 sal_Bool bUsrFmts ) :
60 ListBox ( pWin, rResId ),
61 nCurrFormatType (-1),
62 nStdEntry (0),
63 bOneArea (sal_False),
64 nDefFormat (nDefFmt),
65 pVw (0),
66 pOwnFormatter (0),
67 bShowLanguageControl(sal_False),
68 bUseAutomaticLanguage(sal_True)
70 Init(nFormatType, bUsrFmts);
73 NumFormatListBox::NumFormatListBox( Window* pWin, SwView* pView,
74 const ResId& rResId, short nFormatType,
75 sal_uLong nDefFmt, sal_Bool bUsrFmts ) :
76 ListBox ( pWin, rResId ),
77 nCurrFormatType (-1),
78 nStdEntry (0),
79 bOneArea (sal_False),
80 nDefFormat (nDefFmt),
81 pVw (pView),
82 pOwnFormatter (0),
83 bShowLanguageControl(sal_False),
84 bUseAutomaticLanguage(sal_True)
86 Init(nFormatType, bUsrFmts);
89 void NumFormatListBox::Init(short nFormatType, sal_Bool bUsrFmts)
91 SwView *pView = GetView();
93 if (pView)
94 eCurLanguage = pView->GetWrtShell().GetCurLang();
95 else
96 eCurLanguage = SvtSysLocale().GetLanguageTag().getLanguageType();
98 if (bUsrFmts == sal_False)
100 pOwnFormatter = new SvNumberFormatter(comphelper::getProcessComponentContext(), eCurLanguage);
103 SetFormatType(nFormatType);
104 SetDefFormat(nDefFormat);
106 SetSelectHdl(LINK(this, NumFormatListBox, SelectHdl));
109 NumFormatListBox::~NumFormatListBox()
111 delete pOwnFormatter;
114 SwView* NumFormatListBox::GetView()
116 if( pVw )
117 return pVw;
118 return ::GetActiveView();
121 void NumFormatListBox::SetFormatType(const short nFormatType)
123 if (nCurrFormatType == -1 ||
124 (nCurrFormatType & nFormatType) == 0) // there are mixed formats, like for example DateTime
126 SvNumberFormatter* pFormatter;
128 if( pOwnFormatter )
129 pFormatter = pOwnFormatter;
130 else
132 SwView *pView = GetView();
133 OSL_ENSURE(pView, "no view found");
134 if(!pView)
135 return;
136 SwWrtShell &rSh = pView->GetWrtShell();
137 pFormatter = rSh.GetNumberFormatter();
140 Clear(); // Remove all entries from the Listbox
142 NfIndexTableOffset eOffsetStart = NF_NUMBER_START;
143 NfIndexTableOffset eOffsetEnd = NF_NUMBER_START;
145 switch( nFormatType )
147 case NUMBERFORMAT_NUMBER:
148 eOffsetStart=NF_NUMBER_START;
149 eOffsetEnd=NF_NUMBER_END;
150 break;
152 case NUMBERFORMAT_PERCENT:
153 eOffsetStart=NF_PERCENT_START;
154 eOffsetEnd=NF_PERCENT_END;
155 break;
157 case NUMBERFORMAT_CURRENCY:
158 eOffsetStart=NF_CURRENCY_START;
159 eOffsetEnd=NF_CURRENCY_END;
160 break;
162 case NUMBERFORMAT_DATETIME:
163 eOffsetStart=NF_DATE_START;
164 eOffsetEnd=NF_TIME_END;
165 break;
167 case NUMBERFORMAT_DATE:
168 eOffsetStart=NF_DATE_START;
169 eOffsetEnd=NF_DATE_END;
170 break;
172 case NUMBERFORMAT_TIME:
173 eOffsetStart=NF_TIME_START;
174 eOffsetEnd=NF_TIME_END;
175 break;
177 case NUMBERFORMAT_SCIENTIFIC:
178 eOffsetStart=NF_SCIENTIFIC_START;
179 eOffsetEnd=NF_SCIENTIFIC_END;
180 break;
182 case NUMBERFORMAT_FRACTION:
183 eOffsetStart=NF_FRACTION_START;
184 eOffsetEnd=NF_FRACTION_END;
185 break;
187 case NUMBERFORMAT_LOGICAL:
188 eOffsetStart=NF_BOOLEAN;
189 eOffsetEnd=NF_BOOLEAN;
190 break;
192 case NUMBERFORMAT_TEXT:
193 eOffsetStart=NF_TEXT;
194 eOffsetEnd=NF_TEXT;
195 break;
197 case NUMBERFORMAT_ALL:
198 eOffsetStart=NF_NUMERIC_START;
199 eOffsetEnd = NfIndexTableOffset( NF_INDEX_TABLE_ENTRIES - 1 );
200 break;
202 default:
203 OSL_FAIL("what a format?");
204 break;
207 const SvNumberformat* pFmt;
208 sal_uInt16 nPos, i = 0;
209 sal_uLong nFormat;
210 Color* pCol;
211 double fVal = GetDefValue( nFormatType );
212 OUString sValue;
214 sal_uLong nSysNumFmt = pFormatter->GetFormatIndex(
215 NF_NUMBER_SYSTEM, eCurLanguage );
216 sal_uLong nSysShortDateFmt = pFormatter->GetFormatIndex(
217 NF_DATE_SYSTEM_SHORT, eCurLanguage );
218 sal_uLong nSysLongDateFmt = pFormatter->GetFormatIndex(
219 NF_DATE_SYSTEM_LONG, eCurLanguage );
221 for( long nIndex = eOffsetStart; nIndex <= eOffsetEnd; ++nIndex )
223 nFormat = pFormatter->GetFormatIndex(
224 (NfIndexTableOffset)nIndex, eCurLanguage );
225 pFmt = pFormatter->GetEntry( nFormat );
227 if( nFormat == pFormatter->GetFormatIndex( NF_NUMBER_STANDARD,
228 eCurLanguage )
229 || ((SvNumberformat*)pFmt)->GetOutputString( fVal, sValue, &pCol )
230 || nFormatType == NUMBERFORMAT_UNDEFINED )
232 sValue = pFmt->GetFormatstring();
234 else if( nFormatType == NUMBERFORMAT_TEXT )
236 OUString sTxt("\"ABC\"");
237 pFormatter->GetOutputString( sTxt, nFormat, sValue, &pCol);
240 if (nFormat != nSysNumFmt &&
241 nFormat != nSysShortDateFmt &&
242 nFormat != nSysLongDateFmt)
244 nPos = InsertEntry( sValue );
245 SetEntryData( nPos, (void*)nFormat );
247 if( nFormat == pFormatter->GetStandardFormat(
248 nFormatType, eCurLanguage ) )
249 nStdEntry = i;
250 ++i;
254 if (!pOwnFormatter)
256 nPos = InsertEntry(SW_RESSTR( STR_DEFINE_NUMBERFORMAT ));
257 SetEntryData( nPos, NULL );
260 SelectEntryPos( nStdEntry );
262 nCurrFormatType = nFormatType;
266 void NumFormatListBox::SetDefFormat(const sal_uLong nDefFmt)
268 if (nDefFmt == ULONG_MAX)
270 nDefFormat = nDefFmt;
271 return;
274 SvNumberFormatter* pFormatter;
275 if (pOwnFormatter)
276 pFormatter = pOwnFormatter;
277 else
279 SwView *pView = GetView();
280 OSL_ENSURE(pView, "no view found");
281 if(!pView)
282 return;
283 SwWrtShell &rSh = pView->GetWrtShell();
284 pFormatter = rSh.GetNumberFormatter();
287 short nType = pFormatter->GetType(nDefFmt);
289 SetFormatType(nType);
291 sal_uLong nFormat = pFormatter->GetFormatForLanguageIfBuiltIn(nDefFmt, eCurLanguage);
293 for (sal_uInt16 i = 0; i < GetEntryCount(); i++)
295 if (nFormat == (sal_uLong)GetEntryData(i))
297 SelectEntryPos(i);
298 nStdEntry = i;
299 nDefFormat = GetFormat();
300 return;
304 // No entry found:
305 double fValue = GetDefValue(nType);
306 String sValue;
307 Color* pCol = 0;
309 if (nType == NUMBERFORMAT_TEXT)
311 OUString sTxt("\"ABC\"");
312 OUString sTempOut(sValue);
313 pFormatter->GetOutputString(sTxt, nDefFmt, sTempOut, &pCol);
314 sValue = sTempOut;
316 else
317 pFormatter->GetOutputString(fValue, nDefFmt, sValue, &pCol);
319 sal_uInt16 nPos = 0;
320 while ((sal_uLong)GetEntryData(nPos) == ULONG_MAX)
321 nPos++;
323 sal_uLong nSysNumFmt = pFormatter->GetFormatIndex( NF_NUMBER_SYSTEM, eCurLanguage);
324 sal_uLong nSysShortDateFmt = pFormatter->GetFormatIndex( NF_DATE_SYSTEM_SHORT, eCurLanguage);
325 sal_uLong nSysLongDateFmt = pFormatter->GetFormatIndex( NF_DATE_SYSTEM_LONG, eCurLanguage);
326 bool bSysLang = false;
327 if( eCurLanguage == GetAppLanguage() )
328 bSysLang = true;
329 sal_uLong nNumFormatForLanguage = pFormatter->GetFormatForLanguageIfBuiltIn(nSysNumFmt, LANGUAGE_SYSTEM );
330 sal_uLong nShortDateFormatForLanguage = pFormatter->GetFormatForLanguageIfBuiltIn(nSysShortDateFmt, LANGUAGE_SYSTEM );
331 sal_uLong nLongDateFormatForLanguage = pFormatter->GetFormatForLanguageIfBuiltIn(nSysLongDateFmt, LANGUAGE_SYSTEM );
333 if (
334 nDefFmt == nSysNumFmt ||
335 nDefFmt == nSysShortDateFmt ||
336 nDefFmt == nSysLongDateFmt ||
338 bSysLang &&
340 nDefFmt == nNumFormatForLanguage ||
341 nDefFmt == nShortDateFormatForLanguage ||
342 nDefFmt == nLongDateFormatForLanguage
347 sValue += String(SW_RES(RID_STR_SYSTEM));
350 nPos = InsertEntry(sValue, nPos); // Insert as first numeric entry
351 SetEntryData(nPos, (void*)nDefFmt);
352 SelectEntryPos(nPos);
353 nDefFormat = GetFormat();
356 sal_uLong NumFormatListBox::GetFormat() const
358 sal_uInt16 nPos = GetSelectEntryPos();
360 return (sal_uLong)GetEntryData(nPos);
363 IMPL_LINK( NumFormatListBox, SelectHdl, ListBox *, pBox )
365 sal_uInt16 nPos = pBox->GetSelectEntryPos();
366 String sDefine(SW_RES( STR_DEFINE_NUMBERFORMAT ));
367 SwView *pView = GetView();
369 if( pView && nPos == pBox->GetEntryCount() - 1 &&
370 pBox->GetEntry( nPos ) == sDefine )
372 SwWrtShell &rSh = pView->GetWrtShell();
373 SvNumberFormatter* pFormatter = rSh.GetNumberFormatter();
375 SfxItemSet aCoreSet( rSh.GetAttrPool(),
376 SID_ATTR_NUMBERFORMAT_VALUE, SID_ATTR_NUMBERFORMAT_VALUE,
377 SID_ATTR_NUMBERFORMAT_INFO, SID_ATTR_NUMBERFORMAT_INFO,
378 SID_ATTR_NUMBERFORMAT_ONE_AREA, SID_ATTR_NUMBERFORMAT_ONE_AREA,
379 SID_ATTR_NUMBERFORMAT_NOLANGUAGE, SID_ATTR_NUMBERFORMAT_NOLANGUAGE,
380 SID_ATTR_NUMBERFORMAT_ADD_AUTO, SID_ATTR_NUMBERFORMAT_ADD_AUTO,
381 0 );
383 double fValue = GetDefValue( nCurrFormatType);
385 sal_uLong nFormat = pFormatter->GetStandardFormat( nCurrFormatType, eCurLanguage);
386 aCoreSet.Put( SfxUInt32Item( SID_ATTR_NUMBERFORMAT_VALUE, nFormat ));
388 aCoreSet.Put( SvxNumberInfoItem( pFormatter, fValue,
389 SID_ATTR_NUMBERFORMAT_INFO ) );
391 if( (NUMBERFORMAT_DATE | NUMBERFORMAT_TIME) & nCurrFormatType )
392 aCoreSet.Put(SfxBoolItem(SID_ATTR_NUMBERFORMAT_ONE_AREA, bOneArea));
394 aCoreSet.Put(SfxBoolItem(SID_ATTR_NUMBERFORMAT_NOLANGUAGE, !bShowLanguageControl));
395 aCoreSet.Put(SfxBoolItem(SID_ATTR_NUMBERFORMAT_ADD_AUTO, bUseAutomaticLanguage));
397 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
398 OSL_ENSURE(pFact, "SwAbstractDialogFactory fail!");
400 SfxAbstractDialog* pDlg = pFact->CreateSfxDialog( this, aCoreSet,
401 GetView()->GetViewFrame()->GetFrame().GetFrameInterface(),
402 RC_DLG_SWNUMFMTDLG );
403 OSL_ENSURE(pDlg, "Dialogdiet fail!");
405 if (RET_OK == pDlg->Execute())
407 const SfxPoolItem* pItem = pView->GetDocShell()->
408 GetItem( SID_ATTR_NUMBERFORMAT_INFO );
410 if( pItem && 0 != ((SvxNumberInfoItem*)pItem)->GetDelCount() )
412 const sal_uInt32* pDelArr = ((SvxNumberInfoItem*)pItem)->GetDelArray();
414 for ( sal_uInt16 i = 0; i < ((SvxNumberInfoItem*)pItem)->GetDelCount(); i++ )
415 pFormatter->DeleteEntry( pDelArr[i] );
418 const SfxItemSet* pOutSet = pDlg->GetOutputItemSet();
419 if( SFX_ITEM_SET == pOutSet->GetItemState(
420 SID_ATTR_NUMBERFORMAT_VALUE, sal_False, &pItem ))
422 sal_uInt32 nNumberFormat = ((SfxUInt32Item*)pItem)->GetValue();
423 // oj #105473# change order of calls
424 const SvNumberformat* pFmt = pFormatter->GetEntry(nNumberFormat);
425 if( pFmt )
426 eCurLanguage = pFmt->GetLanguage();
427 // SetDefFormat uses eCurLanguage to look for if this format already in the list
428 SetDefFormat(nNumberFormat);
430 if( bShowLanguageControl && SFX_ITEM_SET == pOutSet->GetItemState(
431 SID_ATTR_NUMBERFORMAT_ADD_AUTO, sal_False, &pItem ))
433 bUseAutomaticLanguage = ((const SfxBoolItem*)pItem)->GetValue();
436 else
437 SetDefFormat(nFormat);
439 delete pDlg;
441 return 0;
444 double NumFormatListBox::GetDefValue(const short nFormatType) const
446 double fDefValue = 0.0;
448 switch (nFormatType)
450 case NUMBERFORMAT_DATE:
451 case NUMBERFORMAT_DATE|NUMBERFORMAT_TIME:
452 fDefValue = SVX_NUMVAL_DATE;
453 break;
455 case NUMBERFORMAT_TIME:
456 fDefValue = SVX_NUMVAL_TIME;
457 break;
459 case NUMBERFORMAT_TEXT:
460 case NUMBERFORMAT_UNDEFINED:
461 fDefValue = 0;
462 break;
464 case NUMBERFORMAT_CURRENCY:
465 fDefValue = SVX_NUMVAL_CURRENCY;
466 break;
468 case NUMBERFORMAT_PERCENT:
469 fDefValue = SVX_NUMVAL_PERCENT;
470 break;
472 case NUMBERFORMAT_LOGICAL:
473 fDefValue = SVX_NUMVAL_BOOLEAN;
474 break;
476 default:
477 fDefValue = SVX_NUMVAL_STANDARD;
478 break;
481 return fDefValue;
484 void NumFormatListBox::Clear()
486 ListBox::Clear();
487 nCurrFormatType = -1;
490 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */