GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Aug 01, 2014 11:53 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Window Icon from GIcon
PostPosted: Sat Dec 19, 2009 3:23 am 
Offline
Familiar Face

Joined: Fri Dec 04, 2009 3:56 am
Posts: 10
I wish to set the icon of a window to show the themed icon of some file. I get this icon as a GIcon like so:
Code:
file_.query_info(gio.FILE_ATTRIBUTE_STANDARD_ICON).get_icon()


However, there is no obvious way to set a window icon from this. gtk.Window.set_icon() takes a gtk.gdk.Pixbuf; is there a way to get a Pixbuf given a GIcon?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 19, 2009 11:09 am 
Offline
Never Seen the Sunlight

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

How do you create your GFile? Solution to this problems depends on this.

Tadej


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 19, 2009 11:51 am 
Offline
Familiar Face

Joined: Fri Dec 04, 2009 3:56 am
Posts: 10
The GFile is created from a user-given filename, like so:
Code:
file_ = gio.File(filename)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 19, 2009 10:46 pm 
Offline
Never Seen the Sunlight

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

Why don't you just set window's icon directly from filename? Creating pixbuf from GFile will involve getting full path to file that GFile represents and then loading it into pixbuf, but this seems unreasonable thing to do since GtkWindow can do that for you using simple path.

Tadej


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 20, 2009 3:16 am 
Offline
Familiar Face

Joined: Fri Dec 04, 2009 3:56 am
Posts: 10
I think you misunderstand what I am asking. The file isn't necessarily an image file (and usually won't be). I want to use the file's icon, as it would be seen in a file manager. At the moment, this always appears to be a gio.ThemedIcon, so it would be possible to assume that this is always the case, and so use gtk.IconTheme.load_icon()
But I'm wondering whether this assumption is reasonable? It isn't guaranteed by the documentation, at any rate.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 20, 2009 10:56 am 
Offline
Never Seen the Sunlight

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

I did misunderstood you completely. I though GFile represents icon, not arbitrary file.

There is no guarantee that GIcon returned by get_icon() method will be GThemedIcon. This will be true most of the time, since file type icons are usually obtained from icon theme, but I would avoid doing any hardcoded assumption and allow icon loading function to work with GFileIcon types too.

I cannot provide python sample code since I'm not too familiar with python, but in C, icon loading function would look something like this:
Code:
/**
* load_icon:
* @filename: path to file from which to obtain mime type icon
*
* This function takes path to file and returns pixbuf with mime type icon of
* this file. If mime type icon cannot be loaded, function returns NULL.
*
* Return: #GdkPixbuf on success, NULL on failure. Returned #GdkPixbuf should be
* freed using g_object_unref() when not needed anymore.
*/
GdkPixbuf *
load_icon( const gchar *filename )
{
   GFile     *file;
   GFileInfo *info;
   GIcon     *icon;
   GdkPixbuf *pix = NULL;

   file = g_file_new_for_path( filename );
   info = g_file_query_info( file, G_FILE_ATTRIBUTE_STANDARD_ICON,
                        G_FILE_QUERY_INFO_NONE, NULL, NULL );
   icon = g_file_info_get_icon( info );

   if( G_IS_THEMED_ICON( icon ) )
   {
      gchar const * const *names;

      names = g_themed_icon_get_names( G_THEMED_ICON( icon ) );
      pix = gtk_icon_theme_load_icon(
            gtk_icon_theme_get_default(), *names, 16, 0, NULL );
   }
   else if( G_IS_FILE_ICON( icon ) )
   {
      GFile *icon_file;
      gchar *path;

      icon_file = g_file_icon_get_file( G_FILE_ICON( icon ) );
      path = g_file_get_path( icon_file );
      pix = gdk_pixbuf_new_from_file_at_size( path, 16, 16, NULL );
      g_free( path );
      g_object_unref( G_OBJECT( icon_file ) );
   }
   g_object_unref( G_OBJECT( icon ) );
   g_object_unref( G_OBJECT( info ) );
   g_object_unref( G_OBJECT( file ) );

   return( pix );
}

Tadej


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 20, 2009 1:08 pm 
Offline
Familiar Face

Joined: Fri Dec 04, 2009 3:56 am
Posts: 10
Thank you tadeboro, for that code!
For reference, below is the directly equivalent (apart from one differing function call) python code.

Code:
def load_icon(filename):
   """Find the icon of the given filename as as
      gtk.gdk.Pixbuf. return None if the icon can't
      be found

   """
   file_ = gio.File(filename)
   icon = file_.query_info("standard::icon").get_icon()
   if isinstance(icon, gio.ThemedIcon):
      theme = gtk.icon_theme_get_default()
      return theme.choose_icon(icon.get_names(), 16, 0).load_icon()
   elif isinstance(icon, gio.FileIcon):
      iconpath = icon.get_file().get_path()
      return gtk.gdk.pixbuf_new_from_file(iconpath)
   return


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] 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:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group