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