1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team
8 *======================================================*/
13 #pragma implementation
26 #include "frontends/GUIRunTime.h"
27 #include "support/LAssert.h"
28 #include "support/lstrings.h"
31 #include "ColorHandler.h"
36 #include "frontends/support/LyXImage.h"
41 Painter::Painter(WorkArea & wa)
44 display = GUIRunTime::x11Display();
48 /* Basic drawing routines */
50 extern bool Lgb_bug_find_hack;
52 PainterBase & Painter::point(int x, int y, LColor::color c)
54 if (lyxerr.debugging(Debug::GUI)) {
55 if (!Lgb_bug_find_hack)
56 lyxerr << "point not called from "
57 "workarea::workhandler\n";
58 lyxerr[Debug::INFO] << "Painter drawable: "
59 << owner.getPixmap() << endl;
62 XDrawPoint(display, owner.getPixmap(),
63 lyxColorHandler->getGCForeground(c), x, y);
68 PainterBase & Painter::line(int x1, int y1, int x2, int y2,
73 if (lyxerr.debugging(Debug::GUI)) {
74 if (!Lgb_bug_find_hack)
75 lyxerr << "line not called from "
76 "workarea::workhandler\n";
77 lyxerr[Debug::INFO] << "Painter drawable: "
78 << owner.getPixmap() << endl;
81 XDrawLine(display, owner.getPixmap(),
82 lyxColorHandler->getGCLinepars(ls, lw, col),
88 PainterBase & Painter::lines(int const * xp, int const * yp, int np,
93 if (lyxerr.debugging(Debug::GUI)) {
94 if (!Lgb_bug_find_hack)
95 lyxerr << "lines not called from "
96 "workarea::workhandler\n";
97 lyxerr[Debug::INFO] << "Painter drawable: "
98 << owner.getPixmap() << endl;
101 #ifndef HAVE_AUTO_PTR
102 XPoint * points = new XPoint[np];
104 auto_ptr<XPoint> points(new Xpoint[np]);
106 for (int i = 0; i < np; ++i) {
111 XDrawLines(display, owner.getPixmap(),
112 lyxColorHandler->getGCLinepars(ls, lw, col),
113 points, np, CoordModeOrigin);
115 #ifndef HAVE_AUTO_PTR
122 PainterBase & Painter::rectangle(int x, int y, int w, int h,
127 if (lyxerr.debugging(Debug::GUI)) {
128 if (!Lgb_bug_find_hack)
129 lyxerr << "rectangle not called from "
130 "workarea::workhandler\n";
131 lyxerr << "Painter drawable: "
132 << owner.getPixmap() << endl;
135 XDrawRectangle(display, owner.getPixmap(),
136 lyxColorHandler->getGCLinepars(ls, lw, col),
142 PainterBase & Painter::fillRectangle(int x, int y, int w, int h,
145 if (lyxerr.debugging(Debug::GUI)) {
146 if (!Lgb_bug_find_hack)
147 lyxerr << "fillrectangle not called from "
148 "workarea::workhandler\n";
149 lyxerr << "Painter drawable: "
150 << owner.getPixmap() << endl;
153 XFillRectangle(display, owner.getPixmap(),
154 lyxColorHandler->getGCForeground(col), x, y, w, h);
159 PainterBase & Painter::fillPolygon(int const * xp, int const * yp, int np,
162 if (lyxerr.debugging(Debug::GUI)) {
163 if (!Lgb_bug_find_hack)
164 lyxerr <<"fillpolygon not called from "
165 "workarea::workhandler\n";
166 lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
169 #ifndef HAVE_AUTO_PTR
170 XPoint * points = new XPoint[np];
172 auto_ptr<XPoint> points(new XPoint[np]);
174 for (int i=0; i < np; ++i) {
179 XFillPolygon(display, owner.getPixmap(),
180 lyxColorHandler->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(Debug::GUI)) {
194 if (!Lgb_bug_find_hack)
195 lyxerr << "arc not called from "
196 "workarea::workhandler\n";
197 lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
200 XDrawArc(display, owner.getPixmap(),
201 lyxColorHandler->getGCForeground(col),
207 /// Draw lines from x1,y1 to x2,y2. They are arrays
208 PainterBase & Painter::segments(int const * x1, int const * y1,
209 int const * x2, int const * y2, int ns,
211 enum line_style ls, enum line_width lw)
213 if (lyxerr.debugging(Debug::GUI)) {
214 if (!Lgb_bug_find_hack)
215 lyxerr << "segments not called from "
216 "workarea::workhandler\n";
217 lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
220 #ifndef HAVE_AUTO_PTR
221 XSegment * s= new XSegment[ns];
223 auto_ptr<XSegment> s(new XSegment[ns]);
225 for (int i=0; i<ns; ++i) {
231 XDrawSegments(display, owner.getPixmap(),
232 lyxColorHandler->getGCLinepars(ls, lw, col), s, ns);
234 #ifndef HAVE_AUTO_PTR
240 PainterBase & Painter::pixmap(int x, int y, int w, int h, Pixmap bitmap)
242 if (lyxerr.debugging(Debug::GUI)) {
243 if (!Lgb_bug_find_hack)
244 lyxerr << "workAreaExpose not called from "
245 "workarea::workhandler\n";
246 lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
250 val.function = GXcopy;
251 GC gc = XCreateGC(display, owner.getPixmap(),
253 XCopyArea(display, bitmap, owner.getPixmap(), gc,
255 XFreeGC(display, gc);
259 PainterBase & Painter::image(int x, int y, int w, int h, LyXImage const * image)
261 Pixmap bitmap = image->getPixmap();
263 return pixmap(x, y, w, h, bitmap);
267 PainterBase & Painter::text(int x, int y, string const & s, LyXFont const & f)
269 return text(x, y, s.data(), s.length(), f);
273 PainterBase & Painter::text(int x, int y, char c, LyXFont const & f)
275 char s[2] = { c, '\0' };
276 return text(x, y, s, 1, f);
280 PainterBase & Painter::text(int x, int y, char const * s, size_t ls,
283 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
284 XChar2b * xs = new XChar2b[ls];
285 Encoding const * encoding = f.language()->encoding();
287 if (f.family() == LyXFont::SYMBOL_FAMILY) {
288 #ifdef USE_UNICODE_FOR_SYMBOLS
289 font.setFamily(LyXFont::ROMAN_FAMILY);
290 font.setShape(LyXFont::UP_SHAPE);
292 encoding = encodings.symbol_encoding();
294 for (size_t i = 0; i < ls; ++i) {
295 Uchar c = encoding->ucs(s[i]);
296 xs[i].byte1 = c >> 8;
297 xs[i].byte2 = c & 0xff;
299 text(x , y, xs, ls, font);
304 if (lyxerr.debugging(Debug::GUI)) {
305 if (!Lgb_bug_find_hack)
306 lyxerr << "text not called from "
307 "workarea::workhandler\n";
308 lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
310 GC gc = lyxColorHandler->getGCForeground(f.realColor());
311 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
312 lyxfont::XSetFont(display, gc, f);
313 XDrawString(display, owner.getPixmap(), gc, x, y, s, ls);
315 LyXFont smallfont(f);
316 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
319 for (size_t i = 0; i < ls; ++i) {
321 if (islower(static_cast<unsigned char>(c))) {
323 lyxfont::XSetFont(display, gc, smallfont);
324 XDrawString(display, owner.getPixmap(),
326 tmpx += lyxfont::XTextWidth(smallfont, &c, 1);
328 lyxfont::XSetFont(display, gc, f);
329 XDrawString(display, owner.getPixmap(),
331 tmpx += lyxfont::XTextWidth(f, &c, 1);
336 if (f.underbar() == LyXFont::ON) {
337 underline(f, x, y, lyxfont::width(s, ls, f));
344 PainterBase & Painter::text(int x, int y, XChar2b const * s, int ls,
347 if (lyxerr.debugging(Debug::GUI)) {
348 if (!Lgb_bug_find_hack)
349 lyxerr << "text not called from "
350 "workarea::workhandler\n";
351 lyxerr << "Painter drawable: " << owner.getPixmap() << endl;
353 GC gc = lyxColorHandler->getGCForeground(f.realColor());
354 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
355 lyxfont::XSetFont(display, gc, f);
356 XDrawString16(display, owner.getPixmap(), gc, x, y, s, ls);
358 LyXFont smallfont(f);
359 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
362 for (int i = 0; i < ls; ++i) {
363 if (s[i].byte1 == 0 && islower(s[i].byte2)) {
364 c.byte2 = toupper(s[i].byte2);
365 lyxfont::XSetFont(display, gc, smallfont);
366 XDrawString16(display, owner.getPixmap(),
368 tmpx += lyxfont::XTextWidth16(smallfont, &c, 1);
370 lyxfont::XSetFont(display, gc, f);
371 XDrawString16(display, owner.getPixmap(),
372 gc, tmpx, y, &s[i], 1);
373 tmpx += lyxfont::XTextWidth16(f, const_cast<XChar2b *>(&s[i]), 1);
378 if (f.underbar() == LyXFont::ON) {
379 underline(f, x, y, lyxfont::width(s, ls, f));
386 void Painter::underline(LyXFont const & f, int x, int y, int width)
388 int below = max(lyxfont::maxDescent(f) / 2, 2);
389 int height = max((lyxfont::maxDescent(f) / 4) - 1, 1);
391 line(x, y + below, x + width, y + below, f.color());
393 fillRectangle(x, y + below, width, below + height,