xref: /AOO41X/main/svx/source/accessibility/DGColorNameLookUp.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svx.hxx"
30 #include "DGColorNameLookUp.hxx"
31 #include <com/sun/star/container/XNameContainer.hpp>
32 #include <com/sun/star/container/XNameAccess.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <vos/mutex.hxx>
36 #include <vcl/svapp.hxx>
37 
38 
39 using ::rtl::OUString;
40 using namespace ::com::sun::star;
41 
42 namespace accessibility {
43 
44 // Initialize the class instance with NULL.  A true instance is created only
45 // when the static <member>Instance</member> is called for the first time.
46 DGColorNameLookUp* DGColorNameLookUp::mpInstance = NULL;
47 
48 DGColorNameLookUp& DGColorNameLookUp::Instance (void)
49 {
50     // Using double check pattern to make sure that exactly one instance of
51     // the shape type handler is instantiated.
52     if (mpInstance == NULL)
53     {
54         ::vos::OGuard aGuard (::Application::GetSolarMutex());
55         if (mpInstance == NULL)
56         {
57             // Create the single instance of the color name look up.
58             mpInstance = new DGColorNameLookUp();
59         }
60     }
61 
62     return *mpInstance;
63 }
64 
65 
66 
67 
68 OUString DGColorNameLookUp::LookUpColor (long int nColor) const
69 {
70     OUString sColorName;
71     tColorValueToNameMap::const_iterator I;
72     I = maColorValueToNameMap.find (nColor);
73     if (I != maColorValueToNameMap.end())
74         // Found the color value.  Return the associated name.
75         sColorName = I->second;
76     else
77     {
78         // Did not find the given color.  Append its rgb tuple to the
79         // description.
80         ::rtl::OUStringBuffer sNameBuffer;
81         sNameBuffer.append (sal_Unicode('#'));
82         sNameBuffer.append (nColor, 16);
83         sColorName = sNameBuffer.makeStringAndClear();
84     }
85     return sColorName;
86 }
87 
88 
89 
90 
91 DGColorNameLookUp::DGColorNameLookUp (void)
92 {
93     uno::Sequence<OUString> aNames;
94     uno::Reference<container::XNameAccess> xNA;
95 
96     try
97     {
98         // Create color table in which to look up the given color.
99         uno::Reference<container::XNameContainer> xColorTable (
100             ::comphelper::getProcessServiceFactory()->createInstance(
101                 OUString::createFromAscii("com.sun.star.drawing.ColorTable")),
102             uno::UNO_QUERY);
103 
104         // Get list of color names in order to iterate over the color table.
105         xNA = uno::Reference<container::XNameAccess>(xColorTable, uno::UNO_QUERY);
106         if (xNA.is())
107         {
108             // Look the solar mutex here as workarround for missing lock in
109             // called function.
110             ::vos::OGuard aGuard (::Application::GetSolarMutex());
111             aNames = xNA->getElementNames();
112         }
113     }
114     catch (uno::RuntimeException e)
115     {
116         // When an excpetion occured then whe have an empty name sequence
117         // and the loop below is not entered.
118     }
119 
120     // Fill the map to convert from numerical color values to names.
121     if (xNA.is())
122         for (long int i=0; i<aNames.getLength(); i++)
123         {
124             // Get the numerical value for the i-th color name.
125             try
126             {
127                 uno::Any aColor (xNA->getByName (aNames[i]));
128                 long nColor = 0;
129                 aColor >>= nColor;
130                 maColorValueToNameMap[nColor] = aNames[i];
131             }
132             catch (uno::RuntimeException e)
133             {
134                 // Ignore the exception: the color who lead to the excpetion
135                 // is not included into the map.
136             }
137         }
138 }
139 
140 
141 
142 
143 DGColorNameLookUp::~DGColorNameLookUp (void)
144 {
145     maColorValueToNameMap.clear();
146 }
147 
148 } // end of namespace accessibility
149