tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / svx / source / gallery2 / galctrl.cxx
blob4e33db6d826726f4831d9a3d058e5b1e84082229
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 <galbrws1.hxx>
27 #include <svx/galtheme.hxx>
28 #include <svx/galmisc.hxx>
29 #include <svx/galctrl.hxx>
30 #include <galobj.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))
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 mxDragDropTargetHelper.reset(new GalleryDragDrop(mpParent, pDrawingArea->get_drop_target()));
71 namespace
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()));
77 bool bRet = false;
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() );
90 else
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 );
100 bRet = true;
103 return bRet;
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);
124 else
125 aGraphicObj.Draw(rRenderContext, aPos, aSize);
129 bool GalleryPreview::MouseButtonDown(const MouseEvent& rMEvt)
131 if (mpTheme && (rMEvt.GetClicks() == 2))
132 mpParent->TogglePreview();
133 return true;
136 bool GalleryPreview::Command(const CommandEvent& rCEvt)
138 if (mpTheme && (rCEvt.GetCommand() == CommandEventId::ContextMenu))
140 mpParent->ShowContextMenu(rCEvt);
141 return true;
143 return false;
146 bool GalleryPreview::KeyInput(const KeyEvent& rKEvt)
148 if(mpTheme)
150 GalleryBrowser1* pBrowser = mpParent;
152 switch( rKEvt.GetKeyCode().GetCode() )
154 case KEY_BACKSPACE:
155 pBrowser->TogglePreview();
156 break;
158 case KEY_HOME:
159 pBrowser->Travel( GalleryBrowserTravel::First );
160 break;
162 case KEY_END:
163 pBrowser->Travel( GalleryBrowserTravel::Last );
164 break;
166 case KEY_LEFT:
167 case KEY_UP:
168 pBrowser->Travel( GalleryBrowserTravel::Previous );
169 break;
171 case KEY_RIGHT:
172 case KEY_DOWN:
173 pBrowser->Travel( GalleryBrowserTravel::Next );
174 break;
176 default:
178 if (!pBrowser->KeyInput(rKEvt))
179 return false;
181 break;
184 return true;
186 return false;
189 bool GalleryPreview::StartDrag()
191 if (mpTheme)
192 return mpParent->StartDrag();
193 return true;
196 void GalleryPreview::PreviewMedia( const INetURLObject& rURL )
198 #if HAVE_FEATURE_AVMEDIA
199 if (rURL.GetProtocol() == INetProtocol::NotValid)
200 return;
202 ::avmedia::MediaFloater* pFloater = avmedia::getMediaFloater();
204 if (!pFloater)
206 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
207 pViewFrm->GetBindings().GetDispatcher()->Execute( SID_AVMEDIA_PLAYER, SfxCallMode::SYNCHRON );
208 pFloater = avmedia::getMediaFloater();
211 if (pFloater)
212 pFloater->setURL( rURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ), u""_ustr, true );
213 #else
214 (void) rURL;
215 #endif
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 )
231 bool bRet = true;
232 Graphic aGraphic;
233 #if HAVE_FEATURE_AVMEDIA
234 if( ::avmedia::MediaWindow::isMediaURL( _aURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ), u""_ustr ) )
236 aGraphic = BitmapEx(RID_SVXBMP_GALLERY_MEDIA);
238 else
239 #endif
241 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
242 GalleryProgress aProgress( &rFilter );
243 if( rFilter.ImportGraphic( aGraphic, _aURL ) )
244 bRet = false;
247 SetGraphic( aGraphic );
248 Invalidate();
249 return bRet;
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);
268 else
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))
285 , mpParent(pParent)
286 , mpTheme(nullptr)
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)
313 return;
315 const tools::Rectangle& rRect = rUDEvt.GetRect();
316 const Size aSize(rRect.GetWidth(), rRect.GetHeight());
317 BitmapEx aBitmapEx;
318 Size aPreparedSize;
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;
336 if (bNeedToCreate)
338 std::unique_ptr<SgaObject> pObj = mpTheme->AcquireObject(nId - 1);
340 if(pObj)
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());
352 const Point aPos(
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();
376 return bRet;
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);
388 return bRet;
391 bool GalleryIconView::KeyInput(const KeyEvent& rKEvt)
393 if (!mpTheme || !mpParent->KeyInput(rKEvt))
394 return ValueSet::KeyInput(rKEvt);
395 return true;
398 bool GalleryIconView::StartDrag()
400 Select();
401 return mpParent->StartDrag();
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */