1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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
)
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";
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
);
71 return isValid(pTempItem
->getPath());
73 TemplateContainerItem
*pContainerItem
= const_cast<TemplateContainerItem
*>(dynamic_cast<const TemplateContainerItem
*>(pItem
));
76 std::vector
<TemplateItemProperties
> &rTemplates
= pContainerItem
->maTemplates
;
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
))
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);
121 bool ViewFilter_Keyword::operator ()(const ThumbnailViewItem
*pItem
)
125 return pItem
->maTitle
.matchIgnoreAsciiCase(maKeyword
);
128 TemplateAbstractView::TemplateAbstractView (Window
*pParent
, WinBits nWinStyle
, bool bDisableTransientChildren
)
129 : ThumbnailView(pParent
,nWinStyle
,bDisableTransientChildren
),
131 maAllButton(this, SfxResId(BTN_ALL_TEMPLATES
)),
132 maFTName(this, SfxResId(FT_NAME
))
135 maAllButton
.SetStyle(maAllButton
.GetStyle() | WB_FLATBUTTON
);
136 maAllButton
.SetClickHdl(LINK(this,TemplateAbstractView
,ShowRootRegionHdl
));
140 TemplateAbstractView::TemplateAbstractView(Window
*pParent
, const ResId
&rResId
, bool bDisableTransientChildren
)
141 : ThumbnailView(pParent
,rResId
,bDisableTransientChildren
),
143 maAllButton(this, SfxResId(BTN_ALL_TEMPLATES
)),
144 maFTName(this, SfxResId(FT_NAME
))
147 maAllButton
.SetStyle(maAllButton
.GetStyle() | WB_FLATBUTTON
);
148 maAllButton
.SetClickHdl(LINK(this,TemplateAbstractView
,ShowRootRegionHdl
));
152 TemplateAbstractView::TemplateAbstractView(Window
*pParent
)
153 : ThumbnailView(pParent
),
155 maAllButton(this, SfxResId(BTN_ALL_TEMPLATES
)),
156 maFTName(this, SfxResId(FT_NAME
))
159 maAllButton
.SetStyle(maAllButton
.GetStyle() | WB_FLATBUTTON
);
160 maAllButton
.SetClickHdl(LINK(this,TemplateAbstractView
,ShowRootRegionHdl
));
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
));
189 CalculateItemPositions();
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
));
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
;
253 Size aSize
= rImg
.GetSizePixel();
255 if (aSize
.Width() == 0)
258 if (aSize
.Height() == 0)
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
));
270 BitmapEx
TemplateAbstractView::getDefaultThumbnail( const OUString
& rPath
)
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
) );
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
)
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
)
308 mnCurRegionId
= pContainerItem
->mnRegionId
+1;
309 maCurRegionName
= pContainerItem
->maTitle
;
310 maFTName
.SetText(maCurRegionName
);
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: */