Fix crash when X11 is not available for VST UIs
[ardour.git] / gtk2_ardour / tempo_dialog.cc
1 /*
2  * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2005 Karsten Wiese <fzuuzf@googlemail.com>
4  * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
5  * Copyright (C) 2008-2015 David Robillard <d@drobilla.net>
6  * Copyright (C) 2009-2010 Carl Hetherington <carl@carlh.net>
7  * Copyright (C) 2014-2015 Colin Fletcher <colin.m.fletcher@googlemail.com>
8  * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
9  * Copyright (C) 2015-2017 Ben Loftis <ben@harrisonconsoles.com>
10  * Copyright (C) 2015-2017 Nick Mainsbridge <mainsbridge@gmail.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include <cstdio> // for snprintf, grrr
28
29 #include <gtkmm/stock.h>
30
31 #include "gtkmm2ext/utils.h"
32
33 #include "tempo_dialog.h"
34 #include "ui_config.h"
35
36 #include "pbd/i18n.h"
37
38 using namespace std;
39 using namespace Gtk;
40 using namespace Gtkmm2ext;
41 using namespace ARDOUR;
42 using namespace PBD;
43
44 TempoDialog::TempoDialog (TempoMap& map, samplepos_t sample, const string&)
45         : ArdourDialog (_("New Tempo"))
46         , _map (&map)
47         , _section (0)
48         , bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
49         , bpm_spinner (bpm_adjustment)
50         , end_bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
51         , end_bpm_spinner (end_bpm_adjustment)
52         , _end_bpm_label (_("End Beats per Minute:"), ALIGN_LEFT, ALIGN_CENTER)
53         , when_bar_label (_("bar:"), ALIGN_RIGHT, ALIGN_CENTER)
54         , when_beat_label (_("beat:"), ALIGN_RIGHT, ALIGN_CENTER)
55         , pulse_selector_label (_("Pulse:"), ALIGN_LEFT, ALIGN_CENTER)
56         , tap_tempo_button (_("Tap tempo"))
57 {
58         Tempo tempo (map.tempo_at_sample (sample));
59         Timecode::BBT_Time when (map.bbt_at_sample (sample));
60
61         init (when, tempo.note_types_per_minute(), tempo.end_note_types_per_minute(), tempo.note_type(), TempoSection::Constant, true, MusicTime);
62 }
63
64 TempoDialog::TempoDialog (TempoMap& map, TempoSection& section, const string&)
65         : ArdourDialog (_("Edit Tempo"))
66         , _map (&map)
67         , _section (&section)
68         , bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
69         , bpm_spinner (bpm_adjustment)
70         , end_bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
71         , end_bpm_spinner (end_bpm_adjustment)
72         , _end_bpm_label (_("End Beats per Minute:"), ALIGN_LEFT, ALIGN_CENTER)
73         , when_bar_label (_("bar:"), ALIGN_RIGHT, ALIGN_CENTER)
74         , when_beat_label (_("beat:"), ALIGN_RIGHT, ALIGN_CENTER)
75         , pulse_selector_label (_("Pulse:"), ALIGN_LEFT, ALIGN_CENTER)
76         , tap_tempo_button (_("Tap tempo"))
77 {
78         Timecode::BBT_Time when (map.bbt_at_sample (section.sample()));
79         init (when, section.note_types_per_minute(), section.end_note_types_per_minute(), section.note_type(), section.type()
80               , section.initial() || section.locked_to_meter(), section.position_lock_style());
81 }
82
83 void
84 TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double end_bpm, double note_type, TempoSection::Type type, bool initial, PositionLockStyle style)
85 {
86         vector<string> strings;
87         NoteTypes::iterator x;
88
89         bpm_spinner.set_numeric (true);
90         bpm_spinner.set_digits (3);
91         bpm_spinner.set_wrap (true);
92         bpm_spinner.set_value (bpm);
93         bpm_spinner.set_alignment (1.0);
94
95         end_bpm_spinner.set_numeric (true);
96         end_bpm_spinner.set_digits (3);
97         end_bpm_spinner.set_wrap (true);
98         end_bpm_spinner.set_value (end_bpm);
99         end_bpm_spinner.set_alignment (1.0);
100
101         Gtkmm2ext::set_size_request_to_display_given_text (pulse_selector, _("one-hundred-twenty-eighth"), 3, 6);
102
103         note_types.insert (make_pair (_("whole"), 1.0));
104         strings.push_back (_("whole"));
105         note_types.insert (make_pair (_("second"), 2.0));
106         strings.push_back (_("second"));
107         note_types.insert (make_pair (_("third"), 3.0));
108         strings.push_back (_("third"));
109         note_types.insert (make_pair (_("quarter"), 4.0));
110         strings.push_back (_("quarter"));
111         note_types.insert (make_pair (_("eighth"), 8.0));
112         strings.push_back (_("eighth"));
113         note_types.insert (make_pair (_("sixteenth"), 16.0));
114         strings.push_back (_("sixteenth"));
115         note_types.insert (make_pair (_("thirty-second"), 32.0));
116         strings.push_back (_("thirty-second"));
117         note_types.insert (make_pair (_("sixty-fourth"), 64.0));
118         strings.push_back (_("sixty-fourth"));
119         note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
120         strings.push_back (_("one-hundred-twenty-eighth"));
121
122         set_popdown_strings (pulse_selector, strings);
123
124         for (x = note_types.begin(); x != note_types.end(); ++x) {
125                 if (x->second == note_type) {
126                         pulse_selector.set_active_text (x->first);
127                         break;
128                 }
129         }
130
131         if (x == note_types.end()) {
132                 pulse_selector.set_active_text (strings[3]); // "quarter"
133         }
134
135         strings.clear();
136
137         tempo_types.insert (make_pair (_("ramped"), TempoSection::Ramp));
138         strings.push_back (_("ramped"));
139         tempo_types.insert (make_pair (_("constant"), TempoSection::Constant));
140         strings.push_back (_("constant"));
141         set_popdown_strings (tempo_type, strings);
142         TempoTypes::iterator tt;
143         for (tt = tempo_types.begin(); tt != tempo_types.end(); ++tt) {
144                 if (tt->second == type) {
145                         tempo_type.set_active_text (tt->first);
146                         break;
147                 }
148         }
149         if (tt == tempo_types.end()) {
150                 tempo_type.set_active_text (strings[1]); // "constant"
151         }
152
153         strings.clear();
154
155         lock_styles.insert (make_pair (_("music"), MusicTime));
156         strings.push_back (_("music"));
157         lock_styles.insert (make_pair (_("audio"), AudioTime));
158         strings.push_back (_("audio"));
159         set_popdown_strings (lock_style, strings);
160         LockStyles::iterator ls;
161         for (ls = lock_styles.begin(); ls != lock_styles.end(); ++ls) {
162                 if (ls->second == style) {
163                         lock_style.set_active_text (ls->first);
164                         break;
165                 }
166         }
167         if (ls == lock_styles.end()) {
168                 lock_style.set_active_text (strings[0]); // "music"
169         }
170
171         Table* table;
172
173         if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
174                 table = manage (new Table (5, 7));
175         } else {
176                 table = manage (new Table (5, 6));
177         }
178
179         table->set_spacings (6);
180         table->set_homogeneous (false);
181
182         int row = 0;
183
184         if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
185                 table->attach (pulse_selector_label, 0, 1, row, row + 1);
186                 table->attach (pulse_selector, 1, 5, row, row + 1);
187
188                 ++row;
189         }
190
191         Label* bpm_label = manage (new Label(_("Start Beats per Minute:"), ALIGN_LEFT, ALIGN_CENTER));
192         table->attach (*bpm_label, 0, 1, row, row + 1);
193         table->attach (bpm_spinner, 1, 5, row, row + 1);
194         ++row;
195
196         table->attach (_end_bpm_label, 0, 1, row, row + 1);
197         table->attach (end_bpm_spinner, 1, 5, row, row + 1);
198         ++row;
199
200         Label* tempo_type_label = manage (new Label(_("Tempo Type:"), ALIGN_LEFT, ALIGN_CENTER));
201         table->attach (*tempo_type_label, 0, 1, row, row + 1);
202         table->attach (tempo_type, 1, 5, row, row + 1);
203
204         ++row;
205
206         char buf[64];
207
208         snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
209         when_bar_entry.set_text (buf);
210         snprintf (buf, sizeof (buf), "%" PRIu32, when.beats);
211         when_beat_entry.set_text (buf);
212
213         if (!initial) {
214                 when_bar_entry.set_width_chars(4);
215                 when_beat_entry.set_width_chars (4);
216                 when_bar_entry.set_alignment (1.0);
217                 when_beat_entry.set_alignment (1.0);
218
219                 when_bar_label.set_name ("MetricLabel");
220                 when_beat_label.set_name ("MetricLabel");
221
222                 table->attach (when_bar_label, 1, 2, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
223                 table->attach (when_bar_entry, 2, 3, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
224
225                 table->attach (when_beat_label, 3, 4, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
226                 table->attach (when_beat_entry, 4, 5, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
227
228                 Label* when_label = manage (new Label(_("Tempo begins at"), ALIGN_LEFT, ALIGN_CENTER));
229                 table->attach (*when_label, 0, 1, row, row+1);
230
231                 ++row;
232                 ++row;
233
234                 Label* lock_style_label = manage (new Label(_("Lock Style:"), ALIGN_LEFT, ALIGN_CENTER));
235                 table->attach (*lock_style_label, 0, 1, row, row + 1);
236                 table->attach (lock_style, 1, 5, row, row + 1);
237
238                 --row;
239         }
240
241         get_vbox()->set_border_width (12);
242         get_vbox()->pack_end (*table);
243
244         table->show_all ();
245
246         add_button (Stock::CANCEL, RESPONSE_CANCEL);
247         add_button (Stock::APPLY, RESPONSE_ACCEPT);
248         set_response_sensitive (RESPONSE_ACCEPT, true);
249         set_default_response (RESPONSE_ACCEPT);
250
251         bpm_spinner.show ();
252         end_bpm_spinner.show ();
253         tap_tempo_button.show ();
254         get_vbox()->set_spacing (6);
255         get_vbox()->pack_end (tap_tempo_button);
256         tap_tempo_button.can_focus ();
257         tap_tempo_button.grab_focus ();
258
259         set_name ("MetricDialog");
260
261         bpm_spinner.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
262         bpm_spinner.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_press), false);
263         bpm_spinner.signal_button_release_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_release), false);
264         bpm_spinner.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::bpm_changed));
265         end_bpm_spinner.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::bpm_changed));
266         when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
267         when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
268         when_beat_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
269         when_beat_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
270         pulse_selector.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::pulse_change));
271         tempo_type.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::tempo_type_change));
272         lock_style.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::lock_style_change));
273         tap_tempo_button.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_button_press), false);
274         tap_tempo_button.signal_key_press_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_key_press), false);
275         tap_tempo_button.signal_focus_out_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_focus_out));
276
277         tempo_type_change();
278
279         tapped = false;
280 }
281
282 bool
283 TempoDialog::is_user_input_valid() const
284 {
285         return (when_beat_entry.get_text() != "")
286                 && (when_bar_entry.get_text() != "")
287                 && (when_bar_entry.get_text() != "0");
288 }
289
290 void
291 TempoDialog::bpm_changed ()
292 {
293         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
294 }
295
296 bool
297 TempoDialog::bpm_button_press (GdkEventButton*)
298 {
299         return false;
300 }
301
302 bool
303 TempoDialog::bpm_button_release (GdkEventButton*)
304 {
305         /* the value has been modified, accept should work now */
306
307         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
308         return false;
309 }
310
311 bool
312 TempoDialog::entry_key_release (GdkEventKey*)
313 {
314         Timecode::BBT_Time bbt;
315         get_bbt_time (bbt);
316
317         if (_section && is_user_input_valid()) {
318                 set_response_sensitive (RESPONSE_ACCEPT, _map->can_solve_bbt (_section, bbt));
319         } else {
320                 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
321         }
322
323         return false;
324 }
325
326 double
327 TempoDialog::get_bpm ()
328 {
329         return bpm_spinner.get_value ();
330 }
331
332 double
333 TempoDialog::get_end_bpm ()
334 {
335         if (get_tempo_type() == TempoSection::Constant) {
336                 return bpm_spinner.get_value ();
337         }
338
339         return end_bpm_spinner.get_value ();
340 }
341
342 bool
343 TempoDialog::get_bbt_time (Timecode::BBT_Time& requested)
344 {
345         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
346                 return false;
347         }
348
349         if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
350                 return false;
351         }
352
353         requested.ticks = 0;
354
355         return true;
356 }
357
358 double
359 TempoDialog::get_note_type ()
360 {
361         NoteTypes::iterator x = note_types.find (pulse_selector.get_active_text());
362
363         if (x == note_types.end()) {
364                 error << string_compose(_("incomprehensible pulse note type (%1)"), pulse_selector.get_active_text()) << endmsg;
365                 return 0;
366         }
367
368         return x->second;
369 }
370
371 TempoSection::Type
372 TempoDialog::get_tempo_type ()
373 {
374         TempoTypes::iterator x = tempo_types.find (tempo_type.get_active_text());
375
376         if (x == tempo_types.end()) {
377                 error << string_compose(_("incomprehensible tempo type (%1)"), tempo_type.get_active_text()) << endmsg;
378                 return TempoSection::Constant;
379         }
380
381         return x->second;
382 }
383
384 PositionLockStyle
385 TempoDialog::get_lock_style ()
386 {
387         LockStyles::iterator x = lock_styles.find (lock_style.get_active_text());
388
389         if (x == lock_styles.end()) {
390                 error << string_compose(_("incomprehensible lock style (%1)"), lock_style.get_active_text()) << endmsg;
391                 return MusicTime;
392         }
393
394         return x->second;
395 }
396
397 void
398 TempoDialog::pulse_change ()
399 {
400         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
401 }
402
403 void
404 TempoDialog::tempo_type_change ()
405 {
406         if (get_tempo_type() == TempoSection::Constant) {
407                 end_bpm_spinner.hide ();
408                 _end_bpm_label.hide();
409         } else {
410                 end_bpm_spinner.show ();
411                 _end_bpm_label.show();
412         }
413
414         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
415 }
416
417 void
418 TempoDialog::lock_style_change ()
419 {
420         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
421 }
422
423 bool
424 TempoDialog::tap_tempo_key_press (GdkEventKey*)
425 {
426         tap_tempo ();
427         return false;
428 }
429
430 bool
431 TempoDialog::tap_tempo_button_press (GdkEventButton* ev)
432 {
433         if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS) {
434                 return true;
435         }
436         if (ev->button != 1) {
437                 return true;
438         }
439         tap_tempo ();
440         return false; // grab focus
441 }
442
443 void
444 TempoDialog::tap_tempo ()
445 {
446         double t;
447
448         // Linear least-squares regression
449         if (tapped) {
450                 t = 1e-6 * (g_get_monotonic_time () - first_t); // Subtract first_t to avoid precision problems
451
452                 double n = tap_count;
453                 sum_y += t;
454                 sum_x += n;
455                 sum_xy += n * t;
456                 sum_xx += n * n;
457                 double T = (sum_xy/n - sum_x/n * sum_y/n) / (sum_xx/n - sum_x/n * sum_x/n);
458
459                 if (t - last_t < T / 1.2 || t - last_t > T * 1.2) {
460                         tapped = false;
461                 } else {
462                         bpm_spinner.set_value (60.0 / T);
463                 }
464         }
465         if (!tapped) {
466                 first_t = g_get_monotonic_time ();
467                 t = 0.0;
468                 sum_y = 0.0;
469                 sum_x = 1.0;
470                 sum_xy = 0.0;
471                 sum_xx = 1.0;
472                 tap_count = 1.0;
473
474                 tapped = true;
475         }
476         tap_count++;
477         last_t = t;
478 }
479
480 bool
481 TempoDialog::tap_tempo_focus_out (GdkEventFocus* )
482 {
483         tapped = false;
484         return false;
485 }
486
487 MeterDialog::MeterDialog (TempoMap& map, samplepos_t sample, const string&)
488         : ArdourDialog (_("New Meter"))
489 {
490         sample = map.round_to_bar(sample, RoundNearest).sample;
491         Timecode::BBT_Time when (map.bbt_at_sample (sample));
492         Meter meter (map.meter_at_sample (sample));
493
494         init (when, meter.divisions_per_bar(), meter.note_divisor(), false, MusicTime);
495 }
496
497 MeterDialog::MeterDialog (TempoMap& map, MeterSection& section, const string&)
498         : ArdourDialog (_("Edit Meter"))
499 {
500         Timecode::BBT_Time when (map.bbt_at_sample (section.sample()));
501
502         init (when, section.divisions_per_bar(), section.note_divisor(), section.initial(), section.position_lock_style());
503 }
504
505 void
506 MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, bool initial, PositionLockStyle style)
507 {
508         char buf[64];
509         vector<string> strings;
510         NoteTypes::iterator x;
511
512         snprintf (buf, sizeof (buf), "%.2f", bpb);
513         bpb_entry.set_text (buf);
514         bpb_entry.select_region (0, -1);
515         bpb_entry.set_alignment (1.0);
516
517         note_types.insert (make_pair (_("whole"), 1.0));
518         strings.push_back (_("whole"));
519         note_types.insert (make_pair (_("second"), 2.0));
520         strings.push_back (_("second"));
521         note_types.insert (make_pair (_("third"), 3.0));
522         strings.push_back (_("third"));
523         note_types.insert (make_pair (_("quarter"), 4.0));
524         strings.push_back (_("quarter"));
525         note_types.insert (make_pair (_("eighth"), 8.0));
526         strings.push_back (_("eighth"));
527         note_types.insert (make_pair (_("sixteenth"), 16.0));
528         strings.push_back (_("sixteenth"));
529         note_types.insert (make_pair (_("thirty-second"), 32.0));
530         strings.push_back (_("thirty-second"));
531         note_types.insert (make_pair (_("sixty-fourth"), 64.0));
532         strings.push_back (_("sixty-fourth"));
533         note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
534         strings.push_back (_("one-hundred-twenty-eighth"));
535
536         set_popdown_strings (note_type, strings);
537
538         for (x = note_types.begin(); x != note_types.end(); ++x) {
539                 if (x->second == divisor) {
540                         note_type.set_active_text (x->first);
541                         break;
542                 }
543         }
544
545         if (x == note_types.end()) {
546                 note_type.set_active_text (strings[3]); // "quarter"
547         }
548
549         strings.clear();
550
551         lock_styles.insert (make_pair (_("music"), MusicTime));
552         strings.push_back (_("music"));
553         lock_styles.insert (make_pair (_("audio"), AudioTime));
554         strings.push_back (_("audio"));
555         set_popdown_strings (lock_style, strings);
556         LockStyles::iterator ls;
557         for (ls = lock_styles.begin(); ls != lock_styles.end(); ++ls) {
558                 if (ls->second == style) {
559                         lock_style.set_active_text (ls->first);
560                         break;
561                 }
562         }
563         if (ls == lock_styles.end()) {
564                 lock_style.set_active_text (strings[0]); // "music"
565         }
566
567         Label* note_label = manage (new Label (_("Note value:"), ALIGN_RIGHT, ALIGN_CENTER));
568         Label* lock_label = manage (new Label (_("Lock style:"), ALIGN_RIGHT, ALIGN_CENTER));
569         Label* bpb_label = manage (new Label (_("Beats per bar:"), ALIGN_RIGHT, ALIGN_CENTER));
570         Table* table = manage (new Table (3, 3));
571         table->set_spacings (6);
572
573         table->attach (*bpb_label, 0, 1, 0, 1, FILL|EXPAND, FILL|EXPAND);
574         table->attach (bpb_entry, 1, 2, 0, 1, FILL|EXPAND, FILL|EXPAND);
575         table->attach (*note_label, 0, 1, 1, 2, FILL|EXPAND, FILL|EXPAND);
576         table->attach (note_type, 1, 2, 1, 2, FILL|EXPAND, FILL|EXPAND);
577
578         snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
579         when_bar_entry.set_text (buf);
580         when_bar_entry.set_alignment (1.0);
581
582         if (!initial) {
583                 Label* when_label = manage (new Label(_("Meter begins at bar:"), ALIGN_LEFT, ALIGN_CENTER));
584
585                 table->attach (*when_label, 0, 1, 2, 3, FILL | EXPAND, FILL | EXPAND);
586                 table->attach (when_bar_entry, 1, 2, 2, 3, FILL | EXPAND, FILL | EXPAND);
587
588                 table->attach (*lock_label, 0, 1, 3, 4, FILL|EXPAND, FILL|EXPAND);
589                 table->attach (lock_style, 1, 2, 3, 4, FILL|EXPAND, SHRINK);
590         }
591
592         get_vbox()->set_border_width (12);
593         get_vbox()->pack_start (*table, false, false);
594
595         add_button (Stock::CANCEL, RESPONSE_CANCEL);
596         add_button (Stock::APPLY, RESPONSE_ACCEPT);
597         set_response_sensitive (RESPONSE_ACCEPT, true);
598         set_default_response (RESPONSE_ACCEPT);
599
600         get_vbox()->show_all ();
601
602         set_name ("MetricDialog");
603         bpb_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
604         bpb_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
605         bpb_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
606         when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
607         when_bar_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
608         when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
609         note_type.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::note_type_change));
610         lock_style.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::lock_style_change));
611
612 }
613
614 bool
615 MeterDialog::is_user_input_valid() const
616 {
617         return (when_bar_entry.get_text() != "")
618                 && (when_bar_entry.get_text() != "0")
619                 && (bpb_entry.get_text() != "");
620 }
621
622 bool
623 MeterDialog::entry_key_press (GdkEventKey* ev)
624 {
625
626         switch (ev->keyval) {
627
628         case GDK_0:
629         case GDK_1:
630         case GDK_2:
631         case GDK_3:
632         case GDK_4:
633         case GDK_5:
634         case GDK_6:
635         case GDK_7:
636         case GDK_8:
637         case GDK_9:
638         case GDK_KP_0:
639         case GDK_KP_1:
640         case GDK_KP_2:
641         case GDK_KP_3:
642         case GDK_KP_4:
643         case GDK_KP_5:
644         case GDK_KP_6:
645         case GDK_KP_7:
646         case GDK_KP_8:
647         case GDK_KP_9:
648         case GDK_period:
649         case GDK_comma:
650         case  GDK_KP_Delete:
651         case  GDK_KP_Enter:
652         case  GDK_Delete:
653         case  GDK_BackSpace:
654         case  GDK_Escape:
655         case  GDK_Return:
656         case  GDK_Home:
657         case  GDK_End:
658         case  GDK_Left:
659         case  GDK_Right:
660         case  GDK_Num_Lock:
661         case  GDK_Tab:
662                 return FALSE;
663         default:
664                 break;
665         }
666
667         return TRUE;
668 }
669
670 bool
671 MeterDialog::entry_key_release (GdkEventKey*)
672 {
673         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
674         return false;
675 }
676
677 void
678 MeterDialog::note_type_change ()
679 {
680         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
681 }
682
683 void
684 MeterDialog::lock_style_change ()
685 {
686         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
687 }
688
689 double
690 MeterDialog::get_bpb ()
691 {
692         double bpb = 0;
693
694         if (sscanf (bpb_entry.get_text().c_str(), "%lf", &bpb) != 1) {
695                 return 0;
696         }
697
698         return bpb;
699 }
700
701 double
702 MeterDialog::get_note_type ()
703 {
704         NoteTypes::iterator x = note_types.find (note_type.get_active_text());
705
706         if (x == note_types.end()) {
707                 error << string_compose(_("incomprehensible meter note type (%1)"), note_type.get_active_text()) << endmsg;
708                 return 0;
709         }
710
711         return x->second;
712 }
713
714 PositionLockStyle
715 MeterDialog::get_lock_style ()
716 {
717         LockStyles::iterator x = lock_styles.find (lock_style.get_active_text());
718
719         if (x == lock_styles.end()) {
720                 error << string_compose(_("incomprehensible meter lock style (%1)"), lock_style.get_active_text()) << endmsg;
721                 return MusicTime;
722         }
723
724         return x->second;
725 }
726
727 bool
728 MeterDialog::get_bbt_time (Timecode::BBT_Time& requested)
729 {
730         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
731                 return false;
732         }
733
734         requested.beats = 1;
735         requested.ticks = 0;
736
737         return true;
738 }