]> git.lyx.org Git - lyx.git/blob - src/Color.cpp
Fixed some lines that were too long. It compiled afterwards.
[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 "debug.h"
20 #include "gettext.h"
21 #include "Color.h"
22 #include "support/lstrings.h"
23
24 #include <map>
25 #include <cmath>
26 #include <sstream>
27 #include <iomanip>
28
29
30 #ifndef CXX_GLOBAL_CSTD
31 using std::floor;
32 #endif
33
34 using std::max;
35 using std::min;
36 using std::setw;
37
38 using std::istringstream;
39 using std::ostringstream;
40 using std::string;
41 using std::endl;
42
43 using lyx::support::compare_ascii_no_case;
44 using lyx::support::ascii_lowercase;
45
46
47 namespace {
48
49 struct ColorEntry {
50         lyx::Color::color lcolor;
51         char const * guiname;
52         char const * latexname;
53         char const * x11name;
54         char const * lyxname;
55 };
56
57 int const nohue = -1;
58
59 int hexstrToInt(string const & str)
60 {
61         int val = 0;
62         istringstream is(str);
63         is >> std::setbase(16) >> val;
64         return val;
65 }
66
67 } // namespace anon
68
69
70
71 namespace lyx {
72
73
74 /////////////////////////////////////////////////////////////////////
75 //
76 // RGBColor
77 //
78 /////////////////////////////////////////////////////////////////////
79
80
81 string const X11hexname(RGBColor const & col)
82 {
83         ostringstream ostr;
84
85         ostr << '#' << std::setbase(16) << std::setfill('0')
86              << setw(2) << col.r
87              << setw(2) << col.g
88              << setw(2) << col.b;
89
90         return ostr.str();
91 }
92
93
94 RGBColor::RGBColor(string const & x11hexname)
95         : r(0), g(0), b(0)
96 {
97         BOOST_ASSERT(x11hexname.size() == 7 && x11hexname[0] == '#');
98         r = hexstrToInt(x11hexname.substr(1,2));
99         g = hexstrToInt(x11hexname.substr(3,2));
100         b = hexstrToInt(x11hexname.substr(5,2));
101 }
102
103
104 /////////////////////////////////////////////////////////////////////
105 //
106 // Color::Pimpl
107 //
108 /////////////////////////////////////////////////////////////////////
109
110 class Color::Pimpl {
111 public:
112         ///
113         class information {
114         public:
115                 /// the name as it appears in the GUI
116                 string guiname;
117                 /// the name used in LaTeX
118                 string latexname;
119                 /// the name for X11
120                 string x11name;
121                 /// the name for LyX
122                 string lyxname;
123         };
124
125         /// initialise a color entry
126         void fill(ColorEntry const & entry)
127         {
128                 information in;
129                 in.lyxname   = entry.lyxname;
130                 in.latexname = entry.latexname;
131                 in.x11name   = entry.x11name;
132                 in.guiname   = entry.guiname;
133                 infotab[entry.lcolor] = in;
134                 lyxcolors[entry.lyxname] = entry.lcolor;
135                 latexcolors[entry.latexname] = entry.lcolor;
136         }
137
138         ///
139         typedef std::map<Color::color, information> InfoTab;
140         /// the table of color information
141         InfoTab infotab;
142
143         typedef std::map<string, Color::color> Transform;
144         /// the transform between LyX color name string and integer code.
145         Transform lyxcolors;
146         /// the transform between LaTeX color name string and integer code.
147         Transform latexcolors;
148
149 };
150
151
152 Color::Color()
153         : pimpl_(new Pimpl)
154 {
155         //  Color::color, gui, latex, x11, lyx
156         static ColorEntry const items[] = {
157         { none, N_("none"), "none", "black", "none" },
158         { black, N_("black"), "black", "black", "black" },
159         { white, N_("white"), "white", "white", "white" },
160         { red, N_("red"), "red", "red", "red" },
161         { green, N_("green"), "green", "green", "green" },
162         { blue, N_("blue"), "blue", "blue", "blue" },
163         { cyan, N_("cyan"), "cyan", "cyan", "cyan" },
164         { magenta, N_("magenta"), "magenta", "magenta", "magenta" },
165         { yellow, N_("yellow"), "yellow", "yellow", "yellow" },
166         { cursor, N_("cursor"), "cursor", "black", "cursor" },
167         { background, N_("background"), "background", "linen", "background" },
168         { foreground, N_("text"), "foreground", "black", "foreground" },
169         { selection, N_("selection"), "selection", "LightBlue", "selection" },
170         { latex, N_("LaTeX text"), "latex", "DarkRed", "latex" },
171         { preview, N_("previewed snippet"), "preview", "black", "preview" },
172         { note, N_("note"), "note", "blue", "note" },
173         { notebg, N_("note background"), "notebg", "yellow", "notebg" },
174         { comment, N_("comment"), "comment", "magenta", "comment" },
175         { commentbg, N_("comment background"), "commentbg", "linen", "commentbg" },
176         { greyedout, N_("greyedout inset"), "greyedout", "red", "greyedout" },
177         { greyedoutbg, N_("greyedout inset background"), "greyedoutbg", "linen", "greyedoutbg" },
178         { shadedbg, N_("shaded box"), "shaded", "#ff0000", "shaded" },
179         { depthbar, N_("depth bar"), "depthbar", "IndianRed", "depthbar" },
180         { language, N_("language"), "language", "Blue", "language" },
181         { command, N_("command inset"), "command", "black", "command" },
182         { commandbg, N_("command inset background"), "commandbg", "azure", "commandbg" },
183         { commandframe, N_("command inset frame"), "commandframe", "black", "commandframe" },
184         { special, N_("special character"), "special", "RoyalBlue", "special" },
185         { math, N_("math"), "math", "DarkBlue", "math" },
186         { mathbg, N_("math background"), "mathbg", "linen", "mathbg" },
187         { graphicsbg, N_("graphics background"), "graphicsbg", "linen", "graphicsbg" },
188         { mathmacrobg, N_("Math macro background"), "mathmacrobg", "linen", "mathmacrobg" },
189         { mathframe, N_("math frame"), "mathframe", "Magenta", "mathframe" },
190         { mathcorners, N_("math corners"), "mathcorners", "linen", "mathcorners" },
191         { mathline, N_("math line"), "mathline", "Blue", "mathline" },
192         { captionframe, N_("caption frame"), "captionframe", "DarkRed", "captionframe" },
193         { collapsable, N_("collapsable inset text"), "collapsable", "DarkRed", "collapsable" },
194         { collapsableframe, N_("collapsable inset frame"), "collapsableframe", "IndianRed", "collapsableframe" },
195         { insetbg, N_("inset background"), "insetbg", "grey80", "insetbg" },
196         { insetframe, N_("inset frame"), "insetframe", "IndianRed", "insetframe" },
197         { error, N_("LaTeX error"), "error", "Red", "error" },
198         { eolmarker, N_("end-of-line marker"), "eolmarker", "Brown", "eolmarker" },
199         { appendix, N_("appendix marker"), "appendix", "Brown", "appendix" },
200         { changebar, N_("change bar"), "changebar", "Blue", "changebar" },
201         { deletedtext, N_("Deleted text"), "deletedtext", "#ff0000", "deletedtext" },
202         { addedtext, N_("Added text"), "addedtext", "#0000ff", "addedtext" },
203         { added_space, N_("added space markers"), "added_space", "Brown", "added_space" },
204         { topline, N_("top/bottom line"), "topline", "Brown", "topline" },
205         { tabularline, N_("table line"), "tabularline", "black", "tabularline" },
206         { tabularonoffline, N_("table on/off line"), "tabularonoffline",
207              "LightSteelBlue", "tabularonoffline" },
208         { bottomarea, N_("bottom area"), "bottomarea", "grey40", "bottomarea" },
209         { pagebreak, N_("page break"), "pagebreak", "RoyalBlue", "pagebreak" },
210         { buttonframe, N_("frame of button"), "buttonframe", "#dcd2c8", "buttonframe" },
211         { buttonbg, N_("button background"), "buttonbg", "#dcd2c8", "buttonbg" },
212         { buttonhoverbg, N_("button background under focus"), "buttonhoverbg", "#C7C7CA", "buttonhoverbg" },
213         { inherit, N_("inherit"), "inherit", "black", "inherit" },
214         { ignore, N_("ignore"), "ignore", "black", "ignore" },
215         { ignore, 0, 0, 0, 0 }
216         };
217
218         for (int i = 0; items[i].guiname; ++i)
219                 pimpl_->fill(items[i]);
220 }
221
222
223 Color::Color(Color const & c)
224         : pimpl_(new Pimpl(*c.pimpl_))
225 {}
226
227
228 Color::~Color()
229 {}
230
231
232 Color & Color::operator=(Color tmp)
233 {
234         boost::swap(pimpl_, tmp.pimpl_);
235         return *this;
236 }
237
238
239 docstring const Color::getGUIName(Color::color c) const
240 {
241         Pimpl::InfoTab::const_iterator it = pimpl_->infotab.find(c);
242         if (it != pimpl_->infotab.end())
243                 return _(it->second.guiname);
244         return from_ascii("none");
245 }
246
247
248 string const Color::getX11Name(Color::color c) const
249 {
250         Pimpl::InfoTab::const_iterator it = pimpl_->infotab.find(c);
251         if (it != pimpl_->infotab.end())
252                 return it->second.x11name;
253
254         lyxerr << "LyX internal error: Missing color"
255                   " entry in Color.cpp for " << c << '\n'
256                << "Using black." << endl;
257         return "black";
258 }
259
260
261 string const Color::getLaTeXName(Color::color c) const
262 {
263         Pimpl::InfoTab::const_iterator it = pimpl_->infotab.find(c);
264         if (it != pimpl_->infotab.end())
265                 return it->second.latexname;
266         return "black";
267 }
268
269
270 string const Color::getLyXName(Color::color c) const
271 {
272         Pimpl::InfoTab::const_iterator it = pimpl_->infotab.find(c);
273         if (it != pimpl_->infotab.end())
274                 return it->second.lyxname;
275         return "black";
276 }
277
278
279 bool Color::setColor(Color::color col, string const & x11name)
280 {
281         Pimpl::InfoTab::iterator it = pimpl_->infotab.find(col);
282         if (it == pimpl_->infotab.end()) {
283                 lyxerr << "Color " << col << " not found in database."
284                        << std::endl;
285                 return false;
286         }
287
288         // "inherit" is returned for colors not in the database
289         // (and anyway should not be redefined)
290         if (col == none || col == inherit || col == ignore) {
291                 lyxerr << "Color " << getLyXName(col)
292                        << " may not be redefined" << endl;
293                 return false;
294         }
295
296         it->second.x11name = x11name;
297         return true;
298 }
299
300
301 bool Color::setColor(string const & lyxname, string const &x11name)
302 {
303         string const lcname = ascii_lowercase(lyxname);
304         if (pimpl_->lyxcolors.find(lcname) == pimpl_->lyxcolors.end()) {
305                 LYXERR(Debug::GUI)
306                         << "Color::setColor: Unknown color \""
307                        << lyxname << '"' << endl;
308                 addColor(static_cast<color>(pimpl_->infotab.size()), lcname);
309         }
310
311         return setColor(pimpl_->lyxcolors[lcname], x11name);
312 }
313
314
315 void Color::addColor(Color::color c, string const & lyxname) const
316 {
317         ColorEntry ce = { c, "", "", "", lyxname.c_str() };
318         pimpl_->fill(ce);
319 }
320
321
322 Color::color Color::getFromLyXName(string const & lyxname) const
323 {
324         string const lcname = ascii_lowercase(lyxname);
325         if (pimpl_->lyxcolors.find(lcname) == pimpl_->lyxcolors.end()) {
326                 lyxerr << "Color::getFromLyXName: Unknown color \""
327                        << lyxname << '"' << endl;
328                 return none;
329         }
330
331         return pimpl_->lyxcolors[lcname];
332 }
333
334
335 Color::color Color::getFromLaTeXName(string const & latexname) const
336 {
337         if (pimpl_->latexcolors.find(latexname) == pimpl_->latexcolors.end()) {
338                 lyxerr << "Color::getFromLaTeXName: Unknown color \""
339                        << latexname << '"' << endl;
340                 return none;
341         }
342
343         return pimpl_->latexcolors[latexname];
344 }
345
346
347 // The evil global Color instance
348 Color lcolor;
349 // An equally evil global system Color instance
350 Color system_lcolor;
351
352
353 } // namespace lyx