1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sdext.hxx"
26 
27 #include "PresenterBitmapContainer.hxx"
28 #include "PresenterComponent.hxx"
29 #include "PresenterConfigurationAccess.hxx"
30 
31 #include <com/sun/star/deployment/XPackageInformationProvider.hpp>
32 #include <com/sun/star/drawing/XPresenterHelper.hpp>
33 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
34 #include <com/sun/star/rendering/CompositeOperation.hpp>
35 #include <com/sun/star/rendering/XIntegerBitmap.hpp>
36 #include <boost/bind.hpp>
37 #include <map>
38 
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::std;
42 using ::rtl::OUString;
43 
44 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
45 
46 
47 namespace sdext { namespace presenter {
48 
49 namespace {
50 static OUString gsNameProperty (A2S("Name"));
51 static OUString gsNormalFileNameProperty (A2S("NormalFileName"));
52 static OUString gsMouseOverFileNameProperty (A2S("MouseOverFileName"));
53 static OUString gsButtonDownFileNameProperty (A2S("ButtonDownFileName"));
54 static OUString gsDisabledFileNameProperty (A2S("DisabledFileName"));
55 static OUString gsMaskFileNameProperty (A2S("MaskFileName"));
56 static OUString gsXOffsetProperty (A2S("XOffset"));
57 static OUString gsYOffsetProperty (A2S("YOffset"));
58 static OUString gsXHotSpotProperty (A2S("XHotSpot"));
59 static OUString gsYHotSpotProperty (A2S("YHotSpot"));
60 static OUString gsReplacementColorProperty (A2S("ReplacementColor"));
61 static OUString gsHorizontalTexturingModeProperty (A2S("HorizontalTexturingMode"));
62 static OUString gsVerticalTexturingModeProperty (A2S("VerticalTexturingMode"));
63 }
64 
65 //===== PresenterBitmapContainer ==============================================
66 
67 PresenterBitmapContainer::PresenterBitmapContainer (
68     const ::rtl::OUString& rsConfigurationBase,
69     const ::boost::shared_ptr<PresenterBitmapContainer>& rpParentContainer,
70     const css::uno::Reference<css::uno::XComponentContext>& rxComponentContext,
71     const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
72     const OUString& rsBasePath,
73     const css::uno::Reference<css::drawing::XPresenterHelper>& rxPresenterHelper)
74     : mpParentContainer(rpParentContainer),
75       maIconContainer(),
76       msBasePath(rsBasePath),
77       mxCanvas(rxCanvas),
78       mxPresenterHelper(rxPresenterHelper)
79 {
80     Initialize(rxComponentContext);
81 
82     // Get access to the configuration.
83     PresenterConfigurationAccess aConfiguration (
84         rxComponentContext,
85         A2S("org.openoffice.Office.extension.PresenterScreen"),
86         PresenterConfigurationAccess::READ_ONLY);
87     Reference<container::XNameAccess> xBitmapList (
88         aConfiguration.GetConfigurationNode(rsConfigurationBase),
89         UNO_QUERY_THROW);
90 
91     LoadBitmaps(xBitmapList);
92 }
93 
94 
95 
96 
97 
98 PresenterBitmapContainer::PresenterBitmapContainer (
99     const css::uno::Reference<css::container::XNameAccess>& rxRootNode,
100     const ::boost::shared_ptr<PresenterBitmapContainer>& rpParentContainer,
101     const css::uno::Reference<css::uno::XComponentContext>& rxComponentContext,
102     const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
103     const OUString& rsBasePath,
104     const css::uno::Reference<css::drawing::XPresenterHelper>& rxPresenterHelper)
105     : mpParentContainer(rpParentContainer),
106       maIconContainer(),
107       msBasePath(rsBasePath),
108       mxCanvas(rxCanvas),
109       mxPresenterHelper(rxPresenterHelper)
110 {
111     Initialize(rxComponentContext);
112 
113     LoadBitmaps(rxRootNode);
114 }
115 
116 
117 
118 
119 void PresenterBitmapContainer::Initialize (
120     const css::uno::Reference<css::uno::XComponentContext>& rxComponentContext)
121 {
122     if ( ! mxPresenterHelper.is())
123     {
124         // Create an object that is able to load the bitmaps in a format that is
125         // supported by the canvas.
126         Reference<lang::XMultiComponentFactory> xFactory (
127             rxComponentContext->getServiceManager(), UNO_QUERY);
128         if ( ! xFactory.is())
129             return;
130         mxPresenterHelper = Reference<drawing::XPresenterHelper>(
131             xFactory->createInstanceWithContext(
132                 A2S("com.sun.star.drawing.PresenterHelper"),
133                 rxComponentContext),
134             UNO_QUERY_THROW);
135     }
136 }
137 
138 
139 
140 
141 PresenterBitmapContainer::~PresenterBitmapContainer (void)
142 {
143     maIconContainer.clear();
144 }
145 
146 
147 
148 
149 SharedBitmapDescriptor PresenterBitmapContainer::GetBitmap (
150     const OUString& rsName) const
151 {
152     BitmapContainer::const_iterator iSet (maIconContainer.find(rsName));
153     if (iSet != maIconContainer.end())
154         return iSet->second;
155     else if (mpParentContainer.get() != NULL)
156         return mpParentContainer->GetBitmap(rsName);
157     else
158         return SharedBitmapDescriptor();
159 }
160 
161 
162 
163 
164 void PresenterBitmapContainer::LoadBitmaps (
165     const css::uno::Reference<css::container::XNameAccess>& rxBitmapList)
166 {
167     if ( ! mxCanvas.is())
168         return;
169 
170     if ( ! rxBitmapList.is())
171         return;
172 
173     try
174     {
175         // Load all button bitmaps.
176         if (rxBitmapList.is())
177         {
178             PresenterConfigurationAccess::ForAll(
179                 rxBitmapList,
180                 ::boost::bind(&PresenterBitmapContainer::ProcessBitmap, this, _1, _2));
181         }
182     }
183     catch (Exception&)
184     {
185         OSL_ASSERT(false);
186     }
187 }
188 
189 
190 
191 
192 SharedBitmapDescriptor PresenterBitmapContainer::LoadBitmap (
193     const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
194     const ::rtl::OUString& rsPath,
195     const css::uno::Reference<css::drawing::XPresenterHelper>& rxPresenterHelper,
196     const OUString& rsBasePath,
197     const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
198     const SharedBitmapDescriptor& rpDefault)
199 {
200     SharedBitmapDescriptor pBitmap;
201 
202     if (rxNode.is())
203     {
204         try
205         {
206             Reference<beans::XPropertySet> xBitmapProperties (
207                 PresenterConfigurationAccess::GetConfigurationNode(rxNode, rsPath),
208                 UNO_QUERY);
209             if (xBitmapProperties.is())
210                 pBitmap = LoadBitmap(
211                     xBitmapProperties,
212                     rxPresenterHelper,
213                     rsBasePath,
214                     rxCanvas,
215                     rpDefault);
216         }
217         catch (Exception&)
218         {
219             OSL_ASSERT(false);
220         }
221     }
222 
223     return pBitmap;
224 }
225 
226 
227 
228 
229 void PresenterBitmapContainer::ProcessBitmap (
230     const OUString& rsKey,
231     const Reference<beans::XPropertySet>& rxProperties)
232 {
233     OUString sName;
234     if ( ! (PresenterConfigurationAccess::GetProperty(rxProperties, gsNameProperty) >>= sName))
235         sName = rsKey;
236 
237     maIconContainer[sName] = LoadBitmap(
238         rxProperties,
239         mxPresenterHelper,
240         msBasePath,
241         mxCanvas,
242         SharedBitmapDescriptor());
243 }
244 
245 
246 
247 
248 SharedBitmapDescriptor PresenterBitmapContainer::LoadBitmap (
249     const Reference<beans::XPropertySet>& rxProperties,
250     const css::uno::Reference<css::drawing::XPresenterHelper>& rxPresenterHelper,
251     const OUString& rsBasePath,
252     const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
253     const SharedBitmapDescriptor& rpDefault)
254 {
255     OSL_ASSERT(rxCanvas.is());
256     OSL_ASSERT(rxPresenterHelper.is());
257 
258     SharedBitmapDescriptor pBitmap (new BitmapDescriptor(rpDefault));
259 
260     if ( ! rxProperties.is())
261         return pBitmap;
262 
263     OUString sFileName;
264 
265     // Load bitmaps.
266     if (PresenterConfigurationAccess::GetProperty(rxProperties, gsNormalFileNameProperty) >>= sFileName)
267         try
268         {
269             pBitmap->SetBitmap(
270                 BitmapDescriptor::Normal,
271                 rxPresenterHelper->loadBitmap(rsBasePath + sFileName, rxCanvas));
272         }
273         catch (Exception&)
274         {}
275     if (PresenterConfigurationAccess::GetProperty(rxProperties, gsMouseOverFileNameProperty) >>= sFileName)
276         try
277         {
278             pBitmap->SetBitmap(
279                 BitmapDescriptor::MouseOver,
280                 rxPresenterHelper->loadBitmap(rsBasePath + sFileName, rxCanvas));
281         }
282         catch (Exception&)
283         {}
284     if (PresenterConfigurationAccess::GetProperty(rxProperties, gsButtonDownFileNameProperty) >>= sFileName)
285         try
286         {
287             pBitmap->SetBitmap(
288                 BitmapDescriptor::ButtonDown,
289                 rxPresenterHelper->loadBitmap(rsBasePath + sFileName, rxCanvas));
290         }
291         catch (Exception&)
292         {}
293     if (PresenterConfigurationAccess::GetProperty(rxProperties, gsDisabledFileNameProperty) >>= sFileName)
294         try
295         {
296             pBitmap->SetBitmap(
297                 BitmapDescriptor::Disabled,
298                 rxPresenterHelper->loadBitmap(rsBasePath + sFileName, rxCanvas));
299         }
300         catch (Exception&)
301         {}
302     if (PresenterConfigurationAccess::GetProperty(rxProperties, gsMaskFileNameProperty) >>= sFileName)
303         try
304         {
305             pBitmap->SetBitmap(
306                 BitmapDescriptor::Mask,
307                 rxPresenterHelper->loadBitmap(rsBasePath + sFileName, rxCanvas));
308         }
309         catch (Exception&)
310         {}
311 
312 
313     PresenterConfigurationAccess::GetProperty(rxProperties, gsXOffsetProperty) >>= pBitmap->mnXOffset;
314     PresenterConfigurationAccess::GetProperty(rxProperties, gsYOffsetProperty) >>= pBitmap->mnYOffset;
315 
316     PresenterConfigurationAccess::GetProperty(rxProperties, gsXHotSpotProperty) >>= pBitmap->mnXHotSpot;
317     PresenterConfigurationAccess::GetProperty(rxProperties, gsYHotSpotProperty) >>= pBitmap->mnYHotSpot;
318 
319     PresenterConfigurationAccess::GetProperty(rxProperties, gsReplacementColorProperty) >>= pBitmap->maReplacementColor;
320 
321     OUString sTexturingMode;
322     if (PresenterConfigurationAccess::GetProperty(rxProperties, gsHorizontalTexturingModeProperty) >>= sTexturingMode)
323         pBitmap->meHorizontalTexturingMode = StringToTexturingMode(sTexturingMode);
324     if (PresenterConfigurationAccess::GetProperty(rxProperties, gsVerticalTexturingModeProperty) >>= sTexturingMode)
325         pBitmap->meVerticalTexturingMode = StringToTexturingMode(sTexturingMode);
326 
327     return pBitmap;
328 }
329 
330 
331 
332 
333 PresenterBitmapContainer::BitmapDescriptor::TexturingMode
334     PresenterBitmapContainer::StringToTexturingMode (const OUString& rsTexturingMode)
335 {
336     if (rsTexturingMode == A2S("Once"))
337         return PresenterBitmapContainer::BitmapDescriptor::Once;
338     else if (rsTexturingMode == A2S("Repeat"))
339         return PresenterBitmapContainer::BitmapDescriptor::Repeat;
340     else if (rsTexturingMode == A2S("Stretch"))
341         return PresenterBitmapContainer::BitmapDescriptor::Stretch;
342     else
343         return PresenterBitmapContainer::BitmapDescriptor::Once;
344 }
345 
346 
347 
348 
349 //===== PresenterBitmapContainer::BitmapSet ===================================
350 
351 PresenterBitmapContainer::BitmapDescriptor::BitmapDescriptor (void)
352     : mnWidth(0),
353       mnHeight(0),
354       mnXOffset(0),
355       mnYOffset(0),
356       mnXHotSpot(0),
357       mnYHotSpot(0),
358       maReplacementColor(0x00000000),
359       meHorizontalTexturingMode(Once),
360       meVerticalTexturingMode(Once),
361       mxNormalBitmap(),
362       mxMouseOverBitmap(),
363       mxButtonDownBitmap(),
364       mxDisabledBitmap(),
365       mxMaskBitmap()
366 {
367 }
368 
369 
370 
371 
372 PresenterBitmapContainer::BitmapDescriptor::BitmapDescriptor (
373     const ::boost::shared_ptr<PresenterBitmapContainer::BitmapDescriptor>& rpDefault)
374     : mnWidth(0),
375       mnHeight(0),
376       mnXOffset(0),
377       mnYOffset(0),
378       mnXHotSpot(0),
379       mnYHotSpot(0),
380       maReplacementColor(0x00000000),
381       meHorizontalTexturingMode(Once),
382       meVerticalTexturingMode(Once),
383       mxNormalBitmap(),
384       mxMouseOverBitmap(),
385       mxButtonDownBitmap(),
386       mxDisabledBitmap(),
387       mxMaskBitmap()
388 {
389     if (rpDefault.get() != NULL)
390     {
391         mnWidth = rpDefault->mnWidth;
392         mnHeight = rpDefault->mnHeight;
393         mnXOffset = rpDefault->mnXOffset;
394         mnYOffset = rpDefault->mnYOffset;
395         mnXHotSpot = rpDefault->mnXHotSpot;
396         mnYHotSpot = rpDefault->mnYHotSpot;
397         maReplacementColor = rpDefault->maReplacementColor;
398         meHorizontalTexturingMode = rpDefault->meHorizontalTexturingMode;
399         meVerticalTexturingMode = rpDefault->meVerticalTexturingMode;
400         mxNormalBitmap = rpDefault->mxNormalBitmap;
401         mxMouseOverBitmap = rpDefault->mxMouseOverBitmap;
402         mxButtonDownBitmap = rpDefault->mxButtonDownBitmap;
403         mxDisabledBitmap = rpDefault->mxDisabledBitmap;
404         mxMaskBitmap = rpDefault->mxMaskBitmap;
405     }
406 }
407 
408 
409 css::uno::Reference<css::rendering::XBitmap>
410     PresenterBitmapContainer::BitmapDescriptor::GetNormalBitmap (void) const
411 {
412     return mxNormalBitmap;
413 }
414 
415 
416 
417 
418 css::uno::Reference<css::rendering::XBitmap>
419     PresenterBitmapContainer::BitmapDescriptor::GetBitmap (
420         const Mode eMode,
421         const bool bMissingDefaultsToNormal) const
422 {
423     switch (eMode)
424     {
425         case Normal:
426         default:
427             return mxNormalBitmap;
428 
429         case MouseOver:
430             if (mxMouseOverBitmap.is())
431                 return mxMouseOverBitmap;
432             else if (bMissingDefaultsToNormal)
433                 return mxNormalBitmap;
434 
435         case ButtonDown:
436             if (mxButtonDownBitmap.is())
437                 return mxButtonDownBitmap;
438             else if (bMissingDefaultsToNormal)
439                 return mxNormalBitmap;
440 
441         case Disabled:
442             if (mxDisabledBitmap.is())
443                 return mxDisabledBitmap;
444             else if (bMissingDefaultsToNormal)
445                 return mxNormalBitmap;
446 
447         case Mask:
448             return mxMaskBitmap;
449     }
450     return NULL;
451 }
452 
453 
454 
455 
456 void PresenterBitmapContainer::BitmapDescriptor::SetBitmap (
457     const Mode eMode,
458     const css::uno::Reference<css::rendering::XBitmap>& rxBitmap)
459 {
460     switch (eMode)
461     {
462         case Normal:
463         default:
464             mxNormalBitmap = rxBitmap;
465             if (mxNormalBitmap.is())
466             {
467                 const geometry::IntegerSize2D aSize (mxNormalBitmap->getSize());
468                 mnWidth = aSize.Width;
469                 mnHeight = aSize.Height;
470             }
471             break;
472 
473         case MouseOver:
474             mxMouseOverBitmap = rxBitmap;
475             break;
476 
477         case ButtonDown:
478             mxButtonDownBitmap = rxBitmap;
479             break;
480 
481         case Disabled:
482             mxDisabledBitmap = rxBitmap;
483             break;
484 
485         case Mask:
486             mxMaskBitmap = rxBitmap;
487             break;
488     }
489 }
490 
491 
492 
493 
494 } } // end of namespace ::sdext::presenter
495