[gtk+] gtk-demo: Improve markup demo
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+] gtk-demo: Improve markup demo
- Date: Mon, 21 Mar 2016 17:32:58 +0000 (UTC)
commit 73fe852be00604324d6e7aeb98a9a1e557b9c8ea
Author: Matthias Clasen <mclasen redhat com>
Date: Wed Mar 16 14:03:44 2016 -0400
gtk-demo: Improve markup demo
Add squiggly underlines and background, and make the markup editable.
https://bugzilla.gnome.org/show_bug.cgi?id=763741
demos/gtk-demo/markup.c | 71 ++++++++++++++++++++++++++++++++++++++++++--
demos/gtk-demo/markup.txt | 4 +-
2 files changed, 69 insertions(+), 6 deletions(-)
---
diff --git a/demos/gtk-demo/markup.c b/demos/gtk-demo/markup.c
index 08b0689..898df8c 100644
--- a/demos/gtk-demo/markup.c
+++ b/demos/gtk-demo/markup.c
@@ -8,6 +8,34 @@
#include <gtk/gtk.h>
+static GtkWidget *stack;
+static GtkWidget *view;
+static GtkWidget *view2;
+
+static void
+source_toggled (GtkToggleButton *button)
+{
+ if (gtk_toggle_button_get_active (button))
+ gtk_stack_set_visible_child_name (GTK_STACK (stack), "source");
+ else
+ {
+ GtkTextBuffer *buffer;
+ GtkTextIter start, end;
+ gchar *markup;
+
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view2));
+ gtk_text_buffer_get_bounds (buffer, &start, &end);
+ markup = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+ gtk_text_buffer_get_start_iter (buffer, &start);
+ gtk_text_buffer_insert_markup (buffer, &start, markup, -1);
+ g_free (markup);
+
+ gtk_stack_set_visible_child_name (GTK_STACK (stack), "formatted");
+ }
+}
+
GtkWidget *
do_markup (GtkWidget *do_widget)
{
@@ -15,12 +43,13 @@ do_markup (GtkWidget *do_widget)
if (!window)
{
- GtkWidget *view;
GtkWidget *sw;
GtkTextBuffer *buffer;
GtkTextIter iter;
GBytes *bytes;
const gchar *markup;
+ GtkWidget *header;
+ GtkWidget *show_source;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_screen (GTK_WINDOW (window),
@@ -29,19 +58,49 @@ do_markup (GtkWidget *do_widget)
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);
+ stack = gtk_stack_new ();
+ gtk_widget_show (stack);
+ gtk_container_add (GTK_CONTAINER (window), stack);
+
+ show_source = gtk_check_button_new_with_label ("Source");
+ gtk_widget_set_valign (show_source, GTK_ALIGN_CENTER);
+ g_signal_connect (show_source, "toggled", G_CALLBACK (source_toggled), stack);
+
+ header = gtk_header_bar_new ();
+ gtk_header_bar_pack_start (GTK_HEADER_BAR (header), show_source);
+ gtk_widget_show_all (header);
+ gtk_window_set_titlebar (GTK_WINDOW (window), header);
+
gtk_window_set_title (GTK_WINDOW (window), "Markup");
view = gtk_text_view_new ();
+ gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE);
gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (view), GTK_WRAP_WORD);
gtk_text_view_set_left_margin (GTK_TEXT_VIEW (view), 10);
gtk_text_view_set_right_margin (GTK_TEXT_VIEW (view), 10);
sw = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
- GTK_POLICY_NEVER,
+ GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
- gtk_container_add (GTK_CONTAINER (window), sw);
gtk_container_add (GTK_CONTAINER (sw), view);
+ gtk_widget_show_all (sw);
+
+ gtk_stack_add_named (GTK_STACK (stack), sw, "formatted");
+
+ view2 = gtk_text_view_new ();
+ gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (view2), GTK_WRAP_WORD);
+ gtk_text_view_set_left_margin (GTK_TEXT_VIEW (view2), 10);
+ gtk_text_view_set_right_margin (GTK_TEXT_VIEW (view2), 10);
+
+ 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 (sw), view2);
+ gtk_widget_show_all (sw);
+
+ gtk_stack_add_named (GTK_STACK (stack), sw, "source");
bytes = g_resources_lookup_data ("/markup/markup.txt", 0, NULL);
markup = (const gchar *)g_bytes_get_data (bytes, NULL);
@@ -50,9 +109,13 @@ do_markup (GtkWidget *do_widget)
gtk_text_buffer_get_start_iter (buffer, &iter);
gtk_text_buffer_insert_markup (buffer, &iter, markup, -1);
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view2));
+ gtk_text_buffer_get_start_iter (buffer, &iter);
+ gtk_text_buffer_insert (buffer, &iter, markup, -1);
+
g_bytes_unref (bytes);
- gtk_widget_show_all (sw);
+ gtk_widget_show (stack);
}
if (!gtk_widget_get_visible (window))
diff --git a/demos/gtk-demo/markup.txt b/demos/gtk-demo/markup.txt
index feece2c..92da324 100644
--- a/demos/gtk-demo/markup.txt
+++ b/demos/gtk-demo/markup.txt
@@ -1,9 +1,9 @@
Text sizes: <span size="xx-small">tiny</span> <span size="x-small">very small</span> <span
size="small">small</span> <span size="medium">normal</span> <span size="large">large</span> <span
size="x-large">very large</span> <span size="xx-large">huge</span>
-Text <span color="gray">c<span color="green">o</span>l<span color="tomato">o</span>rs</span>
+Text <span color="gray">c<span color="green">o</span>l<span color="tomato">o</span>rs</span> and <span
background="pink">backgrounds</span>
-Colorful <span underline="low" underline-color="blue"><span underline="double"
underline-color="red">under</span>lines</span>
+Colorful <span underline="low" underline-color="blue"><span underline="double"
underline-color="red">under</span>lines</span> and <span background="pink"><span
underline="error">mo</span><span underline="error" underline-color="green">re</span></span>
Colorful <span strikethrough="true" strikethrough-color="magenta">strikethroughs</span>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]