show slave info in Min:Sec mode - fill empty space
[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         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         virtual void build_ops_menu ();
91         Gtk::Menu  *ops_menu;
92
93   private:
94         Mode             _mode;
95         std::string      _name;
96         bool              is_transient;
97         bool              is_duration;
98         bool              editable;
99         /** true if this clock follows the playhead, meaning that certain operations are redundant */
100         bool             _follows_playhead;
101         bool             _off;
102         bool             _fixed_width;
103         int              layout_x_offset;
104         int              em_width;
105         bool             _edit_by_click_field;
106         int              _mode_width[4]; /* enum Mode entries */
107
108         Glib::RefPtr<Pango::Layout> _layout;
109         Glib::RefPtr<Pango::Layout> _left_layout;
110         Glib::RefPtr<Pango::Layout> _right_layout;
111
112         Pango::AttrColor*    editing_attr;
113         Pango::AttrColor*    foreground_attr;
114
115         Pango::AttrList normal_attributes;
116         Pango::AttrList editing_attributes;
117         Pango::AttrList info_attributes;
118
119         int first_height;
120         int first_width;
121         int layout_height;
122         int layout_width;
123         int info_height;
124         int upper_height;
125         double mode_based_info_ratio;
126         double corner_radius;
127         uint32_t font_size;
128
129         static const double info_font_scale_factor;
130         static const double separator_height;
131         static const double x_leading_padding;
132
133         enum Field {
134                 Timecode_Hours = 1,
135                 Timecode_Minutes,
136                 Timecode_Seconds,
137                 Timecode_Frames,
138                 MS_Hours,
139                 MS_Minutes,
140                 MS_Seconds,
141                 MS_Milliseconds,
142                 Bars,
143                 Beats,
144                 Ticks,
145                 AudioFrames,
146         };
147
148         Field index_to_field (int index) const;
149
150         /* this maps the number of input characters/digits when editing
151            to a cursor position. insert_map[N] = index of character/digit
152            where the cursor should be after N chars/digits. it is 
153            mode specific and so it is filled during set_mode().
154         */
155
156         std::vector<int> insert_map;
157
158         bool editing;
159         std::string edit_string;
160         std::string pre_edit_string;
161         std::string input_string;
162
163         framepos_t bbt_reference_time;
164         framepos_t last_when;
165         bool last_pdelta;
166         bool last_sdelta;
167
168         bool dragging;
169         double drag_start_y;
170         double drag_y;
171         double drag_accum;
172         Field  drag_field;
173
174         void on_realize ();
175         bool on_key_press_event (GdkEventKey *);
176         bool on_key_release_event (GdkEventKey *);
177         bool on_scroll_event (GdkEventScroll *ev);
178         bool on_button_press_event (GdkEventButton *ev);
179         bool on_button_release_event(GdkEventButton *ev);
180         void on_style_changed (const Glib::RefPtr<Gtk::Style>&);
181         void on_size_request (Gtk::Requisition* req);
182         bool on_motion_notify_event (GdkEventMotion *ev);
183         void on_size_allocate (Gtk::Allocation&);
184         bool on_focus_out_event (GdkEventFocus*);
185
186         void set_slave_info ();
187         void set_timecode (framepos_t, bool);
188         void set_bbt (framepos_t, bool);
189         void set_minsec (framepos_t, bool);
190         void set_frames (framepos_t, bool);
191
192         framepos_t get_frame_step (Field, framepos_t pos = 0, int dir = 1);
193
194         bool timecode_validate_edit (const std::string&);
195         bool bbt_validate_edit (const std::string&);
196         bool minsec_validate_edit (const std::string&);
197
198         framepos_t frames_from_timecode_string (const std::string&) const;
199         framepos_t frames_from_bbt_string (framepos_t, const std::string&) const;
200         framepos_t frame_duration_from_bbt_string (framepos_t, const std::string&) const;
201         framepos_t frames_from_minsec_string (const std::string&) const;
202         framepos_t frames_from_audioframes_string (const std::string&) const;
203
204         void session_configuration_changed (std::string);
205
206         Field index_to_field () const;
207
208         void start_edit (Field f = Field (0));
209         void end_edit (bool);
210         void end_edit_relative (bool);
211         void edit_next_field ();
212         ARDOUR::framecnt_t parse_as_distance (const std::string&);
213
214         ARDOUR::framecnt_t parse_as_timecode_distance (const std::string&);
215         ARDOUR::framecnt_t parse_as_minsec_distance (const std::string&);
216         ARDOUR::framecnt_t parse_as_bbt_distance (const std::string&);
217         ARDOUR::framecnt_t parse_as_frames_distance (const std::string&);
218         
219         void set_font ();
220         void set_colors ();
221         void show_edit_status (int length);
222         int  merge_input_and_edit_string ();
223         std::string get_field (Field);
224
225         void drop_focus ();
226         void dpi_reset ();
227
228         double bg_r, bg_g, bg_b, bg_a;
229         double cursor_r, cursor_g, cursor_b, cursor_a;
230 };
231
232 #endif /* __audio_clock_h__ */