Scrolling to the end of a textview when it changes



Hi All,
  I have an output widget in my program that inherits from a
Gtk::TextView. When my program produces output it feeds this into the
corresponding Gtk::TextBuffer. I want my output widget to scroll to
the end when its contents changes such that the user is presented with
the most recent output. Also, I want the widget to resize itself, such
that it fits the size of the output, with the restriction that it
never gets larger than 20 lines. To do this, I've connected the
following function to Gtk::TextBuffer::change_handler

void GUI::output_widget::on_buffer_changed ()
{
  // Scroll to end
  const Gtk::TextIter end = buffer.end ();
  Glib::RefPtr<Gtk::TextMark> end_mark = buffer.create_mark (end);
  scroll_to (end_mark);

  // Set a sensible widget height
  const int line_height = 17; // XXX: HACK!!
  const int max_num_lines = 20;
  const int rows = std::min (buffer.get_line_count (), max_num_lines);
  set_size_request (-1, line_height * rows);
}

Most of the time this works fine, but if I produce a lot of output
very fast, then I get the following message printed to the terminal:

(<unknown>:8687): Gtk-WARNING **: Invalid text buffer iterator: either
the iterator is uninitialized, or the characters/pixbufs/widgets in
the buffer have been modified since the iterator was created.
You must use marks, character numbers, or line numbers to preserve a
position across buffer modifications.
You can apply tags and insert marks without invalidating your iterators,
but any mutation that affects 'indexable' buffer contents (contents
that can be referred to by character offset)
will invalidate all outstanding iterators

So, any thoughts on how I fix this issue?

Søren


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]