Krolyxon
Commit 8339bb7

Branches

Refs →

nsxiv8339bb7

New key mapping: W, resize window to fit image

Author
Bert
Date
2011-02-04 00:25 +0100
Parent
096c0ed
Commit
8339bb7ee7fe4a2598a30e3c4d0cef42329ee5aa

+63 −23

5 files changed

  1. M Makefile +1 −1
  2. M image.h +2 −0
  3. M main.c +4 −0
  4. M window.c +53 −20
  5. M window.h +3 −2
M Makefile View
@@ -1,6 +1,6 @@
11 all: sxiv
22
3−VERSION=git-20110203
3+VERSION=git-20110204
44
55 CC?=gcc
66 PREFIX?=/usr/local
M image.h View
@@ -37,9 +37,11 @@ typedef enum pandir_e {
3737 typedef struct img_s {
3838 float zoom;
3939 scalemode_t scalemode;
40+
4041 unsigned char re;
4142 unsigned char checkpan;
4243 unsigned char aa;
44+
4345 int x;
4446 int y;
4547 int w;
M main.c View
@@ -329,6 +329,10 @@ void on_keypress(XKeyEvent *kev) {
329329 win_toggle_fullscreen(&win);
330330 /* render on next configurenotify */
331331 break;
332+ case XK_W:
333+ if ((changed = win_resize(&win, img.w * img.zoom, img.h * img.zoom)))
334+ img.checkpan = 1;
335+ break;
332336
333337 /* miscellaneous */
334338 case XK_a:
M window.c View
@@ -31,10 +31,23 @@ static Cursor hand;
3131
3232 static GC bgc;
3333
34+void win_set_sizehints(win_t *win) {
35+ XSizeHints sizehints;
36+
37+ if (!win)
38+ return;
39+
40+ sizehints.flags = PMinSize | PMaxSize;
41+ sizehints.min_width = win->w;
42+ sizehints.max_width = win->w;
43+ sizehints.min_height = win->h;
44+ sizehints.max_height = win->h;
45+ XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
46+}
47+
3448 void win_open(win_t *win) {
3549 win_env_t *e;
3650 XClassHint classhint;
37− XSizeHints sizehints;
3851 XColor bgcol;
3952 int gmask;
4053
@@ -104,14 +117,8 @@ void win_open(win_t *win) {
104117 classhint.res_class = "sxiv";
105118 XSetClassHint(e->dpy, win->xwin, &classhint);
106119
107− if (options->fixed) {
108− sizehints.flags = PMinSize | PMaxSize;
109− sizehints.min_width = win->w;
110− sizehints.max_width = win->w;
111− sizehints.min_height = win->h;
112− sizehints.max_height = win->h;
113− XSetWMNormalHints(e->dpy, win->xwin, &sizehints);
114− }
120+ if (options->fixed)
121+ win_set_sizehints(win);
115122
116123 XMapWindow(e->dpy, win->xwin);
117124 XFlush(e->dpy);
@@ -133,17 +140,6 @@ void win_close(win_t *win) {
133140 XCloseDisplay(win->env.dpy);
134141 }
135142
136−void win_set_title(win_t *win, const char *title) {
137− if (!win)
138− return;
139−
140− if (!title)
141− title = "sxiv";
142−
143− XStoreName(win->env.dpy, win->xwin, title);
144− XSetIconName(win->env.dpy, win->xwin, title);
145−}
146−
147143 int win_configure(win_t *win, XConfigureEvent *c) {
148144 int changed;
149145
@@ -161,6 +157,32 @@ int win_configure(win_t *win, XConfigureEvent *c) {
161157 return changed;
162158 }
163159
160+int win_resize(win_t *win, unsigned int w, unsigned int h) {
161+ if (!win)
162+ return 0;
163+
164+ w = MIN(w, win->env.scrw - 2 * win->bw);
165+ h = MIN(h, win->env.scrh - 2 * win->bw);
166+
167+ if (win->w == w && win->h == h)
168+ return 0;
169+
170+ win->w = w;
171+ win->h = h;
172+
173+ if (win->x + w + 2 * win->bw > win->env.scrw)
174+ win->x = win->env.scrw - w - 2 * win->bw;
175+ if (win->y + h + 2 * win->bw > win->env.scrh)
176+ win->y = win->env.scrh - h - 2 * win->bw;
177+
178+ if (options->fixed)
179+ win_set_sizehints(win);
180+
181+ XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h);
182+
183+ return 1;
184+}
185+
164186 void win_toggle_fullscreen(win_t *win) {
165187 XEvent ev;
166188 XClientMessageEvent *cm;
@@ -213,6 +235,17 @@ void win_draw(win_t *win) {
213235 XClearWindow(win->env.dpy, win->xwin);
214236 }
215237
238+void win_set_title(win_t *win, const char *title) {
239+ if (!win)
240+ return;
241+
242+ if (!title)
243+ title = "sxiv";
244+
245+ XStoreName(win->env.dpy, win->xwin, title);
246+ XSetIconName(win->env.dpy, win->xwin, title);
247+}
248+
216249 void win_set_cursor(win_t *win, win_cur_t cursor) {
217250 if (!win)
218251 return;
M window.h View
@@ -56,14 +56,15 @@ typedef struct win_s {
5656 void win_open(win_t*);
5757 void win_close(win_t*);
5858
59−void win_set_title(win_t*, const char*);
60−
6159 int win_configure(win_t*, XConfigureEvent*);
60+int win_resize(win_t*, unsigned int, unsigned int);
61+
6262 void win_toggle_fullscreen(win_t*);
6363
6464 void win_clear(win_t*);
6565 void win_draw(win_t*);
6666
67+void win_set_title(win_t*, const char*);
6768 void win_set_cursor(win_t*, win_cur_t);
6869
6970 #endif /* WINDOW_H */