GtkToggleButton

GtkToggleButton — Create buttons which retain their state

Properties

gboolean active Read / Write
gboolean draw-indicator Read / Write
gboolean inconsistent Read / Write

Signals

void toggled Run First

Types and Values

Object Hierarchy

    GObject
    ╰── GInitiallyUnowned
        ╰── GtkWidget
            ╰── GtkContainer
                ╰── GtkBin
                    ╰── GtkButton
                        ╰── GtkToggleButton
                            ├── GtkCheckButton
                            ╰── GtkMenuButton

Implemented Interfaces

GtkToggleButton implements AtkImplementorIface, GtkBuildable, GtkActionable and GtkActivatable.

Includes

#include <gtk/gtk.h>

Description

A GtkToggleButton is a GtkButton 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_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 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 GtkButton, it gets the .toggle style class.

Creating two GtkToggleButton 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);
}

Functions

gtk_toggle_button_new ()

GtkWidget *
gtk_toggle_button_new (void);

Creates a new toggle button. A widget should be packed into the button, as in gtk_button_new().

Returns

a new toggle button.

gtk_toggle_button_new_with_label ()

GtkWidget *
gtk_toggle_button_new_with_label (const gchar *label);

Creates a new toggle button with a text label.

Parameters

label

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

Returns

a new toggle button.

gtk_toggle_button_new_with_mnemonic ()

GtkWidget *
gtk_toggle_button_new_with_mnemonic (const gchar *label);

Creates a new GtkToggleButton containing a label. The label will be created using gtk_label_new_with_mnemonic(), so underscores in label indicate the mnemonic for the button.

Parameters

label

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

Returns

a new GtkToggleButton

gtk_toggle_button_set_mode ()

void
gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
                            gboolean draw_indicator);

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 GtkStackSwitcher.

This function only affects instances of classes like GtkCheckButton and GtkRadioButton that derive from GtkToggleButton, not instances of GtkToggleButton itself.

Parameters

toggle_button

a GtkToggleButton

draw_indicator

if TRUE, draw the button as a separate indicator and label; if FALSE, draw the button like a normal button

gtk_toggle_button_get_mode ()

gboolean
gtk_toggle_button_get_mode (GtkToggleButton *toggle_button);

Retrieves whether the button is displayed as a separate indicator and label. See gtk_toggle_button_set_mode().

Parameters

toggle_button

a GtkToggleButton

Returns

TRUE if the togglebutton is drawn as a separate indicator and label.

gtk_toggle_button_toggled ()

void
gtk_toggle_button_toggled (GtkToggleButton *toggle_button);

Emits the “toggled” signal on the GtkToggleButton. There is no good reason for an application ever to call this function.

Parameters

toggle_button

a GtkToggleButton.

gtk_toggle_button_get_active ()

gboolean
gtk_toggle_button_get_active (GtkToggleButton *toggle_button);

Queries a GtkToggleButton and returns its current state. Returns TRUE if the toggle button is pressed in and FALSE if it is raised.

Parameters

toggle_button

a GtkToggleButton.

Returns

a gboolean value.

gtk_toggle_button_set_active ()

void
gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
                              gboolean is_active);

Sets the status of the toggle button. Set to TRUE if you want the GtkToggleButton to be “pressed in”, and FALSE to raise it. This action causes the “toggled” signal and the “clicked” signal to be emitted.

Parameters

toggle_button

a GtkToggleButton.

is_active

TRUE or FALSE.

gtk_toggle_button_get_inconsistent ()

gboolean
gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button);

Gets the value set by gtk_toggle_button_set_inconsistent().

Parameters

toggle_button

a GtkToggleButton

Returns

TRUE if the button is displayed as inconsistent, FALSE otherwise

gtk_toggle_button_set_inconsistent ()

void
gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
                                    gboolean setting);

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.

Parameters

toggle_button

a GtkToggleButton

setting

TRUE if state is inconsistent

Types and Values

struct GtkToggleButton

struct GtkToggleButton;

Property Details

The “active” property

  “active”                   gboolean

If the toggle button should be pressed in.

Owner: GtkToggleButton

Flags: Read / Write

Default value: FALSE

The “draw-indicator” property

  “draw-indicator”           gboolean

If the toggle part of the button is displayed.

Owner: GtkToggleButton

Flags: Read / Write

Default value: FALSE

The “inconsistent” property

  “inconsistent”             gboolean

If the toggle button is in an "in between" state.

Owner: GtkToggleButton

Flags: Read / Write

Default value: FALSE

Signal Details

The “toggled” signal

void
user_function (GtkToggleButton *togglebutton,
               gpointer         user_data)

Should be connected if you wish to perform an action whenever the GtkToggleButton's state is changed.

Parameters

togglebutton

the object which received the signal.

user_data

user data set when the signal handler was connected.

Flags: Run First

© 2005–2020 The GNOME Project
Licensed under the GNU Lesser General Public License version 2.1 or later.
https://developer.gnome.org/gtk3/3.24/GtkToggleButton.html