TextView and ScrolledWindow not behaving properly (repost from gtk-app-devel-list)



Hi,

I posted this on gtk-app-devel-list a couple of days ago, but got no
real replies. So if nobody here can answer my question, I'll report it
as a bug.

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

 * 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 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() line, 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 you type in some text, the windows
gets enlarged and cannot be shrinked down to its previous size.

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]