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