NO-OP: mark various state property names as explicitly non-translated
[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 #ifdef __APPLE__
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)"
43          *
44          * the alternative is to patch gtk's source:
45          * gtk/gtkcolorsel.c  gtk_color_selection_init() which packs
46          *
47          *  top_hbox [ VBOX [ triangle || hbox [ sample-area || picker-button] ] || ... ]
48          */
49         ColorSelection* cs = get_colorsel(); // IS-A VBOX
50         if (!cs) { return ; }
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 ; }
59         picker->hide ();
60 #endif
61 }
62
63 StripableColorDialog::~StripableColorDialog ()
64 {
65         reset ();
66 }
67
68 void
69 StripableColorDialog::palette_changed_hook (const Glib::RefPtr<Gdk::Screen>& s, const Gdk::ArrayHandle_Color& c)
70 {
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);
74 }
75
76 void
77 StripableColorDialog::initialize_color_palette ()
78 {
79         // non-static member, because it needs a screen()
80         if (palette_initialized) {
81                 return;
82         }
83         gtk_palette_changed_hook =
84                 get_colorsel()->set_change_palette_hook (&StripableColorDialog::palette_changed_hook);
85
86         std::string cp = UIConfiguration::instance ().get_stripable_color_palette ();
87         if (!cp.empty()) {
88                 Gdk::ArrayHandle_Color c = ColorSelection::palette_from_string (cp);
89                 gtk_palette_changed_hook (get_screen (), c);
90         }
91         palette_initialized = true;
92 }
93
94 void
95 StripableColorDialog::reset ()
96 {
97         hide ();
98         if (_stripable && _stripable->active_color_picker() == this) {
99                 _stripable->set_active_color_picker (0);
100         }
101         _stripable.reset ();
102         _color_changed_connection.disconnect ();
103 }
104
105 void
106 StripableColorDialog::popup (const std::string& name, uint32_t color)
107 {
108         set_title (string_compose (_("Color Selection: %1"), name));
109         _initial_color = color;
110
111         get_colorsel()->set_has_opacity_control (false);
112         get_colorsel()->set_has_palette (true);
113
114         Gdk::Color c = gdk_color_from_rgba (_initial_color);
115
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));
120
121         present ();
122 }
123
124 void
125 StripableColorDialog::popup (boost::shared_ptr<ARDOUR::Stripable> s)
126 {
127         if (s && s->active_color_picker()) {
128                 s->active_color_picker()->present ();
129                 return;
130         }
131         if (_stripable == s) {
132                 /* keep modified color */
133                 present ();
134                 return;
135         }
136
137         _stripable = s;
138         _stripable->set_active_color_picker (this);
139         popup (s->name(), _stripable->presentation_info().color ());
140 }
141
142 void
143 StripableColorDialog::finish_color_edit (int response)
144 {
145         if (response == RESPONSE_OK) {
146                 ColorChanged (gdk_color_to_rgba (get_colorsel()->get_current_color())); /* EMIT SIGNAL */
147         }
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);
152         }
153         reset ();
154 }
155
156 void
157 StripableColorDialog::color_changed ()
158 {
159         if (_stripable) {
160                 _stripable->presentation_info().set_color (gdk_color_to_rgba (get_colorsel()->get_current_color()));
161         }
162 }
163
164
165 ArdourColorButton::ArdourColorButton ()
166 {
167         _color_picker.ColorChanged.connect (sigc::mem_fun(*this, &ArdourColorButton::color_selected));
168 }
169
170 void
171 ArdourColorButton::on_clicked ()
172 {
173         _color_picker.popup ("", gdk_color_to_rgba (get_color ()));
174         _color_picker.get_window ()->set_transient_for (get_window ());
175 }
176
177 void
178 ArdourColorButton::color_selected (uint32_t color)
179 {
180         Gdk::Color c;
181         set_color_from_rgba (c, color);
182         set_color (c);
183         g_signal_emit_by_name (GTK_WIDGET(gobj()), "color-set", 0);
184 }