GTK+ 2.0 教学-单选按钮 Radio Buttons

单选按钮与复选按钮相似,只是单选按钮是分组的,在一组中只有一个处于选中/按下状态。这在你的应用程式中要从几个选项中选一个的地方可以用到。

用这些呼叫之一来创建一个新的单选按钮:

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 則留言


Comments are closed.