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