LibreOffice Module sw (master)  1
flddinf.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sfx2/frame.hxx>
21 #include <sfx2/sfxsids.hrc>
22 #include <svl/zforlist.hxx>
23 #include <svl/zformat.hxx>
24 
25 #include <swtypes.hxx>
26 #include <flddinf.hrc>
27 #include <strings.hrc>
28 #include <fldbas.hxx>
29 #include <docufld.hxx>
30 #include <wrtsh.hxx>
31 
32 #include "flddinf.hxx"
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/beans/XPropertySetInfo.hpp>
35 #include <com/sun/star/util/Time.hpp>
36 #include <com/sun/star/util/DateTime.hpp>
37 #include <com/sun/star/util/Date.hpp>
38 
39 #define USER_DATA_VERSION_1 "1"
40 #define USER_DATA_VERSION USER_DATA_VERSION_1
41 
42 using namespace nsSwDocInfoSubType;
43 using namespace com::sun::star;
44 
46 {
47  for (size_t i = 0; i < SAL_N_ELEMENTS(FLD_SELECT); ++i)
48  rListBox.append_text(SwResId(FLD_SELECT[i]));
49 }
50 
52  : SwFieldPage(pPage, pController, "modules/swriter/ui/flddocinfopage.ui", "FieldDocInfoPage", pCoreSet)
53  , nOldSel(0)
54  , nOldFormat(0)
55  , m_xTypeTLB(m_xBuilder->weld_tree_view("type"))
56  , m_xSelection(m_xBuilder->weld_widget("selectframe"))
57  , m_xSelectionLB(m_xBuilder->weld_tree_view("select"))
58  , m_xFormat(m_xBuilder->weld_widget("formatframe"))
59  , m_xFormatLB(new SwNumFormatTreeView(m_xBuilder->weld_tree_view("format")))
60  , m_xFixedCB(m_xBuilder->weld_check_button("fixed"))
61 {
62  m_xTypeTLB->make_sorted();
64 
65  auto nWidth = m_xTypeTLB->get_approximate_digit_width() * FIELD_COLUMN_WIDTH;
66  auto nHeight = m_xTypeTLB->get_height_rows(20);
67  m_xTypeTLB->set_size_request(nWidth, nHeight);
68  m_xFormatLB->get_widget().set_size_request(nWidth, nHeight);
69  m_xSelectionLB->set_size_request(nWidth, nHeight);
70 
71  //enable 'active' language selection
72  m_xFormatLB->SetShowLanguageControl(true);
73 
74  const SfxUnoAnyItem* pItem = pCoreSet
75  ? pCoreSet->GetItem<SfxUnoAnyItem>(SID_DOCINFO, false)
76  : nullptr;
77  if ( pItem )
78  pItem->GetValue() >>= xCustomPropertySet;
79 }
80 
82 {
83 }
84 
86 {
87  Init(); // general initialisation
88 
89  // initialise TypeListBox
90  m_xTypeTLB->freeze();
91  m_xTypeTLB->clear();
92  m_xSelEntry.reset();
93 
94  // display SubTypes in TypeLB
95  sal_uInt16 nSubType = USHRT_MAX;
96  if (IsFieldEdit())
97  {
98  const SwField* pCurField = GetCurField();
99  nSubType = static_cast<const SwDocInfoField*>(pCurField)->GetSubType() & 0xff;
100  if( nSubType == DI_CUSTOM )
101  {
102  m_sOldCustomFieldName = static_cast<const SwDocInfoField*>(pCurField)->GetName();
103  }
104  m_xFormatLB->SetAutomaticLanguage(pCurField->IsAutomaticLanguage());
105  SwWrtShell *pSh = GetWrtShell();
106  if(pSh)
107  {
108  const SvNumberformat* pFormat = pSh->GetNumberFormatter()->GetEntry(pCurField->GetFormat());
109  if(pFormat)
110  m_xFormatLB->SetLanguage(pFormat->GetLanguage());
111  }
112  }
113 
114  sal_Int32 nSelEntryData = -1;
115  const OUString sUserData = GetUserData();
116  sal_Int32 nIdx{ 0 };
117  if (sUserData.getToken(0, ';', nIdx).equalsIgnoreAsciiCase(USER_DATA_VERSION_1))
118  {
119  nSelEntryData = sUserData.getToken(0, ';', nIdx).toInt32();
120  }
121 
122  std::vector<OUString> aLst;
124  std::unique_ptr<weld::TreeIter> xEntry(m_xTypeTLB->make_iterator());
125  std::unique_ptr<weld::TreeIter> xExpandEntry;
126  for(size_t i = 0; i < aLst.size(); ++i)
127  {
128  if (!IsFieldEdit() || nSubType == i)
129  {
130  const OUString sId(OUString::number(i));
131  if (DI_CUSTOM == i)
132  {
133  if(xCustomPropertySet.is() )
134  {
135  uno::Reference< beans::XPropertySetInfo > xSetInfo = xCustomPropertySet->getPropertySetInfo();
136  const uno::Sequence< beans::Property > rProperties = xSetInfo->getProperties();
137 
138  if( rProperties.hasElements() )
139  {
140  std::unique_ptr<weld::TreeIter> xInfo(m_xTypeTLB->make_iterator());
141 
142  OUString sText(SwResId(STR_CUSTOM_FIELD));
143  OUString sEntryId(OUString::number(USHRT_MAX));
144  m_xTypeTLB->insert(nullptr, -1, &sText, &sEntryId, nullptr,
145  nullptr, false, xInfo.get());
146  for (const auto& rProperty : rProperties)
147  {
148  const OUString sEntry = rProperty.Name;
149 
150  m_xTypeTLB->insert(xInfo.get(), -1, &sEntry, &sId,
151  nullptr, nullptr, false, xEntry.get());
152  if (m_sOldCustomFieldName == sEntry)
153  {
154  m_xSelEntry = m_xTypeTLB->make_iterator(xEntry.get());
155  xExpandEntry = m_xTypeTLB->make_iterator(xInfo.get());
156  }
157  }
158  }
159  }
160  }
161  else
162  {
163  if (!(IsFieldDlgHtmlMode() && (i == DI_EDIT || i == DI_SUBJECT || i == DI_PRINT)))
164  {
165  m_xTypeTLB->insert(nullptr, -1, &aLst[i], &sId,
166  nullptr, nullptr, false, xEntry.get());
167  }
168  }
169  if (static_cast<size_t>(nSelEntryData) == i)
170  m_xSelEntry = std::move(xEntry);
171  }
172  }
173 
174  m_xTypeTLB->thaw();
175 
176  if (xExpandEntry)
177  m_xTypeTLB->expand_row(*xExpandEntry);
178 
179  // select old Pos
180  if (m_xSelEntry)
181  {
182  m_xTypeTLB->select(*m_xSelEntry);
183  nSubType = m_xTypeTLB->get_id(*m_xSelEntry).toUInt32();
184  }
185  else
186  {
187  m_xSelEntry = m_xTypeTLB->make_iterator();
188  if (m_xTypeTLB->get_iter_first(*m_xSelEntry))
189  nSubType = m_xTypeTLB->get_id(*m_xSelEntry).toUInt32();
190  else
191  m_xSelEntry.reset();
192  }
193 
194  FillSelectionLB(nSubType);
195  if (m_xSelEntry)
196  TypeHdl(*m_xTypeTLB);
197 
198  m_xTypeTLB->connect_changed(LINK(this, SwFieldDokInfPage, TypeHdl));
199  m_xTypeTLB->connect_row_activated(LINK(this, SwFieldDokInfPage, TreeViewInsertHdl));
200  m_xSelectionLB->connect_changed(LINK(this, SwFieldDokInfPage, SubTypeHdl));
201  m_xSelectionLB->connect_row_activated(LINK(this, SwFieldDokInfPage, TreeViewInsertHdl));
202  m_xFormatLB->connect_row_activated(LINK(this, SwFieldDokInfPage, TreeViewInsertHdl));
203 
204  if (IsFieldEdit())
205  {
206  nOldSel = m_xSelectionLB->get_selected_index();
208  m_xFixedCB->save_state();
209  }
210 }
211 
213 {
214  // current ListBoxPos
215  if (!m_xTypeTLB->get_selected(m_xSelEntry.get()) &&
216  m_xTypeTLB->get_iter_first(*m_xSelEntry))
217  {
218  m_xTypeTLB->select(*m_xSelEntry);
219  }
220  FillSelectionLB(m_xTypeTLB->get_id(*m_xSelEntry).toUInt32());
221  SubTypeHdl(*m_xSelectionLB);
222 }
223 
225 {
226  sal_uInt16 nSubType = m_xTypeTLB->get_id(*m_xSelEntry).toUInt32();
227  sal_Int32 nPos = m_xSelectionLB->get_selected_index();
228  sal_uInt16 nExtSubType;
229  SvNumFormatType nNewType = SvNumFormatType::ALL;
230 
231  if (nSubType != DI_EDIT)
232  {
233  if (nPos == -1)
234  {
235  if (!m_xSelectionLB->n_children())
236  {
237  m_xFormatLB->clear();
238  m_xFormat->set_sensitive(false);
239  if( nSubType == DI_CUSTOM )
240  {
241  //find out which type the custom field has - for a start set to DATE format
242  const OUString sName = m_xTypeTLB->get_text(*m_xSelEntry);
243  try
244  {
245  uno::Any aVal = xCustomPropertySet->getPropertyValue( sName );
246  const uno::Type& rValueType = aVal.getValueType();
247  if( rValueType == ::cppu::UnoType<util::DateTime>::get())
248  {
249  nNewType = SvNumFormatType::DATETIME;
250  }
251  else if( rValueType == ::cppu::UnoType<util::Date>::get())
252  {
253  nNewType = SvNumFormatType::DATE;
254  }
255  else if( rValueType == ::cppu::UnoType<util::Time>::get())
256  {
257  nNewType = SvNumFormatType::TIME;
258  }
259  }
260  catch( const uno::Exception& )
261  {
262  }
263  }
264  else
265  return;
266  }
267  nPos = 0;
268  }
269 
270  nExtSubType = m_xSelectionLB->get_id(nPos).toUInt32();
271  }
272  else
273  nExtSubType = DI_SUB_TIME;
274 
275  SvNumFormatType nOldType = SvNumFormatType::ALL;
276  bool bEnable = false;
277  bool bOneArea = false;
278 
279  if (m_xFormatLB->get_active())
280  nOldType = m_xFormatLB->GetFormatType();
281 
282  switch (nExtSubType)
283  {
284  case DI_SUB_AUTHOR:
285  break;
286 
287  case DI_SUB_DATE:
288  nNewType = SvNumFormatType::DATE;
289  bOneArea = true;
290  break;
291 
292  case DI_SUB_TIME:
293  nNewType = SvNumFormatType::TIME;
294  bOneArea = true;
295  break;
296  }
297  if (nNewType == SvNumFormatType::ALL)
298  {
299  m_xFormatLB->clear();
300  }
301  else
302  {
303  if (nOldType != nNewType)
304  {
305  m_xFormatLB->SetFormatType(nNewType);
306  m_xFormatLB->SetOneArea(bOneArea);
307  }
308  bEnable = true;
309  }
310 
311  sal_uInt32 nFormat = IsFieldEdit() ? static_cast<SwDocInfoField*>(GetCurField())->GetFormat() : 0;
312 
313  sal_uInt16 nOldSubType = IsFieldEdit() ? (static_cast<SwDocInfoField*>(GetCurField())->GetSubType() & 0xff00) : 0;
314 
315  if (IsFieldEdit())
316  {
317  nPos = m_xSelectionLB->get_selected_index();
318  if (nPos != -1)
319  {
320  nSubType = m_xSelectionLB->get_id(nPos).toUInt32();
321 
322  nOldSubType &= ~DI_SUB_FIXED;
323  if (nOldSubType == nSubType)
324  {
325  if (!nFormat && (nNewType == SvNumFormatType::DATE || nNewType == SvNumFormatType::TIME))
326  {
327  SwWrtShell *pSh = GetWrtShell();
328  if(pSh)
329  {
330  SvNumberFormatter* pFormatter = pSh->GetNumberFormatter();
331  LanguageType eLang = m_xFormatLB->GetCurLanguage();
332  if (nNewType == SvNumFormatType::DATE)
333  nFormat = pFormatter->GetFormatIndex( NF_DATE_SYSTEM_SHORT, eLang);
334  else if (nNewType == SvNumFormatType::TIME)
335  nFormat = pFormatter->GetFormatIndex( NF_TIME_HHMM, eLang);
336  }
337  }
338  m_xFormatLB->SetDefFormat(nFormat);
339  }
340  }
341  else if( (nSubType == DI_CUSTOM) && (nNewType != SvNumFormatType::ALL) )
342  {
343  m_xFormatLB->SetDefFormat(nFormat);
344  }
345  }
346 
347  m_xFormat->set_sensitive(bEnable);
348 
349  if (bEnable && m_xFormatLB->get_selected_index() == -1)
350  m_xFormatLB->select(0);
351 }
352 
353 sal_Int32 SwFieldDokInfPage::FillSelectionLB(sal_uInt16 nSubType)
354 {
355  // fill Format-Listbox
357 
358  EnableInsert(nSubType != USHRT_MAX);
359 
360  if (nSubType == USHRT_MAX) // Info-Text
361  nSubType = DI_SUBTYPE_BEGIN;
362 
363  m_xSelectionLB->clear();
364 
365  sal_uInt16 nSize = 0;
366  sal_Int32 nSelPos = -1;
367  sal_uInt16 nExtSubType = IsFieldEdit() ? (static_cast<SwDocInfoField*>(GetCurField())->GetSubType() & 0xff00) : 0;
368 
369  if (IsFieldEdit())
370  {
371  m_xFixedCB->set_active((nExtSubType & DI_SUB_FIXED) != 0);
372  nExtSubType = ((nExtSubType & ~DI_SUB_FIXED) >> 8) - 1;
373  }
374 
375  if (nSubType < DI_CREATE || nSubType == DI_DOCNO || nSubType == DI_EDIT|| nSubType == DI_CUSTOM )
376  {
377  // Format Box is empty for Title and Time
378  }
379  else
380  {
381  nSize = GetFieldMgr().GetFormatCount(nTypeId, IsFieldDlgHtmlMode());
382  for (sal_uInt16 i = 0; i < nSize; ++i)
383  {
384  OUString sId(OUString::number(GetFieldMgr().GetFormatId(nTypeId, i)));
385  m_xSelectionLB->append(sId, GetFieldMgr().GetFormatStr(nTypeId, i));
386  if (IsFieldEdit() && i == nExtSubType)
387  nSelPos = i;
388  }
389  }
390 
391  bool bEnable = nSize != 0;
392 
393  if (nSize)
394  {
395  if (m_xSelectionLB->get_selected_index() == -1)
396  m_xSelectionLB->select(nSelPos == USHRT_MAX ? 0 : nSelPos);
397  bEnable = true;
398  }
399 
400  m_xSelection->set_sensitive(bEnable);
401 
402  return nSize;
403 }
404 
406 {
407  if (!m_xSelEntry)
408  return false;
409 
410  sal_uInt16 nSubType = m_xTypeTLB->get_id(*m_xSelEntry).toUInt32();
411  if (nSubType == USHRT_MAX)
412  return false;
413 
414  sal_uInt32 nFormat = 0;
415 
416  sal_Int32 nPos = m_xSelectionLB->get_selected_index();
417 
418  OUString aName;
419  if (DI_CUSTOM == nSubType)
420  aName = m_xTypeTLB->get_text(*m_xSelEntry);
421 
422  if (nPos != -1)
423  nSubType |= m_xSelectionLB->get_id(nPos).toUInt32();
424 
425  if (m_xFixedCB->get_active())
426  nSubType |= DI_SUB_FIXED;
427 
428  nPos = m_xFormatLB->get_selected_index();
429  if(nPos != -1)
430  nFormat = m_xFormatLB->GetFormat();
431 
432  if (!IsFieldEdit() || nOldSel != m_xSelectionLB->get_selected_index() ||
433  nOldFormat != nFormat || m_xFixedCB->get_state_changed_from_saved()
434  || (DI_CUSTOM == nSubType && aName != m_sOldCustomFieldName ))
435  {
436  InsertField(SwFieldTypesEnum::DocumentInfo, nSubType, aName, OUString(), nFormat,
437  ' ', m_xFormatLB->IsAutomaticLanguage());
438  }
439 
440  return false;
441 }
442 
443 std::unique_ptr<SfxTabPage> SwFieldDokInfPage::Create( weld::Container* pPage, weld::DialogController* pController,
444  const SfxItemSet *const pAttrSet)
445 {
446  return std::make_unique<SwFieldDokInfPage>(pPage, pController, pAttrSet);
447 }
448 
450 {
451  return GRP_REG;
452 }
453 
455 {
456  int nEntry = m_xTypeTLB->get_selected_index();
457  sal_uInt16 nTypeSel = nEntry != -1 ? m_xTypeTLB->get_id(nEntry).toUInt32() : USHRT_MAX;
458  SetUserData(USER_DATA_VERSION ";" + OUString::number( nTypeSel ));
459 }
460 
461 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SwDocInfoSubType DI_SUB_TIME
Definition: docufld.hxx:79
SwFieldDokInfPage(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *pSet)
Definition: flddinf.cxx:51
virtual bool FillItemSet(SfxItemSet *rSet) override
Definition: flddinf.cxx:405
#define USER_DATA_VERSION_1
Definition: flddinf.cxx:39
void SetUserData(const OUString &rString)
sal_uInt32 GetFormat() const
Query parameters for dialog and for BASIC.
Definition: fldbas.hxx:401
sal_uInt32 GetFormatIndex(NfIndexTableOffset, LanguageType eLnge=LANGUAGE_DONTKNOW)
virtual ~SwFieldDokInfPage() override
Definition: flddinf.cxx:81
Base class of all fields.
Definition: fldbas.hxx:293
virtual void Reset(const SfxItemSet *rSet) override
Definition: flddinf.cxx:85
virtual sal_uInt16 GetGroup() override
Definition: flddinf.cxx:449
std::unique_ptr< SwNumFormatTreeView > m_xFormatLB
Definition: flddinf.hxx:41
NF_TIME_HHMM
const SwDocInfoSubType DI_EDIT
Definition: docufld.hxx:74
sal_Int32 FillSelectionLB(sal_uInt16 nSubTypeId)
Definition: flddinf.cxx:353
Used by the UI to modify the document model.
Definition: wrtsh.hxx:90
SwFieldMgr & GetFieldMgr()
Definition: fldpage.hxx:81
void append_text(const OUString &rStr)
sal_Int32 nOldSel
Definition: flddinf.hxx:33
IMPL_LINK_NOARG(SwFieldDokInfPage, TypeHdl, weld::TreeView &, void)
Definition: flddinf.cxx:212
const SwDocInfoSubType DI_SUBJECT
Definition: docufld.hxx:67
const OUString & GetUserData() const
sal_uInt16 GetFormatCount(SwFieldTypesEnum nTypeId, bool bHtmlMode) const
Definition: fldmgr.cxx:671
std::unique_ptr< weld::CheckButton > m_xFixedCB
Definition: flddinf.hxx:42
bool IsFieldDlgHtmlMode() const
Definition: fldpage.hxx:49
const css::uno::Any & GetValue() const
css::uno::Reference< css::beans::XPropertySet > xCustomPropertySet
Definition: flddinf.hxx:31
const char * sName
#define SAL_N_ELEMENTS(arr)
const SwDocInfoSubType DI_CUSTOM
Definition: docufld.hxx:75
void FillFieldSelect(weld::TreeView &rListBox)
Definition: flddinf.cxx:45
const SwDocInfoSubType DI_PRINT
Definition: docufld.hxx:72
void EnableInsert(bool bEnable)
Definition: fldpage.cxx:316
int i
const SwDocInfoSubType DI_CREATE
Definition: docufld.hxx:70
SwFieldTypesEnum
List of FieldTypes at UI.
Definition: fldbas.hxx:93
void Init()
Definition: fldpage.cxx:63
std::unique_ptr< weld::Widget > m_xSelection
Definition: flddinf.hxx:38
std::unique_ptr< weld::TreeView > m_xSelectionLB
Definition: flddinf.hxx:39
SvNumFormatType
void InsertField(SwFieldTypesEnum nTypeId, sal_uInt16 nSubType, const OUString &rPar1, const OUString &rPar2, sal_uInt32 nFormatId, sal_Unicode cDelim= ' ', bool bIsAutomaticLanguage=true)
Definition: fldpage.cxx:115
const SwDocInfoSubType DI_SUB_DATE
Definition: docufld.hxx:80
SwField * GetCurField()
Definition: fldpage.hxx:51
OUString SwResId(const char *pId)
Definition: swmodule.cxx:165
bool IsAutomaticLanguage() const
Definition: fldbas.hxx:383
#define FIELD_COLUMN_WIDTH
Definition: fldpage.hxx:25
const SwDocInfoSubType DI_SUB_AUTHOR
Definition: docufld.hxx:78
SwWrtShell * GetWrtShell()
Definition: fldpage.hxx:52
const SwDocInfoSubType DI_SUBTYPE_BEGIN
NB: these must denote consecutive integers! NB2: these are extended by 4 DI_INFO values for backward ...
Definition: docufld.hxx:65
const SwDocInfoSubType DI_DOCNO
Definition: docufld.hxx:73
const SvNumberformat * GetEntry(sal_uInt32 nKey) const
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: flddinf.cxx:443
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:254
OUString aName
std::unique_ptr< weld::TreeIter > m_xSelEntry
Definition: flddinf.hxx:30
NF_DATE_SYSTEM_SHORT
std::unique_ptr< weld::TreeView > m_xTypeTLB
Definition: flddinf.hxx:37
virtual void FillUserData() override
Definition: flddinf.cxx:454
OUString m_sOldCustomFieldName
Definition: flddinf.hxx:35
OUString sId
bool IsFieldEdit() const
Definition: fldpage.hxx:62
const SwDocInfoSubType DI_SUB_FIXED
Definition: docufld.hxx:81
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
#define USER_DATA_VERSION
Definition: flddinf.cxx:40
SvNumberFormatter * GetNumberFormatter()
Query NumberFormatter from document.
Definition: editsh.cxx:744
sal_uLong nOldFormat
Definition: flddinf.hxx:34
LanguageType GetLanguage() const
sal_uInt16 nPos
void GetSubTypes(SwFieldTypesEnum nId, std::vector< OUString > &rToFill)
Definition: fldmgr.cxx:568