[gtk/wip/chergert/textundo: 20/21] gtk-demo: add a demo for TextView undo/redo



commit 166433873a54ed933c89175e36708f7ca6874838
Author: Christian Hergert <chergert redhat com>
Date:   Fri Nov 1 11:20:22 2019 -0700

    gtk-demo: add a demo for TextView undo/redo

 demos/gtk-demo/demo.gresource.xml |  1 +
 demos/gtk-demo/meson.build        |  1 +
 demos/gtk-demo/textundo.c         | 71 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+)
---
diff --git a/demos/gtk-demo/demo.gresource.xml b/demos/gtk-demo/demo.gresource.xml
index a299fe9101..2a30e84349 100644
--- a/demos/gtk-demo/demo.gresource.xml
+++ b/demos/gtk-demo/demo.gresource.xml
@@ -221,6 +221,7 @@
     <file>spinner.c</file>
     <file>tabs.c</file>
     <file>tagged_entry.c</file>
+    <file>textundo.c</file>
     <file>textview.c</file>
     <file>textscroll.c</file>
     <file>theming_style_classes.c</file>
diff --git a/demos/gtk-demo/meson.build b/demos/gtk-demo/meson.build
index a107d52b58..3d20727e6c 100644
--- a/demos/gtk-demo/meson.build
+++ b/demos/gtk-demo/meson.build
@@ -74,6 +74,7 @@ demos = files([
   'tabs.c',
   'tagged_entry.c',
   'textmask.c',
+  'textundo.c',
   'textview.c',
   'textscroll.c',
   'themes.c',
diff --git a/demos/gtk-demo/textundo.c b/demos/gtk-demo/textundo.c
new file mode 100644
index 0000000000..fe057f00f3
--- /dev/null
+++ b/demos/gtk-demo/textundo.c
@@ -0,0 +1,71 @@
+/* Text View/Undo and Redo
+ *
+ * The GtkTextView supports undo and redo through the use of a
+ * GtkTextBuffer. You can enable or disable undo support using
+ * gtk_text_buffer_set_enable_undo().
+ *
+ * Use Primary+Z to undo and Primary+Shift+Z or Primary+Y to
+ * redo previously undone operations.
+ */
+
+#include <gtk/gtk.h>
+#include <stdlib.h> /* for exit() */
+
+GtkWidget *
+do_textundo (GtkWidget *do_widget)
+{
+  static GtkWidget *window = NULL;
+
+  if (!window)
+    {
+      GtkWidget *view;
+      GtkWidget *sw;
+      GtkTextBuffer *buffer;
+      GtkTextIter iter;
+
+      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+      gtk_window_set_display (GTK_WINDOW (window),
+                              gtk_widget_get_display (do_widget));
+      gtk_window_set_default_size (GTK_WINDOW (window),
+                                   450, 450);
+
+      g_signal_connect (window, "destroy",
+                        G_CALLBACK (gtk_widget_destroyed), &window);
+
+      gtk_window_set_title (GTK_WINDOW (window), "TextView Undo");
+
+      view = gtk_text_view_new ();
+      gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (view), GTK_WRAP_WORD);
+      buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+      gtk_text_buffer_set_enable_undo (buffer, TRUE);
+
+      /* this text cannot be undone */
+      gtk_text_buffer_begin_irreversible_action (buffer);
+      gtk_text_buffer_get_start_iter (buffer, &iter);
+      gtk_text_buffer_insert (buffer, &iter,
+                              "Type to add more text.\n"
+                              "Use Primary+Z to undo and Primary+Shift+Z to redo a previously undone 
action.\n"
+                              "\n",
+                              -1);
+      gtk_text_buffer_end_irreversible_action (buffer);
+
+      sw = gtk_scrolled_window_new (NULL, NULL);
+      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
+                                      GTK_POLICY_AUTOMATIC,
+                                      GTK_POLICY_AUTOMATIC);
+      gtk_container_add (GTK_CONTAINER (window), sw);
+      gtk_container_add (GTK_CONTAINER (sw), view);
+    }
+
+  if (!gtk_widget_get_visible (window))
+    {
+      gtk_widget_show (window);
+    }
+  else
+    {
+      gtk_widget_destroy (window);
+      window = NULL;
+    }
+
+  return window;
+}


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