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("BLACK", 1, wxSOLID
);
67 g_oglTransparentPen
= wxThePenList
->FindOrCreatePen("WHITE", 1, wxTRANSPARENT
);
68 g_oglBlackForegroundPen
= wxThePenList
->FindOrCreatePen("BLACK", 1, wxSOLID
);
69 g_oglWhiteBackgroundPen
= wxThePenList
->FindOrCreatePen("WHITE", 1, wxSOLID
);
70 g_oglWhiteBackgroundBrush
= wxTheBrushList
->FindOrCreateBrush("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
)
139 sprintf(buf
, "%d", old_size
);
140 wxString ans
= wxGetTextFromUser("Enter point size", "Font size", buf
, parent
);
144 int new_size
= atoi(ans
);
145 if ((new_size
<= 0) || (new_size
> 40))
147 wxMessageBox("Invalid point size!", "Error", wxOK
);
161 char *ans = wxGetSingleChoice("Choose", "Choose a font size", 8, strings, parent);
165 sscanf(ans, "%d", &size);
166 return oglMatchFont(size);
172 // Centre a list of strings in the given box. xOffset and yOffset are the
173 // the positions that these lines should be relative to, and this might be
174 // the same as m_xpos, m_ypos, but might be zero if formatting from left-justifying.
175 void oglCentreText(wxDC
& dc
, wxList
*text_list
,
176 double m_xpos
, double m_ypos
, double width
, double height
,
179 int n
= text_list
->Number();
181 if (!text_list
|| (n
== 0))
184 // First, get maximum dimensions of box enclosing text
186 long char_height
= 0;
188 long current_width
= 0;
190 // Store text extents for speed
191 double *widths
= new double[n
];
193 wxNode
*current
= text_list
->First();
197 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
198 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
199 widths
[i
] = current_width
;
201 if (current_width
> max_width
)
202 max_width
= current_width
;
203 current
= current
->Next();
207 double max_height
= n
*char_height
;
209 double xoffset
, yoffset
, xOffset
, yOffset
;
211 if (formatMode
& FORMAT_CENTRE_VERT
)
213 if (max_height
< height
)
214 yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
216 yoffset
= (double)(m_ypos
- (height
/2.0));
225 if (formatMode
& FORMAT_CENTRE_HORIZ
)
227 xoffset
= (double)(m_xpos
- width
/2.0);
236 current
= text_list
->First();
241 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
244 if ((formatMode
& FORMAT_CENTRE_HORIZ
) && (widths
[i
] < width
))
245 x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
248 double y
= (double)(i
*char_height
+ yoffset
);
250 line
->SetX( x
- xOffset
); line
->SetY( y
- yOffset
);
251 current
= current
->Next();
258 // Centre a list of strings in the given box
259 void oglCentreTextNoClipping(wxDC
& dc
, wxList
*text_list
,
260 double m_xpos
, double m_ypos
, double width
, double height
)
262 int n
= text_list
->Number();
264 if (!text_list
|| (n
== 0))
267 // First, get maximum dimensions of box enclosing text
269 long char_height
= 0;
271 long current_width
= 0;
273 // Store text extents for speed
274 double *widths
= new double[n
];
276 wxNode
*current
= text_list
->First();
280 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
281 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
282 widths
[i
] = current_width
;
284 if (current_width
> max_width
)
285 max_width
= current_width
;
286 current
= current
->Next();
290 double max_height
= n
*char_height
;
292 double yoffset
= (double)(m_ypos
- (height
/2.0) + (height
- max_height
)/2.0);
294 double xoffset
= (double)(m_xpos
- width
/2.0);
296 current
= text_list
->First();
301 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
303 double x
= (double)((width
- widths
[i
])/2.0 + xoffset
);
304 double y
= (double)(i
*char_height
+ yoffset
);
306 line
->SetX( x
- m_xpos
); line
->SetY( y
- m_ypos
);
307 current
= current
->Next();
313 void oglGetCentredTextExtent(wxDC
& dc
, wxList
*text_list
,
314 double m_xpos
, double m_ypos
, double width
, double height
,
315 double *actual_width
, double *actual_height
)
317 int n
= text_list
->Number();
319 if (!text_list
|| (n
== 0))
326 // First, get maximum dimensions of box enclosing text
328 long char_height
= 0;
330 long current_width
= 0;
332 wxNode
*current
= text_list
->First();
336 wxShapeTextLine
*line
= (wxShapeTextLine
*)current
->Data();
337 dc
.GetTextExtent(line
->GetText(), ¤t_width
, &char_height
);
339 if (current_width
> max_width
)
340 max_width
= current_width
;
341 current
= current
->Next();
345 *actual_height
= n
*char_height
;
346 *actual_width
= max_width
;
349 // Format a string to a list of strings that fit in the given box.
350 // Interpret %n and 10 or 13 as a new line.
351 wxStringList
*oglFormatText(wxDC
& dc
, const wxString
& text
, double width
, double height
, int formatMode
)
353 // First, parse the string into a list of words
354 wxStringList word_list
;
356 // Make new lines into NULL strings at this point
357 int i
= 0; int j
= 0; int len
= strlen(text
);
358 char word
[200]; word
[0] = 0;
359 bool end_word
= FALSE
; bool new_line
= FALSE
;
368 { word
[j
] = '%'; j
++; }
372 { new_line
= TRUE
; end_word
= TRUE
; i
++; }
374 { word
[j
] = '%'; j
++; word
[j
] = text
[i
]; j
++; i
++; }
380 new_line
= TRUE
; end_word
= TRUE
; i
++;
385 new_line
= TRUE
; end_word
= TRUE
; i
++;
400 if (i
== len
) end_word
= TRUE
;
410 word_list
.Append(NULL
);
414 // Now, make a list of strings which can fit in the box
415 wxStringList
*string_list
= new wxStringList
;
419 wxNode
*node
= word_list
.First();
424 wxString
oldBuffer(buffer
);
426 char *s
= (char *)node
->Data();
430 if (strlen(buffer
) > 0)
431 string_list
->Add(buffer
);
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
);
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 char sg_HexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
803 'C', 'D', 'E', 'F' };
805 // Convert 2-digit hex number to decimal
806 unsigned int oglHexToDec(char* buf
)
808 int firstDigit
, secondDigit
;
811 firstDigit
= buf
[0] - 'A' + 10;
813 firstDigit
= buf
[0] - '0';
816 secondDigit
= buf
[1] - 'A' + 10;
818 secondDigit
= buf
[1] - '0';
820 return firstDigit
* 16 + secondDigit
;
823 // Convert decimal integer to 2-character hex string
824 void oglDecToHex(unsigned int dec
, char *buf
)
826 int firstDigit
= (int)(dec
/16.0);
827 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
828 buf
[0] = sg_HexArray
[firstDigit
];
829 buf
[1] = sg_HexArray
[secondDigit
];
833 // 3-digit hex to wxColour
834 wxColour
oglHexToColour(const wxString
& hex
)
836 if (hex
.Length() == 6)
839 strncpy(buf
, hex
, 7);
840 unsigned int r
= oglHexToDec((char *)buf
);
841 unsigned int g
= oglHexToDec((char *)(buf
+2));
842 unsigned int b
= oglHexToDec((char *)(buf
+4));
843 return wxColour(r
, g
, b
);
846 return wxColour(0,0,0);
849 // RGB to 3-digit hex
850 wxString
oglColourToHex(const wxColour
& colour
)
853 unsigned int red
= colour
.Red();
854 unsigned int green
= colour
.Green();
855 unsigned int blue
= colour
.Blue();
857 oglDecToHex(red
, buf
);
858 oglDecToHex(green
, buf
+2);
859 oglDecToHex(blue
, buf
+4);
861 return wxString(buf
);