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/ogl.h"
44 wxFont
* g_oglNormalFont
;
46 wxPen
* g_oglWhiteBackgroundPen
;
47 wxPen
* g_oglTransparentPen
;
48 wxBrush
* g_oglWhiteBackgroundBrush
;
49 wxPen
* g_oglBlackForegroundPen
;
50 wxCursor
* g_oglBullseyeCursor
= NULL
;
52 wxChar
* oglBuffer
= NULL
;
54 wxList
oglObjectCopyMapping(wxKEY_INTEGER
);
58 void wxOGLInitialize()
60 g_oglBullseyeCursor
= new wxCursor(wxCURSOR_BULLSEYE
);
62 g_oglNormalFont
= new wxFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
64 g_oglBlackPen
= new wxPen(wxT("BLACK"), 1, wxSOLID
);
66 g_oglWhiteBackgroundPen
= new wxPen(wxT("WHITE"), 1, wxSOLID
);
67 g_oglTransparentPen
= new wxPen(wxT("WHITE"), 1, wxTRANSPARENT
);
68 g_oglWhiteBackgroundBrush
= new wxBrush(wxT("WHITE"), wxSOLID
);
69 g_oglBlackForegroundPen
= new wxPen(wxT("BLACK"), 1, wxSOLID
);
71 OGLInitializeConstraintTypes();
73 // Initialize big buffer used when writing images
74 oglBuffer
= new wxChar
[3000];
87 if (g_oglBullseyeCursor
)
89 delete g_oglBullseyeCursor
;
90 g_oglBullseyeCursor
= NULL
;
95 delete g_oglNormalFont
;
96 g_oglNormalFont
= NULL
;
100 delete g_oglBlackPen
;
101 g_oglBlackPen
= NULL
;
103 if (g_oglWhiteBackgroundPen
)
105 delete g_oglWhiteBackgroundPen
;
106 g_oglWhiteBackgroundPen
= NULL
;
108 if (g_oglTransparentPen
)
110 delete g_oglTransparentPen
;
111 g_oglTransparentPen
= NULL
;
113 if (g_oglWhiteBackgroundBrush
)
115 delete g_oglWhiteBackgroundBrush
;
116 g_oglWhiteBackgroundBrush
= NULL
;
118 if (g_oglBlackForegroundPen
)
120 delete g_oglBlackForegroundPen
;
121 g_oglBlackForegroundPen
= NULL
;
124 OGLCleanUpConstraintTypes();
127 wxFont
*oglMatchFont(int point_size
)
129 wxFont
*font
= wxTheFontList
->FindOrCreateFont(point_size
, wxSWISS
, wxNORMAL
, wxNORMAL
);
143 font
= swiss_font_12
;
146 font
= swiss_font_14
;
149 font
= swiss_font_18
;
152 font
= swiss_font_24
;
156 font
= swiss_font_10
;
163 int FontSizeDialog(wxFrame
*parent
, int old_size
)
169 wxString ans
= wxGetTextFromUser(wxT("Enter point size"), wxT("Font size"), buf
, parent
);
170 if (ans
.Length() == 0)
174 ans
.ToLong(&new_size
);
175 if ((new_size
<= 0) || (new_size
> 40))
177 wxMessageBox(wxT("Invalid point size!"), wxT("Error"), wxOK
);
191 char *ans = wxGetSingleChoice("Choose", "Choose a font size", 8, strings, parent);
195 sscanf(ans, "%d", &size);
196 return oglMatchFont(size);
202 // Centre a list of strings in the given box. xOffset and yOffset are the
203 // the positions that these lines should be relative to, and this might be
204 // the same as m_xpos, m_ypos, but might be zero if formatting from left-justifying.
205 void oglCentreText(wxDC
& dc
, wxList
*text_list
,
206 double m_xpos
, double m_ypos
, double width
, double height
,
209 int n
= text_list
->GetCount();
211 if (!text_list
|| (n
== 0))
214 // First, get maximum dimensions of box enclosing text
216 long char_height
= 0;
218 long current_width
= 0;
220 // Store text extents for speed
221 double *widths
= new double[n
];
223 wxNode
*current
= text_list
->GetFirst();
227 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
228 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
229 widths
[i
] = current_width
;
231 if (current_width
> max_width
)
232 max_width
= current_width
;
233 current
= current
->GetNext();
237 double max_height
= n
*char_height
;
239 double xoffset
, yoffset
, xOffset
, yOffset
;
241 if (formatMode
& FORMAT_CENTRE_VERT
)
243 if (max_height
< height
)
244 yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
246 yoffset
= (double)(m_ypos
- (height
/2.0));
255 if (formatMode
& FORMAT_CENTRE_HORIZ
)
257 xoffset
= (double)(m_xpos
- width
/2.0);
266 current
= text_list
->GetFirst();
271 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
274 if ((formatMode
& FORMAT_CENTRE_HORIZ
) && (widths
[i
] < width
))
275 x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
278 double y
= (double)(i
*char_height
+ yoffset
);
280 line
->SetX( x
- xOffset
); line
->SetY( y
- yOffset
);
281 current
= current
->GetNext();
288 // Centre a list of strings in the given box
289 void oglCentreTextNoClipping(wxDC
& dc
, wxList
*text_list
,
290 double m_xpos
, double m_ypos
, double width
, double height
)
292 int n
= text_list
->GetCount();
294 if (!text_list
|| (n
== 0))
297 // First, get maximum dimensions of box enclosing text
299 long char_height
= 0;
301 long current_width
= 0;
303 // Store text extents for speed
304 double *widths
= new double[n
];
306 wxNode
*current
= text_list
->GetFirst();
310 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
311 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
312 widths
[i
] = current_width
;
314 if (current_width
> max_width
)
315 max_width
= current_width
;
316 current
= current
->GetNext();
320 double max_height
= n
*char_height
;
322 double yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
324 double xoffset
= (double)(m_xpos
- width
/2.0);
326 current
= text_list
->GetFirst();
331 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
333 double x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
334 double y
= (double)(i
*char_height
+ yoffset
);
336 line
->SetX( x
- m_xpos
); line
->SetY( y
- m_ypos
);
337 current
= current
->GetNext();
343 void oglGetCentredTextExtent(wxDC
& dc
, wxList
*text_list
,
344 double WXUNUSED(m_xpos
), double WXUNUSED(m_ypos
), double WXUNUSED(width
), double WXUNUSED(height
),
345 double *actual_width
, double *actual_height
)
347 int n
= text_list
->GetCount();
349 if (!text_list
|| (n
== 0))
356 // First, get maximum dimensions of box enclosing text
358 long char_height
= 0;
360 long current_width
= 0;
362 wxNode
*current
= text_list
->GetFirst();
366 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
367 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
369 if (current_width
> max_width
)
370 max_width
= current_width
;
371 current
= current
->GetNext();
375 *actual_height
= n
*char_height
;
376 *actual_width
= max_width
;
379 // Format a string to a list of strings that fit in the given box.
380 // Interpret %n and 10 or 13 as a new line.
381 wxStringList
*oglFormatText(wxDC
& dc
, const wxString
& text
, double width
, double WXUNUSED(height
), int formatMode
)
383 // First, parse the string into a list of words
384 wxStringList word_list
;
386 // Make new lines into NULL strings at this point
387 int i
= 0; int j
= 0; int len
= text
.Length();
388 wxChar word
[200]; word
[0] = 0;
389 bool end_word
= FALSE
; bool new_line
= FALSE
;
398 { word
[j
] = wxT('%'); j
++; }
401 if (text
[i
] == wxT('n'))
402 { new_line
= TRUE
; end_word
= TRUE
; i
++; }
404 { word
[j
] = wxT('%'); j
++; word
[j
] = text
[i
]; j
++; i
++; }
410 new_line
= TRUE
; end_word
= TRUE
; i
++;
415 new_line
= TRUE
; end_word
= TRUE
; i
++;
430 if (i
== len
) end_word
= TRUE
;
440 word_list
.Append(NULL
);
444 // Now, make a list of strings which can fit in the box
445 wxStringList
*string_list
= new wxStringList
;
448 wxStringListNode
*node
= word_list
.GetFirst();
453 wxString
oldBuffer(buffer
);
455 wxChar
*s
= (wxChar
*)node
->GetData();
459 if (buffer
.Length() > 0)
460 string_list
->Add(buffer
);
466 if (buffer
.Length() != 0)
470 dc
.GetTextExtent(buffer
, &x
, &y
);
472 // Don't fit within the bounding box if we're fitting shape to contents
473 if ((x
> width
) && !(formatMode
& FORMAT_SIZE_TO_CONTENTS
))
475 // Deal with first word being wider than box
476 if (oldBuffer
.Length() > 0)
477 string_list
->Add(oldBuffer
);
484 node
= node
->GetNext();
486 if (buffer
.Length() != 0)
487 string_list
->Add(buffer
);
492 void oglDrawFormattedText(wxDC
& dc
, wxList
*text_list
,
493 double m_xpos
, double m_ypos
, double width
, double height
,
496 double xoffset
, yoffset
;
497 if (formatMode
& FORMAT_CENTRE_HORIZ
)
500 xoffset
= (double)(m_xpos
- (width
/ 2.0));
502 if (formatMode
& FORMAT_CENTRE_VERT
)
505 yoffset
= (double)(m_ypos
- (height
/ 2.0));
507 dc
.SetClippingRegion(
508 (long)(m_xpos
- width
/2.0), (long)(m_ypos
- height
/2.0),
509 (long)width
, (long)height
);
511 wxNode
*current
= text_list
->GetFirst();
514 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
516 dc
.DrawText(line
->GetText(), WXROUND(xoffset
+ line
->GetX()), WXROUND(yoffset
+ line
->GetY()));
517 current
= current
->GetNext();
520 dc
.DestroyClippingRegion();
524 * Find centroid given list of points comprising polyline
528 void oglFindPolylineCentroid(wxList
*points
, double *x
, double *y
)
533 wxNode
*node
= points
->GetFirst();
536 wxRealPoint
*point
= (wxRealPoint
*)node
->GetData();
539 node
= node
->GetNext();
542 *x
= (xcount
/points
->GetCount());
543 *y
= (ycount
/points
->GetCount());
547 * Check that (x1, y1) -> (x2, y2) hits (x3, y3) -> (x4, y4).
548 * If so, ratio1 gives the proportion along the first line
549 * that the intersection occurs (or something like that).
550 * Used by functions below.
553 void oglCheckLineIntersection(double x1
, double y1
, double x2
, double y2
,
554 double x3
, double y3
, double x4
, double y4
,
555 double *ratio1
, double *ratio2
)
557 double denominator_term
= (y4
- y3
)*(x2
- x1
) - (y2
- y1
)*(x4
- x3
);
558 double numerator_term
= (x3
- x1
)*(y4
- y3
) + (x4
- x3
)*(y1
- y3
);
560 double line_constant
;
561 double length_ratio
= 1.0;
564 // Check for parallel lines
565 if ((denominator_term
< 0.005) && (denominator_term
> -0.005))
566 line_constant
= -1.0;
568 line_constant
= numerator_term
/denominator_term
;
570 // Check for intersection
571 if ((line_constant
< 1.0) && (line_constant
> 0.0))
573 // Now must check that other line hits
574 if (((y4
- y3
) < 0.005) && ((y4
- y3
) > -0.005))
575 k_line
= ((x1
- x3
) + line_constant
*(x2
- x1
))/(x4
- x3
);
577 k_line
= ((y1
- y3
) + line_constant
*(y2
- y1
))/(y4
- y3
);
579 if ((k_line
>= 0.0) && (k_line
< 1.0))
580 length_ratio
= line_constant
;
584 *ratio1
= length_ratio
;
589 * Find where (x1, y1) -> (x2, y2) hits one of the lines in xvec, yvec.
590 * (*x3, *y3) is the point where it hits.
593 void oglFindEndForPolyline(double n
, double xvec
[], double yvec
[],
594 double x1
, double y1
, double x2
, double y2
, double *x3
, double *y3
)
597 double lastx
= xvec
[0];
598 double lasty
= yvec
[0];
600 double min_ratio
= 1.0;
604 for (i
= 1; i
< n
; i
++)
606 oglCheckLineIntersection(x1
, y1
, x2
, y2
, lastx
, lasty
, xvec
[i
], yvec
[i
],
607 &line_ratio
, &other_ratio
);
611 if (line_ratio
< min_ratio
)
612 min_ratio
= line_ratio
;
615 // Do last (implicit) line if last and first doubles are not identical
616 if (!(xvec
[0] == lastx
&& yvec
[0] == lasty
))
618 oglCheckLineIntersection(x1
, y1
, x2
, y2
, lastx
, lasty
, xvec
[0], yvec
[0],
619 &line_ratio
, &other_ratio
);
621 if (line_ratio
< min_ratio
)
622 min_ratio
= line_ratio
;
625 *x3
= (x1
+ (x2
- x1
)*min_ratio
);
626 *y3
= (y1
+ (y2
- y1
)*min_ratio
);
631 * Find where the line hits the box.
635 void oglFindEndForBox(double width
, double height
,
636 double x1
, double y1
, // Centre of box (possibly)
637 double x2
, double y2
, // other end of line
638 double *x3
, double *y3
) // End on box edge
643 xvec
[0] = (double)(x1
- width
/2.0);
644 yvec
[0] = (double)(y1
- height
/2.0);
645 xvec
[1] = (double)(x1
- width
/2.0);
646 yvec
[1] = (double)(y1
+ height
/2.0);
647 xvec
[2] = (double)(x1
+ width
/2.0);
648 yvec
[2] = (double)(y1
+ height
/2.0);
649 xvec
[3] = (double)(x1
+ width
/2.0);
650 yvec
[3] = (double)(y1
- height
/2.0);
651 xvec
[4] = (double)(x1
- width
/2.0);
652 yvec
[4] = (double)(y1
- height
/2.0);
654 oglFindEndForPolyline(5, xvec
, yvec
, x2
, y2
, x1
, y1
, x3
, y3
);
658 * Find where the line hits the circle.
662 void oglFindEndForCircle(double radius
,
663 double x1
, double y1
, // Centre of circle
664 double x2
, double y2
, // Other end of line
665 double *x3
, double *y3
)
667 double H
= (double)sqrt((x2
- x1
)*(x2
- x1
) + (y2
- y1
)*(y2
- y1
));
676 *y3
= radius
* (y2
- y1
)/H
+ y1
;
677 *x3
= radius
* (x2
- x1
)/H
+ x1
;
682 * Given the line (x1, y1) -> (x2, y2), and an arrow size of given length and width,
683 * return the position of the tip of the arrow and the left and right vertices of the arrow.
687 void oglGetArrowPoints(double x1
, double y1
, double x2
, double y2
,
688 double length
, double width
,
689 double *tip_x
, double *tip_y
,
690 double *side1_x
, double *side1_y
,
691 double *side2_x
, double *side2_y
)
693 double l
= (double)sqrt((x2
- x1
)*(x2
- x1
) + (y2
- y1
)*(y2
- y1
));
698 double i_bar
= (x2
- x1
)/l
;
699 double j_bar
= (y2
- y1
)/l
;
701 double x3
= (- length
*i_bar
) + x2
;
702 double y3
= (- length
*j_bar
) + y2
;
704 *side1_x
= width
*(-j_bar
) + x3
;
705 *side1_y
= width
*i_bar
+ y3
;
707 *side2_x
= -width
*(-j_bar
) + x3
;
708 *side2_y
= -width
*i_bar
+ y3
;
710 *tip_x
= x2
; *tip_y
= y2
;
714 * Given an ellipse and endpoints of a line, returns the point at which
715 * the line touches the ellipse in values x4, y4.
716 * This function assumes that the centre of the ellipse is at x1, y1, and the
717 * ellipse has a width of width1 and a height of height1. It also assumes you are
718 * wanting to draw an arc FROM point x2, y2 TOWARDS point x3, y3.
719 * This function calculates the x,y coordinates of the intersection point of
720 * the arc with the ellipse.
721 * Author: Ian Harrison
724 void oglDrawArcToEllipse(double x1
, double y1
, double width1
, double height1
, double x2
, double y2
, double x3
, double y3
,
725 double *x4
, double *y4
)
727 double a1
= (double)(width1
/2.0);
728 double b1
= (double)(height1
/2.0);
730 // These are required to give top left x and y coordinates for DrawEllipse
731 // double top_left_x1 = (double)(x1 - a1);
732 // double top_left_y1 = (double)(y1 - b1);
734 // Check for vertical line
735 if (fabs(x2 - x3) < 0.05)
739 *y4 = (double)(y1 - b1);
741 *y4 = (double)(y1 + b1);
745 // Check that x2 != x3
746 if (fabs(x2
- x3
) < 0.05)
750 *y4
= (double)(y1
- sqrt((b1
*b1
- (((x2
-x1
)*(x2
-x1
))*(b1
*b1
)/(a1
*a1
)))));
752 *y4
= (double)(y1
+ sqrt((b1
*b1
- (((x2
-x1
)*(x2
-x1
))*(b1
*b1
)/(a1
*a1
)))));
756 // Calculate the x and y coordinates of the point where arc intersects ellipse
758 double A
, B
, C
, D
, E
, F
, G
, H
, K
;
759 double ellipse1_x
, ellipse1_y
;
761 A
= (double)(1/(a1
* a1
));
762 B
= (double)((y3
- y2
) * (y3
- y2
)) / ((x3
- x2
) * (x3
- x2
) * b1
* b1
);
763 C
= (double)(2 * (y3
- y2
) * (y2
- y1
)) / ((x3
- x2
) * b1
* b1
);
764 D
= (double)((y2
- y1
) * (y2
- y1
)) / (b1
* b1
);
766 F
= (double)(C
- (2 * A
* x1
) - (2 * B
* x2
));
767 G
= (double)((A
* x1
* x1
) + (B
* x2
* x2
) - (C
* x2
) + D
- 1);
768 H
= (double)((y3
- y2
) / (x3
- x2
));
769 K
= (double)((F
* F
) - (4 * E
* G
));
772 // In this case the line intersects the ellipse, so calculate intersection
776 ellipse1_x
= (double)(((F
* -1) + sqrt(K
)) / (2 * E
));
777 ellipse1_y
= (double)((H
* (ellipse1_x
- x2
)) + y2
);
781 ellipse1_x
= (double)(((F
* -1) - sqrt(K
)) / (2 * E
));
782 ellipse1_y
= (double)((H
* (ellipse1_x
- x2
)) + y2
);
786 // in this case, arc does not intersect ellipse, so just draw arc
795 // Draw a little circle (radius = 2) at the end of the arc where it hits
798 double circle_x = ellipse1_x - 2.0;
799 double circle_y = ellipse1_y - 2.0;
800 m_canvas->DrawEllipse(circle_x, circle_y, 4.0, 4.0);
804 // Update a list item from a list of strings
805 void UpdateListBox(wxListBox
*item
, wxList
*list
)
811 wxNode
*node
= list
->GetFirst();
814 wxChar
*s
= (wxChar
*)node
->GetData();
816 node
= node
->GetNext();
820 bool oglRoughlyEqual(double val1
, double val2
, double tol
)
822 return ( (val1
< (val2
+ tol
)) && (val1
> (val2
- tol
)) &&
823 (val2
< (val1
+ tol
)) && (val2
> (val1
- tol
)));
827 * Hex<->Dec conversion
830 // Array used in DecToHex conversion routine.
831 static wxChar sg_HexArray
[] = { wxT('0'), wxT('1'), wxT('2'), wxT('3'),
832 wxT('4'), wxT('5'), wxT('6'), wxT('7'),
833 wxT('8'), wxT('9'), wxT('A'), wxT('B'),
834 wxT('C'), wxT('D'), wxT('E'), wxT('F')
837 // Convert 2-digit hex number to decimal
838 unsigned int oglHexToDec(wxChar
* buf
)
840 int firstDigit
, secondDigit
;
842 if (buf
[0] >= wxT('A'))
843 firstDigit
= buf
[0] - wxT('A') + 10;
845 firstDigit
= buf
[0] - wxT('0');
847 if (buf
[1] >= wxT('A'))
848 secondDigit
= buf
[1] - wxT('A') + 10;
850 secondDigit
= buf
[1] - wxT('0');
852 return firstDigit
* 16 + secondDigit
;
855 // Convert decimal integer to 2-character hex string
856 void oglDecToHex(unsigned int dec
, wxChar
*buf
)
858 int firstDigit
= (int)(dec
/16.0);
859 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
860 buf
[0] = sg_HexArray
[firstDigit
];
861 buf
[1] = sg_HexArray
[secondDigit
];
865 // 3-digit hex to wxColour
866 wxColour
oglHexToColour(const wxString
& hex
)
868 if (hex
.Length() == 6)
872 hex
.Mid(0,2).ToLong(&r
, 16);
873 hex
.Mid(2,2).ToLong(&g
, 16);
874 hex
.Mid(4,2).ToLong(&b
, 16);
875 return wxColour(r
, g
, b
);
878 return wxColour(0,0,0);
881 // RGB to 3-digit hex
882 wxString
oglColourToHex(const wxColour
& colour
)
885 unsigned int red
= colour
.Red();
886 unsigned int green
= colour
.Green();
887 unsigned int blue
= colour
.Blue();
889 oglDecToHex(red
, buf
);
890 oglDecToHex(green
, buf
+2);
891 oglDecToHex(blue
, buf
+4);
893 return wxString(buf
);