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