xref: /AOO41X/main/svx/source/accessibility/DGColorNameLookUp.cxx (revision f6e50924346d0b8c0b07c91832a97665dd718b0c)
1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir #include "DGColorNameLookUp.hxx"
27cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
28cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
29cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
31cdf0e10cSrcweir #include <vos/mutex.hxx>
32cdf0e10cSrcweir #include <vcl/svapp.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using ::rtl::OUString;
36cdf0e10cSrcweir using namespace ::com::sun::star;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir namespace accessibility {
39cdf0e10cSrcweir 
40cdf0e10cSrcweir // Initialize the class instance with NULL.  A true instance is created only
41cdf0e10cSrcweir // when the static <member>Instance</member> is called for the first time.
42cdf0e10cSrcweir DGColorNameLookUp* DGColorNameLookUp::mpInstance = NULL;
43cdf0e10cSrcweir 
Instance(void)44cdf0e10cSrcweir DGColorNameLookUp& DGColorNameLookUp::Instance (void)
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     // Using double check pattern to make sure that exactly one instance of
47cdf0e10cSrcweir     // the shape type handler is instantiated.
48cdf0e10cSrcweir     if (mpInstance == NULL)
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir         ::vos::OGuard aGuard (::Application::GetSolarMutex());
51cdf0e10cSrcweir         if (mpInstance == NULL)
52cdf0e10cSrcweir         {
53cdf0e10cSrcweir             // Create the single instance of the color name look up.
54cdf0e10cSrcweir             mpInstance = new DGColorNameLookUp();
55cdf0e10cSrcweir         }
56cdf0e10cSrcweir     }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     return *mpInstance;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 
LookUpColor(long int nColor) const64cdf0e10cSrcweir OUString DGColorNameLookUp::LookUpColor (long int nColor) const
65cdf0e10cSrcweir {
66cdf0e10cSrcweir     OUString sColorName;
67cdf0e10cSrcweir     tColorValueToNameMap::const_iterator I;
68cdf0e10cSrcweir     I = maColorValueToNameMap.find (nColor);
69cdf0e10cSrcweir     if (I != maColorValueToNameMap.end())
70cdf0e10cSrcweir         // Found the color value.  Return the associated name.
71cdf0e10cSrcweir         sColorName = I->second;
72cdf0e10cSrcweir     else
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir         // Did not find the given color.  Append its rgb tuple to the
75cdf0e10cSrcweir         // description.
76cdf0e10cSrcweir         ::rtl::OUStringBuffer sNameBuffer;
77cdf0e10cSrcweir         sNameBuffer.append (sal_Unicode('#'));
78cdf0e10cSrcweir         sNameBuffer.append (nColor, 16);
79cdf0e10cSrcweir         sColorName = sNameBuffer.makeStringAndClear();
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir     return sColorName;
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 
DGColorNameLookUp(void)87cdf0e10cSrcweir DGColorNameLookUp::DGColorNameLookUp (void)
88cdf0e10cSrcweir {
89cdf0e10cSrcweir     uno::Sequence<OUString> aNames;
90cdf0e10cSrcweir     uno::Reference<container::XNameAccess> xNA;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     try
93cdf0e10cSrcweir     {
94cdf0e10cSrcweir         // Create color table in which to look up the given color.
95cdf0e10cSrcweir         uno::Reference<container::XNameContainer> xColorTable (
96cdf0e10cSrcweir             ::comphelper::getProcessServiceFactory()->createInstance(
97cdf0e10cSrcweir                 OUString::createFromAscii("com.sun.star.drawing.ColorTable")),
98cdf0e10cSrcweir             uno::UNO_QUERY);
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         // Get list of color names in order to iterate over the color table.
101cdf0e10cSrcweir         xNA = uno::Reference<container::XNameAccess>(xColorTable, uno::UNO_QUERY);
102cdf0e10cSrcweir         if (xNA.is())
103cdf0e10cSrcweir         {
104cdf0e10cSrcweir             // Look the solar mutex here as workarround for missing lock in
105cdf0e10cSrcweir             // called function.
106cdf0e10cSrcweir             ::vos::OGuard aGuard (::Application::GetSolarMutex());
107cdf0e10cSrcweir             aNames = xNA->getElementNames();
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir     catch (uno::RuntimeException e)
111cdf0e10cSrcweir     {
112cdf0e10cSrcweir         // When an excpetion occured then whe have an empty name sequence
113cdf0e10cSrcweir         // and the loop below is not entered.
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     // Fill the map to convert from numerical color values to names.
117cdf0e10cSrcweir     if (xNA.is())
118cdf0e10cSrcweir         for (long int i=0; i<aNames.getLength(); i++)
119cdf0e10cSrcweir         {
120cdf0e10cSrcweir             // Get the numerical value for the i-th color name.
121cdf0e10cSrcweir             try
122cdf0e10cSrcweir             {
123cdf0e10cSrcweir                 uno::Any aColor (xNA->getByName (aNames[i]));
124cdf0e10cSrcweir                 long nColor = 0;
125cdf0e10cSrcweir                 aColor >>= nColor;
126cdf0e10cSrcweir                 maColorValueToNameMap[nColor] = aNames[i];
127cdf0e10cSrcweir             }
128cdf0e10cSrcweir             catch (uno::RuntimeException e)
129cdf0e10cSrcweir             {
130cdf0e10cSrcweir                 // Ignore the exception: the color who lead to the excpetion
131cdf0e10cSrcweir                 // is not included into the map.
132cdf0e10cSrcweir             }
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 
~DGColorNameLookUp(void)139cdf0e10cSrcweir DGColorNameLookUp::~DGColorNameLookUp (void)
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     maColorValueToNameMap.clear();
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir } // end of namespace accessibility
145