[gtk/constraint-list-model] wip: make constraint editor handle files



commit 946cb70937368831effcdebfcdb42ba73128b783
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Jul 1 18:39:36 2019 +0000

    wip: make constraint editor handle files

 .../constraint-editor-application.c                | 21 +++++
 demos/constraint-editor/constraint-editor-window.c | 97 ++++++++++++++++++++++
 .../constraint-editor/constraint-editor-window.ui  | 14 ++++
 3 files changed, 132 insertions(+)
---
diff --git a/demos/constraint-editor/constraint-editor-application.c 
b/demos/constraint-editor/constraint-editor-application.c
index 09b5d64270..aa130d9c13 100644
--- a/demos/constraint-editor/constraint-editor-application.c
+++ b/demos/constraint-editor/constraint-editor-application.c
@@ -51,6 +51,7 @@ static void
 constraint_editor_application_startup (GApplication *app)
 {
   const char *quit_accels[2] = { "<Ctrl>Q", NULL };
+  const char *open_accels[2] = { "<Ctrl>O", NULL };
   GtkCssProvider *provider;
 
   G_APPLICATION_CLASS (constraint_editor_application_parent_class)->startup (app);
@@ -59,6 +60,7 @@ constraint_editor_application_startup (GApplication *app)
                                    app_entries, G_N_ELEMENTS (app_entries),
                                    app);
   gtk_application_set_accels_for_action (GTK_APPLICATION (app), "app.quit", quit_accels);
+  gtk_application_set_accels_for_action (GTK_APPLICATION (app), "win.open", open_accels);
 
   provider = gtk_css_provider_new ();
   gtk_css_provider_load_from_resource (provider, "/org/gtk/gtk4/constraint-editor/constraint-editor.css");
@@ -76,6 +78,23 @@ constraint_editor_application_activate (GApplication *app)
   gtk_window_present (GTK_WINDOW (win));
 }
 
