Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / ui / fldui / fldpage.cxx
blob7e7f5ade3b865c373266e80a97f013479226e700
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 <svl/stritem.hxx>
21 #include <sfx2/request.hxx>
22 #include <sfx2/htmlmode.hxx>
23 #include <sfx2/viewfrm.hxx>
24 #include <dbfld.hxx>
25 #include <flddat.hxx>
26 #include <fmtfld.hxx>
27 #include <viewopt.hxx>
28 #include <fldedt.hxx>
29 #include <docsh.hxx>
30 #include <swmodule.hxx>
31 #include <view.hxx>
32 #include <wrtsh.hxx>
33 #include <expfld.hxx>
34 #include <fldtdlg.hxx>
35 #include "fldpage.hxx"
36 #include <docufld.hxx>
37 #include <cmdid.h>
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)
49 , m_nTypeSel(-1)
50 , m_nSelectionSel(-1)
51 , m_bFieldEdit(false)
52 , m_bInsert(true)
53 , m_bFieldDlgHtmlMode(false)
54 , m_bRefresh(false)
55 , m_bFirstHTMLInit(true)
59 SwFieldPage::~SwFieldPage()
63 // initialise TabPage
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 )
76 return;
78 m_bFieldDlgHtmlMode = bNewMode;
80 // initialise Rangelistbox
81 if( !(m_bFieldDlgHtmlMode && m_bFirstHTMLInit) )
82 return;
84 m_bFirstHTMLInit = false;
85 SwWrtShell *pSh = m_pWrtShell;
86 if(! pSh)
87 pSh = ::GetActiveWrtShell();
88 if(pSh)
90 SwDoc* pDoc = pSh->GetDoc();
91 pSh->InsertFieldType( SwSetExpFieldType( pDoc,
92 "HTML_ON", 1));
93 pSh->InsertFieldType( SwSetExpFieldType(pDoc,
94 "HTML_OFF", 1));
98 // newly initialise page
99 void SwFieldPage::Activate()
101 EnableInsert(m_bInsert);
104 // complete reset; edit new field
105 void SwFieldPage::EditNewField( bool bOnlyActivate )
107 if (!bOnlyActivate)
108 m_nTypeSel = -1;
109 m_nSelectionSel = -1;
110 m_bRefresh = true;
111 Reset(nullptr);
112 m_bRefresh = false;
115 // insert field
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();
121 if (!pView)
122 return;
124 SwWrtShell *pSh = m_pWrtShell ? m_pWrtShell : pView->GetWrtShellPtr();
125 if (!pSh)
126 return;
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);
147 if(bRecordDB)
149 sal_Int32 nIdx{ 0 };
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)));
159 else
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));
169 aReq.Done();
173 else // change field
175 std::unique_ptr<SwField> pTmpField = m_pCurField->CopyField();
177 OUString sPar1(rPar1);
178 OUString sPar2(rPar2);
179 switch( nTypeId )
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));
185 break;
187 case SwFieldTypesEnum::DatabaseName:
188 case SwFieldTypesEnum::DatabaseNextSet:
189 case SwFieldTypesEnum::DatabaseNumberSet:
190 case SwFieldTypesEnum::DatabaseSetNumber:
192 sal_Int32 nPos = 0;
193 SwDBData aData;
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);
202 break;
204 case SwFieldTypesEnum::Database:
206 SwDBData aData;
207 sal_Int32 nIdx{ 0 };
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);
221 break;
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;
231 break;
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();
244 break;
245 case SwFieldTypesEnum::DocumentInfo:
247 if( nSubType == nsSwDocInfoSubType::DI_CUSTOM )
249 SwDocInfoField* pDocInfo = static_cast<SwDocInfoField*>( pTmpField.get() );
250 pDocInfo->SetName( rPar1 );
253 break;
254 default: break;
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();
266 switch (nTypeId)
268 case SwFieldTypesEnum::HiddenText:
269 case SwFieldTypesEnum::HiddenParagraph:
270 m_aMgr.EvalExpFields(pSh);
271 break;
272 default: break;
275 pSh->SetUndoNoResetModified();
276 pSh->EndAllAction();
280 void SwFieldPage::SavePos( const weld::TreeView& rLst1 )
282 if (rLst1.n_children())
283 m_aLstStrArr[ 0 ] = rLst1.get_selected_text();
284 else
285 m_aLstStrArr[ 0 ].clear();
286 m_aLstStrArr[ 1 ].clear();
287 m_aLstStrArr[ 2 ].clear();
290 void SwFieldPage::RestorePos(weld::TreeView& rLst1)
292 sal_Int32 nPos = 0;
293 if (rLst1.n_children() && !m_aLstStrArr[ 0 ].isEmpty() &&
294 -1 != ( nPos = rLst1.find_text(m_aLstStrArr[ 0 ] ) ) )
295 rLst1.select( nPos );
298 // Insert new fields
299 IMPL_LINK( SwFieldPage, TreeViewInsertHdl, weld::TreeView&, rBox, bool )
301 InsertHdl(&rBox);
302 return true;
305 void SwFieldPage::InsertHdl(weld::Widget* pBtn)
307 if (SwFieldDlg *pDlg = dynamic_cast<SwFieldDlg*>(GetDialogController()))
309 pDlg->InsertHdl();
311 if (pBtn)
312 pBtn->grab_focus(); // because of InputField-Dlg
314 else
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);
329 else
331 SwFieldEditDlg *pEditDlg = static_cast<SwFieldEditDlg*>(GetDialogController());
332 pEditDlg->EnableInsert(bEnable);
335 m_bInsert = bEnable;
338 IMPL_LINK_NOARG(SwFieldPage, NumFormatHdl, weld::TreeView&, bool)
340 InsertHdl(nullptr);
341 return true;
344 void SwFieldPage::SetWrtShell( SwWrtShell* pShell )
346 m_pWrtShell = pShell;
347 m_aMgr.SetWrtShell( pShell );
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */