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