Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / gallery2 / galctrl.cxx
blob716a198ec87f5b4a107657485a45c28624fb966a
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 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
209 pViewFrm->GetBindings().GetDispatcher()->Execute( SID_AVMEDIA_PLAYER, SfxCallMode::SYNCHRON );
210 pFloater = avmedia::getMediaFloater();
213 if (pFloater)
214 pFloater->setURL( rURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ), "", true );
215 #else
216 (void) rURL;
217 #endif
220 DialogGalleryPreview::DialogGalleryPreview()
224 void DialogGalleryPreview::SetDrawingArea(weld::DrawingArea* pDrawingArea)
226 CustomWidgetController::SetDrawingArea(pDrawingArea);
227 Size aSize(pDrawingArea->get_ref_device().LogicToPixel(Size(70, 88), MapMode(MapUnit::MapAppFont)));
228 pDrawingArea->set_size_request(aSize.Width(), aSize.Height());
229 pDrawingArea->set_help_id(HID_GALLERY_WINDOW);
232 bool DialogGalleryPreview::SetGraphic( const INetURLObject& _aURL )
234 bool bRet = true;
235 Graphic aGraphic;
236 #if HAVE_FEATURE_AVMEDIA
237 if( ::avmedia::MediaWindow::isMediaURL( _aURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ), "" ) )
239 aGraphic = BitmapEx(RID_SVXBMP_GALLERY_MEDIA);
241 else
242 #endif
244 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
245 GalleryProgress aProgress( &rFilter );
246 if( rFilter.ImportGraphic( aGraphic, _aURL ) )
247 bRet = false;
250 SetGraphic( aGraphic );
251 Invalidate();
252 return bRet;
255 bool DialogGalleryPreview::ImplGetGraphicCenterRect( const Graphic& rGraphic, tools::Rectangle& rResultRect ) const
257 return ::ImplGetGraphicCenterRect(*this, rGraphic, rResultRect);
260 void DialogGalleryPreview::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&)
262 rRenderContext.SetBackground(Wallpaper(GALLERY_BG_COLOR));
264 if (ImplGetGraphicCenterRect(aGraphicObj.GetGraphic(), aPreviewRect))
266 const Point aPos( aPreviewRect.TopLeft() );
267 const Size aSize( aPreviewRect.GetSize() );
269 if( aGraphicObj.IsAnimated() )
270 aGraphicObj.StartAnimation(rRenderContext, aPos, aSize);
271 else
272 aGraphicObj.Draw(rRenderContext, aPos, aSize);
276 void GalleryIconView::drawTransparenceBackground(vcl::RenderContext& rOut, const Point& rPos, const Size& rSize)
278 // draw checkered background
279 static const sal_uInt32 nLen(8);
280 static const Color aW(COL_WHITE);
281 static const Color aG(0xef, 0xef, 0xef);
283 rOut.DrawCheckered(rPos, rSize, nLen, aW, aG);
286 GalleryIconView::GalleryIconView(GalleryBrowser2* pParent, std::unique_ptr<weld::ScrolledWindow> xScrolledWindow)
287 : ValueSet(std::move(xScrolledWindow))
288 , mpParent(pParent)
289 , mpTheme(nullptr)
293 GalleryIconView::~GalleryIconView()
297 void GalleryIconView::SetDrawingArea(weld::DrawingArea* pDrawingArea)
299 ValueSet::SetDrawingArea(pDrawingArea);
301 SetStyle(GetStyle() | WB_TABSTOP | WB_3DLOOK | WB_BORDER | WB_ITEMBORDER | WB_DOUBLEBORDER | WB_VSCROLL | WB_FLATVALUESET);
302 EnableFullItemMode( false );
304 SetHelpId( HID_GALLERY_WINDOW );
305 SetExtraSpacing( 2 );
306 SetItemWidth( S_THUMB + 6 );
307 SetItemHeight( S_THUMB + 6 );
309 mxDragDropTargetHelper.reset(new GalleryDragDrop(mpParent, pDrawingArea->get_drop_target()));
312 void GalleryIconView::UserDraw(const UserDrawEvent& rUDEvt)
314 const sal_uInt16 nId = rUDEvt.GetItemId();
316 if (!nId || !mpTheme)
317 return;
319 const tools::Rectangle& rRect = rUDEvt.GetRect();
320 const Size aSize(rRect.GetWidth(), rRect.GetHeight());
321 BitmapEx aBitmapEx;
322 Size aPreparedSize;
323 OUString aItemTextTitle;
324 OUString aItemTextPath;
326 mpTheme->GetPreviewBitmapExAndStrings(nId - 1, aBitmapEx, aPreparedSize, aItemTextTitle, aItemTextPath);
328 bool bNeedToCreate(aBitmapEx.IsEmpty());
330 if (!bNeedToCreate && aItemTextTitle.isEmpty())
332 bNeedToCreate = true;
335 if (!bNeedToCreate && aPreparedSize != aSize)
337 bNeedToCreate = true;
340 if (bNeedToCreate)
342 std::unique_ptr<SgaObject> pObj = mpTheme->AcquireObject(nId - 1);
344 if(pObj)
346 aBitmapEx = pObj->createPreviewBitmapEx(aSize);
347 aItemTextTitle = GalleryBrowser2::GetItemText(*pObj, GalleryItemFlags::Title);
349 mpTheme->SetPreviewBitmapExAndStrings(nId - 1, aBitmapEx, aSize, aItemTextTitle, aItemTextPath);
353 if (!aBitmapEx.IsEmpty())
355 const Size aBitmapExSizePixel(aBitmapEx.GetSizePixel());
356 const Point aPos(
357 ((aSize.Width() - aBitmapExSizePixel.Width()) >> 1) + rRect.Left(),
358 ((aSize.Height() - aBitmapExSizePixel.Height()) >> 1) + rRect.Top());
359 OutputDevice* pDev = rUDEvt.GetRenderContext();
361 if(aBitmapEx.IsAlpha())
363 // draw checkered background for full rectangle.
364 GalleryIconView::drawTransparenceBackground(*pDev, rRect.TopLeft(), rRect.GetSize());
367 pDev->DrawBitmapEx(aPos, aBitmapEx);
370 SetItemText(nId, aItemTextTitle);
373 bool GalleryIconView::MouseButtonDown(const MouseEvent& rMEvt)
375 bool bRet = ValueSet::MouseButtonDown(rMEvt);
377 if (rMEvt.GetClicks() == 2)
378 mpParent->TogglePreview();
380 return bRet;
383 bool GalleryIconView::Command(const CommandEvent& rCEvt)
385 bool bRet = ValueSet::Command(rCEvt);
387 if (!bRet && rCEvt.GetCommand() == CommandEventId::ContextMenu)
389 bRet = mpParent->ShowContextMenu(rCEvt);
392 return bRet;
395 bool GalleryIconView::KeyInput(const KeyEvent& rKEvt)
397 if (!mpTheme || !mpParent->KeyInput(rKEvt))
398 return ValueSet::KeyInput(rKEvt);
399 return true;
402 bool GalleryIconView::StartDrag()
404 Select();
405 return mpParent->StartDrag();
408 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */