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_oglTransparentPen
;
53 wxPen
* g_oglBlackForegroundPen
;
54 wxPen
* g_oglWhiteBackgroundPen
;
55 wxBrush
* g_oglWhiteBackgroundBrush
;
57 char* oglBuffer
= NULL
;
59 wxList
oglObjectCopyMapping(wxKEY_INTEGER
);
63 void wxOGLInitialize()
65 g_oglNormalFont
= wxTheFontList
->FindOrCreateFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
66 g_oglBlackPen
= wxThePenList
->FindOrCreatePen(wxT("BLACK"), 1, wxSOLID
);
67 g_oglTransparentPen
= wxThePenList
->FindOrCreatePen(wxT("WHITE"), 1, wxTRANSPARENT
);
68 g_oglBlackForegroundPen
= wxThePenList
->FindOrCreatePen(wxT("BLACK"), 1, wxSOLID
);
69 g_oglWhiteBackgroundPen
= wxThePenList
->FindOrCreatePen(wxT("WHITE"), 1, wxSOLID
);
70 g_oglWhiteBackgroundBrush
= wxTheBrushList
->FindOrCreateBrush(wxT("WHITE"), wxSOLID
);
72 OGLInitializeConstraintTypes();
74 // Initialize big buffer used when writing images
75 oglBuffer
= new char[3000];
88 g_oglNormalFont
= NULL
; // These will be cleaned up by their GDI list
90 g_oglTransparentPen
= NULL
;
91 g_oglBlackForegroundPen
= NULL
;
92 g_oglWhiteBackgroundPen
= NULL
;
93 g_oglWhiteBackgroundBrush
= NULL
;
95 OGLCleanUpConstraintTypes();
98 wxFont
*oglMatchFont(int point_size
)
100 wxFont
*font
= wxTheFontList
->FindOrCreateFont(point_size
, wxSWISS
, wxNORMAL
, wxNORMAL
);
114 font
= swiss_font_12
;
117 font
= swiss_font_14
;
120 font
= swiss_font_18
;
123 font
= swiss_font_24
;
127 font
= swiss_font_10
;
134 int FontSizeDialog(wxFrame
*parent
, int old_size
)
140 wxString ans
= wxGetTextFromUser(wxT("Enter point size"), wxT("Font size"), buf
, parent
);
141 if (ans
.Length() == 0)
145 ans
.ToLong(&new_size
);
146 if ((new_size
<= 0) || (new_size
> 40))
148 wxMessageBox(wxT("Invalid point size!"), wxT("Error"), wxOK
);
162 char *ans = wxGetSingleChoice("Choose", "Choose a font size", 8, strings, parent);
166 sscanf(ans, "%d", &size);
167 return oglMatchFont(size);
173 // Centre a list of strings in the given box. xOffset and yOffset are the
174 // the positions that these lines should be relative to, and this might be
175 // the same as m_xpos, m_ypos, but might be zero if formatting from left-justifying.
176 void oglCentreText(wxDC
& dc
, wxList
*text_list
,
177 double m_xpos
, double m_ypos
, double width
, double height
,
180 int n
= text_list
->Number();
182 if (!text_list
|| (n
== 0))
185 // First, get maximum dimensions of box enclosing text
187 long char_height
= 0;
189 long current_width
= 0;
191 // Store text extents for speed
192 double *widths
= new double[n
];
194 wxNode
*current
= text_list
->First();
198 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
199 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
200 widths
[i
] = current_width
;
202 if (current_width
> max_width
)
203 max_width
= current_width
;
204 current
= current
->Next();
208 double max_height
= n
*char_height
;
210 double xoffset
, yoffset
, xOffset
, yOffset
;
212 if (formatMode
& FORMAT_CENTRE_VERT
)
214 if (max_height
< height
)
215 yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
217 yoffset
= (double)(m_ypos
- (height
/2.0));
226 if (formatMode
& FORMAT_CENTRE_HORIZ
)
228 xoffset
= (double)(m_xpos
- width
/2.0);
237 current
= text_list
->First();
242 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
245 if ((formatMode
& FORMAT_CENTRE_HORIZ
) && (widths
[i
] < width
))
246 x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
249 double y
= (double)(i
*char_height
+ yoffset
);
251 line
->SetX( x
- xOffset
); line
->SetY( y
- yOffset
);
252 current
= current
->Next();
259 // Centre a list of strings in the given box
260 void oglCentreTextNoClipping(wxDC
& dc
, wxList
*text_list
,
261 double m_xpos
, double m_ypos
, double width
, double height
)
263 int n
= text_list
->Number();
265 if (!text_list
|| (n
== 0))
268 // First, get maximum dimensions of box enclosing text
270 long char_height
= 0;
272 long current_width
= 0;
274 // Store text extents for speed
275 double *widths
= new double[n
];
277 wxNode
*current
= text_list
->First();
281 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
282 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
283 widths
[i
] = current_width
;
285 if (current_width
> max_width
)
286 max_width
= current_width
;
287 current
= current
->Next();
291 double max_height
= n
*char_height
;
293 double yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
295 double xoffset
= (double)(m_xpos
- width
/2.0);
297 current
= text_list
->First();
302 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
304 double x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
305 double y
= (double)(i
*char_height
+ yoffset
);
307 line
->SetX( x
- m_xpos
); line
->SetY( y
- m_ypos
);
308 current
= current
->Next();
314 void oglGetCentredTextExtent(wxDC
& dc
, wxList
*text_list
,
315 double m_xpos
, double m_ypos
, double width
, double height
,
316 double *actual_width
, double *actual_height
)
318 int n
= text_list
->Number();
320 if (!text_list
|| (n
== 0))
327 // First, get maximum dimensions of box enclosing text
329 long char_height
= 0;
331 long current_width
= 0;
333 wxNode
*current
= text_list
->First();
337 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
338 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
340 if (current_width
> max_width
)
341 max_width
= current_width
;
342 current
= current
->Next();
346 *actual_height
= n
*char_height
;
347 *actual_width
= max_width
;
350 // Format a string to a list of strings that fit in the given box.
351 // Interpret %n and 10 or 13 as a new line.
352 wxStringList
*oglFormatText(wxDC
& dc
, const wxString
& text
, double width
, double height
, int formatMode
)
354 // First, parse the string into a list of words
355 wxStringList word_list
;
357 // Make new lines into NULL strings at this point
358 int i
= 0; int j
= 0; int len
= text
.Length();
359 wxChar word
[200]; word
[0] = 0;
360 bool end_word
= FALSE
; bool new_line
= FALSE
;
369 { word
[j
] = wxT('%'); j
++; }
372 if (text
[i
] == wxT('n'))
373 { new_line
= TRUE
; end_word
= TRUE
; i
++; }
375 { word
[j
] = wxT('%'); j
++; word
[j
] = text
[i
]; j
++; i
++; }
381 new_line
= TRUE
; end_word
= TRUE
; i
++;
386 new_line
= TRUE
; end_word
= TRUE
; i
++;
401 if (i
== len
) end_word
= TRUE
;
411 word_list
.Append(NULL
);
415 // Now, make a list of strings which can fit in the box
416 wxStringList
*string_list
= new wxStringList
;
419 wxNode
*node
= word_list
.First();
424 wxString
oldBuffer(buffer
);
426 char *s
= (char *)node
->Data();
430 if (buffer
.Length() > 0)
431 string_list
->Add(buffer
);
437 if (buffer
.Length() != 0)
441 dc
.GetTextExtent(buffer
, &x
, &y
);
443 // Don't fit within the bounding box if we're fitting shape to contents
444 if ((x
> width
) && !(formatMode
& FORMAT_SIZE_TO_CONTENTS
))
446 // Deal with first word being wider than box
447 if (oldBuffer
.Length() > 0)
448 string_list
->Add(oldBuffer
);
457 if (buffer
.Length() != 0)
458 string_list
->Add(buffer
);
463 void oglDrawFormattedText(wxDC
& dc
, wxList
*text_list
,
464 double m_xpos
, double m_ypos
, double width
, double height
,
467 double xoffset
, yoffset
;
468 if (formatMode
& FORMAT_CENTRE_HORIZ
)
471 xoffset
= (double)(m_xpos
- (width
/ 2.0));
473 if (formatMode
& FORMAT_CENTRE_VERT
)
476 yoffset
= (double)(m_ypos
- (height
/ 2.0));
478 dc
.SetClippingRegion(
479 (long)(m_xpos
- width
/2.0), (long)(m_ypos
- height
/2.0),
480 (long)width
, (long)height
);
482 wxNode
*current
= text_list
->First();
485 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
487 dc
.DrawText(line
->GetText(), WXROUND(xoffset
+ line
->GetX()), WXROUND(yoffset
+ line
->GetY()));
488 current
= current
->Next();
491 dc
.DestroyClippingRegion();
495 * Find centroid given list of points comprising polyline
499 void oglFindPolylineCentroid(wxList
*points
, double *x
, double *y
)
504 wxNode
*node
= points
->First();
507 wxRealPoint
*point
= (wxRealPoint
*)node
->Data();
513 *x
= (xcount
/points
->Number());
514 *y
= (ycount
/points
->Number());
518 * Check that (x1, y1) -> (x2, y2) hits (x3, y3) -> (x4, y4).
519 * If so, ratio1 gives the proportion along the first line
520 * that the intersection occurs (or something like that).
521 * Used by functions below.
524 void oglCheckLineIntersection(double x1
, double y1
, double x2
, double y2
,
525 double x3
, double y3
, double x4
, double y4
,
526 double *ratio1
, double *ratio2
)
528 double denominator_term
= (y4
- y3
)*(x2
- x1
) - (y2
- y1
)*(x4
- x3
);
529 double numerator_term
= (x3
- x1
)*(y4
- y3
) + (x4
- x3
)*(y1
- y3
);
531 double line_constant
;
532 double length_ratio
= 1.0;
535 // Check for parallel lines
536 if ((denominator_term
< 0.005) && (denominator_term
> -0.005))
537 line_constant
= -1.0;
539 line_constant
= numerator_term
/denominator_term
;
541 // Check for intersection
542 if ((line_constant
< 1.0) && (line_constant
> 0.0))
544 // Now must check that other line hits
545 if (((y4
- y3
) < 0.005) && ((y4
- y3
) > -0.005))
546 k_line
= ((x1
- x3
) + line_constant
*(x2
- x1
))/(x4
- x3
);
548 k_line
= ((y1
- y3
) + line_constant
*(y2
- y1
))/(y4
- y3
);
550 if ((k_line
>= 0.0) && (k_line
< 1.0))
551 length_ratio
= line_constant
;
555 *ratio1
= length_ratio
;
560 * Find where (x1, y1) -> (x2, y2) hits one of the lines in xvec, yvec.
561 * (*x3, *y3) is the point where it hits.
564 void oglFindEndForPolyline(double n
, double xvec
[], double yvec
[],
565 double x1
, double y1
, double x2
, double y2
, double *x3
, double *y3
)
568 double lastx
= xvec
[0];
569 double lasty
= yvec
[0];
571 double min_ratio
= 1.0;
575 for (i
= 1; i
< n
; i
++)
577 oglCheckLineIntersection(x1
, y1
, x2
, y2
, lastx
, lasty
, xvec
[i
], yvec
[i
],
578 &line_ratio
, &other_ratio
);
582 if (line_ratio
< min_ratio
)
583 min_ratio
= line_ratio
;
586 // Do last (implicit) line if last and first doubles are not identical
587 if (!(xvec
[0] == lastx
&& yvec
[0] == lasty
))
589 oglCheckLineIntersection(x1
, y1
, x2
, y2
, lastx
, lasty
, xvec
[0], yvec
[0],
590 &line_ratio
, &other_ratio
);
592 if (line_ratio
< min_ratio
)
593 min_ratio
= line_ratio
;
596 *x3
= (x1
+ (x2
- x1
)*min_ratio
);
597 *y3
= (y1
+ (y2
- y1
)*min_ratio
);
602 * Find where the line hits the box.
606 void oglFindEndForBox(double width
, double height
,
607 double x1
, double y1
, // Centre of box (possibly)
608 double x2
, double y2
, // other end of line
609 double *x3
, double *y3
) // End on box edge
614 xvec
[0] = (double)(x1
- width
/2.0);
615 yvec
[0] = (double)(y1
- height
/2.0);
616 xvec
[1] = (double)(x1
- width
/2.0);
617 yvec
[1] = (double)(y1
+ height
/2.0);
618 xvec
[2] = (double)(x1
+ width
/2.0);
619 yvec
[2] = (double)(y1
+ height
/2.0);
620 xvec
[3] = (double)(x1
+ width
/2.0);
621 yvec
[3] = (double)(y1
- height
/2.0);
622 xvec
[4] = (double)(x1
- width
/2.0);
623 yvec
[4] = (double)(y1
- height
/2.0);
625 oglFindEndForPolyline(5, xvec
, yvec
, x2
, y2
, x1
, y1
, x3
, y3
);
629 * Find where the line hits the circle.
633 void oglFindEndForCircle(double radius
,
634 double x1
, double y1
, // Centre of circle
635 double x2
, double y2
, // Other end of line
636 double *x3
, double *y3
)
638 double H
= (double)sqrt((x2
- x1
)*(x2
- x1
) + (y2
- y1
)*(y2
- y1
));
647 *y3
= radius
* (y2
- y1
)/H
+ y1
;
648 *x3
= radius
* (x2
- x1
)/H
+ x1
;
653 * Given the line (x1, y1) -> (x2, y2), and an arrow size of given length and width,
654 * return the position of the tip of the arrow and the left and right vertices of the arrow.
658 void oglGetArrowPoints(double x1
, double y1
, double x2
, double y2
,
659 double length
, double width
,
660 double *tip_x
, double *tip_y
,
661 double *side1_x
, double *side1_y
,
662 double *side2_x
, double *side2_y
)
664 double l
= (double)sqrt((x2
- x1
)*(x2
- x1
) + (y2
- y1
)*(y2
- y1
));
669 double i_bar
= (x2
- x1
)/l
;
670 double j_bar
= (y2
- y1
)/l
;
672 double x3
= (- length
*i_bar
) + x2
;
673 double y3
= (- length
*j_bar
) + y2
;
675 *side1_x
= width
*(-j_bar
) + x3
;
676 *side1_y
= width
*i_bar
+ y3
;
678 *side2_x
= -width
*(-j_bar
) + x3
;
679 *side2_y
= -width
*i_bar
+ y3
;
681 *tip_x
= x2
; *tip_y
= y2
;
685 * Given an ellipse and endpoints of a line, returns the point at which
686 * the line touches the ellipse in values x4, y4.
687 * This function assumes that the centre of the ellipse is at x1, y1, and the
688 * ellipse has a width of width1 and a height of height1. It also assumes you are
689 * wanting to draw an arc FROM point x2, y2 TOWARDS point x3, y3.
690 * This function calculates the x,y coordinates of the intersection point of
691 * the arc with the ellipse.
692 * Author: Ian Harrison
695 void oglDrawArcToEllipse(double x1
, double y1
, double width1
, double height1
, double x2
, double y2
, double x3
, double y3
,
696 double *x4
, double *y4
)
698 double a1
= (double)(width1
/2.0);
699 double b1
= (double)(height1
/2.0);
701 // These are required to give top left x and y coordinates for DrawEllipse
702 // double top_left_x1 = (double)(x1 - a1);
703 // double top_left_y1 = (double)(y1 - b1);
705 // Check for vertical line
706 if (fabs(x2 - x3) < 0.05)
710 *y4 = (double)(y1 - b1);
712 *y4 = (double)(y1 + b1);
716 // Check that x2 != x3
717 if (fabs(x2
- x3
) < 0.05)
721 *y4
= (double)(y1
- sqrt((b1
*b1
- (((x2
-x1
)*(x2
-x1
))*(b1
*b1
)/(a1
*a1
)))));
723 *y4
= (double)(y1
+ sqrt((b1
*b1
- (((x2
-x1
)*(x2
-x1
))*(b1
*b1
)/(a1
*a1
)))));
727 // Calculate the x and y coordinates of the point where arc intersects ellipse
729 double A
, B
, C
, D
, E
, F
, G
, H
, K
;
730 double ellipse1_x
, ellipse1_y
;
732 A
= (double)(1/(a1
* a1
));
733 B
= (double)((y3
- y2
) * (y3
- y2
)) / ((x3
- x2
) * (x3
- x2
) * b1
* b1
);
734 C
= (double)(2 * (y3
- y2
) * (y2
- y1
)) / ((x3
- x2
) * b1
* b1
);
735 D
= (double)((y2
- y1
) * (y2
- y1
)) / (b1
* b1
);
737 F
= (double)(C
- (2 * A
* x1
) - (2 * B
* x2
));
738 G
= (double)((A
* x1
* x1
) + (B
* x2
* x2
) - (C
* x2
) + D
- 1);
739 H
= (double)((y3
- y2
) / (x3
- x2
));
740 K
= (double)((F
* F
) - (4 * E
* G
));
743 // In this case the line intersects the ellipse, so calculate intersection
747 ellipse1_x
= (double)(((F
* -1) + sqrt(K
)) / (2 * E
));
748 ellipse1_y
= (double)((H
* (ellipse1_x
- x2
)) + y2
);
752 ellipse1_x
= (double)(((F
* -1) - sqrt(K
)) / (2 * E
));
753 ellipse1_y
= (double)((H
* (ellipse1_x
- x2
)) + y2
);
757 // in this case, arc does not intersect ellipse, so just draw arc
766 // Draw a little circle (radius = 2) at the end of the arc where it hits
769 double circle_x = ellipse1_x - 2.0;
770 double circle_y = ellipse1_y - 2.0;
771 m_canvas->DrawEllipse(circle_x, circle_y, 4.0, 4.0);
775 // Update a list item from a list of strings
776 void UpdateListBox(wxListBox
*item
, wxList
*list
)
782 wxNode
*node
= list
->First();
785 char *s
= (char *)node
->Data();
791 bool oglRoughlyEqual(double val1
, double val2
, double tol
)
793 return ( (val1
< (val2
+ tol
)) && (val1
> (val2
- tol
)) &&
794 (val2
< (val1
+ tol
)) && (val2
> (val1
- tol
)));
798 * Hex<->Dec conversion
801 // Array used in DecToHex conversion routine.
802 static wxChar sg_HexArray
[] = { wxT('0'), wxT('1'), wxT('2'), wxT('3'),
803 wxT('4'), wxT('5'), wxT('6'), wxT('7'),
804 wxT('8'), wxT('9'), wxT('A'), wxT('B'),
805 wxT('C'), wxT('D'), wxT('E'), wxT('F')
808 // Convert 2-digit hex number to decimal
809 unsigned int oglHexToDec(wxChar
* buf
)
811 int firstDigit
, secondDigit
;
813 if (buf
[0] >= wxT('A'))
814 firstDigit
= buf
[0] - wxT('A') + 10;
816 firstDigit
= buf
[0] - wxT('0');
818 if (buf
[1] >= wxT('A'))
819 secondDigit
= buf
[1] - wxT('A') + 10;
821 secondDigit
= buf
[1] - wxT('0');
823 return firstDigit
* 16 + secondDigit
;
826 // Convert decimal integer to 2-character hex string
827 void oglDecToHex(unsigned int dec
, wxChar
*buf
)
829 int firstDigit
= (int)(dec
/16.0);
830 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
831 buf
[0] = sg_HexArray
[firstDigit
];
832 buf
[1] = sg_HexArray
[secondDigit
];
836 // 3-digit hex to wxColour
837 wxColour
oglHexToColour(const wxString
& hex
)
839 if (hex
.Length() == 6)
843 hex
.Mid(0,2).ToLong(&r
, 16);
844 hex
.Mid(2,2).ToLong(&g
, 16);
845 hex
.Mid(4,2).ToLong(&b
, 16);
846 return wxColour(r
, g
, b
);
849 return wxColour(0,0,0);
852 // RGB to 3-digit hex
853 wxString
oglColourToHex(const wxColour
& colour
)
856 unsigned int red
= colour
.Red();
857 unsigned int green
= colour
.Green();
858 unsigned int blue
= colour
.Blue();
860 oglDecToHex(red
, buf
);
861 oglDecToHex(green
, buf
+2);
862 oglDecToHex(blue
, buf
+4);
864 return wxString(buf
);