aboutsummaryrefslogtreecommitdiff
path: root/src/imv.c
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2019-02-16 17:54:22 +0000
committerHarry Jeffery <harry@exec64.co.uk>2019-02-16 21:01:38 +0000
commit4eaa1c89aefb4ebb0cc5aa40613a555e6d250ed0 (patch)
treedead142317480ad2fbae18e929c2baa2be1e64c2 /src/imv.c
parentb1f7fe555c5a7d30b03398f6f3c23675e568e3ac (diff)
downloadimv-4eaa1c89aefb4ebb0cc5aa40613a555e6d250ed0.tar.gz
Add customisable image resizing behaviour
Two new flags: -w and -W enable resizing and recentering, respectively. Also add an 'autoresize' config variable to control this behaviour. Fixes #122
Diffstat (limited to 'src/imv.c')
-rw-r--r--src/imv.c50
1 files changed, 49 insertions, 1 deletions
diff --git a/src/imv.c b/src/imv.c
index 2af5878..29e3a20 100644
--- a/src/imv.c
+++ b/src/imv.c
@@ -53,6 +53,13 @@ enum background_type {
BACKGROUND_TYPE_COUNT
};
+/* window behaviour on image change */
+enum resize_mode {
+ RESIZE_NONE, /* do nothing */
+ RESIZE_ONLY, /* resize to fit the new image */
+ RESIZE_CENTER /* resize to fit the new image and recenter */
+};
+
struct backend_chain {
const struct imv_backend *backend;
struct backend_chain *next;
@@ -84,6 +91,10 @@ struct imv {
/* dirty state flags */
bool need_redraw;
bool need_rescale;
+ bool need_resize;
+
+ /* mode for resizing the window on image change */
+ enum resize_mode resize_mode;
/* traverse sub-directories for more images */
bool recursive_load;
@@ -555,6 +566,26 @@ static bool parse_upscaling_method(struct imv *imv, const char *method)
return false;
}
+static bool parse_resizing_mode(struct imv *imv, const char *method)
+{
+ if (!strcmp(method, "none")) {
+ imv->upscaling_method = RESIZE_NONE;
+ return true;
+ }
+
+ if (!strcmp(method, "resize")) {
+ imv->upscaling_method = RESIZE_ONLY;
+ return true;
+ }
+
+ if (!strcmp(method, "recenter")) {
+ imv->upscaling_method = RESIZE_CENTER;
+ return true;
+ }
+
+ return false;
+}
+
static int load_paths_from_stdin(void *data)
{
struct imv *imv = data;
@@ -613,11 +644,13 @@ bool imv_parse_args(struct imv *imv, int argc, char **argv)
int o;
- while((o = getopt(argc, argv, "frdxhlu:s:n:b:t:")) != -1) {
+ while((o = getopt(argc, argv, "frdwWxhlu:s:n:b:t:")) != -1) {
switch(o) {
case 'f': imv->fullscreen = true; break;
case 'r': imv->recursive_load = true; break;
case 'd': imv->overlay_enabled = true; break;
+ case 'w': imv->resize_mode = RESIZE_ONLY; break;
+ case 'W': imv->resize_mode = RESIZE_CENTER; break;
case 'x': imv->loop_input = false; break;
case 'l': imv->list_files_at_exit = true; break;
case 'n': imv->starting_path = optarg; break;
@@ -986,6 +1019,17 @@ static void handle_new_image(struct imv *imv, struct imv_bitmap *bitmap, int fra
imv_bitmap_free(bitmap);
imv->need_redraw = true;
imv->need_rescale = true;
+ /* If autoresizing on every image is enabled, make sure we do so */
+ if (imv->resize_mode != RESIZE_NONE) {
+ imv->need_resize = true;
+ }
+ if (imv->need_resize) {
+ imv->need_resize = false;
+ SDL_SetWindowSize(imv->window, imv->current_image.width, imv->current_image.height);
+ if (imv->resize_mode == RESIZE_CENTER) {
+ SDL_SetWindowPosition(imv->window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
+ }
+ }
imv->loading = false;
imv->next_frame_due = frametime ? SDL_GetTicks() + frametime : 0;
imv->next_frame_duration = 0;
@@ -1282,6 +1326,10 @@ static int handle_ini_value(void *user, const char *section, const char *name,
return 1;
}
+ if(!strcmp(name, "autoresize")) {
+ return parse_resizing_mode(imv, value);
+ }
+
if(!strcmp(name, "upscaling_method")) {
return parse_upscaling_method(imv, value);
}