]> git.lyx.org Git - lyx.git/blob - src/Color.cpp
fix "make check" with gcc 4.3
[lyx.git] / src / Color.cpp
1 /**
2  * \file Color.cpp
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 "Color.h"
20 #include "ColorSet.h"
21
22 #include "support/debug.h"
23 #include "support/gettext.h"
24 #include "support/lstrings.h"
25 #include "support/lassert.h"
26
27 #include <map>
28 #include <cmath>
29 #include <sstream>
30 #include <iomanip>
31
32
33 using namespace std;
34 using namespace lyx::support;
35
36 namespace lyx {
37
38
39 struct ColorSet::ColorEntry {
40         ColorCode lcolor;
41         char const * guiname;
42         char const * latexname;
43         char const * x11name;
44         char const * lyxname;
45 };
46
47
48 static int hexstrToInt(string const & str)
49 {
50         int val = 0;
51         istringstream is(str);
52         is >> setbase(16) >> val;
53         return val;
54 }
55
56
57 /////////////////////////////////////////////////////////////////////
58 //
59 // RGBColor
60 //
61 /////////////////////////////////////////////////////////////////////
62
63
64 string const X11hexname(RGBColor const & col)
65 {
66         ostringstream ostr;
67
68         ostr << '#' << setbase(16) << setfill('0')
69              << setw(2) << col.r
70              << setw(2) << col.g
71              << setw(2) << col.b;
72
73         return ostr.str();
74 }
75
76
77 RGBColor rgbFromHexName(string const & x11hexname)
78 {
79         RGBColor c;
80         LASSERT(x11hexname.size() == 7 && x11hexname[0] == '#', /**/);
81         c.r = hexstrToInt(x11hexname.substr(1, 2));
82         c.g = hexstrToInt(x11hexname.substr(3, 2));
83         c.b = hexstrToInt(x11hexname.substr(5, 2));
84         return c;
85 }
86
87
88 Color::Color(ColorCode base_color) : baseColor(base_color), 
89         mergeColor(Color_ignore)
90 {}
91
92
93 bool Color::operator==(Color const & color) const
94 {
95         return baseColor == color.baseColor;
96 }
97
98
99 bool Color::operator!=(Color const & color) const       
100 {
101         return baseColor != color.baseColor;
102 }
103
104
105 bool Color::operator<(Color const & color) const
106 {
107         return baseColor < color.baseColor;
108 }
109
110
111 bool Color::operator<=(Color const & color) const
112 {
113         return baseColor <= color.baseColor;
114 }
115
116
117 std::ostream & operator<<(std::ostream & os, Color color)
118 {
119         os << to_ascii(lcolor.getGUIName(color.baseColor));
120         if (color.mergeColor != Color_ignore)
121                 os << "[merged with:"
122                         << to_ascii(lcolor.getGUIName(color.mergeColor)) << "]";
123         return os;
124 }
125
126
127 ColorSet::ColorSet()
128 {
129         char const * grey40 = "#666666";
130         char const * grey60 = "#999999";
131         char const * grey80 = "#cccccc";
132         //char const * grey90 = "#e5e5e5";
133         //  ColorCode, gui, latex, x11, lyx
134         static ColorEntry const items[] = {
135         { Color_none, N_("none"), "none", "black", "none" },
136         { Color_black, N_("black"), "black", "black", "black" },
137         { Color_white, N_("white"), "white", "white", "white" },
138         { Color_red, N_("red"), "red", "red", "red" },
139         { Color_green, N_("green"), "green", "green", "green" },
140         { Color_blue, N_("blue"), "blue", "blue", "blue" },
141         { Color_cyan, N_("cyan"), "cyan", "cyan", "cyan" },
142         { Color_magenta, N_("magenta"), "magenta", "magenta", "magenta" },
143         { Color_yellow, N_("yellow"), "yellow", "yellow", "yellow" },
144         { Color_cursor, N_("cursor"), "cursor", "black", "cursor" },
145         { Color_background, N_("background"), "background", "linen", "background" },
146         { Color_foreground, N_("text"), "foreground", "black", "foreground" },
147         { Color_selection, N_("selection"), "selection", "LightBlue", "selection" },
148         { Color_selectiontext, N_("selected text"),
149                 "selectiontext", "black", "selectiontext" },
150         { Color_latex, N_("LaTeX text"), "latex", "DarkRed", "latex" },
151         { Color_inlinecompletion, N_("inline completion"),
152                 "inlinecompletion", grey60, "inlinecompletion" },
153         { Color_nonunique_inlinecompletion, N_("non-unique inline completion"),
154                 "nonuniqueinlinecompletion", grey80, "nonuniqueinlinecompletion" },
155         { Color_preview, N_("previewed snippet"), "preview", "black", "preview" },
156         { Color_notelabel, N_("note label"), "note", "yellow", "note" },
157         { Color_notebg, N_("note background"), "notebg", "yellow", "notebg" },
158         { Color_commentlabel, N_("comment label"), "comment", "magenta", "comment" },
159         { Color_commentbg, N_("comment background"), "commentbg", "linen", "commentbg" },
160         { Color_greyedoutlabel, N_("greyedout inset label"), "greyedout", "#ff0080", "greyedout" },
161         { Color_greyedoutbg, N_("greyedout inset background"), "greyedoutbg", "linen", "greyedoutbg" },
162         { Color_phantomtext, N_("phantom inset text"), "phantomtext", "#7f7f7f", "phantomtext" },
163         { Color_shadedbg, N_("shaded box"), "shaded", "#ff0000", "shaded" },
164         { Color_listingsbg, N_("listings background"), "listingsbg", "white", "listingsbg" },
165         { Color_branchlabel, N_("branch label"), "branchlabel", "#c88000", "branchlabel" },
166         { Color_footlabel, N_("footnote label"), "footlabel", "#00aaff", "footlabel" },
167         { Color_indexlabel, N_("index label"), "indexlabel", "green", "indexlabel" },
168         { Color_marginlabel, N_("margin note label"), "marginlabel", "#aa55ff", "marginlabel" },
169         { Color_urllabel, N_("URL label"), "urllabel", "blue", "urllabel" },
170         { Color_urltext, N_("URL text"), "urltext", "blue", "urltext" },
171         { Color_depthbar, N_("depth bar"), "depthbar", "IndianRed", "depthbar" },
172         { Color_language, N_("language"), "language", "Blue", "language" },
173         { Color_command, N_("command inset"), "command", "black", "command" },
174         { Color_commandbg, N_("command inset background"), "commandbg", "azure", "commandbg" },
175         { Color_commandframe, N_("command inset frame"), "commandframe", "black", "commandframe" },
176         { Color_special, N_("special character"), "special", "RoyalBlue", "special" },
177         { Color_math, N_("math"), "math", "DarkBlue", "math" },
178         { Color_mathbg, N_("math background"), "mathbg", "linen", "mathbg" },
179         { Color_graphicsbg, N_("graphics background"), "graphicsbg", "linen", "graphicsbg" },
180         { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", "linen", "mathmacrobg" },
181         { Color_mathframe, N_("math frame"), "mathframe", "Magenta", "mathframe" },
182         { Color_mathcorners, N_("math corners"), "mathcorners", "linen", "mathcorners" },
183         { Color_mathline, N_("math line"), "mathline", "Blue", "mathline" },
184         { Color_mathmacrobg, N_("math macro background"), "mathmacrobg", "#ede2d8", "mathmacrobg" },
185         { Color_mathmacrohoverbg, N_("math macro hovered background"), "mathmacrohoverbg", "#cdc3b8", "mathmacrohoverbg" },
186         { Color_mathmacrolabel, N_("math macro label"), "mathmacrolabel", "#a19992", "mathmacrolabel" },
187         { Color_mathmacroframe, N_("math macro frame"), "mathmacroframe", "#ede2d8", "mathmacroframe" },
188         { Color_mathmacroblend, N_("math macro blended out"), "mathmacroblend", "black", "mathmacroblend" },
189         { Color_mathmacrooldarg, N_("math macro old parameter"), "mathmacrooldarg", grey80, "mathmacrooldarg" },
190         { Color_mathmacronewarg, N_("math macro new parameter"), "mathmacronewarg", "black", "mathmacronewarg" },
191         { Color_captionframe, N_("caption frame"), "captionframe", "DarkRed", "captionframe" },
192         { Color_collapsable, N_("collapsable inset text"), "collapsable", "DarkRed", "collapsable" },
193         { Color_collapsableframe, N_("collapsable inset frame"), "collapsableframe", "IndianRed", "collapsableframe" },
194         { Color_insetbg, N_("inset background"), "insetbg", grey80, "insetbg" },
195         { Color_insetframe, N_("inset frame"), "insetframe", "IndianRed", "insetframe" },
196         { Color_error, N_("LaTeX error"), "error", "Red", "error" },
197         { Color_eolmarker, N_("end-of-line marker"), "eolmarker", "Brown", "eolmarker" },
198         { Color_appendix, N_("appendix marker"), "appendix", "Brown", "appendix" },
199         { Color_changebar, N_("change bar"), "changebar", "Blue", "changebar" },
200         { Color_deletedtext, N_("deleted text"), "deletedtext", "#ff0000", "deletedtext" },
201         { Color_addedtext, N_("added text"), "addedtext", "#0000ff", "addedtext" },
202         { Color_changedtextauthor1, N_("changed text 1st author"), "changedtextauthor1", "#0000ff", "changedtextauthor1" },
203         { Color_changedtextauthor2, N_("changed text 2nd author"), "changedtextauthor2", "#ff00ff", "changedtextauthor2" },
204         { Color_changedtextauthor3, N_("changed text 3rd author"), "changedtextauthor3", "#ff0000", "changedtextauthor3" },
205         { Color_changedtextauthor4, N_("changed text 4th author"), "changedtextauthor4", "#aa00ff", "changedtextauthor4" },
206         { Color_changedtextauthor5, N_("changed text 5th author"), "changedtextauthor5", "#55aa00", "changedtextauthor5" },
207         { Color_deletedtextmodifier, N_("deleted text modifier"), "deletedtextmodifier", "white", "deletedtextmodifier" },
208         { Color_added_space, N_("added space markers"), "added_space", "Brown", "added_space" },
209         { Color_topline, N_("top/bottom line"), "topline", "Brown", "topline" },
210         { Color_tabularline, N_("table line"), "tabularline", "black", "tabularline" },
211         { Color_tabularonoffline, N_("table on/off line"), "tabularonoffline",
212              "LightSteelBlue", "tabularonoffline" },
213         { Color_bottomarea, N_("bottom area"), "bottomarea", grey40, "bottomarea" },
214         { Color_newpage, N_("new page"), "newpage", "Blue", "newpage" },
215         { Color_pagebreak, N_("page break / line break"), "pagebreak", "RoyalBlue", "pagebreak" },
216         { Color_buttonframe, N_("frame of button"), "buttonframe", "#dcd2c8", "buttonframe" },
217         { Color_buttonbg, N_("button background"), "buttonbg", "#dcd2c8", "buttonbg" },
218         { Color_buttonhoverbg, N_("button background under focus"), "buttonhoverbg", "#C7C7CA", "buttonhoverbg" },
219         { Color_inherit, N_("inherit"), "inherit", "black", "inherit" },
220         { Color_ignore, N_("ignore"), "ignore", "black", "ignore" },
221         { Color_ignore, 0, 0, 0, 0 }
222         };
223
224         for (int i = 0; items[i].guiname; ++i)
225                 fill(items[i]);
226 }
227
228
229 /// initialise a color entry
230 void ColorSet::fill(ColorEntry const & entry)
231 {
232         Information in;
233         in.lyxname   = entry.lyxname;
234         in.latexname = entry.latexname;
235         in.x11name   = entry.x11name;
236         in.guiname   = entry.guiname;
237         infotab[entry.lcolor] = in;
238         lyxcolors[entry.lyxname] = entry.lcolor;
239         latexcolors[entry.latexname] = entry.lcolor;
240 }
241
242
243 docstring const ColorSet::getGUIName(ColorCode c) const
244 {
245         InfoTab::const_iterator it = infotab.find(c);
246         if (it != infotab.end())
247                 return _(it->second.guiname);
248         return from_ascii("none");
249 }
250
251
252 string const ColorSet::getX11Name(ColorCode c) const
253 {
254         InfoTab::const_iterator it = infotab.find(c);
255         if (it != infotab.end())
256                 return it->second.x11name;
257
258         lyxerr << "LyX internal error: Missing color"
259                   " entry in Color.cpp for " << c << '\n'
260                << "Using black." << endl;
261         return "black";
262 }
263
264
265 string const ColorSet::getLaTeXName(ColorCode c) const
266 {
267         InfoTab::const_iterator it = infotab.find(c);
268         if (it != infotab.end())
269                 return it->second.latexname;
270         return "black";
271 }
272
273
274 string const ColorSet::getLyXName(ColorCode c) const
275 {
276         InfoTab::const_iterator it = infotab.find(c);
277         if (it != infotab.end())
278                 return it->second.lyxname;
279         return "black";
280 }
281
282
283 bool ColorSet::setColor(ColorCode col, string const & x11name)
284 {
285         InfoTab::iterator it = infotab.find(col);
286         if (it == infotab.end()) {
287                 LYXERR0("Color " << col << " not found in database.");
288                 return false;
289         }
290
291         // "inherit" is returned for colors not in the database
292         // (and anyway should not be redefined)
293         if (col == Color_none || col == Color_inherit || col == Color_ignore) {
294                 LYXERR0("Color " << getLyXName(col) << " may not be redefined.");
295                 return false;
296         }
297
298         it->second.x11name = x11name;
299         return true;
300 }
301
302
303 bool ColorSet::setColor(string const & lyxname, string const &x11name)
304 {
305         string const lcname = ascii_lowercase(lyxname);
306         if (lyxcolors.find(lcname) == lyxcolors.end()) {
307                 LYXERR(Debug::GUI, "ColorSet::setColor: Unknown color \""
308                        << lyxname << '"');
309                 addColor(static_cast<ColorCode>(infotab.size()), lcname);
310         }
311
312         return setColor(lyxcolors[lcname], x11name);
313 }
314
315
316 void ColorSet::addColor(ColorCode c, string const & lyxname)
317 {
318         ColorEntry ce = { c, "", "", "", lyxname.c_str() };
319         fill(ce);
320 }
321
322
323 ColorCode ColorSet::getFromLyXName(string const & lyxname) const
324 {
325         string const lcname = ascii_lowercase(lyxname);
326         Transform::const_iterator it = lyxcolors.find(lcname);
327         if (it == lyxcolors.end()) {
328                 LYXERR0("ColorSet::getFromLyXName: Unknown color \""
329                        << lyxname << '"');
330                 return Color_none;
331         }
332
333         return it->second;
334 }
335
336
337 ColorCode ColorSet::getFromLaTeXName(string const & latexname) const
338 {
339         Transform::const_iterator it = latexcolors.find(latexname);
340         if (it == latexcolors.end()) {
341                 lyxerr << "ColorSet::getFromLaTeXName: Unknown color \""
342                        << latexname << '"' << endl;
343                 return Color_none;
344         }
345
346         return it->second;
347 }
348
349
350 // The evil global Color instance
351 ColorSet lcolor;
352 // An equally evil global system Color instance
353 ColorSet system_lcolor;
354
355
356 } // namespace lyx