Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #include <sfx2/recentdocsviewitem.hxx>
11 :
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 <sfx2/recentdocsview.hxx>
19 : #include <sfx2/sfxresid.hxx>
20 : #include <sfx2/templateabstractview.hxx>
21 : #include <tools/urlobj.hxx>
22 : #include <unotools/historyoptions.hxx>
23 : #include <vcl/svapp.hxx>
24 :
25 : #include <templateview.hrc>
26 :
27 : using namespace basegfx;
28 : using namespace com::sun::star;
29 : using namespace com::sun::star::uno;
30 : using namespace drawinglayer::primitive2d;
31 : using namespace drawinglayer::processor2d;
32 :
33 0 : RecentDocsViewItem::RecentDocsViewItem(ThumbnailView &rView, const OUString &rURL,
34 : const OUString &rTitle, const BitmapEx &rThumbnail, sal_uInt16 nId, long nThumbnailSize)
35 : : ThumbnailViewItem(rView, nId),
36 : maURL(rURL),
37 : m_bRemoveIconHighlighted(false),
38 : m_aRemoveRecentBitmap(SfxResId(IMG_RECENTDOC_REMOVE)),
39 0 : m_aRemoveRecentBitmapHighlighted(SfxResId(IMG_RECENTDOC_REMOVE_HIGHLIGHTED))
40 : {
41 0 : OUString aTitle(rTitle);
42 0 : INetURLObject aURLObj(rURL);
43 :
44 0 : if( aURLObj.GetProtocol() == INetProtocol::File )
45 0 : m_sHelpText = aURLObj.getFSysPath(INetURLObject::FSYS_DETECT);
46 0 : if( m_sHelpText.isEmpty() )
47 0 : m_sHelpText = aURLObj.GetURLNoPass();
48 :
49 0 : if (aTitle.isEmpty())
50 0 : aTitle = aURLObj.GetName(INetURLObject::DECODE_WITH_CHARSET);
51 :
52 0 : BitmapEx aThumbnail(rThumbnail);
53 0 : if (aThumbnail.IsEmpty() && aURLObj.GetProtocol() == INetProtocol::File)
54 0 : aThumbnail = ThumbnailView::readThumbnail(rURL);
55 :
56 0 : if (aThumbnail.IsEmpty())
57 : {
58 : // Use the default thumbnail if we have nothing else
59 0 : BitmapEx aExt(RecentDocsView::getDefaultThumbnail(rURL));
60 0 : Size aExtSize(aExt.GetSizePixel());
61 :
62 : // attempt to make it appear as if it is on a piece of paper
63 : long nPaperHeight;
64 : long nPaperWidth;
65 0 : if( RecentDocsView::typeMatchesExtension(TYPE_IMPRESS, aURLObj.getExtension()) )
66 : {
67 : // Swap width and height (PAPER_SCREEN_4_3 definition make it needed)
68 0 : PaperInfo aInfo(PAPER_SCREEN_4_3);
69 0 : nPaperHeight = aInfo.getWidth();
70 0 : nPaperWidth = aInfo.getHeight();
71 : }
72 : else
73 : {
74 0 : PaperInfo aInfo(PaperInfo::getSystemDefaultPaper());
75 0 : nPaperHeight = aInfo.getHeight();
76 0 : nPaperWidth = aInfo.getWidth();
77 : }
78 0 : double ratio = double(nThumbnailSize) / double(std::max(nPaperHeight, nPaperWidth));
79 0 : Size aThumbnailSize(nPaperWidth * ratio, nPaperHeight * ratio);
80 :
81 0 : if (aExtSize.Width() > aThumbnailSize.Width() || aExtSize.Height() > aThumbnailSize.Height())
82 : {
83 0 : aExt = TemplateAbstractView::scaleImg(aExt, aThumbnailSize.Width(), aThumbnailSize.Height());
84 0 : aExtSize = aExt.GetSizePixel();
85 : }
86 :
87 : // create empty, and copy the default thumbnail in
88 0 : sal_uInt8 nAlpha = 255;
89 0 : aThumbnail = BitmapEx(Bitmap(aThumbnailSize, 24), AlphaMask(aThumbnailSize, &nAlpha));
90 :
91 : aThumbnail.CopyPixel(
92 0 : Rectangle(Point((aThumbnailSize.Width() - aExtSize.Width()) / 2, (aThumbnailSize.Height() - aExtSize.Height()) / 2), aExtSize),
93 : Rectangle(Point(0, 0), aExtSize),
94 0 : &aExt);
95 : }
96 :
97 0 : maTitle = aTitle;
98 0 : maPreview1 = TemplateAbstractView::scaleImg(aThumbnail, nThumbnailSize, nThumbnailSize);
99 0 : }
100 :
101 0 : void RecentDocsViewItem::setEditTitle (bool edit, bool bChangeFocus)
102 : {
103 : // Unused parameters.
104 : (void)edit;
105 : (void)bChangeFocus;
106 0 : }
107 :
108 0 : Rectangle RecentDocsViewItem::updateHighlight(bool bVisible, const Point& rPoint)
109 : {
110 0 : Rectangle aRect(ThumbnailViewItem::updateHighlight(bVisible, rPoint));
111 :
112 0 : if (bVisible && getRemoveIconArea().IsInside(rPoint))
113 : {
114 0 : if (!m_bRemoveIconHighlighted)
115 0 : aRect.Union(getRemoveIconArea());
116 :
117 0 : m_bRemoveIconHighlighted = true;
118 : }
119 : else
120 : {
121 0 : if (m_bRemoveIconHighlighted)
122 0 : aRect.Union(getRemoveIconArea());
123 :
124 0 : m_bRemoveIconHighlighted = false;
125 : }
126 :
127 0 : return aRect;
128 : }
129 :
130 0 : Rectangle RecentDocsViewItem::getRemoveIconArea() const
131 : {
132 0 : Rectangle aArea(getDrawArea());
133 0 : Size aSize(m_aRemoveRecentBitmap.GetSizePixel());
134 :
135 : return Rectangle(
136 0 : Point(aArea.Right() - aSize.Width() - THUMBNAILVIEW_ITEM_CORNER, aArea.Top() + THUMBNAILVIEW_ITEM_CORNER),
137 0 : aSize);
138 : }
139 :
140 0 : OUString RecentDocsViewItem::getHelpText() const
141 : {
142 0 : return m_sHelpText;
143 : }
144 :
145 0 : void RecentDocsViewItem::Paint(drawinglayer::processor2d::BaseProcessor2D *pProcessor, const ThumbnailItemAttributes *pAttrs)
146 : {
147 0 : ThumbnailViewItem::Paint(pProcessor, pAttrs);
148 :
149 : // paint the remove icon when highlighted
150 0 : if (isHighlighted())
151 : {
152 0 : drawinglayer::primitive2d::Primitive2DSequence aSeq(1);
153 :
154 0 : Point aIconPos(getRemoveIconArea().TopLeft());
155 :
156 0 : aSeq[0] = drawinglayer::primitive2d::Primitive2DReference(new DiscreteBitmapPrimitive2D(
157 : m_bRemoveIconHighlighted ? m_aRemoveRecentBitmapHighlighted : m_aRemoveRecentBitmap,
158 0 : B2DPoint(aIconPos.X(), aIconPos.Y())));
159 :
160 0 : pProcessor->process(aSeq);
161 : }
162 0 : }
163 :
164 0 : void RecentDocsViewItem::MouseButtonUp(const MouseEvent& rMEvt)
165 : {
166 0 : if (rMEvt.IsLeft())
167 : {
168 0 : if (getRemoveIconArea().IsInside(rMEvt.GetPosPixel()))
169 : {
170 0 : SvtHistoryOptions().DeleteItem(ePICKLIST, maURL);
171 0 : mrParent.Reload();
172 0 : return;
173 : }
174 :
175 0 : OpenDocument();
176 0 : return;
177 : }
178 : }
179 :
180 0 : void RecentDocsViewItem::OpenDocument()
181 : {
182 : // show busy mouse pointer
183 0 : mrParent.SetPointer(Pointer(PointerStyle::Wait));
184 :
185 0 : Reference<frame::XDispatch> xDispatch;
186 0 : Reference<frame::XDispatchProvider> xDispatchProvider;
187 0 : css::util::URL aTargetURL;
188 0 : Sequence<beans::PropertyValue> aArgsList;
189 :
190 0 : uno::Reference<frame::XDesktop2> xDesktop = frame::Desktop::create(::comphelper::getProcessComponentContext());
191 0 : uno::Reference<frame::XFrame> xActiveFrame = xDesktop->getActiveFrame();
192 :
193 : //osl::ClearableMutexGuard aLock(m_aMutex);
194 0 : xDispatchProvider = Reference<frame::XDispatchProvider>(xActiveFrame, UNO_QUERY);
195 : //aLock.clear();
196 :
197 0 : aTargetURL.Complete = maURL;
198 0 : Reference<util::XURLTransformer> xTrans(util::URLTransformer::create(::comphelper::getProcessComponentContext()));
199 0 : xTrans->parseStrict(aTargetURL);
200 :
201 0 : sal_Int32 nSize = 2;
202 0 : aArgsList.realloc(nSize);
203 0 : aArgsList[0].Name = "Referer";
204 0 : aArgsList[0].Value = makeAny(OUString("private:user"));
205 :
206 : // documents will never be opened as templates
207 0 : aArgsList[1].Name = "AsTemplate";
208 0 : aArgsList[1].Value = makeAny(false);
209 :
210 0 : xDispatch = xDispatchProvider->queryDispatch(aTargetURL, "_default", 0);
211 :
212 0 : if (xDispatch.is())
213 : {
214 : // Call dispatch asychronously as we can be destroyed while dispatch is
215 : // executed. VCL is not able to survive this as it wants to call listeners
216 : // after select!!!
217 0 : LoadRecentFile* pLoadRecentFile = new LoadRecentFile;
218 0 : pLoadRecentFile->xDispatch = xDispatch;
219 0 : pLoadRecentFile->aTargetURL = aTargetURL;
220 0 : pLoadRecentFile->aArgSeq = aArgsList;
221 :
222 0 : Application::PostUserEvent(LINK(0, RecentDocsView, ExecuteHdl_Impl), pLoadRecentFile, true);
223 0 : }
224 648 : }
225 :
226 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|