GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Oct 25, 2014 11:36 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: GtkTreeview get selected column
PostPosted: Thu Apr 30, 2009 3:09 pm 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 69
Location: España
How can i do that?.
I'm looking for into GtkTreeSelection but it only (seems) take a row.
any help, please?.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 30, 2009 4:54 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello.

The highest granularity of GtkTreeView is generally limited to a row (you cannot select single cell).

What you can do is determine which column is located under certain coordinates. This is mostly useful in "button-press-event" handlers, where you can take the coordinates of the button press and obtain cell renderer being clicked on. Function that provides this data is gtk_tree_view_ge_path_at_pos.

Hope this helps a bit.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 04, 2009 9:41 am 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 69
Location: España
Thnak yoo tadeboro.
An example over your's:
(Again, excuse my poor english... and very bad slovenský)


Code:
#include <gtk/gtk.h>

enum
{
    COL_JEDEN,
    COL_DVA,
    COL_TEXT,
    COL_IMG,
    N_COLS
};



void AdjustCol(GtkTreeViewColumn *Column)
{
PangoLayout *layout;
gint w,h;

printf("adjust %s\n",gtk_tree_view_column_get_title(Column));
layout = gtk_widget_create_pango_layout(gtk_tree_view_column_get_tree_view(Column),gtk_tree_view_column_get_title(Column));
pango_layout_get_pixel_size (layout, &w, &h);
g_object_unref(layout);
   
gtk_tree_view_column_set_fixed_width(Column,w);
}




static gboolean mouse(GtkWidget *widget,GdkEventButton *event,GtkWidget * Menu)
{
if((event->button == 1) && (event->type == GDK_BUTTON_PRESS))
  {
  printf("click \n");
 
  GtkTreeViewColumn *col;
  gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), (gint) event->x, (gint) event->y, NULL, &col, NULL, NULL);
  AdjustCol(col);
  }
return FALSE; 
}




void MenuPop(GtkWidget *Container)
{
GtkWidget *menu,*item;
gint items;
gchar texts[3][64] = {"Adjust","item 2","item 3"};

gchar icons[3][32] = {"gtk-indent","gtk-unindent","gtk-justify-right"};

menu = gtk_menu_new();
for(items = 0;items < 18;items++)
   {
   item = gtk_image_menu_item_new_with_label(texts[items]);
   gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),gtk_image_new_from_stock(icons[items],1));
   gtk_menu_shell_append(GTK_MENU_SHELL(menu),item);
   gtk_widget_show(item);
   }
gtk_menu_attach_to_widget(GTK_MENU(menu),Container,NULL);
g_signal_connect(G_OBJECT(Container),"button-press-event",G_CALLBACK(mouse),(gpointer)menu);
}




