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/deprecated/wxexpr.h>
39 #include <wx/ogl/basic.h>
40 #include <wx/ogl/basicp.h>
41 #include <wx/ogl/misc.h>
42 #include <wx/ogl/constrnt.h>
43 #include <wx/ogl/composit.h>
45 wxFont
* g_oglNormalFont
;
47 wxPen
* g_oglTransparentPen
;
48 wxPen
* g_oglBlackForegroundPen
;
49 wxPen
* g_oglWhiteBackgroundPen
;
50 wxBrush
* g_oglWhiteBackgroundBrush
;
52 char* oglBuffer
= NULL
;
54 wxList
oglObjectCopyMapping(wxKEY_INTEGER
);
58 void wxOGLInitialize()
60 g_oglNormalFont
= wxTheFontList
->FindOrCreateFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
61 g_oglBlackPen
= wxThePenList
->FindOrCreatePen(wxT("BLACK"), 1, wxSOLID
);
62 g_oglTransparentPen
= wxThePenList
->FindOrCreatePen(wxT("WHITE"), 1, wxTRANSPARENT
);
63 g_oglBlackForegroundPen
= wxThePenList
->FindOrCreatePen(wxT("BLACK"), 1, wxSOLID
);
64 g_oglWhiteBackgroundPen
= wxThePenList
->FindOrCreatePen(wxT("WHITE"), 1, wxSOLID
);
65 g_oglWhiteBackgroundBrush
= wxTheBrushList
->FindOrCreateBrush(wxT("WHITE"), wxSOLID
);
67 OGLInitializeConstraintTypes();
69 // Initialize big buffer used when writing images
70 oglBuffer
= new char[3000];
83 g_oglNormalFont
= NULL
; // These will be cleaned up by their GDI list
85 g_oglTransparentPen
= NULL
;
86 g_oglBlackForegroundPen
= NULL
;
87 g_oglWhiteBackgroundPen
= NULL
;
88 g_oglWhiteBackgroundBrush
= NULL
;
90 OGLCleanUpConstraintTypes();
93 wxFont
*oglMatchFont(int point_size
)
95 wxFont
*font
= wxTheFontList
->FindOrCreateFont(point_size
, wxSWISS
, wxNORMAL
, wxNORMAL
);
109 font
= swiss_font_12
;
112 font
= swiss_font_14
;
115 font
= swiss_font_18
;
118 font
= swiss_font_24
;
122 font
= swiss_font_10
;
129 int FontSizeDialog(wxFrame
*parent
, int old_size
)
135 wxString ans
= wxGetTextFromUser(wxT("Enter point size"), wxT("Font size"), buf
, parent
);
136 if (ans
.Length() == 0)
140 ans
.ToLong(&new_size
);
141 if ((new_size
<= 0) || (new_size
> 40))
143 wxMessageBox(wxT("Invalid point size!"), wxT("Error"), wxOK
);
157 char *ans = wxGetSingleChoice("Choose", "Choose a font size", 8, strings, parent);
161 sscanf(ans, "%d", &size);
162 return oglMatchFont(size);
168 // Centre a list of strings in the given box. xOffset and yOffset are the
169 // the positions that these lines should be relative to, and this might be
170 // the same as m_xpos, m_ypos, but might be zero if formatting from left-justifying.
171 void oglCentreText(wxDC
& dc
, wxList
*text_list
,
172 double m_xpos
, double m_ypos
, double width
, double height
,
175 int n
= text_list
->GetCount();
177 if (!text_list
|| (n
== 0))
180 // First, get maximum dimensions of box enclosing text
182 long char_height
= 0;
184 long current_width
= 0;
186 // Store text extents for speed
187 double *widths
= new double[n
];
189 wxNode
*current
= text_list
->GetFirst();
193 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
194 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
195 widths
[i
] = current_width
;
197 if (current_width
> max_width
)
198 max_width
= current_width
;
199 current
= current
->GetNext();
203 double max_height
= n
*char_height
;
205 double xoffset
, yoffset
, xOffset
, yOffset
;
207 if (formatMode
& FORMAT_CENTRE_VERT
)
209 if (max_height
< height
)
210 yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
212 yoffset
= (double)(m_ypos
- (height
/2.0));
221 if (formatMode
& FORMAT_CENTRE_HORIZ
)
223 xoffset
= (double)(m_xpos
- width
/2.0);
232 current
= text_list
->GetFirst();
237 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
240 if ((formatMode
& FORMAT_CENTRE_HORIZ
) && (widths
[i
] < width
))
241 x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
244 double y
= (double)(i
*char_height
+ yoffset
);
246 line
->SetX( x
- xOffset
); line
->SetY( y
- yOffset
);
247 current
= current
->GetNext();
254 // Centre a list of strings in the given box
255 void oglCentreTextNoClipping(wxDC
& dc
, wxList
*text_list
,
256 double m_xpos
, double m_ypos
, double width
, double height
)
258 int n
= text_list
->GetCount();
260 if (!text_list
|| (n
== 0))
263 // First, get maximum dimensions of box enclosing text
265 long char_height
= 0;
267 long current_width
= 0;
269 // Store text extents for speed
270 double *widths
= new double[n
];
272 wxNode
*current
= text_list
->GetFirst();
276 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
277 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
278 widths
[i
] = current_width
;
280 if (current_width
> max_width
)
281 max_width
= current_width
;
282 current
= current
->GetNext();
286 double max_height
= n
*char_height
;
288 double yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
290 double xoffset
= (double)(m_xpos
- width
/2.0);
292 current
= text_list
->GetFirst();
297 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
299 double x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
300 double y
= (double)(i
*char_height
+ yoffset
);
302 line
->SetX( x
- m_xpos
); line
->SetY( y
- m_ypos
);
303 current
= current
->GetNext();
309 void oglGetCentredTextExtent(wxDC
& dc
, wxList
*text_list
,
310 double m_xpos
, double m_ypos
, double width
, double height
,
311 double *actual_width
, double *actual_height
)
313 int n
= text_list
->GetCount();
315 if (!text_list
|| (n
== 0))
322 // First, get maximum dimensions of box enclosing text
324 long char_height
= 0;
326 long current_width
= 0;
328 wxNode
*current
= text_list
->GetFirst();
332 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
333 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
335 if (current_width
> max_width
)
336 max_width
= current_width
;
337 current
= current
->GetNext();
341 *actual_height
= n
*char_height
;
342 *actual_width
= max_width
;
345 // Format a string to a list of strings that fit in the given box.
346 // Interpret %n and 10 or 13 as a new line.
347 wxStringList
*oglFormatText(wxDC
& dc
, const wxString
& text
, double width
, double height
, int formatMode
)
349 // First, parse the string into a list of words
350 wxStringList word_list
;
352 // Make new lines into NULL strings at this point
353 int i
= 0; int j
= 0; int len
= text
.Length();
354 wxChar word
[200]; word
[0] = 0;
355 bool end_word
= FALSE
; bool new_line
= FALSE
;
364 { word
[j
] = wxT('%'); j
++; }
367 if (text
[i
] == wxT('n'))
368 { new_line
= TRUE
; end_word
= TRUE
; i
++; }
370 { word
[j
] = wxT('%'); j
++; word
[j
] = text
[i
]; j
++; i
++; }
376 new_line
= TRUE
; end_word
= TRUE
; i
++;
381 new_line
= TRUE
; end_word
= TRUE
; i
++;
396 if (i
== len
) end_word
= TRUE
;
406 word_list
.Append(NULL
);
410 // Now, make a list of strings which can fit in the box
411 wxStringList
*string_list
= new wxStringList
;
414 wxNode
*node
= (wxNode
*)word_list
.GetFirst();
419 wxString
oldBuffer(buffer
);
421 wxChar
*s
= (wxChar
*)node
->GetData();
425 if (buffer
.Length() > 0)
426 string_list
->Add(buffer
);
432 if (buffer
.Length() != 0)
436 dc
.GetTextExtent(buffer
, &x
, &y
);
438 // Don't fit within the bounding box if we're fitting shape to contents
439 if ((x
> width
) && !(formatMode
& FORMAT_SIZE_TO_CONTENTS
))
441 // Deal with first word being wider than box
442 if (oldBuffer
.Length() > 0)
443 string_list
->Add(oldBuffer
);
450 node
= node
->GetNext();
452 if (buffer
.Length() != 0)
453 string_list
->Add(buffer
);
458 void oglDrawFormattedText(wxDC
& dc
, wxList
*text_list
,
459 double m_xpos
, double m_ypos
, double width
, double height
,
462 double xoffset
, yoffset
;
463 if (formatMode
& FORMAT_CENTRE_HORIZ
)
466 xoffset
= (double)(m_xpos
- (width
/ 2.0));
468 if (formatMode
& FORMAT_CENTRE_VERT
)
471 yoffset
= (double)(m_ypos
- (height
/ 2.0));
473 dc
.SetClippingRegion(
474 (long)(m_xpos
- width
/2.0), (long)(m_ypos
- height
/2.0),
475 (long)width
, (long)height
);
477 wxNode
*current
= text_list
->GetFirst();
480 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->GetData();
482 dc
.DrawText(line
->GetText(), WXROUND(xoffset
+ line
->GetX()), WXROUND(yoffset
+ line
->GetY()));
483 current
= current
->GetNext();
486 dc
.DestroyClippingRegion();
490 * Find centroid given list of points comprising polyline
494 void oglFindPolylineCentroid(wxList
*points
, double *x
, double *y
)
499 wxNode
*node
= points
->GetFirst();
502 wxRealPoint
*point
= (wxRealPoint
*)node
->GetData();
505 node
= node
->GetNext();
508 *x
= (xcount
/points
->GetCount());
509 *y
= (ycount
/points
->GetCount());
513 * Check that (x1, y1) -> (x2, y2) hits (x3, y3) -> (x4, y4).
514 * If so, ratio1 gives the proportion along the first line
515 * that the intersection occurs (or something like that).
516 * Used by functions below.
519 void oglCheckLineIntersection(double x1
, double y1
, double x2
, double y2
,
520 double x3
, double y3
, double x4
, double y4
,
521 double *ratio1
, double *ratio2
)
523 double denominator_term
= (y4
- y3
)*(x2
- x1
) - (y2
- y1
)*(x4
- x3
);
524 double numerator_term
= (x3
- x1
)*(y4
- y3
) + (x4
- x3
)*(y1
- y3
);
526 double line_constant
;
527 double length_ratio
= 1.0;
530 // Check for parallel lines
531 if ((denominator_term
< 0.005) && (denominator_term
> -0.005))
532 line_constant
= -1.0;
534 line_constant
= numerator_term
/denominator_term
;
536 // Check for intersection
537 if ((line_constant
< 1.0) && (line_constant
> 0.0))
539 // Now must check that other line hits
540 if (((y4
- y3
) < 0.005) && ((y4
- y3
) > -0.005))
541 k_line
= ((x1
- x3
) + line_constant
*(x2
- x1
))/(x4
- x3
);
543 k_line
= ((y1
- y3
) + line_constant
*(y2
- y1
))/(y4
- y3
);
545 if ((k_line
>= 0.0) && (k_line
< 1.0))
546 length_ratio
= line_constant
;
550 *ratio1
= length_ratio
;
555 * Find where (x1, y1) -> (x2, y2) hits one of the lines in xvec, yvec.
556 * (*x3, *y3) is the point where it hits.
559 void oglFindEndForPolyline(double n
, double xvec
[], double yvec
[],
560 double x1
, double y1
, double x2
, double y2
, double *x3
, double *y3
)
563 double lastx
= xvec
[0];
564 double lasty
= yvec
[0];
566 double min_ratio
= 1.0;
570 for (i
= 1; i
< n
; i
++)
572 oglCheckLineIntersection(x1
, y1
, x2
, y2
, lastx
, lasty
, xvec
[i
], yvec
[i
],
573 &line_ratio
, &other_ratio
);
577 if (line_ratio
< min_ratio
)
578 min_ratio
= line_ratio
;
581 // Do last (implicit) line if last and first doubles are not identical
582 if (!(xvec
[0] == lastx
&& yvec
[0] == lasty
))
584 oglCheckLineIntersection(x1
, y1
, x2
, y2
, lastx
, lasty
, xvec
[0], yvec
[0],
585 &line_ratio
, &other_ratio
);
587 if (line_ratio
< min_ratio
)
588 min_ratio
= line_ratio
;
591 *x3
= (x1
+ (x2
- x1
)*min_ratio
);
592 *y3
= (y1
+ (y2
- y1
)*min_ratio
);
597 * Find where the line hits the box.
601 void oglFindEndForBox(double width
, double height
,
602 double x1
, double y1
, // Centre of box (possibly)
603 double x2
, double y2
, // other end of line
604 double *x3
, double *y3
) // End on box edge
609 xvec
[0] = (double)(x1
- width
/2.0);
610 yvec
[0] = (double)(y1
- height
/2.0);
611 xvec
[1] = (double)(x1
- width
/2.0);
612 yvec
[1] = (double)(y1
+ height
/2.0);
613 xvec
[2] = (double)(x1
+ width
/2.0);
614 yvec
[2] = (double)(y1
+ height
/2.0);
615 xvec
[3] = (double)(x1
+ width
/2.0);
616 yvec
[3] = (double)(y1
- height
/2.0);
617 xvec
[4] = (double)(x1
- width
/2.0);
618 yvec
[4] = (double)(y1
- height
/2.0);
620 oglFindEndForPolyline(5, xvec
, yvec
, x2
, y2
, x1
, y1
, x3
, y3
);
624 * Find where the line hits the circle.
628 void oglFindEndForCircle(double radius
,
629 double x1
, double y1
, // Centre of circle
630 double x2
, double y2
, // Other end of line
631 double *x3
, double *y3
)
633 double H
= (double)sqrt((x2
- x1
)*(x2
- x1
) + (y2
- y1
)*(y2
- y1
));
642 *y3
= radius
* (y2
- y1
)/H
+ y1
;
643 *x3
= radius
* (x2
- x1
)/H
+ x1
;
648 * Given the line (x1, y1) -> (x2, y2), and an arrow size of given length and width,
649 * return the position of the tip of the arrow and the left and right vertices of the arrow.
653 void oglGetArrowPoints(double x1
, double y1
, double x2
, double y2
,
654 double length
, double width
,
655 double *tip_x
, double *tip_y
,
656 double *side1_x
, double *side1_y
,
657 double *side2_x
, double *side2_y
)
659 double l
= (double)sqrt((x2
- x1
)*(x2
- x1
) + (y2
- y1
)*(y2
- y1
));
664 double i_bar
= (x2
- x1
)/l
;
665 double j_bar
= (y2
- y1
)/l
;
667 double x3
= (- length
*i_bar
) + x2
;
668 double y3
= (- length
*j_bar
) + y2
;
670 *side1_x
= width
*(-j_bar
) + x3
;
671 *side1_y
= width
*i_bar
+ y3
;
673 *side2_x
= -width
*(-j_bar
) + x3
;
674 *side2_y
= -width
*i_bar
+ y3
;
676 *tip_x
= x2
; *tip_y
= y2
;
680 * Given an ellipse and endpoints of a line, returns the point at which
681 * the line touches the ellipse in values x4, y4.
682 * This function assumes that the centre of the ellipse is at x1, y1, and the
683 * ellipse has a width of width1 and a height of height1. It also assumes you are
684 * wanting to draw an arc FROM point x2, y2 TOWARDS point x3, y3.
685 * This function calculates the x,y coordinates of the intersection point of
686 * the arc with the ellipse.
687 * Author: Ian Harrison
690 void oglDrawArcToEllipse(double x1
, double y1
, double width1
, double height1
, double x2
, double y2
, double x3
, double y3
,
691 double *x4
, double *y4
)
693 double a1
= (double)(width1
/2.0);
694 double b1
= (double)(height1
/2.0);
696 // These are required to give top left x and y coordinates for DrawEllipse
697 // double top_left_x1 = (double)(x1 - a1);
698 // double top_left_y1 = (double)(y1 - b1);
700 // Check for vertical line
701 if (fabs(x2 - x3) < 0.05)
705 *y4 = (double)(y1 - b1);
707 *y4 = (double)(y1 + b1);
711 // Check that x2 != x3
712 if (fabs(x2
- x3
) < 0.05)
716 *y4
= (double)(y1
- sqrt((b1
*b1
- (((x2
-x1
)*(x2
-x1
))*(b1
*b1
)/(a1
*a1
)))));
718 *y4
= (double)(y1
+ sqrt((b1
*b1
- (((x2
-x1
)*(x2
-x1
))*(b1
*b1
)/(a1
*a1
)))));
722 // Calculate the x and y coordinates of the point where arc intersects ellipse
724 double A
, B
, C
, D
, E
, F
, G
, H
, K
;
725 double ellipse1_x
, ellipse1_y
;
727 A
= (double)(1/(a1
* a1
));
728 B
= (double)((y3
- y2
) * (y3
- y2
)) / ((x3
- x2
) * (x3
- x2
) * b1
* b1
);
729 C
= (double)(2 * (y3
- y2
) * (y2
- y1
)) / ((x3
- x2
) * b1
* b1
);
730 D
= (double)((y2
- y1
) * (y2
- y1
)) / (b1
* b1
);
732 F
= (double)(C
- (2 * A
* x1
) - (2 * B
* x2
));
733 G
= (double)((A
* x1
* x1
) + (B
* x2
* x2
) - (C
* x2
) + D
- 1);
734 H
= (double)((y3
- y2
) / (x3
- x2
));
735 K
= (double)((F
* F
) - (4 * E
* G
));
738 // In this case the line intersects the ellipse, so calculate intersection
742 ellipse1_x
= (double)(((F
* -1) + sqrt(K
)) / (2 * E
));
743 ellipse1_y
= (double)((H
* (ellipse1_x
- x2
)) + y2
);
747 ellipse1_x
= (double)(((F
* -1) - sqrt(K
)) / (2 * E
));
748 ellipse1_y
= (double)((H
* (ellipse1_x
- x2
)) + y2
);
752 // in this case, arc does not intersect ellipse, so just draw arc
761 // Draw a little circle (radius = 2) at the end of the arc where it hits
764 double circle_x = ellipse1_x - 2.0;
765 double circle_y = ellipse1_y - 2.0;
766 m_canvas->DrawEllipse(circle_x, circle_y, 4.0, 4.0);
770 // Update a list item from a list of strings
771 void UpdateListBox(wxListBox
*item
, wxList
*list
)
777 wxNode
*node
= list
->GetFirst();
780 wxChar
*s
= (wxChar
*)node
->GetData();
782 node
= node
->GetNext();
786 bool oglRoughlyEqual(double val1
, double val2
, double tol
)
788 return ( (val1
< (val2
+ tol
)) && (val1
> (val2
- tol
)) &&
789 (val2
< (val1
+ tol
)) && (val2
> (val1
- tol
)));
793 * Hex<->Dec conversion
796 // Array used in DecToHex conversion routine.
797 static wxChar sg_HexArray
[] = { wxT('0'), wxT('1'), wxT('2'), wxT('3'),
798 wxT('4'), wxT('5'), wxT('6'), wxT('7'),
799 wxT('8'), wxT('9'), wxT('A'), wxT('B'),
800 wxT('C'), wxT('D'), wxT('E'), wxT('F')
803 // Convert 2-digit hex number to decimal
804 unsigned int oglHexToDec(wxChar
* buf
)
806 int firstDigit
, secondDigit
;
808 if (buf
[0] >= wxT('A'))
809 firstDigit
= buf
[0] - wxT('A') + 10;
811 firstDigit
= buf
[0] - wxT('0');
813 if (buf
[1] >= wxT('A'))
814 secondDigit
= buf
[1] - wxT('A') + 10;
816 secondDigit
= buf
[1] - wxT('0');
818 return firstDigit
* 16 + secondDigit
;
821 // Convert decimal integer to 2-character hex string
822 void oglDecToHex(unsigned int dec
, wxChar
*buf
)
824 int firstDigit
= (int)(dec
/16.0);
825 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
826 buf
[0] = sg_HexArray
[firstDigit
];
827 buf
[1] = sg_HexArray
[secondDigit
];
831 // 3-digit hex to wxColour
832 wxColour
oglHexToColour(const wxString
& hex
)
834 if (hex
.Length() == 6)
838 hex
.Mid(0,2).ToLong(&r
, 16);
839 hex
.Mid(2,2).ToLong(&g
, 16);
840 hex
.Mid(4,2).ToLong(&b
, 16);
841 return wxColour(r
, g
, b
);
844 return wxColour(0,0,0);
847 // RGB to 3-digit hex
848 wxString
oglColourToHex(const wxColour
& colour
)
851 unsigned int red
= colour
.Red();
852 unsigned int green
= colour
.Green();
853 unsigned int blue
= colour
.Blue();
855 oglDecToHex(red
, buf
);
856 oglDecToHex(green
, buf
+2);
857 oglDecToHex(blue
, buf
+4);
859 return wxString(buf
);