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