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