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