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