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 <swtypes.hxx>
24 #include <unotools/confignode.hxx>
26 #include <sfx2/basedlgs.hxx>
27 #include <sfx2/dispatch.hxx>
28 #include <sfx2/htmlmode.hxx>
29 #include <sfx2/viewfrm.hxx>
30 #include <viewopt.hxx>
31 #include <docsh.hxx>
32 #include <fldwrap.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 
44 #include <globals.hrc>
45 
46 #include <com/sun/star/beans/XPropertySet.hpp>
47 #include <com/sun/star/document/XDocumentProperties.hpp>
48 #include <com/sun/star/document/XDocumentPropertiesSupplier.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
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 }
97 
99 {
100 }
101 
103 {
104  m_bClosing = true;
106  m_bClosing = false;
107 }
108 
110 {
111  if (m_bClosing)
112  return;
115  SfxCallMode::SYNCHRON|SfxCallMode::RECORD);
116 }
117 
119 {
120  OString aWinState = pInfo->aWinState;
121  if (aWinState.isEmpty())
122  return;
123  m_xDialog->set_window_state(aWinState);
124 }
125 
127 {
128  SwDocShell *const pDocSh(static_cast<SwDocShell*>(SfxObjectShell::Current()));
129  if (rID == "docinfo" && pDocSh) // might not have a shell if the dialog is restored on startup
130  {
132  using namespace ::com::sun::star;
133  uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
134  pDocSh->GetModel(), uno::UNO_QUERY_THROW);
135  uno::Reference<document::XDocumentProperties> xDocProps
136  = xDPS->getDocumentProperties();
137  uno::Reference< beans::XPropertySet > xUDProps(
138  xDocProps->getUserDefinedProperties(),
139  uno::UNO_QUERY_THROW);
140  pISet->Put( SfxUnoAnyItem( SID_DOCINFO, uno::makeAny(xUDProps) ) );
141  return pISet;
142  }
143  else
144  return nullptr;
145 }
146 
147 // kick off inserting of new fields
149 {
150  if (GetOKButton().get_sensitive())
151  {
152  SfxTabPage* pPage = GetTabPage(GetCurPageId());
153  assert(pPage);
154  pPage->FillItemSet(nullptr);
155 
156  GetOKButton().grab_focus(); // because of InputField-Dlg
157  }
158 }
159 
161 {
162  Close();
163 }
164 
165 // newly initialise dialog after Doc-Switch
167 {
168  SwDocShell* pDocSh = static_cast<SwDocShell*>(SfxObjectShell::Current());
169  bool bNewMode = (::GetHtmlMode(pDocSh) & HTMLMODE_ON) != 0;
170 
171  if (bNewMode != m_bHtmlMode)
172  {
174  Execute(FN_INSERT_FIELD, SfxCallMode::ASYNCHRON|SfxCallMode::RECORD);
175  Close();
176  }
177 
178  SwView* pActiveView = ::GetActiveView();
179  if(!pActiveView)
180  return;
181  const SwWrtShell& rSh = pActiveView->GetWrtShell();
183  !rSh.HasReadonlySel());
184 
185  ReInitTabPage("document");
186  ReInitTabPage("variables");
187  ReInitTabPage("docinfo");
188 
189  if (!m_bHtmlMode)
190  {
191  ReInitTabPage("ref");
192  ReInitTabPage("functions");
193  ReInitTabPage("database");
194  }
195 
196  m_pChildWin->SetOldDocShell(pDocSh);
197 }
198 
199 // newly initialise TabPage after Doc-Switch
200 void SwFieldDlg::ReInitTabPage(const OString& rPageId, bool bOnlyActivate)
201 {
202  SwFieldPage* pPage = static_cast<SwFieldPage*>(GetTabPage(rPageId));
203  if (pPage)
204  pPage->EditNewField( bOnlyActivate ); // newly initialise TabPage
205 }
206 
207 // newly initialise after activation of a few TabPages
209 {
210  SwView* pView = ::GetActiveView();
211  if( pView )
212  {
213  bool bHtmlMode = (::GetHtmlMode(static_cast<SwDocShell*>(SfxObjectShell::Current())) & HTMLMODE_ON) != 0;
214  const SwWrtShell& rSh = pView->GetWrtShell();
216  !rSh.HasReadonlySel());
217 
218  ReInitTabPage("variables", true);
219 
220  if( !bHtmlMode )
221  {
222  ReInitTabPage("ref", true);
223  ReInitTabPage("functions", true);
224  }
225  }
226 }
227 
228 void SwFieldDlg::EnableInsert(bool bEnable)
229 {
230  if( bEnable )
231  {
232  SwView* pView = ::GetActiveView();
233  OSL_ENSURE(pView, "no view found");
234  if( !pView ||
235  (pView->GetWrtShell().IsReadOnlyAvailable() &&
236  pView->GetWrtShell().HasReadonlySel()) )
237  bEnable = false;
238  }
239  GetOKButton().set_sensitive(bEnable);
240 }
241 
243 {
244  GetOKButton().clicked();
245 }
246 
248 {
249 #if HAVE_FEATURE_DBCONNECTIVITY
250  m_bDataBaseMode = true;
251  ShowPage("database");
252  SfxTabPage* pDBPage = GetTabPage("database");
253  if( pDBPage )
254  {
255  static_cast<SwFieldDBPage*>(pDBPage)->ActivateMailMergeAddress();
256  }
257  //remove all other pages
258  RemoveTabPage("document");
259  RemoveTabPage("variables");
260  RemoveTabPage("docinfo");
261  RemoveTabPage("ref");
262  RemoveTabPage("functions");
263 #endif
264 }
265 
267 {
268  ShowPage("ref");
269 }
270 
271 void SwFieldDlg::PageCreated(const OString& rId, SfxTabPage& rPage)
272 {
273 #if HAVE_FEATURE_DBCONNECTIVITY
274  if (rId == "database")
275  {
276  SfxDispatcher* pDispatch = m_pBindings->GetDispatcher();
277  SfxViewFrame* pViewFrame = pDispatch ? pDispatch->GetFrame() : nullptr;
278  if(pViewFrame)
279  {
280  SfxViewShell* pViewShell = SfxViewShell::GetFirst( true, checkSfxViewShell<SwView> );
281  while(pViewShell && pViewShell->GetViewFrame() != pViewFrame)
282  {
283  pViewShell = SfxViewShell::GetNext( *pViewShell, true, checkSfxViewShell<SwView> );
284  }
285  if(pViewShell)
286  static_cast<SwFieldDBPage&>(rPage).SetWrtShell(static_cast<SwView*>(pViewShell)->GetWrtShell());
287  }
288  }
289 #else
290  (void) rId;
291  (void) rPage;
292 #endif
293 }
294 
295 /* 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:126
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: flddok.cxx:611
SwChildWinWrapper * m_pChildWin
Definition: fldtdlg.hxx:30
SwView * GetActiveView()
Definition: swmodul1.cxx:123
virtual void Close() override
Definition: fldtdlg.cxx:109
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:242
SwWrtShell & GetWrtShell() const
Definition: view.hxx:400
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:1207
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:204
void EnableInsert(bool bEnable)
Definition: fldtdlg.cxx:228
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:148
void ShowPage(const OString &rName)
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: fldfunc.cxx:579
HTMLMODE_ON
sal_uInt16 GetHtmlMode(const SwDocShell *pShell)
Definition: viewopt.cxx:340
virtual void EndDialog() override
Definition: fldtdlg.cxx:102
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: flddb.cxx:258
bool HasReadonlySel() const
Definition: crsrsh.cxx:3312
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:106
bool IsReadOnlyAvailable() const
Definition: crsrsh.hxx:478
void Initialize(SfxChildWinInfo const *pInfo)
Definition: fldtdlg.cxx:118
static SfxViewFrame * Current()
#define FN_INSERT_FIELD
Definition: cmdid.h:192
void ReInitDlg()
Definition: fldtdlg.cxx:166
void ShowReferencePage()
Definition: fldtdlg.cxx:266
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: flddinf.cxx:446
void clicked()
bool m_bDataBaseMode
Definition: fldtdlg.hxx:33
SfxViewFrame * GetFrame() const
void AddTabPage(const OString &rName, CreateTabPage pCreateFunc, GetTabPageRanges pRangesFunc)
css::uno::Any getNodeValue(const OUString &_rPath) const
Reference< XComponentContext > getProcessComponentContext()
SwFieldDlg(SfxBindings *pB, SwChildWinWrapper *pCW, weld::Window *pParent)
Definition: fldtdlg.cxx:51
virtual weld::Button & GetOKButton() const override
SfxDispatcher * GetDispatcher() const
virtual ~SwFieldDlg() override
Definition: fldtdlg.cxx:98
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: fldref.cxx:1101
void ReInitTabPage(const OString &rPageId, bool bOnlyActivate=false)
Definition: fldtdlg.cxx:200
virtual void PageCreated(const OString &rId, SfxTabPage &rPage) override
Definition: fldtdlg.cxx:271
Definition: view.hxx:146
virtual void set_sensitive(bool sensitive)=0
void ActivateDatabasePage()
Definition: fldtdlg.cxx:247
virtual void Activate() override
Definition: fldtdlg.cxx:208
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo