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