merge the formfield patch from ooo-build
[ooovba.git] / sw / source / ui / utlui / numfmtlb.cxx
blobd6ea01d58b3f959330584df66810bbc829d53efb
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: numfmtlb.cxx,v $
10 * $Revision: 1.23 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
35 #include <hintids.hxx>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <comphelper/processfactory.hxx>
38 #include <svx/unolingu.hxx>
39 #include <unotools/localedatawrapper.hxx>
40 #include <i18npool/lang.h>
41 #ifndef _ZFORMAT_HXX //autogen
42 #define _ZFORLIST_DECLARE_TABLE
43 #include <svtools/zformat.hxx>
44 #endif
45 #include <svtools/eitem.hxx>
46 #include <svx/svxids.hrc>
47 #include <svx/numinf.hxx>
48 #include <vcl/msgbox.hxx>
49 #include <svx/flagsdef.hxx>
50 #include <svtools/itemset.hxx>
51 #include <docsh.hxx>
52 #include <swtypes.hxx>
53 #include <swmodule.hxx>
54 #include <view.hxx>
55 #include <wrtsh.hxx>
56 #include <numfmtlb.hxx>
57 #include <utlui.hrc>
58 #include "swabstdlg.hxx"
59 #include "dialog.hrc"
60 #include <unomid.h>
61 #include <sfx2/viewfrm.hxx>
63 using namespace ::com::sun::star::uno;
64 using namespace ::com::sun::star::lang;
67 // STATIC DATA -----------------------------------------------------------
69 /*--------------------------------------------------------------------
70 Beschreibung:
71 nFormatType: Formate dieses Typs anzeigen
72 nDefFmt: Dieses Format selektieren und ggf vorher
73 einfuegen
74 --------------------------------------------------------------------*/
76 NumFormatListBox::NumFormatListBox( Window* pWin, const ResId& rResId,
77 short nFormatType, ULONG nDefFmt,
78 BOOL bUsrFmts ) :
79 ListBox ( pWin, rResId ),
80 nCurrFormatType (-1),
81 nStdEntry (0),
82 bOneArea (FALSE),
83 nDefFormat (nDefFmt),
84 pVw (0),
85 pOwnFormatter (0),
86 bShowLanguageControl(FALSE),
87 bUseAutomaticLanguage(TRUE)
89 Init(nFormatType, bUsrFmts);
92 /*--------------------------------------------------------------------
93 Beschreibung:
94 --------------------------------------------------------------------*/
96 NumFormatListBox::NumFormatListBox( Window* pWin, SwView* pView,
97 const ResId& rResId, short nFormatType,
98 ULONG nDefFmt, BOOL bUsrFmts ) :
99 ListBox ( pWin, rResId ),
100 nCurrFormatType (-1),
101 nStdEntry (0),
102 bOneArea (FALSE),
103 nDefFormat (nDefFmt),
104 pVw (pView),
105 pOwnFormatter (0),
106 bShowLanguageControl(FALSE),
107 bUseAutomaticLanguage(TRUE)
109 Init(nFormatType, bUsrFmts);
112 /* -----------------15.06.98 11:29-------------------
114 * --------------------------------------------------*/
116 void NumFormatListBox::Init(short nFormatType, BOOL bUsrFmts)
118 SwView *pView = GetView();
120 if (pView)
121 eCurLanguage = pView->GetWrtShell().GetCurLang();
122 else
123 eCurLanguage = SvxLocaleToLanguage( SvtSysLocale().GetLocaleData().getLocale() );
125 if (bUsrFmts == FALSE)
127 Reference< XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
128 pOwnFormatter = new SvNumberFormatter(xMSF, eCurLanguage);
131 SetFormatType(nFormatType);
132 SetDefFormat(nDefFormat);
134 SetSelectHdl(LINK(this, NumFormatListBox, SelectHdl));
137 /*--------------------------------------------------------------------
138 Beschreibung:
139 --------------------------------------------------------------------*/
141 NumFormatListBox::~NumFormatListBox()
143 if (pOwnFormatter)
144 delete pOwnFormatter;
146 /*--------------------------------------------------------------------
147 Beschreibung:
148 --------------------------------------------------------------------*/
150 SwView* NumFormatListBox::GetView()
152 if( pVw )
153 return pVw;
154 return ::GetActiveView();
157 /*--------------------------------------------------------------------
158 Beschreibung:
159 --------------------------------------------------------------------*/
161 void NumFormatListBox::SetFormatType(const short nFormatType)
163 if (nCurrFormatType == -1 ||
164 (nCurrFormatType & nFormatType) == 0) // Es gibt Mischformate, wie z.B. DateTime
166 SvNumberFormatter* pFormatter;
168 if( pOwnFormatter )
169 pFormatter = pOwnFormatter;
170 else
172 SwView *pView = GetView();
173 DBG_ASSERT(pView, "no view found");
174 if(!pView)
175 return;
176 SwWrtShell &rSh = pView->GetWrtShell();
177 pFormatter = rSh.GetNumberFormatter();
180 Clear(); // Alle Eintraege in der Listbox entfernen
182 NfIndexTableOffset eOffsetStart = NF_NUMBER_START;
183 NfIndexTableOffset eOffsetEnd = NF_NUMBER_START;
185 switch( nFormatType )
187 case NUMBERFORMAT_NUMBER:
188 eOffsetStart=NF_NUMBER_START;
189 eOffsetEnd=NF_NUMBER_END;
190 break;
192 case NUMBERFORMAT_PERCENT:
193 eOffsetStart=NF_PERCENT_START;
194 eOffsetEnd=NF_PERCENT_END;
195 break;
197 case NUMBERFORMAT_CURRENCY:
198 eOffsetStart=NF_CURRENCY_START;
199 eOffsetEnd=NF_CURRENCY_END;
200 break;
202 case NUMBERFORMAT_DATETIME:
203 eOffsetStart=NF_DATE_START;
204 eOffsetEnd=NF_TIME_END;
205 break;
207 case NUMBERFORMAT_DATE:
208 eOffsetStart=NF_DATE_START;
209 eOffsetEnd=NF_DATE_END;
210 break;
212 case NUMBERFORMAT_TIME:
213 eOffsetStart=NF_TIME_START;
214 eOffsetEnd=NF_TIME_END;
215 break;
217 case NUMBERFORMAT_SCIENTIFIC:
218 eOffsetStart=NF_SCIENTIFIC_START;
219 eOffsetEnd=NF_SCIENTIFIC_END;
220 break;
222 case NUMBERFORMAT_FRACTION:
223 eOffsetStart=NF_FRACTION_START;
224 eOffsetEnd=NF_FRACTION_END;
225 break;
227 case NUMBERFORMAT_LOGICAL:
228 eOffsetStart=NF_BOOLEAN;
229 eOffsetEnd=NF_BOOLEAN;
230 break;
232 case NUMBERFORMAT_TEXT:
233 eOffsetStart=NF_TEXT;
234 eOffsetEnd=NF_TEXT;
235 break;
237 case NUMBERFORMAT_ALL:
238 eOffsetStart=NF_NUMERIC_START;
239 eOffsetEnd = NfIndexTableOffset( NF_INDEX_TABLE_ENTRIES - 1 );
240 break;
242 default:
243 DBG_ERROR("what a format?");
244 break;
247 const SvNumberformat* pFmt;
248 USHORT nPos, i = 0;
249 ULONG nFormat;
250 Color* pCol;
251 double fVal = GetDefValue( nFormatType );
252 String sValue;
254 ULONG nSysNumFmt = pFormatter->GetFormatIndex(
255 NF_NUMBER_SYSTEM, eCurLanguage );
256 ULONG nSysShortDateFmt = pFormatter->GetFormatIndex(
257 NF_DATE_SYSTEM_SHORT, eCurLanguage );
258 ULONG nSysLongDateFmt = pFormatter->GetFormatIndex(
259 NF_DATE_SYSTEM_LONG, eCurLanguage );
261 for( long nIndex = eOffsetStart; nIndex <= eOffsetEnd; ++nIndex )
263 nFormat = pFormatter->GetFormatIndex(
264 (NfIndexTableOffset)nIndex, eCurLanguage );
265 pFmt = pFormatter->GetEntry( nFormat );
267 if( nFormat == pFormatter->GetFormatIndex( NF_NUMBER_STANDARD,
268 eCurLanguage )
269 || ((SvNumberformat*)pFmt)->GetOutputString( fVal, sValue, &pCol )
270 || nFormatType == NUMBERFORMAT_UNDEFINED )
271 sValue = pFmt->GetFormatstring();
272 else if( nFormatType == NUMBERFORMAT_TEXT )
274 String sTxt(C2S("\"ABC\""));
275 pFormatter->GetOutputString( sTxt, nFormat, sValue, &pCol);
278 if (nFormat != nSysNumFmt &&
279 nFormat != nSysShortDateFmt &&
280 nFormat != nSysLongDateFmt)
282 nPos = InsertEntry( sValue );
283 SetEntryData( nPos, (void*)nFormat );
285 if( nFormat == pFormatter->GetStandardFormat(
286 nFormatType, eCurLanguage ) )
287 nStdEntry = i;
288 ++i;
292 if (!pOwnFormatter)
294 nPos = InsertEntry(SW_RESSTR( STR_DEFINE_NUMBERFORMAT ));
295 SetEntryData( nPos, NULL );
298 SelectEntryPos( nStdEntry );
300 nCurrFormatType = nFormatType;
304 /*--------------------------------------------------------------------
305 Beschreibung:
306 --------------------------------------------------------------------*/
308 void NumFormatListBox::SetDefFormat(const ULONG nDefFmt)
310 if (nDefFmt == ULONG_MAX)
312 nDefFormat = nDefFmt;
313 return;
316 SvNumberFormatter* pFormatter;
317 if (pOwnFormatter)
318 pFormatter = pOwnFormatter;
319 else
321 SwView *pView = GetView();
322 DBG_ASSERT(pView, "no view found");
323 if(!pView)
324 return;
325 SwWrtShell &rSh = pView->GetWrtShell();
326 pFormatter = rSh.GetNumberFormatter();
329 short nType = pFormatter->GetType(nDefFmt);
331 SetFormatType(nType);
333 ULONG nFormat = pFormatter->GetFormatForLanguageIfBuiltIn(nDefFmt, eCurLanguage);
335 for (USHORT i = 0; i < GetEntryCount(); i++)
337 if (nFormat == (ULONG)GetEntryData(i))
339 SelectEntryPos(i);
340 nStdEntry = i;
341 nDefFormat = GetFormat();
342 return;
346 // Kein Eintrag gefunden:
347 double fValue = GetDefValue(nType);
348 String sValue;
349 Color* pCol = 0;
351 if (nType == NUMBERFORMAT_TEXT)
353 String sTxt(C2S("\"ABC\""));
354 pFormatter->GetOutputString(sTxt, nDefFmt, sValue, &pCol);
356 else
357 pFormatter->GetOutputString(fValue, nDefFmt, sValue, &pCol);
359 USHORT nPos = 0;
360 while ((ULONG)GetEntryData(nPos) == ULONG_MAX)
361 nPos++;
364 ULONG nSysNumFmt = pFormatter->GetFormatIndex( NF_NUMBER_SYSTEM, eCurLanguage);
365 ULONG nSysShortDateFmt = pFormatter->GetFormatIndex( NF_DATE_SYSTEM_SHORT, eCurLanguage);
366 ULONG nSysLongDateFmt = pFormatter->GetFormatIndex( NF_DATE_SYSTEM_LONG, eCurLanguage);
367 BOOL bSysLang = FALSE;
368 if( eCurLanguage == GetAppLanguage() )
369 bSysLang = TRUE;
370 ULONG nNumFormatForLanguage = pFormatter->GetFormatForLanguageIfBuiltIn(nSysNumFmt, LANGUAGE_SYSTEM );
371 ULONG nShortDateFormatForLanguage = pFormatter->GetFormatForLanguageIfBuiltIn(nSysShortDateFmt, LANGUAGE_SYSTEM );
372 ULONG nLongDateFormatForLanguage = pFormatter->GetFormatForLanguageIfBuiltIn(nSysLongDateFmt, LANGUAGE_SYSTEM );
374 if(nDefFmt == nSysNumFmt||
375 nDefFmt == nSysShortDateFmt||
376 nDefFmt == nSysLongDateFmt||
377 bSysLang && (nDefFmt == nNumFormatForLanguage ||
378 nDefFmt == nShortDateFormatForLanguage ||
379 nDefFmt == nLongDateFormatForLanguage ))
380 sValue += String(SW_RES(RID_STR_SYSTEM));
382 nPos = InsertEntry(sValue, nPos); // Als ersten numerischen Eintrag einfuegen
383 SetEntryData(nPos, (void*)nDefFmt);
384 SelectEntryPos(nPos);
385 nDefFormat = GetFormat();
388 /*--------------------------------------------------------------------
389 Beschreibung:
390 --------------------------------------------------------------------*/
392 ULONG NumFormatListBox::GetFormat() const
394 USHORT nPos = GetSelectEntryPos();
396 return (ULONG)GetEntryData(nPos);
399 /*--------------------------------------------------------------------
400 Beschreibung:
401 --------------------------------------------------------------------*/
403 IMPL_LINK( NumFormatListBox, SelectHdl, ListBox *, pBox )
405 USHORT nPos = pBox->GetSelectEntryPos();
406 String sDefine(SW_RES( STR_DEFINE_NUMBERFORMAT ));
407 SwView *pView = GetView();
409 if( pView && nPos == pBox->GetEntryCount() - 1 &&
410 pBox->GetEntry( nPos ) == sDefine )
412 SwWrtShell &rSh = pView->GetWrtShell();
413 SvNumberFormatter* pFormatter = rSh.GetNumberFormatter();
415 SfxItemSet aCoreSet( rSh.GetAttrPool(),
416 SID_ATTR_NUMBERFORMAT_VALUE, SID_ATTR_NUMBERFORMAT_VALUE,
417 SID_ATTR_NUMBERFORMAT_INFO, SID_ATTR_NUMBERFORMAT_INFO,
418 SID_ATTR_NUMBERFORMAT_ONE_AREA, SID_ATTR_NUMBERFORMAT_ONE_AREA,
419 SID_ATTR_NUMBERFORMAT_NOLANGUAGE, SID_ATTR_NUMBERFORMAT_NOLANGUAGE,
420 SID_ATTR_NUMBERFORMAT_ADD_AUTO, SID_ATTR_NUMBERFORMAT_ADD_AUTO,
421 0 );
423 double fValue = GetDefValue( nCurrFormatType);
425 ULONG nFormat = pFormatter->GetStandardFormat( nCurrFormatType, eCurLanguage);
426 aCoreSet.Put( SfxUInt32Item( SID_ATTR_NUMBERFORMAT_VALUE, nFormat ));
428 aCoreSet.Put( SvxNumberInfoItem( pFormatter, fValue,
429 SID_ATTR_NUMBERFORMAT_INFO ) );
431 if( (NUMBERFORMAT_DATE | NUMBERFORMAT_TIME) & nCurrFormatType )
432 aCoreSet.Put(SfxBoolItem(SID_ATTR_NUMBERFORMAT_ONE_AREA, bOneArea));
434 aCoreSet.Put(SfxBoolItem(SID_ATTR_NUMBERFORMAT_NOLANGUAGE, !bShowLanguageControl));
435 aCoreSet.Put(SfxBoolItem(SID_ATTR_NUMBERFORMAT_ADD_AUTO, bUseAutomaticLanguage));
437 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
438 DBG_ASSERT(pFact, "SwAbstractDialogFactory fail!");
440 SfxAbstractDialog* pDlg = pFact->CreateSfxDialog( this, aCoreSet,
441 GetView()->GetViewFrame()->GetFrame()->GetFrameInterface(),
442 RC_DLG_SWNUMFMTDLG );
443 DBG_ASSERT(pDlg, "Dialogdiet fail!");
445 if (RET_OK == pDlg->Execute())
447 const SfxPoolItem* pItem = pView->GetDocShell()->
448 GetItem( SID_ATTR_NUMBERFORMAT_INFO );
450 if( pItem && 0 != ((SvxNumberInfoItem*)pItem)->GetDelCount() )
452 const sal_uInt32* pDelArr = ((SvxNumberInfoItem*)pItem)->GetDelArray();
454 for ( USHORT i = 0; i < ((SvxNumberInfoItem*)pItem)->GetDelCount(); i++ )
455 pFormatter->DeleteEntry( pDelArr[i] );
458 const SfxItemSet* pOutSet = pDlg->GetOutputItemSet();
459 if( SFX_ITEM_SET == pOutSet->GetItemState(
460 SID_ATTR_NUMBERFORMAT_VALUE, FALSE, &pItem ))
462 UINT32 nNumberFormat = ((SfxUInt32Item*)pItem)->GetValue();
463 // oj #105473# change order of calls
464 const SvNumberformat* pFmt = pFormatter->GetEntry(nNumberFormat);
465 if( pFmt )
466 eCurLanguage = pFmt->GetLanguage();
467 // SetDefFormat uses eCurLanguage to look for if this format already in the list
468 SetDefFormat(nNumberFormat);
470 if( bShowLanguageControl && SFX_ITEM_SET == pOutSet->GetItemState(
471 SID_ATTR_NUMBERFORMAT_ADD_AUTO, FALSE, &pItem ))
473 bUseAutomaticLanguage = ((const SfxBoolItem*)pItem)->GetValue();
476 else
477 SetDefFormat(nFormat);
479 delete pDlg;
481 return 0;
484 /*--------------------------------------------------------------------
485 Beschreibung:
486 --------------------------------------------------------------------*/
488 double NumFormatListBox::GetDefValue(const short nFormatType) const
490 double fDefValue = 0.0;
492 switch (nFormatType)
494 case NUMBERFORMAT_DATE:
495 case NUMBERFORMAT_DATE|NUMBERFORMAT_TIME:
496 fDefValue = SVX_NUMVAL_DATE;
497 break;
499 case NUMBERFORMAT_TIME:
500 fDefValue = SVX_NUMVAL_TIME;
501 break;
502 /* {
503 String sValue("31.8.1997 16:57:34");
504 ULONG nFormat = pFormatter->GetStandardFormat(nFormatType, LANGUAGE_GERMAN);
505 pFormatter->IsNumberFormat( sValue, nFormat, fDefValue );
507 break;*/
509 case NUMBERFORMAT_TEXT:
510 case NUMBERFORMAT_UNDEFINED:
511 fDefValue = 0;
512 break;
514 case NUMBERFORMAT_CURRENCY:
515 fDefValue = SVX_NUMVAL_CURRENCY;
516 break;
518 case NUMBERFORMAT_PERCENT:
519 fDefValue = SVX_NUMVAL_PERCENT;
520 break;
522 case NUMBERFORMAT_LOGICAL:
523 fDefValue = SVX_NUMVAL_BOOLEAN;
524 break;
526 default:
527 fDefValue = SVX_NUMVAL_STANDARD;
528 break;
531 return fDefValue;
534 /*--------------------------------------------------------------------
535 Beschreibung:
536 --------------------------------------------------------------------*/
538 void NumFormatListBox::Clear()
540 ListBox::Clear();
541 nCurrFormatType = -1;