ToggleButtonProtocol

public protocol ToggleButtonProtocol : ButtonProtocol

A GtkToggleButton is 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 [ctorGtk.ToggleButton.new] or [ctorGtk.ToggleButton.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 [classGtk.Button] for more information).

The state of a GtkToggleButton can be set specifically using [methodGtk.ToggleButton.set_active], and retrieved using [methodGtk.ToggleButton.get_active].

To simply switch the state of a toggle button, use [methodGtk.ToggleButton.toggled].

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 [methodGtk.ToggleButton.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.

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 ();
  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);

  text = "Hi, I’m a toggle button.";
  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 a toggle button.";
  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_widget_show (window);
}

The ToggleButtonProtocol protocol exposes the methods and properties of an underlying GtkToggleButton instance. The default implementation of these can be found in the protocol extension below. For a concrete class that implements these methods and properties, see ToggleButton. Alternatively, use ToggleButtonRef as a lighweight, unowned reference if you already have an instance you just want to use.

  • ptr

    Untyped pointer to the underlying GtkToggleButton instance.

    Declaration

    Swift

    var ptr: UnsafeMutableRawPointer! { get }
  • toggle_button_ptr Default implementation

    Typed pointer to the underlying GtkToggleButton instance.

    Default Implementation

    Return the stored, untyped pointer as a typed pointer to the GtkToggleButton instance.

    Declaration

    Swift

    var toggle_button_ptr: UnsafeMutablePointer<GtkToggleButton>! { get }
  • Required Initialiser for types conforming to ToggleButtonProtocol

    Declaration

    Swift

    init(raw: UnsafeMutableRawPointer)

ToggleButton Class

  • Bind a ToggleButtonPropertyName source property to a given target object.

    Declaration

    Swift

    @discardableResult
    @inlinable
    func bind<Q, T>(property source_property: ToggleButtonPropertyName, to target: T, _ target_property: Q, flags f: BindingFlags = .default, transformFrom transform_from: @escaping GLibObject.ValueTransformer = { $0.transform(destValue: $1) }, transformTo transform_to: @escaping GLibObject.ValueTransformer = { $0.transform(destValue: $1) }) -> BindingRef! where Q : PropertyNameProtocol, T : ObjectProtocol

    Parameters

    source_property

    the source property to bind

    target

    the target object to bind to

    target_property

    the target property to bind to

    flags

    the flags to pass to the Binding

    transform_from

    ValueTransformer to use for forward transformation

    transform_to

    ValueTransformer to use for backwards transformation

    Return Value

    binding reference or nil in case of an error

  • get(property:) Extension method

    Get the value of a ToggleButton property

    Declaration

    Swift

    @inlinable
    func get(property: ToggleButtonPropertyName) -> GLibObject.Value

    Parameters

    property

    the property to get the value for

    Return Value

    the value of the named property

  • set(property:value:) Extension method

    Set the value of a ToggleButton property. Note that this will only have an effect on properties that are writable and not construct-only!

    Declaration

    Swift

    @inlinable
    func set(property: ToggleButtonPropertyName, value v: GLibObject.Value)

    Parameters

    property

    the property to get the value for

    Return Value

    the value of the named property

