summaryrefslogtreecommitdiff
path: root/sw/source/ui/misc/titlepage.cxx
blob: 402ace4ef8e042aa16e8ac5940d71b9a838d006f (plain)
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 -*- */
/*
 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License or as specified alternatively below. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Major Contributor(s):
 * Copyright (C) 2010 Red Hat, Inc., Caolán McNamara <caolanm@redhat.com>
 *  (initial developer)
 *
 * All Rights Reserved.
 *
 * For minor contributions see the git repository.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
 * instead of those above.
 */

#include <sfx2/viewfrm.hxx>
#include <vcl/msgbox.hxx>
#include <view.hxx>
#include <swmodule.hxx>
#include <wrtsh.hxx>
#include <poolfmt.hxx>
#include <docsh.hxx>
#include <charfmt.hxx>
#include <docstyle.hxx>

#include "fldbas.hxx"
#include "lineinfo.hxx"
#include "globals.hrc"
#include "titlepage.hxx"
#include "uitool.hxx"
#include "fmtpdsc.hxx"
#include "pagedesc.hxx"

#include <IDocumentStylePoolAccess.hxx>

namespace
{
    bool lcl_GetPageDesc(SwWrtShell *pSh, sal_uInt16 &rPageNo, const SwFmtPageDesc **ppPageFmtDesc)
    {
        bool bRet = false;
        SfxItemSet aSet( pSh->GetAttrPool(), RES_PAGEDESC, RES_PAGEDESC );
        if (pSh->GetCurAttr( aSet ))
        {
            const SfxPoolItem* pItem(0);
            if (SFX_ITEM_SET == aSet.GetItemState( RES_PAGEDESC, sal_True, &pItem ) && pItem)
            {
                rPageNo = ((const SwFmtPageDesc *)pItem)->GetNumOffset();
                if (ppPageFmtDesc)
                    (*ppPageFmtDesc) = (const SwFmtPageDesc *)(pItem->Clone());
                bRet = true;
            }
        }
        return bRet;
    }

    void lcl_ChangePage(SwWrtShell *pSh, sal_uInt16 nNewNumber,
        const SwPageDesc *pNewDesc)
    {
        const sal_uInt16 nCurIdx = pSh->GetCurPageDesc();
        const SwPageDesc &rCurrentDesc = pSh->GetPageDesc( nCurIdx );

        const SwFmtPageDesc *pPageFmtDesc(0);
        sal_uInt16 nDontCare;
        lcl_GetPageDesc(pSh, nDontCare, &pPageFmtDesc);

        //If we want a new number then set it, otherwise reuse the existing one
        sal_uInt16 nPgNo = nNewNumber ?
            nNewNumber : ( pPageFmtDesc ? pPageFmtDesc->GetNumOffset() : 0 );

        //If we want a new descriptior then set it, otherwise reuse the existing one
        if (!pNewDesc)
        {
            SwFmtPageDesc aPageFmtDesc(pPageFmtDesc ? *pPageFmtDesc : &rCurrentDesc);
            if (nPgNo) aPageFmtDesc.SetNumOffset(nPgNo);
            pSh->SetAttr(aPageFmtDesc);
        }
        else
        {
            SwFmtPageDesc aPageFmtDesc(pNewDesc);
            if (nPgNo) aPageFmtDesc.SetNumOffset(nPgNo);
            pSh->SetAttr(aPageFmtDesc);
        }

        delete pPageFmtDesc;
    }

    void lcl_PushCursor(SwWrtShell *pSh)
    {
        pSh->LockView( sal_True );
        pSh->StartAllAction();
        pSh->SwCrsrShell::Push();
    }

    void lcl_PopCursor(SwWrtShell *pSh)
    {
        pSh->SwCrsrShell::Pop( sal_False );
        pSh->EndAllAction();
        pSh->LockView( sal_False );
    }

    sal_uInt16 lcl_GetCurrentPage(SwWrtShell *pSh)
    {
        String sDummy;
        sal_uInt16 nPhyNum=1, nVirtNum=1;
        pSh->GetPageNumber(0, true, nPhyNum, nVirtNum, sDummy);
        return nPhyNum;
    }
}

