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 Sep 19, 2014 7:53 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: gtk_tool_button_set_icon_widget problem
PostPosted: Tue Oct 16, 2012 9:13 pm 
Offline
Familiar Face

Joined: Thu Aug 23, 2012 4:50 am
Posts: 11
Hi,

I've set up a small toolbar with 3 icons, and each time I click on one of those icons i'd like it to display the 'active-up' image and for the other two icons to be updated and display the 'inactive-down' icons.

The code I have below works if I click on an icon in the first instance, but then if I click on another icon, it is set to 'active' but the other icons aren't set to 'inactive' and I get this error message:

Quote:
Gtk-CRITICAL **: gtk_tool_button_set_icon_widget: asserttion 'icon_widget == NULL || GTK_IS_WIDGET (icon_widget)' failed.


Can anyone see how I can correct this? I'm using GTK+ 2.24 on a windows PC.

[Edit] I've added a zip file with the .cpp file and the icons.

Thanks
6

My code:

Code:
#include <gtk/gtk.h>

  GtkWidget *window;
  GtkWidget *vbox;
 
  GtkWidget *toolbar;
  GtkWidget *translateIconUp, *translateIconDown, *rotateIconUp, *rotateIconDown, *scaleIconUp, *scaleIconDown;
  GtkToolItem *translate, *rotate, *scale;

//Functions
gboolean set_manipulator_translate(GtkWidget *widget, gpointer   data)
{
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(translate), translateIconDown);  //Set as Active
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(rotate), rotateIconUp);   //Set as inactive
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(scale), scaleIconUp);   //Set as inactive

    gtk_widget_show_all(GTK_WIDGET(toolbar));
             
    return false;
}
 
gboolean set_manipulator_rotate(GtkWidget *widget, gpointer   data)
{
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(translate), translateIconUp);  //Set as inactive
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(rotate), rotateIconDown);   //Set as Active
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(scale), scaleIconUp);  //Set as inactive
    gtk_widget_show_all(GTK_WIDGET(toolbar));
   
    return false;   
}

gboolean set_manipulator_scale(GtkWidget *widget, gpointer   data)
{
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(translate), translateIconUp);  //Set as inactive
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(rotate), rotateIconUp);  //Set as inactive
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(scale), scaleIconDown);   //Set as Active
    gtk_widget_show_all(GTK_WIDGET(toolbar));
       
    return false;
}
   
//Main
int main( int argc, char *argv[])
{
  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 250, 200);
  gtk_window_set_title(GTK_WINDOW(window), "toolbar");

  vbox = gtk_vbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(window), vbox);

  toolbar = gtk_toolbar_new();
  gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);

  gtk_container_set_border_width(GTK_CONTAINER(toolbar), 2);

  // Add three icons
  translateIconDown = gtk_image_new_from_file( "translate_iconx.png" );
  translateIconUp = gtk_image_new_from_file( "translate_icon2x.png" );
  translate = gtk_tool_button_new(translateIconDown, "translate");
  gtk_toolbar_insert(GTK_TOOLBAR(toolbar), translate, -1);
  g_signal_connect(G_OBJECT(translate), "clicked", G_CALLBACK(set_manipulator_translate), NULL);

  rotateIconDown = gtk_image_new_from_file( "rotate_iconx.png" );
  rotateIconUp = gtk_image_new_from_file( "rotate_icon2x.png" );
  rotate = gtk_tool_button_new(rotateIconUp, "rotate");
  gtk_toolbar_insert(GTK_TOOLBAR(toolbar), rotate, -1);
  g_signal_connect(G_OBJECT(rotate), "clicked", G_CALLBACK(set_manipulator_rotate), NULL);
 
  scaleIconDown = gtk_image_new_from_file( "scale_iconx.png" );
  scaleIconUp = gtk_image_new_from_file( "scale_icon2x.png" );
  scale = gtk_tool_button_new(scaleIconUp, "scale");
  gtk_toolbar_insert(GTK_TOOLBAR(toolbar), scale, -1);
  g_signal_connect(G_OBJECT(scale), "clicked", G_CALLBACK(set_manipulator_scale), NULL);
 
  //Package
  gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 5);

  g_signal_connect_swapped(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit), NULL);

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


Attachments:
File comment: Icons and code
IconTest.zip [12.65 KiB]
Downloaded 61 times
Top
 Profile  
 
 Post subject: Re: gtk_tool_button_set_icon_widget problem
PostPosted: Wed Oct 17, 2012 5:34 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
hmmm. Might have something to do with showing of widgets or memory management.
Workaround for the former would be to set up size toolbar buttons and only show 3 at one time (keep track of the previous state and then it is only a single widget hide and widget show when you want to change the state).
Don't see anything about memory management from a look through of the documentation.
I know GtkToolButton has a bug in its code re: requiring GTK_IS_MISC in gtk2 but that should be fine with your code.


