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, 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,
253 XCopyArea(display, bitmap, drawable, gc,
255 XFreeGC(display, gc);
260 PainterBase & Painter::text(int x, int y, string const & s, LyXFont const & f)
262 if (lyxerr.debugging()) {
263 if (!Lgb_bug_find_hack)
264 lyxerr << "text not called from "
265 "workarea::workhandler\n";
266 lyxerr << "Painter drawable: " << drawable << endl;
269 GC gc = getGCForeground(f.realColor());
270 XSetFont(display, gc, f.getFontID());
271 XDrawString(display, drawable, gc, x, y, s.c_str(), s.length());
272 underline(f, x, y, this->width(s, f));
277 PainterBase & Painter::text(int x, int y, char const * s, int ls,
280 if (lyxerr.debugging()) {
281 if (!Lgb_bug_find_hack)
282 lyxerr << "text not called from "
283 "workarea::workhandler\n";
284 lyxerr << "Painter drawable: " << drawable << endl;
287 GC gc = getGCForeground(f.realColor());
288 XSetFont(display, gc, f.getFontID());
289 XDrawString(display, drawable, gc, x, y, s, ls);
290 underline(f, x, y, this->width(s, ls, f));
295 PainterBase & Painter::text(int x, int y, char c, LyXFont const & f)
297 if (lyxerr.debugging()) {
298 if (!Lgb_bug_find_hack)
299 lyxerr << "text not called from "
300 "workarea::workhandler\n";
301 lyxerr << "Painter drawable: " << drawable << endl;
304 GC gc = getGCForeground(f.realColor());
305 XSetFont(display, gc, f.getFontID());
307 s[0] = c; s[1] = '\0';
308 XDrawString(display, drawable, gc, x, y, s, 1);
309 underline(f, x, y, this->width(c, f));
314 void Painter::underline(LyXFont const & f, int x, int y, int width) {
315 // What about underbars?
316 if (f.underbar() == LyXFont::ON && f.latex() != LyXFont::ON) {
317 int below = f.maxDescent() / 2;
318 if (below < 2) below = 2;
319 int height = (f.maxDescent() / 4) - 1;
320 if (height < 0) height = 0;
321 fillRectangle(x, y + below, width, below + height, f.color());
326 // Gets GC according to color
328 GC Painter::getGCForeground(LColor::color c)
330 if (lyxerr.debugging()) {
331 lyxerr << "Painter drawable: " << drawable << endl;
334 if (colorGCcache[c] != 0) return colorGCcache[c];
337 string s = lcolor.getX11Name(c);
340 // Look up the RGB values for the color, and an approximate
341 // color that we can hope to get on this display.
342 if (XLookupColor(display, colormap, s.c_str(), &xcol, &ccol) == 0) {
343 lyxerr << _("LyX: Unknown X11 color ") << s
344 << _(" for ") << lcolor.getGUIName(c) << '\n'
345 << _(" Using black instead, sorry!.") << endl;
346 unsigned long bla = BlackPixel(display,
347 DefaultScreen(display));
348 val.foreground = bla;
349 // Try the exact RGB values first, then the approximate.
350 } else if (XAllocColor(display, colormap, &xcol) != 0) {
351 lyxerr << _("LyX: X11 color ") << s
352 << _(" allocated for ")
353 << lcolor.getGUIName(c) << endl;
354 val.foreground = xcol.pixel;
355 } else if (XAllocColor(display, colormap, &ccol)) {
356 lyxerr << _("LyX: Using approximated X11 color ") << s
357 << _(" allocated for ")
358 << lcolor.getGUIName(c) << endl;
359 val.foreground = xcol.pixel;
361 // Here we are traversing the current colormap to find
362 // the color closest to the one we want.
363 Visual * vi = DefaultVisual(display, DefaultScreen(display));
365 XColor * cmap = new XColor[vi->map_entries];
367 for(int i = 0; i < vi->map_entries; ++i) {
370 XQueryColors(display, colormap, cmap, vi->map_entries);
372 // Walk through the cmap and look for close colors.
373 int closest_pixel = 0;
374 double closest_distance = 1e20; // we want to minimize this
376 for(int t = 0; t < vi->map_entries; ++t) {
377 // The Euclidean distance between two points in
378 // a three-dimensional space, the RGB color-cube,
379 // is used as the distance measurement between two
382 // Since square-root is monotonous, we don't have to
383 // take the square-root to find the minimum, and thus
384 // we use the squared distance instead to be faster.
386 // If we want to get fancy, we could convert the RGB
387 // coordinates to a different color-cube, maybe HSV,
388 // but the RGB cube seems to work great. (Asger)
389 distance = pow(cmap[t].red - xcol.red, 2.0) +
390 pow(cmap[t].green - xcol.green, 2.0) +
391 pow(cmap[t].blue - xcol.blue, 2.0);
392 if (distance < closest_distance) {
393 closest_distance = distance;
397 lyxerr << _("LyX: Couldn't allocate '") << s
398 << _("' for ") << lcolor.getGUIName(c)
399 << _(" with (r,g,b)=(")
400 << xcol.red << "," << xcol.green << ","
401 << xcol.blue << ").\n"
402 << _(" Using closest allocated "
403 "color with (r,g,b)=(")
404 << cmap[closest_pixel].red << ","
405 << cmap[closest_pixel].green << ","
406 << cmap[closest_pixel].blue << ") instead.\n"
407 << "Pixel [" << closest_pixel << "] is used." << endl;
408 val.foreground = cmap[closest_pixel].pixel;
412 val.function = GXcopy;
413 return colorGCcache[c] = XCreateGC(display, drawable,
414 GCForeground | GCFunction, &val);
419 GC Painter::getGCLinepars(enum line_style ls,
420 enum line_width lw, LColor::color c)
422 if (lyxerr.debugging()) {
423 lyxerr << "Painter drawable: " << drawable << endl;
426 int index = lw + (ls << 1) + (c << 3);
428 if (lineGCcache.find(index) != lineGCcache.end())
429 return lineGCcache[index];
432 XGetGCValues(display, getGCForeground(c), GCForeground, &val);
435 case line_thin: val.line_width = 0; break;
436 case line_thick: val.line_width = 2; break;
440 case line_solid: val.line_style = LineSolid; break;
441 case line_onoffdash: val.line_style = LineOnOffDash; break;
442 case line_doubledash: val.line_style = LineDoubleDash; break;
446 val.cap_style = CapRound;
447 val.join_style = JoinRound;
448 val.function = GXcopy;
450 return lineGCcache[index] =
451 XCreateGC(display, drawable,
452 GCForeground | GCLineStyle | GCLineWidth |
453 GCCapStyle | GCJoinStyle | GCFunction, &val);