Add API allowing plugin preset load to affect automation
[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 "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                 Frames
51         };
52
53         AudioClock (const std::string& clock_name, bool is_transient, const std::string& widget_name,
54                     bool editable, bool follows_playhead, bool duration = false, bool with_info = false,
55                     bool accept_on_focus_out = false);
56         ~AudioClock ();
57
58         Mode mode() const { return _mode; }
59         void set_off (bool yn);
60         bool off() const { return _off; }
61         void set_widget_name (const std::string& name);
62         void set_active_state (Gtkmm2ext::ActiveState s);
63         void set_editable (bool yn);
64         void set_corner_radius (double);
65
66         void focus ();
67
68         void set (framepos_t, bool force = false, ARDOUR::framecnt_t offset = 0);
69         void set_from_playhead ();
70         void locate ();
71         void set_mode (Mode, bool noemit = false);
72         void set_bbt_reference (framepos_t);
73         void set_is_duration (bool);
74
75         void copy_text_to_clipboard () const;
76
77         std::string name() const { return _name; }
78
79         framepos_t current_time (framepos_t position = 0) const;
80         framepos_t current_duration (framepos_t position = 0) const;
81         void set_session (ARDOUR::Session *s);
82         void set_negative_allowed (bool yn);
83
84         ArdourButton* left_btn () { return &_left_btn; }
85         ArdourButton* right_btn () { return &_right_btn; }
86
87         /** Alter cairo scaling during rendering.
88          *
89          * Used by clocks that resize themselves
90          * to fit any given space. Can lead
91          * to font distortion.
92          */
93         void set_scale (double x, double y);
94
95         static void print_minsec (framepos_t, char* buf, size_t bufsize, float frame_rate);
96
97         sigc::signal<void> ValueChanged;
98         sigc::signal<void> mode_changed;
99         sigc::signal<void> ChangeAborted;
100
101         static sigc::signal<void> ModeChanged;
102         static std::vector<AudioClock*> clocks;
103
104         protected:
105         void render (cairo_t*, cairo_rectangle_t*);
106         bool get_is_duration () const { return is_duration; } ;
107
108         virtual void build_ops_menu ();
109         Gtk::Menu  *ops_menu;
110
111         bool on_button_press_event (GdkEventButton *ev);
112         bool on_button_release_event(GdkEventButton *ev);
113
114         ArdourButton _left_btn;
115         ArdourButton _right_btn;
116
117         private:
118         Mode             _mode;
119         std::string      _name;
120         bool              is_transient;
121         bool              is_duration;
122         bool              editable;
123         /** true if this clock follows the playhead, meaning that certain operations are redundant */
124         bool             _follows_playhead;
125         bool             _accept_on_focus_out;
126         bool             _off;
127         int              em_width;
128         bool             _edit_by_click_field;
129         bool             _negative_allowed;
130         bool             edit_is_negative;
131
132         Glib::RefPtr<Pango::Layout> _layout;
133
134         bool         _with_info;
135
136         Pango::AttrColor*    editing_attr;
137         Pango::AttrColor*    foreground_attr;
138
139         Pango::AttrList normal_attributes;
140         Pango::AttrList editing_attributes;
141
142         int first_height;
143         int first_width;
144         bool style_resets_first;
145         int layout_height;
146         int layout_width;
147         double corner_radius;
148         uint32_t font_size;
149
150         static const double info_font_scale_factor;
151         static const double separator_height;
152         static const double x_leading_padding;
153
154         enum Field {
155                 Timecode_Hours = 1,
156                 Timecode_Minutes,
157                 Timecode_Seconds,
158                 Timecode_Frames,
159                 MS_Hours,
160                 MS_Minutes,
161                 MS_Seconds,
162                 MS_Milliseconds,
163                 Bars,
164                 Beats,
165                 Ticks,
166                 AudioFrames,
167         };
168
169         Field index_to_field (int index) const;
170
171         /* this maps the number of input characters/digits when editing
172            to a cursor position. insert_map[N] = index of character/digit
173            where the cursor should be after N chars/digits. it is
174            mode specific and so it is filled during set_mode().
175         */
176
177         std::vector<int> insert_map;
178
179         bool editing;
180         std::string edit_string;
181         std::string pre_edit_string;
182         std::string input_string;
183
184         framepos_t bbt_reference_time;
185         framepos_t last_when;
186         bool last_pdelta;
187         bool last_sdelta;
188
189         bool dragging;
190         double drag_start_y;
191         double drag_y;
192         double drag_accum;
193         Field  drag_field;
194
195         void on_realize ();
196         bool on_key_press_event (GdkEventKey *);
197         bool on_key_release_event (GdkEventKey *);
198         bool on_scroll_event (GdkEventScroll *ev);
199         void on_style_changed (const Glib::RefPtr<Gtk::Style>&);
200         void on_size_request (Gtk::Requisition* req);
201         bool on_motion_notify_event (GdkEventMotion *ev);
202         bool on_focus_out_event (GdkEventFocus*);
203
204         void set_slave_info ();
205         void set_timecode (framepos_t, bool);
206         void set_bbt (framepos_t, ARDOUR::framecnt_t, bool);
207         void set_minsec (framepos_t, bool);
208         void set_frames (framepos_t, bool);
209
210         void set_clock_dimensions (Gtk::Requisition&);
211
212         framepos_t get_frame_step (Field, framepos_t pos = 0, int dir = 1);
213
214         bool timecode_validate_edit (const std::string&);
215         bool bbt_validate_edit (const std::string&);
216         bool minsec_validate_edit (const std::string&);
217
218         framepos_t frames_from_timecode_string (const std::string&) const;
219         framepos_t frames_from_bbt_string (framepos_t, const std::string&) const;
220         framepos_t frame_duration_from_bbt_string (framepos_t, const std::string&) const;
221         framepos_t frames_from_minsec_string (const std::string&) const;
222         framepos_t frames_from_audioframes_string (const std::string&) const;
223
224         void session_configuration_changed (std::string);
225         void session_property_changed (const PBD::PropertyChange&);
226
227         Field index_to_field () const;
228
229         void start_edit (Field f = Field (0));
230         void end_edit (bool);
231         void end_edit_relative (bool);
232         void edit_next_field ();
233         ARDOUR::framecnt_t parse_as_distance (const std::string&);
234
235         ARDOUR::framecnt_t parse_as_timecode_distance (const std::string&);
236         ARDOUR::framecnt_t parse_as_minsec_distance (const std::string&);
237         ARDOUR::framecnt_t parse_as_bbt_distance (const std::string&);
238         ARDOUR::framecnt_t parse_as_frames_distance (const std::string&);
239
240         void set_font (Pango::FontDescription);
241         void set_colors ();
242         void show_edit_status (int length);
243         int  merge_input_and_edit_string ();
244         std::string get_field (Field);
245
246         void drop_focus ();
247         void dpi_reset ();
248
249         double bg_r, bg_g, bg_b, bg_a;
250         double cursor_r, cursor_g, cursor_b, cursor_a;
251
252         double xscale;
253         double yscale;
254 };
255
256 #endif /* __audio_clock_h__ */