]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dcclient.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6f65e337 | 6 | // RCS-ID: $Id$ |
c801d85f KB |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "dcclient.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/dcclient.h" | |
6f65e337 | 16 | #include "wx/dcmemory.h" |
c801d85f KB |
17 | |
18 | //----------------------------------------------------------------------------- | |
19 | // local data | |
20 | //----------------------------------------------------------------------------- | |
21 | ||
22 | #include "bdiag.xbm" | |
23 | #include "fdiag.xbm" | |
24 | #include "cdiag.xbm" | |
25 | #include "horiz.xbm" | |
26 | #include "verti.xbm" | |
27 | #include "cross.xbm" | |
28 | #define num_hatches 6 | |
29 | ||
30 | static GdkPixmap *hatches[num_hatches]; | |
31 | static GdkPixmap **hatch_bitmap = NULL; | |
32 | ||
33 | //----------------------------------------------------------------------------- | |
34 | // constants | |
35 | //----------------------------------------------------------------------------- | |
36 | ||
37 | #define RAD2DEG 57.2957795131 | |
38 | ||
6f65e337 JS |
39 | //----------------------------------------------------------------------------- |
40 | // temporary implementation of the missing GDK function | |
41 | //----------------------------------------------------------------------------- | |
42 | #include "gdk/gdkprivate.h" | |
43 | void gdk_draw_bitmap (GdkDrawable *drawable, | |
44 | GdkGC *gc, | |
45 | GdkDrawable *src, | |
46 | gint xsrc, | |
47 | gint ysrc, | |
48 | gint xdest, | |
49 | gint ydest, | |
50 | gint width, | |
51 | gint height) | |
52 | { | |
53 | GdkWindowPrivate *drawable_private; | |
54 | GdkWindowPrivate *src_private; | |
55 | GdkGCPrivate *gc_private; | |
56 | ||
57 | g_return_if_fail (drawable != NULL); | |
58 | g_return_if_fail (src != NULL); | |
59 | g_return_if_fail (gc != NULL); | |
60 | ||
61 | drawable_private = (GdkWindowPrivate*) drawable; | |
62 | src_private = (GdkWindowPrivate*) src; | |
63 | if (drawable_private->destroyed || src_private->destroyed) | |
64 | return; | |
65 | gc_private = (GdkGCPrivate*) gc; | |
66 | ||
67 | if (width == -1) | |
68 | width = src_private->width; | |
69 | if (height == -1) | |
70 | height = src_private->height; | |
71 | ||
72 | XCopyPlane (drawable_private->xdisplay, | |
73 | src_private->xwindow, | |
74 | drawable_private->xwindow, | |
75 | gc_private->xgc, | |
76 | xsrc, ysrc, | |
77 | width, height, | |
78 | xdest, ydest, | |
79 | 1); | |
80 | } | |
81 | ||
c801d85f KB |
82 | //----------------------------------------------------------------------------- |
83 | // wxPaintDC | |
84 | //----------------------------------------------------------------------------- | |
85 | ||
86 | IMPLEMENT_DYNAMIC_CLASS(wxPaintDC,wxDC) | |
87 | ||
88 | wxPaintDC::wxPaintDC(void) | |
89 | { | |
e3e65dac RR |
90 | m_penGC = NULL; |
91 | m_brushGC = NULL; | |
92 | m_textGC = NULL; | |
93 | m_bgGC = NULL; | |
94 | m_cmap = NULL; | |
c801d85f KB |
95 | }; |
96 | ||
97 | wxPaintDC::wxPaintDC( wxWindow *window ) | |
98 | { | |
e3e65dac RR |
99 | m_penGC = NULL; |
100 | m_brushGC = NULL; | |
101 | m_textGC = NULL; | |
102 | m_bgGC = NULL; | |
103 | m_cmap = NULL; | |
104 | ||
c801d85f KB |
105 | if (!window) return; |
106 | GtkWidget *widget = window->m_wxwindow; | |
107 | if (!widget) return; | |
108 | m_window = widget->window; | |
109 | if (!m_window) return; | |
110 | if (window->m_wxwindow) | |
111 | m_cmap = gtk_widget_get_colormap( window->m_wxwindow ); | |
112 | else | |
113 | m_cmap = gtk_widget_get_colormap( window->m_widget ); | |
0180d5da RR |
114 | |
115 | m_isDrawable = TRUE; | |
116 | ||
c801d85f KB |
117 | SetUpDC(); |
118 | ||
119 | long x = 0; | |
120 | long y = 0; | |
121 | window->GetDrawingOffset( &x, &y ); | |
122 | SetInternalDeviceOrigin( -x, -y ); | |
123 | }; | |
124 | ||
125 | wxPaintDC::~wxPaintDC(void) | |
126 | { | |
127 | }; | |
128 | ||
129 | void wxPaintDC::FloodFill( long WXUNUSED(x1), long WXUNUSED(y1), | |
130 | wxColour *WXUNUSED(col), int WXUNUSED(style) ) | |
131 | { | |
132 | }; | |
133 | ||
134 | bool wxPaintDC::GetPixel( long WXUNUSED(x1), long WXUNUSED(y1), wxColour *WXUNUSED(col) ) const | |
135 | { | |
136 | return FALSE; | |
137 | }; | |
138 | ||
139 | void wxPaintDC::DrawLine( long x1, long y1, long x2, long y2 ) | |
140 | { | |
141 | if (!Ok()) return; | |
142 | ||
143 | if (m_pen.GetStyle() != wxTRANSPARENT) | |
144 | { | |
145 | gdk_draw_line( m_window, m_penGC, | |
146 | XLOG2DEV(x1), YLOG2DEV(y1), XLOG2DEV(x2), YLOG2DEV(y2) ); | |
147 | }; | |
148 | }; | |
149 | ||
150 | void wxPaintDC::CrossHair( long x, long y ) | |
151 | { | |
152 | if (!Ok()) return; | |
153 | ||
154 | if (m_pen.GetStyle() != wxTRANSPARENT) | |
155 | { | |
156 | int w = 0; | |
157 | int h = 0; | |
158 | GetSize( &w, &h ); | |
159 | long xx = XLOG2DEV(x); | |
160 | long yy = YLOG2DEV(y); | |
161 | gdk_draw_line( m_window, m_penGC, | |
162 | 0, yy, XLOG2DEVREL(w), yy ); | |
163 | gdk_draw_line( m_window, m_penGC, | |
164 | xx, 0, xx, YLOG2DEVREL(h) ); | |
165 | }; | |
166 | }; | |
167 | ||
168 | void wxPaintDC::DrawArc( long x1, long y1, long x2, long y2, double xc, double yc ) | |
169 | { | |
170 | if (!Ok()) return; | |
171 | ||
172 | long xx1 = XLOG2DEV(x1); | |
173 | long yy1 = YLOG2DEV(y1); | |
174 | long xx2 = XLOG2DEV(x2); | |
175 | long yy2 = YLOG2DEV(y2); | |
176 | long xxc = XLOG2DEV((long)xc); | |
177 | long yyc = YLOG2DEV((long)yc); | |
178 | double dx = xx1 - xxc; | |
179 | double dy = yy1 - yyc; | |
180 | double radius = sqrt(dx*dx+dy*dy); | |
181 | long r = (long)radius; | |
182 | double radius1, radius2; | |
183 | ||
184 | if (xx1 == xx2 && yy1 == yy2) | |
185 | { | |
186 | radius1 = 0.0; | |
187 | radius2 = 360.0; | |
188 | } | |
189 | else | |
190 | if (radius == 0.0) | |
191 | { | |
192 | radius1 = radius2 = 0.0; | |
193 | } | |
194 | else | |
195 | { | |
196 | radius1 = (xx1 - xxc == 0) ? | |
197 | (yy1 - yyc < 0) ? 90.0 : -90.0 : | |
198 | -atan2(double(yy1-yyc), double(xx1-xxc)) * RAD2DEG; | |
199 | radius2 = (xx2 - xxc == 0) ? | |
200 | (yy2 - yyc < 0) ? 90.0 : -90.0 : | |
201 | -atan2(double(yy2-yyc), double(xx2-xxc)) * RAD2DEG; | |
202 | }; | |
203 | long alpha1 = long(radius1 * 64.0); | |
204 | long alpha2 = long((radius2 - radius1) * 64.0); | |
205 | while (alpha2 <= 0) alpha2 += 360*64; | |
206 | while (alpha1 > 360*64) alpha1 -= 360*64; | |
207 | ||
208 | if (m_brush.GetStyle() != wxTRANSPARENT) | |
209 | gdk_draw_arc( m_window, m_brushGC, TRUE, xxc-r, yyc-r, 2*r,2*r, alpha1, alpha2 ); | |
210 | ||
211 | if (m_pen.GetStyle() != wxTRANSPARENT) | |
212 | gdk_draw_arc( m_window, m_penGC, FALSE, xxc-r, yyc-r, 2*r,2*r, alpha1, alpha2 ); | |
213 | ||
214 | }; | |
215 | ||
216 | void wxPaintDC::DrawEllipticArc( long x, long y, long width, long height, double sa, double ea ) | |
217 | { | |
218 | if (!Ok()) return; | |
219 | ||
c801d85f KB |
220 | long xx = XLOG2DEV(x); |
221 | long yy = YLOG2DEV(y); | |
6f65e337 JS |
222 | long ww = m_signX * XLOG2DEVREL(width); |
223 | long hh = m_signY * YLOG2DEVREL(height); | |
c801d85f | 224 | |
6f65e337 JS |
225 | // CMB: handle -ve width and/or height |
226 | if (ww < 0) { ww = -ww; xx = xx - ww; } | |
227 | if (hh < 0) { hh = -hh; yy = yy - hh; } | |
228 | ||
229 | long start = long(sa * 64.0); | |
230 | long end = long(ea * 64.0); | |
c801d85f | 231 | if (m_brush.GetStyle() != wxTRANSPARENT) |
6f65e337 | 232 | gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, ww, hh, start, end ); |
c801d85f KB |
233 | |
234 | if (m_pen.GetStyle() != wxTRANSPARENT) | |
6f65e337 | 235 | gdk_draw_arc( m_window, m_penGC, FALSE, xx, yy, ww, hh, start, end ); |
c801d85f KB |
236 | }; |
237 | ||
238 | void wxPaintDC::DrawPoint( long x, long y ) | |
239 | { | |
240 | if (!Ok()) return; | |
241 | ||
242 | if (m_pen.GetStyle() != wxTRANSPARENT) | |
243 | gdk_draw_point( m_window, m_penGC, XLOG2DEV(x), YLOG2DEV(y) ); | |
244 | }; | |
245 | ||
246 | void wxPaintDC::DrawLines( int n, wxPoint points[], long xoffset, long yoffset ) | |
247 | { | |
248 | if (!Ok()) return; | |
249 | ||
250 | if (m_pen.GetStyle() == wxTRANSPARENT) return; | |
251 | ||
252 | for (int i = 0; i < n-1; i++) | |
253 | { | |
254 | long x1 = XLOG2DEV(points[i].x + xoffset); | |
255 | long x2 = XLOG2DEV(points[i+1].x + xoffset); | |
256 | long y1 = YLOG2DEV(points[i].y + yoffset); // oh, what a waste | |
257 | long y2 = YLOG2DEV(points[i+1].y + yoffset); | |
4c681997 | 258 | gdk_draw_line( m_window, m_penGC, x1, y1, x2, y2 ); |
c801d85f KB |
259 | }; |
260 | }; | |
261 | ||
262 | void wxPaintDC::DrawLines( wxList *points, long xoffset, long yoffset ) | |
263 | { | |
264 | if (!Ok()) return; | |
265 | ||
266 | if (m_pen.GetStyle() == wxTRANSPARENT) return; | |
267 | ||
268 | wxNode *node = points->First(); | |
269 | while (node->Next()) | |
270 | { | |
271 | wxPoint *point = (wxPoint*)node->Data(); | |
272 | wxPoint *npoint = (wxPoint*)node->Next()->Data(); | |
273 | long x1 = XLOG2DEV(point->x + xoffset); | |
274 | long x2 = XLOG2DEV(npoint->x + xoffset); | |
275 | long y1 = YLOG2DEV(point->y + yoffset); // and again... | |
276 | long y2 = YLOG2DEV(npoint->y + yoffset); | |
4c681997 | 277 | gdk_draw_line( m_window, m_penGC, x1, y1, x2, y2 ); |
c801d85f KB |
278 | node = node->Next(); |
279 | }; | |
280 | }; | |
281 | ||
282 | void wxPaintDC::DrawPolygon( int WXUNUSED(n), wxPoint WXUNUSED(points)[], | |
283 | long WXUNUSED(xoffset), long WXUNUSED(yoffset), int WXUNUSED(fillStyle) ) | |
284 | { | |
285 | if (!Ok()) return; | |
286 | }; | |
287 | ||
288 | void wxPaintDC::DrawPolygon( wxList *WXUNUSED(lines), long WXUNUSED(xoffset), | |
289 | long WXUNUSED(yoffset), int WXUNUSED(fillStyle) ) | |
290 | { | |
291 | if (!Ok()) return; | |
292 | }; | |
293 | ||
294 | void wxPaintDC::DrawRectangle( long x, long y, long width, long height ) | |
295 | { | |
296 | if (!Ok()) return; | |
297 | ||
298 | long xx = XLOG2DEV(x); | |
299 | long yy = YLOG2DEV(y); | |
6f65e337 JS |
300 | long ww = m_signX * XLOG2DEVREL(width); |
301 | long hh = m_signY * YLOG2DEVREL(height); | |
c801d85f | 302 | |
6f65e337 JS |
303 | // CMB: draw nothing if transformed w or h is 0 |
304 | if (ww == 0 || hh == 0) return; | |
305 | ||
306 | // CMB: handle -ve width and/or height | |
307 | if (ww < 0) { ww = -ww; xx = xx - ww; } | |
308 | if (hh < 0) { hh = -hh; yy = yy - hh; } | |
309 | ||
c801d85f KB |
310 | if (m_brush.GetStyle() != wxTRANSPARENT) |
311 | gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy, ww, hh ); | |
312 | ||
313 | if (m_pen.GetStyle() != wxTRANSPARENT) | |
314 | gdk_draw_rectangle( m_window, m_penGC, FALSE, xx, yy, ww-1, hh-1 ); | |
315 | }; | |
316 | ||
317 | void wxPaintDC::DrawRoundedRectangle( long x, long y, long width, long height, double radius ) | |
318 | { | |
319 | if (!Ok()) return; | |
320 | ||
c801d85f KB |
321 | if (radius < 0.0) radius = - radius * ((width < height) ? width : height); |
322 | ||
323 | long xx = XLOG2DEV(x); | |
324 | long yy = YLOG2DEV(y); | |
6f65e337 JS |
325 | long ww = m_signX * XLOG2DEVREL(width); |
326 | long hh = m_signY * YLOG2DEVREL(height); | |
c801d85f | 327 | long rr = XLOG2DEVREL((long)radius); |
6f65e337 JS |
328 | |
329 | // CMB: handle -ve width and/or height | |
330 | if (ww < 0) { ww = -ww; xx = xx - ww; } | |
331 | if (hh < 0) { hh = -hh; yy = yy - hh; } | |
332 | ||
333 | // CMB: if radius is zero use DrawRectangle() instead to avoid | |
334 | // X drawing errors with small radii | |
335 | if (rr == 0) | |
336 | { | |
337 | DrawRectangle( x, y, width, height ); | |
338 | return; | |
339 | } | |
340 | ||
341 | // CMB: draw nothing if transformed w or h is 0 | |
342 | if (ww == 0 || hh == 0) return; | |
343 | ||
344 | // CMB: adjust size if outline is drawn otherwise the result is | |
345 | // 1 pixel too wide and high | |
346 | if (m_pen.GetStyle() != wxTRANSPARENT) | |
347 | { | |
348 | ww--; | |
349 | hh--; | |
350 | } | |
351 | ||
352 | // CMB: ensure dd is not larger than rectangle otherwise we | |
353 | // get an hour glass shape | |
c801d85f | 354 | long dd = 2 * rr; |
6f65e337 JS |
355 | if (dd > ww) dd = ww; |
356 | if (dd > hh) dd = hh; | |
357 | rr = dd / 2; | |
c801d85f KB |
358 | |
359 | if (m_brush.GetStyle() != wxTRANSPARENT) | |
360 | { | |
6f65e337 JS |
361 | gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx+rr, yy, ww-dd+1, hh ); |
362 | gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy+rr, ww, hh-dd+1 ); | |
c801d85f KB |
363 | gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, dd, dd, 90*64, 90*64 ); |
364 | gdk_draw_arc( m_window, m_brushGC, TRUE, xx+ww-dd, yy, dd, dd, 0, 90*64 ); | |
365 | gdk_draw_arc( m_window, m_brushGC, TRUE, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64 ); | |
366 | gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy+hh-dd, dd, dd, 180*64, 90*64 ); | |
367 | }; | |
368 | ||
369 | if (m_pen.GetStyle() != wxTRANSPARENT) | |
370 | { | |
371 | gdk_draw_line( m_window, m_penGC, xx+rr, yy, xx+ww-rr, yy ); | |
372 | gdk_draw_line( m_window, m_penGC, xx+rr, yy+hh, xx+ww-rr, yy+hh ); | |
373 | gdk_draw_line( m_window, m_penGC, xx, yy+rr, xx, yy+hh-rr ); | |
374 | gdk_draw_line( m_window, m_penGC, xx+ww, yy+rr, xx+ww, yy+hh-rr ); | |
375 | gdk_draw_arc( m_window, m_penGC, FALSE, xx, yy, dd, dd, 90*64, 90*64 ); | |
376 | gdk_draw_arc( m_window, m_penGC, FALSE, xx+ww-dd, yy, dd, dd, 0, 90*64 ); | |
377 | gdk_draw_arc( m_window, m_penGC, FALSE, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64 ); | |
378 | gdk_draw_arc( m_window, m_penGC, FALSE, xx, yy+hh-dd, dd, dd, 180*64, 90*64 ); | |
379 | }; | |
380 | }; | |
381 | ||
382 | void wxPaintDC::DrawEllipse( long x, long y, long width, long height ) | |
383 | { | |
384 | if (!Ok()) return; | |
385 | ||
c801d85f KB |
386 | long xx = XLOG2DEV(x); |
387 | long yy = YLOG2DEV(y); | |
6f65e337 JS |
388 | long ww = m_signX * XLOG2DEVREL(width); |
389 | long hh = m_signY * YLOG2DEVREL(height); | |
390 | ||
391 | // CMB: handle -ve width and/or height | |
392 | if (ww < 0) { ww = -ww; xx = xx - ww; } | |
393 | if (hh < 0) { hh = -hh; yy = yy - hh; } | |
c801d85f KB |
394 | |
395 | if (m_brush.GetStyle() != wxTRANSPARENT) | |
396 | gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, ww, hh, 0, 360*64 ); | |
397 | ||
398 | if (m_pen.GetStyle() != wxTRANSPARENT) | |
399 | gdk_draw_arc( m_window, m_penGC, FALSE, xx, yy, ww, hh, 0, 360*64 ); | |
400 | }; | |
401 | ||
402 | bool wxPaintDC::CanDrawBitmap(void) const | |
403 | { | |
404 | return TRUE; | |
405 | }; | |
406 | ||
407 | void wxPaintDC::DrawIcon( const wxIcon &icon, long x, long y, bool useMask ) | |
408 | { | |
409 | if (!Ok()) return; | |
410 | ||
411 | if (!icon.Ok()) return; | |
412 | ||
413 | int xx = XLOG2DEV(x); | |
414 | int yy = YLOG2DEV(y); | |
415 | ||
416 | GdkBitmap *mask = NULL; | |
417 | if (icon.GetMask()) mask = icon.GetMask()->GetBitmap(); | |
418 | ||
419 | if (useMask && mask) | |
420 | { | |
421 | gdk_gc_set_clip_mask( m_penGC, mask ); | |
422 | gdk_gc_set_clip_origin( m_penGC, xx, yy ); | |
423 | }; | |
424 | ||
425 | GdkPixmap *pm = icon.GetPixmap(); | |
426 | gdk_draw_pixmap( m_window, m_penGC, pm, 0, 0, xx, yy, -1, -1 ); | |
427 | ||
428 | if (useMask && mask) | |
429 | { | |
430 | gdk_gc_set_clip_mask( m_penGC, NULL ); | |
431 | gdk_gc_set_clip_origin( m_penGC, 0, 0 ); | |
432 | }; | |
433 | }; | |
434 | ||
435 | bool wxPaintDC::Blit( long xdest, long ydest, long width, long height, | |
436 | wxDC *source, long xsrc, long ysrc, int WXUNUSED(logical_func), bool WXUNUSED(useMask) ) | |
437 | { | |
438 | if (!Ok()) return FALSE; | |
439 | ||
6f65e337 JS |
440 | // CMB 20/5/98: add blitting of bitmaps |
441 | if (source->IsKindOf(CLASSINFO(wxMemoryDC))) | |
442 | { | |
443 | wxMemoryDC* srcDC = (wxMemoryDC*)source; | |
444 | GdkBitmap* bmap = srcDC->m_selected.GetBitmap(); | |
445 | if (bmap) | |
446 | { | |
447 | gdk_draw_bitmap ( | |
448 | m_window, | |
449 | m_textGC, | |
450 | bmap, | |
451 | source->DeviceToLogicalX(xsrc), source->DeviceToLogicalY(ysrc), | |
452 | XLOG2DEV(xdest), YLOG2DEV(ydest), | |
453 | source->DeviceToLogicalXRel(width), source->DeviceToLogicalYRel(height) | |
454 | ); | |
455 | return TRUE; | |
456 | } | |
457 | } | |
c801d85f | 458 | |
6f65e337 | 459 | wxClientDC *csrc = (wxClientDC*)source; |
c801d85f KB |
460 | gdk_window_copy_area ( m_window, m_penGC, |
461 | XLOG2DEV(xdest), YLOG2DEV(ydest), | |
462 | csrc->GetWindow(), | |
463 | source->DeviceToLogicalX(xsrc), source->DeviceToLogicalY(ysrc), | |
464 | source->DeviceToLogicalXRel(width), source->DeviceToLogicalYRel(height) ); | |
465 | ||
466 | /* | |
467 | gdk_window_copy_area ( m_window, m_penGC, | |
468 | XLOG2DEV(xdest), YLOG2DEV(ydest), | |
469 | csrc->GetWindow(), | |
470 | xsrc, ysrc, | |
471 | width, height ); | |
472 | */ | |
473 | ||
474 | return TRUE; | |
475 | }; | |
476 | ||
18a2fa37 KB |
477 | void wxPaintDC::DrawText( const wxString &text, long x, long y, bool |
478 | WXUNUSED(use16) ) | |
c801d85f KB |
479 | { |
480 | if (!Ok()) return; | |
18a2fa37 | 481 | |
c801d85f | 482 | GdkFont *font = m_font.GetInternalFont( m_scaleY ); |
6f65e337 | 483 | |
18a2fa37 KB |
484 | x = XLOG2DEV(x); |
485 | y = YLOG2DEV(y); | |
486 | ||
6f65e337 JS |
487 | // CMB 21/5/98: draw text background if mode is wxSOLID |
488 | if (m_backgroundMode == wxSOLID) | |
489 | { | |
490 | long width = gdk_string_width( font, text ); | |
491 | long height = font->ascent + font->descent; | |
492 | gdk_gc_set_foreground( m_textGC, m_textBackgroundColour.GetColor() ); | |
493 | gdk_draw_rectangle( m_window, m_textGC, TRUE, x, y, width, height ); | |
494 | gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() ); | |
495 | } | |
18a2fa37 KB |
496 | gdk_draw_string( m_window, font, m_textGC, x, y + font->ascent, text ); |
497 | ||
498 | // CMB 17/7/98: simple underline: ignores scaling and underlying | |
499 | // X font's XA_UNDERLINE_POSITION and XA_UNDERLINE_THICKNESS | |
500 | // properties (see wxXt implementation) | |
501 | if (m_font.GetUnderlined()) | |
502 | { | |
503 | long width = gdk_string_width( font, text ); | |
504 | long ul_y = y + font->ascent; | |
505 | if (font->descent > 0) ul_y++; | |
506 | gdk_draw_line( m_window, m_textGC, x, ul_y, x + width, ul_y); | |
507 | } | |
c801d85f KB |
508 | }; |
509 | ||
18a2fa37 KB |
510 | |
511 | ||
c801d85f KB |
512 | bool wxPaintDC::CanGetTextExtent(void) const |
513 | { | |
514 | return TRUE; | |
515 | }; | |
516 | ||
517 | void wxPaintDC::GetTextExtent( const wxString &string, long *width, long *height, | |
518 | long *WXUNUSED(descent), long *WXUNUSED(externalLeading), | |
519 | wxFont *WXUNUSED(theFont), bool WXUNUSED(use16) ) | |
520 | { | |
521 | if (!Ok()) return; | |
522 | ||
523 | GdkFont *font = m_font.GetInternalFont( m_scaleY ); | |
6f65e337 JS |
524 | if (width) (*width) = long(gdk_string_width( font, string ) / m_scaleX); |
525 | if (height) (*height) = long((font->ascent + font->descent) / m_scaleY); | |
c801d85f KB |
526 | }; |
527 | ||
528 | long wxPaintDC::GetCharWidth(void) | |
529 | { | |
530 | if (!Ok()) return 0; | |
531 | ||
532 | GdkFont *font = m_font.GetInternalFont( m_scaleY ); | |
533 | return gdk_string_width( font, "H" ); | |
534 | }; | |
535 | ||
536 | long wxPaintDC::GetCharHeight(void) | |
537 | { | |
538 | if (!Ok()) return 0; | |
539 | ||
540 | GdkFont *font = m_font.GetInternalFont( m_scaleY ); | |
541 | return font->ascent + font->descent; | |
542 | }; | |
543 | ||
544 | void wxPaintDC::Clear(void) | |
545 | { | |
546 | if (!Ok()) return; | |
547 | ||
54ff4a70 | 548 | // DestroyClippingRegion(); |
0180d5da RR |
549 | |
550 | if (m_isDrawable) | |
551 | { | |
552 | gdk_window_clear( m_window ); | |
553 | } | |
554 | else | |
555 | { | |
556 | int width = 0; | |
557 | int height = 0; | |
558 | GetSize( &width, &height ); | |
54ff4a70 | 559 | gdk_draw_rectangle( m_window, m_bgGC, TRUE, 0, 0, width, height ); |
0180d5da | 560 | }; |
c801d85f KB |
561 | }; |
562 | ||
563 | void wxPaintDC::SetFont( const wxFont &font ) | |
564 | { | |
565 | if (!Ok()) return; | |
566 | ||
567 | m_font = font; | |
568 | }; | |
569 | ||
570 | void wxPaintDC::SetPen( const wxPen &pen ) | |
571 | { | |
572 | if (!Ok()) return; | |
573 | ||
574 | if (m_pen == pen) return; | |
575 | ||
576 | m_pen = pen; | |
577 | ||
578 | if (!m_pen.Ok()) return; | |
579 | ||
580 | gint width = m_pen.GetWidth(); | |
6f65e337 JS |
581 | // CMB: if width is non-zero scale it with the dc |
582 | if (width <= 0) | |
583 | { | |
584 | width = 1; | |
585 | } | |
586 | else | |
587 | { | |
588 | // X doesn't allow different width in x and y and so we take | |
589 | // the average | |
590 | double w = 0.5 + (abs(XLOG2DEVREL(width)) + abs(YLOG2DEVREL(width))) / 2.0; | |
591 | width = (int)w; | |
592 | } | |
c801d85f KB |
593 | |
594 | GdkLineStyle lineStyle = GDK_LINE_SOLID; | |
595 | switch (m_pen.GetStyle()) | |
596 | { | |
597 | case wxSOLID: { lineStyle = GDK_LINE_SOLID; break; }; | |
598 | case wxDOT: { lineStyle = GDK_LINE_ON_OFF_DASH; break; }; | |
599 | case wxLONG_DASH: { lineStyle = GDK_LINE_ON_OFF_DASH; break; }; | |
600 | case wxSHORT_DASH: { lineStyle = GDK_LINE_ON_OFF_DASH; break; }; | |
601 | case wxDOT_DASH: { lineStyle = GDK_LINE_DOUBLE_DASH; break; }; | |
602 | }; | |
603 | ||
604 | GdkCapStyle capStyle = GDK_CAP_ROUND; | |
605 | switch (m_pen.GetCap()) | |
606 | { | |
6f65e337 | 607 | case wxCAP_ROUND: { capStyle = (width <= 1) ? GDK_CAP_NOT_LAST : GDK_CAP_ROUND; break; }; |
c801d85f KB |
608 | case wxCAP_PROJECTING: { capStyle = GDK_CAP_PROJECTING; break; }; |
609 | case wxCAP_BUTT: { capStyle = GDK_CAP_BUTT; break; }; | |
610 | }; | |
611 | ||
612 | GdkJoinStyle joinStyle = GDK_JOIN_ROUND; | |
613 | switch (m_pen.GetJoin()) | |
614 | { | |
615 | case wxJOIN_BEVEL: { joinStyle = GDK_JOIN_BEVEL; break; }; | |
616 | case wxJOIN_ROUND: { joinStyle = GDK_JOIN_ROUND; break; }; | |
617 | case wxJOIN_MITER: { joinStyle = GDK_JOIN_MITER; break; }; | |
618 | }; | |
619 | ||
620 | gdk_gc_set_line_attributes( m_penGC, width, lineStyle, capStyle, joinStyle ); | |
621 | ||
622 | m_pen.GetColour().CalcPixel( m_cmap ); | |
623 | gdk_gc_set_foreground( m_penGC, m_pen.GetColour().GetColor() ); | |
624 | }; | |
625 | ||
626 | void wxPaintDC::SetBrush( const wxBrush &brush ) | |
627 | { | |
628 | if (!Ok()) return; | |
629 | ||
630 | if (m_brush == brush) return; | |
631 | ||
632 | m_brush = brush; | |
633 | ||
634 | if (!m_brush.Ok()) return; | |
635 | ||
636 | m_brush.GetColour().CalcPixel( m_cmap ); | |
637 | gdk_gc_set_foreground( m_brushGC, m_brush.GetColour().GetColor() ); | |
638 | ||
639 | GdkFill fillStyle = GDK_SOLID; | |
640 | switch (m_brush.GetStyle()) | |
641 | { | |
642 | case wxSOLID: | |
643 | case wxTRANSPARENT: | |
644 | break; | |
645 | default: | |
646 | fillStyle = GDK_STIPPLED; | |
647 | }; | |
648 | ||
649 | gdk_gc_set_fill( m_brushGC, fillStyle ); | |
650 | ||
651 | if (m_brush.GetStyle() == wxSTIPPLE) | |
652 | { | |
653 | gdk_gc_set_stipple( m_brushGC, m_brush.GetStipple()->GetPixmap() ); | |
654 | }; | |
655 | ||
656 | if (IS_HATCH(m_brush.GetStyle())) | |
657 | { | |
658 | int num = m_brush.GetStyle() - wxBDIAGONAL_HATCH; | |
659 | gdk_gc_set_stipple( m_brushGC, hatches[num] ); | |
660 | }; | |
661 | }; | |
662 | ||
46dc76ba RR |
663 | // CMB 21/7/98: Added SetBackground. Sets background brush |
664 | // for Clear() and bg colour for shapes filled with cross-hatch brush | |
665 | void wxPaintDC::SetBackground( const wxBrush &brush ) | |
666 | { | |
667 | if (!Ok()) return; | |
668 | ||
669 | if (m_backgroundBrush == brush) return; | |
670 | ||
671 | m_backgroundBrush = brush; | |
672 | ||
673 | if (!m_backgroundBrush.Ok()) return; | |
674 | ||
675 | m_backgroundBrush.GetColour().CalcPixel( m_cmap ); | |
676 | gdk_gc_set_background( m_brushGC, m_backgroundBrush.GetColour().GetColor() ); | |
677 | gdk_gc_set_foreground( m_bgGC, m_backgroundBrush.GetColour().GetColor() ); | |
678 | ||
679 | GdkFill fillStyle = GDK_SOLID; | |
680 | switch (m_backgroundBrush.GetStyle()) | |
681 | { | |
682 | case wxSOLID: | |
683 | case wxTRANSPARENT: | |
684 | break; | |
685 | default: | |
686 | fillStyle = GDK_STIPPLED; | |
687 | }; | |
688 | ||
689 | gdk_gc_set_fill( m_bgGC, fillStyle ); | |
690 | ||
691 | if (m_backgroundBrush.GetStyle() == wxSTIPPLE) | |
692 | { | |
693 | gdk_gc_set_stipple( m_bgGC, m_backgroundBrush.GetStipple()->GetPixmap() ); | |
694 | }; | |
695 | ||
696 | if (IS_HATCH(m_backgroundBrush.GetStyle())) | |
697 | { | |
698 | int num = m_backgroundBrush.GetStyle() - wxBDIAGONAL_HATCH; | |
699 | gdk_gc_set_stipple( m_bgGC, hatches[num] ); | |
700 | }; | |
701 | }; | |
702 | ||
c801d85f KB |
703 | void wxPaintDC::SetLogicalFunction( int function ) |
704 | { | |
705 | if (m_logicalFunction == function) return; | |
706 | GdkFunction mode = GDK_COPY; | |
707 | switch (function) | |
708 | { | |
709 | case wxXOR: mode = GDK_INVERT; break; | |
710 | case wxINVERT: mode = GDK_INVERT; break; | |
711 | default: break; | |
712 | }; | |
713 | m_logicalFunction = function; | |
714 | gdk_gc_set_function( m_penGC, mode ); | |
715 | gdk_gc_set_function( m_brushGC, mode ); | |
716 | }; | |
717 | ||
718 | void wxPaintDC::SetTextForeground( const wxColour &col ) | |
719 | { | |
720 | if (!Ok()) return; | |
721 | ||
722 | if (m_textForegroundColour == col) return; | |
723 | ||
724 | m_textForegroundColour = col; | |
725 | if (!m_textForegroundColour.Ok()) return; | |
726 | ||
727 | m_textForegroundColour.CalcPixel( m_cmap ); | |
728 | gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() ); | |
729 | }; | |
730 | ||
731 | void wxPaintDC::SetTextBackground( const wxColour &col ) | |
732 | { | |
733 | if (!Ok()) return; | |
734 | ||
735 | if (m_textBackgroundColour == col) return; | |
736 | ||
737 | m_textBackgroundColour = col; | |
738 | if (!m_textBackgroundColour.Ok()) return; | |
739 | ||
740 | m_textBackgroundColour.CalcPixel( m_cmap ); | |
741 | gdk_gc_set_background( m_textGC, m_textBackgroundColour.GetColor() ); | |
742 | }; | |
743 | ||
6f65e337 | 744 | void wxPaintDC::SetBackgroundMode( int mode ) |
c801d85f | 745 | { |
6f65e337 | 746 | m_backgroundMode = mode; |
46dc76ba RR |
747 | |
748 | // CMB 21/7/98: fill style of cross-hatch brushes is affected by | |
749 | // transparent/solid background mode | |
750 | if (m_brush.GetStyle() != wxSOLID && m_brush.GetStyle() != wxTRANSPARENT) | |
751 | { | |
752 | gdk_gc_set_fill( m_brushGC, | |
753 | (m_backgroundMode == wxTRANSPARENT) ? GDK_STIPPLED : GDK_OPAQUE_STIPPLED); | |
754 | } | |
c801d85f KB |
755 | }; |
756 | ||
757 | void wxPaintDC::SetPalette( const wxPalette& WXUNUSED(palette) ) | |
758 | { | |
759 | }; | |
760 | ||
761 | void wxPaintDC::SetClippingRegion( long x, long y, long width, long height ) | |
762 | { | |
763 | wxDC::SetClippingRegion( x, y, width, height ); | |
764 | ||
765 | GdkRectangle rect; | |
766 | rect.x = XLOG2DEV(x); | |
767 | rect.y = YLOG2DEV(y); | |
768 | rect.width = XLOG2DEV(x+width); | |
769 | rect.height = YLOG2DEV(y+height); | |
770 | gdk_gc_set_clip_rectangle( m_penGC, &rect ); | |
771 | gdk_gc_set_clip_rectangle( m_brushGC, &rect ); | |
772 | gdk_gc_set_clip_rectangle( m_textGC, &rect ); | |
773 | gdk_gc_set_clip_rectangle( m_bgGC, &rect ); | |
774 | ||
775 | }; | |
776 | ||
777 | void wxPaintDC::DestroyClippingRegion(void) | |
778 | { | |
779 | wxDC::DestroyClippingRegion(); | |
780 | ||
781 | gdk_gc_set_clip_rectangle( m_penGC, NULL ); | |
782 | gdk_gc_set_clip_rectangle( m_brushGC, NULL ); | |
783 | gdk_gc_set_clip_rectangle( m_textGC, NULL ); | |
784 | gdk_gc_set_clip_rectangle( m_bgGC, NULL ); | |
785 | }; | |
786 | ||
787 | void wxPaintDC::SetUpDC(void) | |
788 | { | |
789 | m_ok = TRUE; | |
790 | m_logicalFunction = wxCOPY; | |
e3e65dac | 791 | if (m_penGC) gdk_gc_unref( m_penGC ); |
c801d85f | 792 | m_penGC = gdk_gc_new( m_window ); |
e3e65dac | 793 | if (m_brushGC) gdk_gc_unref( m_brushGC ); |
c801d85f | 794 | m_brushGC = gdk_gc_new( m_window ); |
e3e65dac | 795 | if (m_textGC) gdk_gc_unref( m_textGC ); |
c801d85f | 796 | m_textGC = gdk_gc_new( m_window ); |
e3e65dac | 797 | if (m_bgGC) gdk_gc_unref( m_bgGC ); |
c801d85f KB |
798 | m_bgGC = gdk_gc_new( m_window ); |
799 | SetTextForeground( m_textForegroundColour ); | |
800 | SetTextBackground( m_textBackgroundColour ); | |
801 | SetPen( m_pen ); | |
802 | SetFont( m_font ); | |
803 | SetBrush( m_brush ); | |
804 | ||
805 | gdk_gc_set_background( m_penGC, wxWHITE->GetColor() ); | |
806 | ||
807 | if (!hatch_bitmap) | |
808 | { | |
809 | hatch_bitmap = hatches; | |
810 | hatch_bitmap[0] = gdk_bitmap_create_from_data( NULL, bdiag_bits, bdiag_width, bdiag_height ); | |
811 | hatch_bitmap[1] = gdk_bitmap_create_from_data( NULL, cdiag_bits, cdiag_width, cdiag_height ); | |
812 | hatch_bitmap[2] = gdk_bitmap_create_from_data( NULL, fdiag_bits, fdiag_width, fdiag_height ); | |
813 | hatch_bitmap[3] = gdk_bitmap_create_from_data( NULL, cross_bits, cross_width, cross_height ); | |
814 | hatch_bitmap[4] = gdk_bitmap_create_from_data( NULL, horiz_bits, horiz_width, horiz_height ); | |
815 | hatch_bitmap[5] = gdk_bitmap_create_from_data( NULL, verti_bits, verti_width, verti_height ); | |
816 | }; | |
817 | }; | |
818 | ||
819 | GdkWindow *wxPaintDC::GetWindow(void) | |
820 | { | |
821 | return m_window; | |
822 | }; | |
823 | ||
824 | // ----------------------------------- spline code ---------------------------------------- | |
825 | ||
826 | void wx_quadratic_spline(double a1, double b1, double a2, double b2, | |
827 | double a3, double b3, double a4, double b4); | |
828 | void wx_clear_stack(void); | |
829 | int wx_spline_pop(double *x1, double *y1, double *x2, double *y2, double *x3, | |
830 | double *y3, double *x4, double *y4); | |
831 | void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3, | |
832 | double x4, double y4); | |
833 | static bool wx_spline_add_point(double x, double y); | |
834 | static void wx_spline_draw_point_array(wxDC *dc); | |
835 | ||
836 | wxList wx_spline_point_list; | |
837 | ||
838 | #define half(z1, z2) ((z1+z2)/2.0) | |
839 | #define THRESHOLD 5 | |
840 | ||
841 | /* iterative version */ | |
842 | ||
843 | void wx_quadratic_spline(double a1, double b1, double a2, double b2, double a3, double b3, double a4, | |
844 | double b4) | |
845 | { | |
846 | register double xmid, ymid; | |
847 | double x1, y1, x2, y2, x3, y3, x4, y4; | |
848 | ||
849 | wx_clear_stack(); | |
850 | wx_spline_push(a1, b1, a2, b2, a3, b3, a4, b4); | |
851 | ||
852 | while (wx_spline_pop(&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4)) { | |
853 | xmid = (double)half(x2, x3); | |
854 | ymid = (double)half(y2, y3); | |
855 | if (fabs(x1 - xmid) < THRESHOLD && fabs(y1 - ymid) < THRESHOLD && | |
856 | fabs(xmid - x4) < THRESHOLD && fabs(ymid - y4) < THRESHOLD) { | |
857 | wx_spline_add_point( x1, y1 ); | |
858 | wx_spline_add_point( xmid, ymid ); | |
859 | } else { | |
860 | wx_spline_push(xmid, ymid, (double)half(xmid, x3), (double)half(ymid, y3), | |
861 | (double)half(x3, x4), (double)half(y3, y4), x4, y4); | |
862 | wx_spline_push(x1, y1, (double)half(x1, x2), (double)half(y1, y2), | |
863 | (double)half(x2, xmid), (double)half(y2, ymid), xmid, ymid); | |
864 | } | |
865 | } | |
866 | } | |
867 | ||
868 | /* utilities used by spline drawing routines */ | |
869 | ||
870 | typedef struct wx_spline_stack_struct { | |
871 | double x1, y1, x2, y2, x3, y3, x4, y4; | |
872 | } Stack; | |
873 | ||
874 | #define SPLINE_STACK_DEPTH 20 | |
875 | static Stack wx_spline_stack[SPLINE_STACK_DEPTH]; | |
876 | static Stack *wx_stack_top; | |
877 | static int wx_stack_count; | |
878 | ||
879 | void wx_clear_stack(void) | |
880 | { | |
881 | wx_stack_top = wx_spline_stack; | |
882 | wx_stack_count = 0; | |
883 | } | |
884 | ||
885 | void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) | |
886 | { | |
887 | wx_stack_top->x1 = x1; | |
888 | wx_stack_top->y1 = y1; | |
889 | wx_stack_top->x2 = x2; | |
890 | wx_stack_top->y2 = y2; | |
891 | wx_stack_top->x3 = x3; | |
892 | wx_stack_top->y3 = y3; | |
893 | wx_stack_top->x4 = x4; | |
894 | wx_stack_top->y4 = y4; | |
895 | wx_stack_top++; | |
896 | wx_stack_count++; | |
897 | } | |
898 | ||
899 | int wx_spline_pop(double *x1, double *y1, double *x2, double *y2, | |
900 | double *x3, double *y3, double *x4, double *y4) | |
901 | { | |
902 | if (wx_stack_count == 0) | |
903 | return (0); | |
904 | wx_stack_top--; | |
905 | wx_stack_count--; | |
906 | *x1 = wx_stack_top->x1; | |
907 | *y1 = wx_stack_top->y1; | |
908 | *x2 = wx_stack_top->x2; | |
909 | *y2 = wx_stack_top->y2; | |
910 | *x3 = wx_stack_top->x3; | |
911 | *y3 = wx_stack_top->y3; | |
912 | *x4 = wx_stack_top->x4; | |
913 | *y4 = wx_stack_top->y4; | |
914 | return (1); | |
915 | } | |
916 | ||
917 | static bool wx_spline_add_point(double x, double y) | |
918 | { | |
919 | wxPoint *point = new wxPoint ; | |
920 | point->x = (int) x; | |
921 | point->y = (int) y; | |
922 | wx_spline_point_list.Append((wxObject*)point); | |
923 | return TRUE; | |
924 | } | |
925 | ||
926 | static void wx_spline_draw_point_array(wxDC *dc) | |
927 | { | |
928 | dc->DrawLines(&wx_spline_point_list, 0, 0 ); | |
929 | wxNode *node = wx_spline_point_list.First(); | |
930 | while (node) | |
931 | { | |
932 | wxPoint *point = (wxPoint *)node->Data(); | |
933 | delete point; | |
934 | delete node; | |
935 | node = wx_spline_point_list.First(); | |
936 | } | |
937 | } | |
938 | ||
939 | void wxPaintDC::DrawOpenSpline( wxList *points ) | |
940 | { | |
941 | wxPoint *p; | |
942 | double cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4; | |
943 | double x1, y1, x2, y2; | |
944 | ||
945 | wxNode *node = points->First(); | |
946 | p = (wxPoint *)node->Data(); | |
947 | ||
948 | x1 = p->x; | |
949 | y1 = p->y; | |
950 | ||
951 | node = node->Next(); | |
952 | p = (wxPoint *)node->Data(); | |
953 | ||
954 | x2 = p->x; | |
955 | y2 = p->y; | |
956 | cx1 = (double)((x1 + x2) / 2); | |
957 | cy1 = (double)((y1 + y2) / 2); | |
958 | cx2 = (double)((cx1 + x2) / 2); | |
959 | cy2 = (double)((cy1 + y2) / 2); | |
960 | ||
961 | wx_spline_add_point(x1, y1); | |
962 | ||
963 | while ((node = node->Next()) != NULL) | |
964 | { | |
965 | p = (wxPoint *)node->Data(); | |
966 | x1 = x2; | |
967 | y1 = y2; | |
968 | x2 = p->x; | |
969 | y2 = p->y; | |
970 | cx4 = (double)(x1 + x2) / 2; | |
971 | cy4 = (double)(y1 + y2) / 2; | |
972 | cx3 = (double)(x1 + cx4) / 2; | |
973 | cy3 = (double)(y1 + cy4) / 2; | |
974 | ||
975 | wx_quadratic_spline(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4); | |
976 | ||
977 | cx1 = cx4; | |
978 | cy1 = cy4; | |
979 | cx2 = (double)(cx1 + x2) / 2; | |
980 | cy2 = (double)(cy1 + y2) / 2; | |
981 | } | |
982 | ||
983 | wx_spline_add_point( cx1, cy1 ); | |
984 | wx_spline_add_point( x2, y2 ); | |
985 | ||
986 | wx_spline_draw_point_array( this ); | |
987 | }; |