GTK+ Forums

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

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Problems changing GtkToolButton icon
PostPosted: Wed Mar 13, 2013 9:19 pm 
Offline
GTK+ Guru

Joined: Sun Jan 17, 2010 6:48 pm
Posts: 102
I'm working on creating a tool bar menu item that will allow me to select a colour from a pull-down menu. When a new colour is selected I then want to set a different icon in the toolbutton but this never seems to work. The follow snippet shows the functions and type of settings used.

Code:
...
GtkWidget *biggerImg = gtk_image_new_from_file ( "./bigger.png" );
bigger = gtk_tool_button_new  (biggerImg,"");
gtk_image_menu_item_set_image ( bigger, biggerImg );
gtk_image_menu_item_set_always_show_image ( bigger, TRUE );
...
....
GtkWidget *smallerImg = gtk_image_new_from_file ( "./smaller.png" );
gtk_image_menu_item_set_image ( bigger,smallerImg );


At first Everything works fine. The icon is reset with no problems but is never displayed! Although the button is empty it is still usable. There are no Gtk+ console messages and the app doesn't crash.

Has anyone encountered this problem and ever solved it? I'm linking against Gtk+2.22.

Will

_________________
"If you don't stand for something, then you'll fall for anything."


Top
 Profile  
 
 Post subject: Re: Problems changing GtkToolButton icon -WORKAROUND
PostPosted: Thu Mar 14, 2013 9:27 pm 
Offline
GTK+ Guru

Joined: Sun Jan 17, 2010 6:48 pm
Posts: 102
I guess that this setting of the icon is intended to be a 'one-off' operation. So, I've come up with this workaround. The code is straight out of my project sources but the required sequence of Gtk+ function calls can be easily recognised.


Code:
/**
\brief   Configure menubutton settings
\note   This cannot be done directly. The menubutton needs to be replaced with a new item with the desired icon.
**/
static void doCyanBg ( GtkToolButton *toolbutton, gpointer user_data )
{

   RichTextToolbarParams *para = ( RichTextToolbarParams * ) user_data;
   para->bgClr = "<span background='cyan'>";

   /* get the position in the toolbar and a copy of the menu pointer */
   gint idx = gtk_toolbar_get_item_index (para->toolBar, para->bg);
   GtkWidget *mnu = gtk_menu_tool_button_get_menu (para->bg);
   
   /* release the sub-menu, detroy the menubutton */
   gtk_menu_tool_button_set_menu (para->bg,NULL);
   gtk_widget_destroy (para->bg);

   /* create a new button, assign menu and insert into toolbar */
   para->bgImg = gtk_image_new_from_file ( "./cyanBg.png"  );
   para->bg =  gtk_menu_tool_button_new ( para->bgImg, "" );
   gtk_menu_tool_button_set_menu (para->bg,mnu);
   gtk_widget_show(para->bg);
   gtk_tool_button_set_icon_widget ( para->bg, para->bgImg );
   gtk_toolbar_insert (para->toolBar, para->bg, idx);

}

_________________
"If you don't stand for something, then you'll fall for anything."


Top
 Profile  
 
 Post subject: Re: Problems changing GtkToolButton icon
PostPosted: Thu Mar 14, 2013 9:46 pm 
Offline
GTK+ Guru

Joined: Sun Jan 17, 2010 6:48 pm
Posts: 102
Arrgh! I should have thought about this before! All that was require was to call gtk_widget_show on the new image widget. So, all the above can be replaced with something like the following.

Code:
/**
\brief
**/
static void doMagentaBg ( GtkToolButton *toolbutton, gpointer user_data )
{
   g_print ( "%s\n", __FUNCTION__ );

   RichTextToolbarParams *para = ( RichTextToolbarParams * ) user_data;
   para->bgClr = "<span background='magenta'>";

   para->bgImg = gtk_image_new_from_file ( "./magentaBg.png"  );
   gtk_widget_show(para->bgImg);
   gtk_tool_button_set_icon_widget ( para->bg, para->bgImg );
   

}

_________________
"If you don't stand for something, then you'll fall for anything."


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

All times are UTC


Who is online

Users browsing this forum: gabriellg, Google [Bot] and 3 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