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