1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxClientDC class
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dcclient.h"
16 #include "wx/dcclient.h"
17 #include "wx/dcmemory.h"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 #define RAD2DEG 57.2957795131
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 #if !USE_SHARED_LIBRARY
31 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
32 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
33 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxWindowDC
)
40 wxWindowDC::wxWindowDC(void)
44 wxWindowDC::wxWindowDC( wxWindow
*window
)
48 wxWindowDC::~wxWindowDC(void)
52 void wxWindowDC::FloodFill( long WXUNUSED(x1
), long WXUNUSED(y1
),
53 const wxColour
& WXUNUSED(col
), int WXUNUSED(style
) )
57 bool wxWindowDC::GetPixel( long WXUNUSED(x1
), long WXUNUSED(y1
), wxColour
*WXUNUSED(col
) ) const
62 void wxWindowDC::DrawLine( long x1
, long y1
, long x2
, long y2
)
68 void wxWindowDC::CrossHair( long x
, long y
)
74 void wxWindowDC::DrawArc( long x1
, long y1
, long x2
, long y2
, long xc
, long yc
)
78 long xx1
= XLOG2DEV(x1
);
79 long yy1
= YLOG2DEV(y1
);
80 long xx2
= XLOG2DEV(x2
);
81 long yy2
= YLOG2DEV(y2
);
82 long xxc
= XLOG2DEV((long)xc
);
83 long yyc
= YLOG2DEV((long)yc
);
84 double dx
= xx1
- xxc
;
85 double dy
= yy1
- yyc
;
86 double radius
= sqrt(dx
*dx
+dy
*dy
);
87 long r
= (long)radius
;
88 double radius1
, radius2
;
90 if (xx1
== xx2
&& yy1
== yy2
)
98 radius1
= radius2
= 0.0;
102 radius1
= (xx1
- xxc
== 0) ?
103 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
104 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
105 radius2
= (xx2
- xxc
== 0) ?
106 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
107 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
109 long alpha1
= long(radius1
* 64.0);
110 long alpha2
= long((radius2
- radius1
) * 64.0);
111 while (alpha2
<= 0) alpha2
+= 360*64;
112 while (alpha1
> 360*64) alpha1
-= 360*64;
114 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
116 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
120 void wxWindowDC::DrawEllipticArc( long x
, long y
, long width
, long height
, double sa
, double ea
)
124 long xx
= XLOG2DEV(x
);
125 long yy
= YLOG2DEV(y
);
126 long ww
= m_signX
* XLOG2DEVREL(width
);
127 long hh
= m_signY
* YLOG2DEVREL(height
);
129 // CMB: handle -ve width and/or height
130 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
131 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
133 long start
= long(sa
* 64.0);
134 long end
= long(ea
* 64.0);
135 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
137 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
140 void wxWindowDC::DrawPoint( long x
, long y
)
144 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
147 void wxWindowDC::DrawLines( int n
, wxPoint points
[], long xoffset
, long yoffset
)
151 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
153 for (int i
= 0; i
< n
-1; i
++)
155 long x1
= XLOG2DEV(points
[i
].x
+ xoffset
);
156 long x2
= XLOG2DEV(points
[i
+1].x
+ xoffset
);
157 long y1
= YLOG2DEV(points
[i
].y
+ yoffset
); // oh, what a waste
158 long y2
= YLOG2DEV(points
[i
+1].y
+ yoffset
);
162 void wxWindowDC::DrawLines( wxList
*points
, long xoffset
, long yoffset
)
166 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
168 wxNode
*node
= points
->First();
171 wxPoint
*point
= (wxPoint
*)node
->Data();
172 wxPoint
*npoint
= (wxPoint
*)node
->Next()->Data();
173 long x1
= XLOG2DEV(point
->x
+ xoffset
);
174 long x2
= XLOG2DEV(npoint
->x
+ xoffset
);
175 long y1
= YLOG2DEV(point
->y
+ yoffset
); // and again...
176 long y2
= YLOG2DEV(npoint
->y
+ yoffset
);
181 void wxWindowDC::DrawPolygon( int WXUNUSED(n
), wxPoint
WXUNUSED(points
)[],
182 long WXUNUSED(xoffset
), long WXUNUSED(yoffset
), int WXUNUSED(fillStyle
) )
187 void wxWindowDC::DrawPolygon( wxList
*WXUNUSED(lines
), long WXUNUSED(xoffset
),
188 long WXUNUSED(yoffset
), int WXUNUSED(fillStyle
) )
193 void wxWindowDC::DrawRectangle( long x
, long y
, long width
, long height
)
197 long xx
= XLOG2DEV(x
);
198 long yy
= YLOG2DEV(y
);
199 long ww
= m_signX
* XLOG2DEVREL(width
);
200 long hh
= m_signY
* YLOG2DEVREL(height
);
202 // CMB: draw nothing if transformed w or h is 0
203 if (ww
== 0 || hh
== 0) return;
205 // CMB: handle -ve width and/or height
206 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
207 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
209 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
211 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
214 void wxWindowDC::DrawRoundedRectangle( long x
, long y
, long width
, long height
, double radius
)
218 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
220 long xx
= XLOG2DEV(x
);
221 long yy
= YLOG2DEV(y
);
222 long ww
= m_signX
* XLOG2DEVREL(width
);
223 long hh
= m_signY
* YLOG2DEVREL(height
);
224 long rr
= XLOG2DEVREL((long)radius
);
226 // CMB: handle -ve width and/or height
227 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
228 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
230 // CMB: if radius is zero use DrawRectangle() instead to avoid
231 // X drawing errors with small radii
234 DrawRectangle( x
, y
, width
, height
);
238 // CMB: draw nothing if transformed w or h is 0
239 if (ww
== 0 || hh
== 0) return;
241 // CMB: adjust size if outline is drawn otherwise the result is
242 // 1 pixel too wide and high
243 if (m_pen
.GetStyle() != wxTRANSPARENT
)
249 // CMB: ensure dd is not larger than rectangle otherwise we
250 // get an hour glass shape
252 if (dd
> ww
) dd
= ww
;
253 if (dd
> hh
) dd
= hh
;
256 if (m_brush
.GetStyle() != wxTRANSPARENT
)
260 if (m_pen
.GetStyle() != wxTRANSPARENT
)
265 void wxWindowDC::DrawEllipse( long x
, long y
, long width
, long height
)
269 long xx
= XLOG2DEV(x
);
270 long yy
= YLOG2DEV(y
);
271 long ww
= m_signX
* XLOG2DEVREL(width
);
272 long hh
= m_signY
* YLOG2DEVREL(height
);
274 // CMB: handle -ve width and/or height
275 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
276 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
278 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
280 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
283 bool wxWindowDC::CanDrawBitmap(void) const
288 void wxWindowDC::DrawIcon( const wxIcon
&icon
, long x
, long y
, bool useMask
)
292 if (!icon
.Ok()) return;
294 int xx
= XLOG2DEV(x
);
295 int yy
= YLOG2DEV(y
);
299 bool wxWindowDC::Blit( long xdest
, long ydest
, long width
, long height
,
300 wxDC
*source
, long xsrc
, long ysrc
, int WXUNUSED(logical_func
), bool WXUNUSED(useMask
) )
302 if (!Ok()) return FALSE
;
304 // CMB 20/5/98: add blitting of bitmaps
305 if (source
->IsKindOf(CLASSINFO(wxMemoryDC
)))
307 wxMemoryDC
* srcDC
= (wxMemoryDC
*)source
;
309 GdkBitmap* bmap = srcDC->m_selected.GetBitmap();
316 source->DeviceToLogicalX(xsrc), source->DeviceToLogicalY(ysrc),
317 XLOG2DEV(xdest), YLOG2DEV(ydest),
318 source->DeviceToLogicalXRel(width), source->DeviceToLogicalYRel(height)
328 void wxWindowDC::DrawText( const wxString
&text
, long x
, long y
, bool
337 bool wxWindowDC::CanGetTextExtent(void) const
342 void wxWindowDC::GetTextExtent( const wxString
&string
, long *width
, long *height
,
343 long *WXUNUSED(descent
), long *WXUNUSED(externalLeading
),
344 wxFont
*WXUNUSED(theFont
), bool WXUNUSED(use16
) )
350 long wxWindowDC::GetCharWidth(void)
356 long wxWindowDC::GetCharHeight(void)
362 void wxWindowDC::Clear(void)
368 void wxWindowDC::SetFont( const wxFont
&font
)
375 void wxWindowDC::SetPen( const wxPen
&pen
)
379 if (m_pen
== pen
) return;
383 if (!m_pen
.Ok()) return;
386 void wxWindowDC::SetBrush( const wxBrush
&brush
)
390 if (m_brush
== brush
) return;
394 if (!m_brush
.Ok()) return;
398 void wxWindowDC::SetBackground( const wxBrush
&brush
)
402 if (m_backgroundBrush
== brush
) return;
404 m_backgroundBrush
= brush
;
406 if (!m_backgroundBrush
.Ok()) return;
410 void wxWindowDC::SetLogicalFunction( int function
)
412 if (m_logicalFunction
== function
) return;
415 void wxWindowDC::SetTextForeground( const wxColour
&col
)
419 if (m_textForegroundColour
== col
) return;
421 m_textForegroundColour
= col
;
422 if (!m_textForegroundColour
.Ok()) return;
425 void wxWindowDC::SetTextBackground( const wxColour
&col
)
429 if (m_textBackgroundColour
== col
) return;
431 m_textBackgroundColour
= col
;
432 if (!m_textBackgroundColour
.Ok()) return;
435 void wxWindowDC::SetBackgroundMode( int mode
)
437 m_backgroundMode
= mode
;
439 if (m_brush
.GetStyle() != wxSOLID
&& m_brush
.GetStyle() != wxTRANSPARENT
)
444 void wxWindowDC::SetPalette( const wxPalette
& WXUNUSED(palette
) )
448 void wxWindowDC::SetClippingRegion( long x
, long y
, long width
, long height
)
450 wxDC::SetClippingRegion( x
, y
, width
, height
);
456 void wxWindowDC::SetClippingRegion( const wxRegion
& region
)
458 wxRect box
= region
.GetBox();
460 wxDC::SetClippingRegion( box
.x
, box
.y
, box
.width
, box
.height
);
465 void wxWindowDC::DestroyClippingRegion(void)
467 wxDC::DestroyClippingRegion();
471 // ----------------------------------- spline code ----------------------------------------
473 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
474 double a3
, double b3
, double a4
, double b4
);
475 void wx_clear_stack(void);
476 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
477 double *y3
, double *x4
, double *y4
);
478 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
479 double x4
, double y4
);
480 static bool wx_spline_add_point(double x
, double y
);
481 static void wx_spline_draw_point_array(wxDC
*dc
);
483 wxList wx_spline_point_list
;
485 #define half(z1, z2) ((z1+z2)/2.0)
488 /* iterative version */
490 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
493 register double xmid
, ymid
;
494 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
497 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
499 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
500 xmid
= (double)half(x2
, x3
);
501 ymid
= (double)half(y2
, y3
);
502 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
503 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
504 wx_spline_add_point( x1
, y1
);
505 wx_spline_add_point( xmid
, ymid
);
507 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
508 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
509 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
510 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
515 /* utilities used by spline drawing routines */
517 typedef struct wx_spline_stack_struct
{
518 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
521 #define SPLINE_STACK_DEPTH 20
522 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
523 static Stack
*wx_stack_top
;
524 static int wx_stack_count
;
526 void wx_clear_stack(void)
528 wx_stack_top
= wx_spline_stack
;
532 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
534 wx_stack_top
->x1
= x1
;
535 wx_stack_top
->y1
= y1
;
536 wx_stack_top
->x2
= x2
;
537 wx_stack_top
->y2
= y2
;
538 wx_stack_top
->x3
= x3
;
539 wx_stack_top
->y3
= y3
;
540 wx_stack_top
->x4
= x4
;
541 wx_stack_top
->y4
= y4
;
546 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
547 double *x3
, double *y3
, double *x4
, double *y4
)
549 if (wx_stack_count
== 0)
553 *x1
= wx_stack_top
->x1
;
554 *y1
= wx_stack_top
->y1
;
555 *x2
= wx_stack_top
->x2
;
556 *y2
= wx_stack_top
->y2
;
557 *x3
= wx_stack_top
->x3
;
558 *y3
= wx_stack_top
->y3
;
559 *x4
= wx_stack_top
->x4
;
560 *y4
= wx_stack_top
->y4
;
564 static bool wx_spline_add_point(double x
, double y
)
566 wxPoint
*point
= new wxPoint
;
569 wx_spline_point_list
.Append((wxObject
*)point
);
573 static void wx_spline_draw_point_array(wxDC
*dc
)
575 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
576 wxNode
*node
= wx_spline_point_list
.First();
579 wxPoint
*point
= (wxPoint
*)node
->Data();
582 node
= wx_spline_point_list
.First();
586 void wxWindowDC::DrawSpline( wxList
*points
)
589 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
590 double x1
, y1
, x2
, y2
;
592 wxNode
*node
= points
->First();
593 p
= (wxPoint
*)node
->Data();
599 p
= (wxPoint
*)node
->Data();
603 cx1
= (double)((x1
+ x2
) / 2);
604 cy1
= (double)((y1
+ y2
) / 2);
605 cx2
= (double)((cx1
+ x2
) / 2);
606 cy2
= (double)((cy1
+ y2
) / 2);
608 wx_spline_add_point(x1
, y1
);
610 while ((node
= node
->Next()) != NULL
)
612 p
= (wxPoint
*)node
->Data();
617 cx4
= (double)(x1
+ x2
) / 2;
618 cy4
= (double)(y1
+ y2
) / 2;
619 cx3
= (double)(x1
+ cx4
) / 2;
620 cy3
= (double)(y1
+ cy4
) / 2;
622 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
626 cx2
= (double)(cx1
+ x2
) / 2;
627 cy2
= (double)(cy1
+ y2
) / 2;
630 wx_spline_add_point( cx1
, cy1
);
631 wx_spline_add_point( x2
, y2
);
633 wx_spline_draw_point_array( this );