857c067ef5c0490fcf1a62a712b5e1321816ee0a
[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 "gtkmm2ext/gtk_ui.h"
28
29 #include "pbd/error.h"
30 #include "pbd/cartesian.h"
31 #include "ardour/panner.h"
32 #include "ardour/pannable.h"
33 #include "ardour/speakers.h"
34
35 #include "panner2d.h"
36 #include "keyboard.h"
37 #include "gui_thread.h"
38
39 #include "i18n.h"
40
41 using namespace std;
42 using namespace Gtk;
43 using namespace ARDOUR;
44 using namespace PBD;
45 using Gtkmm2ext::Keyboard;
46
47 Panner2d::Target::Target (const AngularVector& a, const char *txt)
48         : position (a)
49         , text (txt)
50         , _selected (false)
51 {
52 }
53
54 Panner2d::Target::~Target ()
55 {
56 }
57
58 void
59 Panner2d::Target::set_text (const char* txt)
60 {
61         text = txt;
62 }
63
64 Panner2d::Panner2d (boost::shared_ptr<Panner> p, int32_t h)
65         : panner (p), width (0), height (h)
66 {
67         panner->StateChanged.connect (connections, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
68         panner->Changed.connect (connections, invalidator (*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
69
70         panner->pannable()->pan_azimuth_control->Changed.connect (connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
71         panner->pannable()->pan_width_control->Changed.connect (connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
72
73         drag_target = 0;
74         set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
75 }
76
77 Panner2d::~Panner2d()
78 {
79         for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
80                 delete *i;
81         }
82 }
83
84 void
85 Panner2d::reset (uint32_t n_inputs)
86 {
87         uint32_t nouts = panner->out().n_audio();
88
89         /* pucks */
90
91         while (pucks.size() < n_inputs) {
92                 add_puck ("", AngularVector());
93         }
94
95         if (pucks.size() > n_inputs) {
96                 for (uint32_t i = pucks.size(); i < n_inputs; ++i) {
97                         delete pucks[i];
98                 }
99
100                 pucks.resize (n_inputs);
101         }
102                                                 
103         switch (n_inputs) {
104         case 0:
105                 break;
106
107         case 1:
108                 pucks[0]->set_text ("");
109                 break;
110
111         case 2:
112                 pucks[0]->set_text ("R");
113                 pucks[1]->set_text ("L");
114                 break;
115
116         default:
117                 for (uint32_t i = 0; i < n_inputs; ++i) {
118                         char buf[64];
119                         snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
120                         pucks[i]->set_text (buf);
121                 }
122                 break;
123         }
124
125         for (uint32_t i = 0; i < n_inputs; ++i) {
126                 pucks[i]->position = panner->signal_position (i);
127         }
128
129         /* add all outputs */
130
131         while (targets.size() < nouts) {
132                 add_target (AngularVector());
133         }
134
135         if (targets.size() > nouts) {
136                 for (uint32_t i = nouts; i < targets.size(); ++i) {
137                         delete targets[i];
138                 }
139
140                 targets.resize (nouts);
141         }
142
143         for (Targets::iterator x = targets.begin(); x != targets.end(); ++x) {
144                 (*x)->visible = false;
145         }
146
147         vector<Speaker>& speakers (panner->get_speakers()->speakers());
148
149         for (uint32_t n = 0; n < nouts; ++n) {
150                 char buf[16];
151
152                 snprintf (buf, sizeof (buf), "%d", n+1);
153                 targets[n]->set_text (buf);
154                 targets[n]->position = speakers[n].angles();
155                 targets[n]->visible = true;
156         }
157
158         queue_draw ();
159 }
160
161 void
162 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
163 {
164         width = alloc.get_width();
165         height = alloc.get_height();
166
167         /* our idea of our width/height must be "square
168          */
169
170         if (height > 100) {
171                 width -= 20;
172                 height -= 20;
173         }
174
175         dimen = min (width, height);
176         
177         DrawingArea::on_size_allocate (alloc);
178 }
179
180 int
181 Panner2d::add_puck (const char* text, const AngularVector& a)
182 {
183         Target* puck = new Target (a, text);
184         pucks.push_back (puck);
185         puck->visible = true;
186
187         return 0;
188 }
189
190 int
191 Panner2d::add_target (const AngularVector& a)
192 {
193         Target* target = new Target (a, "");
194         targets.push_back (target);
195         target->visible = true;
196         queue_draw ();
197
198         return targets.size() - 1;
199 }
200
201 void
202 Panner2d::handle_state_change ()
203 {
204         queue_draw ();
205 }
206
207 void
208 Panner2d::handle_position_change ()
209 {
210         uint32_t n;
211
212         for (uint32_t i = 0; i < pucks.size(); ++i) {
213                 pucks[i]->position = panner->signal_position (i);
214         }
215
216         vector<Speaker>& speakers (panner->get_speakers()->speakers());
217
218         for (n = 0; n < targets.size(); ++n) {
219                 targets[n]->position = speakers[n].angles();
220         }
221
222         queue_draw ();
223 }
224
225 void
226 Panner2d::move_puck (int which, const AngularVector& a)
227 {
228         if (which >= int (targets.size())) {
229                 return;
230         }
231         
232         targets[which]->position = a;
233         queue_draw ();
234 }
235
236 Panner2d::Target *
237 Panner2d::find_closest_object (gdouble x, gdouble y, int& which) const
238 {
239         Target *closest = 0;
240         Target *candidate;
241         float distance;
242         float best_distance = FLT_MAX;
243         int pwhich;
244
245         which = 0;
246         pwhich = 0;
247
248         for (Targets::const_iterator i = pucks.begin(); i != pucks.end(); ++i, ++pwhich) {
249                 candidate = *i;
250
251                 CartesianVector c;
252
253                 candidate->position.cartesian (c);
254                 cart_to_gtk (c);
255
256                 distance = sqrt ((c.x - x) * (c.x - x) +
257                                  (c.y - y) * (c.y - y));
258
259                 if (distance < best_distance) {
260                         closest = candidate;
261                         best_distance = distance;
262                         which = pwhich;
263                 }
264         }
265
266         if (best_distance > 20) { // arbitrary 
267                 return 0;
268         }
269
270         return closest;
271 }
272
273 bool
274 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
275 {
276         gint x, y;
277         GdkModifierType state;
278
279         if (ev->is_hint) {
280                 gdk_window_get_pointer (ev->window, &x, &y, &state);
281         } else {
282                 x = (int) floor (ev->x);
283                 y = (int) floor (ev->y);
284                 state = (GdkModifierType) ev->state;
285         }
286
287         return handle_motion (x, y, state);
288 }
289 bool
290 Panner2d::on_expose_event (GdkEventExpose *event)
291 {
292         gint x, y;
293         cairo_t* cr;
294
295         cr = gdk_cairo_create (get_window()->gobj());
296
297         cairo_set_line_width (cr, 1.0);
298
299         cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
300         if (!panner->bypassed()) {
301                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
302         } else {
303                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 0.2);
304         }
305         cairo_fill_preserve (cr);
306         cairo_clip (cr);
307
308         if (height > 100) {
309                 cairo_translate (cr, 10.0, 10.0);
310         }
311
312         /* horizontal line of "crosshairs" */
313
314         cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
315         cairo_move_to (cr, 0.5, height/2.0+0.5);
316         cairo_line_to (cr, width+0.5, height/2+0.5);
317         cairo_stroke (cr);
318
319         /* vertical line of "crosshairs" */
320         
321         cairo_move_to (cr, width/2+0.5, 0.5);
322         cairo_line_to (cr, width/2+0.5, height+0.5);
323         cairo_stroke (cr);
324
325         /* the circle on which signals live */
326
327         cairo_arc (cr, width/2, height/2, dimen/2, 0, 2.0 * M_PI);
328         cairo_stroke (cr);
329
330         if (!panner->bypassed()) {
331                 float arc_radius;
332
333                 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
334
335                 if (height < 100) {
336                         cairo_set_font_size (cr, 10);
337                         arc_radius = 2.0;
338                 } else {
339                         cairo_set_font_size (cr, 16);
340                         arc_radius = 4.0;
341                 }
342
343                 for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
344
345                         Target* puck = *i;
346
347                         if (puck->visible) {
348                                 /* redraw puck */
349
350                                 CartesianVector c;
351                                 
352                                 puck->position.cartesian (c);
353                                 cart_to_gtk (c);
354
355                                 x = (gint) floor (c.x);
356                                 y = (gint) floor (c.y);
357
358                                 /* XXX need to shift circles so that they are centered on the circle */
359                                         
360                                 cairo_arc (cr, x, y, arc_radius, 0, 2.0 * M_PI);
361                                 cairo_set_source_rgb (cr, 0.8, 0.2, 0.1);
362                                 cairo_close_path (cr);
363                                 cairo_fill (cr);
364
365                                 cairo_move_to (cr, x + 6, y + 6);
366
367                                 char buf[256];
368                                 snprintf (buf, sizeof (buf), "%s:%d", puck->text.c_str(), (int) lrint (puck->position.azi));
369                                 cairo_show_text (cr, buf);
370                         }
371                 }
372
373                 /* redraw any visible targets */
374
375                 int n = 0;
376
377                 for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
378                         Target *target = *i;
379                         char buf[256];
380                         ++n;
381
382                         if (target->visible) {
383
384                                 CartesianVector c;
385                                 
386                                 target->position.cartesian (c);
387                                 cart_to_gtk (c);
388
389                                 x = (int) floor (c.x);
390                                 y = (int) floor (c.y);
391                                 
392                                 snprintf (buf, sizeof (buf), "%d", n);
393
394                                 cairo_set_source_rgb (cr, 0.0, 0.8, 0.1);
395                                 cairo_rectangle (cr, x-2, y-2, 4, 4);
396                                 cairo_fill (cr);
397                                 cairo_move_to (cr, x+6, y+6);
398                                 cairo_show_text (cr, buf);
399
400                         }
401                 }
402         }
403
404         cairo_destroy (cr);
405
406         return true;
407 }
408
409 bool
410 Panner2d::on_button_press_event (GdkEventButton *ev)
411 {
412         GdkModifierType state;
413
414         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
415                 return false;
416         }
417
418         switch (ev->button) {
419         case 1:
420         case 2:
421                 if ((drag_target = find_closest_object (ev->x, ev->y, drag_index)) != 0) {
422                         drag_target->set_selected (true);
423                 }
424
425                 drag_x = (int) floor (ev->x);
426                 drag_y = (int) floor (ev->y);
427                 state = (GdkModifierType) ev->state;
428
429                 return handle_motion (drag_x, drag_y, state);
430                 break;
431
432         default:
433                 break;
434         }
435
436         return false;
437 }
438
439 bool
440 Panner2d::on_button_release_event (GdkEventButton *ev)
441 {
442         gint x, y;
443         GdkModifierType state;
444         bool ret = false;
445
446         switch (ev->button) {
447         case 1:
448                 x = (int) floor (ev->x);
449                 y = (int) floor (ev->y);
450                 state = (GdkModifierType) ev->state;
451
452                 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
453                         
454                         for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
455                                 //Target* puck = i->second;
456                                 /* XXX DO SOMETHING TO SET PUCK BACK TO "normal" */
457                         }
458
459                         queue_draw ();
460                         PuckMoved (-1);
461                         ret = true;
462
463                 } else {
464                         ret = handle_motion (x, y, state);
465                 }
466
467                 drag_target = 0;
468                 break;
469
470         case 2:
471                 x = (int) floor (ev->x);
472                 y = (int) floor (ev->y);
473                 state = (GdkModifierType) ev->state;
474
475                 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
476                         toggle_bypass ();
477                         ret = true;
478                 } else {
479                         ret = handle_motion (x, y, state);
480                 }
481
482                 drag_target = 0;
483                 break;
484
485         case 3:
486                 break;
487
488         }
489
490         return ret;
491 }
492
493 gint
494 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
495 {
496         if (drag_target == 0) {
497                 return false;
498         }
499
500         if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
501                 return false;
502         }
503
504
505         if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
506                 CartesianVector c;
507                 bool need_move = false;
508                 
509                 drag_target->position.cartesian (c);
510                 cart_to_gtk (c);
511
512                 if ((evx != c.x) || (evy != c.y)) {
513                         need_move = true;
514                 }
515
516                 if (need_move) {
517                         CartesianVector cp (evx, evy, 0.0);
518
519                         /* canonicalize position */
520
521                         gtk_to_cart (cp);
522
523                         /* position actual signal on circle */
524
525                         clamp_to_circle (cp.x, cp.y);
526                         
527                         /* generate an angular representation and set drag target (GUI) position */
528
529                         cp.angular (drag_target->position); /* sets drag target position */
530
531                         double degree_fract = drag_target->position.azi / 360.0;
532
533                         panner->set_position (degree_fract);
534                 }
535         } 
536
537         return true;
538 }
539
540 void
541 Panner2d::cart_to_gtk (CartesianVector& c) const
542 {
543         /* "c" uses a coordinate space that is:
544             
545            center = 0.0
546            dimension = 2.0 * 2.0
547            so max values along each axis are -1..+1
548
549            GTK uses a coordinate space that is:
550
551            top left = 0.0
552            dimension = width * height
553            so max values along each axis are 0,width and
554            0,height
555         */
556         
557         const uint32_t hoffset = (width - dimen)/2;
558         const uint32_t voffset = (height - dimen)/2;
559
560         c.x = hoffset + ((dimen / 2) * (c.x + 1));
561         c.y = voffset + ((dimen / 2) * (1 - c.y));
562
563         /* XXX z-axis not handled - 2D for now */
564 }
565
566 void
567 Panner2d::gtk_to_cart (CartesianVector& c) const
568 {
569         c.x = ((c.x / (dimen / 2.0)) - 1.0);
570         c.y = -((c.y / (dimen / 2.0)) - 1.0);
571
572         /* XXX z-axis not handled - 2D for now */
573 }
574
575 void
576 Panner2d::clamp_to_circle (double& x, double& y)
577 {
578         double azi, ele;
579         double z = 0.0;
580         
581         PBD::cart_to_azi_ele (x, y, z, azi, ele);
582         PBD::azi_ele_to_cart (azi, ele, x, y, z);
583 }
584
585 void
586 Panner2d::toggle_bypass ()
587 {
588         panner->set_bypassed (!panner->bypassed());
589 }
590
591 Panner2dWindow::Panner2dWindow (boost::shared_ptr<Panner> p, int32_t h, uint32_t inputs)
592         : ArdourDialog (_("Panner (2D)"))
593         , widget (p, h)
594         , reset_button (_("Reset"))
595         , bypass_button (_("Bypass"))
596         , mute_button (_("Mute"))
597 {
598         widget.set_name ("MixerPanZone");
599
600         set_title (_("Panner"));
601         widget.set_size_request (h, h);
602
603         button_box.set_spacing (6);
604         button_box.pack_start (reset_button, false, false);
605         button_box.pack_start (bypass_button, false, false);
606         button_box.pack_start (mute_button, false, false);
607
608         spinner_box.set_spacing (6);
609         left_side.set_spacing (6);
610
611         left_side.pack_start (button_box, false, false);
612         left_side.pack_start (spinner_box, false, false);
613
614         reset_button.show ();
615         bypass_button.show ();
616         mute_button.show ();
617         button_box.show ();
618         spinner_box.show ();
619         left_side.show ();
620
621         hpacker.set_spacing (6);
622         hpacker.set_border_width (12);
623         hpacker.pack_start (widget, false, false);
624         hpacker.pack_start (left_side, false, false);
625         hpacker.show ();
626
627         get_vbox()->pack_start (hpacker);
628         reset (inputs);
629         widget.show ();
630 }
631
632 void
633 Panner2dWindow::reset (uint32_t n_inputs)
634 {
635         widget.reset (n_inputs);
636
637 #if 0
638         while (spinners.size() < n_inputs) {
639                 // spinners.push_back (new Gtk::SpinButton (widget.azimuth (spinners.size())));
640                 //spinner_box.pack_start (*spinners.back(), false, false);
641                 //spinners.back()->set_digits (4);
642                 spinners.back()->show ();
643         }
644
645         while (spinners.size() > n_inputs) {
646                 spinner_box.remove (*spinners.back());
647                 delete spinners.back();
648                 spinners.erase (--spinners.end());
649         }
650 #endif
651 }