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