| 1 |
/* Text View/Automatic Scrolling |
| 2 |
* |
| 3 |
* This example demonstrates how to use the gravity of |
| 4 |
* GtkTextMarks to keep a text view scrolled to the bottom |
| 5 |
* while appending text. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <gtk/gtk.h> |
| 9 |
|
| 10 |
/* Scroll to the end of the buffer. |
| 11 |
*/ |
| 12 |
static gboolean |
| 13 |
scroll_to_end (GtkTextView *textview) |
| 14 |
{ |
| 15 |
GtkTextBuffer *buffer; |
| 16 |
GtkTextIter iter; |
| 17 |
GtkTextMark *mark; |
| 18 |
char *spaces; |
| 19 |
char *text; |
| 20 |
static int count; |
| 21 |
|
| 22 |
buffer = gtk_text_view_get_buffer (textview); |
| 23 |
|
| 24 |
/* Get "end" mark. It's located at the end of buffer because |
| 25 |
* of right gravity |
| 26 |
*/ |
| 27 |
mark = gtk_text_buffer_get_mark (buffer, "end"); |
| 28 |
gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark); |
| 29 |
|
| 30 |
/* and insert some text at its position, the iter will be |
| 31 |
* revalidated after insertion to point to the end of inserted text |
| 32 |
*/ |
| 33 |
spaces = g_strnfill (count++, ' '); |
| 34 |
gtk_text_buffer_insert (buffer, &iter, "\n", -1); |
| 35 |
gtk_text_buffer_insert (buffer, &iter, spaces, -1); |
| 36 |
text = g_strdup_printf ("Scroll to end scroll to end scroll " |
| 37 |
"to end scroll to end %d", count); |
| 38 |
gtk_text_buffer_insert (buffer, &iter, text, -1); |
| 39 |
g_free (spaces); |
| 40 |
g_free (text); |
| 41 |
|
| 42 |
/* Now scroll the end mark onscreen. |
| 43 |
*/ |
| 44 |
gtk_text_view_scroll_mark_onscreen (textview, mark); |
| 45 |
|
| 46 |
/* Emulate typewriter behavior, shift to the left if we |
| 47 |
* are far enough to the right. |
| 48 |
*/ |
| 49 |
if (count > 150) |
| 50 |
count = 0; |
| 51 |
|
| 52 |
return G_SOURCE_CONTINUE; |
| 53 |
} |
| 54 |
|
| 55 |
/* Scroll to the bottom of the buffer. |
| 56 |
*/ |
| 57 |
static gboolean |
| 58 |
scroll_to_bottom (GtkTextView *textview) |
| 59 |
{ |
| 60 |
GtkTextBuffer *buffer; |
| 61 |
GtkTextIter iter; |
| 62 |
GtkTextMark *mark; |
| 63 |
char *spaces; |
| 64 |
char *text; |
| 65 |
static int count; |
| 66 |
|
| 67 |
buffer = gtk_text_view_get_buffer (textview); |
| 68 |
|
| 69 |
/* Get end iterator */ |
| 70 |
gtk_text_buffer_get_end_iter (buffer, &iter); |
| 71 |
|
| 72 |
/* and insert some text at it, the iter will be revalidated |
| 73 |
* after insertion to point to the end of inserted text |
| 74 |
*/ |
| 75 |
spaces = g_strnfill (count++, ' '); |
| 76 |
gtk_text_buffer_insert (buffer, &iter, "\n", -1); |
| 77 |
gtk_text_buffer_insert (buffer, &iter, spaces, -1); |
| 78 |
text = g_strdup_printf ("Scroll to bottom scroll to bottom scroll " |
| 79 |
"to bottom scroll to bottom %d", count); |
| 80 |
gtk_text_buffer_insert (buffer, &iter, text, -1); |
| 81 |
g_free (spaces); |
| 82 |
g_free (text); |
| 83 |
|
| 84 |
/* Move the iterator to the beginning of line, so we don't scroll |
| 85 |
* in horizontal direction |
| 86 |
*/ |
| 87 |
gtk_text_iter_set_line_offset (&iter, 0); |
| 88 |
|
| 89 |
/* and place the mark at iter. the mark will stay there after we |
| 90 |
* insert some text at the end because it has left gravity. |
| 91 |
*/ |
| 92 |
mark = gtk_text_buffer_get_mark (buffer, "scroll"); |
| 93 |
gtk_text_buffer_move_mark (buffer, mark, &iter); |
| 94 |
|
| 95 |
/* Scroll the mark onscreen. |
| 96 |
*/ |
| 97 |
gtk_text_view_scroll_mark_onscreen (textview, mark); |
| 98 |
|
| 99 |
/* Shift text back if we got enough to the right. |
| 100 |
*/ |
| 101 |
if (count > 40) |
| 102 |
count = 0; |
| 103 |
|
| 104 |
return G_SOURCE_CONTINUE; |
| 105 |
} |
| 106 |
|
| 107 |
static guint |
| 108 |
setup_scroll (GtkTextView *textview, |
| 109 |
gboolean to_end) |
| 110 |
{ |
| 111 |
GtkTextBuffer *buffer; |
| 112 |
GtkTextIter iter; |
| 113 |
|
| 114 |
buffer = gtk_text_view_get_buffer (textview); |
| 115 |
gtk_text_buffer_get_end_iter (buffer, &iter); |
| 116 |
|
| 117 |
if (to_end) |
| 118 |
{ |
| 119 |
/* If we want to scroll to the end, including horizontal scrolling, |
| 120 |
* then we just create a mark with right gravity at the end of the |
| 121 |
* buffer. It will stay at the end unless explicitly moved with |
| 122 |
* gtk_text_buffer_move_mark. |
| 123 |
*/ |
| 124 |
gtk_text_buffer_create_mark (buffer, "end", &iter, FALSE); |
| 125 |
|
| 126 |
/* Add scrolling timeout. */ |
| 127 |
return g_timeout_add (50, (GSourceFunc) scroll_to_end, textview); |
| 128 |
} |
| 129 |
else |
| 130 |
{ |
| 131 |
/* If we want to scroll to the bottom, but not scroll horizontally, |
| 132 |
* then an end mark won't do the job. Just create a mark so we can |
| 133 |
* use it with gtk_text_view_scroll_mark_onscreen, we'll position it |
| 134 |
* explicitly when needed. Use left gravity so the mark stays where |
| 135 |
* we put it after inserting new text. |
| 136 |
*/ |
| 137 |
gtk_text_buffer_create_mark (buffer, "scroll", &iter, TRUE); |
| 138 |
|
| 139 |
/* Add scrolling timeout. */ |
| 140 |
return g_timeout_add (100, (GSourceFunc) scroll_to_bottom, textview); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
static void |
| 145 |
remove_timeout (GtkWidget *window, |
| 146 |
gpointer timeout) |
| 147 |
{ |
| 148 |
g_source_remove (GPOINTER_TO_UINT (timeout)); |
| 149 |
} |
| 150 |
|
| 151 |
static void |
| 152 |
create_text_view (GtkWidget *hbox, |
| 153 |
gboolean to_end) |
| 154 |
{ |
| 155 |
GtkWidget *swindow; |
| 156 |
GtkWidget *textview; |
| 157 |
guint timeout; |
| 158 |
|
| 159 |
swindow = gtk_scrolled_window_new (NULL, NULL); |
| 160 |
gtk_box_pack_start (GTK_BOX (hbox), swindow, TRUE, TRUE, 0); |
| 161 |
textview = gtk_text_view_new (); |
| 162 |
gtk_container_add (GTK_CONTAINER (swindow), textview); |
| 163 |
|
| 164 |
timeout = setup_scroll (GTK_TEXT_VIEW (textview), to_end); |
| 165 |
|
| 166 |
/* Remove the timeout in destroy handler, so we don't try to |
| 167 |
* scroll destroyed widget. |
| 168 |
*/ |
| 169 |
g_signal_connect (textview, "destroy", |
| 170 |
G_CALLBACK (remove_timeout), |
| 171 |
GUINT_TO_POINTER (timeout)); |
| 172 |
} |
| 173 |
|
| 174 |
GtkWidget * |
| 175 |
do_textscroll (GtkWidget *do_widget) |
| 176 |
{ |
| 177 |
static GtkWidget *window = NULL; |
| 178 |
|
| 179 |
if (!window) |
| 180 |
{ |
| 181 |
GtkWidget *hbox; |
| 182 |
|
| 183 |
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
| 184 |
gtk_window_set_title (GTK_WINDOW (window), "Automatic Scrolling"); |
| 185 |
g_signal_connect (window, "destroy", |
| 186 |
G_CALLBACK (gtk_widget_destroyed), &window); |
| 187 |
gtk_window_set_default_size (GTK_WINDOW (window), 600, 400); |
| 188 |
|
| 189 |
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6); |
| 190 |
gtk_box_set_homogeneous (GTK_BOX (hbox), TRUE); |
| 191 |
gtk_container_add (GTK_CONTAINER (window), hbox); |
| 192 |
|
| 193 |
create_text_view (hbox, TRUE); |
| 194 |
create_text_view (hbox, FALSE); |
| 195 |
} |
| 196 |
|
| 197 |
if (!gtk_widget_get_visible (window)) |
| 198 |
gtk_widget_show_all (window); |
| 199 |
else |
| 200 |
gtk_widget_destroy (window); |
| 201 |
|
| 202 |
return window; |
| 203 |
} |