2015-07-29 23:07:56 +00:00
|
|
|
/* Text View/Automatic Scrolling
|
2006-08-26 01:17:17 +00:00
|
|
|
*
|
2011-09-02 03:55:47 +00:00
|
|
|
* This example demonstrates how to use the gravity of
|
2006-08-26 01:17:17 +00:00
|
|
|
* GtkTextMarks to keep a text view scrolled to the bottom
|
|
|
|
* while appending text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
/* Scroll to the end of the buffer.
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
scroll_to_end (GtkTextView *textview)
|
|
|
|
{
|
|
|
|
GtkTextBuffer *buffer;
|
|
|
|
GtkTextIter iter;
|
|
|
|
GtkTextMark *mark;
|
|
|
|
char *spaces;
|
2014-07-21 10:40:12 +00:00
|
|
|
char *text;
|
2006-08-26 01:17:17 +00:00
|
|
|
static int count;
|
|
|
|
|
|
|
|
buffer = gtk_text_view_get_buffer (textview);
|
2011-09-02 03:55:47 +00:00
|
|
|
|
|
|
|
/* Get "end" mark. It's located at the end of buffer because
|
2006-08-26 01:17:17 +00:00
|
|
|
* of right gravity
|
|
|
|
*/
|
|
|
|
mark = gtk_text_buffer_get_mark (buffer, "end");
|
|
|
|
gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
|
|
|
|
|
2011-09-02 03:55:47 +00:00
|
|
|
/* and insert some text at its position, the iter will be
|
2006-08-26 01:17:17 +00:00
|
|
|
* revalidated after insertion to point to the end of inserted text
|
|
|
|
*/
|
|
|
|
spaces = g_strnfill (count++, ' ');
|
|
|
|
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
|
|
|
|
gtk_text_buffer_insert (buffer, &iter, spaces, -1);
|
2014-07-21 10:40:12 +00:00
|
|
|
text = g_strdup_printf ("Scroll to end scroll to end scroll "
|
|
|
|
"to end scroll to end %d", count);
|
|
|
|
gtk_text_buffer_insert (buffer, &iter, text, -1);
|
2006-08-26 01:17:17 +00:00
|
|
|
g_free (spaces);
|
2014-07-21 10:40:12 +00:00
|
|
|
g_free (text);
|
2006-08-26 01:17:17 +00:00
|
|
|
|
|
|
|
/* Now scroll the end mark onscreen.
|
|
|
|
*/
|
|
|
|
gtk_text_view_scroll_mark_onscreen (textview, mark);
|
|
|
|
|
2011-09-02 03:55:47 +00:00
|
|
|
/* Emulate typewriter behavior, shift to the left if we
|
2006-08-26 01:17:17 +00:00
|
|
|
* are far enough to the right.
|
|
|
|
*/
|
|
|
|
if (count > 150)
|
|
|
|
count = 0;
|
|
|
|
|
2012-01-31 00:12:27 +00:00
|
|
|
return G_SOURCE_CONTINUE;
|
2006-08-26 01:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Scroll to the bottom of the buffer.
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
scroll_to_bottom (GtkTextView *textview)
|
|
|
|
{
|
|
|
|
GtkTextBuffer *buffer;
|
|
|
|
GtkTextIter iter;
|
|
|
|
GtkTextMark *mark;
|
|
|
|
char *spaces;
|
2014-07-21 10:40:12 +00:00
|
|
|
char *text;
|
2006-08-26 01:17:17 +00:00
|
|
|
static int count;
|
|
|
|
|
|
|
|
buffer = gtk_text_view_get_buffer (textview);
|
2011-09-02 03:55:47 +00:00
|
|
|
|
2006-08-26 01:17:17 +00:00
|
|
|
/* Get end iterator */
|
|
|
|
gtk_text_buffer_get_end_iter (buffer, &iter);
|
|
|
|
|
|
|
|
/* and insert some text at it, the iter will be revalidated
|
|
|
|
* after insertion to point to the end of inserted text
|
|
|
|
*/
|
|
|
|
spaces = g_strnfill (count++, ' ');
|
|
|
|
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
|
|
|
|
gtk_text_buffer_insert (buffer, &iter, spaces, -1);
|
2014-07-21 10:40:12 +00:00
|
|
|
text = g_strdup_printf ("Scroll to bottom scroll to bottom scroll "
|
|
|
|
"to bottom scroll to bottom %d", count);
|
|
|
|
gtk_text_buffer_insert (buffer, &iter, text, -1);
|
2006-08-26 01:17:17 +00:00
|
|
|
g_free (spaces);
|
2014-07-21 10:40:12 +00:00
|
|
|
g_free (text);
|
2006-08-26 01:17:17 +00:00
|
|
|
|
2011-09-02 03:55:47 +00:00
|
|
|
/* Move the iterator to the beginning of line, so we don't scroll
|
|
|
|
* in horizontal direction
|
2006-08-26 01:17:17 +00:00
|
|
|
*/
|
|
|
|
gtk_text_iter_set_line_offset (&iter, 0);
|
2011-09-02 03:55:47 +00:00
|
|
|
|
2006-08-26 01:17:17 +00:00
|
|
|
/* and place the mark at iter. the mark will stay there after we
|
2012-05-08 01:32:28 +00:00
|
|
|
* insert some text at the end because it has left gravity.
|
2006-08-26 01:17:17 +00:00
|
|
|
*/
|
|
|
|
mark = gtk_text_buffer_get_mark (buffer, "scroll");
|
|
|
|
gtk_text_buffer_move_mark (buffer, mark, &iter);
|
2011-09-02 03:55:47 +00:00
|
|
|
|
2006-08-26 01:17:17 +00:00
|
|
|
/* Scroll the mark onscreen.
|
|
|
|
*/
|
|
|
|
gtk_text_view_scroll_mark_onscreen (textview, mark);
|
|
|
|
|
|
|
|
/* Shift text back if we got enough to the right.
|
|
|
|
*/
|
|
|
|
if (count > 40)
|
|
|
|
count = 0;
|
|
|
|
|
2012-01-31 00:12:27 +00:00
|
|
|
return G_SOURCE_CONTINUE;
|
2006-08-26 01:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
setup_scroll (GtkTextView *textview,
|
|
|
|
gboolean to_end)
|
|
|
|
{
|
|
|
|
GtkTextBuffer *buffer;
|
|
|
|
GtkTextIter iter;
|
|
|
|
|
|
|
|
buffer = gtk_text_view_get_buffer (textview);
|
|
|
|
gtk_text_buffer_get_end_iter (buffer, &iter);
|
|
|
|
|
|
|
|
if (to_end)
|
2011-09-02 03:55:47 +00:00
|
|
|
{
|
|
|
|
/* If we want to scroll to the end, including horizontal scrolling,
|
|
|
|
* then we just create a mark with right gravity at the end of the
|
2014-01-17 15:25:30 +00:00
|
|
|
* buffer. It will stay at the end unless explicitly moved with
|
2011-09-02 03:55:47 +00:00
|
|
|
* gtk_text_buffer_move_mark.
|
|
|
|
*/
|
|
|
|
gtk_text_buffer_create_mark (buffer, "end", &iter, FALSE);
|
|
|
|
|
|
|
|
/* Add scrolling timeout. */
|
|
|
|
return g_timeout_add (50, (GSourceFunc) scroll_to_end, textview);
|
|
|
|
}
|
2006-08-26 01:17:17 +00:00
|
|
|
else
|
2011-09-02 03:55:47 +00:00
|
|
|
{
|
|
|
|
/* If we want to scroll to the bottom, but not scroll horizontally,
|
|
|
|
* then an end mark won't do the job. Just create a mark so we can
|
|
|
|
* use it with gtk_text_view_scroll_mark_onscreen, we'll position it
|
2014-01-17 15:25:30 +00:00
|
|
|
* explicitly when needed. Use left gravity so the mark stays where
|
2011-09-02 03:55:47 +00:00
|
|
|
* we put it after inserting new text.
|
|
|
|
*/
|
|
|
|
gtk_text_buffer_create_mark (buffer, "scroll", &iter, TRUE);
|
|
|
|
|
|
|
|
/* Add scrolling timeout. */
|
|
|
|
return g_timeout_add (100, (GSourceFunc) scroll_to_bottom, textview);
|
|
|
|
}
|
2006-08-26 01:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_timeout (GtkWidget *window,
|
|
|
|
gpointer timeout)
|
|
|
|
{
|
|
|
|
g_source_remove (GPOINTER_TO_UINT (timeout));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_text_view (GtkWidget *hbox,
|
|
|
|
gboolean to_end)
|
|
|
|
{
|
|
|
|
GtkWidget *swindow;
|
|
|
|
GtkWidget *textview;
|
|
|
|
guint timeout;
|
|
|
|
|
2020-06-24 15:25:09 +00:00
|
|
|
swindow = gtk_scrolled_window_new ();
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), swindow);
|
2006-08-26 01:17:17 +00:00
|
|
|
textview = gtk_text_view_new ();
|
2020-05-02 04:51:20 +00:00
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (swindow), textview);
|
2006-08-26 01:17:17 +00:00
|
|
|
|
|
|
|
timeout = setup_scroll (GTK_TEXT_VIEW (textview), to_end);
|
|
|
|
|
|
|
|
/* Remove the timeout in destroy handler, so we don't try to
|
2011-09-02 03:55:47 +00:00
|
|
|
* scroll destroyed widget.
|
2006-08-26 01:17:17 +00:00
|
|
|
*/
|
|
|
|
g_signal_connect (textview, "destroy",
|
|
|
|
G_CALLBACK (remove_timeout),
|
|
|
|
GUINT_TO_POINTER (timeout));
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_textscroll (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
GtkWidget *hbox;
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2015-06-20 01:59:15 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Automatic Scrolling");
|
2020-05-09 16:03:11 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2006-08-26 01:17:17 +00:00
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
|
2011-09-02 03:55:47 +00:00
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
|
|
|
|
gtk_box_set_homogeneous (GTK_BOX (hbox), TRUE);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), hbox);
|
2006-08-26 01:17:17 +00:00
|
|
|
|
|
|
|
create_text_view (hbox, TRUE);
|
|
|
|
create_text_view (hbox, FALSE);
|
|
|
|
}
|
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (window))
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2006-08-26 01:17:17 +00:00
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2006-08-26 01:17:17 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|