Chapter 5. Buttons

## Chapter 5. Buttons

gtkmm provides four basic types of buttons:

Push-Buttons

  `Gtk::Button`. Standard buttons, usually marked with a label or picture. Pushing one triggers an action. See the Button section.

Toggle buttons

  `Gtk::ToggleButton`. Unlike a normal Button, which springs back up, a ToggleButton stays down until you press it again. It might be useful as an on/off switch. See the ToggleButton section.

Checkboxes

  `Gtk::CheckButton`. These act like ToggleButtons, but show their state in small squares, with their label at the side. They should be used in most situations which require an on/off setting. See the CheckBox section.

Radio buttons

  `Gtk::RadioButton`. Named after the station selectors on old car radios, these buttons are used in groups for options which are mutually exclusive. Pressing one causes all the others in its group to turn off. They are similar to CheckBoxes (a small widget with a label at the side), but usually look different. See the RadioButton section.

Note that, due to GTK+'s theming system, the appearance of these widgets will vary. In the case of checkboxes and radio buttons, they may vary considerably.

## Button

### Constructors

There are two ways to create a Button. You can specify a label string in the `Gtk::Button` constructor, or set it later with `set_label()`.

To define an accelerator key for keyboard navigation, place an underscore before one of the label's characters and specify `true` for the optional `mnemonic` parameter. For instance:

    Gtk::Button* pButton = new Gtk::Button("_Something", true);

Wherever possible you should use Stock items, to ensure consistency with other applications, and to improve the appearance of your applications by using icons. For instance,

    Gtk::Button* pButton = new Gtk::Button(Gtk::Stock::OK);

This will use standard text, in all languages, with standard keyboard accelerators, with a standard icon.

`Gtk::Button` is also a container so you could put any other widget, such as a `Gtk::Image` into it.

Reference

### Example

This example creates a button with a picture and a label.

Figure 5.1. buttons example

Source Code

File: `buttons.h` (For use with gtkmm 3, not gtkmm 2)

    #ifndef GTKMM_EXAMPLE_BUTTONS_H
    #define GTKMM_EXAMPLE_BUTTONS_H

    #include <gtkmm/window.h>
    #include <gtkmm/button.h>

    class Buttons : public Gtk::Window
    {
    public:
      Buttons();
      virtual ~Buttons();

    protected:
      //Signal handlers:
      void on_button_clicked();

      //Child widgets:
      Gtk::Button m_button;
    };

    #endif //GTKMM_EXAMPLE_BUTTONS_H

File: `main.cc` (For use with gtkmm 3, not gtkmm 2)

    #include "buttons.h"
    #include <gtkmm/application.h>

    int main(int argc, char *argv[])
    {
      Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

      Buttons buttons;

      //Shows the window and returns when it is closed.
      return app->run(buttons);
    }

File: `buttons.cc` (For use with gtkmm 3, not gtkmm 2)

    #include "buttons.h"
    #include <iostream>

    Buttons::Buttons()
    {
      m_button.add_pixlabel("info.xpm", "cool button");

      set_title("Pixmap'd buttons!");
      set_border_width(10);

      m_button.signal_clicked().connect( sigc::mem_fun(*this,
                  &Buttons::on_button_clicked) );

      add(m_button);

      show_all_children();
    }

    Buttons::~Buttons()
    {
    }

    void Buttons::on_button_clicked()
    {
      std::cout << "The Button was clicked." << std::endl;
    }

### Signals

The `Gtk::Button` widget has the following signals, but most of the time you will just handle the `clicked` signal:

`pressed`

  Emitted when the button is pressed.

`released`

  Emitted when the button is released.

`clicked`

  Emitted when the button is pressed and released.

`enter`

  Emitted when the mouse pointer moves over the button's window.

`leave`

  Emitted when the mouse pointer leaves the button's window.

Chapter 4. Changes in gtkmm 3 |  | ToggleButton