1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Miscellaneous OGL support functions
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "misc.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include <wx/wxprec.h>
27 #include <wx/wxexpr.h>
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>
50 wxFont
* g_oglNormalFont
;
52 wxPen
* g_oglWhiteBackgroundPen
;
53 wxPen
* g_oglTransparentPen
;
54 wxBrush
* g_oglWhiteBackgroundBrush
;
55 wxPen
* g_oglBlackForegroundPen
;
56 wxCursor
* g_oglBullseyeCursor
= NULL
;
58 char* oglBuffer
= NULL
;
60 wxList
oglObjectCopyMapping(wxKEY_INTEGER
);
64 void wxOGLInitialize()
66 g_oglBullseyeCursor
= new wxCursor(wxCURSOR_BULLSEYE
);
68 g_oglNormalFont
= new wxFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
70 g_oglBlackPen
= new wxPen("BLACK", 1, wxSOLID
);
72 g_oglWhiteBackgroundPen
= new wxPen("WHITE", 1, wxSOLID
);
73 g_oglTransparentPen
= new wxPen("WHITE", 1, wxTRANSPARENT
);
74 g_oglWhiteBackgroundBrush
= new wxBrush("WHITE", wxSOLID
);
75 g_oglBlackForegroundPen
= new wxPen("BLACK", 1, wxSOLID
);
77 OGLInitializeConstraintTypes();
79 // Initialize big buffer used when writing images
80 oglBuffer
= new char[3000];
92 if (g_oglBullseyeCursor
)
94 delete g_oglBullseyeCursor
;
95 g_oglBullseyeCursor
= NULL
;
100 delete g_oglNormalFont
;
101 g_oglNormalFont
= NULL
;
105 delete g_oglBlackPen
;
106 g_oglBlackPen
= NULL
;
108 if (g_oglWhiteBackgroundPen
)
110 delete g_oglWhiteBackgroundPen
;
111 g_oglWhiteBackgroundPen
= NULL
;
113 if (g_oglTransparentPen
)
115 delete g_oglTransparentPen
;
116 g_oglTransparentPen
= NULL
;
118 if (g_oglWhiteBackgroundBrush
)
120 delete g_oglWhiteBackgroundBrush
;
121 g_oglWhiteBackgroundBrush
= NULL
;
123 if (g_oglBlackForegroundPen
)
125 delete g_oglBlackForegroundPen
;
126 g_oglBlackForegroundPen
= NULL
;
129 OGLCleanUpConstraintTypes();
132 wxFont
*oglMatchFont(int point_size
)
134 wxFont
*font
= wxTheFontList
->FindOrCreateFont(point_size
, wxSWISS
, wxNORMAL
, wxNORMAL
);
148 font
= swiss_font_12
;
151 font
= swiss_font_14
;
154 font
= swiss_font_18
;
157 font
= swiss_font_24
;
161 font
= swiss_font_10
;
168 int FontSizeDialog(wxFrame
*parent
, int old_size
)
173 sprintf(buf
, "%d", old_size
);
174 wxString ans
= wxGetTextFromUser("Enter point size", "Font size", buf
, parent
);
178 int new_size
= atoi(ans
);
179 if ((new_size
<= 0) || (new_size
> 40))
181 wxMessageBox("Invalid point size!", "Error", wxOK
);
195 char *ans = wxGetSingleChoice("Choose", "Choose a font size", 8, strings, parent);
199 sscanf(ans, "%d", &size);
200 return oglMatchFont(size);
206 // Centre a list of strings in the given box. xOffset and yOffset are the
207 // the positions that these lines should be relative to, and this might be
208 // the same as m_xpos, m_ypos, but might be zero if formatting from left-justifying.
209 void oglCentreText(wxDC
& dc
, wxList
*text_list
,
210 double m_xpos
, double m_ypos
, double width
, double height
,
213 int n
= text_list
->Number();
215 if (!text_list
|| (n
== 0))
218 // First, get maximum dimensions of box enclosing text
220 long char_height
= 0;
222 long current_width
= 0;
224 // Store text extents for speed
225 double *widths
= new double[n
];
227 wxNode
*current
= text_list
->First();
231 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
232 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
233 widths
[i
] = current_width
;
235 if (current_width
> max_width
)
236 max_width
= current_width
;
237 current
= current
->Next();
241 double max_height
= n
*char_height
;
243 double xoffset
, yoffset
, xOffset
, yOffset
;
245 if (formatMode
& FORMAT_CENTRE_VERT
)
247 if (max_height
< height
)
248 yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
250 yoffset
= (double)(m_ypos
- (height
/2.0));
259 if (formatMode
& FORMAT_CENTRE_HORIZ
)
261 xoffset
= (double)(m_xpos
- width
/2.0);
270 current
= text_list
->First();
275 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
278 if ((formatMode
& FORMAT_CENTRE_HORIZ
) && (widths
[i
] < width
))
279 x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
282 double y
= (double)(i
*char_height
+ yoffset
);
284 line
->SetX( x
- xOffset
); line
->SetY( y
- yOffset
);
285 current
= current
->Next();
292 // Centre a list of strings in the given box
293 void oglCentreTextNoClipping(wxDC
& dc
, wxList
*text_list
,
294 double m_xpos
, double m_ypos
, double width
, double height
)
296 int n
= text_list
->Number();
298 if (!text_list
|| (n
== 0))
301 // First, get maximum dimensions of box enclosing text
303 long char_height
= 0;
305 long current_width
= 0;
307 // Store text extents for speed
308 double *widths
= new double[n
];
310 wxNode
*current
= text_list
->First();
314 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
315 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
316 widths
[i
] = current_width
;
318 if (current_width
> max_width
)
319 max_width
= current_width
;
320 current
= current
->Next();
324 double max_height
= n
*char_height
;
326 double yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
328 double xoffset
= (double)(m_xpos
- width
/2.0);
330 current
= text_list
->First();
335 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
337 double x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
338 double y
= (double)(i
*char_height
+ yoffset
);
340 line
->SetX( x
- m_xpos
); line
->SetY( y
- m_ypos
);
341 current
= current
->Next();
347 void oglGetCentredTextExtent(wxDC
& dc
, wxList
*text_list
,
348 double m_xpos
, double m_ypos
, double width
, double height
,
349 double *actual_width
, double *actual_height
)
351 int n
= text_list
->Number();
353 if (!text_list
|| (n
== 0))
360 // First, get maximum dimensions of box enclosing text
362 long char_height
= 0;
364 long current_width
= 0;
366 wxNode
*current
= text_list
->First();
370 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
371 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
373 if (current_width
> max_width
)
374 max_width
= current_width
;
375 current
= current
->Next();
379 *actual_height
= n
*char_height
;
380 *actual_width
= max_width
;
383 // Format a string to a list of strings that fit in the given box.
384 // Interpret %n and 10 or 13 as a new line.
385 wxStringList
*oglFormatText(wxDC
& dc
, const wxString
& text
, double width
, double height
, int formatMode
)
387 // First, parse the string into a list of words
388 wxStringList word_list
;
390 // Make new lines into NULL strings at this point
391 int i
= 0; int j
= 0; int len
= strlen(text
);
392 char word
[200]; word
[0] = 0;
393 bool end_word
= FALSE
; bool new_line
= FALSE
;
402 { word
[j
] = '%'; j
++; }
406 { new_line
= TRUE
; end_word
= TRUE
; i
++; }
408 { word
[j
] = '%'; j
++; word
[j
] = text
[i
]; j
++; i
++; }
414 new_line
= TRUE
; end_word
= TRUE
; i
++;
419 new_line
= TRUE
; end_word
= TRUE
; i
++;
434 if (i
== len
) end_word
= TRUE
;
444 word_list
.Append(NULL
);
448 // Now, make a list of strings which can fit in the box
449 wxStringList
*string_list
= new wxStringList
;
453 wxNode
*node
= word_list
.First();
458 wxString
oldBuffer(buffer
);
460 char *s
= (char *)node
->Data();
464 if (strlen(buffer
) > 0)
465 string_list
->Add(buffer
);
475 dc
.GetTextExtent(buffer
, &x
, &y
);
477 // Don't fit within the bounding box if we're fitting shape to contents
478 if ((x
> width
) && !(formatMode
& FORMAT_SIZE_TO_CONTENTS
))
480 // Deal with first word being wider than box
481 if (oldBuffer
.Length() > 0)
482 string_list
->Add(oldBuffer
);
492 string_list
->Add(buffer
);
497 void oglDrawFormattedText(wxDC
& dc
, wxList
*text_list
,
498 double m_xpos
, double m_ypos
, double width
, double height
,
501 double xoffset
, yoffset
;
502 if (formatMode
& FORMAT_CENTRE_HORIZ
)
505 xoffset
= (double)(m_xpos
- (width
/ 2.0));
507 if (formatMode
& FORMAT_CENTRE_VERT
)
510 yoffset
= (double)(m_ypos
- (height
/ 2.0));
512 dc
.SetClippingRegion(
513 (long)(m_xpos
- width
/2.0), (long)(m_ypos
- height
/2.0),
514 (long)width
, (long)height
);
516 wxNode
*current
= text_list
->First();
519 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
521 dc
.DrawText(line
->GetText(), WXROUND(xoffset
+ line
->GetX()), WXROUND(yoffset
+ line
->GetY()));
522 current
= current
->Next();
525 dc
.DestroyClippingRegion();
529 * Find centroid given list of points comprising polyline
533 void oglFindPolylineCentroid(wxList
*points
, double *x
, double *y
)
538 wxNode
*node
= points
->First();
541 wxRealPoint
*point
= (wxRealPoint
*)node
->Data();
547 *x
= (xcount
/points
->Number());
548 *y
= (ycount
/points
->Number());
552 * Check that (x1, y1) -> (x2, y2) hits (x3, y3) -> (x4, y4).
553 * If so, ratio1 gives the proportion along the first line
554 * that the intersection occurs (or something like that).
555 * Used by functions below.
558 void oglCheckLineIntersection(double x1
, double y1
, double x2
, double y2
,
559 double x3
, double y3
, double x4
, double y4
,
560 double *ratio1
, double *ratio2
)
562 double denominator_term
= (y4
- y3
)*(x2
- x1
) - (y2
- y1
)*(x4
- x3
);
563 double numerator_term
= (x3
- x1
)*(y4
- y3
) + (x4
- x3
)*(y1
- y3
);
565 double line_constant
;
566 double length_ratio
= 1.0;
569 // Check for parallel lines
570 if ((denominator_term
< 0.005) && (denominator_term
> -0.005))
571 line_constant
= -1.0;
573 line_constant
= numerator_term
/denominator_term
;
575 // Check for intersection
576 if ((line_constant
< 1.0) && (line_constant
> 0.0))
578 // Now must check that other line hits
579 if (((y4
- y3
) < 0.005) && ((y4
- y3
) > -0.005))
580 k_line
= ((x1
- x3
) + line_constant
*(x2
- x1
))/(x4
- x3
);
582 k_line
= ((y1
- y3
) + line_constant
*(y2
- y1
))/(y4
- y3
);
584 if ((k_line
>= 0.0) && (k_line
< 1.0))
585 length_ratio
= line_constant
;
589 *ratio1
= length_ratio
;
594 * Find where (x1, y1) -> (x2, y2) hits one of the lines in xvec, yvec.
595 * (*x3, *y3) is the point where it hits.
598 void oglFindEndForPolyline(double n
, double xvec
[], double yvec
[],
599 double x1
, double y1
, double x2
, double y2
, double *x3
, double *y3
)
602 double lastx
= xvec
[0];
603 double lasty
= yvec
[0];
605 double min_ratio
= 1.0;
609 for (i
= 1; i
< n
; i
++)
611 oglCheckLineIntersection(x1
, y1
, x2
, y2
, lastx
, lasty
, xvec
[i
], yvec
[i
],
612 &line_ratio
, &other_ratio
);
616 if (line_ratio
< min_ratio
)
617 min_ratio
= line_ratio
;
620 // Do last (implicit) line if last and first doubles are not identical
621 if (!(xvec
[0] == lastx
&& yvec
[0] == lasty
))
623 oglCheckLineIntersection(x1
, y1
, x2
, y2
, lastx
, lasty
, xvec
[0], yvec
[0],
624 &line_ratio
, &other_ratio
);
626 if (line_ratio
< min_ratio
)
627 min_ratio
= line_ratio
;
630 *x3
= (x1
+ (x2
- x1
)*min_ratio
);
631 *y3
= (y1
+ (y2
- y1
)*min_ratio
);
636 * Find where the line hits the box.
640 void oglFindEndForBox(double width
, double height
,
641 double x1
, double y1
, // Centre of box (possibly)
642 double x2
, double y2
, // other end of line
643 double *x3
, double *y3
) // End on box edge
648 xvec
[0] = (double)(x1
- width
/2.0);
649 yvec
[0] = (double)(y1
- height
/2.0);
650 xvec
[1] = (double)(x1
- width
/2.0);
651 yvec
[1] = (double)(y1
+ height
/2.0);
652 xvec
[2] = (double)(x1
+ width
/2.0);
653 yvec
[2] = (double)(y1
+ height
/2.0);
654 xvec
[3] = (double)(x1
+ width
/2.0);
655 yvec
[3] = (double)(y1
- height
/2.0);
656 xvec
[4] = (double)(x1
- width
/2.0);
657 yvec
[4] = (double)(y1
- height
/2.0);
659 oglFindEndForPolyline(5, xvec
, yvec
, x2
, y2
, x1
, y1
, x3
, y3
);
663 * Find where the line hits the circle.
667 void oglFindEndForCircle(double radius
,
668 double x1
, double y1
, // Centre of circle
669 double x2
, double y2
, // Other end of line
670 double *x3
, double *y3
)
672 double H
= (double)sqrt((x2
- x1
)*(x2
- x1
) + (y2
- y1
)*(y2
- y1
));
681 *y3
= radius
* (y2
- y1
)/H
+ y1
;
682 *x3
= radius
* (x2
- x1
)/H
+ x1
;
687 * Given the line (x1, y1) -> (x2, y2), and an arrow size of given length and width,
688 * return the position of the tip of the arrow and the left and right vertices of the arrow.
692 void oglGetArrowPoints(double x1
, double y1
, double x2
, double y2
,
693 double length
, double width
,
694 double *tip_x
, double *tip_y
,
695 double *side1_x
, double *side1_y
,
696 double *side2_x
, double *side2_y
)
698 double l
= (double)sqrt((x2
- x1
)*(x2
- x1
) + (y2
- y1
)*(y2
- y1
));
703 double i_bar
= (x2
- x1
)/l
;
704 double j_bar
= (y2
- y1
)/l
;
706 double x3
= (- length
*i_bar
) + x2
;
707 double y3
= (- length
*j_bar
) + y2
;
709 *side1_x
= width
*(-j_bar
) + x3
;
710 *side1_y
= width
*i_bar
+ y3
;
712 *side2_x
= -width
*(-j_bar
) + x3
;
713 *side2_y
= -width
*i_bar
+ y3
;
715 *tip_x
= x2
; *tip_y
= y2
;
719 * Given an ellipse and endpoints of a line, returns the point at which
720 * the line touches the ellipse in values x4, y4.
721 * This function assumes that the centre of the ellipse is at x1, y1, and the
722 * ellipse has a width of width1 and a height of height1. It also assumes you are
723 * wanting to draw an arc FROM point x2, y2 TOWARDS point x3, y3.
724 * This function calculates the x,y coordinates of the intersection point of
725 * the arc with the ellipse.
726 * Author: Ian Harrison
729 void oglDrawArcToEllipse(double x1
, double y1
, double width1
, double height1
, double x2
, double y2
, double x3
, double y3
,
730 double *x4
, double *y4
)
732 double a1
= (double)(width1
/2.0);
733 double b1
= (double)(height1
/2.0);
735 // These are required to give top left x and y coordinates for DrawEllipse
736 // double top_left_x1 = (double)(x1 - a1);
737 // double top_left_y1 = (double)(y1 - b1);
739 // Check for vertical line
740 if (fabs(x2 - x3) < 0.05)
744 *y4 = (double)(y1 - b1);
746 *y4 = (double)(y1 + b1);
750 // Check that x2 != x3
751 if (fabs(x2
- x3
) < 0.05)
755 *y4
= (double)(y1
- sqrt((b1
*b1
- (((x2
-x1
)*(x2
-x1
))*(b1
*b1
)/(a1
*a1
)))));
757 *y4
= (double)(y1
+ sqrt((b1
*b1
- (((x2
-x1
)*(x2
-x1
))*(b1
*b1
)/(a1
*a1
)))));
761 // Calculate the x and y coordinates of the point where arc intersects ellipse
763 double A
, B
, C
, D
, E
, F
, G
, H
, K
;
764 double ellipse1_x
, ellipse1_y
;
766 A
= (double)(1/(a1
* a1
));
767 B
= (double)((y3
- y2
) * (y3
- y2
)) / ((x3
- x2
) * (x3
- x2
) * b1
* b1
);
768 C
= (double)(2 * (y3
- y2
) * (y2
- y1
)) / ((x3
- x2
) * b1
* b1
);
769 D
= (double)((y2
- y1
) * (y2
- y1
)) / (b1
* b1
);
771 F
= (double)(C
- (2 * A
* x1
) - (2 * B
* x2
));
772 G
= (double)((A
* x1
* x1
) + (B
* x2
* x2
) - (C
* x2
) + D
- 1);
773 H
= (double)((y3
- y2
) / (x3
- x2
));
774 K
= (double)((F
* F
) - (4 * E
* G
));
777 // In this case the line intersects the ellipse, so calculate intersection
781 ellipse1_x
= (double)(((F
* -1) + sqrt(K
)) / (2 * E
));
782 ellipse1_y
= (double)((H
* (ellipse1_x
- x2
)) + y2
);
786 ellipse1_x
= (double)(((F
* -1) - sqrt(K
)) / (2 * E
));
787 ellipse1_y
= (double)((H
* (ellipse1_x
- x2
)) + y2
);
791 // in this case, arc does not intersect ellipse, so just draw arc
800 // Draw a little circle (radius = 2) at the end of the arc where it hits
803 double circle_x = ellipse1_x - 2.0;
804 double circle_y = ellipse1_y - 2.0;
805 m_canvas->DrawEllipse(circle_x, circle_y, 4.0, 4.0);
809 // Update a list item from a list of strings
810 void UpdateListBox(wxListBox
*item
, wxList
*list
)
816 wxNode
*node
= list
->First();
819 char *s
= (char *)node
->Data();
825 bool oglRoughlyEqual(double val1
, double val2
, double tol
)
827 return ( (val1
< (val2
+ tol
)) && (val1
> (val2
- tol
)) &&
828 (val2
< (val1
+ tol
)) && (val2
> (val1
- tol
)));
832 * Hex<->Dec conversion
835 // Array used in DecToHex conversion routine.
836 static char sg_HexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
837 'C', 'D', 'E', 'F' };
839 // Convert 2-digit hex number to decimal
840 unsigned int oglHexToDec(char* buf
)
842 int firstDigit
, secondDigit
;
845 firstDigit
= buf
[0] - 'A' + 10;
847 firstDigit
= buf
[0] - '0';
850 secondDigit
= buf
[1] - 'A' + 10;
852 secondDigit
= buf
[1] - '0';
854 return firstDigit
* 16 + secondDigit
;
857 // Convert decimal integer to 2-character hex string
858 void oglDecToHex(unsigned int dec
, char *buf
)
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
];
867 // 3-digit hex to wxColour
868 wxColour
oglHexToColour(const wxString
& hex
)
870 if (hex
.Length() == 6)
873 strncpy(buf
, hex
, 7);
874 unsigned int r
= oglHexToDec((char *)buf
);
875 unsigned int g
= oglHexToDec((char *)(buf
+2));
876 unsigned int b
= oglHexToDec((char *)(buf
+4));
877 return wxColour(r
, g
, b
);
880 return wxColour(0,0,0);
883 // RGB to 3-digit hex
884 wxString
oglColourToHex(const wxColour
& colour
)
887 unsigned int red
= colour
.Red();
888 unsigned int green
= colour
.Green();
889 unsigned int blue
= colour
.Blue();
891 oglDecToHex(red
, buf
);
892 oglDecToHex(green
, buf
+2);
893 oglDecToHex(blue
, buf
+4);
895 return wxString(buf
);