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