2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1998-2000 The LyX Team
9 *======================================================*/
14 #pragma implementation
23 #include FORMS_H_LOCATION
28 #include "support/LAssert.h"
29 #include "support/lstrings.h"
36 Painter::Painter(WorkArea & wa)
39 colormap = fl_state[fl_get_vclass()].colormap;
41 for (int i = 0; i <= LColor::ignore; ++i) {
48 // Release all the registered GCs
49 for (int i = 0; i <= LColor::ignore; ++i) {
50 if (colorGCcache[i] != 0) {
51 XFreeGC(display, colorGCcache[i]);
54 // Iterate over the line cache and Free the GCs
55 for (LineGCCache::iterator lit = lineGCcache.begin();
56 lit != lineGCcache.end(); ++lit) {
57 XFreeGC(display, (*lit).second);
62 Drawable Painter::drawable() const
64 return owner.getPixmap();
68 /* Basic drawing routines */
70 extern bool Lgb_bug_find_hack;
72 PainterBase & Painter::point(int x, int y, LColor::color c)
74 if (lyxerr.debugging()) {
75 if (!Lgb_bug_find_hack)
76 lyxerr << "point not called from "
77 "workarea::workhandler\n";
78 lyxerr.debug() << "Painter drawable: " << drawable() << endl;
81 XDrawPoint(display, drawable(), getGCForeground(c), x, y);
86 PainterBase & Painter::line(int x1, int y1, int x2, int y2,
91 if (lyxerr.debugging()) {
92 if (!Lgb_bug_find_hack)
93 lyxerr << "line not called from "
94 "workarea::workhandler\n";
95 lyxerr.debug() << "Painter drawable: " << drawable() << endl;
98 XDrawLine(display, drawable(),
99 getGCLinepars(ls, lw, col), x1, y1, x2, y2);
104 PainterBase & Painter::lines(int const * xp, int const * yp, int np,
109 if (lyxerr.debugging()) {
110 if (!Lgb_bug_find_hack)
111 lyxerr << "lines not called from "
112 "workarea::workhandler\n";
113 lyxerr.debug() << "Painter drawable: " << drawable() << endl;
116 #ifndef HAVE_AUTO_PTR
117 XPoint * points = new XPoint[np];
119 auto_ptr<XPoint> points(new Xpoint[np]);
121 for (int i = 0; i < np; ++i) {
126 XDrawLines(display, drawable(), getGCLinepars(ls, lw, col),
127 points, np, CoordModeOrigin);
129 #ifndef HAVE_AUTO_PTR
136 PainterBase & Painter::rectangle(int x, int y, int w, int h,
141 if (lyxerr.debugging()) {
142 if (!Lgb_bug_find_hack)
143 lyxerr << "rectangle not called from "
144 "workarea::workhandler\n";
145 lyxerr << "Painter drawable: " << drawable() << endl;
148 XDrawRectangle(display, drawable(), getGCLinepars(ls, lw, col),
154 PainterBase & Painter::fillRectangle(int x, int y, int w, int h,
157 if (lyxerr.debugging()) {
158 if (!Lgb_bug_find_hack)
159 lyxerr << "fillrectangle not called from "
160 "workarea::workhandler\n";
161 lyxerr << "Painter drawable: " << drawable() << endl;
164 XFillRectangle(display, drawable(), getGCForeground(col), x, y, w, h);
169 PainterBase & Painter::fillPolygon(int const * xp, int const * yp, int np,
172 if (lyxerr.debugging()) {
173 if (!Lgb_bug_find_hack)
174 lyxerr <<"fillpolygon not called from "
175 "workarea::workhandler\n";
176 lyxerr << "Painter drawable: " << drawable() << endl;
179 #ifndef HAVE_AUTO_PTR
180 XPoint * points = new XPoint[np];
182 auto_ptr<XPoint> points(new XPoint[np]);
184 for (int i=0; i < np; ++i) {
189 XFillPolygon(display, drawable(), getGCForeground(col), points, np,
190 Nonconvex, CoordModeOrigin);
191 #ifndef HAVE_AUTO_PTR
198 PainterBase & Painter::arc(int x, int y,
199 unsigned int w, unsigned int h,
200 int a1, int a2, LColor::color col)
202 if (lyxerr.debugging()) {
203 if (!Lgb_bug_find_hack)
204 lyxerr << "arc not called from "
205 "workarea::workhandler\n";
206 lyxerr << "Painter drawable: " << drawable() << endl;
209 XDrawArc(display, drawable(), getGCForeground(col),
215 /// Draw lines from x1,y1 to x2,y2. They are arrays
216 PainterBase & Painter::segments(int const * x1, int const * y1,
217 int const * x2, int const * y2, int ns,
219 enum line_style ls, enum line_width lw)
221 if (lyxerr.debugging()) {
222 if (!Lgb_bug_find_hack)
223 lyxerr << "segments not called from "
224 "workarea::workhandler\n";
225 lyxerr << "Painter drawable: " << drawable() << endl;
228 #ifndef HAVE_AUTO_PTR
229 XSegment * s= new XSegment[ns];
231 auto_ptr<XSegment> s(new XSegment[ns]);
233 for (int i=0; i<ns; ++i) {
239 XDrawSegments(display, drawable(), getGCLinepars(ls, lw, col), s, ns);
241 #ifndef HAVE_AUTO_PTR
248 PainterBase & Painter::pixmap(int x, int y, int w, int h, Pixmap bitmap)
250 if (lyxerr.debugging()) {
251 if (!Lgb_bug_find_hack)
252 lyxerr << "workAreaExpose not called from "
253 "workarea::workhandler\n";
254 lyxerr << "Painter drawable: " << drawable() << endl;
258 val.function = GXcopy;
259 GC gc = XCreateGC(display, drawable(),
261 XCopyArea(display, bitmap, drawable(), gc,
263 XFreeGC(display, gc);
268 PainterBase & Painter::text(int x, int y, string const & s, LyXFont const & f)
270 return text(x, y, s.c_str(), s.length(), f);
274 PainterBase & Painter::text(int x, int y, char c, LyXFont const & f)
276 char s[2] = { c, '\0' };
277 return text(x, y, s, 1, f);
281 PainterBase & Painter::text(int x, int y, char const * s, int ls,
284 if (lyxerr.debugging()) {
285 if (!Lgb_bug_find_hack)
286 lyxerr << "text not called from "
287 "workarea::workhandler\n";
288 lyxerr << "Painter drawable: " << drawable() << endl;
290 GC gc = getGCForeground(f.realColor());
291 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
292 lyxfont::XSetFont(display, gc, f);
293 XDrawString(display, drawable(), gc, x, y, s, ls);
295 LyXFont smallfont(f);
296 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
299 for(int i = 0; i < ls; ++i) {
301 if (islower(static_cast<unsigned char>(c))) {
303 lyxfont::XSetFont(display, gc, smallfont);
304 XDrawString(display, drawable(),
306 tmpx += lyxfont::XTextWidth(smallfont, &c, 1);
307 //tmpx += lyxfont::width(c, f);
309 lyxfont::XSetFont(display, gc, f);
310 XDrawString(display, drawable(),
312 tmpx += lyxfont::XTextWidth(f, &c, 1);
313 //tmpx += lyxfont::width(c, f);
317 underline(f, x, y, lyxfont::width(s, ls, f));
322 void Painter::underline(LyXFont const & f, int x, int y, int width)
324 // What about underbars?
325 if (f.underbar() == LyXFont::ON && f.latex() != LyXFont::ON) {
326 int below = max(lyxfont::maxDescent(f) / 2, 2);
327 int height = max((lyxfont::maxDescent(f) / 4) - 1, 1);
329 line(x, y + below, x + width, y + below, f.color());
331 fillRectangle(x, y + below, width, below + height,
337 // Gets GC according to color
339 GC Painter::getGCForeground(LColor::color c)
341 if (lyxerr.debugging()) {
342 lyxerr << "Painter drawable: " << drawable() << endl;
345 if (colorGCcache[c] != 0) return colorGCcache[c];
348 string s = lcolor.getX11Name(c);
351 // Look up the RGB values for the color, and an approximate
352 // color that we can hope to get on this display.
353 if (XLookupColor(display, colormap, s.c_str(), &xcol, &ccol) == 0) {
354 lyxerr << _("LyX: Unknown X11 color ") << s
355 << _(" for ") << lcolor.getGUIName(c) << '\n'
356 << _(" Using black instead, sorry!.") << endl;
357 unsigned long bla = BlackPixel(display,
358 DefaultScreen(display));
359 val.foreground = bla;
360 // Try the exact RGB values first, then the approximate.
361 } else if (XAllocColor(display, colormap, &xcol) != 0) {
362 if (lyxerr.debugging()) {
363 lyxerr << _("LyX: X11 color ") << s
364 << _(" allocated for ")
365 << lcolor.getGUIName(c) << endl;
367 val.foreground = xcol.pixel;
368 } else if (XAllocColor(display, colormap, &ccol)) {
369 lyxerr << _("LyX: Using approximated X11 color ") << s
370 << _(" allocated for ")
371 << lcolor.getGUIName(c) << endl;
372 val.foreground = xcol.pixel;
374 // Here we are traversing the current colormap to find
375 // the color closest to the one we want.
376 Visual * vi = DefaultVisual(display, DefaultScreen(display));
378 XColor * cmap = new XColor[vi->map_entries];
380 for(int i = 0; i < vi->map_entries; ++i) {
383 XQueryColors(display, colormap, cmap, vi->map_entries);
385 // Walk through the cmap and look for close colors.
386 int closest_pixel = 0;
387 double closest_distance = 1e20; // we want to minimize this
389 for(int t = 0; t < vi->map_entries; ++t) {
390 // The Euclidean distance between two points in
391 // a three-dimensional space, the RGB color-cube,
392 // is used as the distance measurement between two
395 // Since square-root is monotonous, we don't have to
396 // take the square-root to find the minimum, and thus
397 // we use the squared distance instead to be faster.
399 // If we want to get fancy, we could convert the RGB
400 // coordinates to a different color-cube, maybe HSV,
401 // but the RGB cube seems to work great. (Asger)
402 distance = pow(cmap[t].red - xcol.red, 2.0) +
403 pow(cmap[t].green - xcol.green, 2.0) +
404 pow(cmap[t].blue - xcol.blue, 2.0);
405 if (distance < closest_distance) {
406 closest_distance = distance;
410 lyxerr << _("LyX: Couldn't allocate '") << s
411 << _("' for ") << lcolor.getGUIName(c)
412 << _(" with (r,g,b)=(")
413 << xcol.red << "," << xcol.green << ","
414 << xcol.blue << ").\n"
415 << _(" Using closest allocated "
416 "color with (r,g,b)=(")
417 << cmap[closest_pixel].red << ","
418 << cmap[closest_pixel].green << ","
419 << cmap[closest_pixel].blue << ") instead.\n"
420 << "Pixel [" << closest_pixel << "] is used." << endl;
421 val.foreground = cmap[closest_pixel].pixel;
425 val.function = GXcopy;
426 return colorGCcache[c] = XCreateGC(display, drawable(),
427 GCForeground | GCFunction, &val);
432 GC Painter::getGCLinepars(enum line_style ls,
433 enum line_width lw, LColor::color c)
435 if (lyxerr.debugging()) {
436 lyxerr << "Painter drawable: " << drawable() << endl;
439 int index = lw + (ls << 1) + (c << 3);
441 if (lineGCcache.find(index) != lineGCcache.end())
442 return lineGCcache[index];
445 XGetGCValues(display, getGCForeground(c), GCForeground, &val);
448 case line_thin: val.line_width = 0; break;
449 case line_thick: val.line_width = 2; break;
453 case line_solid: val.line_style = LineSolid; break;
454 case line_onoffdash: val.line_style = LineOnOffDash; break;
455 case line_doubledash: val.line_style = LineDoubleDash; break;
459 val.cap_style = CapRound;
460 val.join_style = JoinRound;
461 val.function = GXcopy;
463 return lineGCcache[index] =
464 XCreateGC(display, drawable(),
465 GCForeground | GCLineStyle | GCLineWidth |
466 GCCapStyle | GCJoinStyle | GCFunction, &val);