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