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