Fix crash when resizing a track quickly.
[ardour.git] / gtk2_ardour / time_axis_view.h
1 /*
2     Copyright (C) 2003 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 __ardour_gtk_time_axis_h__
21 #define __ardour_gtk_time_axis_h__
22
23 #include <vector>
24 #include <list>
25
26 #include <gtkmm/box.h>
27 #include <gtkmm/fixed.h>
28 #include <gtkmm/frame.h>
29 #include <gtkmm/drawingarea.h>
30 #include <gtkmm/eventbox.h>
31 #include <gtkmm/table.h>
32 #include <gtkmm/entry.h>
33 #include <gtkmm/label.h>
34 #include <gtkmm/sizegroup.h>
35
36 #include <gtkmm2ext/focus_entry.h>
37
38 #include "pbd/stateful.h"
39 #include "pbd/signals.h"
40
41 #include "ardour/types.h"
42 #include "ardour/region.h"
43 #include "evoral/Parameter.hpp"
44
45 #include "canvas/line.h"
46
47 #include "prompter.h"
48 #include "axis_view.h"
49 #include "enums.h"
50 #include "editing.h"
51
52 namespace ARDOUR {
53         class Session;
54         class Region;
55         class Session;
56         class RouteGroup;
57         class Playlist;
58 }
59
60 namespace Gtk {
61         class Menu;
62 }
63
64 namespace ArdourCanvas {
65         class Canvas;
66         class Container;
67         class Item;
68 }
69
70 class PublicEditor;
71 class RegionSelection;
72 class TimeSelection;
73 class PointSelection;
74 class TimeAxisViewItem;
75 class Selection;
76 class Selectable;
77 class RegionView;
78 class GhostRegion;
79 class StreamView;
80 class ArdourDialog;
81 class ItemCounts;
82 class PasteContext;
83
84 /** Abstract base class for time-axis views (horizontal editor 'strips')
85  *
86  * This class provides the basic LHS controls and display methods. This should be
87  * extended to create functional time-axis based views.
88  */
89 class TimeAxisView : public virtual AxisView
90 {
91         private:
92         enum NamePackingBits {
93                 NameLabelPacked = 0x1,
94                 NameEntryPacked = 0x2
95         };
96
97         public:
98         TimeAxisView(ARDOUR::Session* sess, PublicEditor& ed, TimeAxisView* parent, ArdourCanvas::Canvas& canvas);
99         virtual ~TimeAxisView ();
100
101         static PBD::Signal1<void,TimeAxisView*> CatchDeletion;
102
103         /** @return index of this TimeAxisView within its parent */
104         int order () const { return _order; }
105
106         /** @return maximum allowable value of order */
107         static int max_order () { return _max_order; }
108
109         virtual void enter_internal_edit_mode () {}
110         virtual void leave_internal_edit_mode () {}
111
112         ArdourCanvas::Container* canvas_display () { return _canvas_display; }
113         ArdourCanvas::Container* ghost_group () { return _ghost_group; }
114
115         /** @return effective height (taking children into account) in canvas units, or
116                 0 if this TimeAxisView has not yet been shown */
117         uint32_t effective_height () const { return _effective_height; }
118
119         /** @return y position, or -1 if hidden */
120         double y_position () const { return _y_position; }
121
122         /** @return our Editor */
123         PublicEditor& editor () const { return _editor; }
124
125         uint32_t current_height() const { return height; }
126
127         void idle_resize (int32_t);
128
129         virtual guint32 show_at (double y, int& nth, Gtk::VBox *parent);
130         virtual void hide ();
131
132         bool touched (double top, double bot);
133
134         /** @return true if hidden, otherwise false */
135         bool hidden () const { return _hidden; }
136
137         void set_selected (bool);
138
139         /**
140          * potential handler for entered events
141          */
142
143         virtual void entered () {}
144         virtual void exited () {}
145
146         virtual void set_height (uint32_t h);
147         void set_height_enum (Height, bool apply_to_selection = false);
148         void reset_height();
149
150         virtual void reset_visual_state ();
151
152         std::pair<TimeAxisView*, double> covers_y_position (double) const;
153         bool covered_by_y_range (double y0, double y1) const;
154
155         virtual void step_height (bool);
156
157         virtual ARDOUR::RouteGroup* route_group() const { return 0; }
158         virtual boost::shared_ptr<ARDOUR::Playlist> playlist() const { return boost::shared_ptr<ARDOUR::Playlist> (); }
159
160         virtual void set_samples_per_pixel (double);
161         virtual void show_selection (TimeSelection&);
162         virtual void hide_selection ();
163         virtual void reshow_selection (TimeSelection&);
164         virtual void show_timestretch (framepos_t start, framepos_t end, int layers, int layer);
165         virtual void hide_timestretch ();
166
167         /* editing operations */
168
169         virtual void cut_copy_clear (Selection&, Editing::CutCopyOp) {}
170
171         /** Paste a selection.
172          *  @param pos Position to paste to (session frames).
173          *  @param selection Selection to paste.
174          *  @param ctx Paste context.
175          */
176         virtual bool paste (ARDOUR::framepos_t pos,
177                             const Selection&   selection,
178                             PasteContext&      ctx) { return false; }
179
180         virtual void set_selected_regionviews (RegionSelection&) {}
181         virtual void set_selected_points (PointSelection&) {}
182
183         virtual void fade_range (TimeSelection&) {}
184
185         virtual boost::shared_ptr<ARDOUR::Region> find_next_region (framepos_t /*pos*/, ARDOUR::RegionPoint, int32_t /*dir*/) {
186                 return boost::shared_ptr<ARDOUR::Region> ();
187         }
188
189         void order_selection_trims (ArdourCanvas::Item *item, bool put_start_on_top);
190
191         virtual void get_selectables (ARDOUR::framepos_t, ARDOUR::framepos_t, double, double, std::list<Selectable*>&);
192         virtual void get_inverted_selectables (Selection&, std::list<Selectable *>& results);
193
194         void add_ghost (RegionView*);
195         void remove_ghost (RegionView*);
196         void erase_ghost (GhostRegion*);
197
198         /** called at load time when first GUI idle occurs. put
199             expensive data loading/redisplay code in here. */
200         virtual void first_idle () {}
201
202         TimeAxisView* get_parent () { return parent; }
203         void set_parent (TimeAxisView& p);
204
205         virtual LayerDisplay layer_display () const { return Overlaid; }
206         virtual StreamView* view () const { return 0; }
207
208         typedef std::vector<boost::shared_ptr<TimeAxisView> > Children;
209         Children get_child_list ();
210
211         SelectionRect* get_selection_rect(uint32_t id);
212
213         static uint32_t preset_height (Height);
214
215         protected:
216         static Glib::RefPtr<Gtk::SizeGroup> controls_meters_size_group;
217         static Glib::RefPtr<Gtk::SizeGroup> midi_scroomer_size_group;
218         static unsigned int name_width_px;
219         /* The Standard LHS Controls */
220         Gtk::Table             controls_table;
221         Glib::RefPtr<Gtk::SizeGroup> controls_button_size_group;
222         Gtk::EventBox          controls_ebox;
223         Gtk::VBox              controls_vbox;
224         Gtk::VBox              time_axis_vbox;
225         Gtk::HBox              time_axis_hbox;
226         Gtk::Frame             time_axis_frame;
227         Gtk::HBox              name_hbox;
228         Gtk::HBox              top_hbox;
229         Gtk::Label             name_label;
230         Gtk::Fixed             scroomer_placeholder;
231         bool                  _name_editing;
232         uint32_t               height;  /* in canvas units */
233         std::string            controls_base_unselected_name;
234         std::string            controls_base_selected_name;
235         Gtk::Menu*             display_menu; /* The standard LHS Track control popup-menus */
236         TimeAxisView*          parent;
237         ArdourCanvas::Container*   selection_group;
238         ArdourCanvas::Container*  _ghost_group;
239         std::list<GhostRegion*> ghosts;
240         std::list<SelectionRect*> free_selection_rects;
241         std::list<SelectionRect*> used_selection_rects;
242         bool                  _hidden;
243         bool                   in_destructor;
244         Gtk::Menu*            _size_menu;
245         ArdourCanvas::Line*       _canvas_separator;
246         ArdourCanvas::Container*  _canvas_display;
247         double                _y_position;
248         PublicEditor&         _editor;
249
250         virtual bool can_edit_name() const;
251
252         bool name_entry_key_release (GdkEventKey *ev);
253         bool name_entry_key_press (GdkEventKey *ev);
254         bool name_entry_focus_out (GdkEventFocus *ev);
255
256         Gtk::Entry* name_entry;
257         void begin_name_edit ();
258         void end_name_edit (int);
259
260         /* derived classes can override these */
261
262         virtual void name_entry_changed ();
263
264         /** Handle mouse relaese on our LHS control name ebox.
265          *
266          *@ param ev the event
267          */
268         virtual bool controls_ebox_button_release (GdkEventButton*);
269         virtual bool controls_ebox_scroll (GdkEventScroll*);
270         virtual bool controls_ebox_button_press (GdkEventButton*);
271         virtual bool controls_ebox_motion (GdkEventMotion*);
272         virtual bool controls_ebox_leave (GdkEventCrossing*);
273
274         /** Display the standard LHS control menu at when.
275          *
276          * @param when the popup activation time
277          */
278         virtual void popup_display_menu (guint32 when);
279
280         /** Build the standard LHS control menu.
281          * Subclasses should extend this method to add their own menu options.
282          */
283         virtual void build_display_menu ();
284
285         /** Do whatever needs to be done to dynamically reset the LHS control menu.
286          */
287         virtual bool handle_display_menu_map_event (GdkEventAny * /*ev*/) { return false; }
288
289         Children children;
290         bool is_child (TimeAxisView*);
291
292         virtual void remove_child (boost::shared_ptr<TimeAxisView>);
293         void add_child (boost::shared_ptr<TimeAxisView>);
294
295         /* selection display */
296
297         virtual void selection_click (GdkEventButton*);
298
299         void color_handler ();
300
301         void conditionally_add_to_selection ();
302
303         void build_size_menu ();
304
305 private:
306         Gtk::VBox*            control_parent;
307         int                  _order;
308         uint32_t             _effective_height;
309         double               _resize_drag_start;
310         bool                 _did_resize;
311         GdkCursor*           _preresize_cursor;
312         bool                 _have_preresize_cursor;
313         bool                 _ebox_release_can_act;
314
315         static uint32_t button_height;
316         static uint32_t extra_height;
317         static int const _max_order;
318
319         void compute_heights ();
320         bool maybe_set_cursor (int y);
321
322 }; /* class TimeAxisView */
323
324 #endif /* __ardour_gtk_time_axis_h__ */