docthemes: Save themes def. to a file when added to ColorSets
[LibreOffice.git] / sw / source / uibase / utlui / numfmtlb.cxx
bloba7251b1599533b32ca01c0a6f48ddf3b1fc884fa
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 <i18nlangtag/lang.h>
21 #include <svl/numformat.hxx>
22 #include <svl/zformat.hxx>
23 #include <svl/eitem.hxx>
24 #include <svx/svxids.hrc>
25 #include <svx/numinf.hxx>
26 #include <svx/flagsdef.hxx>
27 #include <svl/itemset.hxx>
28 #include <osl/diagnose.h>
29 #include <docsh.hxx>
30 #include <swtypes.hxx>
31 #include <swmodule.hxx>
32 #include <view.hxx>
33 #include <wrtsh.hxx>
34 #include <numfmtlb.hxx>
35 #include <strings.hrc>
36 #include <swabstdlg.hxx>
37 #include <memory>
39 /**
40 * Description:
41 * nFormatType: Display the formats of this Type
42 * nDefaultFormat: Select this format and possibly insert it
45 namespace
48 bool lcl_isSystemFormat(sal_uInt32 nDefaultFormat, SvNumberFormatter* pFormatter, LanguageType eCurLanguage)
50 const sal_uInt32 nSysNumFormat = pFormatter->GetFormatIndex(NF_NUMBER_SYSTEM, eCurLanguage);
51 if (nDefaultFormat == nSysNumFormat)
52 return true;
53 const sal_uInt32 nSysShortDateFormat = pFormatter->GetFormatIndex(NF_DATE_SYSTEM_SHORT, eCurLanguage);
54 if (nDefaultFormat == nSysShortDateFormat)
55 return true;
56 const sal_uInt32 nSysLongDateFormat = pFormatter->GetFormatIndex(NF_DATE_SYSTEM_LONG, eCurLanguage);
57 if (nDefaultFormat == nSysLongDateFormat)
58 return true;
60 if ( eCurLanguage != GetAppLanguage() )
61 return false;
63 if (nDefaultFormat == pFormatter->GetFormatForLanguageIfBuiltIn(nSysNumFormat, LANGUAGE_SYSTEM))
64 return true;
65 if (nDefaultFormat == pFormatter->GetFormatForLanguageIfBuiltIn(nSysShortDateFormat, LANGUAGE_SYSTEM))
66 return true;
67 if (nDefaultFormat == pFormatter->GetFormatForLanguageIfBuiltIn(nSysLongDateFormat, LANGUAGE_SYSTEM))
68 return true;
70 return false;
75 double SwNumFormatBase::GetDefValue(const SvNumFormatType nFormatType)
77 SvxNumValCategory nDefValue = SvxNumValCategory::Standard;
79 switch (nFormatType)
81 case SvNumFormatType::DATE:
82 case SvNumFormatType::DATE|SvNumFormatType::TIME:
83 nDefValue = SvxNumValCategory::Date;
84 break;
86 case SvNumFormatType::TIME:
87 nDefValue = SvxNumValCategory::Time;
88 break;
90 case SvNumFormatType::TEXT:
91 case SvNumFormatType::UNDEFINED:
92 nDefValue = SvxNumValCategory::Standard;
93 break;
95 case SvNumFormatType::CURRENCY:
96 nDefValue = SvxNumValCategory::Currency;
97 break;
99 case SvNumFormatType::PERCENT:
100 nDefValue = SvxNumValCategory::Percent;
101 break;
103 case SvNumFormatType::LOGICAL:
104 nDefValue = SvxNumValCategory::Boolean;
105 break;
107 default:
108 nDefValue = SvxNumValCategory::Standard;
109 break;
112 return fSvxNumValConst[nDefValue];
115 SwNumFormatBase::SwNumFormatBase()
116 : m_nStdEntry(0)
117 , m_nDefFormat(0)
118 , m_nCurrFormatType(SvNumFormatType::ALL)
119 , m_bOneArea(false)
120 , mbCurrFormatTypeNeedsInit(true)
121 , m_bShowLanguageControl(false)
122 , m_bUseAutomaticLanguage(true)
126 NumFormatListBox::NumFormatListBox(std::unique_ptr<weld::ComboBox> xControl)
127 : mxControl(std::move(xControl))
129 Init();
132 SwNumFormatTreeView::SwNumFormatTreeView(std::unique_ptr<weld::TreeView> xControl)
133 : mxControl(std::move(xControl))
135 Init();
138 void SwNumFormatBase::Init()
140 if (SwView *pView = GetActiveView())
141 m_eCurLanguage = pView->GetWrtShell().GetCurLang();
142 else
143 m_eCurLanguage = SvtSysLocale().GetLanguageTag().getLanguageType();
145 SetFormatType(SvNumFormatType::NUMBER);
146 SetDefFormat(m_nDefFormat);
149 void NumFormatListBox::Init()
151 SwNumFormatBase::Init();
153 mxControl->connect_changed(LINK(this, NumFormatListBox, SelectHdl));
156 void SwNumFormatTreeView::Init()
158 SwNumFormatBase::Init();
160 mxControl->connect_selection_changed(LINK(this, SwNumFormatTreeView, SelectHdl));
163 void SwNumFormatBase::SetFormatType(const SvNumFormatType nFormatType)
165 if (!mbCurrFormatTypeNeedsInit &&
166 (m_nCurrFormatType & nFormatType)) // there are mixed formats, like for example DateTime
167 return;
169 SwView *pView = GetActiveView();
170 if(!pView)
171 return;
172 SwWrtShell &rSh = pView->GetWrtShell();
173 SvNumberFormatter* pFormatter = rSh.GetNumberFormatter();
175 clear(); // Remove all entries from the Listbox
177 NfIndexTableOffset eOffsetStart = NF_NUMBER_START;
178 NfIndexTableOffset eOffsetEnd = NF_NUMBER_START;
180 switch( nFormatType )
182 case SvNumFormatType::NUMBER:
183 eOffsetStart=NF_NUMBER_START;
184 eOffsetEnd=NF_NUMBER_END;
185 break;
187 case SvNumFormatType::PERCENT:
188 eOffsetStart=NF_PERCENT_START;
189 eOffsetEnd=NF_PERCENT_END;
190 break;
192 case SvNumFormatType::CURRENCY:
193 eOffsetStart=NF_CURRENCY_START;
194 eOffsetEnd=NF_CURRENCY_END;
195 break;
197 case SvNumFormatType::DATETIME:
198 eOffsetStart=NF_DATE_START;
199 eOffsetEnd=NF_TIME_END;
200 break;
202 case SvNumFormatType::DATE:
203 eOffsetStart=NF_DATE_START;
204 eOffsetEnd=NF_DATE_END;
205 break;
207 case SvNumFormatType::TIME:
208 eOffsetStart=NF_TIME_START;
209 eOffsetEnd=NF_TIME_END;
210 break;
212 case SvNumFormatType::SCIENTIFIC:
213 eOffsetStart=NF_SCIENTIFIC_START;
214 eOffsetEnd=NF_SCIENTIFIC_END;
215 break;
217 case SvNumFormatType::FRACTION:
218 eOffsetStart=NF_FRACTION_START;
219 eOffsetEnd=NF_FRACTION_END;
220 break;
222 case SvNumFormatType::LOGICAL:
223 eOffsetStart=NF_BOOLEAN;
224 eOffsetEnd=NF_BOOLEAN;
225 break;
227 case SvNumFormatType::TEXT:
228 eOffsetStart=NF_TEXT;
229 eOffsetEnd=NF_TEXT;
230 break;
232 case SvNumFormatType::ALL:
233 eOffsetStart=NF_NUMERIC_START;
234 eOffsetEnd = NfIndexTableOffset( NF_INDEX_TABLE_ENTRIES - 1 );
235 break;
237 default:
238 OSL_FAIL("what a format?");
239 break;
242 const SvNumberformat* pFormat;
243 sal_Int32 i = 0;
244 const Color* pCol;
245 double fVal = SwNumFormatBase::GetDefValue(nFormatType);
246 OUString sValue;
248 const sal_uInt32 nSysNumFormat = pFormatter->GetFormatIndex(
249 NF_NUMBER_SYSTEM, m_eCurLanguage );
250 const sal_uInt32 nSysShortDateFormat = pFormatter->GetFormatIndex(
251 NF_DATE_SYSTEM_SHORT, m_eCurLanguage );
252 const sal_uInt32 nSysLongDateFormat = pFormatter->GetFormatIndex(
253 NF_DATE_SYSTEM_LONG, m_eCurLanguage );
255 for (int nIndex = eOffsetStart; nIndex <= eOffsetEnd; ++nIndex)
257 const sal_uInt32 nFormat = pFormatter->GetFormatIndex(
258 static_cast<NfIndexTableOffset>(nIndex), m_eCurLanguage );
259 pFormat = pFormatter->GetEntry( nFormat );
261 if( nFormat == pFormatter->GetFormatIndex( NF_NUMBER_STANDARD,
262 m_eCurLanguage )
263 || pFormat->GetOutputString( fVal, sValue, &pCol, pFormatter->GetNatNum(), pFormatter->GetROLanguageData() )
264 || nFormatType == SvNumFormatType::UNDEFINED )
266 sValue = pFormat->GetFormatstring();
268 else if( nFormatType == SvNumFormatType::TEXT )
270 pFormatter->GetOutputString( u"\"ABC\""_ustr, nFormat, sValue, &pCol);
273 if (nFormat != nSysNumFormat &&
274 nFormat != nSysShortDateFormat &&
275 nFormat != nSysLongDateFormat)
277 append(OUString::number(nFormat), sValue);
279 if( nFormat == pFormatter->GetStandardFormat(
280 nFormatType, m_eCurLanguage ) )
281 m_nStdEntry = i;
282 ++i;
286 append_text(SwResId(STR_DEFINE_NUMBERFORMAT));
288 set_active(m_nStdEntry);
290 m_nCurrFormatType = nFormatType;
291 mbCurrFormatTypeNeedsInit = false;
295 void SwNumFormatBase::SetDefFormat(const sal_uInt32 nDefaultFormat)
297 if (nDefaultFormat == NUMBERFORMAT_ENTRY_NOT_FOUND)
299 m_nDefFormat = nDefaultFormat;
300 return;
303 SwView *pView = GetActiveView();
304 if(!pView)
305 return;
306 SwWrtShell &rSh = pView->GetWrtShell();
307 SvNumberFormatter* pFormatter = rSh.GetNumberFormatter();
309 SvNumFormatType nType = pFormatter->GetType(nDefaultFormat);
311 SetFormatType(nType);
313 sal_uInt32 nFormat = pFormatter->GetFormatForLanguageIfBuiltIn(nDefaultFormat, m_eCurLanguage);
315 for (sal_Int32 i = 0, nCount = get_count(); i < nCount; ++i)
317 if (nFormat == get_id(i).toUInt32())
319 set_active(i);
320 m_nStdEntry = i;
321 m_nDefFormat = GetFormat();
322 return;
326 // No entry found:
327 OUString sValue;
328 const Color* pCol = nullptr;
330 if (nType == SvNumFormatType::TEXT)
332 pFormatter->GetOutputString(u"\"ABC\""_ustr, nDefaultFormat, sValue, &pCol);
334 else
336 pFormatter->GetOutputString(SwNumFormatBase::GetDefValue(nType), nDefaultFormat, sValue, &pCol);
339 sal_Int32 nPos = 0;
340 while (get_id(nPos).toUInt32() == NUMBERFORMAT_ENTRY_NOT_FOUND)
341 nPos++;
343 if ( lcl_isSystemFormat(nDefaultFormat, pFormatter, m_eCurLanguage) )
345 sValue += SwResId(RID_STR_SYSTEM);
348 insert_text(nPos, sValue); // Insert as first numeric entry
349 set_id(nPos, OUString::number(nDefaultFormat));
350 set_active(nPos);
351 m_nDefFormat = GetFormat();
354 sal_uInt32 NumFormatListBox::GetFormat() const
356 return mxControl->get_active_id().toUInt32();
359 sal_uInt32 SwNumFormatTreeView::GetFormat() const
361 return mxControl->get_selected_id().toUInt32();
364 void SwNumFormatBase::CallSelectHdl()
366 const sal_Int32 nPos = get_active();
367 OUString sDefine(SwResId( STR_DEFINE_NUMBERFORMAT ));
368 SwView *pView = GetActiveView();
370 if (!pView || nPos != get_count() - 1 || get_text(nPos) != sDefine)
371 return;
373 SwWrtShell &rSh = pView->GetWrtShell();
374 SvNumberFormatter* pFormatter = rSh.GetNumberFormatter();
376 SfxItemSetFixed<
377 SID_ATTR_NUMBERFORMAT_VALUE, SID_ATTR_NUMBERFORMAT_INFO,
378 SID_ATTR_NUMBERFORMAT_ONE_AREA, SID_ATTR_NUMBERFORMAT_ONE_AREA,
379 SID_ATTR_NUMBERFORMAT_NOLANGUAGE,
380 SID_ATTR_NUMBERFORMAT_NOLANGUAGE,
381 SID_ATTR_NUMBERFORMAT_ADD_AUTO,
382 SID_ATTR_NUMBERFORMAT_ADD_AUTO> aCoreSet( rSh.GetAttrPool() );
384 double fValue = SwNumFormatBase::GetDefValue(m_nCurrFormatType);
386 sal_uInt32 nFormat = pFormatter->GetStandardFormat( m_nCurrFormatType, m_eCurLanguage);
387 aCoreSet.Put( SfxUInt32Item( SID_ATTR_NUMBERFORMAT_VALUE, nFormat ));
389 aCoreSet.Put( SvxNumberInfoItem( pFormatter, fValue,
390 SID_ATTR_NUMBERFORMAT_INFO ) );
392 if( (SvNumFormatType::DATE | SvNumFormatType::TIME) & m_nCurrFormatType )
393 aCoreSet.Put(SfxBoolItem(SID_ATTR_NUMBERFORMAT_ONE_AREA, m_bOneArea));
395 aCoreSet.Put(SfxBoolItem(SID_ATTR_NUMBERFORMAT_NOLANGUAGE, !m_bShowLanguageControl));
396 aCoreSet.Put(SfxBoolItem(SID_ATTR_NUMBERFORMAT_ADD_AUTO, m_bUseAutomaticLanguage));
398 // force deselect to break mouse lock on selected entry
399 set_active(-1);
401 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
402 ScopedVclPtr<SfxAbstractDialog> pDlg(pFact->CreateNumFormatDialog(&get_widget(), aCoreSet));
404 if (RET_OK == pDlg->Execute())
406 const SvxNumberInfoItem* pFormatInfoItem = pView->GetDocShell()->
407 GetItem( SID_ATTR_NUMBERFORMAT_INFO );
409 if( pFormatInfoItem )
411 for ( sal_uInt32 key : pFormatInfoItem->GetDelFormats() )
412 pFormatter->DeleteEntry( key );
415 const SfxItemSet* pOutSet = pDlg->GetOutputItemSet();
416 if( const SfxUInt32Item* pFormatValueItem = pOutSet->GetItemIfSet(
417 SID_ATTR_NUMBERFORMAT_VALUE, false ))
419 sal_uInt32 nNumberFormat = pFormatValueItem->GetValue();
420 // oj #105473# change order of calls
421 const SvNumberformat* pFormat = pFormatter->GetEntry(nNumberFormat);
422 if( pFormat )
423 m_eCurLanguage = pFormat->GetLanguage();
424 // SetDefFormat uses eCurLanguage to look for if this format already in the list
425 SetDefFormat(nNumberFormat);
427 const SfxBoolItem* pAddAutoItem;
428 if( m_bShowLanguageControl && (pAddAutoItem = pOutSet->GetItemIfSet(
429 SID_ATTR_NUMBERFORMAT_ADD_AUTO, false)))
431 m_bUseAutomaticLanguage = pAddAutoItem->GetValue();
434 else
435 SetDefFormat(nFormat);
439 IMPL_LINK_NOARG(NumFormatListBox, SelectHdl, weld::ComboBox&, void)
441 CallSelectHdl();
444 IMPL_LINK_NOARG(SwNumFormatTreeView, SelectHdl, weld::TreeView&, void)
446 CallSelectHdl();
449 void SwNumFormatBase::clear()
451 mbCurrFormatTypeNeedsInit = true;
452 m_nCurrFormatType = SvNumFormatType::ALL;
455 void NumFormatListBox::clear()
457 mxControl->clear();
458 SwNumFormatBase::clear();
461 void SwNumFormatTreeView::clear()
463 mxControl->clear();
464 SwNumFormatBase::clear();
467 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */