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