Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / ui / fldui / fldtdlg.cxx
blob93e0e1f761af3cb50dd5506fc5c72502773d1acc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <config_features.h>
21 #include <config_fuzzers.h>
23 #include <cmdid.h>
24 #include <unotools/confignode.hxx>
25 #include <comphelper/processfactory.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>
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>
50 // carrier of the dialog
51 SwFieldDlg::SwFieldDlg(SfxBindings* pB, SwChildWinWrapper* pCW, weld::Window *pParent)
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)
58 m_bHtmlMode = (::GetHtmlMode(static_cast<SwDocShell*>(SfxObjectShell::Current())) & HTMLMODE_ON) != 0;
60 GetCancelButton().connect_clicked(LINK(this, SwFieldDlg, CancelHdl));
61 GetOKButton().connect_clicked(LINK(this, SwFieldDlg, OKHdl));
63 AddTabPage("document", SwFieldDokPage::Create, nullptr);
64 AddTabPage("variables", SwFieldVarPage::Create, nullptr);
65 AddTabPage("docinfo", SwFieldDokInfPage::Create, nullptr);
67 if (!m_bHtmlMode)
69 AddTabPage("ref", SwFieldRefPage::Create, nullptr);
70 AddTabPage("functions", SwFieldFuncPage::Create, nullptr);
72 utl::OConfigurationTreeRoot aCfgRoot
73 = utl::OConfigurationTreeRoot::createWithComponentContext(
74 ::comphelper::getProcessComponentContext(),
75 "/org.openoffice.Office.DataAccess/Policies/Features/Writer",
76 -1,
77 utl::OConfigurationTreeRoot::CM_READONLY);
79 #if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
80 bool bDatabaseFields = true;
81 aCfgRoot.getNodeValue(
82 OUString("DatabaseFields")) >>= bDatabaseFields;
84 if (bDatabaseFields)
85 AddTabPage("database", SwFieldDBPage::Create, nullptr);
86 else
87 #endif
88 RemoveTabPage("database");
90 else
92 RemoveTabPage("ref");
93 RemoveTabPage("functions");
94 RemoveTabPage("database");
97 if (comphelper::LibreOfficeKit::isActive())
98 RemoveTabPage("database");
101 SwFieldDlg::~SwFieldDlg()
105 void SwFieldDlg::EndDialog(int nResponse)
107 m_bClosing = true;
108 SfxTabDialogController::EndDialog(nResponse);
109 m_bClosing = false;
112 void SwFieldDlg::Close()
114 if (m_bClosing)
115 return;
116 const SfxPoolItem* pResult = m_pBindings->GetDispatcher()->
117 Execute(m_bDataBaseMode ? FN_INSERT_FIELD_DATA_ONLY : FN_INSERT_FIELD,
118 SfxCallMode::SYNCHRON|SfxCallMode::RECORD);
119 if (!pResult)
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.
124 SfxTabDialogController::EndDialog(RET_CLOSE);
128 void SwFieldDlg::Initialize(SfxChildWinInfo const *pInfo)
130 OUString aWinState = pInfo->aWinState;
131 if (aWinState.isEmpty())
132 return;
133 m_xDialog->set_window_state(aWinState);
136 SfxItemSet* SwFieldDlg::CreateInputItemSet(const OUString& rID)
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
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);
150 mxInputItemSet->Put( SfxUnoAnyItem( FN_FIELD_DIALOG_DOC_PROPS, uno::Any(xUDProps) ) );
151 return mxInputItemSet.get();
153 else
154 return nullptr;
157 // kick off inserting of new fields
158 IMPL_LINK_NOARG(SwFieldDlg, OKHdl, weld::Button&, void)
160 if (GetOKButton().get_sensitive())
162 SfxTabPage* pPage = GetTabPage(GetCurPageId());
163 assert(pPage);
164 pPage->FillItemSet(nullptr);
166 GetOKButton().grab_focus(); // because of InputField-Dlg
170 IMPL_LINK_NOARG(SwFieldDlg, CancelHdl, weld::Button&, void)
172 Close();
175 // newly initialise dialog after Doc-Switch
176 void SwFieldDlg::ReInitDlg()
178 SwDocShell* pDocSh = static_cast<SwDocShell*>(SfxObjectShell::Current());
179 bool bNewMode = (::GetHtmlMode(pDocSh) & HTMLMODE_ON) != 0;
181 if (bNewMode != m_bHtmlMode)
183 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
185 pViewFrm->GetDispatcher()->
186 Execute(FN_INSERT_FIELD, SfxCallMode::ASYNCHRON|SfxCallMode::RECORD);
188 Close();
191 SwView* pActiveView = ::GetActiveView();
192 if(!pActiveView)
193 return;
194 const SwWrtShell& rSh = pActiveView->GetWrtShell();
195 GetOKButton().set_sensitive(( !rSh.IsReadOnlyAvailable()
196 || !rSh.HasReadonlySel())
197 && !SwCursorShell::PosInsideInputField(*rSh.GetCursor()->GetPoint()));
199 ReInitTabPage(u"document");
200 ReInitTabPage(u"variables");
201 ReInitTabPage(u"docinfo");
203 if (!m_bHtmlMode)
205 ReInitTabPage(u"ref");
206 ReInitTabPage(u"functions");
207 ReInitTabPage(u"database");
210 m_pChildWin->SetOldDocShell(pDocSh);
213 // newly initialise TabPage after Doc-Switch
214 void SwFieldDlg::ReInitTabPage(std::u16string_view rPageId, bool bOnlyActivate)
216 SwFieldPage* pPage = static_cast<SwFieldPage*>(GetTabPage(rPageId));
217 if (pPage)
218 pPage->EditNewField( bOnlyActivate ); // newly initialise TabPage
221 // newly initialise after activation of a few TabPages
222 void SwFieldDlg::Activate()
224 SwView* pView = GetActiveView();
225 if( !pView )
226 return;
228 bool bHtmlMode = (::GetHtmlMode(static_cast<SwDocShell*>(SfxObjectShell::Current())) & HTMLMODE_ON) != 0;
229 const SwWrtShell& rSh = pView->GetWrtShell();
230 GetOKButton().set_sensitive(( !rSh.IsReadOnlyAvailable()
231 || !rSh.HasReadonlySel())
232 && !SwCursorShell::PosInsideInputField(*rSh.GetCursor()->GetPoint()));
235 ReInitTabPage(u"variables", true);
237 if( !bHtmlMode )
239 ReInitTabPage(u"ref", true);
240 ReInitTabPage(u"functions", true);
244 void SwFieldDlg::EnableInsert(bool bEnable)
246 if( bEnable )
248 SwView* pView = ::GetActiveView();
249 if( !pView ||
250 (pView->GetWrtShell().IsReadOnlyAvailable() &&
251 pView->GetWrtShell().HasReadonlySel())
252 || SwCursorShell::PosInsideInputField(*pView->GetWrtShell().GetCursor()->GetPoint()))
254 bEnable = false;
257 GetOKButton().set_sensitive(bEnable);
260 void SwFieldDlg::InsertHdl()
262 GetOKButton().clicked();
265 void SwFieldDlg::ActivateDatabasePage()
267 #if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
268 m_bDataBaseMode = true;
269 ShowPage("database");
270 SfxTabPage* pDBPage = GetTabPage(u"database");
271 if( pDBPage )
273 static_cast<SwFieldDBPage*>(pDBPage)->ActivateMailMergeAddress();
275 //remove all other pages
276 RemoveTabPage("document");
277 RemoveTabPage("variables");
278 RemoveTabPage("docinfo");
279 RemoveTabPage("ref");
280 RemoveTabPage("functions");
281 #endif
284 void SwFieldDlg::ShowReferencePage()
286 ShowPage("ref");
289 void SwFieldDlg::PageCreated(const OUString& rId, SfxTabPage& rPage)
291 #if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
292 if (rId != "database")
293 return;
295 SfxDispatcher* pDispatch = m_pBindings->GetDispatcher();
296 SfxViewFrame* pViewFrame = pDispatch ? pDispatch->GetFrame() : nullptr;
297 if(pViewFrame)
299 SfxViewShell* pViewShell = SfxViewShell::GetFirst( true, checkSfxViewShell<SwView> );
300 while(pViewShell && &pViewShell->GetViewFrame() != pViewFrame)
302 pViewShell = SfxViewShell::GetNext( *pViewShell, true, checkSfxViewShell<SwView> );
304 if(pViewShell)
305 static_cast<SwFieldDBPage&>(rPage).SetWrtShell(static_cast<SwView*>(pViewShell)->GetWrtShell());
307 #else
308 (void) rId;
309 (void) rPage;
310 #endif
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */