add Gtk::ColorButton widget to LuaDialog and add ability to change multiple route...
[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 "public_editor.h"
23 #include "stripable_colorpicker.h"
24 #include "ui_config.h"
25 #include "utils.h"
26
27 using namespace Gtk;
28 using namespace ARDOUR_UI_UTILS;
29
30 bool StripableColorDialog::palette_initialized = false;
31 Gtk::ColorSelection::SlotChangePaletteHook StripableColorDialog::gtk_palette_changed_hook;
32
33 StripableColorDialog::StripableColorDialog ()
34 {
35         initialize_color_palette ();
36         signal_response().connect (sigc::mem_fun (*this, &StripableColorDialog::finish_color_edit));
37
38 #ifdef __APPLE__
39         /* hide eyedropper button -- which does not work on OSX:
40          * " the problem is worse than just `not getting the color' though.
41          *   The action doesn't actually complete, and window focus is in a
42          *   `weird state' until you click inside the color-picker dialog twice;
43          *   then it all seems back to normal (but no color got picked)"
44          *
45          * the alternative is to patch gtk's source:
46          * gtk/gtkcolorsel.c  gtk_color_selection_init() which packs
47          *
48          *  top_hbox [ VBOX [ triangle || hbox [ sample-area || picker-button] ] || ... ]
49          */
50         ColorSelection* cs = get_colorsel(); // IS-A VBOX
51         if (!cs) { return ; }
52         Gtk::HBox* top_hbox = dynamic_cast<Gtk::HBox*> (cs->children()[0].get_widget());
53         if (!top_hbox) { return ; }
54         Gtk::VBox* vbox = dynamic_cast<Gtk::VBox*> (top_hbox->children()[0].get_widget());
55         if (!vbox) { return ; }
56         Gtk::HBox* hbox = dynamic_cast<Gtk::HBox*> (vbox->children()[1].get_widget());
57         if (!hbox) { return ; }
58         Gtk::Button* picker = dynamic_cast<Gtk::Button*> (hbox->children()[1].get_widget());
59         if (!picker) { return ; }
60         picker->hide ();
61 #endif
62 }
63
64 StripableColorDialog::~StripableColorDialog ()
65 {
66         reset ();
67 }
68
69 void
70 StripableColorDialog::palette_changed_hook (const Glib::RefPtr<Gdk::Screen>& s, const Gdk::ArrayHandle_Color& c)
71 {
72         std::string p = std::string (ColorSelection::palette_to_string (c));
73         UIConfiguration::instance ().set_stripable_color_palette (p);
74         gtk_palette_changed_hook (s, c);
75 }
76
77 void
78 StripableColorDialog::initialize_color_palette ()
79 {
80         // non-static member, because it needs a screen()
81         if (palette_initialized) {
82                 return;
83         }
84         gtk_palette_changed_hook =
85                 get_colorsel()->set_change_palette_hook (&StripableColorDialog::palette_changed_hook);
86
87         std::string cp = UIConfiguration::instance ().get_stripable_color_palette ();
88         if (!cp.empty()) {
89                 Gdk::ArrayHandle_Color c = ColorSelection::palette_from_string (cp);
90                 gtk_palette_changed_hook (get_screen (), c);
91         }
92         palette_initialized = true;
93 }
94
95 void
96 StripableColorDialog::reset ()
97 {
98         hide ();
99         if (_stripable && _stripable->active_color_picker() == this) {
100                 _stripable->set_active_color_picker (0);
101         }
102         _stripable.reset ();
103         _color_changed_connection.disconnect ();
104 }
105
106 void
107 StripableColorDialog::popup (const std::string& name, uint32_t color)
108 {
109         set_title (string_compose (_("Color Selection: %1"), name));
110         _initial_color = color;
111
112         get_colorsel()->set_has_opacity_control (false);
113         get_colorsel()->set_has_palette (true);
114
115         Gdk::Color c = gdk_color_from_rgba (_initial_color);
116
117         get_colorsel()->set_previous_color (c);
118         get_colorsel()->set_current_color (c);
119         _color_changed_connection.disconnect ();
120         _color_changed_connection = get_colorsel()->signal_color_changed().connect (sigc::mem_fun (*this, &StripableColorDialog::color_changed));
121
122         present ();
123 }
124
125 void
126 StripableColorDialog::popup (boost::shared_ptr<ARDOUR::Stripable> s)
127 {
128         if (s && s->active_color_picker()) {
129                 s->active_color_picker()->present ();
130                 return;
131         }
132         if (_stripable == s) {
133                 /* keep modified color */
134                 present ();
135                 return;
136         }
137
138         _stripable = s;
139         _stripable->set_active_color_picker (this);
140         popup (s->name(), _stripable->presentation_info().color ());
141 }
142
143 void
144 StripableColorDialog::finish_color_edit (int response)
145 {
146         ARDOUR::RouteList rl = PublicEditor::instance().get_selection().tracks.routelist();
147
148         if (response == RESPONSE_OK) {
149                 ColorChanged (gdk_color_to_rgba (get_colorsel()->get_current_color())); /* EMIT SIGNAL */
150         }
151         if (_stripable && response == RESPONSE_OK) {
152                 for (ARDOUR::RouteList::iterator i = rl.begin(); i != rl.end(); ++i) {
153                         (*i)->presentation_info().set_color (gdk_color_to_rgba (get_colorsel()->get_current_color()));
154                 }
155                 _stripable->presentation_info().set_color (gdk_color_to_rgba (get_colorsel()->get_current_color()));
156         } else if (_stripable) {
157                 _stripable->presentation_info().set_color (_initial_color);
158         }
159         reset ();
160 }
161
162 void
163 StripableColorDialog::color_changed ()
164 {
165         if (_stripable) {
166                 _stripable->presentation_info().set_color (gdk_color_to_rgba (get_colorsel()->get_current_color()));
167         }
168 }
169
170
171 ArdourColorButton::ArdourColorButton ()
172 {
173         _color_picker.ColorChanged.connect (sigc::mem_fun(*this, &ArdourColorButton::color_selected));
174 }
175
176 void
177 ArdourColorButton::on_clicked ()
178 {
179         _color_picker.popup ("", gdk_color_to_rgba (get_color ()));
180         _color_picker.get_window ()->set_transient_for (get_window ());
181 }
182
183 void
184 ArdourColorButton::color_selected (uint32_t color)
185 {
186         Gdk::Color c;
187         set_color_from_rgba (c, color);
188         set_color (c);
189         g_signal_emit_by_name (GTK_WIDGET(gobj()), "color-set", 0);
190 }