Add ColorButton drop-in replacement with palette support
[ardour.git] / gtk2_ardour / stripable_colorpicker.cc
1 /*
2  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #include "pbd/compose.h"
20 #include "pbd/i18n.h"
21
22 #include "stripable_colorpicker.h"
23 #include "ui_config.h"
24 #include "utils.h"
25
26 using namespace Gtk;
27 using namespace ARDOUR_UI_UTILS;
28
29 bool StripableColorDialog::palette_initialized = false;
30 Gtk::ColorSelection::SlotChangePaletteHook StripableColorDialog::gtk_palette_changed_hook;
31
32 StripableColorDialog::StripableColorDialog ()
33 {
34         initialize_color_palette ();
35         signal_response().connect (sigc::mem_fun (*this, &StripableColorDialog::finish_color_edit));
36 }
37
38 StripableColorDialog::~StripableColorDialog ()
39 {
40         reset ();
41 }
42
43 void
44 StripableColorDialog::palette_changed_hook (const Glib::RefPtr<Gdk::Screen>& s, const Gdk::ArrayHandle_Color& c)
45 {
46         std::string p = std::string (ColorSelection::palette_to_string (c));
47         UIConfiguration::instance ().set_stripable_color_palette (p);
48         gtk_palette_changed_hook (s, c);
49 }
50
51 void
52 StripableColorDialog::initialize_color_palette ()
53 {
54         // non-static member, because it needs a screen()
55         if (palette_initialized) {
56                 return;
57         }
58         gtk_palette_changed_hook =
59                 get_colorsel()->set_change_palette_hook (&StripableColorDialog::palette_changed_hook);
60
61         std::string cp = UIConfiguration::instance ().get_stripable_color_palette ();
62         if (!cp.empty()) {
63                 Gdk::ArrayHandle_Color c = ColorSelection::palette_from_string (cp);
64                 gtk_palette_changed_hook (get_screen (), c);
65         }
66         palette_initialized = true;
67 }
68
69 void
70 StripableColorDialog::reset ()
71 {
72         hide ();
73         if (_stripable && _stripable->active_color_picker() == this) {
74                 _stripable->set_active_color_picker (0);
75         }
76         _stripable.reset ();
77         _color_changed_connection.disconnect ();
78 }
79
80 void
81 StripableColorDialog::popup (const std::string& name, uint32_t color)
82 {
83         set_title (string_compose (_("Color Selection: %1"), name));
84         _initial_color = color;
85
86         get_colorsel()->set_has_opacity_control (false);
87         get_colorsel()->set_has_palette (true);
88
89         Gdk::Color c = gdk_color_from_rgba (_initial_color);
90
91         get_colorsel()->set_previous_color (c);
92         get_colorsel()->set_current_color (c);
93         _color_changed_connection.disconnect ();
94         _color_changed_connection = get_colorsel()->signal_color_changed().connect (sigc::mem_fun (*this, &StripableColorDialog::color_changed));
95
96         present ();
97 }
98
99 void
100 StripableColorDialog::popup (boost::shared_ptr<ARDOUR::Stripable> s)
101 {
102         if (s && s->active_color_picker()) {
103                 s->active_color_picker()->present ();
104                 return;
105         }
106         if (_stripable == s) {
107                 /* keep modified color */
108                 present ();
109                 return;
110         }
111
112         _stripable = s;
113         _stripable->set_active_color_picker (this);
114         popup (s->name(), _stripable->presentation_info().color ());
115 }
116
117 void
118 StripableColorDialog::finish_color_edit (int response)
119 {
120         if (response == RESPONSE_OK) {
121                 ColorChanged (gdk_color_to_rgba (get_colorsel()->get_current_color())); /* EMIT SIGNAL */
122         }
123         if (_stripable && response == RESPONSE_OK) {
124                 _stripable->presentation_info().set_color (gdk_color_to_rgba (get_colorsel()->get_current_color()));
125         } else if (_stripable) {
126                 _stripable->presentation_info().set_color (_initial_color);
127         }
128         reset ();
129 }
130
131 void
132 StripableColorDialog::color_changed ()
133 {
134         if (_stripable) {
135                 _stripable->presentation_info().set_color (gdk_color_to_rgba (get_colorsel()->get_current_color()));
136         }
137 }
138
139
140 ArdourColorButton::ArdourColorButton ()
141 {
142         _color_picker.ColorChanged.connect (sigc::mem_fun(*this, &ArdourColorButton::color_selected));
143 }
144
145 void
146 ArdourColorButton::on_clicked ()
147 {
148         _color_picker.popup ("", gdk_color_to_rgba (get_color ()));
149         _color_picker.get_window ()->set_transient_for (get_window ());
150 }
151
152 void
153 ArdourColorButton::color_selected (uint32_t color)
154 {
155         Gdk::Color c;
156         set_color_from_rgba (c, color);
157         set_color (c);
158         g_signal_emit_by_name (GTK_WIDGET(gobj()), "color-set", 0);
159 }