Re: Scrolling to a GtkListBoxRow after adding it to a GtkListBox




 
Hi Mike,

It looks like the new rows height isn't getting added to the adjustment. If the height of the new row gets 
added to the upper value of the adjustment then the last row of the list box can be shown when added. This is 
what I came up with to test it out. Is this similar to what you are seeing?

Eric

/*      
    gcc -Wall list_box1.c -o list_box1 `pkg-config --cflags --libs gtk+-3.0`

    Tested on Ubuntu16.04 and GTK3.18
*/

#include<gtk/gtk.h>

static GtkWidget *create_row(const gchar *text)
  {
    GtkWidget *row=gtk_list_box_row_new();
    gtk_widget_set_size_request(row, 370, 50);
    gtk_list_box_row_set_selectable(GTK_LIST_BOX_ROW(row), TRUE);
    gtk_list_box_row_set_activatable(GTK_LIST_BOX_ROW(row), TRUE);
    GtkWidget *textview=gtk_text_view_new();
    gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_CHAR);
    gtk_widget_set_hexpand(textview, TRUE);
    gtk_widget_set_vexpand(textview, TRUE);
    GtkTextBuffer *buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
    gtk_text_buffer_set_text(buffer, text, -1);
    GtkWidget *scroll=gtk_scrolled_window_new(NULL, NULL);
    gtk_widget_set_hexpand(scroll, TRUE);
    gtk_widget_set_vexpand(scroll, TRUE);
    gtk_container_add(GTK_CONTAINER(scroll), textview);
    GtkWidget *box=gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
    gtk_container_set_border_width(GTK_CONTAINER(box), 5);
    g_object_set (box, "margin-end", 10, NULL);
    GtkWidget *label=gtk_label_new(text);  
    gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(box), scroll, TRUE, TRUE, 0);  
    gtk_container_add(GTK_CONTAINER(row), box);

    return row;
  }
static void add_row(GtkWidget *button, GtkWidget *list_box)
  {
    static gint i=10;
    gchar *text=g_strdup_printf("Row %d", i);
    GtkWidget *row=create_row(text);
    gtk_list_box_insert(GTK_LIST_BOX(list_box), row, -1);
    gtk_widget_show_all(row);
    gtk_list_box_select_row(GTK_LIST_BOX(list_box), GTK_LIST_BOX_ROW(row));

    GtkRequisition minimum_size; 
    GtkRequisition natural_size;
    gtk_widget_get_preferred_size(row, &minimum_size, &natural_size);
    g_print("min %i nat %i\n", minimum_size.height, natural_size.height);

    GtkAdjustment *adj=gtk_list_box_get_adjustment(GTK_LIST_BOX(list_box));
    gdouble upper=gtk_adjustment_get_upper(adj);
    //Add extra height to adjustment.
    gtk_adjustment_set_upper(adj, upper+minimum_size.height);
    upper=gtk_adjustment_get_upper(adj);    
    gtk_adjustment_set_value(adj, upper);
    
    g_free(text);
    i++;
  }
int main(int argc, char **argv)
  {
    gtk_init(&argc, &argv);

    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "List Box");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkWidget *list_box=gtk_list_box_new();
    gtk_list_box_set_activate_on_single_click(GTK_LIST_BOX(list_box), TRUE);
    gtk_widget_set_hexpand(list_box, TRUE);
    gtk_widget_set_vexpand(list_box, TRUE);

    gint i=0;
    GtkWidget *row=NULL;
    for(i=0;i<10;i++) 
      {
        gchar *text=g_strdup_printf("Row %d", i);
        row=create_row(text);
        gtk_list_box_insert(GTK_LIST_BOX(list_box), row, -1);
        g_free(text);
      }

    GtkWidget *button=gtk_button_new_with_label("Add Row");
    gtk_widget_set_hexpand(button, TRUE);
    g_signal_connect(button, "clicked", G_CALLBACK(add_row), list_box);

    GtkWidget *scroll_win=gtk_scrolled_window_new(NULL, NULL);
    gtk_widget_set_hexpand(scroll_win, TRUE);
    gtk_widget_set_vexpand(scroll_win, TRUE);
    gtk_container_add(GTK_CONTAINER(scroll_win), list_box);

    GtkWidget *grid1=gtk_grid_new();
    gtk_container_set_border_width(GTK_CONTAINER(grid1), 15);
    gtk_grid_set_row_spacing(GTK_GRID(grid1), 8);
    gtk_grid_attach(GTK_GRID(grid1), scroll_win, 0, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(grid1), button, 0, 1, 1, 1);
   
    gtk_container_add(GTK_CONTAINER(window), grid1);

    gtk_widget_show_all(window);

    gtk_main();

    return 0;  
  }




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