]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XPainter.C
Really dull and boring header shit
[lyx.git] / src / frontends / xforms / XPainter.C
1 /**
2  * \file XPainter.C
3  * Read the file COPYING
4  *
5  * \author unknown
6  * \author John Levon 
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "XPainter.h"
18 #include "LString.h"
19 #include "debug.h"
20 #include "XWorkArea.h"
21 #include "xfont_metrics.h"
22 #include "ColorHandler.h"
23 #include "lyxrc.h"
24 #include "encoding.h"
25 #include "language.h"
26
27 #ifdef USE_XFORMS_IMAGE_LOADER
28 #include "xformsImage.h"
29 #else
30 #include "graphics/GraphicsImageXPM.h"
31 #endif
32
33 #include "support/LAssert.h"
34 #include "support/lstrings.h"
35
36 #include <boost/scoped_array.hpp>
37
38 #include <cmath>
39
40 using std::endl;
41 using std::max;
42
43  
44 XPainter::XPainter(XWorkArea & xwa)
45         : Painter(), owner_(xwa)
46 {
47 }
48
49
50 int XPainter::paperWidth() const
51 {
52         return owner_.workWidth();
53 }
54
55
56 int XPainter::paperHeight() const
57 {
58         return owner_.workHeight();
59 }
60
61  
62 Painter & XPainter::point(int x, int y, LColor::color c)
63 {
64         XDrawPoint(fl_get_display(), owner_.getPixmap(),
65                 lyxColorHandler->getGCForeground(c), x, y);
66         return *this;
67 }
68
69
70 Painter & XPainter::line(int x1, int y1, 
71         int x2, int y2,
72         LColor::color col,
73         line_style ls,
74         line_width lw)
75 {
76         XDrawLine(fl_get_display(), owner_.getPixmap(), 
77                 lyxColorHandler->getGCLinepars(ls, lw, col),
78                 x1, y1, x2, y2);
79         return *this;
80 }
81
82
83 Painter & XPainter::lines(int const * xp, int const * yp, 
84         int np,
85         LColor::color col,
86         line_style ls,
87         line_width lw)
88 {
89         boost::scoped_array<XPoint> points(new XPoint[np]);
90
91         for (int i = 0; i < np; ++i) {
92                 points[i].x = xp[i];
93                 points[i].y = yp[i];
94         }
95
96         XDrawLines(fl_get_display(), owner_.getPixmap(),
97                 lyxColorHandler->getGCLinepars(ls, lw, col), 
98                 points.get(), np, CoordModeOrigin);
99
100         return *this;
101 }      
102
103
104 Painter & XPainter::rectangle(int x, int y, 
105         int w, int h,
106         LColor::color col,
107         line_style ls,
108         line_width lw)
109 {
110         XDrawRectangle(fl_get_display(), owner_.getPixmap(),
111                 lyxColorHandler->getGCLinepars(ls, lw, col), 
112                 x, y, w, h);
113         return *this;
114 }
115
116
117 Painter & XPainter::fillRectangle(int x, int y, 
118         int w, int h,
119         LColor::color col)
120 {
121         XFillRectangle(fl_get_display(), owner_.getPixmap(),
122                 lyxColorHandler->getGCForeground(col), x, y, w, h);
123         return *this;
124 }
125
126
127 Painter & XPainter::fillPolygon(int const * xp, int const * yp, 
128         int np, LColor::color col)
129 {
130         boost::scoped_array<XPoint> points(new XPoint[np]);
131
132         for (int i = 0; i < np; ++i) {
133                 points[i].x = xp[i];
134                 points[i].y = yp[i];
135         }
136  
137         XFillPolygon(fl_get_display(), owner_.getPixmap(),
138                 lyxColorHandler->getGCForeground(col), points.get(), 
139                 np, Nonconvex, CoordModeOrigin);
140  
141         return *this;
142 }
143
144  
145 Painter & XPainter::arc(int x, int y,
146         unsigned int w, unsigned int h,
147         int a1, int a2, LColor::color col)
148 {
149         XDrawArc(fl_get_display(), owner_.getPixmap(),
150                 lyxColorHandler->getGCForeground(col),
151                 x, y, w, h, a1, a2);
152         return *this;
153 }
154
155  
156 Painter & XPainter::image(int x, int y, 
157         int w, int h,
158         grfx::Image const & i)
159 {
160 #ifdef USE_XFORMS_IMAGE_LOADER
161         grfx::xformsImage const & image = static_cast<grfx::xformsImage const &>(i);
162 #else
163         grfx::ImageXPM const & image = static_cast<grfx::ImageXPM const &>(i);
164 #endif
165         
166         XGCValues val;
167         val.function = GXcopy;
168         GC gc = XCreateGC(fl_get_display(), owner_.getPixmap(),
169                 GCFunction, &val);
170         XCopyArea(fl_get_display(), image.getPixmap(), owner_.getPixmap(), 
171                 gc, 0, 0, w, h, x, y);
172         XFreeGC(fl_get_display(), gc);
173         return *this;
174 }
175
176
177 Painter & XPainter::text(int x, int y, 
178         string const & s, LyXFont const & f)
179 {
180         return text(x, y, s.data(), s.length(), f);
181 }
182
183
184 Painter & XPainter::text(int x, int y, 
185         char c, LyXFont const & f)
186 {
187         char s[2] = { c, '\0' };
188         return text(x, y, s, 1, f);
189 }
190
191
192 Painter & XPainter::text(int x, int y, 
193         char const * s, size_t ls,
194         LyXFont const & f)
195 {
196         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
197                 boost::scoped_array<XChar2b> xs(new XChar2b[ls]);
198                 Encoding const * encoding = f.language()->encoding();
199                 LyXFont font(f);
200                 if (f.isSymbolFont()) {
201 #ifdef USE_UNICODE_FOR_SYMBOLS
202                         font.setFamily(LyXFont::ROMAN_FAMILY);
203                         font.setShape(LyXFont::UP_SHAPE);
204 #endif
205                         encoding = encodings.symbol_encoding();
206                 }
207                 for (size_t i = 0; i < ls; ++i) {
208                         Uchar c = encoding->ucs(s[i]);
209                         xs[i].byte1 = c >> 8;
210                         xs[i].byte2 = c & 0xff;
211                 }
212                 text(x, y, xs.get(), ls, font);
213                 return *this;
214         }
215
216         GC gc = lyxColorHandler->getGCForeground(f.realColor());
217         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
218                 xfont_metrics::XSetFont(fl_get_display(), gc, f);
219                 XDrawString(fl_get_display(), owner_.getPixmap(), gc, x, y, s, ls);
220         } else {
221                 LyXFont smallfont(f);
222                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
223                 int tmpx = x;
224                 for (size_t i = 0; i < ls; ++i) {
225                         char const c = uppercase(s[i]);
226                         if (c != s[i]) {
227                                 xfont_metrics::XSetFont(fl_get_display(), gc, smallfont);
228                                 XDrawString(fl_get_display(), owner_.getPixmap(), gc,
229                                         tmpx, y, &c, 1);
230                                 tmpx += xfont_metrics::XTextWidth(smallfont, &c, 1);
231                         } else {
232                                 xfont_metrics::XSetFont(fl_get_display(), gc, f);
233                                 XDrawString(fl_get_display(), owner_.getPixmap(), gc,
234                                         tmpx, y, &c, 1);
235                                 tmpx += xfont_metrics::XTextWidth(f, &c, 1);
236                         }
237                 }
238         }
239
240         if (f.underbar() == LyXFont::ON) {
241                 underline(f, x, y, font_metrics::width(s, ls, f));
242         }
243         
244         return *this;
245 }
246
247
248 Painter & XPainter::text(int x, int y, 
249         XChar2b const * s, size_t ls,
250         LyXFont const & f)
251 {
252         GC gc = lyxColorHandler->getGCForeground(f.realColor());
253         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
254                 xfont_metrics::XSetFont(fl_get_display(), gc, f);
255                 XDrawString16(fl_get_display(), owner_.getPixmap(), gc, x, y, s, ls);
256         } else {
257                 LyXFont smallfont(f);
258                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
259                 static XChar2b c;
260                 int tmpx = x;
261                 for (size_t i = 0; i < ls; ++i) {
262                         if (s[i].byte1)
263                                 c = s[i];
264                         else {
265                                 c.byte1 = s[i].byte1;
266                                 c.byte2 = uppercase(s[i].byte2);
267                         }
268                         if (c.byte2 != s[i].byte2) {
269                                 xfont_metrics::XSetFont(fl_get_display(), gc, smallfont);
270                                 XDrawString16(fl_get_display(), owner_.getPixmap(), gc,
271                                         tmpx, y, &c, 1);
272                                 tmpx += xfont_metrics::XTextWidth16(smallfont, &c, 1);
273                         } else {
274                                 xfont_metrics::XSetFont(fl_get_display(), gc, f);
275                                 XDrawString16(fl_get_display(), owner_.getPixmap(), gc,
276                                         tmpx, y, &c, 1);
277                                 tmpx += xfont_metrics::XTextWidth16(f, &c, 1);
278                         }
279                 }
280         }
281         
282         if (f.underbar() == LyXFont::ON) {
283                 underline(f, x, y, xfont_metrics::width(s, ls, f));
284         }
285         
286         return *this;
287 }