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