got ardour_dialog compiling
[ardour.git] / gtk2_ardour / tempo_dialog.cc
1 #include <cstdio> // for snprintf, grrr 
2
3 #include <gtkmm2ext/utils.h>
4
5 #include "tempo_dialog.h"
6
7 #include "i18n.h"
8
9 using namespace Gtk;
10 using namespace ARDOUR;
11
12 TempoDialog::TempoDialog (TempoMap& map, jack_nframes_t frame, string action)
13         : ArdourDialog ("tempo dialog"),
14           bpm_frame (_("Beats per minute")),
15           ok_button (action),
16           cancel_button (_("Cancel")),
17           when_bar_label (_("Bar")),
18           when_beat_label (_("Beat")),
19           when_table (2, 2),
20           when_frame (_("Location"))
21 {
22         BBT_Time when;
23         Tempo tempo (map.tempo_at (frame));
24         map.bbt_time (frame, when);
25
26         init (when, tempo.beats_per_minute(), true);
27 }
28
29 TempoDialog::TempoDialog (TempoSection& section, string action)
30         : ArdourDialog ("tempo dialog"),
31           bpm_frame (_("Beats per minute")),
32           ok_button (action),
33           cancel_button (_("Cancel")),
34           when_bar_label (_("Bar")),
35           when_beat_label (_("Beat")),
36           when_table (2, 2),
37           when_frame (_("Location"))
38 {
39         init (section.start(), section.beats_per_minute(), section.movable());
40 }
41
42 void
43 TempoDialog::init (const BBT_Time& when, double bpm, bool movable)
44 {
45         snprintf (buf, sizeof (buf), "%.2f", bpm);
46         bpm_entry.set_text (buf);
47         bpm_entry.select_region (0, -1);
48         
49         hspacer1.set_border_width (5);
50         hspacer1.pack_start (bpm_entry, false, false);
51         vspacer1.set_border_width (5);
52         vspacer1.pack_start (hspacer1, false, false);
53
54         bpm_frame.add (vspacer1);
55
56         button_box.set_border_width (10);
57         button_box.set_spacing (5);
58         button_box.set_homogeneous (true);
59         button_box.pack_start (ok_button); 
60         button_box.pack_start (cancel_button); 
61
62         vpacker.set_border_width (10);
63         vpacker.set_spacing (5);
64
65         if (movable) {
66                 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
67                 when_bar_entry.set_text (buf);
68                 snprintf (buf, sizeof (buf), "%" PRIu32, when.beats);
69                 when_beat_entry.set_text (buf);
70                 
71                 when_bar_entry.set_name ("MetricEntry");
72                 when_beat_entry.set_name ("MetricEntry");
73                 
74                 when_bar_label.set_name ("MetricLabel");
75                 when_beat_label.set_name ("MetricLabel");
76                 
77                 Gtkmm2ext::set_size_request_to_display_given_text (when_bar_entry, "999g", 5, 7);
78                 Gtkmm2ext::set_size_request_to_display_given_text (when_beat_entry, "999g", 5, 7);
79                 
80                 when_table.set_homogeneous (true);
81                 when_table.set_row_spacings (2);
82                 when_table.set_col_spacings (2);
83                 when_table.set_border_width (5);
84                 
85                 when_table.attach (when_bar_label, 0, 1, 0, 1, 0, Gtk::FILL|Gtk::EXPAND);
86                 when_table.attach (when_bar_entry, 0, 1, 1, 2, 0, Gtk::FILL|Gtk::EXPAND);
87                 
88                 when_table.attach (when_beat_label, 1, 2, 0, 1, 0, 0);
89                 when_table.attach (when_beat_entry, 1, 2, 1, 2, 0, 0);
90                 
91                 when_frame.set_name ("MetricDialogFrame");
92                 when_frame.add (when_table);
93
94                 vpacker.pack_start (when_frame, false, false);
95         }
96
97         vpacker.pack_start (bpm_frame, false, false);
98         vpacker.pack_start (button_box, false, false);
99
100         bpm_frame.set_name ("MetricDialogFrame");
101         bpm_entry.set_name ("MetricEntry");
102         ok_button.set_name ("MetricButton");
103         cancel_button.set_name ("MetricButton");
104
105         add (vpacker);
106         set_name ("MetricDialog");
107
108         set_keyboard_input(true);
109 }
110
111 double 
112 TempoDialog::get_bpm ()
113 {
114         double bpm;
115         
116         if (sscanf (bpm_entry.get_text().c_str(), "%lf", &bpm) != 1) {
117                 return 0;
118         }
119
120         return bpm;
121 }       
122
123 bool
124 TempoDialog::get_bbt_time (BBT_Time& requested)
125 {
126         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
127                 return false;
128         }
129         
130         if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
131                 return false;
132         }
133
134         return true;
135 }
136
137
138 MeterDialog::MeterDialog (TempoMap& map, jack_nframes_t frame, string action)
139         : ArdourDialog ("meter dialog"),
140           note_frame (_("Meter denominator")),
141           bpb_frame (_("Beats per bar")),
142           ok_button (action),
143           cancel_button (_("Cancel")),
144           when_bar_label (_("Bar")),
145           when_beat_label (_("Beat")),
146           when_frame (_("Location"))
147 {
148         BBT_Time when;
149         frame = map.round_to_bar(frame,0); 
150         Meter meter (map.meter_at(frame));
151
152         map.bbt_time (frame, when);
153         init (when, meter.beats_per_bar(), meter.note_divisor(), true);
154 }
155
156 MeterDialog::MeterDialog (MeterSection& section, string action)
157         : ArdourDialog ("meter dialog"),
158           note_frame (_("Meter denominator")),
159           bpb_frame (_("Beats per bar")),
160           ok_button (action),
161           cancel_button (_("Cancel")),
162           when_bar_label (_("Bar")),
163           when_beat_label (_("Beat")),
164           when_frame (_("Location"))
165 {
166         init (section.start(), section.beats_per_bar(), section.note_divisor(), section.movable());
167 }
168
169 void
170 MeterDialog::init (const BBT_Time& when, double bpb, double note_type, bool movable)
171 {
172         snprintf (buf, sizeof (buf), "%.2f", bpb);
173         bpb_entry.set_text (buf);
174         bpb_entry.select_region (0, -1);
175         Gtkmm2ext::set_size_request_to_display_given_text (bpb_entry, "999999g", 5, 5);
176
177         
178         strings.push_back (_("whole (1)"));
179         strings.push_back (_("second (2)"));
180         strings.push_back (_("third (3)"));
181         strings.push_back (_("quarter (4)"));
182         strings.push_back (_("eighth (8)"));
183         strings.push_back (_("sixteenth (16)"));
184         strings.push_back (_("thirty-second (32)"));
185         
186         set_popdown_strings (note_types, strings);
187         
188         if (note_type==1.0f)
189                 note_types.get_entry()->set_text(_("whole (1)"));
190         else if (note_type==2.0f)
191                 note_types.get_entry()->set_text(_("second (2)"));
192         else if (note_type==3.0f)
193                 note_types.get_entry()->set_text(_("third (3)"));
194         else if (note_type==4.0f)
195                 note_types.get_entry()->set_text(_("quarter (4)"));
196         else if (note_type==8.0f)
197                 note_types.get_entry()->set_text(_("eighth (8)"));
198         else if (note_type==16.0f)
199                 note_types.get_entry()->set_text(_("sixteenth (16)"));
200         else if (note_type==32.0f)
201                 note_types.get_entry()->set_text(_("thirty-second (32)"));
202         else
203                 note_types.get_entry()->set_text(_("quarter (4)"));
204                 
205         /* strings.back() just happens to be the longest one to display */
206         Gtkmm2ext::set_size_request_to_display_given_text (*(note_types.get_entry()), strings.back(), 7, 7);
207
208         hspacer1.set_border_width (5);
209         hspacer1.pack_start (note_types, false, false);
210         vspacer1.set_border_width (5);
211         vspacer1.pack_start (hspacer1, false, false);
212
213         hspacer2.set_border_width (5);
214         hspacer2.pack_start (bpb_entry, false, false);
215         vspacer2.set_border_width (5);
216         vspacer2.pack_start (hspacer2, false, false);
217
218         note_frame.add (vspacer1);
219         bpb_frame.add (vspacer2);
220
221         button_box.set_border_width (10);
222         button_box.set_spacing (5);
223         button_box.set_homogeneous (true);
224         button_box.pack_start (ok_button); 
225         button_box.pack_start (cancel_button); 
226
227         vpacker.set_border_width (10);
228         vpacker.set_spacing (5);
229         
230         if (movable) {
231                 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
232                 when_bar_entry.set_text (buf);
233                 snprintf (buf, sizeof (buf), "%" PRIu32, when.beats);
234                 when_beat_entry.set_text (buf);
235                 
236                 when_bar_entry.set_name ("MetricEntry");
237                 when_beat_entry.set_name ("MetricEntry");
238                 
239                 when_bar_label.set_name ("MetricLabel");
240                 when_beat_label.set_name ("MetricLabel");
241                 
242                 Gtkmm2ext::set_size_request_to_display_given_text (when_bar_entry, "999g", 5, 7);
243                 Gtkmm2ext::set_size_request_to_display_given_text (when_beat_entry, "999g", 5, 7);
244                 
245                 when_table.set_homogeneous (true);
246                 when_table.set_row_spacings (2);
247                 when_table.set_col_spacings (2);
248                 when_table.set_border_width (5);
249                 
250                 when_table.attach (when_bar_label, 0, 1, 0, 1, 0, Gtk::FILL|Gtk::EXPAND);
251                 when_table.attach (when_bar_entry, 0, 1, 1, 2, 0, Gtk::FILL|Gtk::EXPAND);
252                 
253                 when_table.attach (when_beat_label, 1, 2, 0, 1, 0, 0);
254                 when_table.attach (when_beat_entry, 1, 2, 1, 2, 0, 0);
255                 
256                 when_frame.set_name ("MetricDialogFrame");
257                 when_frame.add (when_table);
258                 
259                 vpacker.pack_start (when_frame, false, false);
260         }
261
262         vpacker.pack_start (bpb_frame, false, false);
263         vpacker.pack_start (note_frame, false, false);
264         vpacker.pack_start (button_box, false, false);
265         
266         bpb_frame.set_name ("MetricDialogFrame");
267         note_frame.set_name ("MetricDialogFrame");
268         note_types.get_entry()->set_name ("MetricEntry");
269         bpb_entry.set_name ("MetricEntry");
270         ok_button.set_name ("MetricButton");
271         cancel_button.set_name ("MetricButton");
272
273         add (vpacker);
274         set_name ("MetricDialog");
275
276         set_keyboard_input(true);
277 }
278
279 double
280 MeterDialog::get_bpb ()
281 {
282         double bpb = 0;
283         
284         if (sscanf (bpb_entry.get_text().c_str(), "%lf", &bpb) != 1) {
285                 return 0;
286         }
287
288         return bpb;
289 }
290         
291 double
292 MeterDialog::get_note_type ()
293 {
294         double note_type = 0;
295         vector<const gchar *>::iterator i;
296         string text = note_types.get_entry()->get_text();
297         
298         for (i = strings.begin(); i != strings.end(); ++i) {
299                 if (text == *i) {
300                         if (sscanf (text.c_str(), "%*[^0-9]%lf", &note_type) != 1) {
301                                 error << compose(_("garbaged note type entry (%1)"), text) << endmsg;
302                                 return 0;
303                         } else {
304                                 break;
305                         }
306                 }
307         } 
308         
309         if (i == strings.end()) {
310                 if (sscanf (text.c_str(), "%lf", &note_type) != 1) {
311                         error << compose(_("incomprehensible note type entry (%1)"), text) << endmsg;
312                         return 0;
313                 }
314         }
315
316         return note_type;
317 }
318
319 bool
320 MeterDialog::get_bbt_time (BBT_Time& requested)
321 {
322         requested.ticks = 0;
323
324         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
325                 return false;
326         }
327         
328         if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
329                 return false;
330         }
331
332         return true;
333 }