Fix a panner crash when reducing a route's output count down to 2. Remove some unuse...
[ardour.git] / gtk2_ardour / panner2d.cc
1 /*
2     Copyright (C) 2002 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 <cmath>
21 #include <climits>
22 #include <string.h>
23
24 #include <cairo.h>
25 #include <gtkmm/menu.h>
26
27 #include "pbd/error.h"
28 #include "ardour/panner.h"
29 #include <gtkmm2ext/gtk_ui.h>
30
31 #include "panner2d.h"
32 #include "keyboard.h"
33 #include "gui_thread.h"
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace Gtk;
39 using namespace ARDOUR;
40 using namespace PBD;
41 using Gtkmm2ext::Keyboard;
42
43 Panner2d::Target::Target (float xa, float ya, const char *txt)
44         : x (xa, 0.0, 1.0, 0.01, 0.1)
45         , y (ya, 0.0, 1.0, 0.01, 0.1)
46         , azimuth (M_PI/2.0, 0.0, 2.0 * M_PI, 0.1, 0.5)
47         , text (txt ? strdup (txt) : 0)
48 {
49         azimuth.set_value ((random() / (double) INT_MAX) * (2.0 * M_PI));
50 }
51
52 Panner2d::Target::~Target ()
53 {
54         if (text) {
55                 free (text);
56         }
57 }
58
59 void
60 Panner2d::Target::set_text (const char* txt)
61 {
62         if (text) {
63                 free (text);
64         }
65         text = strdup (txt);
66 }
67
68 Panner2d::Panner2d (boost::shared_ptr<Panner> p, int32_t h)
69         : panner (p), width (0), height (h)
70 {
71         allow_x = false;
72         allow_y = false;
73         allow_target = false;
74
75         panner->StateChanged.connect (state_connection, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
76         panner->Changed.connect (change_connection, invalidator (*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
77
78         drag_target = 0;
79         set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
80 }
81
82 Panner2d::~Panner2d()
83 {
84         for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
85                 delete *i;
86         }
87 }
88
89 void
90 Panner2d::reset (uint32_t n_inputs)
91 {
92         Targets::size_type existing_pucks = pucks.size();
93
94         /* pucks */
95
96         while (pucks.size() < n_inputs) {
97                 add_puck ("", 0.0, 0.0);
98         }
99
100         if (pucks.size() > n_inputs) {
101                 for (uint32_t i = pucks.size(); i < n_inputs; ++i) {
102                         delete pucks[i];
103                 }
104
105                 pucks.resize (n_inputs);
106         }
107                                                 
108         for (Targets::iterator x = pucks.begin(); x != pucks.end(); ++x) {
109                 (*x)->visible = false;
110         }
111
112         switch (n_inputs) {
113         case 0:
114                 break;
115
116         case 1:
117                 pucks[0]->set_text ("");
118                 break;
119
120         case 2:
121                 pucks[0]->set_text ("R");
122                 pucks[1]->set_text ("L");
123                 break;
124
125         default:
126                 for (uint32_t i = 0; i < n_inputs; ++i) {
127                         char buf[64];
128                         snprintf (buf, sizeof (buf), "%" PRIu32, i);
129                         pucks[i]->set_text (buf);
130                 }
131                 break;
132         }
133
134         for (uint32_t i = existing_pucks; i < n_inputs; ++i) {
135                 float x, y;
136                 panner->streampanner (i).get_position (x, y);
137                 pucks[i]->x.set_value (x);
138                 pucks[i]->y.set_value (y);
139                 pucks[i]->visible = true;
140         }
141
142         /* add all outputs */
143
144         while (targets.size() < panner->nouts()) {
145                 add_target (0.0, 0.0);
146         }
147
148         if (targets.size() > panner->nouts()) {
149                 for (uint32_t i = panner->nouts(); i < targets.size(); ++i) {
150                         delete targets[i];
151                 }
152
153                 targets.resize (panner->nouts ());
154         }
155
156         for (Targets::iterator x = targets.begin(); x != targets.end(); ++x) {
157                 (*x)->visible = false;
158         }
159
160         for (uint32_t n = 0; n < panner->nouts(); ++n) {
161                 char buf[16];
162
163                 snprintf (buf, sizeof (buf), "%d", n+1);
164                 targets[n]->set_text (buf);
165                 targets[n]->x.set_value (panner->output(n).x);
166                 targets[n]->y.set_value (panner->output(n).y);
167                 targets[n]->visible = true;
168         }
169
170         allow_x_motion (true);
171         allow_y_motion (true);
172         allow_target_motion (true);
173
174         queue_draw ();
175 }
176
177 Gtk::Adjustment&
178 Panner2d::azimuth (uint32_t which)
179 {
180         assert (which < pucks.size());
181         return pucks[which]->azimuth;
182 }
183
184 void
185 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
186 {
187         width = alloc.get_width();
188         height = alloc.get_height();
189
190         if (height > 100) {
191                 width -= 20;
192                 height -= 20;
193         }
194
195         DrawingArea::on_size_allocate (alloc);
196 }
197
198 int
199 Panner2d::add_puck (const char* text, float x, float y)
200 {
201         Target* puck = new Target (x, y, text);
202         pucks.push_back (puck);
203         puck->visible = true;
204
205         return 0;
206 }
207
208 int
209 Panner2d::add_target (float x, float y)
210 {
211         Target* target = new Target (x, y, "");
212         targets.push_back (target);
213         target->visible = true;
214         queue_draw ();
215
216         return targets.size() - 1;
217 }
218
219 void
220 Panner2d::handle_state_change ()
221 {
222         ENSURE_GUI_THREAD (*this, &Panner2d::handle_state_change)
223
224         queue_draw ();
225 }
226
227 void
228 Panner2d::handle_position_change ()
229 {
230         uint32_t n;
231         ENSURE_GUI_THREAD (*this, &Panner2d::handle_position_change)
232
233         for (n = 0; n < pucks.size(); ++n) {
234                 float x, y;
235                 panner->streampanner(n).get_position (x, y);
236                 pucks[n]->x.set_value (x);
237                 pucks[n]->y.set_value (y);
238         }
239
240         for (n = 0; n < targets.size(); ++n) {
241                 targets[n]->x.set_value (panner->output(n).x);
242                 targets[n]->y.set_value (panner->output(n).y);
243         }
244
245         queue_draw ();
246 }
247
248 void
249 Panner2d::move_puck (int which, float x, float y)
250 {
251         if (which >= int (targets.size())) {
252                 return;
253         }
254         
255         targets[which]->x.set_value (x);
256         targets[which]->y.set_value (y);
257         queue_draw ();
258 }
259
260 Panner2d::Target *
261 Panner2d::find_closest_object (gdouble x, gdouble y, int& which, bool& is_puck) const
262 {
263         gdouble efx, efy;
264         gdouble cx, cy;
265         Target *closest = 0;
266         Target *candidate;
267         float distance;
268         float best_distance = FLT_MAX;
269         int pwhich;
270
271         efx = x/width;
272         efy = y/height;
273         which = 0;
274         pwhich = 0;
275         is_puck = false;
276
277         for (Targets::const_iterator i = targets.begin(); i != targets.end(); ++i, ++which) {
278                 candidate = *i;
279
280                 cx = candidate->x.get_value();
281                 cy = candidate->y.get_value();
282
283                 distance = sqrt ((cx - efx) * (cx - efx) +
284                                  (cy - efy) * (cy - efy));
285
286                 if (distance < best_distance) {
287                         closest = candidate;
288                         best_distance = distance;
289                 }
290         }
291
292         for (Targets::const_iterator i = pucks.begin(); i != pucks.end(); ++i, ++pwhich) {
293                 candidate = *i;
294
295                 cx = candidate->x.get_value();
296                 cy = candidate->y.get_value();
297
298                 distance = sqrt ((cx - efx) * (cx - efx) +
299                                  (cy - efy) * (cy - efy));
300
301                 if (distance < best_distance) {
302                         closest = candidate;
303                         best_distance = distance;
304                         is_puck = true;
305                         which = pwhich;
306                 }
307         }
308
309         return closest;
310 }
311
312 bool
313 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
314 {
315         gint x, y;
316         GdkModifierType state;
317
318         if (ev->is_hint) {
319                 gdk_window_get_pointer (ev->window, &x, &y, &state);
320         } else {
321                 x = (int) floor (ev->x);
322                 y = (int) floor (ev->y);
323                 state = (GdkModifierType) ev->state;
324         }
325
326         return handle_motion (x, y, state);
327 }
328 bool
329 Panner2d::on_expose_event (GdkEventExpose *event)
330 {
331         gint x, y;
332         float fx, fy;
333         cairo_t* cr;
334
335         cr = gdk_cairo_create (get_window()->gobj());
336
337         cairo_set_line_width (cr, 1.0);
338
339         cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
340         if (!panner->bypassed()) {
341                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
342         } else {
343                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 0.2);
344         }
345         cairo_fill_preserve (cr);
346         cairo_clip (cr);
347
348         if (height > 100) {
349                 cairo_translate (cr, 10.0, 10.0);
350         }
351
352         cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
353         cairo_move_to (cr, 0.5, height/2.0+0.5);
354         cairo_line_to (cr, height+0.5, height/2+0.5);
355         cairo_stroke (cr);
356
357         cairo_move_to (cr, height/2+0.5, 0.5);
358         cairo_line_to (cr, height/2+0.5, height+0.5);
359         cairo_stroke (cr);
360
361         cairo_arc (cr, height/2, height/2, height/2, 0, 2.0 * M_PI);
362         cairo_stroke (cr);
363
364         if (!panner->bypassed()) {
365                 float arc_radius;
366
367                 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
368
369                 if (height < 100) {
370                         cairo_set_font_size (cr, 10);
371                         arc_radius = 2.0;
372                 } else {
373                         cairo_set_font_size (cr, 16);
374                         arc_radius = 4.0;
375                 }
376
377                 for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
378
379                         Target* puck = *i;
380
381                         if (puck->visible) {
382                                 /* redraw puck */
383
384                                 fx = min (puck->x.get_value(), 1.0);
385                                 fx = max (fx, -1.0f);
386                                 x = (gint) floor (width * fx - 4);
387
388                                 fy = min (puck->y.get_value(), 1.0);
389                                 fy = max (fy, -1.0f);
390                                 y = (gint) floor (height * fy - 4);
391
392                                 cairo_arc (cr, x, y, arc_radius, 0, 2.0 * M_PI);
393                                 cairo_set_source_rgb (cr, 0.8, 0.2, 0.1);
394                                 cairo_close_path (cr);
395                                 cairo_fill (cr);
396
397                                 /* arrow */
398
399                                 if (height > 100.0f) {
400
401                                         float endx, endy;
402                                         endx = x;
403                                         endy = y;
404
405                                         cairo_save (cr);
406                                         cairo_translate (cr, x, y);
407                                         cairo_rotate (cr, puck->azimuth.get_value());
408
409                                         /* horizontal left-to-right line (rotation will rotate it, duh) */
410
411                                         endx = 30.0;
412                                         endy = 0.0;
413
414                                         /* stem */
415                                         cairo_set_line_width (cr, 4.0);
416                                         cairo_move_to (cr, 0.0, 0.0);
417                                         cairo_line_to (cr, endx, endy);
418                                         cairo_stroke (cr);
419
420                                         /* arrow head */
421
422                                         cairo_move_to (cr, endx - 10.0, endy + 10.0);
423                                         cairo_line_to (cr, endx, endy);
424                                         cairo_line_to (cr, endx - 10.0, endy - 10.0);
425                                         cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
426                                         cairo_stroke (cr);
427
428                                         cairo_restore (cr);
429                                 }
430
431                                 cairo_move_to (cr, x + 6, y + 6);
432                                 cairo_show_text (cr, puck->text);
433                         }
434                 }
435
436                 /* redraw any visible targets */
437
438                 int n = 0;
439
440                 for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
441                         Target *target = *i;
442                         char buf[256];
443                         ++n;
444
445                         if (target->visible) {
446
447                                 fx = min (target->x.get_value(), 1.0);
448                                 fx = max (fx, -1.0f);
449                                 x = (gint) floor (width  * fx);
450
451                                 fy = min (target->y.get_value(), 1.0);
452                                 fy = max (fy, -1.0f);
453                                 y = (gint) floor (height * fy);
454
455                                 snprintf (buf, sizeof (buf), "%d", n);
456
457                                 cairo_set_source_rgb (cr, 0.0, 0.8, 0.1);
458                                 cairo_rectangle (cr, x-2, y-2, 4, 4);
459                                 cairo_fill (cr);
460                                 cairo_move_to (cr, x+6, y+6);
461                                 cairo_show_text (cr, buf);
462                         }
463                 }
464         }
465
466         cairo_destroy (cr);
467
468         return TRUE;
469 }
470
471 bool
472 Panner2d::on_button_press_event (GdkEventButton *ev)
473 {
474         GdkModifierType state;
475
476         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
477                 return false;
478         }
479
480         switch (ev->button) {
481         case 1:
482         case 2:
483                 drag_target = find_closest_object (ev->x, ev->y, drag_index, drag_is_puck);
484                 drag_x = (int) floor (ev->x);
485                 drag_y = (int) floor (ev->y);
486                 state = (GdkModifierType) ev->state;
487
488                 return handle_motion (drag_x, drag_y, state);
489                 break;
490
491         default:
492                 break;
493         }
494
495         return FALSE;
496 }
497
498 bool
499 Panner2d::on_button_release_event (GdkEventButton *ev)
500 {
501         gint x, y;
502         GdkModifierType state;
503         bool ret = false;
504
505         switch (ev->button) {
506         case 1:
507                 x = (int) floor (ev->x);
508                 y = (int) floor (ev->y);
509                 state = (GdkModifierType) ev->state;
510
511                 if (drag_is_puck && (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier))) {
512
513
514                         for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
515                                 //Target* puck = i->second;
516
517                                 /* XXX DO SOMETHING TO SET PUCK BACK TO "normal" */
518                         }
519
520                         queue_draw ();
521                         PuckMoved (-1);
522                         ret = true;
523
524                 } else {
525                         ret = handle_motion (x, y, state);
526                 }
527
528                 drag_target = 0;
529                 break;
530
531         case 2:
532                 x = (int) floor (ev->x);
533                 y = (int) floor (ev->y);
534                 state = (GdkModifierType) ev->state;
535
536                 if (drag_is_puck && (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier))) {
537                         toggle_bypass ();
538                         ret = true;
539                 } else {
540                         ret = handle_motion (x, y, state);
541                 }
542
543                 drag_target = 0;
544                 break;
545
546         case 3:
547                 break;
548
549         }
550
551         return ret;
552 }
553
554 gint
555 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
556 {
557         if (drag_target == 0) {
558                 return false;
559         }
560
561         if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
562                 return false;
563         }
564
565         int x, y;
566         bool need_move = false;
567
568         if (!drag_is_puck && !allow_target) {
569                 cerr << "dip = " << drag_is_puck << " at = " << allow_target << endl;
570                 return true;
571         }
572
573         if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
574
575                 if (allow_x || !drag_is_puck) {
576                         float new_x;
577                         x = min (evx, width - 1);
578                         x = max (x, 0);
579                         new_x = (float) x / (width - 1);
580                         if (new_x != drag_target->x.get_value()) {
581                                 drag_target->x.set_value (new_x);
582                                 need_move = true;
583                         }
584                 }
585
586                 if (allow_y || drag_is_puck) {
587                         float new_y;
588                         y = min (evy, height - 1);
589                         y = max (y, 0);
590                         new_y = (float) y / (height - 1);
591                         if (new_y != drag_target->y.get_value()) {
592                                 drag_target->y.set_value (new_y);
593                                 need_move = true;
594                         }
595                 }
596
597                 if (need_move) {
598
599                         if (drag_is_puck) {
600
601                                 panner->streampanner(drag_index).set_position (
602                                                 drag_target->x.get_value(), drag_target->y.get_value(), false);
603
604                         } else {
605
606                                 TargetMoved (drag_index);
607                         }
608
609                         queue_draw ();
610                 }
611
612
613         } else if ((state & GDK_BUTTON2_MASK) && !(state & GDK_BUTTON1_MASK)) {
614
615                 if (!drag_is_puck) {
616                         return false;
617                 }
618
619                 int xdelta = drag_x - evx;
620                 int ydelta = drag_x - evy;
621
622                 drag_target->azimuth.set_value (drag_target->azimuth.get_value() + (2 * M_PI) * ((float)ydelta)/height * ((float) -xdelta)/height);
623                 queue_draw ();
624         }
625
626         return true;
627 }
628
629 void
630 Panner2d::toggle_bypass ()
631 {
632         panner->set_bypassed (!panner->bypassed());
633 }
634
635 void
636 Panner2d::allow_x_motion (bool yn)
637 {
638         allow_x = yn;
639 }
640
641 void
642 Panner2d::allow_target_motion (bool yn)
643 {
644         allow_target = yn;
645 }
646
647 void
648 Panner2d::allow_y_motion (bool yn)
649 {
650         allow_y = yn;
651 }
652
653 Panner2dWindow::Panner2dWindow (boost::shared_ptr<Panner> p, int32_t h, uint32_t inputs)
654         : widget (p, h)
655         , reset_button (_("Reset"))
656         , bypass_button (_("Bypass"))
657         , mute_button (_("Mute"))
658 {
659         widget.set_name ("MixerPanZone");
660
661         set_title (_("Panner"));
662         widget.set_size_request (h, h);
663
664         button_box.set_spacing (6);
665         button_box.pack_start (reset_button, false, false);
666         button_box.pack_start (bypass_button, false, false);
667         button_box.pack_start (mute_button, false, false);
668
669         spinner_box.set_spacing (6);
670         left_side.set_spacing (6);
671
672         left_side.pack_start (button_box, false, false);
673         left_side.pack_start (spinner_box, false, false);
674
675         reset_button.show ();
676         bypass_button.show ();
677         mute_button.show ();
678         button_box.show ();
679         spinner_box.show ();
680         left_side.show ();
681
682         hpacker.set_spacing (6);
683         hpacker.set_border_width (12);
684         hpacker.pack_start (widget, false, false);
685         hpacker.pack_start (left_side, false, false);
686         hpacker.show ();
687
688         add (hpacker);
689         reset (inputs);
690         widget.show ();
691 }
692
693 void
694 Panner2dWindow::reset (uint32_t n_inputs)
695 {
696         widget.reset (n_inputs);
697
698         while (spinners.size() < n_inputs) {
699                 spinners.push_back (new Gtk::SpinButton (widget.azimuth (spinners.size())));
700                 spinner_box.pack_start (*spinners.back(), false, false);
701                 spinners.back()->set_digits (4);
702                 spinners.back()->show ();
703         }
704
705         while (spinners.size() > n_inputs) {
706                 spinner_box.remove (*spinners.back());
707                 delete spinners.back();
708                 spinners.erase (--spinners.end());
709         }
710 }