1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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>
34 #include "flddinf.hxx"
37 #include "fldfunc.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
, u
"modules/swriter/ui/fielddialog.ui"_ustr
, u
"FieldDialog"_ustr
)
55 , m_bDataBaseMode(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(u
"document"_ustr
, SwFieldDokPage::Create
, nullptr);
64 AddTabPage(u
"variables"_ustr
, SwFieldVarPage::Create
, nullptr);
65 AddTabPage(u
"docinfo"_ustr
, SwFieldDokInfPage::Create
, nullptr);
69 AddTabPage(u
"ref"_ustr
, SwFieldRefPage::Create
, nullptr);
70 AddTabPage(u
"functions"_ustr
, SwFieldFuncPage::Create
, nullptr);
72 utl::OConfigurationTreeRoot aCfgRoot
73 = utl::OConfigurationTreeRoot::createWithComponentContext(
74 ::comphelper::getProcessComponentContext(),
75 u
"/org.openoffice.Office.DataAccess/Policies/Features/Writer"_ustr
,
77 utl::OConfigurationTreeRoot::CM_READONLY
);
79 #if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
80 bool bDatabaseFields
= true;
81 aCfgRoot
.getNodeValue(u
"DatabaseFields"_ustr
) >>= bDatabaseFields
;
84 AddTabPage(u
"database"_ustr
, SwFieldDBPage::Create
, nullptr);
87 RemoveTabPage(u
"database"_ustr
);
91 RemoveTabPage(u
"ref"_ustr
);
92 RemoveTabPage(u
"functions"_ustr
);
93 RemoveTabPage(u
"database"_ustr
);
96 if (comphelper::LibreOfficeKit::isActive())
97 RemoveTabPage(u
"database"_ustr
);
100 SwFieldDlg::~SwFieldDlg()
104 void SwFieldDlg::EndDialog(int nResponse
)
107 SfxTabDialogController::EndDialog(nResponse
);
111 void SwFieldDlg::Close()
115 const SfxPoolItemHolder
aResult(m_pBindings
->GetDispatcher()->
116 Execute(m_bDataBaseMode
? FN_INSERT_FIELD_DATA_ONLY
: FN_INSERT_FIELD
,
117 SfxCallMode::SYNCHRON
|SfxCallMode::RECORD
));
120 // If Execute action did fail for whatever reason, this means that request
121 // to close did fail or wasn't delivered to SwTextShell::ExecField().
122 // Just explicitly close dialog in this case.
123 SfxTabDialogController::EndDialog(RET_CLOSE
);
127 void SwFieldDlg::Initialize(SfxChildWinInfo
const *pInfo
)
129 OUString aWinState
= pInfo
->aWinState
;
130 if (aWinState
.isEmpty())
132 m_xDialog
->set_window_state(aWinState
);
135 SfxItemSet
* SwFieldDlg::CreateInputItemSet(const OUString
& rID
)
137 SwDocShell
*const pDocSh(static_cast<SwDocShell
*>(SfxObjectShell::Current()));
138 if (rID
== "docinfo" && pDocSh
) // might not have a shell if the dialog is restored on startup
140 mxInputItemSet
= std::make_unique
<SfxItemSetFixed
<FN_FIELD_DIALOG_DOC_PROPS
, FN_FIELD_DIALOG_DOC_PROPS
>>( pDocSh
->GetPool() );
141 using namespace ::com::sun::star
;
142 uno::Reference
<document::XDocumentPropertiesSupplier
> xDPS(
143 pDocSh
->GetModel(), uno::UNO_QUERY_THROW
);
144 uno::Reference
<document::XDocumentProperties
> xDocProps
145 = xDPS
->getDocumentProperties();
146 uno::Reference
< beans::XPropertySet
> xUDProps(
147 xDocProps
->getUserDefinedProperties(),
148 uno::UNO_QUERY_THROW
);
149 mxInputItemSet
->Put( SfxUnoAnyItem( FN_FIELD_DIALOG_DOC_PROPS
, uno::Any(xUDProps
) ) );
150 return mxInputItemSet
.get();
156 // kick off inserting of new fields
157 IMPL_LINK_NOARG(SwFieldDlg
, OKHdl
, weld::Button
&, void)
159 if (GetOKButton().get_sensitive())
161 SfxTabPage
* pPage
= GetTabPage(GetCurPageId());
163 pPage
->FillItemSet(nullptr);
165 GetOKButton().grab_focus(); // because of InputField-Dlg
169 IMPL_LINK_NOARG(SwFieldDlg
, CancelHdl
, weld::Button
&, void)
174 // newly initialise dialog after Doc-Switch
175 void SwFieldDlg::ReInitDlg()
177 SwDocShell
* pDocSh
= static_cast<SwDocShell
*>(SfxObjectShell::Current());
178 bool bNewMode
= (::GetHtmlMode(pDocSh
) & HTMLMODE_ON
) != 0;
180 if (bNewMode
!= m_bHtmlMode
)
182 if (SfxViewFrame
* pViewFrm
= SfxViewFrame::Current())
184 pViewFrm
->GetDispatcher()->
185 Execute(FN_INSERT_FIELD
, SfxCallMode::ASYNCHRON
|SfxCallMode::RECORD
);
190 SwView
* pActiveView
= ::GetActiveView();
193 const SwWrtShell
& rSh
= pActiveView
->GetWrtShell();
194 GetOKButton().set_sensitive(( !rSh
.IsReadOnlyAvailable()
195 || !rSh
.HasReadonlySel())
196 && !SwCursorShell::PosInsideInputField(*rSh
.GetCursor()->GetPoint()));
198 ReInitTabPage(u
"document");
199 ReInitTabPage(u
"variables");
200 ReInitTabPage(u
"docinfo");
204 ReInitTabPage(u
"ref");
205 ReInitTabPage(u
"functions");
206 ReInitTabPage(u
"database");
209 m_pChildWin
->SetOldDocShell(pDocSh
);
212 // newly initialise TabPage after Doc-Switch
213 void SwFieldDlg::ReInitTabPage(std::u16string_view rPageId
, bool bOnlyActivate
)
215 SwFieldPage
* pPage
= static_cast<SwFieldPage
*>(GetTabPage(rPageId
));
217 pPage
->EditNewField( bOnlyActivate
); // newly initialise TabPage
220 // newly initialise after activation of a few TabPages
221 void SwFieldDlg::Activate()
223 SwView
* pView
= GetActiveView();
227 bool bHtmlMode
= (::GetHtmlMode(static_cast<SwDocShell
*>(SfxObjectShell::Current())) & HTMLMODE_ON
) != 0;
228 const SwWrtShell
& rSh
= pView
->GetWrtShell();
229 GetOKButton().set_sensitive(( !rSh
.IsReadOnlyAvailable()
230 || !rSh
.HasReadonlySel())
231 && !SwCursorShell::PosInsideInputField(*rSh
.GetCursor()->GetPoint()));
234 ReInitTabPage(u
"variables", true);
238 ReInitTabPage(u
"ref", true);
239 ReInitTabPage(u
"functions", true);
243 void SwFieldDlg::ActivatePage(const OUString
& rPage
)
245 SfxTabDialogController::ActivatePage(rPage
);
246 if (SfxTabPage
* pPage
= GetTabPage(rPage
))
250 void SwFieldDlg::EnableInsert(bool bEnable
)
254 SwView
* pView
= ::GetActiveView();
256 (pView
->GetWrtShell().IsReadOnlyAvailable() &&
257 pView
->GetWrtShell().HasReadonlySel())
258 || SwCursorShell::PosInsideInputField(*pView
->GetWrtShell().GetCursor()->GetPoint()))
263 GetOKButton().set_sensitive(bEnable
);
266 void SwFieldDlg::InsertHdl()
268 GetOKButton().clicked();
271 void SwFieldDlg::ActivateDatabasePage()
273 #if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
274 m_bDataBaseMode
= true;
275 ShowPage(u
"database"_ustr
);
276 SfxTabPage
* pDBPage
= GetTabPage(u
"database");
279 static_cast<SwFieldDBPage
*>(pDBPage
)->ActivateMailMergeAddress();
281 //remove all other pages
282 RemoveTabPage(u
"document"_ustr
);
283 RemoveTabPage(u
"variables"_ustr
);
284 RemoveTabPage(u
"docinfo"_ustr
);
285 RemoveTabPage(u
"ref"_ustr
);
286 RemoveTabPage(u
"functions"_ustr
);
290 void SwFieldDlg::ShowReferencePage()
292 ShowPage(u
"ref"_ustr
);
295 void SwFieldDlg::PageCreated(const OUString
& rId
, SfxTabPage
& rPage
)
297 #if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
298 if (rId
!= "database")
301 SfxDispatcher
* pDispatch
= m_pBindings
->GetDispatcher();
302 SfxViewFrame
* pViewFrame
= pDispatch
? pDispatch
->GetFrame() : nullptr;
305 SfxViewShell
* pViewShell
= SfxViewShell::GetFirst( true, checkSfxViewShell
<SwView
> );
306 while(pViewShell
&& &pViewShell
->GetViewFrame() != pViewFrame
)
308 pViewShell
= SfxViewShell::GetNext( *pViewShell
, true, checkSfxViewShell
<SwView
> );
311 static_cast<SwFieldDBPage
&>(rPage
).SetWrtShell(static_cast<SwView
*>(pViewShell
)->GetWrtShell());
319 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */