File: gport.h

package info (click to toggle)
treeviewx 0.5.1%2Bgit20100823.7e4d0e9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,664 kB
  • sloc: cpp: 14,362; xml: 82; makefile: 66
file content (383 lines) | stat: -rwxr-xr-x 10,286 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#ifndef GPORTH
#define GPORTH

#ifdef __BORLANDC__
	// Undefine __MINMAX_DEFINED so that min and max are correctly defined
	#ifdef __MINMAX_DEFINED
		#undef __MINMAX_DEFINED
	#endif
    // Ignore "Cannot create precompiled header: code in header" message
    // generated when compiling string.cc
    #pragma warn -pch
#endif

#include <iostream>
#include <fstream>
#include <string>

#ifdef __BORLANDC__
    #pragma warn .pch
#endif


using namespace std;


#include "gdefs.h"

// System specific includes here
#if GPORT_WINDOWS
#endif

#if GPORT_MAC
//	#include <Printing.h>
#endif

enum GPortDevice {devScreen, devPrinter, devPicture, devPostscript};




/* Note that we always draw using the following coordinate system:

(0,0)--------->(+x,0)
  |
  |
  |
 \/
(+y,0)

 Hence the origin is the top left hand corner, and y goes down rather than up.
 This is typical for drawing to a window. Some systems have other coordinate
 systems (such as Postscript). We make the translation internally.

*/

// A point
class GPoint
{
public:
    GPoint () { SetPoint (0, 0); };
    GPoint (GPoint &p) { X = p.X; Y = p.Y; };
    GPoint (const int x, const int y) { SetPoint (x, y); };
    virtual int GetX () const { return X; };
    virtual int GetY () const { return Y; };
    virtual void Offset (const int xoff, const int yoff) { X += xoff; Y += yoff; };
    virtual void SetPoint (const int x, const int y) { X = x; Y = y; };
    virtual void SetX (int x) { X = x; };
    virtual void SetY (int y) { Y = y; };
    
    int	operator== (const GPoint &p) { return (int) ( (X == p.X) && ( Y == p.Y)); };
    int	operator!= (const GPoint &p) { return (int) ( (X != p.X) || ( Y != p.Y)); };
protected:
    int X;
    int Y;
};

// A rectangle
class GRect
{
public:
    GRect () { left = top = right = bottom = 0; };
    GRect (const int l, const int t, const int r, const int b) { SetRect (l, t, r, b); };
    virtual int  GetLeft () const { return left; };
    virtual int  GetTop () const { return top; };
    virtual int  GetRight () const { return right; };
    virtual int  GetBottom () const { return bottom; };
    virtual int  GetWidth () const { return right - left; };
    virtual int  GetHeight () const { return bottom - top; };

    virtual void Inset (const int dx, const int dy) { left += dx; right -= dx; top += dy; bottom -= dy; };
    virtual void Offset (const int dx, const int dy) { left += dx; right += dx; top += dy; bottom += dy; };
    virtual bool PointInRect (GPoint &pt)
    {
    	return (((pt.GetX() >= left) && (pt.GetX() <= right)) &&
        	((pt.GetY() >= top) && (pt.GetY() <= bottom)));
    }

    virtual void SetLeft (const int l) {left = l; };
    virtual void SetTop (const int t) {top = t; };
    virtual void SetRight (const int r) {right = r; };
    virtual void SetBottom (const int b) {bottom = b; };
    virtual void SetRect (const int l, const int t, const int r, const int b)
    	{ left = l; top = t; right = r; bottom = b; };

protected:
    int left, top, right, bottom;
};

// Base class for system specific fonts
class GBaseFont
{
public:
    GBaseFont ();
    virtual ~GBaseFont () {};
    virtual std::string GetName () { return description; };
    virtual std::string GetDescription () { return description; };
    virtual int  GetSize () { return size; };
    virtual bool IsBold () { return bold; };
    virtual bool IsItalic () { return italic; };    
    virtual void SetName (std::string name) { description = name; };
protected:
    std::string 	description;
    std::string 	name;
    int 		size;
    bool 		bold;
    bool 		italic;
};
typedef GBaseFont *GBaseFontPtr;

typedef GBaseFont GFont ; // for now
typedef GFont *GFontPtr;



// Windows needs the two handles for screen and printer fonts
// Mac just sets things
// Postscript writes to the postscript stream


// Virtual class to encapsulate printing
class GBasePrinter
{
public:
    GBasePrinter () {};
    virtual ~GBasePrinter () {};
    virtual void PrinterSetup () = 0;
    virtual void AbortPrinting () = 0;
    virtual void EndDoc ();
    virtual bool EndPage ();
    virtual void GetPrintingRect (GRect &r) = 0;
    virtual void GetPhysicalPageRect (GRect &r) = 0;
    virtual bool StartPage () = 0;
    virtual bool StartDoc (char *jobname = "GBasePrinter") = 0;
};

// Windows  port VPort
// Mac port VPort
// Postscript - just write to file



// Encapsulates the complete graphics system (screen drawing, picture files,
// printing, clipboard).
class GBasePort
{
public:
    GBasePort () { Device = devScreen; PenWidth = 1;};
    virtual ~GBasePort() {};
    virtual void DrawArc (const GPoint &pt, const int radius,
        const double startAngleDegrees, const double endAngleDegrees) = 0;
    virtual void DrawCircle (const GPoint &pt, const int radius) = 0;
    virtual void DrawLine (const int x1, const int y1, const int x2, const int y2) = 0;
    virtual void DrawLinePts (const GPoint &pt1, const GPoint &pt2)
        { DrawLine (pt1.GetX(), pt1.GetY(), pt2.GetX(), pt2.GetY()); };
    virtual void DrawRect (const GRect &r) = 0;
    virtual void DrawText (const int x, const int y, const char *s) = 0;
    
