Calculating the size of TextView



Hi
I am working on a GTK application that creates a form with many text
areas in it. It seems to work fine until I add my widgets to a
scrolled pane. Like this:

test.vala:

using Gtk;

class MainWindow : Gtk.Window {
        
        Box vbox = new Box (Orientation.VERTICAL, 5);
        
        public MainWindow () {
                set_default_size (700, 600);
        
                Button b = new Button ();
                b.clicked.connect (add_text_field);
                vbox.pack_start (b, false, false, 0);

                ScrolledWindow scrolled = new ScrolledWindow (null, null);
                scrolled.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
                scrolled.add_with_viewport (vbox);
                                
                add (scrolled);
                show_all ();
        }
        
        void add_text_field () {
                TextView text_view1 = new TextView ();
                TextBuffer text_buffer = new TextBuffer (null);
                text_buffer.set_text ("""A long text ... A long text ... A long text
... A long text ... A long text ... A long text ... A long text ... A
long text ... A long text ... A long text ... A long text ... A long
text ... A long text ... A long text ... A long text ... A long text
... A long text ... A long text ... A long text ... A long text ... A
long text ... A long text ... A long text ... A long text ... A long
text ... A long text ... A long text ... A long text ... A long text
... A long text ... A long text ... A long text ... A long text ... A
long text ... A long text ... A long text ... A long text ... A long
text ... A long text """);

                text_view1.set_wrap_mode (WrapMode.WORD);
                text_view1.set_buffer (text_buffer);
                
                vbox.pack_start (new Label ("New text view"), false, false, 0);
                vbox.pack_start (text_view1, false, false, 0);
                show_all ();
        }
}

public static void main(string[] args) {
        MainWindow m;
        Gtk.init (ref args);
        m = new MainWindow ();
        Gtk.main ();
}

Compile with valac test.vala --pkg gtk+-3.0

The height of the TextView is twice the number of lines in the box
when TextView is added to the box. GTK will resize TextView when it
gets focus.
Is there a way to force GTK to recalculate the size of TextView before
it receives focus?
/Johan



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