-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgwindow.h
More file actions
342 lines (270 loc) · 10.3 KB
/
gwindow.h
File metadata and controls
342 lines (270 loc) · 10.3 KB
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
//
// File "gwindow.h"
// Simple graphic window, based on Xlib primitives
//
#ifndef _GWINDOW_H
#define _GWINDOW_H
// Classes for simple 2-dimensional objects
#include "R2Graph.h"
// include the X library headers
extern "C" {
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
}
//===============================
class ListHeader {
public:
ListHeader* next;
ListHeader* prev;
public:
ListHeader():
next(0),
prev(0)
{
}
ListHeader(ListHeader* n, ListHeader* p):
next(n),
prev(p)
{
}
ListHeader(const ListHeader& h):
next(h.next),
prev(h.prev)
{
}
~ListHeader() {}
ListHeader& operator=(const ListHeader& h) {
next = h.next;
prev = h.prev;
return *this;
}
void link(ListHeader& h) {
next = &h;
h.prev = this;
}
};
class FontDescriptor: public ListHeader {
public:
Font font_id;
XFontStruct* font_struct;
FontDescriptor(Font id, XFontStruct* fstr):
ListHeader(),
font_id(id),
font_struct(fstr)
{}
};
const int DEFAULT_BORDER_WIDTH = 2;
class GWindow: public ListHeader {
public:
// Xlib objects:
// Display and screen are the same for all windows
static Display* m_Display;
static int m_Screen;
static Atom m_WMProtocolsAtom;
static Atom m_WMDeleteWindowAtom;
Window m_Window;
Pixmap m_Pixmap;
GC m_GC;
// Coordinates in window
I2Point m_WindowPosition; // Window position in screen coord
I2Rectangle m_IWinRect; // Window rectangle in (local) pixel coordinates
R2Rectangle m_RWinRect; // Window rectangle in inner (real) coordinates
I2Point m_ICurPos; // Current position, integer
R2Point m_RCurPos; // real
double m_xcoeff; // Optimization: Coeff. for real->integer conversion
double m_ycoeff;
char m_WindowTitle[128];
// Window state
bool m_WindowCreated;
// Static members:
// List of all windows
static int m_NumWindows;
static int m_NumCreatedWindows;
static ListHeader m_WindowList;
static ListHeader m_FontList;
protected:
// Background, foreground
unsigned long m_bgPixel;
unsigned long m_fgPixel;
const char* m_bgColorName;
const char* m_fgColorName;
// Border width
int m_BorderWidth;
// Clip rectangle
XRectangle m_ClipRectangle;
bool m_BeginExposeSeries;
public:
GWindow();
GWindow(const I2Rectangle& frameRect, const char *title = 0);
GWindow(
const I2Rectangle& frameRect,
const R2Rectangle& coordRect,
const char *title = 0
);
virtual ~GWindow();
// The "createWindow" method uses the m_IWinRect member to
// define the position and size of the window, and the
// m_WindowTitle member to set the window title.
// All parameters have their default values, so the method can
// be called without any parameters: createWindow()
void createWindow(
GWindow* parentWindow = 0, // parent window
int borderWidth = DEFAULT_BORDER_WIDTH, // border width
unsigned int wndClass = InputOutput, // or InputOnly, CopyFromParent
Visual* visual = CopyFromParent, //
unsigned long attributesValueMask = 0, // which attributes are defined
XSetWindowAttributes* attributes = 0 // attributes structure
);
void createWindow(
const I2Rectangle& frameRect,
const char *title = 0,
GWindow* parentWindow = 0,
int borderWidth = DEFAULT_BORDER_WIDTH
);
void createWindow(
const I2Rectangle& frameRect,
const R2Rectangle& coordRect,
const char *title = 0,
GWindow* parentWindow = 0,
int borderWidth = DEFAULT_BORDER_WIDTH
);
static bool initX();
static void closeX();
static int screenMaxX();
static int screenMaxY();
static void releaseFonts();
private:
static GWindow* findWindow(Window w);
static FontDescriptor* findFont(Font fontID);
static void removeFontDescriptor(FontDescriptor* fd);
static void addFontDescriptor(
Font fontID, XFontStruct* fontStructure
);
public:
void drawFrame();
void setCoordinates(double xmin, double ymin, double xmax, double ymax);
void setCoordinates(const R2Rectangle& coordRect);
void setCoordinates(const R2Point& leftBottom, const R2Point& rightTop);
double getXMin() const { return m_RWinRect.getXMin(); }
double getXMax() const { return m_RWinRect.getXMax(); }
double getYMin() const { return m_RWinRect.getYMin(); }
double getYMax() const { return m_RWinRect.getYMax(); }
void drawAxes(
const char *axesColorName = 0,
bool drawGrid = false,
const char *gridColorName = 0,
bool offscreen = false
);
I2Rectangle getWindowRect() const { return m_IWinRect; }
R2Rectangle getCoordRect() const { return m_RWinRect; }
void moveTo(const I2Point& p);
void moveTo(const R2Point& p);
void moveTo(int x, int y);
void moveTo(double x, double y);
void moveRel(const I2Vector& p);
void moveRel(const R2Vector& p);
void moveRel(int x, int y);
void moveRel(double x, double y);
void drawLineTo(const I2Point& p, bool offscreen = false);
void drawLineTo(const R2Point& p, bool offscreen = false);
void drawLineTo(int x, int y, bool offscreen = false);
void drawLineTo(double x, double y, bool offscreen = false);
void drawLineRel(const I2Vector& p, bool offscreen = false);
void drawLineRel(const R2Vector& p, bool offscreen = false);
void drawLineRel(int x, int y, bool offscreen = false);
void drawLineRel(double x, double y, bool offscreen = false);
void drawLine(const I2Point& p1, const I2Point& p2, bool offscreen = false);
void drawLine(const I2Point& p, const I2Vector& v, bool offscreen = false);
void drawLine(int x1, int y1, int x2, int y2, bool offscreen = false);
void drawLine(const R2Point& p1, const R2Point& p2, bool offscreen = false);
void drawLine(const R2Point& p, const R2Vector& v, bool offscreen = false);
void drawLine(double x1, double y1, double x2, double y2, bool offscreen = false);
void drawEllipse(const I2Rectangle&, bool offscreen = false);
void drawEllipse(const R2Rectangle&, bool offscreen = false);
void drawCircle(const I2Point& center, int radius, bool offscreen = false);
void drawCircle(const R2Point& center, double radius, bool offscreen = false);
void drawString(int x, int y, const char *str, int len = (-1), bool offscreen = false);
void drawString(const I2Point& p, const char *str, int len = (-1), bool offscreen = false);
void drawString(const R2Point& p, const char *str, int len = (-1), bool offscreen = false);
void drawString(double x, double y, const char *str, int len = (-1), bool offscreen = false);
// The following methods should be called before "createWindow"
void setBgColorName(const char* colorName);
void setFgColorName(const char* colorName);
void setLineAttributes(
unsigned int line_width, int line_style,
int cap_style, int join_style
);
void setLineWidth(unsigned int line_width);
unsigned long allocateColor(const char *colorName);
void setBackground(unsigned long bg);
void setBackground(const char *colorName);
void setForeground(unsigned long fg);
void setForeground(const char *colorName);
unsigned long getBackground() const { return m_bgPixel; }
unsigned long getForeground() const { return m_fgPixel; }
void fillRectangle(const I2Rectangle&, bool offscreen = false);
void fillRectangle(const R2Rectangle&, bool offscreen = false);
void fillPolygon(const R2Point* points, int numPoints, bool offscreen = false);
void fillPolygon(const I2Point* points, int numPoints, bool offscreen = false);
void fillEllipse(const I2Rectangle&, bool offscreen = false);
void fillEllipse(const R2Rectangle&, bool offscreen = false);
void redraw();
void redrawRectangle(const R2Rectangle&);
void redrawRectangle(const I2Rectangle&);
void setWindowTitle(const char* title);
I2Point map(const R2Point& p) const;
I2Point map(double x, double y) const;
int mapX(double x) const;
int mapY(double y) const;
R2Point invMap(const I2Point& p) const;
void recalculateMap();
// Font methods
Font loadFont(const char* fontName, XFontStruct **fontStruct = 0);
void unloadFont(Font fontID);
XFontStruct* queryFont(Font fontID) const;
void setFont(Font fontID);
// Depths supported
bool supportsDepth24() const;
bool supportsDepth32() const;
bool supportsDepth(int d) const;
// Work with offscreen buffer
bool createOffscreenBuffer();
void swapBuffers();
// Callbacks:
virtual void onExpose(XEvent& event);
virtual void onResize(XEvent& event); // event.xconfigure.width, height
virtual void onKeyPress(XEvent& event);
virtual void onButtonPress(XEvent& event);
virtual void onButtonRelease(XEvent& event);
virtual void onMotionNotify(XEvent& event);
virtual void onCreateNotify(XEvent& event);
virtual void onDestroyNotify(XEvent& event);
virtual void onFocusIn(XEvent& event);
virtual void onFocusOut(XEvent& event);
// Message from Window Manager, such as "Close Window"
virtual void onClientMessage(XEvent& event);
// This method is called from the base implementation of
// method "onClientMessage". It allows a user application
// to save all unsaved data and to do other operations before
// closing a window, when a user pressed the closing box in the upper
// right corner of a window. The application supplied method should return
// "true" to close the window or "false" to leave the window open.
virtual bool onWindowClosing();
// Message loop
static bool getNextEvent(XEvent& e);
static void dispatchEvent(XEvent& e);
static void messageLoop(GWindow* = 0);
// For dialog windows
void doModal();
// Some methods implementing X-primitives
virtual void destroyWindow();
void mapRaised();
void raise();
private:
int clip(const R2Point& p1, const R2Point& p2,
R2Point& c1, R2Point& c2);
};
#endif
//
// End of file "gwindow.h"