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(wxClientDC, wxDC)
32 //IMPLEMENT_DYNAMIC_CLASS(wxWindowDC, wxDC)
33 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxDC
)
37 wxPaintDC::wxPaintDC(void)
41 wxPaintDC::wxPaintDC( wxWindow
*window
)
45 wxPaintDC::~wxPaintDC(void)
49 void wxPaintDC::FloodFill( long WXUNUSED(x1
), long WXUNUSED(y1
),
50 wxColour
*WXUNUSED(col
), int WXUNUSED(style
) )
54 bool wxPaintDC::GetPixel( long WXUNUSED(x1
), long WXUNUSED(y1
), wxColour
*WXUNUSED(col
) ) const
59 void wxPaintDC::DrawLine( long x1
, long y1
, long x2
, long y2
)
65 void wxPaintDC::CrossHair( long x
, long y
)
71 void wxPaintDC::DrawArc( long x1
, long y1
, long x2
, long y2
, double xc
, double yc
)
75 long xx1
= XLOG2DEV(x1
);
76 long yy1
= YLOG2DEV(y1
);
77 long xx2
= XLOG2DEV(x2
);
78 long yy2
= YLOG2DEV(y2
);
79 long xxc
= XLOG2DEV((long)xc
);
80 long yyc
= YLOG2DEV((long)yc
);
81 double dx
= xx1
- xxc
;
82 double dy
= yy1
- yyc
;
83 double radius
= sqrt(dx
*dx
+dy
*dy
);
84 long r
= (long)radius
;
85 double radius1
, radius2
;
87 if (xx1
== xx2
&& yy1
== yy2
)
95 radius1
= radius2
= 0.0;
99 radius1
= (xx1
- xxc
== 0) ?
100 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
101 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
102 radius2
= (xx2
- xxc
== 0) ?
103 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
104 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
106 long alpha1
= long(radius1
* 64.0);
107 long alpha2
= long((radius2
- radius1
) * 64.0);
108 while (alpha2
<= 0) alpha2
+= 360*64;
109 while (alpha1
> 360*64) alpha1
-= 360*64;
111 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
113 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
117 void wxPaintDC::DrawEllipticArc( long x
, long y
, long width
, long height
, double sa
, double ea
)
121 long xx
= XLOG2DEV(x
);
122 long yy
= YLOG2DEV(y
);
123 long ww
= m_signX
* XLOG2DEVREL(width
);
124 long hh
= m_signY
* YLOG2DEVREL(height
);
126 // CMB: handle -ve width and/or height
127 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
128 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
130 long start
= long(sa
* 64.0);
131 long end
= long(ea
* 64.0);
132 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
134 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
137 void wxPaintDC::DrawPoint( long x
, long y
)
141 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
144 void wxPaintDC::DrawLines( int n
, wxPoint points
[], long xoffset
, long yoffset
)
148 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
150 for (int i
= 0; i
< n
-1; i
++)
152 long x1
= XLOG2DEV(points
[i
].x
+ xoffset
);
153 long x2
= XLOG2DEV(points
[i
+1].x
+ xoffset
);
154 long y1
= YLOG2DEV(points
[i
].y
+ yoffset
); // oh, what a waste
155 long y2
= YLOG2DEV(points
[i
+1].y
+ yoffset
);
159 void wxPaintDC::DrawLines( wxList
*points
, long xoffset
, long yoffset
)
163 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
165 wxNode
*node
= points
->First();
168 wxPoint
*point
= (wxPoint
*)node
->Data();
169 wxPoint
*npoint
= (wxPoint
*)node
->Next()->Data();
170 long x1
= XLOG2DEV(point
->x
+ xoffset
);
171 long x2
= XLOG2DEV(npoint
->x
+ xoffset
);
172 long y1
= YLOG2DEV(point
->y
+ yoffset
); // and again...
173 long y2
= YLOG2DEV(npoint
->y
+ yoffset
);
178 void wxPaintDC::DrawPolygon( int WXUNUSED(n
), wxPoint
WXUNUSED(points
)[],
179 long WXUNUSED(xoffset
), long WXUNUSED(yoffset
), int WXUNUSED(fillStyle
) )
184 void wxPaintDC::DrawPolygon( wxList
*WXUNUSED(lines
), long WXUNUSED(xoffset
),
185 long WXUNUSED(yoffset
), int WXUNUSED(fillStyle
) )
190 void wxPaintDC::DrawRectangle( long x
, long y
, long width
, long height
)
194 long xx
= XLOG2DEV(x
);
195 long yy
= YLOG2DEV(y
);
196 long ww
= m_signX
* XLOG2DEVREL(width
);
197 long hh
= m_signY
* YLOG2DEVREL(height
);
199 // CMB: draw nothing if transformed w or h is 0
200 if (ww
== 0 || hh
== 0) return;
202 // CMB: handle -ve width and/or height
203 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
204 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
206 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
208 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
211 void wxPaintDC::DrawRoundedRectangle( long x
, long y
, long width
, long height
, double radius
)
215 if (radius
< 0.0) radius
= - radius
* ((width
< height
) ? width
: height
);
217 long xx
= XLOG2DEV(x
);
218 long yy
= YLOG2DEV(y
);
219 long ww
= m_signX
* XLOG2DEVREL(width
);
220 long hh
= m_signY
* YLOG2DEVREL(height
);
221 long rr
= XLOG2DEVREL((long)radius
);
223 // CMB: handle -ve width and/or height
224 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
225 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
227 // CMB: if radius is zero use DrawRectangle() instead to avoid
228 // X drawing errors with small radii
231 DrawRectangle( x
, y
, width
, height
);
235 // CMB: draw nothing if transformed w or h is 0
236 if (ww
== 0 || hh
== 0) return;
238 // CMB: adjust size if outline is drawn otherwise the result is
239 // 1 pixel too wide and high
240 if (m_pen
.GetStyle() != wxTRANSPARENT
)
246 // CMB: ensure dd is not larger than rectangle otherwise we
247 // get an hour glass shape
249 if (dd
> ww
) dd
= ww
;
250 if (dd
> hh
) dd
= hh
;
253 if (m_brush
.GetStyle() != wxTRANSPARENT
)
257 if (m_pen
.GetStyle() != wxTRANSPARENT
)
262 void wxPaintDC::DrawEllipse( long x
, long y
, long width
, long height
)
266 long xx
= XLOG2DEV(x
);
267 long yy
= YLOG2DEV(y
);
268 long ww
= m_signX
* XLOG2DEVREL(width
);
269 long hh
= m_signY
* YLOG2DEVREL(height
);
271 // CMB: handle -ve width and/or height
272 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
273 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
275 if (m_brush
.GetStyle() != wxTRANSPARENT
) {};
277 if (m_pen
.GetStyle() != wxTRANSPARENT
) {};
280 bool wxPaintDC::CanDrawBitmap(void) const
285 void wxPaintDC::DrawIcon( const wxIcon
&icon
, long x
, long y
, bool useMask
)
289 if (!icon
.Ok()) return;
291 int xx
= XLOG2DEV(x
);
292 int yy
= YLOG2DEV(y
);
296 bool wxPaintDC::Blit( long xdest
, long ydest
, long width
, long height
,
297 wxDC
*source
, long xsrc
, long ysrc
, int WXUNUSED(logical_func
), bool WXUNUSED(useMask
) )
299 if (!Ok()) return FALSE
;
301 // CMB 20/5/98: add blitting of bitmaps
302 if (source
->IsKindOf(CLASSINFO(wxMemoryDC
)))
304 wxMemoryDC
* srcDC
= (wxMemoryDC
*)source
;
306 GdkBitmap* bmap = srcDC->m_selected.GetBitmap();
313 source->DeviceToLogicalX(xsrc), source->DeviceToLogicalY(ysrc),
314 XLOG2DEV(xdest), YLOG2DEV(ydest),
315 source->DeviceToLogicalXRel(width), source->DeviceToLogicalYRel(height)
325 void wxPaintDC::DrawText( const wxString
&text
, long x
, long y
, bool
334 bool wxPaintDC::CanGetTextExtent(void) const
339 void wxPaintDC::GetTextExtent( const wxString
&string
, long *width
, long *height
,
340 long *WXUNUSED(descent
), long *WXUNUSED(externalLeading
),
341 wxFont
*WXUNUSED(theFont
), bool WXUNUSED(use16
) )
347 long wxPaintDC::GetCharWidth(void)
353 long wxPaintDC::GetCharHeight(void)
359 void wxPaintDC::Clear(void)
365 void wxPaintDC::SetFont( const wxFont
&font
)
372 void wxPaintDC::SetPen( const wxPen
&pen
)
376 if (m_pen
== pen
) return;
380 if (!m_pen
.Ok()) return;
383 void wxPaintDC::SetBrush( const wxBrush
&brush
)
387 if (m_brush
== brush
) return;
391 if (!m_brush
.Ok()) return;
395 void wxPaintDC::SetBackground( const wxBrush
&brush
)
399 if (m_backgroundBrush
== brush
) return;
401 m_backgroundBrush
= brush
;
403 if (!m_backgroundBrush
.Ok()) return;
407 void wxPaintDC::SetLogicalFunction( int function
)
409 if (m_logicalFunction
== function
) return;
412 void wxPaintDC::SetTextForeground( const wxColour
&col
)
416 if (m_textForegroundColour
== col
) return;
418 m_textForegroundColour
= col
;
419 if (!m_textForegroundColour
.Ok()) return;
422 void wxPaintDC::SetTextBackground( const wxColour
&col
)
426 if (m_textBackgroundColour
== col
) return;
428 m_textBackgroundColour
= col
;
429 if (!m_textBackgroundColour
.Ok()) return;
432 void wxPaintDC::SetBackgroundMode( int mode
)
434 m_backgroundMode
= mode
;
436 if (m_brush
.GetStyle() != wxSOLID
&& m_brush
.GetStyle() != wxTRANSPARENT
)
441 void wxPaintDC::SetPalette( const wxPalette
& WXUNUSED(palette
) )
445 void wxPaintDC::SetClippingRegion( long x
, long y
, long width
, long height
)
447 wxDC::SetClippingRegion( x
, y
, width
, height
);
451 void wxPaintDC::DestroyClippingRegion(void)
453 wxDC::DestroyClippingRegion();
457 // ----------------------------------- spline code ----------------------------------------
459 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
460 double a3
, double b3
, double a4
, double b4
);
461 void wx_clear_stack(void);
462 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
463 double *y3
, double *x4
, double *y4
);
464 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
465 double x4
, double y4
);
466 static bool wx_spline_add_point(double x
, double y
);
467 static void wx_spline_draw_point_array(wxDC
*dc
);
469 wxList wx_spline_point_list
;
471 #define half(z1, z2) ((z1+z2)/2.0)
474 /* iterative version */
476 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
479 register double xmid
, ymid
;
480 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
483 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
485 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
486 xmid
= (double)half(x2
, x3
);
487 ymid
= (double)half(y2
, y3
);
488 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
489 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
490 wx_spline_add_point( x1
, y1
);
491 wx_spline_add_point( xmid
, ymid
);
493 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
494 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
495 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
496 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
501 /* utilities used by spline drawing routines */
503 typedef struct wx_spline_stack_struct
{
504 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
507 #define SPLINE_STACK_DEPTH 20
508 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
509 static Stack
*wx_stack_top
;
510 static int wx_stack_count
;
512 void wx_clear_stack(void)
514 wx_stack_top
= wx_spline_stack
;
518 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
520 wx_stack_top
->x1
= x1
;
521 wx_stack_top
->y1
= y1
;
522 wx_stack_top
->x2
= x2
;
523 wx_stack_top
->y2
= y2
;
524 wx_stack_top
->x3
= x3
;
525 wx_stack_top
->y3
= y3
;
526 wx_stack_top
->x4
= x4
;
527 wx_stack_top
->y4
= y4
;
532 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
533 double *x3
, double *y3
, double *x4
, double *y4
)
535 if (wx_stack_count
== 0)
539 *x1
= wx_stack_top
->x1
;
540 *y1
= wx_stack_top
->y1
;
541 *x2
= wx_stack_top
->x2
;
542 *y2
= wx_stack_top
->y2
;
543 *x3
= wx_stack_top
->x3
;
544 *y3
= wx_stack_top
->y3
;
545 *x4
= wx_stack_top
->x4
;
546 *y4
= wx_stack_top
->y4
;
550 static bool wx_spline_add_point(double x
, double y
)
552 wxPoint
*point
= new wxPoint
;
555 wx_spline_point_list
.Append((wxObject
*)point
);
559 static void wx_spline_draw_point_array(wxDC
*dc
)
561 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
562 wxNode
*node
= wx_spline_point_list
.First();
565 wxPoint
*point
= (wxPoint
*)node
->Data();
568 node
= wx_spline_point_list
.First();
572 void wxPaintDC::DrawOpenSpline( wxList
*points
)
575 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
576 double x1
, y1
, x2
, y2
;
578 wxNode
*node
= points
->First();
579 p
= (wxPoint
*)node
->Data();
585 p
= (wxPoint
*)node
->Data();
589 cx1
= (double)((x1
+ x2
) / 2);
590 cy1
= (double)((y1
+ y2
) / 2);
591 cx2
= (double)((cx1
+ x2
) / 2);
592 cy2
= (double)((cy1
+ y2
) / 2);
594 wx_spline_add_point(x1
, y1
);
596 while ((node
= node
->Next()) != NULL
)
598 p
= (wxPoint
*)node
->Data();
603 cx4
= (double)(x1
+ x2
) / 2;
604 cy4
= (double)(y1
+ y2
) / 2;
605 cx3
= (double)(x1
+ cx4
) / 2;
606 cy3
= (double)(y1
+ cy4
) / 2;
608 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
612 cx2
= (double)(cx1
+ x2
) / 2;
613 cy2
= (double)(cy1
+ y2
) / 2;
616 wx_spline_add_point( cx1
, cy1
);
617 wx_spline_add_point( x2
, y2
);
619 wx_spline_draw_point_array( this );