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
20namespace
21{
22 bool lcl_GetPageDesc(SwWrtShell& rSh, sal_uInt16 &rPageNo, std::unique_ptr<const SwFormatPageDesc>* ppPageFormatDesc)
23 {
24 bool bRet = false;
26 if (rSh.GetCurAttr(aSet))
27 {
28 if (const SwFormatPageDesc* pDescItem = aSet.GetItemIfSet( RES_PAGEDESC ))
29 {
30 ::std::optional<sal_uInt16> oNumOffset = pDescItem->GetNumOffset();
31 if (oNumOffset)
32 rPageNo = *oNumOffset;
33 if (ppPageFormatDesc)
34 ppPageFormatDesc->reset(static_cast<const SwFormatPageDesc *>(pDescItem->Clone()));
35 bRet = true;
36 }
37 }
38 return bRet;
39 }
40
41 void lcl_ChangePage(SwWrtShell& rSh, sal_uInt16 nNewNumber, const SwPageDesc *pNewDesc)
42 {
43 const size_t nCurIdx = rSh.GetCurPageDesc();
44 const SwPageDesc &rCurrentDesc = rSh.GetPageDesc(nCurIdx);
45
46 std::unique_ptr<const SwFormatPageDesc> pPageFormatDesc;
47 sal_uInt16 nDontCare;
48 lcl_GetPageDesc(rSh, nDontCare, &pPageFormatDesc);
49
50 // If we want a new number then set it, otherwise reuse the existing one
51 sal_uInt16 nPgNo = 0;
52 if (nNewNumber)
53 {
54 // -1: Allow special case to prevent inheriting re-numbering from the existing page.
55 nPgNo = nNewNumber == SAL_MAX_UINT16 ? 0 : nNewNumber;
56 }
57 else if (pPageFormatDesc)
58 {
59 ::std::optional<sal_uInt16> oNumOffset = pPageFormatDesc->GetNumOffset();
60 if (oNumOffset)
61 nPgNo = *oNumOffset;
62 }
63
64 // If we want a new descriptor then set it, otherwise reuse the existing one
65 if (pNewDesc || nPgNo)
66 {
67 SwFormatPageDesc aPageFormatDesc(pNewDesc ? pNewDesc : &rCurrentDesc);
68 if (nPgNo) aPageFormatDesc.SetNumOffset(nPgNo);
69 rSh.SetAttrItem(aPageFormatDesc);
70 }
71 }
72
73 void lcl_PushCursor(SwWrtShell& rSh)
74 {
75 rSh.LockView(true);
76 rSh.StartAllAction();
77 rSh.SwCursorShell::Push();
78 }
79
80 void lcl_PopCursor(SwWrtShell& rSh)
81 {
82 rSh.SwCursorShell::Pop(SwCursorShell::PopMode::DeleteCurrent);
83 rSh.EndAllAction();
84 rSh.LockView(false);
85 }
86
87 sal_uInt16 lcl_GetCurrentPage(const SwWrtShell& rSh)
88 {
89 OUString sDummy;
90 sal_uInt16 nPhyNum=1, nVirtNum=1;
91 rSh.GetPageNumber(0, true, nPhyNum, nVirtNum, sDummy);
92 return nPhyNum;
93 }
94
95bool lcl_GotoPage(SwWrtShell& rSh, const sal_uInt16 nStartingPage, sal_uInt16 nOffset = 0)
96{
97 rSh.GotoPage(nStartingPage, /*bRecord=*/false);
98
99 sal_uInt16 nCurrentPage = lcl_GetCurrentPage(rSh);
100 // return false if at document end (unless that was the requested destination)
101 if (nCurrentPage == rSh.GetPageCnt())
102 return nCurrentPage == nStartingPage + nOffset;
103
104 if (nCurrentPage != nStartingPage)
105 {
106 assert(nStartingPage != 1 && "Physical page 1 couldn't be found/moved to?");
107 // Probably there is an auto-inserted blank page to handle odd/even, which Goto doesn't understand.
108 rSh.GotoPage(nStartingPage + 1, /*bRecord=*/false);
109
110 nCurrentPage = lcl_GetCurrentPage(rSh);
111 assert(nCurrentPage == nStartingPage + 1 && "Impossible, since unknown goes to last page");
112 if (nCurrentPage != nStartingPage + 1)
113 return false;
114 }
115 // Now that we have the correct starting point, move to the correct offset.
116 while (nOffset--)
117 rSh.SttNxtPg();
118 return true;
119}
120} // namespace
121
122/*
123 * Only include the Index page in the list if the page count implies one
124 * to reduce confusing things
125 */
127{
128 sal_uInt16 nTitlePages = m_xPageCountNF->get_value();
129 m_xPagePropertiesLB->clear();
130 if (mpTitleDesc)
131 m_xPagePropertiesLB->append_text(mpTitleDesc->GetName());
132 if (nTitlePages > 1 && mpIndexDesc)
133 m_xPagePropertiesLB->append_text(mpIndexDesc->GetName());
134 if (mpNormalDesc)
136 m_xPagePropertiesLB->set_active(0);
137}
138
140{
141 sal_uInt16 nPage = 1;
142 if (m_xPageStartNF->get_sensitive())
143 nPage = m_xPageStartNF->get_value();
144 return nPage;
145}
146
148 : SfxDialogController(pParent, "modules/swriter/ui/titlepage.ui", "DLG_TITLEPAGE")
149 , mpTitleDesc(nullptr)
150 , mpIndexDesc(nullptr)
151 , mpNormalDesc(nullptr)
152 , m_xUseExistingPagesRB(m_xBuilder->weld_radio_button("RB_USE_EXISTING_PAGES"))
153 , m_xInsertNewPagesRB(m_xBuilder->weld_radio_button("RB_INSERT_NEW_PAGES"))
154 , m_xPageCountNF(m_xBuilder->weld_spin_button("NF_PAGE_COUNT"))
155 , m_xDocumentStartRB(m_xBuilder->weld_radio_button("RB_DOCUMENT_START"))
156 , m_xPageStartRB(m_xBuilder->weld_radio_button("RB_PAGE_START"))
157 , m_xPageStartNF(m_xBuilder->weld_spin_button("NF_PAGE_START"))
158 , m_xRestartNumberingCB(m_xBuilder->weld_check_button("CB_RESTART_NUMBERING"))
159 , m_xRestartNumberingNF(m_xBuilder->weld_spin_button("NF_RESTART_NUMBERING"))
160 , m_xSetPageNumberCB(m_xBuilder->weld_check_button("CB_SET_PAGE_NUMBER"))
161 , m_xSetPageNumberNF(m_xBuilder->weld_spin_button("NF_SET_PAGE_NUMBER"))
162 , m_xPagePropertiesLB(m_xBuilder->weld_combo_box("LB_PAGE_PROPERTIES"))
163 , m_xPagePropertiesPB(m_xBuilder->weld_button("PB_PAGE_PROPERTIES"))
164 , m_xOkPB(m_xBuilder->weld_button("ok"))
165{
166 SwView* pView = GetActiveView();
167 if (!pView)
168 return;
169 SwWrtShell& rWrtShell = pView->GetWrtShell();
170
171 m_xOkPB->connect_clicked(LINK(this, SwTitlePageDlg, OKHdl));
172 m_xRestartNumberingCB->connect_toggled(LINK(this, SwTitlePageDlg, RestartNumberingHdl));
173 m_xSetPageNumberCB->connect_toggled(LINK(this, SwTitlePageDlg, SetPageNumberHdl));
174 m_xPageStartNF->set_max(rWrtShell.GetPageCnt() + 1);
175
176 sal_uInt16 nSetPage = 1;
177 sal_uInt16 nResetPage = 1;
178 sal_uInt16 nTitlePages = 1;
179 lcl_PushCursor(rWrtShell);
180
181 SwView& rView = rWrtShell.GetView();
182 rView.InvalidateRulerPos();
183
184 bool bMaybeResetNumbering = false;
185
189
190 rWrtShell.StartOfSection();
191 if (lcl_GetPageDesc(rWrtShell, nSetPage, &mpPageFormatDesc))
192 {
193 if (mpPageFormatDesc->GetPageDesc() == mpTitleDesc)
194 {
195 while (rWrtShell.SttNxtPg())
196 {
197 const size_t nCurIdx = rWrtShell.GetCurPageDesc();
198 const SwPageDesc& rPageDesc = rWrtShell.GetPageDesc(nCurIdx);
199
200 if (mpIndexDesc != &rPageDesc)
201 {
202 mpNormalDesc = &rPageDesc;
203 bMaybeResetNumbering = lcl_GetPageDesc(rWrtShell, nResetPage, nullptr);
204 break;
205 }
206 ++nTitlePages;
207 }
208 }
209 }
210 lcl_PopCursor(rWrtShell);
211
212 m_xUseExistingPagesRB->set_active(true);
213 m_xPageCountNF->set_value(nTitlePages);
214 m_xPageCountNF->connect_value_changed(LINK(this, SwTitlePageDlg, ValueChangeHdl));
215
216 m_xDocumentStartRB->set_active(true);
217 m_xPageStartNF->set_sensitive(false);
218 m_xPageStartNF->set_value(lcl_GetCurrentPage(rWrtShell));
219 Link<weld::Toggleable&,void> aStartPageHdl = LINK(this, SwTitlePageDlg, StartPageHdl);
220 m_xDocumentStartRB->connect_toggled(aStartPageHdl);
221 m_xPageStartRB->connect_toggled(aStartPageHdl);
222
223 m_xRestartNumberingNF->set_value(nResetPage);
224 if (bMaybeResetNumbering && nResetPage > 0)
225 {
226 m_xRestartNumberingCB->set_active(true);
227 }
228 m_xRestartNumberingNF->set_sensitive(m_xRestartNumberingCB->get_active());
229
230 m_xSetPageNumberNF->set_value(nSetPage);
231 if (nSetPage > 1)
232 m_xSetPageNumberCB->set_active(true);
233 m_xSetPageNumberNF->set_sensitive(m_xSetPageNumberCB->get_active());
234
235 FillList();
236 m_xPagePropertiesPB->connect_clicked(LINK(this, SwTitlePageDlg, EditHdl));
237}
238
240{
241 if (m_xPageCountNF->get_value() == 1 || m_xPageCountNF->get_value() == 2)
242 FillList();
243}
244
246{
247 m_xRestartNumberingNF->set_sensitive(m_xRestartNumberingCB->get_active());
248}
249
251{
252 m_xSetPageNumberNF->set_sensitive(m_xSetPageNumberCB->get_active());
253}
254
256{
257 m_xPageStartNF->set_sensitive(m_xPageStartRB->get_active());
258}
259
261{
262}
263
265{
266 SwView* pView = GetActiveView();
267 if (!pView)
268 return;
269 SwWrtShell& rWrtShell = pView->GetWrtShell();
270 SwView& rView = rWrtShell.GetView();
271 rView.GetDocShell()->FormatPage(getDialog(), m_xPagePropertiesLB->get_active_text(), "page", rWrtShell);
272 rView.InvalidateRulerPos();
273}
274
276{
277 SwView* pView = GetActiveView();
278 if (!pView)
279 return;
280 SwWrtShell& rWrtShell = pView->GetWrtShell();
281 lcl_PushCursor(rWrtShell);
282
283 rWrtShell.StartUndo();
284
285 SwFormatPageDesc aTitleDesc(mpTitleDesc);
286
287 if (m_xSetPageNumberCB->get_active())
288 aTitleDesc.SetNumOffset(m_xSetPageNumberNF->get_value());
289 else if (mpPageFormatDesc)
290 aTitleDesc.SetNumOffset(mpPageFormatDesc->GetNumOffset());
291
292 sal_uInt16 nNumTitlePages = m_xPageCountNF->get_value();
293 if (!m_xUseExistingPagesRB->get_active())
294 {
295 // Assuming that a failure to GotoPage means the end of the document,
296 // insert new pages after the last page.
297 if (!lcl_GotoPage(rWrtShell, GetInsertPosition()))
298 {
299 rWrtShell.EndPg();
300 // Add one more page as a content page to follow the new title pages.
301 rWrtShell.InsertPageBreak();
302 }
303 for (sal_uInt16 nI = 0; nI < nNumTitlePages; ++nI)
304 rWrtShell.InsertPageBreak();
305 // In order to be able to access these new pages, the layout needs to be recalculated first.
306 rWrtShell.CalcLayout();
307 }
308
309 if (lcl_GotoPage(rWrtShell, GetInsertPosition()))
310 {
311 rWrtShell.SetAttrItem(aTitleDesc);
312 for (sal_uInt16 nI = 1; nI < nNumTitlePages; ++nI)
313 {
314 if (rWrtShell.SttNxtPg())
315 lcl_ChangePage(rWrtShell, SAL_MAX_UINT16, mpIndexDesc);
316 }
317 }
318
319 if ((m_xRestartNumberingCB->get_active() || nNumTitlePages > 1)
320 && lcl_GotoPage(rWrtShell, GetInsertPosition(), nNumTitlePages))
321 {
322 sal_uInt16 nPgNo
323 = m_xRestartNumberingCB->get_active() ? m_xRestartNumberingNF->get_value() : 0;
324 const SwPageDesc* pNewDesc = nNumTitlePages > 1 ? mpNormalDesc : nullptr;
325 lcl_ChangePage(rWrtShell, nPgNo, pNewDesc);
326 }
327
328 rWrtShell.EndUndo();
329 lcl_PopCursor(rWrtShell);
330 if (!m_xUseExistingPagesRB->get_active())
331 lcl_GotoPage(rWrtShell, GetInsertPosition());
332 m_xDialog->response(RET_OK);
333}
334
335/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
sal_uInt16 GetPageCnt()
Definition: crsrsh.cxx:1591
void FormatPage(weld::Window *pDialogParent, const OUString &rPage, const OUString &rPageId, SwWrtShell &rActShell, SfxRequest *pRequest=nullptr)
Show page style format dialog.
Definition: docst.cxx:1681
virtual void CalcLayout() override
To enable set up of StartActions and EndActions.
Definition: edws.cxx:108
bool GetCurAttr(SfxItemSet &, const bool bMergeIndentValuesOfNumRule=false) const
Definition: edattr.cxx:171
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
SwUndoId StartUndo(SwUndoId eUndoId=SwUndoId::EMPTY, const SwRewriter *pRewriter=nullptr)
Undo: set up Undo parenthesis, return nUndoId of this parenthesis.
Definition: edws.cxx:223
void SetAttrItem(const SfxPoolItem &, SetAttrMode nFlags=SetAttrMode::DEFAULT, const bool bParagraphSetting=false)
Definition: edatmisc.cxx:98
SwPageDesc * GetPageDescFromPool(sal_uInt16 nId)
Definition: edfmt.cxx:132
SwUndoId EndUndo(SwUndoId eUndoId=SwUndoId::EMPTY, const SwRewriter *pRewriter=nullptr)
Closes parenthesis of nUndoId, not used by UI.
Definition: edws.cxx:234
void EndAllAction()
Definition: edws.cxx:97
size_t GetCurPageDesc(const bool bCalcFrame=true) const
Definition: fedesc.cxx:167
const SwPageDesc & GetPageDesc(size_t i) const
Definition: fedesc.cxx:126
sal_uInt16 GetPageNumber(const Point &rPoint) const
Page number of the page containing Point, O if no page.
Definition: fews.cxx:185
Pagedescriptor Client of SwPageDesc that is "described" by the attribute.
Definition: fmtpdsc.hxx:36
void SetNumOffset(const ::std::optional< sal_uInt16 > &oNum)
Definition: fmtpdsc.hxx:65
const OUString & GetName() const
Definition: pagedesc.hxx:196
std::unique_ptr< weld::RadioButton > m_xDocumentStartRB
Definition: titlepage.hxx:36
std::unique_ptr< weld::SpinButton > m_xPageStartNF
Definition: titlepage.hxx:38
const SwPageDesc * mpIndexDesc
Definition: titlepage.hxx:30
std::unique_ptr< weld::RadioButton > m_xUseExistingPagesRB
Definition: titlepage.hxx:33
std::unique_ptr< weld::ComboBox > m_xPagePropertiesLB
Definition: titlepage.hxx:43
virtual ~SwTitlePageDlg() override
Definition: titlepage.cxx:260
SwTitlePageDlg(weld::Window *pParent)
Definition: titlepage.cxx:147
sal_uInt16 GetInsertPosition() const
Definition: titlepage.cxx:139
std::unique_ptr< weld::SpinButton > m_xRestartNumberingNF
Definition: titlepage.hxx:40
std::unique_ptr< weld::CheckButton > m_xSetPageNumberCB
Definition: titlepage.hxx:41
std::unique_ptr< weld::SpinButton > m_xSetPageNumberNF
Definition: titlepage.hxx:42
std::unique_ptr< weld::CheckButton > m_xRestartNumberingCB
Definition: titlepage.hxx:39
std::unique_ptr< const SwFormatPageDesc > mpPageFormatDesc
Definition: titlepage.hxx:27
std::unique_ptr< weld::RadioButton > m_xPageStartRB
Definition: titlepage.hxx:37
SwPageDesc * mpTitleDesc
Definition: titlepage.hxx:29
const SwPageDesc * mpNormalDesc
Definition: titlepage.hxx:31
std::unique_ptr< weld::Button > m_xPagePropertiesPB
Definition: titlepage.hxx:44
std::unique_ptr< weld::Button > m_xOkPB
Definition: titlepage.hxx:45
std::unique_ptr< weld::SpinButton > m_xPageCountNF
Definition: titlepage.hxx:35
void LockView(bool b)
Definition: viewsh.hxx:491
const SfxItemPool & GetAttrPool() const
Definition: viewsh.hxx:648
Definition: view.hxx:146
SwWrtShell & GetWrtShell() const
Definition: view.hxx:423
SwDocShell * GetDocShell()
Definition: view.cxx:1193
void InvalidateRulerPos()
Definition: viewport.cxx:107
Used by the UI to modify the document model.
Definition: wrtsh.hxx:97
bool EndPg(bool bSelect=false)
Definition: move.cxx:388
bool GotoPage(sal_uInt16 nPage, bool bRecord)
Definition: move.cxx:611
bool SttNxtPg(bool bSelect=false)
Definition: move.cxx:358
void InsertPageBreak(const OUString *pPageDesc=nullptr, const ::std::optional< sal_uInt16 > &rPgNum=std::nullopt)
Definition: wrtsh1.cxx:941
bool StartOfSection(bool bSelect=false)
Definition: move.cxx:346
const SwView & GetView() const
Definition: wrtsh.hxx:443
constexpr TypedWhichId< SwFormatPageDesc > RES_PAGEDESC(99)
@ RES_POOLPAGE_REGISTER
Index.
Definition: poolfmt.hxx:175
@ RES_POOLPAGE_FIRST
First page.
Definition: poolfmt.hxx:171
@ RES_POOLPAGE_STANDARD
Standard page.
Definition: poolfmt.hxx:170
SwView * GetActiveView()
Definition: swmodul1.cxx:115
IMPL_LINK_NOARG(SwTitlePageDlg, ValueChangeHdl, weld::SpinButton &, void)
Definition: titlepage.cxx:239
#define SAL_MAX_UINT16
static SwPageDesc * lcl_GetPageDesc(SwDoc *pDoc, const uno::Any &aValue)
Definition: unosett.cxx:222
RET_OK