ee6594ea0214d773d716c66c0e8c3dc2a8ba4423
[ctsim.git] / include / sgp.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      Name:         sgp.h
5 **      Purpose:      Header file for Simple Graphics Package
6 **      Author:       Kevin Rosenberg
7 **      Date Started: 1984
8 **
9 **  This is part of the CTSim program
10 **  Copyright (c) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id$
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #ifndef __H_SGP
29 #define __H_SGP
30
31 #if HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include "transformmatrix.h"
36
37 #ifdef HAVE_WXWINDOWS
38 #include <wx/wx.h>
39 #include <wx/font.h>
40 #endif
41
42 #if HAVE_G2_H
43 extern "C" {
44 #include "g2.h"
45 #include "g2_X11.h"
46 }
47 #endif
48
49 #include <string>
50
51 class SGPDriver {
52 private:
53   int m_iPhysicalXSize;
54   int m_iPhysicalYSize;
55   std::string m_sWindowTitle;
56   int m_idDriver;
57
58 #ifdef HAVE_WXWINDOWS
59   wxDC* m_pDC;
60 #endif
61   int m_idG2;
62
63 public:
64   enum {
65     SGPDRIVER_WXWINDOWS = 1,
66     SGPDRIVER_G2 = 2,
67     SGPDRIVER_OPENGL = 4,
68   };
69
70 #ifdef HAVE_WXWINDOWS
71   SGPDriver (wxDC* pDC, int xsize = 640, int ysize = 480);
72 #endif
73
74   SGPDriver (const char* szWinTitle = "", int xsize = 640, int ysize = 480);
75   
76   ~SGPDriver ();
77
78   int getPhysicalXSize () const
79     { return m_iPhysicalXSize; }
80
81   int getPhysicalYSize () const
82     { return m_iPhysicalYSize; }
83
84   const std::string& getWindowTitle () const
85     { return m_sWindowTitle; }
86
87   bool isWX () const
88   { return (m_idDriver & SGPDRIVER_WXWINDOWS ? true : false); }
89
90   bool isG2 () const
91   { return (m_idDriver & SGPDRIVER_G2 ? true : false); }
92
93   int idG2 () const
94     { return m_idG2; }
95
96 #ifdef HAVE_WXWINDOWS
97   wxDC* idWX () const
98     { return m_pDC; }
99
100   void setDC (wxDC* dc)
101       { m_pDC = dc; }
102 #endif
103 };
104
105
106 class SGP_RGBColor;
107 class SGP {
108 private:
109   int m_iPhysicalXSize;   // Physical Window size 
110   int m_iPhysicalYSize;
111   SGPDriver m_driver;
112
113   double xw_min;    // Window extents 
114   double yw_min;
115   double xw_max;
116   double yw_max;
117   double xv_min;    // Viewport extents 
118   double yv_min;
119   double xv_max;
120   double yv_max;
121   double viewNDC[4];   // Viewport array for clip_rect() 
122
123   int m_iCurrentPhysicalX;
124   int m_iCurrentPhysicalY;
125   double m_dCurrentWorldX;
126   double m_dCurrentWorldY;
127   double m_dTextAngle;
128   int m_iTextPointSize;
129   bool m_bRecalcTransform;
130   double m_dPointsPerPixel;  // points (72pt/in) per screen pixel;
131   int m_iLinestyle;
132   int m_iMarker;
133
134   // Master coordinates are coordinates before CTM transformation
135   // World coordinates are coordinates defined by setWindow()
136   // Normalized device coordinates range from 0. to 1. in both axes
137   TransformationMatrix2D wc_to_ndc;     // World coord to NDC matrix 
138   TransformationMatrix2D mc_to_ndc;     // Master to NDC 
139   TransformationMatrix2D ndc_to_mc;     // NDC to Master
140   TransformationMatrix2D m_ctm;         // Current transfromation matrix 
141   
142   void calc_transform ();
143
144   static SGP_RGBColor s_aRGBColor[];
145   static int s_iRGBColorCount;
146
147 #if HAVE_WXWINDOWS
148   wxPen m_pen;
149   wxFont* m_pFont;
150
151   void initFromDC (wxDC* pDC);
152 #endif
153
154 public:
155   enum {                  // linestyles 
156       LS_NOLINE = 0,
157       LS_SOLID = 0xffff,
158       LS_DASH1 = 0xff00,
159       LS_DASH2 = 0xf0f0,
160       LS_DASH3 = 0xcccc,
161       LS_DASH4 = 0xff3e,
162       LS_DOTTED = 0xaaaa,
163   };
164
165   enum {            // Codes for marker symbols
166       MARKER_POINT = 0, // small dot 
167       MARKER_SQUARE = 1,        // empty square 
168       MARKER_FSQUARE = 2,       // filled square 
169       MARKER_DIAMOND = 3,       // empty diamond 
170       MARKER_FDIAMOND = 4,      // filled diamond 
171       MARKER_CROSS =  5,        // cross 
172       MARKER_XCROSS = 6,        // x 
173       MARKER_CIRCLE = 7,    // open circle 
174       MARKER_FCIRCLE = 8,       // filled circle 
175       MARKER_BSQUARE = 9,       // big open square 
176       MARKER_BDIAMOND = 10,     // big open diamond 
177   };
178   enum  { MARK_COUNT = 11, };
179   static const unsigned char MARKER_BITMAP[MARK_COUNT][5];
180
181   SGP (const SGPDriver& driver);
182   ~SGP();
183
184   void drawCircle (const double r);
185   void drawArc (const double r, double start, double stop);
186   void drawRect (double xmin, double ymin, double xmax, double ymax);
187   void lineAbs(double x, double y);
188   void moveAbs(double x, double y);
189   void lineRel(double x, double y);
190   void moveRel(double x, double y);
191   void drawText(const char *szMessage);
192   void drawText(const std::string& rsMessage);
193   void polylineAbs(double x[], double y[], int n);
194   void markerAbs (double x, double y);
195   void markerRel(double x, double y);
196   void pointAbs(double x, double y);
197   void pointRel(double x, double y);
198
199   void eraseWindow ();
200   void setWindow (double xmin, double ymin, double xmax, double ymax);
201   void setViewport (double xmin, double ymin, double xmax, double ymax);
202   void frameViewport();
203
204   void setColor (int icol);
205   void setLineStyle (int style);
206   void setTextSize (double height);
207   void setTextNDCSize (double height);
208   void setTextPointSize (double height);
209   void setTextAngle (double angle);
210   void setTextColor (int iFGcolor, int iBGcolor);
211   void setPenWidth (int width);
212   void setMarker (int idMarker);
213   void setRasterOp (int ro);
214
215   void getWindow (double& xmin, double& ymin, double& xmax, double& ymax);
216   void getViewport (double& xmin, double& ymin, double& xmax, double& ymax);
217   void getTextExtent (const char *szText, double* x, double* y);
218   double getCharHeight ();
219   double getCharWidth ();
220   SGPDriver& getDriver() {return m_driver;}
221   const SGPDriver& getDriver() const {return m_driver;}
222
223   void ctmClear ();
224   void ctmSet (const TransformationMatrix2D& m);
225   void preTranslate (double x, double y);
226   void postTranslate (double x, double y);
227   void preScale (double sx, double sy);
228   void postScale (double sx, double sy);
229   void preRotate (double theta);
230   void postRotate (double theta);
231   void preShear (double shrx, double shry);
232   void postShear (double shrx, double shry);
233   void transformNDCtoMC (double* x, double* y);
234   void transformMCtoNDC (double* x, double* y);
235   void transformMCtoNDC (double xIn, double yIn, double* xOut, double* yOut);
236
237   void stylusNDC (double x, double y, bool beam);
238   void pointNDC (double x, double y);
239   void markerNDC (double x, double y);
240
241 #if HAVE_WXWINDOWS
242   void setDC (wxDC* pDC);
243 #endif
244 };
245
246
247 enum {
248     C_BLACK     = 0,     // color codes 
249     C_BLUE      = 1,
250     C_GREEN     = 2,
251     C_CYAN      = 3,
252     C_RED       = 4,
253     C_MAGENTA   = 5,
254     C_BROWN     = 6,
255     C_GRAY      = 7,
256     C_LTGRAY    = 8,
257     C_LTBLUE    = 9,
258     C_LTGREEN   = 10,
259     C_LTCYAN    = 11,
260     C_LTRED     = 12,
261     C_LTMAGENTA = 13,
262     C_YELLOW    = 14,
263     C_WHITE     = 15,
264 };
265
266 enum RasterOp {
267     RO_AND = 1,
268     RO_AND_INVERT,
269     RO_AND_REVERSE,
270     RO_CLEAR,
271     RO_COPY,
272     RO_EQUIV,
273     RO_INVERT,
274     RO_NAND,
275     RO_NOR,
276     RO_NO_OP,
277     RO_OR,
278     RO_OR_INVERT,
279     RO_OR_REVERSE,
280     RO_SET,
281     RO_SRC_INVERT,
282     RO_XOR,
283 };
284
285
286 class SGP_RGBColor {
287  private:
288   short int r;
289   short int g;
290   short int b;
291
292  public:
293   SGP_RGBColor (int r, int g, int b)
294     : r(r), g(g), b(b)
295     {}
296
297   int getRed () const
298     { return r; }
299
300   int getGreen () const
301     { return g; }
302
303   int getBlue () const
304     { return b; }
305
306 };
307
308 #endif