Krolyxon
Commit 376c0d9

Branches

Refs →

nsxivwindow.c

142 lines · 3.4 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
38 if (!(e->dpy = XOpenDisplay(NULL)))
39 DIE("could not open display");
40
41 e->scr = DefaultScreen(e->dpy);
42 e->scrw = DisplayWidth(e->dpy, e->scr);
43 e->scrh = DisplayHeight(e->dpy, e->scr);
44
45 e->vis = DefaultVisual(e->dpy, e->scr);
46 e->cmap = DefaultColormap(e->dpy, e->scr);
47 e->depth = DefaultDepth(e->dpy, e->scr);
48
49 if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
50 &bgcol, &bgcol))
51 DIE("could not allocate color: %s", BG_COLOR);
52
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 win->pm = 0;
70
71 gcval.foreground = bgcol.pixel;
72 win->bgc = XCreateGC(e->dpy, win->xwin, GCForeground, &gcval);
73
74 win_set_title(win, "sxiv");
75
76 if ((classhint = XAllocClassHint())) {
77 classhint->res_name = "sxiv";
78 classhint->res_class = "sxiv";
79 XSetClassHint(e->dpy, win->xwin, classhint);
80 XFree(classhint);
81 }
82
83 XMapWindow(e->dpy, win->xwin);
84 XFlush(e->dpy);
85}
86
87void win_close(win_t *win) {
88 if (!win)
89 return;
90
91 XDestroyWindow(win->env.dpy, win->xwin);
92 XCloseDisplay(win->env.dpy);
93}
94
95void win_set_title(win_t *win, const char *title) {
96 if (!win)
97 return;
98
99 if (!title)
100 title = "sxiv";
101
102 XStoreName(win->env.dpy, win->xwin, title);
103 XSetIconName(win->env.dpy, win->xwin, title);
104}
105
106int win_configure(win_t *win, XConfigureEvent *cev) {
107 int changed;
108
109 if (!win)
110 return 0;
111
112 changed = win->x != cev->x || win->y != cev->y ||
113 win->w != cev->width || win->h != cev->height;
114 win->x = cev->x;
115 win->y = cev->y;
116 win->w = cev->width;
117 win->h = cev->height;
118 win->bw = cev->border_width;
119 return changed;
120}
121
122void win_clear(win_t *win) {
123 win_env_t *e;
124
125 if (!win)
126 return;
127
128 e = &win->env;
129
130 if (win->pm)
131 XFreePixmap(e->dpy, win->pm);
132 win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
133 XFillRectangle(e->dpy, win->pm, win->bgc, 0, 0, e->scrw, e->scrh);
134}
135
136void win_draw(win_t *win) {
137 if (!win)
138 return;
139
140 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
141 XClearWindow(win->env.dpy, win->xwin);
142}