ColorDialog and ColorChooserDialog
| Chapter 17. Dialogs |

## ColorDialog and ColorChooserDialog

The `ColorChooserDialog` (deprecated since gtkmm 4.10) and `ColorDialog` (available since gtkmm 4.10) allow the user to choose a color. The `ColorButton` (deprecated since gtkmm 4.10) and `ColorDialogButton` (available since gtkmm 4.10) open a color selection dialog when it is clicked.

ColorDialog Reference

ColorChooserDialog Reference

### Example

Figure 17.3. ColorDialog

Source Code

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

    #ifndef GTKMM_EXAMPLEWINDOW_H
    #define GTKMM_EXAMPLEWINDOW_H

    #include <gtkmm.h>

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

    protected:
      //Signal handlers:
      void on_color_button_color_set();
      void on_button_dialog_clicked();
      void on_dialog_finish(const Glib::RefPtr<Gio::AsyncResult>& result);

      //Draw function:
      void on_drawing_area_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height);

      //Child widgets:
      Gtk::Box m_VBox;
      Gtk::ColorDialogButton m_ColorDialogButton;
      Gtk::Button m_Button_Dialog;
      Gtk::DrawingArea m_DrawingArea; //To show the color.

      Glib::RefPtr<Gtk::ColorDialog> m_pDialog;
      Gdk::RGBA m_Color;
    };

    #endif //GTKMM_EXAMPLEWINDOW_H

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

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

    ExampleWindow::ExampleWindow()
    : m_VBox(Gtk::Orientation::VERTICAL, 5),
      m_Button_Dialog("Choose Color")
    {
      set_title("Gtk::ColorDialog example");
      set_default_size(200, 200);

      m_pDialog = Gtk::ColorDialog::create();
      m_ColorDialogButton.set_dialog(m_pDialog);

      set_child(m_VBox);

      m_VBox.append(m_ColorDialogButton);
      m_ColorDialogButton.property_rgba().signal_changed().connect(
        sigc::mem_fun(*this, &ExampleWindow::on_color_button_color_set));

      m_VBox.append(m_Button_Dialog);
      m_Button_Dialog.signal_clicked().connect(sigc::mem_fun(*this,
        &ExampleWindow::on_button_dialog_clicked) );

      //Set start color:
      m_Color.set_red(0.0);
      m_Color.set_green(0.0);
      m_Color.set_blue(1.0);
      m_Color.set_alpha(1.0); //opaque
      m_ColorDialogButton.set_rgba(m_Color);

      m_VBox.append(m_DrawingArea);
      m_DrawingArea.set_expand(true);
      m_DrawingArea.set_draw_func(sigc::mem_fun(*this, &ExampleWindow::on_drawing_area_draw));
    }

    ExampleWindow::~ExampleWindow()
    {
    }

    void ExampleWindow::on_color_button_color_set()
    {
      //Store the chosen color:
      m_Color = m_ColorDialogButton.get_rgba();
      m_DrawingArea.queue_draw();
    }

    void ExampleWindow::on_button_dialog_clicked()
    {
      m_pDialog->choose_rgba(*this, m_Color,
        sigc::mem_fun(*this, &ExampleWindow::on_dialog_finish));
    }

    void ExampleWindow::on_dialog_finish(const Glib::RefPtr<Gio::AsyncResult>& result)
    {
      try
      {
        // If this call changes the color, it will trigger a call to
        // on_color_button_color_set().
        m_ColorDialogButton.set_rgba(m_pDialog->choose_rgba_finish(result));

        // This is what you should do if m_ColorDialogButton and
        // on_color_button_color_set() did not exist:
        //m_Color = m_pDialog->choose_rgba_finish(result);
        //m_DrawingArea.queue_draw();
      }
      catch (const Gtk::DialogError& err)
      {
        // Can be thrown by m_pDialog->choose_rgba_finish(result).
        std::cout << "No color selected. " << err.what() << std::endl;
      }
      catch (const Glib::Error& err)
      {
        std::cout << "Unexpected exception. " << err.what() << std::endl;
      }
    }

    void ExampleWindow::on_drawing_area_draw(const Cairo::RefPtr<Cairo::Context>& cr, int, int)
    {
      Gdk::Cairo::set_source_rgba(cr, m_Color);
      cr->paint();
    }

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);
    }

FileDialog and FileChooserDialog |  | FontDialog and FontChooserDialog