tdf#42949 Fix IWYU warnings in sfx2/source/[s-v]*/*cxx and sfx2/qa
[LibreOffice.git] / sfx2 / source / control / recentdocsviewitem.cxx
blob42af6f7527e97a4c2729212fb6c8b3091c5aaec6
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/discretebitmapprimitive2d.hxx>
15 #include <drawinglayer/processor2d/baseprocessor2d.hxx>
16 #include <i18nutil/paper.hxx>
17 #include <officecfg/Office/Common.hxx>
18 #include <sfx2/recentdocsview.hxx>
19 #include <sfx2/templatelocalview.hxx>
20 #include <tools/urlobj.hxx>
21 #include <unotools/historyoptions.hxx>
22 #include <vcl/svapp.hxx>
23 #include <vcl/event.hxx>
24 #include <vcl/ptrstyle.hxx>
26 #include <bitmaps.hlst>
28 using namespace basegfx;
29 using namespace com::sun::star;
30 using namespace com::sun::star::uno;
31 using namespace drawinglayer::primitive2d;
32 using namespace drawinglayer::processor2d;
34 RecentDocsViewItem::RecentDocsViewItem(sfx2::RecentDocsView &rView, const OUString &rURL,
35 const OUString &rTitle, const BitmapEx &rThumbnail, sal_uInt16 nId, long nThumbnailSize)
36 : ThumbnailViewItem(rView, nId),
37 mrParentView(rView),
38 maURL(rURL),
39 m_bRemoveIconHighlighted(false),
40 m_aRemoveRecentBitmap(BMP_RECENTDOC_REMOVE),
41 m_aRemoveRecentBitmapHighlighted(BMP_RECENTDOC_REMOVE_HIGHLIGHTED)
43 OUString aTitle(rTitle);
44 INetURLObject aURLObj(rURL);
46 if( aURLObj.GetProtocol() == INetProtocol::File )
47 m_sHelpText = aURLObj.getFSysPath(FSysStyle::Detect);
48 if( m_sHelpText.isEmpty() )
49 m_sHelpText = aURLObj.GetURLNoPass();
51 if (aTitle.isEmpty())
52 aTitle = aURLObj.GetLastName(INetURLObject::DecodeMechanism::WithCharset);
54 BitmapEx aThumbnail(rThumbnail);
55 //fdo#74834: only load thumbnail if the corresponding option is not disabled in the configuration
56 if (aThumbnail.IsEmpty() && aURLObj.GetProtocol() == INetProtocol::File &&
57 officecfg::Office::Common::History::RecentDocsThumbnail::get())
58 aThumbnail = ThumbnailView::readThumbnail(rURL);
60 if (aThumbnail.IsEmpty())
62 // Use the default thumbnail if we have nothing else
63 BitmapEx aExt(sfx2::RecentDocsView::getDefaultThumbnail(rURL));
64 Size aExtSize(aExt.GetSizePixel());
66 // attempt to make it appear as if it is on a piece of paper
67 long nPaperHeight;
68 long nPaperWidth;
69 if (sfx2::RecentDocsView::typeMatchesExtension(
70 sfx2::ApplicationType::TYPE_IMPRESS, aURLObj.getExtension()))
72 // Swap width and height (PAPER_SCREEN_4_3 definition make it needed)
73 PaperInfo aInfo(PAPER_SCREEN_4_3);
74 nPaperHeight = aInfo.getWidth();
75 nPaperWidth = aInfo.getHeight();
77 else
79 PaperInfo aInfo(PaperInfo::getSystemDefaultPaper());
80 nPaperHeight = aInfo.getHeight();
81 nPaperWidth = aInfo.getWidth();
83 double ratio = double(nThumbnailSize) / double(std::max(nPaperHeight, nPaperWidth));
84 Size aThumbnailSize(nPaperWidth * ratio, nPaperHeight * ratio);
86 if (aExtSize.Width() > aThumbnailSize.Width() || aExtSize.Height() > aThumbnailSize.Height())
88 aExt = TemplateLocalView::scaleImg(aExt, aThumbnailSize.Width(), aThumbnailSize.Height());
89 aExtSize = aExt.GetSizePixel();
92 // create empty, and copy the default thumbnail in
93 sal_uInt8 nAlpha = 255;
94 aThumbnail = BitmapEx(Bitmap(aThumbnailSize, 24), AlphaMask(aThumbnailSize, &nAlpha));
96 aThumbnail.CopyPixel(
97 ::tools::Rectangle(Point((aThumbnailSize.Width() - aExtSize.Width()) / 2, (aThumbnailSize.Height() - aExtSize.Height()) / 2), aExtSize),
98 ::tools::Rectangle(Point(0, 0), aExtSize),
99 &aExt);
102 maTitle = aTitle;
103 maPreview1 = TemplateLocalView::scaleImg(aThumbnail, nThumbnailSize, nThumbnailSize);
106 ::tools::Rectangle RecentDocsViewItem::updateHighlight(bool bVisible, const Point& rPoint)
108 ::tools::Rectangle aRect(ThumbnailViewItem::updateHighlight(bVisible, rPoint));
110 if (bVisible && getRemoveIconArea().IsInside(rPoint))
112 if (!m_bRemoveIconHighlighted)
113 aRect.Union(getRemoveIconArea());
115 m_bRemoveIconHighlighted = true;
117 else
119 if (m_bRemoveIconHighlighted)
120 aRect.Union(getRemoveIconArea());
122 m_bRemoveIconHighlighted = false;
125 return aRect;
128 ::tools::Rectangle RecentDocsViewItem::getRemoveIconArea() const
130 ::tools::Rectangle aArea(getDrawArea());
131 Size aSize(m_aRemoveRecentBitmap.GetSizePixel());
133 return ::tools::Rectangle(
134 Point(aArea.Right() - aSize.Width() - THUMBNAILVIEW_ITEM_CORNER, aArea.Top() + THUMBNAILVIEW_ITEM_CORNER),
135 aSize);
138 OUString RecentDocsViewItem::getHelpText() const
140 return m_sHelpText;
143 void RecentDocsViewItem::Paint(drawinglayer::processor2d::BaseProcessor2D *pProcessor, const ThumbnailItemAttributes *pAttrs)
145 ThumbnailViewItem::Paint(pProcessor, pAttrs);
147 // paint the remove icon when highlighted
148 if (isHighlighted())
150 drawinglayer::primitive2d::Primitive2DContainer aSeq(1);
152 Point aIconPos(getRemoveIconArea().TopLeft());
154 aSeq[0] = drawinglayer::primitive2d::Primitive2DReference(new DiscreteBitmapPrimitive2D(
155 m_bRemoveIconHighlighted ? m_aRemoveRecentBitmapHighlighted : m_aRemoveRecentBitmap,
156 B2DPoint(aIconPos.X(), aIconPos.Y())));
158 pProcessor->process(aSeq);
162 void RecentDocsViewItem::MouseButtonUp(const MouseEvent& rMEvt)
164 if (rMEvt.IsLeft())
166 if (getRemoveIconArea().IsInside(rMEvt.GetPosPixel()))
168 SvtHistoryOptions().DeleteItem(ePICKLIST, maURL);
169 mrParent.Reload();
170 return;
173 OpenDocument();
174 return;
178 void RecentDocsViewItem::OpenDocument()
180 // show busy mouse pointer
181 mrParentView.SetPointer(PointerStyle::Wait);
183 Reference<frame::XDispatch> xDispatch;
184 Reference<frame::XDispatchProvider> xDispatchProvider;
185 css::util::URL aTargetURL;
186 Sequence<beans::PropertyValue> aArgsList;
188 uno::Reference<frame::XDesktop2> xDesktop = frame::Desktop::create(::comphelper::getProcessComponentContext());
189 uno::Reference<frame::XFrame> xActiveFrame = xDesktop->getActiveFrame();
191 //osl::ClearableMutexGuard aLock(m_aMutex);
192 xDispatchProvider.set(xActiveFrame, UNO_QUERY);
193 //aLock.clear();
195 aTargetURL.Complete = maURL;
196 Reference<util::XURLTransformer> xTrans(util::URLTransformer::create(::comphelper::getProcessComponentContext()));
197 xTrans->parseStrict(aTargetURL);
199 aArgsList.realloc(2);
200 aArgsList[0].Name = "Referer";
201 aArgsList[0].Value <<= OUString("private:user");
203 // documents will never be opened as templates
204 aArgsList[1].Name = "AsTemplate";
205 aArgsList[1].Value <<= false;
207 xDispatch = xDispatchProvider->queryDispatch(aTargetURL, "_default", 0);
209 if (!xDispatch.is())
210 return;
212 // Call dispatch asynchronously as we can be destroyed while dispatch is
213 // executed. VCL is not able to survive this as it wants to call listeners
214 // after select!!!
215 sfx2::LoadRecentFile *const pLoadRecentFile = new sfx2::LoadRecentFile;
216 pLoadRecentFile->xDispatch = xDispatch;
217 pLoadRecentFile->aTargetURL = aTargetURL;
218 pLoadRecentFile->aArgSeq = aArgsList;
219 pLoadRecentFile->pView = &mrParentView;
221 Application::PostUserEvent(LINK(nullptr, sfx2::RecentDocsView, ExecuteHdl_Impl), pLoadRecentFile, true);
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */