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