FontChooserDialog
| Chapter 15. Dialogs |

## FontChooserDialog

The `FontChooserDialog` allows the user to choose a font. The `FontButton` opens a font chooser dialog when it is clicked.

Reference

### Example

Figure 15.4. FontChooserDialog

Source Code

File: `examplewindow.h` (For use with gtkmm 4)

    #ifndef GTKMM_EXAMPLEWINDOW_H
    #define GTKMM_EXAMPLEWINDOW_H

    #include <gtkmm.h>
    #include <memory>

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

    protected:
      //Signal handlers:
      void on_font_button_font_set();
      void on_button_dialog_clicked();
      void on_dialog_response(int response_id);

      //Child widgets:
      Gtk::Box m_ButtonBox;
      Gtk::FontButton m_FontButton;
      Gtk::Button m_Button_Dialog;

      std::unique_ptr<Gtk::FontChooserDialog> m_pDialog;
    };

    #endif //GTKMM_EXAMPLEWINDOW_H

File: `examplewindow.cc` (For use with gtkmm 4)

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

    ExampleWindow::ExampleWindow()
    : m_ButtonBox(Gtk::Orientation::VERTICAL),
      m_FontButton("Sans 10"),
      m_Button_Dialog("Choose Font")
    {
      set_title("Gtk::FontChooserDialog example");

      set_child(m_ButtonBox);

      m_ButtonBox.append(m_FontButton);
      m_FontButton.set_expand(true);
      m_FontButton.set_use_font(true);
      m_FontButton.set_use_size(true);
      m_FontButton.signal_font_set().connect(sigc::mem_fun(*this,
        &ExampleWindow::on_font_button_font_set) );

      m_ButtonBox.append(m_Button_Dialog);
      m_Button_Dialog.set_expand(true);
      m_Button_Dialog.signal_clicked().connect(sigc::mem_fun(*this,
        &ExampleWindow::on_button_dialog_clicked) );
    }

    ExampleWindow::~ExampleWindow()
    {
    }

    void ExampleWindow::on_font_button_font_set()
    {
      auto font_name = m_FontButton.get_font();
      std::cout << "Font chosen: " << font_name << std::endl;
    }

    void ExampleWindow::on_button_dialog_clicked()
    {
      if (!m_pDialog)
      {
        m_pDialog = std::make_unique<Gtk::FontChooserDialog>("Please choose a font", *this);
        m_pDialog->set_modal(true);
        m_pDialog->set_hide_on_close(true);
        m_pDialog->signal_response().connect(
          sigc::mem_fun(*this, &ExampleWindow::on_dialog_response));
      }

      //Get the previously selected font name from the FontButton:
      m_pDialog->set_font(m_FontButton.get_font());

      m_pDialog->set_visible(true);
    }

    void ExampleWindow::on_dialog_response(int response_id)
    {
      m_pDialog->set_visible(false);

      //Handle the response:
      switch (response_id)
      {
        case Gtk::ResponseType::OK:
        {
          auto font_name = m_pDialog->get_font();
          std::cout << "Font chosen: " << font_name << std::endl;
          m_FontButton.set_font(font_name);
          break;
        }
        case Gtk::ResponseType::CANCEL:
        {
          std::cout << "Cancel clicked." << std::endl;
          break;
        }
        default:
        {
          std::cout << "Unexpected button clicked: " << response_id << std::endl;
          break;
        }
      }
    }

File: `main.cc` (For use with gtkmm 4)

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

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

      //Shows the window and returns when it is closed.
      return app->make_window_and_run<ExampleWindow>(argc, argv);
    }

ColorChooserDialog |  | Non-modal AboutDialog