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