GtkTextView questions

  1. How do I get the contents of the entire text widget as a string?

    See gtk_text_buffer_get_bounds() and gtk_text_buffer_get_text() or gtk_text_iter_get_text().

    GtkTextIter start, end;
    GtkTextBuffer *buffer;
    char *text;
    
    buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
    gtk_text_buffer_get_bounds (buffer, &start, &end);
    text = gtk_text_iter_get_text (&start, &end);
    /* use text */
    g_free (text);
    
  2. How do I make a text widget display its complete contents in a specific font?

    If you use gtk_text_buffer_insert_with_tags() with appropriate tags to select the font, the inserted text will have the desired appearance, but text typed in by the user before or after the tagged block will appear in the default style.

    To ensure that all text has the desired appearance, use gtk_widget_override_font() to change the default font for the widget.

  3. How do I make a text view scroll to the end of the buffer automatically ?

    A good way to keep a text buffer scrolled to the end is to place a mark at the end of the buffer, and give it right gravity. The gravity has the effect that text inserted at the mark gets inserted before, keeping the mark at the end.

    To ensure that the end of the buffer remains visible, use gtk_text_view_scroll_to_mark() to scroll to the mark after inserting new text.

    The gtk-demo application contains an example of this technique.