add sixteenths as note type for tempo and meter; fix cursor color, selection color...
[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 (_("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 (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 (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         if (movable) {
133                 char buf[64];
134
135                 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
136                 when_bar_entry.set_text (buf);
137                 snprintf (buf, sizeof (buf), "%" PRIu32, when.beats);
138                 when_beat_entry.set_text (buf);
139
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, false);
163         set_default_response (RESPONSE_ACCEPT);
164
165         bpm_spinner.show ();
166
167         set_name ("MetricDialog");
168
169         bpm_spinner.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
170         bpm_spinner.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_press), false);
171         bpm_spinner.signal_button_release_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_release), false);
172         bpm_spinner.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::bpm_changed));
173         when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
174         when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
175         when_beat_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
176         when_beat_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
177         pulse_selector.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::pulse_change));
178 }
179
180 void
181 TempoDialog::bpm_changed ()
182 {
183         set_response_sensitive (RESPONSE_ACCEPT, true);
184 }
185
186 bool
187 TempoDialog::bpm_button_press (GdkEventButton*)
188 {
189         return false;
190 }
191
192 bool
193 TempoDialog::bpm_button_release (GdkEventButton*)
194 {
195         /* the value has been modified, accept should work now */
196
197         set_response_sensitive (RESPONSE_ACCEPT, true);
198         return false;
199 }
200
201 bool
202 TempoDialog::entry_key_release (GdkEventKey*)
203 {
204         if (when_beat_entry.get_text() != "" && when_bar_entry.get_text() != "") {
205                 set_response_sensitive (RESPONSE_ACCEPT, true);
206         } else {
207                 set_response_sensitive (RESPONSE_ACCEPT, false);
208         }
209         return false;
210 }
211
212 double
213 TempoDialog::get_bpm ()
214 {
215         return bpm_spinner.get_value ();
216 }
217
218 bool
219 TempoDialog::get_bbt_time (Timecode::BBT_Time& requested)
220 {
221         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
222                 return false;
223         }
224
225         if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
226                 return false;
227         }
228
229         requested.ticks = 0;
230
231         return true;
232 }
233
234 double
235 TempoDialog::get_note_type ()
236 {
237         NoteTypes::iterator x = note_types.find (pulse_selector.get_active_text());
238         
239         if (x == note_types.end()) {
240                 error << string_compose(_("incomprehensible pulse note type (%1)"), pulse_selector.get_active_text()) << endmsg;
241                 return 0;
242         }
243
244         return x->second;
245 }
246
247 void
248 TempoDialog::pulse_change ()
249 {
250         set_response_sensitive (RESPONSE_ACCEPT, true);
251 }
252
253
254 MeterDialog::MeterDialog (TempoMap& map, framepos_t frame, const string & action)
255         : ArdourDialog ("New Meter")
256 {
257         Timecode::BBT_Time when;
258         frame = map.round_to_bar(frame,0);
259         Meter meter (map.meter_at(frame));
260
261         map.bbt_time (frame, when);
262         init (when, meter.divisions_per_bar(), meter.note_divisor(), true);
263 }
264
265 MeterDialog::MeterDialog (MeterSection& section, const string & action)
266         : ArdourDialog ("Edit Meter")
267 {
268         init (section.start(), section.divisions_per_bar(), section.note_divisor(), section.movable());
269 }
270
271 void
272 MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, bool movable)
273 {
274         char buf[64];
275         vector<string> strings;
276         NoteTypes::iterator x;
277
278         snprintf (buf, sizeof (buf), "%.2f", bpb);
279         bpb_entry.set_text (buf);
280         bpb_entry.select_region (0, -1);
281
282         note_types.insert (make_pair (_("whole"), 1.0));
283         strings.push_back (_("whole"));
284         note_types.insert (make_pair (_("second"), 2.0));
285         strings.push_back (_("second"));
286         note_types.insert (make_pair (_("third"), 3.0));
287         strings.push_back (_("third"));
288         note_types.insert (make_pair (_("quarter"), 4.0));
289         strings.push_back (_("quarter"));
290         note_types.insert (make_pair (_("eighth"), 8.0));
291         strings.push_back (_("eighth"));
292         note_types.insert (make_pair (_("sixteenth"), 16.0));
293         strings.push_back (_("sixteenth"));
294         note_types.insert (make_pair (_("thirty-second"), 32.0));
295         strings.push_back (_("thirty-second"));
296         note_types.insert (make_pair (_("sixty-fourth"), 64.0));
297         strings.push_back (_("sixty-fourth"));
298         note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
299         strings.push_back (_("one-hundred-twenty-eighth"));
300
301         set_popdown_strings (note_type, strings);
302
303         for (x = note_types.begin(); x != note_types.end(); ++x) {
304                 if (x->second == divisor) {
305                         note_type.set_active_text (x->first);
306                         break;
307                 }
308         }
309         
310         if (x == note_types.end()) {
311                 note_type.set_active_text (strings[3]); // "quarter"
312         }
313
314         Label* note_label = manage (new Label (_("Note value:"), ALIGN_LEFT, ALIGN_CENTER));
315         Label* bpb_label = manage (new Label (_("Beats per bar:"), ALIGN_LEFT, ALIGN_CENTER));
316         Table* table = manage (new Table (3, 2));
317         table->set_spacings (6);
318
319         table->attach (*bpb_label, 0, 1, 0, 1, FILL|EXPAND, FILL|EXPAND);
320         table->attach (bpb_entry, 1, 2, 0, 1, FILL|EXPAND, FILL|EXPAND);
321         table->attach (*note_label, 0, 1, 1, 2, FILL|EXPAND, FILL|EXPAND);
322         table->attach (note_type, 1, 2, 1, 2, FILL|EXPAND, SHRINK);
323
324         if (movable) {
325                 char buf[64];
326
327                 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
328                 when_bar_entry.set_text (buf);
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         add_button (Stock::CANCEL, RESPONSE_CANCEL);
342         add_button (Stock::APPLY, RESPONSE_ACCEPT);
343         set_response_sensitive (RESPONSE_ACCEPT, false);
344         set_default_response (RESPONSE_ACCEPT);
345
346         get_vbox()->show_all ();
347
348         set_name ("MetricDialog");
349         bpb_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
350         bpb_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
351         bpb_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
352         when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
353         when_bar_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
354         when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
355         note_type.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::note_type_change));
356 }
357
358 bool
359 MeterDialog::entry_key_press (GdkEventKey* ev)
360 {
361
362         switch (ev->keyval) {
363
364         case GDK_0:
365         case GDK_1:
366         case GDK_2:
367         case GDK_3:
368         case GDK_4:
369         case GDK_5:
370         case GDK_6:
371         case GDK_7:
372         case GDK_8:
373         case GDK_9:
374         case GDK_KP_0:
375         case GDK_KP_1:
376         case GDK_KP_2:
377         case GDK_KP_3:
378         case GDK_KP_4:
379         case GDK_KP_5:
380         case GDK_KP_6:
381         case GDK_KP_7:
382         case GDK_KP_8:
383         case GDK_KP_9:
384         case GDK_period:
385         case GDK_comma:
386         case  GDK_KP_Delete:
387         case  GDK_KP_Enter:
388         case  GDK_Delete:
389         case  GDK_BackSpace:
390         case  GDK_Escape:
391         case  GDK_Return:
392         case  GDK_Home:
393         case  GDK_End:
394         case  GDK_Left:
395         case  GDK_Right:
396         case  GDK_Num_Lock:
397         case  GDK_Tab:
398                 return FALSE;
399         default:
400                 break;
401         }
402
403         return TRUE;
404 }
405
406 bool
407 MeterDialog::entry_key_release (GdkEventKey*)
408 {
409         if (when_bar_entry.get_text() != "" && bpb_entry.get_text() != "") {
410                 set_response_sensitive (RESPONSE_ACCEPT, true);
411         } else {
412                 set_response_sensitive (RESPONSE_ACCEPT, false);
413         }
414         return false;
415 }
416
417 void
418 MeterDialog::note_type_change ()
419 {
420         set_response_sensitive (RESPONSE_ACCEPT, true);
421 }
422
423 double
424 MeterDialog::get_bpb ()
425 {
426         double bpb = 0;
427
428         if (sscanf (bpb_entry.get_text().c_str(), "%lf", &bpb) != 1) {
429                 return 0;
430         }
431
432         return bpb;
433 }
434
435 double
436 MeterDialog::get_note_type ()
437 {
438         NoteTypes::iterator x = note_types.find (note_type.get_active_text());
439         
440         if (x == note_types.end()) {
441                 error << string_compose(_("incomprehensible meter note type (%1)"), note_type.get_active_text()) << endmsg;
442                 return 0;
443         }
444
445         return x->second;
446 }
447
448 bool
449 MeterDialog::get_bbt_time (Timecode::BBT_Time& requested)
450 {
451         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
452                 return false;
453         }
454
455         requested.beats = 1;
456         requested.ticks = 0;
457
458         return true;
459 }