LibreOffice Module sfx2 (master) 1
new.cxx
Go to the documentation of this file.
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 <osl/file.hxx>
21#include <sfx2/new.hxx>
22#include <vcl/idle.hxx>
23#include <vcl/gdimtf.hxx>
24#include <vcl/outdev.hxx>
25#include <svl/itemset.hxx>
26#include <svl/eitem.hxx>
27#include <svtools/sfxecode.hxx>
28#include <svtools/ehdl.hxx>
29#include <tools/urlobj.hxx>
30#include <tools/debug.hxx>
31
32#include <sfx2/strings.hrc>
33#include <sfx2/sfxsids.hrc>
34#include <sfx2/app.hxx>
35#include <sfx2/objsh.hxx>
36#include <sfx2/sfxresid.hxx>
37#include <sfx2/docfile.hxx>
38#include <preview.hxx>
40
42{
43 std::shared_ptr<GDIMetaFile> xFile = pObj
44 ? pObj->GetPreviewMetaFile()
45 : std::shared_ptr<GDIMetaFile>();
46 xMetaFile = xFile;
47 Invalidate();
48}
49
51{
52}
53
55{
56 rRenderContext.SetLineColor();
57 rRenderContext.SetFillColor(COL_LIGHTGRAY);
58 rRenderContext.DrawRect(tools::Rectangle(Point(0,0), rRenderContext.GetOutputSize()));
59
60 Size aTmpSize = pFile ? pFile->GetPrefSize() : Size(1, 1);
61 DBG_ASSERT(!aTmpSize.IsEmpty(), "size of first page is 0, override GetFirstPageSize or set visible-area!");
62
63#define FRAME 4
64
65 tools::Long nWidth = rRenderContext.GetOutputSize().Width() - 2 * FRAME;
66 tools::Long nHeight = rRenderContext.GetOutputSize().Height() - 2 * FRAME;
67 if (nWidth <= 0 || nHeight <= 0)
68 return;
69
70 double dRatio = aTmpSize.Height() ? (double(aTmpSize.Width()) / aTmpSize.Height()) : 1;
71 double dRatioPreV = double(nWidth) / nHeight;
72 Size aSize;
73 Point aPoint;
74 if (dRatio > dRatioPreV)
75 {
76 aSize = Size(nWidth, sal_uInt16(nWidth / dRatio));
77 aPoint = Point(0, sal_uInt16((nHeight - aSize.Height()) / 2));
78 }
79 else
80 {
81 aSize = Size(sal_uInt16(nHeight * dRatio), nHeight);
82 aPoint = Point(sal_uInt16((nWidth - aSize.Width()) / 2), 0);
83 }
84 Point bPoint = Point(nWidth, nHeight) - aPoint;
85
86 if (pFile)
87 {
88 rRenderContext.SetLineColor(COL_BLACK);
89 rRenderContext.SetFillColor(COL_WHITE);
90 rRenderContext.DrawRect(tools::Rectangle(aPoint + Point(FRAME, FRAME), bPoint + Point(FRAME, FRAME)));
91 pFile->WindStart();
92 pFile->Play(rRenderContext, aPoint + Point(FRAME, FRAME), aSize);
93 }
94}
95
97{
98 ImpPaint(rRenderContext, xMetaFile.get());
99}
100
102{
103 if (m_xDocShell.Is())
104 {
105 if (m_xDocShell->GetProgress())
106 return;
107 m_xDocShell.Clear();
108 }
109
110 const sal_uInt16 nEntry = GetSelectedTemplatePos();
111 if (!nEntry)
112 {
113 m_xPreviewController->Invalidate();
114 m_xPreviewController->SetObjectShell(nullptr);
115 return;
116 }
117
118 if (!m_xMoreBt->get_expanded() || (m_nFlags != SfxNewFileDialogMode::Preview))
119 return;
120
121 OUString aFileName = m_aTemplates.GetPath(m_xRegionLb->get_selected_index(), nEntry - 1);
122 INetURLObject aTestObj(aFileName);
123 if (aTestObj.GetProtocol() == INetProtocol::NotValid)
124 {
125 // temp. fix until Templates are managed by UCB compatible service
126 // does NOT work with locally cached components !
127 OUString aTemp;
128 osl::FileBase::getFileURLFromSystemPath( aFileName, aTemp );
129 aFileName = aTemp;
130 }
131
132 INetURLObject aObj(aFileName);
133 for (SfxObjectShell* pTmp = SfxObjectShell::GetFirst(); pTmp; pTmp = SfxObjectShell::GetNext(*pTmp))
134 {
136 if (pTmp->GetMedium())
137 // ??? HasName() MM
138 if (INetURLObject( pTmp->GetMedium()->GetName() ) == aObj)
139 {
140 m_xDocShell = pTmp;
141 break;
142 }
143 }
144
145 if (!m_xDocShell.Is())
146 {
148 SfxApplication *pSfxApp = SfxGetpApp();
149 std::unique_ptr<SfxItemSet> pSet(new SfxAllItemSet(pSfxApp->GetPool()));
150 pSet->Put(SfxBoolItem(SID_TEMPLATE, true));
151 pSet->Put(SfxBoolItem(SID_PREVIEW, true));
152 ErrCode lErr = pSfxApp->LoadTemplate(m_xDocShell, aFileName, std::move(pSet));
153 if (lErr)
155 if (!m_xDocShell.Is())
156 {
157 m_xPreviewController->SetObjectShell(nullptr);
158 return;
159 }
160 }
161
162 m_xPreviewController->SetObjectShell(m_xDocShell);
163}
164
165IMPL_LINK( SfxNewFileDialog, RegionSelect, weld::TreeView&, rBox, void )
166{
167 if (m_xDocShell.Is() && m_xDocShell->GetProgress())
168 return;
169
170 const sal_uInt16 nRegion = rBox.get_selected_index();
171 const sal_uInt16 nCount = m_aTemplates.GetRegionCount() ? m_aTemplates.GetCount(nRegion): 0;
172 m_xTemplateLb->freeze();
173 m_xTemplateLb->clear();
174 OUString aSel = m_xRegionLb->get_selected_text();
175 sal_Int32 nc = aSel.indexOf('(');
176 if (nc != -1 && nc != 0)
177 aSel = aSel.replaceAt(nc-1, 1, u"");
178 if ( aSel.compareToIgnoreAsciiCase( SfxResId(STR_STANDARD) ) == 0 )
179 m_xTemplateLb->append_text(SfxResId(STR_NONE));
180 for (sal_uInt16 i = 0; i < nCount; ++i)
181 m_xTemplateLb->append_text(m_aTemplates.GetName(nRegion, i));
182 m_xTemplateLb->thaw();
183 if (nCount)
184 m_xTemplateLb->select(0);
185 TemplateSelect(*m_xTemplateLb);
186}
187
189{
190 TemplateSelect(*m_xTemplateLb);
191}
192
194{
195 // Still loading
196 if (m_xDocShell && m_xDocShell->GetProgress())
197 return;
198
199 if (!m_xMoreBt->get_expanded())
200 {
201 // Dialog is not opened
202 return;
203 }
204
205 m_aPrevIdle.Start();
206}
207
209{
210 // Still loading
211 if (!m_xDocShell.Is() || !m_xDocShell->GetProgress())
212 m_xDialog->response(RET_OK);
213 return true;
214}
215
217{
218 int nEntry = m_xTemplateLb->get_selected_index();
219 if (nEntry == -1)
220 return 0;
221 OUString aSel = m_xRegionLb->get_selected_text();
222 sal_Int32 nc = aSel.indexOf('(');
223 if (nc != -1 && nc != 0)
224 aSel = aSel.replaceAt(nc-1, 1, u"");
225 if ( aSel.compareToIgnoreAsciiCase(SfxResId(STR_STANDARD)) != 0 )
226 nEntry++;
227 return nEntry;
228}
229
231 : SfxDialogController(pParent, "sfx/ui/loadtemplatedialog.ui", "LoadTemplateDialog")
232 , m_aPrevIdle("SfxNewFileDialog m_aPrevIdle")
233 , m_nFlags(nFlags)
234 , m_xPreviewController(new SfxPreviewWin_Impl)
235 , m_xRegionLb(m_xBuilder->weld_tree_view("categories"))
236 , m_xTemplateLb(m_xBuilder->weld_tree_view("templates"))
237 , m_xTextStyleCB(m_xBuilder->weld_check_button("text"))
238 , m_xFrameStyleCB(m_xBuilder->weld_check_button("frame"))
239 , m_xPageStyleCB(m_xBuilder->weld_check_button("pages"))
240 , m_xNumStyleCB(m_xBuilder->weld_check_button("numbering"))
241 , m_xMergeStyleCB(m_xBuilder->weld_check_button("overwrite"))
242 , m_xLoadFilePB(m_xBuilder->weld_button("fromfile"))
243 , m_xMoreBt(m_xBuilder->weld_expander("expander"))
244 , m_xPreviewWin(new weld::CustomWeld(*m_xBuilder, "image", *m_xPreviewController))
245 , m_xAltTitleFt(m_xBuilder->weld_label("alttitle"))
246{
247 const int nWidth = m_xRegionLb->get_approximate_digit_width() * 32;
248 const int nHeight = m_xRegionLb->get_height_rows(8);
249 m_xRegionLb->set_size_request(nWidth, nHeight);
250 m_xTemplateLb->set_size_request(nWidth, nHeight);
251 m_xPreviewWin->set_size_request(nWidth, nWidth);
252
253 if (nFlags == SfxNewFileDialogMode::NONE)
254 m_xMoreBt->hide();
255 else if(SfxNewFileDialogMode::LoadTemplate == nFlags)
256 {
257 m_xLoadFilePB->show();
258 m_xTextStyleCB->show();
259 m_xFrameStyleCB->show();
260 m_xPageStyleCB->show();
261 m_xNumStyleCB->show();
262 m_xMergeStyleCB->show();
263 m_xMoreBt->hide();
264 m_xTextStyleCB->set_active(true);
265 m_xDialog->set_title(m_xAltTitleFt->get_label());
266 }
267 else
268 {
269 m_xMoreBt->connect_expanded(LINK(this, SfxNewFileDialog, Expand));
270 m_xPreviewWin->show();
271 }
272
273 OUString sExtraData;
274 SvtViewOptions aDlgOpt(EViewType::Dialog, m_xDialog->get_help_id());
275 if (aDlgOpt.Exists())
276 {
277 css::uno::Any aUserItem = aDlgOpt.GetUserItem("UserItem");
278 aUserItem >>= sExtraData;
279 }
280
281 bool bExpand = !sExtraData.isEmpty() && sExtraData[0] == 'Y';
282 m_xMoreBt->set_expanded(bExpand && (nFlags != SfxNewFileDialogMode::NONE));
283
284 m_xTemplateLb->connect_changed(LINK(this, SfxNewFileDialog, TemplateSelect));
285 m_xTemplateLb->connect_row_activated(LINK(this, SfxNewFileDialog, DoubleClick));
286
287 // update the template configuration if necessary
288 {
289 weld::WaitObject aWaitCursor(m_xDialog.get());
291 }
292 // fill the list boxes
293 const sal_uInt16 nCount = m_aTemplates.GetRegionCount();
294 if (nCount)
295 {
296 for(sal_uInt16 i = 0; i < nCount; ++i)
298 m_xRegionLb->connect_changed(LINK(this, SfxNewFileDialog, RegionSelect));
299 }
300
301 m_aPrevIdle.SetPriority( TaskPriority::LOWEST );
303
304 m_xRegionLb->select(0);
305 RegionSelect(*m_xRegionLb);
306}
307
309{
310 SvtViewOptions aDlgOpt(EViewType::Dialog, m_xDialog->get_help_id());
311 aDlgOpt.SetUserItem("UserItem", css::uno::Any(m_xMoreBt->get_expanded() ? OUString("Y") : OUString("N")));
312}
313
315{
316 return GetSelectedTemplatePos()!=0;
317}
318
320{
322 return OUString();
323 return m_aTemplates.GetPath(m_xRegionLb->get_selected_index(),
325}
326
328{
330 if(m_xFrameStyleCB->get_active())
332 if(m_xPageStyleCB->get_active())
334 if(m_xNumStyleCB->get_active())
336 if(m_xMergeStyleCB->get_active())
338 return nRet;
339}
340
342{
343 m_xTextStyleCB->set_active( bool(nSet & SfxTemplateFlags::LOAD_TEXT_STYLES ));
344 m_xFrameStyleCB->set_active( bool(nSet & SfxTemplateFlags::LOAD_FRAME_STYLES));
345 m_xPageStyleCB->set_active( bool(nSet & SfxTemplateFlags::LOAD_PAGE_STYLES ));
346 m_xNumStyleCB->set_active( bool(nSet & SfxTemplateFlags::LOAD_NUM_STYLES ));
347 m_xMergeStyleCB->set_active( bool(nSet & SfxTemplateFlags::MERGE_STYLES ));
348}
349
350/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxApplication * SfxGetpApp()
Definition: app.hxx:231
Reference< XExecutableDialog > m_xDialog
static DialogMask HandleError(ErrCode nId, weld::Window *pParent=nullptr, DialogMask nMask=DialogMask::MAX)
void WindStart()
const Size & GetPrefSize() const
void Play(GDIMetaFile &rMtf)
INetProtocol GetProtocol() const
Size GetOutputSize() const
void DrawRect(const tools::Rectangle &rRect)
void SetLineColor()
void SetFillColor()
ErrCode LoadTemplate(SfxObjectShellLock &xDoc, const OUString &rFileName, std::unique_ptr< SfxItemSet > pArgs)
Definition: appopen.cxx:290
OUString GetPath(sal_uInt16 nRegion, sal_uInt16 nIdx) const
Definition: doctempl.cxx:397
void Update()
updates the configuration where the document templates structure is stored.
Definition: doctempl.cxx:1237
sal_uInt16 GetRegionCount() const
Definition: doctempl.cxx:313
OUString GetFullRegionName(sal_uInt16 nIdx) const
Definition: doctempl.cxx:251
std::unique_ptr< weld::Expander > m_xMoreBt
Definition: new.hxx:80
std::unique_ptr< weld::Button > m_xLoadFilePB
Definition: new.hxx:79
std::unique_ptr< weld::CustomWeld > m_xPreviewWin
Definition: new.hxx:81
std::unique_ptr< weld::CheckButton > m_xTextStyleCB
Definition: new.hxx:74
bool IsTemplate() const
Definition: new.cxx:314
SfxNewFileDialog(weld::Window *pParent, SfxNewFileDialogMode nFlags)
Definition: new.cxx:230
std::unique_ptr< weld::CheckButton > m_xPageStyleCB
Definition: new.hxx:76
OUString GetTemplateFileName() const
Definition: new.cxx:319
std::unique_ptr< weld::CheckButton > m_xFrameStyleCB
Definition: new.hxx:75
SfxTemplateFlags GetTemplateFlags() const
Definition: new.cxx:327
void SetTemplateFlags(SfxTemplateFlags nSet)
Definition: new.cxx:341
sal_uInt16 GetSelectedTemplatePos() const
Definition: new.cxx:216
std::unique_ptr< weld::TreeView > m_xRegionLb
Definition: new.hxx:72
std::unique_ptr< weld::CheckButton > m_xNumStyleCB
Definition: new.hxx:77
std::unique_ptr< weld::CheckButton > m_xMergeStyleCB
Definition: new.hxx:78
std::unique_ptr< weld::TreeView > m_xTemplateLb
Definition: new.hxx:73
SfxDocumentTemplates m_aTemplates
Definition: new.hxx:67
Idle m_aPrevIdle
Definition: new.hxx:65
std::unique_ptr< weld::Label > m_xAltTitleFt
Definition: new.hxx:82
virtual ~SfxNewFileDialog() override
Definition: new.cxx:308
std::shared_ptr< GDIMetaFile > GetPreviewMetaFile(bool bFullContent=false) const
Definition: objcont.cxx:95
static SAL_WARN_UNUSED_RESULT SfxObjectShell * GetNext(const SfxObjectShell &rPrev, const std::function< bool(const SfxObjectShell *)> &isObjectShell=nullptr, bool bOnlyVisible=true)
Definition: objxtor.cxx:452
static SAL_WARN_UNUSED_RESULT SfxObjectShell * GetFirst(const std::function< bool(const SfxObjectShell *)> &isObjectShell=nullptr, bool bOnlyVisible=true)
Definition: objxtor.cxx:427
static void ImpPaint(vcl::RenderContext &rRenderContext, GDIMetaFile *pFile)
Definition: new.cxx:54
virtual void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &) override
Definition: new.cxx:96
SfxPreviewWin_Impl()
Definition: new.cxx:50
void SetObjectShell(SfxObjectShell const *pObj)
Definition: new.cxx:41
std::shared_ptr< GDIMetaFile > xMetaFile
Definition: preview.hxx:31
SfxItemPool & GetPool() const
Each Subclass of SfxShell must reference a pool.
Definition: shell.hxx:511
bool IsEmpty() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
css::uno::Any GetUserItem(const OUString &sName) const
void SetUserItem(const OUString &sName, const css::uno::Any &aValue)
bool Exists() const
void SetPriority(TaskPriority ePriority)
void SetInvokeHandler(const Link< Timer *, void > &rLink)
std::shared_ptr< weld::Dialog > m_xDialog
constexpr ::Color COL_WHITE(0xFF, 0xFF, 0xFF)
constexpr ::Color COL_LIGHTGRAY(0xC0, 0xC0, 0xC0)
constexpr ::Color COL_BLACK(0x00, 0x00, 0x00)
int nCount
#define DBG_ASSERT(sCon, aError)
float u
sal_Int32 m_nFlags
int i
long Long
IMPL_LINK_NOARG(SfxNewFileDialog, Update, Timer *, void)
Definition: new.cxx:101
IMPL_LINK(SfxNewFileDialog, RegionSelect, weld::TreeView &, rBox, void)
Definition: new.cxx:165
#define FRAME
SfxTemplateFlags
Definition: new.hxx:47
SfxNewFileDialogMode
Definition: new.hxx:42
#define ERRCTX_SFX_LOADTEMPLATE
OUString SfxResId(TranslateId aId)
Definition: sfxresid.cxx:22
RET_OK