ToggleButton

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.ToggleButton.new_ or gtk.toggle_button.ToggleButton.newWithLabel. 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.ToggleButton.setActive, and retrieved using gtk.toggle_button.ToggleButton.getActive.

To simply switch the state of a toggle button, use gtk.toggle_button.ToggleButton.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);
}

Constructors

this
this(void* ptr, Flag!"Take" take)
this
this()

Creates a new toggle button. A widget should be packed into the button, as in gtk.button.Button.new_.

Members

Functions

connectToggled
gulong connectToggled(T callback, Flag!"After" after)

Connect to Toggled signal.

getActive
bool getActive()

Queries a #GtkToggleButton and returns its current state. Returns true if the toggle button is pressed in and false if it is raised.

getInconsistent
bool getInconsistent()

Gets the value set by gtk.toggle_button.ToggleButton.setInconsistent.

getMode
bool getMode()

Retrieves whether the button is displayed as a separate indicator and label. See gtk.toggle_button.ToggleButton.setMode.

self
ToggleButton self()

Returns this, for use in with statements.

setActive
void setActive(bool isActive)

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 #GtkToggleButton::toggled signal and the #GtkButton::clicked signal to be emitted.

setInconsistent
void setInconsistent(bool 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.ToggleButton.setInconsistent only affects visual appearance, it doesn’t affect the semantics of the button.

setMode
void setMode(bool drawIndicator)

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.

toggled
void toggled()

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

Properties

_gType
GType _gType [@property getter]
active
bool active [@property getter]
active
bool active [@property setter]
drawIndicator
bool drawIndicator [@property getter]
drawIndicator
bool drawIndicator [@property setter]
inconsistent
bool inconsistent [@property getter]
inconsistent
bool inconsistent [@property setter]

Static functions

_getGType
GType _getGType()
builder
ToggleButtonGidBuilder builder()

Get builder for gtk.toggle_button.ToggleButton

newWithLabel
gtk.toggle_button.ToggleButton newWithLabel(string label)

Creates a new toggle button with a text label.

newWithMnemonic
gtk.toggle_button.ToggleButton newWithMnemonic(string label)

Creates a new #GtkToggleButton containing a label. The label will be created using gtk.label.Label.newWithMnemonic, so underscores in label indicate the mnemonic for the button.

Inherited Members

From Button

_getGType
GType _getGType()
_gType
GType _gType [@property getter]
self
Button self()

Returns this, for use in with statements.

builder
ButtonGidBuilder builder()

Get builder for gtk.button.Button

alwaysShowImage
bool alwaysShowImage [@property getter]

Get alwaysShowImage property.

alwaysShowImage
bool alwaysShowImage [@property setter]

Set alwaysShowImage property.

image
gtk.widget.Widget image [@property getter]

Get image property.

image
gtk.widget.Widget image [@property setter]

Set image property.

imagePosition
gtk.types.PositionType imagePosition [@property getter]

Get imagePosition property.

imagePosition
gtk.types.PositionType imagePosition [@property setter]

Set imagePosition property.

label
string label [@property getter]
label
string label [@property setter]
relief
gtk.types.ReliefStyle relief [@property getter]
relief
gtk.types.ReliefStyle relief [@property setter]
useStock
bool useStock [@property getter]
useStock
bool useStock [@property setter]
useUnderline
bool useUnderline [@property getter]
useUnderline
bool useUnderline [@property setter]
xalign
float xalign [@property getter]

Get xalign property.

xalign
float xalign [@property setter]

Set xalign property.

yalign
float yalign [@property getter]

Get yalign property.

yalign
float yalign [@property setter]

Set yalign property.

newFromIconName
gtk.button.Button newFromIconName(string iconName, gtk.types.IconSize size)

Creates a new button containing an icon from the current icon theme.

newFromStock
gtk.button.Button newFromStock(string stockId)

Creates a new #GtkButton containing the image and text from a [stock item]gtkstock. Some stock ids have preprocessor macros like #GTK_STOCK_OK and #GTK_STOCK_APPLY.

newWithLabel
gtk.button.Button newWithLabel(string label)

Creates a #GtkButton widget with a #GtkLabel child containing the given text.

newWithMnemonic
gtk.button.Button newWithMnemonic(string label)

Creates a new #GtkButton containing a label. If characters in label are preceded by an underscore, they are underlined. If you need a literal underscore character in a label, use “__” (two underscores). The first underlined character represents a keyboard accelerator called a mnemonic. Pressing Alt and that key activates the button.

clicked
void clicked()

Emits a #GtkButton::clicked signal to the given #GtkButton.

enter
void enter()

Emits a #GtkButton::enter signal to the given #GtkButton.

getAlignment
void getAlignment(float xalign, float yalign)

Gets the alignment of the child in the button.

getAlwaysShowImage
bool getAlwaysShowImage()

Returns whether the button will ignore the #GtkSettings:gtk-button-images setting and always show the image, if available.

getEventWindow
gdk.window.Window getEventWindow()

Returns the button’s event window if it is realized, null otherwise. This function should be rarely needed.

getFocusOnClick
bool getFocusOnClick()

Returns whether the button grabs focus when it is clicked with the mouse. See gtk.button.Button.setFocusOnClick.

getImage
gtk.widget.Widget getImage()

Gets the widget that is currenty set as the image of button. This may have been explicitly set by gtk.button.Button.setImage or constructed by gtk.button.Button.newFromStock.

getImagePosition
gtk.types.PositionType getImagePosition()

Gets the position of the image relative to the text inside the button.

getLabel
string getLabel()

Fetches the text from the label of the button, as set by gtk.button.Button.setLabel. If the label text has not been set the return value will be null. This will be the case if you create an empty button with gtk.button.Button.new_ to use as a container.

getRelief
gtk.types.ReliefStyle getRelief()

Returns the current relief style of the given #GtkButton.

getUseStock
bool getUseStock()

Returns whether the button label is a stock item.

getUseUnderline
bool getUseUnderline()

Returns whether an embedded underline in the button label indicates a mnemonic. See gtk_button_set_use_underline ().

leave
void leave()

Emits a #GtkButton::leave signal to the given #GtkButton.

pressed
void pressed()

Emits a #GtkButton::pressed signal to the given #GtkButton.

released
void released()

Emits a #GtkButton::released signal to the given #GtkButton.

setAlignment
void setAlignment(float xalign, float yalign)

Sets the alignment of the child. This property has no effect unless the child is a #GtkMisc or a #GtkAlignment.

setAlwaysShowImage
void setAlwaysShowImage(bool alwaysShow)

If true, the button will ignore the #GtkSettings:gtk-button-images setting and always show the image, if available.

setFocusOnClick
void setFocusOnClick(bool focusOnClick)

Sets whether the button will grab focus when it is clicked with the mouse. Making mouse clicks not grab focus is useful in places like toolbars where you don’t want the keyboard focus removed from the main area of the application.

setImage
void setImage(gtk.widget.Widget image)

Set the image of button to the given widget. The image will be displayed if the label text is null or if #GtkButton:always-show-image is true. You don’t have to call gtk.widget.Widget.show on image yourself.

setImagePosition
void setImagePosition(gtk.types.PositionType position)

Sets the position of the image relative to the text inside the button.

setLabel
void setLabel(string label)

Sets the text of the label of the button to str. This text is also used to select the stock item if gtk.button.Button.setUseStock is used.

setRelief
void setRelief(gtk.types.ReliefStyle relief)

Sets the relief style of the edges of the given #GtkButton widget. Two styles exist, gtk.types.ReliefStyle.Normal and gtk.types.ReliefStyle.None. The default style is, as one can guess, gtk.types.ReliefStyle.Normal. The deprecated value gtk.types.ReliefStyle.Half behaves the same as gtk.types.ReliefStyle.Normal.

setUseStock
void setUseStock(bool useStock)

If true, the label set on the button is used as a stock id to select the stock item for the button.

setUseUnderline
void setUseUnderline(bool useUnderline)

If true, an underline in the text of the button label indicates the next character should be used for the mnemonic accelerator key.

connectActivate
gulong connectActivate(T callback, Flag!"After" after)

Connect to Activate signal.

connectClicked
gulong connectClicked(T callback, Flag!"After" after)

Connect to Clicked signal.

connectEnter
gulong connectEnter(T callback, Flag!"After" after)

Connect to Enter signal.

connectLeave
gulong connectLeave(T callback, Flag!"After" after)

Connect to Leave signal.

connectPressed
gulong connectPressed(T callback, Flag!"After" after)

Connect to Pressed signal.

connectReleased
gulong connectReleased(T callback, Flag!"After" after)

Connect to Released signal.