int
main( int    argc,
      char **argv )
{
    GtkWidget         *window;
    GtkWidget         *treeview;
    GtkListStore      *store;
    GtkTreeIter        iter;
    GtkTreeViewColumn *column;
    GtkCellRenderer   *renderer;
   
    register gint      i;
   
   
    gtk_init( &argc, &argv );
   
    /* Create main window */
    window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    gtk_window_set_default_size( GTK_WINDOW( window ), 400, 300 );
    g_signal_connect( G_OBJECT( window ), "destroy",
                      G_CALLBACK( gtk_main_quit ), NULL );
   
    /* Create tree view and pack it into window */
    treeview = gtk_tree_view_new();
    gtk_container_add( GTK_CONTAINER( window ), treeview );
   
    /* Create store and populate it with some data */
    store = gtk_list_store_new( N_COLS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, GDK_TYPE_PIXBUF );
    for( i = 0; i < 15; i++ )
    {
        GdkPixbuf *pixbuf;
       
        pixbuf = gtk_widget_render_icon( GTK_WIDGET( treeview ),
                GTK_STOCK_CANCEL,GTK_ICON_SIZE_BUTTON,
                                         NULL );
       
        gtk_list_store_append( store, &iter );
       gtk_list_store_set( store, &iter,
                            COL_JEDEN, g_strdup_printf( "Sample %d", i + 1 ),
                            COL_DVA, g_strdup_printf( "vzor %d", i + 1 ),
                            COL_TEXT, g_strdup_printf( "ejemplo %d", i + 1 ),
                            COL_IMG, pixbuf,
                            -1 );
        g_object_unref( G_OBJECT( pixbuf ) );
    }
   
    /* Set treeview's model */
    gtk_tree_view_set_model( GTK_TREE_VIEW( treeview ),
                             GTK_TREE_MODEL( store ) );
    g_object_unref( G_OBJECT( store ) );
   
    /* Create display part of tree view - column 1 - 2 - 3*/
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new();
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column ), renderer, TRUE );
    gtk_tree_view_column_set_attributes( column, renderer,
                                         "text", COL_JEDEN, NULL );
    gtk_tree_view_column_set_title(column,"English");
   
    //LOOK!!!!!!!   
    gtk_tree_view_column_set_resizable(column,TRUE);
    gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_FIXED);
    gtk_tree_view_column_set_fixed_width(column,40);
   
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );                       

    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new();
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column ), renderer, TRUE );
    gtk_tree_view_column_set_attributes( column, renderer,
                                         "text", COL_DVA, NULL );
    gtk_tree_view_column_set_title(column,"Slovenský");
    gtk_tree_view_column_set_resizable(column,TRUE);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );

    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new();
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column ), renderer, TRUE );
    gtk_tree_view_column_set_attributes( column, renderer,
                                         "text", COL_TEXT, NULL );
    gtk_tree_view_column_set_title(column,"Español");
    gtk_tree_view_column_set_resizable(column,TRUE);
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );
   
    /* Create display part of tree view - column 2 */
    renderer = gtk_cell_renderer_pixbuf_new();
    column = gtk_tree_view_column_new();
    gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( column ), renderer, TRUE );
    gtk_tree_view_column_set_attributes( column, renderer,
                                         "pixbuf", COL_IMG, NULL );
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );
   
    MenuPop(treeview);
   
   
    gtk_widget_show_all( window );
   
    gtk_main();
   
    return( 0 );
}



Top
 Profile  
 
 Post subject:
PostPosted: Tue May 05, 2009 2:50 pm 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 69
Location: España
In this case, with integers.
You have two important items:
switch(G_VALUE_TYPE(&val))
and
g_object_set_data(G_OBJECT(column),"idcoln",GINT_TO_POINTER(4));
ViewCol(GTK_TREE_VIEW(widget),GPOINTER_TO_INT(g_object_get_data(G_OBJECT(col),"idcoln")));


Code:
#include <gtk/gtk.h>
#include <string.h>



enum
{
    COL_JEDEN,
    COL_DVA,
    COL_TEXT,
    COL_INT,
    COL_IMG,
    N_COLS
};

void ViewCol (GtkTreeView *tview,gint column)
{
GtkTreeModel *Tmod=gtk_tree_view_get_model(GTK_TREE_VIEW(tview));
GtkTreePath *tpath=gtk_tree_path_new_first();
GtkTreeIter itr;
GValue val = {0};
gchar *col;
gint i;

col = (gchar *)g_malloc(1);

while (gtk_tree_model_get_iter(Tmod,&itr,tpath) == TRUE)
     {
     gtk_tree_model_get_value(Tmod,&itr,column,&val);

     switch(G_VALUE_TYPE(&val))
          {
          case G_TYPE_STRING:
               {
               col = (gchar *)g_realloc(col,(gint)strlen(g_value_get_string(&val))+1);
               strcpy(col,g_value_get_string(&val));
               } break;
          case G_TYPE_INT:
               {
               col = (gchar *)g_realloc(col,32); //up 32 digits
               i = g_value_get_int(&val);
               g_ascii_dtostr(col,32,i);
               } break;
          }

     g_value_unset(&val);
     g_print("test: %s \n",col);
     gtk_tree_path_next(tpath);

     }
g_free(col);
}



static gboolean treeclic(GtkWidget *widget,GdkEventButton *event,gpointer data)
{
if((event->button == 1) && (event->type == GDK_BUTTON_PRESS))
  {
  printf("tree click \n");

  GtkTreeViewColumn *col;
  gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), (gint) event->x,(gint) event->y, NULL, &col, NULL, NULL);
  ViewCol(GTK_TREE_VIEW(widget),GPOINTER_TO_INT(g_object_get_data(G_OBJECT(col),"idcoln")));
  }
return FALSE;
}






