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