zeniba-nsxiv/window.c

109 lines
2.9 KiB
C
Raw Normal View History

2011-01-17 14:57:59 +01:00
/* sxiv: window.c
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
2011-01-17 16:42:49 +01:00
#include <stdlib.h>
#include <stdio.h>
#include <X11/Xutil.h>
2011-01-17 14:57:59 +01:00
#include "sxiv.h"
#include "window.h"
2011-01-17 16:42:49 +01:00
void win_open(win_t *win) {
win_env_t *e;
2011-01-17 16:42:49 +01:00
XClassHint *classhint;
XColor bgcol;
2011-01-17 19:53:48 +01:00
XSetWindowAttributes attr;
unsigned long mask;
2011-01-17 16:42:49 +01:00
2011-01-17 20:14:15 +01:00
if (!win)
2011-01-17 16:42:49 +01:00
return;
e = &win->env;
2011-01-17 16:42:49 +01:00
if (!(e->dpy = XOpenDisplay(NULL)))
2011-01-18 17:21:59 +01:00
DIE("could not open display");
2011-01-17 16:42:49 +01:00
e->scr = DefaultScreen(e->dpy);
e->scrw = DisplayWidth(e->dpy, e->scr);
e->scrh = DisplayHeight(e->dpy, e->scr);
2011-01-17 16:42:49 +01:00
e->vis = DefaultVisual(e->dpy, e->scr);
e->cmap = DefaultColormap(e->dpy, e->scr);
e->depth = DefaultDepth(e->dpy, e->scr);
2011-01-17 20:14:15 +01:00
if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
&bgcol, &bgcol))
2011-01-18 17:21:59 +01:00
DIE("could not allocate color: %s", BG_COLOR);
2011-01-17 16:42:49 +01:00
if (win->w > e->scrw)
win->w = e->scrw;
if (win->h > e->scrh)
win->h = e->scrh;
win->x = (e->scrw - win->w) / 2;
win->y = (e->scrh - win->h) / 2;
2011-01-17 16:42:49 +01:00
2011-01-17 19:53:48 +01:00
attr.backing_store = NotUseful;
attr.background_pixel = bgcol.pixel;
attr.save_under = False;
mask = CWBackingStore | CWBackPixel | CWSaveUnder;
win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
win->x, win->y, win->w, win->h, 0,
e->depth, InputOutput, e->vis, mask, &attr);
2011-01-17 16:42:49 +01:00
if (win->xwin == None)
2011-01-18 17:21:59 +01:00
DIE("could not create window");
2011-01-17 16:42:49 +01:00
XSelectInput(e->dpy, win->xwin,
StructureNotifyMask | ExposureMask | KeyPressMask);
2011-01-17 16:42:49 +01:00
if ((classhint = XAllocClassHint())) {
classhint->res_name = "sxvi";
classhint->res_class = "sxvi";
XSetClassHint(e->dpy, win->xwin, classhint);
2011-01-17 16:42:49 +01:00
XFree(classhint);
}
XMapWindow(e->dpy, win->xwin);
XFlush(e->dpy);
2011-01-17 16:42:49 +01:00
}
void win_close(win_t *win) {
2011-01-17 20:14:15 +01:00
if (!win)
2011-01-17 16:42:49 +01:00
return;
XDestroyWindow(win->env.dpy, win->xwin);
XCloseDisplay(win->env.dpy);
2011-01-17 16:42:49 +01:00
}
2011-01-17 19:50:46 +01:00
int win_configure(win_t *win, XConfigureEvent *cev) {
int changed;
2011-01-17 20:14:15 +01:00
if (!win)
2011-01-17 19:50:46 +01:00
return 0;
changed = win->x != cev->x || win->y != cev->y ||
win->w != cev->width || win->h != cev->height;
2011-01-17 19:50:46 +01:00
win->x = cev->x;
win->y = cev->y;
win->w = cev->width;
win->h = cev->height;
win->bw = cev->border_width;
return changed;
}