LibreOffice Module sdext (master)  1
PresenterBitmapContainer.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
22 
23 #include <com/sun/star/drawing/XPresenterHelper.hpp>
24 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
25 #include <osl/diagnose.h>
26 
27 using namespace ::com::sun::star;
28 using namespace ::com::sun::star::uno;
29 using namespace ::std;
30 
31 namespace sdext { namespace presenter {
32 
33 //===== PresenterBitmapContainer ==============================================
34 
36  const OUString& rsConfigurationBase,
37  const std::shared_ptr<PresenterBitmapContainer>& rpParentContainer,
38  const css::uno::Reference<css::uno::XComponentContext>& rxComponentContext,
39  const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
40  const css::uno::Reference<css::drawing::XPresenterHelper>& rxPresenterHelper)
41  : mpParentContainer(rpParentContainer),
42  maIconContainer(),
43  mxCanvas(rxCanvas),
44  mxPresenterHelper(rxPresenterHelper)
45 {
46  Initialize(rxComponentContext);
47 
48  // Get access to the configuration.
49  PresenterConfigurationAccess aConfiguration (
50  rxComponentContext,
51  "org.openoffice.Office.PresenterScreen",
54  aConfiguration.GetConfigurationNode(rsConfigurationBase),
55  UNO_QUERY_THROW);
56 
57  LoadBitmaps(xBitmapList);
58 }
59 
61  const css::uno::Reference<css::container::XNameAccess>& rxRootNode,
62  const std::shared_ptr<PresenterBitmapContainer>& rpParentContainer,
63  const css::uno::Reference<css::uno::XComponentContext>& rxComponentContext,
64  const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
65  const css::uno::Reference<css::drawing::XPresenterHelper>& rxPresenterHelper)
66  : mpParentContainer(rpParentContainer),
67  maIconContainer(),
68  mxCanvas(rxCanvas),
69  mxPresenterHelper(rxPresenterHelper)
70 {
71  Initialize(rxComponentContext);
72 
73  LoadBitmaps(rxRootNode);
74 }
75 
77  const css::uno::Reference<css::uno::XComponentContext>& rxComponentContext)
78 {
79  if ( mxPresenterHelper.is())
80  return;
81 
82  // Create an object that is able to load the bitmaps in a format that is
83  // supported by the canvas.
84  Reference<lang::XMultiComponentFactory> xFactory =
85  rxComponentContext->getServiceManager();
86  if ( ! xFactory.is())
87  return;
89  xFactory->createInstanceWithContext(
90  "com.sun.star.drawing.PresenterHelper",
91  rxComponentContext),
92  UNO_QUERY_THROW);
93 }
94 
96 {
97  maIconContainer.clear();
98 }
99 
100 std::shared_ptr<PresenterBitmapContainer::BitmapDescriptor> PresenterBitmapContainer::GetBitmap (
101  const OUString& rsName) const
102 {
103  BitmapContainer::const_iterator iSet (maIconContainer.find(rsName));
104  if (iSet != maIconContainer.end())
105  return iSet->second;
106  else if (mpParentContainer != nullptr)
107  return mpParentContainer->GetBitmap(rsName);
108  else
109  return SharedBitmapDescriptor();
110 }
111 
113  const css::uno::Reference<css::container::XNameAccess>& rxBitmapList)
114 {
115  if ( ! mxCanvas.is())
116  return;
117 
118  if ( ! rxBitmapList.is())
119  return;
120 
121  try
122  {
123  // Load all button bitmaps.
124  if (rxBitmapList.is())
125  {
127  rxBitmapList,
128  [this](OUString const& rKey, Reference<beans::XPropertySet> const& xProps)
129  {
130  this->ProcessBitmap(rKey, xProps);
131  });
132  }
133  }
134  catch (Exception&)
135  {
136  OSL_ASSERT(false);
137  }
138 }
139 
140 std::shared_ptr<PresenterBitmapContainer::BitmapDescriptor> PresenterBitmapContainer::LoadBitmap (
141  const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
142  const OUString& rsPath,
143  const css::uno::Reference<css::drawing::XPresenterHelper>& rxPresenterHelper,
144  const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
145  const std::shared_ptr<BitmapDescriptor>& rpDefault)
146 {
147  SharedBitmapDescriptor pBitmap;
148 
149  if (rxNode.is())
150  {
151  try
152  {
153  Reference<beans::XPropertySet> xBitmapProperties (
155  UNO_QUERY);
156  if (xBitmapProperties.is())
157  pBitmap = LoadBitmap(
158  xBitmapProperties,
159  rxPresenterHelper,
160  rxCanvas,
161  rpDefault);
162  }
163  catch (Exception&)
164  {
165  OSL_ASSERT(false);
166  }
167  }
168 
169  return pBitmap;
170 }
171 
173  const OUString& rsKey,
174  const Reference<beans::XPropertySet>& rxProperties)
175 {
176  OUString sName;
177  if ( ! (PresenterConfigurationAccess::GetProperty(rxProperties, "Name") >>= sName))
178  sName = rsKey;
179 
180  maIconContainer[sName] = LoadBitmap(
181  rxProperties,
183  mxCanvas,
185 }
186 
187 std::shared_ptr<PresenterBitmapContainer::BitmapDescriptor> PresenterBitmapContainer::LoadBitmap (
188  const Reference<beans::XPropertySet>& rxProperties,
189  const css::uno::Reference<css::drawing::XPresenterHelper>& rxPresenterHelper,
190  const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
191  const std::shared_ptr<BitmapDescriptor>& rpDefault)
192 {
193  OSL_ASSERT(rxCanvas.is());
194  OSL_ASSERT(rxPresenterHelper.is());
195 
196  SharedBitmapDescriptor pBitmap (new BitmapDescriptor(rpDefault));
197 
198  if ( ! rxProperties.is())
199  return pBitmap;
200 
201  OUString sFileName;
202 
203  // Load bitmaps.
204  if (PresenterConfigurationAccess::GetProperty(rxProperties, "NormalFileName") >>= sFileName)
205  try
206  {
207  pBitmap->SetBitmap(
209  rxPresenterHelper->loadBitmap(sFileName, rxCanvas));
210  }
211  catch (Exception&)
212  {}
213  if (PresenterConfigurationAccess::GetProperty(rxProperties, "MouseOverFileName") >>= sFileName)
214  try
215  {
216  pBitmap->SetBitmap(
218  rxPresenterHelper->loadBitmap(sFileName, rxCanvas));
219  }
220  catch (Exception&)
221  {}
222  if (PresenterConfigurationAccess::GetProperty(rxProperties, "ButtonDownFileName") >>= sFileName)
223  try
224  {
225  pBitmap->SetBitmap(
227  rxPresenterHelper->loadBitmap(sFileName, rxCanvas));
228  }
229  catch (Exception&)
230  {}
231  if (PresenterConfigurationAccess::GetProperty(rxProperties, "DisabledFileName") >>= sFileName)
232  try
233  {
234  pBitmap->SetBitmap(
236  rxPresenterHelper->loadBitmap(sFileName, rxCanvas));
237  }
238  catch (Exception&)
239  {}
240  if (PresenterConfigurationAccess::GetProperty(rxProperties, "MaskFileName") >>= sFileName)
241  try
242  {
243  pBitmap->SetBitmap(
245  rxPresenterHelper->loadBitmap(sFileName, rxCanvas));
246  }
247  catch (Exception&)
248  {}
249 
250  PresenterConfigurationAccess::GetProperty(rxProperties, "XOffset") >>= pBitmap->mnXOffset;
251  PresenterConfigurationAccess::GetProperty(rxProperties, "YOffset") >>= pBitmap->mnYOffset;
252 
253  PresenterConfigurationAccess::GetProperty(rxProperties, "XHotSpot") >>= pBitmap->mnXHotSpot;
254  PresenterConfigurationAccess::GetProperty(rxProperties, "YHotSpot") >>= pBitmap->mnYHotSpot;
255 
256  PresenterConfigurationAccess::GetProperty(rxProperties, "ReplacementColor") >>= pBitmap->maReplacementColor;
257 
258  OUString sTexturingMode;
259  if (PresenterConfigurationAccess::GetProperty(rxProperties, "HorizontalTexturingMode") >>= sTexturingMode)
260  pBitmap->meHorizontalTexturingMode = StringToTexturingMode(sTexturingMode);
261  if (PresenterConfigurationAccess::GetProperty(rxProperties, "VerticalTexturingMode") >>= sTexturingMode)
262  pBitmap->meVerticalTexturingMode = StringToTexturingMode(sTexturingMode);
263 
264  return pBitmap;
265 }
266 
268  PresenterBitmapContainer::StringToTexturingMode (const OUString& rsTexturingMode)
269 {
270  if (rsTexturingMode == "Once")
272  else if (rsTexturingMode == "Repeat")
274  else if (rsTexturingMode == "Stretch")
276  else
278 }
279 
280 //===== PresenterBitmapContainer::BitmapSet ===================================
281 
283  : mnWidth(0),
284  mnHeight(0),
285  mnXOffset(0),
286  mnYOffset(0),
287  mnXHotSpot(0),
288  mnYHotSpot(0),
289  maReplacementColor(0x00000000),
290  meHorizontalTexturingMode(Once),
291  meVerticalTexturingMode(Once),
292  mxNormalBitmap(),
293  mxMouseOverBitmap(),
294  mxButtonDownBitmap(),
295  mxDisabledBitmap(),
296  mxMaskBitmap()
297 {
298 }
299 
301  const std::shared_ptr<PresenterBitmapContainer::BitmapDescriptor>& rpDefault)
302  : mnWidth(0),
303  mnHeight(0),
304  mnXOffset(0),
305  mnYOffset(0),
306  mnXHotSpot(0),
307  mnYHotSpot(0),
308  maReplacementColor(0x00000000),
309  meHorizontalTexturingMode(Once),
310  meVerticalTexturingMode(Once),
311  mxNormalBitmap(),
312  mxMouseOverBitmap(),
313  mxButtonDownBitmap(),
314  mxDisabledBitmap(),
315  mxMaskBitmap()
316 {
317  if (rpDefault == nullptr)
318  return;
319 
320  mnWidth = rpDefault->mnWidth;
321  mnHeight = rpDefault->mnHeight;
322  mnXOffset = rpDefault->mnXOffset;
323  mnYOffset = rpDefault->mnYOffset;
324  mnXHotSpot = rpDefault->mnXHotSpot;
325  mnYHotSpot = rpDefault->mnYHotSpot;
326  maReplacementColor = rpDefault->maReplacementColor;
327  meHorizontalTexturingMode = rpDefault->meHorizontalTexturingMode;
328  meVerticalTexturingMode = rpDefault->meVerticalTexturingMode;
329  mxNormalBitmap = rpDefault->mxNormalBitmap;
330  mxMouseOverBitmap = rpDefault->mxMouseOverBitmap;
331  mxButtonDownBitmap = rpDefault->mxButtonDownBitmap;
332  mxDisabledBitmap = rpDefault->mxDisabledBitmap;
333  mxMaskBitmap = rpDefault->mxMaskBitmap;
334 }
335 
336 const css::uno::Reference<css::rendering::XBitmap>&
338 {
339  return mxNormalBitmap;
340 }
341 
342 css::uno::Reference<css::rendering::XBitmap> const &
344 {
345  switch (eMode)
346  {
347  case Normal:
348  default:
349  return mxNormalBitmap;
350 
351  case MouseOver:
352  if (mxMouseOverBitmap.is())
353  return mxMouseOverBitmap;
354  else
355  return mxNormalBitmap;
356 
357  case ButtonDown:
358  if (mxButtonDownBitmap.is())
359  return mxButtonDownBitmap;
360  else
361  return mxNormalBitmap;
362 
363  case Disabled:
364  if (mxDisabledBitmap.is())
365  return mxDisabledBitmap;
366  else
367  return mxNormalBitmap;
368 
369  case Mask:
370  return mxMaskBitmap;
371  }
372 }
373 
375  const Mode eMode,
376  const css::uno::Reference<css::rendering::XBitmap>& rxBitmap)
377 {
378  switch (eMode)
379  {
380  case Normal:
381  default:
382  mxNormalBitmap = rxBitmap;
383  if (mxNormalBitmap.is())
384  {
385  const geometry::IntegerSize2D aSize (mxNormalBitmap->getSize());
386  mnWidth = aSize.Width;
387  mnHeight = aSize.Height;
388  }
389  break;
390 
391  case MouseOver:
392  mxMouseOverBitmap = rxBitmap;
393  break;
394 
395  case ButtonDown:
396  mxButtonDownBitmap = rxBitmap;
397  break;
398 
399  case Disabled:
400  mxDisabledBitmap = rxBitmap;
401  break;
402 
403  case Mask:
404  mxMaskBitmap = rxBitmap;
405  break;
406  }
407 }
408 
409 } } // end of namespace ::sdext::presenter
410 
411 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void ForAll(const css::uno::Reference< css::container::XNameAccess > &rxContainer, const ::std::vector< OUString > &rArguments, const ItemProcessor &rProcessor)
Execute a functor for all elements of the given container.
Reference< rendering::XCanvas > mxCanvas
double mnHeight
void SetBitmap(const Mode eMode, const css::uno::Reference< css::rendering::XBitmap > &rxBitmap)
static css::uno::Any GetProperty(const css::uno::Reference< css::beans::XPropertySet > &rxProperties, const OUString &rsKey)
This method wraps a call to getPropertyValue() and returns an empty Any instead of throwing an except...
css::uno::Reference< css::rendering::XBitmap > mxNormalBitmap
void ProcessBitmap(const OUString &rsKey, const css::uno::Reference< css::beans::XPropertySet > &rProperties)
std::shared_ptr< PresenterBitmapContainer::BitmapDescriptor > SharedBitmapDescriptor
Reference< drawing::XPresenterHelper > mxPresenterHelper
This class gives access to the configuration.
css::uno::Reference< css::rendering::XBitmap > mxButtonDownBitmap
css::uno::Reference< css::rendering::XBitmap > mxMaskBitmap
if(nullptr==pCandidateA||nullptr==pCandidateB)
const char * sName
void Initialize(const css::uno::Reference< css::uno::XComponentContext > &rxComponentContext)
css::uno::Reference< css::rendering::XCanvas > mxCanvas
css::uno::Reference< css::rendering::XBitmap > mxMouseOverBitmap
css::uno::Any GetConfigurationNode(const OUString &rsPathToNode)
Return a configuration node below the root of the called object.
std::shared_ptr< BitmapDescriptor > GetBitmap(const OUString &rsName) const
Return the bitmap set that is associated with the given name.
css::uno::Reference< css::rendering::XBitmap > const & GetBitmap(const Mode eMode) const
css::uno::Reference< css::drawing::XPresenterHelper > mxPresenterHelper
static std::shared_ptr< BitmapDescriptor > LoadBitmap(const css::uno::Reference< css::container::XHierarchicalNameAccess > &rxNode, const OUString &rsPathToBitmapNode, const css::uno::Reference< css::drawing::XPresenterHelper > &rxPresenterHelper, const css::uno::Reference< css::rendering::XCanvas > &rxCanvas, const std::shared_ptr< BitmapDescriptor > &rpDefaultBitmap)
PresenterBitmapContainer(const OUString &rsConfigurationBase, const std::shared_ptr< PresenterBitmapContainer > &rpParentContainer, const css::uno::Reference< css::uno::XComponentContext > &rxComponentContext, const css::uno::Reference< css::rendering::XCanvas > &rxCanvas, const css::uno::Reference< css::drawing::XPresenterHelper > &rxPresenterHelper=nullptr)
Create a new bitmap container from a section of the configuration.
double mnWidth
css::uno::Reference< css::rendering::XBitmap > mxDisabledBitmap
const css::uno::Reference< css::rendering::XBitmap > & GetNormalBitmap() const
Reference< XSingleServiceFactory > xFactory
std::shared_ptr< PresenterBitmapContainer > mpParentContainer
void LoadBitmaps(const css::uno::Reference< css::container::XNameAccess > &rsRootNode)
static BitmapDescriptor::TexturingMode StringToTexturingMode(const OUString &rsTexturingMode)