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