/* Text View/Automatic Scrolling *#Keywords:GtkTextView,GtkScrolledWindow * *Thisexampledemonstrateshowtousethegravityof *GtkTextMarkstokeepatextviewscrolledtothebottom *whileappendingtext.
*/
#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; char *text; staticint count;
buffer = gtk_text_view_get_buffer (textview);
/* Get "end" mark. It's located at the end of buffer because *ofrightgravity
*/
mark = gtk_text_buffer_get_mark (buffer, "end");
gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
/* and insert some text at its position, the iter will be *revalidatedafterinsertiontopointtotheendofinsertedtext
*/
spaces = g_strnfill (count++, ' ');
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
gtk_text_buffer_insert (buffer, &iter, spaces, -1);
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);
g_free (spaces);
g_free (text);
/* Now scroll the end mark onscreen.
*/
gtk_text_view_scroll_mark_onscreen (textview, mark);
/* Emulate typewriter behavior, shift to the left if we *arefarenoughtotheright.
*/ if (count > 150)
count = 0;
return G_SOURCE_CONTINUE;
}
/* Scroll to the bottom of the buffer.
*/ static gboolean
scroll_to_bottom (GtkTextView *textview)
{
GtkTextBuffer *buffer;
GtkTextIter iter;
GtkTextMark *mark; char *spaces; char *text; staticint count;
buffer = gtk_text_view_get_buffer (textview);
/* Get end iterator */
gtk_text_buffer_get_end_iter (buffer, &iter);
/* and insert some text at it, the iter will be revalidated *afterinsertiontopointtotheendofinsertedtext
*/
spaces = g_strnfill (count++, ' ');
gtk_text_buffer_insert (buffer, &iter, "\n", -1);
gtk_text_buffer_insert (buffer, &iter, spaces, -1);
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);
g_free (spaces);
g_free (text);
/* Move the iterator to the beginning of line, so we don't scroll *inhorizontaldirection
*/
gtk_text_iter_set_line_offset (&iter, 0);
/* and place the mark at iter. the mark will stay there after we *insertsometextattheendbecauseithasleftgravity.
*/
mark = gtk_text_buffer_get_mark (buffer, "scroll");
gtk_text_buffer_move_mark (buffer, mark, &iter);
/* 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;
if (to_end)
{ /* If we want to scroll to the end, including horizontal scrolling, *thenwejustcreateamarkwithrightgravityattheendofthe *buffer.Itwillstayattheendunlessexplicitlymovedwith *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);
} else
{ /* If we want to scroll to the bottom, but not scroll horizontally, *thenanendmarkwon'tdothejob.Justcreateamarksowecan *useitwithgtk_text_view_scroll_mark_onscreen,we'llpositionit *explicitlywhenneeded.Useleftgravitysothemarkstayswhere *weputitafterinsertingnewtext.
*/
gtk_text_buffer_create_mark (buffer, "scroll", &iter, TRUE);
/* Remove the timeout in destroy handler, so we don't try to *scrolldestroyedwidget.
*/
g_signal_connect (textview, "destroy",
G_CALLBACK (remove_timeout),
GUINT_TO_POINTER (timeout));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.