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