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