]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dcclient.cpp
* Bug fixes
[wxWidgets.git] / src / gtk1 / 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 // CMB 21/7/98: Added SetBackground. Sets background brush
653 // for Clear() and bg colour for shapes filled with cross-hatch brush
654 void wxPaintDC::SetBackground( const wxBrush &brush )
655 {
656 if (!Ok()) return;
657
658 if (m_backgroundBrush == brush) return;
659
660 m_backgroundBrush = brush;
661
662 if (!m_backgroundBrush.Ok()) return;
663
664 m_backgroundBrush.GetColour().CalcPixel( m_cmap );
665 gdk_gc_set_background( m_brushGC, m_backgroundBrush.GetColour().GetColor() );
666 gdk_gc_set_foreground( m_bgGC, m_backgroundBrush.GetColour().GetColor() );
667
668 GdkFill fillStyle = GDK_SOLID;
669 switch (m_backgroundBrush.GetStyle())
670 {
671 case wxSOLID:
672 case wxTRANSPARENT:
673 break;
674 default:
675 fillStyle = GDK_STIPPLED;
676 };
677
678 gdk_gc_set_fill( m_bgGC, fillStyle );
679
680 if (m_backgroundBrush.GetStyle() == wxSTIPPLE)
681 {
682 gdk_gc_set_stipple( m_bgGC, m_backgroundBrush.GetStipple()->GetPixmap() );
683 };
684
685 if (IS_HATCH(m_backgroundBrush.GetStyle()))
686 {
687 int num = m_backgroundBrush.GetStyle() - wxBDIAGONAL_HATCH;
688 gdk_gc_set_stipple( m_bgGC, hatches[num] );
689 };
690 };
691
692 void wxPaintDC::SetLogicalFunction( int function )
693 {
694 if (m_logicalFunction == function) return;
695 GdkFunction mode = GDK_COPY;
696 switch (function)
697 {
698 case wxXOR: mode = GDK_INVERT; break;
699 case wxINVERT: mode = GDK_INVERT; break;
700 default: break;
701 };
702 m_logicalFunction = function;
703 gdk_gc_set_function( m_penGC, mode );
704 gdk_gc_set_function( m_brushGC, mode );
705 };
706
707 void wxPaintDC::SetTextForeground( const wxColour &col )
708 {
709 if (!Ok()) return;
710
711 if (m_textForegroundColour == col) return;
712
713 m_textForegroundColour = col;
714 if (!m_textForegroundColour.Ok()) return;
715
716 m_textForegroundColour.CalcPixel( m_cmap );
717 gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
718 };
719
720 void wxPaintDC::SetTextBackground( const wxColour &col )
721 {
722 if (!Ok()) return;
723
724 if (m_textBackgroundColour == col) return;
725
726 m_textBackgroundColour = col;
727 if (!m_textBackgroundColour.Ok()) return;
728
729 m_textBackgroundColour.CalcPixel( m_cmap );
730 gdk_gc_set_background( m_textGC, m_textBackgroundColour.GetColor() );
731 };
732
733 void wxPaintDC::SetBackgroundMode( int mode )
734 {
735 m_backgroundMode = mode;
736
737 // CMB 21/7/98: fill style of cross-hatch brushes is affected by
738 // transparent/solid background mode
739 if (m_brush.GetStyle() != wxSOLID && m_brush.GetStyle() != wxTRANSPARENT)
740 {
741 gdk_gc_set_fill( m_brushGC,
742 (m_backgroundMode == wxTRANSPARENT) ? GDK_STIPPLED : GDK_OPAQUE_STIPPLED);
743 }
744 };
745
746 void wxPaintDC::SetPalette( const wxPalette& WXUNUSED(palette) )
747 {
748 };
749
750 void wxPaintDC::SetClippingRegion( long x, long y, long width, long height )
751 {
752 wxDC::SetClippingRegion( x, y, width, height );
753
754 GdkRectangle rect;
755 rect.x = XLOG2DEV(x);
756 rect.y = YLOG2DEV(y);
757 rect.width = XLOG2DEV(x+width);
758 rect.height = YLOG2DEV(y+height);
759 gdk_gc_set_clip_rectangle( m_penGC, &rect );
760 gdk_gc_set_clip_rectangle( m_brushGC, &rect );
761 gdk_gc_set_clip_rectangle( m_textGC, &rect );
762 gdk_gc_set_clip_rectangle( m_bgGC, &rect );
763
764 };
765
766 void wxPaintDC::DestroyClippingRegion(void)
767 {
768 wxDC::DestroyClippingRegion();
769
770 gdk_gc_set_clip_rectangle( m_penGC, NULL );
771 gdk_gc_set_clip_rectangle( m_brushGC, NULL );
772 gdk_gc_set_clip_rectangle( m_textGC, NULL );
773 gdk_gc_set_clip_rectangle( m_bgGC, NULL );
774 };
775
776 void wxPaintDC::SetUpDC(void)
777 {
778 m_ok = TRUE;
779 m_logicalFunction = wxCOPY;
780 m_penGC = gdk_gc_new( m_window );
781 m_brushGC = gdk_gc_new( m_window );
782 m_textGC = gdk_gc_new( m_window );
783 m_bgGC = gdk_gc_new( m_window );
784 SetTextForeground( m_textForegroundColour );
785 SetTextBackground( m_textBackgroundColour );
786 SetPen( m_pen );
787 SetFont( m_font );
788 SetBrush( m_brush );
789
790 gdk_gc_set_background( m_penGC, wxWHITE->GetColor() );
791
792 if (!hatch_bitmap)
793 {
794 hatch_bitmap = hatches;
795 hatch_bitmap[0] = gdk_bitmap_create_from_data( NULL, bdiag_bits, bdiag_width, bdiag_height );
796 hatch_bitmap[1] = gdk_bitmap_create_from_data( NULL, cdiag_bits, cdiag_width, cdiag_height );
797 hatch_bitmap[2] = gdk_bitmap_create_from_data( NULL, fdiag_bits, fdiag_width, fdiag_height );
798 hatch_bitmap[3] = gdk_bitmap_create_from_data( NULL, cross_bits, cross_width, cross_height );
799 hatch_bitmap[4] = gdk_bitmap_create_from_data( NULL, horiz_bits, horiz_width, horiz_height );
800 hatch_bitmap[5] = gdk_bitmap_create_from_data( NULL, verti_bits, verti_width, verti_height );
801 };
802 };
803
804 GdkWindow *wxPaintDC::GetWindow(void)
805 {
806 return m_window;
807 };
808
809 // ----------------------------------- spline code ----------------------------------------
810
811 void wx_quadratic_spline(double a1, double b1, double a2, double b2,
812 double a3, double b3, double a4, double b4);
813 void wx_clear_stack(void);
814 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2, double *x3,
815 double *y3, double *x4, double *y4);
816 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3,
817 double x4, double y4);
818 static bool wx_spline_add_point(double x, double y);
819 static void wx_spline_draw_point_array(wxDC *dc);
820
821 wxList wx_spline_point_list;
822
823 #define half(z1, z2) ((z1+z2)/2.0)
824 #define THRESHOLD 5
825
826 /* iterative version */
827
828 void wx_quadratic_spline(double a1, double b1, double a2, double b2, double a3, double b3, double a4,
829 double b4)
830 {
831 register double xmid, ymid;
832 double x1, y1, x2, y2, x3, y3, x4, y4;
833
834 wx_clear_stack();
835 wx_spline_push(a1, b1, a2, b2, a3, b3, a4, b4);
836
837 while (wx_spline_pop(&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4)) {
838 xmid = (double)half(x2, x3);
839 ymid = (double)half(y2, y3);
840 if (fabs(x1 - xmid) < THRESHOLD && fabs(y1 - ymid) < THRESHOLD &&
841 fabs(xmid - x4) < THRESHOLD && fabs(ymid - y4) < THRESHOLD) {
842 wx_spline_add_point( x1, y1 );
843 wx_spline_add_point( xmid, ymid );
844 } else {
845 wx_spline_push(xmid, ymid, (double)half(xmid, x3), (double)half(ymid, y3),
846 (double)half(x3, x4), (double)half(y3, y4), x4, y4);
847 wx_spline_push(x1, y1, (double)half(x1, x2), (double)half(y1, y2),
848 (double)half(x2, xmid), (double)half(y2, ymid), xmid, ymid);
849 }
850 }
851 }
852
853 /* utilities used by spline drawing routines */
854
855 typedef struct wx_spline_stack_struct {
856 double x1, y1, x2, y2, x3, y3, x4, y4;
857 } Stack;
858
859 #define SPLINE_STACK_DEPTH 20
860 static Stack wx_spline_stack[SPLINE_STACK_DEPTH];
861 static Stack *wx_stack_top;
862 static int wx_stack_count;
863
864 void wx_clear_stack(void)
865 {
866 wx_stack_top = wx_spline_stack;
867 wx_stack_count = 0;
868 }
869
870 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
871 {
872 wx_stack_top->x1 = x1;
873 wx_stack_top->y1 = y1;
874 wx_stack_top->x2 = x2;
875 wx_stack_top->y2 = y2;
876 wx_stack_top->x3 = x3;
877 wx_stack_top->y3 = y3;
878 wx_stack_top->x4 = x4;
879 wx_stack_top->y4 = y4;
880 wx_stack_top++;
881 wx_stack_count++;
882 }
883
884 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2,
885 double *x3, double *y3, double *x4, double *y4)
886 {
887 if (wx_stack_count == 0)
888 return (0);
889 wx_stack_top--;
890 wx_stack_count--;
891 *x1 = wx_stack_top->x1;
892 *y1 = wx_stack_top->y1;
893 *x2 = wx_stack_top->x2;
894 *y2 = wx_stack_top->y2;
895 *x3 = wx_stack_top->x3;
896 *y3 = wx_stack_top->y3;
897 *x4 = wx_stack_top->x4;
898 *y4 = wx_stack_top->y4;
899 return (1);
900 }
901
902 static bool wx_spline_add_point(double x, double y)
903 {
904 wxPoint *point = new wxPoint ;
905 point->x = (int) x;
906 point->y = (int) y;
907 wx_spline_point_list.Append((wxObject*)point);
908 return TRUE;
909 }
910
911 static void wx_spline_draw_point_array(wxDC *dc)
912 {
913 dc->DrawLines(&wx_spline_point_list, 0, 0 );
914 wxNode *node = wx_spline_point_list.First();
915 while (node)
916 {
917 wxPoint *point = (wxPoint *)node->Data();
918 delete point;
919 delete node;
920 node = wx_spline_point_list.First();
921 }
922 }
923
924 void wxPaintDC::DrawOpenSpline( wxList *points )
925 {
926 wxPoint *p;
927 double cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4;
928 double x1, y1, x2, y2;
929
930 wxNode *node = points->First();
931 p = (wxPoint *)node->Data();
932
933 x1 = p->x;
934 y1 = p->y;
935
936 node = node->Next();
937 p = (wxPoint *)node->Data();
938
939 x2 = p->x;
940 y2 = p->y;
941 cx1 = (double)((x1 + x2) / 2);
942 cy1 = (double)((y1 + y2) / 2);
943 cx2 = (double)((cx1 + x2) / 2);
944 cy2 = (double)((cy1 + y2) / 2);
945
946 wx_spline_add_point(x1, y1);
947
948 while ((node = node->Next()) != NULL)
949 {
950 p = (wxPoint *)node->Data();
951 x1 = x2;
952 y1 = y2;
953 x2 = p->x;
954 y2 = p->y;
955 cx4 = (double)(x1 + x2) / 2;
956 cy4 = (double)(y1 + y2) / 2;
957 cx3 = (double)(x1 + cx4) / 2;
958 cy3 = (double)(y1 + cy4) / 2;
959
960 wx_quadratic_spline(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
961
962 cx1 = cx4;
963 cy1 = cy4;
964 cx2 = (double)(cx1 + x2) / 2;
965 cy2 = (double)(cy1 + y2) / 2;
966 }
967
968 wx_spline_add_point( cx1, cy1 );
969 wx_spline_add_point( x2, y2 );
970
971 wx_spline_draw_point_array( this );
972 };