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 <svl/stritem.hxx>
21 #include <sfx2/request.hxx>
22 #include <sfx2/htmlmode.hxx>
23 #include <sfx2/viewfrm.hxx>
27 #include <viewopt.hxx>
30 #include <swmodule.hxx>
34 #include <fldtdlg.hxx>
35 #include "fldpage.hxx"
36 #include <docufld.hxx>
38 #include <sfx2/bindings.hxx>
39 #include <o3tl/string_view.hxx>
41 using namespace ::com::sun::star
;
43 // note: pAttrSet may be null if the dialog is restored on startup
44 SwFieldPage::SwFieldPage(weld::Container
* pPage
, weld::DialogController
* pController
, const OUString
& rUIXMLDescription
,
45 const OUString
& rID
, const SfxItemSet
*pAttrSet
)
46 : SfxTabPage(pPage
, pController
, rUIXMLDescription
, rID
, pAttrSet
)
47 , m_pCurField(nullptr)
48 , m_pWrtShell(nullptr)
53 , m_bFieldDlgHtmlMode(false)
55 , m_bFirstHTMLInit(true)
59 SwFieldPage::~SwFieldPage()
64 void SwFieldPage::Init()
66 SwDocShell
* pDocSh
= static_cast<SwDocShell
*>(SfxObjectShell::Current());
67 bool bNewMode
= 0 != (::GetHtmlMode(pDocSh
) & HTMLMODE_ON
);
69 m_bFieldEdit
= nullptr == dynamic_cast<SwFieldDlg
*>(GetDialogController());
71 // newly initialise FieldManager. important for
72 // Dok-Switch (fldtdlg:ReInitTabPage)
73 m_pCurField
= m_aMgr
.GetCurField();
75 if( bNewMode
== m_bFieldDlgHtmlMode
)
78 m_bFieldDlgHtmlMode
= bNewMode
;
80 // initialise Rangelistbox
81 if( !(m_bFieldDlgHtmlMode
&& m_bFirstHTMLInit
) )
84 m_bFirstHTMLInit
= false;
85 SwWrtShell
*pSh
= m_pWrtShell
;
87 pSh
= ::GetActiveWrtShell();
90 SwDoc
* pDoc
= pSh
->GetDoc();
91 pSh
->InsertFieldType( SwSetExpFieldType( pDoc
,
93 pSh
->InsertFieldType( SwSetExpFieldType(pDoc
,
98 // newly initialise page
99 void SwFieldPage::Activate()
101 EnableInsert(m_bInsert
);
104 // complete reset; edit new field
105 void SwFieldPage::EditNewField( bool bOnlyActivate
)
109 m_nSelectionSel
= -1;
116 void SwFieldPage::InsertField(SwFieldTypesEnum nTypeId
, sal_uInt16 nSubType
, const OUString
& rPar1
,
117 const OUString
& rPar2
, sal_uInt32 nFormatId
,
118 sal_Unicode cSeparator
, bool bIsAutomaticLanguage
)
120 SwView
* pView
= GetActiveView();
124 SwWrtShell
*pSh
= m_pWrtShell
? m_pWrtShell
: pView
->GetWrtShellPtr();
128 if (!IsFieldEdit()) // insert new field
130 SwInsertField_Data
aData(nTypeId
, nSubType
, rPar1
, rPar2
, nFormatId
, nullptr, cSeparator
, bIsAutomaticLanguage
);
131 //#i26566# provide parent for SwWrtShell::StartInputFieldDlg
132 aData
.m_pParent
= &GetDialogController()->GetOKButton();
133 m_aMgr
.InsertField( aData
);
135 uno::Reference
< frame::XDispatchRecorder
> xRecorder
=
136 pView
->GetViewFrame().GetBindings().GetRecorder();
137 if ( xRecorder
.is() )
139 bool bRecordDB
= SwFieldTypesEnum::Database
== nTypeId
||
140 SwFieldTypesEnum::DatabaseSetNumber
== nTypeId
||
141 SwFieldTypesEnum::DatabaseNumberSet
== nTypeId
||
142 SwFieldTypesEnum::DatabaseNextSet
== nTypeId
||
143 SwFieldTypesEnum::DatabaseName
== nTypeId
;
145 SfxRequest
aReq(pView
->GetViewFrame(),
146 bRecordDB
? FN_INSERT_DBFIELD
: FN_INSERT_FIELD
);
150 aReq
.AppendItem(SfxStringItem
151 (FN_INSERT_DBFIELD
,rPar1
.getToken(0, DB_DELIM
, nIdx
)));
152 aReq
.AppendItem(SfxStringItem
153 (FN_PARAM_1
,rPar1
.getToken(0, DB_DELIM
, nIdx
)));
154 aReq
.AppendItem(SfxInt32Item
155 (TypedWhichId
<SfxInt32Item
>(FN_PARAM_3
), o3tl::toInt32(o3tl::getToken(rPar1
, 0, DB_DELIM
, nIdx
))));
156 aReq
.AppendItem(SfxStringItem
157 (FN_PARAM_2
,rPar1
.getToken(0, DB_DELIM
, nIdx
)));
161 aReq
.AppendItem(SfxStringItem(FN_INSERT_FIELD
, rPar1
));
162 aReq
.AppendItem(SfxStringItem
163 (FN_PARAM_3
, OUString(cSeparator
)));
164 aReq
.AppendItem(SfxUInt16Item(FN_PARAM_FIELD_SUBTYPE
, nSubType
));
166 aReq
.AppendItem(SfxUInt16Item(FN_PARAM_FIELD_TYPE
, static_cast<sal_uInt16
>(nTypeId
)));
167 aReq
.AppendItem(SfxStringItem(FN_PARAM_FIELD_CONTENT
, rPar2
));
168 aReq
.AppendItem(SfxUInt32Item(FN_PARAM_FIELD_FORMAT
, nFormatId
));
175 std::unique_ptr
<SwField
> pTmpField
= m_pCurField
->CopyField();
177 OUString
sPar1(rPar1
);
178 OUString
sPar2(rPar2
);
181 case SwFieldTypesEnum::Date
:
182 case SwFieldTypesEnum::Time
:
183 nSubType
= static_cast< sal_uInt16
>(((nTypeId
== SwFieldTypesEnum::Date
) ? DATEFLD
: TIMEFLD
) |
184 ((nSubType
== DATE_VAR
) ? 0 : FIXEDFLD
));
187 case SwFieldTypesEnum::DatabaseName
:
188 case SwFieldTypesEnum::DatabaseNextSet
:
189 case SwFieldTypesEnum::DatabaseNumberSet
:
190 case SwFieldTypesEnum::DatabaseSetNumber
:
195 aData
.sDataSource
= rPar1
.getToken(0, DB_DELIM
, nPos
);
196 aData
.sCommand
= rPar1
.getToken(0, DB_DELIM
, nPos
);
197 aData
.nCommandType
= o3tl::toInt32(o3tl::getToken(rPar1
, 0, DB_DELIM
, nPos
));
198 sPar1
= rPar1
.copy(nPos
);
200 static_cast<SwDBNameInfField
*>(pTmpField
.get())->SetDBData(aData
);
204 case SwFieldTypesEnum::Database
:
208 aData
.sDataSource
= rPar1
.getToken(0, DB_DELIM
, nIdx
);
209 aData
.sCommand
= rPar1
.getToken(0, DB_DELIM
, nIdx
);
210 aData
.nCommandType
= o3tl::toInt32(o3tl::getToken(rPar1
, 0, DB_DELIM
, nIdx
));
211 OUString sColumn
= rPar1
.getToken(0, DB_DELIM
, nIdx
);
213 auto pOldType
= static_cast<SwDBFieldType
*>(pTmpField
->GetTyp());
214 auto pType
= static_cast<SwDBFieldType
*>(pSh
->InsertFieldType(SwDBFieldType(pSh
->GetDoc(), sColumn
, aData
)));
215 if(auto pFormatField
= pOldType
->FindFormatForField(m_pCurField
))
217 pFormatField
->RegisterToFieldType(*pType
);
218 pTmpField
->ChgTyp(pType
);
223 case SwFieldTypesEnum::Sequence
:
225 SwSetExpFieldType
* pTyp
= static_cast<SwSetExpFieldType
*>(pTmpField
->GetTyp());
226 pTyp
->SetOutlineLvl( static_cast< sal_uInt8
>(nSubType
& 0xff));
227 pTyp
->SetDelimiter(OUString(cSeparator
));
229 nSubType
= nsSwGetSetExpType::GSE_SEQ
;
233 case SwFieldTypesEnum::Input
:
235 // User- or SetField ?
236 if (m_aMgr
.GetFieldType(SwFieldIds::User
, sPar1
) == nullptr &&
237 !(pTmpField
->GetSubType() & INP_TXT
)) // SETEXPFLD
239 SwSetExpField
* pField
= static_cast<SwSetExpField
*>(pTmpField
.get());
240 pField
->SetPromptText(sPar2
);
241 sPar2
= pField
->GetPar2();
245 case SwFieldTypesEnum::DocumentInfo
:
247 if( nSubType
== nsSwDocInfoSubType::DI_CUSTOM
)
249 SwDocInfoField
* pDocInfo
= static_cast<SwDocInfoField
*>( pTmpField
.get() );
250 pDocInfo
->SetName( rPar1
);
257 pSh
->StartAllAction();
259 pTmpField
->SetSubType(nSubType
);
260 pTmpField
->SetAutomaticLanguage(bIsAutomaticLanguage
);
262 m_aMgr
.UpdateCurField( nFormatId
, sPar1
, sPar2
, std::move(pTmpField
) );
264 m_pCurField
= m_aMgr
.GetCurField();
268 case SwFieldTypesEnum::HiddenText
:
269 case SwFieldTypesEnum::HiddenParagraph
:
270 m_aMgr
.EvalExpFields(pSh
);
275 pSh
->SetUndoNoResetModified();
280 void SwFieldPage::SavePos( const weld::TreeView
& rLst1
)
282 if (rLst1
.n_children())
283 m_aLstStrArr
[ 0 ] = rLst1
.get_selected_text();
285 m_aLstStrArr
[ 0 ].clear();
286 m_aLstStrArr
[ 1 ].clear();
287 m_aLstStrArr
[ 2 ].clear();
290 void SwFieldPage::RestorePos(weld::TreeView
& rLst1
)
293 if (rLst1
.n_children() && !m_aLstStrArr
[ 0 ].isEmpty() &&
294 -1 != ( nPos
= rLst1
.find_text(m_aLstStrArr
[ 0 ] ) ) )
295 rLst1
.select( nPos
);
299 IMPL_LINK( SwFieldPage
, TreeViewInsertHdl
, weld::TreeView
&, rBox
, bool )
305 void SwFieldPage::InsertHdl(weld::Widget
* pBtn
)
307 if (SwFieldDlg
*pDlg
= dynamic_cast<SwFieldDlg
*>(GetDialogController()))
312 pBtn
->grab_focus(); // because of InputField-Dlg
316 SwFieldEditDlg
*pEditDlg
= static_cast<SwFieldEditDlg
*>(GetDialogController());
317 pEditDlg
->InsertHdl();
321 // enable/disable "Insert"-Button
322 void SwFieldPage::EnableInsert(bool bEnable
)
324 if (SwFieldDlg
*pDlg
= dynamic_cast<SwFieldDlg
*>(GetDialogController()))
326 if (pDlg
->GetCurTabPage() == this)
327 pDlg
->EnableInsert(bEnable
);
331 SwFieldEditDlg
*pEditDlg
= static_cast<SwFieldEditDlg
*>(GetDialogController());
332 pEditDlg
->EnableInsert(bEnable
);
338 IMPL_LINK_NOARG(SwFieldPage
, NumFormatHdl
, weld::TreeView
&, bool)
344 void SwFieldPage::SetWrtShell( SwWrtShell
* pShell
)
346 m_pWrtShell
= pShell
;
347 m_aMgr
.SetWrtShell( pShell
);
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */