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