tags:

views:

523

answers:

1

I have a GTK# Text View with word wrap and scroll bars turned on. I know that horizontal scroll bars will not be show because the words will wrap instead.

I want to add some text but before adding it I want to find out if the text will fit without the need for a vertical scroll bar.

I don't want to add the text if it means that the text view will require a vertical scroll bar.

How do I do this?

+2  A: 

I'm not aware of any way to measure the contents of a TextView without actually updating the text. It's a complex text layout widget that can handle formatted text, images, etc. Inserting text could change the layout quite substantially, so you can't "predict" how some new text will change the size without actually modifying the buffer and recalculating its layout.

It would be easier to suggest a solution if you explain why you need this functionality.

For example, if you simply need to know after adding text whether when the view will have scrollbars, you could use the scroll adjustments - the TextView is a "scrollable" widget, i.e. its layoutspace is virtualised, and is manipulated by a parent ScrolledWindow via "Adjustment" objects that contain the range, page size, scroll increment size, etc. You could detect when they change by accessing them from the ScrolledWindow and subscribing to their change events.

mhutch
I don't want to add the text if it means that the text view will require a vertical scroll bar.
trampster
I guess you could add it, then remove it if it caused a scrollbar.
mhutch
Is it possible to do this without it causing a flicker. Can I turn drawing to the screen off so the user does not see the Scroll bar appear and then disappear.
trampster
AFAIK GTK won't redraw immediately, so if you remove the text immediately, the scrollbars might not get shown. Try it and see.Another trick might be to avoid using a ScrolledWindow completely. I'm not sure if this is possible - you'd have certainly have to fake some things, like passing dummy adjustments to the TextView.I'm curious what you're using this for. If your formatting requirements are minimal, you could write a custom label widget using Pango directly (I've done this myself). That way you'd have absolute control over everything.
mhutch