[gtk/wip/otte/nodeeditor: 12/12] Add gtk4-node-editor



commit c17d5f2aeae2594e9058a624526a686136751995
Author: Benjamin Otte <otte redhat com>
Date:   Tue Mar 12 15:05:25 2019 +0100

    Add gtk4-node-editor
    
    It's meant to be a little editor for render nodes so we can do testing
    with it.

 demos/meson.build                           |   1 +
 demos/node-editor/meson.build               |  17 +++
 demos/node-editor/node-editor-application.c |  93 ++++++++++++++
 demos/node-editor/node-editor-application.h |  38 ++++++
 demos/node-editor/node-editor-window.c      | 180 ++++++++++++++++++++++++++++
 demos/node-editor/node-editor-window.h      |  40 +++++++
 demos/node-editor/node-editor-window.ui     |  85 +++++++++++++
 demos/node-editor/node-editor.gresource.xml |   6 +
 8 files changed, 460 insertions(+)
---
diff --git a/demos/meson.build b/demos/meson.build
index e45a7c8b4a..6f1905ddb6 100644
--- a/demos/meson.build
+++ b/demos/meson.build
@@ -1,3 +1,4 @@
 subdir('gtk-demo')
 subdir('icon-browser')
+subdir('node-editor')
 subdir('widget-factory')
