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