LibreOffice Module sw (master)  1
titlepage.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 
10 #include <view.hxx>
11 #include <swmodule.hxx>
12 #include <wrtsh.hxx>
13 #include <poolfmt.hxx>
14 #include <docsh.hxx>
15 
16 #include <titlepage.hxx>
17 #include <fmtpdsc.hxx>
18 #include <pagedesc.hxx>
19 
20 namespace
21 {
22  bool lcl_GetPageDesc(SwWrtShell *pSh, sal_uInt16 &rPageNo, std::unique_ptr<const SwFormatPageDesc>* ppPageFormatDesc)
23  {
24  bool bRet = false;
26  if (pSh->GetCurAttr( aSet ))
27  {
28  const SfxPoolItem* pItem(nullptr);
29  if (SfxItemState::SET == aSet.GetItemState( RES_PAGEDESC, true, &pItem ) && pItem)
30  {
31  ::std::optional<sal_uInt16> oNumOffset = static_cast<const SwFormatPageDesc *>(pItem)->GetNumOffset();
32  if (oNumOffset)
33  rPageNo = *oNumOffset;
34  if (ppPageFormatDesc)
35  ppPageFormatDesc->reset(static_cast<const SwFormatPageDesc *>(pItem->Clone()));
36  bRet = true;
37  }
38  }
39  return bRet;
40  }
41 
42  void lcl_ChangePage(SwWrtShell *pSh, sal_uInt16 nNewNumber,
43  const SwPageDesc *pNewDesc)
44  {
45  const size_t nCurIdx = pSh->GetCurPageDesc();
46  const SwPageDesc &rCurrentDesc = pSh->GetPageDesc( nCurIdx );
47 
48  std::unique_ptr<const SwFormatPageDesc> pPageFormatDesc;
49  sal_uInt16 nDontCare;
50  lcl_GetPageDesc(pSh, nDontCare, &pPageFormatDesc);
51 
52  // If we want a new number then set it, otherwise reuse the existing one
53  sal_uInt16 nPgNo;
54  if (nNewNumber)
55  {
56  nPgNo = nNewNumber;
57  }
58  else
59  {
60  if (pPageFormatDesc)
61  {
62  ::std::optional<sal_uInt16> oNumOffset = pPageFormatDesc->GetNumOffset();
63  if (oNumOffset)
64  {
65  nPgNo = *oNumOffset;
66  }
67  else
68  {
69  nPgNo = 0;
70  }
71  }
72  else
73  {
74  nPgNo = 0;
75  }
76  }
77 
78  // If we want a new descriptor then set it, otherwise reuse the existing one
79  if (!pNewDesc)
80  {
81  SwFormatPageDesc aPageFormatDesc(pPageFormatDesc ? *pPageFormatDesc : &rCurrentDesc);
82  if (nPgNo) aPageFormatDesc.SetNumOffset(nPgNo);
83  pSh->SetAttrItem(aPageFormatDesc);
84  }
85  else
86  {
87  SwFormatPageDesc aPageFormatDesc(pNewDesc);
88  if (nPgNo) aPageFormatDesc.SetNumOffset(nPgNo);
89  pSh->SetAttrItem(aPageFormatDesc);
90  }
91  }
92 
93  void lcl_PushCursor(SwWrtShell *pSh)
94  {
95  pSh->LockView( true );
96  pSh->StartAllAction();
97  pSh->SwCursorShell::Push();
98  }
99 
100  void lcl_PopCursor(SwWrtShell *pSh)
101  {
102  pSh->SwCursorShell::Pop(SwCursorShell::PopMode::DeleteCurrent);
103  pSh->EndAllAction();
104  pSh->LockView( false );
105  }
106 
107  sal_uInt16 lcl_GetCurrentPage(SwWrtShell const *pSh)
108  {
109  OUString sDummy;
110  sal_uInt16 nPhyNum=1, nVirtNum=1;
111  pSh->GetPageNumber(0, true, nPhyNum, nVirtNum, sDummy);
112  return nPhyNum;
113  }
114 }
115 
116 /*
117  * Only include the Index page in the list if the page count implies one
118  * to reduce confusing things
119  */
121 {
122  sal_uInt16 nTitlePages = m_xPageCountNF->get_value();
123  m_xPagePropertiesLB->clear();
124  if (mpTitleDesc)
125  m_xPagePropertiesLB->append_text(mpTitleDesc->GetName());
126  if (nTitlePages > 1 && mpIndexDesc)
127  m_xPagePropertiesLB->append_text(mpIndexDesc->GetName());
128  if (mpNormalDesc)
129  m_xPagePropertiesLB->append_text(mpNormalDesc->GetName());
130  m_xPagePropertiesLB->set_active(0);
131 }
132 
134 {
135  sal_uInt16 nPage = 1;
136  if (m_xPageStartNF->get_sensitive())
137  nPage = m_xPageStartNF->get_value();
138  return nPage;
139 }
140 
142  : SfxDialogController(pParent, "modules/swriter/ui/titlepage.ui", "DLG_TITLEPAGE")
143  , m_xUseExistingPagesRB(m_xBuilder->weld_radio_button("RB_USE_EXISTING_PAGES"))
144  , m_xPageCountNF(m_xBuilder->weld_spin_button("NF_PAGE_COUNT"))
145  , m_xDocumentStartRB(m_xBuilder->weld_radio_button("RB_DOCUMENT_START"))
146  , m_xPageStartRB(m_xBuilder->weld_radio_button("RB_PAGE_START"))
147  , m_xPageStartNF(m_xBuilder->weld_spin_button("NF_PAGE_START"))
148  , m_xRestartNumberingCB(m_xBuilder->weld_check_button("CB_RESTART_NUMBERING"))
149  , m_xRestartNumberingNF(m_xBuilder->weld_spin_button("NF_RESTART_NUMBERING"))
150  , m_xSetPageNumberCB(m_xBuilder->weld_check_button("CB_SET_PAGE_NUMBER"))
151  , m_xSetPageNumberNF(m_xBuilder->weld_spin_button("NF_SET_PAGE_NUMBER"))
152  , m_xPagePropertiesLB(m_xBuilder->weld_combo_box("LB_PAGE_PROPERTIES"))
153  , m_xPagePropertiesPB(m_xBuilder->weld_button("PB_PAGE_PROPERTIES"))
154  , m_xOkPB(m_xBuilder->weld_button("ok"))
155 {
156  m_xOkPB->connect_clicked(LINK(this, SwTitlePageDlg, OKHdl));
157  m_xRestartNumberingCB->connect_toggled(LINK(this, SwTitlePageDlg, RestartNumberingHdl));
158  m_xSetPageNumberCB->connect_toggled(LINK(this, SwTitlePageDlg, SetPageNumberHdl));
159 
160  sal_uInt16 nSetPage = 1;
161  sal_uInt16 nResetPage = 1;
162  sal_uInt16 nTitlePages = 1;
164  lcl_PushCursor(mpSh);
165 
166  SwView& rView = mpSh->GetView();
167  rView.InvalidateRulerPos();
168 
169  bool bMaybeResetNumbering = false;
170 
174 
175  mpSh->StartOfSection();
176  if (lcl_GetPageDesc( mpSh, nSetPage, &mpPageFormatDesc ))
177  {
178  if (mpPageFormatDesc->GetPageDesc() == mpTitleDesc)
179  {
180  while (mpSh->SttNxtPg())
181  {
182  const size_t nCurIdx = mpSh->GetCurPageDesc();
183  const SwPageDesc &rPageDesc = mpSh->GetPageDesc( nCurIdx );
184 
185  if (mpIndexDesc != &rPageDesc)
186  {
187  mpNormalDesc = &rPageDesc;
188  bMaybeResetNumbering = lcl_GetPageDesc(mpSh, nResetPage, nullptr);
189  break;
190  }
191  ++nTitlePages;
192  }
193  }
194  }
195  lcl_PopCursor(mpSh);
196 
197  m_xUseExistingPagesRB->set_active(true);
198  m_xPageCountNF->set_value(nTitlePages);
199  m_xPageCountNF->connect_value_changed(LINK(this, SwTitlePageDlg, ValueChangeHdl));
200 
201  m_xDocumentStartRB->set_active(true);
202  m_xPageStartNF->set_sensitive(false);
203  m_xPageStartNF->set_value(lcl_GetCurrentPage(mpSh));
204  Link<weld::ToggleButton&,void> aStartPageHdl = LINK(this, SwTitlePageDlg, StartPageHdl);
205  m_xDocumentStartRB->connect_toggled(aStartPageHdl);
206  m_xPageStartRB->connect_toggled(aStartPageHdl);
207 
208  if (bMaybeResetNumbering && nResetPage > 0)
209  {
210  m_xRestartNumberingCB->set_active(true);
211  m_xRestartNumberingNF->set_value(nResetPage);
212  }
213  m_xRestartNumberingNF->set_sensitive(m_xRestartNumberingCB->get_active());
214 
215  m_xSetPageNumberNF->set_value(nSetPage);
216  if (nSetPage > 1)
217  m_xSetPageNumberCB->set_active(true);
218  m_xSetPageNumberNF->set_sensitive(m_xSetPageNumberCB->get_active());
219 
220  FillList();
221  m_xPagePropertiesPB->connect_clicked(LINK(this, SwTitlePageDlg, EditHdl));
222 }
223 
225 {
226  if (m_xPageCountNF->get_value() == 1 || m_xPageCountNF->get_value() == 2)
227  FillList();
228 }
229 
230 IMPL_LINK_NOARG(SwTitlePageDlg, RestartNumberingHdl, weld::ToggleButton&, void)
231 {
232  m_xRestartNumberingNF->set_sensitive(m_xRestartNumberingCB->get_active());
233 }
234 
236 {
237  m_xSetPageNumberNF->set_sensitive(m_xSetPageNumberCB->get_active());
238 }
239 
241 {
242  m_xPageStartNF->set_sensitive(m_xPageStartRB->get_active());
243 }
244 
246 {
247 }
248 
250 {
251  SwView& rView = mpSh->GetView();
252  rView.GetDocShell()->FormatPage(m_xPagePropertiesLB->get_active_text(), "page", *mpSh);
253  rView.InvalidateRulerPos();
254 }
255 
257 {
258  lcl_PushCursor(mpSh);
259 
260  mpSh->StartUndo();
261 
262  SwFormatPageDesc aTitleDesc(mpTitleDesc);
263 
264  if (m_xSetPageNumberCB->get_active())
265  aTitleDesc.SetNumOffset(m_xSetPageNumberNF->get_value());
266  else if (mpPageFormatDesc)
267  aTitleDesc.SetNumOffset(mpPageFormatDesc->GetNumOffset());
268 
269  sal_uInt16 nNoPages = m_xPageCountNF->get_value();
270  if (!m_xUseExistingPagesRB->get_active())
271  {
272  mpSh->GotoPage(GetInsertPosition(), false);
273  for (sal_uInt16 nI=0; nI < nNoPages; ++nI)
274  mpSh->InsertPageBreak();
275  }
276 
277  mpSh->GotoPage(GetInsertPosition(), false);
278  for (sal_uInt16 nI=1; nI < nNoPages; ++nI)
279  {
280  if (mpSh->SttNxtPg())
281  lcl_ChangePage(mpSh, 0, mpIndexDesc);
282  }
283 
284  mpSh->GotoPage(GetInsertPosition(), false);
285  mpSh->SetAttrItem(aTitleDesc);
286 
287  if (nNoPages > 1 && mpSh->GotoPage(GetInsertPosition() + nNoPages, false))
288  {
289  SwFormatPageDesc aPageFormatDesc(mpNormalDesc);
290  mpSh->SetAttrItem(aPageFormatDesc);
291  }
292 
293  if (m_xRestartNumberingCB->get_active() || nNoPages > 1)
294  {
295  sal_uInt16 nPgNo = m_xRestartNumberingCB->get_active() ? m_xRestartNumberingNF->get_value() : 0;
296  const SwPageDesc *pNewDesc = nNoPages > 1 ? mpNormalDesc : nullptr;
297  mpSh->GotoPage(GetInsertPosition() + nNoPages, false);
298  lcl_ChangePage(mpSh, nPgNo, pNewDesc);
299  }
300 
301  mpSh->EndUndo();
302  lcl_PopCursor(mpSh);
303  if (!m_xUseExistingPagesRB->get_active())
304  mpSh->GotoPage(GetInsertPosition(), false);
305  m_xDialog->response(RET_OK);
306 }
307 
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr TypedWhichId< SwFormatPageDesc > RES_PAGEDESC(93)
std::unique_ptr< weld::CheckButton > m_xSetPageNumberCB
Definition: titlepage.hxx:42
Pagedescriptor Client of SwPageDesc that is "described" by the attribute.
Definition: fmtpdsc.hxx:35
SwWrtShell * mpSh
Definition: titlepage.hxx:27
SwView * GetActiveView()
Definition: swmodul1.cxx:115
bool SttNxtPg(bool bSelect=false)
Definition: move.cxx:316
std::unique_ptr< weld::ComboBox > m_xPagePropertiesLB
Definition: titlepage.hxx:44
std::unique_ptr< weld::Button > m_xPagePropertiesPB
Definition: titlepage.hxx:45
std::unique_ptr< weld::SpinButton > m_xPageCountNF
Definition: titlepage.hxx:36
void EndAllAction()
Definition: edws.cxx:97
SwTitlePageDlg(weld::Window *pParent)
Definition: titlepage.cxx:141
const OUString & GetName() const
Definition: pagedesc.hxx:186
Used by the UI to modify the document model.
Definition: wrtsh.hxx:90
const SwView & GetView() const
Definition: wrtsh.hxx:428
std::unique_ptr< weld::RadioButton > m_xDocumentStartRB
Definition: titlepage.hxx:37
IMPL_LINK_NOARG(SwTitlePageDlg, ValueChangeHdl, weld::SpinButton &, void)
Definition: titlepage.cxx:224
std::unique_ptr< weld::SpinButton > m_xPageStartNF
Definition: titlepage.hxx:39
sal_uInt16 GetInsertPosition() const
Definition: titlepage.cxx:133
std::unique_ptr< weld::CheckButton > m_xRestartNumberingCB
Definition: titlepage.hxx:40
const SfxItemPool & GetAttrPool() const
Definition: viewsh.hxx:612
First page.
Definition: poolfmt.hxx:171
void SetAttrItem(const SfxPoolItem &, SetAttrMode nFlags=SetAttrMode::DEFAULT, const bool bParagraphSetting=false)
Definition: edatmisc.cxx:112
const SwPageDesc & GetPageDesc(size_t i) const
Definition: fedesc.cxx:125
const SwPageDesc * mpIndexDesc
Definition: titlepage.hxx:32
std::unique_ptr< weld::RadioButton > m_xPageStartRB
Definition: titlepage.hxx:38
bool StartOfSection(bool bSelect=false)
Definition: move.cxx:304
SwDocShell * GetDocShell()
Definition: view.cxx:1109
const SwPageDesc * mpNormalDesc
Definition: titlepage.hxx:33
SwWrtShell * GetWrtShellPtr() const
Definition: view.hxx:400
std::unique_ptr< weld::SpinButton > m_xSetPageNumberNF
Definition: titlepage.hxx:43
void LockView(bool b)
Definition: viewsh.hxx:462
std::unique_ptr< weld::SpinButton > m_xRestartNumberingNF
Definition: titlepage.hxx:41
std::unique_ptr< const SwFormatPageDesc > mpPageFormatDesc
Definition: titlepage.hxx:29
RET_OK
virtual ~SwTitlePageDlg() override
Definition: titlepage.cxx:245
SwPageDesc * GetPageDescFromPool(sal_uInt16 nId)
Definition: edfmt.cxx:134
Reference< XExecutableDialog > m_xDialog
void InvalidateRulerPos()
Definition: viewport.cxx:104
static SwPageDesc * lcl_GetPageDesc(SwDoc *pDoc, const uno::Any &aValue)
Definition: unosett.cxx:235
bool GetCurAttr(SfxItemSet &, const bool bMergeIndentValuesOfNumRule=false) const
Definition: edattr.cxx:177
std::unique_ptr< weld::RadioButton > m_xUseExistingPagesRB
Definition: titlepage.hxx:35
std::unique_ptr< weld::Button > m_xOkPB
Definition: titlepage.hxx:46
void SetNumOffset(const ::std::optional< sal_uInt16 > &oNum)
Definition: fmtpdsc.hxx:66
sal_uInt16 GetPageNumber(const Point &rPoint) const
Page number of the page containing Point, O if no page.
Definition: fews.cxx:184
void FormatPage(const OUString &rPage, const OString &rPageId, SwWrtShell &rActShell, SfxRequest *pRequest=nullptr)
Show page style format dialog.
Definition: docst.cxx:1544
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
size_t GetCurPageDesc(const bool bCalcFrame=true) const
Definition: fedesc.cxx:166
Standard page.
Definition: poolfmt.hxx:170
SwPageDesc * mpTitleDesc
Definition: titlepage.hxx:31
Definition: view.hxx:144