]> git.saurik.com Git - wxWidgets.git/blob - src/common/dcbase.cpp
Fixed memory leak in textcmn.cpp
[wxWidgets.git] / src / common / dcbase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dc.cpp
3 // Purpose: wxDC Class
4 // Author: Brian Macy
5 // Modified by:
6 // Created: 05/25/99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "dcbase.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/dc.h"
24
25 #include <math.h>
26
27 void wxDCBase::DoDrawCheckMark(wxCoord x1, wxCoord y1,
28 wxCoord width, wxCoord height)
29 {
30 wxCHECK_RET( Ok(), wxT("invalid window dc") );
31
32 wxCoord x2 = x1 + width,
33 y2 = y1 + height;
34
35 // this is to yield width of 3 for width == height == 10
36 SetPen(wxPen(GetTextForeground(), (width + height + 1) / 7, wxSOLID));
37
38 // we're drawing a scaled version of wx/generic/tick.xpm here
39 wxCoord x3 = x1 + (4*width) / 10, // x of the tick bottom
40 y3 = y1 + height / 2; // y of the left tick branch
41 DoDrawLine(x1, y3, x3, y2);
42 DoDrawLine(x3, y2, x2, y1);
43
44 CalcBoundingBox(x1, y1);
45 CalcBoundingBox(x2, y2);
46 }
47
48 void wxDCBase::DrawLines(const wxList *list, wxCoord xoffset, wxCoord yoffset)
49 {
50 int n = list->Number();
51 wxPoint *points = new wxPoint[n];
52
53 int i = 0;
54 for ( wxNode *node = list->First(); node; node = node->Next(), i++ )
55 {
56 wxPoint *point = (wxPoint *)node->Data();
57 points[i].x = point->x;
58 points[i].y = point->y;
59 }
60
61 DoDrawLines(n, points, xoffset, yoffset);
62
63 delete [] points;
64 }
65
66
67 void wxDCBase::DrawPolygon(const wxList *list,
68 wxCoord xoffset, wxCoord yoffset,
69 int fillStyle)
70 {
71 int n = list->Number();
72 wxPoint *points = new wxPoint[n];
73
74 int i = 0;
75 for ( wxNode *node = list->First(); node; node = node->Next(), i++ )
76 {
77 wxPoint *point = (wxPoint *)node->Data();
78 points[i].x = point->x;
79 points[i].y = point->y;
80 }
81
82 DoDrawPolygon(n, points, xoffset, yoffset, fillStyle);
83
84 delete [] points;
85 }
86
87
88 #if wxUSE_SPLINES
89
90 // TODO: this API needs fixing (wxPointList, why (!const) "wxList *"?)
91 void wxDCBase::DrawSpline(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord x3, wxCoord y3)
92 {
93 wxList point_list;
94
95 wxPoint *point1 = new wxPoint;
96 point1->x = x1; point1->y = y1;
97 point_list.Append((wxObject*)point1);
98
99 wxPoint *point2 = new wxPoint;
100 point2->x = x2; point2->y = y2;
101 point_list.Append((wxObject*)point2);
102
103 wxPoint *point3 = new wxPoint;
104 point3->x = x3; point3->y = y3;
105 point_list.Append((wxObject*)point3);
106
107 DrawSpline(&point_list);
108
109 for( wxNode *node = point_list.First(); node; node = node->Next() )
110 {
111 wxPoint *p = (wxPoint *)node->Data();
112 delete p;
113 }
114 }
115
116 void wxDCBase::DrawSpline(int n, wxPoint points[])
117 {
118 wxList list;
119 for (int i =0; i < n; i++)
120 {
121 list.Append((wxObject*)&points[i]);
122 }
123
124 DrawSpline(&list);
125 }
126
127 // ----------------------------------- spline code ----------------------------------------
128
129 void wx_quadratic_spline(double a1, double b1, double a2, double b2,
130 double a3, double b3, double a4, double b4);
131 void wx_clear_stack();
132 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2, double *x3,
133 double *y3, double *x4, double *y4);
134 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3,
135 double x4, double y4);
136 static bool wx_spline_add_point(double x, double y);
137 static void wx_spline_draw_point_array(wxDCBase *dc);
138
139 wxList wx_spline_point_list;
140
141 #define half(z1, z2) ((z1+z2)/2.0)
142 #define THRESHOLD 5
143
144 /* iterative version */
145
146 void wx_quadratic_spline(double a1, double b1, double a2, double b2, double a3, double b3, double a4,
147 double b4)
148 {
149 register double xmid, ymid;
150 double x1, y1, x2, y2, x3, y3, x4, y4;
151
152 wx_clear_stack();
153 wx_spline_push(a1, b1, a2, b2, a3, b3, a4, b4);
154
155 while (wx_spline_pop(&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4)) {
156 xmid = (double)half(x2, x3);
157 ymid = (double)half(y2, y3);
158 if (fabs(x1 - xmid) < THRESHOLD && fabs(y1 - ymid) < THRESHOLD &&
159 fabs(xmid - x4) < THRESHOLD && fabs(ymid - y4) < THRESHOLD) {
160 wx_spline_add_point( x1, y1 );
161 wx_spline_add_point( xmid, ymid );
162 } else {
163 wx_spline_push(xmid, ymid, (double)half(xmid, x3), (double)half(ymid, y3),
164 (double)half(x3, x4), (double)half(y3, y4), x4, y4);
165 wx_spline_push(x1, y1, (double)half(x1, x2), (double)half(y1, y2),
166 (double)half(x2, xmid), (double)half(y2, ymid), xmid, ymid);
167 }
168 }
169 }
170
171 /* utilities used by spline drawing routines */
172
173 typedef struct wx_spline_stack_struct {
174 double x1, y1, x2, y2, x3, y3, x4, y4;
175 } Stack;
176
177 #define SPLINE_STACK_DEPTH 20
178 static Stack wx_spline_stack[SPLINE_STACK_DEPTH];
179 static Stack *wx_stack_top;
180 static int wx_stack_count;
181
182 void wx_clear_stack()
183 {
184 wx_stack_top = wx_spline_stack;
185 wx_stack_count = 0;
186 }
187
188 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
189 {
190 wx_stack_top->x1 = x1;
191 wx_stack_top->y1 = y1;
192 wx_stack_top->x2 = x2;
193 wx_stack_top->y2 = y2;
194 wx_stack_top->x3 = x3;
195 wx_stack_top->y3 = y3;
196 wx_stack_top->x4 = x4;
197 wx_stack_top->y4 = y4;
198 wx_stack_top++;
199 wx_stack_count++;
200 }
201
202 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2,
203 double *x3, double *y3, double *x4, double *y4)
204 {
205 if (wx_stack_count == 0)
206 return (0);
207 wx_stack_top--;
208 wx_stack_count--;
209 *x1 = wx_stack_top->x1;
210 *y1 = wx_stack_top->y1;
211 *x2 = wx_stack_top->x2;
212 *y2 = wx_stack_top->y2;
213 *x3 = wx_stack_top->x3;
214 *y3 = wx_stack_top->y3;
215 *x4 = wx_stack_top->x4;
216 *y4 = wx_stack_top->y4;
217 return (1);
218 }
219
220 static bool wx_spline_add_point(double x, double y)
221 {
222 wxPoint *point = new wxPoint ;
223 point->x = (int) x;
224 point->y = (int) y;
225 wx_spline_point_list.Append((wxObject*)point);
226 return TRUE;
227 }
228
229 static void wx_spline_draw_point_array(wxDCBase *dc)
230 {
231 dc->DrawLines(&wx_spline_point_list, 0, 0 );
232 wxNode *node = wx_spline_point_list.First();
233 while (node)
234 {
235 wxPoint *point = (wxPoint *)node->Data();
236 delete point;
237 delete node;
238 node = wx_spline_point_list.First();
239 }
240 }
241
242 void wxDCBase::DoDrawSpline( wxList *points )
243 {
244 wxCHECK_RET( Ok(), wxT("invalid window dc") );
245
246 wxPoint *p;
247 double cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4;
248 double x1, y1, x2, y2;
249
250 wxNode *node = points->First();
251 p = (wxPoint *)node->Data();
252
253 x1 = p->x;
254 y1 = p->y;
255
256 node = node->Next();
257 p = (wxPoint *)node->Data();
258
259 x2 = p->x;
260 y2 = p->y;
261 cx1 = (double)((x1 + x2) / 2);
262 cy1 = (double)((y1 + y2) / 2);
263 cx2 = (double)((cx1 + x2) / 2);
264 cy2 = (double)((cy1 + y2) / 2);
265
266 wx_spline_add_point(x1, y1);
267
268 while ((node = node->Next()) != NULL)
269 {
270 p = (wxPoint *)node->Data();
271 x1 = x2;
272 y1 = y2;
273 x2 = p->x;
274 y2 = p->y;
275 cx4 = (double)(x1 + x2) / 2;
276 cy4 = (double)(y1 + y2) / 2;
277 cx3 = (double)(x1 + cx4) / 2;
278 cy3 = (double)(y1 + cy4) / 2;
279
280 wx_quadratic_spline(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
281
282 cx1 = cx4;
283 cy1 = cy4;
284 cx2 = (double)(cx1 + x2) / 2;
285 cy2 = (double)(cy1 + y2) / 2;
286 }
287
288 wx_spline_add_point( cx1, cy1 );
289 wx_spline_add_point( x2, y2 );
290
291 wx_spline_draw_point_array( this );
292 }
293
294 #endif // wxUSE_SPLINES