summaryrefslogtreecommitdiff
path: root/sd/source/ui/dlg/sdpreslt.cxx
blob: 3e4f60861fea3f5aeeac0de2a1f092a035e6bf9a (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
/* -*- 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <svl/itemset.hxx>
#include <sfx2/new.hxx>
#include <vcl/msgbox.hxx>

#include "strings.hrc"
#include "res_bmp.hrc"
#include "sdpreslt.hxx"
#include "sdattr.hxx"
#include "sdresid.hxx"
#include "drawdoc.hxx"
#include "sdpage.hxx"
#include "DrawDocShell.hxx"
#include <memory>

SdPresLayoutDlg::SdPresLayoutDlg(::sd::DrawDocShell* pDocShell,
    vcl::Window* pWindow, const SfxItemSet& rInAttrs)
    : ModalDialog(pWindow, "SlideDesignDialog",
        "modules/simpress/ui/slidedesigndialog.ui")
    , mpDocSh(pDocShell)
    , mrOutAttrs(rInAttrs)
    , maStrNone(SD_RESSTR(STR_NULL))
{
    get(m_pVS, "select");
    Size aPref(LogicToPixel(Size(144 , 141), MapUnit::MapAppFont));
    m_pVS->set_width_request(aPref.Width());
    m_pVS->set_height_request(aPref.Height());
    get(m_pCbxMasterPage, "masterpage");
    get(m_pCbxCheckMasters, "checkmasters");
    get(m_pBtnLoad, "load");

    m_pVS->SetDoubleClickHdl(LINK(this, SdPresLayoutDlg, ClickLayoutHdl));
    m_pBtnLoad->SetClickHdl(LINK(this, SdPresLayoutDlg, ClickLoadHdl));

    Reset();
}

SdPresLayoutDlg::~SdPresLayoutDlg()
{
    disposeOnce();
}

void SdPresLayoutDlg::dispose()
{
    m_pVS.clear();
    m_pCbxMasterPage.clear();
    m_pCbxCheckMasters.clear();
    m_pBtnLoad.clear();
    ModalDialog::dispose();
}

/**
 *    Initialize
 */
void SdPresLayoutDlg::Reset()
{
    const SfxPoolItem *pPoolItem = nullptr;
    long nName;

    // replace master page
    if( mrOutAttrs.GetItemState( ATTR_PRESLAYOUT_MASTER_PAGE, false, &pPoolItem ) == SfxItemState::SET )
    {
        bool bMasterPage = static_cast<const SfxBoolItem*>(pPoolItem)->GetValue();
        m_pCbxMasterPage->Enable( !bMasterPage );
        m_pCbxMasterPage->Check( bMasterPage );
    }

    // remove not used master pages
    m_pCbxCheckMasters->Check(false);

    if(mrOutAttrs.GetItemState(ATTR_PRESLAYOUT_NAME, true, &pPoolItem) == SfxItemState::SET)
        maName = static_cast<const SfxStringItem*>(pPoolItem)->GetValue();
    else
        maName.clear();

    FillValueSet();

    mnLayoutCount = maLayoutNames.size();
    for( nName = 0; nName < mnLayoutCount; nName++ )
    {
        if (maLayoutNames[nName] == maName)
            break;
    }
    DBG_ASSERT(nName < mnLayoutCount, "Layout not found");

    m_pVS->SelectItem((sal_uInt16)nName + 1);  // Indices of the ValueSets start at 1

}

/**
 * Fills the provided Item-Set with dialog box attributes
 */
void SdPresLayoutDlg::GetAttr(SfxItemSet& rOutAttrs)
{
    short nId = m_pVS->GetSelectItemId();
    bool bLoad = nId > mnLayoutCount;
    rOutAttrs.Put( SfxBoolItem( ATTR_PRESLAYOUT_LOAD, bLoad ) );

    OUString aLayoutName;

    if( bLoad )
    {
        aLayoutName = maName + "#" + maLayoutNames[ nId - 1 ];
    }
    else if (nId)
    {
        aLayoutName = maLayoutNames[ nId - 1 ];
        if( aLayoutName == maStrNone )
            aLayoutName.clear(); // that way we encode "- nothing -" (see below)
    }

    rOutAttrs.Put( SfxStringItem( ATTR_PRESLAYOUT_NAME, aLayoutName ) );
    rOutAttrs.Put( SfxBoolItem( ATTR_PRESLAYOUT_MASTER_PAGE, m_pCbxMasterPage->IsChecked() ) );
    rOutAttrs.Put( SfxBoolItem( ATTR_PRESLAYOUT_CHECK_MASTERS, m_pCbxCheckMasters->IsChecked() ) );
}

/**
 * Fills ValueSet with bitmaps
 */
void SdPresLayoutDlg::FillValueSet()
{
    m_pVS->SetStyle(m_pVS->GetStyle() | WB_ITEMBORDER | WB_DOUBLEBORDER
                                      | WB_VSCROLL | WB_NAMEFIELD);

    m_pVS->SetColCount(2);
    m_pVS->SetLineCount(2);
    m_pVS->SetExtraSpacing(2);

    SdDrawDocument* pDoc = mpDocSh->GetDoc();

    sal_uInt16 nCount = pDoc->GetMasterPageCount();

    for (sal_uInt16 nLayout = 0; nLayout < nCount; nLayout++)
    {
        SdPage* pMaster = static_cast<SdPage*>(pDoc->GetMasterPage(nLayout));
        if (pMaster->GetPageKind() == PageKind::Standard)
        {
            OUString aLayoutName(pMaster->GetLayoutName());
            aLayoutName = aLayoutName.copy(0, aLayoutName.indexOf(SD_LT_SEPARATOR));
            maLayoutNames.push_back(aLayoutName);

            Image aBitmap(Bitmap(mpDocSh->GetPagePreviewBitmap(pMaster)));
            m_pVS->InsertItem((sal_uInt16)maLayoutNames.size(), aBitmap, aLayoutName);
        }
    }

    m_pVS->Show();
}

/**
 * DoubleClick handler
 */
IMPL_LINK_NOARG(SdPresLayoutDlg, ClickLayoutHdl, ValueSet*, void)
{
    EndDialog(RET_OK);
}

/**
 * Click handler for load button
 */
IMPL_LINK_NOARG(SdPresLayoutDlg, ClickLoadHdl, Button*, void)
{
    VclPtrInstance< SfxNewFileDialog > pDlg(this, SFXWB_PREVIEW);
    pDlg->SetText(SD_RESSTR(STR_LOAD_PRESENTATION_LAYOUT));

    if(!IsReallyVisible())
        return;

    sal_uInt16 nResult = pDlg->Execute();
    // Inserted update to force repaint
    Update();

    bool   bCancel = false;

    switch (nResult)
    {
        case RET_OK:
        {
            if (pDlg->IsTemplate())
            {
                maName = pDlg->GetTemplateFileName();
            }
            else
            {
                // that way we encode "- nothing -"
                maName.clear();
            }
        }
        break;

        default:
            bCancel = true;
    }
    pDlg.reset();

    if( !bCancel )
    {
        // check if template already exists
        bool bExists = false;
        OUString aCompareStr(maName);
        if (aCompareStr.isEmpty())
            aCompareStr = maStrNone;

        sal_uInt16 aPos = 0;
        for (std::vector<OUString>::iterator it = maLayoutNames.begin();
              it != maLayoutNames.end() && !bExists; ++it, ++aPos)
        {
            if( aCompareStr == *it )
            {
                bExists = true;
                // select template
                m_pVS->SelectItem( aPos + 1 );
            }
        }

        if( !bExists )
        {
            // load document in order to determine preview bitmap (if template is selected)
            if (!maName.isEmpty())
            {
                // determine document in order to call OpenBookmarkDoc
                SdDrawDocument* pDoc = mpDocSh->GetDoc();
                SdDrawDocument* pTemplDoc  = pDoc->OpenBookmarkDoc( maName );

                if (pTemplDoc)
                {
                    ::sd::DrawDocShell*  pTemplDocSh= pTemplDoc->GetDocSh();

                    sal_uInt16 nCount = pTemplDoc->GetMasterPageCount();

                    for (sal_uInt16 nLayout = 0; nLayout < nCount; nLayout++)
                    {
                        SdPage* pMaster = static_cast<SdPage*>( pTemplDoc->GetMasterPage(nLayout) );
                        if (pMaster->GetPageKind() == PageKind::Standard)
                        {
                            OUString aLayoutName(pMaster->GetLayoutName());
                            aLayoutName = aLayoutName.copy(0, aLayoutName.indexOf(SD_LT_SEPARATOR));
                            maLayoutNames.push_back(aLayoutName);

                            Image aBitmap(Bitmap(pTemplDocSh->GetPagePreviewBitmap(pMaster)));
                            m_pVS->InsertItem((sal_uInt16)maLayoutNames.size(), aBitmap, aLayoutName);
                        }
                    }
                }
                else
                {
                    bCancel = true;
                }

                pDoc->CloseBookmarkDoc();
            }
            else
            {
                // empty layout
                maLayoutNames.push_back(maStrNone);
                m_pVS->InsertItem( (sal_uInt16) maLayoutNames.size(),
                        Image(Bitmap(SdResId(BMP_FOIL_NONE))), maStrNone );
            }

            if (!bCancel)
            {
                // select template
                m_pVS->SelectItem( (sal_uInt16) maLayoutNames.size() );
            }
        }
    }
}

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