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