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(wxT("BLACK"), 1, wxSOLID
);
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
);
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
)
171 wxString ans
= wxGetTextFromUser(wxT("Enter point size"), wxT("Font size"), buf
, parent
);
172 if (ans
.Length() == 0)
176 ans
.ToLong(&new_size
);
177 if ((new_size
<= 0) || (new_size
> 40))
179 wxMessageBox(wxT("Invalid point size!"), wxT("Error"), wxOK
);
193 char *ans = wxGetSingleChoice("Choose", "Choose a font size", 8, strings, parent);
197 sscanf(ans, "%d", &size);
198 return oglMatchFont(size);
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
,
211 int n
= text_list
->GetCount();
213 if (!text_list
|| (n
== 0))
216 // First, get maximum dimensions of box enclosing text
218 long char_height
= 0;
220 long current_width
= 0;
222 // Store text extents for speed
223 double *widths
= new double[n
];
225 wxNode
*current
= text_list
->GetFirst();
229 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
230 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
231 widths
[i
] = current_width
;
233 if (current_width
> max_width
)
234 max_width
= current_width
;
235 current
= current
->GetNext();
239 double max_height
= n
*char_height
;
241 double xoffset
, yoffset
, xOffset
, yOffset
;
243 if (formatMode
& FORMAT_CENTRE_VERT
)
245 if (max_height
< height
)
246 yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
248 yoffset
= (double)(m_ypos
- (height
/2.0));
257 if (formatMode
& FORMAT_CENTRE_HORIZ
)
259 xoffset
= (double)(m_xpos
- width
/2.0);
268 current
= text_list
->GetFirst();
273 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
276 if ((formatMode
& FORMAT_CENTRE_HORIZ
) && (widths
[i
] < width
))
277 x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
280 double y
= (double)(i
*char_height
+ yoffset
);
282 line
->SetX( x
- xOffset
); line
->SetY( y
- yOffset
);
283 current
= current
->GetNext();
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
)
294 int n
= text_list
->GetCount();
296 if (!text_list
|| (n
== 0))
299 // First, get maximum dimensions of box enclosing text
301 long char_height
= 0;
303 long current_width
= 0;
305 // Store text extents for speed
306 double *widths
= new double[n
];
308 wxNode
*current
= text_list
->GetFirst();
312 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
313 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
314 widths
[i
] = current_width
;
316 if (current_width
> max_width
)
317 max_width
= current_width
;
318 current
= current
->GetNext();
322 double max_height
= n
*char_height
;
324 double yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
326 double xoffset
= (double)(m_xpos
- width
/2.0);
328 current
= text_list
->GetFirst();
333 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
335 double x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
336 double y
= (double)(i
*char_height
+ yoffset
);
338 line
->SetX( x
- m_xpos
); line
->SetY( y
- m_ypos
);
339 current
= current
->GetNext();
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
)
349 int n
= text_list
->GetCount();
351 if (!text_list
|| (n
== 0))
358 // First, get maximum dimensions of box enclosing text
360 long char_height
= 0;
362 long current_width
= 0;
364 wxNode
*current
= text_list
->GetFirst();
368 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
369 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
371 if (current_width
> max_width
)
372 max_width
= current_width
;
373 current
= current
->GetNext();
377 *actual_height
= n
*char_height
;
378 *actual_width
= max_width
;
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
)
385 // First, parse the string into a list of words
386 wxStringList word_list
;
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
;
400 { word
[j
] = wxT('%'); j
++; }
403 if (text
[i
] == wxT('n'))
404 { new_line
= TRUE
; end_word
= TRUE
; i
++; }
406 { word
[j
] = wxT('%'); j
++; word
[j
] = text
[i
]; j
++; i
++; }
412 new_line
= TRUE
; end_word
= TRUE
; i
++;
417 new_line
= TRUE
; end_word
= TRUE
; i
++;
432 if (i
== len
) end_word
= TRUE
;
442 word_list
.Append(NULL
);
446 // Now, make a list of strings which can fit in the box
447 wxStringList
*string_list
= new wxStringList
;
450 wxStringListNode
*node
= word_list
.GetFirst();
455 wxString
oldBuffer(buffer
);
457 wxChar
*s
= (wxChar
*)node
->GetData();
461 if (buffer
.Length() > 0)
462 string_list
->Add(buffer
);
468 if (buffer
.Length() != 0)
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();
488 if (buffer
.Length() != 0)
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 wxChar
*s
= (wxChar
*)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 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')
839 // Convert 2-digit hex number to decimal
840 unsigned int oglHexToDec(wxChar
* buf
)
842 int firstDigit
, secondDigit
;
844 if (buf
[0] >= wxT('A'))
845 firstDigit
= buf
[0] - wxT('A') + 10;
847 firstDigit
= buf
[0] - wxT('0');
849 if (buf
[1] >= wxT('A'))
850 secondDigit
= buf
[1] - wxT('A') + 10;
852 secondDigit
= buf
[1] - wxT('0');
854 return firstDigit
* 16 + secondDigit
;
857 // Convert decimal integer to 2-character hex string
858 void oglDecToHex(unsigned int dec
, wxChar
*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)
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
);
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
);