	// Display
    virtual GPortDevice GetCurrentDevice () { return Device; };
    virtual void GetDisplayRect (GRect &r) { r = DisplayRect; };
    virtual void SetDisplayRect (GRect &r) { DisplayRect = r; };
    
    // Pen
    virtual int  GetPenWidth () { return PenWidth; };
    virtual void SetPenWidth (int w) { PenWidth = w; };
    
    // Fonts
    virtual void SetCurrentFont (GBaseFont &font) = 0;
    
    // Pictures
    virtual void StartPicture (char *pictFileName) = 0;
    virtual void EndPicture () = 0;
    
    // Groups
    virtual void BeginGroup () = 0;
    virtual void EndGroup () = 0;
    
    // Printing
    virtual void GetPrintingRect (GRect &r) = 0;
	
    
protected:
    // list of fonts
    // printer class
    //pens

    int PenWidth;

    // Device info
    GPortDevice Device;
    GRect	DisplayRect;
};

// Mac
// Win
// Postscript

class GFilePort : public GBasePort
{
public:
    GFilePort () {};
protected:
    ofstream 		portStream;
};

class GPostscriptPort : public GFilePort
{
public:
    GPostscriptPort ();
    virtual ~GPostscriptPort () {};
    virtual void DrawArc (const GPoint &pt, const int radius,
            const double startAngleDegrees, const double endAngleDegrees);
    virtual void DrawCircle (const GPoint &pt, const int radius);
    virtual void DrawLine (const int x1, const int y1, const int x2, const int y2);
    virtual void DrawRect (const GRect &r);
	virtual void DrawFilledRect (const GRect &r, double greylevel); //greylevel of fill - 0 is black, 1 is white
    virtual void DrawText (const int x, const int y, const char *text);
    
    // Pen
    virtual void SetPenWidth (int w);
    
    
    // Fonts
    virtual void SetCurrentFont (GBaseFont &font);
    
    // Pictures
    virtual void StartPicture (char *pictFileName);
    virtual void EndPicture ();
    
    // Groups
    virtual void BeginGroup () {};
    virtual void EndGroup () {};
    
    // Printing
    virtual void GetPrintingRect (GRect &r);
	
	// Random
	virtual void AddPSCommands (char* c);  //allow direct access to postscript file - needed for nice spacing in histogram

protected:
    std::string 	DocumentFonts;
};

class GMacPort : public GBasePort
{
public:
    // Groups
    virtual void BeginGroup ();
    virtual void EndGroup ();
protected:
};

class SVGPort : public GFilePort
{
public:
    SVGPort ();
    virtual void DrawArc (const GPoint &pt, const int radius,
        const double startAngleDegrees, const double endAngleDegrees) {};
    virtual void DrawCircle (const GPoint &pt, const int radius);
    virtual void DrawLine (const int x1, const int y1, const int x2, const int y2);
    virtual void DrawRect (const GRect &r);
    virtual void DrawText (const int x, const int y, const char *text);

    // Pen
    virtual void SetPenWidth (int w) {};


    // Fonts
    virtual void SetCurrentFont (GBaseFont &font);

    // Pictures
    virtual void StartPicture (char *pictFileName);
    virtual void EndPicture ();

    // Groups
    virtual void BeginGroup () {};
    virtual void EndGroup () {};

    // Printing
    virtual void GetPrintingRect (GRect &r);
protected:
    std::string 	fontString;
};

class MVGPort : public GFilePort
{
public:
    MVGPort ();
    virtual void DrawArc (const GPoint &pt, const int radius,
        const double startAngleDegrees, const double endAngleDegrees) {};
    virtual void DrawCircle (const GPoint &pt, const int radius);
    virtual void DrawLine (const int x1, const int y1, const int x2, const int y2);
    virtual void DrawRect (const GRect &r);
    virtual void DrawText (const int x, const int y, const char *text);

    // Pen
    virtual void SetPenWidth (int w) {};

    // Fonts
    virtual void SetCurrentFont (GBaseFont &font);

    // Pictures
    virtual void StartPicture (char *pictFileName);
    virtual void EndPicture ();

    // Groups
    virtual void BeginGroup () {};
    virtual void EndGroup () {};

    // Printing
    virtual void GetPrintingRect (GRect &r);
protected:
    std::string 	fontString;
};

class ImageMapPort : public GFilePort
{
public:
    ImageMapPort ();
    virtual void DrawArc (const GPoint &pt, const int radius,
        const double startAngleDegrees, const double endAngleDegrees) {};
    virtual void DrawCircle (const GPoint &pt, const int radius);
    virtual void DrawLine (const int x1, const int y1, const int x2, const int y2);
    virtual void DrawRect (const GRect &r);
    virtual void DrawText (const int x, const int y, const char *text);

    // Pen
    virtual void SetPenWidth (int w) {};


    // Fonts
    virtual void SetCurrentFont (GBaseFont &font);

    // Pictures
    virtual void StartPicture (char *pictFileName);
    virtual void EndPicture ();

    // Groups
    virtual void BeginGroup () {};
    virtual void EndGroup () {};

    // Printing
    virtual void GetPrintingRect (GRect &r);
protected:
    std::string 	fontString;
};




#ifndef USE_VC2
extern GBasePort *Port; // for now
#endif

#ifdef __BORLANDC__
	// Redefine __MINMAX_DEFINED so Windows header files compile
	#ifndef __MINMAX_DEFINED
    		#define __MINMAX_DEFINED
	#endif
#endif


#endif