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