GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Jul 30, 2014 9:06 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Acess stock items like "accesories", "audiovideo", etc.
PostPosted: Mon Dec 26, 2011 4:55 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
I'm building a personal app to create deaktopfiles. The thing left to do is to add in my combo box the images of each categories, which I have:
Code:
const gchar *categories[] =
{
   "AudioVideo", "Audio", "Video", "Development",
   "Education", "Game", "Graphics", "Network",
   "Office", "Settings", "System", "Utility"
};

Question: How can I access those icons? They are the same icons that have in menus like gnome-menu, lxpanel, etc.

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


Top
 Profile  
 
 Post subject: Re: Acess stock items like "accesories", "audiovideo", etc.
PostPosted: Mon Dec 26, 2011 6:34 pm 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
Usually you can get an "named" icon by using gtk_image_new_from_icon_name (http://developer.gimp.org/api/2.0/gtk/G ... -icon-name). But since GtkComboBox implements GtkCellLayout you can program something like this:

Code:

int main(int argc, char **argv)
{
    gtk_init(&argc,&argv);
    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);

    GtkListStore *store=gtk_list_store_new(2,G_TYPE_STRING,G_TYPE_STRING);

    struct
    {
        char *icon_name;
        char *label;
    }
    labels[]=
    {
        {"video","Video"},
        {"sound","Sound"},
        {"applications-multimedia","AudioVideo"},
        {"applications-office","Office"},
    };

    int i;
    for (i=0; i<G_N_ELEMENTS(labels); i++)
    {
        GtkTreeIter iter;
        gtk_list_store_append(store,&iter);
        gtk_list_store_set(store,&iter,
                           0,labels[i].icon_name,1,labels[i].label,-1);
    }

    GtkWidget *combo=gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
    gtk_container_add(GTK_CONTAINER(window),combo);

    GtkCellRenderer *renderer_icon=gtk_cell_renderer_pixbuf_new();
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo),renderer_icon,FALSE);
    gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(combo),renderer_icon,
                                  "icon-name",0);

    GtkCellRenderer *renderer_text=gtk_cell_renderer_text_new();
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo),renderer_text,TRUE);
    gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(combo),renderer_text,
                                  "text",1);

    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}


Top
 Profile  
 
 Post subject: Re: Acess stock items like "accesories", "audiovideo", etc.
PostPosted: Mon Dec 26, 2011 11:50 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
Thanks, but a get errors like:
Quote:
GLib-GObject-WARNING **: unable to set property `pixbuf' of type `GdkPixbuf' from value of type `gchararray'

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


Top
 Profile  
 
 Post subject: Re: Acess stock items like "accesories", "audiovideo", etc.
PostPosted: Tue Dec 27, 2011 4:44 am 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
Works here (if you include the first line "#include <gtk/gtk.h>" (which i have forgot) and copy and paste it in an empty g.c.
Ubuntu 11.04, gtk+-2.0. Compile with the usual "gcc g.c `pkg-config --cflags --libs gtk+-2.0` -o g"


Code:
#include <gtk/gtk.h>

int main(int argc, char **argv)
{
    gtk_init(&argc,&argv);
    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);

    GtkListStore *store=gtk_list_store_new(2,G_TYPE_STRING,G_TYPE_STRING);

    struct
    {
        char *icon_name;
        char *label;
    }
    labels[]=
    {
        {"video","Video"},
        {"sound","Sound"},
        {"applications-multimedia","AudioVideo"},
        {"applications-office","Office"},
    };

    int i;
    for (i=0; i<G_N_ELEMENTS(labels); i++)
    {
        GtkTreeIter iter;
        gtk_list_store_append(store,&iter);
        gtk_list_store_set(store,&iter,
                           0,labels[i].icon_name,1,labels[i].label,-1);
    }

    GtkWidget *combo=gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
    gtk_container_add(GTK_CONTAINER(window),combo);

    GtkCellRenderer *renderer_icon=gtk_cell_renderer_pixbuf_new();
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo),renderer_icon,FALSE);
    gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(combo),renderer_icon,
                                  "icon-name",0);

    GtkCellRenderer *renderer_text=gtk_cell_renderer_text_new();
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo),renderer_text,TRUE);
    gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(combo),renderer_text,
                                  "text",1);

    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}


Top
 Profile  
 
 Post subject: Re: Acess stock items like "accesories", "audiovideo", etc.
PostPosted: Wed Dec 28, 2011 12:20 am 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
Thanks I found from a site the API named gtk_icon_theme_load_icon, more or less my final code:
Code:
const gchar *categories[][2] =
{
   {"applications-multimedia", "AudioVideo"},
   {"applications-multimedia", "Audio"},
   {"applications-multimedia", "Video"},
   {"applications-development", "Development"},
   {"applications-science", "Education"},
   {"applications-games", "Game"},
   {"applications-graphics", "Graphics"},
   {"applications-internet", "Network"},
   {"applications-office", "Office"},
   {"applications-accessories", "Settings"},
   {"applications-system", "System"},
   {"applications-utilities", "Utility"}
};

model = gtk_list_store_new (2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
   n_items = G_N_ELEMENTS (categories);
   icon_theme = gtk_icon_theme_get_default ();
   for (pos = 0; pos != n_items; pos++) {
      GdkPixbuf *pixbuf;
      GtkTreeIter iter;
      gtk_list_store_append (model, &iter);
      pixbuf = gtk_icon_theme_load_icon (icon_theme, categories[pos][0], 32, 0, NULL);
      gtk_list_store_set (model, &iter, 0, pixbuf, 1, categories[pos][1], -1);
      g_object_unref (pixbuf);
   }
   ptr->combo1 = gtk_combo_box_new_with_model (GTK_TREE_MODEL (model));
   render_pixbuf = gtk_cell_renderer_pixbuf_new ();
   render_text = gtk_cell_renderer_text_new ();
   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (ptr->combo1), render_pixbuf, FALSE);
   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (ptr->combo1), render_pixbuf, "pixbuf", 0);
   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (ptr->combo1), render_text, FALSE);
   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (ptr->combo1), render_text, "text", 1);
   gtk_combo_box_set_active (GTK_COMBO_BOX (ptr->combo1), 0);
   g_object_unref (model);

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


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:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group