]> git.lyx.org Git - lyx.git/blob - src/LColor.C
cleanup some debug messages
[lyx.git] / src / LColor.C
1 /**
2  * \file LColor.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Matthias Ettrich
9  * \author Jean-Marc Lasgouttes
10  * \author John Levon
11  * \author André Pönitz
12  * \author Martin Vermeer
13  *
14  * Full author contact details are available in file CREDITS.
15  */
16
17 #include <config.h>
18
19 #include "debug.h"
20 #include "gettext.h"
21 #include "LColor.h"
22 #include "support/lstrings.h"
23
24 #include <map>
25
26 using lyx::support::compare_ascii_no_case;
27 using lyx::support::ascii_lowercase;
28
29 using std::endl;
30 using std::string;
31
32
33 namespace {
34
35 struct ColorEntry {
36         LColor::color lcolor;
37         char const * guiname;
38         char const * latexname;
39         char const * x11name;
40         char const * lyxname;
41 };
42
43 }
44
45 class LColor::Pimpl {
46 public:
47         ///
48         class information {
49         public:
50                 /// the name as it appears in the GUI
51                 string guiname;
52                 /// the name used in LaTeX
53                 string latexname;
54                 /// the name for X11
55                 string x11name;
56                 /// the name for LyX
57                 string lyxname;
58         };
59
60         /// initialise a color entry
61         void fill(ColorEntry const & entry)
62         {
63                 information in;
64                 in.lyxname   = entry.lyxname;
65                 in.latexname = entry.latexname;
66                 in.x11name   = entry.x11name;
67                 in.guiname   = entry.guiname;
68                 infotab[entry.lcolor] = in;
69                 lyxcolors[entry.lyxname] = entry.lcolor;
70                 latexcolors[entry.latexname] = entry.lcolor;
71         }
72
73         ///
74         typedef std::map<LColor::color, information> InfoTab;
75         /// the table of color information
76         InfoTab infotab;
77
78         typedef std::map<string, LColor::color> Transform;
79         /// the transform between LyX color name string and integer code.
80         Transform lyxcolors;
81         /// the transform between LaTeX color name string and integer code.
82         Transform latexcolors;
83
84 };
85
86
87 LColor::LColor()
88         : pimpl_(new Pimpl)
89 {
90         //  LColor::color, gui, latex, x11, lyx
91         static ColorEntry const items[] = {
92         { none, N_("none"), "none", "black", "none" },
93         { black, N_("black"), "black", "black", "black" },
94         { white, N_("white"), "white", "white", "white" },
95         { red, N_("red"), "red", "red", "red" },
96         { green, N_("green"), "green", "green", "green" },
97         { blue, N_("blue"), "blue", "blue", "blue" },
98         { cyan, N_("cyan"), "cyan", "cyan", "cyan" },
99         { magenta, N_("magenta"), "magenta", "magenta", "magenta" },
100         { yellow, N_("yellow"), "yellow", "yellow", "yellow" },
101         { cursor, N_("cursor"), "cursor", "black", "cursor" },
102         { background, N_("background"), "background", "linen", "background" },
103         { foreground, N_("text"), "foreground", "black", "foreground" },
104         { selection, N_("selection"), "selection", "LightBlue", "selection" },
105         { latex, N_("LaTeX text"), "latex", "DarkRed", "latex" },
106         { preview, N_("previewed snippet"), "preview", "black", "preview" },
107         { note, N_("note"), "note", "yellow", "note" },
108         { notebg, N_("note background"), "notebg", "yellow", "notebg" },
109         { comment, N_("comment"), "comment", "magenta", "comment" },
110         { commentbg, N_("comment background"), "commentbg", "linen", "commentbg" },
111         { greyedout, N_("greyedout inset"), "greyedout", "red", "greyedout" },
112         { greyedoutbg, N_("greyedout inset background"), "greyedoutbg", "linen", "greyedoutbg" },
113         { depthbar, N_("depth bar"), "depthbar", "IndianRed", "depthbar" },
114         { language, N_("language"), "language", "Blue", "language" },
115         { command, N_("command inset"), "command", "black", "command" },
116         { commandbg, N_("command inset background"), "commandbg", "azure", "commandbg" },
117         { commandframe, N_("command inset frame"), "commandframe", "black", "commandframe" },
118         { special, N_("special character"), "special", "RoyalBlue", "special" },
119         { math, N_("math"), "math", "DarkBlue", "math" },
120         { mathbg, N_("math background"), "mathbg", "linen", "mathbg" },
121         { graphicsbg, N_("graphics background"), "graphicsbg", "linen", "graphicsbg" },
122         { mathmacrobg, N_("Math macro background"), "mathmacrobg", "linen", "mathmacrobg" },
123         { mathframe, N_("math frame"), "mathframe", "Magenta", "mathframe" },
124         { mathline, N_("math line"), "mathline", "Blue", "mathline" },
125         { captionframe, N_("caption frame"), "captionframe", "DarkRed", "captionframe" },
126         { collapsable, N_("collapsable inset text"), "collapsable", "DarkRed", "collapsable" },
127         { collapsableframe, N_("collapsable inset frame"), "collapsableframe", "IndianRed", "collapsableframe" },
128         { insetbg, N_("inset background"), "insetbg", "grey80", "insetbg" },
129         { insetframe, N_("inset frame"), "insetframe", "IndianRed", "insetframe" },
130         { error, N_("LaTeX error"), "error", "Red", "error" },
131         { eolmarker, N_("end-of-line marker"), "eolmarker", "Brown", "eolmarker" },
132         { appendix, N_("appendix marker"), "appendix", "Brown", "appendix" },
133         { changebar, N_("change bar"), "changebar", "Blue", "changebar" },
134         { strikeout, N_("Deleted text"), "strikeout", "Red", "strikeout" },
135         { newtext, N_("Added text"), "newtext", "Blue", "newtext" },
136         { added_space, N_("added space markers"), "added_space", "Brown", "added_space" },
137         { topline, N_("top/bottom line"), "topline", "Brown", "topline" },
138         { tabularline, N_("table line"), "tabularline", "black",
139              "tabularline" },
140         { tabularonoffline, N_("table on/off line"), "tabularonoffline",
141              "LightSteelBlue", "tabularonoffline" },
142         { bottomarea, N_("bottom area"), "bottomarea", "grey40", "bottomarea" },
143         { pagebreak, N_("page break"), "pagebreak", "RoyalBlue", "pagebreak" },
144         { top, N_("top of button"), "top", "grey90", "top" },
145         { bottom, N_("bottom of button"), "bottom", "grey60", "bottom" },
146         { left, N_("left of button"), "left", "grey90", "left" },
147         { right, N_("right of button"), "right", "grey60", "right" },
148         { buttonbg, N_("button background"), "buttonbg", "grey80", "buttonbg" },
149         { inherit, N_("inherit"), "inherit", "black", "inherit" },
150         { ignore, N_("ignore"), "ignore", "black", "ignore" },
151         { ignore, 0, 0, 0, 0 }
152         };
153
154         for (int i = 0; items[i].guiname; ++i)
155                 pimpl_->fill(items[i]);
156 }
157
158
159 LColor::LColor(LColor const & c)
160         : pimpl_(new Pimpl(*c.pimpl_))
161 {}
162
163
164 LColor::~LColor()
165 {}
166
167
168 LColor & LColor::operator=(LColor tmp)
169 {
170         boost::swap(pimpl_, tmp.pimpl_);
171         return *this;
172 }
173
174
175 string const LColor::getGUIName(LColor::color c) const
176 {
177         Pimpl::InfoTab::const_iterator it = pimpl_->infotab.find(c);
178         if (it != pimpl_->infotab.end())
179                 return _(it->second.guiname);
180         return "none";
181 }
182
183
184 string const LColor::getX11Name(LColor::color c) const
185 {
186         Pimpl::InfoTab::const_iterator it = pimpl_->infotab.find(c);
187         if (it != pimpl_->infotab.end())
188                 return it->second.x11name;
189
190         lyxerr << "LyX internal error: Missing color"
191                   " entry in LColor.C for " << c << '\n'
192                << "Using black." << endl;
193         return "black";
194 }
195
196
197 string const LColor::getLaTeXName(LColor::color c) const
198 {
199         Pimpl::InfoTab::const_iterator it = pimpl_->infotab.find(c);
200         if (it != pimpl_->infotab.end())
201                 return it->second.latexname;
202         return "black";
203 }
204
205
206 string const LColor::getLyXName(LColor::color c) const
207 {
208         Pimpl::InfoTab::const_iterator it = pimpl_->infotab.find(c);
209         if (it != pimpl_->infotab.end())
210                 return it->second.lyxname;
211         return "black";
212 }
213
214
215 bool LColor::setColor(LColor::color col, string const & x11name)
216 {
217         Pimpl::InfoTab::iterator it = pimpl_->infotab.find(col);
218         if (it == pimpl_->infotab.end()) {
219                 lyxerr << "Color " << col << " not found in database."
220                        << std::endl;
221                 return false;
222         }
223
224         // "inherit" is returned for colors not in the database
225         // (and anyway should not be redefined)
226         if (col == none || col == inherit || col == ignore) {
227                 lyxerr << "Color " << getLyXName(col)
228                        << " may not be redefined" << endl;
229                 return false;
230         }
231
232         it->second.x11name = x11name;
233         return true;
234 }
235
236
237 bool LColor::setColor(string const & lyxname, string const &x11name)
238 {
239         string const lcname = ascii_lowercase(lyxname);
240         if (pimpl_->lyxcolors.find(lcname) == pimpl_->lyxcolors.end()) {
241                 lyxerr[Debug::GUI]
242                         << "LColor::setColor: Unknown color \""
243                        << lyxname << '"' << endl;
244                 addColor(static_cast<color>(pimpl_->infotab.size()), lcname);
245         }
246
247         return setColor(pimpl_->lyxcolors[lcname], x11name);
248 }
249
250
251 LColor::color LColor::getFromGUIName(string const & guiname) const
252 {
253         Pimpl::InfoTab::const_iterator it = pimpl_->infotab.begin();
254         Pimpl::InfoTab::const_iterator end = pimpl_->infotab.end();
255         for (; it != end; ++it) {
256                 if (!compare_ascii_no_case(_(it->second.guiname), guiname))
257                         return it->first;
258         }
259         return LColor::inherit;
260 }
261
262
263 void LColor::addColor(LColor::color c, string const & lyxname) const
264 {
265         ColorEntry ce = { c, "", "", "", lyxname.c_str() };
266         pimpl_->fill(ce);
267 }
268
269
270 LColor::color LColor::getFromLyXName(string const & lyxname) const
271 {
272         string const lcname = ascii_lowercase(lyxname);
273         if (pimpl_->lyxcolors.find(lcname) == pimpl_->lyxcolors.end()) {
274                 lyxerr << "LColor::getFromLyXName: Unknown color \""
275                        << lyxname << '"' << endl;
276                 return none;
277         }
278
279         return pimpl_->lyxcolors[lcname];
280 }
281
282
283 LColor::color LColor::getFromLaTeXName(string const & latexname) const
284 {
285         if (pimpl_->latexcolors.find(latexname) == pimpl_->latexcolors.end()) {
286                 lyxerr << "LColor::getFromLaTeXName: Unknown color \""
287                        << latexname << '"' << endl;
288                 return none;
289         }
290
291         return pimpl_->latexcolors[latexname];
292 }
293
294
295 // The evil global LColor instance
296 LColor lcolor;
297 // An equally evil global system LColor instance
298 LColor system_lcolor;