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