milsko/src/x11.c

123 lines
3 KiB
C
Raw Normal View History

/* $Id$ */
#include <Milsko/Milsko.h>
static unsigned long mask = ExposureMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask;
MilskoLL MilskoLLCreate(MilskoLL parent, int x, int y, int width, int height) {
MilskoLL r;
Window p;
Window root;
unsigned int border, depth;
r = malloc(sizeof(*r));
MilskoLLCreateCommon(r);
if(parent == NULL) {
r->display = XOpenDisplay(NULL);
p = XRootWindow(r->display, XDefaultScreen(r->display));
} else {
r->display = parent->display;
p = parent->window;
}
r->window = XCreateSimpleWindow(r->display, p, x, y, width, height, 0, 0, WhitePixel(r->display, XDefaultScreen(r->display)));
r->colormap = DefaultColormap(r->display, XDefaultScreen(r->display));
r->gc = XCreateGC(r->display, r->window, 0, 0);
XSelectInput(r->display, r->window, mask);
XMapWindow(r->display, r->window);
return r;
}
void MilskoLLDestroy(MilskoLL handle) {
MilskoLLDestroyCommon(handle);
XFreeGC(handle->display, handle->gc);
XDestroyWindow(handle->display, handle->window);
free(handle);
}
void MilskoLLPolygon(MilskoLL handle, MilskoPoint* points, int points_count, MilskoLLColor color) {
int i;
XPoint* p = malloc(sizeof(*p) * points_count);
XSetForeground(handle->display, handle->gc, color->pixel);
for(i = 0; i < points_count; i++) {
p[i].x = points[i].x;
p[i].y = points[i].y;
}
XFillPolygon(handle->display, handle->window, handle->gc, p, points_count, Convex, CoordModeOrigin);
free(p);
}
MilskoLLColor MilskoLLAllocColor(MilskoLL handle, int r, int g, int b) {
MilskoLLColor c = malloc(sizeof(*c));
XColor xc;
if(r > 255) r = 255;
if(g > 255) g = 255;
if(b > 255) b = 255;
if(r < 0) r = 0;
if(g < 0) g = 0;
if(b < 0) b = 0;
xc.red = 256 * r;
xc.green = 256 * g;
xc.blue = 256 * b;
XAllocColor(handle->display, handle->colormap, &xc);
c->pixel = xc.pixel;
c->red = r;
c->green = g;
c->blue = b;
return c;
}
void MilskoLLGetXYWH(MilskoLL handle, int* x, int* y, unsigned int* w, unsigned int* h) {
Window root;
unsigned int border, depth;
XGetGeometry(handle->display, handle->window, &root, x, y, w, h, &border, &depth);
}
void MilskoLLSetXY(MilskoLL handle, int x, int y) {
XMoveWindow(handle->display, handle->window, x, y);
}
void MilskoLLSetWH(MilskoLL handle, int w, int h) {
XResizeWindow(handle->display, handle->window, w, h);
}
void MilskoLLFreeColor(MilskoLLColor color) {
free(color);
}
int MilskoLLPending(MilskoLL handle) {
XEvent ev;
if(XCheckWindowEvent(handle->display, handle->window, mask, &ev)) {
XPutBackEvent(handle->display, &ev);
return 1;
}
return 0;
}
void MilskoLLNextEvent(MilskoLL handle) {
XEvent ev;
if(XCheckWindowEvent(handle->display, handle->window, mask, &ev)) {
if(ev.type == Expose) {
MilskoLLDispatch(handle, draw);
}
}
}
void MilskoLLSleep(int ms) {
usleep(ms * 1000);
}
void MilskoLLSetTitle(MilskoLL handle, const char* title) {
XSetStandardProperties(handle->display, handle->window, title, "Milsko Widget Toolkit", None, (char**)NULL, 0, NULL);
}