How to create a scrolled text window with GTK 2.x?



  In GTK 1.x I could say this:

    GtkWidget* textWindow = gtk_text_new(0, 0);
    GtkWidget* scrollBar = gtk_vscrollbar_new(GTK_TEXT(textWindow)->vadj);

    GtkWidget* hBox = gtk_hbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(hBox), textWindow, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(hBox), scrollBar, FALSE, TRUE, 0);

  This created a nice scrolled text window which always scrolled to the
end when new text appeared at the end of the text window. This is exactly
the behaviour I want in an application I'm currently developing.

  However, I'm using GTK 2.x, and the above code is deprecated, and I'm
unable to get the same behaviour with the new version of the library. I have
been reading the API reference all over, but I don't get it.

  I have tried a similar code with the new GtkTextView widget, like this:

    GtkWidget* textWindow = gtk_text_view_new();
    GtkWidget* textWindowScrollBar =
        gtk_vscrollbar_new(GTK_TEXT_VIEW(textWindow)->vadjustment);

    GtkWidget* hBox = gtk_hbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(hBox), textWindow, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(hBox), textWindowScrollBar, FALSE, TRUE, 0);

  However, this doesn't work. The scrollbar is completely non-functional
(and taking textWindow->vadjustment is probably non-standard as well).

  I have also tried this:

    GtkWidget* textWindow = gtk_text_view_new();
    GtkWidget* scrolledTextWindow = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledTextWindow),
                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_add_with_viewport
        (GTK_SCROLLED_WINDOW(scrolledTextWindow), textWindow);

  However, this doesn't work like I want: It does not automatically scroll
to the end when new text appears at the end of the text window.

  How can I get the same type of scrolling text window as in GTK 1.x?


  And by the way, another question:

  For example the GtkPaned widget is specified to throw eg. a signal
called "accept-position". However, if I create eg. a GtkVPaned and
connect this signal to a function, like this:

    g_signal_connect(G_OBJECT(mainPanedWindow), "accept-position",
                     G_CALLBACK(mainPanedWindowResize), NULL);

nothing happens. The mainPanedWindowResize() function is never called.

  GTK is accepting this signal connection (because if I put an invalid
signal name in the string, GTK prints an error message at runtime, but
with "accept-position" it doesn't).

  The same holds for all the other signals GtkPaned is documented to throw
(eg. "move-handle" etc). Even if I connect the signal with a function, the
function is never called.

  To do what I wanted I had to connect the "size-allocate" signal thrown
by GtkWidget, and that worked (even though the function is called more
often than I would really want). But why doesn't connecting "accept-position"
work?

							- Juha Nieminen




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