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