Make add speaker button work.
[ardour.git] / gtk2_ardour / speaker_dialog.cc
1 /*
2     Copyright (C) 2011 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 "pbd/cartesian.h"
21
22 #include "gtkmm2ext/keyboard.h"
23
24 #include "speaker_dialog.h"
25
26 #include "i18n.h"
27
28 using namespace ARDOUR;
29 using namespace PBD;
30 using namespace std;
31 using namespace Gtk;
32 using namespace Gtkmm2ext;
33
34 SpeakerDialog::SpeakerDialog ()
35         : ArdourDialog (_("Speaker Configuration"))
36         , aspect_frame ("", 0.5, 0.5, 1.0, false)
37         , azimuth_adjustment (0, 0.0, 360.0, 10.0, 1.0)
38         , azimuth_spinner (azimuth_adjustment)
39         , add_speaker_button (_("Add Speaker"))
40         , use_system_button (_("Use System"))
41                               
42 {
43         side_vbox.set_homogeneous (false);
44         side_vbox.set_border_width (9);
45         side_vbox.set_spacing (6);
46         side_vbox.pack_start (azimuth_spinner, false, false);
47         side_vbox.pack_start (add_speaker_button, false, false);
48         side_vbox.pack_start (use_system_button, false, false);
49
50         aspect_frame.set_size_request (200, 200);
51         aspect_frame.set_shadow_type (SHADOW_NONE);
52         aspect_frame.add (darea);
53
54         hbox.set_spacing (6);
55         hbox.set_border_width (6);
56         hbox.pack_start (aspect_frame, true, true);
57         hbox.pack_start (side_vbox, false, false);
58
59         get_vbox()->pack_start (hbox);
60         get_vbox()->show_all ();
61
62         darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
63
64         darea.signal_size_allocate().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_size_allocate));
65         darea.signal_expose_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_expose_event));
66         darea.signal_button_press_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_button_press_event));
67         darea.signal_button_release_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_button_release_event));
68         darea.signal_motion_notify_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_motion_notify_event));
69
70         add_speaker_button.signal_clicked().connect (sigc::mem_fun (*this, &SpeakerDialog::add_speaker));
71
72         drag_index = -1;
73 }
74
75 void
76 SpeakerDialog::set_speakers (boost::shared_ptr<Speakers> s) 
77 {
78         speakers = *s;
79 }
80
81 Speakers
82 SpeakerDialog::get_speakers () const
83 {
84         return speakers;
85 }
86
87 bool
88 SpeakerDialog::darea_expose_event (GdkEventExpose* event)
89 {
90         gint x, y;
91         cairo_t* cr;
92
93         cr = gdk_cairo_create (darea.get_window()->gobj());
94
95         cairo_set_line_width (cr, 1.0);
96
97         cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
98         cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
99         cairo_fill_preserve (cr);
100         cairo_clip (cr);
101
102         if (height > 100) {
103                 cairo_translate (cr, 10.0, 10.0);
104         }
105
106         /* horizontal line of "crosshairs" */
107
108         cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
109         cairo_move_to (cr, 0.5, height/2.0+0.5);
110         cairo_line_to (cr, width+0.5, height/2+0.5);
111         cairo_stroke (cr);
112
113         /* vertical line of "crosshairs" */
114         
115         cairo_move_to (cr, width/2+0.5, 0.5);
116         cairo_line_to (cr, width/2+0.5, height+0.5);
117         cairo_stroke (cr);
118
119         /* the circle on which signals live */
120
121         cairo_arc (cr, width/2, height/2, height/2, 0, 2.0 * M_PI);
122         cairo_stroke (cr);
123
124         float arc_radius;
125         
126         cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
127         
128         if (height < 100) {
129                 cairo_set_font_size (cr, 10);
130                 arc_radius = 2.0;
131         } else {
132                 cairo_set_font_size (cr, 16);
133                 arc_radius = 4.0;
134         }
135
136         uint32_t n = 0;
137         for (vector<Speaker>::iterator i = speakers.speakers().begin(); i != speakers.speakers().end(); ++i) {
138                 
139                 Speaker& s (*i);
140                 CartesianVector c (s.coords());
141                 
142                 cart_to_gtk (c);
143                 
144                 x = (gint) floor (c.x);
145                 y = (gint) floor (c.y);
146                 
147                 /* XXX need to shift circles so that they are centered on the circle */
148                 
149                 cairo_arc (cr, x, y, arc_radius, 0, 2.0 * M_PI);
150                 cairo_set_source_rgb (cr, 0.8, 0.2, 0.1);
151                 cairo_close_path (cr);
152                 cairo_fill (cr);
153                 
154                 cairo_move_to (cr, x + 6, y + 6);
155                 
156                 char buf[256];
157                 snprintf (buf, sizeof (buf), "%d:%d", n+1, (int) lrint (s.angles().azi));
158                 cairo_show_text (cr, buf);
159                 ++n;
160         }
161
162         cairo_destroy (cr);
163
164         return true;
165         
166 }
167
168 void
169 SpeakerDialog::cart_to_gtk (CartesianVector& c) const
170 {
171         /* "c" uses a coordinate space that is:
172             
173            center = 0.0
174            dimension = 2.0 * 2.0
175            so max values along each axis are -1..+1
176
177            GTK uses a coordinate space that is:
178
179            top left = 0.0
180            dimension = width * height
181            so max values along each axis are 0,width and
182            0,height
183         */
184
185         c.x = (width / 2) * (c.x + 1);
186         c.y = (height / 2) * (1 - c.y);
187
188         /* XXX z-axis not handled - 2D for now */
189 }
190
191 void
192 SpeakerDialog::gtk_to_cart (CartesianVector& c) const
193 {
194         c.x = (c.x / (width / 2.0)) - 1.0;
195         c.y = -((c.y / (height / 2.0)) - 1.0);
196
197         /* XXX z-axis not handled - 2D for now */
198 }
199
200 void
201 SpeakerDialog::clamp_to_circle (double& x, double& y)
202 {
203         double azi, ele;
204         double z = 0.0;
205         double l;
206
207         PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
208         PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
209 }
210
211 void
212 SpeakerDialog::darea_size_allocate (Gtk::Allocation& alloc)
213 {
214         width = alloc.get_width();
215         height = alloc.get_height();
216
217         if (height > 100) {
218                 width -= 20;
219                 height -= 20;
220         }
221 }
222
223 bool
224 SpeakerDialog::darea_button_press_event (GdkEventButton *ev)
225 {
226         GdkModifierType state;
227
228         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
229                 return false;
230         }
231
232         drag_index = -1;
233
234         switch (ev->button) {
235         case 1:
236         case 2:
237                 drag_index = find_closest_object (ev->x, ev->y);
238                 drag_x = (int) floor (ev->x);
239                 drag_y = (int) floor (ev->y);
240                 state = (GdkModifierType) ev->state;
241
242                 return handle_motion (drag_x, drag_y, state);
243                 break;
244
245         default:
246                 break;
247         }
248
249         return false;
250 }
251
252 bool
253 SpeakerDialog::darea_button_release_event (GdkEventButton *ev)
254 {
255         gint x, y;
256         GdkModifierType state;
257         bool ret = false;
258
259         switch (ev->button) {
260         case 1:
261                 x = (int) floor (ev->x);
262                 y = (int) floor (ev->y);
263                 state = (GdkModifierType) ev->state;
264
265                 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
266                         
267                         for (vector<Speaker>::iterator i = speakers.speakers().begin(); i != speakers.speakers().end(); ++i) {
268                                 /* XXX DO SOMETHING TO SET SPEAKER BACK TO "normal" */
269                         }
270
271                         queue_draw ();
272                         ret = true;
273
274                 } else {
275                         ret = handle_motion (x, y, state);
276                 }
277
278                 break;
279
280         case 2:
281                 x = (int) floor (ev->x);
282                 y = (int) floor (ev->y);
283                 state = (GdkModifierType) ev->state;
284
285                 ret = handle_motion (x, y, state);
286                 break;
287
288         case 3:
289                 break;
290
291         }
292         
293         drag_index = -1;
294
295         return ret;
296 }
297
298 int
299 SpeakerDialog::find_closest_object (gdouble x, gdouble y) 
300 {
301         float distance;
302         float best_distance = FLT_MAX;
303         int n = 0;
304         int which = -1;
305
306         for (vector<Speaker>::iterator i = speakers.speakers().begin(); i != speakers.speakers().end(); ++i, ++n) {
307
308                 Speaker& candidate (*i);
309                 CartesianVector c;
310         
311                 candidate.angles().cartesian (c);
312                 cart_to_gtk (c);
313
314                 distance = sqrt ((c.x - x) * (c.x - x) +
315                                  (c.y - y) * (c.y - y));
316
317
318                 if (distance < best_distance) {
319                         best_distance = distance;
320                         which = n;
321                 }
322         }
323
324         if (best_distance > 20) { // arbitrary 
325                 return -1;
326         }
327
328         return which;
329 }
330
331 bool
332 SpeakerDialog::darea_motion_notify_event (GdkEventMotion *ev)
333 {
334         gint x, y;
335         GdkModifierType state;
336
337         if (ev->is_hint) {
338                 gdk_window_get_pointer (ev->window, &x, &y, &state);
339         } else {
340                 x = (int) floor (ev->x);
341                 y = (int) floor (ev->y);
342                 state = (GdkModifierType) ev->state;
343         }
344
345         return handle_motion (x, y, state);
346 }
347
348 bool
349 SpeakerDialog::handle_motion (gint evx, gint evy, GdkModifierType state)
350 {
351         if (drag_index < 0) {
352                 return false;
353         }
354
355         if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
356                 return false;
357         }
358
359
360         if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
361                 CartesianVector c;
362                 bool need_move = false;
363                 Speaker& moving (speakers.speakers()[drag_index]);
364
365                 moving.angles().cartesian (c);
366                 cart_to_gtk (c);
367
368                 if ((evx != c.x) || (evy != c.y)) {
369                         need_move = true;
370                 }
371
372                 if (need_move) {
373                         CartesianVector cp (evx, evy, 0.0);
374
375                         /* canonicalize position */
376
377                         gtk_to_cart (cp);
378
379                         /* position actual signal on circle */
380
381                         clamp_to_circle (cp.x, cp.y);
382                         
383                         /* generate an angular representation and set drag target (GUI) position */
384
385                         AngularVector a;
386
387                         cp.angular (a);
388
389                         moving.move (a);
390
391                         queue_draw ();
392                 }
393         } 
394
395         return true;
396 }
397
398 void
399 SpeakerDialog::add_speaker ()
400 {
401         speakers.add_speaker (PBD::AngularVector (0, 0, 0));
402         queue_draw ();
403 }