2021-07-14 07:09:19 +02:00
|
|
|
//
|
|
|
|
// fastiv.c: fast image viewer
|
|
|
|
//
|
|
|
|
// 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 <glib.h>
|
2021-09-17 20:42:37 +02:00
|
|
|
#include <glib/gstdio.h>
|
2021-11-01 04:59:38 +01:00
|
|
|
#include <gtk/gtk.h>
|
2021-07-14 07:09:19 +02:00
|
|
|
|
2021-11-29 00:35:39 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <math.h>
|
2021-07-14 07:09:19 +02:00
|
|
|
#include <stdarg.h>
|
2021-11-01 04:59:38 +01:00
|
|
|
#include <stdio.h>
|
2021-07-14 07:09:19 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-09-17 20:42:37 +02:00
|
|
|
#include <fnmatch.h>
|
|
|
|
|
2021-07-14 07:09:19 +02:00
|
|
|
#include "config.h"
|
2021-12-18 06:38:30 +01:00
|
|
|
#include "fiv-browser.h"
|
|
|
|
#include "fiv-io.h"
|
|
|
|
#include "fiv-sidebar.h"
|
|
|
|
#include "fiv-view.h"
|
2021-11-01 05:08:55 +01:00
|
|
|
#include "xdg.h"
|
2021-07-14 07:09:19 +02:00
|
|
|
|
|
|
|
// --- Utilities ---------------------------------------------------------------
|
|
|
|
|
2021-11-01 04:59:38 +01:00
|
|
|
static void exit_fatal(const gchar *format, ...) G_GNUC_PRINTF(1, 2);
|
2021-07-14 07:09:19 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
exit_fatal(const gchar *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
|
|
|
|
gchar *format_nl = g_strdup_printf("%s\n", format);
|
|
|
|
vfprintf(stderr, format_nl, ap);
|
|
|
|
free(format_nl);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2021-12-21 08:36:11 +01:00
|
|
|
// --- Keyboard shortcuts ------------------------------------------------------
|
2021-12-20 14:25:59 +01:00
|
|
|
// Fuck XML, this can be easily represented in static structures.
|
|
|
|
// Though it would be nice if the accelerators could be customized.
|
|
|
|
|
|
|
|
struct key {
|
|
|
|
const char *accelerator;
|
|
|
|
const char *title;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct key_group {
|
|
|
|
const char *title;
|
|
|
|
const struct key *keys;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct key_section {
|
|
|
|
const char *title;
|
|
|
|
const char *section_name;
|
|
|
|
const struct key_group *groups;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct key help_keys_general[] = {
|
|
|
|
{"F1 <control>F1", "Show this list of shortcuts"},
|
|
|
|
{"F11 f", "Toggle fullscreen view"},
|
|
|
|
{"<alt><shift>d", "Toggle dark theme variant"},
|
|
|
|
{"q <control>q", "Exit the program"},
|
|
|
|
{"Escape <control>w", "Exit the program"},
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2021-12-21 08:36:11 +01:00
|
|
|
static struct key_group help_keys_browser[] = {
|
|
|
|
{"General", help_keys_general},
|
|
|
|
{"View", (struct key[]) {
|
|
|
|
{"F9", "Toggle navigation sidebar"},
|
|
|
|
{}
|
|
|
|
}},
|
|
|
|
{"Navigation", (struct key[]) {
|
|
|
|
{"<control>l", "Open location..."},
|
|
|
|
{"<control>n", "Open a new window"},
|
|
|
|
{"<alt>Left", "Go back in history"},
|
|
|
|
{"<alt>Right", "Go forward in history"},
|
|
|
|
{"<alt>Up", "Go to parent directory"},
|
|
|
|
{"<alt>Home", "Go home"},
|
|
|
|
{"F5 r <control>r", "Refresh"},
|
|
|
|
{}
|
|
|
|
}},
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct key_group help_keys_view[] = {
|
|
|
|
{"General", help_keys_general},
|
|
|
|
{"View", (struct key[]) {
|
|
|
|
{"F8", "Toggle toolbar"},
|
|
|
|
{}
|
|
|
|
}},
|
|
|
|
{"Navigation", (struct key[]) {
|
|
|
|
{"<control>l", "Open location..."},
|
|
|
|
{"<control>n", "Open a new window"},
|
|
|
|
{"Left Up Page_Up", "Previous image"},
|
|
|
|
{"Right Down Page_Down", "Next image"},
|
|
|
|
{"Return <alt>Left", "Return to browser"},
|
|
|
|
{}
|
|
|
|
}},
|
|
|
|
{"Zoom", (struct key[]) {
|
|
|
|
{"<control>0", "Set zoom to 100%"},
|
|
|
|
{"1...9", "Set zoom to N:1"},
|
|
|
|
{"plus <control>plus", "Zoom in"},
|
|
|
|
{"minus <control>minus", "Zoom out"},
|
|
|
|
{"w", "Zoom to fit width if larger"},
|
|
|
|
{"h", "Zoom to fit height if larger"},
|
2021-12-20 14:25:59 +01:00
|
|
|
{}
|
|
|
|
}},
|
2021-12-21 08:36:11 +01:00
|
|
|
{"Orientation", (struct key[]) {
|
|
|
|
{"less", "Rotate anticlockwise"},
|
|
|
|
{"equal", "Mirror"},
|
|
|
|
{"greater", "Rotate clockwise"},
|
|
|
|
{}
|
|
|
|
}},
|
|
|
|
{"Configuration", (struct key[]) {
|
|
|
|
{"x", "Toggle scale to fit if larger"},
|
|
|
|
{"i", "Toggle smooth scaling"},
|
|
|
|
{"t", "Toggle transparency highlighting"},
|
|
|
|
{}
|
|
|
|
}},
|
|
|
|
{"Control", (struct key[]) {
|
|
|
|
{"bracketleft", "Previous page"},
|
|
|
|
{"bracketright", "Next page"},
|
|
|
|
{"braceleft", "Previous frame"},
|
|
|
|
{"braceright", "Next frame"},
|
|
|
|
{"space", "Toggle playback"},
|
|
|
|
{}
|
|
|
|
}},
|
|
|
|
{"Tools", (struct key[]) {
|
|
|
|
{"<control>p", "Print..."},
|
|
|
|
{"<control>s", "Save page as..."},
|
|
|
|
{"<control><shift>s", "Save frame as..."},
|
2021-12-19 12:35:53 +01:00
|
|
|
{"<alt>Return", "Show file information"},
|
2021-12-20 14:25:59 +01:00
|
|
|
{}
|
|
|
|
}},
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2021-12-21 08:36:11 +01:00
|
|
|
static struct key_section help_keys[] = {
|
|
|
|
{"Browser", "browser", help_keys_browser},
|
|
|
|
{"View", "view", help_keys_view},
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2021-12-20 14:25:59 +01:00
|
|
|
static GtkWidget *
|
|
|
|
make_key(const struct key *key)
|
|
|
|
{
|
|
|
|
return gtk_widget_new(GTK_TYPE_SHORTCUTS_SHORTCUT,
|
|
|
|
"title", key->title,
|
|
|
|
"shortcut-type", GTK_SHORTCUT_ACCELERATOR,
|
|
|
|
"accelerator", key->accelerator, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
make_key_group(const struct key_group *group)
|
|
|
|
{
|
|
|
|
GtkWidget *widget = gtk_widget_new(
|
|
|
|
GTK_TYPE_SHORTCUTS_GROUP, "title", group->title, NULL);
|
|
|
|
for (const struct key *p = group->keys; p->title; p++)
|
|
|
|
gtk_container_add(GTK_CONTAINER(widget), make_key(p));
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
make_key_section(const struct key_section *section)
|
|
|
|
{
|
|
|
|
GtkWidget *widget = gtk_widget_new(GTK_TYPE_SHORTCUTS_SECTION,
|
|
|
|
"title", section->title, "section-name", section->section_name, NULL);
|
|
|
|
for (const struct key_group *p = section->groups; p->title; p++)
|
|
|
|
gtk_container_add(GTK_CONTAINER(widget), make_key_group(p));
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
make_key_window(void)
|
|
|
|
{
|
|
|
|
GtkWidget *window = gtk_widget_new(GTK_TYPE_SHORTCUTS_WINDOW, NULL);
|
|
|
|
for (const struct key_section *p = help_keys; p->title; p++) {
|
|
|
|
GtkWidget *section = make_key_section(p);
|
|
|
|
gtk_widget_show_all(section);
|
|
|
|
gtk_container_add(GTK_CONTAINER(window), section);
|
|
|
|
}
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
2021-07-14 07:09:19 +02:00
|
|
|
// --- Main --------------------------------------------------------------------
|
|
|
|
|
2021-12-20 05:33:04 +01:00
|
|
|
// TODO(p): See if it's possible to give separators room to shrink
|
|
|
|
// by some minor amount of pixels, margin-wise.
|
2021-11-29 00:35:39 +01:00
|
|
|
#define B make_toolbar_button
|
2021-12-17 07:39:53 +01:00
|
|
|
#define T make_toolbar_toggle
|
2021-11-29 00:35:39 +01:00
|
|
|
#define TOOLBAR(XX) \
|
|
|
|
XX(BROWSE, B("view-grid-symbolic", "Browse")) \
|
|
|
|
XX(FILE_PREVIOUS, B("go-previous-symbolic", "Previous file")) \
|
|
|
|
XX(FILE_NEXT, B("go-next-symbolic", "Next file")) \
|
2021-12-20 05:33:04 +01:00
|
|
|
XX(S1, gtk_separator_new(GTK_ORIENTATION_HORIZONTAL)) \
|
2021-11-29 00:35:39 +01:00
|
|
|
XX(PAGE_FIRST, B("go-top-symbolic", "First page")) \
|
|
|
|
XX(PAGE_PREVIOUS, B("go-up-symbolic", "Previous page")) \
|
|
|
|
XX(PAGE_NEXT, B("go-down-symbolic", "Next page")) \
|
|
|
|
XX(PAGE_LAST, B("go-bottom-symbolic", "Last page")) \
|
2021-12-20 05:33:04 +01:00
|
|
|
XX(S2, gtk_separator_new(GTK_ORIENTATION_HORIZONTAL)) \
|
2021-11-29 00:35:39 +01:00
|
|
|
XX(SKIP_BACK, B("media-skip-backward-symbolic", "Rewind playback")) \
|
|
|
|
XX(SEEK_BACK, B("media-seek-backward-symbolic", "Previous frame")) \
|
2021-12-19 05:33:57 +01:00
|
|
|
XX(PLAY_PAUSE, B("media-playback-start-symbolic", "Pause")) \
|
2021-11-29 00:35:39 +01:00
|
|
|
XX(SEEK_FORWARD, B("media-seek-forward-symbolic", "Next frame")) \
|
2021-12-20 05:33:04 +01:00
|
|
|
XX(S3, gtk_separator_new(GTK_ORIENTATION_HORIZONTAL)) \
|
2021-11-29 00:35:39 +01:00
|
|
|
XX(PLUS, B("zoom-in-symbolic", "Zoom in")) \
|
2021-12-19 05:33:57 +01:00
|
|
|
XX(SCALE, gtk_label_new("")) \
|
2021-11-29 00:35:39 +01:00
|
|
|
XX(MINUS, B("zoom-out-symbolic", "Zoom out")) \
|
|
|
|
XX(ONE, B("zoom-original-symbolic", "Original size")) \
|
2021-12-17 07:39:53 +01:00
|
|
|
XX(FIT, T("zoom-fit-best-symbolic", "Scale to fit")) \
|
2021-12-20 05:33:04 +01:00
|
|
|
XX(S4, gtk_separator_new(GTK_ORIENTATION_HORIZONTAL)) \
|
2021-11-29 00:35:39 +01:00
|
|
|
/* XX(PIN, B("view-pin-symbolic", "Keep view configuration")) */ \
|
|
|
|
/* Or perhaps "blur-symbolic", also in the extended set. */ \
|
2021-12-17 07:39:53 +01:00
|
|
|
XX(SMOOTH, T("blend-tool-symbolic", "Smooth scaling")) \
|
2021-12-20 11:08:36 +01:00
|
|
|
XX(CHECKERBOARD, T("checkerboard-symbolic", "Highlight transparency")) \
|
2021-11-29 00:35:39 +01:00
|
|
|
/* XX(COLOR, B("preferences-color-symbolic", "Color management")) */ \
|
|
|
|
XX(SAVE, B("document-save-as-symbolic", "Save as...")) \
|
|
|
|
XX(PRINT, B("document-print-symbolic", "Print...")) \
|
2021-12-19 12:35:53 +01:00
|
|
|
XX(INFO, B("info-symbolic", "Information")) \
|
2021-12-20 05:33:04 +01:00
|
|
|
XX(S5, gtk_separator_new(GTK_ORIENTATION_HORIZONTAL)) \
|
2021-11-29 00:35:39 +01:00
|
|
|
XX(LEFT, B("object-rotate-left-symbolic", "Rotate left")) \
|
|
|
|
XX(MIRROR, B("object-flip-horizontal-symbolic", "Mirror")) \
|
|
|
|
XX(RIGHT, B("object-rotate-right-symbolic", "Rotate right")) \
|
2021-12-20 05:33:04 +01:00
|
|
|
XX(S6, gtk_separator_new(GTK_ORIENTATION_HORIZONTAL)) \
|
2021-11-29 00:35:39 +01:00
|
|
|
/* We are YouTube. */ \
|
|
|
|
XX(FULLSCREEN, B("view-fullscreen-symbolic", "Fullscreen"))
|
|
|
|
|
|
|
|
enum {
|
|
|
|
#define XX(id, constructor) TOOLBAR_ ## id,
|
|
|
|
TOOLBAR(XX)
|
|
|
|
#undef XX
|
|
|
|
TOOLBAR_COUNT
|
|
|
|
};
|
|
|
|
|
2021-07-14 07:09:19 +02:00
|
|
|
struct {
|
2021-09-17 20:42:37 +02:00
|
|
|
gchar **supported_globs;
|
2021-11-21 11:00:25 +01:00
|
|
|
gboolean filtering;
|
2021-09-17 20:42:37 +02:00
|
|
|
|
2021-12-19 09:59:27 +01:00
|
|
|
gchar *directory; ///< Full path to the currently browsed directory
|
|
|
|
GList *directory_back; ///< History paths going backwards
|
|
|
|
GList *directory_forward; ///< History paths going forwards
|
2021-09-17 20:42:37 +02:00
|
|
|
GPtrArray *files;
|
|
|
|
gint files_index;
|
|
|
|
|
2021-12-19 08:48:32 +01:00
|
|
|
gchar *path;
|
2021-09-17 20:42:37 +02:00
|
|
|
|
2021-09-17 19:48:16 +02:00
|
|
|
GtkWidget *window;
|
2021-11-08 08:00:18 +01:00
|
|
|
GtkWidget *stack;
|
2021-11-29 00:35:39 +01:00
|
|
|
|
2021-11-18 10:35:58 +01:00
|
|
|
GtkWidget *browser_paned;
|
|
|
|
GtkWidget *browser_sidebar;
|
2021-11-21 16:03:54 +01:00
|
|
|
GtkWidget *plus;
|
|
|
|
GtkWidget *minus;
|
2021-11-29 00:35:39 +01:00
|
|
|
GtkWidget *browser_scroller;
|
|
|
|
GtkWidget *browser;
|
|
|
|
|
|
|
|
GtkWidget *view_box;
|
2021-12-20 12:06:32 +01:00
|
|
|
GtkWidget *view_toolbar;
|
2021-11-29 00:35:39 +01:00
|
|
|
GtkWidget *toolbar[TOOLBAR_COUNT];
|
|
|
|
GtkWidget *view;
|
2021-07-14 07:09:19 +02:00
|
|
|
} g;
|
|
|
|
|
2021-09-17 20:42:37 +02:00
|
|
|
static gboolean
|
|
|
|
is_supported(const gchar *filename)
|
|
|
|
{
|
|
|
|
gchar *utf8 = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
|
|
|
|
if (!utf8)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
gchar *lowercased = g_utf8_strdown(utf8, -1);
|
|
|
|
g_free(utf8);
|
|
|
|
|
|
|
|
// XXX: fnmatch() uses the /locale/ encoding, but who cares nowadays.
|
|
|
|
for (gchar **p = g.supported_globs; *p; p++)
|
|
|
|
if (!fnmatch(*p, lowercased, 0)) {
|
|
|
|
g_free(lowercased);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(lowercased);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
show_error_dialog(GError *error)
|
|
|
|
{
|
2021-11-01 04:59:38 +01:00
|
|
|
GtkWidget *dialog =
|
|
|
|
gtk_message_dialog_new(GTK_WINDOW(g.window), GTK_DIALOG_MODAL,
|
|
|
|
GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", error->message);
|
2021-09-17 20:42:37 +02:00
|
|
|
gtk_dialog_run(GTK_DIALOG(dialog));
|
|
|
|
gtk_widget_destroy(dialog);
|
|
|
|
g_error_free(error);
|
|
|
|
}
|
|
|
|
|
2021-11-21 21:52:52 +01:00
|
|
|
static void
|
|
|
|
switch_to_browser(void)
|
|
|
|
{
|
|
|
|
gtk_window_set_title(GTK_WINDOW(g.window), g.directory);
|
|
|
|
gtk_stack_set_visible_child(GTK_STACK(g.stack), g.browser_paned);
|
|
|
|
gtk_widget_grab_focus(g.browser_scroller);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
switch_to_view(const char *path)
|
|
|
|
{
|
|
|
|
gtk_window_set_title(GTK_WINDOW(g.window), path);
|
2021-11-29 00:35:39 +01:00
|
|
|
gtk_stack_set_visible_child(GTK_STACK(g.stack), g.view_box);
|
2021-11-12 07:23:24 +01:00
|
|
|
gtk_widget_grab_focus(g.view);
|
2021-11-21 21:52:52 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 09:07:14 +01:00
|
|
|
static gint
|
|
|
|
files_compare(gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
gchar *path1 = g_canonicalize_filename(*(gchar **) a, g.directory);
|
|
|
|
gchar *path2 = g_canonicalize_filename(*(gchar **) b, g.directory);
|
|
|
|
GFile *location1 = g_file_new_for_path(path1);
|
|
|
|
GFile *location2 = g_file_new_for_path(path2);
|
|
|
|
g_free(path1);
|
|
|
|
g_free(path2);
|
|
|
|
gint result = fiv_io_filecmp(location1, location2);
|
|
|
|
g_object_unref(location1);
|
|
|
|
g_object_unref(location2);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
update_files_index(void)
|
|
|
|
{
|
|
|
|
g.files_index = -1;
|
|
|
|
|
|
|
|
// FIXME: We presume that this basename is from the same directory.
|
|
|
|
gchar *basename = g.path ? g_path_get_basename(g.path) : NULL;
|
|
|
|
for (guint i = 0; i < g.files->len; i++) {
|
|
|
|
if (!g_strcmp0(basename, g_ptr_array_index(g.files, i)))
|
|
|
|
g.files_index = i;
|
|
|
|
}
|
|
|
|
g_free(basename);
|
|
|
|
}
|
|
|
|
|
2021-12-19 09:59:27 +01:00
|
|
|
static void
|
|
|
|
load_directory_without_reload(const gchar *dirname)
|
|
|
|
{
|
|
|
|
gchar *dirname_duplicated = g_strdup(dirname);
|
|
|
|
if (g.directory_back &&
|
|
|
|
!strcmp(dirname, g.directory_back->data)) {
|
|
|
|
// We're going back in history.
|
|
|
|
if (g.directory) {
|
|
|
|
g.directory_forward =
|
|
|
|
g_list_prepend(g.directory_forward, g.directory);
|
|
|
|
g.directory = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GList *link = g.directory_back;
|
|
|
|
g.directory_back = g_list_remove_link(g.directory_back, link);
|
|
|
|
g_list_free_full(link, g_free);
|
|
|
|
} else if (g.directory_forward &&
|
|
|
|
!strcmp(dirname, g.directory_forward->data)) {
|
|
|
|
// We're going forward in history.
|
|
|
|
if (g.directory) {
|
|
|
|
g.directory_back =
|
|
|
|
g_list_prepend(g.directory_back, g.directory);
|
|
|
|
g.directory = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GList *link = g.directory_forward;
|
|
|
|
g.directory_forward = g_list_remove_link(g.directory_forward, link);
|
|
|
|
g_list_free_full(link, g_free);
|
|
|
|
} else if (g.directory && strcmp(dirname, g.directory)) {
|
|
|
|
// We're on a new subpath.
|
|
|
|
g_list_free_full(g.directory_forward, g_free);
|
|
|
|
g.directory_forward = NULL;
|
|
|
|
|
|
|
|
g.directory_back = g_list_prepend(g.directory_back, g.directory);
|
|
|
|
g.directory = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(g.directory);
|
|
|
|
g.directory = dirname_duplicated;
|
|
|
|
}
|
|
|
|
|
2021-09-17 20:42:37 +02:00
|
|
|
static void
|
|
|
|
load_directory(const gchar *dirname)
|
|
|
|
{
|
2021-11-18 22:00:08 +01:00
|
|
|
if (dirname) {
|
2021-12-19 09:59:27 +01:00
|
|
|
load_directory_without_reload(dirname);
|
2021-11-22 01:43:15 +01:00
|
|
|
|
|
|
|
GtkAdjustment *vadjustment = gtk_scrolled_window_get_vadjustment(
|
|
|
|
GTK_SCROLLED_WINDOW(g.browser_scroller));
|
|
|
|
gtk_adjustment_set_value(
|
|
|
|
vadjustment, gtk_adjustment_get_lower(vadjustment));
|
2021-11-18 22:00:08 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 20:42:37 +02:00
|
|
|
g_ptr_array_set_size(g.files, 0);
|
|
|
|
g.files_index = -1;
|
|
|
|
|
2021-11-18 22:00:08 +01:00
|
|
|
GFile *file = g_file_new_for_path(g.directory);
|
2021-12-18 06:38:30 +01:00
|
|
|
fiv_sidebar_set_location(FIV_SIDEBAR(g.browser_sidebar), file);
|
2021-11-18 10:35:58 +01:00
|
|
|
g_object_unref(file);
|
2021-12-18 06:38:30 +01:00
|
|
|
fiv_browser_load(
|
|
|
|
FIV_BROWSER(g.browser), g.filtering ? is_supported : NULL, g.directory);
|
2021-10-17 15:38:27 +02:00
|
|
|
|
2021-09-17 20:42:37 +02:00
|
|
|
GError *error = NULL;
|
2021-11-18 22:00:08 +01:00
|
|
|
GDir *dir = g_dir_open(g.directory, 0, &error);
|
2021-09-17 20:42:37 +02:00
|
|
|
if (dir) {
|
|
|
|
for (const gchar *name = NULL; (name = g_dir_read_name(dir)); ) {
|
2021-10-19 09:07:29 +02:00
|
|
|
// This really wants to make you use readdir() directly.
|
|
|
|
char *absolute = g_canonicalize_filename(name, g.directory);
|
|
|
|
gboolean is_dir = g_file_test(absolute, G_FILE_TEST_IS_DIR);
|
|
|
|
g_free(absolute);
|
2021-12-19 09:07:14 +01:00
|
|
|
if (!is_dir && is_supported(name))
|
|
|
|
g_ptr_array_add(g.files, g_strdup(name));
|
2021-09-17 20:42:37 +02:00
|
|
|
}
|
|
|
|
g_dir_close(dir);
|
2021-12-19 09:07:14 +01:00
|
|
|
|
|
|
|
g_ptr_array_sort(g.files, files_compare);
|
|
|
|
update_files_index();
|
2021-09-17 20:42:37 +02:00
|
|
|
} else {
|
|
|
|
show_error_dialog(error);
|
|
|
|
}
|
2021-12-19 10:13:26 +01:00
|
|
|
|
|
|
|
gtk_widget_set_sensitive(
|
|
|
|
g.toolbar[TOOLBAR_FILE_PREVIOUS], g.files->len > 1);
|
|
|
|
gtk_widget_set_sensitive(
|
|
|
|
g.toolbar[TOOLBAR_FILE_NEXT], g.files->len > 1);
|
2021-09-17 20:42:37 +02:00
|
|
|
g_ptr_array_add(g.files, NULL);
|
|
|
|
|
|
|
|
// XXX: When something outside the filtered entries is open, the index is
|
|
|
|
// kept at -1, and browsing doesn't work. How to behave here?
|
|
|
|
// Should we add it to the pointer array as an exception?
|
2021-11-21 21:52:52 +01:00
|
|
|
if (dirname)
|
|
|
|
switch_to_browser();
|
2021-09-17 20:42:37 +02:00
|
|
|
}
|
|
|
|
|
2021-11-21 11:00:25 +01:00
|
|
|
static void
|
|
|
|
on_filtering_toggled(GtkToggleButton *button, G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
|
|
|
g.filtering = gtk_toggle_button_get_active(button);
|
|
|
|
if (g.directory)
|
|
|
|
load_directory(NULL);
|
|
|
|
}
|
|
|
|
|
2021-09-17 19:48:16 +02:00
|
|
|
static void
|
|
|
|
open(const gchar *path)
|
|
|
|
{
|
2021-09-17 20:42:37 +02:00
|
|
|
g_return_if_fail(g_path_is_absolute(path));
|
|
|
|
|
2021-09-17 19:48:16 +02:00
|
|
|
GError *error = NULL;
|
2021-12-18 06:38:30 +01:00
|
|
|
if (!fiv_view_open(FIV_VIEW(g.view), path, &error)) {
|
2021-11-10 03:13:33 +01:00
|
|
|
char *base = g_filename_display_basename(path);
|
|
|
|
g_prefix_error(&error, "%s: ", base);
|
2021-09-17 20:42:37 +02:00
|
|
|
show_error_dialog(error);
|
2021-11-10 03:13:33 +01:00
|
|
|
g_free(base);
|
2021-09-17 19:48:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-16 14:02:26 +01:00
|
|
|
gchar *uri = g_filename_to_uri(path, NULL, NULL);
|
|
|
|
if (uri) {
|
2021-11-18 09:43:51 +01:00
|
|
|
gtk_recent_manager_add_item(gtk_recent_manager_get_default(), uri);
|
2021-11-16 14:02:26 +01:00
|
|
|
g_free(uri);
|
|
|
|
}
|
|
|
|
|
2021-12-19 09:59:27 +01:00
|
|
|
g_list_free_full(g.directory_forward, g_free);
|
|
|
|
g.directory_forward = NULL;
|
|
|
|
|
2021-12-19 08:48:32 +01:00
|
|
|
g_free(g.path);
|
|
|
|
g.path = g_strdup(path);
|
2021-09-17 20:42:37 +02:00
|
|
|
|
|
|
|
// So that load_directory() itself can be used for reloading.
|
|
|
|
gchar *dirname = g_path_get_dirname(path);
|
2021-11-22 12:41:53 +01:00
|
|
|
if (!g.files->len /* hack to always load the directory after launch */ ||
|
2021-12-19 09:07:14 +01:00
|
|
|
!g.directory || strcmp(dirname, g.directory))
|
2021-09-17 20:42:37 +02:00
|
|
|
load_directory(dirname);
|
2021-12-19 09:07:14 +01:00
|
|
|
else
|
|
|
|
update_files_index();
|
2021-09-17 20:42:37 +02:00
|
|
|
g_free(dirname);
|
2021-11-20 13:03:30 +01:00
|
|
|
|
2021-11-21 21:52:52 +01:00
|
|
|
switch_to_view(path);
|
2021-09-17 19:48:16 +02:00
|
|
|
}
|
|
|
|
|
2021-11-18 11:20:52 +01:00
|
|
|
static GtkWidget *
|
|
|
|
create_open_dialog(void)
|
2021-09-17 19:48:16 +02:00
|
|
|
{
|
|
|
|
GtkWidget *dialog = gtk_file_chooser_dialog_new("Open file",
|
|
|
|
GTK_WINDOW(g.window), GTK_FILE_CHOOSER_ACTION_OPEN,
|
|
|
|
"_Cancel", GTK_RESPONSE_CANCEL,
|
|
|
|
"_Open", GTK_RESPONSE_ACCEPT, NULL);
|
|
|
|
|
2021-10-23 23:56:59 +02:00
|
|
|
GtkFileFilter *filter = gtk_file_filter_new();
|
2021-12-18 06:38:30 +01:00
|
|
|
for (const char **p = fiv_io_supported_media_types; *p; p++)
|
2021-11-01 04:40:58 +01:00
|
|
|
gtk_file_filter_add_mime_type(filter, *p);
|
2021-11-10 21:33:30 +01:00
|
|
|
#ifdef HAVE_GDKPIXBUF
|
|
|
|
gtk_file_filter_add_pixbuf_formats(filter);
|
|
|
|
#endif // HAVE_GDKPIXBUF
|
2021-10-23 23:56:59 +02:00
|
|
|
gtk_file_filter_set_name(filter, "Supported images");
|
|
|
|
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
|
|
|
|
|
|
|
|
GtkFileFilter *all_files = gtk_file_filter_new();
|
|
|
|
gtk_file_filter_set_name(all_files, "All files");
|
|
|
|
gtk_file_filter_add_pattern(all_files, "*");
|
|
|
|
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), all_files);
|
2021-11-18 11:20:52 +01:00
|
|
|
return dialog;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_open(void)
|
|
|
|
{
|
|
|
|
static GtkWidget *dialog;
|
|
|
|
if (!dialog)
|
|
|
|
dialog = create_open_dialog();
|
|
|
|
|
|
|
|
// Apparently, just keeping the dialog around doesn't mean
|
|
|
|
// that it will remember its last location.
|
|
|
|
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), g.directory);
|
2021-10-23 23:56:59 +02:00
|
|
|
|
2021-09-17 20:42:37 +02:00
|
|
|
// The default is local-only, single item. Paths are returned absolute.
|
2021-11-18 11:20:52 +01:00
|
|
|
switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
|
|
|
|
gchar *path;
|
|
|
|
case GTK_RESPONSE_ACCEPT:
|
|
|
|
path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
|
2021-09-17 19:48:16 +02:00
|
|
|
open(path);
|
|
|
|
g_free(path);
|
2021-11-18 11:20:52 +01:00
|
|
|
break;
|
|
|
|
case GTK_RESPONSE_NONE:
|
|
|
|
dialog = NULL;
|
|
|
|
return;
|
2021-09-17 19:48:16 +02:00
|
|
|
}
|
|
|
|
|
2021-11-18 11:20:52 +01:00
|
|
|
gtk_widget_hide(dialog);
|
2021-09-17 19:48:16 +02:00
|
|
|
}
|
|
|
|
|
2021-09-17 20:42:37 +02:00
|
|
|
static void
|
|
|
|
on_previous(void)
|
|
|
|
{
|
|
|
|
if (g.files_index >= 0) {
|
|
|
|
int previous =
|
|
|
|
(g.files->len - 1 + g.files_index - 1) % (g.files->len - 1);
|
2021-11-01 04:59:38 +01:00
|
|
|
char *absolute = g_canonicalize_filename(
|
|
|
|
g_ptr_array_index(g.files, previous), g.directory);
|
2021-09-17 20:42:37 +02:00
|
|
|
open(absolute);
|
|
|
|
g_free(absolute);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_next(void)
|
|
|
|
{
|
|
|
|
if (g.files_index >= 0) {
|
|
|
|
int next = (g.files_index + 1) % (g.files->len - 1);
|
2021-11-01 04:59:38 +01:00
|
|
|
char *absolute = g_canonicalize_filename(
|
|
|
|
g_ptr_array_index(g.files, next), g.directory);
|
2021-09-17 20:42:37 +02:00
|
|
|
open(absolute);
|
|
|
|
g_free(absolute);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 21:58:15 +01:00
|
|
|
static void
|
|
|
|
spawn_path(const char *path)
|
|
|
|
{
|
|
|
|
char *argv[] = {PROJECT_NAME, (char *) path, NULL};
|
|
|
|
GError *error = NULL;
|
2021-12-18 06:38:30 +01:00
|
|
|
g_spawn_async(
|
|
|
|
NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error);
|
2021-11-18 21:58:15 +01:00
|
|
|
g_clear_error(&error);
|
|
|
|
}
|
|
|
|
|
2021-11-20 12:45:05 +01:00
|
|
|
static void
|
2021-12-18 06:38:30 +01:00
|
|
|
on_item_activated(G_GNUC_UNUSED FivBrowser *browser, GFile *location,
|
2021-11-20 12:45:05 +01:00
|
|
|
GtkPlacesOpenFlags flags, G_GNUC_UNUSED gpointer data)
|
|
|
|
{
|
2021-11-27 02:33:28 +01:00
|
|
|
gchar *path = g_file_get_path(location);
|
|
|
|
if (path) {
|
|
|
|
if (flags == GTK_PLACES_OPEN_NEW_WINDOW)
|
|
|
|
spawn_path(path);
|
|
|
|
else
|
|
|
|
open(path);
|
|
|
|
g_free(path);
|
|
|
|
}
|
2021-11-20 12:45:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-18 22:00:08 +01:00
|
|
|
static gboolean
|
2021-11-22 20:36:41 +01:00
|
|
|
open_any_path(const char *path, gboolean force_browser)
|
2021-11-18 22:00:08 +01:00
|
|
|
{
|
|
|
|
GStatBuf st;
|
|
|
|
gchar *canonical = g_canonicalize_filename(path, g.directory);
|
|
|
|
gboolean success = !g_stat(canonical, &st);
|
2021-11-22 20:36:41 +01:00
|
|
|
if (!success) {
|
2021-11-18 22:00:08 +01:00
|
|
|
show_error_dialog(g_error_new(G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno(errno), "%s: %s", path, g_strerror(errno)));
|
2021-11-22 20:36:41 +01:00
|
|
|
} else if (S_ISDIR(st.st_mode)) {
|
2021-11-18 22:00:08 +01:00
|
|
|
load_directory(canonical);
|
2021-11-22 20:36:41 +01:00
|
|
|
} else if (force_browser) {
|
|
|
|
// GNOME, e.g., invokes this as a hint to focus the particular file,
|
|
|
|
// which we can't currently do yet.
|
|
|
|
gchar *directory = g_path_get_dirname(canonical);
|
|
|
|
load_directory(directory);
|
|
|
|
g_free(directory);
|
|
|
|
} else {
|
2021-11-18 22:00:08 +01:00
|
|
|
open(canonical);
|
2021-11-22 20:36:41 +01:00
|
|
|
}
|
2021-11-18 22:00:08 +01:00
|
|
|
g_free(canonical);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2021-11-18 10:35:58 +01:00
|
|
|
static void
|
|
|
|
on_open_location(G_GNUC_UNUSED GtkPlacesSidebar *sidebar, GFile *location,
|
2021-11-26 23:00:17 +01:00
|
|
|
GtkPlacesOpenFlags flags, G_GNUC_UNUSED gpointer user_data)
|
2021-11-18 10:35:58 +01:00
|
|
|
{
|
|
|
|
gchar *path = g_file_get_path(location);
|
2021-11-18 13:58:27 +01:00
|
|
|
if (path) {
|
2021-11-18 21:58:15 +01:00
|
|
|
if (flags & GTK_PLACES_OPEN_NEW_WINDOW)
|
|
|
|
spawn_path(path);
|
|
|
|
else
|
2021-11-22 20:36:41 +01:00
|
|
|
open_any_path(path, FALSE);
|
2021-11-18 13:58:27 +01:00
|
|
|
g_free(path);
|
|
|
|
}
|
2021-11-18 10:35:58 +01:00
|
|
|
}
|
|
|
|
|
2021-11-21 16:03:54 +01:00
|
|
|
static void
|
|
|
|
on_toolbar_zoom(G_GNUC_UNUSED GtkButton *button, gpointer user_data)
|
|
|
|
{
|
2021-12-18 06:38:30 +01:00
|
|
|
FivIoThumbnailSize size = FIV_IO_THUMBNAIL_SIZE_COUNT;
|
2021-11-21 16:03:54 +01:00
|
|
|
g_object_get(g.browser, "thumbnail-size", &size, NULL);
|
|
|
|
|
2021-11-21 20:46:50 +01:00
|
|
|
size += (gintptr) user_data;
|
2021-12-18 06:38:30 +01:00
|
|
|
g_return_if_fail(size >= FIV_IO_THUMBNAIL_SIZE_MIN &&
|
|
|
|
size <= FIV_IO_THUMBNAIL_SIZE_MAX);
|
2021-11-21 16:03:54 +01:00
|
|
|
|
2021-11-21 20:46:50 +01:00
|
|
|
g_object_set(g.browser, "thumbnail-size", size, NULL);
|
2021-11-21 16:03:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_notify_thumbnail_size(
|
|
|
|
GObject *object, GParamSpec *param_spec, G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
2021-12-18 06:38:30 +01:00
|
|
|
FivIoThumbnailSize size = 0;
|
2021-11-21 16:03:54 +01:00
|
|
|
g_object_get(object, g_param_spec_get_name(param_spec), &size, NULL);
|
2021-12-18 06:38:30 +01:00
|
|
|
gtk_widget_set_sensitive(g.plus, size < FIV_IO_THUMBNAIL_SIZE_MAX);
|
|
|
|
gtk_widget_set_sensitive(g.minus, size > FIV_IO_THUMBNAIL_SIZE_MIN);
|
2021-11-21 16:03:54 +01:00
|
|
|
}
|
|
|
|
|
2021-11-21 21:21:45 +01:00
|
|
|
static void
|
|
|
|
toggle_fullscreen(void)
|
|
|
|
{
|
|
|
|
if (gdk_window_get_state(gtk_widget_get_window(g.window)) &
|
|
|
|
GDK_WINDOW_STATE_FULLSCREEN)
|
|
|
|
gtk_window_unfullscreen(GTK_WINDOW(g.window));
|
|
|
|
else
|
|
|
|
gtk_window_fullscreen(GTK_WINDOW(g.window));
|
|
|
|
}
|
|
|
|
|
2021-11-29 00:35:39 +01:00
|
|
|
static void
|
|
|
|
on_window_state_event(G_GNUC_UNUSED GtkWidget *widget,
|
|
|
|
GdkEventWindowState *event, G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
|
|
|
if (!(event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const char *name = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN)
|
|
|
|
? "view-restore-symbolic"
|
|
|
|
: "view-fullscreen-symbolic";
|
2021-12-19 05:33:57 +01:00
|
|
|
|
|
|
|
GtkButton *button = GTK_BUTTON(g.toolbar[TOOLBAR_FULLSCREEN]);
|
|
|
|
GtkImage *image = GTK_IMAGE(gtk_button_get_image(button));
|
|
|
|
gtk_image_set_from_icon_name(image, name, GTK_ICON_SIZE_BUTTON);
|
2021-11-29 00:35:39 +01:00
|
|
|
}
|
|
|
|
|
2021-12-20 14:25:59 +01:00
|
|
|
static void
|
|
|
|
on_help_destroyed(GtkWidget *window, GtkWidget **storage)
|
|
|
|
{
|
|
|
|
g_return_if_fail(*storage == window);
|
|
|
|
*storage = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
show_help_shortcuts(void)
|
|
|
|
{
|
|
|
|
static GtkWidget *window;
|
|
|
|
if (!window) {
|
|
|
|
window = make_key_window();
|
|
|
|
g_signal_connect(
|
|
|
|
window, "destroy", G_CALLBACK(on_help_destroyed), &window);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_set(window, "section-name",
|
|
|
|
gtk_stack_get_visible_child(GTK_STACK(g.stack)) == g.view_box
|
|
|
|
? "view"
|
|
|
|
: "browser",
|
|
|
|
NULL);
|
|
|
|
gtk_widget_show(window);
|
|
|
|
}
|
|
|
|
|
2021-11-01 06:54:36 +01:00
|
|
|
// Cursor keys, e.g., simply cannot be bound through accelerators
|
|
|
|
// (and GtkWidget::keynav-failed would arguably be an awful solution).
|
|
|
|
//
|
|
|
|
// GtkBindingSets can be added directly through GtkStyleContext,
|
|
|
|
// but that would still require setting up action signals on the widget class,
|
|
|
|
// which is extremely cumbersome. GtkWidget::move-focus has no return value,
|
|
|
|
// so we can't override that and abort further handling.
|
|
|
|
//
|
|
|
|
// Therefore, bind directly to keypresses. Order can be fine-tuned with
|
|
|
|
// g_signal_connect{,after}(), or overriding the handler and either tactically
|
|
|
|
// chaining up or using gtk_window_propagate_key_event().
|
|
|
|
static gboolean
|
2021-11-12 07:22:06 +01:00
|
|
|
on_key_press(G_GNUC_UNUSED GtkWidget *widget, GdkEventKey *event,
|
2021-11-01 06:54:36 +01:00
|
|
|
G_GNUC_UNUSED gpointer data)
|
|
|
|
{
|
2021-11-12 07:22:06 +01:00
|
|
|
switch (event->state & gtk_accelerator_get_default_mod_mask()) {
|
2021-12-20 03:55:27 +01:00
|
|
|
case GDK_MOD1_MASK | GDK_SHIFT_MASK:
|
|
|
|
if (event->keyval == GDK_KEY_D) {
|
|
|
|
GtkSettings *settings = gtk_settings_get_default();
|
|
|
|
const char *property = "gtk-application-prefer-dark-theme";
|
|
|
|
gboolean set = FALSE;
|
|
|
|
g_object_get(settings, property, &set, NULL);
|
|
|
|
g_object_set(settings, property, !set, NULL);
|
|
|
|
}
|
|
|
|
break;
|
2021-11-01 06:54:36 +01:00
|
|
|
case GDK_CONTROL_MASK:
|
2021-11-12 07:22:06 +01:00
|
|
|
switch (event->keyval) {
|
2021-11-01 06:54:36 +01:00
|
|
|
case GDK_KEY_o:
|
|
|
|
on_open();
|
|
|
|
return TRUE;
|
2021-11-18 22:00:08 +01:00
|
|
|
case GDK_KEY_l:
|
2021-12-18 06:38:30 +01:00
|
|
|
fiv_sidebar_show_enter_location(FIV_SIDEBAR(g.browser_sidebar));
|
2021-11-18 22:00:08 +01:00
|
|
|
return TRUE;
|
2021-11-18 21:58:15 +01:00
|
|
|
case GDK_KEY_n:
|
|
|
|
spawn_path(g.directory);
|
2021-11-10 03:13:33 +01:00
|
|
|
return TRUE;
|
2021-12-19 11:37:40 +01:00
|
|
|
case GDK_KEY_r:
|
|
|
|
// TODO(p): Reload the image instead, if it's currently visible.
|
|
|
|
load_directory(NULL);
|
|
|
|
return TRUE;
|
2021-11-29 22:44:51 +01:00
|
|
|
case GDK_KEY_q:
|
|
|
|
case GDK_KEY_w:
|
|
|
|
gtk_widget_destroy(g.window);
|
|
|
|
return TRUE;
|
2021-12-20 14:25:59 +01:00
|
|
|
|
|
|
|
case GDK_KEY_F1:
|
|
|
|
show_help_shortcuts();
|
|
|
|
return TRUE;
|
2021-11-10 03:13:33 +01:00
|
|
|
}
|
2021-11-01 06:54:36 +01:00
|
|
|
break;
|
2021-12-19 09:59:27 +01:00
|
|
|
case GDK_MOD1_MASK:
|
|
|
|
switch (event->keyval) {
|
|
|
|
case GDK_KEY_Left:
|
|
|
|
if (gtk_stack_get_visible_child(GTK_STACK(g.stack)) == g.view_box)
|
|
|
|
switch_to_browser();
|
|
|
|
else if (g.directory_back)
|
|
|
|
load_directory(g.directory_back->data);
|
|
|
|
return TRUE;
|
|
|
|
case GDK_KEY_Right:
|
|
|
|
if (g.directory_forward)
|
|
|
|
load_directory(g.directory_forward->data);
|
|
|
|
else
|
|
|
|
switch_to_view(g.path);
|
|
|
|
return TRUE;
|
2021-12-20 09:58:17 +01:00
|
|
|
case GDK_KEY_Up:
|
|
|
|
if (gtk_stack_get_visible_child(GTK_STACK(g.stack)) != g.view_box) {
|
|
|
|
// This isn't exact, trailing slashes should be ignored.
|
|
|
|
gchar *parent = g_path_get_dirname(g.directory);
|
|
|
|
load_directory(parent);
|
|
|
|
g_free(parent);
|
|
|
|
}
|
|
|
|
return TRUE;
|
2021-12-21 07:19:44 +01:00
|
|
|
case GDK_KEY_Home:
|
|
|
|
load_directory(g_get_home_dir());
|
|
|
|
return TRUE;
|
2021-12-19 09:59:27 +01:00
|
|
|
}
|
|
|
|
break;
|
2021-11-01 06:54:36 +01:00
|
|
|
case 0:
|
2021-11-12 07:22:06 +01:00
|
|
|
switch (event->keyval) {
|
2021-11-01 06:54:36 +01:00
|
|
|
case GDK_KEY_Escape:
|
|
|
|
case GDK_KEY_q:
|
2021-11-23 17:12:31 +01:00
|
|
|
gtk_widget_destroy(g.window);
|
2021-11-01 06:54:36 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case GDK_KEY_o:
|
|
|
|
on_open();
|
|
|
|
return TRUE;
|
2021-11-18 13:58:27 +01:00
|
|
|
case GDK_KEY_F5:
|
2021-11-18 22:00:08 +01:00
|
|
|
case GDK_KEY_r:
|
2021-12-19 11:37:40 +01:00
|
|
|
// TODO(p): See the comment for C-r above.
|
2021-11-18 22:00:08 +01:00
|
|
|
load_directory(NULL);
|
2021-11-18 13:58:27 +01:00
|
|
|
return TRUE;
|
|
|
|
|
2021-12-20 14:25:59 +01:00
|
|
|
case GDK_KEY_F1:
|
|
|
|
show_help_shortcuts();
|
|
|
|
return TRUE;
|
2021-11-18 10:35:58 +01:00
|
|
|
case GDK_KEY_F9:
|
2021-11-23 14:59:25 +01:00
|
|
|
gtk_widget_set_visible(g.browser_sidebar,
|
|
|
|
!gtk_widget_is_visible(g.browser_sidebar));
|
2021-11-18 10:35:58 +01:00
|
|
|
return TRUE;
|
|
|
|
|
2021-11-18 21:53:08 +01:00
|
|
|
case GDK_KEY_F11:
|
|
|
|
case GDK_KEY_f:
|
2021-11-21 21:21:45 +01:00
|
|
|
toggle_fullscreen();
|
2021-11-13 09:40:10 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
on_key_press_view(G_GNUC_UNUSED GtkWidget *widget, GdkEventKey *event,
|
|
|
|
G_GNUC_UNUSED gpointer data)
|
|
|
|
{
|
|
|
|
switch (event->state & gtk_accelerator_get_default_mod_mask()) {
|
|
|
|
case 0:
|
|
|
|
switch (event->keyval) {
|
2021-12-20 12:06:32 +01:00
|
|
|
case GDK_KEY_F8:
|
|
|
|
gtk_widget_set_visible(g.view_toolbar,
|
|
|
|
!gtk_widget_is_visible(g.view_toolbar));
|
|
|
|
return TRUE;
|
|
|
|
|
2021-11-01 06:54:36 +01:00
|
|
|
case GDK_KEY_Left:
|
|
|
|
case GDK_KEY_Up:
|
|
|
|
case GDK_KEY_Page_Up:
|
|
|
|
on_previous();
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case GDK_KEY_Right:
|
|
|
|
case GDK_KEY_Down:
|
|
|
|
case GDK_KEY_Page_Down:
|
|
|
|
on_next();
|
|
|
|
return TRUE;
|
2021-11-18 21:53:08 +01:00
|
|
|
|
|
|
|
case GDK_KEY_Return:
|
2021-11-21 21:52:52 +01:00
|
|
|
switch_to_browser();
|
2021-11-18 21:53:08 +01:00
|
|
|
return TRUE;
|
2021-11-22 16:45:34 +01:00
|
|
|
}
|
|
|
|
break;
|
2021-11-01 06:54:36 +01:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-11-13 10:04:40 +01:00
|
|
|
static gboolean
|
|
|
|
on_button_press_view(G_GNUC_UNUSED GtkWidget *widget, GdkEventButton *event)
|
|
|
|
{
|
2021-11-18 12:43:08 +01:00
|
|
|
if ((event->state & gtk_accelerator_get_default_mod_mask()))
|
|
|
|
return FALSE;
|
|
|
|
switch (event->button) {
|
|
|
|
case 8: // back
|
2021-11-21 21:52:52 +01:00
|
|
|
switch_to_browser();
|
2021-11-13 10:04:40 +01:00
|
|
|
return TRUE;
|
2021-11-21 21:21:45 +01:00
|
|
|
case GDK_BUTTON_PRIMARY:
|
|
|
|
if (event->type == GDK_2BUTTON_PRESS) {
|
|
|
|
toggle_fullscreen();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
2021-11-18 12:43:08 +01:00
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-12-19 09:59:27 +01:00
|
|
|
on_button_press_browser_paned(
|
|
|
|
G_GNUC_UNUSED GtkWidget *widget, GdkEventButton *event)
|
2021-11-18 12:43:08 +01:00
|
|
|
{
|
|
|
|
if ((event->state & gtk_accelerator_get_default_mod_mask()))
|
|
|
|
return FALSE;
|
|
|
|
switch (event->button) {
|
2021-12-19 09:59:27 +01:00
|
|
|
case 8: // back
|
|
|
|
if (g.directory_back)
|
|
|
|
load_directory(g.directory_back->data);
|
|
|
|
return TRUE;
|
2021-11-18 12:43:08 +01:00
|
|
|
case 9: // forward
|
2021-12-19 09:59:27 +01:00
|
|
|
// FIXME: It may be inappropriate to go to the picture,
|
|
|
|
// which may be left over from a different directory.
|
|
|
|
if (g.directory_forward)
|
|
|
|
load_directory(g.directory_forward->data);
|
|
|
|
else
|
|
|
|
switch_to_view(g.path);
|
2021-11-18 12:43:08 +01:00
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
2021-11-13 10:04:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-29 00:35:39 +01:00
|
|
|
static GtkWidget *
|
|
|
|
make_toolbar_button(const gchar *symbolic, const gchar *tooltip)
|
|
|
|
{
|
|
|
|
GtkWidget *button =
|
|
|
|
gtk_button_new_from_icon_name(symbolic, GTK_ICON_SIZE_BUTTON);
|
|
|
|
gtk_widget_set_tooltip_text(button, tooltip);
|
|
|
|
gtk_widget_set_focus_on_click(button, FALSE);
|
|
|
|
gtk_style_context_add_class(
|
|
|
|
gtk_widget_get_style_context(button), GTK_STYLE_CLASS_FLAT);
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
2021-12-17 07:39:53 +01:00
|
|
|
static GtkWidget *
|
|
|
|
make_toolbar_toggle(const gchar *symbolic, const gchar *tooltip)
|
|
|
|
{
|
|
|
|
GtkWidget *button = gtk_toggle_button_new();
|
|
|
|
gtk_button_set_image(GTK_BUTTON(button),
|
|
|
|
gtk_image_new_from_icon_name(symbolic, GTK_ICON_SIZE_BUTTON));
|
|
|
|
gtk_widget_set_tooltip_text(button, tooltip);
|
|
|
|
gtk_widget_set_focus_on_click(button, FALSE);
|
|
|
|
gtk_style_context_add_class(
|
|
|
|
gtk_widget_get_style_context(button), GTK_STYLE_CLASS_FLAT);
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
2021-12-19 07:04:04 +01:00
|
|
|
static void
|
|
|
|
on_view_actions_changed(void)
|
|
|
|
{
|
|
|
|
gboolean has_image = FALSE, can_animate = FALSE;
|
|
|
|
gboolean has_previous = FALSE, has_next = FALSE;
|
|
|
|
g_object_get(g.view, "has-image", &has_image, "can-animate", &can_animate,
|
|
|
|
"has-previous-page", &has_previous, "has-next-page", &has_next, NULL);
|
|
|
|
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_PAGE_FIRST], has_previous);
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_PAGE_PREVIOUS], has_previous);
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_PAGE_NEXT], has_next);
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_PAGE_LAST], has_next);
|
|
|
|
|
|
|
|
// We don't want these to flash during playback.
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_SKIP_BACK], can_animate);
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_SEEK_BACK], can_animate);
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_PLAY_PAUSE], can_animate);
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_SEEK_FORWARD], can_animate);
|
|
|
|
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_PLUS], has_image);
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_SCALE], has_image);
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_MINUS], has_image);
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_ONE], has_image);
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_FIT], has_image);
|
|
|
|
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_SMOOTH], has_image);
|
2021-12-20 11:08:36 +01:00
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_CHECKERBOARD], has_image);
|
2021-12-19 07:04:04 +01:00
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_SAVE], has_image);
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_PRINT], has_image);
|
2021-12-19 12:35:53 +01:00
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_INFO], has_image);
|
2021-12-19 07:04:04 +01:00
|
|
|
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_LEFT], has_image);
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_MIRROR], has_image);
|
|
|
|
gtk_widget_set_sensitive(g.toolbar[TOOLBAR_RIGHT], has_image);
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2021-11-29 00:35:39 +01:00
|
|
|
static void
|
2021-12-17 07:39:53 +01:00
|
|
|
on_notify_view_scale(
|
2021-11-29 00:35:39 +01:00
|
|
|
GObject *object, GParamSpec *param_spec, G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
|
|
|
double scale = 0;
|
|
|
|
g_object_get(object, g_param_spec_get_name(param_spec), &scale, NULL);
|
|
|
|
|
|
|
|
gchar *scale_str = g_strdup_printf("%.0f%%", round(scale * 100));
|
|
|
|
gtk_label_set_text(GTK_LABEL(g.toolbar[TOOLBAR_SCALE]), scale_str);
|
|
|
|
g_free(scale_str);
|
2021-12-17 07:39:53 +01:00
|
|
|
|
|
|
|
// FIXME: The label doesn't immediately assume its new width.
|
2021-11-29 00:35:39 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 05:33:57 +01:00
|
|
|
static void
|
|
|
|
on_notify_view_playing(
|
|
|
|
GObject *object, GParamSpec *param_spec, G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
|
|
|
gboolean b = FALSE;
|
|
|
|
g_object_get(object, g_param_spec_get_name(param_spec), &b, NULL);
|
|
|
|
const char *name = b
|
|
|
|
? "media-playback-pause-symbolic"
|
|
|
|
: "media-playback-start-symbolic";
|
|
|
|
|
|
|
|
GtkButton *button = GTK_BUTTON(g.toolbar[TOOLBAR_PLAY_PAUSE]);
|
|
|
|
GtkImage *image = GTK_IMAGE(gtk_button_get_image(button));
|
|
|
|
gtk_image_set_from_icon_name(image, name, GTK_ICON_SIZE_BUTTON);
|
|
|
|
}
|
|
|
|
|
2021-11-29 00:35:39 +01:00
|
|
|
static void
|
2021-12-17 07:39:53 +01:00
|
|
|
on_notify_view_boolean(
|
|
|
|
GObject *object, GParamSpec *param_spec, gpointer user_data)
|
2021-11-29 00:35:39 +01:00
|
|
|
{
|
2021-12-17 07:39:53 +01:00
|
|
|
gboolean b = FALSE;
|
|
|
|
g_object_get(object, g_param_spec_get_name(param_spec), &b, NULL);
|
|
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(user_data), b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_toolbar_view_toggle(GtkToggleButton *button, const char *property)
|
|
|
|
{
|
|
|
|
g_object_set(g.view, property, gtk_toggle_button_get_active(button), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
toolbar_toggler(int index, const char *property)
|
|
|
|
{
|
|
|
|
g_signal_connect(g.toolbar[index], "toggled",
|
|
|
|
G_CALLBACK(on_toolbar_view_toggle), (gpointer) property);
|
2021-11-29 00:35:39 +01:00
|
|
|
}
|
|
|
|
|
2021-12-17 07:39:53 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2021-11-29 00:35:39 +01:00
|
|
|
static void
|
2021-12-17 07:39:53 +01:00
|
|
|
on_toolbar_view_command(intptr_t command)
|
2021-11-29 00:35:39 +01:00
|
|
|
{
|
2021-12-18 06:38:30 +01:00
|
|
|
fiv_view_command(FIV_VIEW(g.view), command);
|
2021-11-29 00:35:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-12-18 06:38:30 +01:00
|
|
|
toolbar_command(int index, FivViewCommand command)
|
2021-11-29 00:35:39 +01:00
|
|
|
{
|
|
|
|
g_signal_connect_swapped(g.toolbar[index], "clicked",
|
2021-12-17 07:39:53 +01:00
|
|
|
G_CALLBACK(on_toolbar_view_command), (void *) (intptr_t) command);
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
static void
|
|
|
|
toolbar_connect(int index, GCallback callback)
|
|
|
|
{
|
|
|
|
g_signal_connect_swapped(g.toolbar[index], "clicked", callback, NULL);
|
2021-11-29 00:35:39 +01:00
|
|
|
}
|
|
|
|
|
2021-12-20 03:49:07 +01:00
|
|
|
// TODO(p): The text and icons should be faded, unless the mouse cursor is
|
|
|
|
// on the toolbar. However, GtkEventBox is of no use, because either buttons
|
|
|
|
// steal our {enter,leave}-notify-events, or we steal all their input.
|
|
|
|
// Not even connecting to these signals on children works, insensitive buttons
|
|
|
|
// will not trigger anything.
|
|
|
|
// TODO(p): The toolbar should not be visible in fullscreen, or should show up
|
|
|
|
// only when the cursor reaches the top of the screen. Translucency sounds
|
|
|
|
// like a good mechanism here. Presumably, GtkOverlay could be used for this,
|
|
|
|
// but it faces the same problem as above--the input model sucks.
|
|
|
|
// TODO(p): Simply hide it in fullscreen and add a replacement context menu.
|
2021-11-29 00:35:39 +01:00
|
|
|
static GtkWidget *
|
|
|
|
make_view_toolbar(void)
|
|
|
|
{
|
|
|
|
#define XX(id, constructor) g.toolbar[TOOLBAR_ ## id] = constructor;
|
|
|
|
TOOLBAR(XX)
|
|
|
|
#undef XX
|
|
|
|
|
|
|
|
gtk_widget_set_margin_start(g.toolbar[TOOLBAR_SCALE], 5);
|
|
|
|
gtk_widget_set_margin_end(g.toolbar[TOOLBAR_SCALE], 5);
|
|
|
|
|
2021-12-19 07:31:10 +01:00
|
|
|
// So that the width doesn't jump around in the usual zoom range.
|
|
|
|
// Ideally, we'd measure the widest digit and use width(NNN%).
|
|
|
|
gtk_label_set_width_chars(GTK_LABEL(g.toolbar[TOOLBAR_SCALE]), 5);
|
|
|
|
gtk_widget_set_halign(g.toolbar[TOOLBAR_SCALE], GTK_ALIGN_CENTER);
|
|
|
|
|
2021-11-29 00:35:39 +01:00
|
|
|
// GtkStatusBar solves a problem we do not have here.
|
|
|
|
GtkWidget *view_toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
|
2021-12-20 05:23:42 +01:00
|
|
|
gtk_widget_set_name(view_toolbar, "toolbar");
|
2021-11-29 00:35:39 +01:00
|
|
|
GtkBox *box = GTK_BOX(view_toolbar);
|
2021-12-20 05:23:42 +01:00
|
|
|
|
|
|
|
// Exploring different versions of awkward layouts.
|
|
|
|
for (int i = 0; i <= TOOLBAR_S1; i++)
|
2021-11-29 00:35:39 +01:00
|
|
|
gtk_box_pack_start(box, g.toolbar[i], FALSE, FALSE, 0);
|
2021-12-20 05:23:42 +01:00
|
|
|
for (int i = TOOLBAR_COUNT; --i >= TOOLBAR_S6; )
|
|
|
|
gtk_box_pack_end(box, g.toolbar[i], FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
GtkWidget *center = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
|
|
|
|
for (int i = TOOLBAR_S1; ++i < TOOLBAR_S6; )
|
|
|
|
gtk_box_pack_start(GTK_BOX(center), g.toolbar[i], FALSE, FALSE, 0);
|
|
|
|
gtk_box_set_center_widget(box, center);
|
2021-11-29 00:35:39 +01:00
|
|
|
|
|
|
|
toolbar_connect(TOOLBAR_BROWSE, G_CALLBACK(switch_to_browser));
|
|
|
|
toolbar_connect(TOOLBAR_FILE_PREVIOUS, G_CALLBACK(on_previous));
|
|
|
|
toolbar_connect(TOOLBAR_FILE_NEXT, G_CALLBACK(on_next));
|
2021-12-18 06:38:30 +01:00
|
|
|
toolbar_command(TOOLBAR_PAGE_FIRST, FIV_VIEW_COMMAND_PAGE_FIRST);
|
|
|
|
toolbar_command(TOOLBAR_PAGE_PREVIOUS, FIV_VIEW_COMMAND_PAGE_PREVIOUS);
|
|
|
|
toolbar_command(TOOLBAR_PAGE_NEXT, FIV_VIEW_COMMAND_PAGE_NEXT);
|
|
|
|
toolbar_command(TOOLBAR_PAGE_LAST, FIV_VIEW_COMMAND_PAGE_LAST);
|
|
|
|
toolbar_command(TOOLBAR_SKIP_BACK, FIV_VIEW_COMMAND_FRAME_FIRST);
|
|
|
|
toolbar_command(TOOLBAR_SEEK_BACK, FIV_VIEW_COMMAND_FRAME_PREVIOUS);
|
2021-12-19 05:33:57 +01:00
|
|
|
toolbar_command(TOOLBAR_PLAY_PAUSE, FIV_VIEW_COMMAND_TOGGLE_PLAYBACK);
|
2021-12-18 06:38:30 +01:00
|
|
|
toolbar_command(TOOLBAR_SEEK_FORWARD, FIV_VIEW_COMMAND_FRAME_NEXT);
|
|
|
|
toolbar_command(TOOLBAR_PLUS, FIV_VIEW_COMMAND_ZOOM_IN);
|
|
|
|
toolbar_command(TOOLBAR_MINUS, FIV_VIEW_COMMAND_ZOOM_OUT);
|
|
|
|
toolbar_command(TOOLBAR_ONE, FIV_VIEW_COMMAND_ZOOM_1);
|
2021-12-17 07:39:53 +01:00
|
|
|
toolbar_toggler(TOOLBAR_FIT, "scale-to-fit");
|
|
|
|
toolbar_toggler(TOOLBAR_SMOOTH, "filter");
|
2021-12-20 11:08:36 +01:00
|
|
|
toolbar_toggler(TOOLBAR_CHECKERBOARD, "checkerboard");
|
2021-12-18 06:38:30 +01:00
|
|
|
toolbar_command(TOOLBAR_PRINT, FIV_VIEW_COMMAND_PRINT);
|
|
|
|
toolbar_command(TOOLBAR_SAVE, FIV_VIEW_COMMAND_SAVE_PAGE);
|
2021-12-19 12:35:53 +01:00
|
|
|
toolbar_command(TOOLBAR_INFO, FIV_VIEW_COMMAND_INFO);
|
2021-12-18 06:38:30 +01:00
|
|
|
toolbar_command(TOOLBAR_LEFT, FIV_VIEW_COMMAND_ROTATE_LEFT);
|
|
|
|
toolbar_command(TOOLBAR_MIRROR, FIV_VIEW_COMMAND_MIRROR);
|
|
|
|
toolbar_command(TOOLBAR_RIGHT, FIV_VIEW_COMMAND_ROTATE_RIGHT);
|
2021-11-29 00:35:39 +01:00
|
|
|
toolbar_connect(TOOLBAR_FULLSCREEN, G_CALLBACK(toggle_fullscreen));
|
2021-12-17 07:39:53 +01:00
|
|
|
|
2021-12-19 05:33:57 +01:00
|
|
|
g_signal_connect(g.view, "notify::scale",
|
|
|
|
G_CALLBACK(on_notify_view_scale), NULL);
|
|
|
|
g_signal_connect(g.view, "notify::playing",
|
|
|
|
G_CALLBACK(on_notify_view_playing), NULL);
|
2021-12-17 07:39:53 +01:00
|
|
|
g_signal_connect(g.view, "notify::scale-to-fit",
|
|
|
|
G_CALLBACK(on_notify_view_boolean), g.toolbar[TOOLBAR_FIT]);
|
|
|
|
g_signal_connect(g.view, "notify::filter",
|
|
|
|
G_CALLBACK(on_notify_view_boolean), g.toolbar[TOOLBAR_SMOOTH]);
|
2021-12-20 11:08:36 +01:00
|
|
|
g_signal_connect(g.view, "notify::checkerboard",
|
|
|
|
G_CALLBACK(on_notify_view_boolean), g.toolbar[TOOLBAR_CHECKERBOARD]);
|
2021-12-19 05:33:57 +01:00
|
|
|
|
|
|
|
g_object_notify(G_OBJECT(g.view), "scale");
|
|
|
|
g_object_notify(G_OBJECT(g.view), "playing");
|
2021-12-17 07:39:53 +01:00
|
|
|
g_object_notify(G_OBJECT(g.view), "scale-to-fit");
|
|
|
|
g_object_notify(G_OBJECT(g.view), "filter");
|
2021-12-20 11:08:36 +01:00
|
|
|
g_object_notify(G_OBJECT(g.view), "checkerboard");
|
2021-12-19 07:04:04 +01:00
|
|
|
|
|
|
|
GCallback callback = G_CALLBACK(on_view_actions_changed);
|
|
|
|
g_signal_connect(g.view, "notify::has-image", callback, NULL);
|
|
|
|
g_signal_connect(g.view, "notify::can-animate", callback, NULL);
|
|
|
|
g_signal_connect(g.view, "notify::has-previous-page", callback, NULL);
|
|
|
|
g_signal_connect(g.view, "notify::has-next-page", callback, NULL);
|
|
|
|
callback();
|
2021-11-29 00:35:39 +01:00
|
|
|
return view_toolbar;
|
|
|
|
}
|
|
|
|
|
2021-12-21 08:36:11 +01:00
|
|
|
// This is incredibly broken https://stackoverflow.com/a/51054396/76313
|
|
|
|
// thus resolving the problem using overlaps.
|
|
|
|
// We're trying to be universal for light and dark themes both. It's hard.
|
|
|
|
static const char stylesheet[] = "@define-color fiv-tile @content_view_bg; \
|
|
|
|
fiv-view, fiv-browser { background: @content_view_bg; } \
|
|
|
|
placessidebar.fiv .toolbar { padding: 2px 6px; } \
|
|
|
|
placessidebar.fiv box > separator { margin: 4px 0; } \
|
|
|
|
#toolbar button { padding-left: 0; padding-right: 0; } \
|
|
|
|
#toolbar > button:first-child { padding-left: 4px; } \
|
|
|
|
#toolbar > button:last-child { padding-right: 4px; } \
|
|
|
|
#toolbar separator { \
|
|
|
|
background: mix(@insensitive_fg_color, \
|
|
|
|
@insensitive_bg_color, 0.4); margin: 6px 10px; \
|
|
|
|
} \
|
|
|
|
fiv-browser { padding: 5px; } \
|
|
|
|
fiv-browser.item { \
|
|
|
|
color: mix(#000, @content_view_bg, 0.625); margin: 8px; \
|
|
|
|
border: 2px solid #fff; \
|
|
|
|
} \
|
|
|
|
fiv-browser.item, fiv-view.checkerboard { \
|
|
|
|
background: @theme_bg_color; background-image: \
|
|
|
|
linear-gradient(45deg, @fiv-tile 26%, transparent 26%), \
|
|
|
|
linear-gradient(-45deg, @fiv-tile 26%, transparent 26%), \
|
|
|
|
linear-gradient(45deg, transparent 74%, @fiv-tile 74%), \
|
|
|
|
linear-gradient(-45deg, transparent 74%, @fiv-tile 74%); \
|
|
|
|
background-size: 40px 40px; \
|
|
|
|
background-position: 0 0, 0 20px, 20px -20px, -20px 0px; \
|
|
|
|
} \
|
|
|
|
fiv-browser.item:backdrop { \
|
|
|
|
color: mix(#000, @content_view_bg, 0.875); \
|
|
|
|
border-color: mix(#fff, @content_view_bg, 0.5); \
|
|
|
|
} \
|
|
|
|
fiv-browser.item.symbolic { \
|
|
|
|
border-color: transparent; color: shade(@theme_bg_color, 0.875); \
|
|
|
|
background: @theme_bg_color; background-image: none; \
|
|
|
|
}";
|
|
|
|
|
2021-07-14 07:09:19 +02:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2021-11-22 20:36:41 +01:00
|
|
|
gboolean show_version = FALSE, show_supported_media_types = FALSE,
|
|
|
|
browse = FALSE;
|
2021-09-17 20:42:37 +02:00
|
|
|
gchar **path_args = NULL;
|
2021-07-14 07:09:19 +02:00
|
|
|
const GOptionEntry options[] = {
|
2021-09-17 20:42:37 +02:00
|
|
|
{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &path_args,
|
|
|
|
NULL, "[FILE | DIRECTORY]"},
|
2021-11-18 12:44:25 +01:00
|
|
|
{"list-supported-media-types", 0, G_OPTION_FLAG_IN_MAIN,
|
|
|
|
G_OPTION_ARG_NONE, &show_supported_media_types,
|
|
|
|
"Output supported media types and exit", NULL},
|
2021-11-22 20:36:41 +01:00
|
|
|
{"browse", 0, G_OPTION_FLAG_IN_MAIN,
|
|
|
|
G_OPTION_ARG_NONE, &browse,
|
|
|
|
"Start in filesystem browsing mode", NULL},
|
2021-07-14 07:09:19 +02:00
|
|
|
{"version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
|
2021-11-18 12:44:25 +01:00
|
|
|
&show_version, "Output version information and exit", NULL},
|
2021-07-14 07:09:19 +02:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
GError *error = NULL;
|
2021-11-18 12:44:25 +01:00
|
|
|
gboolean initialized = gtk_init_with_args(
|
|
|
|
&argc, &argv, " - fast image viewer", options, NULL, &error);
|
2021-07-14 07:09:19 +02:00
|
|
|
if (show_version) {
|
|
|
|
printf(PROJECT_NAME " " PROJECT_VERSION "\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2021-11-18 12:44:25 +01:00
|
|
|
if (show_supported_media_types) {
|
2021-12-18 06:38:30 +01:00
|
|
|
for (char **types = fiv_io_all_supported_media_types(); *types; )
|
2021-11-18 12:44:25 +01:00
|
|
|
g_print("%s\n", *types++);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!initialized)
|
|
|
|
exit_fatal("%s", error->message);
|
2021-07-14 07:09:19 +02:00
|
|
|
|
2021-09-17 20:42:37 +02:00
|
|
|
// NOTE: Firefox and Eye of GNOME both interpret multiple arguments
|
|
|
|
// in a special way. This is problematic, because one-element lists
|
|
|
|
// are unrepresentable.
|
|
|
|
// TODO(p): Complain to the user if there's more than one argument.
|
|
|
|
// Best show the help message, if we can figure that out.
|
|
|
|
const gchar *path_arg = path_args ? path_args[0] : NULL;
|
|
|
|
|
2021-09-16 17:34:28 +02:00
|
|
|
gtk_window_set_default_icon_name(PROJECT_NAME);
|
2021-11-20 20:55:24 +01:00
|
|
|
gtk_icon_theme_add_resource_path(
|
|
|
|
gtk_icon_theme_get_default(), "/org/gnome/design/IconLibrary/");
|
2021-09-16 17:34:28 +02:00
|
|
|
|
2021-10-17 12:44:23 +02:00
|
|
|
GtkCssProvider *provider = gtk_css_provider_new();
|
2021-12-21 08:36:11 +01:00
|
|
|
gtk_css_provider_load_from_data(
|
|
|
|
provider, stylesheet, sizeof stylesheet - 1, NULL);
|
2021-10-17 12:44:23 +02:00
|
|
|
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
|
|
|
|
GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
2021-11-23 17:12:31 +01:00
|
|
|
g_object_unref(provider);
|
2021-10-17 12:44:23 +02:00
|
|
|
|
2021-11-29 00:35:39 +01:00
|
|
|
GtkWidget *view_scroller = gtk_scrolled_window_new(NULL, NULL);
|
2021-12-18 06:38:30 +01:00
|
|
|
g.view = g_object_new(FIV_TYPE_VIEW, NULL);
|
2021-11-13 09:40:10 +01:00
|
|
|
g_signal_connect(g.view, "key-press-event",
|
|
|
|
G_CALLBACK(on_key_press_view), NULL);
|
2021-11-13 10:04:40 +01:00
|
|
|
g_signal_connect(g.view, "button-press-event",
|
|
|
|
G_CALLBACK(on_button_press_view), NULL);
|
2021-11-29 00:35:39 +01:00
|
|
|
gtk_container_add(GTK_CONTAINER(view_scroller), g.view);
|
2021-10-17 15:38:27 +02:00
|
|
|
|
2021-11-29 00:35:39 +01:00
|
|
|
// Need to put the toolbar at the top, because of the horizontal scrollbar.
|
|
|
|
g.view_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
2021-12-20 12:06:32 +01:00
|
|
|
g.view_toolbar = make_view_toolbar();
|
|
|
|
gtk_box_pack_start(GTK_BOX(g.view_box), g.view_toolbar, FALSE, FALSE, 0);
|
2021-11-29 00:35:39 +01:00
|
|
|
gtk_box_pack_start(GTK_BOX(g.view_box),
|
|
|
|
gtk_separator_new(GTK_ORIENTATION_VERTICAL), FALSE, FALSE, 0);
|
|
|
|
gtk_box_pack_start(GTK_BOX(g.view_box), view_scroller, TRUE, TRUE, 0);
|
|
|
|
gtk_widget_show_all(g.view_box);
|
|
|
|
|
2021-10-17 15:38:27 +02:00
|
|
|
g.browser_scroller = gtk_scrolled_window_new(NULL, NULL);
|
2021-12-18 06:38:30 +01:00
|
|
|
g.browser = g_object_new(FIV_TYPE_BROWSER, NULL);
|
2021-10-17 15:38:27 +02:00
|
|
|
gtk_widget_set_vexpand(g.browser, TRUE);
|
|
|
|
gtk_widget_set_hexpand(g.browser, TRUE);
|
2021-11-08 08:00:18 +01:00
|
|
|
g_signal_connect(g.browser, "item-activated",
|
|
|
|
G_CALLBACK(on_item_activated), NULL);
|
2021-10-17 15:38:27 +02:00
|
|
|
gtk_container_add(GTK_CONTAINER(g.browser_scroller), g.browser);
|
2021-11-18 10:35:58 +01:00
|
|
|
|
2021-11-22 12:06:40 +01:00
|
|
|
// Christ, no, do not scroll all the way to the top on focus.
|
2021-11-22 01:43:15 +01:00
|
|
|
GtkWidget *browser_port = gtk_bin_get_child(GTK_BIN(g.browser_scroller));
|
|
|
|
gtk_container_set_focus_hadjustment(GTK_CONTAINER(browser_port), NULL);
|
|
|
|
gtk_container_set_focus_vadjustment(GTK_CONTAINER(browser_port), NULL);
|
|
|
|
|
2021-12-21 08:36:11 +01:00
|
|
|
// TODO(p): As with GtkFileChooserWidget, bind C-h to filtering,
|
|
|
|
// and mayhaps forward the rest to the sidebar, somehow.
|
2021-12-18 06:38:30 +01:00
|
|
|
g.browser_sidebar = g_object_new(FIV_TYPE_SIDEBAR, NULL);
|
2021-11-18 10:35:58 +01:00
|
|
|
g_signal_connect(g.browser_sidebar, "open-location",
|
|
|
|
G_CALLBACK(on_open_location), NULL);
|
|
|
|
|
2021-11-22 12:06:40 +01:00
|
|
|
// The opposite case, and it doesn't work from the init function.
|
|
|
|
GtkWidget *sidebar_port = gtk_bin_get_child(GTK_BIN(g.browser_sidebar));
|
|
|
|
gtk_container_set_focus_hadjustment(GTK_CONTAINER(sidebar_port),
|
|
|
|
gtk_scrolled_window_get_hadjustment(
|
|
|
|
GTK_SCROLLED_WINDOW(g.browser_sidebar)));
|
|
|
|
gtk_container_set_focus_vadjustment(GTK_CONTAINER(sidebar_port),
|
|
|
|
gtk_scrolled_window_get_vadjustment(
|
|
|
|
GTK_SCROLLED_WINDOW(g.browser_sidebar)));
|
|
|
|
|
2021-11-21 16:03:54 +01:00
|
|
|
g.plus = gtk_button_new_from_icon_name("zoom-in-symbolic",
|
2021-11-21 11:00:25 +01:00
|
|
|
GTK_ICON_SIZE_BUTTON);
|
2021-11-21 16:03:54 +01:00
|
|
|
gtk_widget_set_tooltip_text(g.plus, "Larger thumbnails");
|
|
|
|
g_signal_connect(g.plus, "clicked",
|
|
|
|
G_CALLBACK(on_toolbar_zoom), (gpointer) +1);
|
|
|
|
|
|
|
|
g.minus = gtk_button_new_from_icon_name("zoom-out-symbolic",
|
2021-11-21 11:00:25 +01:00
|
|
|
GTK_ICON_SIZE_BUTTON);
|
2021-11-21 16:03:54 +01:00
|
|
|
gtk_widget_set_tooltip_text(g.minus, "Smaller thumbnails");
|
|
|
|
g_signal_connect(g.minus, "clicked",
|
|
|
|
G_CALLBACK(on_toolbar_zoom), (gpointer) -1);
|
2021-11-21 11:00:25 +01:00
|
|
|
|
|
|
|
GtkWidget *zoom_group = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
|
|
|
|
gtk_style_context_add_class(
|
|
|
|
gtk_widget_get_style_context(zoom_group), GTK_STYLE_CLASS_LINKED);
|
2021-11-21 16:03:54 +01:00
|
|
|
gtk_box_pack_start(GTK_BOX(zoom_group), g.plus, FALSE, FALSE, 0);
|
|
|
|
gtk_box_pack_start(GTK_BOX(zoom_group), g.minus, FALSE, FALSE, 0);
|
2021-11-21 11:00:25 +01:00
|
|
|
|
|
|
|
GtkWidget *funnel = gtk_toggle_button_new();
|
|
|
|
gtk_container_add(GTK_CONTAINER(funnel),
|
|
|
|
gtk_image_new_from_icon_name("funnel-symbolic", GTK_ICON_SIZE_BUTTON));
|
|
|
|
gtk_widget_set_tooltip_text(funnel, "Hide unsupported files");
|
2021-11-21 16:03:54 +01:00
|
|
|
g_signal_connect(funnel, "toggled",
|
|
|
|
G_CALLBACK(on_filtering_toggled), NULL);
|
2021-11-21 11:00:25 +01:00
|
|
|
|
2021-12-18 06:38:30 +01:00
|
|
|
GtkBox *toolbar = fiv_sidebar_get_toolbar(FIV_SIDEBAR(g.browser_sidebar));
|
2021-11-21 11:00:25 +01:00
|
|
|
gtk_box_pack_start(toolbar, zoom_group, FALSE, FALSE, 0);
|
|
|
|
gtk_box_pack_start(toolbar, funnel, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_set_halign(GTK_WIDGET(toolbar), GTK_ALIGN_CENTER);
|
|
|
|
|
2021-11-18 10:35:58 +01:00
|
|
|
g.browser_paned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
|
|
|
|
gtk_paned_add1(GTK_PANED(g.browser_paned), g.browser_sidebar);
|
|
|
|
gtk_paned_add2(GTK_PANED(g.browser_paned), g.browser_scroller);
|
2021-12-19 09:59:27 +01:00
|
|
|
g_signal_connect(g.browser_paned, "button-press-event",
|
|
|
|
G_CALLBACK(on_button_press_browser_paned), NULL);
|
2021-11-18 10:35:58 +01:00
|
|
|
|
2021-10-17 15:38:27 +02:00
|
|
|
// TODO(p): Can we not do it here separately?
|
2021-11-18 10:35:58 +01:00
|
|
|
gtk_widget_show_all(g.browser_paned);
|
2021-10-17 15:38:27 +02:00
|
|
|
|
2021-11-08 08:00:18 +01:00
|
|
|
g.stack = gtk_stack_new();
|
2021-10-17 15:38:27 +02:00
|
|
|
gtk_stack_set_transition_type(
|
2021-11-08 08:00:18 +01:00
|
|
|
GTK_STACK(g.stack), GTK_STACK_TRANSITION_TYPE_NONE);
|
2021-11-29 00:35:39 +01:00
|
|
|
gtk_container_add(GTK_CONTAINER(g.stack), g.view_box);
|
2021-11-18 10:35:58 +01:00
|
|
|
gtk_container_add(GTK_CONTAINER(g.stack), g.browser_paned);
|
2021-10-17 15:38:27 +02:00
|
|
|
|
2021-09-17 19:48:16 +02:00
|
|
|
g.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
2021-11-01 06:54:36 +01:00
|
|
|
g_signal_connect(g.window, "destroy",
|
|
|
|
G_CALLBACK(gtk_main_quit), NULL);
|
|
|
|
g_signal_connect(g.window, "key-press-event",
|
|
|
|
G_CALLBACK(on_key_press), NULL);
|
2021-11-29 00:35:39 +01:00
|
|
|
g_signal_connect(g.window, "window-state-event",
|
|
|
|
G_CALLBACK(on_window_state_event), NULL);
|
2021-11-08 08:00:18 +01:00
|
|
|
gtk_container_add(GTK_CONTAINER(g.window), g.stack);
|
2021-09-16 19:43:36 +02:00
|
|
|
|
2021-12-18 06:38:30 +01:00
|
|
|
char **types = fiv_io_all_supported_media_types();
|
2021-11-18 12:44:25 +01:00
|
|
|
g.supported_globs = extract_mime_globs((const char **) types);
|
|
|
|
g_strfreev(types);
|
|
|
|
|
2021-11-21 16:03:54 +01:00
|
|
|
g_signal_connect(g.browser, "notify::thumbnail-size",
|
|
|
|
G_CALLBACK(on_notify_thumbnail_size), NULL);
|
|
|
|
on_toolbar_zoom(NULL, (gpointer) 0);
|
2021-11-21 11:00:25 +01:00
|
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(funnel), TRUE);
|
|
|
|
|
2021-09-17 20:42:37 +02:00
|
|
|
g.files = g_ptr_array_new_full(16, g_free);
|
2021-11-18 22:00:08 +01:00
|
|
|
g.directory = g_get_current_dir();
|
2021-11-22 20:36:41 +01:00
|
|
|
if (!path_arg || !open_any_path(path_arg, browse))
|
|
|
|
open_any_path(g.directory, FALSE);
|
2021-10-17 15:38:27 +02:00
|
|
|
|
2021-11-18 21:51:12 +01:00
|
|
|
// Try to get half of the screen vertically, in 4:3 aspect ratio.
|
|
|
|
//
|
|
|
|
// We need the GdkMonitor before the GtkWindow has a GdkWindow (i.e.,
|
|
|
|
// before it is realized). Take the smallest dimensions, out of desperation.
|
|
|
|
GdkDisplay *display = gtk_widget_get_display(g.window);
|
|
|
|
int unit = G_MAXINT;
|
|
|
|
for (int i = gdk_display_get_n_monitors(display); i--; ) {
|
|
|
|
GdkRectangle geometry = {};
|
|
|
|
gdk_monitor_get_geometry(
|
|
|
|
gdk_display_get_monitor(display, i), &geometry);
|
|
|
|
unit = MIN(unit, MIN(geometry.width, geometry.height) / 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for at least 800x600, to cover ridiculously heterogenous setups.
|
|
|
|
unit = MAX(200, unit);
|
|
|
|
gtk_window_set_default_size(GTK_WINDOW(g.window), 4 * unit, 3 * unit);
|
|
|
|
|
2021-09-17 19:48:16 +02:00
|
|
|
gtk_widget_show_all(g.window);
|
2021-07-14 07:09:19 +02:00
|
|
|
gtk_main();
|
|
|
|
return 0;
|
|
|
|
}
|