]> git.lyx.org Git - lyx.git/blob - src/Color.cpp
Further cleanup of InsetFlex, InsetCollapsable and InsetLayout:
[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
21 #include "debug.h"
22 #include "gettext.h"
23
24 #include "support/lstrings.h"
25
26 #include <boost/assert.hpp>
27
28 #include <map>
29 #include <cmath>
30 #include <sstream>
31 #include <iomanip>
32
33
34 #ifndef CXX_GLOBAL_CSTD
35 using std::floor;
36 #endif
37
38 using std::max;
39 using std::min;
40 using std::setw;
41
42 using std::istringstream;
43 using std::ostringstream;
44 using std::string;
45 using std::endl;
46
47 namespace lyx {
48
49 using support::compare_ascii_no_case;
50 using support::ascii_lowercase;
51
52 struct ColorSet::ColorEntry {
53         ColorCode lcolor;
54         char const * guiname;
55         char const * latexname;
56         char const * x11name;
57         char const * lyxname;
58 };
59
60
61 static int hexstrToInt(string const & str)
62 {
63         int val = 0;
64         istringstream is(str);
65         is >> std::setbase(16) >> val;
66         return val;
67 }
68
69
70 /////////////////////////////////////////////////////////////////////
71 //
72 // RGBColor
73 //
74 /////////////////////////////////////////////////////////////////////
75
76
77 string const X11hexname(RGBColor const & col)
78 {
79         ostringstream ostr;
80
81         ostr << '#' << std::setbase(16) << std::setfill('0')
82              << setw(2) << col.r
83              << setw(2) << col.g
84              << setw(2) << col.b;
85
86         return ostr.str();
87 }
88
89
90 RGBColor rgbFromHexName(string const & x11hexname)
91 {
92         RGBColor c;
93         BOOST_ASSERT(x11hexname.size() == 7 && x11hexname[0] == '#');
94         c.r = hexstrToInt(x11hexname.substr(1,2));
95         c.g = hexstrToInt(x11hexname.substr(3,2));
96         c.b = hexstrToInt(x11hexname.substr(5,2));
97         return c;
98 }
99
100
101 ColorSet::ColorSet()
102 {
103         //  ColorCode, gui, latex, x11, lyx
104         static ColorEntry const items[] = {
105         { Color_none, N_("none"), "none", "black", "none" },
106         { Color_black, N_("black"), "black", "black", "black" },
107         { Color_white, N_("white"), "white", "white", "white" },
108         { Color_red, N_("red"), "red", "red", "red" },
109         { Color_green, N_("green"), "green", "green", "green" },
110         { Color_blue, N_("blue"), "blue", "blue", "blue" },
111         { Color_cyan, N_("cyan"), "cyan", "cyan", "cyan" },
112         { Color_magenta, N_("magenta"), "magenta", "magenta", "magenta" },
113         { Color_yellow, N_("yellow"), "yellow", "yellow", "yellow" },
114         { Color_cursor, N_("cursor"), "cursor", "black", "cursor" },
115         { Color_background, N_("background"), "background", "linen", "background" },
116         { Color_foreground, N_("text"), "foreground", "black", "foreground" },
117         { Color_selection, N_("selection"), "selection", "LightBlue", "selection" },
118         { Color_latex, N_("LaTeX text"), "latex", "DarkRed", "latex" },
119         { Color_preview, N_("previewed snippet"), "preview", "black", "preview" },
120         { Color_note, N_("note"), "note", "blue", "note" },
121         { Color_notebg, N_("note background"), "notebg", "yellow", "notebg" },
122         { Color_comment, N_("comment"), "comment", "magenta", "comment" },
123         { Color_commentbg, N_("comment background"), "commentbg", "linen", "commentbg" },
124         { Color_greyedout, N_("greyedout inset"), "greyedout", "red", "greyedout" },
125         { Color_greyedoutbg, N_("greyedout inset background"), "greyedoutbg", "linen", "greyedoutbg" },
126         { Color_shadedbg, N_("shaded box"), "shaded", "#ff0000", "shaded" },
127         { Color_depthbar, N_("depth bar"), "depthbar", "IndianRed", "depthbar" },
128         { Color_language, N_("language"), "language", "Blue", "language" },
129         { Color_command, N_("command inset"), "command", "black", "command" },
130         { Color_commandbg, N_("command inset background"), "commandbg", "azure", "commandbg" },
131         { Color_commandframe, N_("command inset frame"), "commandframe", "black", "commandframe" },
132         { Color_special, N_("special character"), "special", "RoyalBlue", "special" },
133         { Color_math, N_("math"), "math", "DarkBlue", "math" },
134         { Color_mathbg, N_("math background"), "mathbg", "linen", "mathbg" },
135         { Color_graphicsbg, N_("graphics background"), "graphicsbg", "linen", "graphicsbg" },
136         { Color_mathmacrobg, N_("Math macro background"), "mathmacrobg", "linen", "mathmacrobg" },
137         { Color_mathframe, N_("math frame"), "mathframe", "Magenta", "mathframe" },
138         { Color_mathcorners, N_("math corners"), "mathcorners", "linen", "mathcorners" },
139         { Color_mathline, N_("math line"), "mathline", "Blue", "mathline" },
140         { Color_mathmacrobg, N_("Math macro background"), "mathmacrobg", "#ede2d8", "mathmacrobg" },
141         { Color_mathmacrohoverbg, N_("Math macro hovered background"), "mathmacrohoverbg", "#cdc3b8", "mathmacrohoverbg" },
142         { Color_mathmacrolabel, N_("Math macro label"), "mathmacrolabel", "#a19992", "mathmacrolabel" },
143         { Color_mathmacroframe, N_("Math macro frame"), "mathmacroframe", "#ede2d8", "mathmacroframe" },
144         { Color_mathmacroblend, N_("Math macro blended out"), "mathmacroblend", "#000000", "mathmacroblend" },
145         { Color_captionframe, N_("caption frame"), "captionframe", "DarkRed", "captionframe" },
146         { Color_collapsable, N_("collapsable inset text"), "collapsable", "DarkRed", "collapsable" },
147         { Color_collapsableframe, N_("collapsable inset frame"), "collapsableframe", "IndianRed", "collapsableframe" },
148         { Color_insetbg, N_("inset background"), "insetbg", "grey80", "insetbg" },
149         { Color_insetframe, N_("inset frame"), "insetframe", "IndianRed", "insetframe" },
150         { Color_error, N_("LaTeX error"), "error", "Red", "error" },
151         { Color_eolmarker, N_("end-of-line marker"), "eolmarker", "Brown", "eolmarker" },
152         { Color_appendix, N_("appendix marker"), "appendix", "Brown", "appendix" },
153         { Color_changebar, N_("change bar"), "changebar", "Blue", "changebar" },
154         { Color_deletedtext, N_("Deleted text"), "deletedtext", "#ff0000", "deletedtext" },
155         { Color_addedtext, N_("Added text"), "addedtext", "#0000ff", "addedtext" },
156         { Color_added_space, N_("added space markers"), "added_space", "Brown", "added_space" },
157         { Color_topline, N_("top/bottom line"), "topline", "Brown", "topline" },
158         { Color_tabularline, N_("table line"), "tabularline", "black", "tabularline" },
159         { Color_tabularonoffline, N_("table on/off line"), "tabularonoffline",
160              "LightSteelBlue", "tabularonoffline" },
161         { Color_bottomarea, N_("bottom area"), "bottomarea", "grey40", "bottomarea" },
162         { Color_pagebreak, N_("page break"), "pagebreak", "RoyalBlue", "pagebreak" },
163         { Color_buttonframe, N_("frame of button"), "buttonframe", "#dcd2c8", "buttonframe" },
164         { Color_buttonbg, N_("button background"), "buttonbg", "#dcd2c8", "buttonbg" },
165         { Color_buttonhoverbg, N_("button background under focus"), "buttonhoverbg", "#C7C7CA", "buttonhoverbg" },
166         { Color_inherit, N_("inherit"), "inherit", "black", "inherit" },
167         { Color_ignore, N_("ignore"), "ignore", "black", "ignore" },
168         { Color_ignore, 0, 0, 0, 0 }
169         };
170
171         for (int i = 0; items[i].guiname; ++i)
172                 fill(items[i]);
173 }
174
175
176 /// initialise a color entry
177 void ColorSet::fill(ColorEntry const & entry)
178 {
179         Information in;
180         in.lyxname   = entry.lyxname;
181         in.latexname = entry.latexname;
182         in.x11name   = entry.x11name;
183         in.guiname   = entry.guiname;
184         infotab[entry.lcolor] = in;
185         lyxcolors[entry.lyxname] = entry.lcolor;
186         latexcolors[entry.latexname] = entry.lcolor;
187 }
188
189
190 docstring const ColorSet::getGUIName(ColorCode c) const
191 {
192         InfoTab::const_iterator it = infotab.find(c);
193         if (it != infotab.end())
194                 return _(it->second.guiname);
195         return from_ascii("none");
196 }
197
198
199 string const ColorSet::getX11Name(ColorCode c) const
200 {
201         InfoTab::const_iterator it = infotab.find(c);
202         if (it != infotab.end())
203                 return it->second.x11name;
204
205         lyxerr << "LyX internal error: Missing color"
206                   " entry in Color.cpp for " << c << '\n'
207                << "Using black." << endl;
208         return "black";
209 }
210
211
212 string const ColorSet::getLaTeXName(ColorCode c) const
213 {
214         InfoTab::const_iterator it = infotab.find(c);
215         if (it != infotab.end())
216                 return it->second.latexname;
217         return "black";
218 }
219
220
221 string const ColorSet::getLyXName(ColorCode c) const
222 {
223         InfoTab::const_iterator it = infotab.find(c);
224         if (it != infotab.end())
225                 return it->second.lyxname;
226         return "black";
227 }
228
229
230 bool ColorSet::setColor(ColorCode col, string const & x11name)
231 {
232         InfoTab::iterator it = infotab.find(col);
233         if (it == infotab.end()) {
234                 lyxerr << "Color " << col << " not found in database."
235                        << std::endl;
236                 return false;
237         }
238
239         // "inherit" is returned for colors not in the database
240         // (and anyway should not be redefined)
241         if (col == Color_none || col == Color_inherit || col == Color_ignore) {
242                 lyxerr << "Color " << getLyXName(col)
243                        << " may not be redefined" << endl;
244                 return false;
245         }
246
247         it->second.x11name = x11name;
248         return true;
249 }
250
251
252 bool ColorSet::setColor(string const & lyxname, string const &x11name)
253 {
254         string const lcname = ascii_lowercase(lyxname);
255         if (lyxcolors.find(lcname) == lyxcolors.end()) {
256                 LYXERR(Debug::GUI)
257                         << "ColorSet::setColor: Unknown color \""
258                        << lyxname << '"' << endl;
259                 addColor(static_cast<ColorCode>(infotab.size()), lcname);
260         }
261
262         return setColor(lyxcolors[lcname], x11name);
263 }
264
265
266 void ColorSet::addColor(ColorCode c, string const & lyxname)
267 {
268         ColorEntry ce = { c, "", "", "", lyxname.c_str() };
269         fill(ce);
270 }
271
272
273 ColorCode ColorSet::getFromLyXName(string const & lyxname) const
274 {
275         string const lcname = ascii_lowercase(lyxname);
276         Transform::const_iterator it = lyxcolors.find(lcname);
277         if (it == lyxcolors.end()) {
278                 lyxerr << "ColorSet::getFromLyXName: Unknown color \""
279                        << lyxname << '"' << endl;
280                 return Color_none;
281         }
282
283         return it->second;
284 }
285
286
287 ColorCode ColorSet::getFromLaTeXName(string const & latexname) const
288 {
289         Transform::const_iterator it = latexcolors.find(latexname);
290         if (it == latexcolors.end()) {
291                 lyxerr << "ColorSet::getFromLaTeXName: Unknown color \""
292                        << latexname << '"' << endl;
293                 return Color_none;
294         }
295
296         return it->second;
297 }
298
299
300 // The evil global Color instance
301 ColorSet lcolor;
302 // An equally evil global system Color instance
303 ColorSet system_lcolor;
304
305
306 } // namespace lyx