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