2019-11-01 18:20:22 +00:00
|
|
|
/* 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().
|
|
|
|
*
|
2020-06-17 22:08:17 +00:00
|
|
|
* Use Control+z to undo and Control+Shift+z or Control+y to
|
2019-11-01 18:20:22 +00:00
|
|
|
* 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;
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2019-11-01 18:20:22 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
2020-05-09 16:03:11 +00:00
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 450, 450);
|
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2019-11-01 18:20:22 +00:00
|
|
|
|
2020-06-17 22:08:17 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Undo and Redo");
|
2019-11-01 18:20:22 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-06-24 15:25:09 +00:00
|
|
|
sw = gtk_scrolled_window_new ();
|
2019-11-01 18:20:22 +00:00
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
|
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), sw);
|
2020-05-02 04:51:20 +00:00
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), view);
|
2019-11-01 18:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gtk_widget_get_visible (window))
|
|
|
|
{
|
|
|
|
gtk_widget_show (window);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2019-11-01 18:20:22 +00:00
|
|
|
window = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|