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  SwFieldPage* pTabPage = static_cast<SwFieldPage*>(GetTabPage());
139  if (pTabPage)
140  {
141  SwFieldMgr& rMgr = pTabPage->GetFieldMgr();
142 
143  SwField *pCurField = rMgr.GetCurField();
144 
145  if(!pCurField)
146  return;
147 
148  // Traveling only when more than one field
149  pSh->StartAction();
150  pSh->ClearMark();
151  pSh->CreateCursor();
152 
153  bool bMove = rMgr.GoNext();
154  if( bMove )
155  rMgr.GoPrev();
156  m_xNextBT->set_sensitive(bMove);
157 
158  bMove = rMgr.GoPrev();
159  if( bMove )
160  rMgr.GoNext();
161  m_xPrevBT->set_sensitive( bMove );
162 
163  if (pCurField->GetTypeId() == SwFieldTypesEnum::ExtendedUser)
164  m_xAddressBT->set_sensitive(true);
165  else
166  m_xAddressBT->set_sensitive(false);
167 
168  pSh->DestroyCursor();
169  pSh->EndAction();
170  }
171 
173  !pSh->HasReadonlySel());
174 }
175 
177 {
178  // create TabPage
179  std::unique_ptr<SfxTabPage> xTabPage;
180 
181  switch (nGroup)
182  {
183  case GRP_DOC:
184  xTabPage = SwFieldDokPage::Create(get_content_area(), this, nullptr);
185  break;
186  case GRP_FKT:
187  xTabPage = SwFieldFuncPage::Create(get_content_area(), this, nullptr);
188  break;
189  case GRP_REF:
190  xTabPage = SwFieldRefPage::Create(get_content_area(), this, nullptr);
191  break;
192  case GRP_REG:
193  {
196  using namespace ::com::sun::star;
197  uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
198  pDocSh->GetModel(), uno::UNO_QUERY_THROW);
199  uno::Reference<document::XDocumentProperties> xDocProps
200  = xDPS->getDocumentProperties();
201  uno::Reference< beans::XPropertySet > xUDProps(
202  xDocProps->getUserDefinedProperties(),
203  uno::UNO_QUERY_THROW);
204  pSet->Put( SfxUnoAnyItem( SID_DOCINFO, uno::makeAny(xUDProps) ) );
205  xTabPage = SwFieldDokInfPage::Create(get_content_area(), this, pSet);
206  break;
207  }
208 #if HAVE_FEATURE_DBCONNECTIVITY
209  case GRP_DB:
210  xTabPage = SwFieldDBPage::Create(get_content_area(), this, nullptr);
211  static_cast<SwFieldDBPage*>(xTabPage.get())->SetWrtShell(*pSh);
212  break;
213 #endif
214  case GRP_VAR:
215  xTabPage = SwFieldVarPage::Create(get_content_area(), this, nullptr);
216  break;
217 
218  }
219 
220  assert(xTabPage);
221 
222  static_cast<SwFieldPage*>(xTabPage.get())->SetWrtShell(pSh);
223  SetTabPage(std::move(xTabPage));
224 
225  return GetTabPage();
226 }
227 
229 {
231  pSh->EnterStdMode();
232 }
233 
235 {
236  if( bEnable && pSh->IsReadOnlyAvailable() && pSh->HasReadonlySel() )
237  bEnable = false;
238  GetOKButton().set_sensitive(bEnable);
239 }
240 
242 {
243  GetOKButton().clicked();
244 }
245 
246 // kick off changing of the field
248 {
249  if (GetOKButton().get_sensitive())
250  {
251  SfxTabPage* pTabPage = GetTabPage();
252  if (pTabPage)
253  pTabPage->FillItemSet(nullptr);
254  m_xDialog->response(RET_OK);
255  }
256 }
257 
259 {
260  // without TabPage no dialog
261  return GetTabPage() ? SfxSingleTabDialogController::run() : static_cast<short>(RET_CANCEL);
262 }
263 
264 // Traveling between fields of the same type
265 IMPL_LINK(SwFieldEditDlg, NextPrevHdl, weld::Button&, rButton, void)
266 {
267  bool bNext = &rButton == m_xNextBT.get();
268 
269  pSh->EnterStdMode();
270 
271  SwFieldType *pOldTyp = nullptr;
272  SwFieldPage* pTabPage = static_cast<SwFieldPage*>(GetTabPage());
273 
274  //#112462# FillItemSet may delete the current field
275  //that's why it has to be called before accessing the current field
276  if (GetOKButton().get_sensitive())
277  pTabPage->FillItemSet(nullptr);
278 
279  SwFieldMgr& rMgr = pTabPage->GetFieldMgr();
280  SwField *pCurField = rMgr.GetCurField();
281  if (pCurField->GetTypeId() == SwFieldTypesEnum::Database)
282  pOldTyp = pCurField->GetTyp();
283 
284  rMgr.GoNextPrev( bNext, pOldTyp );
285  pCurField = rMgr.GetCurField();
286 
287  EnsureSelection(pCurField, rMgr);
288 
289  sal_uInt16 nGroup = SwFieldMgr::GetGroup(pCurField->GetTypeId(), pCurField->GetSubType());
290 
291  if (nGroup != pTabPage->GetGroup())
292  pTabPage = static_cast<SwFieldPage*>(CreatePage(nGroup));
293 
294  pTabPage->EditNewField();
295 
296  Init();
297 }
298 
300 {
301  SwFieldPage* pTabPage = static_cast<SwFieldPage*>(GetTabPage());
302  SwFieldMgr& rMgr = pTabPage->GetFieldMgr();
303  SwField *pCurField = rMgr.GetCurField();
304 
305  SfxItemSet aSet( pSh->GetAttrPool(),
307 
308  EditPosition nEditPos = EditPosition::UNKNOWN;
309 
310  switch(pCurField->GetSubType())
311  {
312  case EU_FIRSTNAME: nEditPos = EditPosition::FIRSTNAME; break;
313  case EU_NAME: nEditPos = EditPosition::LASTNAME; break;
314  case EU_SHORTCUT: nEditPos = EditPosition::SHORTNAME; break;
315  case EU_COMPANY: nEditPos = EditPosition::COMPANY; break;
316  case EU_STREET: nEditPos = EditPosition::STREET; break;
317  case EU_TITLE: nEditPos = EditPosition::TITLE; break;
318  case EU_POSITION: nEditPos = EditPosition::POSITION; break;
319  case EU_PHONE_PRIVATE:nEditPos = EditPosition::TELPRIV; break;
320  case EU_PHONE_COMPANY:nEditPos = EditPosition::TELCOMPANY; break;
321  case EU_FAX: nEditPos = EditPosition::FAX; break;
322  case EU_EMAIL: nEditPos = EditPosition::EMAIL; break;
323  case EU_COUNTRY: nEditPos = EditPosition::COUNTRY; break;
324  case EU_ZIP: nEditPos = EditPosition::PLZ; break;
325  case EU_CITY: nEditPos = EditPosition::CITY; break;
326  case EU_STATE: nEditPos = EditPosition::STATE; break;
327 
328  default: nEditPos = EditPosition::UNKNOWN; break;
329 
330  }
331  aSet.Put(SfxUInt16Item(SID_FIELD_GRABFOCUS, static_cast<sal_uInt16>(nEditPos)));
333 
334  ScopedVclPtr<SfxAbstractDialog> pDlg(rFact.CreateSwAddressAbstractDlg(m_xDialog.get(), aSet));
335  if (RET_OK == pDlg->Execute())
336  {
337  pSh->UpdateOneField(*pCurField);
338  }
339 }
340 
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Instances of SwFields and those derived from it occur 0 to n times.
Definition: fldbas.hxx:234
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
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:611
SwShellCursor * getShellCursor(bool bBlock)
Delivers the current shell cursor.
Definition: crsrsh.cxx:3006
Base class of all fields.
Definition: fldbas.hxx:280
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:555
void DestroyCursor()
transform TableCursor to normal cursor, nullify Tablemode
Definition: crsrsh.cxx:141
static SfxObjectShell * Current()
css::uno::Reference< css::frame::XModel > GetModel() const
virtual sal_uInt16 GetGroup()=0
void EnableInsert(bool bEnable)
Definition: fldedt.cxx:234
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:1207
void NormalizePam(bool bPointFirst=true)
Ensure point and mark of the current PaM are in a specific order.
Definition: crsrsh.cxx:957
IMPL_LINK(SwFieldEditDlg, NextPrevHdl, weld::Button &, rButton, void)
Definition: fldedt.cxx:265
void InsertHdl()
Definition: fldedt.cxx:241
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:117
virtual ~SwFieldEditDlg() override
Definition: fldedt.cxx:228
const SwPosition * GetPoint() const
Definition: pam.hxx:207
SwAbstractDialogFactory & GetFactory()
Definition: swuiexp.cxx:57
void EndAction(const bool bIdleEnd=false, const bool DoSetPosX=false)
Definition: crsrsh.cxx:228
IMPL_LINK_NOARG(SwFieldEditDlg, OKHdl, weld::Button &, void)
Definition: fldedt.cxx:247
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:579
virtual VclPtr< SfxAbstractDialog > CreateSwAddressAbstractDlg(weld::Window *pParent, const SfxItemSet &rSet)=0
SwField * GetCurField()
Definition: fldmgr.cxx:433
void Init()
Definition: fldedt.cxx:136
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:258
SwFieldEditDlg(SwView const &rVw)
Definition: fldedt.cxx:103
bool HasReadonlySel() const
Definition: crsrsh.cxx:3312
bool GoNextPrev(bool bNext=true, SwFieldType *pTyp=nullptr)
Definition: fldmgr.cxx:858
virtual sal_uInt16 GetSubType() const
Definition: fldbas.cxx:284
SwFieldType * GetTyp() const
Definition: fldbas.hxx:383
void StartAction()
Definition: crsrsh.cxx:211
void EnsureSelection(SwField *pCurField, SwFieldMgr &rMgr)
Definition: fldedt.cxx:51
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
void EditNewField(bool bOnlyActivate=false)
Definition: fldpage.cxx:106
SwFieldTypesEnum GetTypeId() const
Definition: fldbas.cxx:199
SwFormatField * GetFormatField()
Definition: expfld.hxx:219
bool IsReadOnlyAvailable() const
Definition: crsrsh.hxx:478
bool GotoField(const SwFormatField &rField)
Definition: wrtsh3.cxx:70
SfxTabPage * CreatePage(sal_uInt16 nGroup)
Definition: fldedt.cxx:176
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:2498
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: flddinf.cxx:446
void clicked()
static sal_uInt16 GetGroup(SwFieldTypesEnum nTypeId, sal_uInt16 nSubType)
Definition: fldmgr.cxx:501
SwFormatField * GetFormatField()
Definition: expfld.hxx:310
RET_OK
virtual short run() override
Definition: fldedt.cxx:258
void ClearMark()
Definition: crsrsh.cxx:926
const sal_uInt16 CRSR_SKIP_CHARS
Definition: swcrsr.hxx:63
SfxTabPage * GetTabPage() const
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: fldref.cxx:1105
virtual weld::Button & GetOKButton() const override
Definition: view.hxx:146
virtual void set_sensitive(bool sensitive)=0
bool CursorInsideInputField() const
Definition: crstrvl.cxx:948