+static void
+constraint_editor_application_open (GApplication  *app,
+                                    GFile        **files,
+                                    gint           n_files,
+                                    const gchar   *hint)
+{
+  ConstraintEditorWindow *win;
+  gint i;
+
+  for (i = 0; i < n_files; i++)
+    {
+      win = constraint_editor_window_new (CONSTRAINT_EDITOR_APPLICATION (app));
+      constraint_editor_window_load (win, files[i]);
+      gtk_window_present (GTK_WINDOW (win));
+    }
+}
+
 static void
 constraint_editor_application_class_init (ConstraintEditorApplicationClass *class)
 {
@@ -83,6 +102,7 @@ constraint_editor_application_class_init (ConstraintEditorApplicationClass *clas
 
   application_class->startup = constraint_editor_application_startup;
   application_class->activate = constraint_editor_application_activate;
+  application_class->open = constraint_editor_application_open;
 }
 
 ConstraintEditorApplication *
@@ -90,5 +110,6 @@ constraint_editor_application_new (void)
 {
   return g_object_new (CONSTRAINT_EDITOR_APPLICATION_TYPE,
                        "application-id", "org.gtk.gtk4.ConstraintEditor",
+                       "flags", G_APPLICATION_HANDLES_OPEN,
                        NULL);
 }
diff --git a/demos/constraint-editor/constraint-editor-window.c 
b/demos/constraint-editor/constraint-editor-window.c
index bd9fd3e171..c6e4fa42e7 100644
--- a/demos/constraint-editor/constraint-editor-window.c
+++ b/demos/constraint-editor/constraint-editor-window.c
@@ -65,6 +65,101 @@ constraint_editor_window_load (ConstraintEditorWindow *self,
   return TRUE;
 }
 
+static void
+open_response_cb (GtkNativeDialog        *dialog,
+                  gint                    response,
+                  ConstraintEditorWindow *self)
+{
+  gtk_native_dialog_hide (dialog);
+
+  if (response == GTK_RESPONSE_ACCEPT)
+    {
+      GFile *file;
+
+      file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
+      constraint_editor_window_load (self, file);
+      g_object_unref (file);
+    }
+
+  gtk_native_dialog_destroy (dialog);
+}
+
+static void
+open_cb (GtkWidget              *button,
+         ConstraintEditorWindow *self)
+{
+  GtkFileChooserNative *dialog;
+
+  dialog = gtk_file_chooser_native_new ("Open file",
+                                        GTK_WINDOW (self),
+                                        GTK_FILE_CHOOSER_ACTION_OPEN,
+                                        "_Load",
+                                        "_Cancel");
+
+  gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG (dialog), TRUE);
+  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), ".");
+  g_signal_connect (dialog, "response", G_CALLBACK (open_response_cb), self);
+  gtk_native_dialog_show (GTK_NATIVE_DIALOG (dialog));
+}
+
+static void
+save_response_cb (GtkNativeDialog        *dialog,
+                  gint                    response,
+                  ConstraintEditorWindow *self)
+{
+  gtk_native_dialog_hide (dialog);
+
+  if (response == GTK_RESPONSE_ACCEPT)
+    {
+      char *text, *filename;
+      GError *error = NULL;
+
+#if 0
+      text = get_current_text (self->text_buffer);
+#else
+      text = "Sorry, Dave";
+#endif
+
+      filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+      if (!g_file_set_contents (filename, text, -1, &error))
+        {
+          GtkWidget *dialog;
+
+          dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (self))),
+                                           GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
+                                           GTK_MESSAGE_INFO,
+                                           GTK_BUTTONS_OK,
+                                           "Saving failed");
+          gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+                                                    "%s", error->message);
+          g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
+          gtk_widget_show (dialog);
+          g_error_free (error);
+        }
+      g_free (filename);
+    }
+
+  gtk_native_dialog_destroy (dialog);
+}
+
+static void
+save_cb (GtkWidget              *button,
+         ConstraintEditorWindow *self)
+{
+  GtkFileChooserNative *dialog;
+
+  dialog = gtk_file_chooser_native_new ("Save constraints",
+                                        GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (button))),
+                                        GTK_FILE_CHOOSER_ACTION_SAVE,
+                                        "_Save",
+                                        "_Cancel");
+
+  gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG (dialog), TRUE);
+  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), ".");
+  g_signal_connect (dialog, "response", G_CALLBACK (save_response_cb), self);
+  gtk_native_dialog_show (GTK_NATIVE_DIALOG (dialog));
+}
+
 static void
 constraint_editor_window_finalize (GObject *object)
 {
@@ -213,6 +308,8 @@ constraint_editor_window_class_init (ConstraintEditorWindowClass *class)
   gtk_widget_class_bind_template_child (widget_class, ConstraintEditorWindow, view);
   gtk_widget_class_bind_template_child (widget_class, ConstraintEditorWindow, list);
 
+  gtk_widget_class_bind_template_callback (widget_class, open_cb);
+  gtk_widget_class_bind_template_callback (widget_class, save_cb);
   gtk_widget_class_bind_template_callback (widget_class, add_child);
   gtk_widget_class_bind_template_callback (widget_class, add_guide);
   gtk_widget_class_bind_template_callback (widget_class, add_constraint);
diff --git a/demos/constraint-editor/constraint-editor-window.ui 
b/demos/constraint-editor/constraint-editor-window.ui
index c2c9a009ec..0d2e72fcea 100644
--- a/demos/constraint-editor/constraint-editor-window.ui
+++ b/demos/constraint-editor/constraint-editor-window.ui
@@ -11,6 +11,20 @@
       <object class="GtkHeaderBar" id="header">
         <property name="title" translatable="yes">GTK Constraint Editor</property>
         <property name="show-title-buttons">1</property>
+        <child type="start">
+          <object class="GtkButton">
+            <property name="icon-name">document-open-symbolic</property>
+            <property name="tooltip-text">Open ui file</property>
+            <signal name="clicked" handler="open_cb"/>
+          </object>
+        </child>
+        <child type="start">
+          <object class="GtkButton">
+            <property name="icon-name">document-save-symbolic</property>
+            <property name="tooltip-text">Save to ui file</property>
+            <signal name="clicked" handler="save_cb"/>
+          </object>
+        </child>
       </object>
     </child>
     <child>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]