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, 0);
328 fillRectangle(x, y + below, width, below + height, f.color());
333 // Gets GC according to color
335 GC Painter::getGCForeground(LColor::color c)
337 if (lyxerr.debugging()) {
338 lyxerr << "Painter drawable: " << drawable() << endl;
341 if (colorGCcache[c] != 0) return colorGCcache[c];
344 string s = lcolor.getX11Name(c);
347 // Look up the RGB values for the color, and an approximate
348 // color that we can hope to get on this display.
349 if (XLookupColor(display, colormap, s.c_str(), &xcol, &ccol) == 0) {
350 lyxerr << _("LyX: Unknown X11 color ") << s
351 << _(" for ") << lcolor.getGUIName(c) << '\n'
352 << _(" Using black instead, sorry!.") << endl;
353 unsigned long bla = BlackPixel(display,
354 DefaultScreen(display));
355 val.foreground = bla;
356 // Try the exact RGB values first, then the approximate.
357 } else if (XAllocColor(display, colormap, &xcol) != 0) {
358 if (lyxerr.debugging()) {
359 lyxerr << _("LyX: X11 color ") << s
360 << _(" allocated for ")
361 << lcolor.getGUIName(c) << endl;
363 val.foreground = xcol.pixel;
364 } else if (XAllocColor(display, colormap, &ccol)) {
365 lyxerr << _("LyX: Using approximated X11 color ") << s
366 << _(" allocated for ")
367 << lcolor.getGUIName(c) << endl;
368 val.foreground = xcol.pixel;
370 // Here we are traversing the current colormap to find
371 // the color closest to the one we want.
372 Visual * vi = DefaultVisual(display, DefaultScreen(display));
374 XColor * cmap = new XColor[vi->map_entries];
376 for(int i = 0; i < vi->map_entries; ++i) {
379 XQueryColors(display, colormap, cmap, vi->map_entries);
381 // Walk through the cmap and look for close colors.
382 int closest_pixel = 0;
383 double closest_distance = 1e20; // we want to minimize this
385 for(int t = 0; t < vi->map_entries; ++t) {
386 // The Euclidean distance between two points in
387 // a three-dimensional space, the RGB color-cube,
388 // is used as the distance measurement between two
391 // Since square-root is monotonous, we don't have to
392 // take the square-root to find the minimum, and thus
393 // we use the squared distance instead to be faster.
395 // If we want to get fancy, we could convert the RGB
396 // coordinates to a different color-cube, maybe HSV,
397 // but the RGB cube seems to work great. (Asger)
398 distance = pow(cmap[t].red - xcol.red, 2.0) +
399 pow(cmap[t].green - xcol.green, 2.0) +
400 pow(cmap[t].blue - xcol.blue, 2.0);
401 if (distance < closest_distance) {
402 closest_distance = distance;
406 lyxerr << _("LyX: Couldn't allocate '") << s
407 << _("' for ") << lcolor.getGUIName(c)
408 << _(" with (r,g,b)=(")
409 << xcol.red << "," << xcol.green << ","
410 << xcol.blue << ").\n"
411 << _(" Using closest allocated "
412 "color with (r,g,b)=(")
413 << cmap[closest_pixel].red << ","
414 << cmap[closest_pixel].green << ","
415 << cmap[closest_pixel].blue << ") instead.\n"
416 << "Pixel [" << closest_pixel << "] is used." << endl;
417 val.foreground = cmap[closest_pixel].pixel;
421 val.function = GXcopy;
422 return colorGCcache[c] = XCreateGC(display, drawable(),
423 GCForeground | GCFunction, &val);
428 GC Painter::getGCLinepars(enum line_style ls,
429 enum line_width lw, LColor::color c)
431 if (lyxerr.debugging()) {
432 lyxerr << "Painter drawable: " << drawable() << endl;
435 int index = lw + (ls << 1) + (c << 3);
437 if (lineGCcache.find(index) != lineGCcache.end())
438 return lineGCcache[index];
441 XGetGCValues(display, getGCForeground(c), GCForeground, &val);
444 case line_thin: val.line_width = 0; break;
445 case line_thick: val.line_width = 2; break;
449 case line_solid: val.line_style = LineSolid; break;
450 case line_onoffdash: val.line_style = LineOnOffDash; break;
451 case line_doubledash: val.line_style = LineDoubleDash; break;
455 val.cap_style = CapRound;
456 val.join_style = JoinRound;
457 val.function = GXcopy;
459 return lineGCcache[index] =
460 XCreateGC(display, drawable(),
461 GCForeground | GCLineStyle | GCLineWidth |
462 GCCapStyle | GCJoinStyle | GCFunction, &val);