1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <osl/file.hxx>
21 #include <sfx2/new.hxx>
22 #include <vcl/idle.hxx>
23 #include <vcl/gdimtf.hxx>
24 #include <svl/itemset.hxx>
25 #include <svl/eitem.hxx>
26 #include <svtools/sfxecode.hxx>
27 #include <svtools/ehdl.hxx>
28 #include <tools/urlobj.hxx>
29 #include <tools/debug.hxx>
31 #include <sfx2/strings.hrc>
32 #include <sfx2/sfxsids.hrc>
33 #include <sfx2/app.hxx>
34 #include <sfx2/objsh.hxx>
35 #include <sfx2/sfxresid.hxx>
36 #include <sfx2/docfile.hxx>
37 #include <preview.hxx>
38 #include <sfx2/printer.hxx>
39 #include <unotools/viewoptions.hxx>
40 #include <vcl/waitobj.hxx>
42 void SfxPreviewWin_Impl::SetObjectShell(SfxObjectShell
const * pObj
)
44 std::shared_ptr
<GDIMetaFile
> xFile
= pObj
45 ? pObj
->GetPreviewMetaFile()
46 : std::shared_ptr
<GDIMetaFile
>();
51 SfxPreviewWin_Impl::SfxPreviewWin_Impl()
55 void SfxPreviewWin_Impl::ImpPaint(vcl::RenderContext
& rRenderContext
, GDIMetaFile
* pFile
)
57 rRenderContext
.SetLineColor();
58 rRenderContext
.SetFillColor(COL_LIGHTGRAY
);
59 rRenderContext
.DrawRect(tools::Rectangle(Point(0,0), rRenderContext
.GetOutputSize()));
61 Size aTmpSize
= pFile
? pFile
->GetPrefSize() : Size(1, 1);
62 DBG_ASSERT(aTmpSize
.Height() != 0 && aTmpSize
.Width() != 0, "size of first page is 0, override GetFirstPageSize or set visible-area!");
66 long nWidth
= rRenderContext
.GetOutputSize().Width() - 2 * FRAME
;
67 long nHeight
= rRenderContext
.GetOutputSize().Height() - 2 * FRAME
;
68 if (nWidth
<= 0 || nHeight
<= 0)
71 double dRatio
= aTmpSize
.Height() ? (double(aTmpSize
.Width()) / aTmpSize
.Height()) : 1;
72 double dRatioPreV
= double(nWidth
) / nHeight
;
75 if (dRatio
> dRatioPreV
)
77 aSize
= Size(nWidth
, sal_uInt16(nWidth
/ dRatio
));
78 aPoint
= Point(0, sal_uInt16((nHeight
- aSize
.Height()) / 2));
82 aSize
= Size(sal_uInt16(nHeight
* dRatio
), nHeight
);
83 aPoint
= Point(sal_uInt16((nWidth
- aSize
.Width()) / 2), 0);
85 Point bPoint
= Point(nWidth
, nHeight
) - aPoint
;
89 rRenderContext
.SetLineColor(COL_BLACK
);
90 rRenderContext
.SetFillColor(COL_WHITE
);
91 rRenderContext
.DrawRect(tools::Rectangle(aPoint
+ Point(FRAME
, FRAME
), bPoint
+ Point(FRAME
, FRAME
)));
93 pFile
->Play(&rRenderContext
, aPoint
+ Point(FRAME
, FRAME
), aSize
);
97 void SfxPreviewWin_Impl::Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
&)
99 ImpPaint(rRenderContext
, xMetaFile
.get());
102 IMPL_LINK_NOARG(SfxNewFileDialog
, Update
, Timer
*, void)
104 if (m_xDocShell
.Is())
106 if (m_xDocShell
->GetProgress())
111 const sal_uInt16 nEntry
= GetSelectedTemplatePos();
114 m_xPreviewController
->Invalidate();
115 m_xPreviewController
->SetObjectShell(nullptr);
119 if (!m_xMoreBt
->get_expanded() || (m_nFlags
!= SfxNewFileDialogMode::Preview
))
122 OUString aFileName
= m_aTemplates
.GetPath(m_xRegionLb
->get_selected_index(), nEntry
- 1);
123 INetURLObject
aTestObj(aFileName
);
124 if (aTestObj
.GetProtocol() == INetProtocol::NotValid
)
126 // temp. fix until Templates are managed by UCB compatible service
127 // does NOT work with locally cached components !
129 osl::FileBase::getFileURLFromSystemPath( aFileName
, aTemp
);
133 INetURLObject
aObj(aFileName
);
134 for (SfxObjectShell
* pTmp
= SfxObjectShell::GetFirst(); pTmp
; pTmp
= SfxObjectShell::GetNext(*pTmp
))
137 if (pTmp
->GetMedium())
139 if (INetURLObject( pTmp
->GetMedium()->GetName() ) == aObj
)
146 if (!m_xDocShell
.Is())
148 SfxErrorContext
eEC(ERRCTX_SFX_LOADTEMPLATE
, m_xDialog
.get());
149 SfxApplication
*pSfxApp
= SfxGetpApp();
150 std::unique_ptr
<SfxItemSet
> pSet(new SfxAllItemSet(pSfxApp
->GetPool()));
151 pSet
->Put(SfxBoolItem(SID_TEMPLATE
, true));
152 pSet
->Put(SfxBoolItem(SID_PREVIEW
, true));
153 ErrCode lErr
= pSfxApp
->LoadTemplate(m_xDocShell
, aFileName
, std::move(pSet
));
155 ErrorHandler::HandleError(lErr
);
156 if (!m_xDocShell
.Is())
158 m_xPreviewController
->SetObjectShell(nullptr);
163 m_xPreviewController
->SetObjectShell(m_xDocShell
);
166 IMPL_LINK( SfxNewFileDialog
, RegionSelect
, weld::TreeView
&, rBox
, void )
168 if (m_xDocShell
.Is() && m_xDocShell
->GetProgress())
171 const sal_uInt16 nRegion
= rBox
.get_selected_index();
172 const sal_uInt16 nCount
= m_aTemplates
.GetRegionCount() ? m_aTemplates
.GetCount(nRegion
): 0;
173 m_xTemplateLb
->freeze();
174 m_xTemplateLb
->clear();
175 OUString aSel
= m_xRegionLb
->get_selected_text();
176 sal_Int32 nc
= aSel
.indexOf('(');
177 if (nc
!= -1 && nc
!= 0)
178 aSel
= aSel
.replaceAt(nc
-1, 1, "");
179 if ( aSel
.compareToIgnoreAsciiCase( SfxResId(STR_STANDARD
) ) == 0 )
180 m_xTemplateLb
->append_text(SfxResId(STR_NONE
));
181 for (sal_uInt16 i
= 0; i
< nCount
; ++i
)
182 m_xTemplateLb
->append_text(m_aTemplates
.GetName(nRegion
, i
));
183 m_xTemplateLb
->thaw();
185 m_xTemplateLb
->select(0);
186 TemplateSelect(*m_xTemplateLb
);
189 IMPL_LINK_NOARG(SfxNewFileDialog
, Expand
, weld::Expander
&, void)
191 TemplateSelect(*m_xTemplateLb
);
194 IMPL_LINK_NOARG(SfxNewFileDialog
, TemplateSelect
, weld::TreeView
&, void)
197 if (m_xDocShell
&& m_xDocShell
->GetProgress())
200 if (!m_xMoreBt
->get_expanded())
202 // Dialog is not opened
209 IMPL_LINK_NOARG( SfxNewFileDialog
, DoubleClick
, weld::TreeView
&, bool )
212 if (!m_xDocShell
.Is() || !m_xDocShell
->GetProgress())
213 m_xDialog
->response(RET_OK
);
217 sal_uInt16
SfxNewFileDialog::GetSelectedTemplatePos() const
219 int nEntry
= m_xTemplateLb
->get_selected_index();
222 OUString aSel
= m_xRegionLb
->get_selected_text();
223 sal_Int32 nc
= aSel
.indexOf('(');
224 if (nc
!= -1 && nc
!= 0)
225 aSel
= aSel
.replaceAt(nc
-1, 1, "");
226 if ( aSel
.compareToIgnoreAsciiCase(SfxResId(STR_STANDARD
)) != 0 )
231 SfxNewFileDialog::SfxNewFileDialog(weld::Window
*pParent
, SfxNewFileDialogMode nFlags
)
232 : SfxDialogController(pParent
, "sfx/ui/loadtemplatedialog.ui", "LoadTemplateDialog")
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"))
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
);
253 if (nFlags
== SfxNewFileDialogMode::NONE
)
255 else if(SfxNewFileDialogMode::LoadTemplate
== nFlags
)
257 m_xLoadFilePB
->show();
258 m_xTextStyleCB
->show();
259 m_xFrameStyleCB
->show();
260 m_xPageStyleCB
->show();
261 m_xNumStyleCB
->show();
262 m_xMergeStyleCB
->show();
264 m_xTextStyleCB
->set_active(true);
265 m_xDialog
->set_title(m_xAltTitleFt
->get_label());
269 m_xMoreBt
->connect_expanded(LINK(this, SfxNewFileDialog
, Expand
));
270 m_xPreviewWin
->show();
274 SvtViewOptions
aDlgOpt(EViewType::Dialog
, OStringToOUString(m_xDialog
->get_help_id(), RTL_TEXTENCODING_UTF8
));
275 if (aDlgOpt
.Exists())
277 css::uno::Any aUserItem
= aDlgOpt
.GetUserItem("UserItem");
278 aUserItem
>>= sExtraData
;
281 bool bExpand
= !sExtraData
.isEmpty() && sExtraData
[0] == 'Y';
282 m_xMoreBt
->set_expanded(bExpand
&& (nFlags
!= SfxNewFileDialogMode::NONE
));
284 m_xTemplateLb
->connect_changed(LINK(this, SfxNewFileDialog
, TemplateSelect
));
285 m_xTemplateLb
->connect_row_activated(LINK(this, SfxNewFileDialog
, DoubleClick
));
287 // update the template configuration if necessary
289 weld::WaitObject
aWaitCursor(m_xDialog
.get());
290 m_aTemplates
.Update();
292 // fill the list boxes
293 const sal_uInt16 nCount
= m_aTemplates
.GetRegionCount();
296 for(sal_uInt16 i
= 0; i
< nCount
; ++i
)
297 m_xRegionLb
->append_text(m_aTemplates
.GetFullRegionName(i
));
298 m_xRegionLb
->connect_changed(LINK(this, SfxNewFileDialog
, RegionSelect
));
301 m_aPrevIdle
.SetPriority( TaskPriority::LOWEST
);
302 m_aPrevIdle
.SetInvokeHandler( LINK( this, SfxNewFileDialog
, Update
));
304 m_xRegionLb
->select(0);
305 RegionSelect(*m_xRegionLb
);
308 SfxNewFileDialog::~SfxNewFileDialog()
310 SvtViewOptions
aDlgOpt(EViewType::Dialog
, OStringToOUString(m_xDialog
->get_help_id(), RTL_TEXTENCODING_UTF8
));
311 aDlgOpt
.SetUserItem("UserItem", css::uno::makeAny(m_xMoreBt
->get_expanded() ? OUString("Y") : OUString("N")));
314 bool SfxNewFileDialog::IsTemplate() const
316 return GetSelectedTemplatePos()!=0;
319 OUString
SfxNewFileDialog::GetTemplateFileName() const
321 if (!IsTemplate() || !m_aTemplates
.GetRegionCount())
323 return m_aTemplates
.GetPath(m_xRegionLb
->get_selected_index(),
324 GetSelectedTemplatePos()-1);
327 SfxTemplateFlags
SfxNewFileDialog::GetTemplateFlags()const
329 SfxTemplateFlags nRet
= m_xTextStyleCB
->get_active() ? SfxTemplateFlags::LOAD_TEXT_STYLES
: SfxTemplateFlags::NONE
;
330 if(m_xFrameStyleCB
->get_active())
331 nRet
|= SfxTemplateFlags::LOAD_FRAME_STYLES
;
332 if(m_xPageStyleCB
->get_active())
333 nRet
|= SfxTemplateFlags::LOAD_PAGE_STYLES
;
334 if(m_xNumStyleCB
->get_active())
335 nRet
|= SfxTemplateFlags::LOAD_NUM_STYLES
;
336 if(m_xMergeStyleCB
->get_active())
337 nRet
|= SfxTemplateFlags::MERGE_STYLES
;
341 void SfxNewFileDialog::SetTemplateFlags(SfxTemplateFlags nSet
)
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
));
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */