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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <config_features.h>
22 #include <sfx2/viewfrm.hxx>
23 #include <sfx2/dispatch.hxx>
24 #include <sfx2/sfxsids.hrc>
25 #include <avmedia/mediaplayer.hxx>
26 #include <galbrws1.hxx>
27 #include <svx/galtheme.hxx>
28 #include <svx/galmisc.hxx>
29 #include <svx/galctrl.hxx>
31 #include <avmedia/mediawindow.hxx>
32 #include <vcl/event.hxx>
33 #include <vcl/commandevent.hxx>
34 #include <vcl/graphicfilter.hxx>
35 #include <bitmaps.hlst>
36 #include <svl/itemset.hxx>
38 GalleryPreview::GalleryPreview(GalleryBrowser1
* pParent
, std::unique_ptr
<weld::ScrolledWindow
> xScrolledWindow
)
39 : mxScrolledWindow(std::move(xScrolledWindow
))
45 void GalleryPreview::Show()
47 mxScrolledWindow
->show();
48 weld::CustomWidgetController::Show();
51 void GalleryPreview::Hide()
53 weld::CustomWidgetController::Hide();
54 mxScrolledWindow
->hide();
57 GalleryPreview::~GalleryPreview()
61 void GalleryPreview::SetDrawingArea(weld::DrawingArea
* pDrawingArea
)
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
);
68 mxDragDropTargetHelper
.reset(new GalleryDragDrop(mpParent
, pDrawingArea
->get_drop_target()));
73 bool ImplGetGraphicCenterRect(const weld::CustomWidgetController
& rWidget
, const Graphic
& rGraphic
, tools::Rectangle
& rResultRect
)
75 const Size
aWinSize(rWidget
.GetOutputSizePixel());
76 Size
aNewSize(rWidget
.GetDrawingArea()->get_ref_device().LogicToPixel(rGraphic
.GetPrefSize(), rGraphic
.GetPrefMapMode()));
79 if( aNewSize
.Width() && aNewSize
.Height() )
81 // scale to fit window
82 const double fGrfWH
= static_cast<double>(aNewSize
.Width()) / aNewSize
.Height();
83 const double fWinWH
= static_cast<double>(aWinSize
.Width()) / aWinSize
.Height();
85 if ( fGrfWH
< fWinWH
)
87 aNewSize
.setWidth( static_cast<tools::Long
>( aWinSize
.Height() * fGrfWH
) );
88 aNewSize
.setHeight( aWinSize
.Height() );
92 aNewSize
.setWidth( aWinSize
.Width() );
93 aNewSize
.setHeight( static_cast<tools::Long
>( aWinSize
.Width() / fGrfWH
) );
96 const Point
aNewPos( ( aWinSize
.Width() - aNewSize
.Width() ) >> 1,
97 ( aWinSize
.Height() - aNewSize
.Height() ) >> 1 );
99 rResultRect
= tools::Rectangle( aNewPos
, aNewSize
);
107 bool GalleryPreview::ImplGetGraphicCenterRect( const Graphic
& rGraphic
, tools::Rectangle
& rResultRect
) const
109 return ::ImplGetGraphicCenterRect(*this, rGraphic
, rResultRect
);
112 void GalleryPreview::Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
& /*rRect*/)
114 rRenderContext
.SetBackground(Wallpaper(GALLERY_BG_COLOR
));
115 rRenderContext
.Erase();
117 if (ImplGetGraphicCenterRect(aGraphicObj
.GetGraphic(), aPreviewRect
))
119 const Point
aPos( aPreviewRect
.TopLeft() );
120 const Size
aSize( aPreviewRect
.GetSize() );
122 if( aGraphicObj
.IsAnimated() )
123 aGraphicObj
.StartAnimation(rRenderContext
, aPos
, aSize
);
125 aGraphicObj
.Draw(rRenderContext
, aPos
, aSize
);
129 bool GalleryPreview::MouseButtonDown(const MouseEvent
& rMEvt
)
131 if (mpTheme
&& (rMEvt
.GetClicks() == 2))
132 mpParent
->TogglePreview();
136 bool GalleryPreview::Command(const CommandEvent
& rCEvt
)
138 if (mpTheme
&& (rCEvt
.GetCommand() == CommandEventId::ContextMenu
))
140 mpParent
->ShowContextMenu(rCEvt
);
146 bool GalleryPreview::KeyInput(const KeyEvent
& rKEvt
)
150 GalleryBrowser1
* pBrowser
= mpParent
;
152 switch( rKEvt
.GetKeyCode().GetCode() )
155 pBrowser
->TogglePreview();
159 pBrowser
->Travel( GalleryBrowserTravel::First
);
163 pBrowser
->Travel( GalleryBrowserTravel::Last
);
168 pBrowser
->Travel( GalleryBrowserTravel::Previous
);
173 pBrowser
->Travel( GalleryBrowserTravel::Next
);
178 if (!pBrowser
->KeyInput(rKEvt
))
189 bool GalleryPreview::StartDrag()
192 return mpParent
->StartDrag();
196 void GalleryPreview::PreviewMedia( const INetURLObject
& rURL
)
198 #if HAVE_FEATURE_AVMEDIA
199 if (rURL
.GetProtocol() == INetProtocol::NotValid
)
202 ::avmedia::MediaFloater
* pFloater
= avmedia::getMediaFloater();
206 if (SfxViewFrame
* pViewFrm
= SfxViewFrame::Current())
207 pViewFrm
->GetBindings().GetDispatcher()->Execute( SID_AVMEDIA_PLAYER
, SfxCallMode::SYNCHRON
);
208 pFloater
= avmedia::getMediaFloater();
212 pFloater
->setURL( rURL
.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous
), u
""_ustr
, true );
218 DialogGalleryPreview::DialogGalleryPreview()
222 void DialogGalleryPreview::SetDrawingArea(weld::DrawingArea
* pDrawingArea
)
224 CustomWidgetController::SetDrawingArea(pDrawingArea
);
225 Size
aSize(pDrawingArea
->get_ref_device().LogicToPixel(Size(70, 88), MapMode(MapUnit::MapAppFont
)));
226 pDrawingArea
->set_size_request(aSize
.Width(), aSize
.Height());
229 bool DialogGalleryPreview::SetGraphic( const INetURLObject
& _aURL
)
233 #if HAVE_FEATURE_AVMEDIA
234 if( ::avmedia::MediaWindow::isMediaURL( _aURL
.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous
), u
""_ustr
) )
236 aGraphic
= BitmapEx(RID_SVXBMP_GALLERY_MEDIA
);
241 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
242 GalleryProgress
aProgress( &rFilter
);
243 if( rFilter
.ImportGraphic( aGraphic
, _aURL
) )
247 SetGraphic( aGraphic
);
252 bool DialogGalleryPreview::ImplGetGraphicCenterRect( const Graphic
& rGraphic
, tools::Rectangle
& rResultRect
) const
254 return ::ImplGetGraphicCenterRect(*this, rGraphic
, rResultRect
);
257 void DialogGalleryPreview::Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
&)
259 rRenderContext
.SetBackground(Wallpaper(GALLERY_BG_COLOR
));
261 if (ImplGetGraphicCenterRect(aGraphicObj
.GetGraphic(), aPreviewRect
))
263 const Point
aPos( aPreviewRect
.TopLeft() );
264 const Size
aSize( aPreviewRect
.GetSize() );
266 if( aGraphicObj
.IsAnimated() )
267 aGraphicObj
.StartAnimation(rRenderContext
, aPos
, aSize
);
269 aGraphicObj
.Draw(rRenderContext
, aPos
, aSize
);
273 void GalleryIconView::drawTransparenceBackground(vcl::RenderContext
& rOut
, const Point
& rPos
, const Size
& rSize
)
275 // draw checkered background
276 static const sal_uInt32
nLen(8);
277 static const Color
aW(COL_WHITE
);
278 static const Color
aG(0xef, 0xef, 0xef);
280 rOut
.DrawCheckered(rPos
, rSize
, nLen
, aW
, aG
);
283 GalleryIconView::GalleryIconView(GalleryBrowser1
* pParent
, std::unique_ptr
<weld::ScrolledWindow
> xScrolledWindow
)
284 : ValueSet(std::move(xScrolledWindow
))
290 GalleryIconView::~GalleryIconView()
294 void GalleryIconView::SetDrawingArea(weld::DrawingArea
* pDrawingArea
)
296 ValueSet::SetDrawingArea(pDrawingArea
);
298 SetStyle(GetStyle() | WB_TABSTOP
| WB_3DLOOK
| WB_BORDER
| WB_ITEMBORDER
| WB_DOUBLEBORDER
| WB_VSCROLL
| WB_FLATVALUESET
);
299 EnableFullItemMode( false );
301 SetExtraSpacing( 2 );
302 SetItemWidth( S_THUMB
+ 6 );
303 SetItemHeight( S_THUMB
+ 6 );
305 mxDragDropTargetHelper
.reset(new GalleryDragDrop(mpParent
, pDrawingArea
->get_drop_target()));
308 void GalleryIconView::UserDraw(const UserDrawEvent
& rUDEvt
)
310 const sal_uInt16 nId
= rUDEvt
.GetItemId();
312 if (!nId
|| !mpTheme
)
315 const tools::Rectangle
& rRect
= rUDEvt
.GetRect();
316 const Size
aSize(rRect
.GetWidth(), rRect
.GetHeight());
319 OUString aItemTextTitle
;
320 OUString aItemTextPath
;
322 mpTheme
->GetPreviewBitmapExAndStrings(nId
- 1, aBitmapEx
, aPreparedSize
, aItemTextTitle
, aItemTextPath
);
324 bool bNeedToCreate(aBitmapEx
.IsEmpty());
326 if (!bNeedToCreate
&& aItemTextTitle
.isEmpty())
328 bNeedToCreate
= true;
331 if (!bNeedToCreate
&& aPreparedSize
!= aSize
)
333 bNeedToCreate
= true;
338 std::unique_ptr
<SgaObject
> pObj
= mpTheme
->AcquireObject(nId
- 1);
342 aBitmapEx
= pObj
->createPreviewBitmapEx(aSize
);
343 aItemTextTitle
= GalleryBrowser1::GetItemText(*pObj
, GalleryItemFlags::Title
);
345 mpTheme
->SetPreviewBitmapExAndStrings(nId
- 1, aBitmapEx
, aSize
, aItemTextTitle
, aItemTextPath
);
349 if (!aBitmapEx
.IsEmpty())
351 const Size
aBitmapExSizePixel(aBitmapEx
.GetSizePixel());
353 ((aSize
.Width() - aBitmapExSizePixel
.Width()) >> 1) + rRect
.Left(),
354 ((aSize
.Height() - aBitmapExSizePixel
.Height()) >> 1) + rRect
.Top());
355 OutputDevice
* pDev
= rUDEvt
.GetRenderContext();
357 if(aBitmapEx
.IsAlpha())
359 // draw checkered background for full rectangle.
360 GalleryIconView::drawTransparenceBackground(*pDev
, rRect
.TopLeft(), rRect
.GetSize());
363 pDev
->DrawBitmapEx(aPos
, aBitmapEx
);
366 SetItemText(nId
, aItemTextTitle
);
369 bool GalleryIconView::MouseButtonDown(const MouseEvent
& rMEvt
)
371 bool bRet
= ValueSet::MouseButtonDown(rMEvt
);
373 if (rMEvt
.GetClicks() == 2)
374 mpParent
->TogglePreview();
379 bool GalleryIconView::Command(const CommandEvent
& rCEvt
)
381 bool bRet
= ValueSet::Command(rCEvt
);
383 if (!bRet
&& rCEvt
.GetCommand() == CommandEventId::ContextMenu
)
385 bRet
= mpParent
->ShowContextMenu(rCEvt
);
391 bool GalleryIconView::KeyInput(const KeyEvent
& rKEvt
)
393 if (!mpTheme
|| !mpParent
->KeyInput(rKEvt
))
394 return ValueSet::KeyInput(rKEvt
);
398 bool GalleryIconView::StartDrag()
401 return mpParent
->StartDrag();
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */