]> git.lyx.org Git - features.git/blob - src/frontends/xforms/ColorHandler.C
Rename EnumLColor as LColor_color.
[features.git] / src / frontends / xforms / ColorHandler.C
1 /**
2  * \file ColorHandler.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ColorHandler.h"
14
15 #include "debug.h"
16 #include "gettext.h"
17 #include "LColor.h"
18
19 #include "support/lstrings.h"
20 #include "support/tostr.h"
21
22 #include "lyx_forms.h"
23
24 #include <boost/scoped_array.hpp>
25
26 #include <cmath>
27
28 using lyx::support::bformat;
29
30 #ifndef CXX_GLOBAL_CSTD
31 using std::pow;
32 #endif
33
34 using std::endl;
35
36
37 namespace {
38
39         string tostr(XColor const & col)
40         {
41                 return bformat("(%1$s,%2$s,%3$s)",
42                         ::tostr(col.red), ::tostr(col.green), ::tostr(col.blue));
43         }
44
45 }
46
47
48 LyXColorHandler::LyXColorHandler()
49                 : colorGCcache(LColor::ignore + 1)
50 {
51         display = fl_get_display();
52         drawable = XCreatePixmap(display,
53                                  RootWindow(display, fl_screen),
54                                  10, 10, fl_get_visual_depth());
55
56         colormap = fl_state[fl_get_vclass()].colormap;
57         // Clear the GC cache
58         for (int i = 0; i < colorGCcache.size(); ++i) {
59                 colorGCcache[i] = 0;
60         }
61 }
62
63
64 LyXColorHandler::~LyXColorHandler()
65 {
66         // Release all the registered GCs
67         for (unsigned i = 0; i < colorGCcache.size(); ++i) {
68                 if (colorGCcache[i] != 0) {
69                         XFreeGC(display, colorGCcache[i]);
70                 }
71         }
72         // Iterate over the line cache and Free the GCs
73         for (LineGCCache::iterator lit = lineGCcache.begin();
74              lit != lineGCcache.end(); ++lit) {
75                 XFreeGC(display, lit->second);
76         }
77 }
78
79
80 unsigned long LyXColorHandler::colorPixel(LColor_color c)
81 {
82         XGCValues val;
83         XGetGCValues(display, getGCForeground(c), GCForeground, &val);
84         return val.foreground;
85 }
86
87
88 GC LyXColorHandler::getGCForeground(string const & s)
89 {
90         XColor xcol;
91         XColor ccol;
92         XGCValues val;
93         // Look up the RGB values for the color, and an approximate
94         // color that we can hope to get on this display.
95         if (XLookupColor(display, colormap, s.c_str(), &xcol, &ccol) == 0) {
96                 lyxerr << bformat(
97                         _("LyX: Unknown X11 color %1$s\n"
98                           "     Using black instead, sorry!"),
99                         s) << endl;
100                 unsigned long bla = BlackPixel(display,
101                                                DefaultScreen(display));
102                 val.foreground = bla;
103         // Try the exact RGB values first, then the approximate.
104         } else if (XAllocColor(display, colormap, &xcol) != 0) {
105                 if (lyxerr.debugging(Debug::GUI)) {
106                         lyxerr << bformat(_("LyX: X11 color %1$s allocated"),
107                                 s) << endl;
108                 }
109                 val.foreground = xcol.pixel;
110         } else {
111                 // Here we are traversing the current colormap to find
112                 // the color closest to the one we want.
113                 Visual * vi = DefaultVisual(display, DefaultScreen(display));
114
115                 boost::scoped_array<XColor> cmap(new XColor[vi->map_entries]);
116
117                 for (int i = 0; i < vi->map_entries; ++i) {
118                         cmap[i].pixel = i;
119                 }
120                 XQueryColors(display, colormap, cmap.get(), vi->map_entries);
121
122                 // Walk through the cmap and look for close colors.
123                 int closest_pixel = 0;
124                 double closest_distance = 1e20; // we want to minimize this
125                 double distance = 0;
126                 for (int t = 0; t < vi->map_entries; ++t) {
127                         // The Euclidean distance between two points in
128                         // a three-dimensional space, the RGB color-cube,
129                         // is used as the distance measurement between two
130                         // colors.
131
132                         // Since square-root is monotonous, we don't have to
133                         // take the square-root to find the minimum, and thus
134                         // we use the squared distance instead to be faster.
135
136                         // If we want to get fancy, we could convert the RGB
137                         // coordinates to a different color-cube, maybe HSV,
138                         // but the RGB cube seems to work great.  (Asger)
139                         distance = pow(cmap[t].red   - xcol.red,   2.0) +
140                                    pow(cmap[t].green - xcol.green, 2.0) +
141                                    pow(cmap[t].blue  - xcol.blue,  2.0);
142                         if (distance < closest_distance) {
143                                 closest_distance = distance;
144                                 closest_pixel = t;
145                         }
146                 }
147
148                 lyxerr << bformat(
149                         _("LyX: Couldn't allocate '%1$s' with (r,g,b)=%3$s.\n"),
150                         s, tostr(xcol));
151
152                 lyxerr << bformat(
153                                 _("     Using closest allocated color with (r,g,b)=%1$s instead.\n"
154                           "Pixel [%2$s] is used."),
155                         tostr(cmap[closest_pixel]), tostr(closest_pixel)) << endl;
156
157                 val.foreground = cmap[closest_pixel].pixel;
158         }
159         val.function = GXcopy;
160         return XCreateGC(display, drawable,
161                                            GCForeground | GCFunction, &val);
162 }
163
164 // Gets GC according to color
165 // Uses caching
166 GC LyXColorHandler::getGCForeground(LColor_color c)
167 {
168         if (static_cast<unsigned>(c) >= colorGCcache.size()) {
169                 colorGCcache.resize(c + 1, 0);
170         }
171
172         if (colorGCcache[c] != 0) {
173                 return colorGCcache[c];
174         }
175         XColor xcol;
176         XColor ccol;
177         string const s = lcolor.getX11Name(c);
178         // Look up the RGB values for the color, and an approximate
179         // color that we can hope to get on this display.
180         if (XLookupColor(display, colormap, s.c_str(), &xcol, &ccol) == 0) {
181                 lyxerr << bformat(
182                         _("LyX: Unknown X11 color %1$s for %2$s\n"),
183                         s, lcolor.getGUIName(c)) << endl;
184         }
185         return colorGCcache[c] = getGCForeground(s);
186 }
187
188
189 // Gets GC for line
190 GC LyXColorHandler::getGCLinepars(Painter::line_style ls,
191                                   Painter::line_width lw, LColor_color c)
192 {
193         //if (lyxerr.debugging()) {
194         //      lyxerr << "Painter drawable: " << drawable() << endl;
195         //}
196
197         int index = lw + (ls << 1) + (c << 6);
198
199         LineGCCache::iterator it = lineGCcache.find(index);
200         if (it != lineGCcache.end())
201                 return it->second;
202
203         XGCValues val;
204         XGetGCValues(display, getGCForeground(c), GCForeground, &val);
205
206         switch (lw) {
207         case Painter::line_thin:
208                 val.line_width = 0;
209                 break;
210         case Painter::line_thick:
211                 val.line_width = 2;
212                 break;
213         }
214
215         switch (ls) {
216         case Painter::line_solid:
217                 val.line_style = LineSolid;
218                 break;
219         case Painter::line_onoffdash:
220                 val.line_style = LineOnOffDash;
221                 break;
222         }
223
224
225         val.cap_style = CapRound;
226         val.join_style = JoinRound;
227         val.function = GXcopy;
228
229         return lineGCcache[index] =
230                 XCreateGC(display, drawable,
231                           GCForeground | GCLineStyle | GCLineWidth |
232                           GCCapStyle | GCJoinStyle | GCFunction, &val);
233 }
234
235
236 // update GC cache after color redefinition
237 void LyXColorHandler::updateColor (LColor_color c)
238 {
239         // color GC cache
240         GC gc = colorGCcache[c];
241         if (gc != 0) {
242                 XFreeGC(display, gc);
243                 colorGCcache[c] = NULL;
244                 getGCForeground(c);
245         }
246
247         // line GC cache
248
249         for (int ls = 0; ls < 3; ++ls)
250                 for (int lw = 0; lw < 2; ++lw) {
251                         int const index = lw + (ls << 1) + (c << 6);
252                         LineGCCache::iterator it = lineGCcache.find(index);
253                         if (it != lineGCcache.end()) {
254                                 gc = it->second;
255                                 XFreeGC(display, gc);
256                                 lineGCcache.erase(it);
257                                 getGCLinepars(Painter::line_style(ls),
258                                               Painter::line_width(lw), c);
259                         }
260                 }
261 }
262
263
264 //
265 boost::scoped_ptr<LyXColorHandler> lyxColorHandler;