Make speaker dialogue manage the session's Speakers. Make azimuth control work....
[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 #include "gui_thread.h"
26
27 #include "i18n.h"
28
29 using namespace ARDOUR;
30 using namespace PBD;
31 using namespace std;
32 using namespace Gtk;
33 using namespace Gtkmm2ext;
34
35 SpeakerDialog::SpeakerDialog ()
36         : ArdourDialog (_("Speaker Configuration"))
37         , aspect_frame ("", 0.5, 0.5, 1.0, false)
38         , azimuth_adjustment (0, 0.0, 360.0, 10.0, 1.0)
39         , azimuth_spinner (azimuth_adjustment)
40         , add_speaker_button (_("Add Speaker"))
41         , remove_speaker_button (_("Remove Speaker"))
42         /* initialize to 0 so that set_selected works below */
43         , selected_index (0)
44         , ignore_speaker_position_change (false)
45         , ignore_azimuth_change (false)
46 {
47         side_vbox.set_homogeneous (false);
48         side_vbox.set_border_width (6);
49         side_vbox.set_spacing (6);
50         side_vbox.pack_start (add_speaker_button, false, false);
51
52         aspect_frame.set_size_request (200, 200);
53         aspect_frame.set_shadow_type (SHADOW_NONE);
54         aspect_frame.add (darea);
55
56         hbox.set_spacing (6);
57         hbox.set_border_width (6);
58         hbox.pack_start (aspect_frame, true, true);
59         hbox.pack_start (side_vbox, true, true);
60
61         HBox* current_speaker_hbox = manage (new HBox);
62         current_speaker_hbox->set_spacing (4);
63         current_speaker_hbox->pack_start (*manage (new Label (_("Azimuth:"))), false, false);
64         current_speaker_hbox->pack_start (azimuth_spinner, true, true);
65         current_speaker_hbox->pack_start (remove_speaker_button, true, true);
66
67         get_vbox()->pack_start (hbox);
68         get_vbox()->pack_start (*current_speaker_hbox, true, true);
69         get_vbox()->show_all ();
70
71         darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
72
73         darea.signal_size_allocate().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_size_allocate));
74         darea.signal_expose_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_expose_event));
75         darea.signal_button_press_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_button_press_event));
76         darea.signal_button_release_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_button_release_event));
77         darea.signal_motion_notify_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_motion_notify_event));
78
79         add_speaker_button.signal_clicked().connect (sigc::mem_fun (*this, &SpeakerDialog::add_speaker));
80         remove_speaker_button.signal_clicked().connect (sigc::mem_fun (*this, &SpeakerDialog::remove_speaker));
81         azimuth_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &SpeakerDialog::azimuth_changed));
82
83         drag_index = -1;
84
85         /* selected index initialised to 0 above; this will set `no selection' and
86            sensitize widgets accordingly.
87         */
88         set_selected (-1);
89 }
90
91 void
92 SpeakerDialog::set_speakers (boost::shared_ptr<Speakers> s) 
93 {
94         _speakers = s;
95 }
96
97 boost::shared_ptr<Speakers>
98 SpeakerDialog::get_speakers () const
99 {
100         return _speakers.lock ();
101 }
102
103 bool
104 SpeakerDialog::darea_expose_event (GdkEventExpose* event)
105 {
106         boost::shared_ptr<Speakers> speakers = _speakers.lock ();
107         if (!speakers) {
108                 return false;
109         }
110
111         gint x, y;
112         cairo_t* cr;
113
114         cr = gdk_cairo_create (darea.get_window()->gobj());
115
116         cairo_set_line_width (cr, 1.0);
117
118         cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
119         cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
120         cairo_fill_preserve (cr);
121         cairo_clip (cr);
122
123         cairo_translate (cr, x_origin, y_origin);
124
125         /* horizontal line of "crosshairs" */
126
127         cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
128         cairo_move_to (cr, 0.5, height/2.0+0.5);
129         cairo_line_to (cr, width+0.5, height/2+0.5);
130         cairo_stroke (cr);
131
132         /* vertical line of "crosshairs" */
133         
134         cairo_move_to (cr, width/2+0.5, 0.5);
135         cairo_line_to (cr, width/2+0.5, height+0.5);
136         cairo_stroke (cr);
137
138         /* the circle on which signals live */
139
140         cairo_arc (cr, width/2, height/2, height/2, 0, 2.0 * M_PI);
141         cairo_stroke (cr);
142
143         float arc_radius;
144         
145         cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
146         
147         if (height < 100) {
148                 cairo_set_font_size (cr, 10);
149                 arc_radius = 2.0;
150         } else {
151                 cairo_set_font_size (cr, 16);
152                 arc_radius = 4.0;
153         }
154
155         int n = 0;
156         for (vector<Speaker>::iterator i = speakers->speakers().begin(); i != speakers->speakers().end(); ++i) {
157                 
158                 Speaker& s (*i);
159                 CartesianVector c (s.coords());
160                 
161                 cart_to_gtk (c);
162                 
163                 x = (gint) floor (c.x);
164                 y = (gint) floor (c.y);
165                 
166                 /* XXX need to shift circles so that they are centered on the circle */
167                 
168                 cairo_arc (cr, x, y, arc_radius, 0, 2.0 * M_PI);
169                 if (selected_index == n) {
170                         cairo_set_source_rgb (cr, 0.8, 0.8, 0.2);
171                 } else {
172                         cairo_set_source_rgb (cr, 0.8, 0.2, 0.1);
173                 }
174                 cairo_close_path (cr);
175                 cairo_fill (cr);
176                 
177                 cairo_move_to (cr, x + 6, y + 6);
178                 
179                 char buf[256];
180                 snprintf (buf, sizeof (buf), "%d:%d", n+1, (int) lrint (s.angles().azi));
181                 cairo_show_text (cr, buf);
182                 ++n;
183         }
184
185         cairo_destroy (cr);
186
187         return true;
188         
189 }
190
191 void
192 SpeakerDialog::cart_to_gtk (CartesianVector& c) const
193 {
194         /* "c" uses a coordinate space that is:
195             
196            center = 0.0
197            dimension = 2.0 * 2.0
198            so max values along each axis are -1..+1
199
200            GTK uses a coordinate space that is:
201
202            top left = 0.0
203            dimension = width * height
204            so max values along each axis are 0,width and
205            0,height
206         */
207
208         c.x = (width / 2) * (c.x + 1);
209         c.y = (height / 2) * (1 - c.y);
210
211         /* XXX z-axis not handled - 2D for now */
212 }
213
214 void
215 SpeakerDialog::gtk_to_cart (CartesianVector& c) const
216 {
217         c.x = (c.x / (width / 2.0)) - 1.0;
218         c.y = -((c.y / (height / 2.0)) - 1.0);
219
220         /* XXX z-axis not handled - 2D for now */
221 }
222
223 void
224 SpeakerDialog::clamp_to_circle (double& x, double& y)
225 {
226         double azi, ele;
227         double z = 0.0;
228         double l;
229
230         PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
231         PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
232 }
233
234 void
235 SpeakerDialog::darea_size_allocate (Gtk::Allocation& alloc)
236 {
237         width = alloc.get_width();
238         height = alloc.get_height();
239
240         if (height > 100) {
241                 width -= 20;
242                 height -= 20;
243         }
244
245         x_origin = (alloc.get_width() - width) / 2;
246         y_origin = (alloc.get_height() - height) / 2;
247 }
248
249 bool
250 SpeakerDialog::darea_button_press_event (GdkEventButton *ev)
251 {
252         boost::shared_ptr<Speakers> speakers = _speakers.lock ();
253         if (!speakers) {
254                 return false;
255         }
256         
257         GdkModifierType state;
258
259         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
260                 return false;
261         }
262
263         drag_index = -1;
264
265         switch (ev->button) {
266         case 1:
267         case 2:
268         {
269                 int const index = find_closest_object (ev->x, ev->y);
270                 set_selected (index);
271
272                 drag_index = index;
273                 int const drag_x = (int) floor (ev->x);
274                 int const drag_y = (int) floor (ev->y);
275                 state = (GdkModifierType) ev->state;
276
277                 if (drag_index >= 0) {
278                         CartesianVector c;
279                         speakers->speakers()[drag_index].angles().cartesian (c);
280                         cart_to_gtk (c);
281                         drag_offset_x = drag_x - x_origin - c.x;
282                         drag_offset_y = drag_y - y_origin - c.y;
283                 }
284
285                 return handle_motion (drag_x, drag_y, state);
286                 break;
287         }
288
289         default:
290                 break;
291         }
292
293         return false;
294 }
295
296 bool
297 SpeakerDialog::darea_button_release_event (GdkEventButton *ev)
298 {
299         boost::shared_ptr<Speakers> speakers = _speakers.lock ();
300         if (!speakers) {
301                 return false;
302         }
303         
304         gint x, y;
305         GdkModifierType state;
306         bool ret = false;
307
308         switch (ev->button) {
309         case 1:
310                 x = (int) floor (ev->x);
311                 y = (int) floor (ev->y);
312                 state = (GdkModifierType) ev->state;
313
314                 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
315                         
316                         for (vector<Speaker>::iterator i = speakers->speakers().begin(); i != speakers->speakers().end(); ++i) {
317                                 /* XXX DO SOMETHING TO SET SPEAKER BACK TO "normal" */
318                         }
319
320                         queue_draw ();
321                         ret = true;
322
323                 } else {
324                         ret = handle_motion (x, y, state);
325                 }
326
327                 break;
328
329         case 2:
330                 x = (int) floor (ev->x);
331                 y = (int) floor (ev->y);
332                 state = (GdkModifierType) ev->state;
333
334                 ret = handle_motion (x, y, state);
335                 break;
336
337         case 3:
338                 break;
339
340         }
341         
342         drag_index = -1;
343
344         return ret;
345 }
346
347 int
348 SpeakerDialog::find_closest_object (gdouble x, gdouble y) 
349 {
350         boost::shared_ptr<Speakers> speakers = _speakers.lock ();
351         if (!speakers) {
352                 return -1;
353         }
354
355         float distance;
356         float best_distance = FLT_MAX;
357         int n = 0;
358         int which = -1;
359
360         for (vector<Speaker>::iterator i = speakers->speakers().begin(); i != speakers->speakers().end(); ++i, ++n) {
361
362                 Speaker& candidate (*i);
363                 CartesianVector c;
364         
365                 candidate.angles().cartesian (c);
366                 cart_to_gtk (c);
367
368                 distance = sqrt ((c.x - x) * (c.x - x) +
369                                  (c.y - y) * (c.y - y));
370
371
372                 if (distance < best_distance) {
373                         best_distance = distance;
374                         which = n;
375                 }
376         }
377
378         if (best_distance > 20) { // arbitrary 
379                 return -1;
380         }
381
382         return which;
383 }
384
385 bool
386 SpeakerDialog::darea_motion_notify_event (GdkEventMotion *ev)
387 {
388         gint x, y;
389         GdkModifierType state;
390
391         if (ev->is_hint) {
392                 gdk_window_get_pointer (ev->window, &x, &y, &state);
393         } else {
394                 x = (int) floor (ev->x);
395                 y = (int) floor (ev->y);
396                 state = (GdkModifierType) ev->state;
397         }
398
399         return handle_motion (x, y, state);
400 }
401
402 bool
403 SpeakerDialog::handle_motion (gint evx, gint evy, GdkModifierType state)
404 {
405         boost::shared_ptr<Speakers> speakers = _speakers.lock ();
406         if (!speakers) {
407                 return false;
408         }
409
410         if (drag_index < 0) {
411                 return false;
412         }
413
414         if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
415                 return false;
416         }
417
418         /* correct event coordinates to have their origin at the corner of our graphic
419            rather than the corner of our allocation */
420
421         double obx = evx - x_origin;
422         double oby = evy - y_origin;
423
424         /* and compensate for any distance between the mouse pointer and the centre
425            of the object being dragged */
426
427         obx -= drag_offset_x;
428         oby -= drag_offset_y;
429
430         if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
431                 CartesianVector c;
432                 bool need_move = false;
433                 Speaker& moving (speakers->speakers()[drag_index]);
434
435                 moving.angles().cartesian (c);
436                 cart_to_gtk (c);
437
438                 if (obx != c.x || oby != c.y) {
439                         need_move = true;
440                 }
441
442                 if (need_move) {
443                         CartesianVector cp (obx, oby, 0.0);
444
445                         /* canonicalize position */
446
447                         gtk_to_cart (cp);
448
449                         /* position actual signal on circle */
450
451                         clamp_to_circle (cp.x, cp.y);
452                         
453                         /* generate an angular representation and set drag target (GUI) position */
454
455                         AngularVector a;
456
457                         cp.angular (a);
458
459                         moving.move (a);
460
461                         queue_draw ();
462                 }
463         } 
464
465         return true;
466 }
467
468 void
469 SpeakerDialog::add_speaker ()
470 {
471         boost::shared_ptr<Speakers> speakers = _speakers.lock ();
472         if (!speakers) {
473                 return;
474         }
475         
476         speakers->add_speaker (PBD::AngularVector (0, 0, 0));
477         queue_draw ();
478 }
479
480 void
481 SpeakerDialog::set_selected (int i)
482 {
483         boost::shared_ptr<Speakers> speakers = _speakers.lock ();
484         if (!speakers) {
485                 return;
486         }
487         
488         if (i == selected_index) {
489                 return;
490         }
491
492         selected_index = i;
493         queue_draw ();
494
495         selected_speaker_connection.disconnect ();
496         
497         azimuth_spinner.set_sensitive (selected_index != -1);
498         remove_speaker_button.set_sensitive (selected_index != -1);
499         
500         if (selected_index != -1) {
501                 azimuth_adjustment.set_value (speakers->speakers()[selected_index].angles().azi);
502                 speakers->speakers()[selected_index].PositionChanged.connect (
503                         selected_speaker_connection, MISSING_INVALIDATOR,
504                         boost::bind (&SpeakerDialog::speaker_position_changed, this),
505                         gui_context ()
506                         );
507         }
508 }
509
510 void
511 SpeakerDialog::azimuth_changed ()
512 {
513         boost::shared_ptr<Speakers> speakers = _speakers.lock ();
514         if (!speakers) {
515                 return;
516         }
517
518         assert (selected_index != -1);
519
520         if (ignore_azimuth_change) {
521                 return;
522         }
523         
524         ignore_speaker_position_change = true;
525         speakers->move_speaker (speakers->speakers()[selected_index].id, PBD::AngularVector (azimuth_adjustment.get_value (), 0, 0));
526         ignore_speaker_position_change = false;
527
528         queue_draw ();
529 }
530
531 void
532 SpeakerDialog::speaker_position_changed ()
533 {
534         boost::shared_ptr<Speakers> speakers = _speakers.lock ();
535         if (!speakers) {
536                 return;
537         }
538         
539         assert (selected_index != -1);
540
541         if (ignore_speaker_position_change) {
542                 return;
543         }
544         
545         ignore_azimuth_change = true;
546         azimuth_adjustment.set_value (speakers->speakers()[selected_index].angles().azi);
547         ignore_azimuth_change = false;
548
549         queue_draw ();
550 }
551                                            
552 void
553 SpeakerDialog::remove_speaker ()
554 {
555         boost::shared_ptr<Speakers> speakers = _speakers.lock ();
556         if (!speakers) {
557                 return;
558         }
559         
560         assert (selected_index != -1);
561
562         speakers->remove_speaker (speakers->speakers()[selected_index].id);
563         set_selected (-1);
564         
565         queue_draw ();
566 }