]> git.lyx.org Git - lyx.git/blob - src/LColor.C
two patches from john
[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 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include <X11/Xlib.h>
17
18 #include "debug.h"
19 #include "LColor.h"
20 #include "support/LAssert.h"
21 #include "gettext.h"
22 #include "support/lstrings.h"
23
24 using std::endl;
25
26 void LColor::fill(LColor::color col, string const & gui,
27                   string const & latex, string const & x11,
28                   string const & lyx) {
29         information in;
30         in.guiname = gui;
31         in.latexname = latex;
32         in.x11name = x11;
33         in.lyxname = lyx;
34
35         infotab[col] = in;
36 }
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 LColor::LColor()
48 {
49         //  LColor::color, gui, latex, x11, lyx
50         ColorEntry items[] = {
51         { none, N_("none"), "none", "black", "none" },
52         { black, N_("black"), "black", "black", "black" },
53         { white, N_("white"), "white", "white", "white" },
54         { red, N_("red"), "red", "red", "red" },
55         { green, N_("green"), "green", "green", "green" },
56         { blue, N_("blue"), "blue", "blue", "blue" },
57         { cyan, N_("cyan"), "cyan", "cyan", "cyan" },
58         { magenta, N_("magenta"), "magenta", "magenta", "magenta" },
59         { yellow, N_("yellow"), "yellow", "yellow", "yellow" },
60         { cursor, N_("cursor"), "cursor", "black", "cursor" },
61         { background, N_("background"), "background", "linen", "background" },
62         { foreground, N_("text"), "foreground", "black", "foreground" },
63         { selection, N_("selection"), "selection", "LightBlue", "selection" },
64         { latex, N_("latex text"), "latex", "DarkRed", "latex" },
65         { note, N_("note"), "note", "yellow", "note" },
66         { notebg, N_("note background"), "notebg", "yellow", "notebg" },
67         { depthbar, N_("depth bar"), "depthbar", "IndianRed", "depthbar" },
68         { language, N_("language"), "language", "Blue", "language" },
69         { command, N_("command inset"), "command", "black", "command" },
70         { commandbg, N_("command inset background"), "commandbg", "azure", "commandbg" },
71         { commandframe, N_("command inset frame"), "commandframe", "black", "commandframe" },
72         { special, N_("special character"), "special", "RoyalBlue", "special" },
73         { math, N_("math"), "math", "DarkBlue", "math" },
74         { mathbg, N_("math background"), "mathbg", "linen", "mathbg" },
75         { graphicsbg, N_("graphics background"), "graphicsbg", "linen", "graphicsbg" },
76         { mathmacrobg, N_("Math macro background"), "mathmacrobg", "linen", "mathmacrobg" },
77         { mathframe, N_("math frame"), "mathframe", "Magenta", "mathframe" },
78         { mathcursor, N_("math cursor"), "mathcursor", "black", "mathcursor" },
79         { mathline, N_("math line"), "mathline", "Blue", "mathline" },
80         { captionframe, N_("caption frame"), "captionframe", "DarkRed", "captionframe" },
81         { collapsable, N_("collapsable inset text"), "collapsable", "DarkRed", "collapsable" },
82         { collapsableframe, N_("collapsable inset frame"), "collapsableframe", "IndianRed", "collapsableframe" },
83         { insetbg, N_("inset background"), "insetbg", "grey80", "insetbg" },
84         { insetframe, N_("inset frame"), "insetframe", "IndianRed", "insetframe" },
85         { error, N_("LaTeX error"), "error", "Red", "error" },
86         { eolmarker, N_("end-of-line marker"), "eolmarker", "Brown", "eolmarker" },
87         { appendixline, N_("appendix line"), "appendixline", "Brown", "appendixline" },
88         { added_space, N_("added space markers"), "added_space", "Brown", "added_space" },
89         { topline, N_("top/bottom line"), "topline", "Brown", "topline" },
90         { tabularline, N_("tabular line"), "tabularline", "black",
91              "tabularline" },
92         { tabularonoffline, N_("tabular on/off line"), "tabularonoffline",
93              "LightSteelBlue", "tabularonoffline" },
94         { bottomarea, N_("bottom area"), "bottomarea", "grey40", "bottomarea" },
95         { pagebreak, N_("page break"), "pagebreak", "RoyalBlue", "pagebreak" },
96         { top, N_("top of button"), "top", "grey90", "top" },
97         { bottom, N_("bottom of button"), "bottom", "grey60", "bottom" },
98         { left, N_("left of button"), "left", "grey90", "left" },
99         { right, N_("right of button"), "right", "grey60", "right" },
100         { buttonbg, N_("button background"), "buttonbg", "grey80", "buttonbg" },
101         { inherit, N_("inherit"), "inherit", "black", "inherit" },
102         { ignore, N_("ignore"), "ignore", "black", "ignore" },
103         { ignore, 0, 0, 0, 0 }
104         };
105
106         int i = 0;
107         while (items[i].guiname) {
108                 fill(items[i].lcolor, items[i].guiname, items[i].latexname,
109                      items[i].x11name, items[i].lyxname);
110                 ++i;
111         }
112 }
113
114
115 string const LColor::getGUIName(LColor::color c) const
116 {
117         InfoTab::const_iterator ici = infotab.find(c);
118         if (ici != infotab.end())
119                 return _(ici->second.guiname);
120
121         return "none";
122 }
123
124
125 string const LColor::getX11Name(LColor::color c) const
126 {
127         InfoTab::const_iterator ici = infotab.find(c);
128         if (ici != infotab.end()) 
129                 return ici->second.x11name;
130
131         lyxerr << "LyX internal error: Missing color"
132                 " entry in LColor.C for " << int(c) << '\n';
133         lyxerr << "Using black.\n";
134         return "black";
135 }
136
137
138 string const LColor::getLaTeXName(LColor::color c) const
139 {
140         InfoTab::const_iterator ici = infotab.find(c);
141         if (ici != infotab.end())
142                 return ici->second.latexname;
143         return "black";
144 }
145
146
147 string const LColor::getLyXName(LColor::color c) const
148 {
149         InfoTab::const_iterator ici = infotab.find(c);
150         if (ici != infotab.end())
151                 return ici->second.lyxname;
152         return "black";
153 }
154
155
156 void LColor::setColor(LColor::color col, string const & x11name)
157 {
158         InfoTab::iterator iti = infotab.find(col);
159         if (iti != infotab.end()) {
160                 iti->second.x11name = x11name;
161                 return;
162         }
163         lyxerr << "LyX internal error: color and such.\n";
164         lyx::Assert(false);
165 }
166
167
168 bool LColor::setColor(string const & lyxname, string const & x11name)
169 {
170         color col = getFromLyXName (lyxname);
171
172         // "inherit" is returned for colors not in the database
173         // (and anyway should not be redefined)
174         if (col == inherit || col == ignore) {
175                 lyxerr << "Color " << lyxname << " is undefined or may not be"
176                         " redefined" << endl;
177                 return false;
178         }
179         setColor (col, x11name);
180         return true;
181 }
182
183
184 LColor::color LColor::getFromGUIName(string const & guiname) const
185 {
186         InfoTab::const_iterator ici = infotab.begin();
187         InfoTab::const_iterator end = infotab.end();
188         for (; ici != end; ++ici) {
189                 if (!compare_no_case(_(ici->second.guiname), guiname))
190                         return ici->first;
191         }
192         return LColor::inherit;
193 }
194
195
196 LColor::color LColor::getFromLyXName(string const & lyxname) const
197 {
198         
199         InfoTab::const_iterator ici = infotab.begin();
200         InfoTab::const_iterator end = infotab.end();
201         for (; ici != end; ++ici) {
202                 if (!compare_no_case(ici->second.lyxname, lyxname))
203                         return ici->first;
204         }
205         return LColor::inherit;
206 }
207
208 // The evil global LColor instance
209 LColor lcolor;
210 // An equally evil global system LColor instance
211 LColor system_lcolor;