GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 02, 2014 9:10 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Create multiple toggle button in loop& handle it.
PostPosted: Tue Nov 27, 2012 9:35 am 
Offline
Familiar Face

Joined: Wed Sep 19, 2012 9:08 am
Posts: 10
Hi Sir,

I want to build a panel. There are many buttons (User design) in it and then capture how many button be pressed (besides the amount of button be pressed also have to get the widget ).

I don't know how to create a button by loop and how to listen for the signal.

The following code is from Gtk.org.
Code:
//create button
GtkWidget *gtk_toggle_button_new( void );

//capture and handle signal
void toggle_button_callback (GtkWidget *widget, gpointer data)
{
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
    {
        /* If control reaches here, the toggle button is down */
   
    } else {
   
        /* If control reaches here, the toggle button is up */
    }
}

Thanks a lot.
Jay.


Top
 Profile  
 
 Post subject: Re: Create multiple toggle button in loop& handle it.
PostPosted: Tue Nov 27, 2012 10:19 am 
Offline
Familiar Face

Joined: Wed Sep 19, 2012 9:08 am
Posts: 10
I tried to declare data type through a loop, but get some error message :

Code:
for(i=0;i<5;i++){
      GtkWidget *button[i]= gtk_check_button_new();
   }


Top
 Profile  
 
 Post subject: Re: Create multiple toggle button in loop& handle it.
PostPosted: Tue Nov 27, 2012 10:09 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
I can't really tell what you want from what you have posted. Also saying you get an error is not of much help if you don't say what the error is. I think your problem is that you are declaring an element of an array one at a time. Arrays of this are fixed length so not appropriate if your user is meant to be able to set an arbitrary number of buttons. In this case you should use dynamic memory to have expandable arrays (e.g. GLibs GArray - have a look at http://developer.gnome.org/glib/2.31/glib-data-types.html to see the different data structures available to you). If you really only want a fixed length of 5 widgets then you should declare the array outside of the loop and specify the length: [5].
If your not sure about signal connection then you really need to have a look through the tutorials listed on the gtk website http://www.gtk.org/documentation.php. The zetcode ones (http://zetcode.com/tutorials/gtktutorial/) are quite good. The documentation of specific widgets (e.g. http://developer.gnome.org/gtk/2.24/GtkCheckButton.html) will help you get a more advanced understanding once the basics are in place.
Signal connection works one widget at a time and it is a rare situation in which your user will be pressing buttons faster than you can process them, so you won't really have to worry about the "how many" aspect of your question.
By the way in the future could you also list what gtk version you are using as there are differences in approaches needed for each (the links I have given are for version 2.24)


Top
 Profile  
 
 Post subject: Re: Create multiple toggle button in loop& handle it.
PostPosted: Wed Nov 28, 2012 2:24 am 
Offline
Familiar Face

Joined: Wed Sep 19, 2012 9:08 am
Posts: 10
Hi Paul,

Thanks for your help again and sorry for my ambiguous question.

Actually I will design a panel that user can input the size and program will create button relatively(3*3,4*4,5*5 matrix)

Does posible create the button by loop so that I don't new widget step by step.
Also I can't know how many button be created before user input.

Finally, I have to capture which one and how many button be pressed.

Error message: variable-sized object may not be initialized
Code:
void mbtn()
{
   int i;   

   for(i=0;i<5;i++)
   {
      GtkWidget *button[i]= gtk_check_button_new();
   }
}

GTK version: 2.24

Thanks.
Jay.


Top
 Profile  
 
 Post subject: Re: Create multiple toggle button in loop& handle it.
PostPosted: Wed Nov 28, 2012 3:56 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
ok so lets say you have a square jxj matrix.
yes you would create these in a loop but you declare them outside the loop. Depending on the scope of what you are doing, you might not have to a declare a full array, e.g.:
Code:
gboolean toggle_cb(GtkToggleButton* widget, gpointer data)
{
  int k;
  k=GPOINTER_TO_INT(data);
  ...
}
  ...
  GtkWidget* button;
  for (i=0;i<j;i++)
  {
    for (k=0;k<j;k++)
    {
      button=gtk_check_button_new();
      g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(toggle_cb), GINT_TO_POINTER((i*j)+k));
      ..
    }
  }
  ...

The signal connect passes both the widget itself as well as another parameter (I made use of the array indices in this case).


Top
 Profile  
 
 Post subject: Re: Create multiple toggle button in loop& handle it.
PostPosted: Wed Nov 28, 2012 10:10 am 
Offline
Familiar Face

Joined: Wed Sep 19, 2012 9:08 am
Posts: 10
Hi Paul,

The problem is resolved.my concept is a little unclear.

Thank you for your patient help.That is of great benefit to me :P

Jay.


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