|
Packit |
1470ea |
|
|
Packit |
1470ea |
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="radiobutton.c" xml:lang="ca">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">RadioButton (C)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="c#buttons"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="togglebutton.c"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="grid.c"/>
|
|
Packit |
1470ea |
<revision version="0.2" date="2012-06-22" status="draft"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Monica Kochofar</name>
|
|
Packit |
1470ea |
<email its:translate="no">monicakochofar@gmail.com</email>
|
|
Packit |
1470ea |
<years>2012</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<desc>A choice from multiple checkbuttons</desc>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>RadioButton</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/radiobutton.png"/>
|
|
Packit |
1470ea |
These radiobuttons report their activity in the terminal.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
#include <gtk/gtk.h>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/*Signal handler for the "toggled" signal of the RadioButton*/
|
|
Packit |
1470ea |
static void
|
|
Packit |
1470ea |
button_toggled_cb (GtkWidget *button,
|
|
Packit |
1470ea |
gpointer user_data)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
char *b_state;
|
|
Packit |
1470ea |
const char *button_label;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
|
|
Packit |
1470ea |
b_state = "on";
|
|
Packit |
1470ea |
else {
|
|
Packit |
1470ea |
b_state = "off";
|
|
Packit |
1470ea |
g_print ("\n");
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
button_label = gtk_button_get_label (GTK_BUTTON (button));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
g_print ("%s was turned %s\n", button_label, b_state);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
static void
|
|
Packit |
1470ea |
activate (GtkApplication *app,
|
|
Packit |
1470ea |
gpointer user_data)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
GtkWidget *grid;
|
|
Packit |
1470ea |
GtkWidget *window;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
GtkWidget *button1;
|
|
Packit |
1470ea |
GtkWidget *button2;
|
|
Packit |
1470ea |
GtkWidget *button3;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/*Create a window with a set title and default size.
|
|
Packit |
1470ea |
Also, set a border width for the amount of space to leave
|
|
Packit |
1470ea |
inside the window*/
|
|
Packit |
1470ea |
window = gtk_application_window_new (app);
|
|
Packit |
1470ea |
gtk_window_set_title (GTK_WINDOW (window), "RadioButton Example");
|
|
Packit |
1470ea |
gtk_window_set_default_size (GTK_WINDOW (window), 250, 100);
|
|
Packit |
1470ea |
gtk_container_set_border_width (GTK_CONTAINER(window), 20);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/*Create an initial radio button*/
|
|
Packit |
1470ea |
button1 = gtk_radio_button_new_with_label (NULL, "Button 1");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/*Create a second radio button, and add it to the same group as Button 1*/
|
|
Packit |
1470ea |
button2 = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (button1),
|
|
Packit |
1470ea |
"Button 2");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/*Create a third button, and add it to the same group as Button 1*/
|
|
Packit |
1470ea |
button3 = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (button1),
|
|
Packit |
1470ea |
"Button 3");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/*Create a grid, attach the buttons, and position them accordingly*/
|
|
Packit |
1470ea |
grid = gtk_grid_new ();
|
|
Packit |
1470ea |
gtk_grid_attach (GTK_GRID (grid), button1, 0, 0, 1, 1);
|
|
Packit |
1470ea |
gtk_grid_attach (GTK_GRID (grid), button2, 0, 1, 1, 1);
|
|
Packit |
1470ea |
gtk_grid_attach (GTK_GRID (grid), button3, 0, 2, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/*Be sure to set the initial state of each button*/
|
|
Packit |
1470ea |
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button2), TRUE);
|
|
Packit |
1470ea |
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button1), FALSE);
|
|
Packit |
1470ea |
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button3), FALSE);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/*Connect the signal handlers (aka Callback functions) to the buttons*/
|
|
Packit |
1470ea |
g_signal_connect (GTK_TOGGLE_BUTTON (button1), "toggled",
|
|
Packit |
1470ea |
G_CALLBACK (button_toggled_cb), window);
|
|
Packit |
1470ea |
g_signal_connect (GTK_TOGGLE_BUTTON (button2), "toggled",
|
|
Packit |
1470ea |
G_CALLBACK (button_toggled_cb), window);
|
|
Packit |
1470ea |
g_signal_connect (GTK_TOGGLE_BUTTON (button3), "toggled",
|
|
Packit |
1470ea |
G_CALLBACK (button_toggled_cb), window);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (grid));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
gtk_widget_show_all (window);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
int
|
|
Packit |
1470ea |
main (int argc, char **argv)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
GtkApplication *app;
|
|
Packit |
1470ea |
int status;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
|
|
Packit |
1470ea |
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
|
|
Packit |
1470ea |
status = g_application_run (G_APPLICATION (app), argc, argv);
|
|
Packit |
1470ea |
g_object_unref (app);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
return status;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
In this sample we used the following:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/3.4/GtkApplication.html">GtkApplication</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/3.4/GtkWindow.html">GtkWindow</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkLabel.html">GtkLabel</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkGrid.html">GtkGrid</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkSwitch.html">GtkSwitch</link> </item>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</page>
|