2 Copyright (C) 2011 Paul Davis
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.
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.
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.
20 #include "pbd/cartesian.h"
22 #include "gtkmm2ext/keyboard.h"
24 #include "speaker_dialog.h"
28 using namespace ARDOUR;
32 using namespace Gtkmm2ext;
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"))
42 set_size_request (400, 200);
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);
52 hbox.set_border_width (6);
53 hbox.pack_start (darea, true, true);
54 hbox.pack_start (side_vbox, false, false);
56 get_vbox()->pack_start (hbox);
57 get_vbox()->show_all ();
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));
69 SpeakerDialog::set_speakers (const Speakers& s)
75 SpeakerDialog::get_speakers () const
81 SpeakerDialog::darea_expose_event (GdkEventExpose* event)
86 cr = gdk_cairo_create (darea.get_window()->gobj());
88 cairo_set_line_width (cr, 1.0);
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);
96 cairo_translate (cr, 10.0, 10.0);
99 /* horizontal line of "crosshairs" */
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);
106 /* vertical line of "crosshairs" */
108 cairo_move_to (cr, width/2+0.5, 0.5);
109 cairo_line_to (cr, width/2+0.5, height+0.5);
112 /* the circle on which signals live */
114 cairo_arc (cr, width/2, height/2, height/2, 0, 2.0 * M_PI);
119 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
122 cairo_set_font_size (cr, 10);
125 cairo_set_font_size (cr, 16);
130 for (vector<Speaker>::iterator i = speakers.speakers().begin(); i != speakers.speakers().end(); ++i) {
133 CartesianVector c (s.coords());
137 x = (gint) floor (c.x);
138 y = (gint) floor (c.y);
140 /* XXX need to shift circles so that they are centered on the circle */
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);
147 cairo_move_to (cr, x + 6, y + 6);
150 snprintf (buf, sizeof (buf), "%d:%d", n+1, (int) lrint (s.angles().azi));
151 cairo_show_text (cr, buf);
162 SpeakerDialog::cart_to_gtk (CartesianVector& c) const
164 /* "c" uses a coordinate space that is:
167 dimension = 2.0 * 2.0
168 so max values along each axis are -1..+1
170 GTK uses a coordinate space that is:
173 dimension = width * height
174 so max values along each axis are 0,width and
178 c.x = (width / 2) * (c.x + 1);
179 c.y = (height / 2) * (1 - c.y);
181 /* XXX z-axis not handled - 2D for now */
185 SpeakerDialog::gtk_to_cart (CartesianVector& c) const
187 c.x = (c.x / (width / 2.0)) - 1.0;
188 c.y = -((c.y / (height / 2.0)) - 1.0);
190 /* XXX z-axis not handled - 2D for now */
194 SpeakerDialog::clamp_to_circle (double& x, double& y)
199 PBD::cart_to_azi_ele (x, y, z, azi, ele);
200 PBD::azi_ele_to_cart (azi, ele, x, y, z);
204 SpeakerDialog::darea_size_allocate (Gtk::Allocation& alloc)
206 width = alloc.get_width();
207 height = alloc.get_height();
216 SpeakerDialog::darea_button_press_event (GdkEventButton *ev)
218 GdkModifierType state;
220 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
226 switch (ev->button) {
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;
234 return handle_motion (drag_x, drag_y, state);
245 SpeakerDialog::darea_button_release_event (GdkEventButton *ev)
248 GdkModifierType state;
251 switch (ev->button) {
253 x = (int) floor (ev->x);
254 y = (int) floor (ev->y);
255 state = (GdkModifierType) ev->state;
257 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
259 for (vector<Speaker>::iterator i = speakers.speakers().begin(); i != speakers.speakers().end(); ++i) {
260 /* XXX DO SOMETHING TO SET SPEAKER BACK TO "normal" */
267 ret = handle_motion (x, y, state);
273 x = (int) floor (ev->x);
274 y = (int) floor (ev->y);
275 state = (GdkModifierType) ev->state;
277 ret = handle_motion (x, y, state);
291 SpeakerDialog::find_closest_object (gdouble x, gdouble y, int& which)
294 float best_distance = FLT_MAX;
300 for (vector<Speaker>::iterator i = speakers.speakers().begin(); i != speakers.speakers().end(); ++i, ++pwhich) {
302 Speaker& candidate (*i);
306 candidate.angles().cartesian (c);
309 distance = sqrt ((c.x - x) * (c.x - x) +
310 (c.y - y) * (c.y - y));
312 if (distance < best_distance) {
313 best_distance = distance;
319 if (best_distance > 20) { // arbitrary
327 SpeakerDialog::darea_motion_notify_event (GdkEventMotion *ev)
330 GdkModifierType state;
333 gdk_window_get_pointer (ev->window, &x, &y, &state);
335 x = (int) floor (ev->x);
336 y = (int) floor (ev->y);
337 state = (GdkModifierType) ev->state;
340 return handle_motion (x, y, state);
344 SpeakerDialog::handle_motion (gint evx, gint evy, GdkModifierType state)
346 if (drag_index < 0) {
350 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
355 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
357 bool need_move = false;
358 Speaker& moving (speakers.speakers()[drag_index]);
360 moving.angles().cartesian (c);
363 if ((evx != c.x) || (evy != c.y)) {
368 CartesianVector cp (evx, evy, 0.0);
370 /* canonicalize position */
374 /* position actual signal on circle */
376 clamp_to_circle (cp.x, cp.y);
378 /* generate an angular representation and set drag target (GUI) position */