diff --git a/demos/node-editor/meson.build b/demos/node-editor/meson.build
new file mode 100644
index 0000000000..b12bc326e6
--- /dev/null
+++ b/demos/node-editor/meson.build
@@ -0,0 +1,17 @@
+node_editor_sources = [
+  'main.c',
+  'node-editor-application.c',
+  'node-editor-window.c',
+]
+
+node_editor_resources = gnome.compile_resources('node_editor_resources',
+                                                'node-editor.gresource.xml',
+                                                source_dir: '.')
+
+executable('gtk4-node-editor',
+           node_editor_sources, node_editor_resources,
+           dependencies: libgtk_dep,
+           include_directories: confinc,
+           gui_app: true,
+           link_args: extra_demo_ldflags,
+           install: false)
diff --git a/demos/node-editor/node-editor-application.c b/demos/node-editor/node-editor-application.c
new file mode 100644
index 0000000000..fd02104344
--- /dev/null
+++ b/demos/node-editor/node-editor-application.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright © 2019 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#include "config.h"
+
+#include "node-editor-application.h"
+
+#include "node-editor-window.h"
+
+struct _NodeEditorApplication
+{
+  GtkApplication parent;
+};
+
+struct _NodeEditorApplicationClass
+{
+  GtkApplicationClass parent_class;
+};
+
+G_DEFINE_TYPE(NodeEditorApplication, node_editor_application, GTK_TYPE_APPLICATION);
+
+static void
+node_editor_application_init (NodeEditorApplication *app)
+{
+}
+
+static void
+quit_activated (GSimpleAction *action,
+                GVariant      *parameter,
+                gpointer       data)
+{
+  g_application_quit (G_APPLICATION (data));
+}
+
+static GActionEntry app_entries[] =
+{
+  { "quit", quit_activated, NULL, NULL, NULL }
+};
+
+static void
+node_editor_application_startup (GApplication *app)
+{
+  const gchar *quit_accels[2] = { "<Ctrl>Q", NULL };
+
+  G_APPLICATION_CLASS (node_editor_application_parent_class)->startup (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.quit",
+                                         quit_accels);
+}
+
+static void
+node_editor_application_activate (GApplication *app)
+{
+  NodeEditorWindow *win;
+
+  win = node_editor_window_new (NODE_EDITOR_APPLICATION (app));
+  gtk_window_present (GTK_WINDOW (win));
+}
+
+static void
+node_editor_application_class_init (NodeEditorApplicationClass *class)
+{
+  G_APPLICATION_CLASS (class)->startup = node_editor_application_startup;
+  G_APPLICATION_CLASS (class)->activate = node_editor_application_activate;
+}
+
+NodeEditorApplication *
+node_editor_application_new (void)
+{
+  return g_object_new (NODE_EDITOR_APPLICATION_TYPE,
+                       "application-id", "org.gtk.gtk4.NodeEditor",
+                       NULL);
+}
diff --git a/demos/node-editor/node-editor-application.h b/demos/node-editor/node-editor-application.h
new file mode 100644
index 0000000000..90fbb24cb6
--- /dev/null
+++ b/demos/node-editor/node-editor-application.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright © 2019 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#ifndef __NODE_EDITOR_APPLICATION_H__
+#define __NODE_EDITOR_APPLICATION_H__
+
+#include <gtk/gtk.h>
+
+
+#define NODE_EDITOR_APPLICATION_TYPE (node_editor_application_get_type ())
+#define NODE_EDITOR_APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NODE_EDITOR_APPLICATION_TYPE, 
NodeEditorApplication))
+
+
+typedef struct _NodeEditorApplication       NodeEditorApplication;
+typedef struct _NodeEditorApplicationClass  NodeEditorApplicationClass;
+
+
+GType           node_editor_application_get_type    (void);
+NodeEditorApplication *node_editor_application_new         (void);
+
+
+#endif /* __NODE_EDITOR_APPLICATION_H__ */
diff --git a/demos/node-editor/node-editor-window.c b/demos/node-editor/node-editor-window.c
new file mode 100644
index 0000000000..985bf88589
--- /dev/null
+++ b/demos/node-editor/node-editor-window.c
@@ -0,0 +1,180 @@
+/*
+ * Copyright © 2019 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#include "config.h"
+
+#include "node-editor-window.h"
+
+#include "gsk/gskrendernodeparserprivate.h"
+
+struct _NodeEditorWindow
+{
+  GtkApplicationWindow parent;
+
+  guint text_timeout;
+
+  GtkWidget *text_view;
+  GtkTextBuffer *text_buffer;
+};
+
+struct _NodeEditorWindowClass
+{
+  GtkApplicationWindowClass parent_class;
+};
+
+G_DEFINE_TYPE(NodeEditorWindow, node_editor_window, GTK_TYPE_APPLICATION_WINDOW);
+
+static gchar *
+get_current_text (GtkTextBuffer *buffer)
+{
+  GtkTextIter start, end;
+
+  gtk_text_buffer_get_start_iter (buffer, &start);
+  gtk_text_buffer_get_end_iter (buffer, &end);
+  gtk_text_buffer_remove_all_tags (buffer, &start, &end);
+
+  return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+}
+
+static void
+update_node (NodeEditorWindow *self)
+{
+  GskRenderNode *node;
+  GError *error = NULL;
+  char *text;
+  GBytes *bytes;
+
+  text = get_current_text (self->text_buffer);
+  bytes = g_bytes_new_take (text, strlen (text));
+
+  node = gsk_render_node_deserialize (bytes, &error);
+  g_bytes_unref (bytes);
+  if (node)
+    gsk_render_node_unref (node);
+  else
+    g_clear_error (&error);
+}
+
+static gboolean
+update_timeout (gpointer data)
+{
+  NodeEditorWindow *self = data;
+
+  self->text_timeout = 0;
+
+  update_node (self);
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+text_changed (GtkTextBuffer    *buffer,
+              NodeEditorWindow *self)
+{
+  if (self->text_timeout != 0)
+    g_source_remove (self->text_timeout);
+
+  self->text_timeout = g_timeout_add (100, update_timeout, self); 
+}
+
+static gboolean
+query_tooltip_cb (GtkWidget        *widget,
+                  gint              x,
+                  gint              y,
+                  gboolean          keyboard_tip,
+                  GtkTooltip       *tooltip,
+                  NodeEditorWindow *self)
+{
+  GtkTextIter iter;
+  //GList *l;
+
+  if (keyboard_tip)
+    {
+      gint offset;
+
+      g_object_get (self->text_buffer, "cursor-position", &offset, NULL);
+      gtk_text_buffer_get_iter_at_offset (self->text_buffer, &iter, offset);
+    }
+  else
+    {
+      gint bx, by, trailing;
+
+      gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (self->text_view), GTK_TEXT_WINDOW_TEXT,
+                                             x, y, &bx, &by);
+      gtk_text_view_get_iter_at_position (GTK_TEXT_VIEW (self->text_view), &iter, &trailing, bx, by);
+    }
+
+#if 0
+  for (l = ce->priv->errors; l; l = l->next)
+    {
+      CssError *css_error = l->data;
+
+      if (gtk_text_iter_in_range (&iter, &css_error->start, &css_error->end))
+        {
+          gtk_tooltip_set_text (tooltip, css_error->error->message);
+          return TRUE;
+        }
+    }
+#endif
+
+  return FALSE;
+}
+
+static void
+node_editor_window_finalize (GObject *object)
+{
+  NodeEditorWindow *self = NODE_EDITOR_WINDOW (object);
+
+  if (self->text_timeout != 0)
+    g_source_remove (self->text_timeout);
+
+  G_OBJECT_CLASS (node_editor_window_parent_class)->finalize (object);
+}
+
+static void
+node_editor_window_class_init (NodeEditorWindowClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
+
+  object_class->finalize = node_editor_window_finalize;
+
+  gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
+                                               "/org/gtk/gtk4/node-editor/node-editor-window.ui");
+
+  gtk_widget_class_bind_template_child (widget_class, NodeEditorWindow, text_buffer);
+  gtk_widget_class_bind_template_child (widget_class, NodeEditorWindow, text_view);
+
+  gtk_widget_class_bind_template_callback (widget_class, text_changed);
+  gtk_widget_class_bind_template_callback (widget_class, query_tooltip_cb);
+}
+
+static void
+node_editor_window_init (NodeEditorWindow *self)
+{
+  gtk_widget_init_template (GTK_WIDGET (self));
+}
+
+NodeEditorWindow *
+node_editor_window_new (NodeEditorApplication *application)
+{
+  return g_object_new (NODE_EDITOR_WINDOW_TYPE,
+                       "application", application,
+                       NULL);
+}
diff --git a/demos/node-editor/node-editor-window.h b/demos/node-editor/node-editor-window.h
new file mode 100644
index 0000000000..475271cb3e
--- /dev/null
+++ b/demos/node-editor/node-editor-window.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright © 2019 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#ifndef __NODE_EDITOR_WINDOW_H__
+#define __NODE_EDITOR_WINDOW_H__
+
+#include <gtk/gtk.h>
+
+#include "node-editor-application.h"
+
+#define NODE_EDITOR_WINDOW_TYPE (node_editor_window_get_type ())
+#define NODE_EDITOR_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NODE_EDITOR_WINDOW_TYPE, 
NodeEditorWindow))
+
+
+typedef struct _NodeEditorWindow         NodeEditorWindow;
+typedef struct _NodeEditorWindowClass    NodeEditorWindowClass;
+
+
+GType                   node_editor_window_get_type     (void);
+
+NodeEditorWindow *      node_editor_window_new          (NodeEditorApplication  *application);
+
+
+#endif /* __NODE_EDITOR_WINDOW_H__ */
diff --git a/demos/node-editor/node-editor-window.ui b/demos/node-editor/node-editor-window.ui
new file mode 100644
index 0000000000..934b55c0c9
--- /dev/null
+++ b/demos/node-editor/node-editor-window.ui
@@ -0,0 +1,85 @@
+<interface>
+  <object class="GtkTextTagTable" id="tags">
+    <child type="tag">
+      <object class="GtkTextTag">
+        <property name="name">warning</property>
+        <property name="underline">single</property>
+        <property name="underline-rgba">darkorange</property>
+      </object>
+    </child>
+    <child type="tag">
+      <object class="GtkTextTag">
+        <property name="name">error</property>
+        <property name="underline">error</property>
+      </object>
+    </child>
+  </object>
+  <object class="GtkTextBuffer" id="text_buffer">
+    <property name="tag-table">tags</property>
+    <signal name="changed" handler="text_changed"/>
+  </object>
+
+  <template class="NodeEditorWindow" parent="GtkApplicationWindow">
+    <style>
+      <class name="devel"/>
+    </style>
+    <property name="title" translatable="yes">GTK Node Editor</property>
+    <property name="default-width">1024</property>
+    <property name="default-height">768</property>
+    <child type="titlebar">
+      <object class="GtkHeaderBar" id="header">
+        <property name="title" translatable="yes">GTK Node Editor</property>
+        <property name="show-title-buttons">1</property>
+        <child type="title">
+          <object class="GtkBox">
+            <style>
+              <class name="linked"/>
+            </style>
+            <child>
+              <object class="GtkLabel">
+                <property name="label" translatable="yes">GTK Node Editor</property>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkToggleButton">
+            <style>
+              <class name="image-button"/>
+            </style>
+            <child>
+              <object class="GtkImage">
+                <property name="icon-name">view-more-symbolic</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="pack-type">end</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <child>
+      <object class="GtkPaned">
+        <child>
+          <object class="GtkScrolledWindow">
+            <property name="hscrollbar-policy">never</property>
+            <property name="expand">1</property>
+            <child>
+              <object class="GtkTextView" id="text_view">
+                <property name="buffer">text_buffer</property>
+                <property name="wrap-mode">word</property>
+                <property name="monospace">1</property>
+                <property name="has-focus">1</property>
+                <property name="left-margin">6</property>
+                <property name="right-margin">6</property>
+                <property name="has-tooltip">1</property>
+                <signal name="query-tooltip" handler="query_tooltip_cb"/>
+              </object>
+            </child>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/demos/node-editor/node-editor.gresource.xml b/demos/node-editor/node-editor.gresource.xml
new file mode 100644
index 0000000000..ca6cdd2c1b
--- /dev/null
+++ b/demos/node-editor/node-editor.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/gtk/gtk4/node-editor">
+    <file preprocess="xml-stripblanks">node-editor-window.ui</file>
+  </gresource>
+</gresources>


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