1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <sfx2/viewfrm.hxx>
12 #include <swmodule.hxx>
14 #include <poolfmt.hxx>
16 #include <charfmt.hxx>
17 #include <docstyle.hxx>
20 #include <lineinfo.hxx>
21 #include <globals.hrc>
22 #include <titlepage.hxx>
24 #include <fmtpdsc.hxx>
25 #include <pagedesc.hxx>
27 #include <IDocumentStylePoolAccess.hxx>
31 bool lcl_GetPageDesc(SwWrtShell
*pSh
, sal_uInt16
&rPageNo
, std::unique_ptr
<const SwFormatPageDesc
>* ppPageFormatDesc
)
34 SfxItemSet
aSet( pSh
->GetAttrPool(), svl::Items
<RES_PAGEDESC
, RES_PAGEDESC
>{} );
35 if (pSh
->GetCurAttr( aSet
))
37 const SfxPoolItem
* pItem(nullptr);
38 if (SfxItemState::SET
== aSet
.GetItemState( RES_PAGEDESC
, true, &pItem
) && pItem
)
40 ::boost::optional
<sal_uInt16
> oNumOffset
= static_cast<const SwFormatPageDesc
*>(pItem
)->GetNumOffset();
42 rPageNo
= oNumOffset
.get();
44 ppPageFormatDesc
->reset(static_cast<const SwFormatPageDesc
*>(pItem
->Clone()));
51 void lcl_ChangePage(SwWrtShell
*pSh
, sal_uInt16 nNewNumber
,
52 const SwPageDesc
*pNewDesc
)
54 const size_t nCurIdx
= pSh
->GetCurPageDesc();
55 const SwPageDesc
&rCurrentDesc
= pSh
->GetPageDesc( nCurIdx
);
57 std::unique_ptr
<const SwFormatPageDesc
> pPageFormatDesc
;
59 lcl_GetPageDesc(pSh
, nDontCare
, &pPageFormatDesc
);
61 // If we want a new number then set it, otherwise reuse the existing one
71 ::boost::optional
<sal_uInt16
> oNumOffset
= pPageFormatDesc
->GetNumOffset();
74 nPgNo
= oNumOffset
.get();
87 // If we want a new descriptor then set it, otherwise reuse the existing one
90 SwFormatPageDesc
aPageFormatDesc(pPageFormatDesc
? *pPageFormatDesc
: &rCurrentDesc
);
91 if (nPgNo
) aPageFormatDesc
.SetNumOffset(nPgNo
);
92 pSh
->SetAttrItem(aPageFormatDesc
);
96 SwFormatPageDesc
aPageFormatDesc(pNewDesc
);
97 if (nPgNo
) aPageFormatDesc
.SetNumOffset(nPgNo
);
98 pSh
->SetAttrItem(aPageFormatDesc
);
102 void lcl_PushCursor(SwWrtShell
*pSh
)
104 pSh
->LockView( true );
105 pSh
->StartAllAction();
106 pSh
->SwCursorShell::Push();
109 void lcl_PopCursor(SwWrtShell
*pSh
)
111 pSh
->SwCursorShell::Pop(SwCursorShell::PopMode::DeleteCurrent
);
113 pSh
->LockView( false );
116 sal_uInt16
lcl_GetCurrentPage(SwWrtShell
const *pSh
)
119 sal_uInt16 nPhyNum
=1, nVirtNum
=1;
120 pSh
->GetPageNumber(0, true, nPhyNum
, nVirtNum
, sDummy
);
126 * Only include the Index page in the list if the page count implies one
127 * to reduce confusing things
129 void SwTitlePageDlg::FillList()
131 sal_uInt16 nTitlePages
= m_xPageCountNF
->get_value();
132 m_xPagePropertiesLB
->clear();
134 m_xPagePropertiesLB
->append_text(mpTitleDesc
->GetName());
135 if (nTitlePages
> 1 && mpIndexDesc
)
136 m_xPagePropertiesLB
->append_text(mpIndexDesc
->GetName());
138 m_xPagePropertiesLB
->append_text(mpNormalDesc
->GetName());
139 m_xPagePropertiesLB
->set_active(0);
142 sal_uInt16
SwTitlePageDlg::GetInsertPosition() const
144 sal_uInt16 nPage
= 1;
145 if (m_xPageStartNF
->get_sensitive())
146 nPage
= m_xPageStartNF
->get_value();
150 SwTitlePageDlg::SwTitlePageDlg(weld::Window
*pParent
)
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"))
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
));
169 sal_uInt16 nSetPage
= 1;
170 sal_uInt16 nResetPage
= 1;
171 sal_uInt16 nTitlePages
= 1;
172 mpSh
= ::GetActiveView()->GetWrtShellPtr();
173 lcl_PushCursor(mpSh
);
175 SwView
& rView
= mpSh
->GetView();
176 rView
.InvalidateRulerPos();
178 bool bMaybeResetNumbering
= false;
180 mpTitleDesc
= mpSh
->GetPageDescFromPool(RES_POOLPAGE_FIRST
);
181 mpIndexDesc
= mpSh
->GetPageDescFromPool(RES_POOLPAGE_REGISTER
);
182 mpNormalDesc
= mpSh
->GetPageDescFromPool(RES_POOLPAGE_STANDARD
);
184 mpSh
->StartOfSection();
185 if (lcl_GetPageDesc( mpSh
, nSetPage
, &mpPageFormatDesc
))
187 if (mpPageFormatDesc
->GetPageDesc() == mpTitleDesc
)
189 while (mpSh
->SttNxtPg())
191 const size_t nCurIdx
= mpSh
->GetCurPageDesc();
192 const SwPageDesc
&rPageDesc
= mpSh
->GetPageDesc( nCurIdx
);
194 if (mpIndexDesc
!= &rPageDesc
)
196 mpNormalDesc
= &rPageDesc
;
197 bMaybeResetNumbering
= lcl_GetPageDesc(mpSh
, nResetPage
, nullptr);
206 m_xUseExistingPagesRB
->set_active(true);
207 m_xPageCountNF
->set_value(nTitlePages
);
208 m_xPageCountNF
->connect_value_changed(LINK(this, SwTitlePageDlg
, ValueChangeHdl
));
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
);
217 if (bMaybeResetNumbering
&& nResetPage
> 0)
219 m_xRestartNumberingCB
->set_active(true);
220 m_xRestartNumberingNF
->set_value(nResetPage
);
222 m_xRestartNumberingNF
->set_sensitive(m_xRestartNumberingCB
->get_active());
224 m_xSetPageNumberNF
->set_value(nSetPage
);
226 m_xSetPageNumberCB
->set_active(true);
227 m_xSetPageNumberNF
->set_sensitive(m_xSetPageNumberCB
->get_active());
230 m_xPagePropertiesPB
->connect_clicked(LINK(this, SwTitlePageDlg
, EditHdl
));
233 IMPL_LINK_NOARG(SwTitlePageDlg
, ValueChangeHdl
, weld::SpinButton
&, void)
235 if (m_xPageCountNF
->get_value() == 1 || m_xPageCountNF
->get_value() == 2)
239 IMPL_LINK_NOARG(SwTitlePageDlg
, RestartNumberingHdl
, weld::ToggleButton
&, void)
241 m_xRestartNumberingNF
->set_sensitive(m_xRestartNumberingCB
->get_active());
244 IMPL_LINK_NOARG(SwTitlePageDlg
, SetPageNumberHdl
, weld::ToggleButton
&, void)
246 m_xSetPageNumberNF
->set_sensitive(m_xSetPageNumberCB
->get_active());
249 IMPL_LINK_NOARG(SwTitlePageDlg
, StartPageHdl
, weld::ToggleButton
&, void)
251 m_xPageStartNF
->set_sensitive(m_xPageStartRB
->get_active());
254 SwTitlePageDlg::~SwTitlePageDlg()
258 IMPL_LINK_NOARG(SwTitlePageDlg
, EditHdl
, weld::Button
&, void)
260 SwView
& rView
= mpSh
->GetView();
261 rView
.GetDocShell()->FormatPage(m_xPagePropertiesLB
->get_active_text(), "page", *mpSh
);
262 rView
.InvalidateRulerPos();
265 IMPL_LINK_NOARG(SwTitlePageDlg
, OKHdl
, weld::Button
&, void)
267 lcl_PushCursor(mpSh
);
271 SwFormatPageDesc
aTitleDesc(mpTitleDesc
);
273 if (m_xSetPageNumberCB
->get_active())
274 aTitleDesc
.SetNumOffset(m_xSetPageNumberNF
->get_value());
275 else if (mpPageFormatDesc
)
276 aTitleDesc
.SetNumOffset(mpPageFormatDesc
->GetNumOffset());
278 sal_uInt16 nNoPages
= m_xPageCountNF
->get_value();
279 if (!m_xUseExistingPagesRB
->get_active())
281 mpSh
->GotoPage(GetInsertPosition(), false);
282 for (sal_uInt16 nI
=0; nI
< nNoPages
; ++nI
)
283 mpSh
->InsertPageBreak();
286 mpSh
->GotoPage(GetInsertPosition(), false);
287 for (sal_uInt16 nI
=1; nI
< nNoPages
; ++nI
)
289 if (mpSh
->SttNxtPg())
290 lcl_ChangePage(mpSh
, 0, mpIndexDesc
);
293 mpSh
->GotoPage(GetInsertPosition(), false);
294 mpSh
->SetAttrItem(aTitleDesc
);
296 if (nNoPages
> 1 && mpSh
->GotoPage(GetInsertPosition() + nNoPages
, false))
298 SwFormatPageDesc
aPageFormatDesc(mpNormalDesc
);
299 mpSh
->SetAttrItem(aPageFormatDesc
);
302 if (m_xRestartNumberingCB
->get_active() || nNoPages
> 1)
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
);
312 if (!m_xUseExistingPagesRB
->get_active())
313 mpSh
->GotoPage(GetInsertPosition(), false);
314 m_xDialog
->response(RET_OK
);
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */