Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / doc / new.cxx
bloba7e9678adcdfdb8b972589733cac5a9bb5f23923
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <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>
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>
39 #include <unotools/viewoptions.hxx>
41 void SfxPreviewWin_Impl::SetObjectShell(SfxObjectShell const * pObj)
43 std::shared_ptr<GDIMetaFile> xFile = pObj
44 ? pObj->GetPreviewMetaFile()
45 : std::shared_ptr<GDIMetaFile>();
46 xMetaFile = xFile;
47 Invalidate();
50 SfxPreviewWin_Impl::SfxPreviewWin_Impl()
54 void SfxPreviewWin_Impl::ImpPaint(vcl::RenderContext& rRenderContext, GDIMetaFile* pFile)
56 rRenderContext.SetLineColor();
57 rRenderContext.SetFillColor(COL_LIGHTGRAY);
58 rRenderContext.DrawRect(tools::Rectangle(Point(0,0), rRenderContext.GetOutputSize()));
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!");
63 #define FRAME 4
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;
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)
76 aSize = Size(nWidth, sal_uInt16(nWidth / dRatio));
77 aPoint = Point(0, sal_uInt16((nHeight - aSize.Height()) / 2));
79 else
81 aSize = Size(sal_uInt16(nHeight * dRatio), nHeight);
82 aPoint = Point(sal_uInt16((nWidth - aSize.Width()) / 2), 0);
84 Point bPoint = Point(nWidth, nHeight) - aPoint;
86 if (pFile)
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);
96 void SfxPreviewWin_Impl::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&)
98 ImpPaint(rRenderContext, xMetaFile.get());
101 IMPL_LINK_NOARG(SfxNewFileDialog, Update, Timer*, void)
103 if (m_xDocShell.Is())
105 if (m_xDocShell->GetProgress())
106 return;
107 m_xDocShell.Clear();
110 const sal_uInt16 nEntry = GetSelectedTemplatePos();
111 if (!nEntry)
113 m_xPreviewController->Invalidate();
114 m_xPreviewController->SetObjectShell(nullptr);
115 return;
118 if (!m_xMoreBt->get_expanded() || (m_nFlags != SfxNewFileDialogMode::Preview))
119 return;
121 OUString aFileName = m_aTemplates.GetPath(m_xRegionLb->get_selected_index(), nEntry - 1);
122 INetURLObject aTestObj(aFileName);
123 if (aTestObj.GetProtocol() == INetProtocol::NotValid)
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;
132 INetURLObject aObj(aFileName);
133 for (SfxObjectShell* pTmp = SfxObjectShell::GetFirst(); pTmp; pTmp = SfxObjectShell::GetNext(*pTmp))
135 //! fsys bug op==
136 if (pTmp->GetMedium())
137 // ??? HasName() MM
138 if (INetURLObject( pTmp->GetMedium()->GetName() ) == aObj)
140 m_xDocShell = pTmp;
141 break;
145 if (!m_xDocShell.Is())
147 SfxErrorContext eEC(ERRCTX_SFX_LOADTEMPLATE, m_xDialog.get());
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)
154 ErrorHandler::HandleError(lErr);
155 if (!m_xDocShell.Is())
157 m_xPreviewController->SetObjectShell(nullptr);
158 return;
162 m_xPreviewController->SetObjectShell(m_xDocShell);
165 IMPL_LINK( SfxNewFileDialog, RegionSelect, weld::TreeView&, rBox, void )
167 if (m_xDocShell.Is() && m_xDocShell->GetProgress())
168 return;
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, "");
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);
188 IMPL_LINK_NOARG(SfxNewFileDialog, Expand, weld::Expander&, void)
190 TemplateSelect(*m_xTemplateLb);
193 IMPL_LINK_NOARG(SfxNewFileDialog, TemplateSelect, weld::TreeView&, void)
195 // Still loading
196 if (m_xDocShell && m_xDocShell->GetProgress())
197 return;
199 if (!m_xMoreBt->get_expanded())
201 // Dialog is not opened
202 return;
205 m_aPrevIdle.Start();
208 IMPL_LINK_NOARG( SfxNewFileDialog, DoubleClick, weld::TreeView&, bool )
210 // Still loading
211 if (!m_xDocShell.Is() || !m_xDocShell->GetProgress())
212 m_xDialog->response(RET_OK);
213 return true;
216 sal_uInt16 SfxNewFileDialog::GetSelectedTemplatePos() const
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, "");
225 if ( aSel.compareToIgnoreAsciiCase(SfxResId(STR_STANDARD)) != 0 )
226 nEntry++;
227 return nEntry;
230 SfxNewFileDialog::SfxNewFileDialog(weld::Window *pParent, SfxNewFileDialogMode nFlags)
231 : SfxDialogController(pParent, "sfx/ui/loadtemplatedialog.ui", "LoadTemplateDialog")
232 , m_nFlags(nFlags)
233 , m_xPreviewController(new SfxPreviewWin_Impl)
234 , m_xRegionLb(m_xBuilder->weld_tree_view("categories"))
235 , m_xTemplateLb(m_xBuilder->weld_tree_view("templates"))
236 , m_xTextStyleCB(m_xBuilder->weld_check_button("text"))
237 , m_xFrameStyleCB(m_xBuilder->weld_check_button("frame"))
238 , m_xPageStyleCB(m_xBuilder->weld_check_button("pages"))
239 , m_xNumStyleCB(m_xBuilder->weld_check_button("numbering"))
240 , m_xMergeStyleCB(m_xBuilder->weld_check_button("overwrite"))
241 , m_xLoadFilePB(m_xBuilder->weld_button("fromfile"))
242 , m_xMoreBt(m_xBuilder->weld_expander("expander"))
243 , m_xPreviewWin(new weld::CustomWeld(*m_xBuilder, "image", *m_xPreviewController))
244 , m_xAltTitleFt(m_xBuilder->weld_label("alttitle"))
246 const int nWidth = m_xRegionLb->get_approximate_digit_width() * 32;
247 const int nHeight = m_xRegionLb->get_height_rows(8);
248 m_xRegionLb->set_size_request(nWidth, nHeight);
249 m_xTemplateLb->set_size_request(nWidth, nHeight);
250 m_xPreviewWin->set_size_request(nWidth, nWidth);
252 if (nFlags == SfxNewFileDialogMode::NONE)
253 m_xMoreBt->hide();
254 else if(SfxNewFileDialogMode::LoadTemplate == nFlags)
256 m_xLoadFilePB->show();
257 m_xTextStyleCB->show();
258 m_xFrameStyleCB->show();
259 m_xPageStyleCB->show();
260 m_xNumStyleCB->show();
261 m_xMergeStyleCB->show();
262 m_xMoreBt->hide();
263 m_xTextStyleCB->set_active(true);
264 m_xDialog->set_title(m_xAltTitleFt->get_label());
266 else
268 m_xMoreBt->connect_expanded(LINK(this, SfxNewFileDialog, Expand));
269 m_xPreviewWin->show();
272 OUString sExtraData;
273 SvtViewOptions aDlgOpt(EViewType::Dialog, OStringToOUString(m_xDialog->get_help_id(), RTL_TEXTENCODING_UTF8));
274 if (aDlgOpt.Exists())
276 css::uno::Any aUserItem = aDlgOpt.GetUserItem("UserItem");
277 aUserItem >>= sExtraData;
280 bool bExpand = !sExtraData.isEmpty() && sExtraData[0] == 'Y';
281 m_xMoreBt->set_expanded(bExpand && (nFlags != SfxNewFileDialogMode::NONE));
283 m_xTemplateLb->connect_changed(LINK(this, SfxNewFileDialog, TemplateSelect));
284 m_xTemplateLb->connect_row_activated(LINK(this, SfxNewFileDialog, DoubleClick));
286 // update the template configuration if necessary
288 weld::WaitObject aWaitCursor(m_xDialog.get());
289 m_aTemplates.Update();
291 // fill the list boxes
292 const sal_uInt16 nCount = m_aTemplates.GetRegionCount();
293 if (nCount)
295 for(sal_uInt16 i = 0; i < nCount; ++i)
296 m_xRegionLb->append_text(m_aTemplates.GetFullRegionName(i));
297 m_xRegionLb->connect_changed(LINK(this, SfxNewFileDialog, RegionSelect));
300 m_aPrevIdle.SetPriority( TaskPriority::LOWEST );
301 m_aPrevIdle.SetInvokeHandler( LINK( this, SfxNewFileDialog, Update));
303 m_xRegionLb->select(0);
304 RegionSelect(*m_xRegionLb);
307 SfxNewFileDialog::~SfxNewFileDialog()
309 SvtViewOptions aDlgOpt(EViewType::Dialog, OStringToOUString(m_xDialog->get_help_id(), RTL_TEXTENCODING_UTF8));
310 aDlgOpt.SetUserItem("UserItem", css::uno::makeAny(m_xMoreBt->get_expanded() ? OUString("Y") : OUString("N")));
313 bool SfxNewFileDialog::IsTemplate() const
315 return GetSelectedTemplatePos()!=0;
318 OUString SfxNewFileDialog::GetTemplateFileName() const
320 if (!IsTemplate() || !m_aTemplates.GetRegionCount())
321 return OUString();
322 return m_aTemplates.GetPath(m_xRegionLb->get_selected_index(),
323 GetSelectedTemplatePos()-1);
326 SfxTemplateFlags SfxNewFileDialog::GetTemplateFlags()const
328 SfxTemplateFlags nRet = m_xTextStyleCB->get_active() ? SfxTemplateFlags::LOAD_TEXT_STYLES : SfxTemplateFlags::NONE;
329 if(m_xFrameStyleCB->get_active())
330 nRet |= SfxTemplateFlags::LOAD_FRAME_STYLES;
331 if(m_xPageStyleCB->get_active())
332 nRet |= SfxTemplateFlags::LOAD_PAGE_STYLES;
333 if(m_xNumStyleCB->get_active())
334 nRet |= SfxTemplateFlags::LOAD_NUM_STYLES;
335 if(m_xMergeStyleCB->get_active())
336 nRet |= SfxTemplateFlags::MERGE_STYLES;
337 return nRet;
340 void SfxNewFileDialog::SetTemplateFlags(SfxTemplateFlags nSet)
342 m_xTextStyleCB->set_active( bool(nSet & SfxTemplateFlags::LOAD_TEXT_STYLES ));
343 m_xFrameStyleCB->set_active( bool(nSet & SfxTemplateFlags::LOAD_FRAME_STYLES));
344 m_xPageStyleCB->set_active( bool(nSet & SfxTemplateFlags::LOAD_PAGE_STYLES ));
345 m_xNumStyleCB->set_active( bool(nSet & SfxTemplateFlags::LOAD_NUM_STYLES ));
346 m_xMergeStyleCB->set_active( bool(nSet & SfxTemplateFlags::MERGE_STYLES ));
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */