]> git.lyx.org Git - lyx.git/blob - src/LColor.C
Point fix, earlier forgotten
[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 "support/LAssert.h"
22 #include "gettext.h"
23 #include "support/lstrings.h"
24
25 #include <map>
26
27 using namespace lyx::support;
28
29 using std::endl;
30
31
32 namespace {
33
34 struct ColorEntry {
35         int lcolor;
36         char const * guiname;
37         char const * latexname;
38         char const * x11name;
39         char const * lyxname;
40 };
41
42 }
43
44 struct TransformEntry {
45         char const * lyxname;
46         int ncolor;
47 };
48
49
50 struct LColor::Pimpl {
51         ///
52         struct information {
53                 /// the name as it appears in the GUI
54                 string guiname;
55                 /// the name used in LaTeX
56                 string latexname;
57                 /// the name for X11
58                 string x11name;
59                 /// the name for LyX
60                 string lyxname;
61         };
62
63         /// initialise a color entry
64         void fill(ColorEntry const & entry)
65         {
66                 information in;
67                 in.lyxname   = string(entry.lyxname);
68                 in.latexname = string(entry.latexname);
69                 in.x11name   = string(entry.x11name);
70                 in.guiname   = string(entry.guiname);
71                 infotab[entry.lcolor] = in;
72                 transform[string(entry.lyxname)] = int(entry.lcolor);
73         }
74
75         ///
76         typedef std::map<int, information> InfoTab;
77         /// the table of color information
78         InfoTab infotab;
79
80         typedef std::map<string, int> Transform;
81         /// the transform between colour name string and integer code.
82         Transform transform;
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 void LColor::operator=(LColor const & c)
169 {
170         LColor tmp(c);
171         boost::swap(pimpl_, tmp.pimpl_);
172 }
173
174
175 void LColor::fill(LColor::color c, 
176                                 string const & lyxname,
177                                 string const & x11name, 
178                                 string const & latexname, 
179                                 string const & guiname) 
180 {
181         ColorEntry ce;
182         ce.lcolor = c;
183         ce.guiname = guiname.c_str();   
184         ce.latexname = latexname.c_str();       
185         ce.x11name = x11name.c_str();   
186         ce.lyxname = lyxname.c_str();   
187         pimpl_->fill(ce);
188 }
189
190
191 string const LColor::getGUIName(LColor::color c) const
192 {
193         Pimpl::InfoTab::const_iterator ici = pimpl_->infotab.find(c);
194         if (ici != pimpl_->infotab.end())
195                 return _(ici->second.guiname);
196         return "none";
197 }
198
199
200 string const LColor::getGUIName(string const &  s) const
201 {
202         Pimpl::Transform::const_iterator ici = pimpl_->transform.find(s);
203         if (ici != pimpl_->transform.end()) {
204                 Pimpl::InfoTab::const_iterator 
205                         it = pimpl_->infotab.find(ici->second);
206                 if (it != pimpl_->infotab.end()) 
207                         return it->second.guiname;      
208         }
209         return "none";
210 }
211
212
213 string const LColor::getX11Name(LColor::color c) const
214 {
215         Pimpl::InfoTab::const_iterator ici = pimpl_->infotab.find(c);
216         if (ici != pimpl_->infotab.end())
217                 return ici->second.x11name;
218
219         lyxerr << "LyX internal error: Missing color"
220                 " entry in LColor.C for " << int(c) << endl;
221         lyxerr << "Using black." << endl;
222         return "black";
223 }
224
225
226 string const LColor::getX11Name(string const & s) const
227 {
228         Pimpl::Transform::const_iterator ici = pimpl_->transform.find(s);
229         if (ici != pimpl_->transform.end()) {
230                 Pimpl::InfoTab::const_iterator 
231                         it = pimpl_->infotab.find(ici->second);
232                 if (it != pimpl_->infotab.end()) 
233                         return it->second.x11name;
234         }
235         lyxerr << "LyX internal error: Missing color"
236                 " entry in LColor.C for " << s << endl;
237         lyxerr << "Using black." << endl;
238         return "black";
239 }
240
241
242 string const LColor::getLaTeXName(LColor::color c) const
243 {
244         Pimpl::InfoTab::const_iterator ici = pimpl_->infotab.find(c);
245         if (ici != pimpl_->infotab.end())
246                 return ici->second.latexname;
247         return "black";
248 }
249
250
251 string const LColor::getLaTeXName(string const & s) const
252 {
253         Pimpl::Transform::const_iterator ici = pimpl_->transform.find(s);
254         if (ici != pimpl_->transform.end()) {
255                 Pimpl::InfoTab::const_iterator 
256                         it = pimpl_->infotab.find(ici->second);
257                 if (it != pimpl_->infotab.end()) 
258                         return it->second.latexname;
259         }
260         return "black";
261 }
262
263
264 string const LColor::getLyXName(LColor::color c) const
265 {
266         Pimpl::InfoTab::const_iterator ici = pimpl_->infotab.find(c);
267         if (ici != pimpl_->infotab.end())
268                 return ici->second.lyxname;
269         return "black";
270 }
271
272
273 size_t LColor::size() const
274 {
275         return pimpl_->infotab.size();
276 }
277
278
279 void LColor::setColor(LColor::color col, string const & x11name)
280 {
281         Pimpl::InfoTab::iterator iti = pimpl_->infotab.find(col);
282         if (iti != pimpl_->infotab.end()) {
283                 iti->second.x11name = x11name;
284                 return;
285         }
286         lyxerr << "LyX internal error: color and such." << endl;
287         Assert(false);
288 }
289
290
291 bool LColor::setColor(string const & lyxname, string const & x11name)
292 {
293         color col = getFromLyXName(lyxname);
294
295         // "inherit" is returned for colors not in the database
296         // (and anyway should not be redefined)
297         if (col == inherit || col == ignore) {
298                 lyxerr << "Color " << lyxname << " is undefined or may not be"
299                         " redefined" << endl;
300                 return false;
301         }
302         setColor(col, x11name);
303         return true;
304 }
305
306
307 LColor::color LColor::getFromGUIName(string const & guiname) const
308 {
309         Pimpl::InfoTab::const_iterator ici = pimpl_->infotab.begin();
310         Pimpl::InfoTab::const_iterator end = pimpl_->infotab.end();
311         for (; ici != end; ++ici) {
312                 if (!compare_ascii_no_case(_(ici->second.guiname), guiname))
313                         return static_cast<LColor::color>(ici->first);
314         }
315         return LColor::inherit;
316 }
317
318
319 LColor::color LColor::getFromLyXName(string const & lyxname) const
320 {
321         return static_cast<LColor::color>(pimpl_->transform[lyxname]);
322 }
323
324
325 // The evil global LColor instance
326 LColor lcolor;
327 // An equally evil global system LColor instance
328 LColor system_lcolor;