2 * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
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.
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.
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.
19 #include "pbd/compose.h"
22 #include "stripable_colorpicker.h"
23 #include "ui_config.h"
27 using namespace ARDOUR_UI_UTILS;
29 bool StripableColorDialog::palette_initialized = false;
30 Gtk::ColorSelection::SlotChangePaletteHook StripableColorDialog::gtk_palette_changed_hook;
32 StripableColorDialog::StripableColorDialog ()
34 initialize_color_palette ();
35 signal_response().connect (sigc::mem_fun (*this, &StripableColorDialog::finish_color_edit));
38 /* hide eyedropper button -- which does not work on OSX:
39 * " the problem is worse than just `not getting the color' though.
40 * The action doesn't actually complete, and window focus is in a
41 * `weird state' until you click inside the color-picker dialog twice;
42 * then it all seems back to normal (but no color got picked)"
44 * the alternative is to patch gtk's source:
45 * gtk/gtkcolorsel.c gtk_color_selection_init() which packs
47 * top_hbox [ VBOX [ triangle || hbox [ sample-area || picker-button] ] || ... ]
49 ColorSelection* cs = get_colorsel(); // IS-A VBOX
51 Gtk::HBox* top_hbox = dynamic_cast<Gtk::HBox*> (cs->children()[0].get_widget());
52 if (!top_hbox) { return ; }
53 Gtk::VBox* vbox = dynamic_cast<Gtk::VBox*> (top_hbox->children()[0].get_widget());
54 if (!vbox) { return ; }
55 Gtk::HBox* hbox = dynamic_cast<Gtk::HBox*> (vbox->children()[1].get_widget());
56 if (!hbox) { return ; }
57 Gtk::Button* picker = dynamic_cast<Gtk::Button*> (hbox->children()[1].get_widget());
58 if (!picker) { return ; }
63 StripableColorDialog::~StripableColorDialog ()
69 StripableColorDialog::palette_changed_hook (const Glib::RefPtr<Gdk::Screen>& s, const Gdk::ArrayHandle_Color& c)
71 std::string p = std::string (ColorSelection::palette_to_string (c));
72 UIConfiguration::instance ().set_stripable_color_palette (p);
73 gtk_palette_changed_hook (s, c);
77 StripableColorDialog::initialize_color_palette ()
79 // non-static member, because it needs a screen()
80 if (palette_initialized) {
83 gtk_palette_changed_hook =
84 get_colorsel()->set_change_palette_hook (&StripableColorDialog::palette_changed_hook);
86 std::string cp = UIConfiguration::instance ().get_stripable_color_palette ();
88 Gdk::ArrayHandle_Color c = ColorSelection::palette_from_string (cp);
89 gtk_palette_changed_hook (get_screen (), c);
91 palette_initialized = true;
95 StripableColorDialog::reset ()
98 if (_stripable && _stripable->active_color_picker() == this) {
99 _stripable->set_active_color_picker (0);
102 _color_changed_connection.disconnect ();
106 StripableColorDialog::popup (const std::string& name, uint32_t color)
108 set_title (string_compose (_("Color Selection: %1"), name));
109 _initial_color = color;
111 get_colorsel()->set_has_opacity_control (false);
112 get_colorsel()->set_has_palette (true);
114 Gdk::Color c = gdk_color_from_rgba (_initial_color);
116 get_colorsel()->set_previous_color (c);
117 get_colorsel()->set_current_color (c);
118 _color_changed_connection.disconnect ();
119 _color_changed_connection = get_colorsel()->signal_color_changed().connect (sigc::mem_fun (*this, &StripableColorDialog::color_changed));
125 StripableColorDialog::popup (boost::shared_ptr<ARDOUR::Stripable> s)
127 if (s && s->active_color_picker()) {
128 s->active_color_picker()->present ();
131 if (_stripable == s) {
132 /* keep modified color */
138 _stripable->set_active_color_picker (this);
139 popup (s->name(), _stripable->presentation_info().color ());
143 StripableColorDialog::finish_color_edit (int response)
145 if (response == RESPONSE_OK) {
146 ColorChanged (gdk_color_to_rgba (get_colorsel()->get_current_color())); /* EMIT SIGNAL */
148 if (_stripable && response == RESPONSE_OK) {
149 _stripable->presentation_info().set_color (gdk_color_to_rgba (get_colorsel()->get_current_color()));
150 } else if (_stripable) {
151 _stripable->presentation_info().set_color (_initial_color);
157 StripableColorDialog::color_changed ()
160 _stripable->presentation_info().set_color (gdk_color_to_rgba (get_colorsel()->get_current_color()));
165 ArdourColorButton::ArdourColorButton ()
167 _color_picker.ColorChanged.connect (sigc::mem_fun(*this, &ArdourColorButton::color_selected));
171 ArdourColorButton::on_clicked ()
173 _color_picker.popup ("", gdk_color_to_rgba (get_color ()));
174 _color_picker.get_window ()->set_transient_for (get_window ());
178 ArdourColorButton::color_selected (uint32_t color)
181 set_color_from_rgba (c, color);
183 g_signal_emit_by_name (GTK_WIDGET(gobj()), "color-set", 0);