單選按鈕與複選按鈕相似,只是單選按鈕是分組的,在一組中只有一個處於選中/按下狀態。這在你的應用程式中要從幾個選項中選一個的地方可以用到。
用這些呼叫之一來創建一個新的單選按鈕:
GtkWidget *gtk_radio_button_new( GSList *group ); GtkWidget *gtk_radio_button_new_from_widget( GtkRadioButton *group ); GtkWidget *gtk_radio_button_new_with_label( GSList *group, const gchar *label ); GtkWidget* gtk_radio_button_new_with_label_from_widget( GtkRadioButton *group, const gchar *label ); GtkWidget *gtk_radio_button_new_with_mnemonic( GSList *group, const gchar *label ); GtkWidget *gtk_radio_button_new_with_mnemonic_from_widget( GtkRadioButton *group, const gchar *label ); |
你可能注意到了,這些呼叫有個額外的參數。它們需要一個group才能正常運作。第一次呼叫 gtk_radio_button_new() 或 gtk_radio_button_new_with_label() 應該傳遞 NULL 值作為第一個參數。接著用如下函式創建一個group:
GSList *gtk_radio_button_get_group( GtkRadioButton *radio_button ); |
有一點很重要,必須為每個添加到group的新按鈕呼叫 gtk_radio_button_get_group(),並把前一個按鈕作為參數。返回的結果再傳給下一個呼叫 gtk_radio_button_new() 或 gtk_radio_button_new_with_label()。這樣才能建立一系列的按鈕。看一下下面的範例會更清楚一些。
你可以使用下面的語法來稍微縮短上面的步驟,它可以少用一個變數並且保留按鈕列表:
button2 = gtk_radio_button_new_with_label( gtk_radio_button_get_group (GTK_RADIO_BUTTON (button1)), "button2"); |
而 _from_widget() 建構函式可以讓你更簡潔,它完全省略了 gtk_radio_button_get_group() 呼叫。在下面範例的第三個按鈕就是用這種方法建構的。
button2 = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON (button1), "button2"); |
明確地指定哪個按鈕應該被預設按下也是個好主意,用:
void gtk_toggle_button_set_active( GtkToggleButton *toggle_button, gboolean state ); |
這在雙態按鈕那節談過,在這裡它也確切地以同樣的方式工作。多個單選按鈕組合到一起後,群組中一次只能有一個是作用中的。如果使用者點擊一個單選按鈕,接著點另一個,第一個單選按鈕會先發出 “toggled” 信號 (以報告變得不作用了),然後第二個也會發出 “toggled” 信號 (以報告變成作用中了)。
下面的範例創建一個含三個按鈕的單選按鈕組。
#include <glib.h> #include <gtk/gtk.h> static gboolean close_application( GtkWidget *widget, GdkEvent *event, gpointer data ) { gtk_main_quit (); return FALSE; } int main( int argc, char *argv[] ) { GtkWidget *window = NULL; GtkWidget *box1; GtkWidget *box2; GtkWidget *button; GtkWidget *separator; GSList *group; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); g_signal_connect (window, "delete-event", G_CALLBACK (close_application), NULL); gtk_window_set_title (GTK_WINDOW (window), "radio buttons"); gtk_container_set_border_width (GTK_CONTAINER (window), 0); box1 = gtk_vbox_new (FALSE, 0); gtk_container_add (GTK_CONTAINER (window), box1); gtk_widget_show (box1); box2 = gtk_vbox_new (FALSE, 10); gtk_container_set_border_width (GTK_CONTAINER (box2), 10); gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0); gtk_widget_show (box2); button = gtk_radio_button_new_with_label (NULL, "button1"); gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0); gtk_widget_show (button); group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (button)); button = gtk_radio_button_new_with_label (group, "button2"); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE); gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0); gtk_widget_show (button); button = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (button), "button3"); gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0); gtk_widget_show (button); separator = gtk_hseparator_new (); gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0); gtk_widget_show (separator); box2 = gtk_vbox_new (FALSE, 10); gtk_container_set_border_width (GTK_CONTAINER (box2), 10); gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0); gtk_widget_show (box2); button = gtk_button_new_with_label ("close"); g_signal_connect_swapped (button, "clicked", G_CALLBACK (close_application), window); gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0); gtk_widget_set_can_default (button, TRUE); gtk_widget_grab_default (button); gtk_widget_show (button); gtk_widget_show (window); gtk_main (); return 0; } |
1 則留言