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