e1f4a0c56b5326ddd3a799af52d67aa500d23faf
[ardour.git] / gtk2_ardour / stereo_panner.cc
1 /*
2     Copyright (C) 2000-2007 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 <iostream>
21 #include <iomanip>
22 #include <cstring>
23
24 #include "pbd/controllable.h"
25
26 #include "gtkmm2ext/gui_thread.h"
27
28 #include "ardour/panner.h"
29 #include "stereo_panner.h"
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace Gtk;
35
36 StereoPanner::StereoPanner (boost::shared_ptr<PBD::Controllable> position, boost::shared_ptr<PBD::Controllable> width)
37         : position_control (position)
38         , width_control (width)
39         , dragging (false)
40         , dragging_position (false)
41         , drag_start_x (0)
42         , last_drag_x (0)
43 {
44         set_size_request (-1, 15);
45
46         position_control->Changed.connect (connections, invalidator(*this), boost::bind (&DrawingArea::queue_draw, this), gui_context());
47         width_control->Changed.connect (connections, invalidator(*this), boost::bind (&DrawingArea::queue_draw, this), gui_context());
48         
49         add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK|Gdk::POINTER_MOTION_MASK);
50 }
51
52 StereoPanner::~StereoPanner ()
53 {
54 }
55
56 bool
57 StereoPanner::on_expose_event (GdkEventExpose* ev)
58 {
59         Glib::RefPtr<Gdk::Window> win (get_window());
60         Glib::RefPtr<Gdk::GC> gc (get_style()->get_base_gc (get_state()));
61
62         cairo_t* cr = gdk_cairo_create (win->gobj());
63        
64         int x1, x2;
65         int width, height;
66         double pos = position_control->get_value (); /* 0..1 */
67         double swidth = width_control->get_value (); /* -1..+1 */
68         const int pos_box_size = 5;
69
70         width = get_width();
71         height = get_height ();
72
73         /* compute where the central box is */
74
75         x1 = (int) floor (width * pos);
76         x1 -= pos_box_size/2;
77
78         cairo_set_source_rgb (cr, 255, 0, 0);
79         cairo_rectangle (cr, x1, 4, pos_box_size, pos_box_size);
80         cairo_fill (cr);
81
82         /* compute & draw the line through the box */
83
84         x2 = x1 - (int) floor ((fabs (swidth) * width)/2.0); // center, then back up half the swidth value
85
86         cairo_set_source_rgb (cr, 0, 255, 0);
87         cairo_move_to (cr, x2, 4+(pos_box_size/2));
88         cairo_line_to (cr, x2 + floor ((fabs (swidth * width))), 4+(pos_box_size/2));
89         cairo_stroke (cr);
90
91         cairo_destroy (cr);
92         return true;
93 }
94
95 bool
96 StereoPanner::on_button_press_event (GdkEventButton* ev)
97 {
98         drag_start_x = ev->x;
99         last_drag_x = ev->x;
100
101         /* center 8 pixels are for position drag */
102
103         int w = get_width();
104         double pos = position_control->get_value ();
105
106         if ((ev->x >= (int) floor ((pos * w)-4)) && (ev->x <= (int) floor ((pos * w)+4))) {
107                 dragging_position = true;
108         } else {
109                 dragging_position = false;
110         }
111
112         if (ev->type == GDK_2BUTTON_PRESS) {
113                 if (dragging_position) {
114                         cerr << "Reset pos\n";
115                         position_control->set_value (0.5); // reset position to center
116                 } else {
117                         cerr << "Reset width\n";
118                         width_control->set_value (1.0); // reset position to full, LR
119                 }
120                 dragging = false;
121         } else {
122                 dragging = true;
123         }
124
125         return true;
126 }
127
128 bool
129 StereoPanner::on_button_release_event (GdkEventButton* ev)
130 {
131         dragging = false;
132         dragging_position = false;
133         return true;
134 }
135
136 bool
137 StereoPanner::on_motion_notify_event (GdkEventMotion* ev)
138 {
139         if (!dragging) {
140                 return false;
141         }
142
143         int w = get_width();
144         float delta = (abs (ev->x - last_drag_x)) / (double) (w/2);
145
146         if (!dragging_position) {
147                 double wv = width_control->get_value();        
148                 
149                 if (((drag_start_x < w/2) && ev->x > last_drag_x) || // start left of center, move towards it
150                     ((drag_start_x > w/2) && ev->x < last_drag_x)) { // start right of center, move towards it
151                         wv = wv  * (1.0 - delta);
152                 } else {
153                         /* moving out, so increase the width */
154                         wv = wv * (1.0 + delta);
155                 }
156                 
157                 width_control->set_value (wv);
158
159         } else {
160
161                 double pv = position_control->get_value(); // 0..1.0 ; 0 = left
162                 
163                 if (ev->x > last_drag_x) { // increasing 
164                         pv = pv * (1.0 + delta);
165                 } else {
166                         pv = pv * (1.0 - delta);
167                 }
168
169                 position_control->set_value (pv);
170         }
171
172         last_drag_x = ev->x;
173         return true;
174 }