]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dcclient.cpp
I debug DialogEd for five hours and then Karsten
[wxWidgets.git] / src / gtk / dcclient.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: dcclient.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6f65e337 6// RCS-ID: $Id$
c801d85f
KB
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"
6f65e337 16#include "wx/dcmemory.h"
c801d85f
KB
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
30static GdkPixmap *hatches[num_hatches];
c67daf87 31static GdkPixmap **hatch_bitmap = (GdkPixmap **) NULL;
c801d85f
KB
32
33//-----------------------------------------------------------------------------
34// constants
35//-----------------------------------------------------------------------------
36
37#define RAD2DEG 57.2957795131
38
6f65e337
JS
39//-----------------------------------------------------------------------------
40// temporary implementation of the missing GDK function
41//-----------------------------------------------------------------------------
42#include "gdk/gdkprivate.h"
43void 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
c801d85f
KB
82//-----------------------------------------------------------------------------
83// wxPaintDC
84//-----------------------------------------------------------------------------
85
86IMPLEMENT_DYNAMIC_CLASS(wxPaintDC,wxDC)
87
88wxPaintDC::wxPaintDC(void)
89{
c67daf87
UR
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;
903f689b 95}
c801d85f
KB
96
97wxPaintDC::wxPaintDC( wxWindow *window )
98{
c67daf87
UR
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;
e3e65dac 104
c801d85f
KB
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 );
0180d5da
RR
114
115 m_isDrawable = TRUE;
116
c801d85f 117 SetUpDC();
903f689b 118}
c801d85f
KB
119
120wxPaintDC::~wxPaintDC(void)
121{
903f689b 122}
c801d85f
KB
123
124void wxPaintDC::FloodFill( long WXUNUSED(x1), long WXUNUSED(y1),
125 wxColour *WXUNUSED(col), int WXUNUSED(style) )
126{
cf7a7e13 127 wxFAIL_MSG( "wxPaintDC::FloodFill not implemented" );
903f689b 128}
c801d85f
KB
129
130bool wxPaintDC::GetPixel( long WXUNUSED(x1), long WXUNUSED(y1), wxColour *WXUNUSED(col) ) const
131{
cf7a7e13 132 wxFAIL_MSG( "wxPaintDC::GetPixel not implemented" );
c801d85f 133 return FALSE;
903f689b 134}
c801d85f
KB
135
136void wxPaintDC::DrawLine( long x1, long y1, long x2, long y2 )
137{
138 if (!Ok()) return;
139
cf7a7e13
RR
140 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
141
c801d85f
KB
142 if (m_pen.GetStyle() != wxTRANSPARENT)
143 {
144 gdk_draw_line( m_window, m_penGC,
145 XLOG2DEV(x1), YLOG2DEV(y1), XLOG2DEV(x2), YLOG2DEV(y2) );
903f689b
RR
146 }
147}
c801d85f
KB
148
149void wxPaintDC::CrossHair( long x, long y )
150{
151 if (!Ok()) return;
152
cf7a7e13
RR
153 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
154
c801d85f
KB
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) );
903f689b
RR
166 }
167}
c801d85f
KB
168
169void wxPaintDC::DrawArc( long x1, long y1, long x2, long y2, double xc, double yc )
170{
171 if (!Ok()) return;
172
cf7a7e13
RR
173 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
174
c801d85f
KB
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;
903f689b 205 }
c801d85f
KB
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
903f689b 217}
c801d85f
KB
218
219void wxPaintDC::DrawEllipticArc( long x, long y, long width, long height, double sa, double ea )
220{
221 if (!Ok()) return;
222
cf7a7e13
RR
223 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
224
c801d85f
KB
225 long xx = XLOG2DEV(x);
226 long yy = YLOG2DEV(y);
6f65e337
JS
227 long ww = m_signX * XLOG2DEVREL(width);
228 long hh = m_signY * YLOG2DEVREL(height);
c801d85f 229
6f65e337
JS
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);
c801d85f 236 if (m_brush.GetStyle() != wxTRANSPARENT)
6f65e337 237 gdk_draw_arc( m_window, m_brushGC, TRUE, xx, yy, ww, hh, start, end );
c801d85f
KB
238
239 if (m_pen.GetStyle() != wxTRANSPARENT)
6f65e337 240 gdk_draw_arc( m_window, m_penGC, FALSE, xx, yy, ww, hh, start, end );
903f689b 241}
c801d85f
KB
242
243void wxPaintDC::DrawPoint( long x, long y )
244{
245 if (!Ok()) return;
246
cf7a7e13
RR
247 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
248
c801d85f
KB
249 if (m_pen.GetStyle() != wxTRANSPARENT)
250 gdk_draw_point( m_window, m_penGC, XLOG2DEV(x), YLOG2DEV(y) );
903f689b 251}
c801d85f
KB
252
253void wxPaintDC::DrawLines( int n, wxPoint points[], long xoffset, long yoffset )
254{
255 if (!Ok()) return;
256
cf7a7e13
RR
257 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
258
c801d85f
KB
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);
4c681997 267 gdk_draw_line( m_window, m_penGC, x1, y1, x2, y2 );
903f689b
RR
268 }
269}
c801d85f
KB
270
271void wxPaintDC::DrawLines( wxList *points, long xoffset, long yoffset )
272{
273 if (!Ok()) return;
274
cf7a7e13
RR
275 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
276
c801d85f
KB
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);
4c681997 288 gdk_draw_line( m_window, m_penGC, x1, y1, x2, y2 );
c801d85f 289 node = node->Next();
903f689b
RR
290 }
291}
c801d85f 292
cf7a7e13
RR
293void 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
b6af8d80
RR
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;
903f689b 318}
c801d85f 319
cf7a7e13
RR
320void 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
b6af8d80 326 int n = lines->Number();
b6af8d80
RR
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;
903f689b 352}
c801d85f
KB
353
354void wxPaintDC::DrawRectangle( long x, long y, long width, long height )
355{
356 if (!Ok()) return;
357
cf7a7e13
RR
358 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
359
c801d85f
KB
360 long xx = XLOG2DEV(x);
361 long yy = YLOG2DEV(y);
6f65e337
JS
362 long ww = m_signX * XLOG2DEVREL(width);
363 long hh = m_signY * YLOG2DEVREL(height);
c801d85f 364
6f65e337
JS
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
c801d85f
KB
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 );
903f689b 377}
c801d85f
KB
378
379void wxPaintDC::DrawRoundedRectangle( long x, long y, long width, long height, double radius )
380{
381 if (!Ok()) return;
382
cf7a7e13
RR
383 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
384
c801d85f
KB
385 if (radius < 0.0) radius = - radius * ((width < height) ? width : height);
386
387 long xx = XLOG2DEV(x);
388 long yy = YLOG2DEV(y);
6f65e337
JS
389 long ww = m_signX * XLOG2DEVREL(width);
390 long hh = m_signY * YLOG2DEVREL(height);
c801d85f 391 long rr = XLOG2DEVREL((long)radius);
6f65e337
JS
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
c801d85f 418 long dd = 2 * rr;
6f65e337
JS
419 if (dd > ww) dd = ww;
420 if (dd > hh) dd = hh;
421 rr = dd / 2;
c801d85f
KB
422
423 if (m_brush.GetStyle() != wxTRANSPARENT)
424 {
6f65e337
JS
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 );
c801d85f
KB
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 );
903f689b 431 }
c801d85f
KB
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 );
903f689b
RR
443 }
444}
c801d85f
KB
445
446void wxPaintDC::DrawEllipse( long x, long y, long width, long height )
447{
448 if (!Ok()) return;
449
cf7a7e13
RR
450 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
451
c801d85f
KB
452 long xx = XLOG2DEV(x);
453 long yy = YLOG2DEV(y);
6f65e337
JS
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; }
c801d85f
KB
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 );
903f689b 466}
c801d85f
KB
467
468bool wxPaintDC::CanDrawBitmap(void) const
469{
470 return TRUE;
903f689b 471}
c801d85f
KB
472
473void wxPaintDC::DrawIcon( const wxIcon &icon, long x, long y, bool useMask )
474{
475 if (!Ok()) return;
476
477 if (!icon.Ok()) return;
478
cf7a7e13
RR
479 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
480
c801d85f
KB
481 int xx = XLOG2DEV(x);
482 int yy = YLOG2DEV(y);
483
c67daf87 484 GdkBitmap *mask = (GdkBitmap *) NULL;
c801d85f
KB
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 );
903f689b 491 }
c801d85f
KB
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 {
c67daf87 498 gdk_gc_set_clip_mask( m_penGC, (GdkBitmap *) NULL );
c801d85f 499 gdk_gc_set_clip_origin( m_penGC, 0, 0 );
903f689b
RR
500 }
501}
c801d85f
KB
502
503bool 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
cf7a7e13
RR
508 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
509
6f65e337
JS
510 // CMB 20/5/98: add blitting of bitmaps
511 if (source->IsKindOf(CLASSINFO(wxMemoryDC)))
512 {
513 wxMemoryDC* srcDC = (wxMemoryDC*)source;
cf7a7e13 514 GdkBitmap* bmap = srcDC->m_selected.GetBitmap();
6f65e337
JS
515 if (bmap)
516 {
cf7a7e13
RR
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) );
6f65e337
JS
524 return TRUE;
525 }
526 }
c801d85f 527
6f65e337 528 wxClientDC *csrc = (wxClientDC*)source;
c801d85f
KB
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;
903f689b 544}
c801d85f 545
903f689b 546void wxPaintDC::DrawText( const wxString &text, long x, long y, bool WXUNUSED(use16) )
c801d85f
KB
547{
548 if (!Ok()) return;
18a2fa37 549
cf7a7e13
RR
550 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
551
c801d85f 552 GdkFont *font = m_font.GetInternalFont( m_scaleY );
6f65e337 553
18a2fa37
KB
554 x = XLOG2DEV(x);
555 y = YLOG2DEV(y);
556
6f65e337
JS
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 }
18a2fa37
KB
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 }
903f689b 578}
c801d85f
KB
579
580bool wxPaintDC::CanGetTextExtent(void) const
581{
582 return TRUE;
903f689b 583}
c801d85f
KB
584
585void wxPaintDC::GetTextExtent( const wxString &string, long *width, long *height,
c33c4050
RR
586 long *descent, long *externalLeading,
587 wxFont *theFont, bool WXUNUSED(use16) )
c801d85f
KB
588{
589 if (!Ok()) return;
590
c33c4050
RR
591 wxFont fontToUse = m_font;
592 if (theFont) fontToUse = *theFont;
593
594 GdkFont *font = fontToUse.GetInternalFont( m_scaleY );
6f65e337
JS
595 if (width) (*width) = long(gdk_string_width( font, string ) / m_scaleX);
596 if (height) (*height) = long((font->ascent + font->descent) / m_scaleY);
c33c4050
RR
597 if (descent) (*descent) = long(font->descent / m_scaleY);
598 if (externalLeading) (*externalLeading) = 0; // ??
903f689b 599}
c801d85f
KB
600
601long wxPaintDC::GetCharWidth(void)
602{
603 if (!Ok()) return 0;
604
605 GdkFont *font = m_font.GetInternalFont( m_scaleY );
c33c4050 606 return long(gdk_string_width( font, "H" ) / m_scaleX);
903f689b 607}
c801d85f
KB
608
609long wxPaintDC::GetCharHeight(void)
610{
611 if (!Ok()) return 0;
612
613 GdkFont *font = m_font.GetInternalFont( m_scaleY );
c33c4050 614 return long((font->ascent + font->descent) / m_scaleY);
903f689b 615}
c801d85f
KB
616
617void wxPaintDC::Clear(void)
618{
619 if (!Ok()) return;
620
cf7a7e13 621 if (!m_isDrawable) ((wxMemoryDC*)this)->m_selected.DestroyImage();
0180d5da
RR
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 );
54ff4a70 632 gdk_draw_rectangle( m_window, m_bgGC, TRUE, 0, 0, width, height );
903f689b
RR
633 }
634}
c801d85f
KB
635
636void wxPaintDC::SetFont( const wxFont &font )
637{
638 if (!Ok()) return;
639
640 m_font = font;
903f689b 641}
c801d85f
KB
642
643void 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();
6f65e337
JS
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 }
c801d85f
KB
666
667 GdkLineStyle lineStyle = GDK_LINE_SOLID;
668 switch (m_pen.GetStyle())
669 {
903f689b
RR
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 }
c801d85f
KB
676
677 GdkCapStyle capStyle = GDK_CAP_ROUND;
678 switch (m_pen.GetCap())
679 {
903f689b
RR
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 }
c801d85f
KB
684
685 GdkJoinStyle joinStyle = GDK_JOIN_ROUND;
686 switch (m_pen.GetJoin())
687 {
903f689b
RR
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 }
c801d85f
KB
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() );
903f689b 697}
c801d85f
KB
698
699void 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;
903f689b 720 }
c801d85f
KB
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() );
903f689b 727 }
c801d85f
KB
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] );
903f689b
RR
733 }
734}
c801d85f 735
46dc76ba
RR
736// CMB 21/7/98: Added SetBackground. Sets background brush
737// for Clear() and bg colour for shapes filled with cross-hatch brush
738void 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;
903f689b 760 }
46dc76ba
RR
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() );
903f689b 767 }
46dc76ba
RR
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] );
903f689b
RR
773 }
774}
46dc76ba 775
c801d85f
KB
776void 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;
903f689b 785 }
c801d85f
KB
786 m_logicalFunction = function;
787 gdk_gc_set_function( m_penGC, mode );
788 gdk_gc_set_function( m_brushGC, mode );
903f689b 789}
c801d85f
KB
790
791void 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() );
903f689b 802}
c801d85f
KB
803
804void 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() );
903f689b 815}
c801d85f 816
6f65e337 817void wxPaintDC::SetBackgroundMode( int mode )
c801d85f 818{
6f65e337 819 m_backgroundMode = mode;
46dc76ba
RR
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 }
903f689b 828}
c801d85f
KB
829
830void wxPaintDC::SetPalette( const wxPalette& WXUNUSED(palette) )
831{
903f689b 832}
c801d85f
KB
833
834void 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);
903f689b
RR
841 rect.width = XLOG2DEVREL(width);
842 rect.height = YLOG2DEVREL(height);
c801d85f
KB
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
903f689b 848}
c801d85f
KB
849
850void wxPaintDC::DestroyClippingRegion(void)
851{
852 wxDC::DestroyClippingRegion();
853
c67daf87
UR
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 );
903f689b 858}
c801d85f
KB
859
860void wxPaintDC::SetUpDC(void)
861{
862 m_ok = TRUE;
863 m_logicalFunction = wxCOPY;
e3e65dac 864 if (m_penGC) gdk_gc_unref( m_penGC );
c801d85f 865 m_penGC = gdk_gc_new( m_window );
e3e65dac 866 if (m_brushGC) gdk_gc_unref( m_brushGC );
c801d85f 867 m_brushGC = gdk_gc_new( m_window );
e3e65dac 868 if (m_textGC) gdk_gc_unref( m_textGC );
c801d85f 869 m_textGC = gdk_gc_new( m_window );
e3e65dac 870 if (m_bgGC) gdk_gc_unref( m_bgGC );
c801d85f
KB
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;
c67daf87
UR
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 );
903f689b
RR
889 }
890}
c801d85f
KB
891
892GdkWindow *wxPaintDC::GetWindow(void)
893{
894 return m_window;
903f689b 895}
c801d85f
KB
896
897// ----------------------------------- spline code ----------------------------------------
898
899void wx_quadratic_spline(double a1, double b1, double a2, double b2,
900 double a3, double b3, double a4, double b4);
901void wx_clear_stack(void);
902int wx_spline_pop(double *x1, double *y1, double *x2, double *y2, double *x3,
903 double *y3, double *x4, double *y4);
904void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3,
905 double x4, double y4);
906static bool wx_spline_add_point(double x, double y);
907static void wx_spline_draw_point_array(wxDC *dc);
908
909wxList wx_spline_point_list;
910
911#define half(z1, z2) ((z1+z2)/2.0)
912#define THRESHOLD 5
913
914/* iterative version */
915
916void 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
943typedef struct wx_spline_stack_struct {
944 double x1, y1, x2, y2, x3, y3, x4, y4;
945} Stack;
946
947#define SPLINE_STACK_DEPTH 20
948static Stack wx_spline_stack[SPLINE_STACK_DEPTH];
949static Stack *wx_stack_top;
950static int wx_stack_count;
951
952void wx_clear_stack(void)
953{
954 wx_stack_top = wx_spline_stack;
955 wx_stack_count = 0;
956}
957
958void 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
972int 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
990static 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
999static 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
1012void 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 );
903f689b 1060}