-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbwindow.cpp
218 lines (186 loc) · 4.76 KB
/
bwindow.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include "bwindow.h"
bwindow::bwindow()
{
width = MIN_WIDTH;
height = MIN_HEIGHT;
strcpy(window_name,"Default Window");
strcpy(icon_name,"Defwin");
line_width = 1;
border_width = 4;
strcpy(fontname,"9x15");
}
bwindow::bwindow(int w,int h)
{
width = w > MIN_WIDTH ? w : MIN_WIDTH;
height = h > MIN_HEIGHT ? h : MIN_HEIGHT;
strcpy(window_name,"Default Window");
strcpy(icon_name,"Defwin");
line_width = 1;
border_width = 4;
strcpy(fontname,"lucidasans-12");
}
bwindow::bwindow(int w,int h, char * name)
{
width = w > MIN_WIDTH ? w : MIN_WIDTH;
height = h > MIN_HEIGHT ? h : MIN_HEIGHT;
strcpy(window_name,name);
strcpy(icon_name,name);
line_width = 1;
border_width = 4;
strcpy(fontname,"9x15");
}
bwindow::bwindow(int w,int h, char * name,char * font)
{
width = w > MIN_WIDTH ? w : MIN_WIDTH;
height = h > MIN_HEIGHT ? h : MIN_HEIGHT;
strcpy(window_name,name);
strcpy(icon_name,name);
strcpy(fontname,font);
line_width = 1;
border_width = 4;
}
bwindow::~bwindow()
{
clean_up();
}
int bwindow::init()
{
Pixmap icon_pixmap;
if ((display=XOpenDisplay(0x0)) == NULL)
{
return ERR_BAD_DISPLAY;
}
screen = DefaultScreen(display);
display_width = DisplayWidth(display,screen);
display_height = DisplayHeight(display,screen);
win = XCreateSimpleWindow(display, RootWindow(display,screen),
x, y, width, height, border_width,
BlackPixel(display,screen),
WhitePixel(display,screen));
size_hints.flags = PPosition | PSize | PMinSize;
size_hints.x = x;
size_hints.y = y;
size_hints.width = width;
size_hints.height = height;
size_hints.min_width = MIN_WIDTH;
size_hints.min_height = MIN_HEIGHT;
XSetStandardProperties(display, win, window_name, icon_name,0 ,0, icon_pixmap, &size_hints);
XSelectInput(display, win, ExposureMask |
KeyPressMask |
ButtonPressMask |
StructureNotifyMask);
if (load_font()!=0)
return ERR_BAD_FONT;
get_gc();
return 0;
}
int bwindow::load_font()
{
if ((font_info=XLoadQueryFont(display,fontname)) == NULL)
{
return -1;
}
else
return 0;
}
int bwindow::reload_font(char * font_name)
{
strcpy(fontname,font_name);
return load_font();
}
void bwindow::clean_up()
{
XUnloadFont(display, font_info->fid);
XFreeGC(display, gc);
XCloseDisplay(display);
}
void bwindow::map()
{
XMapWindow(display,win);
}
int bwindow::get_gc()
{
unsigned long valuemask = 0;
XGCValues values;
gc = XCreateGC(display, win, valuemask, &values);
XSetFont(display, gc, font_info->fid);
XSetForeground(display, gc, 0x0 /* BlackPixel(display,screen) */);
XSetLineAttributes(display, gc, line_width, LineSolid, CapButt, JoinMiter);
return 0;
}
int bwindow::parse_event()
{
while((XPending(display) > 0))
{
XNextEvent(display, &report);
switch (report.type)
{
case Expose:
return BEXPOSE;
case ConfigureNotify:
width = report.xconfigure.width;
height = report.xconfigure.height;
if (width < size_hints.min_width)
width = size_hints.min_width;
if (height < size_hints.min_height)
height = size_hints.min_height;
return BCONFIGURE;
break;
case ButtonPress:
return BBPRESS;
break;
case KeyPress:
strcpy(lastkey,XKeysymToString(XKeycodeToKeysym(display,report.xkey.keycode,0)));
if (!strcmp(lastkey,"Escape"))
exit(0);
return BKPRESS;
break;
default:
break;
}
}
}
void bwindow::draw_text(int x, int y, unsigned int color, const char * text, int len)
{
XSetForeground(display, gc,color);
XDrawString(display, win, gc, x, y, text, len);
XSetForeground(display, gc,0x0);
}
void bwindow::draw_square(int x1,int y1,int x2,int y2,unsigned int color)
{
XSetForeground(display, gc,color);
XDrawRectangle(display, win, gc, x1, y1,x2-x1, y2-y1);
XSetForeground(display, gc,0x0);
}
void bwindow::draw_line(int x1,int y1,int x2,int y2,unsigned int color)
{
XSetForeground(display, gc,color);
XDrawLine(display, win, gc, x1, y1, x2,y2);
XSetForeground(display, gc,0x0);
}
void bwindow::draw_point(int x, int y, unsigned int color)
{
XSetForeground(display, gc,color);
XDrawPoint(display, win, gc, x, y);
XSetForeground(display, gc,0x0);
}
void bwindow::draw_fsquare(int x1,int y1,int x2,int y2,unsigned int color)
{
XSetForeground(display, gc,color);
XFillRectangle(display, win, gc, x1, y1, x2-x1, y2-y1);
XSetForeground(display, gc,0x0);
}
char * bwindow::get_lastkey()
{
return lastkey;
}
unsigned int bwindow::get_width()
{
return width;
}
unsigned int bwindow::get_height()
{
return height;
}