From 8339bb7ee7fe4a2598a30e3c4d0cef42329ee5aa Mon Sep 17 00:00:00 2001
From: Bert <ber.t@gmx.com>
Date: Fri, 4 Feb 2011 00:25:57 +0100
Subject: [PATCH] New key mapping: W, resize window to fit image

---
 Makefile |  2 +-
 image.h  |  2 ++
 main.c   |  4 ++++
 window.c | 73 ++++++++++++++++++++++++++++++++++++++++----------------
 window.h |  5 ++--
 5 files changed, 63 insertions(+), 23 deletions(-)

diff --git a/Makefile b/Makefile
index 016224b..1f24280 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 all: sxiv
 
-VERSION=git-20110203
+VERSION=git-20110204
 
 CC?=gcc
 PREFIX?=/usr/local
diff --git a/image.h b/image.h
index de4f3cc..95ba949 100644
--- a/image.h
+++ b/image.h
@@ -37,9 +37,11 @@ typedef enum pandir_e {
 typedef struct img_s {
 	float zoom;
 	scalemode_t scalemode;
+
 	unsigned char re;
 	unsigned char checkpan;
 	unsigned char aa;
+
 	int x;
 	int y;
 	int w;
diff --git a/main.c b/main.c
index 369c1a2..4ce19de 100644
--- a/main.c
+++ b/main.c
@@ -329,6 +329,10 @@ void on_keypress(XKeyEvent *kev) {
 			win_toggle_fullscreen(&win);
 			/* render on next configurenotify */
 			break;
+		case XK_W:
+			if ((changed = win_resize(&win, img.w * img.zoom, img.h * img.zoom)))
+				img.checkpan = 1;
+			break;
 
 		/* miscellaneous */
 		case XK_a:
diff --git a/window.c b/window.c
index aa2f699..2c8ea68 100644
--- a/window.c
+++ b/window.c
@@ -31,10 +31,23 @@ static Cursor hand;
 
 static GC bgc;
 
+void win_set_sizehints(win_t *win) {
+	XSizeHints sizehints;
+
+	if (!win)
+		return;
+
+	sizehints.flags = PMinSize | PMaxSize;
+	sizehints.min_width = win->w;
+	sizehints.max_width = win->w;
+	sizehints.min_height = win->h;
+	sizehints.max_height = win->h;
+	XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
+}
+
 void win_open(win_t *win) {
 	win_env_t *e;
 	XClassHint classhint;
-	XSizeHints sizehints;
 	XColor bgcol;
 	int gmask;
 
@@ -104,14 +117,8 @@ void win_open(win_t *win) {
 	classhint.res_class = "sxiv";
 	XSetClassHint(e->dpy, win->xwin, &classhint);
 
-	if (options->fixed) {
-		sizehints.flags = PMinSize | PMaxSize;
-		sizehints.min_width = win->w;
-		sizehints.max_width = win->w;
-		sizehints.min_height = win->h;
-		sizehints.max_height = win->h;
-		XSetWMNormalHints(e->dpy, win->xwin, &sizehints);
-	}
+	if (options->fixed)
+		win_set_sizehints(win);
 
 	XMapWindow(e->dpy, win->xwin);
 	XFlush(e->dpy);
@@ -133,17 +140,6 @@ void win_close(win_t *win) {
 	XCloseDisplay(win->env.dpy);
 }
 
-void win_set_title(win_t *win, const char *title) {
-	if (!win)
-		return;
-
-	if (!title)
-		title = "sxiv";
-
-	XStoreName(win->env.dpy, win->xwin, title);
-	XSetIconName(win->env.dpy, win->xwin, title);
-}
-
 int win_configure(win_t *win, XConfigureEvent *c) {
 	int changed;
 
@@ -161,6 +157,32 @@ int win_configure(win_t *win, XConfigureEvent *c) {
 	return changed;
 }
 
+int win_resize(win_t *win, unsigned int w, unsigned int h) {
+	if (!win)
+		return 0;
+
+	w = MIN(w, win->env.scrw - 2 * win->bw);
+	h = MIN(h, win->env.scrh - 2 * win->bw);
+
+	if (win->w == w && win->h == h)
+		return 0;
+
+	win->w = w;
+	win->h = h;
+
+	if (win->x + w + 2 * win->bw > win->env.scrw)
+		win->x = win->env.scrw - w - 2 * win->bw;
+	if (win->y + h + 2 * win->bw > win->env.scrh)
+		win->y = win->env.scrh - h - 2 * win->bw;
+
+	if (options->fixed)
+		win_set_sizehints(win);
+
+	XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h);
+
+	return 1;
+}
+
 void win_toggle_fullscreen(win_t *win) {
 	XEvent ev;
 	XClientMessageEvent *cm;
@@ -213,6 +235,17 @@ void win_draw(win_t *win) {
 	XClearWindow(win->env.dpy, win->xwin);
 }
 
+void win_set_title(win_t *win, const char *title) {
+	if (!win)
+		return;
+
+	if (!title)
+		title = "sxiv";
+
+	XStoreName(win->env.dpy, win->xwin, title);
+	XSetIconName(win->env.dpy, win->xwin, title);
+}
+
 void win_set_cursor(win_t *win, win_cur_t cursor) {
 	if (!win)
 		return;
diff --git a/window.h b/window.h
index d9413e0..cc2598f 100644
--- a/window.h
+++ b/window.h
@@ -56,14 +56,15 @@ typedef struct win_s {
 void win_open(win_t*);
 void win_close(win_t*);
 
-void win_set_title(win_t*, const char*);
-
 int win_configure(win_t*, XConfigureEvent*);
+int win_resize(win_t*, unsigned int, unsigned int);
+
 void win_toggle_fullscreen(win_t*);
 
 void win_clear(win_t*);
 void win_draw(win_t*);
 
+void win_set_title(win_t*, const char*);
 void win_set_cursor(win_t*, win_cur_t);
 
 #endif /* WINDOW_H */