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