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