1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "dcclient.h"
15 #include "wx/dcclient.h"
16 #include "wx/dcmemory.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 #define RAD2DEG 57.2957795131
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
,wxDC
)
44 wxPaintDC::wxPaintDC(void)
48 wxPaintDC::wxPaintDC( wxWindow
*window
)
52 wxPaintDC::~wxPaintDC(void)
56 void wxPaintDC::FloodFill( long WXUNUSED(x1
), long WXUNUSED(y1
),
57 wxColour
*WXUNUSED(col
), int WXUNUSED(style
) )
61 bool wxPaintDC::GetPixel( long WXUNUSED(x1
), long WXUNUSED(y1
), wxColour
*WXUNUSED(col
) ) const
66 void wxPaintDC::DrawLine( long x1
, long y1
, long x2
, long y2
)
72 void wxPaintDC::CrossHair( long x
, long y
)
78 void wxPaintDC::DrawArc( long x1
, long y1
, long x2
, long y2
, double xc
, double yc
)
82 long xx1
= XLOG2DEV(x1
);
83 long yy1
= YLOG2DEV(y1
);
84 long xx2
= XLOG2DEV(x2
);
85 long yy2
= YLOG2DEV(y2
);
86 long xxc
= XLOG2DEV((long)xc
);
87 long yyc
= YLOG2DEV((long)yc
);
88 double dx
= xx1
- xxc
;
89 double dy
= yy1
- yyc
;
90 double radius
= sqrt(dx
*dx
+dy
*dy
);
91 long r
= (long)radius
;
92 double radius1
, radius2
;
94 if (xx1
== xx2
&& yy1
== yy2
)
102 radius1
= radius2
= 0.0;
106 radius1
= (xx1
- xxc
== 0) ?
107 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
108 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
109 radius2
= (xx2
- xxc
== 0) ?
110 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
111 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
113 long alpha1
= long(radius1
* 64.0);
114 long alpha2
= long((radius2
- radius1
) * 64.0);
115 while (alpha2
<= 0) alpha2
+= 360*64;
116 while (alpha1
> 360*64) alpha1
-= 360*64;
118 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
120 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
124 void wxPaintDC::DrawEllipticArc( long x
, long y
, long width
, long height
, double sa
, double ea
)
128 long xx
= XLOG2DEV(x
);
129 long yy
= YLOG2DEV(y
);
130 long ww
= m_signX
* XLOG2DEVREL(width
);
131 long hh
= m_signY
* YLOG2DEVREL(height
);
133 // CMB: handle -ve width and/or height
134 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
135 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
137 long start
= long(sa
* 64.0);
138 long end
= long(ea
* 64.0);
139 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
141 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
144 void wxPaintDC::DrawPoint( long x
, long y
)
148 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
151 void wxPaintDC::DrawLines( int n
, wxPoint points
[], long xoffset
, long yoffset
)
155 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
157 for (int i
= 0; i
< n
-1; i
++)
159 long x1
= XLOG2DEV(points
[i
].x
+ xoffset
);
160 long x2
= XLOG2DEV(points
[i
+1].x
+ xoffset
);
161 long y1
= YLOG2DEV(points
[i
].y
+ yoffset
); // oh, what a waste
162 long y2
= YLOG2DEV(points
[i
+1].y
+ yoffset
);
166 void wxPaintDC::DrawLines( wxList
*points
, long xoffset
, long yoffset
)
170 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
172 wxNode
*node
= points
->First();
175 wxPoint
*point
= (wxPoint
*)node
->Data();
176 wxPoint
*npoint
= (wxPoint
*)node
->Next()->Data();
177 long x1
= XLOG2DEV(point
->x
+ xoffset
);
178 long x2
= XLOG2DEV(npoint
->x
+ xoffset
);
179 long y1
= YLOG2DEV(point
->y
+ yoffset
); // and again...
180 long y2
= YLOG2DEV(npoint
->y
+ yoffset
);
185 void wxPaintDC::DrawPolygon( int WXUNUSED(n
), wxPoint
WXUNUSED(points
)[],
186 long WXUNUSED(xoffset
), long WXUNUSED(yoffset
), int WXUNUSED(fillStyle
) )
191 void wxPaintDC::DrawPolygon( wxList
*WXUNUSED(lines
), long WXUNUSED(xoffset
),
192 long WXUNUSED(yoffset
), int WXUNUSED(fillStyle
) )
197 void wxPaintDC::DrawRectangle( long x
, long y
, long width
, long height
)
201 long xx
= XLOG2DEV(x
);
202 long yy
= YLOG2DEV(y
);
203 long ww
= m_signX
* XLOG2DEVREL(width
);
204 long hh
= m_signY
* YLOG2DEVREL(height
);
206 // CMB: draw nothing if transformed w or h is 0
207 if (ww
== 0 || hh
== 0) return;
209 // CMB: handle -ve width and/or height
210 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
211 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
213 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
215 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
218 void wxPaintDC::DrawRoundedRectangle( long x
, long y
, long width
, long height
, double radius
)
222 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
224 long xx
= XLOG2DEV(x
);
225 long yy
= YLOG2DEV(y
);
226 long ww
= m_signX
* XLOG2DEVREL(width
);
227 long hh
= m_signY
* YLOG2DEVREL(height
);
228 long rr
= XLOG2DEVREL((long)radius
);
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
; }
234 // CMB: if radius is zero use DrawRectangle() instead to avoid
235 // X drawing errors with small radii
238 DrawRectangle( x
, y
, width
, height
);
242 // CMB: draw nothing if transformed w or h is 0
243 if (ww
== 0 || hh
== 0) return;
245 // CMB: adjust size if outline is drawn otherwise the result is
246 // 1 pixel too wide and high
247 if (m_pen
.GetStyle() != wxTRANSPARENT
)
253 // CMB: ensure dd is not larger than rectangle otherwise we
254 // get an hour glass shape
256 if (dd
> ww
) dd
= ww
;
257 if (dd
> hh
) dd
= hh
;
260 if (m_brush
.GetStyle() != wxTRANSPARENT
)
264 if (m_pen
.GetStyle() != wxTRANSPARENT
)
269 void wxPaintDC::DrawEllipse( long x
, long y
, long width
, long height
)
273 long xx
= XLOG2DEV(x
);
274 long yy
= YLOG2DEV(y
);
275 long ww
= m_signX
* XLOG2DEVREL(width
);
276 long hh
= m_signY
* YLOG2DEVREL(height
);
278 // CMB: handle -ve width and/or height
279 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
280 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
282 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
284 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
287 bool wxPaintDC::CanDrawBitmap(void) const
292 void wxPaintDC::DrawIcon( const wxIcon
&icon
, long x
, long y
, bool useMask
)
296 if (!icon
.Ok()) return;
298 int xx
= XLOG2DEV(x
);
299 int yy
= YLOG2DEV(y
);
303 bool wxPaintDC::Blit( long xdest
, long ydest
, long width
, long height
,
304 wxDC
*source
, long xsrc
, long ysrc
, int WXUNUSED(logical_func
), bool WXUNUSED(useMask
) )
306 if (!Ok()) return FALSE
;
308 // CMB 20/5/98: add blitting of bitmaps
309 if (source
->IsKindOf(CLASSINFO(wxMemoryDC
)))
311 wxMemoryDC
* srcDC
= (wxMemoryDC
*)source
;
313 GdkBitmap* bmap = srcDC->m_selected.GetBitmap();
320 source->DeviceToLogicalX(xsrc), source->DeviceToLogicalY(ysrc),
321 XLOG2DEV(xdest), YLOG2DEV(ydest),
322 source->DeviceToLogicalXRel(width), source->DeviceToLogicalYRel(height)
332 void wxPaintDC::DrawText( const wxString
&text
, long x
, long y
, bool
341 bool wxPaintDC::CanGetTextExtent(void) const
346 void wxPaintDC::GetTextExtent( const wxString
&string
, long *width
, long *height
,
347 long *WXUNUSED(descent
), long *WXUNUSED(externalLeading
),
348 wxFont
*WXUNUSED(theFont
), bool WXUNUSED(use16
) )
354 long wxPaintDC::GetCharWidth(void)
360 long wxPaintDC::GetCharHeight(void)
366 void wxPaintDC::Clear(void)
372 void wxPaintDC::SetFont( const wxFont
&font
)
379 void wxPaintDC::SetPen( const wxPen
&pen
)
383 if (m_pen
== pen
) return;
387 if (!m_pen
.Ok()) return;
390 void wxPaintDC::SetBrush( const wxBrush
&brush
)
394 if (m_brush
== brush
) return;
398 if (!m_brush
.Ok()) return;
402 void wxPaintDC::SetBackground( const wxBrush
&brush
)
406 if (m_backgroundBrush
== brush
) return;
408 m_backgroundBrush
= brush
;
410 if (!m_backgroundBrush
.Ok()) return;
414 void wxPaintDC::SetLogicalFunction( int function
)
416 if (m_logicalFunction
== function
) return;
419 void wxPaintDC::SetTextForeground( const wxColour
&col
)
423 if (m_textForegroundColour
== col
) return;
425 m_textForegroundColour
= col
;
426 if (!m_textForegroundColour
.Ok()) return;
429 void wxPaintDC::SetTextBackground( const wxColour
&col
)
433 if (m_textBackgroundColour
== col
) return;
435 m_textBackgroundColour
= col
;
436 if (!m_textBackgroundColour
.Ok()) return;
439 void wxPaintDC::SetBackgroundMode( int mode
)
441 m_backgroundMode
= mode
;
443 if (m_brush
.GetStyle() != wxSOLID
&& m_brush
.GetStyle() != wxTRANSPARENT
)
448 void wxPaintDC::SetPalette( const wxPalette
& WXUNUSED(palette
) )
452 void wxPaintDC::SetClippingRegion( long x
, long y
, long width
, long height
)
454 wxDC::SetClippingRegion( x
, y
, width
, height
);
458 void wxPaintDC::DestroyClippingRegion(void)
460 wxDC::DestroyClippingRegion();
464 // ----------------------------------- spline code ----------------------------------------
466 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
467 double a3
, double b3
, double a4
, double b4
);
468 void wx_clear_stack(void);
469 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
470 double *y3
, double *x4
, double *y4
);
471 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
472 double x4
, double y4
);
473 static bool wx_spline_add_point(double x
, double y
);
474 static void wx_spline_draw_point_array(wxDC
*dc
);
476 wxList wx_spline_point_list
;
478 #define half(z1, z2) ((z1+z2)/2.0)
481 /* iterative version */
483 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
486 register double xmid
, ymid
;
487 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
490 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
492 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
493 xmid
= (double)half(x2
, x3
);
494 ymid
= (double)half(y2
, y3
);
495 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
496 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
497 wx_spline_add_point( x1
, y1
);
498 wx_spline_add_point( xmid
, ymid
);
500 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
501 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
502 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
503 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
508 /* utilities used by spline drawing routines */
510 typedef struct wx_spline_stack_struct
{
511 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
514 #define SPLINE_STACK_DEPTH 20
515 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
516 static Stack
*wx_stack_top
;
517 static int wx_stack_count
;
519 void wx_clear_stack(void)
521 wx_stack_top
= wx_spline_stack
;
525 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
527 wx_stack_top
->x1
= x1
;
528 wx_stack_top
->y1
= y1
;
529 wx_stack_top
->x2
= x2
;
530 wx_stack_top
->y2
= y2
;
531 wx_stack_top
->x3
= x3
;
532 wx_stack_top
->y3
= y3
;
533 wx_stack_top
->x4
= x4
;
534 wx_stack_top
->y4
= y4
;
539 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
540 double *x3
, double *y3
, double *x4
, double *y4
)
542 if (wx_stack_count
== 0)
546 *x1
= wx_stack_top
->x1
;
547 *y1
= wx_stack_top
->y1
;
548 *x2
= wx_stack_top
->x2
;
549 *y2
= wx_stack_top
->y2
;
550 *x3
= wx_stack_top
->x3
;
551 *y3
= wx_stack_top
->y3
;
552 *x4
= wx_stack_top
->x4
;
553 *y4
= wx_stack_top
->y4
;
557 static bool wx_spline_add_point(double x
, double y
)
559 wxPoint
*point
= new wxPoint
;
562 wx_spline_point_list
.Append((wxObject
*)point
);
566 static void wx_spline_draw_point_array(wxDC
*dc
)
568 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
569 wxNode
*node
= wx_spline_point_list
.First();
572 wxPoint
*point
= (wxPoint
*)node
->Data();
575 node
= wx_spline_point_list
.First();
579 void wxPaintDC::DrawOpenSpline( wxList
*points
)
582 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
583 double x1
, y1
, x2
, y2
;
585 wxNode
*node
= points
->First();
586 p
= (wxPoint
*)node
->Data();
592 p
= (wxPoint
*)node
->Data();
596 cx1
= (double)((x1
+ x2
) / 2);
597 cy1
= (double)((y1
+ y2
) / 2);
598 cx2
= (double)((cx1
+ x2
) / 2);
599 cy2
= (double)((cy1
+ y2
) / 2);
601 wx_spline_add_point(x1
, y1
);
603 while ((node
= node
->Next()) != NULL
)
605 p
= (wxPoint
*)node
->Data();
610 cx4
= (double)(x1
+ x2
) / 2;
611 cy4
= (double)(y1
+ y2
) / 2;
612 cx3
= (double)(x1
+ cx4
) / 2;
613 cy3
= (double)(y1
+ cy4
) / 2;
615 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
619 cx2
= (double)(cx1
+ x2
) / 2;
620 cy2
= (double)(cy1
+ y2
) / 2;
623 wx_spline_add_point( cx1
, cy1
);
624 wx_spline_add_point( x2
, y2
);
626 wx_spline_draw_point_array( this );