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>
27 #include <galbrws2.hxx>
28 #include <svx/galtheme.hxx>
29 #include <svx/galmisc.hxx>
30 #include <svx/galctrl.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>
38 GalleryPreview::GalleryPreview(GalleryBrowser2
* 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 pDrawingArea
->set_help_id(HID_GALLERY_WINDOW
);
70 mxDragDropTargetHelper
.reset(new GalleryDragDrop(mpParent
, pDrawingArea
->get_drop_target()));
75 bool ImplGetGraphicCenterRect(const weld::CustomWidgetController
& rWidget
, const Graphic
& rGraphic
, tools::Rectangle
& rResultRect
)
77 const Size
aWinSize(rWidget
.GetOutputSizePixel());
78 Size
aNewSize(rWidget
.GetDrawingArea()->get_ref_device().LogicToPixel(rGraphic
.GetPrefSize(), rGraphic
.GetPrefMapMode()));
81 if( aNewSize
.Width() && aNewSize
.Height() )
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();
87 if ( fGrfWH
< fWinWH
)
89 aNewSize
.setWidth( static_cast<tools::Long
>( aWinSize
.Height() * fGrfWH
) );
90 aNewSize
.setHeight( aWinSize
.Height() );
94 aNewSize
.setWidth( aWinSize
.Width() );
95 aNewSize
.setHeight( static_cast<tools::Long
>( aWinSize
.Width() / fGrfWH
) );
98 const Point
aNewPos( ( aWinSize
.Width() - aNewSize
.Width() ) >> 1,
99 ( aWinSize
.Height() - aNewSize
.Height() ) >> 1 );
101 rResultRect
= tools::Rectangle( aNewPos
, aNewSize
);
109 bool GalleryPreview::ImplGetGraphicCenterRect( const Graphic
& rGraphic
, tools::Rectangle
& rResultRect
) const
111 return ::ImplGetGraphicCenterRect(*this, rGraphic
, rResultRect
);
114 void GalleryPreview::Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
& /*rRect*/)
116 rRenderContext
.SetBackground(Wallpaper(GALLERY_BG_COLOR
));
117 rRenderContext
.Erase();
119 if (ImplGetGraphicCenterRect(aGraphicObj
.GetGraphic(), aPreviewRect
))
121 const Point
aPos( aPreviewRect
.TopLeft() );
122 const Size
aSize( aPreviewRect
.GetSize() );
124 if( aGraphicObj
.IsAnimated() )
125 aGraphicObj
.StartAnimation(rRenderContext
, aPos
, aSize
);
127 aGraphicObj
.Draw(rRenderContext
, aPos
, aSize
);
131 bool GalleryPreview::MouseButtonDown(const MouseEvent
& rMEvt
)
133 if (mpTheme
&& (rMEvt
.GetClicks() == 2))
134 mpParent
->TogglePreview();
138 bool GalleryPreview::Command(const CommandEvent
& rCEvt
)
140 if (mpTheme
&& (rCEvt
.GetCommand() == CommandEventId::ContextMenu
))
142 mpParent
->ShowContextMenu(rCEvt
);
148 bool GalleryPreview::KeyInput(const KeyEvent
& rKEvt
)
152 GalleryBrowser2
* pBrowser
= mpParent
;
154 switch( rKEvt
.GetKeyCode().GetCode() )
157 pBrowser
->TogglePreview();
161 pBrowser
->Travel( GalleryBrowserTravel::First
);
165 pBrowser
->Travel( GalleryBrowserTravel::Last
);
170 pBrowser
->Travel( GalleryBrowserTravel::Previous
);
175 pBrowser
->Travel( GalleryBrowserTravel::Next
);
180 if (!pBrowser
->KeyInput(rKEvt
))
191 bool GalleryPreview::StartDrag()
194 return mpParent
->StartDrag();
198 void GalleryPreview::PreviewMedia( const INetURLObject
& rURL
)
200 #if HAVE_FEATURE_AVMEDIA
201 if (rURL
.GetProtocol() == INetProtocol::NotValid
)
204 ::avmedia::MediaFloater
* pFloater
= avmedia::getMediaFloater();
208 SfxViewFrame::Current()->GetBindings().GetDispatcher()->Execute( SID_AVMEDIA_PLAYER
, SfxCallMode::SYNCHRON
);
209 pFloater
= avmedia::getMediaFloater();
213 pFloater
->setURL( rURL
.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous
), "", true );
219 DialogGalleryPreview::DialogGalleryPreview()
223 void DialogGalleryPreview::SetDrawingArea(weld::DrawingArea
* pDrawingArea
)
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
);
231 bool DialogGalleryPreview::SetGraphic( const INetURLObject
& _aURL
)
235 #if HAVE_FEATURE_AVMEDIA
236 if( ::avmedia::MediaWindow::isMediaURL( _aURL
.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous
), "" ) )
238 aGraphic
= BitmapEx(RID_SVXBMP_GALLERY_MEDIA
);
243 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
244 GalleryProgress
aProgress( &rFilter
);
245 if( rFilter
.ImportGraphic( aGraphic
, _aURL
) )
249 SetGraphic( aGraphic
);
254 bool DialogGalleryPreview::ImplGetGraphicCenterRect( const Graphic
& rGraphic
, tools::Rectangle
& rResultRect
) const
256 return ::ImplGetGraphicCenterRect(*this, rGraphic
, rResultRect
);
259 void DialogGalleryPreview::Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
&)
261 rRenderContext
.SetBackground(Wallpaper(GALLERY_BG_COLOR
));
263 if (ImplGetGraphicCenterRect(aGraphicObj
.GetGraphic(), aPreviewRect
))
265 const Point
aPos( aPreviewRect
.TopLeft() );
266 const Size
aSize( aPreviewRect
.GetSize() );
268 if( aGraphicObj
.IsAnimated() )
269 aGraphicObj
.StartAnimation(rRenderContext
, aPos
, aSize
);
271 aGraphicObj
.Draw(rRenderContext
, aPos
, aSize
);
275 void GalleryIconView::drawTransparenceBackground(vcl::RenderContext
& rOut
, const Point
& rPos
, const Size
& rSize
)
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);
282 rOut
.DrawCheckered(rPos
, rSize
, nLen
, aW
, aG
);
285 GalleryIconView::GalleryIconView(GalleryBrowser2
* pParent
, std::unique_ptr
<weld::ScrolledWindow
> xScrolledWindow
)
286 : ValueSet(std::move(xScrolledWindow
))
292 GalleryIconView::~GalleryIconView()
296 void GalleryIconView::SetDrawingArea(weld::DrawingArea
* pDrawingArea
)
298 ValueSet::SetDrawingArea(pDrawingArea
);
300 SetStyle(GetStyle() | WB_TABSTOP
| WB_3DLOOK
| WB_BORDER
| WB_ITEMBORDER
| WB_DOUBLEBORDER
| WB_VSCROLL
| WB_FLATVALUESET
);
301 EnableFullItemMode( false );
303 SetHelpId( HID_GALLERY_WINDOW
);
304 SetExtraSpacing( 2 );
305 SetItemWidth( S_THUMB
+ 6 );
306 SetItemHeight( S_THUMB
+ 6 );
308 mxDragDropTargetHelper
.reset(new GalleryDragDrop(mpParent
, pDrawingArea
->get_drop_target()));
311 void GalleryIconView::UserDraw(const UserDrawEvent
& rUDEvt
)
313 const sal_uInt16 nId
= rUDEvt
.GetItemId();
315 if (!nId
|| !mpTheme
)
318 const tools::Rectangle
& rRect
= rUDEvt
.GetRect();
319 const Size
aSize(rRect
.GetWidth(), rRect
.GetHeight());
322 OUString aItemTextTitle
;
323 OUString aItemTextPath
;
325 mpTheme
->GetPreviewBitmapExAndStrings(nId
- 1, aBitmapEx
, aPreparedSize
, aItemTextTitle
, aItemTextPath
);
327 bool bNeedToCreate(aBitmapEx
.IsEmpty());
329 if (!bNeedToCreate
&& aItemTextTitle
.isEmpty())
331 bNeedToCreate
= true;
334 if (!bNeedToCreate
&& aPreparedSize
!= aSize
)
336 bNeedToCreate
= true;
341 std::unique_ptr
<SgaObject
> pObj
= mpTheme
->AcquireObject(nId
- 1);
345 aBitmapEx
= pObj
->createPreviewBitmapEx(aSize
);
346 aItemTextTitle
= GalleryBrowser2::GetItemText(*pObj
, GalleryItemFlags::Title
);
348 mpTheme
->SetPreviewBitmapExAndStrings(nId
- 1, aBitmapEx
, aSize
, aItemTextTitle
, aItemTextPath
);
352 if (!aBitmapEx
.IsEmpty())
354 const Size
aBitmapExSizePixel(aBitmapEx
.GetSizePixel());
356 ((aSize
.Width() - aBitmapExSizePixel
.Width()) >> 1) + rRect
.Left(),
357 ((aSize
.Height() - aBitmapExSizePixel
.Height()) >> 1) + rRect
.Top());
358 OutputDevice
* pDev
= rUDEvt
.GetRenderContext();
360 if(aBitmapEx
.IsAlpha())
362 // draw checkered background for full rectangle.
363 GalleryIconView::drawTransparenceBackground(*pDev
, rRect
.TopLeft(), rRect
.GetSize());
366 pDev
->DrawBitmapEx(aPos
, aBitmapEx
);
369 SetItemText(nId
, aItemTextTitle
);
372 bool GalleryIconView::MouseButtonDown(const MouseEvent
& rMEvt
)
374 bool bRet
= ValueSet::MouseButtonDown(rMEvt
);
376 if (rMEvt
.GetClicks() == 2)
377 mpParent
->TogglePreview();
382 bool GalleryIconView::Command(const CommandEvent
& rCEvt
)
384 bool bRet
= ValueSet::Command(rCEvt
);
386 if (rCEvt
.GetCommand() == CommandEventId::ContextMenu
)
388 mpParent
->ShowContextMenu(rCEvt
);
394 bool GalleryIconView::KeyInput(const KeyEvent
& rKEvt
)
396 if (!mpTheme
|| !mpParent
->KeyInput(rKEvt
))
397 return ValueSet::KeyInput(rKEvt
);
401 bool GalleryIconView::StartDrag()
404 return mpParent
->StartDrag();
407 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */