开发者

Gtk, How to scroll at bottom of viewport list?

I have created one list with GtkVBox and GtkViewPort.

and i am doing the scroll by two up/down GtkButtons.

    GtkAdjustment* adjustment;
    adjustment = gtk_viewport_get_vadjustment(GTK_VIEWPORT(viewport_ptr));
    gtk_adjustment_set_value(adjustment, gtk_adjustment_get_value(adjustment)+(gdouble)SCROLL_SIZE);
    gtk_widget_show_all(viewport_ptr);

But when I add the widget to VBox it gets added at the end of the VBox as I am using gtk_box_pack_start. So i want to scroll viewport up to this newly added last widge开发者_StackOverflowt which is at bottom of the list.


Did you try just setting the adjustment to the maximum value:

gtk_adjustment_set_value(adjustment, gtk_adjustment_get_upper(adjustment));

Note that you might need to delay this, or trigger it from a suitable event, since the upper bound will change as the scrolled vbox grows.

My suggestion would be to hook it into the size-allocate signal of the GtkVBox.


Here's an example that shows what @unwind means when he suggests hooking the adjustment into the size-allocate signal.

/***
This basic example program shows how to (automatically) move the
scrollbar of a GTK+ text view to the very bottom after text has been added.

Public domain code by N.L.M. de Jonge.

gcc `pkg-config gtk+-3.0 --cflags` -O2 -std=c99 -g -pedantic -Wall \
-Wextra -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes \
-Wmissing-prototypes -D_REENTRANT example.c -o example `pkg-config \
gtk+-3.0 --libs`
***/

#include <gtk/gtk.h>
#include <stdlib.h>

GtkAdjustment *adj;
GtkWidget *scroll;
GtkTextBuffer *buffer;
GtkTextIter iter;
int iCounter;

void ScrollToEnd (GtkWidget *widget, GdkRectangle *allocation);
void Add (GtkButton *button);

/*****************************************************************************/
void ScrollToEnd (GtkWidget *widget, GdkRectangle *allocation)
/*****************************************************************************/
{
    if (widget != NULL) { }
    if (allocation != NULL) { }

    adj = gtk_scrolled_window_get_vadjustment
        (GTK_SCROLLED_WINDOW (scroll));
    gtk_adjustment_set_value (adj, gtk_adjustment_get_upper (adj));
}
/*****************************************************************************/
void Add (GtkButton *button)
/*****************************************************************************/
{
    char sText[10];

    if (button != NULL) { }

    iCounter++;
    snprintf (sText, 10, "\n%i", iCounter);
    gtk_text_buffer_get_end_iter (buffer, &iter);
    gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
        g_locale_to_utf8 (sText, -1, NULL, NULL, NULL), -1, "red_fg", NULL);

    /*** We just added text, but we do NOT call ScrollToEnd() here. ***/
}
/*****************************************************************************/
int main (int argc, char *argv[])
/*****************************************************************************/
{
    GtkWidget *window, *box, *grid, *text, *button;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    g_signal_connect (window, "delete_event",
        G_CALLBACK (exit), NULL);
    gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
    gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
    gtk_widget_realize (window);

    box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add (GTK_CONTAINER (window), box);
    gtk_widget_show (box);

    grid = gtk_grid_new();
    gtk_widget_show (grid);
    gtk_box_pack_start (GTK_BOX (box), grid, TRUE, TRUE, 0);

    scroll = gtk_scrolled_window_new (NULL, NULL);
    gtk_widget_set_size_request (scroll, 200, 200);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    /***/
    /*** We call ScrollToEnd() if scroll receives the size-allocate signal. ***/
    /***/
    g_signal_connect (scroll, "size-allocate", G_CALLBACK (ScrollToEnd), NULL);
    gtk_grid_attach (GTK_GRID (grid), scroll, 0, 0, 1, 1);
    g_object_set (scroll, "vexpand", TRUE, NULL);
    g_object_set (scroll, "margin", 10, NULL);
    gtk_widget_show (scroll);

    text = gtk_text_view_new();
    gtk_container_add (GTK_CONTAINER (scroll), text);
    gtk_widget_show (text);

    buffer = gtk_text_buffer_new (NULL);
    gtk_text_buffer_create_tag (buffer, "red_fg", "foreground", "red", NULL);
    gtk_text_view_set_buffer (GTK_TEXT_VIEW (text), buffer);

    button = gtk_button_new_with_label ("Add");
    g_signal_connect (button, "clicked", G_CALLBACK (Add), NULL);
    gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 1, 1);
    g_object_set (button, "hexpand", TRUE, NULL);
    gtk_widget_show (button);

    gtk_widget_show (window);

    iCounter = 0;
    gtk_text_buffer_get_end_iter (buffer, &iter);
    gtk_text_buffer_insert (buffer, &iter, g_locale_to_utf8
        ("Click \"Add\" to add numbers.", -1, NULL, NULL, NULL), -1);

    gtk_main();

    return 0;
}
/*****************************************************************************/

As a bonus it shows how to add colored text.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