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"
28 #include <wx/deprecated/wxexpr.h>
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>
47 wxFont
* g_oglNormalFont
;
49 wxPen
* g_oglWhiteBackgroundPen
;
50 wxPen
* g_oglTransparentPen
;
51 wxBrush
* g_oglWhiteBackgroundBrush
;
52 wxPen
* g_oglBlackForegroundPen
;
53 wxCursor
* g_oglBullseyeCursor
= NULL
;
55 char* oglBuffer
= NULL
;
57 wxList
oglObjectCopyMapping(wxKEY_INTEGER
);
61 void wxOGLInitialize()
63 g_oglBullseyeCursor
= new wxCursor(wxCURSOR_BULLSEYE
);
65 g_oglNormalFont
= new wxFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
67 g_oglBlackPen
= new wxPen("BLACK", 1, wxSOLID
);
69 g_oglWhiteBackgroundPen
= new wxPen("WHITE", 1, wxSOLID
);
70 g_oglTransparentPen
= new wxPen("WHITE", 1, wxTRANSPARENT
);
71 g_oglWhiteBackgroundBrush
= new wxBrush("WHITE", wxSOLID
);
72 g_oglBlackForegroundPen
= new wxPen("BLACK", 1, wxSOLID
);
74 OGLInitializeConstraintTypes();
76 // Initialize big buffer used when writing images
77 oglBuffer
= new char[3000];
89 if (g_oglBullseyeCursor
)
91 delete g_oglBullseyeCursor
;
92 g_oglBullseyeCursor
= NULL
;
97 delete g_oglNormalFont
;
98 g_oglNormalFont
= NULL
;
102 delete g_oglBlackPen
;
103 g_oglBlackPen
= NULL
;
105 if (g_oglWhiteBackgroundPen
)
107 delete g_oglWhiteBackgroundPen
;
108 g_oglWhiteBackgroundPen
= NULL
;
110 if (g_oglTransparentPen
)
112 delete g_oglTransparentPen
;
113 g_oglTransparentPen
= NULL
;
115 if (g_oglWhiteBackgroundBrush
)
117 delete g_oglWhiteBackgroundBrush
;
118 g_oglWhiteBackgroundBrush
= NULL
;
120 if (g_oglBlackForegroundPen
)
122 delete g_oglBlackForegroundPen
;
123 g_oglBlackForegroundPen
= NULL
;
126 OGLCleanUpConstraintTypes();
129 wxFont
*oglMatchFont(int point_size
)
131 wxFont
*font
= wxTheFontList
->FindOrCreateFont(point_size
, wxSWISS
, wxNORMAL
, wxNORMAL
);
145 font
= swiss_font_12
;
148 font
= swiss_font_14
;
151 font
= swiss_font_18
;
154 font
= swiss_font_24
;
158 font
= swiss_font_10
;
165 int FontSizeDialog(wxFrame
*parent
, int old_size
)
170 sprintf(buf
, "%d", old_size
);
171 wxString ans
= wxGetTextFromUser("Enter point size", "Font size", buf
, parent
);
175 int new_size
= atoi(ans
);
176 if ((new_size
<= 0) || (new_size
> 40))
178 wxMessageBox("Invalid point size!", "Error", wxOK
);
192 char *ans = wxGetSingleChoice("Choose", "Choose a font size", 8, strings, parent);
196 sscanf(ans, "%d", &size);
197 return oglMatchFont(size);
203 // Centre a list of strings in the given box. xOffset and yOffset are the
204 // the positions that these lines should be relative to, and this might be
205 // the same as m_xpos, m_ypos, but might be zero if formatting from left-justifying.
206 void oglCentreText(wxDC
& dc
, wxList
*text_list
,
207 double m_xpos
, double m_ypos
, double width
, double height
,
210 int n
= text_list
->GetCount();
212 if (!text_list
|| (n
== 0))
215 // First, get maximum dimensions of box enclosing text
217 long char_height
= 0;
219 long current_width
= 0;
221 // Store text extents for speed
222 double *widths
= new double[n
];
224 wxNode
*current
= text_list
->GetFirst();
228 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
229 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
230 widths
[i
] = current_width
;
232 if (current_width
> max_width
)
233 max_width
= current_width
;
234 current
= current
->GetNext();
238 double max_height
= n
*char_height
;
240 double xoffset
, yoffset
, xOffset
, yOffset
;
242 if (formatMode
& FORMAT_CENTRE_VERT
)
244 if (max_height
< height
)
245 yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
247 yoffset
= (double)(m_ypos
- (height
/2.0));
256 if (formatMode
& FORMAT_CENTRE_HORIZ
)
258 xoffset
= (double)(m_xpos
- width
/2.0);
267 current
= text_list
->GetFirst();
272 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
275 if ((formatMode
& FORMAT_CENTRE_HORIZ
) && (widths
[i
] < width
))
276 x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
279 double y
= (double)(i
*char_height
+ yoffset
);
281 line
->SetX( x
- xOffset
); line
->SetY( y
- yOffset
);
282 current
= current
->GetNext();
289 // Centre a list of strings in the given box
290 void oglCentreTextNoClipping(wxDC
& dc
, wxList
*text_list
,
291 double m_xpos
, double m_ypos
, double width
, double height
)
293 int n
= text_list
->GetCount();
295 if (!text_list
|| (n
== 0))
298 // First, get maximum dimensions of box enclosing text
300 long char_height
= 0;
302 long current_width
= 0;
304 // Store text extents for speed
305 double *widths
= new double[n
];
307 wxNode
*current
= text_list
->GetFirst();
311 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
312 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
313 widths
[i
] = current_width
;
315 if (current_width
> max_width
)
316 max_width
= current_width
;
317 current
= current
->GetNext();
321 double max_height
= n
*char_height
;
323 double yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
325 double xoffset
= (double)(m_xpos
- width
/2.0);
327 current
= text_list
->GetFirst();
332 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
334 double x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
335 double y
= (double)(i
*char_height
+ yoffset
);
337 line
->SetX( x
- m_xpos
); line
->SetY( y
- m_ypos
);
338 current
= current
->GetNext();
344 void oglGetCentredTextExtent(wxDC
& dc
, wxList
*text_list
,
345 double m_xpos
, double m_ypos
, double width
, double height
,
346 double *actual_width
, double *actual_height
)
348 int n
= text_list
->GetCount();
350 if (!text_list
|| (n
== 0))
357 // First, get maximum dimensions of box enclosing text
359 long char_height
= 0;
361 long current_width
= 0;
363 wxNode
*current
= text_list
->GetFirst();
367 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
368 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
370 if (current_width
> max_width
)
371 max_width
= current_width
;
372 current
= current
->GetNext();
376 *actual_height
= n
*char_height
;
377 *actual_width
= max_width
;
380 // Format a string to a list of strings that fit in the given box.
381 // Interpret %n and 10 or 13 as a new line.
382 wxStringList
*oglFormatText(wxDC
& dc
, const wxString
& text
, double width
, double height
, int formatMode
)
384 // First, parse the string into a list of words
385 wxStringList word_list
;
387 // Make new lines into NULL strings at this point
388 int i
= 0; int j
= 0; int len
= strlen(text
);
389 char word
[200]; word
[0] = 0;
390 bool end_word
= FALSE
; bool new_line
= FALSE
;
399 { word
[j
] = '%'; j
++; }
403 { new_line
= TRUE
; end_word
= TRUE
; i
++; }
405 { word
[j
] = '%'; j
++; word
[j
] = text
[i
]; j
++; i
++; }
411 new_line
= TRUE
; end_word
= TRUE
; i
++;
416 new_line
= TRUE
; end_word
= TRUE
; i
++;
431 if (i
== len
) end_word
= TRUE
;
441 word_list
.Append(NULL
);
445 // Now, make a list of strings which can fit in the box
446 wxStringList
*string_list
= new wxStringList
;
450 wxStringListNode
*node
= word_list
.GetFirst();
455 wxString
oldBuffer(buffer
);
457 char *s
= (char *)node
->GetData();
461 if (strlen(buffer
) > 0)
462 string_list
->Add(buffer
);
472 dc
.GetTextExtent(buffer
, &x
, &y
);
474 // Don't fit within the bounding box if we're fitting shape to contents
475 if ((x
> width
) && !(formatMode
& FORMAT_SIZE_TO_CONTENTS
))
477 // Deal with first word being wider than box
478 if (oldBuffer
.Length() > 0)
479 string_list
->Add(oldBuffer
);
486 node
= node
->GetNext();
489 string_list
->Add(buffer
);
494 void oglDrawFormattedText(wxDC
& dc
, wxList
*text_list
,
495 double m_xpos
, double m_ypos
, double width
, double height
,
498 double xoffset
, yoffset
;
499 if (formatMode
& FORMAT_CENTRE_HORIZ
)
502 xoffset
= (double)(m_xpos
- (width
/ 2.0));
504 if (formatMode
& FORMAT_CENTRE_VERT
)
507 yoffset
= (double)(m_ypos
- (height
/ 2.0));
509 dc
.SetClippingRegion(
510 (long)(m_xpos
- width
/2.0), (long)(m_ypos
- height
/2.0),
511 (long)width
, (long)height
);
513 wxNode
*current
= text_list
->GetFirst();
516 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
518 dc
.DrawText(line
->GetText(), WXROUND(xoffset
+ line
->GetX()), WXROUND(yoffset
+ line
->GetY()));
519 current
= current
->GetNext();
522 dc
.DestroyClippingRegion();
526 * Find centroid given list of points comprising polyline
530 void oglFindPolylineCentroid(wxList
*points
, double *x
, double *y
)
535 wxNode
*node
= points
->GetFirst();
538 wxRealPoint
*point
= (wxRealPoint
*)node
->GetData();
541 node
= node
->GetNext();
544 *x
= (xcount
/points
->GetCount());
545 *y
= (ycount
/points
->GetCount());
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.
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
)
559 double denominator_term
= (y4
- y3
)*(x2
- x1
) - (y2
- y1
)*(x4
- x3
);
560 double numerator_term
= (x3
- x1
)*(y4
- y3
) + (x4
- x3
)*(y1
- y3
);
562 double line_constant
;
563 double length_ratio
= 1.0;
566 // Check for parallel lines
567 if ((denominator_term
< 0.005) && (denominator_term
> -0.005))
568 line_constant
= -1.0;
570 line_constant
= numerator_term
/denominator_term
;
572 // Check for intersection
573 if ((line_constant
< 1.0) && (line_constant
> 0.0))
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
);
579 k_line
= ((y1
- y3
) + line_constant
*(y2
- y1
))/(y4
- y3
);
581 if ((k_line
>= 0.0) && (k_line
< 1.0))
582 length_ratio
= line_constant
;
586 *ratio1
= length_ratio
;
591 * Find where (x1, y1) -> (x2, y2) hits one of the lines in xvec, yvec.
592 * (*x3, *y3) is the point where it hits.
595 void oglFindEndForPolyline(double n
, double xvec
[], double yvec
[],
596 double x1
, double y1
, double x2
, double y2
, double *x3
, double *y3
)
599 double lastx
= xvec
[0];
600 double lasty
= yvec
[0];
602 double min_ratio
= 1.0;
606 for (i
= 1; i
< n
; i
++)
608 oglCheckLineIntersection(x1
, y1
, x2
, y2
, lastx
, lasty
, xvec
[i
], yvec
[i
],
609 &line_ratio
, &other_ratio
);
613 if (line_ratio
< min_ratio
)
614 min_ratio
= line_ratio
;
617 // Do last (implicit) line if last and first doubles are not identical
618 if (!(xvec
[0] == lastx
&& yvec
[0] == lasty
))
620 oglCheckLineIntersection(x1
, y1
, x2
, y2
, lastx
, lasty
, xvec
[0], yvec
[0],
621 &line_ratio
, &other_ratio
);
623 if (line_ratio
< min_ratio
)
624 min_ratio
= line_ratio
;
627 *x3
= (x1
+ (x2
- x1
)*min_ratio
);
628 *y3
= (y1
+ (y2
- y1
)*min_ratio
);
633 * Find where the line hits the box.
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
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);
656 oglFindEndForPolyline(5, xvec
, yvec
, x2
, y2
, x1
, y1
, x3
, y3
);
660 * Find where the line hits the circle.
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
)
669 double H
= (double)sqrt((x2
- x1
)*(x2
- x1
) + (y2
- y1
)*(y2
- y1
));
678 *y3
= radius
* (y2
- y1
)/H
+ y1
;
679 *x3
= radius
* (x2
- x1
)/H
+ x1
;
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.
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
)
695 double l
= (double)sqrt((x2
- x1
)*(x2
- x1
) + (y2
- y1
)*(y2
- y1
));
700 double i_bar
= (x2
- x1
)/l
;
701 double j_bar
= (y2
- y1
)/l
;
703 double x3
= (- length
*i_bar
) + x2
;
704 double y3
= (- length
*j_bar
) + y2
;
706 *side1_x
= width
*(-j_bar
) + x3
;
707 *side1_y
= width
*i_bar
+ y3
;
709 *side2_x
= -width
*(-j_bar
) + x3
;
710 *side2_y
= -width
*i_bar
+ y3
;
712 *tip_x
= x2
; *tip_y
= y2
;
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
726 void oglDrawArcToEllipse(double x1
, double y1
, double width1
, double height1
, double x2
, double y2
, double x3
, double y3
,
727 double *x4
, double *y4
)
729 double a1
= (double)(width1
/2.0);
730 double b1
= (double)(height1
/2.0);
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);
736 // Check for vertical line
737 if (fabs(x2 - x3) < 0.05)
741 *y4 = (double)(y1 - b1);
743 *y4 = (double)(y1 + b1);
747 // Check that x2 != x3
748 if (fabs(x2
- x3
) < 0.05)
752 *y4
= (double)(y1
- sqrt((b1
*b1
- (((x2
-x1
)*(x2
-x1
))*(b1
*b1
)/(a1
*a1
)))));
754 *y4
= (double)(y1
+ sqrt((b1
*b1
- (((x2
-x1
)*(x2
-x1
))*(b1
*b1
)/(a1
*a1
)))));
758 // Calculate the x and y coordinates of the point where arc intersects ellipse
760 double A
, B
, C
, D
, E
, F
, G
, H
, K
;
761 double ellipse1_x
, ellipse1_y
;
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
);
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
));
774 // In this case the line intersects the ellipse, so calculate intersection
778 ellipse1_x
= (double)(((F
* -1) + sqrt(K
)) / (2 * E
));
779 ellipse1_y
= (double)((H
* (ellipse1_x
- x2
)) + y2
);
783 ellipse1_x
= (double)(((F
* -1) - sqrt(K
)) / (2 * E
));
784 ellipse1_y
= (double)((H
* (ellipse1_x
- x2
)) + y2
);
788 // in this case, arc does not intersect ellipse, so just draw arc
797 // Draw a little circle (radius = 2) at the end of the arc where it hits
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);
806 // Update a list item from a list of strings
807 void UpdateListBox(wxListBox
*item
, wxList
*list
)
813 wxNode
*node
= list
->GetFirst();
816 char *s
= (char *)node
->GetData();
818 node
= node
->GetNext();
822 bool oglRoughlyEqual(double val1
, double val2
, double tol
)
824 return ( (val1
< (val2
+ tol
)) && (val1
> (val2
- tol
)) &&
825 (val2
< (val1
+ tol
)) && (val2
> (val1
- tol
)));
829 * Hex<->Dec conversion
832 // Array used in DecToHex conversion routine.
833 static char sg_HexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
834 'C', 'D', 'E', 'F' };
836 // Convert 2-digit hex number to decimal
837 unsigned int oglHexToDec(char* buf
)
839 int firstDigit
, secondDigit
;
842 firstDigit
= buf
[0] - 'A' + 10;
844 firstDigit
= buf
[0] - '0';
847 secondDigit
= buf
[1] - 'A' + 10;
849 secondDigit
= buf
[1] - '0';
851 return firstDigit
* 16 + secondDigit
;
854 // Convert decimal integer to 2-character hex string
855 void oglDecToHex(unsigned int dec
, char *buf
)
857 int firstDigit
= (int)(dec
/16.0);
858 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
859 buf
[0] = sg_HexArray
[firstDigit
];
860 buf
[1] = sg_HexArray
[secondDigit
];
864 // 3-digit hex to wxColour
865 wxColour
oglHexToColour(const wxString
& hex
)
867 if (hex
.Length() == 6)
870 strncpy(buf
, hex
, 7);
871 unsigned int r
= oglHexToDec((char *)buf
);
872 unsigned int g
= oglHexToDec((char *)(buf
+2));
873 unsigned int b
= oglHexToDec((char *)(buf
+4));
874 return wxColour(r
, g
, b
);
877 return wxColour(0,0,0);
880 // RGB to 3-digit hex
881 wxString
oglColourToHex(const wxColour
& colour
)
884 unsigned int red
= colour
.Red();
885 unsigned int green
= colour
.Green();
886 unsigned int blue
= colour
.Blue();
888 oglDecToHex(red
, buf
);
889 oglDecToHex(green
, buf
+2);
890 oglDecToHex(blue
, buf
+4);
892 return wxString(buf
);