From 4283d4074c45d27704b00d9ea80eae7847d66375 Mon Sep 17 00:00:00 2001 From: nirav Date: Sun, 2 Sep 2018 13:07:04 +0530 Subject: Added support for loading next, prev image --- dir.c | 54 +++++++++++++++++++++++++++++++ dir.h | 7 ++++ main.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ makefile | 7 +--- 4 files changed, 162 insertions(+), 15 deletions(-) create mode 100644 dir.c create mode 100644 dir.h diff --git a/dir.c b/dir.c new file mode 100644 index 0000000..3baec19 --- /dev/null +++ b/dir.c @@ -0,0 +1,54 @@ +#include "dir.h" +#include +#include +#include +#include +#include +#include + +char *get_filename_ext(char *file_name) +{ + char *dot = strrchr(file_name, '.'); + if (!dot || dot == file_name) + return ""; + return dot + 1; +} + +int image_filter(const struct dirent *dir) +{ + char *ext = get_filename_ext(strdup(dir->d_name)); + if (dir->d_type == DT_REG && (!strcmp(ext, "png") || !strcmp(ext, "jpg"))) { + return 1; + } + return 0; +} + +int scan(const char *file_name, char ***list, int *list_count) +{ + struct dirent **name_list; + char **files; + int curr_index; + char *dir_name = dirname(strdup(file_name)); + size_t dir_name_len = strlen(dir_name); + *list_count = scandir(dir_name, &name_list, image_filter, alphasort); + if (list_count < 0) { + return -1; + } + + files = malloc(*list_count * sizeof(char *)); + char *file_basename = basename(strdup(file_name)); + int i = 0; + while (i < *list_count) { + if (!strcmp(file_basename, name_list[i]->d_name)) + curr_index = i; + files[i] = malloc((strlen(name_list[i]->d_name) + dir_name_len + 2) * + sizeof(char)); + sprintf(files[i], "%s/%s", dir_name, name_list[i]->d_name); + free(name_list[i]); + i++; + } + *list = files; + free(name_list); + + return curr_index; +} diff --git a/dir.h b/dir.h new file mode 100644 index 0000000..a3e6865 --- /dev/null +++ b/dir.h @@ -0,0 +1,7 @@ +#ifndef __DIR_H +#define __DIR_H +#define _DEFAULT_SOURCE + +int scan(const char *file_name, char ***list, int *list_count); + +#endif diff --git a/main.c b/main.c index 7743c8e..5ac2806 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,12 @@ +#include +#include #include +#include +#include +#include + +#include "dir.h" + #define TIMEOUT 20 #define ZOOM_FACTOR 0.1 @@ -15,6 +23,34 @@ int pixbuf_width, pixbuf_height; double aspect_ratio; double curr_zoom = 1.0; enum scale_mode curr_scale_mod; +GError *error = NULL; + +char *curr_file_name; +GDir *dir; +char **file_list; +int curr_file_index, file_list_count; + +void print_supported_formats() +{ + GSList *list, *it; + GdkPixbufFormat *format; + gchar **extensions; + gchar *ex; + + list = gdk_pixbuf_get_formats(); + if (list != NULL) { + for (it = list; it->next != NULL; it = it->next) { + format = (GdkPixbufFormat *)it->data; + g_print("%s:", gdk_pixbuf_format_get_description(format)); + extensions = gdk_pixbuf_format_get_extensions(format); + for (ex = *extensions; *ex; ex++) { + g_print(" %s", ex); + } + g_print("\n"); + } + g_slist_free(list); + } +} void fit_image() { @@ -50,6 +86,52 @@ void fit_image() gtk_image_set_from_pixbuf(GTK_IMAGE(image), GDK_PIXBUF(curr_pixbuf)); } +void next() +{ + if (curr_file_index >= file_list_count - 1) { + return; + } + curr_file_index++; + curr_file_name = file_list[curr_file_index]; + g_print("qwe2: %s\n", curr_file_name); + g_object_unref(pixbuf); + pixbuf = gdk_pixbuf_new_from_file(curr_file_name, &error); + if (error) { + g_warning("gdk_pixbuf_new_from_file() failed with error: %s\n", + error->message); + g_clear_error(&error); + return; + } + pixbuf_width = gdk_pixbuf_get_width(GDK_PIXBUF(pixbuf)); + pixbuf_height = gdk_pixbuf_get_height(GDK_PIXBUF(pixbuf)); + aspect_ratio = (double)pixbuf_width / (double)pixbuf_height; + curr_zoom = 1.0; + fit_image(); +} + +void prev() +{ + if (curr_file_index <= 0) { + return; + } + curr_file_index--; + curr_file_name = file_list[curr_file_index]; + g_print("qwe2: %s\n", curr_file_name); + g_object_unref(pixbuf); + pixbuf = gdk_pixbuf_new_from_file(curr_file_name, &error); + if (error) { + g_warning("gdk_pixbuf_new_from_file() failed with error: %s\n", + error->message); + g_clear_error(&error); + return; + } + pixbuf_width = gdk_pixbuf_get_width(GDK_PIXBUF(pixbuf)); + pixbuf_height = gdk_pixbuf_get_height(GDK_PIXBUF(pixbuf)); + aspect_ratio = (double)pixbuf_width / (double)pixbuf_height; + curr_zoom = 1.0; + fit_image(); +} + void zoom(int type) { if (type == 0) { @@ -85,17 +167,22 @@ static gboolean key_press(GtkWindow *window, GdkEvent *event, gpointer data) fit_image(); return FALSE; case GDK_KEY_plus: - case GDK_KEY_ScrollUp: zoom(1); return FALSE; case GDK_KEY_minus: - case GDK_KEY_ScrollDown: zoom(-1); return FALSE; case GDK_KEY_equal: - case GDK_KEY_ScrollClick: zoom(0); return FALSE; + case GDK_KEY_n: + case GDK_KEY_j: + next(); + return FALSE; + case GDK_KEY_p: + case GDK_KEY_k: + prev(); + return FALSE; default: return TRUE; } @@ -140,8 +227,6 @@ gboolean configure_callback(GtkWindow *window, GdkEvent *event, gpointer data) static void activate(GtkApplication *app, gpointer user_data) { - GError *error = NULL; - window = gtk_application_window_new(app); gtk_window_set_title(GTK_WINDOW(window), "qwe"); gtk_window_set_default_size(GTK_WINDOW(window), 800, 600); @@ -153,23 +238,29 @@ static void activate(GtkApplication *app, gpointer user_data) g_signal_connect(G_OBJECT(window), "scroll-event", G_CALLBACK(scroll_callback), NULL); - pixbuf = gdk_pixbuf_new_from_file( - "/home/nirav/Downloads/Saved Pictures/bash help shortcuts.png", &error); + char *curr_filename = + "/home/nirav/Downloads/Saved Pictures/bash help shortcuts.png"; + + curr_file_index = scan(curr_filename, &file_list, &file_list_count); + printf("num %d %d\n", curr_file_index, file_list_count); + printf("file %s\n", file_list[curr_file_index]); + + pixbuf = gdk_pixbuf_new_from_file(curr_filename, &error); if (error) { g_warning("gdk_pixbuf_new_from_file() failed with error: %s\n", error->message); g_clear_error(&error); return; } - image = gtk_image_new_from_pixbuf(GDK_PIXBUF(pixbuf)); pixbuf_width = gdk_pixbuf_get_width(GDK_PIXBUF(pixbuf)); pixbuf_height = gdk_pixbuf_get_height(GDK_PIXBUF(pixbuf)); aspect_ratio = (double)pixbuf_width / (double)pixbuf_height; + + image = gtk_image_new_from_pixbuf(GDK_PIXBUF(pixbuf)); GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(image)); - gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(scrolled_window)); gtk_widget_show_all(GTK_WIDGET(window)); } diff --git a/makefile b/makefile index f50f036..f5610fe 100644 --- a/makefile +++ b/makefile @@ -3,12 +3,10 @@ INSTALL=install CFLAGS=-o2 -pipe -Wall -std=c11 $(shell pkg-config --cflags gtk+-3.0) LDFLAGS=$(shell pkg-config --libs gtk+-3.0) -# GLIB_COMPILE_RESOURCES=$(shell pkg-config --variable=glib_compile_resources gio-2.0) PREFIX=/usr BINDIR=$(PREFIX)/bin -OBJECTS=main.o -# RESOURCES=resources.c +OBJECTS=main.o dir.o all: qwe @@ -18,9 +16,6 @@ qwe: $(OBJECTS) .c.o: $(CC) -c $(CFLAGS) -o $@ $< -# resources.c: qwe.gresource.xml window.ui -# $(GLIB_COMPILE_RESOURCES) qwe.gresource.xml --target=$(RESOURCES) --sourcedir=. --generate-source - clean: $(RM) $(OBJECTS) $(RESOURCES) qwe -- cgit v1.2.3