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