Implemented wxScreenDC the hard way
[wxWidgets.git] / src / gtk1 / 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 WXUNUSED(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 GdkBitmap* bmap = srcDC->m_selected.GetBitmap();
489 if (bmap)
490 {
491 gdk_draw_bitmap( m_window, m_textGC, bmap,
492 source->DeviceToLogicalX(xsrc),
493 source->DeviceToLogicalY(ysrc),
494 XLOG2DEV(xdest),
495 YLOG2DEV(ydest),
496 source->DeviceToLogicalXRel(width),
497 source->DeviceToLogicalYRel(height) );
498 return TRUE;
499 }
500 }
501
502 gdk_window_copy_area ( m_window, m_penGC,
503 XLOG2DEV(xdest), YLOG2DEV(ydest),
504 csrc->GetWindow(),
505 source->DeviceToLogicalX(xsrc), source->DeviceToLogicalY(ysrc),
506 source->DeviceToLogicalXRel(width), source->DeviceToLogicalYRel(height) );
507
508 /*
509 gdk_window_copy_area ( m_window, m_penGC,
510 XLOG2DEV(xdest), YLOG2DEV(ydest),
511 csrc->GetWindow(),
512 xsrc, ysrc,
513 width, height );
514 */
515
516 return TRUE;
517 }
518
519 void wxPaintDC::DrawText( const wxString &text, long x, long y, bool WXUNUSED(use16) )
520 {
521 if (!Ok()) return;
522
523 GdkFont *font = m_font.GetInternalFont( m_scaleY );
524
525 x = XLOG2DEV(x);
526 y = YLOG2DEV(y);
527
528 // CMB 21/5/98: draw text background if mode is wxSOLID
529 if (m_backgroundMode == wxSOLID)
530 {
531 long width = gdk_string_width( font, text );
532 long height = font->ascent + font->descent;
533 gdk_gc_set_foreground( m_textGC, m_textBackgroundColour.GetColor() );
534 gdk_draw_rectangle( m_window, m_textGC, TRUE, x, y, width, height );
535 gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
536 }
537 gdk_draw_string( m_window, font, m_textGC, x, y + font->ascent, text );
538
539 // CMB 17/7/98: simple underline: ignores scaling and underlying
540 // X font's XA_UNDERLINE_POSITION and XA_UNDERLINE_THICKNESS
541 // properties (see wxXt implementation)
542 if (m_font.GetUnderlined())
543 {
544 long width = gdk_string_width( font, text );
545 long ul_y = y + font->ascent;
546 if (font->descent > 0) ul_y++;
547 gdk_draw_line( m_window, m_textGC, x, ul_y, x + width, ul_y);
548 }
549 }
550
551 bool wxPaintDC::CanGetTextExtent(void) const
552 {
553 return TRUE;
554 }
555
556 void wxPaintDC::GetTextExtent( const wxString &string, long *width, long *height,
557 long *descent, long *externalLeading,
558 wxFont *theFont, bool WXUNUSED(use16) )
559 {
560 if (!Ok()) return;
561
562 wxFont fontToUse = m_font;
563 if (theFont) fontToUse = *theFont;
564
565 GdkFont *font = fontToUse.GetInternalFont( m_scaleY );
566 if (width) (*width) = long(gdk_string_width( font, string ) / m_scaleX);
567 if (height) (*height) = long((font->ascent + font->descent) / m_scaleY);
568 if (descent) (*descent) = long(font->descent / m_scaleY);
569 if (externalLeading) (*externalLeading) = 0; // ??
570 }
571
572 long wxPaintDC::GetCharWidth(void)
573 {
574 if (!Ok()) return 0;
575
576 GdkFont *font = m_font.GetInternalFont( m_scaleY );
577 return long(gdk_string_width( font, "H" ) / m_scaleX);
578 }
579
580 long wxPaintDC::GetCharHeight(void)
581 {
582 if (!Ok()) return 0;
583
584 GdkFont *font = m_font.GetInternalFont( m_scaleY );
585 return long((font->ascent + font->descent) / m_scaleY);
586 }
587
588 void wxPaintDC::Clear(void)
589 {
590 if (!Ok()) return;
591
592 if (!m_isMemDC)
593 {
594 gdk_window_clear( m_window );
595 }
596 else
597 {
598 int width = 0;
599 int height = 0;
600 GetSize( &width, &height );
601 gdk_draw_rectangle( m_window, m_bgGC, TRUE, 0, 0, width, height );
602 }
603 }
604
605 void wxPaintDC::SetFont( const wxFont &font )
606 {
607 if (!Ok()) return;
608
609 m_font = font;
610 }
611
612 void wxPaintDC::SetPen( const wxPen &pen )
613 {
614 if (!Ok()) return;
615
616 if (m_pen == pen) return;
617
618 m_pen = pen;
619
620 if (!m_pen.Ok()) return;
621
622 gint width = m_pen.GetWidth();
623 // CMB: if width is non-zero scale it with the dc
624 if (width <= 0)
625 {
626 width = 1;
627 }
628 else
629 {
630 // X doesn't allow different width in x and y and so we take
631 // the average
632 double w = 0.5 + (abs(XLOG2DEVREL(width)) + abs(YLOG2DEVREL(width))) / 2.0;
633 width = (int)w;
634 }
635
636 GdkLineStyle lineStyle = GDK_LINE_SOLID;
637 switch (m_pen.GetStyle())
638 {
639 case wxSOLID: { lineStyle = GDK_LINE_SOLID; break; }
640 case wxDOT: { lineStyle = GDK_LINE_ON_OFF_DASH; break; }
641 case wxLONG_DASH: { lineStyle = GDK_LINE_ON_OFF_DASH; break; }
642 case wxSHORT_DASH: { lineStyle = GDK_LINE_ON_OFF_DASH; break; }
643 case wxDOT_DASH: { lineStyle = GDK_LINE_DOUBLE_DASH; break; }
644 }
645
646 GdkCapStyle capStyle = GDK_CAP_ROUND;
647 switch (m_pen.GetCap())
648 {
649 case wxCAP_ROUND: { capStyle = (width <= 1) ? GDK_CAP_NOT_LAST : GDK_CAP_ROUND; break; }
650 case wxCAP_PROJECTING: { capStyle = GDK_CAP_PROJECTING; break; }
651 case wxCAP_BUTT: { capStyle = GDK_CAP_BUTT; break; }
652 }
653
654 GdkJoinStyle joinStyle = GDK_JOIN_ROUND;
655 switch (m_pen.GetJoin())
656 {
657 case wxJOIN_BEVEL: { joinStyle = GDK_JOIN_BEVEL; break; }
658 case wxJOIN_ROUND: { joinStyle = GDK_JOIN_ROUND; break; }
659 case wxJOIN_MITER: { joinStyle = GDK_JOIN_MITER; break; }
660 }
661
662 gdk_gc_set_line_attributes( m_penGC, width, lineStyle, capStyle, joinStyle );
663
664 m_pen.GetColour().CalcPixel( m_cmap );
665 gdk_gc_set_foreground( m_penGC, m_pen.GetColour().GetColor() );
666 }
667
668 void wxPaintDC::SetBrush( const wxBrush &brush )
669 {
670 if (!Ok()) return;
671
672 if (m_brush == brush) return;
673
674 m_brush = brush;
675
676 if (!m_brush.Ok()) return;
677
678 m_brush.GetColour().CalcPixel( m_cmap );
679 gdk_gc_set_foreground( m_brushGC, m_brush.GetColour().GetColor() );
680
681 GdkFill fillStyle = GDK_SOLID;
682 switch (m_brush.GetStyle())
683 {
684 case wxSOLID:
685 case wxTRANSPARENT:
686 break;
687 default:
688 fillStyle = GDK_STIPPLED;
689 }
690
691 gdk_gc_set_fill( m_brushGC, fillStyle );
692
693 if (m_brush.GetStyle() == wxSTIPPLE)
694 {
695 gdk_gc_set_stipple( m_brushGC, m_brush.GetStipple()->GetPixmap() );
696 }
697
698 if (IS_HATCH(m_brush.GetStyle()))
699 {
700 int num = m_brush.GetStyle() - wxBDIAGONAL_HATCH;
701 gdk_gc_set_stipple( m_brushGC, hatches[num] );
702 }
703 }
704
705 // CMB 21/7/98: Added SetBackground. Sets background brush
706 // for Clear() and bg colour for shapes filled with cross-hatch brush
707 void wxPaintDC::SetBackground( const wxBrush &brush )
708 {
709 if (!Ok()) return;
710
711 if (m_backgroundBrush == brush) return;
712
713 m_backgroundBrush = brush;
714
715 if (!m_backgroundBrush.Ok()) return;
716
717 m_backgroundBrush.GetColour().CalcPixel( m_cmap );
718 gdk_gc_set_background( m_brushGC, m_backgroundBrush.GetColour().GetColor() );
719 gdk_gc_set_foreground( m_bgGC, m_backgroundBrush.GetColour().GetColor() );
720
721 GdkFill fillStyle = GDK_SOLID;
722 switch (m_backgroundBrush.GetStyle())
723 {
724 case wxSOLID:
725 case wxTRANSPARENT:
726 break;
727 default:
728 fillStyle = GDK_STIPPLED;
729 }
730
731 gdk_gc_set_fill( m_bgGC, fillStyle );
732
733 if (m_backgroundBrush.GetStyle() == wxSTIPPLE)
734 {
735 gdk_gc_set_stipple( m_bgGC, m_backgroundBrush.GetStipple()->GetPixmap() );
736 }
737
738 if (IS_HATCH(m_backgroundBrush.GetStyle()))
739 {
740 int num = m_backgroundBrush.GetStyle() - wxBDIAGONAL_HATCH;
741 gdk_gc_set_stipple( m_bgGC, hatches[num] );
742 }
743 }
744
745 void wxPaintDC::SetLogicalFunction( int function )
746 {
747 if (m_logicalFunction == function) return;
748 GdkFunction mode = GDK_COPY;
749 switch (function)
750 {
751 case wxXOR: mode = GDK_INVERT; break;
752 case wxINVERT: mode = GDK_INVERT; break;
753 default: break;
754 }
755 m_logicalFunction = function;
756 gdk_gc_set_function( m_penGC, mode );
757 gdk_gc_set_function( m_brushGC, mode );
758 }
759
760 void wxPaintDC::SetTextForeground( const wxColour &col )
761 {
762 if (!Ok()) return;
763
764 if (m_textForegroundColour == col) return;
765
766 m_textForegroundColour = col;
767 if (!m_textForegroundColour.Ok()) return;
768
769 m_textForegroundColour.CalcPixel( m_cmap );
770 gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() );
771 }
772
773 void wxPaintDC::SetTextBackground( const wxColour &col )
774 {
775 if (!Ok()) return;
776
777 if (m_textBackgroundColour == col) return;
778
779 m_textBackgroundColour = col;
780 if (!m_textBackgroundColour.Ok()) return;
781
782 m_textBackgroundColour.CalcPixel( m_cmap );
783 gdk_gc_set_background( m_textGC, m_textBackgroundColour.GetColor() );
784 }
785
786 void wxPaintDC::SetBackgroundMode( int mode )
787 {
788 m_backgroundMode = mode;
789
790 // CMB 21/7/98: fill style of cross-hatch brushes is affected by
791 // transparent/solid background mode
792 if (m_brush.GetStyle() != wxSOLID && m_brush.GetStyle() != wxTRANSPARENT)
793 {
794 gdk_gc_set_fill( m_brushGC,
795 (m_backgroundMode == wxTRANSPARENT) ? GDK_STIPPLED : GDK_OPAQUE_STIPPLED);
796 }
797 }
798
799 void wxPaintDC::SetPalette( const wxPalette& WXUNUSED(palette) )
800 {
801 }
802
803 void wxPaintDC::SetClippingRegion( long x, long y, long width, long height )
804 {
805 wxDC::SetClippingRegion( x, y, width, height );
806
807 GdkRectangle rect;
808 rect.x = XLOG2DEV(x);
809 rect.y = YLOG2DEV(y);
810 rect.width = XLOG2DEVREL(width);
811 rect.height = YLOG2DEVREL(height);
812 gdk_gc_set_clip_rectangle( m_penGC, &rect );
813 gdk_gc_set_clip_rectangle( m_brushGC, &rect );
814 gdk_gc_set_clip_rectangle( m_textGC, &rect );
815 gdk_gc_set_clip_rectangle( m_bgGC, &rect );
816
817 }
818
819 void wxPaintDC::DestroyClippingRegion(void)
820 {
821 wxDC::DestroyClippingRegion();
822
823 gdk_gc_set_clip_rectangle( m_penGC, (GdkRectangle *) NULL );
824 gdk_gc_set_clip_rectangle( m_brushGC, (GdkRectangle *) NULL );
825 gdk_gc_set_clip_rectangle( m_textGC, (GdkRectangle *) NULL );
826 gdk_gc_set_clip_rectangle( m_bgGC, (GdkRectangle *) NULL );
827 }
828
829 void wxPaintDC::SetUpDC(void)
830 {
831 Destroy();
832 m_ok = TRUE;
833 m_logicalFunction = wxCOPY;
834 m_penGC = gdk_gc_new( m_window );
835 m_brushGC = gdk_gc_new( m_window );
836 m_textGC = gdk_gc_new( m_window );
837 m_bgGC = gdk_gc_new( m_window );
838 SetTextForeground( m_textForegroundColour );
839 SetTextBackground( m_textBackgroundColour );
840 SetPen( m_pen );
841 SetFont( m_font );
842 SetBrush( m_brush );
843
844 gdk_gc_set_background( m_penGC, wxWHITE->GetColor() );
845
846 if (!hatch_bitmap)
847 {
848 hatch_bitmap = hatches;
849 hatch_bitmap[0] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, bdiag_bits, bdiag_width, bdiag_height );
850 hatch_bitmap[1] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, cdiag_bits, cdiag_width, cdiag_height );
851 hatch_bitmap[2] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, fdiag_bits, fdiag_width, fdiag_height );
852 hatch_bitmap[3] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, cross_bits, cross_width, cross_height );
853 hatch_bitmap[4] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, horiz_bits, horiz_width, horiz_height );
854 hatch_bitmap[5] = gdk_bitmap_create_from_data( (GdkWindow *) NULL, verti_bits, verti_width, verti_height );
855 }
856 }
857
858 void wxPaintDC::Destroy(void)
859 {
860 if (m_penGC) gdk_gc_unref( m_penGC );
861 m_penGC = (GdkGC*) NULL;
862 if (m_brushGC) gdk_gc_unref( m_brushGC );
863 m_brushGC = (GdkGC*) NULL;
864 if (m_textGC) gdk_gc_unref( m_textGC );
865 m_textGC = (GdkGC*) NULL;
866 if (m_bgGC) gdk_gc_unref( m_bgGC );
867 m_bgGC = (GdkGC*) NULL;
868 }
869
870 GdkWindow *wxPaintDC::GetWindow(void)
871 {
872 return m_window;
873 }
874
875 // ----------------------------------- spline code ----------------------------------------
876
877 void wx_quadratic_spline(double a1, double b1, double a2, double b2,
878 double a3, double b3, double a4, double b4);
879 void wx_clear_stack(void);
880 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2, double *x3,
881 double *y3, double *x4, double *y4);
882 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3,
883 double x4, double y4);
884 static bool wx_spline_add_point(double x, double y);
885 static void wx_spline_draw_point_array(wxDC *dc);
886
887 wxList wx_spline_point_list;
888
889 #define half(z1, z2) ((z1+z2)/2.0)
890 #define THRESHOLD 5
891
892 /* iterative version */
893
894 void wx_quadratic_spline(double a1, double b1, double a2, double b2, double a3, double b3, double a4,
895 double b4)
896 {
897 register double xmid, ymid;
898 double x1, y1, x2, y2, x3, y3, x4, y4;
899
900 wx_clear_stack();
901 wx_spline_push(a1, b1, a2, b2, a3, b3, a4, b4);
902
903 while (wx_spline_pop(&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4)) {
904 xmid = (double)half(x2, x3);
905 ymid = (double)half(y2, y3);
906 if (fabs(x1 - xmid) < THRESHOLD && fabs(y1 - ymid) < THRESHOLD &&
907 fabs(xmid - x4) < THRESHOLD && fabs(ymid - y4) < THRESHOLD) {
908 wx_spline_add_point( x1, y1 );
909 wx_spline_add_point( xmid, ymid );
910 } else {
911 wx_spline_push(xmid, ymid, (double)half(xmid, x3), (double)half(ymid, y3),
912 (double)half(x3, x4), (double)half(y3, y4), x4, y4);
913 wx_spline_push(x1, y1, (double)half(x1, x2), (double)half(y1, y2),
914 (double)half(x2, xmid), (double)half(y2, ymid), xmid, ymid);
915 }
916 }
917 }
918
919 /* utilities used by spline drawing routines */
920
921 typedef struct wx_spline_stack_struct {
922 double x1, y1, x2, y2, x3, y3, x4, y4;
923 } Stack;
924
925 #define SPLINE_STACK_DEPTH 20
926 static Stack wx_spline_stack[SPLINE_STACK_DEPTH];
927 static Stack *wx_stack_top;
928 static int wx_stack_count;
929
930 void wx_clear_stack(void)
931 {
932 wx_stack_top = wx_spline_stack;
933 wx_stack_count = 0;
934 }
935
936 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
937 {
938 wx_stack_top->x1 = x1;
939 wx_stack_top->y1 = y1;
940 wx_stack_top->x2 = x2;
941 wx_stack_top->y2 = y2;
942 wx_stack_top->x3 = x3;
943 wx_stack_top->y3 = y3;
944 wx_stack_top->x4 = x4;
945 wx_stack_top->y4 = y4;
946 wx_stack_top++;
947 wx_stack_count++;
948 }
949
950 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2,
951 double *x3, double *y3, double *x4, double *y4)
952 {
953 if (wx_stack_count == 0)
954 return (0);
955 wx_stack_top--;
956 wx_stack_count--;
957 *x1 = wx_stack_top->x1;
958 *y1 = wx_stack_top->y1;
959 *x2 = wx_stack_top->x2;
960 *y2 = wx_stack_top->y2;
961 *x3 = wx_stack_top->x3;
962 *y3 = wx_stack_top->y3;
963 *x4 = wx_stack_top->x4;
964 *y4 = wx_stack_top->y4;
965 return (1);
966 }
967
968 static bool wx_spline_add_point(double x, double y)
969 {
970 wxPoint *point = new wxPoint ;
971 point->x = (int) x;
972 point->y = (int) y;
973 wx_spline_point_list.Append((wxObject*)point);
974 return TRUE;
975 }
976
977 static void wx_spline_draw_point_array(wxDC *dc)
978 {
979 dc->DrawLines(&wx_spline_point_list, 0, 0 );
980 wxNode *node = wx_spline_point_list.First();
981 while (node)
982 {
983 wxPoint *point = (wxPoint *)node->Data();
984 delete point;
985 delete node;
986 node = wx_spline_point_list.First();
987 }
988 }
989
990 void wxPaintDC::DrawSpline( wxList *points )
991 {
992 wxPoint *p;
993 double cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4;
994 double x1, y1, x2, y2;
995
996 wxNode *node = points->First();
997 p = (wxPoint *)node->Data();
998
999 x1 = p->x;
1000 y1 = p->y;
1001
1002 node = node->Next();
1003 p = (wxPoint *)node->Data();
1004
1005 x2 = p->x;
1006 y2 = p->y;
1007 cx1 = (double)((x1 + x2) / 2);
1008 cy1 = (double)((y1 + y2) / 2);
1009 cx2 = (double)((cx1 + x2) / 2);
1010 cy2 = (double)((cy1 + y2) / 2);
1011
1012 wx_spline_add_point(x1, y1);
1013
1014 while ((node = node->Next()) != NULL)
1015 {
1016 p = (wxPoint *)node->Data();
1017 x1 = x2;
1018 y1 = y2;
1019 x2 = p->x;
1020 y2 = p->y;
1021 cx4 = (double)(x1 + x2) / 2;
1022 cy4 = (double)(y1 + y2) / 2;
1023 cx3 = (double)(x1 + cx4) / 2;
1024 cy3 = (double)(y1 + cy4) / 2;
1025
1026 wx_quadratic_spline(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
1027
1028 cx1 = cx4;
1029 cy1 = cy4;
1030 cx2 = (double)(cx1 + x2) / 2;
1031 cy2 = (double)(cy1 + y2) / 2;
1032 }
1033
1034 wx_spline_add_point( cx1, cy1 );
1035 wx_spline_add_point( x2, y2 );
1036
1037 wx_spline_draw_point_array( this );
1038 }