New wxDC hierarchy
[wxWidgets.git] / src / gtk1 / dcclient.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dcclient.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Markus Holzem, Chris Breeze
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "dcclient.h"
12 #endif
13
14 #include "wx/dcclient.h"
15 #include "wx/dcmemory.h"
16 #include <math.h>
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 = (GdkPixmap **) NULL;
32
33 //-----------------------------------------------------------------------------
34 // constants
35 //-----------------------------------------------------------------------------
36
37 #define RAD2DEG 57.2957795131
38
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
66 gc_private = (GdkGCPrivate*) gc;
67
68 if (width == -1) width = src_private->width;
69 if (height == -1) height = src_private->height;
70
71 XCopyPlane( drawable_private->xdisplay,
72 src_private->xwindow,
73 drawable_private->xwindow,
74 gc_private->xgc,
75 xsrc, ysrc,
76 width, height,
77 xdest, ydest,
78 1 );
79 }
80
81 //-----------------------------------------------------------------------------
82 // wxWindowDC
83 //-----------------------------------------------------------------------------
84
85 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC,wxDC)
86
87 wxWindowDC::wxWindowDC(void)
88 {
89 m_penGC = (GdkGC *) NULL;
90 m_brushGC = (GdkGC *) NULL;
91 m_textGC = (GdkGC *) NULL;
92 m_bgGC = (GdkGC *) NULL;
93 m_cmap = (GdkColormap *) NULL;
94 m_isMemDC = FALSE;
95 }
96
97 wxWindowDC::wxWindowDC( wxWindow *window )
98 {
99 m_penGC = (GdkGC *) NULL;
100 m_brushGC = (GdkGC *) NULL;
101 m_textGC = (GdkGC *) NULL;
102 m_bgGC = (GdkGC *) NULL;
103 m_cmap = (GdkColormap *) NULL;
104
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 );
114
115 m_isMemDC = FALSE;
116
117 SetUpDC();
118
119 wxRegion update = window->GetUpdateRegion();
120 if (update.Empty()) return;
121
122 gdk_gc_set_clip_region( m_penGC, update.GetRegion() );
123 gdk_gc_set_clip_region( m_brushGC, update.GetRegion() );
124 gdk_gc_set_clip_region( m_textGC, update.GetRegion() );
125 gdk_gc_set_clip_region( m_bgGC, update.GetRegion() );
126 }
127
128 wxWindowDC::~wxWindowDC(void)
129 {
130 Destroy();
131 }
132
133 void wxWindowDC::FloodFill( long WXUNUSED(x1), long WXUNUSED(y1),
134 wxColour *WXUNUSED(col), int WXUNUSED(style) )
135 {
136 wxFAIL_MSG( "wxWindowDC::FloodFill not implemented" );
137 }
138
139 bool wxWindowDC::GetPixel( long WXUNUSED(x1), long WXUNUSED(y1), wxColour *WXUNUSED(col) ) const
140 {
141 wxFAIL_MSG( "wxWindowDC::GetPixel not implemented" );
142 return FALSE;
143 }
144
145 void wxWindowDC::DrawLine( long x1, long y1, long x2, long y2 )
146 {
147 if (!Ok()) return;
148
149 if (m_pen.GetStyle() != wxTRANSPARENT)
150 {
151 gdk_draw_line( m_window, m_penGC,
152 XLOG2DEV(x1), YLOG2DEV(y1), XLOG2DEV(x2), YLOG2DEV(y2) );
153 }
154
155 CalcBoundingBox(x1, y1);
156 CalcBoundingBox(x2, y2);
157 }
158
159 void wxWindowDC::CrossHair( long x, long y )
160 {
161 if (!Ok()) return;
162
163 if (m_pen.GetStyle() != wxTRANSPARENT)
164 {
165 int w = 0;
166 int h = 0;
167 GetSize( &w, &h );
168 long xx = XLOG2DEV(x);
169 long yy = YLOG2DEV(y);
170 gdk_draw_line( m_window, m_penGC, 0, yy, XLOG2DEVREL(w), yy );
171 gdk_draw_line( m_window, m_penGC, xx, 0, xx, YLOG2DEVREL(h) );
172 }
173 }
174
175 void wxWindowDC::DrawArc( long x1, long y1, long x2, long y2, double xc, double yc )
176 {
177 if (!Ok()) return;
178
179 long xx1 = XLOG2DEV(x1);
180 long yy1 = YLOG2DEV(y1);
181 long xx2 = XLOG2DEV(x2);
182 long yy2 = YLOG2DEV(y2);
183 long xxc = XLOG2DEV((long)xc);
184 long yyc = YLOG2DEV((long)yc);
185 double dx = xx1 - xxc;
186 double dy = yy1 - yyc;
187 double radius = sqrt(dx*dx+dy*dy);
188 long r = (long)radius;
189 double radius1, radius2;
190
191 if (xx1 == xx2 && yy1 == yy2)
192 {
193 radius1 = 0.0;
194 radius2 = 360.0;
195 }
196 else
197 if (radius == 0.0)
198 {
199 radius1 = radius2 = 0.0;
200 }
201 else
202 {
203 radius1 = (xx1 - xxc == 0) ?
204 (yy1 - yyc < 0) ? 90.0 : -90.0 :
205 -atan2(double(yy1-yyc), double(xx1-xxc)) * RAD2DEG;
206 radius2 = (xx2 - xxc == 0) ?
207 (yy2 - yyc < 0) ? 90.0 : -90.0 :
208 -atan2(double(yy2-yyc), double(xx2-xxc)) * RAD2DEG;
209 }
210 long alpha1 = long(radius1 * 64.0);
211 long alpha2 = long((radius2 - radius1) * 64.0);
212 while (alpha2 <= 0) alpha2 += 360*64;
213 while (alpha1 > 360*64) alpha1 -= 360*64;
214
215 if (m_brush.GetStyle() != wxTRANSPARENT)
216 gdk_draw_arc( m_window, m_brushGC, TRUE, xxc-r, yyc-r, 2*r,2*r, alpha1, alpha2 );
217
218 if (m_pen.GetStyle() != wxTRANSPARENT)
219 gdk_draw_arc( m_window, m_penGC, FALSE, xxc-r, yyc-r, 2*r,2*r, alpha1, alpha2 );
220
221 CalcBoundingBox (x1, y1);
222 CalcBoundingBox (x2, y2);
223 }
224
225 void wxWindowDC::DrawEllipticArc( long x, long y, long width, long height, double sa, double ea )
226 {
227 if (!Ok()) return;
228
229 long xx = XLOG2DEV(x);
230 long yy = YLOG2DEV(y);
231 long ww = m_signX * XLOG2DEVREL(width);
232 long hh = m_signY * YLOG2DEVREL(height);
233
234 // CMB: handle -ve width and/or height
235 if (ww < 0) { ww = -ww; xx = xx - ww; }
236 if (hh < 0) { hh = -hh; yy = yy - hh; }
237
238 long start = long(sa * 64.0);
239 long end = long(ea * 64.0);
240 if (m_brush.GetStyle() != wxTRANSPARENT)
241 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, ww, hh, start, end );
242
243 if (m_pen.GetStyle() != wxTRANSPARENT)
244 gdk_draw_arc( m_window, m_penGC, FALSE, xx, yy, ww, hh, start, end );
245
246 CalcBoundingBox (x, y);
247 CalcBoundingBox (x + width, y + height);
248 }
249
250 void wxWindowDC::DrawPoint( long x, long y )
251 {
252 if (!Ok()) return;
253
254 if (m_pen.GetStyle() != wxTRANSPARENT)
255 gdk_draw_point( m_window, m_penGC, XLOG2DEV(x), YLOG2DEV(y) );
256
257 CalcBoundingBox (x, y);
258 }
259
260 void wxWindowDC::DrawLines( int n, wxPoint points[], long xoffset, long yoffset )
261 {
262 if (!Ok()) return;
263
264 if (m_pen.GetStyle() == wxTRANSPARENT) return;
265 if (n <= 0) return;
266
267 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
268
269 for (int i = 0; i < n-1; i++)
270 {
271 long x1 = XLOG2DEV(points[i].x + xoffset);
272 long x2 = XLOG2DEV(points[i+1].x + xoffset);
273 long y1 = YLOG2DEV(points[i].y + yoffset); // oh, what a waste
274 long y2 = YLOG2DEV(points[i+1].y + yoffset);
275 gdk_draw_line( m_window, m_penGC, x1, y1, x2, y2 );
276
277 CalcBoundingBox( points[i+1].x + xoffset, points[i+1].y + yoffset );
278 }
279 }
280
281 void wxWindowDC::DrawLines( wxList *points, long xoffset, long yoffset )
282 {
283 if (!Ok()) return;
284
285 if (m_pen.GetStyle() == wxTRANSPARENT) return;
286
287 wxNode *node = points->First();
288 if (!node) return;
289
290 wxPoint *pt = (wxPoint*)node->Data();
291 CalcBoundingBox( pt->x + xoffset, pt->y + yoffset );
292
293 while (node->Next())
294 {
295 wxPoint *point = (wxPoint*)node->Data();
296 wxPoint *npoint = (wxPoint*)node->Next()->Data();
297 long x1 = XLOG2DEV(point->x + xoffset);
298 long x2 = XLOG2DEV(npoint->x + xoffset);
299 long y1 = YLOG2DEV(point->y + yoffset); // and a waste again...
300 long y2 = YLOG2DEV(npoint->y + yoffset);
301 gdk_draw_line( m_window, m_penGC, x1, y1, x2, y2 );
302 node = node->Next();
303
304 CalcBoundingBox( npoint->x + xoffset, npoint->y + yoffset );
305 }
306 }
307
308 void wxWindowDC::DrawPolygon( int n, wxPoint points[], long xoffset, long yoffset, int WXUNUSED(fillStyle) )
309 {
310 if (!Ok()) return;
311
312 if (!n) return;
313
314 GdkPoint *gdkpoints = new GdkPoint[n+1];
315 int i;
316 for (i = 0 ; i < n ; i++)
317 {
318 gdkpoints[i].x = XLOG2DEV(points[i].x + xoffset);
319 gdkpoints[i].y = YLOG2DEV(points[i].y + yoffset);
320
321 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset );
322 }
323
324 if (m_brush.GetStyle() != wxTRANSPARENT)
325 gdk_draw_polygon (m_window, m_brushGC, TRUE, gdkpoints, n);
326
327 // To do: Fillstyle
328
329 if (m_pen.GetStyle() != wxTRANSPARENT)
330 for (i = 0 ; i < n ; i++)
331 {
332 gdk_draw_line( m_window, m_penGC,
333 gdkpoints[i%n].x,
334 gdkpoints[i%n].y,
335 gdkpoints[(i+1)%n].x,
336 gdkpoints[(i+1)%n].y);
337 }
338
339 delete[] gdkpoints;
340 }
341
342 void wxWindowDC::DrawPolygon( wxList *lines, long xoffset, long yoffset, int WXUNUSED(fillStyle))
343 {
344 if (!Ok()) return;
345
346 int n = lines->Number();
347 if (!n) return;
348
349 GdkPoint *gdkpoints = new GdkPoint[n];
350 wxNode *node = lines->First();
351 int cnt = 0;
352 while (node)
353 {
354 wxPoint *p = (wxPoint *) node->Data();
355 gdkpoints[cnt].x = XLOG2DEV(p->x + xoffset);
356 gdkpoints[cnt].y = YLOG2DEV(p->y + yoffset);
357 node = node->Next();
358 cnt++;
359
360 CalcBoundingBox( p->x + xoffset, p->y + yoffset );
361 }
362
363 if (m_brush.GetStyle() != wxTRANSPARENT)
364 gdk_draw_polygon (m_window, m_brushGC, TRUE, gdkpoints, n);
365
366 // To do: Fillstyle
367
368 if (m_pen.GetStyle() != wxTRANSPARENT)
369 {
370 int i;
371 for (i = 0 ; i < n ; i++)
372 {
373 gdk_draw_line( m_window, m_penGC,
374 gdkpoints[i%n].x,
375 gdkpoints[i%n].y,
376 gdkpoints[(i+1)%n].x,
377 gdkpoints[(i+1)%n].y );
378 }
379 }
380 delete[] gdkpoints;
381 }
382
383 void wxWindowDC::DrawRectangle( long x, long y, long width, long height )
384 {
385 if (!Ok()) return;
386
387 long xx = XLOG2DEV(x);
388 long yy = YLOG2DEV(y);
389 long ww = m_signX * XLOG2DEVREL(width);
390 long hh = m_signY * YLOG2DEVREL(height);
391
392 // CMB: draw nothing if transformed w or h is 0
393 if (ww == 0 || hh == 0) return;
394
395 // CMB: handle -ve width and/or height
396 if (ww < 0) { ww = -ww; xx = xx - ww; }
397 if (hh < 0) { hh = -hh; yy = yy - hh; }
398
399 if (m_brush.GetStyle() != wxTRANSPARENT)
400 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy, ww, hh );
401
402 if (m_pen.GetStyle() != wxTRANSPARENT)
403 gdk_draw_rectangle( m_window, m_penGC, FALSE, xx, yy, ww-1, hh-1 );
404
405 CalcBoundingBox( x, y );
406 CalcBoundingBox( x + width, y + height );
407 }
408
409 void wxWindowDC::DrawRoundedRectangle( long x, long y, long width, long height, double radius )
410 {
411 if (!Ok()) return;
412
413 if (radius < 0.0) radius = - radius * ((width < height) ? width : height);
414
415 long xx = XLOG2DEV(x);
416 long yy = YLOG2DEV(y);
417 long ww = m_signX * XLOG2DEVREL(width);
418 long hh = m_signY * YLOG2DEVREL(height);
419 long rr = XLOG2DEVREL((long)radius);
420
421 // CMB: handle -ve width and/or height
422 if (ww < 0) { ww = -ww; xx = xx - ww; }
423 if (hh < 0) { hh = -hh; yy = yy - hh; }
424
425 // CMB: if radius is zero use DrawRectangle() instead to avoid
426 // X drawing errors with small radii
427 if (rr == 0)
428 {
429 DrawRectangle( x, y, width, height );
430 return;
431 }
432
433 // CMB: draw nothing if transformed w or h is 0
434 if (ww == 0 || hh == 0) return;
435
436 // CMB: adjust size if outline is drawn otherwise the result is
437 // 1 pixel too wide and high
438 if (m_pen.GetStyle() != wxTRANSPARENT)
439 {
440 ww--;
441 hh--;
442 }
443
444 // CMB: ensure dd is not larger than rectangle otherwise we
445 // get an hour glass shape
446 long dd = 2 * rr;
447 if (dd > ww) dd = ww;
448 if (dd > hh) dd = hh;
449 rr = dd / 2;
450
451 if (m_brush.GetStyle() != wxTRANSPARENT)
452 {
453 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx+rr, yy, ww-dd+1, hh );
454 gdk_draw_rectangle( m_window, m_brushGC, TRUE, xx, yy+rr, ww, hh-dd+1 );
455 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, dd, dd, 90*64, 90*64 );
456 gdk_draw_arc( m_window, m_brushGC, TRUE, xx+ww-dd, yy, dd, dd, 0, 90*64 );
457 gdk_draw_arc( m_window, m_brushGC, TRUE, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64 );
458 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy+hh-dd, dd, dd, 180*64, 90*64 );
459 }
460
461 if (m_pen.GetStyle() != wxTRANSPARENT)
462 {
463 gdk_draw_line( m_window, m_penGC, xx+rr, yy, xx+ww-rr, yy );
464 gdk_draw_line( m_window, m_penGC, xx+rr, yy+hh, xx+ww-rr, yy+hh );
465 gdk_draw_line( m_window, m_penGC, xx, yy+rr, xx, yy+hh-rr );
466 gdk_draw_line( m_window, m_penGC, xx+ww, yy+rr, xx+ww, yy+hh-rr );
467 gdk_draw_arc( m_window, m_penGC, FALSE, xx, yy, dd, dd, 90*64, 90*64 );
468 gdk_draw_arc( m_window, m_penGC, FALSE, xx+ww-dd, yy, dd, dd, 0, 90*64 );
469 gdk_draw_arc( m_window, m_penGC, FALSE, xx+ww-dd, yy+hh-dd, dd, dd, 270*64, 90*64 );
470 gdk_draw_arc( m_window, m_penGC, FALSE, xx, yy+hh-dd, dd, dd, 180*64, 90*64 );
471 }
472
473 // this ignores the radius
474 CalcBoundingBox( x, y );
475 CalcBoundingBox( x + width, y + height );
476 }
477
478 void wxWindowDC::DrawEllipse( long x, long y, long width, long height )
479 {
480 if (!Ok()) return;
481
482 long xx = XLOG2DEV(x);
483 long yy = YLOG2DEV(y);
484 long ww = m_signX * XLOG2DEVREL(width);
485 long hh = m_signY * YLOG2DEVREL(height);
486
487 // CMB: handle -ve width and/or height
488 if (ww < 0) { ww = -ww; xx = xx - ww; }
489 if (hh < 0) { hh = -hh; yy = yy - hh; }
490
491 if (m_brush.GetStyle() != wxTRANSPARENT)
492 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, ww, hh, 0, 360*64 );
493
494 if (m_pen.GetStyle() != wxTRANSPARENT)
495 gdk_draw_arc( m_window, m_penGC, FALSE, xx, yy, ww, hh, 0, 360*64 );
496
497 CalcBoundingBox( x, y );
498 CalcBoundingBox( x + width, y + height );
499 }
500
501 bool wxWindowDC::CanDrawBitmap(void) const
502 {
503 return TRUE;
504 }
505
506 void wxWindowDC::DrawIcon( const wxIcon &icon, long x, long y, bool useMask )
507 {
508 if (!Ok()) return;
509
510 if (!icon.Ok()) return;
511
512 int xx = XLOG2DEV(x);
513 int yy = YLOG2DEV(y);
514
515 GdkBitmap *mask = (GdkBitmap *) NULL;
516 if (icon.GetMask()) mask = icon.GetMask()->GetBitmap();
517
518 if (useMask && mask)
519 {
520 gdk_gc_set_clip_mask( m_penGC, mask );
521 gdk_gc_set_clip_origin( m_penGC, xx, yy );
522 }
523
524 GdkPixmap *pm = icon.GetPixmap();
525 gdk_draw_pixmap( m_window, m_penGC, pm, 0, 0, xx, yy, -1, -1 );
526
527 if (useMask && mask)
528 {
529 gdk_gc_set_clip_mask( m_penGC, (GdkBitmap *) NULL );
530 gdk_gc_set_clip_origin( m_penGC, 0, 0 );
531 }
532
533 CalcBoundingBox( x, y );
534 int width = icon.GetWidth();
535 int height = icon.GetHeight();
536 CalcBoundingBox( x + width, y + height );
537 }
538
539 bool wxWindowDC::Blit( long xdest, long ydest, long width, long height,
540 wxDC *source, long xsrc, long ysrc, int WXUNUSED(logical_func), bool useMask )
541 {
542 if (!Ok()) return FALSE;
543
544 CalcBoundingBox( xdest, ydest );
545 CalcBoundingBox( xdest + width, ydest + height );
546
547 wxClientDC *csrc = (wxClientDC*)source;
548
549 if (csrc->m_isMemDC)
550 {
551 wxMemoryDC* srcDC = (wxMemoryDC*)source;
552 GdkPixmap* pmap = srcDC->m_selected.GetPixmap();
553 if (pmap)
554 {
555 long xx = XLOG2DEV(xdest);
556 long yy = YLOG2DEV(ydest);
557
558 GdkBitmap *mask = (GdkBitmap *) NULL;
559 if (srcDC->m_selected.GetMask()) mask = srcDC->m_selected.GetMask()->GetBitmap();
560
561 if (useMask && mask)
562 {
563 gdk_gc_set_clip_mask( m_textGC, mask );
564 gdk_gc_set_clip_origin( m_textGC, xx, yy );
565 }
566
567 gdk_draw_pixmap( m_window, m_textGC, pmap,
568 source->DeviceToLogicalX(xsrc),
569 source->DeviceToLogicalY(ysrc),
570 xx,
571 yy,
572 source->DeviceToLogicalXRel(width),
573 source->DeviceToLogicalYRel(height) );
574
575 if (useMask && mask)
576 {
577 gdk_gc_set_clip_mask( m_textGC, (GdkBitmap *) NULL );
578 gdk_gc_set_clip_origin( m_textGC, 0, 0 );
579 }
580
581 return TRUE;
582 }
583
584 GdkBitmap* bmap = srcDC->m_selected.GetBitmap();
585 if (bmap)
586 {
587 long xx = XLOG2DEV(xdest);
588 long yy = YLOG2DEV(ydest);
589
590 GdkBitmap *mask = (GdkBitmap *) NULL;
591 if (srcDC->m_selected.GetMask()) mask = srcDC->m_selected.GetMask()->GetBitmap();
592
593 if (useMask && mask)
594 {
595 gdk_gc_set_clip_mask( m_textGC, mask );
596 gdk_gc_set_clip_origin( m_textGC, xx, yy );
597 }
598
599 gdk_draw_bitmap( m_window, m_textGC, bmap,
600 source->DeviceToLogicalX(xsrc),
601 source->DeviceToLogicalY(ysrc),
602 xx,
603 yy,
604 source->DeviceToLogicalXRel(width),
605 source->DeviceToLogicalYRel(height) );
606
607 if (useMask && mask)
608 {
609 gdk_gc_set_clip_mask( m_textGC, (GdkBitmap *) NULL );
610 gdk_gc_set_clip_origin( m_textGC, 0, 0 );
611 }
612
613 return TRUE;
614 }
615 }
616
617 gdk_window_copy_area ( m_window, m_textGC,
618 XLOG2DEV(xdest), YLOG2DEV(ydest),
619 csrc->GetWindow(),
620 source->DeviceToLogicalX(xsrc),
621 source->DeviceToLogicalY(ysrc),
622 source->DeviceToLogicalXRel(width),
623 source->DeviceToLogicalYRel(height) );
624
625 /*
626 gdk_window_copy_area ( m_window, m_textGC,
627 XLOG2DEV(xdest), YLOG2DEV(ydest),
628 csrc->GetWindow(),
629 xsrc, ysrc,
630 width, height );
631 */
632
633 return TRUE;
634 }
635
636 void wxWindowDC::DrawText( const wxString &text, long x, long y, bool WXUNUSED(use16) )
637 {
638 if (!Ok()) return;
639
640 GdkFont *font = m_font.GetInternalFont( m_scaleY );
641
642 x = XLOG2DEV(x);
643 y = YLOG2DEV(y);
644
645 // CMB 21/5/98: draw text background if mode is wxSOLID
646 if (m_backgroundMode == wxSOLID)
647 {
648 long width = gdk_string_width( font, text );
649 long height = font->ascent + font->descent;
650 gdk_gc_set_foreground( m_textGC, m_textBackgroundColour.GetColor() );
651 gdk_draw_rectangle( m_window, m_textGC, TRUE, x, y, width, height );
652 gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
653 }
654 gdk_draw_string( m_window, font, m_textGC, x, y + font->ascent, text );
655
656 // CMB 17/7/98: simple underline: ignores scaling and underlying
657 // X font's XA_UNDERLINE_POSITION and XA_UNDERLINE_THICKNESS
658 // properties (see wxXt implementation)
659 if (m_font.GetUnderlined())
660 {
661 long width = gdk_string_width( font, text );
662 long ul_y = y + font->ascent;
663 if (font->descent > 0) ul_y++;
664 gdk_draw_line( m_window, m_textGC, x, ul_y, x + width, ul_y);
665 }
666
667 long w, h;
668 GetTextExtent (text, &w, &h);
669 CalcBoundingBox (x + w, y + h);
670 CalcBoundingBox (x, y);
671 }
672
673 bool wxWindowDC::CanGetTextExtent(void) const
674 {
675 return TRUE;
676 }
677
678 void wxWindowDC::GetTextExtent( const wxString &string, long *width, long *height,
679 long *descent, long *externalLeading,
680 wxFont *theFont, bool WXUNUSED(use16) )
681 {
682 if (!Ok()) return;
683
684 wxFont fontToUse = m_font;
685 if (theFont) fontToUse = *theFont;
686
687 GdkFont *font = fontToUse.GetInternalFont( m_scaleY );
688 if (width) (*width) = long(gdk_string_width( font, string ) / m_scaleX);
689 if (height) (*height) = long((font->ascent + font->descent) / m_scaleY);
690 if (descent) (*descent) = long(font->descent / m_scaleY);
691 if (externalLeading) (*externalLeading) = 0; // ??
692 }
693
694 long wxWindowDC::GetCharWidth(void)
695 {
696 if (!Ok()) return 0;
697
698 GdkFont *font = m_font.GetInternalFont( m_scaleY );
699 return long(gdk_string_width( font, "H" ) / m_scaleX);
700 }
701
702 long wxWindowDC::GetCharHeight(void)
703 {
704 if (!Ok()) return 0;
705
706 GdkFont *font = m_font.GetInternalFont( m_scaleY );
707 return long((font->ascent + font->descent) / m_scaleY);
708 }
709
710 void wxWindowDC::Clear(void)
711 {
712 if (!Ok()) return;
713
714 if (!m_isMemDC)
715 {
716 gdk_window_clear( m_window );
717 }
718 else
719 {
720 int width,height;
721 GetSize( &width, &height );
722 gdk_draw_rectangle( m_window, m_bgGC, TRUE, 0, 0, width, height );
723 }
724 }
725
726 void wxWindowDC::SetFont( const wxFont &font )
727 {
728 if (!Ok()) return;
729
730 m_font = font;
731 }
732
733 void wxWindowDC::SetPen( const wxPen &pen )
734 {
735 if (!Ok()) return;
736
737 if (m_pen == pen) return;
738
739 m_pen = pen;
740
741 if (!m_pen.Ok()) return;
742
743 gint width = m_pen.GetWidth();
744 // CMB: if width is non-zero scale it with the dc
745 if (width <= 0)
746 {
747 width = 1;
748 }
749 else
750 {
751 // X doesn't allow different width in x and y and so we take
752 // the average
753 double w = 0.5 + (abs(XLOG2DEVREL(width)) + abs(YLOG2DEVREL(width))) / 2.0;
754 width = (int)w;
755 }
756
757 GdkLineStyle lineStyle = GDK_LINE_SOLID;
758 switch (m_pen.GetStyle())
759 {
760 case wxSOLID: { lineStyle = GDK_LINE_SOLID; break; }
761 case wxDOT: { lineStyle = GDK_LINE_ON_OFF_DASH; break; }
762 case wxLONG_DASH: { lineStyle = GDK_LINE_ON_OFF_DASH; break; }
763 case wxSHORT_DASH: { lineStyle = GDK_LINE_ON_OFF_DASH; break; }
764 case wxDOT_DASH: { lineStyle = GDK_LINE_DOUBLE_DASH; break; }
765 }
766
767 GdkCapStyle capStyle = GDK_CAP_ROUND;
768 switch (m_pen.GetCap())
769 {
770 case wxCAP_ROUND: { capStyle = (width <= 1) ? GDK_CAP_NOT_LAST : GDK_CAP_ROUND; break; }
771 case wxCAP_PROJECTING: { capStyle = GDK_CAP_PROJECTING; break; }
772 case wxCAP_BUTT: { capStyle = GDK_CAP_BUTT; break; }
773 }
774
775 GdkJoinStyle joinStyle = GDK_JOIN_ROUND;
776 switch (m_pen.GetJoin())
777 {
778 case wxJOIN_BEVEL: { joinStyle = GDK_JOIN_BEVEL; break; }
779 case wxJOIN_ROUND: { joinStyle = GDK_JOIN_ROUND; break; }
780 case wxJOIN_MITER: { joinStyle = GDK_JOIN_MITER; break; }
781 }
782
783 gdk_gc_set_line_attributes( m_penGC, width, lineStyle, capStyle, joinStyle );
784
785 m_pen.GetColour().CalcPixel( m_cmap );
786 gdk_gc_set_foreground( m_penGC, m_pen.GetColour().GetColor() );
787 }
788
789 void wxWindowDC::SetBrush( const wxBrush &brush )
790 {
791 if (!Ok()) return;
792
793 if (m_brush == brush) return;
794
795 m_brush = brush;
796
797 if (!m_brush.Ok()) return;
798
799 m_brush.GetColour().CalcPixel( m_cmap );
800 gdk_gc_set_foreground( m_brushGC, m_brush.GetColour().GetColor() );
801
802 GdkFill fillStyle = GDK_SOLID;
803 switch (m_brush.GetStyle())
804 {
805 case wxSOLID:
806 case wxTRANSPARENT:
807 break;
808 default:
809 fillStyle = GDK_STIPPLED;
810 }
811
812 gdk_gc_set_fill( m_brushGC, fillStyle );
813
814 if (m_brush.GetStyle() == wxSTIPPLE)
815 {
816 gdk_gc_set_stipple( m_brushGC, m_brush.GetStipple()->GetPixmap() );
817 }
818
819 if (IS_HATCH(m_brush.GetStyle()))
820 {
821 int num = m_brush.GetStyle() - wxBDIAGONAL_HATCH;
822 gdk_gc_set_stipple( m_brushGC, hatches[num] );
823 }
824 }
825
826 // CMB 21/7/98: Added SetBackground. Sets background brush
827 // for Clear() and bg colour for shapes filled with cross-hatch brush
828 void wxWindowDC::SetBackground( const wxBrush &brush )
829 {
830 if (!Ok()) return;
831
832 if (m_backgroundBrush == brush) return;
833
834 m_backgroundBrush = brush;
835
836 if (!m_backgroundBrush.Ok()) return;
837
838 m_backgroundBrush.GetColour().CalcPixel( m_cmap );
839 gdk_gc_set_background( m_brushGC, m_backgroundBrush.GetColour().GetColor() );
840 gdk_gc_set_background( m_penGC, m_backgroundBrush.GetColour().GetColor() );
841 gdk_gc_set_background( m_bgGC, m_backgroundBrush.GetColour().GetColor() );
842 gdk_gc_set_foreground( m_bgGC, m_backgroundBrush.GetColour().GetColor() );
843
844 GdkFill fillStyle = GDK_SOLID;
845 switch (m_backgroundBrush.GetStyle())
846 {
847 case wxSOLID:
848 case wxTRANSPARENT:
849 break;
850 default:
851 fillStyle = GDK_STIPPLED;
852 }
853
854 gdk_gc_set_fill( m_bgGC, fillStyle );
855
856 if (m_backgroundBrush.GetStyle() == wxSTIPPLE)
857 {
858 gdk_gc_set_stipple( m_bgGC, m_backgroundBrush.GetStipple()->GetPixmap() );
859 }
860
861 if (IS_HATCH(m_backgroundBrush.GetStyle()))
862 {
863 int num = m_backgroundBrush.GetStyle() - wxBDIAGONAL_HATCH;
864 gdk_gc_set_stipple( m_bgGC, hatches[num] );
865 }
866 }
867
868 void wxWindowDC::SetLogicalFunction( int function )
869 {
870 if (m_logicalFunction == function) return;
871 GdkFunction mode = GDK_COPY;
872 switch (function)
873 {
874 case wxXOR: mode = GDK_INVERT; break;
875 case wxINVERT: mode = GDK_INVERT; break;
876 default: break;
877 }
878 m_logicalFunction = function;
879 gdk_gc_set_function( m_penGC, mode );
880 gdk_gc_set_function( m_brushGC, mode );
881 gdk_gc_set_function( m_textGC, mode );
882 }
883
884 void wxWindowDC::SetTextForeground( const wxColour &col )
885 {
886 if (!Ok()) return;
887
888 if (m_textForegroundColour == col) return;
889
890 m_textForegroundColour = col;
891 if (!m_textForegroundColour.Ok()) return;
892
893 m_textForegroundColour.CalcPixel( m_cmap );
894 gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
895 }
896
897 void wxWindowDC::SetTextBackground( const wxColour &col )
898 {
899 if (!Ok()) return;
900
901 if (m_textBackgroundColour == col) return;
902
903 m_textBackgroundColour = col;
904 if (!m_textBackgroundColour.Ok()) return;
905
906 m_textBackgroundColour.CalcPixel( m_cmap );
907 gdk_gc_set_background( m_textGC, m_textBackgroundColour.GetColor() );
908 }
909
910 void wxWindowDC::SetBackgroundMode( int mode )
911 {
912 m_backgroundMode = mode;
913
914 // CMB 21/7/98: fill style of cross-hatch brushes is affected by
915 // transparent/solid background mode
916 if (m_brush.GetStyle() != wxSOLID && m_brush.GetStyle() != wxTRANSPARENT)
917 {
918 gdk_gc_set_fill( m_brushGC,
919 (m_backgroundMode == wxTRANSPARENT) ? GDK_STIPPLED : GDK_OPAQUE_STIPPLED);
920 }
921 }
922
923 void wxWindowDC::SetPalette( const wxPalette& WXUNUSED(palette) )
924 {
925 }
926
927 void wxWindowDC::SetClippingRegion( long x, long y, long width, long height )
928 {
929 wxDC::SetClippingRegion( x, y, width, height );
930
931 GdkRectangle rect;
932 rect.x = XLOG2DEV(x);
933 rect.y = YLOG2DEV(y);
934 rect.width = XLOG2DEVREL(width);
935 rect.height = YLOG2DEVREL(height);
936 gdk_gc_set_clip_rectangle( m_penGC, &rect );
937 gdk_gc_set_clip_rectangle( m_brushGC, &rect );
938 gdk_gc_set_clip_rectangle( m_textGC, &rect );
939 gdk_gc_set_clip_rectangle( m_bgGC, &rect );
940 }
941
942 void wxWindowDC::DestroyClippingRegion(void)
943 {
944 wxDC::DestroyClippingRegion();
945
946 gdk_gc_set_clip_rectangle( m_penGC, (GdkRectangle *) NULL );
947 gdk_gc_set_clip_rectangle( m_brushGC, (GdkRectangle *) NULL );
948 gdk_gc_set_clip_rectangle( m_textGC, (GdkRectangle *) NULL );
949 gdk_gc_set_clip_rectangle( m_bgGC, (GdkRectangle *) NULL );
950 }
951
952 void wxWindowDC::SetUpDC(void)
953 {
954 Destroy();
955 m_ok = TRUE;
956 m_logicalFunction = wxCOPY;
957 m_penGC = gdk_gc_new( m_window );
958 m_brushGC = gdk_gc_new( m_window );
959 m_textGC = gdk_gc_new( m_window );
960 m_bgGC = gdk_gc_new( m_window );
961
962 wxColour tmp_col( m_textForegroundColour );
963 m_textForegroundColour = wxNullColour;
964 SetTextForeground( tmp_col );
965 tmp_col = m_textBackgroundColour;
966 m_textBackgroundColour = wxNullColour;
967 SetTextBackground( tmp_col );
968
969 wxPen tmp_pen( m_pen );
970 m_pen = wxNullPen;
971 SetPen( tmp_pen );
972
973 wxFont tmp_font( m_font );
974 m_font = wxNullFont;
975 SetFont( tmp_font );
976
977 wxBrush tmp_brush( m_brush );
978 m_brush = wxNullBrush;
979 SetBrush( tmp_brush );
980
981 tmp_brush = m_backgroundBrush;
982 m_backgroundBrush = wxNullBrush;
983 SetBackground( tmp_brush );
984
985 if (!hatch_bitmap)
986 {
987 hatch_bitmap = hatches;
988 hatch_bitmap[0] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, bdiag_bits, bdiag_width, bdiag_height );
989 hatch_bitmap[1] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, cdiag_bits, cdiag_width, cdiag_height );
990 hatch_bitmap[2] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, fdiag_bits, fdiag_width, fdiag_height );
991 hatch_bitmap[3] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, cross_bits, cross_width, cross_height );
992 hatch_bitmap[4] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, horiz_bits, horiz_width, horiz_height );
993 hatch_bitmap[5] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, verti_bits, verti_width, verti_height );
994 }
995 }
996
997 void wxWindowDC::Destroy(void)
998 {
999 if (m_penGC) gdk_gc_unref( m_penGC );
1000 m_penGC = (GdkGC*) NULL;
1001 if (m_brushGC) gdk_gc_unref( m_brushGC );
1002 m_brushGC = (GdkGC*) NULL;
1003 if (m_textGC) gdk_gc_unref( m_textGC );
1004 m_textGC = (GdkGC*) NULL;
1005 if (m_bgGC) gdk_gc_unref( m_bgGC );
1006 m_bgGC = (GdkGC*) NULL;
1007 }
1008
1009 GdkWindow *wxWindowDC::GetWindow(void)
1010 {
1011 return m_window;
1012 }
1013
1014 // ----------------------------------- spline code ----------------------------------------
1015
1016 void wx_quadratic_spline(double a1, double b1, double a2, double b2,
1017 double a3, double b3, double a4, double b4);
1018 void wx_clear_stack(void);
1019 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2, double *x3,
1020 double *y3, double *x4, double *y4);
1021 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3,
1022 double x4, double y4);
1023 static bool wx_spline_add_point(double x, double y);
1024 static void wx_spline_draw_point_array(wxDC *dc);
1025
1026 wxList wx_spline_point_list;
1027
1028 #define half(z1, z2) ((z1+z2)/2.0)
1029 #define THRESHOLD 5
1030
1031 /* iterative version */
1032
1033 void wx_quadratic_spline(double a1, double b1, double a2, double b2, double a3, double b3, double a4,
1034 double b4)
1035 {
1036 register double xmid, ymid;
1037 double x1, y1, x2, y2, x3, y3, x4, y4;
1038
1039 wx_clear_stack();
1040 wx_spline_push(a1, b1, a2, b2, a3, b3, a4, b4);
1041
1042 while (wx_spline_pop(&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4)) {
1043 xmid = (double)half(x2, x3);
1044 ymid = (double)half(y2, y3);
1045 if (fabs(x1 - xmid) < THRESHOLD && fabs(y1 - ymid) < THRESHOLD &&
1046 fabs(xmid - x4) < THRESHOLD && fabs(ymid - y4) < THRESHOLD) {
1047 wx_spline_add_point( x1, y1 );
1048 wx_spline_add_point( xmid, ymid );
1049 } else {
1050 wx_spline_push(xmid, ymid, (double)half(xmid, x3), (double)half(ymid, y3),
1051 (double)half(x3, x4), (double)half(y3, y4), x4, y4);
1052 wx_spline_push(x1, y1, (double)half(x1, x2), (double)half(y1, y2),
1053 (double)half(x2, xmid), (double)half(y2, ymid), xmid, ymid);
1054 }
1055 }
1056 }
1057
1058 /* utilities used by spline drawing routines */
1059
1060 typedef struct wx_spline_stack_struct {
1061 double x1, y1, x2, y2, x3, y3, x4, y4;
1062 } Stack;
1063
1064 #define SPLINE_STACK_DEPTH 20
1065 static Stack wx_spline_stack[SPLINE_STACK_DEPTH];
1066 static Stack *wx_stack_top;
1067 static int wx_stack_count;
1068
1069 void wx_clear_stack(void)
1070 {
1071 wx_stack_top = wx_spline_stack;
1072 wx_stack_count = 0;
1073 }
1074
1075 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
1076 {
1077 wx_stack_top->x1 = x1;
1078 wx_stack_top->y1 = y1;
1079 wx_stack_top->x2 = x2;
1080 wx_stack_top->y2 = y2;
1081 wx_stack_top->x3 = x3;
1082 wx_stack_top->y3 = y3;
1083 wx_stack_top->x4 = x4;
1084 wx_stack_top->y4 = y4;
1085 wx_stack_top++;
1086 wx_stack_count++;
1087 }
1088
1089 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2,
1090 double *x3, double *y3, double *x4, double *y4)
1091 {
1092 if (wx_stack_count == 0)
1093 return (0);
1094 wx_stack_top--;
1095 wx_stack_count--;
1096 *x1 = wx_stack_top->x1;
1097 *y1 = wx_stack_top->y1;
1098 *x2 = wx_stack_top->x2;
1099 *y2 = wx_stack_top->y2;
1100 *x3 = wx_stack_top->x3;
1101 *y3 = wx_stack_top->y3;
1102 *x4 = wx_stack_top->x4;
1103 *y4 = wx_stack_top->y4;
1104 return (1);
1105 }
1106
1107 static bool wx_spline_add_point(double x, double y)
1108 {
1109 wxPoint *point = new wxPoint ;
1110 point->x = (int) x;
1111 point->y = (int) y;
1112 wx_spline_point_list.Append((wxObject*)point);
1113 return TRUE;
1114 }
1115
1116 static void wx_spline_draw_point_array(wxDC *dc)
1117 {
1118 dc->DrawLines(&wx_spline_point_list, 0, 0 );
1119 wxNode *node = wx_spline_point_list.First();
1120 while (node)
1121 {
1122 wxPoint *point = (wxPoint *)node->Data();
1123 delete point;
1124 delete node;
1125 node = wx_spline_point_list.First();
1126 }
1127 }
1128
1129 void wxWindowDC::DrawSpline( wxList *points )
1130 {
1131 wxPoint *p;
1132 double cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4;
1133 double x1, y1, x2, y2;
1134
1135 wxNode *node = points->First();
1136 p = (wxPoint *)node->Data();
1137
1138 x1 = p->x;
1139 y1 = p->y;
1140
1141 node = node->Next();
1142 p = (wxPoint *)node->Data();
1143
1144 x2 = p->x;
1145 y2 = p->y;
1146 cx1 = (double)((x1 + x2) / 2);
1147 cy1 = (double)((y1 + y2) / 2);
1148 cx2 = (double)((cx1 + x2) / 2);
1149 cy2 = (double)((cy1 + y2) / 2);
1150
1151 wx_spline_add_point(x1, y1);
1152
1153 while ((node = node->Next()) != NULL)
1154 {
1155 p = (wxPoint *)node->Data();
1156 x1 = x2;
1157 y1 = y2;
1158 x2 = p->x;
1159 y2 = p->y;
1160 cx4 = (double)(x1 + x2) / 2;
1161 cy4 = (double)(y1 + y2) / 2;
1162 cx3 = (double)(x1 + cx4) / 2;
1163 cy3 = (double)(y1 + cy4) / 2;
1164
1165 wx_quadratic_spline(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
1166
1167 cx1 = cx4;
1168 cy1 = cy4;
1169 cx2 = (double)(cx1 + x2) / 2;
1170 cy2 = (double)(cy1 + y2) / 2;
1171 }
1172
1173 wx_spline_add_point( cx1, cy1 );
1174 wx_spline_add_point( x2, y2 );
1175
1176 wx_spline_draw_point_array( this );
1177 }
1178
1179
1180 //-----------------------------------------------------------------------------
1181 // wxPaintDC
1182 //-----------------------------------------------------------------------------
1183
1184 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC,wxWindowDC)
1185
1186 wxPaintDC::wxPaintDC(void)
1187 : wxWindowDC()
1188 {
1189 }
1190
1191 wxPaintDC::wxPaintDC( wxWindow *win )
1192 : wxWindowDC( win )
1193 {
1194 }
1195
1196 //-----------------------------------------------------------------------------
1197 // wxClientDC
1198 //-----------------------------------------------------------------------------
1199
1200 IMPLEMENT_DYNAMIC_CLASS(wxClientDC,wxWindowDC)
1201
1202 wxClientDC::wxClientDC(void)
1203 : wxWindowDC()
1204 {
1205 }
1206
1207 wxClientDC::wxClientDC( wxWindow *win )
1208 : wxWindowDC( win )
1209 {
1210 }
1211