Krolyxon
Commit 6480a9d

Branches

Refs →

nsxivwindow.c

141 lines · 3.5 KB · C

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
27void win_open(win_t *win) {
28 win_env_t *e;
29 XClassHint *classhint;
30 XColor bgcol;
31 XGCValues gcval;
32
33 if (!win)
34 return;
35
36 e = &win->env;
37 if (!(e->dpy = XOpenDisplay(NULL)))
38 DIE("could not open display");
39
40 e->scr = DefaultScreen(e->dpy);
41 e->scrw = DisplayWidth(e->dpy, e->scr);
42 e->scrh = DisplayHeight(e->dpy, e->scr);
43 e->vis = DefaultVisual(e->dpy, e->scr);
44 e->cmap = DefaultColormap(e->dpy, e->scr);
45 e->depth = DefaultDepth(e->dpy, e->scr);
46
47 if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
48 &bgcol, &bgcol))
49 DIE("could not allocate color: %s", BG_COLOR);
50
51 win->w = WIN_WIDTH;
52 win->h = WIN_HEIGHT;
53 if (win->w > e->scrw)
54 win->w = e->scrw;
55 if (win->h > e->scrh)
56 win->h = e->scrh;
57 win->x = (e->scrw - win->w) / 2;
58 win->y = (e->scrh - win->h) / 2;
59
60 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
61 win->x, win->y, win->w, win->h, 0,
62 e->depth, InputOutput, e->vis, 0, None);
63 if (win->xwin == None)
64 DIE("could not create window");
65
66 XSelectInput(e->dpy, win->xwin,
67 StructureNotifyMask | KeyPressMask);
68
69 gcval.foreground = bgcol.pixel;
70 win->bgc = XCreateGC(e->dpy, win->xwin, GCForeground, &gcval);
71 win->pm = 0;
72
73 win_set_title(win, "sxiv");
74
75 if ((classhint = XAllocClassHint())) {
76 classhint->res_name = "sxiv";
77 classhint->res_class = "sxiv";
78 XSetClassHint(e->dpy, win->xwin, classhint);
79 XFree(classhint);
80 }
81
82 XMapWindow(e->dpy, win->xwin);
83 XFlush(e->dpy);
84}
85
86void 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
94void win_set_title(win_t *win, const char *title) {
95 if (!win)
96 return;
97
98 if (!title)
99 title = "sxiv";
100
101 XStoreName(win->env.dpy, win->xwin, title);
102 XSetIconName(win->env.dpy, win->xwin, title);
103}
104
105int win_configure(win_t *win, XConfigureEvent *cev) {
106 int changed;
107
108 if (!win)
109 return 0;
110
111 changed = win->x != cev->x || win->y != cev->y ||
112 win->w != cev->width || win->h != cev->height;
113 win->x = cev->x;
114 win->y = cev->y;
115 win->w = cev->width;
116 win->h = cev->height;
117 win->bw = cev->border_width;
118 return changed;
119}
120
121void win_clear(win_t *win) {
122 win_env_t *e;
123
124 if (!win)
125 return;
126
127 e = &win->env;
128
129 if (win->pm)
130 XFreePixmap(e->dpy, win->pm);
131 win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
132 XFillRectangle(e->dpy, win->pm, win->bgc, 0, 0, e->scrw, e->scrh);
133}
134
135void win_draw(win_t *win) {
136 if (!win)
137 return;
138
139 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
140 XClearWindow(win->env.dpy, win->xwin);
141}