Right align some more numeric entries in the meter/tempo dialogs.
[ardour.git] / gtk2_ardour / tempo_dialog.cc
1 /*
2     Copyright (C) 2000-2007 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 #include <cstdio> // for snprintf, grrr
21
22 #include <gtkmm/stock.h>
23
24 #include "gtkmm2ext/utils.h"
25
26 #include "ardour_ui.h"
27 #include "tempo_dialog.h"
28
29 #include "i18n.h"
30
31 using namespace std;
32 using namespace Gtk;
33 using namespace Gtkmm2ext;
34 using namespace ARDOUR;
35 using namespace PBD;
36
37 TempoDialog::TempoDialog (TempoMap& map, framepos_t frame, const string&)
38         : ArdourDialog (_("New Tempo"))
39         , bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
40         , bpm_spinner (bpm_adjustment)
41         , when_bar_label (_("bar:"), ALIGN_LEFT, ALIGN_CENTER)
42         , when_beat_label (_("beat:"), ALIGN_LEFT, ALIGN_CENTER)
43         , pulse_selector_label (_("Pulse note"), ALIGN_LEFT, ALIGN_CENTER)
44         , tap_tempo_button (_("Tap tempo"))
45 {
46         Timecode::BBT_Time when;
47         Tempo tempo (map.tempo_at (frame));
48         map.bbt_time (frame, when);
49
50         init (when, tempo.beats_per_minute(), tempo.note_type(), true);
51 }
52
53 TempoDialog::TempoDialog (TempoSection& section, const string&)
54         : ArdourDialog (_("Edit Tempo"))
55         , bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
56         , bpm_spinner (bpm_adjustment)
57         , when_bar_label (_("bar:"), ALIGN_LEFT, ALIGN_CENTER)
58         , when_beat_label (_("beat:"), ALIGN_LEFT, ALIGN_CENTER)
59         , pulse_selector_label (_("Pulse note"), ALIGN_LEFT, ALIGN_CENTER)
60         , tap_tempo_button (_("Tap tempo"))
61 {
62         init (section.start(), section.beats_per_minute(), section.note_type(), section.movable());
63 }
64
65 void
66 TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type, bool movable)
67 {
68         vector<string> strings;
69         NoteTypes::iterator x;
70
71         bpm_spinner.set_numeric (true);
72         bpm_spinner.set_digits (2);
73         bpm_spinner.set_wrap (true);
74         bpm_spinner.set_value (bpm);
75         bpm_spinner.set_alignment (1.0);
76
77         note_types.insert (make_pair (_("whole"), 1.0));
78         strings.push_back (_("whole"));
79         note_types.insert (make_pair (_("second"), 2.0));
80         strings.push_back (_("second"));
81         note_types.insert (make_pair (_("third"), 3.0));
82         strings.push_back (_("third"));
83         note_types.insert (make_pair (_("quarter"), 4.0));
84         strings.push_back (_("quarter"));
85         note_types.insert (make_pair (_("eighth"), 8.0));
86         strings.push_back (_("eighth"));
87         note_types.insert (make_pair (_("sixteenth"), 16.0));
88         strings.push_back (_("sixteenth"));
89         note_types.insert (make_pair (_("thirty-second"), 32.0));
90         strings.push_back (_("thirty-second"));
91         note_types.insert (make_pair (_("sixty-fourth"), 64.0));
92         strings.push_back (_("sixty-fourth"));
93         note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
94         strings.push_back (_("one-hundred-twenty-eighth"));
95
96         set_popdown_strings (pulse_selector, strings);
97
98         for (x = note_types.begin(); x != note_types.end(); ++x) {
99                 if (x->second == note_type) {
100                         pulse_selector.set_active_text (x->first);
101                         break;
102                 }
103         }
104
105         if (x == note_types.end()) {
106                 pulse_selector.set_active_text (strings[3]); // "quarter"
107         }
108         
109         Table* table;
110
111         if (ARDOUR_UI::config()->get_allow_non_quarter_pulse()) {
112                 table = manage (new Table (5, 5));
113         } else {
114                 table = manage (new Table (5, 4));
115         }
116
117         table->set_spacings (6);
118         table->set_homogeneous (false);
119
120         int row;
121         Label* bpm_label = manage (new Label(_("Beats per minute:"), ALIGN_LEFT, ALIGN_CENTER));
122         table->attach (*bpm_label, 0, 1, 0, 1);
123         table->attach (bpm_spinner, 1, 5, 0, 1);
124
125         if (ARDOUR_UI::config()->get_allow_non_quarter_pulse()) {
126                 table->attach (pulse_selector_label, 0, 1, 1, 2);
127                 table->attach (pulse_selector, 1, 5, 1, 2);
128                 row = 2;
129         } else {
130                 row = 1;
131         }
132
133         char buf[64];
134
135         snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
136         when_bar_entry.set_text (buf);
137         snprintf (buf, sizeof (buf), "%" PRIu32, when.beats);
138         when_beat_entry.set_text (buf);
139
140         if (movable) {
141                 when_bar_entry.set_width_chars(4);
142                 when_beat_entry.set_width_chars (4);
143                 when_bar_entry.set_alignment (1.0);
144                 when_beat_entry.set_alignment (1.0);
145
146                 when_bar_label.set_name ("MetricLabel");
147                 when_beat_label.set_name ("MetricLabel");
148
149                 table->attach (when_bar_label, 1, 2, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
150                 table->attach (when_bar_entry, 2, 3, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
151
152                 table->attach (when_beat_label, 3, 4, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
153                 table->attach (when_beat_entry, 4, 5, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
154
155                 Label* when_label = manage (new Label(_("Tempo begins at"), ALIGN_LEFT, ALIGN_CENTER));
156                 table->attach (*when_label, 0, 1, row, row+1);
157         }
158
159         get_vbox()->set_border_width (12);
160         get_vbox()->pack_end (*table);
161         table->show_all ();
162
163         add_button (Stock::CANCEL, RESPONSE_CANCEL);
164         add_button (Stock::APPLY, RESPONSE_ACCEPT);
165         set_response_sensitive (RESPONSE_ACCEPT, true);
166         set_default_response (RESPONSE_ACCEPT);
167
168         bpm_spinner.show ();
169         tap_tempo_button.show ();
170         get_vbox()->pack_end (tap_tempo_button);
171         bpm_spinner.grab_focus ();
172
173         set_name ("MetricDialog");
174
175         bpm_spinner.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
176         bpm_spinner.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_press), false);
177         bpm_spinner.signal_button_release_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_release), false);
178         bpm_spinner.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::bpm_changed));
179         when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
180         when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
181         when_beat_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
182         when_beat_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
183         pulse_selector.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::pulse_change));
184         tap_tempo_button.signal_clicked().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo));
185 }
186
187 bool
188 TempoDialog::is_user_input_valid() const
189 {
190         return (when_beat_entry.get_text() != "")
191                 && (when_bar_entry.get_text() != "")
192                 && (when_bar_entry.get_text() != "0");
193 }
194
195 void
196 TempoDialog::bpm_changed ()
197 {
198         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
199 }
200
201 bool
202 TempoDialog::bpm_button_press (GdkEventButton*)
203 {
204         return false;
205 }
206
207 bool
208 TempoDialog::bpm_button_release (GdkEventButton*)
209 {
210         /* the value has been modified, accept should work now */
211
212         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
213         return false;
214 }
215
216 bool
217 TempoDialog::entry_key_release (GdkEventKey*)
218 {
219         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
220         return false;
221 }
222
223 double
224 TempoDialog::get_bpm ()
225 {
226         return bpm_spinner.get_value ();
227 }
228
229 bool
230 TempoDialog::get_bbt_time (Timecode::BBT_Time& requested)
231 {
232         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
233                 return false;
234         }
235
236         if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
237                 return false;
238         }
239
240         requested.ticks = 0;
241
242         return true;
243 }
244
245 double
246 TempoDialog::get_note_type ()
247 {
248         NoteTypes::iterator x = note_types.find (pulse_selector.get_active_text());
249         
250         if (x == note_types.end()) {
251                 error << string_compose(_("incomprehensible pulse note type (%1)"), pulse_selector.get_active_text()) << endmsg;
252                 return 0;
253         }
254
255         return x->second;
256 }
257
258 void
259 TempoDialog::pulse_change ()
260 {
261         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
262 }
263
264 void
265 TempoDialog::tap_tempo ()
266 {
267         gint64 now;
268         now = g_get_monotonic_time (); // microseconds
269
270         if (last_tap > 0) {
271                 double interval, bpm;
272                 static const double decay = 0.5;
273
274                 interval = (now - last_tap) * 1.0e-6;
275                 if (interval <= 6.0) {
276                         // >= 10 bpm, say
277                         if (average_interval > 0) {
278                                 average_interval = interval * decay
279                                         + average_interval * (1.0-decay);
280                         } else {
281                                 average_interval = interval;
282                         }
283
284                         bpm = 60.0 / average_interval;
285                         bpm_spinner.set_value (bpm);
286                 } else {
287                         average_interval = 0;
288                 }
289         } else {
290                 average_interval = 0;
291         }
292         last_tap = now;
293 }
294
295 MeterDialog::MeterDialog (TempoMap& map, framepos_t frame, const string&)
296         : ArdourDialog (_("New Meter"))
297 {
298         Timecode::BBT_Time when;
299         frame = map.round_to_bar(frame, RoundNearest);
300         Meter meter (map.meter_at(frame));
301
302         map.bbt_time (frame, when);
303         init (when, meter.divisions_per_bar(), meter.note_divisor(), true);
304 }
305
306 MeterDialog::MeterDialog (MeterSection& section, const string&)
307         : ArdourDialog (_("Edit Meter"))
308 {
309         init (section.start(), section.divisions_per_bar(), section.note_divisor(), section.movable());
310 }
311
312 void
313 MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, bool movable)
314 {
315         char buf[64];
316         vector<string> strings;
317         NoteTypes::iterator x;
318
319         snprintf (buf, sizeof (buf), "%.2f", bpb);
320         bpb_entry.set_text (buf);
321         bpb_entry.select_region (0, -1);
322         bpb_entry.set_alignment (1.0);
323
324         note_types.insert (make_pair (_("whole"), 1.0));
325         strings.push_back (_("whole"));
326         note_types.insert (make_pair (_("second"), 2.0));
327         strings.push_back (_("second"));
328         note_types.insert (make_pair (_("third"), 3.0));
329         strings.push_back (_("third"));
330         note_types.insert (make_pair (_("quarter"), 4.0));
331         strings.push_back (_("quarter"));
332         note_types.insert (make_pair (_("eighth"), 8.0));
333         strings.push_back (_("eighth"));
334         note_types.insert (make_pair (_("sixteenth"), 16.0));
335         strings.push_back (_("sixteenth"));
336         note_types.insert (make_pair (_("thirty-second"), 32.0));
337         strings.push_back (_("thirty-second"));
338         note_types.insert (make_pair (_("sixty-fourth"), 64.0));
339         strings.push_back (_("sixty-fourth"));
340         note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
341         strings.push_back (_("one-hundred-twenty-eighth"));
342
343         set_popdown_strings (note_type, strings);
344
345         for (x = note_types.begin(); x != note_types.end(); ++x) {
346                 if (x->second == divisor) {
347                         note_type.set_active_text (x->first);
348                         break;
349                 }
350         }
351         
352         if (x == note_types.end()) {
353                 note_type.set_active_text (strings[3]); // "quarter"
354         }
355
356         Label* note_label = manage (new Label (_("Note value:"), ALIGN_LEFT, ALIGN_CENTER));
357         Label* bpb_label = manage (new Label (_("Beats per bar:"), ALIGN_LEFT, ALIGN_CENTER));
358         Table* table = manage (new Table (3, 2));
359         table->set_spacings (6);
360
361         table->attach (*bpb_label, 0, 1, 0, 1, FILL|EXPAND, FILL|EXPAND);
362         table->attach (bpb_entry, 1, 2, 0, 1, FILL|EXPAND, FILL|EXPAND);
363         table->attach (*note_label, 0, 1, 1, 2, FILL|EXPAND, FILL|EXPAND);
364         table->attach (note_type, 1, 2, 1, 2, FILL|EXPAND, SHRINK);
365
366         snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
367         when_bar_entry.set_text (buf);
368         when_bar_entry.set_alignment (1.0);
369
370         if (movable) {
371                 Label* when_label = manage (new Label(_("Meter begins at bar:"), ALIGN_LEFT, ALIGN_CENTER));
372
373                 table->attach (*when_label, 0, 1, 2, 3, FILL | EXPAND, FILL | EXPAND);
374                 table->attach (when_bar_entry, 1, 2, 2, 3, FILL | EXPAND, FILL | EXPAND);
375         }
376
377         get_vbox()->set_border_width (12);
378         get_vbox()->pack_start (*table, false, false);
379
380         add_button (Stock::CANCEL, RESPONSE_CANCEL);
381         add_button (Stock::APPLY, RESPONSE_ACCEPT);
382         set_response_sensitive (RESPONSE_ACCEPT, true);
383         set_default_response (RESPONSE_ACCEPT);
384
385         get_vbox()->show_all ();
386
387         set_name ("MetricDialog");
388         bpb_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
389         bpb_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
390         bpb_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
391         when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
392         when_bar_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
393         when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
394         note_type.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::note_type_change));
395 }
396
397 bool
398 MeterDialog::is_user_input_valid() const
399 {
400         return (when_bar_entry.get_text() != "")
401                 && (when_bar_entry.get_text() != "0")
402                 && (bpb_entry.get_text() != "");
403 }
404
405 bool
406 MeterDialog::entry_key_press (GdkEventKey* ev)
407 {
408
409         switch (ev->keyval) {
410
411         case GDK_0:
412         case GDK_1:
413         case GDK_2:
414         case GDK_3:
415         case GDK_4:
416         case GDK_5:
417         case GDK_6:
418         case GDK_7:
419         case GDK_8:
420         case GDK_9:
421         case GDK_KP_0:
422         case GDK_KP_1:
423         case GDK_KP_2:
424         case GDK_KP_3:
425         case GDK_KP_4:
426         case GDK_KP_5:
427         case GDK_KP_6:
428         case GDK_KP_7:
429         case GDK_KP_8:
430         case GDK_KP_9:
431         case GDK_period:
432         case GDK_comma:
433         case  GDK_KP_Delete:
434         case  GDK_KP_Enter:
435         case  GDK_Delete:
436         case  GDK_BackSpace:
437         case  GDK_Escape:
438         case  GDK_Return:
439         case  GDK_Home:
440         case  GDK_End:
441         case  GDK_Left:
442         case  GDK_Right:
443         case  GDK_Num_Lock:
444         case  GDK_Tab:
445                 return FALSE;
446         default:
447                 break;
448         }
449
450         return TRUE;
451 }
452
453 bool
454 MeterDialog::entry_key_release (GdkEventKey*)
455 {
456         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
457         return false;
458 }
459
460 void
461 MeterDialog::note_type_change ()
462 {
463         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
464 }
465
466 double
467 MeterDialog::get_bpb ()
468 {
469         double bpb = 0;
470
471         if (sscanf (bpb_entry.get_text().c_str(), "%lf", &bpb) != 1) {
472                 return 0;
473         }
474
475         return bpb;
476 }
477
478 double
479 MeterDialog::get_note_type ()
480 {
481         NoteTypes::iterator x = note_types.find (note_type.get_active_text());
482         
483         if (x == note_types.end()) {
484                 error << string_compose(_("incomprehensible meter note type (%1)"), note_type.get_active_text()) << endmsg;
485                 return 0;
486         }
487
488         return x->second;
489 }
490
491 bool
492 MeterDialog::get_bbt_time (Timecode::BBT_Time& requested)
493 {
494         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
495                 return false;
496         }
497
498         requested.beats = 1;
499         requested.ticks = 0;
500
501         return true;
502 }