Add newline to DEBUG::Soundcloud so that it's readable & flushed
[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 (boost::shared_ptr<ARDOUR::Stripable> s)
82 {
83         if (s && s->active_color_picker()) {
84                 s->active_color_picker()->present ();
85                 return;
86         }
87         if (_stripable == s) {
88                 /* keep modified color */
89                 present ();
90                 return;
91         }
92
93         _stripable = s;
94         _stripable->set_active_color_picker (this);
95         _initial_color = _stripable->presentation_info().color ();
96         set_title (string_compose (_("Color Selection: %1"), s->name()));
97
98         get_colorsel()->set_has_opacity_control (false);
99         get_colorsel()->set_has_palette (true);
100
101         Gdk::Color c = gdk_color_from_rgba (_initial_color);
102
103         get_colorsel()->set_previous_color (c);
104         get_colorsel()->set_current_color (c);
105         _color_changed_connection = get_colorsel()->signal_color_changed().connect (sigc::mem_fun (*this, &StripableColorDialog::color_changed));
106
107         present ();
108 }
109
110 void
111 StripableColorDialog::finish_color_edit (int response)
112 {
113         if (_stripable && response == RESPONSE_OK) {
114                 _stripable->presentation_info().set_color (gdk_color_to_rgba (get_colorsel()->get_current_color()));
115         } else {
116                 _stripable->presentation_info().set_color (_initial_color);
117         }
118         reset ();
119 }
120
121 void
122 StripableColorDialog::color_changed ()
123 {
124         if (_stripable) {
125                 _stripable->presentation_info().set_color (gdk_color_to_rgba (get_colorsel()->get_current_color()));
126         }
127 }