Make menu option for speaker config the same as the window title.
[ardour.git] / gtk2_ardour / speaker_dialog.cc
1 /*
2     Copyright (C) 2011 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 "pbd/cartesian.h"
21
22 #include "gtkmm2ext/keyboard.h"
23
24 #include "speaker_dialog.h"
25
26 #include "i18n.h"
27
28 using namespace ARDOUR;
29 using namespace PBD;
30 using namespace std;
31 using namespace Gtk;
32 using namespace Gtkmm2ext;
33
34 SpeakerDialog::SpeakerDialog ()
35         : ArdourDialog (_("Speaker Configuration"))
36         , aspect_frame ("", 0.5, 0.5, 1.0, false)
37         , azimuth_adjustment (0, 0.0, 360.0, 10.0, 1.0)
38         , azimuth_spinner (azimuth_adjustment)
39         , add_speaker_button (_("Add Speaker"))
40         , use_system_button (_("Use System"))
41                               
42 {
43         
44         side_vbox.set_homogeneous (false);
45         side_vbox.set_border_width (9);
46         side_vbox.set_spacing (6);
47         side_vbox.pack_start (azimuth_spinner, false, false);
48         side_vbox.pack_start (add_speaker_button, false, false);
49         side_vbox.pack_start (use_system_button, false, false);
50
51         aspect_frame.set_size_request (200, 200);
52         aspect_frame.set_shadow_type (SHADOW_NONE);
53         aspect_frame.add (darea);
54
55         hbox.set_spacing (6);
56         hbox.set_border_width (6);
57         hbox.pack_start (aspect_frame, true, true);
58         hbox.pack_start (side_vbox, false, false);
59
60         get_vbox()->pack_start (hbox);
61         get_vbox()->show_all ();
62
63         darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
64
65         darea.signal_size_allocate().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_size_allocate));
66         darea.signal_expose_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_expose_event));
67         darea.signal_button_press_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_button_press_event));
68         darea.signal_button_release_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_button_release_event));
69         darea.signal_motion_notify_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_motion_notify_event));
70
71         drag_index = -1;
72 }
73
74 void
75 SpeakerDialog::set_speakers (boost::shared_ptr<Speakers> s) 
76 {
77         speakers = *s;
78 }
79
80 Speakers
81 SpeakerDialog::get_speakers () const
82 {
83         return speakers;
84 }
85
86 bool
87 SpeakerDialog::darea_expose_event (GdkEventExpose* event)
88 {
89         gint x, y;
90         cairo_t* cr;
91
92         cr = gdk_cairo_create (darea.get_window()->gobj());
93
94         cairo_set_line_width (cr, 1.0);
95
96         cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
97         cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
98         cairo_fill_preserve (cr);
99         cairo_clip (cr);
100
101         if (height > 100) {
102                 cairo_translate (cr, 10.0, 10.0);
103         }
104
105         /* horizontal line of "crosshairs" */
106
107         cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
108         cairo_move_to (cr, 0.5, height/2.0+0.5);
109         cairo_line_to (cr, width+0.5, height/2+0.5);
110         cairo_stroke (cr);
111
112         /* vertical line of "crosshairs" */
113         
114         cairo_move_to (cr, width/2+0.5, 0.5);
115         cairo_line_to (cr, width/2+0.5, height+0.5);
116         cairo_stroke (cr);
117
118         /* the circle on which signals live */
119
120         cairo_arc (cr, width/2, height/2, height/2, 0, 2.0 * M_PI);
121         cairo_stroke (cr);
122
123         float arc_radius;
124         
125         cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
126         
127         if (height < 100) {
128                 cairo_set_font_size (cr, 10);
129                 arc_radius = 2.0;
130         } else {
131                 cairo_set_font_size (cr, 16);
132                 arc_radius = 4.0;
133         }
134
135         uint32_t n = 0;
136         for (vector<Speaker>::iterator i = speakers.speakers().begin(); i != speakers.speakers().end(); ++i) {
137                 
138                 Speaker& s (*i);
139                 CartesianVector c (s.coords());
140                 
141                 cart_to_gtk (c);
142                 
143                 x = (gint) floor (c.x);
144                 y = (gint) floor (c.y);
145                 
146                 /* XXX need to shift circles so that they are centered on the circle */
147                 
148                 cairo_arc (cr, x, y, arc_radius, 0, 2.0 * M_PI);
149                 cairo_set_source_rgb (cr, 0.8, 0.2, 0.1);
150                 cairo_close_path (cr);
151                 cairo_fill (cr);
152                 
153                 cairo_move_to (cr, x + 6, y + 6);
154                 
155                 char buf[256];
156                 snprintf (buf, sizeof (buf), "%d:%d", n+1, (int) lrint (s.angles().azi));
157                 cairo_show_text (cr, buf);
158                 ++n;
159         }
160
161         cairo_destroy (cr);
162
163         return true;
164         
165 }
166
167 void
168 SpeakerDialog::cart_to_gtk (CartesianVector& c) const
169 {
170         /* "c" uses a coordinate space that is:
171             
172            center = 0.0
173            dimension = 2.0 * 2.0
174            so max values along each axis are -1..+1
175
176            GTK uses a coordinate space that is:
177
178            top left = 0.0
179            dimension = width * height
180            so max values along each axis are 0,width and
181            0,height
182         */
183
184         c.x = (width / 2) * (c.x + 1);
185         c.y = (height / 2) * (1 - c.y);
186
187         /* XXX z-axis not handled - 2D for now */
188 }
189
190 void
191 SpeakerDialog::gtk_to_cart (CartesianVector& c) const
192 {
193         c.x = (c.x / (width / 2.0)) - 1.0;
194         c.y = -((c.y / (height / 2.0)) - 1.0);
195
196         /* XXX z-axis not handled - 2D for now */
197 }
198
199 void
200 SpeakerDialog::clamp_to_circle (double& x, double& y)
201 {
202         double azi, ele;
203         double z = 0.0;
204         double l;
205
206         PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
207         PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
208 }
209
210 void
211 SpeakerDialog::darea_size_allocate (Gtk::Allocation& alloc)
212 {
213         width = alloc.get_width();
214         height = alloc.get_height();
215
216         if (height > 100) {
217                 width -= 20;
218                 height -= 20;
219         }
220 }
221
222 bool
223 SpeakerDialog::darea_button_press_event (GdkEventButton *ev)
224 {
225         GdkModifierType state;
226
227         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
228                 return false;
229         }
230
231         drag_index = -1;
232
233         switch (ev->button) {
234         case 1:
235         case 2:
236                 drag_index = find_closest_object (ev->x, ev->y);
237                 drag_x = (int) floor (ev->x);
238                 drag_y = (int) floor (ev->y);
239                 state = (GdkModifierType) ev->state;
240
241                 return handle_motion (drag_x, drag_y, state);
242                 break;
243
244         default:
245                 break;
246         }
247
248         return false;
249 }
250
251 bool
252 SpeakerDialog::darea_button_release_event (GdkEventButton *ev)
253 {
254         gint x, y;
255         GdkModifierType state;
256         bool ret = false;
257
258         switch (ev->button) {
259         case 1:
260                 x = (int) floor (ev->x);
261                 y = (int) floor (ev->y);
262                 state = (GdkModifierType) ev->state;
263
264                 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
265                         
266                         for (vector<Speaker>::iterator i = speakers.speakers().begin(); i != speakers.speakers().end(); ++i) {
267                                 /* XXX DO SOMETHING TO SET SPEAKER BACK TO "normal" */
268                         }
269
270                         queue_draw ();
271                         ret = true;
272
273                 } else {
274                         ret = handle_motion (x, y, state);
275                 }
276
277                 break;
278
279         case 2:
280                 x = (int) floor (ev->x);
281                 y = (int) floor (ev->y);
282                 state = (GdkModifierType) ev->state;
283
284                 ret = handle_motion (x, y, state);
285                 break;
286
287         case 3:
288                 break;
289
290         }
291         
292         drag_index = -1;
293
294         return ret;
295 }
296
297 int
298 SpeakerDialog::find_closest_object (gdouble x, gdouble y) 
299 {
300         float distance;
301         float best_distance = FLT_MAX;
302         int n = 0;
303         int which = -1;
304
305         for (vector<Speaker>::iterator i = speakers.speakers().begin(); i != speakers.speakers().end(); ++i, ++n) {
306
307                 Speaker& candidate (*i);
308                 CartesianVector c;
309         
310                 candidate.angles().cartesian (c);
311                 cart_to_gtk (c);
312
313                 distance = sqrt ((c.x - x) * (c.x - x) +
314                                  (c.y - y) * (c.y - y));
315
316
317                 if (distance < best_distance) {
318                         best_distance = distance;
319                         which = n;
320                 }
321         }
322
323         if (best_distance > 20) { // arbitrary 
324                 return -1;
325         }
326
327         return which;
328 }
329
330 bool
331 SpeakerDialog::darea_motion_notify_event (GdkEventMotion *ev)
332 {
333         gint x, y;
334         GdkModifierType state;
335
336         if (ev->is_hint) {
337                 gdk_window_get_pointer (ev->window, &x, &y, &state);
338         } else {
339                 x = (int) floor (ev->x);
340                 y = (int) floor (ev->y);
341                 state = (GdkModifierType) ev->state;
342         }
343
344         return handle_motion (x, y, state);
345 }
346
347 bool
348 SpeakerDialog::handle_motion (gint evx, gint evy, GdkModifierType state)
349 {
350         if (drag_index < 0) {
351                 return false;
352         }
353
354         if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
355                 return false;
356         }
357
358
359         if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
360                 CartesianVector c;
361                 bool need_move = false;
362                 Speaker& moving (speakers.speakers()[drag_index]);
363
364                 moving.angles().cartesian (c);
365                 cart_to_gtk (c);
366
367                 if ((evx != c.x) || (evy != c.y)) {
368                         need_move = true;
369                 }
370
371                 if (need_move) {
372                         CartesianVector cp (evx, evy, 0.0);
373
374                         /* canonicalize position */
375
376                         gtk_to_cart (cp);
377
378                         /* position actual signal on circle */
379
380                         clamp_to_circle (cp.x, cp.y);
381                         
382                         /* generate an angular representation and set drag target (GUI) position */
383
384                         AngularVector a;
385
386                         cp.angular (a);
387
388                         moving.move (a);
389
390                         queue_draw ();
391                 }
392         } 
393
394         return true;
395 }