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