/*
 * 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_pPageCountNF->GetValue();
    m_pPagePropertiesLB->Clear();
    if (mpTitleDesc)
        m_pPagePropertiesLB->InsertEntry(mpTitleDesc->GetName());
    if (nTitlePages > 1 && mpIndexDesc)
        m_pPagePropertiesLB->InsertEntry(mpIndexDesc->GetName());
    if (mpNormalDesc)
        m_pPagePropertiesLB->InsertEntry(mpNormalDesc->GetName());
    m_pPagePropertiesLB->SelectEntryPos(0);
}

sal_uInt16 SwTitlePageDlg::GetInsertPosition() const
{
    sal_uInt16 nPage = 1;
    if (m_pPageStartNF->IsEnabled())
        nPage = m_pPageStartNF->GetValue();
    return nPage;
}

SwTitlePageDlg::SwTitlePageDlg( Window *pParent ) :
    SfxModalDialog( pParent, "DLG_TITLEPAGE", "modules/swriter/ui/titlepage.ui"),
    mpPageFmtDesc(0)
{
    get(m_pUseExistingPagesRB, "RB_USE_EXISTING_PAGES");
    get(m_pPageCountNF, "NF_PAGE_COUNT");
    get(m_pDocumentStartRB, "RB_DOCUMENT_START");
    get(m_pPageStartRB, "RB_PAGE_START");
    get(m_pPageStartNF, "NF_PAGE_START");
    get(m_pRestartNumberingCB, "CB_RESTART_NUMBERING");
    get(m_pRestartNumberingNF, "NF_RESTART_NUMBERING");
    get(m_pSetPageNumberCB, "CB_SET_PAGE_NUMBER");
    get(m_pSetPageNumberNF, "NF_SET_PAGE_NUMBER");
    get(m_pPagePropertiesLB, "LB_PAGE_PROPERTIES");
    get(m_pPagePropertiesPB, "PB_PAGE_PROPERTIES");
    get(m_pOkPB, "PB_OK");

    m_pOkPB->SetClickHdl(LINK(this, SwTitlePageDlg, OKHdl));
    m_pRestartNumberingCB->SetClickHdl(LINK(this, SwTitlePageDlg, RestartNumberingHdl));
    m_pSetPageNumberCB->SetClickHdl(LINK(this, SwTitlePageDlg, SetPageNumberHdl));

    sal_uInt16 nSetPage = 1;
    sal_uInt16 nResetPage = 1;
    sal_uInt16 nTitlePages = 1;
    mpSh = ::GetActiveView()->GetWrtShellPtr();
    lcl_PushCursor(mpSh);

    SwView& rView = mpSh->GetView();
    rView.InvalidateRulerPos();

    bool bMaybeResetNumbering = false;

    mpTitleDesc = mpSh->GetPageDescFromPool(RES_POOLPAGE_FIRST);
    mpIndexDesc = mpSh->GetPageDescFromPool(RES_POOLPAGE_REGISTER);
    mpNormalDesc = mpSh->GetPageDescFromPool(RES_POOLPAGE_STANDARD);

    mpSh->SttDoc();
    if (lcl_GetPageDesc( mpSh, nSetPage, &mpPageFmtDesc ))
    {
        if (mpPageFmtDesc->GetPageDesc() == mpTitleDesc)
        {
            while (mpSh->SttNxtPg())
            {
                const sal_uInt16 nCurIdx = mpSh->GetCurPageDesc();
                const SwPageDesc &rPageDesc = mpSh->GetPageDesc( nCurIdx );

                if (mpIndexDesc != &rPageDesc)
                {
                    mpNormalDesc = &rPageDesc;
                    bMaybeResetNumbering = lcl_GetPageDesc(mpSh, nResetPage, NULL);
                    break;
                }
                ++nTitlePages;
            }
        }
    }
    lcl_PopCursor(mpSh);

    m_pUseExistingPagesRB->Check();
    m_pPageCountNF->SetValue(nTitlePages);
    m_pPageCountNF->SetUpHdl(LINK(this, SwTitlePageDlg, UpHdl));
    m_pPageCountNF->SetDownHdl(LINK(this, SwTitlePageDlg, DownHdl));

    m_pDocumentStartRB->Check();
    m_pPageStartNF->Enable(false);
    m_pPageStartNF->SetValue(lcl_GetCurrentPage(mpSh));
    Link aStartPageHdl = LINK(this, SwTitlePageDlg, StartPageHdl);
    m_pDocumentStartRB->SetClickHdl(aStartPageHdl);
    m_pPageStartRB->SetClickHdl(aStartPageHdl);

    if (bMaybeResetNumbering && nResetPage > 0)
    {
        m_pRestartNumberingCB->Check();
        m_pRestartNumberingNF->SetValue(nResetPage);
    }
    m_pRestartNumberingNF->Enable(m_pRestartNumberingCB->IsChecked());

    m_pSetPageNumberNF->SetValue(nSetPage);
    if (nSetPage > 1)
        m_pSetPageNumberCB->Check();
    m_pSetPageNumberNF->Enable(m_pSetPageNumberCB->IsChecked());

    FillList();
    m_pPagePropertiesPB->SetClickHdl(LINK(this, SwTitlePageDlg, EditHdl));
}

IMPL_LINK_NOARG(SwTitlePageDlg, UpHdl)
{
    if (m_pPageCountNF->GetValue() == 2)
        FillList();
    return 0;
}

IMPL_LINK_NOARG(SwTitlePageDlg, DownHdl)
{
    if (m_pPageCountNF->GetValue() == 1)
        FillList();
    return 0;
}

IMPL_LINK_NOARG(SwTitlePageDlg, RestartNumberingHdl)
{
    m_pRestartNumberingNF->Enable(m_pRestartNumberingCB->IsChecked());
    return 0;
}

IMPL_LINK_NOARG(SwTitlePageDlg, SetPageNumberHdl)
{
    m_pSetPageNumberNF->Enable(m_pSetPageNumberCB->IsChecked());
    return 0;
}

IMPL_LINK_NOARG(SwTitlePageDlg, StartPageHdl)
{
    m_pPageStartNF->Enable(m_pPageStartRB->IsChecked());
    return 0;
}

SwTitlePageDlg::~SwTitlePageDlg()
{
    delete mpPageFmtDesc;
}

IMPL_LINK_NOARG(SwTitlePageDlg, EditHdl)
{
    SwView& rView = mpSh->GetView();
    rView.GetDocShell()->FormatPage(m_pPagePropertiesLB->GetSelectEntry(), false, mpSh);
    rView.InvalidateRulerPos();

    return 0;
}

IMPL_LINK_NOARG(SwTitlePageDlg, OKHdl)
{
    lcl_PushCursor(mpSh);

    mpSh->StartUndo();

    SwFmtPageDesc aTitleDesc(mpTitleDesc);

    if (m_pSetPageNumberCB->IsChecked())
        aTitleDesc.SetNumOffset(m_pSetPageNumberNF->GetValue());
    else if (mpPageFmtDesc)
        aTitleDesc.SetNumOffset(mpPageFmtDesc->GetNumOffset());

    sal_uInt16 nNoPages = m_pPageCountNF->GetValue();
    if (!m_pUseExistingPagesRB->IsChecked())
    {
        mpSh->GotoPage(GetInsertPosition(), false);
        for (sal_uInt16 nI=0; nI < nNoPages; ++nI)
            mpSh->InsertPageBreak();
    }

    mpSh->GotoPage(GetInsertPosition(), false);
    for (sal_uInt16 nI=1; nI < nNoPages; ++nI)
    {
        if (mpSh->SttNxtPg())
            lcl_ChangePage(mpSh, 0, mpIndexDesc);
    }

    mpSh->GotoPage(GetInsertPosition(), false);
    mpSh->SetAttr(aTitleDesc);

    if (nNoPages > 1 && mpSh->GotoPage(GetInsertPosition() + nNoPages, false))
    {
        SwFmtPageDesc aPageFmtDesc(mpNormalDesc);
        mpSh->SetAttr(aPageFmtDesc);
    }

    if (m_pRestartNumberingCB->IsChecked() || nNoPages > 1)
    {
        sal_uInt16 nPgNo = m_pRestartNumberingCB->IsChecked() ? m_pRestartNumberingNF->GetValue() : 0;
        const SwPageDesc *pNewDesc = nNoPages > 1 ? mpNormalDesc : 0;
        mpSh->GotoPage(GetInsertPosition() + nNoPages, false);
        lcl_ChangePage(mpSh, nPgNo, pNewDesc);
    }

    mpSh->EndUndo();
    lcl_PopCursor(mpSh);
    if (!m_pUseExistingPagesRB->IsChecked())
        mpSh->GotoPage(GetInsertPosition(), false);
    EndDialog( RET_OK );
    return 0;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */