Re: Proper way to show unique icon in treeview <--with code



Tony,

I don't claim to be an expert but I have used IconViews & TreeViews.  I
have never seen it attempted the way you show.  Using GDK_TYPE_PIXBUF as
the column type is all I have every tried, based on code I've seen from
others, and it has always worked.

Here are a few observation of your code.
1. I have been successful using the following creation order.
    A. Load all icons I plan to use into an array of *pixbuf or some
type of permanent(life of the app) container.
    B. Create the list store
    model = GTK_TREE_MODEL (gtk_list_store_new (2,
                                              GDK_TYPE_PIXBUF,  /* ICON
*/
                                              G_TYPE_STRING     /*
Location Name  */
                                            ));
    C. Create the iconview (or treeview) & show it, plan on adding it to
a scrolled_window.
      iconview = gtk_icon_view_new_with_model (GTK_TREE_MODEL (model));
      gtk_icon_view_set_orientation (GTK_ICON_VIEW (iconview),
GTK_ORIENTATION_VERTICAL);
      gtk_icon_view_set_columns (GTK_ICON_VIEW (iconview), -1);
      gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (iconview),
GTK_SELECTION_SINGLE);
      gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (iconview),
ID_SEL_ICON);
      gtk_icon_view_set_markup_column (GTK_ICON_VIEW (iconview),
ID_SEL_LABEL);

    D. Load each record to the list store with a pointer to pixmap, and
text string
      gtk_list_store_prepend (GTK_LIST_STORE (model), &iter);
      gtk_list_store_set (GTK_LIST_STORE (model), &iter,
                      ID_SEL_ICON, pixbuf, ID_SEL_LABEL, "Home", -1);

Iconviews don't need all the renderer fuss and may work well for you.
gfhcm.sourceforge.net contains an application i wrote that uses both
icon views and tree views, look at the gfhcm-client or gfhcmc codeset.  

You might also find answers for the GTK API Docs or the TreeView
tutorial.
http://developer.gnome.org/doc/API/2.0/gtk/TreeWidget.html
http://developer.gnome.org/doc/API/2.0/gtk/GtkIconView.html
http://scentric.net/tutorial/treeview-tutorial.html

Hope this helps,

James,

On Sun, 2006-12-17 at 23:06 -0500, Tony Freeman wrote:

Hello,

I need help understanding how one would create a treeview so that the
first column is an icon and the second column is text.  The icon and
text represent the type of machine the user can choose (linux
workstation, linux server, hp).  I want to have a different icon for
each machine; however, I notice that whatever icon is called last in the
code that I have written, is the icon that is shown for ALL the rows.  

Please help!  What am I doing wrong?

I forgot to attach the code!

Here it is:


void build_server_list (GtkWidget *treeview)
{
      GtkListStore *liststore;
      GtkTreeIter iter;
      GtkCellRenderer *text_renderer;
      GtkCellRenderer *icon_renderer;
      gint i = 0;
      gint count = 0;
      

      icon_renderer = gtk_cell_renderer_pixbuf_new();
      gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(treeview),
                                               0,      
                                               " ",  
                                               icon_renderer,
                                               NULL);
      
      text_renderer = gtk_cell_renderer_text_new();
      gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(treeview),
                                               1,      
                                               " ",  
                                               text_renderer,
                                               NULL);
      
      liststore = gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
      
      /* workstations */
      count = g_strv_length(workstations);
      g_object_set(icon_renderer, "stock-id", "gtk-close", NULL);
      for (i=0; i<count; i++) {
              gtk_list_store_append(liststore, &iter);
              gtk_list_store_set(liststore, &iter,
                      0, icon_renderer,
                      1, workstations[i], -1);
      }
      
      /* linux servers */
      count = g_strv_length(servers_linux);
      g_object_set(icon_renderer, "stock-id", "gtk-save", NULL);
      for (i=0; i<count; i++) {
              gtk_list_store_append(liststore, &iter);
              gtk_list_store_set(liststore, &iter,
                      0, icon_renderer,
                      1, servers_linux[i], -1);
      }
      
      
      /* hp servers */
      count = g_strv_length(servers_hp);
      g_object_set(icon_renderer, "stock-id", "gtk-open", NULL);
      for (i=0; i<count; i++) {
              gtk_list_store_append(liststore, &iter);
              gtk_list_store_set(liststore, &iter,
                      0, icon_renderer,
                      1, servers_hp[i], -1);
      }
      
      gtk_tree_view_set_model(GTK_TREE_VIEW(treeview),
GTK_TREE_MODEL(liststore));
}


_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



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