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"
18 #include "wx/region.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 #define RAD2DEG 57.2957795131
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
32 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
33 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxWindowDC
)
39 wxWindowDC::wxWindowDC(void)
43 wxWindowDC::wxWindowDC( wxWindow
*window
)
47 wxWindowDC::~wxWindowDC(void)
51 void wxWindowDC::FloodFill( long WXUNUSED(x1
), long WXUNUSED(y1
),
52 const wxColour
& WXUNUSED(col
), int WXUNUSED(style
) )
56 bool wxWindowDC::GetPixel( long WXUNUSED(x1
), long WXUNUSED(y1
), wxColour
*WXUNUSED(col
) ) const
61 void wxWindowDC::DrawLine( long x1
, long y1
, long x2
, long y2
)
67 void wxWindowDC::CrossHair( long x
, long y
)
73 void wxWindowDC::DrawArc( long x1
, long y1
, long x2
, long y2
, long xc
, long yc
)
77 long xx1
= XLOG2DEV(x1
);
78 long yy1
= YLOG2DEV(y1
);
79 long xx2
= XLOG2DEV(x2
);
80 long yy2
= YLOG2DEV(y2
);
81 long xxc
= XLOG2DEV((long)xc
);
82 long yyc
= YLOG2DEV((long)yc
);
83 double dx
= xx1
- xxc
;
84 double dy
= yy1
- yyc
;
85 double radius
= sqrt(dx
*dx
+dy
*dy
);
86 long r
= (long)radius
;
87 double radius1
, radius2
;
89 if (xx1
== xx2
&& yy1
== yy2
)
97 radius1
= radius2
= 0.0;
101 radius1
= (xx1
- xxc
== 0) ?
102 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
103 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
104 radius2
= (xx2
- xxc
== 0) ?
105 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
106 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
108 long alpha1
= long(radius1
* 64.0);
109 long alpha2
= long((radius2
- radius1
) * 64.0);
110 while (alpha2
<= 0) alpha2
+= 360*64;
111 while (alpha1
> 360*64) alpha1
-= 360*64;
113 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
115 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
119 void wxWindowDC::DrawEllipticArc( long x
, long y
, long width
, long height
, double sa
, double ea
)
123 long xx
= XLOG2DEV(x
);
124 long yy
= YLOG2DEV(y
);
125 long ww
= m_signX
* XLOG2DEVREL(width
);
126 long hh
= m_signY
* YLOG2DEVREL(height
);
128 // CMB: handle -ve width and/or height
129 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
130 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
132 long start
= long(sa
* 64.0);
133 long end
= long(ea
* 64.0);
134 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
136 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
139 void wxWindowDC::DrawPoint( long x
, long y
)
143 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
146 void wxWindowDC::DrawLines( int n
, wxPoint points
[], long xoffset
, long yoffset
)
150 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
152 for (int i
= 0; i
< n
-1; i
++)
154 long x1
= XLOG2DEV(points
[i
].x
+ xoffset
);
155 long x2
= XLOG2DEV(points
[i
+1].x
+ xoffset
);
156 long y1
= YLOG2DEV(points
[i
].y
+ yoffset
); // oh, what a waste
157 long y2
= YLOG2DEV(points
[i
+1].y
+ yoffset
);
161 void wxWindowDC::DrawLines( wxList
*points
, long xoffset
, long yoffset
)
165 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
167 wxNode
*node
= points
->First();
170 wxPoint
*point
= (wxPoint
*)node
->Data();
171 wxPoint
*npoint
= (wxPoint
*)node
->Next()->Data();
172 long x1
= XLOG2DEV(point
->x
+ xoffset
);
173 long x2
= XLOG2DEV(npoint
->x
+ xoffset
);
174 long y1
= YLOG2DEV(point
->y
+ yoffset
); // and again...
175 long y2
= YLOG2DEV(npoint
->y
+ yoffset
);
180 void wxWindowDC::DrawPolygon( int WXUNUSED(n
), wxPoint
WXUNUSED(points
)[],
181 long WXUNUSED(xoffset
), long WXUNUSED(yoffset
), int WXUNUSED(fillStyle
) )
186 void wxWindowDC::DrawPolygon( wxList
*WXUNUSED(lines
), long WXUNUSED(xoffset
),
187 long WXUNUSED(yoffset
), int WXUNUSED(fillStyle
) )
192 void wxWindowDC::DrawRectangle( long x
, long y
, long width
, long height
)
196 long xx
= XLOG2DEV(x
);
197 long yy
= YLOG2DEV(y
);
198 long ww
= m_signX
* XLOG2DEVREL(width
);
199 long hh
= m_signY
* YLOG2DEVREL(height
);
201 // CMB: draw nothing if transformed w or h is 0
202 if (ww
== 0 || hh
== 0) return;
204 // CMB: handle -ve width and/or height
205 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
206 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
208 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
210 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
213 void wxWindowDC::DrawRoundedRectangle( long x
, long y
, long width
, long height
, double radius
)
217 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
219 long xx
= XLOG2DEV(x
);
220 long yy
= YLOG2DEV(y
);
221 long ww
= m_signX
* XLOG2DEVREL(width
);
222 long hh
= m_signY
* YLOG2DEVREL(height
);
223 long rr
= XLOG2DEVREL((long)radius
);
225 // CMB: handle -ve width and/or height
226 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
227 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
229 // CMB: if radius is zero use DrawRectangle() instead to avoid
230 // X drawing errors with small radii
233 DrawRectangle( x
, y
, width
, height
);
237 // CMB: draw nothing if transformed w or h is 0
238 if (ww
== 0 || hh
== 0) return;
240 // CMB: adjust size if outline is drawn otherwise the result is
241 // 1 pixel too wide and high
242 if (m_pen
.GetStyle() != wxTRANSPARENT
)
248 // CMB: ensure dd is not larger than rectangle otherwise we
249 // get an hour glass shape
251 if (dd
> ww
) dd
= ww
;
252 if (dd
> hh
) dd
= hh
;
255 if (m_brush
.GetStyle() != wxTRANSPARENT
)
259 if (m_pen
.GetStyle() != wxTRANSPARENT
)
264 void wxWindowDC::DrawEllipse( long x
, long y
, long width
, long height
)
268 long xx
= XLOG2DEV(x
);
269 long yy
= YLOG2DEV(y
);
270 long ww
= m_signX
* XLOG2DEVREL(width
);
271 long hh
= m_signY
* YLOG2DEVREL(height
);
273 // CMB: handle -ve width and/or height
274 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
275 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
277 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
279 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
282 bool wxWindowDC::CanDrawBitmap(void) const
287 void wxWindowDC::DrawIcon( const wxIcon
&icon
, long x
, long y
, bool useMask
)
291 if (!icon
.Ok()) return;
293 int xx
= XLOG2DEV(x
);
294 int yy
= YLOG2DEV(y
);
298 bool wxWindowDC::Blit( long xdest
, long ydest
, long width
, long height
,
299 wxDC
*source
, long xsrc
, long ysrc
, int WXUNUSED(logical_func
), bool WXUNUSED(useMask
) )
301 if (!Ok()) return FALSE
;
303 // CMB 20/5/98: add blitting of bitmaps
304 if (source
->IsKindOf(CLASSINFO(wxMemoryDC
)))
306 wxMemoryDC
* srcDC
= (wxMemoryDC
*)source
;
308 GdkBitmap* bmap = srcDC->m_selected.GetBitmap();
315 source->DeviceToLogicalX(xsrc), source->DeviceToLogicalY(ysrc),
316 XLOG2DEV(xdest), YLOG2DEV(ydest),
317 source->DeviceToLogicalXRel(width), source->DeviceToLogicalYRel(height)
327 void wxWindowDC::DrawText( const wxString
&text
, long x
, long y
, bool
336 bool wxWindowDC::CanGetTextExtent(void) const
341 void wxWindowDC::GetTextExtent( const wxString
&string
, long *width
, long *height
,
342 long *WXUNUSED(descent
), long *WXUNUSED(externalLeading
),
343 wxFont
*WXUNUSED(theFont
), bool WXUNUSED(use16
) )
349 long wxWindowDC::GetCharWidth(void)
355 long wxWindowDC::GetCharHeight(void)
361 void wxWindowDC::Clear(void)
367 void wxWindowDC::SetFont( const wxFont
&font
)
374 void wxWindowDC::SetPen( const wxPen
&pen
)
378 if (m_pen
== pen
) return;
382 if (!m_pen
.Ok()) return;
385 void wxWindowDC::SetBrush( const wxBrush
&brush
)
389 if (m_brush
== brush
) return;
393 if (!m_brush
.Ok()) return;
397 void wxWindowDC::SetBackground( const wxBrush
&brush
)
401 if (m_backgroundBrush
== brush
) return;
403 m_backgroundBrush
= brush
;
405 if (!m_backgroundBrush
.Ok()) return;
409 void wxWindowDC::SetLogicalFunction( int function
)
411 if (m_logicalFunction
== function
) return;
414 void wxWindowDC::SetTextForeground( const wxColour
&col
)
418 if (m_textForegroundColour
== col
) return;
420 m_textForegroundColour
= col
;
421 if (!m_textForegroundColour
.Ok()) return;
424 void wxWindowDC::SetTextBackground( const wxColour
&col
)
428 if (m_textBackgroundColour
== col
) return;
430 m_textBackgroundColour
= col
;
431 if (!m_textBackgroundColour
.Ok()) return;
434 void wxWindowDC::SetBackgroundMode( int mode
)
436 m_backgroundMode
= mode
;
438 if (m_brush
.GetStyle() != wxSOLID
&& m_brush
.GetStyle() != wxTRANSPARENT
)
443 void wxWindowDC::SetPalette( const wxPalette
& WXUNUSED(palette
) )
447 void wxWindowDC::SetClippingRegion( long x
, long y
, long width
, long height
)
449 wxDC::SetClippingRegion( x
, y
, width
, height
);
455 void wxWindowDC::SetClippingRegion( const wxRegion
& region
)
457 wxRect box
= region
.GetBox();
459 wxDC::SetClippingRegion( box
.x
, box
.y
, box
.width
, box
.height
);
464 void wxWindowDC::DestroyClippingRegion(void)
466 wxDC::DestroyClippingRegion();
470 // ----------------------------------- spline code ----------------------------------------
472 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
473 double a3
, double b3
, double a4
, double b4
);
474 void wx_clear_stack(void);
475 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
476 double *y3
, double *x4
, double *y4
);
477 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
478 double x4
, double y4
);
479 static bool wx_spline_add_point(double x
, double y
);
480 static void wx_spline_draw_point_array(wxDC
*dc
);
482 wxList wx_spline_point_list
;
484 #define half(z1, z2) ((z1+z2)/2.0)
487 /* iterative version */
489 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
492 register double xmid
, ymid
;
493 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
496 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
498 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
499 xmid
= (double)half(x2
, x3
);
500 ymid
= (double)half(y2
, y3
);
501 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
502 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
503 wx_spline_add_point( x1
, y1
);
504 wx_spline_add_point( xmid
, ymid
);
506 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
507 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
508 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
509 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
514 /* utilities used by spline drawing routines */
516 typedef struct wx_spline_stack_struct
{
517 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
520 #define SPLINE_STACK_DEPTH 20
521 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
522 static Stack
*wx_stack_top
;
523 static int wx_stack_count
;
525 void wx_clear_stack(void)
527 wx_stack_top
= wx_spline_stack
;
531 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
533 wx_stack_top
->x1
= x1
;
534 wx_stack_top
->y1
= y1
;
535 wx_stack_top
->x2
= x2
;
536 wx_stack_top
->y2
= y2
;
537 wx_stack_top
->x3
= x3
;
538 wx_stack_top
->y3
= y3
;
539 wx_stack_top
->x4
= x4
;
540 wx_stack_top
->y4
= y4
;
545 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
546 double *x3
, double *y3
, double *x4
, double *y4
)
548 if (wx_stack_count
== 0)
552 *x1
= wx_stack_top
->x1
;
553 *y1
= wx_stack_top
->y1
;
554 *x2
= wx_stack_top
->x2
;
555 *y2
= wx_stack_top
->y2
;
556 *x3
= wx_stack_top
->x3
;
557 *y3
= wx_stack_top
->y3
;
558 *x4
= wx_stack_top
->x4
;
559 *y4
= wx_stack_top
->y4
;
563 static bool wx_spline_add_point(double x
, double y
)
565 wxPoint
*point
= new wxPoint
;
568 wx_spline_point_list
.Append((wxObject
*)point
);
572 static void wx_spline_draw_point_array(wxDC
*dc
)
574 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
575 wxNode
*node
= wx_spline_point_list
.First();
578 wxPoint
*point
= (wxPoint
*)node
->Data();
581 node
= wx_spline_point_list
.First();
585 void wxWindowDC::DrawSpline( wxList
*points
)
588 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
589 double x1
, y1
, x2
, y2
;
591 wxNode
*node
= points
->First();
592 p
= (wxPoint
*)node
->Data();
598 p
= (wxPoint
*)node
->Data();
602 cx1
= (double)((x1
+ x2
) / 2);
603 cy1
= (double)((y1
+ y2
) / 2);
604 cx2
= (double)((cx1
+ x2
) / 2);
605 cy2
= (double)((cy1
+ y2
) / 2);
607 wx_spline_add_point(x1
, y1
);
609 while ((node
= node
->Next()) != NULL
)
611 p
= (wxPoint
*)node
->Data();
616 cx4
= (double)(x1
+ x2
) / 2;
617 cy4
= (double)(y1
+ y2
) / 2;
618 cx3
= (double)(x1
+ cx4
) / 2;
619 cy3
= (double)(y1
+ cy4
) / 2;
621 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
625 cx2
= (double)(cx1
+ x2
) / 2;
626 cy2
= (double)(cy1
+ y2
) / 2;
629 wx_spline_add_point( cx1
, cy1
);
630 wx_spline_add_point( x2
, y2
);
632 wx_spline_draw_point_array( this );