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