Buttons

../../_images/button.png

A button is a clickable UI element that typically contains text or an icon. Whenever it is clicked, it performs an action.

Text buttons

Text buttons are buttons with a GtkLabel as a child. By default, text buttons will have the text-button CSS class assigned to them.

img/tutorials/text-button.png
<object class="GtkButton">
  <property name="label">Hello!</property>
</object>

Image buttons

Image buttons are buttons with a GtkImage as a child. By default, image buttons will have the image-button CSS class assigned to them.

img/tutorials/image-button.png
<object class="GtkButton">
  <property name="icon-name">audio-volume-muted-symbolic</property>
</object>

Custom buttons

You can decide the contents of a button by setting its child directly, both programmatically or in .ui files. For instance, if you want to have a button with both a label and an image, you can pack the individual widgets into a box, and set the box as the child of the button:

img/tutorials/custom-button.png
<object class="GtkButton" id="custom-button">
  <property name="child">
    <object class="GtkBox">
      <property name="orientation">horizontal</property>
      <property name="spacing">12</property>
      <child>
        <object class="GtkImage">
          <property name="icon-name">audio-volume-muted-symbolic</property>
        </object>
      </child>
      <child>
        <object class="GtkLabel">
          <property name="text">_Mute</property>
          <property name="use-underline">true</property>
        </object>
      </child>
    </object>
  </property>
</object>

Mnemonics

../../_images/mnemonic-button.png

If your buttons contain text, it is a good idea to give them a mnemonic (that is the underlined character that can be activated when holding the Alt key). To do so in a .ui file, you can use the following:

<object class="GtkButton" id="mybutton">
  <property name="label" translatable="yes">Frob_nicate</property>
  <property name="use_underline">True</property>
</object>

Notice the last property, use-underline: it is the important piece here. Without it, the button label would display the _ between the b and the n.

Default widget

In a dialog, one widget may be marked as the default, which means that it will be activated when the user hits Enter. Often, it is appropriate for a button to be marked as the default widget. In the simplest case, you can make a button the default by calling gtk_widget_grab_default(); you must also set the GtkWidget:can-default property for this to work.

gtk_widget_set_can_default (button, TRUE);

// "window" is the top level window that contains the button
gtk_window_set_default_widget (window, button);

One more detail that is good to know about default activation is that hitting Enter while the focus is in a GtkEntry will only activate the default widget if the entry is marked as ‘activates-default’.

gtk_entry_set_activates_default (entry, TRUE);

Styles

Buttons can be given a distinctive visual style, which can be appropriate in certain situations.

Suggested action

While most buttons have a neutral color some of them can have a different style, to emphasize buttons that are the suggested action in a set of potential choices. In the default theme, the suggested action color is blue.

Note

Suggested actions are typically tied to the default widget. While default activation is more about behaviour, the suggested action is more about the visuals. If you want the one, you probably want the other too. For more information, you should follow the GNOME human interface guidelines.

To mark a button as suggested action, add the suggested-action style class to it.

../../_images/suggested-action-button.png

In a .ui file, this looks as follows:

<object class="GtkButton" id="good-stuff">
  <property name="label">Good stuff</property>
  <style>
    <class name="suggested-action"/>
  </style>
</object>

Destructive action

Buttons that perform an action that is potentially destructive can use the destructive-action style class:

../../_images/destructive-action-button.png

In a .ui file, this looks as follows:

<object class="GtkButton" id="delete-everything">
  <property name="label">Delete Everything</property>
  <style>
    <class name="destructive-action"/>
  </style>
</object>

Useful methods for a Button widget

  • set_has_frame() will give a button a “flat” appearance, by adding a flat CSS class

API references

In this sample we used the following: