Re: TextView scrolling/cursor problems




--- Begin Message ---


Andreas Ntaflos wrote:

Hello list, I am new here and quite new to gtkmm and have found the docs, especially the book, very useful so far. Unfortunately the book doesn't cover everything (Iterators in a TextBuffer for example) so I have a few questions that can hopefully be answered by one of the more experienced gtkmm users :)

What is the best way to create a TextView (non-editable, no cursor visible, in a ScrolledWindow) that scrolls automatically when text is inserted? I've gathered something like the following basically works:

// Get current mark at insert (cursor) position cur_mark = textviewBuffer->get_insert();

// Get iterator from mark
iter = textviewBuffer->get_iter_at_mark(cur_mark);

// Insert text and save iterator (although not used anymore?)
iter = textviewBuffer->insert(iter, "Text portion with many lines");

// Get current mark at insert position
cur_mark = textviewBuffer->get_insert();

// Scroll to mark
textview->scroll_to(cur_mark, 0.0);

This gets a mark from the current cursor position, an iterator from that mark, inserts text at that interator position, saves the resulting iterator (which isn't used again though). Then the mark gets updated from the new cursor position and scrolling to that mark occurs.

This can be repeated (without the first call to get_insert()) and works fine for inserting text and scrolling to the end of it.

However, and here's my problem, when clicking somewhere in the TextView the cursor position gets set to where I clicked. Calling get_insert(), get_iter_at_mark() and insert() afterwards put the new text right where I clicked in the TextView.

This of course not the behaviour I want from the TextView and TextViewBuffer. How can I avoid that? I want new text to be inserted only at the bottom of the view, no matter whether I click the TextView or not, like seen in countless other GTK applications. Or are there other (better) ways to have an autoscrolling TextView?

Help and ideas would be appreciated!

Andreas,

Two possibilities.

First, disable button press events on the TextView to prevent responding to mouse clicks.

Or, instead of this:

// Get current mark at insert (cursor) position cur_mark = textviewBuffer->get_insert();

// Get iterator from mark
iter = textviewBuffer->get_iter_at_mark(cur_mark);

// Insert text and save iterator (although not used anymore?)
iter = textviewBuffer->insert(iter, "Text portion with many lines");

// Get current mark at insert position
cur_mark = textviewBuffer->get_insert();

// Scroll to mark
textview->scroll_to(cur_mark, 0.0);

Try:

// Insert text and save iterator (although not used anymore?)
iter = textviewBuffer->insert(textviewBuffer->end(), "Text portion with many lines");

// Get current mark at insert position
cur_mark = textviewBuffer->get_insert();

// Scroll to mark
textview->scroll_to(cur_mark, 0.0);


Granted I haven't tried compiling this. But assuming the mark thingy (technical term) gets updated, all should be well. And assuming this works, its probably the better method. Cause you should probably want to leave moust button events so users can select some portion of the text and copy it to the clipboard.

Cheers,
Paul

Thanks in advance!

Andreas
------------------------------------------------------------------------

_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list


--- End Message ---


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