bump product version to 7.2.5.1
[LibreOffice.git] / svx / source / gallery2 / galctrl.cxx
blobbd17df54b73719ea9f4638209a4805d805ecab92
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/.
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 <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>
38 GalleryPreview::GalleryPreview(GalleryBrowser2* pParent, std::unique_ptr<weld::ScrolledWindow> xScrolledWindow)
39 : mxScrolledWindow(std::move(xScrolledWindow))
40 , mpParent(pParent)
41 , mpTheme(nullptr)
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()));
73 namespace
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()));
79 bool bRet = false;
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() );
92 else
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 );
102 bRet = true;
105 return bRet;
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);
126 else
127 aGraphicObj.Draw(rRenderContext, aPos, aSize);
131 bool GalleryPreview::MouseButtonDown(const MouseEvent& rMEvt)
133 if (mpTheme && (rMEvt.GetClicks() == 2))
134 mpParent->TogglePreview();
135 return true;
138 bool GalleryPreview::Command(const CommandEvent& rCEvt)
140 if (mpTheme && (rCEvt.GetCommand() == CommandEventId::ContextMenu))
142 mpParent->ShowContextMenu(rCEvt);
143 return true;
145 return false;
148 bool GalleryPreview::KeyInput(const KeyEvent& rKEvt)
150 if(mpTheme)
152 GalleryBrowser2* pBrowser = mpParent;
154 switch( rKEvt.GetKeyCode().GetCode() )
156 case KEY_BACKSPACE:
157 pBrowser->TogglePreview();
158 break;
160 case KEY_HOME:
161 pBrowser->Travel( GalleryBrowserTravel::First );
162 break;
164 case KEY_END:
165 pBrowser->Travel( GalleryBrowserTravel::Last );
166 break;
168 case KEY_LEFT:
169 case KEY_UP:
170 pBrowser->Travel( GalleryBrowserTravel::Previous );
171 break;
173 case KEY_RIGHT:
174 case KEY_DOWN:
175 pBrowser->Travel( GalleryBrowserTravel::Next );
176 break;
178 default:
180 if (!pBrowser->KeyInput(rKEvt))
181 return false;
183 break;
186 return true;
188 return false;
191 bool GalleryPreview::StartDrag()
193 if (mpTheme)
194 return mpParent->StartDrag();
195 return true;
198 void GalleryPreview::PreviewMedia( const INetURLObject& rURL )
200 #if HAVE_FEATURE_AVMEDIA
201 if (rURL.GetProtocol() == INetProtocol::NotValid)
202 return;
204 ::avmedia::MediaFloater* pFloater = avmedia::getMediaFloater();
206 if (!pFloater)
208 SfxViewFrame::Current()->GetBindings().GetDispatcher()->Execute( SID_AVMEDIA_PLAYER, SfxCallMode::SYNCHRON );
209 pFloater = avmedia::getMediaFloater();
212 if (pFloater)
213 pFloater->setURL( rURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ), "", true );
214 #else
215 (void) rURL;
216 #endif
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 )
233 bool bRet = true;
234 Graphic aGraphic;
235 #if HAVE_FEATURE_AVMEDIA
236 if( ::avmedia::MediaWindow::isMediaURL( _aURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ), "" ) )
238 aGraphic = BitmapEx(RID_SVXBMP_GALLERY_MEDIA);
240 else
241 #endif
243 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
244 GalleryProgress aProgress( &rFilter );
245 if( rFilter.ImportGraphic( aGraphic, _aURL ) )
246 bRet = false;
249 SetGraphic( aGraphic );
250 Invalidate();
251 return bRet;
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);
270 else
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))
287 , mpParent(pParent)
288 , mpTheme(nullptr)
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)
316 return;
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;
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;
339 if (bNeedToCreate)
341 std::unique_ptr<SgaObject> pObj = mpTheme->AcquireObject(nId - 1);
343 if(pObj)
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());
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();
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();
379 return bRet;
382 bool GalleryIconView::Command(const CommandEvent& rCEvt)
384 bool bRet = ValueSet::Command(rCEvt);
386 if (rCEvt.GetCommand() == CommandEventId::ContextMenu)
388 mpParent->ShowContextMenu(rCEvt);
391 return bRet;
394 bool GalleryIconView::KeyInput(const KeyEvent& rKEvt)
396 if (!mpTheme || !mpParent->KeyInput(rKEvt))
397 return ValueSet::KeyInput(rKEvt);
398 return true;
401 bool GalleryIconView::StartDrag()
403 Select();
404 return mpParent->StartDrag();
407 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */