GtkSwitch: GTK+ 4 Reference Manual

## GtkSwitch GtkSwitch — A “light switch” style toggle |

## Functions

GtkWidget * | gtk_switch_new ()
void | gtk_switch_set_active ()
gboolean | gtk_switch_get_active ()
void | gtk_switch_set_state ()
gboolean | gtk_switch_get_state ()

## Types and Values

struct | GtkSwitch
struct | GtkSwitchClass

## Includes

    #include <gtk/gtk.h>

## Description

GtkSwitch is a widget that has two states: on or off. The user can control which state should be active by clicking the empty area, or by dragging the handle.

GtkSwitch can also handle situations where the underlying state changes with a delay. See “state-set” for details.

### CSS nodes

    1
    2
    3
    4
    switch
    ├── label
    ├── label
    ╰── slider

GtkSwitch has four css nodes, the main node with the name switch and subnodes for the slider and the on and off labels. Neither of them is using any style classes.

## Functions

### gtk_switch_new ()

    GtkWidget *
    gtk_switch_new (void);

Creates a new GtkSwitch widget.

#### Returns

the newly created GtkSwitch instance

Since: 3.0

### gtk_switch_set_active ()

    void
    gtk_switch_set_active (GtkSwitch *sw,
                           gboolean is_active);

Changes the state of `sw` to the desired one.

#### Parameters

sw | a GtkSwitch |
is_active | `TRUE` if `sw` should be active, and `FALSE` otherwise |

Since: 3.0

### gtk_switch_get_active ()

    gboolean
    gtk_switch_get_active (GtkSwitch *sw);

Gets whether the GtkSwitch is in its “on” or “off” state.

#### Parameters

sw | a GtkSwitch |

#### Returns

`TRUE` if the GtkSwitch is active, and `FALSE` otherwise

Since: 3.0

### gtk_switch_set_state ()

    void
    gtk_switch_set_state (GtkSwitch *sw,
                          gboolean state);

Sets the underlying state of the GtkSwitch.

Normally, this is the same as “active”, unless the switch is set up for delayed state changes. This function is typically called from a “state-set” signal handler.

See “state-set” for details.

#### Parameters

sw | a GtkSwitch |
state | the new state |

Since: 3.14

### gtk_switch_get_state ()

    gboolean
    gtk_switch_get_state (GtkSwitch *sw);

Gets the underlying state of the GtkSwitch.

#### Parameters

sw | a GtkSwitch |

#### Returns

the underlying state

Since: 3.14

## Types and Values

### struct GtkSwitch

    struct GtkSwitch {
    };

The GtkSwitch contains private data and it should only be accessed using the provided API.

### struct GtkSwitchClass

    struct GtkSwitchClass {
      GtkWidgetClass parent_class;

      void (* activate) (GtkSwitch *sw);

      gboolean (* state_set) (GtkSwitch *sw, gboolean state);
    };

#### Members

`activate` () | An action signal and emitting it causes the switch to animate. |
`state_set` () | Class handler for the ::state-set signal. |

## See Also

GtkToggleButton