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