911b9a7df6ee55bdd25be1ff3023f314895d6a06
[ardour.git] / gtk2_ardour / crossfade_edit.h
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 #ifndef __gtk_ardour_xfade_edit_h__
21 #define __gtk_ardour_xfade_edit_h__
22
23 #include <list>
24
25 #include <gtkmm/box.h>
26 #include <gtkmm/button.h>
27 #include <gtkmm/radiobutton.h>
28
29 #include "canvas/canvas.h"
30
31 #include "evoral/Curve.hpp"
32 #include "ardour/session_handle.h"
33
34 #include "ardour_dialog.h"
35
36 namespace ARDOUR
37 {
38         class Session;
39         class AutomationList;
40         class Crossfade;
41 }
42
43 namespace ArdourCanvas {
44         class Rectangle;
45         class Line;
46         class Polygon;
47         class WaveView;
48 }
49
50 class CrossfadeEditor : public ArdourDialog
51 {
52 public:
53         CrossfadeEditor (ARDOUR::Session*, boost::shared_ptr<ARDOUR::Crossfade>, double miny, double maxy);
54         ~CrossfadeEditor ();
55
56         void apply ();
57
58         static const double canvas_border;
59
60         /* these are public so that a caller/subclass can make them do the right thing. */
61
62         Gtk::Button* cancel_button;
63         Gtk::Button* ok_button;
64
65         struct PresetPoint {
66                 double x;
67                 double y;
68
69                 PresetPoint (double a, double b)
70                         : x (a), y (b) {}
71         };
72
73         struct Preset : public std::list<PresetPoint> {
74                 const char* name;
75                 const char* image_name;
76
77                 Preset (const char* n, const char* x) : name (n), image_name (x) {}
78         };
79
80         typedef std::list<Preset*> Presets;
81
82         static Presets* fade_in_presets;
83         static Presets* fade_out_presets;
84
85 protected:
86         bool on_key_press_event (GdkEventKey*);
87         bool on_key_release_event (GdkEventKey*);
88
89 private:
90         boost::shared_ptr<ARDOUR::Crossfade> xfade;
91
92         Gtk::VBox vpacker;
93
94         struct Point {
95                 ~Point();
96
97                 ArdourCanvas::Rectangle* box;
98                 ArdourCanvas::PolyLine* curve;
99                 double x;
100                 double y;
101
102                 static const int32_t size;
103
104                 void move_to (double x, double y, double xfract, double yfract);
105         };
106
107         struct PointSorter {
108                 bool operator() (const CrossfadeEditor::Point* a, const CrossfadeEditor::Point *b) {
109                         return a->x < b->x;
110                 }
111         };
112
113         ArdourCanvas::Rectangle*   toplevel;
114         ArdourCanvas::GtkCanvas* canvas;
115
116         struct Half {
117                 ArdourCanvas::PolyLine* line;
118                 ArdourCanvas::Polygon*  shading;
119                 std::list<Point*>       points;
120                 ARDOUR::AutomationList  normative_curve; /* 0 - 1.0, linear */
121                 ARDOUR::AutomationList  gain_curve;      /* 0 - 2.0, gain mapping */
122                 std::vector<ArdourCanvas::WaveView*>  waves;
123
124                 Half();
125         };
126
127         enum WhichFade {
128                 In = 0,
129                 Out = 1
130         };
131
132         Half fade[2];
133         WhichFade current;
134
135         bool point_grabbed;
136         std::vector<Gtk::Button*> fade_out_buttons;
137         std::vector<Gtk::Button*> fade_in_buttons;
138
139         Gtk::VBox vpacker2;
140
141         Gtk::Button clear_button;
142         Gtk::Button revert_button;
143
144         Gtk::ToggleButton audition_both_button;
145         Gtk::ToggleButton audition_left_dry_button;
146         Gtk::ToggleButton audition_left_button;
147         Gtk::ToggleButton audition_right_dry_button;
148         Gtk::ToggleButton audition_right_button;
149
150         Gtk::ToggleButton preroll_button;
151         Gtk::ToggleButton postroll_button;
152
153         Gtk::HBox roll_box;
154
155         gint event_handler (GdkEvent*);
156
157         bool canvas_event (GdkEvent* event);
158         bool point_event (GdkEvent* event, Point*);
159         bool curve_event (GdkEvent* event);
160
161         void canvas_allocation (Gtk::Allocation&);
162         void add_control_point (double x, double y);
163         Point* make_point ();
164         void redraw ();
165
166         double effective_width () const { return canvas->get_allocation().get_width() - (2.0 * canvas_border); }
167         double effective_height () const { return canvas->get_allocation().get_height() - (2.0 * canvas_border); }
168
169         void clear ();
170         void reset ();
171
172         double miny;
173         double maxy;
174
175         Gtk::Table fade_in_table;
176         Gtk::Table fade_out_table;
177
178         void build_presets ();
179         void apply_preset (Preset*);
180
181         Gtk::RadioButton select_in_button;
182         Gtk::RadioButton select_out_button;
183         Gtk::HBox   curve_button_box;
184         Gtk::HBox   audition_box;
185
186         void curve_select_clicked (WhichFade);
187
188         double x_coordinate (double& xfract) const;
189         double y_coordinate (double& yfract) const;
190
191         void set (const ARDOUR::AutomationList& alist, WhichFade);
192
193         PBD::ScopedConnection* _peaks_ready_connection;
194         PBD::ScopedConnection state_connection;
195
196         void make_waves (boost::shared_ptr<ARDOUR::AudioRegion>, WhichFade);
197         void peaks_ready (boost::weak_ptr<ARDOUR::AudioRegion> r, WhichFade);
198
199         void _apply_to (boost::shared_ptr<ARDOUR::Crossfade> xf);
200         void setup (boost::shared_ptr<ARDOUR::Crossfade>);
201         void cancel_audition ();
202         void audition_state_changed (bool);
203
204         enum Audition {
205                 Both,
206                 Left,
207                 Right
208         };
209
210         void audition_toggled ();
211         void audition_right_toggled ();
212         void audition_right_dry_toggled ();
213         void audition_left_toggled ();
214         void audition_left_dry_toggled ();
215
216         void audition (Audition);
217         void audition_both ();
218         void audition_left_dry ();
219         void audition_left ();
220         void audition_right_dry ();
221         void audition_right ();
222
223         void xfade_changed (const PBD::PropertyChange&);
224
225         void dump ();
226 };
227
228 #endif /* __gtk_ardour_xfade_edit_h__ */