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 Sep 17, 2014 5:34 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: button remains clicked
PostPosted: Fri Jul 04, 2008 5:40 pm 
Offline
Familiar Face

Joined: Sun May 06, 2007 12:12 pm
Posts: 14
Hi all,

I have a application wish contains a lot of buttons, i want the clicked button to remains clicked until another button is clicked!

Help!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 7:46 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
NOTE: this is obsolete now, follow the instructions given in the next 2 posts
try using GtkToggleButton
http://library.gnome.org/devel/gtk/stab ... utton.html
then connect the 'toggled' signal to a function that toggles/untoggles the buttons,
something like this:
Code:
#include <gtk/gtk.h>

void toggled_cb(GtkToggleButton *but, gpointer user_data)
{
   static GtkToggleButton *old_toggled = NULL;

   if(old_toggled == NULL)
   {
      old_toggled = but;
      return;
   }

   g_signal_handlers_block_by_func(old_toggled, toggled_cb, user_data);
   gtk_toggle_button_set_active(old_toggled, !gtk_toggle_button_get_active(old_toggled));
   g_signal_handlers_unblock_by_func(old_toggled, toggled_cb, user_data);

   old_toggled = but;
}

int main(int argc, char **argv)
{
   GtkWidget *window;
   GtkWidget *table;
   GtkWidget *but;

   gint r, c;
   gchar s[16];

   gtk_init(&argc, &argv);

   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   g_signal_connect(G_OBJECT(window), "delete-event", G_CALLBACK(gtk_main_quit), NULL);
   gtk_container_set_border_width(GTK_CONTAINER(window), 10);

   table = gtk_table_new(5, 5, TRUE);
   gtk_container_add(GTK_CONTAINER(window), table);
   gtk_widget_show(table);

   for(r = 0; r < 5; r++)
   {
      for(c = 0; c < 5; c++)
      {
         sprintf(s, "Button %d", ((5*r)+c+1));
         but = gtk_toggle_button_new_with_label(s);
         g_signal_connect(G_OBJECT(but), "toggled", G_CALLBACK(toggled_cb), NULL);
         gtk_table_attach_defaults(GTK_TABLE(table), but, c, c+1, r, r+1);
         gtk_widget_show(but);
      }
   }

   gtk_widget_show(window);

   gtk_main();

   return 0;
}

because of the way that I wrote this, when you press a button twice in a row, it will stay depressed instead of popping back up
I hope that this helps...


Last edited by dreblen on Fri Jul 04, 2008 8:24 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 7:53 pm 
Offline
Familiar Face

Joined: Sat Jun 28, 2008 8:20 pm
Posts: 22
Location: Chicago, IL
I think what you want is to make all of those GtkButtons into GtkRadioButtons instead. Give them all the same "group" property, and set their "draw-indicator" properties to false to make them look like toggle buttons. Now when you click one, it stays "pressed in." Click a different one, and the last button pops back out while the new one stays depressed.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 8:13 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
wow! that's a ton simpler than my re-invention of the wheel,
thank you gedward, I never knew that that existed.

with this new knowledge, here's an updated example:
Code:
#include <gtk/gtk.h>

int main(int argc, char **argv)
{
   GtkWidget *window;
   GtkWidget *table;
   GtkWidget *but;

   gint r, c;
   gchar s[16];

   gtk_init(&argc, &argv);

   /* notice that but is initialized to NULL */
   but = NULL;

   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   g_signal_connect(G_OBJECT(window), "delete-event", G_CALLBACK(gtk_main_quit), NULL);
   gtk_container_set_border_width(GTK_CONTAINER(window), 10);

   table = gtk_table_new(5, 5, TRUE);
   gtk_container_add(GTK_CONTAINER(window), table);
   gtk_widget_show(table);

   for(r = 0; r < 5; r++)
   {
      for(c = 0; c < 5; c++)
      {
         sprintf(s, "Button %d", ((5*r)+c+1));
         /* since but is NULL the first time, it will create a new group */
         but = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(but), s);
         gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(but), FALSE);
         gtk_table_attach_defaults(GTK_TABLE(table), but, c, c+1, r, r+1);
         gtk_widget_show(but);
      }
   }

   gtk_widget_show(window);

   gtk_main();

   return 0;
}

It does exactly the same thing except that it starts with "Button 1" active


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 05, 2008 11:06 am 
Offline
Familiar Face

Joined: Sun May 06, 2007 12:12 pm
Posts: 14
That was perfect, another question i have is the fact that i have to emulate the a button to be drawn as clicked but without emitting signal on it ! is that possible!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 05, 2008 5:52 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
well, I don't know if you can block the signal outright, but you can block your callback from being called using g_signal_handlers_block_by_func:
http://library.gnome.org/devel/gobject/ ... ck-by-func
I did this in my first example


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 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