1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: numfmtlb.cxx,v $
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>
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>
52 #include <swtypes.hxx>
53 #include <swmodule.hxx>
56 #include <numfmtlb.hxx>
58 #include "swabstdlg.hxx"
61 #include <sfx2/viewfrm.hxx>
63 using namespace ::com::sun::star::uno
;
64 using namespace ::com::sun::star::lang
;
67 // STATIC DATA -----------------------------------------------------------
69 /*--------------------------------------------------------------------
71 nFormatType: Formate dieses Typs anzeigen
72 nDefFmt: Dieses Format selektieren und ggf vorher
74 --------------------------------------------------------------------*/
76 NumFormatListBox::NumFormatListBox( Window
* pWin
, const ResId
& rResId
,
77 short nFormatType
, ULONG nDefFmt
,
79 ListBox ( pWin
, rResId
),
86 bShowLanguageControl(FALSE
),
87 bUseAutomaticLanguage(TRUE
)
89 Init(nFormatType
, bUsrFmts
);
92 /*--------------------------------------------------------------------
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),
103 nDefFormat (nDefFmt
),
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();
121 eCurLanguage
= pView
->GetWrtShell().GetCurLang();
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 /*--------------------------------------------------------------------
139 --------------------------------------------------------------------*/
141 NumFormatListBox::~NumFormatListBox()
144 delete pOwnFormatter
;
146 /*--------------------------------------------------------------------
148 --------------------------------------------------------------------*/
150 SwView
* NumFormatListBox::GetView()
154 return ::GetActiveView();
157 /*--------------------------------------------------------------------
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
;
169 pFormatter
= pOwnFormatter
;
172 SwView
*pView
= GetView();
173 DBG_ASSERT(pView
, "no view found");
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
;
192 case NUMBERFORMAT_PERCENT
:
193 eOffsetStart
=NF_PERCENT_START
;
194 eOffsetEnd
=NF_PERCENT_END
;
197 case NUMBERFORMAT_CURRENCY
:
198 eOffsetStart
=NF_CURRENCY_START
;
199 eOffsetEnd
=NF_CURRENCY_END
;
202 case NUMBERFORMAT_DATETIME
:
203 eOffsetStart
=NF_DATE_START
;
204 eOffsetEnd
=NF_TIME_END
;
207 case NUMBERFORMAT_DATE
:
208 eOffsetStart
=NF_DATE_START
;
209 eOffsetEnd
=NF_DATE_END
;
212 case NUMBERFORMAT_TIME
:
213 eOffsetStart
=NF_TIME_START
;
214 eOffsetEnd
=NF_TIME_END
;
217 case NUMBERFORMAT_SCIENTIFIC
:
218 eOffsetStart
=NF_SCIENTIFIC_START
;
219 eOffsetEnd
=NF_SCIENTIFIC_END
;
222 case NUMBERFORMAT_FRACTION
:
223 eOffsetStart
=NF_FRACTION_START
;
224 eOffsetEnd
=NF_FRACTION_END
;
227 case NUMBERFORMAT_LOGICAL
:
228 eOffsetStart
=NF_BOOLEAN
;
229 eOffsetEnd
=NF_BOOLEAN
;
232 case NUMBERFORMAT_TEXT
:
233 eOffsetStart
=NF_TEXT
;
237 case NUMBERFORMAT_ALL
:
238 eOffsetStart
=NF_NUMERIC_START
;
239 eOffsetEnd
= NfIndexTableOffset( NF_INDEX_TABLE_ENTRIES
- 1 );
243 DBG_ERROR("what a format?");
247 const SvNumberformat
* pFmt
;
251 double fVal
= GetDefValue( nFormatType
);
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
,
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
) )
294 nPos
= InsertEntry(SW_RESSTR( STR_DEFINE_NUMBERFORMAT
));
295 SetEntryData( nPos
, NULL
);
298 SelectEntryPos( nStdEntry
);
300 nCurrFormatType
= nFormatType
;
304 /*--------------------------------------------------------------------
306 --------------------------------------------------------------------*/
308 void NumFormatListBox::SetDefFormat(const ULONG nDefFmt
)
310 if (nDefFmt
== ULONG_MAX
)
312 nDefFormat
= nDefFmt
;
316 SvNumberFormatter
* pFormatter
;
318 pFormatter
= pOwnFormatter
;
321 SwView
*pView
= GetView();
322 DBG_ASSERT(pView
, "no view found");
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
))
341 nDefFormat
= GetFormat();
346 // Kein Eintrag gefunden:
347 double fValue
= GetDefValue(nType
);
351 if (nType
== NUMBERFORMAT_TEXT
)
353 String
sTxt(C2S("\"ABC\""));
354 pFormatter
->GetOutputString(sTxt
, nDefFmt
, sValue
, &pCol
);
357 pFormatter
->GetOutputString(fValue
, nDefFmt
, sValue
, &pCol
);
360 while ((ULONG
)GetEntryData(nPos
) == ULONG_MAX
)
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() )
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 /*--------------------------------------------------------------------
390 --------------------------------------------------------------------*/
392 ULONG
NumFormatListBox::GetFormat() const
394 USHORT nPos
= GetSelectEntryPos();
396 return (ULONG
)GetEntryData(nPos
);
399 /*--------------------------------------------------------------------
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
,
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
);
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();
477 SetDefFormat(nFormat
);
484 /*--------------------------------------------------------------------
486 --------------------------------------------------------------------*/
488 double NumFormatListBox::GetDefValue(const short nFormatType
) const
490 double fDefValue
= 0.0;
494 case NUMBERFORMAT_DATE
:
495 case NUMBERFORMAT_DATE
|NUMBERFORMAT_TIME
:
496 fDefValue
= SVX_NUMVAL_DATE
;
499 case NUMBERFORMAT_TIME
:
500 fDefValue
= SVX_NUMVAL_TIME
;
503 String sValue("31.8.1997 16:57:34");
504 ULONG nFormat = pFormatter->GetStandardFormat(nFormatType, LANGUAGE_GERMAN);
505 pFormatter->IsNumberFormat( sValue, nFormat, fDefValue );
509 case NUMBERFORMAT_TEXT
:
510 case NUMBERFORMAT_UNDEFINED
:
514 case NUMBERFORMAT_CURRENCY
:
515 fDefValue
= SVX_NUMVAL_CURRENCY
;
518 case NUMBERFORMAT_PERCENT
:
519 fDefValue
= SVX_NUMVAL_PERCENT
;
522 case NUMBERFORMAT_LOGICAL
:
523 fDefValue
= SVX_NUMVAL_BOOLEAN
;
527 fDefValue
= SVX_NUMVAL_STANDARD
;
534 /*--------------------------------------------------------------------
536 --------------------------------------------------------------------*/
538 void NumFormatListBox::Clear()
541 nCurrFormatType
= -1;