ToggleButton
Object Hierarchy:
Description:
public class ToggleButton : Button, Implementor, Actionable, Activatable, Buildable
A ToggleButton is a Button which will remain “pressed-in” when clicked.
Clicking again will cause the toggle button to return to its normal state.
A toggle button is created by calling either ToggleButton or ToggleButton.with_label. If using the former, it is advisable to pack a widget, (such as a Label and/or a Image), into the toggle button’s container. (See Button for more information).
The state of a ToggleButton can be set specifically using set_active, and retrieved using get_active.
To simply switch the state of a toggle button, use toggled.
CSS nodes
GtkToggleButton has a single CSS node with name button. To differentiate it from a plain Button, it gets the .toggle style class.
Creating two ToggleButton widgets.
static void output_state (GtkToggleButton *source, gpointer user_data) {
printf ("Active: %d\n", gtk_toggle_button_get_active (source));
}
void make_toggles (void) {
GtkWidget *window, *toggle1, *toggle2;
GtkWidget *box;
const char *text;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
text = "Hi, I’m a toggle button.";
toggle1 = gtk_toggle_button_new_with_label (text);
// Makes this toggle button invisible
gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle1),
TRUE);
g_signal_connect (toggle1, "toggled",
G_CALLBACK (output_state),
NULL);
gtk_container_add (GTK_CONTAINER (box), toggle1);
text = "Hi, I’m a toggle button.";
toggle2 = gtk_toggle_button_new_with_label (text);
gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle2),
FALSE);
g_signal_connect (toggle2, "toggled",
G_CALLBACK (output_state),
NULL);
gtk_container_add (GTK_CONTAINER (box), toggle2);
gtk_container_add (GTK_CONTAINER (window), box);
gtk_widget_show_all (window);
}
Example: ToggleButton:
public class Application : Gtk.Window {
private int click_counter = 1;
public Application () {
// Prepare Gtk.Window:
this.title = "My Gtk.ToggleButton";
this.window_position = Gtk.WindowPosition.CENTER;
this.destroy.connect (Gtk.main_quit);
// The button:
Gtk.ToggleButton button = new Gtk.ToggleButton.with_label ("Click me (1)");
this.add (button);
// Set button to "avtive":
button.set_active (true);
button.toggled.connect (() => {
// Emitted when the button has been activated:
if (button.active) {
this.click_counter++;
} else {
this.click_counter--;
}
button.label = "Click me (%d)".printf (this.click_counter);
});
}
public static int main (string[] args) {
Gtk.init (ref args);
Application app = new Application ();
app.show_all ();
Gtk.main ();
return 0;
}
}
valac --pkg gtk+-3.0 Gtk.ToggleButton.vala