Line data Source code
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 <vcl/msgbox.hxx>
12 : #include <view.hxx>
13 : #include <swmodule.hxx>
14 : #include <wrtsh.hxx>
15 : #include <poolfmt.hxx>
16 : #include <docsh.hxx>
17 : #include <charfmt.hxx>
18 : #include <docstyle.hxx>
19 :
20 : #include "fldbas.hxx"
21 : #include "lineinfo.hxx"
22 : #include "globals.hrc"
23 : #include "titlepage.hxx"
24 : #include "uitool.hxx"
25 : #include "fmtpdsc.hxx"
26 : #include "pagedesc.hxx"
27 :
28 : #include <IDocumentStylePoolAccess.hxx>
29 :
30 : namespace
31 : {
32 0 : bool lcl_GetPageDesc(SwWrtShell *pSh, sal_uInt16 &rPageNo, const SwFmtPageDesc **ppPageFmtDesc)
33 : {
34 0 : bool bRet = false;
35 0 : SfxItemSet aSet( pSh->GetAttrPool(), RES_PAGEDESC, RES_PAGEDESC );
36 0 : if (pSh->GetCurAttr( aSet ))
37 : {
38 0 : const SfxPoolItem* pItem(0);
39 0 : if (SFX_ITEM_SET == aSet.GetItemState( RES_PAGEDESC, sal_True, &pItem ) && pItem)
40 : {
41 0 : rPageNo = ((const SwFmtPageDesc *)pItem)->GetNumOffset();
42 0 : if (ppPageFmtDesc)
43 0 : (*ppPageFmtDesc) = (const SwFmtPageDesc *)(pItem->Clone());
44 0 : bRet = true;
45 : }
46 : }
47 0 : return bRet;
48 : }
49 :
50 0 : void lcl_ChangePage(SwWrtShell *pSh, sal_uInt16 nNewNumber,
51 : const SwPageDesc *pNewDesc)
52 : {
53 0 : const sal_uInt16 nCurIdx = pSh->GetCurPageDesc();
54 0 : const SwPageDesc &rCurrentDesc = pSh->GetPageDesc( nCurIdx );
55 :
56 0 : const SwFmtPageDesc *pPageFmtDesc(0);
57 : sal_uInt16 nDontCare;
58 0 : lcl_GetPageDesc(pSh, nDontCare, &pPageFmtDesc);
59 :
60 : //If we want a new number then set it, otherwise reuse the existing one
61 : sal_uInt16 nPgNo = nNewNumber ?
62 0 : nNewNumber : ( pPageFmtDesc ? pPageFmtDesc->GetNumOffset() : 0 );
63 :
64 : //If we want a new descriptior then set it, otherwise reuse the existing one
65 0 : if (!pNewDesc)
66 : {
67 0 : SwFmtPageDesc aPageFmtDesc(pPageFmtDesc ? *pPageFmtDesc : &rCurrentDesc);
68 0 : if (nPgNo) aPageFmtDesc.SetNumOffset(nPgNo);
69 0 : pSh->SetAttr(aPageFmtDesc);
70 : }
71 : else
72 : {
73 0 : SwFmtPageDesc aPageFmtDesc(pNewDesc);
74 0 : if (nPgNo) aPageFmtDesc.SetNumOffset(nPgNo);
75 0 : pSh->SetAttr(aPageFmtDesc);
76 : }
77 :
78 0 : delete pPageFmtDesc;
79 0 : }
80 :
81 0 : void lcl_PushCursor(SwWrtShell *pSh)
82 : {
83 0 : pSh->LockView( sal_True );
84 0 : pSh->StartAllAction();
85 0 : pSh->SwCrsrShell::Push();
86 0 : }
87 :
88 0 : void lcl_PopCursor(SwWrtShell *pSh)
89 : {
90 0 : pSh->SwCrsrShell::Pop( sal_False );
91 0 : pSh->EndAllAction();
92 0 : pSh->LockView( sal_False );
93 0 : }
94 :
95 0 : sal_uInt16 lcl_GetCurrentPage(SwWrtShell *pSh)
96 : {
97 0 : String sDummy;
98 0 : sal_uInt16 nPhyNum=1, nVirtNum=1;
99 0 : pSh->GetPageNumber(0, true, nPhyNum, nVirtNum, sDummy);
100 0 : return nPhyNum;
101 : }
102 : }
103 :
104 : /*
105 : * Only include the Index page in the list if the page count implies one
106 : * to reduce confusing things
107 : */
108 0 : void SwTitlePageDlg::FillList()
109 : {
110 0 : sal_uInt16 nTitlePages = m_pPageCountNF->GetValue();
111 0 : m_pPagePropertiesLB->Clear();
112 0 : if (mpTitleDesc)
113 0 : m_pPagePropertiesLB->InsertEntry(mpTitleDesc->GetName());
114 0 : if (nTitlePages > 1 && mpIndexDesc)
115 0 : m_pPagePropertiesLB->InsertEntry(mpIndexDesc->GetName());
116 0 : if (mpNormalDesc)
117 0 : m_pPagePropertiesLB->InsertEntry(mpNormalDesc->GetName());
118 0 : m_pPagePropertiesLB->SelectEntryPos(0);
119 0 : }
120 :
121 0 : sal_uInt16 SwTitlePageDlg::GetInsertPosition() const
122 : {
123 0 : sal_uInt16 nPage = 1;
124 0 : if (m_pPageStartNF->IsEnabled())
125 0 : nPage = m_pPageStartNF->GetValue();
126 0 : return nPage;
127 : }
128 :
129 0 : SwTitlePageDlg::SwTitlePageDlg( Window *pParent ) :
130 : SfxModalDialog( pParent, "DLG_TITLEPAGE", "modules/swriter/ui/titlepage.ui"),
131 0 : mpPageFmtDesc(0)
132 : {
133 0 : get(m_pUseExistingPagesRB, "RB_USE_EXISTING_PAGES");
134 0 : get(m_pPageCountNF, "NF_PAGE_COUNT");
135 0 : get(m_pDocumentStartRB, "RB_DOCUMENT_START");
136 0 : get(m_pPageStartRB, "RB_PAGE_START");
137 0 : get(m_pPageStartNF, "NF_PAGE_START");
138 0 : get(m_pRestartNumberingCB, "CB_RESTART_NUMBERING");
139 0 : get(m_pRestartNumberingNF, "NF_RESTART_NUMBERING");
140 0 : get(m_pSetPageNumberCB, "CB_SET_PAGE_NUMBER");
141 0 : get(m_pSetPageNumberNF, "NF_SET_PAGE_NUMBER");
142 0 : get(m_pPagePropertiesLB, "LB_PAGE_PROPERTIES");
143 0 : get(m_pPagePropertiesPB, "PB_PAGE_PROPERTIES");
144 0 : get(m_pOkPB, "PB_OK");
145 :
146 0 : m_pOkPB->SetClickHdl(LINK(this, SwTitlePageDlg, OKHdl));
147 0 : m_pRestartNumberingCB->SetClickHdl(LINK(this, SwTitlePageDlg, RestartNumberingHdl));
148 0 : m_pSetPageNumberCB->SetClickHdl(LINK(this, SwTitlePageDlg, SetPageNumberHdl));
149 :
150 0 : sal_uInt16 nSetPage = 1;
151 0 : sal_uInt16 nResetPage = 1;
152 0 : sal_uInt16 nTitlePages = 1;
153 0 : mpSh = ::GetActiveView()->GetWrtShellPtr();
154 0 : lcl_PushCursor(mpSh);
155 :
156 0 : SwView& rView = mpSh->GetView();
157 0 : rView.InvalidateRulerPos();
158 :
159 0 : bool bMaybeResetNumbering = false;
160 :
161 0 : mpTitleDesc = mpSh->GetPageDescFromPool(RES_POOLPAGE_FIRST);
162 0 : mpIndexDesc = mpSh->GetPageDescFromPool(RES_POOLPAGE_REGISTER);
163 0 : mpNormalDesc = mpSh->GetPageDescFromPool(RES_POOLPAGE_STANDARD);
164 :
165 0 : mpSh->SttDoc();
166 0 : if (lcl_GetPageDesc( mpSh, nSetPage, &mpPageFmtDesc ))
167 : {
168 0 : if (mpPageFmtDesc->GetPageDesc() == mpTitleDesc)
169 : {
170 0 : while (mpSh->SttNxtPg())
171 : {
172 0 : const sal_uInt16 nCurIdx = mpSh->GetCurPageDesc();
173 0 : const SwPageDesc &rPageDesc = mpSh->GetPageDesc( nCurIdx );
174 :
175 0 : if (mpIndexDesc != &rPageDesc)
176 : {
177 0 : mpNormalDesc = &rPageDesc;
178 0 : bMaybeResetNumbering = lcl_GetPageDesc(mpSh, nResetPage, NULL);
179 0 : break;
180 : }
181 0 : ++nTitlePages;
182 : }
183 : }
184 : }
185 0 : lcl_PopCursor(mpSh);
186 :
187 0 : m_pUseExistingPagesRB->Check();
188 0 : m_pPageCountNF->SetValue(nTitlePages);
189 0 : m_pPageCountNF->SetUpHdl(LINK(this, SwTitlePageDlg, UpHdl));
190 0 : m_pPageCountNF->SetDownHdl(LINK(this, SwTitlePageDlg, DownHdl));
191 :
192 0 : m_pDocumentStartRB->Check();
193 0 : m_pPageStartNF->Enable(false);
194 0 : m_pPageStartNF->SetValue(lcl_GetCurrentPage(mpSh));
195 0 : Link aStartPageHdl = LINK(this, SwTitlePageDlg, StartPageHdl);
196 0 : m_pDocumentStartRB->SetClickHdl(aStartPageHdl);
197 0 : m_pPageStartRB->SetClickHdl(aStartPageHdl);
198 :
199 0 : if (bMaybeResetNumbering && nResetPage > 0)
200 : {
201 0 : m_pRestartNumberingCB->Check();
202 0 : m_pRestartNumberingNF->SetValue(nResetPage);
203 : }
204 0 : m_pRestartNumberingNF->Enable(m_pRestartNumberingCB->IsChecked());
205 :
206 0 : m_pSetPageNumberNF->SetValue(nSetPage);
207 0 : if (nSetPage > 1)
208 0 : m_pSetPageNumberCB->Check();
209 0 : m_pSetPageNumberNF->Enable(m_pSetPageNumberCB->IsChecked());
210 :
211 0 : FillList();
212 0 : m_pPagePropertiesPB->SetClickHdl(LINK(this, SwTitlePageDlg, EditHdl));
213 0 : }
214 :
215 0 : IMPL_LINK_NOARG(SwTitlePageDlg, UpHdl)
216 : {
217 0 : if (m_pPageCountNF->GetValue() == 2)
218 0 : FillList();
219 0 : return 0;
220 : }
221 :
222 0 : IMPL_LINK_NOARG(SwTitlePageDlg, DownHdl)
223 : {
224 0 : if (m_pPageCountNF->GetValue() == 1)
225 0 : FillList();
226 0 : return 0;
227 : }
228 :
229 0 : IMPL_LINK_NOARG(SwTitlePageDlg, RestartNumberingHdl)
230 : {
231 0 : m_pRestartNumberingNF->Enable(m_pRestartNumberingCB->IsChecked());
232 0 : return 0;
233 : }
234 :
235 0 : IMPL_LINK_NOARG(SwTitlePageDlg, SetPageNumberHdl)
236 : {
237 0 : m_pSetPageNumberNF->Enable(m_pSetPageNumberCB->IsChecked());
238 0 : return 0;
239 : }
240 :
241 0 : IMPL_LINK_NOARG(SwTitlePageDlg, StartPageHdl)
242 : {
243 0 : m_pPageStartNF->Enable(m_pPageStartRB->IsChecked());
244 0 : return 0;
245 : }
246 :
247 0 : SwTitlePageDlg::~SwTitlePageDlg()
248 : {
249 0 : delete mpPageFmtDesc;
250 0 : }
251 :
252 0 : IMPL_LINK_NOARG(SwTitlePageDlg, EditHdl)
253 : {
254 0 : SwView& rView = mpSh->GetView();
255 0 : rView.GetDocShell()->FormatPage(m_pPagePropertiesLB->GetSelectEntry(), false, *mpSh);
256 0 : rView.InvalidateRulerPos();
257 :
258 0 : return 0;
259 : }
260 :
261 0 : IMPL_LINK_NOARG(SwTitlePageDlg, OKHdl)
262 : {
263 0 : lcl_PushCursor(mpSh);
264 :
265 0 : mpSh->StartUndo();
266 :
267 0 : SwFmtPageDesc aTitleDesc(mpTitleDesc);
268 :
269 0 : if (m_pSetPageNumberCB->IsChecked())
270 0 : aTitleDesc.SetNumOffset(m_pSetPageNumberNF->GetValue());
271 0 : else if (mpPageFmtDesc)
272 0 : aTitleDesc.SetNumOffset(mpPageFmtDesc->GetNumOffset());
273 :
274 0 : sal_uInt16 nNoPages = m_pPageCountNF->GetValue();
275 0 : if (!m_pUseExistingPagesRB->IsChecked())
276 : {
277 0 : mpSh->GotoPage(GetInsertPosition(), false);
278 0 : for (sal_uInt16 nI=0; nI < nNoPages; ++nI)
279 0 : mpSh->InsertPageBreak();
280 : }
281 :
282 0 : mpSh->GotoPage(GetInsertPosition(), false);
283 0 : for (sal_uInt16 nI=1; nI < nNoPages; ++nI)
284 : {
285 0 : if (mpSh->SttNxtPg())
286 0 : lcl_ChangePage(mpSh, 0, mpIndexDesc);
287 : }
288 :
289 0 : mpSh->GotoPage(GetInsertPosition(), false);
290 0 : mpSh->SetAttr(aTitleDesc);
291 :
292 0 : if (nNoPages > 1 && mpSh->GotoPage(GetInsertPosition() + nNoPages, false))
293 : {
294 0 : SwFmtPageDesc aPageFmtDesc(mpNormalDesc);
295 0 : mpSh->SetAttr(aPageFmtDesc);
296 : }
297 :
298 0 : if (m_pRestartNumberingCB->IsChecked() || nNoPages > 1)
299 : {
300 0 : sal_uInt16 nPgNo = m_pRestartNumberingCB->IsChecked() ? m_pRestartNumberingNF->GetValue() : 0;
301 0 : const SwPageDesc *pNewDesc = nNoPages > 1 ? mpNormalDesc : 0;
302 0 : mpSh->GotoPage(GetInsertPosition() + nNoPages, false);
303 0 : lcl_ChangePage(mpSh, nPgNo, pNewDesc);
304 : }
305 :
306 0 : mpSh->EndUndo();
307 0 : lcl_PopCursor(mpSh);
308 0 : if (!m_pUseExistingPagesRB->IsChecked())
309 0 : mpSh->GotoPage(GetInsertPosition(), false);
310 0 : EndDialog( RET_OK );
311 0 : return 0;
312 0 : }
313 :
314 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|