aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2021-10-17 15:38:27 +0200
committerPřemysl Eric Janouch <p@janouch.name>2021-10-20 18:56:18 +0200
commitde9e91e9a509d4e14f845f255c7f8331d35b45b1 (patch)
tree244036c49c246942a66d32283296a7050f70920d
parentdff4e316bb85b85216699e6fe0c98539aeb42a49 (diff)
downloadfiv-de9e91e9a509d4e14f845f255c7f8331d35b45b1.tar.gz
fiv-de9e91e9a509d4e14f845f255c7f8331d35b45b1.tar.xz
fiv-de9e91e9a509d4e14f845f255c7f8331d35b45b1.zip
Add a prototype of a directory view widget
-rw-r--r--fastiv-browser.c233
-rw-r--r--fastiv-browser.h25
-rw-r--r--fastiv-io.c17
-rw-r--r--fastiv-view.h3
-rw-r--r--fastiv.c30
-rw-r--r--meson.build1
6 files changed, 298 insertions, 11 deletions
diff --git a/fastiv-browser.c b/fastiv-browser.c
new file mode 100644
index 0000000..883a8b2
--- /dev/null
+++ b/fastiv-browser.c
@@ -0,0 +1,233 @@
+//
+// fastiv-browser.c: fast image viewer - filesystem browser widget
+//
+// Copyright (c) 2021, Přemysl Eric Janouch <p@janouch.name>
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+//
+
+#include <math.h>
+
+#include "fastiv-browser.h"
+#include "fastiv-view.h"
+
+typedef struct entry Entry;
+
+struct entry {
+ char *filename;
+ cairo_surface_t *thumbnail;
+};
+
+static void
+entry_free(Entry *self)
+{
+ g_free(self->filename);
+ if (self->thumbnail)
+ cairo_surface_destroy(self->thumbnail);
+}
+
+// --- Boilerplate -------------------------------------------------------------
+
+struct _FastivBrowser {
+ GtkWidget parent_instance;
+
+ // TODO(p): We probably want to pre-arrange everything into rows.
+ // - All rows are the same height.
+ GArray *entries;
+ int selected;
+};
+
+// TODO(p): For proper navigation, we need to implement GtkScrollable.
+G_DEFINE_TYPE_EXTENDED(FastivBrowser, fastiv_browser, GTK_TYPE_WIDGET, 0,
+ /* G_IMPLEMENT_INTERFACE(GTK_TYPE_SCROLLABLE,
+ fastiv_browser_scrollable_init) */)
+
+enum {
+ ITEM_ACTIVATED,
+ LAST_SIGNAL,
+};
+
+// Globals are, sadly, the canonical way of storing signal numbers.
+static guint browser_signals[LAST_SIGNAL];
+
+static void
+fastiv_browser_finalize(GObject *gobject)
+{
+ G_GNUC_UNUSED FastivBrowser *self = FASTIV_BROWSER(gobject);
+ G_OBJECT_CLASS(fastiv_browser_parent_class)->finalize(gobject);
+}
+
+static GtkSizeRequestMode
+fastiv_browser_get_request_mode(G_GNUC_UNUSED GtkWidget *widget)
+{
+ return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
+}
+
+static void
+fastiv_browser_get_preferred_width(GtkWidget *widget,
+ gint *minimum, gint *natural)
+{
+ G_GNUC_UNUSED FastivBrowser *self = FASTIV_BROWSER(widget);
+ // TODO(p): Set it to the width of the widget with one wide item within.
+ *minimum = *natural = 0;
+}
+
+static void
+fastiv_browser_get_preferred_height_for_width(GtkWidget *widget,
+ G_GNUC_UNUSED gint width, gint *minimum, gint *natural)
+{
+ G_GNUC_UNUSED FastivBrowser *self = FASTIV_BROWSER(widget);
+ // TODO(p): Re-layout, figure it out.
+ *minimum = *natural = 0;
+}
+
+static void
+fastiv_browser_realize(GtkWidget *widget)
+{
+ GtkAllocation allocation;
+ gtk_widget_get_allocation(widget, &allocation);
+
+ GdkWindowAttr attributes = {
+ .window_type = GDK_WINDOW_CHILD,
+ .x = allocation.x,
+ .y = allocation.y,
+ .width = allocation.width,
+ .height = allocation.height,
+
+ // Input-only would presumably also work (as in GtkPathBar, e.g.),
+ // but it merely seems to involve more work.
+ .wclass = GDK_INPUT_OUTPUT,
+
+ .visual = gtk_widget_get_visual(widget),
+ .event_mask = gtk_widget_get_events(widget)
+ | GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK,
+ };
+
+ // We need this window to receive input events at all.
+ // TODO(p): See if input events bubble up to parents.
+ GdkWindow *window = gdk_window_new(gtk_widget_get_parent_window(widget),
+ &attributes, GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL);
+ gtk_widget_register_window(widget, window);
+ gtk_widget_set_window(widget, window);
+ gtk_widget_set_realized(widget, TRUE);
+}
+
+static gboolean
+fastiv_browser_draw(GtkWidget *widget, cairo_t *cr)
+{
+ G_GNUC_UNUSED FastivBrowser *self = FASTIV_BROWSER(widget);
+ if (!gtk_cairo_should_draw_window(cr, gtk_widget_get_window(widget)))
+ return TRUE;
+
+ GtkAllocation allocation;
+ gtk_widget_get_allocation(widget, &allocation);
+ gtk_render_background(gtk_widget_get_style_context(widget), cr,
+ 0, 0, allocation.width, allocation.height);
+
+ const double row_height = 256;
+
+ gint occupied_width = 0, y = 0;
+ for (guint i = 0; i < self->entries->len; i++) {
+ const Entry *entry = &g_array_index(self->entries, Entry, i);
+ if (!entry->thumbnail)
+ continue;
+
+ int width = cairo_image_surface_get_width(entry->thumbnail);
+ int height = cairo_image_surface_get_height(entry->thumbnail);
+
+ double scale = row_height / height;
+ if (width * scale > 2 * row_height)
+ scale = 2 * row_height / width;
+
+ int projected_width = round(scale * width);
+ int projected_height = round(scale * height);
+
+ if (occupied_width != 0
+ && occupied_width + projected_width > allocation.width) {
+ occupied_width = 0;
+ y += row_height;
+ }
+
+ cairo_save(cr);
+ cairo_translate(cr, occupied_width, y + row_height - projected_height);
+ cairo_scale(cr, scale, scale);
+ cairo_set_source_surface(cr, entry->thumbnail, 0, 0);
+ cairo_paint(cr);
+ cairo_restore(cr);
+
+ occupied_width += projected_width;
+ }
+ return TRUE;
+}
+
+static void
+fastiv_browser_class_init(FastivBrowserClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS(klass);
+ object_class->finalize = fastiv_browser_finalize;
+
+ GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
+ widget_class->get_request_mode = fastiv_browser_get_request_mode;
+ widget_class->get_preferred_width =
+ fastiv_browser_get_preferred_width;
+ widget_class->get_preferred_height_for_width =
+ fastiv_browser_get_preferred_height_for_width;
+ widget_class->realize = fastiv_browser_realize;
+ widget_class->draw = fastiv_browser_draw;
+
+ // TODO(p): Connect to this and emit it.
+ browser_signals[ITEM_ACTIVATED] =
+ g_signal_new("item-activated", G_TYPE_FROM_CLASS(klass),
+ 0, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
+
+ // TODO(p): Later override "screen_changed", recreate Pango layouts there,
+ // if we get to have any, or otherwise reflect DPI changes.
+ gtk_widget_class_set_css_name(widget_class, "fastiv-browser");
+}
+
+static void
+fastiv_browser_init(FastivBrowser *self)
+{
+ gtk_widget_set_can_focus(GTK_WIDGET(self), TRUE);
+
+ self->entries = g_array_new(FALSE, TRUE, sizeof(Entry));
+ g_array_set_clear_func(self->entries, (GDestroyNotify) entry_free);
+ self->selected = -1;
+}
+
+void
+fastiv_browser_load(FastivBrowser *self, const char *path)
+{
+ g_array_set_size(self->entries, 0);
+
+ // TODO(p): Use opendir(), in order to get file type directly.
+ GDir *dir = g_dir_open(path, 0, NULL);
+ if (!dir)
+ return;
+
+ const char *filename;
+ while ((filename = g_dir_read_name(dir))) {
+ if (!strcmp(filename, ".")
+ || !strcmp(filename, ".."))
+ continue;
+
+ gchar *subpath = g_build_filename(path, filename, NULL);
+ g_array_append_val(self->entries, ((Entry) {
+ .thumbnail = fastiv_io_lookup_thumbnail(subpath),
+ .filename = subpath,
+ }));
+ }
+ g_dir_close(dir);
+
+ // TODO(p): Sort the entries.
+ gtk_widget_queue_draw(GTK_WIDGET(self));
+}
diff --git a/fastiv-browser.h b/fastiv-browser.h
new file mode 100644
index 0000000..ca0f745
--- /dev/null
+++ b/fastiv-browser.h
@@ -0,0 +1,25 @@
+//
+// fastiv-browser.h: fast image viewer - filesystem browser widget
+//
+// Copyright (c) 2021, Přemysl Eric Janouch <p@janouch.name>
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+//
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+#define FASTIV_TYPE_BROWSER (fastiv_browser_get_type())
+G_DECLARE_FINAL_TYPE(FastivBrowser, fastiv_browser, FASTIV, BROWSER, GtkWidget)
+
+void fastiv_browser_load(FastivBrowser *self, const char *path);
diff --git a/fastiv-io.c b/fastiv-io.c
index 28e2c4e..9c862e3 100644
--- a/fastiv-io.c
+++ b/fastiv-io.c
@@ -482,11 +482,11 @@ check_png_thumbnail(png_structp pngp, png_infop infop, const gchar *target,
// TODO(p): Support spng as well (it can't premultiply alpha by itself,
// but at least it won't gamma-adjust it for us).
static cairo_surface_t *
-read_png_thumbnail(const gchar *filename, const gchar *target, time_t mtime,
+read_png_thumbnail(const gchar *path, const gchar *uri, time_t mtime,
GError **error)
{
FILE *fp;
- if (!(fp = fopen(filename, "rb"))) {
+ if (!(fp = fopen(path, "rb"))) {
set_error(error, g_strerror(errno));
return NULL;
}
@@ -514,7 +514,7 @@ read_png_thumbnail(const gchar *filename, const gchar *target, time_t mtime,
// XXX: libpng will premultiply with the alpha, but it also gamma-adjust it.
png_set_alpha_mode(pngp, PNG_ALPHA_BROKEN, PNG_DEFAULT_sRGB);
png_read_info(pngp, infop);
- if (check_png_thumbnail(pngp, infop, target, mtime) == FALSE)
+ if (check_png_thumbnail(pngp, infop, uri, mtime) == FALSE)
png_error(pngp, "mismatch");
// Asking for at least 8-bit channels. This call is a superset of:
@@ -572,7 +572,7 @@ read_png_thumbnail(const gchar *filename, const gchar *target, time_t mtime,
// The specification does not say where the required metadata should be,
// it could very well be broken up into two parts.
png_read_end(pngp, infop);
- if (check_png_thumbnail(pngp, infop, target, mtime) != TRUE)
+ if (check_png_thumbnail(pngp, infop, uri, mtime) != TRUE)
png_error(pngp, "mismatch or not a thumbnail");
fail:
@@ -601,10 +601,15 @@ fastiv_io_lookup_thumbnail(const gchar *target)
cairo_surface_t *result = NULL;
const gchar *sizes[] = {"large", "x-large", "xx-large", "normal"};
+ GError *error = NULL;
for (gsize i = 0; !result && i < G_N_ELEMENTS(sizes); i++) {
gchar *path = g_strdup_printf("%s/thumbnails/%s/%s.png",
- cache_dir, "large", sum);
- result = read_png_thumbnail(path, target, st.st_mtim.tv_sec, NULL);
+ cache_dir, sizes[i], sum);
+ result = read_png_thumbnail(path, uri, st.st_mtim.tv_sec, &error);
+ if (error) {
+ g_debug("%s: %s", path, error->message);
+ g_clear_error(&error);
+ }
g_free(path);
}
diff --git a/fastiv-view.h b/fastiv-view.h
index d9cfdba..edc99d4 100644
--- a/fastiv-view.h
+++ b/fastiv-view.h
@@ -19,7 +19,7 @@
#include <gtk/gtk.h>
-#define FASTIV_TYPE_VIEW (fastiv_view_get_type ())
+#define FASTIV_TYPE_VIEW (fastiv_view_get_type())
G_DECLARE_FINAL_TYPE(FastivView, fastiv_view, FASTIV, VIEW, GtkWidget)
/// Try to open the given file, synchronously, to be displayed by the widget.
@@ -27,3 +27,4 @@ gboolean fastiv_view_open(FastivView *self, const gchar *path, GError **error);
// Private, fastiv-io.c
cairo_surface_t *fastiv_io_open(const gchar *path, GError **error);
+cairo_surface_t *fastiv_io_lookup_thumbnail(const gchar *target);
diff --git a/fastiv.c b/fastiv.c
index 6a74216..0d05852 100644
--- a/fastiv.c
+++ b/fastiv.c
@@ -28,6 +28,7 @@
#include "config.h"
#include "fastiv-view.h"
+#include "fastiv-browser.h"
// --- Utilities ---------------------------------------------------------------
@@ -246,6 +247,8 @@ struct {
GtkWidget *window;
GtkWidget *view;
+ GtkWidget *browser;
+ GtkWidget *browser_scroller;
} g;
static gboolean
@@ -288,6 +291,8 @@ load_directory(const gchar *dirname)
g_ptr_array_set_size(g.files, 0);
g.files_index = -1;
+ fastiv_browser_load(FASTIV_BROWSER(g.browser), dirname);
+
GError *error = NULL;
GDir *dir = g_dir_open(dirname, 0, &error);
if (dir) {
@@ -429,16 +434,32 @@ main(int argc, char *argv[])
gtk_window_set_default_icon_name(PROJECT_NAME);
- const char *style = "fastiv-view { background: black; }";
+ const char *style = "fastiv-view, fastiv-browser { background: black; }";
GtkCssProvider *provider = gtk_css_provider_new();
gtk_css_provider_load_from_data(provider, style, strlen(style), NULL);
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g.view = g_object_new(FASTIV_TYPE_VIEW, NULL);
+ gtk_widget_show_all(g.view);
+
+ g.browser_scroller = gtk_scrolled_window_new(NULL, NULL);
+ g.browser = g_object_new(FASTIV_TYPE_BROWSER, NULL);
+ gtk_widget_set_vexpand(g.browser, TRUE);
+ gtk_widget_set_hexpand(g.browser, TRUE);
+ gtk_container_add(GTK_CONTAINER(g.browser_scroller), g.browser);
+ // TODO(p): Can we not do it here separately?
+ gtk_widget_show_all(g.browser_scroller);
+
+ GtkWidget *stack = gtk_stack_new();
+ gtk_stack_set_transition_type(
+ GTK_STACK(stack), GTK_STACK_TRANSITION_TYPE_NONE);
+ gtk_container_add(GTK_CONTAINER(stack), g.view);
+ gtk_container_add(GTK_CONTAINER(stack), g.browser_scroller);
+
g.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(g.window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
- gtk_container_add(GTK_CONTAINER(g.window), g.view);
+ gtk_container_add(GTK_CONTAINER(g.window), stack);
// The references to closures are initially floating and sunk on connect.
GtkAccelGroup *accel_group = gtk_accel_group_new();
@@ -491,8 +512,9 @@ main(int argc, char *argv[])
}
g_free(cwd);
- // TODO(p): When no picture is loaded, show a view of this directory
- // (we're missing a widget for that).
+ if (g.files_index < 0)
+ gtk_stack_set_visible_child(GTK_STACK(stack), g.browser_scroller);
+
gtk_widget_show_all(g.window);
gtk_main();
return 0;
diff --git a/meson.build b/meson.build
index ee20657..944413a 100644
--- a/meson.build
+++ b/meson.build
@@ -20,6 +20,7 @@ configure_file(
)
executable('fastiv', 'fastiv.c', 'fastiv-view.c', 'fastiv-io.c',
+ 'fastiv-browser.c',
install : true,
dependencies : [dependencies])