Re: GtkTreeViewColumn width




 
Hi Phil,

You should be able to resize columns if you want. If you have set sizes of text to display in the tree view 
and want to allocate sizes you can do that.

Eric


/*
    Tested with Ubuntu16.04 and GTK3.18.
    gcc -Wall tree_column1.c -o tree_column1 `pkg-config --cflags --libs gtk+-3.0`
*/

#include<gtk/gtk.h>

static GtkTreeStore* get_tree_store();
static void set_column2_width(GtkComboBox *combo, GtkTreeViewColumn *column2);

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), "Tree View");
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkTreeStore *store=get_tree_store();

    GtkWidget *tree=gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
    g_object_unref(G_OBJECT(store));

    GtkCellRenderer *renderer1=gtk_cell_renderer_text_new();
    g_object_set(renderer1, "editable", FALSE, NULL);
   
    GtkTreeViewColumn *column1=gtk_tree_view_column_new_with_attributes("Column1", renderer1, "text", 0, 
NULL);
    gtk_tree_view_column_set_sizing(GTK_TREE_VIEW_COLUMN(column1), GTK_TREE_VIEW_COLUMN_FIXED);
    gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(column1), 100);

    GtkTreeViewColumn *column2=gtk_tree_view_column_new_with_attributes("Column2", renderer1, "text", 1, 
NULL);
    gtk_tree_view_column_set_sizing(GTK_TREE_VIEW_COLUMN(column2), GTK_TREE_VIEW_COLUMN_FIXED);
    gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(column2), 25);

    GtkTreeViewColumn *column3=gtk_tree_view_column_new_with_attributes("Column3", renderer1, "text", 2, 
NULL);
    
    gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column1);
    gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column2); 
    gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column3); 

    GtkWidget *scroll=gtk_scrolled_window_new(NULL, NULL);
    gtk_container_set_border_width(GTK_CONTAINER(scroll), 5);
    gtk_widget_set_vexpand(scroll, TRUE);
    gtk_widget_set_hexpand(scroll, TRUE);
    gtk_container_add(GTK_CONTAINER(scroll), tree); 

    GtkWidget *combo=gtk_combo_box_text_new();
    gtk_widget_set_hexpand(combo, TRUE);
    gtk_combo_box_text_insert(GTK_COMBO_BOX_TEXT(combo), 0, "1", "Column2 width 25");
    gtk_combo_box_text_insert(GTK_COMBO_BOX_TEXT(combo), 1, "2", "Column2 width 50");
    gtk_combo_box_text_insert(GTK_COMBO_BOX_TEXT(combo), 2, "3", "Column2 width 100");
    gtk_combo_box_text_insert(GTK_COMBO_BOX_TEXT(combo), 3, "4", "Column2 width 150");
    gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
    g_signal_connect(combo, "changed", G_CALLBACK(set_column2_width), column2);   
    
    GtkWidget *grid=gtk_grid_new();
    gtk_container_set_border_width(GTK_CONTAINER(grid), 20);
    gtk_grid_attach(GTK_GRID(grid), scroll, 0, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), combo, 0, 1, 1, 1);
    gtk_container_add(GTK_CONTAINER(window), grid);
   
    gtk_widget_show_all(window);
    gtk_main();
    return 0;   
  }
static GtkTreeStore* get_tree_store()
  {
    gint i=0;
    GtkTreeStore *store=gtk_tree_store_new(3, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING);
        
    GtkTreeIter iter1;
    for(i=0;i<10;i++)
      {
        gchar *string1=g_strdup_printf("Name %i", i);
        gtk_tree_store_append(store, &iter1, NULL);
        gtk_tree_store_set(store, &iter1, 0, i, 1, string1, 2, string1, -1);
        g_free(string1);
      }
  
    return store;
  }
static void set_column2_width(GtkComboBox *combo, GtkTreeViewColumn *column2)
  {
    gint combo_id=gtk_combo_box_get_active(combo);
    if(combo_id==0) gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(column2), 25);
    else if(combo_id==1) gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(column2), 50);
    else if(combo_id==2) gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(column2), 100);
    else gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(column2), 150);
  }


 




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