[gtk/matthiasc/for-master: 4/4] node-editor: Add a help window



commit d4ff275002c2d487ed6a424d5a661c5843a74d64
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Jun 28 23:47:26 2020 -0400

    node-editor: Add a help window
    
    Add a Help item to the gear menu that opens the
    node-format.md file in a new window. This could
    be improved if we could parse markdown and apply
    tags, similar to how we can load pango markup.

 demos/node-editor/help-window.ui            | 27 +++++++++++++++++++++++++
 demos/node-editor/node-editor-application.c | 31 +++++++++++++++++++++++++++++
 demos/node-editor/node-editor-window.ui     |  4 ++++
 demos/node-editor/node-editor.gresource.xml |  2 ++
 4 files changed, 64 insertions(+)
---
diff --git a/demos/node-editor/help-window.ui b/demos/node-editor/help-window.ui
new file mode 100644
index 0000000000..a5387393db
--- /dev/null
+++ b/demos/node-editor/help-window.ui
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <object class="GtkWindow" id="window">
+    <style>
+      <class name="devel"/>
+    </style>
+    <property name="title" translatable="yes">Help</property>
+    <property name="default-width">720</property>
+    <property name="default-height">520</property>
+    <child>
+      <object class="GtkScrolledWindow">
+        <child>
+          <object class="GtkTextView" id="text_view">
+            <property name="wrap-mode">word</property>
+            <property name="left-margin">20</property>
+            <property name="right-margin">20</property>
+            <property name="top-margin">20</property>
+            <property name="bottom-margin">20</property>
+            <property name="buffer">
+              <object class="GtkTextBuffer" id="buffer"/>
+            </property>
+          </object>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>
diff --git a/demos/node-editor/node-editor-application.c b/demos/node-editor/node-editor-application.c
index dd55284ca3..4a0245be8a 100644
--- a/demos/node-editor/node-editor-application.c
+++ b/demos/node-editor/node-editor-application.c
@@ -126,16 +126,46 @@ activate_inspector (GSimpleAction *action,
   gtk_window_set_interactive_debugging (TRUE);
 }
 
+static void
+activate_help (GSimpleAction *action,
+               GVariant      *parameter,
+               gpointer       user_data)
+{
+  GtkBuilder *builder;
+  GtkWidget *window;
+  GtkTextBuffer *buffer;
+  GBytes *bytes;
+  const char *text;
+  gsize len;
+
+  builder = gtk_builder_new ();
+  gtk_builder_add_from_resource (builder, "/org/gtk/gtk4/node-editor/help-window.ui", NULL);
+  window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
+  buffer = GTK_TEXT_BUFFER (gtk_builder_get_object (builder, "buffer"));
+
+  bytes = g_resources_lookup_data ("/org/gtk/gtk4/node-editor/node-format.md",
+                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
+                                   NULL);
+  text = g_bytes_get_data (bytes, &len);
+  gtk_text_buffer_set_text (buffer, text, len);
+  g_bytes_unref (bytes);
+
+  gtk_window_present (GTK_WINDOW (window));
+  g_object_unref (builder);
+}
+
 static GActionEntry app_entries[] =
 {
   { "about", activate_about, NULL, NULL, NULL },
   { "quit", activate_quit, NULL, NULL, NULL },
   { "inspector", activate_inspector, NULL, NULL, NULL },
+  { "help", activate_help, NULL, NULL, NULL },
 };
 
 static void
 node_editor_application_startup (GApplication *app)
 {
+  const char *help_accels[2] = { "F1", NULL };
   const char *quit_accels[2] = { "<Ctrl>Q", NULL };
   const char *open_accels[2] = { "<Ctrl>O", NULL };
   GtkCssProvider *provider;
@@ -145,6 +175,7 @@ node_editor_application_startup (GApplication *app)
   g_action_map_add_action_entries (G_ACTION_MAP (app),
                                    app_entries, G_N_ELEMENTS (app_entries),
                                    app);
+  gtk_application_set_accels_for_action (GTK_APPLICATION (app), "app.help", help_accels);
   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);
 
diff --git a/demos/node-editor/node-editor-window.ui b/demos/node-editor/node-editor-window.ui
index 1b49269ab1..791d8c0f06 100644
--- a/demos/node-editor/node-editor-window.ui
+++ b/demos/node-editor/node-editor-window.ui
@@ -2,6 +2,10 @@
 <interface>
   <menu id="gear_menu">
     <section>
+      <item>
+        <attribute name="label" translatable="yes">_Help</attribute>
+        <attribute name="action">app.help</attribute>
+      </item>
       <item>
         <attribute name="label" translatable="yes">_Inspector</attribute>
         <attribute name="action">app.inspector</attribute>
diff --git a/demos/node-editor/node-editor.gresource.xml b/demos/node-editor/node-editor.gresource.xml
index ca6cdd2c1b..5cb5478ca3 100644
--- a/demos/node-editor/node-editor.gresource.xml
+++ b/demos/node-editor/node-editor.gresource.xml
@@ -2,5 +2,7 @@
 <gresources>
   <gresource prefix="/org/gtk/gtk4/node-editor">
     <file preprocess="xml-stripblanks">node-editor-window.ui</file>
+    <file preprocess="xml-stripblanks">help-window.ui</file>
+    <file>node-format.md</file>
   </gresource>
 </gresources>


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