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