int
main( int    argc,
      char **argv )
{
    GtkWidget         *window;
    GtkWidget         *treeview;
    GtkListStore      *store;
    GtkTreeIter        iter;
    GtkTreeViewColumn *column;
    GtkCellRenderer   *renderer;
   
    register gint      i;
   
   
    gtk_init( &argc, &argv );
   
    /* Create main window */
    window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    gtk_window_set_default_size( GTK_WINDOW( window ), 400, 300 );
    g_signal_connect( G_OBJECT( window ), "destroy",
                      G_CALLBACK( gtk_main_quit ), NULL );
   
    /* Create tree view and pack it into window */
    treeview = gtk_tree_view_new();
    gtk_container_add( GTK_CONTAINER( window ), treeview );
   
    /* Create store and populate it with some data */
    store = gtk_list_store_new( N_COLS, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING, G_TYPE_INT, GDK_TYPE_PIXBUF );
    for( i = 0; i < 15; i++ )
    {
        GdkPixbuf *pixbuf;
       
        pixbuf = gtk_widget_render_icon( GTK_WIDGET( treeview ),
                GTK_STOCK_CANCEL,GTK_ICON_SIZE_BUTTON,
                                         NULL );
       
        gtk_list_store_append( store, &iter );
       gtk_list_store_set( store, &iter,
                            COL_JEDEN, g_strdup_printf( "Sample %d", i + 1),
                            COL_DVA, g_strdup_printf( "vzor %d", i + 1 ),
                            COL_TEXT, g_strdup_printf( "ejemplo %d", i + 1),
                            COL_INT, i + 100,
                            COL_IMG, pixbuf,-1 );
        g_object_unref( G_OBJECT( pixbuf ) );
    }
   
    /* Set treeview's model */
    gtk_tree_view_set_model( GTK_TREE_VIEW( treeview ),
                             GTK_TREE_MODEL( store ) );
    g_object_unref( G_OBJECT( store ) );
   
    /* Create display part of tree view - column 1 - 2 - 3*/
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new();
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column ), renderer, TRUE );
    gtk_tree_view_column_set_attributes( column, renderer,
                                         "text", COL_JEDEN, NULL );
    gtk_tree_view_column_set_title(column,"English");
   
    //LOOK!!!!!!!   
    gtk_tree_view_column_set_resizable(column,TRUE);
    gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_FIXED);
    gtk_tree_view_column_set_fixed_width(column,40);
    g_object_set_data(G_OBJECT(column),"idcoln",GINT_TO_POINTER(0));
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );     
                     

    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new();
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column ), renderer, TRUE );
    gtk_tree_view_column_set_attributes( column, renderer,
                                         "text", COL_DVA, NULL );
    gtk_tree_view_column_set_title(column,"Slovenský");
    gtk_tree_view_column_set_resizable(column,TRUE);
    g_object_set_data(G_OBJECT(column),"idcoln",GINT_TO_POINTER(1));
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );


    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new();
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column ), renderer, TRUE );
    gtk_tree_view_column_set_attributes( column, renderer,
                                         "text", COL_TEXT, NULL );
    gtk_tree_view_column_set_title(column,"Español");
    gtk_tree_view_column_set_resizable(column,TRUE);
    g_object_set_data(G_OBJECT(column),"idcoln",GINT_TO_POINTER(2));
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );
   
   
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new();
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column ), renderer, TRUE );
    gtk_tree_view_column_set_attributes( column, renderer,
                                         "text", COL_INT, NULL );
    gtk_tree_view_column_set_title(column,"Integer");
    gtk_tree_view_column_set_resizable(column,TRUE);
    g_object_set_data(G_OBJECT(column),"idcoln",GINT_TO_POINTER(3));
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );

   
    /* Create display part of tree view - column 2 */
    renderer = gtk_cell_renderer_pixbuf_new();
    column = gtk_tree_view_column_new();
    gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( column ), renderer, TRUE );
    gtk_tree_view_column_set_attributes( column, renderer,
                                         "pixbuf", COL_IMG, NULL );
    g_object_set_data(G_OBJECT(column),"idcoln",GINT_TO_POINTER(4));                                   
    gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );
   
   
    g_signal_connect(G_OBJECT(treeview),"button-press-event",G_CALLBACK(treeclic),NULL);
   
   
   
    gtk_widget_show_all( window );
   
    gtk_main();
   
    return( 0 );
}
 


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group