📄 gtk_tut-6.html
字号:
{
/* If control reaches here, the toggle button is down */
} else {
/* If control reaches here, the toggle button is up */
}
}
</PRE>
</CODE></BLOCKQUOTE>
<P>To force the state of a toggle button, and its children, the radio and
check buttons, use this function:
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_toggle_button_set_active( GtkToggleButton *toggle_button,
gint state );
</PRE>
</CODE></BLOCKQUOTE>
<P>The above call can be used to set the state of the toggle button, and
its children the radio and check buttons. Passing in your created
button as the first argument, and a TRUE or FALSE for the second state
argument to specify whether it should be down (depressed) or up
(released). Default is up, or FALSE.
<P>Note that when you use the gtk_toggle_button_set_active() function, and
the state is actually changed, it causes the "clicked" signal to be
emitted from the button.
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_toggle_button_toggled (GtkToggleButton *toggle_button);
</PRE>
</CODE></BLOCKQUOTE>
<P>This simply toggles the button, and emits the "toggled" signal.
<P>
<H2><A NAME="ss6.3">6.3 Check Buttons</A>
</H2>
<P>Check buttons inherit many properties and functions from the the
toggle buttons above, but look a little different. Rather than being
buttons with text inside them, they are small squares with the text to
the right of them. These are often used for toggling options on and
off in applications.
<P>The two creation functions are similar to those of the normal button.
<P>
<BLOCKQUOTE><CODE>
<PRE>
GtkWidget *gtk_check_button_new( void );
GtkWidget *gtk_check_button_new_with_label ( gchar *label );
</PRE>
</CODE></BLOCKQUOTE>
<P>The new_with_label function creates a check button with a label beside
it.
<P>Checking the state of the check button is identical to that of the
toggle button.
<P>
<H2><A NAME="sec_Radio_Buttons"></A> <A NAME="ss6.4">6.4 Radio Buttons </A>
</H2>
<P>Radio buttons are similar to check buttons except they are grouped so
that only one may be selected/depressed at a time. This is good for
places in your application where you need to select from a short list
of options.
<P>Creating a new radio button is done with one of these calls:
<P>
<BLOCKQUOTE><CODE>
<PRE>
GtkWidget *gtk_radio_button_new( GSList *group );
GtkWidget *gtk_radio_button_new_with_label( GSList *group,
gchar *label );
</PRE>
</CODE></BLOCKQUOTE>
<P>You'll notice the extra argument to these calls. They require a group
to perform their duty properly. The first call to
gtk_radio_button_new_with_label or gtk_radio_button_new_with_label
should pass NULL as the first argument. Then create a group using:
<P>
<BLOCKQUOTE><CODE>
<PRE>
GSList *gtk_radio_button_group( GtkRadioButton *radio_button );
</PRE>
</CODE></BLOCKQUOTE>
<P>The important thing to remember is that gtk_radio_button_group must be
called for each new button added to the group, with the previous
button passed in as an argument. The result is then passed into the
next call to gtk_radio_button_new or
gtk_radio_button_new_with_label. This allows a chain of buttons to be
established. The example below should make this clear.
<P>You can shorten this slightly by using the following syntax, which
removes the need for a variable to hold the list of buttons. This form
is used in the example to create the third button:
<P>
<BLOCKQUOTE><CODE>
<PRE>
button2 = gtk_radio_button_new_with_label(
gtk_radio_button_group (GTK_RADIO_BUTTON (button1)),
"button2");
</PRE>
</CODE></BLOCKQUOTE>
<P>It is also a good idea to explicitly set which button should be the
default depressed button with:
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_toggle_button_set_active( GtkToggleButton *toggle_button,
gint state );
</PRE>
</CODE></BLOCKQUOTE>
<P>This is described in the section on toggle buttons, and works in
exactly the same way. Once the radio buttons are grouped together,
only one of the group may be active at a time. If the user clicks on
one radio button, and then on another, the first radio button will
first emit a "toggled" signal (to report becoming inactive), and then
the second will emit its "toggled" signal (to report becoming active).
<P>The following example creates a radio button group with three buttons.
<P>
<BLOCKQUOTE><CODE>
<PRE>
/* example-start radiobuttons radiobuttons.c */
#include <gtk/gtk.h>
#include <glib.h>
gint 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);
gtk_signal_connect (GTK_OBJECT (window), "delete_event",
GTK_SIGNAL_FUNC(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_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(
gtk_radio_button_group (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");
gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC(close_application),
GTK_OBJECT (window));
gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
gtk_widget_grab_default (button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main();
return(0);
}
/* example-end */
</PRE>
</CODE></BLOCKQUOTE>
<P>
<P>
<HR>
<A HREF="gtk_tut-7.html">Next</A>
<A HREF="gtk_tut-5.html">Previous</A>
<A HREF="gtk_tut.html#toc6">Contents</A>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -