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