2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1998-2000 The LyX Team
9 *======================================================*/
16 #pragma implementation
25 #include FORMS_H_LOCATION
30 #include "support/LAssert.h"
31 #include "support/lstrings.h"
33 Painter::Painter(WorkArea & wa)
36 colormap = fl_state[fl_get_vclass()].colormap;
38 for (int i=0; i <= LColor::ignore; ++i) {
45 // Release all the registered GCs
46 for (int i=0; i <= LColor::ignore; ++i) {
47 if (colorGCcache[i] != 0) {
48 XFreeGC(display, colorGCcache[i]);
51 // Iterate over the line cache and Free the GCs
52 LineGCCache::iterator lit = lineGCcache.begin();
53 for (; lit != lineGCcache.end(); ++lit) {
54 XFreeGC(display, (*lit).second);
59 /* Basic drawing routines */
61 extern bool Lgb_bug_find_hack;
63 PainterBase & Painter::point(int x, int y, LColor::color c)
65 if (lyxerr.debugging()) {
66 if (!Lgb_bug_find_hack)
67 lyxerr << "point not called from "
68 "workarea::workhandler\n";
69 lyxerr.debug() << "Painter drawable: " << drawable << endl;
72 XDrawPoint(display, drawable, getGCForeground(c), x, y);
77 PainterBase & Painter::line(int x1, int y1, int x2, int y2,
82 if (lyxerr.debugging()) {
83 if (!Lgb_bug_find_hack)
84 lyxerr << "line not called from "
85 "workarea::workhandler\n";
86 lyxerr.debug() << "Painter drawable: " << drawable << endl;
89 XDrawLine(display, drawable,
90 getGCLinepars(ls, lw, col), x1, y1, x2, y2);
95 PainterBase & Painter::lines(int const * xp, int const * yp, int np,
100 if (lyxerr.debugging()) {
101 if (!Lgb_bug_find_hack)
102 lyxerr << "lines not called from "
103 "workarea::workhandler\n";
104 lyxerr.debug() << "Painter drawable: " << drawable << endl;
107 #ifndef HAVE_AUTO_PTR
108 XPoint * points = new XPoint[np];
110 auto_ptr<XPoint> points(new Xpoint[np]);
112 for (int i=0; i<np; ++i) {
117 XDrawLines(display, drawable, getGCLinepars(ls, lw, col),
118 points, np, CoordModeOrigin);
120 #ifndef HAVE_AUTO_PTR
127 PainterBase & Painter::rectangle(int x, int y, int w, int h,
132 if (lyxerr.debugging()) {
133 if (!Lgb_bug_find_hack)
134 lyxerr << "rectangle not called from "
135 "workarea::workhandler\n";
136 lyxerr << "Painter drawable: " << drawable << endl;
139 XDrawRectangle(display, drawable, getGCLinepars(ls, lw, col),
145 PainterBase & Painter::fillRectangle(int x, int y, int w, int h,
148 if (lyxerr.debugging()) {
149 if (!Lgb_bug_find_hack)
150 lyxerr << "fillrectangle not called from "
151 "workarea::workhandler\n";
152 lyxerr << "Painter drawable: " << drawable << endl;
155 XFillRectangle(display, drawable, getGCForeground(col), x, y, w, h);
160 PainterBase & Painter::fillPolygon(int const * xp, int const * yp, int np,
163 if (lyxerr.debugging()) {
164 if (!Lgb_bug_find_hack)
165 lyxerr <<"fillpolygon not called from "
166 "workarea::workhandler\n";
167 lyxerr << "Painter drawable: " << drawable << endl;
170 #ifndef HAVE_AUTO_PTR
171 XPoint * points = new XPoint[np];
173 auto_ptr<XPoint> points(new XPoint[np]);
175 for (int i=0; i<np; ++i) {
180 XFillPolygon(display, drawable, getGCForeground(col), points, np,
181 Nonconvex, CoordModeOrigin);
182 #ifndef HAVE_AUTO_PTR
189 PainterBase & Painter::arc(int x, int y,
190 unsigned int w, unsigned int h,
191 int a1, int a2, LColor::color col)
193 if (lyxerr.debugging()) {
194 if (!Lgb_bug_find_hack)
195 lyxerr << "arc not called from "
196 "workarea::workhandler\n";
197 lyxerr << "Painter drawable: " << drawable << endl;
200 XDrawArc(display, drawable, getGCForeground(col),
206 /// Draw lines from x1,y1 to x2,y2. They are arrays
207 PainterBase & Painter::segments(int const * x1, int const * y1,
208 int const * x2, int const * y2, int ns,
210 enum line_style ls, enum line_width lw)
212 if (lyxerr.debugging()) {
213 if (!Lgb_bug_find_hack)
214 lyxerr << "segments not called from "
215 "workarea::workhandler\n";
216 lyxerr << "Painter drawable: " << drawable << endl;
219 #ifndef HAVE_AUTO_PTR
220 XSegment * s= new XSegment[ns];
222 auto_ptr<XSegment> s(new XSegment[ns]);
224 for (int i=0; i<ns; ++i) {
230 XDrawSegments(display, drawable, getGCLinepars(ls, lw, col), s, ns);
232 #ifndef HAVE_AUTO_PTR
239 PainterBase & Painter::pixmap(int x, int y, int w, int h, Pixmap bitmap)
241 if (lyxerr.debugging()) {
242 if (!Lgb_bug_find_hack)
243 lyxerr << "workAreaExpose not called from "
244 "workarea::workhandler\n";
245 lyxerr << "Painter drawable: " << drawable << endl;
249 val.function = GXcopy;
250 GC gc = XCreateGC(display, drawable,
252 XCopyArea(display, bitmap, drawable, gc,
254 XFreeGC(display, gc);
259 PainterBase & Painter::text(int x, int y, string const & s, LyXFont const & f)
261 if (lyxerr.debugging()) {
262 if (!Lgb_bug_find_hack)
263 lyxerr << "text not called from "
264 "workarea::workhandler\n";
265 lyxerr << "Painter drawable: " << drawable << endl;
268 GC gc = getGCForeground(f.realColor());
269 XSetFont(display, gc, f.getFontID());
270 XDrawString(display, drawable, gc, x, y, s.c_str(), s.length());
271 underline(f, x, y, this->width(s, f));
276 PainterBase & Painter::text(int x, int y, char const * s, int ls,
279 if (lyxerr.debugging()) {
280 if (!Lgb_bug_find_hack)
281 lyxerr << "text not called from "
282 "workarea::workhandler\n";
283 lyxerr << "Painter drawable: " << drawable << endl;
286 GC gc = getGCForeground(f.realColor());
287 XSetFont(display, gc, f.getFontID());
288 XDrawString(display, drawable, gc, x, y, s, ls);
289 underline(f, x, y, this->width(s, ls, f));
294 PainterBase & Painter::text(int x, int y, char c, LyXFont const & f)
296 if (lyxerr.debugging()) {
297 if (!Lgb_bug_find_hack)
298 lyxerr << "text not called from "
299 "workarea::workhandler\n";
300 lyxerr << "Painter drawable: " << drawable << endl;
303 GC gc = getGCForeground(f.realColor());
304 XSetFont(display, gc, f.getFontID());
306 s[0] = c; s[1] = '\0';
307 XDrawString(display, drawable, gc, x, y, s, 1);
308 underline(f, x, y, this->width(c, f));
313 void Painter::underline(LyXFont const & f, int x, int y, int width) {
314 // What about underbars?
315 if (f.underbar() == LyXFont::ON && f.latex() != LyXFont::ON) {
316 int below = f.maxDescent() / 2;
317 if (below < 2) below = 2;
318 int height = (f.maxDescent() / 4) - 1;
319 if (height < 0) height = 0;
320 fillRectangle(x, y + below, width, below + height, f.color());
325 // Gets GC according to color
327 GC Painter::getGCForeground(LColor::color c)
329 if (lyxerr.debugging()) {
330 lyxerr << "Painter drawable: " << drawable << endl;
333 if (colorGCcache[c] != 0) return colorGCcache[c];
336 string s = lcolor.getX11Name(c);
339 // Look up the RGB values for the color, and an approximate
340 // color that we can hope to get on this display.
341 if (XLookupColor(display, colormap, s.c_str(), &xcol, &ccol) == 0) {
342 lyxerr << _("LyX: Unknown X11 color ") << s
343 << _(" for ") << lcolor.getGUIName(c) << '\n'
344 << _(" Using black instead, sorry!.") << endl;
345 unsigned long bla = BlackPixel(display,
346 DefaultScreen(display));
347 val.foreground = bla;
348 // Try the exact RGB values first, then the approximate.
349 } else if (XAllocColor(display, colormap, &xcol) != 0) {
350 lyxerr << _("LyX: X11 color ") << s
351 << _(" allocated for ")
352 << lcolor.getGUIName(c) << endl;
353 val.foreground = xcol.pixel;
354 } else if (XAllocColor(display, colormap, &ccol)) {
355 lyxerr << _("LyX: Using approximated X11 color ") << s
356 << _(" allocated for ")
357 << lcolor.getGUIName(c) << endl;
358 val.foreground = xcol.pixel;
360 // Here we are traversing the current colormap to find
361 // the color closest to the one we want.
362 Visual * vi = DefaultVisual(display, DefaultScreen(display));
364 XColor * cmap = new XColor[vi->map_entries];
366 for(int i = 0; i < vi->map_entries; ++i) {
369 XQueryColors(display, colormap, cmap, vi->map_entries);
371 // Walk through the cmap and look for close colors.
372 int closest_pixel = 0;
373 double closest_distance = 1e20; // we want to minimize this
375 for(int t = 0; t < vi->map_entries; ++t) {
376 // The Euclidean distance between two points in
377 // a three-dimensional space, the RGB color-cube,
378 // is used as the distance measurement between two
381 // Since square-root is monotonous, we don't have to
382 // take the square-root to find the minimum, and thus
383 // we use the squared distance instead to be faster.
385 // If we want to get fancy, we could convert the RGB
386 // coordinates to a different color-cube, maybe HSV,
387 // but the RGB cube seems to work great. (Asger)
388 distance = pow(cmap[t].red - xcol.red, 2.0) +
389 pow(cmap[t].green - xcol.green, 2.0) +
390 pow(cmap[t].blue - xcol.blue, 2.0);
391 if (distance < closest_distance) {
392 closest_distance = distance;
396 lyxerr << _("LyX: Couldn't allocate '") << s
397 << _("' for ") << lcolor.getGUIName(c)
398 << _(" with (r,g,b)=(")
399 << xcol.red << "," << xcol.green << ","
400 << xcol.blue << ").\n"
401 << _(" Using closest allocated "
402 "color with (r,g,b)=(")
403 << cmap[closest_pixel].red << ","
404 << cmap[closest_pixel].green << ","
405 << cmap[closest_pixel].blue << ") instead.\n"
406 << "Pixel [" << closest_pixel << "] is used." << endl;
407 val.foreground = cmap[closest_pixel].pixel;
411 val.function = GXcopy;
412 return colorGCcache[c] = XCreateGC(display, drawable,
413 GCForeground | GCFunction, &val);
418 GC Painter::getGCLinepars(enum line_style ls,
419 enum line_width lw, LColor::color c)
421 if (lyxerr.debugging()) {
422 lyxerr << "Painter drawable: " << drawable << endl;
425 int index = lw + (ls << 1) + (c << 3);
427 if (lineGCcache.find(index) != lineGCcache.end())
428 return lineGCcache[index];
431 XGetGCValues(display, getGCForeground(c), GCForeground, &val);
434 case line_thin: val.line_width = 0; break;
435 case line_thick: val.line_width = 2; break;
439 case line_solid: val.line_style = LineSolid; break;
440 case line_onoffdash: val.line_style = LineOnOffDash; break;
441 case line_doubledash: val.line_style = LineDoubleDash; break;
445 val.cap_style = CapRound;
446 val.join_style = JoinRound;
447 val.function = GXcopy;
449 return lineGCcache[index] =
450 XCreateGC(display, drawable,
451 GCForeground | GCLineStyle | GCLineWidth |
452 GCCapStyle | GCJoinStyle | GCFunction, &val);