Posted: Sat Jul 07, 2007 3:16 pm Post subject: Single button or array ?
Hello,
I'm building a C/GTK1.2 application that will control 15 devices. The window will contain 15 "Start" buttons, and for any device that is already running - the button should display "STOP" instead, and a label would show the time elapsed. I have 3 questions:
1) Do I need to use an array of 15 button widgets ? Or is there some way to do with only 1 widget ?
2) How do I make the buttons change labels from "start" to "stop" ?
3) Do you think this GUI arrangement would look good ? I'm placing the buttons/labels in a 3x5 table. If you can think of any alternative way to do this then please tell me.
I know that my questions may sound very trivial for some of you, but this is my first experience with any graphical toolkit what-so-ever.
Joined: 21 Sep 2005 Posts: 505 Location: Portland, OR USA
Posted: Sat Jul 07, 2007 6:29 pm Post subject:
You can initially create the GUI with glade or by using a loop (iterating 15 times) to create each button. So you don't need to create a 15-element array. The label text is changed via the button's "clicked" signal callback using the gtk_button_set_label() function. You could use something like g_ascii_strcasecmp() to see which label you need to set or possible store the states in static local variables or a global array.
Micah, Thank you for your reply. I used one button as you told me, and toggling the buttons between Start/Stop is OK.
But there's a small problem. I need the callback function to know which button got clicked, in order to take other action in the program. Here is my code:
Code: (Plaintext)
1 2 3 4 5 6 7 8 9
void click_callback( GtkWidget *button, gpointer data )
{
g_print ("Hello again - %s was pressed\n", (char *) data);
gtk_button_set_label(GTK_BUTTON(button), "STOP");
}
Code: (Plaintext)
1 2 3 4 5 6 7 8 9
for(z=1; z <= 15; z++)
{
sprintf(name,"%d",z);
button = gtk_button_new_with_label("Start");
gtk_signal_connect( GTK_OBJECT(button), "clicked",GTK_SIGNAL_FUNC (click_callback), (gpointer) name );
}
Now the problem is: I always get "15" as the output of g_print. I think it's a problem with the signals overwriting each other or something... Please advise.