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