Line data Source code
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 :
20 : #include "sal/config.h"
21 :
22 : #include "boost/noncopyable.hpp"
23 : #include "boost/unordered_map.hpp"
24 : #include "com/sun/star/container/XNameAccess.hpp"
25 : #include "com/sun/star/container/XNameContainer.hpp"
26 : #include "com/sun/star/lang/XMultiServiceFactory.hpp"
27 : #include "com/sun/star/uno/Any.hxx"
28 : #include "com/sun/star/uno/Reference.hxx"
29 : #include "com/sun/star/uno/RuntimeException.hpp"
30 : #include "com/sun/star/uno/Sequence.hxx"
31 : #include "comphelper/processfactory.hxx"
32 : #include "rtl/ustring.h"
33 : #include "rtl/ustring.hxx"
34 : #include "vcl/svapp.hxx"
35 :
36 : namespace {
37 :
38 0 : class ColorNameMap: private boost::noncopyable {
39 : public:
40 : ColorNameMap();
41 :
42 : rtl::OUString lookUp(long color) const;
43 :
44 : private:
45 : typedef boost::unordered_map< long, rtl::OUString > Map;
46 :
47 : Map map_;
48 : };
49 :
50 0 : ColorNameMap::ColorNameMap() {
51 0 : css::uno::Sequence< rtl::OUString > aNames;
52 0 : css::uno::Reference< css::container::XNameAccess > xNA;
53 :
54 : try
55 : {
56 : // Create color table in which to look up the given color.
57 : css::uno::Reference< css::container::XNameContainer > xColorTable (
58 0 : comphelper::getProcessServiceFactory()->createInstance(
59 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.ColorTable")) ),
60 0 : css::uno::UNO_QUERY);
61 :
62 : // Get list of color names in order to iterate over the color table.
63 0 : xNA = css::uno::Reference< css::container::XNameAccess >(xColorTable, css::uno::UNO_QUERY);
64 0 : if (xNA.is())
65 : {
66 : // Lock the solar mutex here as workarround for missing lock in
67 : // called function.
68 0 : SolarMutexGuard aGuard;
69 0 : aNames = xNA->getElementNames();
70 0 : }
71 : }
72 0 : catch (css::uno::RuntimeException const&)
73 : {
74 : // When an exception occurred then whe have an empty name sequence
75 : // and the loop below is not entered.
76 : }
77 :
78 : // Fill the map to convert from numerical color values to names.
79 0 : if (xNA.is())
80 0 : for (long int i=0; i<aNames.getLength(); i++)
81 : {
82 : // Get the numerical value for the i-th color name.
83 : try
84 : {
85 0 : css::uno::Any aColor (xNA->getByName (aNames[i]));
86 0 : long nColor = 0;
87 0 : aColor >>= nColor;
88 0 : map_[nColor] = aNames[i];
89 : }
90 0 : catch (css::uno::RuntimeException const&)
91 : {
92 : // Ignore the exception: the color who lead to the exception
93 : // is not included into the map.
94 : }
95 0 : }
96 0 : }
97 :
98 0 : rtl::OUString ColorNameMap::lookUp(long color) const {
99 0 : Map::const_iterator i(map_.find(color));
100 0 : if (i != map_.end()) {
101 0 : return i->second;
102 : }
103 : // Did not find the given color; return its RGB tuple representation:
104 0 : rtl::OUStringBuffer buf;
105 0 : buf.append(sal_Unicode('#'));
106 0 : buf.append(color, 16);
107 0 : return buf.makeStringAndClear();
108 : }
109 :
110 : struct theColorNameMap: public rtl::Static< ColorNameMap, theColorNameMap > {};
111 :
112 : }
113 :
114 : namespace accessibility {
115 :
116 0 : rtl::OUString lookUpColorName(long color) {
117 0 : return theColorNameMap::get().lookUp(color);
118 : }
119 :
120 : }
121 :
122 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|