LibreOffice Module sw (master)  1
fldtdlg.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 <config_features.h>
21 #include <config_fuzzers.h>
22 
23 #include <cmdid.h>
24 #include <unotools/confignode.hxx>
26 #include <sfx2/dispatch.hxx>
27 #include <sfx2/htmlmode.hxx>
28 #include <sfx2/viewfrm.hxx>
29 #include <osl/diagnose.h>
30 #include <viewopt.hxx>
31 #include <chldwrap.hxx>
32 #include <docsh.hxx>
33 #include "flddb.hxx"
34 #include "flddinf.hxx"
35 #include "fldvar.hxx"
36 #include "flddok.hxx"
37 #include "fldfunc.hxx"
38 #include "fldref.hxx"
39 #include <wrtsh.hxx>
40 #include <view.hxx>
41 #include <fldtdlg.hxx>
42 #include <swmodule.hxx>
43 #include <comphelper/lok.hxx>
44 
45 #include <com/sun/star/beans/XPropertySet.hpp>
46 #include <com/sun/star/document/XDocumentProperties.hpp>
47 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
48 #include <com/sun/star/frame/XModel.hpp>
49 
50 // carrier of the dialog
52  : SfxTabDialogController(pParent, "modules/swriter/ui/fielddialog.ui", "FieldDialog")
53  , m_pChildWin(pCW)
54  , m_pBindings(pB)
55  , m_bDataBaseMode(false)
56  , m_bClosing(false)
57 {
58  m_bHtmlMode = (::GetHtmlMode(static_cast<SwDocShell*>(SfxObjectShell::Current())) & HTMLMODE_ON) != 0;
59 
60  GetCancelButton().connect_clicked(LINK(this, SwFieldDlg, CancelHdl));
61  GetOKButton().connect_clicked(LINK(this, SwFieldDlg, OKHdl));
62 
63  AddTabPage("document", SwFieldDokPage::Create, nullptr);
64  AddTabPage("variables", SwFieldVarPage::Create, nullptr);
65  AddTabPage("docinfo", SwFieldDokInfPage::Create, nullptr);
66 
67  if (!m_bHtmlMode)
68  {
69  AddTabPage("ref", SwFieldRefPage::Create, nullptr);
70  AddTabPage("functions", SwFieldFuncPage::Create, nullptr);
71 
75  "/org.openoffice.Office.DataAccess/Policies/Features/Writer",
76  -1,
78 
79 #if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
80  bool bDatabaseFields = true;
81  aCfgRoot.getNodeValue(
82  OUString("DatabaseFields")) >>= bDatabaseFields;
83 
84  if (bDatabaseFields)
85  AddTabPage("database", SwFieldDBPage::Create, nullptr);
86  else
87 #endif
88  RemoveTabPage("database");
89  }
90  else
91  {
92  RemoveTabPage("ref");
93  RemoveTabPage("functions");
94  RemoveTabPage("database");
95  }
96 
98  RemoveTabPage("database");
99 }
100 
102 {
103 }
104 
105 void SwFieldDlg::EndDialog(int nResponse)
106 {
107  m_bClosing = true;
109  m_bClosing = false;
110 }
111 
113 {
114  if (m_bClosing)
115  return;
116  const SfxPoolItem* pResult = m_pBindings->GetDispatcher()->
118  SfxCallMode::SYNCHRON|SfxCallMode::RECORD);
119  if (!pResult)
120  {
121  // If Execute action did fail for whatever reason, this means that request
122  // to close did fail or wasn't delivered to SwTextShell::ExecField().
123  // Just explicitly close dialog in this case.
125  }
126 }
127 
129 {
130  OString aWinState = pInfo->aWinState;
131  if (aWinState.isEmpty())
132  return;
133  m_xDialog->set_window_state(aWinState);
134 }
135 
137 {
138  SwDocShell *const pDocSh(static_cast<SwDocShell*>(SfxObjectShell::Current()));
139  if (rID == "docinfo" && pDocSh) // might not have a shell if the dialog is restored on startup
140  {
141  mxInputItemSet = std::make_unique<SfxItemSetFixed<FN_FIELD_DIALOG_DOC_PROPS, FN_FIELD_DIALOG_DOC_PROPS>>( pDocSh->GetPool() );
142  using namespace ::com::sun::star;
143  uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
144  pDocSh->GetModel(), uno::UNO_QUERY_THROW);
145  uno::Reference<document::XDocumentProperties> xDocProps
146  = xDPS->getDocumentProperties();
147  uno::Reference< beans::XPropertySet > xUDProps(
148  xDocProps->getUserDefinedProperties(),
149  uno::UNO_QUERY_THROW);
151  return mxInputItemSet.get();
152  }
153  else
154  return nullptr;
155 }
156 
157 // kick off inserting of new fields
159 {
160  if (GetOKButton().get_sensitive())
161  {
162  SfxTabPage* pPage = GetTabPage(GetCurPageId());
163  assert(pPage);
164  pPage->FillItemSet(nullptr);
165 
166  GetOKButton().grab_focus(); // because of InputField-Dlg
167  }
168 }
169 
171 {
172  Close();
173 }
174 
175 // newly initialise dialog after Doc-Switch
177 {
178  SwDocShell* pDocSh = static_cast<SwDocShell*>(SfxObjectShell::Current());
179  bool bNewMode = (::GetHtmlMode(pDocSh) & HTMLMODE_ON) != 0;
180 
181  if (bNewMode != m_bHtmlMode)
182  {
184  Execute(FN_INSERT_FIELD, SfxCallMode::ASYNCHRON|SfxCallMode::RECORD);
185  Close();
186  }
187 
188  SwView* pActiveView = ::GetActiveView();
189  if(!pActiveView)
190  return;
191  const SwWrtShell& rSh = pActiveView->GetWrtShell();
193  !rSh.HasReadonlySel());
194 
195  ReInitTabPage("document");
196  ReInitTabPage("variables");
197  ReInitTabPage("docinfo");
198 
199  if (!m_bHtmlMode)
200  {
201  ReInitTabPage("ref");
202  ReInitTabPage("functions");
203  ReInitTabPage("database");
204  }
205 
206  m_pChildWin->SetOldDocShell(pDocSh);
207 }
208 
209 // newly initialise TabPage after Doc-Switch
210 void SwFieldDlg::ReInitTabPage(std::string_view rPageId, bool bOnlyActivate)
211 {
212  SwFieldPage* pPage = static_cast<SwFieldPage*>(GetTabPage(rPageId));
213  if (pPage)
214  pPage->EditNewField( bOnlyActivate ); // newly initialise TabPage
215 }
216 
217 // newly initialise after activation of a few TabPages
219 {
220  SwView* pView = ::GetActiveView();
221  if( !pView )
222  return;
223 
224  bool bHtmlMode = (::GetHtmlMode(static_cast<SwDocShell*>(SfxObjectShell::Current())) & HTMLMODE_ON) != 0;
225  const SwWrtShell& rSh = pView->GetWrtShell();
227  !rSh.HasReadonlySel());
228 
229  ReInitTabPage("variables", true);
230 
231  if( !bHtmlMode )
232  {
233  ReInitTabPage("ref", true);
234  ReInitTabPage("functions", true);
235  }
236 }
237 
238 void SwFieldDlg::EnableInsert(bool bEnable)
239 {
240  if( bEnable )
241  {
242  SwView* pView = ::GetActiveView();
243  OSL_ENSURE(pView, "no view found");
244  if( !pView ||
245  (pView->GetWrtShell().IsReadOnlyAvailable() &&
246  pView->GetWrtShell().HasReadonlySel()) )
247  bEnable = false;
248  }
249  GetOKButton().set_sensitive(bEnable);
250 }
251 
253 {
254  GetOKButton().clicked();
255 }
256 
258 {
259 #if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
260  m_bDataBaseMode = true;
261  ShowPage("database");
262  SfxTabPage* pDBPage = GetTabPage("database");
263  if( pDBPage )
264  {
265  static_cast<SwFieldDBPage*>(pDBPage)->ActivateMailMergeAddress();
266  }
267  //remove all other pages
268  RemoveTabPage("document");
269  RemoveTabPage("variables");
270  RemoveTabPage("docinfo");
271  RemoveTabPage("ref");
272  RemoveTabPage("functions");
273 #endif
274 }
275 
277 {
278  ShowPage("ref");
279 }
280 
281 void SwFieldDlg::PageCreated(const OString& rId, SfxTabPage& rPage)
282 {
283 #if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
284  if (rId != "database")
285  return;
286 
287  SfxDispatcher* pDispatch = m_pBindings->GetDispatcher();
288  SfxViewFrame* pViewFrame = pDispatch ? pDispatch->GetFrame() : nullptr;
289  if(pViewFrame)
290  {
291  SfxViewShell* pViewShell = SfxViewShell::GetFirst( true, checkSfxViewShell<SwView> );
292  while(pViewShell && pViewShell->GetViewFrame() != pViewFrame)
293  {
294  pViewShell = SfxViewShell::GetNext( *pViewShell, true, checkSfxViewShell<SwView> );
295  }
296  if(pViewShell)
297  static_cast<SwFieldDBPage&>(rPage).SetWrtShell(static_cast<SwView*>(pViewShell)->GetWrtShell());
298  }
299 #else
300  (void) rId;
301  (void) rPage;
302 #endif
303 }
304 
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxViewFrame * GetViewFrame() const
bool m_bHtmlMode
Definition: fldtdlg.hxx:37
virtual SfxItemSet * CreateInputItemSet(const OString &rId) override
Definition: fldtdlg.cxx:136
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: flddok.cxx:622
SwChildWinWrapper * m_pChildWin
Definition: fldtdlg.hxx:35
SwView * GetActiveView()
Definition: swmodul1.cxx:116
virtual void Close() override
Definition: fldtdlg.cxx:112
SfxDispatcher * GetDispatcher()
weld::Button & GetCancelButton() const
css::uno::Reference< css::frame::XModel3 > GetModel() const
const ::avmedia::MediaItem * Execute(const SdrMarkView *pSdrView, SfxRequest const &rReq)
void RemoveTabPage(const OString &rName)
static SfxObjectShell * Current()
void InsertHdl()
Definition: fldtdlg.cxx:252
SwWrtShell & GetWrtShell() const
Definition: view.hxx:413
Used by the UI to modify the document model.
Definition: wrtsh.hxx:96
virtual bool FillItemSet(SfxItemSet *)
static OConfigurationTreeRoot createWithComponentContext(const css::uno::Reference< css::uno::XComponentContext > &_rxContext, const OUString &_rPath, sal_Int32 _nDepth=-1, CREATION_MODE _eMode=CM_UPDATABLE)
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: fldvar.cxx:1227
static SfxViewShell * GetNext(const SfxViewShell &rPrev, bool bOnlyVisible=true, const std::function< bool(const SfxViewShell *)> &isViewShell=nullptr)
virtual void EndDialog(int nResponse) override
Definition: fldtdlg.cxx:105
SfxItemPool & GetPool() const
#define FN_INSERT_FIELD_DATA_ONLY
Definition: cmdid.h:219
void EnableInsert(bool bEnable)
Definition: fldtdlg.cxx:238
void connect_clicked(const Link< Button &, void > &rLink)
SfxBindings * m_pBindings
Definition: fldtdlg.hxx:36
IMPL_LINK_NOARG(SwFieldDlg, OKHdl, weld::Button &, void)
Definition: fldtdlg.cxx:158
void ShowPage(const OString &rName)
css::uno::Any getNodeValue(const OUString &_rPath) const noexcept
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: fldfunc.cxx:580
HTMLMODE_ON
sal_uInt16 GetHtmlMode(const SwDocShell *pShell)
Definition: viewopt.cxx:354
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: flddb.cxx:262
bool HasReadonlySel() const
Definition: crsrsh.cxx:3355
static SfxViewShell * GetFirst(bool bOnlyVisible=true, const std::function< bool(const SfxViewShell *)> &isViewShell=nullptr)
void SetOldDocShell(SwDocShell *pDcSh)
Definition: chldwrap.hxx:42
bool m_bClosing
Definition: fldtdlg.hxx:39
void EditNewField(bool bOnlyActivate=false)
Definition: fldpage.cxx:105
RET_CLOSE
bool IsReadOnlyAvailable() const
Definition: crsrsh.hxx:479
void Initialize(SfxChildWinInfo const *pInfo)
Definition: fldtdlg.cxx:128
static SfxViewFrame * Current()
#define FN_INSERT_FIELD
Definition: cmdid.h:207
std::unique_ptr< SfxItemSet > mxInputItemSet
Definition: fldtdlg.hxx:40
void ReInitDlg()
Definition: fldtdlg.cxx:176
void ShowReferencePage()
Definition: fldtdlg.cxx:276
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: flddinf.cxx:465
void clicked()
Close
bool m_bDataBaseMode
Definition: fldtdlg.hxx:38
SfxViewFrame * GetFrame() const
void AddTabPage(const OString &rName, CreateTabPage pCreateFunc, GetTabPageRanges pRangesFunc)
Reference< XExecutableDialog > m_xDialog
virtual void EndDialog(int nResponse)
Reference< XComponentContext > getProcessComponentContext()
SwFieldDlg(SfxBindings *pB, SwChildWinWrapper *pCW, weld::Window *pParent)
Definition: fldtdlg.cxx:51
virtual weld::Button & GetOKButton() const override
void ReInitTabPage(std::string_view rPageId, bool bOnlyActivate=false)
Definition: fldtdlg.cxx:210
SfxDispatcher * GetDispatcher() const
virtual ~SwFieldDlg() override
Definition: fldtdlg.cxx:101
SfxTabPage * GetTabPage(std::string_view rPageId) const
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: fldref.cxx:1127
virtual void PageCreated(const OString &rId, SfxTabPage &rPage) override
Definition: fldtdlg.cxx:281
Definition: view.hxx:144
virtual void set_sensitive(bool sensitive)=0
void ActivateDatabasePage()
Definition: fldtdlg.cxx:257
virtual void Activate() override
Definition: fldtdlg.cxx:218
bool m_bDetectedRangeSegmentation false
#define FN_FIELD_DIALOG_DOC_PROPS
Definition: cmdid.h:639
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo