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