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