LibreOffice Module sw (master)  1
fldpage.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 <svl/stritem.hxx>
21 #include <sfx2/request.hxx>
22 #include <sfx2/htmlmode.hxx>
23 #include <sfx2/viewfrm.hxx>
24 #include <dbfld.hxx>
25 #include <flddat.hxx>
26 #include <fmtfld.hxx>
27 #include <viewopt.hxx>
28 #include <fldedt.hxx>
29 #include <docsh.hxx>
30 #include <swmodule.hxx>
31 #include <view.hxx>
32 #include <wrtsh.hxx>
33 #include <expfld.hxx>
34 #include <fldtdlg.hxx>
35 #include "fldpage.hxx"
36 #include <docufld.hxx>
37 #include <cmdid.h>
38 #include <sfx2/bindings.hxx>
39 #include <o3tl/string_view.hxx>
40 
41 using namespace ::com::sun::star;
42 
43 // note: pAttrSet may be null if the dialog is restored on startup
44 SwFieldPage::SwFieldPage(weld::Container* pPage, weld::DialogController* pController, const OUString& rUIXMLDescription,
45  const OString& rID, const SfxItemSet *pAttrSet)
46  : SfxTabPage(pPage, pController, rUIXMLDescription, rID, pAttrSet)
47  , m_pCurField(nullptr)
48  , m_pWrtShell(nullptr)
49  , m_nTypeSel(-1)
50  , m_nSelectionSel(-1)
51  , m_bFieldEdit(false)
52  , m_bInsert(true)
53  , m_bFieldDlgHtmlMode(false)
54  , m_bRefresh(false)
55  , m_bFirstHTMLInit(true)
56 {
57 }
58 
60 {
61 }
62 
63 // initialise TabPage
65 {
66  SwDocShell* pDocSh = static_cast<SwDocShell*>(SfxObjectShell::Current());
67  bool bNewMode = 0 != (::GetHtmlMode(pDocSh) & HTMLMODE_ON);
68 
69  m_bFieldEdit = nullptr == dynamic_cast<SwFieldDlg*>(GetDialogController());
70 
71  // newly initialise FieldManager. important for
72  // Dok-Switch (fldtdlg:ReInitTabPage)
74 
75  if( bNewMode == m_bFieldDlgHtmlMode )
76  return;
77 
78  m_bFieldDlgHtmlMode = bNewMode;
79 
80  // initialise Rangelistbox
82  return;
83 
84  m_bFirstHTMLInit = false;
85  SwWrtShell *pSh = m_pWrtShell;
86  if(! pSh)
87  pSh = ::GetActiveWrtShell();
88  if(pSh)
89  {
90  SwDoc* pDoc = pSh->GetDoc();
92  "HTML_ON", 1));
94  "HTML_OFF", 1));
95  }
96 }
97 
98 // newly initialise page
100 {
102 }
103 
104 // complete reset; edit new field
105 void SwFieldPage::EditNewField( bool bOnlyActivate )
106 {
107  if (!bOnlyActivate)
108  m_nTypeSel = -1;
109  m_nSelectionSel = -1;
110  m_bRefresh = true;
111  Reset(nullptr);
112  m_bRefresh = false;
113 }
114 
115 // insert field
116 void SwFieldPage::InsertField(SwFieldTypesEnum nTypeId, sal_uInt16 nSubType, const OUString& rPar1,
117  const OUString& rPar2, sal_uInt32 nFormatId,
118  sal_Unicode cSeparator, bool bIsAutomaticLanguage)
119 {
120  SwView* pView = GetActiveView();
121  SwWrtShell *pSh = m_pWrtShell ? m_pWrtShell : pView->GetWrtShellPtr();
122 
123  if (!IsFieldEdit()) // insert new field
124  {
125  SwInsertField_Data aData(nTypeId, nSubType, rPar1, rPar2, nFormatId, nullptr, cSeparator, bIsAutomaticLanguage );
126  //#i26566# provide parent for SwWrtShell::StartInputFieldDlg
128  m_aMgr.InsertField( aData );
129 
130  uno::Reference< frame::XDispatchRecorder > xRecorder =
131  pView->GetViewFrame()->GetBindings().GetRecorder();
132  if ( xRecorder.is() )
133  {
134  bool bRecordDB = SwFieldTypesEnum::Database == nTypeId ||
138  SwFieldTypesEnum::DatabaseName == nTypeId ;
139 
140  SfxRequest aReq( pView->GetViewFrame(),
141  bRecordDB ? FN_INSERT_DBFIELD : FN_INSERT_FIELD );
142  if(bRecordDB)
143  {
144  sal_Int32 nIdx{ 0 };
146  (FN_INSERT_DBFIELD,rPar1.getToken(0, DB_DELIM, nIdx)));
147  aReq.AppendItem(SfxStringItem
148  (FN_PARAM_1,rPar1.getToken(0, DB_DELIM, nIdx)));
149  aReq.AppendItem(SfxInt32Item
150  (FN_PARAM_3, o3tl::toInt32(o3tl::getToken(rPar1, 0, DB_DELIM, nIdx))));
151  aReq.AppendItem(SfxStringItem
152  (FN_PARAM_2,rPar1.getToken(0, DB_DELIM, nIdx)));
153  }
154  else
155  {
156  aReq.AppendItem(SfxStringItem(FN_INSERT_FIELD, rPar1));
157  aReq.AppendItem(SfxStringItem
158  (FN_PARAM_3, OUString(cSeparator)));
159  aReq.AppendItem(SfxUInt16Item(FN_PARAM_FIELD_SUBTYPE, nSubType));
160  }
161  aReq.AppendItem(SfxUInt16Item(FN_PARAM_FIELD_TYPE , static_cast<sal_uInt16>(nTypeId)));
162  aReq.AppendItem(SfxStringItem(FN_PARAM_FIELD_CONTENT, rPar2));
163  aReq.AppendItem(SfxUInt32Item(FN_PARAM_FIELD_FORMAT , nFormatId));
164  aReq.Done();
165  }
166 
167  }
168  else // change field
169  {
170  std::unique_ptr<SwField> pTmpField = m_pCurField->CopyField();
171 
172  OUString sPar1(rPar1);
173  OUString sPar2(rPar2);
174  switch( nTypeId )
175  {
178  nSubType = static_cast< sal_uInt16 >(((nTypeId == SwFieldTypesEnum::Date) ? DATEFLD : TIMEFLD) |
179  ((nSubType == DATE_VAR) ? 0 : FIXEDFLD));
180  break;
181 
186  {
187  sal_Int32 nPos = 0;
188  SwDBData aData;
189 
190  aData.sDataSource = rPar1.getToken(0, DB_DELIM, nPos);
191  aData.sCommand = rPar1.getToken(0, DB_DELIM, nPos);
192  aData.nCommandType = o3tl::toInt32(o3tl::getToken(rPar1, 0, DB_DELIM, nPos));
193  sPar1 = rPar1.copy(nPos);
194 
195  static_cast<SwDBNameInfField*>(pTmpField.get())->SetDBData(aData);
196  }
197  break;
198 
200  {
201  SwDBData aData;
202  sal_Int32 nIdx{ 0 };
203  aData.sDataSource = rPar1.getToken(0, DB_DELIM, nIdx);
204  aData.sCommand = rPar1.getToken(0, DB_DELIM, nIdx);
205  aData.nCommandType = o3tl::toInt32(o3tl::getToken(rPar1, 0, DB_DELIM, nIdx));
206  OUString sColumn = rPar1.getToken(0, DB_DELIM, nIdx);
207 
208  auto pOldType = static_cast<SwDBFieldType*>(pTmpField->GetTyp());
209  auto pType = static_cast<SwDBFieldType*>(pSh->InsertFieldType(SwDBFieldType(pSh->GetDoc(), sColumn, aData)));
210  if(auto pFormatField = pOldType->FindFormatForField(m_pCurField))
211  {
212  pFormatField->RegisterToFieldType(*pType);
213  pTmpField->ChgTyp(pType);
214  }
215  }
216  break;
217 
219  {
220  SwSetExpFieldType* pTyp = static_cast<SwSetExpFieldType*>(pTmpField->GetTyp());
221  pTyp->SetOutlineLvl( static_cast< sal_uInt8 >(nSubType & 0xff));
222  pTyp->SetDelimiter(OUString(cSeparator));
223 
224  nSubType = nsSwGetSetExpType::GSE_SEQ;
225  }
226  break;
227 
229  {
230  // User- or SetField ?
231  if (m_aMgr.GetFieldType(SwFieldIds::User, sPar1) == nullptr &&
232  !(pTmpField->GetSubType() & INP_TXT)) // SETEXPFLD
233  {
234  SwSetExpField* pField = static_cast<SwSetExpField*>(pTmpField.get());
235  pField->SetPromptText(sPar2);
236  sPar2 = pField->GetPar2();
237  }
238  }
239  break;
241  {
242  if( nSubType == nsSwDocInfoSubType::DI_CUSTOM )
243  {
244  SwDocInfoField* pDocInfo = static_cast<SwDocInfoField*>( pTmpField.get() );
245  pDocInfo->SetName( rPar1 );
246  }
247  }
248  break;
249  default: break;
250  }
251 
252  pSh->StartAllAction();
253 
254  pTmpField->SetSubType(nSubType);
255  pTmpField->SetAutomaticLanguage(bIsAutomaticLanguage);
256 
257  m_aMgr.UpdateCurField( nFormatId, sPar1, sPar2, std::move(pTmpField) );
258 
260 
261  switch (nTypeId)
262  {
265  m_aMgr.EvalExpFields(pSh);
266  break;
267  default: break;
268  }
269 
270  pSh->SetUndoNoResetModified();
271  pSh->EndAllAction();
272  }
273 }
274 
276 {
277  if (rLst1.n_children())
278  m_aLstStrArr[ 0 ] = rLst1.get_selected_text();
279  else
280  m_aLstStrArr[ 0 ].clear();
281  m_aLstStrArr[ 1 ].clear();
282  m_aLstStrArr[ 2 ].clear();
283 }
284 
286 {
287  sal_Int32 nPos = 0;
288  if (rLst1.n_children() && !m_aLstStrArr[ 0 ].isEmpty() &&
289  -1 != ( nPos = rLst1.find_text(m_aLstStrArr[ 0 ] ) ) )
290  rLst1.select( nPos );
291 }
292 
293 // Insert new fields
294 IMPL_LINK( SwFieldPage, TreeViewInsertHdl, weld::TreeView&, rBox, bool )
295 {
296  InsertHdl(&rBox);
297  return true;
298 }
299 
301 {
302  if (SwFieldDlg *pDlg = dynamic_cast<SwFieldDlg*>(GetDialogController()))
303  {
304  pDlg->InsertHdl();
305 
306  if (pBtn)
307  pBtn->grab_focus(); // because of InputField-Dlg
308  }
309  else
310  {
311  SwFieldEditDlg *pEditDlg = static_cast<SwFieldEditDlg*>(GetDialogController());
312  pEditDlg->InsertHdl();
313  }
314 }
315 
316 // enable/disable "Insert"-Button
317 void SwFieldPage::EnableInsert(bool bEnable)
318 {
319  if (SwFieldDlg *pDlg = dynamic_cast<SwFieldDlg*>(GetDialogController()))
320  {
321  if (pDlg->GetCurTabPage() == this)
322  pDlg->EnableInsert(bEnable);
323  }
324  else
325  {
326  SwFieldEditDlg *pEditDlg = static_cast<SwFieldEditDlg*>(GetDialogController());
327  pEditDlg->EnableInsert(bEnable);
328  }
329 
330  m_bInsert = bEnable;
331 }
332 
334 {
335  InsertHdl(nullptr);
336  return true;
337 }
338 
340 {
341  m_pWrtShell = pShell;
342  m_aMgr.SetWrtShell( pShell );
343 }
344 
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwFieldType * GetFieldType(SwFieldIds nResId, size_t nField=0) const
Definition: fldmgr.cxx:415
SfxViewFrame * GetViewFrame() const
void SetDelimiter(const OUString &s)
Definition: expfld.hxx:181
void EvalExpFields(SwWrtShell *pSh)
Definition: fldmgr.cxx:1735
sal_Int32 nCommandType
Definition: swdbdata.hxx:32
void RestorePos(weld::TreeView &rLst1)
Definition: fldpage.cxx:285
SwView * GetActiveView()
Definition: swmodul1.cxx:116
bool m_bFirstHTMLInit
Definition: fldpage.hxx:41
void SetUndoNoResetModified()
Definition: edws.cxx:80
void SetPromptText(const OUString &rStr)
Definition: expfld.hxx:259
void SetOutlineLvl(sal_uInt8 n)
Definition: expfld.hxx:183
OUString sDataSource
Definition: swdbdata.hxx:30
SwWrtShell * GetActiveWrtShell()
Definition: swmodul1.cxx:108
void UpdateCurField(sal_uInt32 nFormat, const OUString &rPar1, const OUString &rPar2, std::unique_ptr< SwField > _pField=nullptr)
Definition: fldmgr.cxx:1536
Definition: doc.hxx:187
void SetWrtShell(SwWrtShell *pShell)
Definition: fldmgr.hxx:122
std::unique_ptr< SwField > CopyField() const
Definition: fldbas.cxx:504
bool m_bRefresh
Definition: fldpage.hxx:40
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
#define FN_PARAM_FIELD_FORMAT
Definition: cmdid.h:801
static SfxObjectShell * Current()
sal_Int32 m_nSelectionSel
Definition: fldpage.hxx:36
bool InsertField(const SwInsertField_Data &rData)
Definition: fldmgr.cxx:895
void EndAllAction()
Definition: edws.cxx:97
void EnableInsert(bool bEnable)
Definition: fldedt.cxx:231
bool m_bFieldEdit
Definition: fldpage.hxx:37
Used by the UI to modify the document model.
Definition: wrtsh.hxx:96
sal_uInt16 sal_Unicode
virtual OUString get_selected_text() const =0
#define FN_PARAM_FIELD_SUBTYPE
Definition: cmdid.h:800
virtual weld::Button & GetOKButton() const =0
virtual int n_children() const =0
virtual void Reset(const SfxItemSet *)
SwDoc * GetDoc() const
Definition: viewsh.hxx:282
void InsertHdl()
Definition: fldedt.cxx:238
constexpr OUStringLiteral aData
Definition: ww8scan.hxx:48
weld::Widget * m_pParent
Definition: fldmgr.hxx:81
const SwDocInfoSubType DI_CUSTOM
Definition: docufld.hxx:80
bool m_bInsert
Definition: fldpage.hxx:38
virtual ~SwFieldPage() override
Definition: fldpage.cxx:59
void EnableInsert(bool bEnable)
Definition: fldpage.cxx:317
SwFieldType * InsertFieldType(const SwFieldType &)
insert field type
Definition: edfld.cxx:338
void SavePos(const weld::TreeView &rLst1)
Definition: fldpage.cxx:275
SfxBindings & GetBindings()
SwFieldTypesEnum
List of FieldTypes at UI.
Definition: fldbas.hxx:94
void Init()
Definition: fldpage.cxx:64
HTMLMODE_ON
sal_uInt16 GetHtmlMode(const SwDocShell *pShell)
Definition: viewopt.cxx:354
SwField * GetCurField()
Definition: fldmgr.cxx:430
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:116
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
#define FN_INSERT_DBFIELD
Definition: cmdid.h:211
OUString m_aLstStrArr[coLBCount]
Definition: fldpage.hxx:31
SwFieldPage(weld::Container *pPage, weld::DialogController *pController, const OUString &rUIXMLDescription, const OString &rID, const SfxItemSet *pAttrSet)
Definition: fldpage.cxx:44
#define DB_DELIM
Definition: swtypes.hxx:130
virtual int find_text(const OUString &rText) const =0
SwFieldMgr m_aMgr
Definition: fldpage.hxx:32
SwWrtShell * m_pWrtShell
Definition: fldpage.hxx:34
void EditNewField(bool bOnlyActivate=false)
Definition: fldpage.cxx:105
virtual void select(int pos)=0
const SwGetSetExpType GSE_SEQ
Sequence.
Definition: fldbas.hxx:205
const css::uno::Reference< css::frame::XDispatchRecorder > & GetRecorder() const
SwWrtShell * GetWrtShellPtr() const
Definition: view.hxx:414
#define FN_INSERT_FIELD
Definition: cmdid.h:207
IMPL_LINK(SwFieldPage, TreeViewInsertHdl, weld::TreeView &, rBox, bool)
Definition: fldpage.cxx:294
virtual void Activate() override
Definition: fldpage.cxx:99
void SetWrtShell(SwWrtShell *m_pWrtShell)
Definition: fldpage.cxx:339
virtual void grab_focus()=0
#define FN_PARAM_FIELD_TYPE
Definition: cmdid.h:799
SwField * m_pCurField
Definition: fldpage.hxx:33
bool m_bFieldDlgHtmlMode
Definition: fldpage.hxx:39
SfxOkDialogController * GetDialogController() const
void SetName(const OUString &rName)
Definition: docufld.hxx:539
IMPL_LINK_NOARG(SwFieldPage, NumFormatHdl, weld::TreeView &, bool)
Definition: fldpage.cxx:333
bool IsFieldEdit() const
Definition: fldpage.hxx:62
void AppendItem(const SfxPoolItem &)
sal_Int32 m_nTypeSel
Definition: fldpage.hxx:35
virtual OUString GetPar2() const override
Query formula.
Definition: expfld.cxx:1031
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
void InsertHdl(weld::Widget *)
Definition: fldpage.cxx:300
OUString sCommand
Definition: swdbdata.hxx:31
Definition: view.hxx:144
sal_uInt16 nPos
bool m_bDetectedRangeSegmentation false