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