A Gtk.Toggle_Button.Gtk_Toggle_Button is a Gtk.Button.Gtk_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 gtk_toggle_button_new or Gtk.Toggle_Button.Gtk_New. If using the former, it is advisable to pack a widget, (such as a Gtk.Label.Gtk_Label and/or a Gtk.Image.Gtk_Image), into the toggle button's container. (See Gtk.Button.Gtk_Button for more information).
The state of a Gtk.Toggle_Button.Gtk_Toggle_Button can be set specifically using Gtk.Toggle_Button.Set_Active, and retrieved using Gtk.Toggle_Button.Get_Active.
To simply switch the state of a toggle button, use Gtk.Toggle_Button.Toggled.
# CSS nodes
GtkToggleButton has a single CSS node with name button. To differentiate it from a plain Gtk.Button.Gtk_Button, it gets the .toggle style class.
## Creating two Gtk.Toggle_Button.Gtk_Toggle_Button 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);
}
You should consider using a Gtk_Check_Button instead, since it looks nicer and provides more visual clues that the button can be toggled.
<screenshot>gtk-toggle_button</screenshot> <group>Buttons and Toggles</group> <testgtk>create_toggle_buttons.adb</testgtk>
function "+"
(Widget : access Gtk_Toggle_Button_Record'Class)
return Gtk.Actionable.Gtk_Actionable
function "+"
(Widget : access Gtk_Toggle_Button_Record'Class)
return Gtk.Activatable.Gtk_Activatable
function "+"
(Widget : access Gtk_Toggle_Button_Record'Class)
return Gtk.Buildable.Gtk_Buildable
function "-"
(Interf : Gtk.Actionable.Gtk_Actionable)
return Gtk_Toggle_Button
function "-"
(Interf : Gtk.Activatable.Gtk_Activatable)
return Gtk_Toggle_Button
function "-"
(Interf : Gtk.Buildable.Gtk_Buildable)
return Gtk_Toggle_Button
Active_Property : constant Glib.Properties.Property_Boolean;
type Cb_GObject_Void is not null access procedure
(Self : access Glib.Object.GObject_Record'Class);
type Cb_Gtk_Toggle_Button_Void is not null access procedure
(Self : access Gtk_Toggle_Button_Record'Class);
procedure Do_Set_Related_Action
(Self : not null access Gtk_Toggle_Button_Record;
Action : not null access Gtk.Action.Gtk_Action_Record'Class)
Draw_Indicator_Property : constant Glib.Properties.Property_Boolean;
function Get_Action_Name
(Self : not null access Gtk_Toggle_Button_Record) return UTF8_String
function Get_Action_Target_Value
(Self : not null access Gtk_Toggle_Button_Record)
return Glib.Variant.Gvariant
function Get_Active
(Toggle_Button : not null access Gtk_Toggle_Button_Record)
return Boolean
Queries a Gtk.Toggle_Button.Gtk_Toggle_Button and returns its current state. Returns True if the toggle button is pressed in and False if it is raised.
a Boolean value.
function Get_Inconsistent
(Toggle_Button : not null access Gtk_Toggle_Button_Record)
return Boolean
Gets the value set by Gtk.Toggle_Button.Set_Inconsistent.
True if the button is displayed as inconsistent, False otherwise
function Get_Mode
(Toggle_Button : not null access Gtk_Toggle_Button_Record)
return Boolean
Retrieves whether the button is displayed as a separate indicator and label. See Gtk.Toggle_Button.Set_Mode.
True if the togglebutton is drawn as a separate indicator and label.
function Get_Related_Action
(Self : not null access Gtk_Toggle_Button_Record)
return Gtk.Action.Gtk_Action
function Get_Type return Glib.GType
function Get_Use_Action_Appearance
(Self : not null access Gtk_Toggle_Button_Record) return Boolean
procedure Gtk_New
(Toggle_Button : out Gtk_Toggle_Button;
Label : UTF8_String := "")
Initialize a button. If Label is "", then no label is created inside the button and you will have to provide your own child through a call to Gtk.Container.Add. This is the recommended way to put a pixmap inside a toggle button. Initialize does nothing if the object was already created with another call to Initialize* or G_New.
a string containing the message to be placed in the toggle button.
procedure Gtk_New_With_Mnemonic
(Toggle_Button : out Gtk_Toggle_Button;
Label : UTF8_String)
Creates a new Gtk.Toggle_Button.Gtk_Toggle_Button containing a label. The label will be created using Gtk.Label.Gtk_New_With_Mnemonic, so underscores in Label indicate the mnemonic for the button. Initialize_With_Mnemonic does nothing if the object was already created with another call to Initialize* or G_New.
the text of the button, with an underscore in front of the mnemonic character
type Gtk_Toggle_Button is access all Gtk_Toggle_Button_Record'Class;
function Gtk_Toggle_Button_New_With_Label
(Label : UTF8_String := "") return Gtk_Toggle_Button
Initialize a button. If Label is "", then no label is created inside the button and you will have to provide your own child through a call to Gtk.Container.Add. This is the recommended way to put a pixmap inside a toggle button.
a string containing the message to be placed in the toggle button.
function Gtk_Toggle_Button_New_With_Mnemonic
(Label : UTF8_String) return Gtk_Toggle_Button
Creates a new Gtk.Toggle_Button.Gtk_Toggle_Button containing a label. The label will be created using Gtk.Label.Gtk_New_With_Mnemonic, so underscores in Label indicate the mnemonic for the button.
the text of the button, with an underscore in front of the mnemonic character
type Gtk_Toggle_Button_Record is new Gtk_Button_Record with null record;
package Implements_Gtk_Actionable is new Glib.Types.Implements
(Gtk.Actionable.Gtk_Actionable, Gtk_Toggle_Button_Record, Gtk_Toggle_Button);
package Implements_Gtk_Activatable is new Glib.Types.Implements
(Gtk.Activatable.Gtk_Activatable, Gtk_Toggle_Button_Record, Gtk_Toggle_Button);
package Implements_Gtk_Buildable is new Glib.Types.Implements
(Gtk.Buildable.Gtk_Buildable, Gtk_Toggle_Button_Record, Gtk_Toggle_Button);
Inconsistent_Property : constant Glib.Properties.Property_Boolean;
procedure Initialize
(Toggle_Button : not null access Gtk_Toggle_Button_Record'Class;
Label : UTF8_String := "")
Initialize a button. If Label is "", then no label is created inside the button and you will have to provide your own child through a call to Gtk.Container.Add. This is the recommended way to put a pixmap inside a toggle button. Initialize does nothing if the object was already created with another call to Initialize* or G_New.
a string containing the message to be placed in the toggle button.
procedure Initialize_With_Mnemonic
(Toggle_Button : not null access Gtk_Toggle_Button_Record'Class;
Label : UTF8_String)
Creates a new Gtk.Toggle_Button.Gtk_Toggle_Button containing a label. The label will be created using Gtk.Label.Gtk_New_With_Mnemonic, so underscores in Label indicate the mnemonic for the button. Initialize_With_Mnemonic does nothing if the object was already created with another call to Initialize* or G_New.
the text of the button, with an underscore in front of the mnemonic character
procedure On_Toggled
(Self : not null access Gtk_Toggle_Button_Record;
Call : Cb_GObject_Void;
Slot : not null access Glib.Object.GObject_Record'Class;
After : Boolean := False)
Should be connected if you wish to perform an action whenever the Gtk.Toggle_Button.Gtk_Toggle_Button's state is changed.
procedure On_Toggled
(Self : not null access Gtk_Toggle_Button_Record;
Call : Cb_Gtk_Toggle_Button_Void;
After : Boolean := False)
Should be connected if you wish to perform an action whenever the Gtk.Toggle_Button.Gtk_Toggle_Button's state is changed.
procedure Set_Action_Name
(Self : not null access Gtk_Toggle_Button_Record;
Action_Name : UTF8_String := "")
procedure Set_Action_Target_Value
(Self : not null access Gtk_Toggle_Button_Record;
Target_Value : Glib.Variant.Gvariant)
procedure Set_Active
(Toggle_Button : not null access Gtk_Toggle_Button_Record;
Is_Active : Boolean)
Change the state of the button. When Is_Active is True, the button is drawn as a pressed button
True or False.
procedure Set_Detailed_Action_Name
(Self : not null access Gtk_Toggle_Button_Record;
Detailed_Action_Name : UTF8_String)
procedure Set_Inconsistent
(Toggle_Button : not null access Gtk_Toggle_Button_Record;
Setting : Boolean := True)
If the user has selected a range of elements (such as some text or spreadsheet cells) that are affected by a toggle button, and the current values in that range are inconsistent, you may want to display the toggle in an "in between" state. This function turns on "in between" display. Normally you would turn off the inconsistent state again if the user toggles the toggle button. This has to be done manually, Gtk.Toggle_Button.Set_Inconsistent only affects visual appearance, it doesn't affect the semantics of the button.
True if state is inconsistent
procedure Set_Mode
(Toggle_Button : not null access Gtk_Toggle_Button_Record;
Draw_Indicator : Boolean)
Sets whether the button is displayed as a separate indicator and label. You can call this function on a checkbutton or a radiobutton with Draw_Indicator = False to make the button look like a normal button. This can be used to create linked strip of buttons that work like a Gtk.Stack_Switcher.Gtk_Stack_Switcher. This function only affects instances of classes like Gtk.Check_Button.Gtk_Check_Button and Gtk.Radio_Button.Gtk_Radio_Button that derive from Gtk.Toggle_Button.Gtk_Toggle_Button, not instances of Gtk.Toggle_Button.Gtk_Toggle_Button itself.
if True, draw the button as a separate indicator and label; if False, draw the button like a normal button
procedure Set_Related_Action
(Self : not null access Gtk_Toggle_Button_Record;
Action : not null access Gtk.Action.Gtk_Action_Record'Class)
procedure Set_Use_Action_Appearance
(Self : not null access Gtk_Toggle_Button_Record;
Use_Appearance : Boolean)
Signal_Toggled : constant Glib.Signal_Name := "toggled";
Should be connected if you wish to perform an action whenever the Gtk.Toggle_Button.Gtk_Toggle_Button's state is changed.
procedure Sync_Action_Properties
(Self : not null access Gtk_Toggle_Button_Record;
Action : access Gtk.Action.Gtk_Action_Record'Class)
procedure Toggled
(Toggle_Button : not null access Gtk_Toggle_Button_Record)
Emits the Gtk.Toggle_Button.Gtk_Toggle_Button::toggled signal on the Gtk.Toggle_Button.Gtk_Toggle_Button. There is no good reason for an application ever to call this function.