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