| 1 | /* sxiv: window.c |
| 2 | * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | #include <X11/Xutil.h> |
| 23 | |
| 24 | #include "sxiv.h" |
| 25 | #include "window.h" |
| 26 | |
| 27 | void win_open(win_t *win) { |
| 28 | win_env_t *e; |
| 29 | XClassHint *classhint; |
| 30 | XColor bgcol; |
| 31 | XSetWindowAttributes attr; |
| 32 | unsigned long mask; |
| 33 | |
| 34 | if (!win) |
| 35 | return; |
| 36 | |
| 37 | e = &win->env; |
| 38 | |
| 39 | if (!(e->dpy = XOpenDisplay(NULL))) |
| 40 | DIE("could not open display"); |
| 41 | |
| 42 | e->scr = DefaultScreen(e->dpy); |
| 43 | e->scrw = DisplayWidth(e->dpy, e->scr); |
| 44 | e->scrh = DisplayHeight(e->dpy, e->scr); |
| 45 | |
| 46 | e->vis = DefaultVisual(e->dpy, e->scr); |
| 47 | e->cmap = DefaultColormap(e->dpy, e->scr); |
| 48 | e->depth = DefaultDepth(e->dpy, e->scr); |
| 49 | |
| 50 | if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR, |
| 51 | &bgcol, &bgcol)) |
| 52 | DIE("could not allocate color: %s", BG_COLOR); |
| 53 | |
| 54 | if (win->w > e->scrw) |
| 55 | win->w = e->scrw; |
| 56 | if (win->h > e->scrh) |
| 57 | win->h = e->scrh; |
| 58 | win->x = (e->scrw - win->w) / 2; |
| 59 | win->y = (e->scrh - win->h) / 2; |
| 60 | |
| 61 | attr.backing_store = NotUseful; |
| 62 | attr.background_pixel = bgcol.pixel; |
| 63 | attr.save_under = False; |
| 64 | mask = CWBackingStore | CWBackPixel | CWSaveUnder; |
| 65 | |
| 66 | win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr), |
| 67 | win->x, win->y, win->w, win->h, 0, |
| 68 | e->depth, InputOutput, e->vis, mask, &attr); |
| 69 | if (win->xwin == None) |
| 70 | DIE("could not create window"); |
| 71 | |
| 72 | XSelectInput(e->dpy, win->xwin, |
| 73 | StructureNotifyMask | ExposureMask | KeyPressMask); |
| 74 | |
| 75 | if ((classhint = XAllocClassHint())) { |
| 76 | classhint->res_name = "sxvi"; |
| 77 | classhint->res_class = "sxvi"; |
| 78 | XSetClassHint(e->dpy, win->xwin, classhint); |
| 79 | XFree(classhint); |
| 80 | } |
| 81 | |
| 82 | XMapWindow(e->dpy, win->xwin); |
| 83 | XFlush(e->dpy); |
| 84 | } |
| 85 | |
| 86 | void win_close(win_t *win) { |
| 87 | if (!win) |
| 88 | return; |
| 89 | |
| 90 | XDestroyWindow(win->env.dpy, win->xwin); |
| 91 | XCloseDisplay(win->env.dpy); |
| 92 | } |
| 93 | |
| 94 | int win_configure(win_t *win, XConfigureEvent *cev) { |
| 95 | int changed; |
| 96 | |
| 97 | if (!win) |
| 98 | return 0; |
| 99 | |
| 100 | changed = win->x != cev->x || win->y != cev->y || |
| 101 | win->w != cev->width || win->h != cev->height; |
| 102 | win->x = cev->x; |
| 103 | win->y = cev->y; |
| 104 | win->w = cev->width; |
| 105 | win->h = cev->height; |
| 106 | win->bw = cev->border_width; |
| 107 | return changed; |
| 108 | } |