LibreOffice Module svx (master)  1
galctrl.cxx
Go to the documentation of this file.
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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <config_features.h>
21 
22 #include <sfx2/viewfrm.hxx>
23 #include <sfx2/dispatch.hxx>
24 #include <sfx2/sfxsids.hrc>
25 #include <avmedia/mediaplayer.hxx>
26 #include <helpids.h>
27 #include <galbrws2.hxx>
28 #include <svx/galtheme.hxx>
29 #include <svx/galmisc.hxx>
30 #include <svx/galctrl.hxx>
31 #include <galobj.hxx>
32 #include <avmedia/mediawindow.hxx>
33 #include <vcl/event.hxx>
34 #include <vcl/commandevent.hxx>
35 #include <vcl/graphicfilter.hxx>
36 #include <bitmaps.hlst>
37 
38 GalleryPreview::GalleryPreview(GalleryBrowser2* pParent, std::unique_ptr<weld::ScrolledWindow> xScrolledWindow)
39  : mxScrolledWindow(std::move(xScrolledWindow))
40  , mpParent(pParent)
41  , mpTheme(nullptr)
42 {
43 }
44 
46 {
47  mxScrolledWindow->show();
49 }
50 
52 {
54  mxScrolledWindow->hide();
55 }
56 
58 {
59 }
60 
62 {
63  CustomWidgetController::SetDrawingArea(pDrawingArea);
64  Size aSize = pDrawingArea->get_ref_device().LogicToPixel(Size(70, 88), MapMode(MapUnit::MapAppFont));
65  pDrawingArea->set_size_request(aSize.Width(), aSize.Height());
66  SetOutputSizePixel(aSize);
67 
68  pDrawingArea->set_help_id(HID_GALLERY_WINDOW);
69 
71 }
72 
73 namespace
74 {
75  bool ImplGetGraphicCenterRect(const weld::CustomWidgetController& rWidget, const Graphic& rGraphic, tools::Rectangle& rResultRect)
76  {
77  const Size aWinSize(rWidget.GetOutputSizePixel());
78  Size aNewSize(rWidget.GetDrawingArea()->get_ref_device().LogicToPixel(rGraphic.GetPrefSize(), rGraphic.GetPrefMapMode()));
79  bool bRet = false;
80 
81  if( aNewSize.Width() && aNewSize.Height() )
82  {
83  // scale to fit window
84  const double fGrfWH = static_cast<double>(aNewSize.Width()) / aNewSize.Height();
85  const double fWinWH = static_cast<double>(aWinSize.Width()) / aWinSize.Height();
86 
87  if ( fGrfWH < fWinWH )
88  {
89  aNewSize.setWidth( static_cast<long>( aWinSize.Height() * fGrfWH ) );
90  aNewSize.setHeight( aWinSize.Height() );
91  }
92  else
93  {
94  aNewSize.setWidth( aWinSize.Width() );
95  aNewSize.setHeight( static_cast<long>( aWinSize.Width() / fGrfWH) );
96  }
97 
98  const Point aNewPos( ( aWinSize.Width() - aNewSize.Width() ) >> 1,
99  ( aWinSize.Height() - aNewSize.Height() ) >> 1 );
100 
101  rResultRect = tools::Rectangle( aNewPos, aNewSize );
102  bRet = true;
103  }
104 
105  return bRet;
106  }
107 }
108 
109 bool GalleryPreview::ImplGetGraphicCenterRect( const Graphic& rGraphic, tools::Rectangle& rResultRect ) const
110 {
111  return ::ImplGetGraphicCenterRect(*this, rGraphic, rResultRect);
112 }
113 
114 void GalleryPreview::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
115 {
116  rRenderContext.SetBackground(Wallpaper(GALLERY_BG_COLOR));
117  rRenderContext.Erase();
118 
120  {
121  const Point aPos( aPreviewRect.TopLeft() );
122  const Size aSize( aPreviewRect.GetSize() );
123 
124  if( aGraphicObj.IsAnimated() )
125  aGraphicObj.StartAnimation(&rRenderContext, aPos, aSize);
126  else
127  aGraphicObj.Draw(&rRenderContext, aPos, aSize);
128  }
129 }
130 
132 {
133  if (mpTheme && (rMEvt.GetClicks() == 2))
135  return true;
136 }
137 
139 {
140  if (mpTheme && (rCEvt.GetCommand() == CommandEventId::ContextMenu))
141  {
142  mpParent->ShowContextMenu(rCEvt);
143  return true;
144  }
145  return false;
146 }
147 
149 {
150  if(mpTheme)
151  {
152  GalleryBrowser2* pBrowser = mpParent;
153 
154  switch( rKEvt.GetKeyCode().GetCode() )
155  {
156  case KEY_BACKSPACE:
157  pBrowser->TogglePreview();
158  break;
159 
160  case KEY_HOME:
161  pBrowser->Travel( GalleryBrowserTravel::First );
162  break;
163 
164  case KEY_END:
165  pBrowser->Travel( GalleryBrowserTravel::Last );
166  break;
167 
168  case KEY_LEFT:
169  case KEY_UP:
171  break;
172 
173  case KEY_RIGHT:
174  case KEY_DOWN:
175  pBrowser->Travel( GalleryBrowserTravel::Next );
176  break;
177 
178  default:
179  {
180  if (!pBrowser->KeyInput(rKEvt))
181  return false;
182  }
183  break;
184  }
185 
186  return true;
187  }
188  return false;
189 }
190 
192 {
193  if (mpTheme)
194  return mpParent->StartDrag();
195  return true;
196 }
197 
199 {
200 #if HAVE_FEATURE_AVMEDIA
201  if (rURL.GetProtocol() != INetProtocol::NotValid)
202  {
203  ::avmedia::MediaFloater* pFloater = avmedia::getMediaFloater();
204 
205  if (!pFloater)
206  {
207  SfxViewFrame::Current()->GetBindings().GetDispatcher()->Execute( SID_AVMEDIA_PLAYER, SfxCallMode::SYNCHRON );
208  pFloater = avmedia::getMediaFloater();
209  }
210 
211  if (pFloater)
212  pFloater->setURL( rURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ), "", true );
213  }
214 #else
215  (void) rURL;
216 #endif
217 }
218 
220 {
221 }
222 
224 {
225  CustomWidgetController::SetDrawingArea(pDrawingArea);
226  Size aSize(pDrawingArea->get_ref_device().LogicToPixel(Size(70, 88), MapMode(MapUnit::MapAppFont)));
227  pDrawingArea->set_size_request(aSize.Width(), aSize.Height());
228  pDrawingArea->set_help_id(HID_GALLERY_WINDOW);
229 }
230 
232 {
233  bool bRet = true;
234  Graphic aGraphic;
235 #if HAVE_FEATURE_AVMEDIA
237  {
238  aGraphic = BitmapEx(RID_SVXBMP_GALLERY_MEDIA);
239  }
240  else
241 #endif
242  {
244  GalleryProgress aProgress( &rFilter );
245  if( rFilter.ImportGraphic( aGraphic, _aURL ) )
246  bRet = false;
247  }
248 
249  SetGraphic( aGraphic );
250  Invalidate();
251  return bRet;
252 }
253 
255 {
256  return ::ImplGetGraphicCenterRect(*this, rGraphic, rResultRect);
257 }
258 
260 {
261  rRenderContext.SetBackground(Wallpaper(GALLERY_BG_COLOR));
262 
264  {
265  const Point aPos( aPreviewRect.TopLeft() );
266  const Size aSize( aPreviewRect.GetSize() );
267 
268  if( aGraphicObj.IsAnimated() )
269  aGraphicObj.StartAnimation(&rRenderContext, aPos, aSize);
270  else
271  aGraphicObj.Draw(&rRenderContext, aPos, aSize);
272  }
273 }
274 
275 void GalleryIconView::drawTransparenceBackground(vcl::RenderContext& rOut, const Point& rPos, const Size& rSize)
276 {
277  // draw checkered background
278  static const sal_uInt32 nLen(8);
279  static const Color aW(COL_WHITE);
280  static const Color aG(0xef, 0xef, 0xef);
281 
282  rOut.DrawCheckered(rPos, rSize, nLen, aW, aG);
283 }
284 
285 GalleryIconView::GalleryIconView(GalleryBrowser2* pParent, std::unique_ptr<weld::ScrolledWindow> xScrolledWindow)
286  : ValueSet(std::move(xScrolledWindow))
287  , mpParent(pParent)
288  , mpTheme(nullptr)
289 {
290 }
291 
293 {
294 }
295 
297 {
298  ValueSet::SetDrawingArea(pDrawingArea);
299 
301  EnableFullItemMode( false );
302 
303  SetHelpId( HID_GALLERY_WINDOW );
304  SetExtraSpacing( 2 );
305  SetItemWidth( S_THUMB + 6 );
306  SetItemHeight( S_THUMB + 6 );
307 
308  mxDragDropTargetHelper.reset(new GalleryDragDrop(mpParent, pDrawingArea->get_drop_target()));
309 }
310 
312 {
313  const sal_uInt16 nId = rUDEvt.GetItemId();
314 
315  if (!nId || !mpTheme)
316  return;
317 
318  const tools::Rectangle& rRect = rUDEvt.GetRect();
319  const Size aSize(rRect.GetWidth(), rRect.GetHeight());
320  BitmapEx aBitmapEx;
321  Size aPreparedSize;
322  OUString aItemTextTitle;
323  OUString aItemTextPath;
324 
325  mpTheme->GetPreviewBitmapExAndStrings(nId - 1, aBitmapEx, aPreparedSize, aItemTextTitle, aItemTextPath);
326 
327  bool bNeedToCreate(aBitmapEx.IsEmpty());
328 
329  if (!bNeedToCreate && aItemTextTitle.isEmpty())
330  {
331  bNeedToCreate = true;
332  }
333 
334  if (!bNeedToCreate && aPreparedSize != aSize)
335  {
336  bNeedToCreate = true;
337  }
338 
339  if (bNeedToCreate)
340  {
341  std::unique_ptr<SgaObject> pObj = mpTheme->AcquireObject(nId - 1);
342 
343  if(pObj)
344  {
345  aBitmapEx = pObj->createPreviewBitmapEx(aSize);
346  aItemTextTitle = GalleryBrowser2::GetItemText(*pObj, GalleryItemFlags::Title);
347 
348  mpTheme->SetPreviewBitmapExAndStrings(nId - 1, aBitmapEx, aSize, aItemTextTitle, aItemTextPath);
349  }
350  }
351 
352  if (!aBitmapEx.IsEmpty())
353  {
354  const Size aBitmapExSizePixel(aBitmapEx.GetSizePixel());
355  const Point aPos(
356  ((aSize.Width() - aBitmapExSizePixel.Width()) >> 1) + rRect.Left(),
357  ((aSize.Height() - aBitmapExSizePixel.Height()) >> 1) + rRect.Top());
358  OutputDevice* pDev = rUDEvt.GetRenderContext();
359 
360  if(aBitmapEx.IsTransparent())
361  {
362  // draw checkered background for full rectangle.
364  }
365 
366  pDev->DrawBitmapEx(aPos, aBitmapEx);
367  }
368 
369  SetItemText(nId, aItemTextTitle);
370 }
371 
373 {
374  bool bRet = ValueSet::MouseButtonDown(rMEvt);
375 
376  if (rMEvt.GetClicks() == 2)
378 
379  return bRet;
380 }
381 
383 {
384  bool bRet = ValueSet::Command(rCEvt);
385 
386  if (rCEvt.GetCommand() == CommandEventId::ContextMenu)
387  {
388  mpParent->ShowContextMenu(rCEvt);
389  }
390 
391  return bRet;
392 }
393 
395 {
396  if (!mpTheme || !mpParent->KeyInput(rKEvt))
397  return ValueSet::KeyInput(rKEvt);
398  return true;
399 }
400 
402 {
403  Select();
404  return mpParent->StartDrag();
405 }
406 
407 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
GraphicObject aGraphicObj
Definition: galctrl.hxx:73
Point TopLeft() const
virtual void SetDrawingArea(weld::DrawingArea *pDrawingArea) override
Definition: galctrl.cxx:61
long Width() const
long GetWidth() const
virtual void SetDrawingArea(weld::DrawingArea *pDrawingArea) override
bool Draw(OutputDevice *pOut, const Point &rPt, const Size &rSz, const GraphicAttr *pAttr=nullptr)
GraphicObject aGraphicObj
Definition: galctrl.hxx:41
virtual bool KeyInput(const KeyEvent &rKEvt) override
virtual void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &rRect) override
Definition: galctrl.cxx:114
long GetHeight() const
ErrCode ImportGraphic(Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat=GRFILTER_FORMAT_DONTKNOW, sal_uInt16 *pDeterminedFormat=nullptr, GraphicFilterImportFlags nImportFlags=GraphicFilterImportFlags::NONE)
virtual ~GalleryPreview() override
Definition: galctrl.cxx:57
void SetItemWidth(long nItemWidth)
virtual bool Command(const CommandEvent &rCEvt) override
Definition: galctrl.cxx:382
const tools::Rectangle & GetRect() const
bool StartAnimation(OutputDevice *pOut, const Point &rPt, const Size &rSz, long nExtraData=0, OutputDevice *pFirstFrameOutDev=nullptr)
bool StartDrag()
Definition: galbrws2.cxx:473
long Height() const
virtual css::uno::Reference< css::datatransfer::dnd::XDropTarget > get_drop_target()=0
static OUString GetItemText(const SgaObject &rObj, GalleryItemFlags nItemTextFlags)
Definition: galbrws2.cxx:1160
virtual bool MouseButtonDown(const MouseEvent &rMEvt) override
Definition: galctrl.cxx:131
sal_Int16 nId
sal_uInt16 GetCode() const
SAL_DLLPRIVATE void SetPreviewBitmapExAndStrings(sal_uInt32 nPos, const BitmapEx &rBitmapEx, const Size &rSize, const OUString &rTitle, const OUString &rPath)
Definition: galtheme.cxx:203
virtual ~GalleryIconView() override
Definition: galctrl.cxx:292
tools::Rectangle aPreviewRect
Definition: galctrl.hxx:74
WinBits const WB_VSCROLL
virtual void SetDrawingArea(weld::DrawingArea *pDrawingArea) override
Definition: galctrl.cxx:296
class UNLESS_MERGELIBS(AVMEDIA_DLLPUBLIC) MediaFloater final MediaFloater * getMediaFloater()
void SetStyle(WinBits nStyle)
SAL_DLLPRIVATE void GetPreviewBitmapExAndStrings(sal_uInt32 nPos, BitmapEx &rBitmapEx, Size &rSize, OUString &rTitle, OUString &rPath) const
Definition: galtheme.cxx:193
constexpr sal_uInt16 KEY_UP
Size const & GetOutputSizePixel() const
constexpr sal_uInt16 KEY_END
sal_uInt16 GetClicks() const
void SetBackground()
#define HID_GALLERY_WINDOW
Definition: helpids.h:32
SVX_DLLPRIVATE bool ImplGetGraphicCenterRect(const Graphic &rGraphic, tools::Rectangle &rResultRect) const
Definition: galctrl.cxx:254
#define WB_ITEMBORDER
virtual void set_help_id(const OString &rName)=0
void SetGraphic(const Graphic &rGraphic)
Definition: galctrl.hxx:85
void SetItemHeight(long nLineHeight)
long Top() const
#define S_THUMB
Definition: galobj.hxx:30
virtual OutputDevice & get_ref_device()=0
WinBits GetStyle() const
virtual bool KeyInput(const KeyEvent &rKEvt) override
Definition: galctrl.cxx:394
GalleryTheme * mpTheme
Definition: galctrl.hxx:95
constexpr sal_uInt16 KEY_DOWN
virtual bool MouseButtonDown(const MouseEvent &rMEvt) override
vcl::RenderContext * GetRenderContext() const
void Travel(GalleryBrowserTravel eTravel)
Definition: galbrws2.cxx:729
void EnableFullItemMode(bool bFullMode)
virtual bool Command(const CommandEvent &rCEvt) override
Definition: galctrl.cxx:138
constexpr sal_uInt16 KEY_HOME
std::unique_ptr< GalleryDragDrop > mxDragDropTargetHelper
Definition: galctrl.hxx:92
SfxBindings & GetBindings()
CommandEventId GetCommand() const
#define GALLERY_BG_COLOR
Definition: galmisc.hxx:76
void SetItemText(sal_uInt16 nItemId, const OUString &rStr)
static bool isMediaURL(const OUString &rURL, const OUString &rReferer, bool bDeep=false, Size *pPreferredSizePixel=nullptr)
void ShowContextMenu(const CommandEvent &rCEvt)
Definition: galbrws2.cxx:492
void SetOutputSizePixel(const Size &rSize)
Size GetSize() const
sal_uInt16 GetItemId() const
constexpr sal_uInt16 KEY_RIGHT
GalleryIconView(GalleryBrowser2 *pParent, std::unique_ptr< weld::ScrolledWindow > xScrolledWindow)
Definition: galctrl.cxx:285
Point LogicToPixel(const Point &rLogicPt) const
Size GetPrefSize() const
std::unique_ptr< weld::ScrolledWindow > mxScrolledWindow
Definition: galctrl.hxx:40
RegionData_Impl * mpParent
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
virtual bool KeyInput(const KeyEvent &rKEvt) override
Definition: galctrl.cxx:148
WinBits const WB_3DLOOK
void TogglePreview()
Definition: galbrws2.cxx:486
const vcl::KeyCode & GetKeyCode() const
weld::DrawingArea * GetDrawingArea() const
static SfxViewFrame * Current()
static void PreviewMedia(const INetURLObject &rURL)
Definition: galctrl.cxx:198
void SetExtraSpacing(sal_uInt16 nNewSpacing)
bool ImplGetGraphicCenterRect(const Graphic &rGraphic, tools::Rectangle &rResultRect) const
Definition: galctrl.cxx:109
static void drawTransparenceBackground(vcl::RenderContext &rOut, const Point &rPos, const Size &rSize)
Definition: galctrl.cxx:275
const Graphic & GetGraphic() const
MapMode GetPrefMapMode() const
WinBits const WB_BORDER
virtual void Hide() override
Definition: galctrl.cxx:51
GalleryBrowser2 * mpParent
Definition: galctrl.hxx:43
virtual void Show() override
Definition: galctrl.cxx:45
INetProtocol GetProtocol() const
virtual void Select()
GalleryTheme * mpTheme
Definition: galctrl.hxx:44
tools::Rectangle aPreviewRect
Definition: galctrl.hxx:42
virtual bool StartDrag() override
Definition: galctrl.cxx:191
constexpr::Color COL_WHITE(0xFF, 0xFF, 0xFF)
long Left() const
constexpr sal_uInt16 KEY_BACKSPACE
void DrawCheckered(const Point &rPos, const Size &rSize, sal_uInt32 nLen=8, Color aStart=COL_WHITE, Color aEnd=COL_BLACK)
WinBits const WB_TABSTOP
bool KeyInput(const KeyEvent &rEvt)
Definition: galbrws2.cxx:524
SfxDispatcher * GetDispatcher() const
static GraphicFilter & GetGraphicFilter()
#define WB_DOUBLEBORDER
virtual bool StartDrag() override
Definition: galctrl.cxx:401
std::unique_ptr< GalleryDragDrop > mxDragDropTargetHelper
Definition: galctrl.hxx:39
GalleryBrowser2 * mpParent
Definition: galctrl.hxx:94
GalleryPreview(GalleryBrowser2 *pParent, std::unique_ptr< weld::ScrolledWindow > xScrolledWindow)
Definition: galctrl.cxx:38
constexpr sal_uInt16 KEY_LEFT
const SfxPoolItem * Execute(sal_uInt16 nSlot, SfxCallMode nCall=SfxCallMode::SLOT, const SfxPoolItem **pArgs=nullptr, sal_uInt16 nModi=0, const SfxPoolItem **pInternalArgs=nullptr)
bool IsAnimated() const
#define WB_FLATVALUESET
virtual void set_size_request(int nWidth, int nHeight)=0
virtual bool MouseButtonDown(const MouseEvent &rMEvt) override
Definition: galctrl.cxx:372
virtual SVX_DLLPRIVATE void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &rRect) override
Definition: galctrl.cxx:259
virtual void UserDraw(const UserDrawEvent &rUDEvt) override
Definition: galctrl.cxx:311
virtual void SetDrawingArea(weld::DrawingArea *pDrawingArea) override
Definition: galctrl.cxx:223
std::unique_ptr< SgaObject > AcquireObject(sal_uInt32 nPos)
Definition: galtheme.cxx:188
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo