Toggle button as radio button solution
- From: Michal Tomcanyi <michal tomcanyi matfyz cz>
- To: gtk-app-devel-list gnome org
- Subject: Toggle button as radio button solution
- Date: Mon, 4 Aug 2003 13:56:01 +0200
Hello,
yesterday I've been searching archive for a code where toggle button
acts like a radio button. I found nothing out of use and the discussion
in mail list ended up - author used radio buttons instead. I am writing
chess client, and want to use toggle button as a chessboard square.
Selected square (movement start) will be pressed until another one
(movement destination) is pressed. Afterwards both buttons will raise.
This code should provide desired effect and I think it can be rewritten
for case "pressing one button will cause all other to raise".
If somebody has a better solution, please let me know. An inspiration
could be "tictactoe" example, where buttons are raised while callbacks
are disconnected by g_signal_handler_diconnect_by_func. I tried using
it, however some errors were returned in run-time so I gave up.
(Documentation says very little about it and I am not so skilled)
Maybe somebody find it useful.
Michal
------ Code start -----
#include <gtk/gtk.h>
void button_toggled(GtkToggleButton *bttn, gpointer data) {
static GtkToggleButton *receiver=NULL;
static GtkToggleButton *sender=NULL;
g_print("Button %d toggled\n",GPOINTER_TO_INT(data));
/* no button clicked before - user clicked for the first time */
if (!receiver) {
receiver = bttn;
return;
}
/* one button has been toggled - user clicked on another one */
if (receiver && receiver != bttn &&
gtk_toggle_button_get_active(receiver)) {
sender = bttn;
gtk_toggle_button_set_active(receiver,FALSE);
return;
}
/* Serves for two possibilities:
* 1, double click on the same button (push, raise)
* 2, another button ("sender") sent signal to "receiver"
* which means, receiver should be raised */
if (receiver == bttn) {
if (sender) gtk_toggle_button_set_active(sender,FALSE);
receiver=NULL;
return;
}
/* "receiver" sends signal back to "sender", so that "sender" will
* raise again */
if (sender == bttn) {
sender = NULL;
receiver = NULL;
/* when the function exits, standard handler will raise
* "sender" */
}
}
int main(int argc, char *argv[]) {
GtkWidget *win, *hbox, *bttn1, *bttn2;
gtk_init(&argc, &argv);
win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(win,200,100);
g_signal_connect(G_OBJECT(win),"destroy",G_CALLBACK(gtk_main_quit),
NULL);
bttn1=gtk_toggle_button_new_with_label("Button1");
bttn2=gtk_toggle_button_new_with_label("Button2");
g_signal_connect(G_OBJECT(bttn1),"toggled",G_CALLBACK(button_toggled),
GINT_TO_POINTER(1));
g_signal_connect(G_OBJECT(bttn2),"toggled",G_CALLBACK(button_toggled),
GINT_TO_POINTER(2));
hbox = gtk_hbox_new(TRUE,2);
gtk_box_pack_start(GTK_BOX(hbox),bttn1,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(hbox),bttn2,TRUE,TRUE,0);
gtk_container_add(GTK_CONTAINER(win),hbox);
gtk_widget_show_all(win);
gtk_main();
return(0);
}
---------- Code end ----------
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]