Top
 Profile  
 
 Post subject: Re: gtk_tool_button_set_icon_widget problem
PostPosted: Wed Oct 17, 2012 6:33 am 
Offline
Familiar Face

Joined: Thu Aug 23, 2012 4:50 am
Posts: 11
Hi,

Yeah, I was reading up on floating references and thought I would try nailing down the images with g_object_ref_sink(). This turned out to the reason for the errors and it all works fine now.

Working code is below:

Code:
#include <gtk/gtk.h>

  GtkWidget *window;
  GtkWidget *vbox;
 
  GtkWidget *toolbar;
  GtkWidget *translateIconUp, *translateIconDown, *rotateIconUp, *rotateIconDown, *scaleIconUp, *scaleIconDown;
  GtkToolItem *translate, *rotate, *scale;

//Functions
gboolean set_manipulator_translate(GtkWidget *widget, gpointer   data)
{
    //Set translate to Active
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(translate), translateIconUp);
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(rotate), rotateIconDown);
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(scale), scaleIconDown);
    gtk_widget_show_all(GTK_WIDGET(toolbar));
             
    return false;
}
 
gboolean set_manipulator_rotate(GtkWidget *widget, gpointer   data)
{
    //Set Rotate to Active
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(translate), translateIconDown);
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(rotate), rotateIconUp);
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(scale), scaleIconDown);
    gtk_widget_show_all(GTK_WIDGET(toolbar));
   
    return false;   
}

gboolean set_manipulator_scale(GtkWidget *widget, gpointer   data)
{
    //Set Scale to active
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(translate), translateIconDown);
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(rotate), rotateIconDown);
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(scale), scaleIconUp);
    gtk_widget_show_all(GTK_WIDGET(toolbar));
       
    return false;
}

   
//Main
int main( int argc, char *argv[])
{


  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 250, 200);
  gtk_window_set_title(GTK_WINDOW(window), "toolbar");

  vbox = gtk_vbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(window), vbox);


  toolbar = gtk_toolbar_new();
  gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);

  gtk_container_set_border_width(GTK_CONTAINER(toolbar), 2);

  // load icons and make them permanent
  translateIconDown = gtk_image_new_from_file( "translate_iconx.png" );
  translateIconUp = gtk_image_new_from_file( "translate_icon2x.png" );
  rotateIconDown = gtk_image_new_from_file( "rotate_iconx.png" );
  rotateIconUp = gtk_image_new_from_file( "rotate_icon2x.png" );
  scaleIconDown = gtk_image_new_from_file( "scale_iconx.png" );
  scaleIconUp = gtk_image_new_from_file( "scale_icon2x.png" );
  g_object_ref_sink(G_OBJECT(scaleIconDown));
  g_object_ref_sink(G_OBJECT(scaleIconUp));
  g_object_ref_sink(G_OBJECT(rotateIconDown));
  g_object_ref_sink(G_OBJECT(rotateIconUp));
  g_object_ref_sink(G_OBJECT(translateIconDown));
  g_object_ref_sink(G_OBJECT(translateIconUp));
 
  //Seperate
  GtkToolItem *sep= gtk_separator_tool_item_new();
  gtk_toolbar_insert(GTK_TOOLBAR(toolbar), sep, -1);
 
  //Make 3 buttons for the manipulation widgets
  translate = gtk_tool_button_new(translateIconUp, "translate");
  gtk_toolbar_insert(GTK_TOOLBAR(toolbar), translate, -1);
  g_signal_connect(G_OBJECT(translate), "clicked", G_CALLBACK(set_manipulator_translate), NULL);

  rotate = gtk_tool_button_new(rotateIconDown, "rotate");
  gtk_toolbar_insert(GTK_TOOLBAR(toolbar), rotate, -1);
  g_signal_connect(G_OBJECT(rotate), "clicked", G_CALLBACK(set_manipulator_rotate), NULL);
 
  scale = gtk_tool_button_new(scaleIconDown, "scale");
  gtk_toolbar_insert(GTK_TOOLBAR(toolbar), scale, -1);
  g_signal_connect(G_OBJECT(scale), "clicked", G_CALLBACK(set_manipulator_scale), NULL);
 
  //Package
  gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 5);

  g_signal_connect_swapped(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show_all(window);
   
  gtk_main();

  return 0;
}



Top
 Profile  
 
 Post subject: Re: gtk_tool_button_set_icon_widget problem
PostPosted: Thu Oct 18, 2012 12:35 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Looks good but I have a feeling that the memory of the sunken references won't be freed after gtk_main is quit. This can be tidied up with g_object_unref after gtk_main. If I'm wrong you should get errors in the console (something like `assertion object!=NULL failed'). Another alternative is to bracket the set_icon_widget calls with g_object_ref and g_object_unref but your way should provide better performance.


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 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