TextView and ScrolledWindow not behaving properly



Hi,

I have a TextView in a ScrolledWindow, but it's not behaving like it
should:

 * It should be possible to shrink the window to a size less than the
   text width (my example can show why this isn't always true)
 * The TextWidget should not expand the window while you type or insert
   text into it. E.g. a 50x50 window should stay 50x50 nomatter the text
   inserted. The user should still be able to resize it as he wishes.
 * The text should wrap.
 * There should be no horizontal scrollbar.

The below example illustrates my problem, I have set wrap_mode to
GTK_WRAP_CHAR, but the text doesn't wrap when the window is shrinked.

If you uncomment the gtk_widget_set_size_request(), the text does wrap,
but when shrinking the window, the window can't get any smaller than the
length of the text. E.g. if I type and the window gets enlarged to
100x100 pixels, it can't be shrinked down.

Enough mumbling:

#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *scrolled_window;
    GtkWidget *view;
    GtkTextBuffer *buffer;

    gtk_init (&argc, &argv);

    scrolled_window = gtk_scrolled_window_new (NULL, NULL);

    gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), 10);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW (window), 600, 500);
    gtk_window_set_resizable (GTK_WINDOW (window), TRUE);

    view = gtk_text_view_new ();

    buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));

    gtk_text_buffer_set_text (buffer, "Hello, this is some text", -1);

    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, 
GTK_POLICY_ALWAYS);

    /* when this line is enabled, the window can be shrinked but the text
     * doesn't wrap */
    gtk_widget_set_size_request (scrolled_window, 0, -1);

    gtk_scrolled_window_add_with_viewport (
            GTK_SCROLLED_WINDOW (scrolled_window), view);

    gtk_container_add (GTK_CONTAINER (window), scrolled_window);
    gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_CHAR);

    gtk_widget_show (window);
    gtk_widget_show (view);
    gtk_widget_show (scrolled_window);

    gtk_main ();

    return 0;
}
 



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