ToggleButton signals

  • Connect a Swift signal handler to the given, typed ToggleButtonSignalName signal

    Declaration

    Swift

    @discardableResult
    @inlinable
    func connect(signal s: ToggleButtonSignalName, flags f: ConnectFlags = ConnectFlags(0), handler h: @escaping SignalHandler) -> Int

    Parameters

    signal

    The signal to connect

    flags

    The connection flags to use

    data

    A pointer to user data to provide to the callback

    destroyData

    A GClosureNotify C function to destroy the data pointed to by userData

    handler

    The Swift signal handler (function or callback) to invoke on the given signal

    Return Value

    The signal handler ID (always greater than 0 for successful connections)

  • Connect a C signal handler to the given, typed ToggleButtonSignalName signal

    Declaration

    Swift

    @discardableResult
    @inlinable
    func connect(signal s: ToggleButtonSignalName, flags f: ConnectFlags = ConnectFlags(0), data userData: gpointer!, destroyData destructor: GClosureNotify? = nil, signalHandler h: @escaping GCallback) -> Int

    Parameters

    signal

    The signal to connect

    flags

    The connection flags to use

    data

    A pointer to user data to provide to the callback

    destroyData

    A GClosureNotify C function to destroy the data pointed to by userData

    signalHandler

    The C function to be called on the given signal

    Return Value

    The signal handler ID (always greater than 0 for successful connections)

  • onToggled(flags:handler:) Extension method

    Emitted whenever the GtkToggleButton‘s state is changed.

    Note

    This represents the underlying toggled signal

    Declaration

    Swift

    @discardableResult
    @inlinable
    func onToggled(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: ToggleButtonRef) -> Void) -> Int

    Parameters

    flags

    Flags

    unownedSelf

    Reference to instance of self

    handler

    The signal handler to call Run the given callback whenever the toggled signal is emitted

  • toggledSignal Extension method

    Typed toggled signal for using the connect(signal:) methods

    Declaration

    Swift

    static var toggledSignal: ToggleButtonSignalName { get }
  • The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.

    Note that getting this signal doesn’t itself guarantee that the value of the property has actually changed. When it is emitted is determined by the derived GObject class. If the implementor did not create the property with G_PARAM_EXPLICIT_NOTIFY, then any call to g_object_set_property() results in notify being emitted, even if the new value is the same as the old. If they did pass G_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only when they explicitly call g_object_notify() or g_object_notify_by_pspec(), and common practice is to do that only when the value has actually changed.

    This signal is typically used to obtain change notification for a single property, by specifying the property name as a detail in the g_signal_connect() call, like this:

    (C Language Example):

    g_signal_connect (text_view->buffer, "notify::paste-target-list",
                      G_CALLBACK (gtk_text_view_target_list_notify),
                      text_view)
    

    It is important to note that you must use canonical parameter names as detail strings for the notify signal.

    Note

    This represents the underlying notify::active signal

    Declaration

    Swift

    @discardableResult
    @inlinable
    func onNotifyActive(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: ToggleButtonRef, _ pspec: ParamSpecRef) -> Void) -> Int

    Parameters

    flags

    Flags

    unownedSelf

    Reference to instance of self

    pspec

    the GParamSpec of the property which changed.

    handler

    The signal handler to call Run the given callback whenever the notifyActive signal is emitted

  • notifyActiveSignal Extension method

    Typed notify::active signal for using the connect(signal:) methods

    Declaration

    Swift

    static var notifyActiveSignal: ToggleButtonSignalName { get }
  • The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.

    Note that getting this signal doesn’t itself guarantee that the value of the property has actually changed. When it is emitted is determined by the derived GObject class. If the implementor did not create the property with G_PARAM_EXPLICIT_NOTIFY, then any call to g_object_set_property() results in notify being emitted, even if the new value is the same as the old. If they did pass G_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only when they explicitly call g_object_notify() or g_object_notify_by_pspec(), and common practice is to do that only when the value has actually changed.

    This signal is typically used to obtain change notification for a single property, by specifying the property name as a detail in the g_signal_connect() call, like this:

    (C Language Example):

    g_signal_connect (text_view->buffer, "notify::paste-target-list",
                      G_CALLBACK (gtk_text_view_target_list_notify),
                      text_view)
    

    It is important to note that you must use canonical parameter names as detail strings for the notify signal.

    Note

    This represents the underlying notify::group signal

    Declaration

    Swift

    @discardableResult
    @inlinable
    func onNotifyGroup(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: ToggleButtonRef, _ pspec: ParamSpecRef) -> Void) -> Int

    Parameters

    flags

    Flags

    unownedSelf

    Reference to instance of self

    pspec

    the GParamSpec of the property which changed.

    handler

    The signal handler to call Run the given callback whenever the notifyGroup signal is emitted

  • notifyGroupSignal Extension method

    Typed notify::group signal for using the connect(signal:) methods

    Declaration

    Swift

    static var notifyGroupSignal: ToggleButtonSignalName { get }

ToggleButton Class: ToggleButtonProtocol extension (methods and fields)

  • getActive() Extension method

    Queries a GtkToggleButton and returns its current state.

    Returns true if the toggle button is pressed in and false if it is raised.

    Declaration

    Swift

    @inlinable
    func getActive() -> Bool
  • setActive(isActive:) Extension method

    Sets the status of the toggle button.

    Set to true if you want the GtkToggleButton to be “pressed in”, and false to raise it.

    If the status of the button changes, this action causes the [signalGtkToggleButton::toggled] signal to be emitted.

    Declaration

    Swift

    @inlinable
    func setActive(isActive: Bool)
  • set(group:) Extension method

    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 [ifaceGtk.Actionable] 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.

    Declaration

    Swift

    @inlinable
    func set(group: ToggleButtonRef? = nil)
  • set(group:) Extension method

    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 [ifaceGtk.Actionable] 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.

    Declaration

    Swift

    @inlinable
    func set<ToggleButtonT>(group: ToggleButtonT?) where ToggleButtonT : ToggleButtonProtocol
  • toggled() Extension method

    Emits the toggled signal on the GtkToggleButton.

    There is no good reason for an application ever to call this function.

    Declaration

    Swift

    @inlinable
    func toggled()
  • active Extension method

    If the toggle button should be pressed in.

    Declaration

    Swift

    @inlinable
    var active: Bool { get nonmutating set }