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