]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/ogl/oglmisc.cpp
Warning fixes and deprecated method elimination from ABX.
[wxWidgets.git] / contrib / src / ogl / oglmisc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: misc.cpp
3 // Purpose: Miscellaneous OGL support functions
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 12/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "misc.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 #ifndef WX_PRECOMP
24 #include <wx/wx.h>
25 #endif
26
27 #if wxUSE_PROLOGIO
28 #include <wx/deprecated/wxexpr.h>
29 #endif
30
31 #include <wx/types.h>
32
33 #ifdef new
34 #undef new
35 #endif
36
37 #include <ctype.h>
38 #include <math.h>
39 #include <stdlib.h>
40
41 #include "wx/ogl/ogl.h"
42
43
44 wxFont* g_oglNormalFont;
45 wxPen* g_oglBlackPen;
46 wxPen* g_oglWhiteBackgroundPen;
47 wxPen* g_oglTransparentPen;
48 wxBrush* g_oglWhiteBackgroundBrush;
49 wxPen* g_oglBlackForegroundPen;
50 wxCursor* g_oglBullseyeCursor = NULL;
51
52 wxChar* oglBuffer = NULL;
53
54 wxList oglObjectCopyMapping(wxKEY_INTEGER);
55
56
57
58 void wxOGLInitialize()
59 {
60 g_oglBullseyeCursor = new wxCursor(wxCURSOR_BULLSEYE);
61
62 g_oglNormalFont = new wxFont(10, wxSWISS, wxNORMAL, wxNORMAL);
63
64 g_oglBlackPen = new wxPen(wxT("BLACK"), 1, wxSOLID);
65
66 g_oglWhiteBackgroundPen = new wxPen(wxT("WHITE"), 1, wxSOLID);
67 g_oglTransparentPen = new wxPen(wxT("WHITE"), 1, wxTRANSPARENT);
68 g_oglWhiteBackgroundBrush = new wxBrush(wxT("WHITE"), wxSOLID);
69 g_oglBlackForegroundPen = new wxPen(wxT("BLACK"), 1, wxSOLID);
70
71 OGLInitializeConstraintTypes();
72
73 // Initialize big buffer used when writing images
74 oglBuffer = new wxChar[3000];
75
76 }
77
78 void wxOGLCleanUp()
79 {
80 if (oglBuffer)
81 {
82 delete[] oglBuffer;
83 oglBuffer = NULL;
84 }
85 oglBuffer = NULL;
86
87 if (g_oglBullseyeCursor)
88 {
89 delete g_oglBullseyeCursor;
90 g_oglBullseyeCursor = NULL;
91 }
92
93 if (g_oglNormalFont)
94 {
95 delete g_oglNormalFont;
96 g_oglNormalFont = NULL;
97 }
98 if (g_oglBlackPen)
99 {
100 delete g_oglBlackPen;
101 g_oglBlackPen = NULL;
102 }
103 if (g_oglWhiteBackgroundPen)
104 {
105 delete g_oglWhiteBackgroundPen;
106 g_oglWhiteBackgroundPen = NULL;
107 }
108 if (g_oglTransparentPen)
109 {
110 delete g_oglTransparentPen;
111 g_oglTransparentPen = NULL;
112 }
113 if (g_oglWhiteBackgroundBrush)
114 {
115 delete g_oglWhiteBackgroundBrush;
116 g_oglWhiteBackgroundBrush = NULL;
117 }
118 if (g_oglBlackForegroundPen)
119 {
120 delete g_oglBlackForegroundPen;
121 g_oglBlackForegroundPen = NULL;
122 }
123
124 OGLCleanUpConstraintTypes();
125 }
126
127 wxFont *oglMatchFont(int point_size)
128 {
129 wxFont *font = wxTheFontList->FindOrCreateFont(point_size, wxSWISS, wxNORMAL, wxNORMAL);
130 #if 0
131 switch (point_size)
132 {
133 case 4:
134 font = swiss_font_4;
135 break;
136 case 6:
137 font = swiss_font_6;
138 break;
139 case 8:
140 font = swiss_font_8;
141 break;
142 case 12:
143 font = swiss_font_12;
144 break;
145 case 14:
146 font = swiss_font_14;
147 break;
148 case 18:
149 font = swiss_font_18;
150 break;
151 case 24:
152 font = swiss_font_24;
153 break;
154 default:
155 case 10:
156 font = swiss_font_10;
157 break;
158 }
159 #endif
160 return font;
161 }
162
163 int FontSizeDialog(wxFrame *parent, int old_size)
164 {
165 if (old_size <= 0)
166 old_size = 10;
167 wxString buf;
168 buf << old_size;
169 wxString ans = wxGetTextFromUser(wxT("Enter point size"), wxT("Font size"), buf, parent);
170 if (ans.Length() == 0)
171 return 0;
172
173 long new_size = 0;
174 ans.ToLong(&new_size);
175 if ((new_size <= 0) || (new_size > 40))
176 {
177 wxMessageBox(wxT("Invalid point size!"), wxT("Error"), wxOK);
178 return 0;
179 }
180 return new_size;
181 /*
182 char *strings[8];
183 strings[0] = "4";
184 strings[1] = "6";
185 strings[2] = "8";
186 strings[3] = "10";
187 strings[4] = "12";
188 strings[5] = "14";
189 strings[6] = "18";
190 strings[7] = "24";
191 char *ans = wxGetSingleChoice("Choose", "Choose a font size", 8, strings, parent);
192 if (ans)
193 {
194 int size;
195 sscanf(ans, "%d", &size);
196 return oglMatchFont(size);
197 }
198 else return NULL;
199 */
200 }
201
202 // Centre a list of strings in the given box. xOffset and yOffset are the
203 // the positions that these lines should be relative to, and this might be
204 // the same as m_xpos, m_ypos, but might be zero if formatting from left-justifying.
205 void oglCentreText(wxDC& dc, wxList *text_list,
206 double m_xpos, double m_ypos, double width, double height,
207 int formatMode)
208 {
209 int n = text_list->GetCount();
210
211 if (!text_list || (n == 0))
212 return;
213
214 // First, get maximum dimensions of box enclosing text
215
216 long char_height = 0;
217 long max_width = 0;
218 long current_width = 0;
219
220 // Store text extents for speed
221 double *widths = new double[n];
222
223 wxNode *current = text_list->GetFirst();
224 int i = 0;
225 while (current)
226 {
227 wxShapeTextLine *line = (wxShapeTextLine *)current->GetData();
228 dc.GetTextExtent(line->GetText(), &current_width, &char_height);
229 widths[i] = current_width;
230
231 if (current_width > max_width)
232 max_width = current_width;
233 current = current->GetNext();
234 i ++;
235 }
236
237 double max_height = n*char_height;
238
239 double xoffset, yoffset, xOffset, yOffset;
240
241 if (formatMode & FORMAT_CENTRE_VERT)
242 {
243 if (max_height < height)
244 yoffset = (double)(m_ypos - (height/2.0) + (height - max_height)/2.0);
245 else
246 yoffset = (double)(m_ypos - (height/2.0));
247 yOffset = m_ypos;
248 }
249 else
250 {
251 yoffset = 0.0;
252 yOffset = 0.0;
253 }
254
255 if (formatMode & FORMAT_CENTRE_HORIZ)
256 {
257 xoffset = (double)(m_xpos - width/2.0);
258 xOffset = m_xpos;
259 }
260 else
261 {
262 xoffset = 0.0;
263 xOffset = 0.0;
264 }
265
266 current = text_list->GetFirst();
267 i = 0;
268
269 while (current)
270 {
271 wxShapeTextLine *line = (wxShapeTextLine *)current->GetData();
272
273 double x;
274 if ((formatMode & FORMAT_CENTRE_HORIZ) && (widths[i] < width))
275 x = (double)((width - widths[i])/2.0 + xoffset);
276 else
277 x = xoffset;
278 double y = (double)(i*char_height + yoffset);
279
280 line->SetX( x - xOffset ); line->SetY( y - yOffset );
281 current = current->GetNext();
282 i ++;
283 }
284
285 delete widths;
286 }
287
288 // Centre a list of strings in the given box
289 void oglCentreTextNoClipping(wxDC& dc, wxList *text_list,
290 double m_xpos, double m_ypos, double width, double height)
291 {
292 int n = text_list->GetCount();
293
294 if (!text_list || (n == 0))
295 return;
296
297 // First, get maximum dimensions of box enclosing text
298
299 long char_height = 0;
300 long max_width = 0;
301 long current_width = 0;
302
303 // Store text extents for speed
304 double *widths = new double[n];
305
306 wxNode *current = text_list->GetFirst();
307 int i = 0;
308 while (current)
309 {
310 wxShapeTextLine *line = (wxShapeTextLine *)current->GetData();
311 dc.GetTextExtent(line->GetText(), &current_width, &char_height);
312 widths[i] = current_width;
313
314 if (current_width > max_width)
315 max_width = current_width;
316 current = current->GetNext();
317 i ++;
318 }
319
320 double max_height = n*char_height;
321
322 double yoffset = (double)(m_ypos - (height/2.0) + (height - max_height)/2.0);
323
324 double xoffset = (double)(m_xpos - width/2.0);
325
326 current = text_list->GetFirst();
327 i = 0;
328
329 while (current)
330 {
331 wxShapeTextLine *line = (wxShapeTextLine *)current->GetData();
332
333 double x = (double)((width - widths[i])/2.0 + xoffset);
334 double y = (double)(i*char_height + yoffset);
335
336 line->SetX( x - m_xpos ); line->SetY( y - m_ypos );
337 current = current->GetNext();
338 i ++;
339 }
340 delete widths;
341 }
342
343 void oglGetCentredTextExtent(wxDC& dc, wxList *text_list,
344 double WXUNUSED(m_xpos), double WXUNUSED(m_ypos), double WXUNUSED(width), double WXUNUSED(height),
345 double *actual_width, double *actual_height)
346 {
347 int n = text_list->GetCount();
348
349 if (!text_list || (n == 0))
350 {
351 *actual_width = 0;
352 *actual_height = 0;
353 return;
354 }
355
356 // First, get maximum dimensions of box enclosing text
357
358 long char_height = 0;
359 long max_width = 0;
360 long current_width = 0;
361
362 wxNode *current = text_list->GetFirst();
363 while (current)
364 {
365 wxShapeTextLine *line = (wxShapeTextLine *)current->GetData();
366 dc.GetTextExtent(line->GetText(), &current_width, &char_height);
367
368 if (current_width > max_width)
369 max_width = current_width;
370 current = current->GetNext();
371 }
372
373 *actual_height = n*char_height;
374 *actual_width = max_width;
375 }
376
377 // Format a string to a list of strings that fit in the given box.
378 // Interpret %n and 10 or 13 as a new line.
379 wxStringList *oglFormatText(wxDC& dc, const wxString& text, double width, double WXUNUSED(height), int formatMode)
380 {
381 // First, parse the string into a list of words
382 wxStringList word_list;
383
384 // Make new lines into NULL strings at this point
385 int i = 0; int j = 0; int len = text.Length();
386 wxChar word[200]; word[0] = 0;
387 bool end_word = FALSE; bool new_line = FALSE;
388 while (i < len)
389 {
390 switch (text[i])
391 {
392 case wxT('%'):
393 {
394 i ++;
395 if (i == len)
396 { word[j] = wxT('%'); j ++; }
397 else
398 {
399 if (text[i] == wxT('n'))
400 { new_line = TRUE; end_word = TRUE; i++; }
401 else
402 { word[j] = wxT('%'); j ++; word[j] = text[i]; j ++; i ++; }
403 }
404 break;
405 }
406 case 10:
407 {
408 new_line = TRUE; end_word = TRUE; i++;
409 break;
410 }
411 case 13:
412 {
413 new_line = TRUE; end_word = TRUE; i++;
414 }
415 case wxT(' '):
416 {
417 end_word = TRUE;
418 i ++;
419 break;
420 }
421 default:
422 {
423 word[j] = text[i];
424 j ++; i ++;
425 break;
426 }
427 }
428 if (i == len) end_word = TRUE;
429 if (end_word)
430 {
431 word[j] = 0;
432 j = 0;
433 word_list.Add(word);
434 end_word = FALSE;
435 }
436 if (new_line)
437 {
438 word_list.Append(NULL);
439 new_line = FALSE;
440 }
441 }
442 // Now, make a list of strings which can fit in the box
443 wxStringList *string_list = new wxStringList;
444
445 wxString buffer;
446 wxStringListNode *node = word_list.GetFirst();
447 long x, y;
448
449 while (node)
450 {
451 wxString oldBuffer(buffer);
452
453 wxChar *s = (wxChar *)node->GetData();
454 if (!s)
455 {
456 // FORCE NEW LINE
457 if (buffer.Length() > 0)
458 string_list->Add(buffer);
459
460 buffer.Empty();
461 }
462 else
463 {
464 if (buffer.Length() != 0)
465 buffer += wxT(" ");
466
467 buffer += s;
468 dc.GetTextExtent(buffer, &x, &y);
469
470 // Don't fit within the bounding box if we're fitting shape to contents
471 if ((x > width) && !(formatMode & FORMAT_SIZE_TO_CONTENTS))
472 {
473 // Deal with first word being wider than box
474 if (oldBuffer.Length() > 0)
475 string_list->Add(oldBuffer);
476
477 buffer.Empty();
478 buffer += s;
479 }
480 }
481
482 node = node->GetNext();
483 }
484 if (buffer.Length() != 0)
485 string_list->Add(buffer);
486
487 return string_list;
488 }
489
490 void oglDrawFormattedText(wxDC& dc, wxList *text_list,
491 double m_xpos, double m_ypos, double width, double height,
492 int formatMode)
493 {
494 double xoffset, yoffset;
495 if (formatMode & FORMAT_CENTRE_HORIZ)
496 xoffset = m_xpos;
497 else
498 xoffset = (double)(m_xpos - (width / 2.0));
499
500 if (formatMode & FORMAT_CENTRE_VERT)
501 yoffset = m_ypos;
502 else
503 yoffset = (double)(m_ypos - (height / 2.0));
504
505 dc.SetClippingRegion(
506 (long)(m_xpos - width/2.0), (long)(m_ypos - height/2.0),
507 (long)width, (long)height);
508
509 wxNode *current = text_list->GetFirst();
510 while (current)
511 {
512 wxShapeTextLine *line = (wxShapeTextLine *)current->GetData();
513
514 dc.DrawText(line->GetText(), WXROUND(xoffset + line->GetX()), WXROUND(yoffset + line->GetY()));
515 current = current->GetNext();
516 }
517
518 dc.DestroyClippingRegion();
519 }
520
521 /*
522 * Find centroid given list of points comprising polyline
523 *
524 */
525
526 void oglFindPolylineCentroid(wxList *points, double *x, double *y)
527 {
528 double xcount = 0;
529 double ycount = 0;
530
531 wxNode *node = points->GetFirst();
532 while (node)
533 {
534 wxRealPoint *point = (wxRealPoint *)node->GetData();
535 xcount += point->x;
536 ycount += point->y;
537 node = node->GetNext();
538 }
539
540 *x = (xcount/points->GetCount());
541 *y = (ycount/points->GetCount());
542 }
543
544 /*
545 * Check that (x1, y1) -> (x2, y2) hits (x3, y3) -> (x4, y4).
546 * If so, ratio1 gives the proportion along the first line
547 * that the intersection occurs (or something like that).
548 * Used by functions below.
549 *
550 */
551 void oglCheckLineIntersection(double x1, double y1, double x2, double y2,
552 double x3, double y3, double x4, double y4,
553 double *ratio1, double *ratio2)
554 {
555 double denominator_term = (y4 - y3)*(x2 - x1) - (y2 - y1)*(x4 - x3);
556 double numerator_term = (x3 - x1)*(y4 - y3) + (x4 - x3)*(y1 - y3);
557
558 double line_constant;
559 double length_ratio = 1.0;
560 double k_line = 1.0;
561
562 // Check for parallel lines
563 if ((denominator_term < 0.005) && (denominator_term > -0.005))
564 line_constant = -1.0;
565 else
566 line_constant = numerator_term/denominator_term;
567
568 // Check for intersection
569 if ((line_constant < 1.0) && (line_constant > 0.0))
570 {
571 // Now must check that other line hits
572 if (((y4 - y3) < 0.005) && ((y4 - y3) > -0.005))
573 k_line = ((x1 - x3) + line_constant*(x2 - x1))/(x4 - x3);
574 else
575 k_line = ((y1 - y3) + line_constant*(y2 - y1))/(y4 - y3);
576
577 if ((k_line >= 0.0) && (k_line < 1.0))
578 length_ratio = line_constant;
579 else
580 k_line = 1.0;
581 }
582 *ratio1 = length_ratio;
583 *ratio2 = k_line;
584 }
585
586 /*
587 * Find where (x1, y1) -> (x2, y2) hits one of the lines in xvec, yvec.
588 * (*x3, *y3) is the point where it hits.
589 *
590 */
591 void oglFindEndForPolyline(double n, double xvec[], double yvec[],
592 double x1, double y1, double x2, double y2, double *x3, double *y3)
593 {
594 int i;
595 double lastx = xvec[0];
596 double lasty = yvec[0];
597
598 double min_ratio = 1.0;
599 double line_ratio;
600 double other_ratio;
601
602 for (i = 1; i < n; i++)
603 {
604 oglCheckLineIntersection(x1, y1, x2, y2, lastx, lasty, xvec[i], yvec[i],
605 &line_ratio, &other_ratio);
606 lastx = xvec[i];
607 lasty = yvec[i];
608
609 if (line_ratio < min_ratio)
610 min_ratio = line_ratio;
611 }
612
613 // Do last (implicit) line if last and first doubles are not identical
614 if (!(xvec[0] == lastx && yvec[0] == lasty))
615 {
616 oglCheckLineIntersection(x1, y1, x2, y2, lastx, lasty, xvec[0], yvec[0],
617 &line_ratio, &other_ratio);
618
619 if (line_ratio < min_ratio)
620 min_ratio = line_ratio;
621 }
622
623 *x3 = (x1 + (x2 - x1)*min_ratio);
624 *y3 = (y1 + (y2 - y1)*min_ratio);
625
626 }
627
628 /*
629 * Find where the line hits the box.
630 *
631 */
632
633 void oglFindEndForBox(double width, double height,
634 double x1, double y1, // Centre of box (possibly)
635 double x2, double y2, // other end of line
636 double *x3, double *y3) // End on box edge
637 {
638 double xvec[5];
639 double yvec[5];
640
641 xvec[0] = (double)(x1 - width/2.0);
642 yvec[0] = (double)(y1 - height/2.0);
643 xvec[1] = (double)(x1 - width/2.0);
644 yvec[1] = (double)(y1 + height/2.0);
645 xvec[2] = (double)(x1 + width/2.0);
646 yvec[2] = (double)(y1 + height/2.0);
647 xvec[3] = (double)(x1 + width/2.0);
648 yvec[3] = (double)(y1 - height/2.0);
649 xvec[4] = (double)(x1 - width/2.0);
650 yvec[4] = (double)(y1 - height/2.0);
651
652 oglFindEndForPolyline(5, xvec, yvec, x2, y2, x1, y1, x3, y3);
653 }
654
655 /*
656 * Find where the line hits the circle.
657 *
658 */
659
660 void oglFindEndForCircle(double radius,
661 double x1, double y1, // Centre of circle
662 double x2, double y2, // Other end of line
663 double *x3, double *y3)
664 {
665 double H = (double)sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
666
667 if (H == 0.0)
668 {
669 *x3 = x1;
670 *y3 = y1;
671 }
672 else
673 {
674 *y3 = radius * (y2 - y1)/H + y1;
675 *x3 = radius * (x2 - x1)/H + x1;
676 }
677 }
678
679 /*
680 * Given the line (x1, y1) -> (x2, y2), and an arrow size of given length and width,
681 * return the position of the tip of the arrow and the left and right vertices of the arrow.
682 *
683 */
684
685 void oglGetArrowPoints(double x1, double y1, double x2, double y2,
686 double length, double width,
687 double *tip_x, double *tip_y,
688 double *side1_x, double *side1_y,
689 double *side2_x, double *side2_y)
690 {
691 double l = (double)sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
692
693 if (l < 0.01)
694 l = (double) 0.01;
695
696 double i_bar = (x2 - x1)/l;
697 double j_bar = (y2 - y1)/l;
698
699 double x3 = (- length*i_bar) + x2;
700 double y3 = (- length*j_bar) + y2;
701
702 *side1_x = width*(-j_bar) + x3;
703 *side1_y = width*i_bar + y3;
704
705 *side2_x = -width*(-j_bar) + x3;
706 *side2_y = -width*i_bar + y3;
707
708 *tip_x = x2; *tip_y = y2;
709 }
710
711 /*
712 * Given an ellipse and endpoints of a line, returns the point at which
713 * the line touches the ellipse in values x4, y4.
714 * This function assumes that the centre of the ellipse is at x1, y1, and the
715 * ellipse has a width of width1 and a height of height1. It also assumes you are
716 * wanting to draw an arc FROM point x2, y2 TOWARDS point x3, y3.
717 * This function calculates the x,y coordinates of the intersection point of
718 * the arc with the ellipse.
719 * Author: Ian Harrison
720 */
721
722 void oglDrawArcToEllipse(double x1, double y1, double width1, double height1, double x2, double y2, double x3, double y3,
723 double *x4, double *y4)
724 {
725 double a1 = (double)(width1/2.0);
726 double b1 = (double)(height1/2.0);
727
728 // These are required to give top left x and y coordinates for DrawEllipse
729 // double top_left_x1 = (double)(x1 - a1);
730 // double top_left_y1 = (double)(y1 - b1);
731 /*
732 // Check for vertical line
733 if (fabs(x2 - x3) < 0.05)
734 {
735 *x4 = x3;
736 if (y2 < y3)
737 *y4 = (double)(y1 - b1);
738 else
739 *y4 = (double)(y1 + b1);
740 return;
741 }
742 */
743 // Check that x2 != x3
744 if (fabs(x2 - x3) < 0.05)
745 {
746 *x4 = x2;
747 if (y3 > y2)
748 *y4 = (double)(y1 - sqrt((b1*b1 - (((x2-x1)*(x2-x1))*(b1*b1)/(a1*a1)))));
749 else
750 *y4 = (double)(y1 + sqrt((b1*b1 - (((x2-x1)*(x2-x1))*(b1*b1)/(a1*a1)))));
751 return;
752 }
753
754 // Calculate the x and y coordinates of the point where arc intersects ellipse
755
756 double A, B, C, D, E, F, G, H, K;
757 double ellipse1_x, ellipse1_y;
758
759 A = (double)(1/(a1 * a1));
760 B = (double)((y3 - y2) * (y3 - y2)) / ((x3 - x2) * (x3 - x2) * b1 * b1);
761 C = (double)(2 * (y3 - y2) * (y2 - y1)) / ((x3 - x2) * b1 * b1);
762 D = (double)((y2 - y1) * (y2 - y1)) / (b1 * b1);
763 E = (double)(A + B);
764 F = (double)(C - (2 * A * x1) - (2 * B * x2));
765 G = (double)((A * x1 * x1) + (B * x2 * x2) - (C * x2) + D - 1);
766 H = (double)((y3 - y2) / (x3 - x2));
767 K = (double)((F * F) - (4 * E * G));
768
769 if (K >= 0)
770 // In this case the line intersects the ellipse, so calculate intersection
771 {
772 if(x2 >= x1)
773 {
774 ellipse1_x = (double)(((F * -1) + sqrt(K)) / (2 * E));
775 ellipse1_y = (double)((H * (ellipse1_x - x2)) + y2);
776 }
777 else
778 {
779 ellipse1_x = (double)(((F * -1) - sqrt(K)) / (2 * E));
780 ellipse1_y = (double)((H * (ellipse1_x - x2)) + y2);
781 }
782 }
783 else
784 // in this case, arc does not intersect ellipse, so just draw arc
785 {
786 ellipse1_x = x3;
787 ellipse1_y = y3;
788 }
789 *x4 = ellipse1_x;
790 *y4 = ellipse1_y;
791
792 /*
793 // Draw a little circle (radius = 2) at the end of the arc where it hits
794 // the ellipse .
795
796 double circle_x = ellipse1_x - 2.0;
797 double circle_y = ellipse1_y - 2.0;
798 m_canvas->DrawEllipse(circle_x, circle_y, 4.0, 4.0);
799 */
800 }
801
802 // Update a list item from a list of strings
803 void UpdateListBox(wxListBox *item, wxList *list)
804 {
805 item->Clear();
806 if (!list)
807 return;
808
809 wxNode *node = list->GetFirst();
810 while (node)
811 {
812 wxChar *s = (wxChar *)node->GetData();
813 item->Append(s);
814 node = node->GetNext();
815 }
816 }
817
818 bool oglRoughlyEqual(double val1, double val2, double tol)
819 {
820 return ( (val1 < (val2 + tol)) && (val1 > (val2 - tol)) &&
821 (val2 < (val1 + tol)) && (val2 > (val1 - tol)));
822 }
823
824 /*
825 * Hex<->Dec conversion
826 */
827
828 // Array used in DecToHex conversion routine.
829 static wxChar sg_HexArray[] = { wxT('0'), wxT('1'), wxT('2'), wxT('3'),
830 wxT('4'), wxT('5'), wxT('6'), wxT('7'),
831 wxT('8'), wxT('9'), wxT('A'), wxT('B'),
832 wxT('C'), wxT('D'), wxT('E'), wxT('F')
833 };
834
835 // Convert 2-digit hex number to decimal
836 unsigned int oglHexToDec(wxChar* buf)
837 {
838 int firstDigit, secondDigit;
839
840 if (buf[0] >= wxT('A'))
841 firstDigit = buf[0] - wxT('A') + 10;
842 else
843 firstDigit = buf[0] - wxT('0');
844
845 if (buf[1] >= wxT('A'))
846 secondDigit = buf[1] - wxT('A') + 10;
847 else
848 secondDigit = buf[1] - wxT('0');
849
850 return firstDigit * 16 + secondDigit;
851 }
852
853 // Convert decimal integer to 2-character hex string
854 void oglDecToHex(unsigned int dec, wxChar *buf)
855 {
856 int firstDigit = (int)(dec/16.0);
857 int secondDigit = (int)(dec - (firstDigit*16.0));
858 buf[0] = sg_HexArray[firstDigit];
859 buf[1] = sg_HexArray[secondDigit];
860 buf[2] = 0;
861 }
862
863 // 3-digit hex to wxColour
864 wxColour oglHexToColour(const wxString& hex)
865 {
866 if (hex.Length() == 6)
867 {
868 long r, g, b;
869 r = g = b = 0;
870 hex.Mid(0,2).ToLong(&r, 16);
871 hex.Mid(2,2).ToLong(&g, 16);
872 hex.Mid(4,2).ToLong(&b, 16);
873 return wxColour(r, g, b);
874 }
875 else
876 return wxColour(0,0,0);
877 }
878
879 // RGB to 3-digit hex
880 wxString oglColourToHex(const wxColour& colour)
881 {
882 wxChar buf[7];
883 unsigned int red = colour.Red();
884 unsigned int green = colour.Green();
885 unsigned int blue = colour.Blue();
886
887 oglDecToHex(red, buf);
888 oglDecToHex(green, buf+2);
889 oglDecToHex(blue, buf+4);
890
891 return wxString(buf);
892 }
893
894