Optimize automation-event process splitting
[ardour.git] / gtk2_ardour / audio_clock.h
1 /*
2     Copyright (C) 1999 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 __audio_clock_h__
21 #define __audio_clock_h__
22
23 #include <map>
24 #include <vector>
25
26 #include <pangomm.h>
27
28 #include <gtkmm/alignment.h>
29 #include <gtkmm/box.h>
30 #include <gtkmm/menu.h>
31 #include <gtkmm/label.h>
32
33 #include "ardour/ardour.h"
34 #include "ardour/session_handle.h"
35
36 #include "gtkmm2ext/cairo_widget.h"
37 #include "widgets/ardour_button.h"
38
39 namespace ARDOUR {
40         class Session;
41 }
42
43 class AudioClock : public CairoWidget, public ARDOUR::SessionHandlePtr
44 {
45         public:
46         enum Mode {
47                 Timecode,
48                 BBT,
49                 MinSec,
50                 Seconds,
51                 Samples
52         };
53
54         AudioClock (const std::string& clock_name, bool is_transient, const std::string& widget_name,
55                     bool editable, bool follows_playhead, bool duration = false, bool with_info = false,
56                     bool accept_on_focus_out = false);
57         ~AudioClock ();
58
59         Mode mode() const { return _mode; }
60         void set_off (bool yn);
61         bool off() const { return _off; }
62         bool on() const { return !_off; }
63         void set_widget_name (const std::string& name);
64         void set_active_state (Gtkmm2ext::ActiveState s);
65         void set_editable (bool yn);
66         void set_corner_radius (double);
67
68         void focus ();
69
70         virtual void set (samplepos_t, bool force = false, ARDOUR::samplecnt_t offset = 0);
71         void set_from_playhead ();
72         void locate ();
73         void set_mode (Mode, bool noemit = false);
74         void set_bbt_reference (samplepos_t);
75         void set_is_duration (bool);
76
77         void copy_text_to_clipboard () const;
78
79         std::string name() const { return _name; }
80
81         samplepos_t current_time (samplepos_t position = 0) const;
82         samplepos_t current_duration (samplepos_t position = 0) const;
83         void set_session (ARDOUR::Session *s);
84         void set_negative_allowed (bool yn);
85
86         ArdourWidgets::ArdourButton* left_btn () { return &_left_btn; }
87         ArdourWidgets::ArdourButton* right_btn () { return &_right_btn; }
88
89         /** Alter cairo scaling during rendering.
90          *
91          * Used by clocks that resize themselves
92          * to fit any given space. Can lead
93          * to font distortion.
94          */
95         void set_scale (double x, double y);
96
97         static void print_minsec (samplepos_t, char* buf, size_t bufsize, float sample_rate);
98
99         sigc::signal<void> ValueChanged;
100         sigc::signal<void> mode_changed;
101         sigc::signal<void> ChangeAborted;
102
103         static sigc::signal<void> ModeChanged;
104         static std::vector<AudioClock*> clocks;
105
106         protected:
107         void render (Cairo::RefPtr<Cairo::Context> const&, cairo_rectangle_t*);
108         bool get_is_duration () const { return is_duration; }
109         ARDOUR::samplecnt_t offset () const { return _offset; }
110
111         virtual void build_ops_menu ();
112         Gtk::Menu  *ops_menu;
113
114         bool on_button_press_event (GdkEventButton *ev);
115         bool on_button_release_event(GdkEventButton *ev);
116
117         ArdourWidgets::ArdourButton _left_btn;
118         ArdourWidgets::ArdourButton _right_btn;
119
120         private:
121         Mode             _mode;
122         std::string      _name;
123         bool              is_transient;
124         bool              is_duration;
125         bool              editable;
126         /** true if this clock follows the playhead, meaning that certain operations are redundant */
127         bool             _follows_playhead;
128         bool             _accept_on_focus_out;
129         bool             _off;
130         int              em_width;
131         bool             _edit_by_click_field;
132         bool             _negative_allowed;
133         bool             edit_is_negative;
134
135         samplepos_t       _limit_pos;
136
137         ARDOUR::samplecnt_t _offset;
138
139         Glib::RefPtr<Pango::Layout> _layout;
140
141         bool         _with_info;
142
143         Pango::AttrColor*    editing_attr;
144         Pango::AttrColor*    foreground_attr;
145
146         Pango::AttrList normal_attributes;
147         Pango::AttrList editing_attributes;
148
149         int first_height;
150         int first_width;
151         bool style_resets_first;
152         int layout_height;
153         int layout_width;
154         double corner_radius;
155         uint32_t font_size;
156
157         static const double info_font_scale_factor;
158         static const double separator_height;
159         static const double x_leading_padding;
160
161         enum Field {
162                 Timecode_Hours = 1,
163                 Timecode_Minutes,
164                 Timecode_Seconds,
165                 Timecode_frames,
166                 MS_Hours,
167                 MS_Minutes,
168                 MS_Seconds,
169                 MS_Milliseconds,
170                 Bars,
171                 Beats,
172                 Ticks,
173                 SS_Seconds,
174                 SS_Deciseconds,
175                 S_Samples,
176         };
177
178         Field index_to_field (int index) const;
179
180         /* this maps the number of input characters/digits when editing
181            to a cursor position. insert_map[N] = index of character/digit
182            where the cursor should be after N chars/digits. it is
183            mode specific and so it is filled during set_mode().
184         */
185
186         std::vector<int> insert_map;
187
188         bool editing;
189         std::string edit_string;
190         std::string pre_edit_string;
191         std::string input_string;
192
193         samplepos_t bbt_reference_time;
194         samplepos_t last_when;
195         bool last_pdelta;
196         bool last_sdelta;
197
198         bool dragging;
199         double drag_start_y;
200         double drag_y;
201         double drag_accum;
202         Field  drag_field;
203
204         void on_realize ();
205         bool on_key_press_event (GdkEventKey *);
206         bool on_key_release_event (GdkEventKey *);
207         bool on_scroll_event (GdkEventScroll *ev);
208         void on_style_changed (const Glib::RefPtr<Gtk::Style>&);
209         void on_size_request (Gtk::Requisition* req);
210         bool on_motion_notify_event (GdkEventMotion *ev);
211         bool on_focus_out_event (GdkEventFocus*);
212
213         void set_slave_info ();
214         void set_timecode (samplepos_t, bool);
215         void set_bbt (samplepos_t, ARDOUR::samplecnt_t, bool);
216         void set_minsec (samplepos_t, bool);
217         void set_seconds (samplepos_t, bool);
218         void set_samples (samplepos_t, bool);
219         void set_out_of_bounds (bool negative);
220
221         void set_clock_dimensions (Gtk::Requisition&);
222
223         samplepos_t get_sample_step (Field, samplepos_t pos = 0, int dir = 1);
224
225         bool timecode_validate_edit (const std::string&);
226         bool bbt_validate_edit (std::string&);
227         bool minsec_validate_edit (const std::string&);
228
229         samplepos_t samples_from_timecode_string (const std::string&) const;
230         samplepos_t samples_from_bbt_string (samplepos_t, const std::string&) const;
231         samplepos_t sample_duration_from_bbt_string (samplepos_t, const std::string&) const;
232         samplepos_t samples_from_minsec_string (const std::string&) const;
233         samplepos_t samples_from_seconds_string (const std::string&) const;
234         samplepos_t samples_from_audiosamples_string (const std::string&) const;
235
236         void session_configuration_changed (std::string);
237         void session_property_changed (const PBD::PropertyChange&);
238
239         Field index_to_field () const;
240
241         void start_edit (Field f = Field (0));
242         void end_edit (bool);
243         void end_edit_relative (bool);
244         void edit_next_field ();
245         ARDOUR::samplecnt_t parse_as_distance (const std::string&);
246
247         ARDOUR::samplecnt_t parse_as_timecode_distance (const std::string&);
248         ARDOUR::samplecnt_t parse_as_minsec_distance (const std::string&);
249         ARDOUR::samplecnt_t parse_as_bbt_distance (const std::string&);
250         ARDOUR::samplecnt_t parse_as_seconds_distance (const std::string&);
251         ARDOUR::samplecnt_t parse_as_samples_distance (const std::string&);
252
253         void set_font (Pango::FontDescription);
254         void set_colors ();
255         void show_edit_status (int length);
256         int  merge_input_and_edit_string ();
257         std::string get_field (Field);
258
259         void drop_focus ();
260         void dpi_reset ();
261
262         double bg_r, bg_g, bg_b, bg_a;
263         double cursor_r, cursor_g, cursor_b, cursor_a;
264
265         double xscale;
266         double yscale;
267 };
268
269 #endif /* __audio_clock_h__ */