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  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <svl/itemset.hxx>
21 #include <svl/eitem.hxx>
22 #include <svl/stritem.hxx>
23 #include <sfx2/new.hxx>
24 #include <svtools/valueset.hxx>
25 #include <tools/debug.hxx>
26 #include <vcl/image.hxx>
27 
28 #include <strings.hrc>
29 
30 #include <bitmaps.hlst>
31 #include <sdpreslt.hxx>
32 #include <sdattr.hrc>
33 #include <sdresid.hxx>
34 #include <drawdoc.hxx>
35 #include <sdpage.hxx>
36 #include <DrawDocShell.hxx>
37 #include <memory>
38 
SdPresLayoutDlg(::sd::DrawDocShell * pDocShell,weld::Window * pWindow,const SfxItemSet & rInAttrs)39 SdPresLayoutDlg::SdPresLayoutDlg(::sd::DrawDocShell* pDocShell,
40     weld::Window* pWindow, const SfxItemSet& rInAttrs)
41     : GenericDialogController(pWindow, "modules/simpress/ui/slidedesigndialog.ui", "SlideDesignDialog")
42     , mpDocSh(pDocShell)
43     , mrOutAttrs(rInAttrs)
44     , maStrNone(SdResId(STR_NULL))
45     , m_xCbxMasterPage(m_xBuilder->weld_check_button("masterpage"))
46     , m_xCbxCheckMasters(m_xBuilder->weld_check_button("checkmasters"))
47     , m_xBtnLoad(m_xBuilder->weld_button("load"))
48     , m_xVS(new ValueSet(m_xBuilder->weld_scrolled_window("selectwin", true)))
49     , m_xVSWin(new weld::CustomWeld(*m_xBuilder, "select", *m_xVS))
50 {
51     m_xVSWin->set_size_request(m_xBtnLoad->get_approximate_digit_width() * 60,
52                                m_xBtnLoad->get_text_height() * 20);
53 
54     m_xVS->SetDoubleClickHdl(LINK(this, SdPresLayoutDlg, ClickLayoutHdl));
55     m_xBtnLoad->connect_clicked(LINK(this, SdPresLayoutDlg, ClickLoadHdl));
56 
57     Reset();
58 }
59 
~SdPresLayoutDlg()60 SdPresLayoutDlg::~SdPresLayoutDlg()
61 {
62 }
63 
64 /**
65  *    Initialize
66  */
Reset()67 void SdPresLayoutDlg::Reset()
68 {
69     const SfxPoolItem *pPoolItem = nullptr;
70     tools::Long nName;
71 
72     // replace master page
73     if( mrOutAttrs.GetItemState( ATTR_PRESLAYOUT_MASTER_PAGE, false, &pPoolItem ) == SfxItemState::SET )
74     {
75         bool bMasterPage = static_cast<const SfxBoolItem*>(pPoolItem)->GetValue();
76         m_xCbxMasterPage->set_sensitive( !bMasterPage );
77         m_xCbxMasterPage->set_active( bMasterPage );
78     }
79 
80     // remove not used master pages
81     m_xCbxCheckMasters->set_active(false);
82 
83     if(mrOutAttrs.GetItemState(ATTR_PRESLAYOUT_NAME, true, &pPoolItem) == SfxItemState::SET)
84         maName = static_cast<const SfxStringItem*>(pPoolItem)->GetValue();
85     else
86         maName.clear();
87 
88     FillValueSet();
89 
90     mnLayoutCount = maLayoutNames.size();
91     for( nName = 0; nName < mnLayoutCount; nName++ )
92     {
93         if (maLayoutNames[nName] == maName)
94             break;
95     }
96     DBG_ASSERT(nName < mnLayoutCount, "Layout not found");
97 
98     m_xVS->SelectItem(static_cast<sal_uInt16>(nName) + 1);  // Indices of the ValueSets start at 1
99 
100 }
101 
102 /**
103  * Fills the provided Item-Set with dialog box attributes
104  */
GetAttr(SfxItemSet & rOutAttrs)105 void SdPresLayoutDlg::GetAttr(SfxItemSet& rOutAttrs)
106 {
107     short nId = m_xVS->GetSelectedItemId();
108     bool bLoad = nId > mnLayoutCount;
109     rOutAttrs.Put( SfxBoolItem( ATTR_PRESLAYOUT_LOAD, bLoad ) );
110 
111     OUString aLayoutName;
112 
113     if( bLoad )
114     {
115         aLayoutName = maName + "#" + maLayoutNames[ nId - 1 ];
116     }
117     else if (nId)
118     {
119         aLayoutName = maLayoutNames[ nId - 1 ];
120         if( aLayoutName == maStrNone )
121             aLayoutName.clear(); // that way we encode "- nothing -" (see below)
122     }
123 
124     rOutAttrs.Put( SfxStringItem( ATTR_PRESLAYOUT_NAME, aLayoutName ) );
125     rOutAttrs.Put( SfxBoolItem( ATTR_PRESLAYOUT_MASTER_PAGE, m_xCbxMasterPage->get_active() ) );
126     rOutAttrs.Put( SfxBoolItem( ATTR_PRESLAYOUT_CHECK_MASTERS, m_xCbxCheckMasters->get_active() ) );
127 }
128 
129 /**
130  * Fills ValueSet with bitmaps
131  */
FillValueSet()132 void SdPresLayoutDlg::FillValueSet()
133 {
134     m_xVS->SetStyle(m_xVS->GetStyle() | WB_ITEMBORDER | WB_DOUBLEBORDER
135                                       | WB_VSCROLL | WB_NAMEFIELD);
136 
137     m_xVS->SetColCount(2);
138     m_xVS->SetLineCount(2);
139     m_xVS->SetExtraSpacing(2);
140 
141     SdDrawDocument* pDoc = mpDocSh->GetDoc();
142 
143     sal_uInt16 nCount = pDoc->GetMasterPageCount();
144 
145     for (sal_uInt16 nLayout = 0; nLayout < nCount; nLayout++)
146     {
147         SdPage* pMaster = static_cast<SdPage*>(pDoc->GetMasterPage(nLayout));
148         if (pMaster->GetPageKind() == PageKind::Standard)
149         {
150             OUString aLayoutName(pMaster->GetLayoutName());
151             aLayoutName = aLayoutName.copy(0, aLayoutName.indexOf(SD_LT_SEPARATOR));
152             maLayoutNames.push_back(aLayoutName);
153 
154             Image aBitmap(mpDocSh->GetPagePreviewBitmap(pMaster));
155             m_xVS->InsertItem(static_cast<sal_uInt16>(maLayoutNames.size()), aBitmap, aLayoutName);
156         }
157     }
158 
159     m_xVS->Show();
160 }
161 
162 /**
163  * DoubleClick handler
164  */
IMPL_LINK_NOARG(SdPresLayoutDlg,ClickLayoutHdl,ValueSet *,void)165 IMPL_LINK_NOARG(SdPresLayoutDlg, ClickLayoutHdl, ValueSet*, void)
166 {
167     m_xDialog->response(RET_OK);
168 }
169 
170 /**
171  * Click handler for load button
172  */
IMPL_LINK_NOARG(SdPresLayoutDlg,ClickLoadHdl,weld::Button &,void)173 IMPL_LINK_NOARG(SdPresLayoutDlg, ClickLoadHdl, weld::Button&, void)
174 {
175     SfxNewFileDialog aDlg(m_xDialog.get(), SfxNewFileDialogMode::Preview);
176     aDlg.set_title(SdResId(STR_LOAD_PRESENTATION_LAYOUT));
177     sal_uInt16 nResult = aDlg.run();
178 
179     bool   bCancel = false;
180 
181     switch (nResult)
182     {
183         case RET_OK:
184         {
185             if (aDlg.IsTemplate())
186             {
187                 maName = aDlg.GetTemplateFileName();
188             }
189             else
190             {
191                 // that way we encode "- nothing -"
192                 maName.clear();
193             }
194         }
195         break;
196 
197         default:
198             bCancel = true;
199     }
200 
201     if( bCancel )
202         return;
203 
204     // check if template already exists
205     OUString aCompareStr(maName);
206     if (aCompareStr.isEmpty())
207         aCompareStr = maStrNone;
208 
209     auto it = std::find(maLayoutNames.begin(), maLayoutNames.end(), aCompareStr);
210     if (it != maLayoutNames.end())
211     {
212         sal_uInt16 aPos = static_cast<sal_uInt16>(std::distance(maLayoutNames.begin(), it));
213         // select template
214         m_xVS->SelectItem( aPos + 1 );
215     }
216     else
217     {
218         // load document in order to determine preview bitmap (if template is selected)
219         if (!maName.isEmpty())
220         {
221             // determine document in order to call OpenBookmarkDoc
222             SdDrawDocument* pDoc = mpDocSh->GetDoc();
223             SdDrawDocument* pTemplDoc  = pDoc->OpenBookmarkDoc( maName );
224 
225             if (pTemplDoc)
226             {
227                 ::sd::DrawDocShell*  pTemplDocSh= pTemplDoc->GetDocSh();
228 
229                 sal_uInt16 nCount = pTemplDoc->GetMasterPageCount();
230 
231                 for (sal_uInt16 nLayout = 0; nLayout < nCount; nLayout++)
232                 {
233                     SdPage* pMaster = static_cast<SdPage*>( pTemplDoc->GetMasterPage(nLayout) );
234                     if (pMaster->GetPageKind() == PageKind::Standard)
235                     {
236                         OUString aLayoutName(pMaster->GetLayoutName());
237                         aLayoutName = aLayoutName.copy(0, aLayoutName.indexOf(SD_LT_SEPARATOR));
238                         maLayoutNames.push_back(aLayoutName);
239 
240                         Image aBitmap(pTemplDocSh->GetPagePreviewBitmap(pMaster));
241                         m_xVS->InsertItem(static_cast<sal_uInt16>(maLayoutNames.size()), aBitmap, aLayoutName);
242                     }
243                 }
244             }
245             else
246             {
247                 bCancel = true;
248             }
249 
250             pDoc->CloseBookmarkDoc();
251         }
252         else
253         {
254             // empty layout
255             maLayoutNames.push_back(maStrNone);
256             m_xVS->InsertItem( static_cast<sal_uInt16>(maLayoutNames.size()),
257                     Image(BMP_FOIL_NONE), maStrNone );
258         }
259 
260         if (!bCancel)
261         {
262             // select template
263             m_xVS->SelectItem( static_cast<sal_uInt16>(maLayoutNames.size()) );
264         }
265     }
266 }
267 
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
269