diff options
author | Alexander Larsson <alexl@redhat.com> | 2015-11-05 16:25:59 +0100 |
---|---|---|
committer | Alexander Larsson <alexl@redhat.com> | 2015-11-05 16:54:12 +0100 |
commit | 5744c757c21e359556dc246cd4c87fba218e4d6c (patch) | |
tree | 40df7003bbe0f93ebfdb3463e1da3c2adcad4790 | |
parent | 13c2717d88d46234a61de2035df7c948c3ab157b (diff) | |
download | gtk+-wip/native-file-chooser.tar.gz |
gtk3-demo: Add open file to the application demowip/native-file-chooser
-rw-r--r-- | 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 7ae64f0e63..02c12f95a5 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[]) { |