2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1998-2001 The LyX Team
9 *======================================================*/
14 #pragma implementation
23 #include FORMS_H_LOCATION
28 #include "support/LAssert.h"
29 #include "support/lstrings.h"
32 #include "ColorHandler.h"
37 #include "frontends/support/LyXImage.h"
42 Painter::Painter(WorkArea & wa)
45 display = fl_get_display();
49 /* Basic drawing routines */
51 extern bool Lgb_bug_find_hack;
53 PainterBase & Painter::point(int x, int y, LColor::color c)
55 if (lyxerr.debugging(Debug::GUI)) {
56 if (!Lgb_bug_find_hack)
57 lyxerr << "point not called from "
58 "workarea::workhandler\n";
59 lyxerr.debug() << "Painter drawable: "
60 << owner.getPixmap() << endl;
63 XDrawPoint(display, owner.getPixmap(),
64 lyxColorHandler->getGCForeground(c), x, y);
69 PainterBase & Painter::line(int x1, int y1, int x2, int y2,
74 if (lyxerr.debugging(Debug::GUI)) {
75 if (!Lgb_bug_find_hack)
76 lyxerr << "line not called from "
77 "workarea::workhandler\n";
78 lyxerr.debug() << "Painter drawable: "
79 << owner.getPixmap() << endl;
82 XDrawLine(display, owner.getPixmap(),
83 lyxColorHandler->getGCLinepars(ls, lw, col),
89 PainterBase & Painter::lines(int const * xp, int const * yp, int np,
94 if (lyxerr.debugging(Debug::GUI)) {
95 if (!Lgb_bug_find_hack)
96 lyxerr << "lines not called from "
97 "workarea::workhandler\n";
98 lyxerr.debug() << "Painter drawable: "
99 << owner.getPixmap() << endl;
102 #ifndef HAVE_AUTO_PTR
103 XPoint * points = new XPoint[np];
105 auto_ptr<XPoint> points(new Xpoint[np]);
107 for (int i = 0; i < np; ++i) {
112 XDrawLines(display, owner.getPixmap(),
113 lyxColorHandler->getGCLinepars(ls, lw, col),
114 points, np, CoordModeOrigin);
116 #ifndef HAVE_AUTO_PTR
123 PainterBase & Painter::rectangle(int x, int y, int w, int h,
128 if (lyxerr.debugging(Debug::GUI)) {
129 if (!Lgb_bug_find_hack)
130 lyxerr << "rectangle not called from "
131 "workarea::workhandler\n";
132 lyxerr << "Painter drawable: "
133 << owner.getPixmap() << endl;
136 XDrawRectangle(display, owner.getPixmap(),
137 lyxColorHandler->getGCLinepars(ls, lw, col),
143 PainterBase & Painter::fillRectangle(int x, int y, int w, int h,
146 if (lyxerr.debugging(Debug::GUI)) {
147 if (!Lgb_bug_find_hack)
148 lyxerr << "fillrectangle not called from "
149 "workarea::workhandler\n";
150 lyxerr << "Painter drawable: "
151 << owner.getPixmap() << endl;
154 XFillRectangle(display, owner.getPixmap(),
155 lyxColorHandler->getGCForeground(col), x, y, w, h);
160 PainterBase & Painter::fillPolygon(int const * xp, int const * yp, int np,
163 if (lyxerr.debugging(Debug::GUI)) {
164 if (!Lgb_bug_find_hack)
165 lyxerr <<"fillpolygon not called from "
166 "workarea::workhandler\n";
167 lyxerr << "Painter drawable: " << owner.getPixmap() << 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, owner.getPixmap(),
181 lyxColorHandler->getGCForeground(col), points, np,
182 Nonconvex, CoordModeOrigin);
183 #ifndef HAVE_AUTO_PTR
190 PainterBase & Painter::arc(int x, int y,
191 unsigned int w, unsigned int h,
192 int a1, int a2, LColor::color col)
194 if (lyxerr.debugging(Debug::GUI)) {
195 if (!Lgb_bug_find_hack)
196 lyxerr << "arc not called from "
197 "workarea::workhandler\n";
198 lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
201 XDrawArc(display, owner.getPixmap(),
202 lyxColorHandler->getGCForeground(col),
208 /// Draw lines from x1,y1 to x2,y2. They are arrays
209 PainterBase & Painter::segments(int const * x1, int const * y1,
210 int const * x2, int const * y2, int ns,
212 enum line_style ls, enum line_width lw)
214 if (lyxerr.debugging(Debug::GUI)) {
215 if (!Lgb_bug_find_hack)
216 lyxerr << "segments not called from "
217 "workarea::workhandler\n";
218 lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
221 #ifndef HAVE_AUTO_PTR
222 XSegment * s= new XSegment[ns];
224 auto_ptr<XSegment> s(new XSegment[ns]);
226 for (int i=0; i<ns; ++i) {
232 XDrawSegments(display, owner.getPixmap(),
233 lyxColorHandler->getGCLinepars(ls, lw, col), s, ns);
235 #ifndef HAVE_AUTO_PTR
241 PainterBase & Painter::pixmap(int x, int y, int w, int h, Pixmap bitmap)
243 if (lyxerr.debugging(Debug::GUI)) {
244 if (!Lgb_bug_find_hack)
245 lyxerr << "workAreaExpose not called from "
246 "workarea::workhandler\n";
247 lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
251 val.function = GXcopy;
252 GC gc = XCreateGC(display, owner.getPixmap(),
254 XCopyArea(display, bitmap, owner.getPixmap(), gc,
256 XFreeGC(display, gc);
260 PainterBase & Painter::image(int x, int y, int w, int h, LyXImage const * image)
262 Pixmap bitmap = image->getPixmap();
264 return pixmap(x, y, w, h, bitmap);
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, size_t ls,
284 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
285 XChar2b * xs = new XChar2b[ls];
286 Encoding const * encoding = f.language()->encoding();
288 if (f.family() == LyXFont::SYMBOL_FAMILY) {
289 #ifdef USE_UNICODE_FOR_SYMBOLS
290 font.setFamily(LyXFont::ROMAN_FAMILY);
291 font.setShape(LyXFont::UP_SHAPE);
293 encoding = encodings.symbol_encoding();
295 for (size_t i = 0; i < ls; ++i) {
296 Uchar c = encoding->ucs(s[i]);
297 xs[i].byte1 = c >> 8;
298 xs[i].byte2 = c & 0xff;
300 text(x , y, xs, ls, font);
305 if (lyxerr.debugging(Debug::GUI)) {
306 if (!Lgb_bug_find_hack)
307 lyxerr << "text not called from "
308 "workarea::workhandler\n";
309 lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
311 GC gc = lyxColorHandler->getGCForeground(f.realColor());
312 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
313 lyxfont::XSetFont(display, gc, f);
314 XDrawString(display, owner.getPixmap(), gc, x, y, s, ls);
316 LyXFont smallfont(f);
317 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
320 for (size_t i = 0; i < ls; ++i) {
322 if (islower(static_cast<unsigned char>(c))) {
324 lyxfont::XSetFont(display, gc, smallfont);
325 XDrawString(display, owner.getPixmap(),
327 tmpx += lyxfont::XTextWidth(smallfont, &c, 1);
329 lyxfont::XSetFont(display, gc, f);
330 XDrawString(display, owner.getPixmap(),
332 tmpx += lyxfont::XTextWidth(f, &c, 1);
336 if (f.underbar() == LyXFont::ON && f.latex() != LyXFont::ON)
337 underline(f, x, y, lyxfont::width(s, ls, f));
342 PainterBase & Painter::text(int x, int y, XChar2b const * s, int ls,
345 if (lyxerr.debugging(Debug::GUI)) {
346 if (!Lgb_bug_find_hack)
347 lyxerr << "text not called from "
348 "workarea::workhandler\n";
349 lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
351 GC gc = lyxColorHandler->getGCForeground(f.realColor());
352 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
353 lyxfont::XSetFont(display, gc, f);
354 XDrawString16(display, owner.getPixmap(), gc, x, y, s, ls);
356 LyXFont smallfont(f);
357 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
358 static XChar2b c = {0, 0};
360 for (int i = 0; i < ls; ++i) {
361 if (s[i].byte1 == 0 && islower(s[i].byte2)) {
362 c.byte2 = toupper(s[i].byte2);
363 lyxfont::XSetFont(display, gc, smallfont);
364 XDrawString16(display, owner.getPixmap(),
366 tmpx += lyxfont::XTextWidth16(smallfont, &c, 1);
368 lyxfont::XSetFont(display, gc, f);
369 XDrawString16(display, owner.getPixmap(),
370 gc, tmpx, y, &s[i], 1);
371 tmpx += lyxfont::XTextWidth16(f, const_cast<XChar2b *>(&s[i]), 1);
375 if (f.underbar() == LyXFont::ON && f.latex() != LyXFont::ON)
376 underline(f, x, y, lyxfont::width(s, ls, f));
381 void Painter::underline(LyXFont const & f, int x, int y, int width)
383 int below = max(lyxfont::maxDescent(f) / 2, 2);
384 int height = max((lyxfont::maxDescent(f) / 4) - 1, 1);
386 line(x, y + below, x + width, y + below, f.color());
388 fillRectangle(x, y + below, width, below + height,