bump product version to 4.2.0.1
[LibreOffice.git] / sfx2 / source / control / templateabstractview.cxx
blobddaac09e4b0be75f5754c37163218a500bd2bb60
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/templateabstractview.hxx>
12 #include <comphelper/processfactory.hxx>
13 #include <sfx2/sfxresid.hxx>
14 #include <sfx2/templatecontaineritem.hxx>
15 #include <sfx2/templateviewitem.hxx>
16 #include <tools/urlobj.hxx>
17 #include <unotools/ucbstreamhelper.hxx>
18 #include <vcl/pngread.hxx>
20 #include <basegfx/polygon/b2dpolygon.hxx>
21 #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
22 #include <drawinglayer/processor2d/baseprocessor2d.hxx>
24 #include <com/sun/star/embed/ElementModes.hpp>
25 #include <com/sun/star/embed/XStorage.hpp>
26 #include <com/sun/star/embed/StorageFactory.hpp>
27 #include <com/sun/star/lang/XComponent.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
31 #include "../doc/doc.hrc"
32 #include "templateview.hrc"
34 using namespace basegfx;
35 using namespace drawinglayer::primitive2d;
37 bool ViewFilter_Application::isFilteredExtension(FILTER_APPLICATION filter, const OUString &rExt)
39 bool bRet = true;
41 if (filter == FILTER_APP_WRITER)
43 bRet = rExt == "ott" || rExt == "stw" || rExt == "oth" || rExt == "dot" || rExt == "dotx";
45 else if (filter == FILTER_APP_CALC)
47 bRet = rExt == "ots" || rExt == "stc" || rExt == "xlt" || rExt == "xltm" || rExt == "xltx";
49 else if (filter == FILTER_APP_IMPRESS)
51 bRet = rExt == "otp" || rExt == "sti" || rExt == "pot" || rExt == "potm" || rExt == "potx";
53 else if (filter == FILTER_APP_DRAW)
55 bRet = rExt == "otg" || rExt == "std";
58 return bRet;
61 bool ViewFilter_Application::isValid (const OUString &rPath) const
63 INetURLObject aUrl(rPath);
64 return isFilteredExtension(mApp, aUrl.getExtension());
67 bool ViewFilter_Application::operator () (const ThumbnailViewItem *pItem)
69 const TemplateViewItem *pTempItem = dynamic_cast<const TemplateViewItem*>(pItem);
70 if (pTempItem)
71 return isValid(pTempItem->getPath());
73 TemplateContainerItem *pContainerItem = const_cast<TemplateContainerItem*>(dynamic_cast<const TemplateContainerItem*>(pItem));
74 if (pContainerItem)
76 std::vector<TemplateItemProperties> &rTemplates = pContainerItem->maTemplates;
78 size_t nVisCount = 0;
80 // Clear thumbnails
81 pContainerItem->maPreview1.Clear();
82 pContainerItem->maPreview2.Clear();
83 pContainerItem->maPreview3.Clear();
84 pContainerItem->maPreview4.Clear();
86 for (size_t i = 0, n = rTemplates.size(); i < n && pContainerItem->HasMissingPreview(); ++i)
88 if (isValid(rTemplates[i].aPath))
90 ++nVisCount;
91 if ( pContainerItem->maPreview1.IsEmpty( ) )
93 pContainerItem->maPreview1 = TemplateAbstractView::scaleImg(rTemplates[i].aThumbnail,
94 TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
95 TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
97 else if ( pContainerItem->maPreview2.IsEmpty() )
99 pContainerItem->maPreview2 = TemplateAbstractView::scaleImg(rTemplates[i].aThumbnail,
100 TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
101 TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
103 else if ( pContainerItem->maPreview3.IsEmpty() )
105 pContainerItem->maPreview3 = TemplateAbstractView::scaleImg(rTemplates[i].aThumbnail,
106 TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
107 TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
109 else if ( pContainerItem->maPreview4.IsEmpty() )
111 pContainerItem->maPreview4 = TemplateAbstractView::scaleImg(rTemplates[i].aThumbnail,
112 TEMPLATE_THUMBNAIL_MAX_WIDTH*0.75,
113 TEMPLATE_THUMBNAIL_MAX_HEIGHT*0.75);
118 return true;
121 bool ViewFilter_Keyword::operator ()(const ThumbnailViewItem *pItem)
123 assert(pItem);
125 return pItem->maTitle.matchIgnoreAsciiCase(maKeyword);
128 TemplateAbstractView::TemplateAbstractView (Window *pParent, WinBits nWinStyle, bool bDisableTransientChildren)
129 : ThumbnailView(pParent,nWinStyle,bDisableTransientChildren),
130 mnCurRegionId(0),
131 maAllButton(this, SfxResId(BTN_ALL_TEMPLATES)),
132 maFTName(this, SfxResId(FT_NAME))
134 maAllButton.Hide();
135 maAllButton.SetStyle(maAllButton.GetStyle() | WB_FLATBUTTON);
136 maAllButton.SetClickHdl(LINK(this,TemplateAbstractView,ShowRootRegionHdl));
137 maFTName.Hide();
140 TemplateAbstractView::TemplateAbstractView(Window *pParent, const ResId &rResId, bool bDisableTransientChildren)
141 : ThumbnailView(pParent,rResId,bDisableTransientChildren),
142 mnCurRegionId(0),
143 maAllButton(this, SfxResId(BTN_ALL_TEMPLATES)),
144 maFTName(this, SfxResId(FT_NAME))
146 maAllButton.Hide();
147 maAllButton.SetStyle(maAllButton.GetStyle() | WB_FLATBUTTON);
148 maAllButton.SetClickHdl(LINK(this,TemplateAbstractView,ShowRootRegionHdl));
149 maFTName.Hide();
152 TemplateAbstractView::TemplateAbstractView(Window *pParent)
153 : ThumbnailView(pParent),
154 mnCurRegionId(0),
155 maAllButton(this, SfxResId(BTN_ALL_TEMPLATES)),
156 maFTName(this, SfxResId(FT_NAME))
158 maAllButton.Hide();
159 maAllButton.SetStyle(maAllButton.GetStyle() | WB_FLATBUTTON);
160 maAllButton.SetClickHdl(LINK(this,TemplateAbstractView,ShowRootRegionHdl));
161 maFTName.Hide();
164 TemplateAbstractView::~TemplateAbstractView ()
168 void TemplateAbstractView::insertItem(const TemplateItemProperties &rTemplate)
170 const TemplateItemProperties *pCur = &rTemplate;
172 TemplateViewItem *pChild = new TemplateViewItem(*this, pCur->nId);
173 pChild->mnDocId = pCur->nDocId;
174 pChild->mnRegionId = pCur->nRegionId;
175 pChild->maTitle = pCur->aName;
176 pChild->setPath(pCur->aPath);
177 pChild->maPreview1 = pCur->aThumbnail;
179 if ( pCur->aThumbnail.IsEmpty() )
181 // Use the default thumbnail if we have nothing else
182 pChild->maPreview1 = TemplateAbstractView::getDefaultThumbnail(pCur->aPath);
185 pChild->setSelectClickHdl(LINK(this,ThumbnailView,OnItemSelected));
187 AppendItem(pChild);
189 CalculateItemPositions();
190 Invalidate();
193 void TemplateAbstractView::insertItems(const std::vector<TemplateItemProperties> &rTemplates)
195 std::vector<ThumbnailViewItem*> aItems(rTemplates.size());
196 for (size_t i = 0, n = rTemplates.size(); i < n; ++i )
198 //TODO: CHECK IF THE ITEM IS A FOLDER OR NOT
199 const TemplateItemProperties *pCur = &rTemplates[i];
201 TemplateViewItem *pChild = new TemplateViewItem(*this, pCur->nId);
202 pChild->mnDocId = pCur->nDocId;
203 pChild->mnRegionId = pCur->nRegionId;
204 pChild->maTitle = pCur->aName;
205 pChild->setPath(pCur->aPath);
206 pChild->maPreview1 = pCur->aThumbnail;
208 if ( pCur->aThumbnail.IsEmpty() )
210 // Use the default thumbnail if we have nothing else
211 pChild->maPreview1 = TemplateAbstractView::getDefaultThumbnail(pCur->aPath);
214 pChild->setSelectClickHdl(LINK(this,ThumbnailView,OnItemSelected));
216 aItems[i] = pChild;
219 updateItems(aItems);
222 sal_uInt16 TemplateAbstractView::getCurRegionId() const
224 return mnCurRegionId;
227 const OUString &TemplateAbstractView::getCurRegionName() const
229 return maCurRegionName;
232 bool TemplateAbstractView::isNonRootRegionVisible () const
234 return mnCurRegionId;
237 void TemplateAbstractView::setOpenRegionHdl(const Link &rLink)
239 maOpenRegionHdl = rLink;
242 void TemplateAbstractView::setOpenTemplateHdl(const Link &rLink)
244 maOpenTemplateHdl = rLink;
247 BitmapEx TemplateAbstractView::scaleImg (const BitmapEx &rImg, long width, long height)
249 BitmapEx aImg = rImg;
251 if (!rImg.IsEmpty())
253 Size aSize = rImg.GetSizePixel();
255 if (aSize.Width() == 0)
256 aSize.Width() = 1;
258 if (aSize.Height() == 0)
259 aSize.Height() = 1;
261 // make the picture fit the given width/height constraints
262 double nRatio = std::min(double(width)/double(aSize.Width()), double(height)/double(aSize.Height()));
264 aImg.Scale(Size(aSize.Width() * nRatio, aSize.Height() * nRatio));
267 return aImg;
270 BitmapEx TemplateAbstractView::getDefaultThumbnail( const OUString& rPath )
272 BitmapEx aImg;
273 INetURLObject aUrl(rPath);
274 OUString aExt = aUrl.getExtension();
276 if ( ViewFilter_Application::isFilteredExtension( FILTER_APP_WRITER, aExt) )
277 aImg = BitmapEx ( SfxResId( SFX_THUMBNAIL_TEXT ) );
278 else if ( ViewFilter_Application::isFilteredExtension( FILTER_APP_CALC, aExt) )
279 aImg = BitmapEx ( SfxResId( SFX_THUMBNAIL_SHEET ) );
280 else if ( ViewFilter_Application::isFilteredExtension( FILTER_APP_IMPRESS, aExt) )
281 aImg = BitmapEx ( SfxResId( SFX_THUMBNAIL_PRESENTATION ) );
282 else if ( ViewFilter_Application::isFilteredExtension( FILTER_APP_DRAW, aExt) )
283 aImg = BitmapEx ( SfxResId( SFX_THUMBNAIL_DRAWING ) );
285 return aImg;
288 BitmapEx TemplateAbstractView::fetchThumbnail (const OUString &msURL, long width, long height)
290 return TemplateAbstractView::scaleImg(ThumbnailView::readThumbnail(msURL), width, height);
293 IMPL_LINK_NOARG(TemplateAbstractView, ShowRootRegionHdl)
295 showRootRegion();
296 return 0;
299 void TemplateAbstractView::OnItemDblClicked (ThumbnailViewItem *pItem)
301 //Check if the item is a TemplateContainerItem (Folder) or a TemplateViewItem (File)
303 TemplateContainerItem* pContainerItem = dynamic_cast<TemplateContainerItem*>(pItem);
304 if ( pContainerItem )
306 // Fill templates
308 mnCurRegionId = pContainerItem->mnRegionId+1;
309 maCurRegionName = pContainerItem->maTitle;
310 maFTName.SetText(maCurRegionName);
311 showRegion(pItem);
313 else
315 maOpenTemplateHdl.Call(pItem);
319 void TemplateAbstractView::Paint( const Rectangle& rRect )
321 ThumbnailView::Paint( rRect );
323 Rectangle aRect(rRect.TopLeft(),
324 Point(rRect.BottomRight().X(), mnHeaderHeight));
326 drawinglayer::primitive2d::Primitive2DSequence aSeq(1);
327 aSeq[0] = drawinglayer::primitive2d::Primitive2DReference(
328 new PolyPolygonColorPrimitive2D(B2DPolyPolygon(Polygon(aRect).getB2DPolygon()),
329 BColor(1.0, 1.0, 1.0)));
331 mpProcessor->process(aSeq);
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */