Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sfx2 / source / control / recentdocsviewitem.cxx
blob85d84ddc699dd5e8d6f4ad825be77138a54210b0
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/.
8 */
10 #include <sfx2/recentdocsviewitem.hxx>
12 #include <com/sun/star/frame/Desktop.hpp>
13 #include <com/sun/star/util/URLTransformer.hpp>
14 #include <drawinglayer/primitive2d/baseprimitive2d.hxx>
15 #include <drawinglayer/primitive2d/discretebitmapprimitive2d.hxx>
16 #include <drawinglayer/processor2d/baseprocessor2d.hxx>
17 #include <i18nutil/paper.hxx>
18 #include <officecfg/Office/Common.hxx>
19 #include <sfx2/recentdocsview.hxx>
20 #include <sfx2/templatelocalview.hxx>
21 #include <tools/urlobj.hxx>
22 #include <unotools/historyoptions.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/event.hxx>
25 #include <vcl/ptrstyle.hxx>
27 #include <bitmaps.hlst>
29 using namespace basegfx;
30 using namespace com::sun::star;
31 using namespace com::sun::star::uno;
32 using namespace drawinglayer::primitive2d;
33 using namespace drawinglayer::processor2d;
35 RecentDocsViewItem::RecentDocsViewItem(sfx2::RecentDocsView &rView, const OUString &rURL,
36 const OUString &rTitle, const BitmapEx &rThumbnail, sal_uInt16 nId, long nThumbnailSize)
37 : ThumbnailViewItem(rView, nId),
38 mrParentView(rView),
39 maURL(rURL),
40 m_bRemoveIconHighlighted(false),
41 m_aRemoveRecentBitmap(BMP_RECENTDOC_REMOVE),
42 m_aRemoveRecentBitmapHighlighted(BMP_RECENTDOC_REMOVE_HIGHLIGHTED)
44 OUString aTitle(rTitle);
45 INetURLObject aURLObj(rURL);
47 if( aURLObj.GetProtocol() == INetProtocol::File )
48 m_sHelpText = aURLObj.getFSysPath(FSysStyle::Detect);
49 if( m_sHelpText.isEmpty() )
50 m_sHelpText = aURLObj.GetURLNoPass();
52 if (aTitle.isEmpty())
53 aTitle = aURLObj.GetLastName(INetURLObject::DecodeMechanism::WithCharset);
55 BitmapEx aThumbnail(rThumbnail);
56 //fdo#74834: only load thumbnail if the corresponding option is not disabled in the configuration
57 if (aThumbnail.IsEmpty() && aURLObj.GetProtocol() == INetProtocol::File &&
58 officecfg::Office::Common::History::RecentDocsThumbnail::get())
59 aThumbnail = ThumbnailView::readThumbnail(rURL);
61 if (aThumbnail.IsEmpty())
63 // Use the default thumbnail if we have nothing else
64 BitmapEx aExt(sfx2::RecentDocsView::getDefaultThumbnail(rURL));
65 Size aExtSize(aExt.GetSizePixel());
67 // attempt to make it appear as if it is on a piece of paper
68 long nPaperHeight;
69 long nPaperWidth;
70 if (sfx2::RecentDocsView::typeMatchesExtension(
71 sfx2::ApplicationType::TYPE_IMPRESS, aURLObj.getExtension()))
73 // Swap width and height (PAPER_SCREEN_4_3 definition make it needed)
74 PaperInfo aInfo(PAPER_SCREEN_4_3);
75 nPaperHeight = aInfo.getWidth();
76 nPaperWidth = aInfo.getHeight();
78 else
80 PaperInfo aInfo(PaperInfo::getSystemDefaultPaper());
81 nPaperHeight = aInfo.getHeight();
82 nPaperWidth = aInfo.getWidth();
84 double ratio = double(nThumbnailSize) / double(std::max(nPaperHeight, nPaperWidth));
85 Size aThumbnailSize(nPaperWidth * ratio, nPaperHeight * ratio);
87 if (aExtSize.Width() > aThumbnailSize.Width() || aExtSize.Height() > aThumbnailSize.Height())
89 aExt = TemplateLocalView::scaleImg(aExt, aThumbnailSize.Width(), aThumbnailSize.Height());
90 aExtSize = aExt.GetSizePixel();
93 // create empty, and copy the default thumbnail in
94 sal_uInt8 nAlpha = 255;
95 aThumbnail = BitmapEx(Bitmap(aThumbnailSize, 24), AlphaMask(aThumbnailSize, &nAlpha));
97 aThumbnail.CopyPixel(
98 ::tools::Rectangle(Point((aThumbnailSize.Width() - aExtSize.Width()) / 2, (aThumbnailSize.Height() - aExtSize.Height()) / 2), aExtSize),
99 ::tools::Rectangle(Point(0, 0), aExtSize),
100 &aExt);
103 maTitle = aTitle;
104 maPreview1 = TemplateLocalView::scaleImg(aThumbnail, nThumbnailSize, nThumbnailSize);
107 ::tools::Rectangle RecentDocsViewItem::updateHighlight(bool bVisible, const Point& rPoint)
109 ::tools::Rectangle aRect(ThumbnailViewItem::updateHighlight(bVisible, rPoint));
111 if (bVisible && getRemoveIconArea().IsInside(rPoint))
113 if (!m_bRemoveIconHighlighted)
114 aRect.Union(getRemoveIconArea());
116 m_bRemoveIconHighlighted = true;
118 else
120 if (m_bRemoveIconHighlighted)
121 aRect.Union(getRemoveIconArea());
123 m_bRemoveIconHighlighted = false;
126 return aRect;
129 ::tools::Rectangle RecentDocsViewItem::getRemoveIconArea() const
131 ::tools::Rectangle aArea(getDrawArea());
132 Size aSize(m_aRemoveRecentBitmap.GetSizePixel());
134 return ::tools::Rectangle(
135 Point(aArea.Right() - aSize.Width() - THUMBNAILVIEW_ITEM_CORNER, aArea.Top() + THUMBNAILVIEW_ITEM_CORNER),
136 aSize);
139 OUString RecentDocsViewItem::getHelpText() const
141 return m_sHelpText;
144 void RecentDocsViewItem::Paint(drawinglayer::processor2d::BaseProcessor2D *pProcessor, const ThumbnailItemAttributes *pAttrs)
146 ThumbnailViewItem::Paint(pProcessor, pAttrs);
148 // paint the remove icon when highlighted
149 if (isHighlighted())
151 drawinglayer::primitive2d::Primitive2DContainer aSeq(1);
153 Point aIconPos(getRemoveIconArea().TopLeft());
155 aSeq[0] = drawinglayer::primitive2d::Primitive2DReference(new DiscreteBitmapPrimitive2D(
156 m_bRemoveIconHighlighted ? m_aRemoveRecentBitmapHighlighted : m_aRemoveRecentBitmap,
157 B2DPoint(aIconPos.X(), aIconPos.Y())));
159 pProcessor->process(aSeq);
163 void RecentDocsViewItem::MouseButtonUp(const MouseEvent& rMEvt)
165 if (rMEvt.IsLeft())
167 if (getRemoveIconArea().IsInside(rMEvt.GetPosPixel()))
169 SvtHistoryOptions().DeleteItem(ePICKLIST, maURL);
170 mrParent.Reload();
171 return;
174 OpenDocument();
175 return;
179 void RecentDocsViewItem::OpenDocument()
181 // show busy mouse pointer
182 mrParentView.SetPointer(PointerStyle::Wait);
184 Reference<frame::XDispatch> xDispatch;
185 Reference<frame::XDispatchProvider> xDispatchProvider;
186 css::util::URL aTargetURL;
187 Sequence<beans::PropertyValue> aArgsList;
189 uno::Reference<frame::XDesktop2> xDesktop = frame::Desktop::create(::comphelper::getProcessComponentContext());
190 uno::Reference<frame::XFrame> xActiveFrame = xDesktop->getActiveFrame();
192 //osl::ClearableMutexGuard aLock(m_aMutex);
193 xDispatchProvider.set(xActiveFrame, UNO_QUERY);
194 //aLock.clear();
196 aTargetURL.Complete = maURL;
197 Reference<util::XURLTransformer> xTrans(util::URLTransformer::create(::comphelper::getProcessComponentContext()));
198 xTrans->parseStrict(aTargetURL);
200 aArgsList.realloc(2);
201 aArgsList[0].Name = "Referer";
202 aArgsList[0].Value <<= OUString("private:user");
204 // documents will never be opened as templates
205 aArgsList[1].Name = "AsTemplate";
206 aArgsList[1].Value <<= false;
208 xDispatch = xDispatchProvider->queryDispatch(aTargetURL, "_default", 0);
210 if (!xDispatch.is())
211 return;
213 // Call dispatch asynchronously as we can be destroyed while dispatch is
214 // executed. VCL is not able to survive this as it wants to call listeners
215 // after select!!!
216 sfx2::LoadRecentFile *const pLoadRecentFile = new sfx2::LoadRecentFile;
217 pLoadRecentFile->xDispatch = xDispatch;
218 pLoadRecentFile->aTargetURL = aTargetURL;
219 pLoadRecentFile->aArgSeq = aArgsList;
220 pLoadRecentFile->pView = &mrParentView;
222 Application::PostUserEvent(LINK(nullptr, sfx2::RecentDocsView, ExecuteHdl_Impl), pLoadRecentFile, true);
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */