LibreOffice Module sw (master)  1
fldedt.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 <sfx2/basedlgs.hxx>
23 #include <sfx2/viewfrm.hxx>
24 #include <svx/optgenrl.hxx>
25 #include <docufld.hxx>
26 #include <expfld.hxx>
27 #include <view.hxx>
28 #include <wrtsh.hxx>
29 #include "flddb.hxx"
30 #include "flddinf.hxx"
31 #include "fldvar.hxx"
32 #include "flddok.hxx"
33 #include "fldfunc.hxx"
34 #include "fldref.hxx"
35 #include <fldedt.hxx>
36 
37 #include <cmdid.h>
38 #include <swabstdlg.hxx>
39 
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <com/sun/star/document/XDocumentProperties.hpp>
42 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
43 #include <com/sun/star/frame/XModel.hpp>
44 #include <memory>
45 #include <swuiexp.hxx>
46 
48 {
50  {
51  // move cursor to start of Input Field
52  SwInputField* pInputField = dynamic_cast<SwInputField*>(pCurField);
53  if (pInputField && pInputField->GetFormatField())
54  {
55  pSh->GotoField( *(pInputField->GetFormatField()) );
56  }
57  else
58  {
59  SwSetExpField *const pSetField(dynamic_cast<SwSetExpField*>(pCurField));
60  if (pSetField)
61  {
62  assert(pSetField->GetFormatField());
63  pSh->GotoField( *(pSetField->GetFormatField()) );
64  }
65  else
66  {
67  assert(!"what input field is this");
68  }
69  }
70  }
71 
72  /* Only create selection if there is none already.
73  Normalize PaM instead of swapping. */
74  if (!pSh->HasSelection())
75  {
76  SwShellCursor* pCursor = pSh->getShellCursor(true);
77  SwPosition aOrigPos(*pCursor->GetPoint());
78 
79  //After this attempt it is possible that rMgr.GetCurField() != pCurField if
80  //the field was in e.g. a zero height portion and so invisible in which
81  //case it will be skipped over
82  pSh->Right(CRSR_SKIP_CHARS, true, 1, false );
83  //So (fdo#50640) if it didn't work then reposition back to the original
84  //location where the field was
85  SwField *pRealCurField = rMgr.GetCurField();
86  bool bSelectionFailed = pCurField != pRealCurField;
87  if (bSelectionFailed)
88  {
89  pCursor->DeleteMark();
90  *pCursor->GetPoint() = aOrigPos;
91  }
92  }
93 
94  pSh->NormalizePam();
95 
96  assert(pCurField == rMgr.GetCurField());
97 }
98 
100  : SfxSingleTabDialogController(rVw.GetViewFrame()->GetWindow().GetFrameWeld(), nullptr,
101  "modules/swriter/ui/editfielddialog.ui", "EditFieldDialog")
102  , pSh(rVw.GetWrtShellPtr())
103  , m_xPrevBT(m_xBuilder->weld_button("prev"))
104  , m_xNextBT(m_xBuilder->weld_button("next"))
105  , m_xAddressBT(m_xBuilder->weld_button("edit"))
106 {
107  SwFieldMgr aMgr(pSh);
108 
109  SwField *pCurField = aMgr.GetCurField();
110  if (!pCurField)
111  return;
112 
114 
115  EnsureSelection(pCurField, aMgr);
116 
117  sal_uInt16 nGroup = SwFieldMgr::GetGroup(pCurField->GetTypeId(), pCurField->GetSubType());
118 
119  CreatePage(nGroup);
120 
122 
123  m_xPrevBT->connect_clicked(LINK(this, SwFieldEditDlg, NextPrevHdl));
124  m_xNextBT->connect_clicked(LINK(this, SwFieldEditDlg, NextPrevHdl));
125 
126  m_xAddressBT->connect_clicked(LINK(this, SwFieldEditDlg, AddressHdl));
127 
128  Init();
129 }
130 
131 // initialise controls
133 {
134  SwFieldPage* pTabPage = static_cast<SwFieldPage*>(GetTabPage());
135  if (pTabPage)
136  {
137  SwFieldMgr& rMgr = pTabPage->GetFieldMgr();
138 
139  SwField *pCurField = rMgr.GetCurField();
140 
141  if(!pCurField)
142  return;
143 
144  // Traveling only when more than one field
145  pSh->StartAction();
146  pSh->ClearMark();
147  pSh->CreateCursor();
148 
149  bool bMove = rMgr.GoNext();
150  if( bMove )
151  rMgr.GoPrev();
152  m_xNextBT->set_sensitive(bMove);
153 
154  bMove = rMgr.GoPrev();
155  if( bMove )
156  rMgr.GoNext();
157  m_xPrevBT->set_sensitive( bMove );
158 
159  if (pCurField->GetTypeId() == SwFieldTypesEnum::ExtendedUser)
160  m_xAddressBT->set_sensitive(true);
161  else
162  m_xAddressBT->set_sensitive(false);
163 
164  pSh->DestroyCursor();
165  pSh->EndAction();
166  }
167 
169  !pSh->HasReadonlySel());
170 }
171 
173 {
174  // create TabPage
175  std::unique_ptr<SfxTabPage> xTabPage;
176 
177  switch (nGroup)
178  {
179  case GRP_DOC:
180  xTabPage = SwFieldDokPage::Create(get_content_area(), this, nullptr);
181  break;
182  case GRP_FKT:
183  xTabPage = SwFieldFuncPage::Create(get_content_area(), this, nullptr);
184  break;
185  case GRP_REF:
186  xTabPage = SwFieldRefPage::Create(get_content_area(), this, nullptr);
187  break;
188  case GRP_REG:
189  {
192  using namespace ::com::sun::star;
193  uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
194  pDocSh->GetModel(), uno::UNO_QUERY_THROW);
195  uno::Reference<document::XDocumentProperties> xDocProps
196  = xDPS->getDocumentProperties();
197  uno::Reference< beans::XPropertySet > xUDProps(
198  xDocProps->getUserDefinedProperties(),
199  uno::UNO_QUERY_THROW);
200  pSet->Put( SfxUnoAnyItem( SID_DOCINFO, uno::makeAny(xUDProps) ) );
201  xTabPage = SwFieldDokInfPage::Create(get_content_area(), this, pSet);
202  break;
203  }
204 #if HAVE_FEATURE_DBCONNECTIVITY
205  case GRP_DB:
206  xTabPage = SwFieldDBPage::Create(get_content_area(), this, nullptr);
207  static_cast<SwFieldDBPage*>(xTabPage.get())->SetWrtShell(*pSh);
208  break;
209 #endif
210  case GRP_VAR:
211  xTabPage = SwFieldVarPage::Create(get_content_area(), this, nullptr);
212  break;
213 
214  }
215 
216  assert(xTabPage);
217 
218  static_cast<SwFieldPage*>(xTabPage.get())->SetWrtShell(pSh);
219  SetTabPage(std::move(xTabPage));
220 
221  return GetTabPage();
222 }
223 
225 {
227  pSh->EnterStdMode();
228 }
229 
231 {
232  if( bEnable && pSh->IsReadOnlyAvailable() && pSh->HasReadonlySel() )
233  bEnable = false;
234  GetOKButton().set_sensitive(bEnable);
235 }
236 
238 {
239  GetOKButton().clicked();
240 }
241 
242 // kick off changing of the field
244 {
245  if (GetOKButton().get_sensitive())
246  {
247  SfxTabPage* pTabPage = GetTabPage();
248  if (pTabPage)
249  pTabPage->FillItemSet(nullptr);
250  m_xDialog->response(RET_OK);
251  }
252 }
253 
255 {
256  // without TabPage no dialog
257  return GetTabPage() ? SfxSingleTabDialogController::run() : static_cast<short>(RET_CANCEL);
258 }
259 
260 // Traveling between fields of the same type
261 IMPL_LINK(SwFieldEditDlg, NextPrevHdl, weld::Button&, rButton, void)
262 {
263  bool bNext = &rButton == m_xNextBT.get();
264 
265  pSh->EnterStdMode();
266 
267  SwFieldType *pOldTyp = nullptr;
268  SwFieldPage* pTabPage = static_cast<SwFieldPage*>(GetTabPage());
269 
270  //#112462# FillItemSet may delete the current field
271  //that's why it has to be called before accessing the current field
272  if (GetOKButton().get_sensitive())
273  pTabPage->FillItemSet(nullptr);
274 
275  SwFieldMgr& rMgr = pTabPage->GetFieldMgr();
276  SwField *pCurField = rMgr.GetCurField();
277  if (pCurField->GetTypeId() == SwFieldTypesEnum::Database)
278  pOldTyp = pCurField->GetTyp();
279 
280  rMgr.GoNextPrev( bNext, pOldTyp );
281  pCurField = rMgr.GetCurField();
282 
283  sal_uInt16 nGroup = SwFieldMgr::GetGroup(pCurField->GetTypeId(), pCurField->GetSubType());
284 
285  if (nGroup != pTabPage->GetGroup())
286  pTabPage = static_cast<SwFieldPage*>(CreatePage(nGroup));
287 
288  pTabPage->EditNewField();
289 
290  Init();
291  EnsureSelection(pCurField, rMgr);
292 }
293 
295 {
296  SwFieldPage* pTabPage = static_cast<SwFieldPage*>(GetTabPage());
297  SwFieldMgr& rMgr = pTabPage->GetFieldMgr();
298  SwField *pCurField = rMgr.GetCurField();
299 
300  SfxItemSet aSet( pSh->GetAttrPool(),
302 
303  EditPosition nEditPos = EditPosition::UNKNOWN;
304 
305  switch(pCurField->GetSubType())
306  {
307  case EU_FIRSTNAME: nEditPos = EditPosition::FIRSTNAME; break;
308  case EU_NAME: nEditPos = EditPosition::LASTNAME; break;
309  case EU_SHORTCUT: nEditPos = EditPosition::SHORTNAME; break;
310  case EU_COMPANY: nEditPos = EditPosition::COMPANY; break;
311  case EU_STREET: nEditPos = EditPosition::STREET; break;
312  case EU_TITLE: nEditPos = EditPosition::TITLE; break;
313  case EU_POSITION: nEditPos = EditPosition::POSITION; break;
314  case EU_PHONE_PRIVATE:nEditPos = EditPosition::TELPRIV; break;
315  case EU_PHONE_COMPANY:nEditPos = EditPosition::TELCOMPANY; break;
316  case EU_FAX: nEditPos = EditPosition::FAX; break;
317  case EU_EMAIL: nEditPos = EditPosition::EMAIL; break;
318  case EU_COUNTRY: nEditPos = EditPosition::COUNTRY; break;
319  case EU_ZIP: nEditPos = EditPosition::PLZ; break;
320  case EU_CITY: nEditPos = EditPosition::CITY; break;
321  case EU_STATE: nEditPos = EditPosition::STATE; break;
322 
323  default: nEditPos = EditPosition::UNKNOWN; break;
324 
325  }
326  aSet.Put(SfxUInt16Item(SID_FIELD_GRABFOCUS, static_cast<sal_uInt16>(nEditPos)));
328 
330  if (RET_OK == pDlg->Execute())
331  {
332  pSh->UpdateOneField(*pCurField);
333  }
334 }
335 
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Instances of SwFields and those derived from it occur 0 to n times.
Definition: fldbas.hxx:240
void DeleteMark()
Definition: pam.hxx:177
bool Right(sal_uInt16 nMode, bool bSelect, sal_uInt16 nCount, bool bBasicCall, bool bVisual=false)
Definition: move.cxx:123
Marks a position in the document model.
Definition: pam.hxx:35
void Init()
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: flddok.cxx:608
SwShellCursor * getShellCursor(bool bBlock)
Delivers the current shell cursor.
Definition: crsrsh.cxx:3029
Base class of all fields.
Definition: fldbas.hxx:289
std::unique_ptr< IconChoicePage >(* CreatePage)(weld::Container *pParent, SvxHpLinkDlg *pDlg, const SfxItemSet *pAttrSet)
weld::Container * get_content_area()
bool GoPrev()
Definition: fldmgr.hxx:151
void EnterStdMode()
Definition: select.cxx:550
void DestroyCursor()
transform TableCursor to normal cursor, nullify Tablemode
Definition: crsrsh.cxx:147
static SfxObjectShell * Current()
css::uno::Reference< css::frame::XModel > GetModel() const
virtual sal_uInt16 GetGroup()=0
void EnableInsert(bool bEnable)
Definition: fldedt.cxx:230
RET_CANCEL
virtual bool FillItemSet(SfxItemSet *)
SwFieldMgr & GetFieldMgr()
Definition: fldpage.hxx:81
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: fldvar.cxx:1205
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
void NormalizePam(bool bPointFirst=true)
Ensure point and mark of the current PaM are in a specific order.
Definition: crsrsh.cxx:969
IMPL_LINK(SwFieldEditDlg, NextPrevHdl, weld::Button &, rButton, void)
Definition: fldedt.cxx:261
void InsertHdl()
Definition: fldedt.cxx:237
SfxItemPool & GetPool() const
std::unique_ptr< weld::Button > m_xAddressBT
Definition: fldedt.hxx:35
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
Definition: dialoghelp.cxx:20
void connect_clicked(const Link< Button &, void > &rLink)
bool HasSelection() const
Definition: wrtsh.hxx:141
SwPaM * CreateCursor()
delete the current cursor and make the following into the current
Definition: crsrsh.cxx:123
virtual ~SwFieldEditDlg() override
Definition: fldedt.cxx:224
const SwPosition * GetPoint() const
Definition: pam.hxx:207
SwAbstractDialogFactory & GetFactory()
Definition: swuiexp.cxx:26
void EndAction(const bool bIdleEnd=false, const bool DoSetPosX=false)
Definition: crsrsh.cxx:239
IMPL_LINK_NOARG(SwFieldEditDlg, OKHdl, weld::Button &, void)
Definition: fldedt.cxx:243
std::unique_ptr< weld::Button > m_xNextBT
Definition: fldedt.hxx:34
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: fldfunc.cxx:573
virtual VclPtr< SfxAbstractDialog > CreateSwAddressAbstractDlg(weld::Window *pParent, const SfxItemSet &rSet)=0
SwField * GetCurField()
Definition: fldmgr.cxx:425
void Init()
Definition: fldedt.cxx:132
SwWrtShell * pSh
Definition: fldedt.hxx:32
void SetTabPage(std::unique_ptr< SfxTabPage > xTabPage)
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: flddb.cxx:252
SwFieldEditDlg(SwView const &rVw)
Definition: fldedt.cxx:99
bool HasReadonlySel() const
Definition: crsrsh.cxx:3336
bool GoNextPrev(bool bNext=true, SwFieldType *pTyp=nullptr)
Definition: fldmgr.cxx:850
virtual sal_uInt16 GetSubType() const
Definition: fldbas.cxx:335
SwFieldType * GetTyp() const
Definition: fldbas.hxx:392
void StartAction()
Definition: crsrsh.cxx:222
void EnsureSelection(SwField *pCurField, SwFieldMgr &rMgr)
Definition: fldedt.cxx:47
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
void EditNewField(bool bOnlyActivate=false)
Definition: fldpage.cxx:104
SwFieldTypesEnum GetTypeId() const
Definition: fldbas.cxx:250
SwFormatField * GetFormatField()
Definition: expfld.hxx:220
bool IsReadOnlyAvailable() const
Definition: crsrsh.hxx:474
bool GotoField(const SwFormatField &rField)
Definition: wrtsh3.cxx:65
SfxTabPage * CreatePage(sal_uInt16 nGroup)
Definition: fldedt.cxx:172
bool GoNext()
Definition: fldmgr.hxx:150
std::unique_ptr< weld::Button > m_xPrevBT
Definition: fldedt.hxx:33
static void SetCareDialog(const std::shared_ptr< weld::Window > &rNew)
Definition: viewsh.cxx:2553
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: flddinf.cxx:443
void clicked()
static sal_uInt16 GetGroup(SwFieldTypesEnum nTypeId, sal_uInt16 nSubType)
Definition: fldmgr.cxx:493
SwFormatField * GetFormatField()
Definition: expfld.hxx:311
RET_OK
Reference< XExecutableDialog > m_xDialog
virtual short run() override
Definition: fldedt.cxx:254
void ClearMark()
Definition: crsrsh.cxx:938
const sal_uInt16 CRSR_SKIP_CHARS
Definition: swcrsr.hxx:65
SfxTabPage * GetTabPage() const
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: fldref.cxx:1102
virtual weld::Button & GetOKButton() const override
Definition: view.hxx:144
virtual void set_sensitive(bool sensitive)=0
bool CursorInsideInputField() const
Definition: crstrvl.cxx:925