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_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 SwView* pView = GetActiveView();
166 if (!pView)
167 return;
168 SwWrtShell& rWrtShell = pView->GetWrtShell();
169
170 m_xOkPB->connect_clicked(LINK(this, SwTitlePageDlg, OKHdl));
171 m_xRestartNumberingCB->connect_toggled(LINK(this, SwTitlePageDlg, RestartNumberingHdl));
172 m_xSetPageNumberCB->connect_toggled(LINK(this, SwTitlePageDlg, SetPageNumberHdl));
173 m_xPageStartNF->set_max(rWrtShell.GetPageCnt() + 1);
174
175 sal_uInt16 nSetPage = 1;
176 sal_uInt16 nResetPage = 1;
177 sal_uInt16 nTitlePages = 1;
178 lcl_PushCursor(rWrtShell);
179
180 SwView& rView = rWrtShell.GetView();
181 rView.InvalidateRulerPos();
182
183 bool bMaybeResetNumbering = false;
184
188
189 rWrtShell.StartOfSection();
190 if (lcl_GetPageDesc(rWrtShell, nSetPage, &mpPageFormatDesc))
191 {
192 if (mpPageFormatDesc->GetPageDesc() == mpTitleDesc)
193 {
194 while (rWrtShell.SttNxtPg())
195 {
196 const size_t nCurIdx = rWrtShell.GetCurPageDesc();
197 const SwPageDesc& rPageDesc = rWrtShell.GetPageDesc(nCurIdx);
198
199 if (mpIndexDesc != &rPageDesc)
200 {
201 mpNormalDesc = &rPageDesc;
202 bMaybeResetNumbering = lcl_GetPageDesc(rWrtShell, nResetPage, nullptr);
203 break;
204 }
205 ++nTitlePages;
206 }
207 }
208 }
209 lcl_PopCursor(rWrtShell);
210
211 m_xUseExistingPagesRB->set_active(true);
212 m_xPageCountNF->set_value(nTitlePages);
213 m_xPageCountNF->connect_value_changed(LINK(this, SwTitlePageDlg, ValueChangeHdl));
214
215 m_xDocumentStartRB->set_active(true);
216 m_xPageStartNF->set_sensitive(false);
217 m_xPageStartNF->set_value(lcl_GetCurrentPage(rWrtShell));
218 Link<weld::Toggleable&,void> aStartPageHdl = LINK(this, SwTitlePageDlg, StartPageHdl);
219 m_xDocumentStartRB->connect_toggled(aStartPageHdl);
220 m_xPageStartRB->connect_toggled(aStartPageHdl);
221
222 m_xRestartNumberingNF->set_value(nResetPage);
223 if (bMaybeResetNumbering && nResetPage > 0)
224 {
225 m_xRestartNumberingCB->set_active(true);
226 }
227 m_xRestartNumberingNF->set_sensitive(m_xRestartNumberingCB->get_active());
228
229 m_xSetPageNumberNF->set_value(nSetPage);
230 if (nSetPage > 1)
231 m_xSetPageNumberCB->set_active(true);
232 m_xSetPageNumberNF->set_sensitive(m_xSetPageNumberCB->get_active());
233
234 FillList();
235 m_xPagePropertiesPB->connect_clicked(LINK(this, SwTitlePageDlg, EditHdl));
236}
237
239{
240 if (m_xPageCountNF->get_value() == 1 || m_xPageCountNF->get_value() == 2)
241 FillList();
242}
243
245{
246 m_xRestartNumberingNF->set_sensitive(m_xRestartNumberingCB->get_active());
247}
248
250{
251 m_xSetPageNumberNF->set_sensitive(m_xSetPageNumberCB->get_active());
252}
253
255{
256 m_xPageStartNF->set_sensitive(m_xPageStartRB->get_active());
257}
258
260{
261}
262
264{
265 SwView* pView = GetActiveView();
266 if (!pView)
267 return;
268 SwWrtShell& rWrtShell = pView->GetWrtShell();
269 SwView& rView = rWrtShell.GetView();
270 rView.GetDocShell()->FormatPage(getDialog(), m_xPagePropertiesLB->get_active_text(), "page", rWrtShell);
271 rView.InvalidateRulerPos();
272}
273
275{
276 SwView* pView = GetActiveView();
277 if (!pView)
278 return;
279 SwWrtShell& rWrtShell = pView->GetWrtShell();
280 lcl_PushCursor(rWrtShell);
281
282 rWrtShell.StartUndo();
283
284 SwFormatPageDesc aTitleDesc(mpTitleDesc);
285
286 if (m_xSetPageNumberCB->get_active())
287 aTitleDesc.SetNumOffset(m_xSetPageNumberNF->get_value());
288 else if (mpPageFormatDesc)
289 aTitleDesc.SetNumOffset(mpPageFormatDesc->GetNumOffset());
290
291 sal_uInt16 nNumTitlePages = m_xPageCountNF->get_value();
292 if (!m_xUseExistingPagesRB->get_active())
293 {
294 // Assuming that a failure to GotoPage means the end of the document,
295 // insert new pages after the last page.
296 if (!lcl_GotoPage(rWrtShell, GetInsertPosition()))
297 {
298 rWrtShell.EndPg();
299 // Add one more page as a content page to follow the new title pages.
300 rWrtShell.InsertPageBreak();
301 }
302 for (sal_uInt16 nI = 0; nI < nNumTitlePages; ++nI)
303 rWrtShell.InsertPageBreak();
304 // In order to be able to access these new pages, the layout needs to be recalculated first.
305 rWrtShell.CalcLayout();
306 }
307
308 if (lcl_GotoPage(rWrtShell, GetInsertPosition()))
309 {
310 rWrtShell.SetAttrItem(aTitleDesc);
311 for (sal_uInt16 nI = 1; nI < nNumTitlePages; ++nI)
312 {
313 if (rWrtShell.SttNxtPg())
314 lcl_ChangePage(rWrtShell, SAL_MAX_UINT16, mpIndexDesc);
315 }
316 }
317
318 if ((m_xRestartNumberingCB->get_active() || nNumTitlePages > 1)
319 && lcl_GotoPage(rWrtShell, GetInsertPosition(), nNumTitlePages))
320 {
321 sal_uInt16 nPgNo
322 = m_xRestartNumberingCB->get_active() ? m_xRestartNumberingNF->get_value() : 0;
323 const SwPageDesc* pNewDesc = nNumTitlePages > 1 ? mpNormalDesc : nullptr;
324 lcl_ChangePage(rWrtShell, nPgNo, pNewDesc);
325 }
326
327 rWrtShell.EndUndo();
328 lcl_PopCursor(rWrtShell);
329 if (!m_xUseExistingPagesRB->get_active())
330 lcl_GotoPage(rWrtShell, GetInsertPosition());
331 m_xDialog->response(RET_OK);
332}
333
334/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
sal_uInt16 GetPageCnt()
Definition: crsrsh.cxx:1559
void FormatPage(weld::Window *pDialogParent, const OUString &rPage, const OUString &rPageId, SwWrtShell &rActShell, SfxRequest *pRequest=nullptr)
Show page style format dialog.
Definition: docst.cxx:1650
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:35
std::unique_ptr< weld::SpinButton > m_xPageStartNF
Definition: titlepage.hxx:37
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:42
virtual ~SwTitlePageDlg() override
Definition: titlepage.cxx:259
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:39
std::unique_ptr< weld::CheckButton > m_xSetPageNumberCB
Definition: titlepage.hxx:40
std::unique_ptr< weld::SpinButton > m_xSetPageNumberNF
Definition: titlepage.hxx:41
std::unique_ptr< weld::CheckButton > m_xRestartNumberingCB
Definition: titlepage.hxx:38
std::unique_ptr< const SwFormatPageDesc > mpPageFormatDesc
Definition: titlepage.hxx:27
std::unique_ptr< weld::RadioButton > m_xPageStartRB
Definition: titlepage.hxx:36
SwPageDesc * mpTitleDesc
Definition: titlepage.hxx:29
const SwPageDesc * mpNormalDesc
Definition: titlepage.hxx:31
std::unique_ptr< weld::Button > m_xPagePropertiesPB
Definition: titlepage.hxx:43
std::unique_ptr< weld::Button > m_xOkPB
Definition: titlepage.hxx:44
std::unique_ptr< weld::SpinButton > m_xPageCountNF
Definition: titlepage.hxx:34
void LockView(bool b)
Definition: viewsh.hxx:490
const SfxItemPool & GetAttrPool() const
Definition: viewsh.hxx:649
Definition: view.hxx:146
SwWrtShell & GetWrtShell() const
Definition: view.hxx:423
SwDocShell * GetDocShell()
Definition: view.cxx:1180
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:238
#define SAL_MAX_UINT16
static SwPageDesc * lcl_GetPageDesc(SwDoc *pDoc, const uno::Any &aValue)
Definition: unosett.cxx:222
RET_OK