1.204 <GtkToggleButton>

Class: <GtkToggleButton>

It derives from <GtkButton>.

Shows a button which remains “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 toggle-button:new or toggle-button:new-with-label. If using the former, it is advisable to pack a widget, (such as a <GtkLabel> and/or a <GtkImage>), into the toggle button’s container. (See <GtkButton> for more information).

The state of a <GtkToggleButton> can be set specifically using toggle-button:set-active, and retrieved using toggle-button:get-active?.

Grouping

Toggle buttons can be grouped together, to form mutually exclusive groups - only one of the buttons can be toggled at a time, and toggling another one will switch the currently toggled one off.

To add a <GtkToggleButton> to a group, use toggle-button:set-group.

CSS nodes

<GtkToggleButton> has a single CSS node with name button. To differentiate it from a plain <GtkButton>, it gets the .toggle style class.

Accessibility

<GtkToggleButton> uses the Gtk.AccessibleRole.toggle_button role.

Creating two ‘GtkToggleButton‘ widgets.

static void
output_state (GtkToggleButton *source,
              gpointer         user_data)
{
  g_print ("Toggle button "%s" is active: %s",
           gtk_button_get_label (GTK_BUTTON (source)),
           gtk_toggle_button_get_active (source) ? "Yes" : "No");
}

static void
make_toggles (void)
{
  GtkWidget *window, *toggle1, *toggle2;
  GtkWidget *box;
  const char *text;

  window = gtk_window_new ();
  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);

  text = "Hi, I’m toggle button one";
  toggle1 = gtk_toggle_button_new_with_label (text);

  g_signal_connect (toggle1, "toggled",
                    G_CALLBACK (output_state),
                    NULL);
  gtk_box_append (GTK_BOX (box), toggle1);

  text = "Hi, I’m toggle button two";
  toggle2 = gtk_toggle_button_new_with_label (text);
  g_signal_connect (toggle2, "toggled",
                    G_CALLBACK (output_state),
                    NULL);
  gtk_box_append (GTK_BOX (box), toggle2);

  gtk_window_set_child (GTK_WINDOW (window), box);
  gtk_window_present (GTK_WINDOW (window));
}

Properties

Property: toggle-button:active self

If the toggle button should be pressed in.

Its value is a boolean.

Set it with (set! (toggle-button:active self) value).

Property: toggle-button:group self

The toggle button whose group this widget belongs to.

Its value is a <GtkToggleButton>, or #f.

It can only be set, with (set! (toggle-button:group self) value).

Procedures

Method: toggle-button:get-active? self ⇒ %return
Method: get-active? self ⇒ %return

Queries a <GtkToggleButton> and returns its current state.

Returns #t if the toggle button is pressed in and #f if it is raised.

self

Passed: a <GtkToggleButton>.

a <GtkToggleButton>.

%return

Returned: a boolean.

whether the button is pressed

Procedure: toggle-button:new ⇒ %return

Creates a new toggle button.

A widget should be packed into the button, as in button:new.

%return

Returned: a <GtkWidget>.

a new toggle button.

Procedure: toggle-button:new-with-label label ⇒ %return

Creates a new toggle button with a text label.

label

Passed: a string.

a string containing the message to be placed in the toggle button.

%return

Returned: a <GtkWidget>.

a new toggle button.

Procedure: toggle-button:new-with-mnemonic label ⇒ %return

Creates a new <GtkToggleButton> containing a label.

The label will be created using label:new-with-mnemonic, so underscores in label indicate the mnemonic for the button.

label

Passed: a string.

the text of the button, with an underscore in front of the mnemonic character

%return

Returned: a <GtkWidget>.

a new <GtkToggleButton>

Method: toggle-button:set-active self is-active
Method: set-active self is-active

Sets the status of the toggle button.

Set to #t if you want the <GtkToggleButton> to be “pressed in”, and #f to raise it.

If the status of the button changes, this action causes the toggle-button:toggled signal to be emitted.

self

Passed: a <GtkToggleButton>.

a <GtkToggleButton>.

is-active

Passed: a boolean.

#t or #f.

Method: toggle-button:set-group self [group]
Method: set-group self [group]

Adds self to the group of group.

In a group of multiple toggle buttons, only one button can be active at a time.

Setting up groups in a cycle leads to undefined behavior.

Note that the same effect can be achieved via the <GtkActionable> API, by using the same action with parameter type and state type ’s’ for all buttons in the group, and giving each button its own target value.

self

Passed: a <GtkToggleButton>.

a <GtkToggleButton>

group

Passed, or left out: a <GtkToggleButton>, or #f.

another <GtkToggleButton> to form a group with

Method: toggle-button:toggled self
Method: toggled self

Emits the ::toggled signal on the <GtkToggleButton>.

self

Passed: a <GtkToggleButton>.

a <GtkToggleButton>.

Signals

Signal: toggle-button:toggled

Connect a handler with (connect object toggle-button:toggled handler). It is called as (handler self).

Emitted whenever the <GtkToggleButton>’s state is changed.

self

Passed: a <GtkToggleButton>.

Virtual methods

Virtual method: toggle-button:do-toggled implementor self

Emits the ::toggled signal on the <GtkToggleButton>.

To override it in a new class, give register-type #:vfuncs `((toggled . ,procedure)). The procedure is called as (procedure self).

implementor

Passed: the class whose implementation to call: <GtkToggleButton>, or a class that derives from it, or its GType number.

self

Passed: a <GtkToggleButton>.

a <GtkToggleButton>.