[gtk+/wip/native-file-chooser: 15/15] gtk3-demo: Add open file to the application demo
- From: Alexander Larsson <alexl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/wip/native-file-chooser: 15/15] gtk3-demo: Add open file to the application demo
- Date: Thu, 5 Nov 2015 15:28:59 +0000 (UTC)
commit ca2042e1668424ebd51d0ece72945642e6a05bed
Author: Alexander Larsson <alexl redhat com>
Date: Thu Nov 5 16:25:59 2015 +0100
gtk3-demo: Add open file to the application demo
demos/gtk-demo/application.c | 90 ++++++++++++++++++++++++++++++++++++++++-
1 files changed, 87 insertions(+), 3 deletions(-)
---
diff --git a/demos/gtk-demo/application.c b/demos/gtk-demo/application.c
index 7ae64f0..02c12f9 100644
--- a/demos/gtk-demo/application.c
+++ b/demos/gtk-demo/application.c
@@ -13,6 +13,8 @@
#ifdef STANDALONE
+static void create_window (GApplication *app, const char *contents);
+
static void
show_action_dialog (GSimpleAction *action)
{
@@ -66,6 +68,79 @@ activate_action (GSimpleAction *action,
}
static void
+activate_new (GSimpleAction *action,
+ GVariant *parameter,
+ gpointer user_data)
+{
+ GApplication *app = user_data;
+
+ create_window (app, NULL);
+}
+
+static void
+open_response_cb (GtkNativeDialog *dialog,
+ gint response_id,
+ gpointer user_data)
+{
+ GtkFileChooserNative *native = user_data;
+ GApplication *app = g_object_get_data (G_OBJECT (native), "app");
+ GtkWidget *message_dialog;
+ GFile *file;
+ char *contents;
+ GError *error = NULL;
+
+ if (response_id == GTK_RESPONSE_ACCEPT)
+ {
+ file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (native));
+
+ if (g_file_load_contents (file, NULL, &contents, NULL, NULL, &error))
+ {
+ create_window (app, contents);
+ g_free (contents);
+ }
+ else
+ {
+ message_dialog = gtk_message_dialog_new (NULL,
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_ERROR,
+ GTK_BUTTONS_CLOSE,
+ "Error loading file: \"%s\"",
+ error->message);
+ g_signal_connect (message_dialog, "response",
+ G_CALLBACK (gtk_widget_destroy), NULL);
+ gtk_widget_show (message_dialog);
+ g_error_free (error);
+ }
+ }
+
+ g_object_unref (native);
+}
+
+
+static void
+activate_open (GSimpleAction *action,
+ GVariant *parameter,
+ gpointer user_data)
+{
+ GApplication *app = user_data;
+ GtkFileChooserNative *native;
+
+ native = gtk_file_chooser_native_new ("Open File",
+ NULL,
+ GTK_FILE_CHOOSER_ACTION_OPEN,
+ "_Open",
+ "_Cancel");
+
+ g_object_set_data_full (G_OBJECT (native), "app", g_object_ref (app), g_object_unref);
+ g_signal_connect (native,
+ "response",
+ G_CALLBACK (open_response_cb),
+ native);
+
+ gtk_native_dialog_show (GTK_NATIVE_DIALOG (native));
+}
+
+static void
activate_toggle (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
@@ -226,8 +301,8 @@ change_radio_state (GSimpleAction *action,
}
static GActionEntry app_entries[] = {
- { "new", activate_action, NULL, NULL, NULL },
- { "open", activate_action, NULL, NULL, NULL },
+ { "new", activate_new, NULL, NULL, NULL },
+ { "open", activate_open, NULL, NULL, NULL },
{ "save", activate_action, NULL, NULL, NULL },
{ "save-as", activate_action, NULL, NULL, NULL },
{ "quit", activate_quit, NULL, NULL, NULL },
@@ -269,7 +344,8 @@ startup (GApplication *app)
}
static void
-activate (GApplication *app)
+create_window (GApplication *app,
+ const char *content_text)
{
GtkBuilder *builder;
GtkWidget *window;
@@ -317,6 +393,8 @@ activate (GApplication *app)
/* Show text widget info in the statusbar */
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
+ if (content_text)
+ gtk_text_buffer_set_text (buffer, content_text, -1);
g_signal_connect_object (buffer, "changed",
G_CALLBACK (update_statusbar), status, 0);
g_signal_connect_object (buffer, "mark-set",
@@ -329,6 +407,12 @@ activate (GApplication *app)
g_object_unref (builder);
}
+static void
+activate (GApplication *app)
+{
+ create_window (app, NULL);
+}
+
int
main (int argc, char *argv[])
{
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]