2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation. //
7 // This program is distributed in the hope that it will be useful, but //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
10 // General Public License for more details. //
12 // You should have received a copy of the GNU General Public License //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
15 // Written by and Copyright (C) Francois Fleuret //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
17 ///////////////////////////////////////////////////////////////////////////
19 #include "simple_window.h"
21 SimpleWindow::SimpleWindow(const char *name, int w, int h) {
23 width = w; height = h;
25 dpy = XOpenDisplay(0);
29 Visual *v = XDefaultVisual(dpy, DefaultScreen(dpy));
31 blue_mask = v->blue_mask;
32 green_mask = v->green_mask;
33 red_mask = v->red_mask;
35 if(blue_mask == 0 || green_mask == 0 || red_mask == 0) {
36 std::cerr << "Can not deal with the colors on that display.\n";
39 // We compute how many bits we have for each component, and
40 // compute the corresponding mask
42 while(!(red_mask & 1)) {
43 red_mask = red_mask >> 1;
44 red_shift = red_shift << 1;
48 while(!(green_mask & 1)) {
49 green_mask = green_mask >> 1;
50 green_shift = green_shift << 1;
54 while(!(blue_mask & 1)) {
55 blue_mask = blue_mask >> 1;
56 blue_shift = blue_shift << 1;
59 gc = DefaultGC(dpy, DefaultScreen(dpy));
61 XSetWindowAttributes xswa;
63 pixmap = XCreatePixmap(dpy, DefaultRootWindow(dpy),
64 width, height, DisplayPlanes(dpy, DefaultScreen(dpy)));
66 xswa.background_pixmap = pixmap;
67 xswa.event_mask = NoEventMask;
68 xswa.backing_store = Always;
70 window = XCreateWindow(dpy, DefaultRootWindow(dpy), 0, 0, width, height,
71 0, 0, InputOutput, CopyFromParent,
72 CWBackPixmap | CWEventMask | CWBackingStore,
75 XSizeHints size_hints;
76 size_hints.flags = PMinSize | PMaxSize;
77 size_hints.min_width = width;
78 size_hints.min_height = height;
79 size_hints.max_width = width;
80 size_hints.max_height = height;
81 XSetNormalHints(dpy, window, &size_hints);
83 XStoreName(dpy, window, name);
85 XSetState(dpy, gc, 0, 0, GXcopy, AllPlanes);
86 XFillRectangle(dpy, pixmap, gc, 0, 0, width, height);
91 SimpleWindow::~SimpleWindow() {
92 XUnmapWindow(dpy, window);
93 XDestroyWindow(dpy, window);
97 int SimpleWindow::get_width() {
101 int SimpleWindow::get_height() {
105 void SimpleWindow::map() {
106 XMapWindow(dpy, window);
110 void SimpleWindow::unmap() {
111 XUnmapWindow(dpy, window);
115 void SimpleWindow::color(float red, float green, float blue) {
116 // We work under the assumption that the bits come in the
117 // red/green/blue order with the corresponding masks
119 ((unsigned int) ( red * red_mask)) * red_shift
120 + ((unsigned int) (green * green_mask)) * green_shift
121 + ((unsigned int) ( blue * blue_mask)) * blue_shift,
122 0, GXcopy, AllPlanes);
125 void SimpleWindow::draw_point(int x, int y) {
126 XDrawPoint(dpy, pixmap, gc, x, y);
129 void SimpleWindow::draw_line(int x1, int y1, int x2, int y2) {
130 XDrawLine(dpy, pixmap, gc, x1, y1, x2, y2);
133 void SimpleWindow::draw_circle(int x, int y, int r) {
134 XDrawArc(dpy, pixmap, gc, x-r, y-r, 2*r, 2*r, 0, 360*64);
137 void SimpleWindow::draw_text(char *s, int x, int y) {
138 XDrawString(dpy, pixmap, gc, x, y, s, strlen(s));
141 void SimpleWindow::fill_rectangle(int x, int y, int w, int h) {
142 XFillRectangle(dpy, pixmap, gc, x, y, w, h);
145 void SimpleWindow::show() {
146 XCopyArea(dpy, pixmap, window, gc, 0, 0, width, height, 0, 0);
150 void SimpleWindow::fill() {
151 XFillRectangle(dpy, pixmap, gc, 0, 0, width, height);