1  /* Color Chooser
2  *
3  * Gtk::ColorChooserDialog lets the user choose a color.
4  *
5  */
6 
7 #include <gtkmm.h>
8 
9 class Example_ColorSel : public Gtk::Window
10 {
11 public:
12   Example_ColorSel();
13   ~Example_ColorSel() override;
14 
15 protected:
16   //Signal handlers:
17   void on_button_clicked();
18   bool on_drawing_area_draw(const Cairo::RefPtr<Cairo::Context>& cr);
19 
20   //Member widgets:
21   Gtk::Box m_VBox;
22   Gtk::Frame m_Frame;
23   Gtk::DrawingArea m_DrawingArea;
24   Gtk::Button m_Button;
25   Gdk::RGBA m_Color;
26 };
27 
28 //Called by DemoWindow;
do_colorsel()29 Gtk::Window* do_colorsel()
30 {
31   return new Example_ColorSel();
32 }
33 
Example_ColorSel()34 Example_ColorSel::Example_ColorSel()
35 : m_VBox(Gtk::ORIENTATION_VERTICAL, 8),
36   m_Button("_Change the above color", true)
37 {
38   set_title("Color Chooser");
39   set_border_width(8);
40 
41   m_VBox.set_border_width(8);
42   add(m_VBox);
43 
44   // Create the color swatch area
45   m_Frame.set_shadow_type(Gtk::SHADOW_IN);
46   m_VBox.pack_start(m_Frame);
47 
48   // set a fixed size
49   m_DrawingArea.set_size_request(200, 200);
50 
51   // set the color
52   m_Color.set_rgba(0, 0, 1, 1);
53   m_DrawingArea.signal_draw().connect(sigc::mem_fun(*this, &Example_ColorSel::on_drawing_area_draw));
54 
55   m_Frame.add(m_DrawingArea);
56 
57   m_Button.set_halign(Gtk::ALIGN_END);
58   m_Button.set_valign(Gtk::ALIGN_CENTER);
59 
60   m_VBox.pack_start(m_Button, Gtk::PACK_SHRINK);
61 
62   m_Button.signal_clicked().connect(sigc::mem_fun(*this, &Example_ColorSel::on_button_clicked));
63 
64   show_all();
65 }
66 
~Example_ColorSel()67 Example_ColorSel::~Example_ColorSel()
68 {
69 }
70 
on_button_clicked()71 void Example_ColorSel::on_button_clicked()
72 {
73   Gtk::ColorChooserDialog dialog("Changing color");
74   dialog.set_transient_for(*this);
75   dialog.set_rgba(m_Color);
76 
77   const int response = dialog.run();
78 
79   if(response == Gtk::RESPONSE_OK)
80   {
81     m_Color = dialog.get_rgba();
82   }
83 }
84 
on_drawing_area_draw(const Cairo::RefPtr<Cairo::Context> & cr)85 bool Example_ColorSel::on_drawing_area_draw(const Cairo::RefPtr<Cairo::Context>& cr)
86 {
87   Gdk::Cairo::set_source_rgba(cr, m_Color);
88   cr->paint();
89 
90   return true;
91 }
92 
93