LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / doc / new.cxx
blob37edc065bd8860847b7e531e708118b6df350641
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, 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);
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, u"");
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_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"))
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)
254 m_xMoreBt->hide();
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();
263 m_xMoreBt->hide();
264 m_xTextStyleCB->set_active(true);
265 m_xDialog->set_title(m_xAltTitleFt->get_label());
267 else
269 m_xMoreBt->connect_expanded(LINK(this, SfxNewFileDialog, Expand));
270 m_xPreviewWin->show();
273 OUString sExtraData;
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();
294 if (nCount)
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())
322 return OUString();
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;
338 return nRet;
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: */