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