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
|
|
|
|
|
|
|
#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-11-01 04:59:38 +01:00
|
|
|
#include "fastiv-browser.h"
|
2021-11-01 04:40:58 +01:00
|
|
|
#include "fastiv-io.h"
|
2021-07-14 07:09:19 +02:00
|
|
|
#include "fastiv-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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Main --------------------------------------------------------------------
|
|
|
|
|
|
|
|
struct {
|
2021-09-17 20:42:37 +02:00
|
|
|
gchar **supported_globs;
|
|
|
|
|
|
|
|
gchar *directory;
|
|
|
|
GPtrArray *files;
|
|
|
|
gint files_index;
|
|
|
|
|
|
|
|
gchar *basename;
|
|
|
|
|
2021-09-17 19:48:16 +02:00
|
|
|
GtkWidget *window;
|
2021-11-08 08:00:18 +01:00
|
|
|
GtkWidget *stack;
|
2021-09-17 19:48:16 +02:00
|
|
|
GtkWidget *view;
|
2021-11-09 06:55:00 +01:00
|
|
|
GtkWidget *view_scroller;
|
2021-10-17 15:38:27 +02:00
|
|
|
GtkWidget *browser;
|
|
|
|
GtkWidget *browser_scroller;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_directory(const gchar *dirname)
|
|
|
|
{
|
|
|
|
free(g.directory);
|
|
|
|
g.directory = g_strdup(dirname);
|
|
|
|
g_ptr_array_set_size(g.files, 0);
|
|
|
|
g.files_index = -1;
|
|
|
|
|
2021-10-17 15:38:27 +02:00
|
|
|
fastiv_browser_load(FASTIV_BROWSER(g.browser), dirname);
|
|
|
|
|
2021-09-17 20:42:37 +02:00
|
|
|
GError *error = NULL;
|
|
|
|
GDir *dir = g_dir_open(dirname, 0, &error);
|
|
|
|
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);
|
|
|
|
if (is_dir || !is_supported(name))
|
2021-09-17 20:42:37 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// XXX: We presume that this basename is from the same directory.
|
|
|
|
if (!g_strcmp0(g.basename, name))
|
|
|
|
g.files_index = g.files->len;
|
|
|
|
|
|
|
|
g_ptr_array_add(g.files, g_strdup(name));
|
|
|
|
}
|
|
|
|
g_dir_close(dir);
|
|
|
|
} else {
|
|
|
|
show_error_dialog(error);
|
|
|
|
}
|
|
|
|
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-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;
|
|
|
|
if (!fastiv_view_open(FASTIV_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-10 03:15:42 +01:00
|
|
|
gtk_recent_manager_add_item(gtk_recent_manager_get_default(), path);
|
2021-09-17 19:48:16 +02:00
|
|
|
gtk_window_set_title(GTK_WINDOW(g.window), path);
|
2021-11-09 06:55:00 +01:00
|
|
|
gtk_stack_set_visible_child(GTK_STACK(g.stack), g.view_scroller);
|
2021-09-17 20:42:37 +02:00
|
|
|
|
|
|
|
gchar *basename = g_path_get_basename(path);
|
|
|
|
g_free(g.basename);
|
|
|
|
g.basename = basename;
|
|
|
|
|
|
|
|
// So that load_directory() itself can be used for reloading.
|
|
|
|
gchar *dirname = g_path_get_dirname(path);
|
|
|
|
if (!g.directory || strcmp(dirname, g.directory)) {
|
|
|
|
load_directory(dirname);
|
|
|
|
} else {
|
|
|
|
g.files_index = -1;
|
|
|
|
for (guint i = 0; i + 1 < g.files->len; i++) {
|
|
|
|
if (!g_strcmp0(g.basename, g_ptr_array_index(g.files, i)))
|
|
|
|
g.files_index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(dirname);
|
2021-09-17 19:48:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_open(void)
|
|
|
|
{
|
|
|
|
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-11-01 04:40:58 +01:00
|
|
|
for (const char **p = fastiv_io_supported_media_types; *p; p++)
|
|
|
|
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-09-17 20:42:37 +02:00
|
|
|
// The default is local-only, single item. Paths are returned absolute.
|
2021-09-17 19:48:16 +02:00
|
|
|
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
|
|
|
|
gchar *path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
|
|
|
|
open(path);
|
|
|
|
g_free(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_destroy(dialog);
|
|
|
|
}
|
|
|
|
|
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-08 08:00:18 +01:00
|
|
|
static void
|
|
|
|
on_item_activated(G_GNUC_UNUSED FastivBrowser *browser, const char *path,
|
|
|
|
G_GNUC_UNUSED gpointer data)
|
|
|
|
{
|
|
|
|
open(path);
|
|
|
|
}
|
|
|
|
|
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-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-10 03:13:33 +01:00
|
|
|
case GDK_KEY_n: {
|
|
|
|
char *argv[] = {PROJECT_NAME, g.directory, NULL};
|
|
|
|
GError *error = NULL;
|
|
|
|
g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
|
|
|
|
NULL, &error);
|
|
|
|
g_clear_error(&error);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2021-11-01 06:54:36 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
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:
|
|
|
|
gtk_main_quit();
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case GDK_KEY_o:
|
|
|
|
on_open();
|
|
|
|
return TRUE;
|
|
|
|
|
2021-11-13 09:40:10 +01:00
|
|
|
case GDK_KEY_Tab:
|
|
|
|
case GDK_KEY_Return:
|
|
|
|
gtk_stack_set_visible_child(GTK_STACK(g.stack),
|
|
|
|
gtk_stack_get_visible_child(GTK_STACK(g.stack)) ==
|
|
|
|
g.view_scroller
|
|
|
|
? g.browser_scroller
|
|
|
|
: g.view_scroller);
|
|
|
|
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-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:
|
|
|
|
case GDK_KEY_space:
|
|
|
|
on_next();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-11-13 10:04:40 +01:00
|
|
|
static gboolean
|
|
|
|
on_button_press_view(G_GNUC_UNUSED GtkWidget *widget, GdkEventButton *event)
|
|
|
|
{
|
|
|
|
if (!(event->state & gtk_accelerator_get_default_mod_mask()) &&
|
|
|
|
event->button == 8 /* back */) {
|
|
|
|
gtk_stack_set_visible_child(GTK_STACK(g.stack), g.browser_scroller);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-07-14 07:09:19 +02:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
gboolean show_version = 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-07-14 07:09:19 +02:00
|
|
|
{"version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
|
2021-11-01 04:59:38 +01:00
|
|
|
&show_version, "output version information and exit", NULL},
|
2021-07-14 07:09:19 +02:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
GError *error = NULL;
|
2021-11-01 04:59:38 +01:00
|
|
|
if (!gtk_init_with_args(
|
|
|
|
&argc, &argv, " - fast image viewer", options, NULL, &error))
|
2021-07-14 07:09:19 +02:00
|
|
|
exit_fatal("%s", error->message);
|
|
|
|
if (show_version) {
|
|
|
|
printf(PROJECT_NAME " " PROJECT_VERSION "\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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-10 20:53:24 +01:00
|
|
|
// This is incredibly broken https://stackoverflow.com/a/51054396/76313
|
|
|
|
// thus resolving the problem using overlaps.
|
|
|
|
const char *style = "@define-color fastiv-tile #3c3c3c; \
|
|
|
|
fastiv-view, fastiv-browser { background: #222; } \
|
|
|
|
fastiv-browser { padding: 5px; } \
|
|
|
|
fastiv-browser.item { \
|
|
|
|
border: 1px solid rgba(255, 255, 255, 0.5); \
|
|
|
|
margin: 10px; color: #000; \
|
|
|
|
background: #333; \
|
|
|
|
background-image: \
|
|
|
|
linear-gradient(45deg, @fastiv-tile 26%, transparent 26%), \
|
|
|
|
linear-gradient(-45deg, @fastiv-tile 26%, transparent 26%), \
|
|
|
|
linear-gradient(45deg, transparent 74%, @fastiv-tile 74%), \
|
|
|
|
linear-gradient(-45deg, transparent 74%, @fastiv-tile 74%); \
|
|
|
|
background-size: 40px 40px; \
|
|
|
|
background-position: 0 0, 0 20px, 20px -20px, -20px 0px; \
|
|
|
|
}";
|
|
|
|
|
2021-10-17 12:44:23 +02:00
|
|
|
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);
|
|
|
|
|
2021-11-09 06:55:00 +01:00
|
|
|
g.view_scroller = gtk_scrolled_window_new(NULL, NULL);
|
2021-09-17 19:48:16 +02:00
|
|
|
g.view = g_object_new(FASTIV_TYPE_VIEW, NULL);
|
2021-11-09 06:55:00 +01:00
|
|
|
gtk_widget_set_vexpand(g.view, TRUE);
|
|
|
|
gtk_widget_set_hexpand(g.view, TRUE);
|
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-09 06:55:00 +01:00
|
|
|
gtk_container_add(GTK_CONTAINER(g.view_scroller), g.view);
|
|
|
|
gtk_widget_show_all(g.view_scroller);
|
2021-10-17 15:38:27 +02:00
|
|
|
|
|
|
|
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);
|
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);
|
|
|
|
// TODO(p): Can we not do it here separately?
|
|
|
|
gtk_widget_show_all(g.browser_scroller);
|
|
|
|
|
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-09 06:55:00 +01:00
|
|
|
gtk_container_add(GTK_CONTAINER(g.stack), g.view_scroller);
|
2021-11-08 08:00:18 +01:00
|
|
|
gtk_container_add(GTK_CONTAINER(g.stack), g.browser_scroller);
|
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-08 07:53:37 +01:00
|
|
|
gtk_window_set_default_size (GTK_WINDOW (g.window), 800, 600);
|
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-08 08:00:18 +01:00
|
|
|
gtk_container_add(GTK_CONTAINER(g.window), g.stack);
|
2021-09-16 19:43:36 +02:00
|
|
|
|
2021-11-10 21:33:30 +01:00
|
|
|
// TODO(p): Also milk gdk-pixbuf, if linked in, needs to be done in runtime.
|
2021-11-01 05:08:55 +01:00
|
|
|
g.supported_globs = extract_mime_globs(fastiv_io_supported_media_types);
|
2021-09-17 20:42:37 +02:00
|
|
|
g.files = g_ptr_array_new_full(16, g_free);
|
|
|
|
gchar *cwd = g_get_current_dir();
|
2021-07-14 07:09:19 +02:00
|
|
|
|
2021-09-17 20:42:37 +02:00
|
|
|
GStatBuf st;
|
|
|
|
if (!path_arg) {
|
|
|
|
load_directory(cwd);
|
|
|
|
} else if (g_stat(path_arg, &st)) {
|
2021-11-01 04:59:38 +01:00
|
|
|
show_error_dialog(
|
|
|
|
g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno), "%s: %s",
|
|
|
|
path_arg, g_strerror(errno)));
|
2021-09-17 20:42:37 +02:00
|
|
|
load_directory(cwd);
|
|
|
|
} else {
|
|
|
|
gchar *path_arg_absolute = g_canonicalize_filename(path_arg, cwd);
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
load_directory(path_arg_absolute);
|
|
|
|
else
|
|
|
|
open(path_arg_absolute);
|
|
|
|
g_free(path_arg_absolute);
|
2021-07-14 07:09:19 +02:00
|
|
|
}
|
2021-09-17 20:42:37 +02:00
|
|
|
g_free(cwd);
|
2021-07-14 07:09:19 +02:00
|
|
|
|
2021-10-17 15:38:27 +02:00
|
|
|
if (g.files_index < 0)
|
2021-11-08 08:00:18 +01:00
|
|
|
gtk_stack_set_visible_child(GTK_STACK(g.stack), g.browser_scroller);
|
2021-10-17 15:38:27 +02:00
|
|
|
|
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;
|
|
|
|
}
|