Merge libs/ardour and gtk2_ardour with 2.0-ongoing R2837.
[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 Gtk;
31 using namespace Gtkmm2ext;
32 using namespace ARDOUR;
33 using namespace PBD;
34
35 TempoDialog::TempoDialog (TempoMap& map, nframes_t frame, const string & action)
36         : ArdourDialog (_("edit tempo")),
37           note_frame (_("BPM denominator")),
38           bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0, 1.0),
39           bpm_spinner (bpm_adjustment),
40           bpm_frame (_("Beats per minute")),
41           ok_button (action),
42           cancel_button (_("Cancel")),
43           when_bar_label (_("Bar")),
44           when_beat_label (_("Beat")),
45           when_table (2, 2),
46           when_frame (_("Location"))
47 {
48         BBT_Time when;
49         Tempo tempo (map.tempo_at (frame));
50         map.bbt_time (frame, when);
51
52         init (when, tempo.beats_per_minute(), tempo.note_type(), true);
53 }
54
55 TempoDialog::TempoDialog (TempoSection& section, const string & action)
56         : ArdourDialog ("tempo dialog"),
57           bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0, 1.0),
58           bpm_spinner (bpm_adjustment),
59           bpm_frame (_("Beats per minute")),
60           ok_button (action),
61           cancel_button (_("Cancel")),
62           when_bar_label (_("Bar")),
63           when_beat_label (_("Beat")),
64           when_table (2, 2),
65           when_frame (_("Location"))
66 {
67         init (section.start(), section.beats_per_minute(), section.note_type(), section.movable());
68 }
69
70 void
71 TempoDialog::init (const BBT_Time& when, double bpm, double note_type, bool movable)
72 {
73         bpm_spinner.set_numeric (true);
74         bpm_spinner.set_digits (2);
75         bpm_spinner.set_wrap (true);
76         bpm_spinner.set_value (bpm);
77
78         strings.push_back (_("whole (1)"));
79         strings.push_back (_("second (2)"));
80         strings.push_back (_("third (3)"));
81         strings.push_back (_("quarter (4)"));
82         strings.push_back (_("eighth (8)"));
83         strings.push_back (_("sixteenth (16)"));
84         strings.push_back (_("thirty-second (32)"));
85         
86         /* the string here needs to be the longest one to display */
87         const guint32 FUDGE = 20; // Combo's are stupid - they steal space from the entry for the button
88         // TRANSLATORS: this is not a mis-spelling of "thirty", we're including a vertical 
89         // descender to make sure the height gets computed properly.
90         Gtkmm2ext::set_size_request_to_display_given_text (note_types, "thirtq-second (32)", 7+FUDGE, 15);
91
92         set_popdown_strings (note_types, strings);
93
94         if (note_type==1.0f)
95                 note_types.set_active_text (_("whole (1)"));
96         else if (note_type==2.0f)
97                 note_types.set_active_text (_("second (2)"));
98         else if (note_type==3.0f)
99                 note_types.set_active_text (_("third (3)"));
100         else if (note_type==4.0f)
101                 note_types.set_active_text (_("quarter (4)"));
102         else if (note_type==8.0f)
103                 note_types.set_active_text (_("eighth (8)"));
104         else if (note_type==16.0f)
105                 note_types.set_active_text (_("sixteenth (16)"));
106         else if (note_type==32.0f)
107                 note_types.set_active_text (_("thirty-second (32)"));
108         else
109                 note_types.set_active_text (_("quarter (4)"));
110
111         hspacer1.set_border_width (5);
112         hspacer1.pack_start (bpm_spinner, false, false);
113         vspacer1.set_border_width (5);
114         vspacer1.pack_start (hspacer1, false, false);
115
116         hspacer2.set_border_width (5);
117         hspacer2.pack_start (note_types, false, false);
118         vspacer2.set_border_width (5);
119         vspacer2.pack_start (hspacer2, false, false);
120
121         bpm_frame.add (vspacer1);
122         note_frame.add (vspacer2);
123
124         if (movable) {
125                 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
126                 when_bar_entry.set_text (buf);
127                 snprintf (buf, sizeof (buf), "%" PRIu32, when.beats);
128                 when_beat_entry.set_text (buf);
129                 
130                 when_bar_entry.set_name ("MetricEntry");
131                 when_beat_entry.set_name ("MetricEntry");
132                 
133                 when_bar_label.set_name ("MetricLabel");
134                 when_beat_label.set_name ("MetricLabel");
135                 
136                 Gtkmm2ext::set_size_request_to_display_given_text (when_bar_entry, "999g", 5, 7);
137                 Gtkmm2ext::set_size_request_to_display_given_text (when_beat_entry, "999g", 5, 7);
138                 
139                 when_table.set_homogeneous (true);
140                 when_table.set_row_spacings (2);
141                 when_table.set_col_spacings (2);
142                 when_table.set_border_width (5);
143                 
144                 when_table.attach (when_bar_label, 0, 1, 0, 1, Gtk::AttachOptions(0), Gtk::FILL|Gtk::EXPAND);
145                 when_table.attach (when_bar_entry, 0, 1, 1, 2, Gtk::AttachOptions(0), Gtk::FILL|Gtk::EXPAND);
146                 
147                 when_table.attach (when_beat_label, 1, 2, 0, 1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
148                 when_table.attach (when_beat_entry, 1, 2, 1, 2, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
149                 
150                 when_frame.set_name ("MetricDialogFrame");
151                 when_frame.add (when_table);
152
153                 get_vbox()->pack_start (when_frame, false, false);
154         }
155
156         bpm_frame.set_name ("MetricDialogFrame");
157         bpm_spinner.set_name ("MetricEntry");
158         note_frame.set_name ("MetricDialogFrame");
159
160         get_vbox()->set_border_width (12);
161         get_vbox()->pack_start (bpm_frame, false, false);
162         get_vbox()->pack_start (note_frame, false, false);
163
164         add_button (Stock::CANCEL, RESPONSE_CANCEL);
165         add_button (Stock::APPLY, RESPONSE_ACCEPT);
166         set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
167         set_default_response (RESPONSE_ACCEPT);
168
169         get_vbox()->show_all();
170         bpm_spinner.show();
171
172         set_name ("MetricDialog");
173
174         bpm_spinner.signal_activate().connect (bind (mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
175         bpm_spinner.signal_button_press_event().connect (mem_fun (*this, &TempoDialog::bpm_button_press), false);
176         bpm_spinner.signal_button_release_event().connect (mem_fun (*this, &TempoDialog::bpm_button_release), false);
177         note_types.signal_changed().connect (mem_fun (*this, &TempoDialog::note_types_change));
178 }
179
180 bool
181 TempoDialog::bpm_button_press (GdkEventButton* ev)
182 {
183         return false;
184 }
185
186 bool
187 TempoDialog::bpm_button_release (GdkEventButton* ev)
188 {       
189         /* the value has been modified, accept should work now */
190
191         set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
192         return false;
193 }
194
195 double 
196 TempoDialog::get_bpm ()
197 {
198         return bpm_spinner.get_value ();
199 }       
200
201 bool
202 TempoDialog::get_bbt_time (BBT_Time& requested)
203 {
204         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
205                 return false;
206         }
207         
208         if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
209                 return false;
210         }
211
212         return true;
213 }
214
215 double
216 TempoDialog::get_note_type ()
217 {
218         double note_type = 0;
219         vector<string>::iterator i;
220         string text = note_types.get_active_text();
221         
222         for (i = strings.begin(); i != strings.end(); ++i) {
223                 if (text == *i) {
224                         if (sscanf (text.c_str(), "%*[^0-9]%lf", &note_type) != 1) {
225                                 error << string_compose(_("garbaged note type entry (%1)"), text) << endmsg;
226                                 return 0;
227                         } else {
228                                 break;
229                         }
230                 }
231         } 
232         
233         if (i == strings.end()) {
234                 if (sscanf (text.c_str(), "%lf", &note_type) != 1) {
235                         error << string_compose(_("incomprehensible note type entry (%1)"), text) << endmsg;
236                         return 0;
237                 }
238         }
239
240         cerr << "returning " << note_type << " based on " << text << endl;
241         return note_type;
242 }
243
244 void
245 TempoDialog::note_types_change ()
246 {
247         set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
248 }
249
250
251 MeterDialog::MeterDialog (TempoMap& map, nframes_t frame, const string & action)
252         : ArdourDialog ("meter dialog"),
253           note_frame (_("Meter denominator")),
254           bpb_frame (_("Beats per bar")),
255           ok_button (action),
256           cancel_button (_("Cancel")),
257           when_bar_label (_("Bar")),
258           when_beat_label (_("Beat")),
259           when_frame (_("Location"))
260 {
261         BBT_Time when;
262         frame = map.round_to_bar(frame,0); 
263         Meter meter (map.meter_at(frame));
264
265         map.bbt_time (frame, when);
266         init (when, meter.beats_per_bar(), meter.note_divisor(), true);
267 }
268
269 MeterDialog::MeterDialog (MeterSection& section, const string & action)
270         : ArdourDialog ("meter dialog"),
271           note_frame (_("Meter denominator")),
272           bpb_frame (_("Beats per bar")),
273           ok_button (action),
274           cancel_button (_("Cancel")),
275           when_bar_label (_("Bar")),
276           when_beat_label (_("Beat")),
277           when_frame (_("Location"))
278 {
279         init (section.start(), section.beats_per_bar(), section.note_divisor(), section.movable());
280 }
281
282 void
283 MeterDialog::init (const BBT_Time& when, double bpb, double note_type, bool movable)
284 {
285         snprintf (buf, sizeof (buf), "%.2f", bpb);
286         bpb_entry.set_text (buf);
287         bpb_entry.select_region (0, -1);
288         Gtkmm2ext::set_size_request_to_display_given_text (bpb_entry, "999999g", 5, 5);
289
290         strings.push_back (_("whole (1)"));
291         strings.push_back (_("second (2)"));
292         strings.push_back (_("third (3)"));
293         strings.push_back (_("quarter (4)"));
294         strings.push_back (_("eighth (8)"));
295         strings.push_back (_("sixteenth (16)"));
296         strings.push_back (_("thirty-second (32)"));
297         
298         /* the string here needs to be the longest one to display */
299         const guint32 FUDGE = 20; // Combo's are stupid - they steal space from the entry for the button
300
301         // TRANSLATORS: this is not a mis-spelling of "thirty", we're including a vertical 
302         // descender to make sure the height gets computed properly.
303         Gtkmm2ext::set_size_request_to_display_given_text (note_types, _("thirtq-second (32)"), 7+FUDGE, 15);
304
305         set_popdown_strings (note_types, strings);
306
307         if (note_type==1.0f)
308                 note_types.set_active_text (_("whole (1)"));
309         else if (note_type==2.0f)
310                 note_types.set_active_text (_("second (2)"));
311         else if (note_type==3.0f)
312                 note_types.set_active_text (_("third (3)"));
313         else if (note_type==4.0f)
314                 note_types.set_active_text (_("quarter (4)"));
315         else if (note_type==8.0f)
316                 note_types.set_active_text (_("eighth (8)"));
317         else if (note_type==16.0f)
318                 note_types.set_active_text (_("sixteenth (16)"));
319         else if (note_type==32.0f)
320                 note_types.set_active_text (_("thirty-second (32)"));
321         else
322                 note_types.set_active_text (_("quarter (4)"));
323                 
324         hspacer1.set_border_width (5);
325         hspacer1.pack_start (note_types, false, false);
326         vspacer1.set_border_width (5);
327         vspacer1.pack_start (hspacer1, false, false);
328
329         hspacer2.set_border_width (5);
330         hspacer2.pack_start (bpb_entry, false, false);
331         vspacer2.set_border_width (5);
332         vspacer2.pack_start (hspacer2, false, false);
333
334         note_frame.add (vspacer1);
335         bpb_frame.add (vspacer2);
336
337         if (movable) {
338                 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
339                 when_bar_entry.set_text (buf);
340                 snprintf (buf, sizeof (buf), "%" PRIu32, when.beats);
341                 when_beat_entry.set_text (buf);
342                 
343                 when_bar_entry.set_name ("MetricEntry");
344                 when_beat_entry.set_name ("MetricEntry");
345                 
346                 when_bar_label.set_name ("MetricLabel");
347                 when_beat_label.set_name ("MetricLabel");
348                 
349                 Gtkmm2ext::set_size_request_to_display_given_text (when_bar_entry, "999g", 5, 7);
350                 Gtkmm2ext::set_size_request_to_display_given_text (when_beat_entry, "999g", 5, 7);
351                 
352                 when_table.set_homogeneous (true);
353                 when_table.set_row_spacings (2);
354                 when_table.set_col_spacings (2);
355                 when_table.set_border_width (5);
356                 
357                 when_table.attach (when_bar_label, 0, 1, 0, 1, Gtk::AttachOptions(0), Gtk::FILL|Gtk::EXPAND);
358                 when_table.attach (when_bar_entry, 0, 1, 1, 2, Gtk::AttachOptions(0), Gtk::FILL|Gtk::EXPAND);
359                 
360                 when_table.attach (when_beat_label, 1, 2, 0, 1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
361                 when_table.attach (when_beat_entry, 1, 2, 1, 2, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
362                 
363                 when_frame.set_name ("MetricDialogFrame");
364                 when_frame.add (when_table);
365                 
366                 get_vbox()->pack_start (when_frame, false, false);
367         }
368
369         get_vbox()->set_border_width (12);
370         get_vbox()->pack_start (bpb_frame, false, false);
371         get_vbox()->pack_start (note_frame, false, false);
372         
373         bpb_frame.set_name ("MetricDialogFrame");
374         note_frame.set_name ("MetricDialogFrame");
375         bpb_entry.set_name ("MetricEntry");
376
377         add_button (Stock::CANCEL, RESPONSE_CANCEL);
378         add_button (Stock::APPLY, RESPONSE_ACCEPT);
379         set_response_sensitive (RESPONSE_ACCEPT, false);
380         set_default_response (RESPONSE_ACCEPT);
381
382         get_vbox()->show_all ();
383         bpb_entry.show ();
384
385         set_name ("MetricDialog");
386         bpb_entry.signal_activate().connect (bind (mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
387         bpb_entry.signal_key_press_event().connect (mem_fun (*this, &MeterDialog::bpb_key_press), false);
388         bpb_entry.signal_key_release_event().connect (mem_fun (*this, &MeterDialog::bpb_key_release));
389         note_types.signal_changed().connect (mem_fun (*this, &MeterDialog::note_types_change));
390 }
391
392 bool
393 MeterDialog::bpb_key_press (GdkEventKey* ev)
394 {
395
396 switch (ev->keyval) { 
397
398  case GDK_0:
399  case GDK_1:
400  case GDK_2:
401  case GDK_3:
402  case GDK_4:
403  case GDK_5:
404  case GDK_6:
405  case GDK_7:
406  case GDK_8:
407  case GDK_9:
408  case GDK_KP_0:
409  case GDK_KP_1:
410  case GDK_KP_2:
411  case GDK_KP_3:
412  case GDK_KP_4:
413  case GDK_KP_5:
414  case GDK_KP_6:
415  case GDK_KP_7:
416  case GDK_KP_8:
417  case GDK_KP_9:
418  case GDK_period:
419  case GDK_comma:
420  case  GDK_KP_Delete:
421  case  GDK_KP_Enter:
422  case  GDK_Delete:
423  case  GDK_BackSpace:
424  case  GDK_Escape:
425  case  GDK_Return:
426  case  GDK_Home:
427  case  GDK_End:
428  case  GDK_Left:
429  case  GDK_Right:
430  case  GDK_Num_Lock:
431  case  GDK_Tab:
432     return FALSE;
433  default:
434       break;
435  }
436
437    return TRUE;
438 }
439
440 bool
441 MeterDialog::bpb_key_release (GdkEventKey* ev)
442 {
443         if (bpb_entry.get_text() != "") {
444                 set_response_sensitive (RESPONSE_ACCEPT, true);
445         } else {
446                 set_response_sensitive (RESPONSE_ACCEPT, false);
447         }
448         return false;
449 }
450
451 void
452 MeterDialog::note_types_change ()
453 {
454         set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
455 }
456
457 double
458 MeterDialog::get_bpb ()
459 {
460         double bpb = 0;
461         
462         if (sscanf (bpb_entry.get_text().c_str(), "%lf", &bpb) != 1) {
463                 return 0;
464         }
465
466         return bpb;
467 }
468         
469 double
470 MeterDialog::get_note_type ()
471 {
472         double note_type = 0;
473         vector<string>::iterator i;
474         string text = note_types.get_active_text();
475         
476         for (i = strings.begin(); i != strings.end(); ++i) {
477                 if (text == *i) {
478                         if (sscanf (text.c_str(), "%*[^0-9]%lf", &note_type) != 1) {
479                                 error << string_compose(_("garbaged note type entry (%1)"), text) << endmsg;
480                                 return 0;
481                         } else {
482                                 break;
483                         }
484                 }
485         } 
486         
487         if (i == strings.end()) {
488                 if (sscanf (text.c_str(), "%lf", &note_type) != 1) {
489                         error << string_compose(_("incomprehensible note type entry (%1)"), text) << endmsg;
490                         return 0;
491                 }
492         }
493
494         return note_type;
495 }
496
497 bool
498 MeterDialog::get_bbt_time (BBT_Time& requested)
499 {
500         requested.ticks = 0;
501
502         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
503                 return false;
504         }
505         
506         if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
507                 return false;
508         }
509
510         return true;
511 }