RE: Scrollbars in a GtkTreeView



FYI, I got the scrollbar on a tree view working pretty nicely based on Tadej's suggestion, but it wasn't quite that simple to get the scroll bar positioned and sized properly. For the record, the code to do it is below (for vertical scrollbar only). Maybe this could be something to add to the Tree View tutorial which doesn't mention scrolling at all.

Ian


/*============================================================================*/
/*
 * Adds a vertical scrollbar to a tree view, returning an hbox holding them both
 */
GtkWidget *addScrollBarToTreeView(GtkWidget *treeView)
{
    GtkAdjustment *pAdj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(treeView));
    GtkWidget *vScroll = gtk_vscrollbar_new(pAdj);
    GtkWidget *padBox, *hBox, *vBox; 

    hBox = gtk_hbox_new(FALSE, 0);

    /* First insert the tree view */
    gtk_box_pack_start(GTK_BOX(hBox), treeView, TRUE, TRUE, 0);
    gtk_widget_show(treeView);

    /* Then, packed up against its right side, add in a vbox containing a
     * box for padding at the top and the scrollbar below that */
    vBox = gtk_vbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(hBox), vBox, FALSE, FALSE, 0);
    gtk_widget_show(vBox);
    padBox = gtk_hbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(vBox), padBox, FALSE, FALSE, 0);
    gtk_widget_show(padBox);
    gtk_box_pack_start(GTK_BOX(vBox), vScroll, TRUE, TRUE, 0);
    gtk_widget_show(vScroll);

    /* The padding box above the scroll bar needs to be set to the height of the
     * tree view's header_window, but we can't do that until it is mapped */
    SIGNAL_CONNECT(treeView, "map", treeViewMapHandler, padBox);

    return hBox;
}

/*============================================================================*/
void treeViewMapHandler(GtkTreeView *treeView, gpointer data)
{
    GtkWidget *padBox = (GtkWidget*)data; 
    gint x, y;

    g_assert(GTK_IS_HBOX(padBox));

    /* Set the size of the padding above the tree view's vertical scrollbar to the
     * height of its header_window (i.e. the offset to the top of its bin_window) */
    gtk_tree_view_convert_bin_window_to_widget_coords(GTK_TREE_VIEW(treeView),
                                                      0, 0, &x, &y);
    gtk_widget_set_size_request(padBox, -1, y);
}





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