add validation checks on TC.hours, BBT.ticks and validate minsec data entry
[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 "cairo_widget.h"
37
38 namespace ARDOUR {
39         class Session;
40 }
41
42 class AudioClock : public CairoWidget, public ARDOUR::SessionHandlePtr
43 {
44   public:
45         enum Mode {
46                 Timecode,
47                 BBT,
48                 MinSec,
49                 Frames
50         };
51
52         AudioClock (const std::string& clock_name, bool is_transient, const std::string& widget_name,
53                     bool editable, bool follows_playhead, bool duration = false, bool with_info = false);
54         ~AudioClock ();
55
56         Mode mode() const { return _mode; }
57         void set_off (bool yn);
58         bool off() const { return _off; }
59         void set_widget_name (const std::string& name);
60         void set_active_state (Gtkmm2ext::ActiveState s);
61         void set_editable (bool yn);
62         void set_corner_radius (double);
63         void set_fixed_width (bool);
64
65         void focus ();
66
67         void set (framepos_t, bool force = false, ARDOUR::framecnt_t offset = 0);
68         void set_from_playhead ();
69         void locate ();
70         void set_mode (Mode);
71         void set_bbt_reference (framepos_t);
72         void set_is_duration (bool);
73
74         std::string name() const { return _name; }
75
76         framepos_t current_time (framepos_t position = 0) const;
77         framepos_t current_duration (framepos_t position = 0) const;
78         void set_session (ARDOUR::Session *s);
79
80         sigc::signal<void> ValueChanged;
81         sigc::signal<void> mode_changed;
82         sigc::signal<void> ChangeAborted;
83
84         static sigc::signal<void> ModeChanged;
85         static std::vector<AudioClock*> clocks;
86
87   protected:
88         void render (cairo_t*);
89
90   private:
91         Mode             _mode;
92         std::string      _name;
93         bool              is_transient;
94         bool              is_duration;
95         bool              editable;
96         /** true if this clock follows the playhead, meaning that certain operations are redundant */
97         bool             _follows_playhead;
98         bool             _off;
99         bool             _fixed_width;
100         int              layout_x_offset;
101         int              em_width;
102
103         Gtk::Menu  *ops_menu;
104
105         Glib::RefPtr<Pango::Layout> _layout;
106         Glib::RefPtr<Pango::Layout> _left_layout;
107         Glib::RefPtr<Pango::Layout> _right_layout;
108
109         Pango::AttrColor*    editing_attr;
110         Pango::AttrColor*    foreground_attr;
111
112         Pango::AttrList normal_attributes;
113         Pango::AttrList editing_attributes;
114         Pango::AttrList info_attributes;
115
116         int first_height;
117         int first_width;
118         int layout_height;
119         int layout_width;
120         int info_height;
121         int upper_height;
122         double mode_based_info_ratio;
123         double corner_radius;
124
125         static const double info_font_scale_factor;
126         static const double separator_height;
127         static const double x_leading_padding;
128
129         enum Field {
130                 Timecode_Hours,
131                 Timecode_Minutes,
132                 Timecode_Seconds,
133                 Timecode_Frames,
134                 MS_Hours,
135                 MS_Minutes,
136                 MS_Seconds,
137                 MS_Milliseconds,
138                 Bars,
139                 Beats,
140                 Ticks,
141                 AudioFrames,
142         };
143
144         Field index_to_field (int index) const;
145
146         /* this maps the number of input characters/digits when editing
147            to a cursor position. insert_map[N] = index of character/digit
148            where the cursor should be after N chars/digits. it is 
149            mode specific and so it is filled during set_mode().
150         */
151
152         std::vector<int> insert_map;
153
154         bool editing;
155         std::string edit_string;
156         std::string pre_edit_string;
157         std::string input_string;
158
159         framepos_t bbt_reference_time;
160         framepos_t last_when;
161         bool last_pdelta;
162         bool last_sdelta;
163
164         bool dragging;
165         double drag_start_y;
166         double drag_y;
167         double drag_accum;
168         Field  drag_field;
169
170         void on_realize ();
171         bool on_key_press_event (GdkEventKey *);
172         bool on_key_release_event (GdkEventKey *);
173         bool on_scroll_event (GdkEventScroll *ev);
174         bool on_button_press_event (GdkEventButton *ev);
175         bool on_button_release_event(GdkEventButton *ev);
176         void on_style_changed (const Glib::RefPtr<Gtk::Style>&);
177         void on_size_request (Gtk::Requisition* req);
178         bool on_motion_notify_event (GdkEventMotion *ev);
179         void on_size_allocate (Gtk::Allocation&);
180         bool on_focus_out_event (GdkEventFocus*);
181
182         void set_timecode (framepos_t, bool);
183         void set_bbt (framepos_t, bool);
184         void set_minsec (framepos_t, bool);
185         void set_frames (framepos_t, bool);
186
187         framepos_t get_frame_step (Field, framepos_t pos = 0, int dir = 1);
188
189         bool timecode_validate_edit (const std::string&);
190         bool bbt_validate_edit (const std::string&);
191         bool minsec_validate_edit (const std::string&);
192
193         framepos_t frames_from_timecode_string (const std::string&) const;
194         framepos_t frames_from_bbt_string (framepos_t, const std::string&) const;
195         framepos_t frame_duration_from_bbt_string (framepos_t, const std::string&) const;
196         framepos_t frames_from_minsec_string (const std::string&) const;
197         framepos_t frames_from_audioframes_string (const std::string&) const;
198
199         void build_ops_menu ();
200
201         void session_configuration_changed (std::string);
202
203         Field index_to_field () const;
204
205         void start_edit ();
206         void end_edit (bool);
207         void end_edit_relative (bool);
208         void edit_next_field ();
209         ARDOUR::framecnt_t parse_as_distance (const std::string&);
210
211         ARDOUR::framecnt_t parse_as_timecode_distance (const std::string&);
212         ARDOUR::framecnt_t parse_as_minsec_distance (const std::string&);
213         ARDOUR::framecnt_t parse_as_bbt_distance (const std::string&);
214         ARDOUR::framecnt_t parse_as_frames_distance (const std::string&);
215         
216         void set_font ();
217         void set_colors ();
218         void show_edit_status (int length);
219
220         void drop_focus ();
221         void dpi_reset ();
222
223         double bg_r, bg_g, bg_b, bg_a;
224         double cursor_r, cursor_g, cursor_b, cursor_a;
225 };
226
227 #endif /* __audio_clock_h__ */