]> git.saurik.com Git - wxWidgets.git/blame - src/common/dcbase.cpp
more standard (although less RFC-conformant) treatment of file: URIs (patch 1415189)
[wxWidgets.git] / src / common / dcbase.cpp
CommitLineData
dbe94982 1/////////////////////////////////////////////////////////////////////////////
1e6feb95
VZ
2// Name: common/dcbase.cpp
3// Purpose: generic methods of the wxDC Class
4// Author: Vadim Zeitlin
dbe94982
BM
5// Modified by:
6// Created: 05/25/99
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
dbe94982
BM
10/////////////////////////////////////////////////////////////////////////////
11
1e6feb95
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
1e6feb95
VZ
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
dbe94982
BM
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
bd1e9c12 24 #pragma hdrstop
dbe94982
BM
25#endif
26
dbe94982 27#include "wx/dc.h"
c77a6796 28#include "wx/math.h"
dbe94982 29
68379eaf 30// bool wxDCBase::sm_cacheing = false;
0cbff120 31
1e6feb95
VZ
32// ============================================================================
33// implementation
34// ============================================================================
35
68df211f
JG
36#if WXWIN_COMPATIBILITY_2_6
37void wxDCBase::BeginDrawing()
38{
39}
40
41void wxDCBase::EndDrawing()
42{
43}
44#endif // WXWIN_COMPATIBILITY_2_6
45
1e6feb95
VZ
46// ----------------------------------------------------------------------------
47// special symbols
48// ----------------------------------------------------------------------------
49
cd9da200
VZ
50void wxDCBase::DoDrawCheckMark(wxCoord x1, wxCoord y1,
51 wxCoord width, wxCoord height)
52{
53 wxCHECK_RET( Ok(), wxT("invalid window dc") );
54
55 wxCoord x2 = x1 + width,
56 y2 = y1 + height;
57
58 // this is to yield width of 3 for width == height == 10
59 SetPen(wxPen(GetTextForeground(), (width + height + 1) / 7, wxSOLID));
60
61 // we're drawing a scaled version of wx/generic/tick.xpm here
62 wxCoord x3 = x1 + (4*width) / 10, // x of the tick bottom
63 y3 = y1 + height / 2; // y of the left tick branch
64 DoDrawLine(x1, y3, x3, y2);
65 DoDrawLine(x3, y2, x2, y1);
66
67 CalcBoundingBox(x1, y1);
68 CalcBoundingBox(x2, y2);
69}
70
1e6feb95
VZ
71// ----------------------------------------------------------------------------
72// line/polygons
73// ----------------------------------------------------------------------------
74
72cdf4c9 75void wxDCBase::DrawLines(const wxList *list, wxCoord xoffset, wxCoord yoffset)
dbe94982 76{
b1d4dd7a 77 int n = list->GetCount();
dbe94982
BM
78 wxPoint *points = new wxPoint[n];
79
80 int i = 0;
222ed1d6 81 for ( wxList::compatibility_iterator node = list->GetFirst(); node; node = node->GetNext(), i++ )
dbe94982 82 {
b1d4dd7a 83 wxPoint *point = (wxPoint *)node->GetData();
dbe94982
BM
84 points[i].x = point->x;
85 points[i].y = point->y;
86 }
87
88 DoDrawLines(n, points, xoffset, yoffset);
89
90 delete [] points;
91}
92
93
94void wxDCBase::DrawPolygon(const wxList *list,
72cdf4c9 95 wxCoord xoffset, wxCoord yoffset,
dbe94982
BM
96 int fillStyle)
97{
b1d4dd7a 98 int n = list->GetCount();
dbe94982
BM
99 wxPoint *points = new wxPoint[n];
100
101 int i = 0;
222ed1d6 102 for ( wxList::compatibility_iterator node = list->GetFirst(); node; node = node->GetNext(), i++ )
dbe94982 103 {
b1d4dd7a 104 wxPoint *point = (wxPoint *)node->GetData();
dbe94982
BM
105 points[i].x = point->x;
106 points[i].y = point->y;
107 }
108
109 DoDrawPolygon(n, points, xoffset, yoffset, fillStyle);
110
111 delete [] points;
112}
113
63b9e659
VZ
114void
115wxDCBase::DoDrawPolyPolygon(int n,
793db755 116 int count[],
63b9e659
VZ
117 wxPoint points[],
118 wxCoord xoffset, wxCoord yoffset,
119 int fillStyle)
120{
121 if ( n == 1 )
122 {
793db755 123 DoDrawPolygon(count[0], points, xoffset, yoffset, fillStyle);
63b9e659
VZ
124 return;
125 }
126
127 int i, j, lastOfs;
128 wxPoint* pts;
129 wxPen pen;
130
131 for (i = j = lastOfs = 0; i < n; i++)
132 {
133 lastOfs = j;
793db755 134 j += count[i];
63b9e659
VZ
135 }
136 pts = new wxPoint[j+n-1];
137 for (i = 0; i < j; i++)
138 pts[i] = points[i];
139 for (i = 2; i <= n; i++)
140 {
793db755 141 lastOfs -= count[n-i];
63b9e659
VZ
142 pts[j++] = pts[lastOfs];
143 }
144
145 pen = GetPen();
146 SetPen(wxPen(*wxBLACK, 0, wxTRANSPARENT));
147 DoDrawPolygon(j, pts, xoffset, yoffset, fillStyle);
148 SetPen(pen);
149 for (i = j = 0; i < n; i++)
150 {
793db755
VZ
151 DoDrawLines(count[i], pts+j, xoffset, yoffset);
152 j += count[i];
63b9e659 153 }
6db1f43b 154 delete[] pts;
63b9e659
VZ
155}
156
1e6feb95
VZ
157// ----------------------------------------------------------------------------
158// splines
159// ----------------------------------------------------------------------------
dbe94982 160
88ac883a 161#if wxUSE_SPLINES
dbe94982
BM
162
163// TODO: this API needs fixing (wxPointList, why (!const) "wxList *"?)
72cdf4c9 164void wxDCBase::DrawSpline(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord x3, wxCoord y3)
dbe94982
BM
165{
166 wxList point_list;
167
168 wxPoint *point1 = new wxPoint;
169 point1->x = x1; point1->y = y1;
170 point_list.Append((wxObject*)point1);
171
172 wxPoint *point2 = new wxPoint;
173 point2->x = x2; point2->y = y2;
174 point_list.Append((wxObject*)point2);
175
176 wxPoint *point3 = new wxPoint;
177 point3->x = x3; point3->y = y3;
178 point_list.Append((wxObject*)point3);
179
180 DrawSpline(&point_list);
181
222ed1d6 182 for( wxList::compatibility_iterator node = point_list.GetFirst(); node; node = node->GetNext() )
dbe94982 183 {
b1d4dd7a 184 wxPoint *p = (wxPoint *)node->GetData();
dbe94982
BM
185 delete p;
186 }
187}
188
189void wxDCBase::DrawSpline(int n, wxPoint points[])
190{
191 wxList list;
192 for (int i =0; i < n; i++)
193 {
194 list.Append((wxObject*)&points[i]);
195 }
196
197 DrawSpline(&list);
198}
199
fe2e4366
VS
200// ----------------------------------- spline code ----------------------------------------
201
202void wx_quadratic_spline(double a1, double b1, double a2, double b2,
203 double a3, double b3, double a4, double b4);
204void wx_clear_stack();
205int wx_spline_pop(double *x1, double *y1, double *x2, double *y2, double *x3,
206 double *y3, double *x4, double *y4);
207void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3,
208 double x4, double y4);
209static bool wx_spline_add_point(double x, double y);
210static void wx_spline_draw_point_array(wxDCBase *dc);
211
212wxList wx_spline_point_list;
213
214#define half(z1, z2) ((z1+z2)/2.0)
215#define THRESHOLD 5
216
217/* iterative version */
218
219void wx_quadratic_spline(double a1, double b1, double a2, double b2, double a3, double b3, double a4,
220 double b4)
221{
222 register double xmid, ymid;
223 double x1, y1, x2, y2, x3, y3, x4, y4;
224
225 wx_clear_stack();
226 wx_spline_push(a1, b1, a2, b2, a3, b3, a4, b4);
227
228 while (wx_spline_pop(&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4)) {
229 xmid = (double)half(x2, x3);
230 ymid = (double)half(y2, y3);
231 if (fabs(x1 - xmid) < THRESHOLD && fabs(y1 - ymid) < THRESHOLD &&
232 fabs(xmid - x4) < THRESHOLD && fabs(ymid - y4) < THRESHOLD) {
233 wx_spline_add_point( x1, y1 );
234 wx_spline_add_point( xmid, ymid );
235 } else {
236 wx_spline_push(xmid, ymid, (double)half(xmid, x3), (double)half(ymid, y3),
237 (double)half(x3, x4), (double)half(y3, y4), x4, y4);
238 wx_spline_push(x1, y1, (double)half(x1, x2), (double)half(y1, y2),
239 (double)half(x2, xmid), (double)half(y2, ymid), xmid, ymid);
240 }
241 }
242}
243
244/* utilities used by spline drawing routines */
245
246typedef struct wx_spline_stack_struct {
247 double x1, y1, x2, y2, x3, y3, x4, y4;
248} Stack;
249
250#define SPLINE_STACK_DEPTH 20
251static Stack wx_spline_stack[SPLINE_STACK_DEPTH];
252static Stack *wx_stack_top;
253static int wx_stack_count;
254
255void wx_clear_stack()
256{
257 wx_stack_top = wx_spline_stack;
258 wx_stack_count = 0;
259}
260
261void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
262{
263 wx_stack_top->x1 = x1;
264 wx_stack_top->y1 = y1;
265 wx_stack_top->x2 = x2;
266 wx_stack_top->y2 = y2;
267 wx_stack_top->x3 = x3;
268 wx_stack_top->y3 = y3;
269 wx_stack_top->x4 = x4;
270 wx_stack_top->y4 = y4;
271 wx_stack_top++;
272 wx_stack_count++;
273}
274
275int wx_spline_pop(double *x1, double *y1, double *x2, double *y2,
276 double *x3, double *y3, double *x4, double *y4)
277{
278 if (wx_stack_count == 0)
279 return (0);
280 wx_stack_top--;
281 wx_stack_count--;
282 *x1 = wx_stack_top->x1;
283 *y1 = wx_stack_top->y1;
284 *x2 = wx_stack_top->x2;
285 *y2 = wx_stack_top->y2;
286 *x3 = wx_stack_top->x3;
287 *y3 = wx_stack_top->y3;
288 *x4 = wx_stack_top->x4;
289 *y4 = wx_stack_top->y4;
290 return (1);
291}
292
293static bool wx_spline_add_point(double x, double y)
294{
295 wxPoint *point = new wxPoint ;
296 point->x = (int) x;
297 point->y = (int) y;
298 wx_spline_point_list.Append((wxObject*)point);
68379eaf 299 return true;
fe2e4366
VS
300}
301
302static void wx_spline_draw_point_array(wxDCBase *dc)
303{
304 dc->DrawLines(&wx_spline_point_list, 0, 0 );
222ed1d6 305 wxList::compatibility_iterator node = wx_spline_point_list.GetFirst();
fe2e4366
VS
306 while (node)
307 {
b1d4dd7a 308 wxPoint *point = (wxPoint *)node->GetData();
fe2e4366 309 delete point;
222ed1d6 310 wx_spline_point_list.Erase(node);
b1d4dd7a 311 node = wx_spline_point_list.GetFirst();
fe2e4366
VS
312 }
313}
314
315void wxDCBase::DoDrawSpline( wxList *points )
316{
317 wxCHECK_RET( Ok(), wxT("invalid window dc") );
318
319 wxPoint *p;
320 double cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4;
321 double x1, y1, x2, y2;
322
222ed1d6 323 wxList::compatibility_iterator node = points->GetFirst();
55e3f1c4 324 if (node == wxList::compatibility_iterator())
410bb8c1
RD
325 // empty list
326 return;
e0d31471 327
b1d4dd7a 328 p = (wxPoint *)node->GetData();
fe2e4366
VS
329
330 x1 = p->x;
331 y1 = p->y;
332
b1d4dd7a
RL
333 node = node->GetNext();
334 p = (wxPoint *)node->GetData();
fe2e4366
VS
335
336 x2 = p->x;
337 y2 = p->y;
338 cx1 = (double)((x1 + x2) / 2);
339 cy1 = (double)((y1 + y2) / 2);
340 cx2 = (double)((cx1 + x2) / 2);
341 cy2 = (double)((cy1 + y2) / 2);
342
343 wx_spline_add_point(x1, y1);
344
28b4db7f
VZ
345 while ((node = node->GetNext())
346#if !wxUSE_STL
347 != NULL
348#endif // !wxUSE_STL
349 )
fe2e4366 350 {
b1d4dd7a 351 p = (wxPoint *)node->GetData();
fe2e4366
VS
352 x1 = x2;
353 y1 = y2;
354 x2 = p->x;
355 y2 = p->y;
356 cx4 = (double)(x1 + x2) / 2;
357 cy4 = (double)(y1 + y2) / 2;
358 cx3 = (double)(x1 + cx4) / 2;
359 cy3 = (double)(y1 + cy4) / 2;
360
361 wx_quadratic_spline(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
362
363 cx1 = cx4;
364 cy1 = cy4;
365 cx2 = (double)(cx1 + x2) / 2;
366 cy2 = (double)(cy1 + y2) / 2;
367 }
368
369 wx_spline_add_point( cx1, cy1 );
370 wx_spline_add_point( x2, y2 );
371
372 wx_spline_draw_point_array( this );
373}
374
88ac883a 375#endif // wxUSE_SPLINES
1e6feb95 376
0919e93e
RD
377// ----------------------------------------------------------------------------
378// Partial Text Extents
379// ----------------------------------------------------------------------------
380
381
174ee1b3 382// Each element of the widths array will be the width of the string up to and
3103e8a9 383// including the corresponding character in text. This is the generic
0919e93e 384// implementation, the port-specific classes should do this with native APIs
174ee1b3
RD
385// if available and if faster. Note: pango_layout_index_to_pos is much slower
386// than calling GetTextExtent!!
387
06fe86b7 388#define FWC_SIZE 256
174ee1b3
RD
389
390class FontWidthCache
391{
392public:
393 FontWidthCache() : m_scaleX(1), m_widths(NULL) { }
394 ~FontWidthCache() { delete []m_widths; }
2aaa050c
VZ
395
396 void Reset()
397 {
398 if (!m_widths)
399 m_widths = new int[FWC_SIZE];
400
401 memset(m_widths, 0, sizeof(int)*FWC_SIZE);
402 }
403
174ee1b3
RD
404 wxFont m_font;
405 double m_scaleX;
406 int *m_widths;
407};
408
409static FontWidthCache s_fontWidthCache;
0919e93e
RD
410
411bool wxDCBase::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
412{
413 int totalWidth = 0;
0919e93e 414
c77a6796 415 const size_t len = text.Length();
0919e93e 416 widths.Empty();
174ee1b3 417 widths.Add(0, len);
2aaa050c 418
174ee1b3 419 // reset the cache if font or horizontal scale have changed
c77a6796
VZ
420 if ( !s_fontWidthCache.m_widths ||
421 !wxIsSameDouble(s_fontWidthCache.m_scaleX, m_scaleX) ||
422 (s_fontWidthCache.m_font != GetFont()) )
174ee1b3
RD
423 {
424 s_fontWidthCache.Reset();
425 s_fontWidthCache.m_font = GetFont();
426 s_fontWidthCache.m_scaleX = m_scaleX;
427 }
428
0919e93e
RD
429 // Calculate the position of each character based on the widths of
430 // the previous characters
c77a6796
VZ
431 int w, h;
432 for ( size_t i = 0; i < len; i++ )
174ee1b3
RD
433 {
434 const wxChar c = text[i];
435 unsigned int c_int = (unsigned int)c;
436
2aaa050c 437 if ((c_int < FWC_SIZE) && (s_fontWidthCache.m_widths[c_int] != 0))
174ee1b3
RD
438 {
439 w = s_fontWidthCache.m_widths[c_int];
440 }
2aaa050c 441 else
174ee1b3
RD
442 {
443 GetTextExtent(c, &w, &h);
444 if (c_int < FWC_SIZE)
445 s_fontWidthCache.m_widths[c_int] = w;
446 }
447
0919e93e
RD
448 totalWidth += w;
449 widths[i] = totalWidth;
450 }
2aaa050c 451
0919e93e
RD
452 return true;
453}
454
455
1e6feb95
VZ
456// ----------------------------------------------------------------------------
457// enhanced text drawing
458// ----------------------------------------------------------------------------
459
460void wxDCBase::GetMultiLineTextExtent(const wxString& text,
461 wxCoord *x,
462 wxCoord *y,
463 wxCoord *h,
464 wxFont *font)
465{
c7aaa64f
VS
466 wxCoord widthTextMax = 0, widthLine,
467 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
1e6feb95
VZ
468
469 wxString curLine;
470 for ( const wxChar *pc = text; ; pc++ )
471 {
472 if ( *pc == _T('\n') || *pc == _T('\0') )
473 {
474 if ( curLine.empty() )
475 {
476 // we can't use GetTextExtent - it will return 0 for both width
477 // and height and an empty line should count in height
478 // calculation
479
480 // assume that this line has the same height as the previous
481 // one
482 if ( !heightLineDefault )
483 heightLineDefault = heightLine;
484
485 if ( !heightLineDefault )
486 {
487 // but we don't know it yet - choose something reasonable
488 GetTextExtent(_T("W"), NULL, &heightLineDefault,
489 NULL, NULL, font);
490 }
491
492 heightTextTotal += heightLineDefault;
493 }
494 else
495 {
496 GetTextExtent(curLine, &widthLine, &heightLine,
497 NULL, NULL, font);
498 if ( widthLine > widthTextMax )
499 widthTextMax = widthLine;
500 heightTextTotal += heightLine;
501 }
502
503 if ( *pc == _T('\n') )
504 {
505 curLine.clear();
506 }
507 else
508 {
509 // the end of string
510 break;
511 }
512 }
513 else
514 {
515 curLine += *pc;
516 }
517 }
518
519 if ( x )
520 *x = widthTextMax;
521 if ( y )
522 *y = heightTextTotal;
523 if ( h )
524 *h = heightLine;
525}
526
527void wxDCBase::DrawLabel(const wxString& text,
528 const wxBitmap& bitmap,
529 const wxRect& rect,
530 int alignment,
531 int indexAccel,
532 wxRect *rectBounding)
533{
534 // find the text position
535 wxCoord widthText, heightText, heightLine;
536 GetMultiLineTextExtent(text, &widthText, &heightText, &heightLine);
537
538 wxCoord width, height;
539 if ( bitmap.Ok() )
540 {
541 width = widthText + bitmap.GetWidth();
542 height = bitmap.GetHeight();
543 }
544 else // no bitmap
545 {
546 width = widthText;
547 height = heightText;
548 }
549
550 wxCoord x, y;
551 if ( alignment & wxALIGN_RIGHT )
552 {
553 x = rect.GetRight() - width;
554 }
555 else if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
556 {
557 x = (rect.GetLeft() + rect.GetRight() + 1 - width) / 2;
558 }
559 else // alignment & wxALIGN_LEFT
560 {
561 x = rect.GetLeft();
562 }
563
564 if ( alignment & wxALIGN_BOTTOM )
565 {
566 y = rect.GetBottom() - height;
567 }
568 else if ( alignment & wxALIGN_CENTRE_VERTICAL )
569 {
570 y = (rect.GetTop() + rect.GetBottom() + 1 - height) / 2;
571 }
572 else // alignment & wxALIGN_TOP
573 {
574 y = rect.GetTop();
575 }
576
577 // draw the bitmap first
578 wxCoord x0 = x,
579 y0 = y,
580 width0 = width;
581 if ( bitmap.Ok() )
582 {
68379eaf 583 DrawBitmap(bitmap, x, y, true /* use mask */);
1e6feb95
VZ
584
585 wxCoord offset = bitmap.GetWidth() + 4;
586 x += offset;
587 width -= offset;
588
589 y += (height - heightText) / 2;
590 }
591
592 // we will draw the underscore under the accel char later
593 wxCoord startUnderscore = 0,
594 endUnderscore = 0,
595 yUnderscore = 0;
596
597 // split the string into lines and draw each of them separately
598 wxString curLine;
599 for ( const wxChar *pc = text; ; pc++ )
600 {
601 if ( *pc == _T('\n') || *pc == _T('\0') )
602 {
603 int xRealStart = x; // init it here to avoid compielr warnings
604
605 if ( !curLine.empty() )
606 {
607 // NB: can't test for !(alignment & wxALIGN_LEFT) because
608 // wxALIGN_LEFT is 0
609 if ( alignment & (wxALIGN_RIGHT | wxALIGN_CENTRE_HORIZONTAL) )
610 {
611 wxCoord widthLine;
612 GetTextExtent(curLine, &widthLine, NULL);
613
614 if ( alignment & wxALIGN_RIGHT )
615 {
616 xRealStart += width - widthLine;
617 }
618 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
619 {
620 xRealStart += (width - widthLine) / 2;
621 }
622 }
623 //else: left aligned, nothing to do
624
625 DrawText(curLine, xRealStart, y);
626 }
627
628 y += heightLine;
629
630 // do we have underscore in this line? we can check yUnderscore
631 // because it is set below to just y + heightLine if we do
632 if ( y == yUnderscore )
633 {
634 // adjust the horz positions to account for the shift
635 startUnderscore += xRealStart;
636 endUnderscore += xRealStart;
637 }
638
639 if ( *pc == _T('\0') )
640 break;
641
642 curLine.clear();
643 }
644 else // not end of line
645 {
328bfc22 646 if ( pc - text.c_str() == indexAccel )
1e6feb95
VZ
647 {
648 // remeber to draw underscore here
649 GetTextExtent(curLine, &startUnderscore, NULL);
650 curLine += *pc;
651 GetTextExtent(curLine, &endUnderscore, NULL);
652
653 yUnderscore = y + heightLine;
654 }
655 else
656 {
657 curLine += *pc;
658 }
659 }
660 }
661
662 // draw the underscore if found
663 if ( startUnderscore != endUnderscore )
664 {
665 // it should be of the same colour as text
666 SetPen(wxPen(GetTextForeground(), 0, wxSOLID));
667
668 yUnderscore--;
669
670 DrawLine(startUnderscore, yUnderscore, endUnderscore, yUnderscore);
671 }
672
673 // return bounding rect if requested
674 if ( rectBounding )
675 {
676 *rectBounding = wxRect(x, y - heightText, widthText, heightText);
677 }
678
679 CalcBoundingBox(x0, y0);
680 CalcBoundingBox(x0 + width0, y0 + height);
681}
12bdd77c
JS
682
683/*
77ffb593 684Notes for wxWidgets DrawEllipticArcRot(...)
12bdd77c
JS
685
686wxDCBase::DrawEllipticArcRot(...) draws a rotated elliptic arc or an ellipse.
687It uses wxDCBase::CalculateEllipticPoints(...) and wxDCBase::Rotate(...),
688which are also new.
689
690All methods are generic, so they can be implemented in wxDCBase.
691DoDrawEllipticArcRot(...) is virtual, so it can be called from deeper
692methods like (WinCE) wxDC::DoDrawArc(...).
693
694CalculateEllipticPoints(...) fills a given list of wxPoints with some points
2aaa050c 695of an elliptic arc. The algorithm is pixel-based: In every row (in flat
12bdd77c
JS
696parts) or every column (in steep parts) only one pixel is calculated.
697Trigonometric calculation (sin, cos, tan, atan) is only done if the
2aaa050c 698starting angle is not equal to the ending angle. The calculation of the
12bdd77c
JS
699pixels is done using simple arithmetic only and should perform not too
700bad even on devices without floating point processor. I didn't test this yet.
701
702Rotate(...) rotates a list of point pixel-based, you will see rounding errors.
2aaa050c 703For instance: an ellipse rotated 180 degrees is drawn
12bdd77c
JS
704slightly different from the original.
705
2aaa050c
VZ
706The points are then moved to an array and used to draw a polyline and/or polygon
707(with center added, the pie).
12bdd77c
JS
708The result looks quite similar to the native ellipse, only e few pixels differ.
709
710The performance on a desktop system (Athlon 1800, WinXP) is about 7 times
711slower as DrawEllipse(...), which calls the native API.
712An rotated ellipse outside the clipping region takes nearly the same time,
713while an native ellipse outside takes nearly no time to draw.
714
2aaa050c 715If you draw an arc with this new method, you will see the starting and ending angles
12bdd77c
JS
716are calculated properly.
717If you use DrawEllipticArc(...), you will see they are only correct for circles
718and not properly calculated for ellipses.
719
720Peter Lenhard
721p.lenhard@t-online.de
722*/
723
724#ifdef __WXWINCE__
2aaa050c
VZ
725void wxDCBase::DoDrawEllipticArcRot( wxCoord x, wxCoord y,
726 wxCoord w, wxCoord h,
12bdd77c
JS
727 double sa, double ea, double angle )
728{
729 wxList list;
730
731 CalculateEllipticPoints( &list, x, y, w, h, sa, ea );
732 Rotate( &list, angle, wxPoint( x+w/2, y+h/2 ) );
733
734 // Add center (for polygon/pie)
735 list.Append( (wxObject*) new wxPoint( x+w/2, y+h/2 ) );
736
737 // copy list into array and delete list elements
738 int n = list.Number();
739 wxPoint *points = new wxPoint[n];
740 int i = 0;
2aaa050c 741 wxNode* node = 0;
12bdd77c
JS
742 for ( node = list.First(); node; node = node->Next(), i++ )
743 {
744 wxPoint *point = (wxPoint *)node->Data();
745 points[i].x = point->x;
746 points[i].y = point->y;
747 delete point;
748 }
749
750 // first draw the pie without pen, if necessary
63b9e659 751 if( GetBrush() != *wxTRANSPARENT_BRUSH )
12bdd77c
JS
752 {
753 wxPen tempPen( GetPen() );
754 SetPen( *wxTRANSPARENT_PEN );
755 DoDrawPolygon( n, points, 0, 0 );
756 SetPen( tempPen );
757 }
758
759 // then draw the arc without brush, if necessary
63b9e659 760 if( GetPen() != *wxTRANSPARENT_PEN )
12bdd77c
JS
761 {
762 // without center
763 DoDrawLines( n-1, points, 0, 0 );
764 }
765
766 delete [] points;
767
768} // DrawEllipticArcRot
769
770void wxDCBase::Rotate( wxList* points, double angle, wxPoint center )
771{
772 if( angle != 0.0 )
773 {
e0d31471 774 double pi(M_PI);
12bdd77c
JS
775 double dSinA = -sin(angle*2.0*pi/360.0);
776 double dCosA = cos(angle*2.0*pi/360.0);
777 for ( wxNode* node = points->First(); node; node = node->Next() )
778 {
779 wxPoint* point = (wxPoint*)node->Data();
2aaa050c 780
12bdd77c
JS
781 // transform coordinates, if necessary
782 if( center.x ) point->x -= center.x;
783 if( center.y ) point->y -= center.y;
784
785 // calculate rotation, rounding simply by implicit cast to integer
786 int xTemp = point->x * dCosA - point->y * dSinA;
787 point->y = point->x * dSinA + point->y * dCosA;
788 point->x = xTemp;
789
790 // back transform coordinates, if necessary
791 if( center.x ) point->x += center.x;
792 if( center.y ) point->y += center.y;
793 }
794 }
795}
796
2aaa050c
VZ
797void wxDCBase::CalculateEllipticPoints( wxList* points,
798 wxCoord xStart, wxCoord yStart,
799 wxCoord w, wxCoord h,
12bdd77c
JS
800 double sa, double ea )
801{
e0d31471 802 double pi = M_PI;
12bdd77c
JS
803 double sar = 0;
804 double ear = 0;
805 int xsa = 0;
806 int ysa = 0;
807 int xea = 0;
808 int yea = 0;
809 int sq = 0;
810 int eq = 0;
811 bool bUseAngles = false;
812 if( w<0 ) w = -w;
813 if( h<0 ) h = -h;
814 // half-axes
815 wxCoord a = w/2;
816 wxCoord b = h/2;
817 // decrement 1 pixel if ellipse is smaller than 2*a, 2*b
818 int decrX = 0;
2aaa050c 819 if( 2*a == w ) decrX = 1;
12bdd77c 820 int decrY = 0;
2aaa050c 821 if( 2*b == h ) decrY = 1;
12bdd77c
JS
822 // center
823 wxCoord xCenter = xStart + a;
824 wxCoord yCenter = yStart + b;
825 // calculate data for start and end, if necessary
826 if( sa != ea )
827 {
828 bUseAngles = true;
829 // normalisation of angles
830 while( sa<0 ) sa += 360;
831 while( ea<0 ) ea += 360;
832 while( sa>=360 ) sa -= 360;
833 while( ea>=360 ) ea -= 360;
834 // calculate quadrant numbers
835 if( sa > 270 ) sq = 3;
836 else if( sa > 180 ) sq = 2;
837 else if( sa > 90 ) sq = 1;
838 if( ea > 270 ) eq = 3;
839 else if( ea > 180 ) eq = 2;
840 else if( ea > 90 ) eq = 1;
841 sar = sa * pi / 180.0;
842 ear = ea * pi / 180.0;
843 // correct angle circle -> ellipse
844 sar = atan( -a/(double)b * tan( sar ) );
2aaa050c 845 if ( sq == 1 || sq == 2 ) sar += pi;
12bdd77c
JS
846 ear = atan( -a/(double)b * tan( ear ) );
847 if ( eq == 1 || eq == 2 ) ear += pi;
848 // coordinates of points
849 xsa = xCenter + a * cos( sar );
850 if( sq == 0 || sq == 3 ) xsa -= decrX;
851 ysa = yCenter + b * sin( sar );
852 if( sq == 2 || sq == 3 ) ysa -= decrY;
853 xea = xCenter + a * cos( ear );
854 if( eq == 0 || eq == 3 ) xea -= decrX;
855 yea = yCenter + b * sin( ear );
856 if( eq == 2 || eq == 3 ) yea -= decrY;
857 } // if iUseAngles
858 // calculate c1 = b^2, c2 = b^2/a^2 with a = w/2, b = h/2
859 double c1 = b * b;
860 double c2 = 2.0 / w;
861 c2 *= c2;
862 c2 *= c1;
863 wxCoord x = 0;
864 wxCoord y = b;
865 long x2 = 1;
866 long y2 = y*y;
867 long y2_old = 0;
868 long y_old = 0;
869 // Lists for quadrant 1 to 4
870 wxList pointsarray[4];
871 // Calculate points for first quadrant and set in all quadrants
872 for( x = 0; x <= a; ++x )
873 {
874 x2 = x2+x+x-1;
875 y2_old = y2;
876 y_old = y;
877 bool bNewPoint = false;
878 while( y2 > c1 - c2 * x2 && y > 0 )
879 {
880 bNewPoint = true;
881 y2 = y2-y-y+1;
882 --y;
883 }
2aaa050c 884 // old y now to big: set point with old y, old x
12bdd77c
JS
885 if( bNewPoint && x>1)
886 {
887 int x1 = x - 1;
888 // remove points on the same line
889 pointsarray[0].Insert( (wxObject*) new wxPoint( xCenter + x1 - decrX, yCenter - y_old ) );
890 pointsarray[1].Append( (wxObject*) new wxPoint( xCenter - x1, yCenter - y_old ) );
891 pointsarray[2].Insert( (wxObject*) new wxPoint( xCenter - x1, yCenter + y_old - decrY ) );
892 pointsarray[3].Append( (wxObject*) new wxPoint( xCenter + x1 - decrX, yCenter + y_old - decrY ) );
893 } // set point
894 } // calculate point
2aaa050c 895
12bdd77c
JS
896 // Starting and/or ending points for the quadrants, first quadrant gets both.
897 pointsarray[0].Insert( (wxObject*) new wxPoint( xCenter + a - decrX, yCenter ) );
898 pointsarray[0].Append( (wxObject*) new wxPoint( xCenter, yCenter - b ) );
899 pointsarray[1].Append( (wxObject*) new wxPoint( xCenter - a, yCenter ) );
900 pointsarray[2].Append( (wxObject*) new wxPoint( xCenter, yCenter + b - decrY ) );
901 pointsarray[3].Append( (wxObject*) new wxPoint( xCenter + a - decrX, yCenter ) );
902
903 // copy quadrants in original list
904 if( bUseAngles )
905 {
906 // Copy the right part of the points in the lists
907 // and delete the wxPoints, because they do not leave this method.
908 points->Append( (wxObject*) new wxPoint( xsa, ysa ) );
909 int q = sq;
910 bool bStarted = false;
911 bool bReady = false;
912 bool bForceTurn = ( sq == eq && sa > ea );
913 while( !bReady )
914 {
915 for( wxNode *node = pointsarray[q].First(); node; node = node->Next() )
916 {
917 // once: go to starting point in start quadrant
918 if( !bStarted &&
2aaa050c
VZ
919 (
920 ( (wxPoint*) node->Data() )->x < xsa+1 && q <= 1
921 ||
12bdd77c
JS
922 ( (wxPoint*) node->Data() )->x > xsa-1 && q >= 2
923 )
2aaa050c 924 )
12bdd77c
JS
925 {
926 bStarted = true;
927 }
928
929 // copy point, if not at ending point
930 if( bStarted )
931 {
932 if( q != eq || bForceTurn
933 ||
2aaa050c
VZ
934 ( (wxPoint*) node->Data() )->x > xea+1 && q <= 1
935 ||
12bdd77c
JS
936 ( (wxPoint*) node->Data() )->x < xea-1 && q >= 2
937 )
938 {
939 // copy point
940 wxPoint* pPoint = new wxPoint( *((wxPoint*) node->Data() ) );
941 points->Append( (wxObject*) pPoint );
942 }
943 else if( q == eq && !bForceTurn || ( (wxPoint*) node->Data() )->x == xea)
944 {
2aaa050c 945 bReady = true;
12bdd77c
JS
946 }
947 }
948 } // for node
949 ++q;
950 if( q > 3 ) q = 0;
951 bForceTurn = false;
952 bStarted = true;
953 } // while not bReady
954 points->Append( (wxObject*) new wxPoint( xea, yea ) );
955
956 // delete points
957 for( q = 0; q < 4; ++q )
958 {
959 for( wxNode *node = pointsarray[q].First(); node; node = node->Next() )
960 {
961 wxPoint *p = (wxPoint *)node->Data();
962 delete p;
963 }
2aaa050c 964 }
12bdd77c
JS
965 }
966 else
967 {
968 // copy whole ellipse, wxPoints will be deleted outside
969 for( wxNode *node = pointsarray[0].First(); node; node = node->Next() )
970 {
971 wxObject *p = node->Data();
972 points->Append( p );
973 }
974 for( node = pointsarray[1].First(); node; node = node->Next() )
975 {
976 wxObject *p = node->Data();
977 points->Append( p );
978 }
979 for( node = pointsarray[2].First(); node; node = node->Next() )
980 {
981 wxObject *p = node->Data();
982 points->Append( p );
983 }
984 for( node = pointsarray[3].First(); node; node = node->Next() )
985 {
986 wxObject *p = node->Data();
987 points->Append( p );
988 }
989 } // not iUseAngles
990} // CalculateEllipticPoints
991
992#endif
993