-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.cc
78 lines (60 loc) · 2.21 KB
/
window.cc
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
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <cstdlib>
#include <iostream>
#include <string>
#include <unistd.h>
#include "window.h"
using namespace std;
Xwindow::Xwindow(int width, int height): width{width}, height{height} {
d = XOpenDisplay(NULL);
if (d == NULL) exit(1);
s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, height, width, 1, BlackPixel(d, s), WhitePixel(d, s));
XSelectInput(d, w, ExposureMask | KeyPressMask);
XMapRaised(d, w);
Pixmap pix = XCreatePixmap(d, w, height, width, DefaultDepth(d, DefaultScreen(d)));
gc = XCreateGC(d, pix, 0, (XGCValues *)0);
XFlush(d);
XFlush(d);
// Colour setup:
XColor xcolour;
Colormap cmap;
char color_vals[8][20] = {"white", "black", "darkturquoise", "deepskyblue", "royalblue", "indianred", "lightcoral", "firebrick"};
cmap = DefaultColormap(d, DefaultScreen(d));
for (int i = 0; i < 8; ++i) {
XParseColor(d, cmap, color_vals[i], &xcolour);
XAllocColor(d, cmap, &xcolour);
colours[i] = xcolour.pixel;
}
XSetForeground(d, gc, colours[Black]);
// Make window non-resizeable:
XSizeHints hints;
hints.flags = (USPosition | PSize | PMinSize | PMaxSize);
hints.width = hints.base_width = hints.min_width = hints.max_width = height;
hints.height = hints.base_height = hints.min_height = hints.max_height = width;
XSetNormalHints(d, w, &hints);
Atom WW_DELETE_WINDOW = XInternAtom(d, "WW_DELETE_WINDOW", False);
XSetWMProtocols(d, w, &WW_DELETE_WINDOW, True);
XSynchronize(d, True);
usleep(1000);
// Make sure we don't race against the Window being shown:
XEvent ev;
while (1) {
XNextEvent(d, &ev);
if (ev.type == Expose) break;
else if (ev.type == ClientMessage) break;
}
}
Xwindow::~Xwindow() {
XFreeGC(d, gc);
XCloseDisplay(d);
}
void Xwindow::fillRectangle(int x, int y, int width, int height, int colour) {
XSetForeground(d, gc, colours[colour]);
XFillRectangle(d, w, gc, x, y, width, height);
XSetForeground(d, gc, colours[Black]);
}
void Xwindow::drawString(int x, int y, string msg) {
XDrawString(d, w, DefaultGC(d, s), x, y, msg.c_str(), msg.length());
}