1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <view.hxx>
#include <swmodule.hxx>
#include <wrtsh.hxx>
#include <poolfmt.hxx>
#include <docsh.hxx>
#include <titlepage.hxx>
#include <fmtpdsc.hxx>
#include <pagedesc.hxx>
namespace
{
bool lcl_GetPageDesc(SwWrtShell& rSh, sal_uInt16 &rPageNo, std::unique_ptr<const SwFormatPageDesc>* ppPageFormatDesc)
{
bool bRet = false;
SfxItemSetFixed<RES_PAGEDESC, RES_PAGEDESC> aSet(rSh.GetAttrPool());
if (rSh.GetCurAttr(aSet))
{
if (const SwFormatPageDesc* pDescItem = aSet.GetItemIfSet( RES_PAGEDESC ))
{
::std::optional<sal_uInt16> oNumOffset = pDescItem->GetNumOffset();
if (oNumOffset)
rPageNo = *oNumOffset;
if (ppPageFormatDesc)
ppPageFormatDesc->reset(static_cast<const SwFormatPageDesc *>(pDescItem->Clone()));
bRet = true;
}
}
return bRet;
}
void lcl_ChangePage(SwWrtShell& rSh, sal_uInt16 nNewNumber, const SwPageDesc *pNewDesc)
{
const size_t nCurIdx = rSh.GetCurPageDesc();
const SwPageDesc &rCurrentDesc = rSh.GetPageDesc(nCurIdx);
std::unique_ptr<const SwFormatPageDesc> pPageFormatDesc;
sal_uInt16 nDontCare;
lcl_GetPageDesc(rSh, nDontCare, &pPageFormatDesc);
// If we want a new number then set it, otherwise reuse the existing one
sal_uInt16 nPgNo = 0;
if (nNewNumber)
{
// -1: Allow special case to prevent inheriting re-numbering from the existing page.
nPgNo = nNewNumber == SAL_MAX_UINT16 ? 0 : nNewNumber;
}
else if (pPageFormatDesc)
{
::std::optional<sal_uInt16> oNumOffset = pPageFormatDesc->GetNumOffset();
if (oNumOffset)
nPgNo = *oNumOffset;
}
// If we want a new descriptor then set it, otherwise reuse the existing one
if (pNewDesc || nPgNo)
{
SwFormatPageDesc aPageFormatDesc(pNewDesc ? pNewDesc : &rCurrentDesc);
if (nPgNo) aPageFormatDesc.SetNumOffset(nPgNo);
rSh.SetAttrItem(aPageFormatDesc);
}
}
void lcl_PushCursor(SwWrtShell& rSh)
{
rSh.LockView(true);
rSh.StartAllAction();
rSh.SwCursorShell::Push();
}
void lcl_PopCursor(SwWrtShell& rSh)
{
rSh.SwCursorShell::Pop(SwCursorShell::PopMode::DeleteCurrent);
rSh.EndAllAction();
rSh.LockView(false);
}
sal_uInt16 lcl_GetCurrentPage(const SwWrtShell& rSh)
{
OUString sDummy;
sal_uInt16 nPhyNum=1, nVirtNum=1;
rSh.GetPageNumber(0, true, nPhyNum, nVirtNum, sDummy);
return nPhyNum;
}
bool lcl_GotoPage(SwWrtShell& rSh, const sal_uInt16 nStartingPage, sal_uInt16 nOffset = 0)
{
rSh.GotoPage(nStartingPage, /*bRecord=*/false);
sal_uInt16 nCurrentPage = lcl_GetCurrentPage(rSh);
// return false if at document end (unless that was the requested destination)
if (nCurrentPage == rSh.GetPageCnt())
return nCurrentPage == nStartingPage + nOffset;
if (nCurrentPage != nStartingPage)
{
assert(nStartingPage != 1 && "Physical page 1 couldn't be found/moved to?");
// Probably there is an auto-inserted blank page to handle odd/even, which Goto doesn't understand.
rSh.GotoPage(nStartingPage + 1, /*bRecord=*/false);
nCurrentPage = lcl_GetCurrentPage(rSh);
assert(nCurrentPage == nStartingPage + 1 && "Impossible, since unknown goes to last page");
if (nCurrentPage != nStartingPage + 1)
return false;
}
// Now that we have the correct starting point, move to the correct offset.
while (nOffset--)
rSh.SttNxtPg();
return true;
}
} // namespace
/*
* Only include the Index page in the list if the page count implies one
* to reduce confusing things
*/
void SwTitlePageDlg::FillList()
{
sal_uInt16 nTitlePages = m_xPageCountNF->get_value();
m_xPagePropertiesLB->clear();
if (mpTitleDesc)
m_xPagePropertiesLB->append_text(mpTitleDesc->GetName());
if (nTitlePages > 1 && mpIndexDesc)
m_xPagePropertiesLB->append_text(mpIndexDesc->GetName());
if (mpNormalDesc)
m_xPagePropertiesLB->append_text(mpNormalDesc->GetName());
m_xPagePropertiesLB->set_active(0);
}
sal_uInt16 SwTitlePageDlg::GetInsertPosition() const
{
sal_uInt16 nPage = 1;
if (m_xPageStartNF->get_sensitive())
nPage = m_xPageStartNF->get_value();
return nPage;
}
SwTitlePageDlg::SwTitlePageDlg(weld::Window *pParent)
: SfxDialogController(pParent, "modules/swriter/ui/titlepage.ui", "DLG_TITLEPAGE")
, mpTitleDesc(nullptr)
, mpIndexDesc(nullptr)
, mpNormalDesc(nullptr)
, m_xUseExistingPagesRB(m_xBuilder->weld_radio_button("RB_USE_EXISTING_PAGES"))
, m_xPageCountNF(m_xBuilder->weld_spin_button("NF_PAGE_COUNT"))
, m_xDocumentStartRB(m_xBuilder->weld_radio_button("RB_DOCUMENT_START"))
, m_xPageStartRB(m_xBuilder->weld_radio_button("RB_PAGE_START"))
, m_xPageStartNF(m_xBuilder->weld_spin_button("NF_PAGE_START"))
, m_xRestartNumberingCB(m_xBuilder->weld_check_button("CB_RESTART_NUMBERING"))
, m_xRestartNumberingNF(m_xBuilder->weld_spin_button("NF_RESTART_NUMBERING"))
, m_xSetPageNumberCB(m_xBuilder->weld_check_button("CB_SET_PAGE_NUMBER"))
, m_xSetPageNumberNF(m_xBuilder->weld_spin_button("NF_SET_PAGE_NUMBER"))
, m_xPagePropertiesLB(m_xBuilder->weld_combo_box("LB_PAGE_PROPERTIES"))
, m_xPagePropertiesPB(m_xBuilder->weld_button("PB_PAGE_PROPERTIES"))
, m_xOkPB(m_xBuilder->weld_button("ok"))
{
SwView* pView = GetActiveView();
if (!pView)
return;
SwWrtShell& rWrtShell = pView->GetWrtShell();
m_xOkPB->connect_clicked(LINK(this, SwTitlePageDlg, OKHdl));
m_xRestartNumberingCB->connect_toggled(LINK(this, SwTitlePageDlg, RestartNumberingHdl));
m_xSetPageNumberCB->connect_toggled(LINK(this, SwTitlePageDlg, SetPageNumberHdl));
m_xPageStartNF->set_max(rWrtShell.GetPageCnt() + 1);
sal_uInt16 nSetPage = 1;
sal_uInt16 nResetPage = 1;
sal_uInt16 nTitlePages = 1;
lcl_PushCursor(rWrtShell);
SwView& rView = rWrtShell.GetView();
rView.InvalidateRulerPos();
bool bMaybeResetNumbering = false;
mpTitleDesc = rWrtShell.GetPageDescFromPool(RES_POOLPAGE_FIRST);
mpIndexDesc = rWrtShell.GetPageDescFromPool(RES_POOLPAGE_REGISTER);
mpNormalDesc = rWrtShell.GetPageDescFromPool(RES_POOLPAGE_STANDARD);
rWrtShell.StartOfSection();
if (lcl_GetPageDesc(rWrtShell, nSetPage, &mpPageFormatDesc))
{
if (mpPageFormatDesc->GetPageDesc() == mpTitleDesc)
{
while (rWrtShell.SttNxtPg())
{
const size_t nCurIdx = rWrtShell.GetCurPageDesc();
const SwPageDesc& rPageDesc = rWrtShell.GetPageDesc(nCurIdx);
if (mpIndexDesc != &rPageDesc)
{
mpNormalDesc = &rPageDesc;
bMaybeResetNumbering = lcl_GetPageDesc(rWrtShell, nResetPage, nullptr);
break;
}
++nTitlePages;
}
}
}
lcl_PopCursor(rWrtShell);
m_xUseExistingPagesRB->set_active(true);
m_xPageCountNF->set_value(nTitlePages);
m_xPageCountNF->connect_value_changed(LINK(this, SwTitlePageDlg, ValueChangeHdl));
m_xDocumentStartRB->set_active(true);
m_xPageStartNF->set_sensitive(false);
m_xPageStartNF->set_value(lcl_GetCurrentPage(rWrtShell));
Link<weld::Toggleable&,void> aStartPageHdl = LINK(this, SwTitlePageDlg, StartPageHdl);
m_xDocumentStartRB->connect_toggled(aStartPageHdl);
m_xPageStartRB->connect_toggled(aStartPageHdl);
m_xRestartNumberingNF->set_value(nResetPage);
if (bMaybeResetNumbering && nResetPage > 0)
{
m_xRestartNumberingCB->set_active(true);
}
m_xRestartNumberingNF->set_sensitive(m_xRestartNumberingCB->get_active());
m_xSetPageNumberNF->set_value(nSetPage);
if (nSetPage > 1)
m_xSetPageNumberCB->set_active(true);
m_xSetPageNumberNF->set_sensitive(m_xSetPageNumberCB->get_active());
FillList();
m_xPagePropertiesPB->connect_clicked(LINK(this, SwTitlePageDlg, EditHdl));
}
IMPL_LINK_NOARG(SwTitlePageDlg, ValueChangeHdl, weld::SpinButton&, void)
{
if (m_xPageCountNF->get_value() == 1 || m_xPageCountNF->get_value() == 2)
FillList();
}
IMPL_LINK_NOARG(SwTitlePageDlg, RestartNumberingHdl, weld::Toggleable&, void)
{
m_xRestartNumberingNF->set_sensitive(m_xRestartNumberingCB->get_active());
}
IMPL_LINK_NOARG(SwTitlePageDlg, SetPageNumberHdl, weld::Toggleable&, void)
{
m_xSetPageNumberNF->set_sensitive(m_xSetPageNumberCB->get_active());
}
IMPL_LINK_NOARG(SwTitlePageDlg, StartPageHdl, weld::Toggleable&, void)
{
m_xPageStartNF->set_sensitive(m_xPageStartRB->get_active());
}
SwTitlePageDlg::~SwTitlePageDlg()
{
}
IMPL_LINK_NOARG(SwTitlePageDlg, EditHdl, weld::Button&, void)
{
SwView* pView = GetActiveView();
if (!pView)
return;
SwWrtShell& rWrtShell = pView->GetWrtShell();
SwView& rView = rWrtShell.GetView();
rView.GetDocShell()->FormatPage(getDialog(), m_xPagePropertiesLB->get_active_text(), "page", rWrtShell);
rView.InvalidateRulerPos();
}
IMPL_LINK_NOARG(SwTitlePageDlg, OKHdl, weld::Button&, void)
{
SwView* pView = GetActiveView();
if (!pView)
return;
SwWrtShell& rWrtShell = pView->GetWrtShell();
lcl_PushCursor(rWrtShell);
rWrtShell.StartUndo();
SwFormatPageDesc aTitleDesc(mpTitleDesc);
if (m_xSetPageNumberCB->get_active())
aTitleDesc.SetNumOffset(m_xSetPageNumberNF->get_value());
else if (mpPageFormatDesc)
aTitleDesc.SetNumOffset(mpPageFormatDesc->GetNumOffset());
sal_uInt16 nNumTitlePages = m_xPageCountNF->get_value();
if (!m_xUseExistingPagesRB->get_active())
{
// Assuming that a failure to GotoPage means the end of the document,
// insert new pages after the last page.
if (!lcl_GotoPage(rWrtShell, GetInsertPosition()))
{
rWrtShell.EndPg();
// Add one more page as a content page to follow the new title pages.
rWrtShell.InsertPageBreak();
}
for (sal_uInt16 nI = 0; nI < nNumTitlePages; ++nI)
rWrtShell.InsertPageBreak();
// In order to be able to access these new pages, the layout needs to be recalculated first.
rWrtShell.CalcLayout();
}
if (lcl_GotoPage(rWrtShell, GetInsertPosition()))
{
rWrtShell.SetAttrItem(aTitleDesc);
for (sal_uInt16 nI = 1; nI < nNumTitlePages; ++nI)
{
if (rWrtShell.SttNxtPg())
lcl_ChangePage(rWrtShell, SAL_MAX_UINT16, mpIndexDesc);
}
}
if ((m_xRestartNumberingCB->get_active() || nNumTitlePages > 1)
&& lcl_GotoPage(rWrtShell, GetInsertPosition(), nNumTitlePages))
{
sal_uInt16 nPgNo
= m_xRestartNumberingCB->get_active() ? m_xRestartNumberingNF->get_value() : 0;
const SwPageDesc* pNewDesc = nNumTitlePages > 1 ? mpNormalDesc : nullptr;
lcl_ChangePage(rWrtShell, nPgNo, pNewDesc);
}
rWrtShell.EndUndo();
lcl_PopCursor(rWrtShell);
if (!m_xUseExistingPagesRB->get_active())
lcl_GotoPage(rWrtShell, GetInsertPosition());
m_xDialog->response(RET_OK);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|