Preliminary MIDI plugin support.
[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 sigc;
40 using namespace ARDOUR;
41 using namespace PBD;
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 (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 (mem_fun(*this, &Panner2d::handle_state_change));
76         panner.Changed.connect (mem_fun(*this, &Panner2d::handle_position_change));
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->second;
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         while (pucks.size() > n_inputs) {
101                 pucks.erase (pucks.begin());
102         }
103
104         for (Targets::iterator x = pucks.begin(); x != pucks.end(); ++x) {
105                 (*x).second->visible = false;
106         }
107
108         switch (n_inputs) {
109         case 0:
110                 break;
111                 
112         case 1:
113                 pucks[0]->set_text ("");
114                 pucks[0]->x.set_value (0.0);
115                 pucks[0]->y.set_value (0.5);
116                 pucks[0]->visible = true;
117                 break;
118                 
119         case 2:
120                 pucks[0]->set_text ("R");
121                 pucks[0]->visible = true;
122                 pucks[1]->set_text ("L");
123                 if (existing_pucks < 2) {
124                         pucks[1]->x.set_value (0.25f);
125                         pucks[1]->y.set_value (0.5f);
126                 }
127                 pucks[1]->visible = true;
128                 break;
129                 
130         default:
131                 for (uint32_t i = 0; i < n_inputs; ++i) {
132                         char buf[64];
133                         snprintf (buf, sizeof (buf), "%" PRIu32, i);
134                         pucks[i]->set_text (buf);
135                         
136                         if (existing_pucks < i) {
137                                 float x, y;
138                                 panner.streampanner (i).get_position (x, y);
139                                 pucks[i]->x.set_value (x);
140                                 pucks[i]->y.set_value (y);
141                         }
142
143                         pucks[i]->visible = true;
144                 }
145                 break;
146         }
147         
148         /* add all outputs */
149         
150         while (targets.size() < panner.nouts()) {
151                 add_target (0.0, 0.0);
152         }
153         
154         while (targets.size() > panner.nouts()) {
155                 targets.erase (targets.begin());
156         }
157
158         for (Targets::iterator x = targets.begin(); x != targets.end(); ++x) {
159                 (*x).second->visible = false;
160         }
161
162         for (uint32_t n = 0; n < panner.nouts(); ++n) {
163                 char buf[16];
164
165                 snprintf (buf, sizeof (buf), "%d", n+1);
166                 targets[n]->set_text (buf);
167                 targets[n]->x.set_value (panner.output(n).x);
168                 targets[n]->y.set_value (panner.output(n).y);
169                 targets[n]->visible = true;
170         }
171         
172         allow_x_motion (true);
173         allow_y_motion (true);
174         allow_target_motion (true);
175
176         queue_draw ();
177 }
178
179 Gtk::Adjustment&
180 Panner2d::azimuth (uint32_t which)
181 {
182         assert (which < pucks.size());
183         return pucks[which]->azimuth;
184 }
185
186 void
187 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
188 {
189         width = alloc.get_width();
190         height = alloc.get_height();
191
192         if (height > 100) {
193                 width -= 20;
194                 height -= 20;
195         }
196
197         DrawingArea::on_size_allocate (alloc);
198 }
199
200 int
201 Panner2d::add_puck (const char* text, float x, float y)
202 {
203         Target* puck = new Target (x, y, text);
204
205         pair<int,Target *> newpair;
206         newpair.first = pucks.size();
207         newpair.second = puck;
208
209         pucks.insert (newpair);
210         puck->visible = true;
211         
212         return 0;
213 }
214
215 int
216 Panner2d::add_target (float x, float y)
217 {
218         Target *target = new Target (x, y, "");
219
220         pair<int,Target *> newpair;
221         newpair.first = targets.size();
222         newpair.second = target;
223
224         targets.insert (newpair);
225         target->visible = true;
226         queue_draw ();
227
228         return newpair.first;
229 }
230
231 void
232 Panner2d::drop_targets ()
233 {
234         for (Targets::iterator i = targets.begin(); i != targets.end(); ) {
235
236                 Targets::iterator tmp;
237
238                 tmp = i;
239                 ++tmp;
240
241                 delete i->second;
242                 targets.erase (i);
243
244                 i = tmp;
245         }
246
247         queue_draw ();
248 }
249
250 void
251 Panner2d::remove_target (int which)
252 {
253         Targets::iterator i = targets.find (which);
254
255         if (i != targets.end()) {
256                 delete i->second;
257                 targets.erase (i);
258                 queue_draw ();
259         }
260 }               
261
262 void
263 Panner2d::handle_state_change ()
264 {
265         ENSURE_GUI_THREAD(mem_fun(*this, &Panner2d::handle_state_change));
266
267         queue_draw ();
268 }
269
270 void
271 Panner2d::handle_position_change ()
272 {
273         uint32_t n;
274         ENSURE_GUI_THREAD(mem_fun(*this, &Panner2d::handle_position_change));
275
276         for (n = 0; n < pucks.size(); ++n) {
277                 float x, y;
278                 panner.streampanner(n).get_position (x, y);
279                 pucks[n]->x.set_value (x);
280                 pucks[n]->y.set_value (y);
281         }
282
283         for (n = 0; n < targets.size(); ++n) {
284                 targets[n]->x.set_value (panner.output(n).x);
285                 targets[n]->y.set_value (panner.output(n).y);
286         }
287
288         queue_draw ();
289 }
290
291 void
292 Panner2d::move_target (int which, float x, float y)
293 {
294         Targets::iterator i = targets.find (which);
295         Target *target;
296
297         if (!allow_target) {
298                 return;
299         }
300
301         if (i != targets.end()) {
302                 target = i->second;
303                 target->x.set_value (x);
304                 target->y.set_value (y);
305                 
306                 queue_draw ();
307         }
308 }               
309
310 void
311 Panner2d::move_puck (int which, float x, float y)
312 {
313         Targets::iterator i = pucks.find (which);
314         Target *target;
315
316         if (i != pucks.end()) {
317                 target = i->second;
318                 target->x.set_value (x);
319                 target->y.set_value (y);
320                 
321                 queue_draw ();
322         }
323 }               
324
325 void
326 Panner2d::show_puck (int which)
327 {
328         Targets::iterator i = pucks.find (which);
329
330         if (i != pucks.end()) {
331                 Target* puck = i->second;
332                 if (!puck->visible) {
333                         puck->visible = true;
334                         queue_draw ();
335                 }
336         }
337 }
338
339 void
340 Panner2d::hide_puck (int which)
341 {
342         Targets::iterator i = pucks.find (which);
343
344         if (i != pucks.end()) {
345                 Target* puck = i->second;
346                 if (!puck->visible) {
347                         puck->visible = false;
348                         queue_draw ();
349                 }
350         }
351 }
352
353 void
354 Panner2d::show_target (int which)
355 {
356         Targets::iterator i = targets.find (which);
357         if (i != targets.end()) {
358                 if (!i->second->visible) {
359                         i->second->visible = true;
360                         queue_draw ();
361                 }
362         }
363 }
364
365 void
366 Panner2d::hide_target (int which)
367 {
368         Targets::iterator i = targets.find (which);
369         if (i != targets.end()) {
370                 if (i->second->visible) {
371                         i->second->visible = false;
372                         queue_draw ();
373                 }
374         }
375 }
376
377 Panner2d::Target *
378 Panner2d::find_closest_object (gdouble x, gdouble y, int& which, bool& is_puck) const
379 {
380         gdouble efx, efy;
381         gdouble cx, cy;
382         Target *closest = 0;
383         Target *candidate;
384         float distance;
385         float best_distance = FLT_MAX;
386         int pwhich;
387
388         efx = x/width;
389         efy = y/height;
390         which = 0;
391         pwhich = 0;
392         is_puck = false;
393
394         for (Targets::const_iterator i = targets.begin(); i != targets.end(); ++i, ++which) {
395                 candidate = i->second;
396
397                 cx = candidate->x.get_value();
398                 cy = candidate->y.get_value();
399
400                 distance = sqrt ((cx - efx) * (cx - efx) +
401                                  (cy - efy) * (cy - efy));
402
403                 if (distance < best_distance) {
404                         closest = candidate;
405                         best_distance = distance;
406                 }
407         }
408
409         for (Targets::const_iterator i = pucks.begin(); i != pucks.end(); ++i, ++pwhich) {
410                 candidate = i->second;
411
412                 cx = candidate->x.get_value();
413                 cy = candidate->y.get_value();
414
415                 distance = sqrt ((cx - efx) * (cx - efx) +
416                                  (cy - efy) * (cy - efy));
417
418                 if (distance < best_distance) {
419                         closest = candidate;
420                         best_distance = distance;
421                         is_puck = true;
422                         which = pwhich;
423                 }
424         }
425         
426         return closest;
427 }               
428
429 bool
430 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
431 {
432         gint x, y;
433         GdkModifierType state;
434
435         if (ev->is_hint) {
436                 gdk_window_get_pointer (ev->window, &x, &y, &state);
437         } else {
438                 x = (int) floor (ev->x);
439                 y = (int) floor (ev->y);
440                 state = (GdkModifierType) ev->state;
441         }
442
443         return handle_motion (x, y, state);
444 }
445 bool
446 Panner2d::on_expose_event (GdkEventExpose *event)
447 {
448         gint x, y;
449         float fx, fy;
450         cairo_t* cr;
451
452         cr = gdk_cairo_create (get_window()->gobj());
453
454         cairo_set_line_width (cr, 1.0);
455
456         cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
457         if (!panner.bypassed()) {
458                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
459         } else {
460                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 0.2);
461         }
462         cairo_fill_preserve (cr);
463         cairo_clip (cr);
464
465         if (height > 100) {
466                 cairo_translate (cr, 10.0, 10.0);
467         }
468
469         cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
470         cairo_move_to (cr, 0.5, height/2.0+0.5);
471         cairo_line_to (cr, height+0.5, height/2+0.5);
472         cairo_stroke (cr);
473
474         cairo_move_to (cr, height/2+0.5, 0.5);
475         cairo_line_to (cr, height/2+0.5, height+0.5);
476         cairo_stroke (cr);
477
478         cairo_arc (cr, height/2, height/2, height/2, 0, 2.0 * M_PI);
479         cairo_stroke (cr);
480
481         if (!panner.bypassed()) {
482                 float arc_radius;
483
484                 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
485
486                 if (height < 100) {
487                         cairo_set_font_size (cr, 10);
488                         arc_radius = 2.0;
489                 } else {
490                         cairo_set_font_size (cr, 16);
491                         arc_radius = 4.0;
492                 }
493
494                 for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
495
496                         Target* puck = i->second;
497
498                         if (puck->visible) {
499                                 /* redraw puck */
500                                 
501                                 fx = min (puck->x.get_value(), 1.0);
502                                 fx = max (fx, -1.0f);
503                                 x = (gint) floor (width * fx - 4);
504                                 
505                                 fy = min (puck->y.get_value(), 1.0);
506                                 fy = max (fy, -1.0f);
507                                 y = (gint) floor (height * fy - 4);
508                                 
509                                 cairo_arc (cr, x, y, arc_radius, 0, 2.0 * M_PI);
510                                 cairo_set_source_rgb (cr, 0.8, 0.2, 0.1);
511                                 cairo_close_path (cr);
512                                 cairo_fill (cr);
513
514                                 /* arrow */
515                                 
516                                 if (height > 100.0f) {
517
518                                         float endx, endy;
519                                         endx = x;
520                                         endy = y;
521                                         
522                                         cairo_save (cr);
523                                         cairo_translate (cr, x, y);
524                                         cairo_rotate (cr, puck->azimuth.get_value());
525
526                                         /* horizontal left-to-right line (rotation will rotate it, duh) */
527
528                                         endx = 30.0;
529                                         endy = 0.0;
530
531                                         /* stem */
532                                         cairo_set_line_width (cr, 4.0);
533                                         cairo_move_to (cr, 0.0, 0.0);
534                                         cairo_line_to (cr, endx, endy);
535                                         cairo_stroke (cr);
536                                         
537                                         /* arrow head */
538                                         
539                                         cairo_move_to (cr, endx - 10.0, endy + 10.0);
540                                         cairo_line_to (cr, endx, endy);
541                                         cairo_line_to (cr, endx - 10.0, endy - 10.0);
542                                         cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
543                                         cairo_stroke (cr);
544
545                                         cairo_restore (cr);
546                                 }
547
548                                 cairo_move_to (cr, x + 6, y + 6);
549                                 cairo_show_text (cr, puck->text);
550                         }
551                 }
552
553                 /* redraw any visible targets */
554                 
555                 int n = 0;
556
557                 for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
558                         Target *target = i->second;
559                         char buf[256];
560                         ++n;
561
562                         if (target->visible) {
563                                 
564                                 fx = min (target->x.get_value(), 1.0);
565                                 fx = max (fx, -1.0f);
566                                 x = (gint) floor (width  * fx);
567                         
568                                 fy = min (target->y.get_value(), 1.0);
569                                 fy = max (fy, -1.0f);
570                                 y = (gint) floor (height * fy);
571
572                                 snprintf (buf, sizeof (buf), "%d", n);
573
574                                 cairo_set_source_rgb (cr, 0.0, 0.8, 0.1);
575                                 cairo_rectangle (cr, x-2, y-2, 4, 4);
576                                 cairo_fill (cr);
577                                 cairo_move_to (cr, x+6, y+6);
578                                 cairo_show_text (cr, buf);
579                         }
580                 }
581         }
582
583         cairo_destroy (cr);
584
585         return TRUE;
586 }
587
588 bool
589 Panner2d::on_button_press_event (GdkEventButton *ev)
590 {
591         GdkModifierType state;
592
593         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
594                 return false;
595         }
596
597         switch (ev->button) {
598         case 1:
599         case 2:
600                 drag_target = find_closest_object (ev->x, ev->y, drag_index, drag_is_puck);
601                 drag_x = (int) floor (ev->x);
602                 drag_y = (int) floor (ev->y);
603                 state = (GdkModifierType) ev->state;
604
605                 return handle_motion (drag_x, drag_y, state);
606                 break;
607
608         default:
609                 break;
610         }
611         
612         return FALSE;
613 }
614
615 bool
616 Panner2d::on_button_release_event (GdkEventButton *ev)
617 {
618         gint x, y;
619         GdkModifierType state;
620         bool ret = false;
621
622         switch (ev->button) {
623         case 1:
624                 x = (int) floor (ev->x);
625                 y = (int) floor (ev->y);
626                 state = (GdkModifierType) ev->state;
627
628                 if (drag_is_puck && (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier))) {
629
630
631                         for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
632                                 //Target* puck = i->second;
633
634                                 /* XXX DO SOMETHING TO SET PUCK BACK TO "normal" */
635                         }
636
637                         queue_draw ();
638                         PuckMoved (-1);
639                         ret = true;
640
641                 } else {
642                         ret = handle_motion (x, y, state);
643                 }
644                 
645                 drag_target = 0;
646                 break;
647
648         case 2:
649                 x = (int) floor (ev->x);
650                 y = (int) floor (ev->y);
651                 state = (GdkModifierType) ev->state;
652
653                 if (drag_is_puck && (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier))) {
654                         toggle_bypass ();
655                         ret = true;
656                 } else {
657                         ret = handle_motion (x, y, state);
658                 }
659                 
660                 drag_target = 0;
661                 break;
662
663         case 3:
664                 break;
665
666         }
667
668         return ret;
669 }
670
671 gint
672 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
673 {
674         if (drag_target == 0) {
675                 return false;
676         }
677
678         if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
679                 return false;
680         }
681
682         int x, y;
683         bool need_move = false;
684
685         if (!drag_is_puck && !allow_target) {
686                 cerr << "dip = " << drag_is_puck << " at = " << allow_target << endl;
687                 return true;
688         }
689
690         if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
691
692                 if (allow_x || !drag_is_puck) {
693                         float new_x;
694                         x = min (evx, width - 1);
695                         x = max (x, 0);
696                         new_x = (float) x / (width - 1);
697                         if (new_x != drag_target->x.get_value()) {
698                                 drag_target->x.set_value (new_x);
699                                 need_move = true;
700                         }
701                 }
702                 
703                 if (allow_y || drag_is_puck) {
704                         float new_y;
705                         y = min (evy, height - 1);
706                         y = max (y, 0);
707                         new_y = (float) y / (height - 1);
708                         if (new_y != drag_target->y.get_value()) {
709                                 drag_target->y.set_value (new_y);
710                                 need_move = true;
711                         }
712                 }
713                 
714                 if (need_move) {
715                         
716                         if (drag_is_puck) {
717                                 
718                                 panner.streampanner(drag_index).set_position (drag_target->x.get_value(), drag_target->y.get_value(), false);
719                                 
720                         } else {
721                                 
722                                 TargetMoved (drag_index);
723                         }
724
725                         queue_draw ();
726                 }
727                 
728
729         } else if ((state & GDK_BUTTON2_MASK) && !(state & GDK_BUTTON1_MASK)) {
730
731                 if (!drag_is_puck) {
732                         return false;
733                 }
734
735                 int xdelta = drag_x - evx;
736                 int ydelta = drag_x - evy;
737                 
738                 drag_target->azimuth.set_value (drag_target->azimuth.get_value() + (2 * M_PI) * ((float)ydelta)/height * ((float) -xdelta)/height);
739                 queue_draw ();
740         }
741
742         return true;
743 }
744
745 void
746 Panner2d::toggle_bypass ()
747 {
748         panner.set_bypassed (!panner.bypassed());
749 }
750
751 void
752 Panner2d::allow_x_motion (bool yn)
753 {
754         allow_x = yn;
755 }
756
757 void
758 Panner2d::allow_target_motion (bool yn)
759 {
760         allow_target = yn;
761 }
762
763 void
764 Panner2d::allow_y_motion (bool yn)
765 {
766         allow_y = yn;
767 }
768
769 Panner2dWindow::Panner2dWindow (Panner&p, int32_t h, uint32_t inputs)
770         : widget (p, h)
771         , reset_button (_("Reset"))
772         , bypass_button (_("Bypass"))
773         , mute_button (_("Mute"))
774 {
775         widget.set_name ("MixerPanZone");
776
777         set_title (_("Panner"));
778         widget.set_size_request (h, h);
779         
780         button_box.set_spacing (6);
781         button_box.pack_start (reset_button, false, false);
782         button_box.pack_start (bypass_button, false, false);
783         button_box.pack_start (mute_button, false, false);
784
785         spinner_box.set_spacing (6);
786         left_side.set_spacing (6);
787
788         left_side.pack_start (button_box, false, false);
789         left_side.pack_start (spinner_box, false, false);
790
791         reset_button.show ();
792         bypass_button.show ();
793         mute_button.show ();
794         button_box.show ();
795         spinner_box.show ();
796         left_side.show ();
797
798         hpacker.set_spacing (6);
799         hpacker.set_border_width (12);
800         hpacker.pack_start (widget, false, false);
801         hpacker.pack_start (left_side, false, false);
802         hpacker.show ();
803         
804         add (hpacker);
805         reset (inputs);
806         widget.show ();
807 }
808
809 void
810 Panner2dWindow::reset (uint32_t n_inputs)
811 {
812         widget.reset (n_inputs);
813
814         while (spinners.size() < n_inputs) {
815                 spinners.push_back (new Gtk::SpinButton (widget.azimuth (spinners.size())));
816                 spinner_box.pack_start (*spinners.back(), false, false);
817                 spinners.back()->set_digits (4);
818                 spinners.back()->show ();
819         }
820
821         while (spinners.size() > n_inputs) {
822                 spinner_box.remove (*spinners.back());
823                 delete spinners.back();
824                 spinners.erase (--spinners.end());
825         }
826 }