aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--image.c62
-rw-r--r--image.h3
-rw-r--r--main.c13
-rw-r--r--sxiv.h1
4 files changed, 79 insertions, 0 deletions
diff --git a/image.c b/image.c
index c98c310..0b6d6c5 100644
--- a/image.c
+++ b/image.c
@@ -88,6 +88,28 @@ void img_display(img_t *img, win_t *win) {
img_render(img, win);
}
+void img_check_pan(img_t *img, win_t *win) {
+ if (!img)
+ return;
+
+ if (img->w * img->zoom > win->w) {
+ if (img->x > 0 && img->x + img->w * img->zoom > win->w)
+ img->x = 0;
+ if (img->x < 0 && img->x + img->w * img->zoom < win->w)
+ img->x = win->w - img->w * img->zoom;
+ } else {
+ img->x = (win->w - img->w * img->zoom) / 2;
+ }
+ if (img->h * img->zoom > win->h) {
+ if (img->y > 0 && img->y + img->h * img->zoom > win->h)
+ img->y = 0;
+ if (img->y < 0 && img->y + img->h * img->zoom < win->h)
+ img->y = win->h - img->h * img->zoom;
+ } else {
+ img->y = (win->h - img->h * img->zoom) / 2;
+ }
+}
+
void img_render(img_t *img, win_t *win) {
int sx, sy, sw, sh;
int dx, dy, dw, dh;
@@ -95,6 +117,8 @@ void img_render(img_t *img, win_t *win) {
if (!img || !win || !imlib_context_get_image())
return;
+ img_check_pan(img, win);
+
if (img->x < 0) {
sx = -img->x / img->zoom;
sw = win->w / img->zoom;
@@ -125,3 +149,41 @@ void img_render(img_t *img, win_t *win) {
win_draw(win);
}
+
+int img_zoom(img_t *img, int d) {
+ int ad, iz;
+ float z;
+
+ if (!img)
+ return 0;
+
+ ad = ABS(d);
+ iz = (int) (img->zoom * 1000.0) + d;
+ if (iz % ad > ad / 2)
+ iz += ad - iz % ad;
+ else
+ iz -= iz % ad;
+ z = (float) iz / 1000.0;
+
+ if (z * 100.0 < ZOOM_MIN)
+ z = ZOOM_MIN / 100.0;
+ else if (z * 100.0 > ZOOM_MAX)
+ z = ZOOM_MAX / 100.0;
+
+ if (z != img->zoom) {
+ img->x -= (img->w * z - img->w * img->zoom) / 2;
+ img->y -= (img->h * z - img->h * img->zoom) / 2;
+ img->zoom = z;
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+int img_zoom_in(img_t *img) {
+ return img_zoom(img, 125);
+}
+
+int img_zoom_out(img_t *img) {
+ return img_zoom(img, -125);
+}
diff --git a/image.h b/image.h
index 9e14f15..659f3b9 100644
--- a/image.h
+++ b/image.h
@@ -43,4 +43,7 @@ int img_load(img_t*, const char*);
void img_display(img_t*, win_t*);
void img_render(img_t*, win_t*);
+int img_zoom_in(img_t*);
+int img_zoom_out(img_t*);
+
#endif /* IMAGE_H */
diff --git a/main.c b/main.c
index 440b9f1..e6f05c8 100644
--- a/main.c
+++ b/main.c
@@ -142,6 +142,19 @@ void on_keypress(XEvent *ev) {
update_title();
}
break;
+ case XK_plus:
+ case XK_equal:
+ if (img_zoom_in(&img)) {
+ img_render(&img, &win);
+ update_title();
+ }
+ break;
+ case XK_minus:
+ if (img_zoom_out(&img)) {
+ img_render(&img, &win);
+ update_title();
+ }
+ break;
}
}
diff --git a/sxiv.h b/sxiv.h
index d8e329f..a8be2fe 100644
--- a/sxiv.h
+++ b/sxiv.h
@@ -23,6 +23,7 @@
#define VERSION "git-20110119"
+#define ABS(a) ((a) < 0 ? (-(a)) : (a))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))