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