Move UIConfiguration Singleton into UIConfiguration header
[ardour.git] / gtk2_ardour / button_joiner.cc
1 /*
2     Copyright (C) 2012 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <iostream>
21 #include <algorithm>
22
23
24 #include <gtkmm/toggleaction.h>
25
26 #include "pbd/compose.h"
27 #include "gtkmm2ext/utils.h"
28 #include "gtkmm2ext/rgb_macros.h"
29
30 #include "ardour_ui.h"
31 #include "button_joiner.h"
32 #include "ui_config.h"
33
34 using namespace Gtk;
35
36 ButtonJoiner::ButtonJoiner (const std::string& str, Gtk::Widget& lw, Gtk::Widget& rw, bool central_joiner)
37         : left (lw)
38         , right (rw)
39         , name (str)
40         , active_fill_pattern (0)
41         , inactive_fill_pattern (0)
42         , central_link (central_joiner)
43 {
44         packer.set_homogeneous (true);
45
46         if (central_link) {
47                 packer.set_spacing (20);
48         }
49
50         packer.pack_start (left);
51         packer.pack_start (right);
52         packer.show ();
53
54         /* this alignment is how we position the box that holds the two widgets
55            within our allocation, and how we request more space around them.
56         */
57
58         align.add (packer);
59
60         if (!central_link) {
61                 align.set (0.5, 1.0);
62                 align.set_padding (9, 0, 9, 9);
63         } else {
64                 align.set (0.5, 0.5);
65                 align.set_padding (1, 1, 1, 1);
66         }
67
68         align.show ();
69
70         add (align);
71
72         add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|
73                     Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK);
74
75         uint32_t border_color;
76         uint32_t r, g, b, a;
77
78         border_color = UIConfiguration::instance().color (string_compose ("%1: border end", name));
79         UINT_TO_RGBA (border_color, &r, &g, &b, &a);
80         
81         border_r = r/255.0;
82         border_g = g/255.0;
83         border_b = b/255.0;
84
85         /* child cairo widgets need the color of the inner edge as their
86          * "background"
87          */
88
89         Gdk::Color col;
90         col.set_rgb_p (border_r, border_g, border_b);
91         provide_background_for_cairo_widget (*this, col);
92 }
93
94 ButtonJoiner::~ButtonJoiner ()
95 {
96         if (active_fill_pattern) {
97                 cairo_pattern_destroy (active_fill_pattern);
98                 cairo_pattern_destroy (inactive_fill_pattern);
99         }
100 }
101
102 void
103 ButtonJoiner::render (cairo_t* cr, cairo_rectangle_t*)
104 {
105         double h = get_height();
106         
107         if (!get_active()) {
108                 cairo_set_source (cr, inactive_fill_pattern);
109         } else {
110                 cairo_set_source (cr, active_fill_pattern);
111         }
112
113         if (!central_link) {
114                 /* outer rect */
115                 
116                 Gtkmm2ext::rounded_top_rectangle (cr, 0, 0, get_width(), h, 8);
117                 cairo_fill_preserve (cr);
118                 
119                 /* outer edge */
120                 
121                 cairo_set_line_width (cr, 1.5);
122                 cairo_set_source_rgb (cr, border_r, border_g, border_b);
123                 cairo_stroke (cr);
124                 
125                 /* inner "edge" */
126                 
127                 Gtkmm2ext::rounded_top_rectangle (cr, 8, 8, get_width() - 16, h - 8, 6);
128                 cairo_stroke (cr);
129         } else {
130                 if (get_active()) {
131                         Gtkmm2ext::rounded_top_rectangle (cr, 0, 0, (get_width() - 20.0)/2.0 , h, 8);
132                         cairo_fill_preserve (cr);
133                         
134                         Gtkmm2ext::rounded_top_rectangle (cr, (get_width() - 20.)/2.0 + 20.0, 0.0, 
135                                                           (get_width() - 20.0)/2.0 , h, 8);
136                         cairo_fill_preserve (cr);
137
138                         cairo_move_to (cr, get_width()/2.0 - 10.0, h/2.0);
139                         cairo_set_line_width (cr, 1.5);
140                         cairo_rel_line_to (cr, 20.0, 0.0);
141                         cairo_set_source (cr, active_fill_pattern);
142                         cairo_stroke (cr);
143                 } else {
144                         cairo_arc (cr, get_width()/2.0, h/2.0, 6.0, 0, M_PI*2.0);
145                         cairo_set_line_width (cr, 1.5);
146                         cairo_fill_preserve (cr);
147                         cairo_set_source_rgb (cr, border_r, border_g, border_b);
148                         cairo_stroke (cr);              
149                 }
150         }
151 }
152
153 void
154 ButtonJoiner::on_size_allocate (Allocation& alloc)
155 {
156         CairoWidget::on_size_allocate (alloc);
157         set_colors ();
158 }
159
160 bool
161 ButtonJoiner::on_button_release_event (GdkEventButton*)
162 {
163         if (_action) {
164                 _action->activate ();
165         }
166
167         return true;
168 }
169
170 void
171 ButtonJoiner::on_size_request (Gtk::Requisition* r)
172 {
173         CairoWidget::on_size_request (r);
174 }
175
176 void
177 ButtonJoiner::set_related_action (Glib::RefPtr<Action> act)
178 {
179         Gtkmm2ext::Activatable::set_related_action (act);
180
181         if (_action) {
182
183                 action_tooltip_changed ();
184
185                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
186                 if (tact) {
187                         action_toggled ();
188                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ButtonJoiner::action_toggled));
189                 } 
190
191                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ButtonJoiner::action_sensitivity_changed));
192                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ButtonJoiner::action_visibility_changed));
193                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ButtonJoiner::action_tooltip_changed));
194         }
195 }
196
197 void
198 ButtonJoiner::action_sensitivity_changed ()
199 {
200         if (_action->property_sensitive ()) {
201                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
202         } else {
203                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
204         }
205         
206 }
207
208 void
209 ButtonJoiner::action_visibility_changed ()
210 {
211         if (_action->property_visible ()) {
212                 show ();
213         } else {
214                 hide ();
215         }
216 }
217
218 void
219 ButtonJoiner::action_tooltip_changed ()
220 {
221         std::string str = _action->property_tooltip().get_value();
222         ARDOUR_UI::instance()->set_tip (*this, str);
223 }
224
225 void
226 ButtonJoiner::action_toggled ()
227 {
228         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
229
230         if (tact) {
231                 set_active (tact->get_active());
232         }
233 }       
234
235 void
236 ButtonJoiner::set_active_state (Gtkmm2ext::ActiveState s)
237 {
238         bool changed = (_active_state != s);
239         CairoWidget::set_active_state (s);
240         if (changed) {
241                 set_colors ();
242         }
243 }
244
245 void
246 ButtonJoiner::set_colors ()
247 {
248         uint32_t start_color;
249         uint32_t end_color;
250         uint32_t r, g, b, a;
251
252         if (active_fill_pattern) {
253                 cairo_pattern_destroy (active_fill_pattern);
254                 cairo_pattern_destroy (inactive_fill_pattern);
255         }
256
257         active_fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
258         inactive_fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
259
260         start_color = UIConfiguration::instance().color (string_compose ("%1: fill start", name));
261         end_color = UIConfiguration::instance().color (string_compose ("%1: fill end", name));
262         UINT_TO_RGBA (start_color, &r, &g, &b, &a);
263         cairo_pattern_add_color_stop_rgba (inactive_fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
264         UINT_TO_RGBA (end_color, &r, &g, &b, &a);
265         cairo_pattern_add_color_stop_rgba (inactive_fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
266
267         start_color = UIConfiguration::instance().color (string_compose ("%1: fill start active", name));
268         end_color = UIConfiguration::instance().color (string_compose ("%1: fill end active", name));
269         UINT_TO_RGBA (start_color, &r, &g, &b, &a);
270         cairo_pattern_add_color_stop_rgba (active_fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
271         UINT_TO_RGBA (end_color, &r, &g, &b, &a);
272         cairo_pattern_add_color_stop_rgba (active_fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
273
274         queue_draw ();
275 }
276