aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2021-09-17 19:48:16 +0200
committerPřemysl Eric Janouch <p@janouch.name>2021-09-17 20:08:56 +0200
commitbe6f3dfe991ed6f8d4868a215415dd9ecb6a0c5c (patch)
treec7d5e022c81d6851ca1a571249f031779dcbd8e0
parentddcc878424ebb8879ace2bd8238b7bd92d7143cf (diff)
downloadfiv-be6f3dfe991ed6f8d4868a215415dd9ecb6a0c5c.tar.gz
fiv-be6f3dfe991ed6f8d4868a215415dd9ecb6a0c5c.tar.xz
fiv-be6f3dfe991ed6f8d4868a215415dd9ecb6a0c5c.zip
Invoke a file open dialog on C-o/o
-rw-r--r--fastiv.c59
1 files changed, 47 insertions, 12 deletions
diff --git a/fastiv.c b/fastiv.c
index 98bdf91..cc3e2ac 100644
--- a/fastiv.c
+++ b/fastiv.c
@@ -48,10 +48,44 @@ exit_fatal(const gchar *format, ...)
// --- Main --------------------------------------------------------------------
struct {
- // TODO(p): Add some state variables.
- // - Directory filenames, index within the list.
+ // TODO(p): Directory filenames, index within the list, ...
+ GtkWidget *window;
+ GtkWidget *view;
} g;
+static void
+open(const gchar *path)
+{
+ GError *error = NULL;
+ if (!fastiv_view_open(FASTIV_VIEW(g.view), path, &error)) {
+ GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(g.window),
+ GTK_DIALOG_MODAL,
+ GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", error->message);
+ gtk_dialog_run(GTK_DIALOG(dialog));
+ gtk_widget_destroy(dialog);
+ return;
+ }
+
+ gtk_window_set_title(GTK_WINDOW(g.window), path);
+}
+
+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);
+
+ 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);
+}
+
int
main(int argc, char *argv[])
{
@@ -79,10 +113,10 @@ main(int argc, char *argv[])
gtk_window_set_default_icon_name(PROJECT_NAME);
- GtkWidget *view = g_object_new(FASTIV_TYPE_VIEW, NULL);
- GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
- gtk_container_add(GTK_CONTAINER(window), view);
+ g.view = g_object_new(FASTIV_TYPE_VIEW, NULL);
+ 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);
// The references to closures are initially floating and sunk on connect.
GtkAccelGroup *accel_group = gtk_accel_group_new();
@@ -90,7 +124,11 @@ main(int argc, char *argv[])
g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
gtk_accel_group_connect(accel_group, GDK_KEY_q, 0, 0,
g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
- gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
+ gtk_accel_group_connect(accel_group, GDK_KEY_o, 0, 0,
+ g_cclosure_new(G_CALLBACK(on_open), NULL, NULL));
+ gtk_accel_group_connect(accel_group, GDK_KEY_o, GDK_CONTROL_MASK, 0,
+ g_cclosure_new(G_CALLBACK(on_open), NULL, NULL));
+ gtk_window_add_accel_group(GTK_WINDOW(g.window), accel_group);
// TODO(p): Load directory entries, store in `g`.
// - Only when there's just one filename.
@@ -112,13 +150,10 @@ main(int argc, char *argv[])
g_dir_close(dir);
}
- if (!fastiv_view_open(FASTIV_VIEW(view), files[0], &error))
- g_printerr("error: %s\n", error->message);
- else
- gtk_window_set_title(GTK_WINDOW(window), files[0]);
+ open(files[0]);
}
- gtk_widget_show_all(window);
+ gtk_widget_show_all(g.window);
gtk_main();
return 0;
}