Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "sal/config.h"
30 : :
31 : : #include "boost/noncopyable.hpp"
32 : : #include "boost/unordered_map.hpp"
33 : : #include "com/sun/star/container/XNameAccess.hpp"
34 : : #include "com/sun/star/container/XNameContainer.hpp"
35 : : #include "com/sun/star/lang/XMultiServiceFactory.hpp"
36 : : #include "com/sun/star/uno/Any.hxx"
37 : : #include "com/sun/star/uno/Reference.hxx"
38 : : #include "com/sun/star/uno/RuntimeException.hpp"
39 : : #include "com/sun/star/uno/Sequence.hxx"
40 : : #include "comphelper/processfactory.hxx"
41 : : #include "rtl/ustring.h"
42 : : #include "rtl/ustring.hxx"
43 : : #include "vcl/svapp.hxx"
44 : :
45 : : namespace {
46 : :
47 : : namespace css = com::sun::star;
48 : :
49 [ # # ]: 0 : class ColorNameMap: private boost::noncopyable {
50 : : public:
51 : : ColorNameMap();
52 : :
53 : : rtl::OUString lookUp(long color) const;
54 : :
55 : : private:
56 : : typedef boost::unordered_map< long, rtl::OUString > Map;
57 : :
58 : : Map map_;
59 : : };
60 : :
61 [ # # ]: 0 : ColorNameMap::ColorNameMap() {
62 [ # # ]: 0 : css::uno::Sequence< rtl::OUString > aNames;
63 : 0 : css::uno::Reference< css::container::XNameAccess > xNA;
64 : :
65 : : try
66 : : {
67 : : // Create color table in which to look up the given color.
68 : : css::uno::Reference< css::container::XNameContainer > xColorTable (
69 [ # # ][ # # ]: 0 : comphelper::getProcessServiceFactory()->createInstance(
70 : 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.ColorTable")) ),
71 [ # # ][ # # ]: 0 : css::uno::UNO_QUERY);
[ # # ]
72 : :
73 : : // Get list of color names in order to iterate over the color table.
74 [ # # ][ # # ]: 0 : xNA = css::uno::Reference< css::container::XNameAccess >(xColorTable, css::uno::UNO_QUERY);
75 [ # # ]: 0 : if (xNA.is())
76 : : {
77 : : // Lock the solar mutex here as workarround for missing lock in
78 : : // called function.
79 [ # # ]: 0 : SolarMutexGuard aGuard;
80 [ # # ][ # # ]: 0 : aNames = xNA->getElementNames();
[ # # ][ # # ]
[ # # ]
81 [ # # ]: 0 : }
82 : : }
83 [ # # ]: 0 : catch (css::uno::RuntimeException const&)
84 : : {
85 : : // When an exception occurred then whe have an empty name sequence
86 : : // and the loop below is not entered.
87 : : }
88 : :
89 : : // Fill the map to convert from numerical color values to names.
90 [ # # ]: 0 : if (xNA.is())
91 [ # # ]: 0 : for (long int i=0; i<aNames.getLength(); i++)
92 : : {
93 : : // Get the numerical value for the i-th color name.
94 : : try
95 : : {
96 [ # # ][ # # ]: 0 : css::uno::Any aColor (xNA->getByName (aNames[i]));
[ # # ]
97 : 0 : long nColor = 0;
98 : 0 : aColor >>= nColor;
99 [ # # ][ # # ]: 0 : map_[nColor] = aNames[i];
[ # # ]
100 : : }
101 [ # # ]: 0 : catch (css::uno::RuntimeException const&)
102 : : {
103 : : // Ignore the exception: the color who lead to the exception
104 : : // is not included into the map.
105 : : }
106 [ # # ]: 0 : }
107 : 0 : }
108 : :
109 : 0 : rtl::OUString ColorNameMap::lookUp(long color) const {
110 [ # # ]: 0 : Map::const_iterator i(map_.find(color));
111 [ # # ][ # # ]: 0 : if (i != map_.end()) {
112 [ # # ]: 0 : return i->second;
113 : : }
114 : : // Did not find the given color; return its RGB tuple representation:
115 : 0 : rtl::OUStringBuffer buf;
116 [ # # ]: 0 : buf.append(sal_Unicode('#'));
117 [ # # ]: 0 : buf.append(color, 16);
118 [ # # ]: 0 : return buf.makeStringAndClear();
119 : : }
120 : :
121 : : struct theColorNameMap: public rtl::Static< ColorNameMap, theColorNameMap > {};
122 : :
123 : : }
124 : :
125 : : namespace accessibility {
126 : :
127 : 0 : rtl::OUString lookUpColorName(long color) {
128 : 0 : return theColorNameMap::get().lookUp(color);
129 : : }
130 : :
131 : : }
132 : :
133 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|