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