enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / gtk2_ardour / export_timespan_selector.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "export_timespan_selector.h"
22
23 #include "ardour/location.h"
24 #include "ardour/types.h"
25 #include "ardour/session.h"
26 #include "ardour/export_handler.h"
27 #include "ardour/export_timespan.h"
28
29 #include "pbd/enumwriter.h"
30 #include "pbd/convert.h"
31
32 #include <sstream>
33 #include <iomanip>
34
35 #include "pbd/i18n.h"
36
37 using namespace Glib;
38 using namespace ARDOUR;
39 using namespace PBD;
40 using std::string;
41
42 ExportTimespanSelector::ExportTimespanSelector (ARDOUR::Session * session, ProfileManagerPtr manager) :
43         manager (manager),
44         time_format_label (_("Show Times as:"), Gtk::ALIGN_LEFT)
45 {
46         set_session (session);
47
48         option_hbox.pack_start (time_format_label, false, false, 0);
49         option_hbox.pack_start (time_format_combo, false, false, 6);
50
51         Gtk::Button* b = Gtk::manage (new Gtk::Button (_("Select All")));
52         b->signal_clicked().connect (
53                 sigc::bind (
54                         sigc::mem_fun (*this, &ExportTimespanSelector::set_selection_state_of_all_timespans), true
55                         )
56                 );
57         option_hbox.pack_start (*b, false, false, 6);
58
59         b = Gtk::manage (new Gtk::Button (_("Deselect All")));
60         b->signal_clicked().connect (
61                 sigc::bind (
62                         sigc::mem_fun (*this, &ExportTimespanSelector::set_selection_state_of_all_timespans), false
63                         )
64                 );
65         option_hbox.pack_start (*b, false, false, 6);
66
67         range_scroller.add (range_view);
68
69         pack_start (option_hbox, false, false, 0);
70         pack_start (range_scroller, true, true, 6);
71
72         /*** Combo boxes ***/
73
74         Gtk::TreeModel::iterator iter;
75         Gtk::TreeModel::Row row;
76
77         /* Time format combo */
78
79         time_format_list = Gtk::ListStore::create (time_format_cols);
80         time_format_combo.set_model (time_format_list);
81
82         iter = time_format_list->append();
83         row = *iter;
84         row[time_format_cols.format] = ExportProfileManager::Timecode;
85         row[time_format_cols.label] = _("Timecode");
86
87         iter = time_format_list->append();
88         row = *iter;
89         row[time_format_cols.format] = ExportProfileManager::MinSec;
90         row[time_format_cols.label] = _("Minutes:Seconds");
91
92         iter = time_format_list->append();
93         row = *iter;
94         row[time_format_cols.format] = ExportProfileManager::BBT;
95         row[time_format_cols.label] = _("Bars:Beats");
96
97         time_format_combo.pack_start (time_format_cols.label);
98         time_format_combo.set_active (0);
99
100         time_format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportTimespanSelector::change_time_format));
101
102         /* Range view */
103
104         range_list = Gtk::ListStore::create (range_cols);
105         // order by location start times
106         range_list->set_sort_column(range_cols.location, Gtk::SORT_ASCENDING);
107         range_list->set_sort_func(range_cols.location, sigc::mem_fun(*this, &ExportTimespanSelector::location_sorter));
108         range_view.set_model (range_list);
109         range_view.set_headers_visible (true);
110 }
111
112 ExportTimespanSelector::~ExportTimespanSelector ()
113 {
114
115 }
116
117 int
118 ExportTimespanSelector::location_sorter(Gtk::TreeModel::iterator a, Gtk::TreeModel::iterator b)
119 {
120         Location *l1 = (*a)[range_cols.location];
121         Location *l2 = (*b)[range_cols.location];
122         const Location *ls = _session->locations()->session_range_location();
123
124         // always sort session range first
125         if (l1 == ls)
126                 return -1;
127         if (l2 == ls)
128                 return +1;
129
130         return l1->start() - l2->start();
131 }
132
133 void
134 ExportTimespanSelector::add_range_to_selection (ARDOUR::Location const * loc)
135 {
136         ExportTimespanPtr span = _session->get_export_handler()->add_timespan();
137
138         std::string id;
139         if (loc == state->selection_range.get()) {
140                 id = "selection";
141         } else {
142                 id = loc->id().to_s();
143         }
144
145         span->set_range (loc->start(), loc->end());
146         span->set_name (loc->name());
147         span->set_range_id (id);
148         state->timespans->push_back (span);
149 }
150
151 void
152 ExportTimespanSelector::set_time_format_from_state ()
153 {
154         Gtk::TreeModel::Children::iterator tree_it;
155         for (tree_it = time_format_list->children().begin(); tree_it != time_format_list->children().end(); ++tree_it) {
156                 if (tree_it->get_value (time_format_cols.format) == state->time_format) {
157                         time_format_combo.set_active (tree_it);
158                 }
159         }
160 }
161
162 void
163 ExportTimespanSelector::sync_with_manager ()
164 {
165         state = manager->get_timespans().front();
166         fill_range_list ();
167         CriticalSelectionChanged();
168 }
169
170 void
171 ExportTimespanSelector::change_time_format ()
172 {
173         state->time_format = time_format_combo.get_active()->get_value (time_format_cols.format);
174
175         for (Gtk::ListStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
176                 Location * location = it->get_value (range_cols.location);
177                 it->set_value (range_cols.label, construct_label (location));
178                 it->set_value (range_cols.length, construct_length (location));
179         }
180 }
181
182 std::string
183 ExportTimespanSelector::construct_label (ARDOUR::Location const * location) const
184 {
185         std::string label;
186         std::string start;
187         std::string end;
188
189         framepos_t start_frame = location->start();
190         framepos_t end_frame = location->end();
191
192         switch (state->time_format) {
193           case AudioClock::BBT:
194                 start = bbt_str (start_frame);
195                 end = bbt_str (end_frame);
196                 break;
197
198           case AudioClock::Timecode:
199                 start = timecode_str (start_frame);
200                 end = timecode_str (end_frame);
201                 break;
202
203           case AudioClock::MinSec:
204                 start = ms_str (start_frame);
205                 end = ms_str (end_frame);
206                 break;
207
208           case AudioClock::Frames:
209                 start = to_string (start_frame, std::dec);
210                 end = to_string (end_frame, std::dec);
211                 break;
212         }
213
214         // label += _("from ");
215
216         // label += "<span color=\"#7fff7f\">";
217         label += start;
218 //      label += "</span>";
219
220         label += _(" to ");
221
222 //      label += "<span color=\"#7fff7f\">";
223         label += end;
224 //      label += "</span>";
225
226         return label;
227 }
228
229 std::string
230 ExportTimespanSelector::construct_length (ARDOUR::Location const * location) const
231 {
232         if (location->length() == 0) {
233                 return "";
234         }
235
236         std::stringstream s;
237
238         switch (state->time_format) {
239         case AudioClock::BBT:
240                 s << bbt_str (location->length ());
241                 break;
242
243         case AudioClock::Timecode:
244         {
245                 Timecode::Time tc;
246                 _session->timecode_duration (location->length(), tc);
247                 tc.print (s);
248                 break;
249         }
250
251         case AudioClock::MinSec:
252                 s << ms_str (location->length ());
253                 break;
254
255         case AudioClock::Frames:
256                 s << location->length ();
257                 break;
258         }
259
260         return s.str ();
261 }
262
263
264 std::string
265 ExportTimespanSelector::bbt_str (framepos_t frames) const
266 {
267         if (!_session) {
268                 return "Error!";
269         }
270
271         std::ostringstream oss;
272         Timecode::BBT_Time time;
273         _session->bbt_time (frames, time);
274
275         print_padded (oss, time);
276         return oss.str ();
277 }
278
279 std::string
280 ExportTimespanSelector::timecode_str (framecnt_t frames) const
281 {
282         if (!_session) {
283                 return "Error!";
284         }
285
286         std::ostringstream oss;
287         Timecode::Time time;
288
289         _session->timecode_time (frames, time);
290
291         oss << std::setfill('0') << std::right <<
292           std::setw(2) <<
293           time.hours << ":" <<
294           std::setw(2) <<
295           time.minutes << ":" <<
296           std::setw(2) <<
297           time.seconds << ":" <<
298           std::setw(2) <<
299           time.frames;
300
301         return oss.str();
302 }
303
304 std::string
305 ExportTimespanSelector::ms_str (framecnt_t frames) const
306 {
307         if (!_session) {
308                 return "Error!";
309         }
310
311         std::ostringstream oss;
312         framecnt_t left;
313         int hrs;
314         int mins;
315         int secs;
316         int sec_promilles;
317
318         left = frames;
319         hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
320         left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
321         mins = (int) floor (left / (_session->frame_rate() * 60.0f));
322         left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
323         secs = (int) floor (left / (float) _session->frame_rate());
324         left -= (framecnt_t) floor ((double)(secs * _session->frame_rate()));
325         sec_promilles = (int) (left * 1000 / (float) _session->frame_rate() + 0.5);
326
327         oss << std::setfill('0') << std::right <<
328           std::setw(2) <<
329           hrs << ":" <<
330           std::setw(2) <<
331           mins << ":" <<
332           std::setw(2) <<
333           secs << "." <<
334           std::setw(3) <<
335           sec_promilles;
336
337         return oss.str();
338 }
339
340 void
341 ExportTimespanSelector::update_range_name (std::string const & path, std::string const & new_text)
342 {
343         Gtk::TreeStore::iterator it = range_list->get_iter (path);
344         it->get_value (range_cols.location)->set_name (new_text);
345
346         CriticalSelectionChanged();
347 }
348
349 void
350 ExportTimespanSelector::set_selection_state_of_all_timespans (bool s)
351 {
352         for (Gtk::ListStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
353                 it->set_value (range_cols.selected, s);
354         }
355 }
356
357 /*** ExportTimespanSelectorSingle ***/
358
359 ExportTimespanSelectorSingle::ExportTimespanSelectorSingle (ARDOUR::Session * session, ProfileManagerPtr manager, std::string range_id) :
360         ExportTimespanSelector (session, manager),
361         range_id (range_id)
362 {
363         range_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_NEVER);
364         range_view.append_column_editable (_("Range"), range_cols.name);
365
366         if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (0))) {
367                 renderer->signal_edited().connect (sigc::mem_fun (*this, &ExportTimespanSelectorSingle::update_range_name));
368         }
369
370         Gtk::CellRendererText * label_render = Gtk::manage (new Gtk::CellRendererText());
371         Gtk::TreeView::Column * label_col = Gtk::manage (new Gtk::TreeView::Column (_("Time Span"), *label_render));
372         label_col->add_attribute (label_render->property_markup(), range_cols.label);
373         range_view.append_column (*label_col);
374
375         range_view.append_column (_("Length"), range_cols.length);
376 }
377
378 void
379 ExportTimespanSelectorSingle::fill_range_list ()
380 {
381         if (!state) { return; }
382
383         std::string id;
384         if (!range_id.compare (X_("selection"))) {
385                 id = state->selection_range->id().to_s();
386         } else {
387                 id = range_id;
388         }
389
390         range_list->clear();
391         state->timespans->clear();
392
393         Gtk::TreeModel::iterator iter;
394         Gtk::TreeModel::Row row;
395         for (LocationList::const_iterator it = state->ranges->begin(); it != state->ranges->end(); ++it) {
396
397                 if (!(*it)->id().to_s().compare (id)) {
398                         iter = range_list->append();
399                         row = *iter;
400
401                         row[range_cols.location] = *it;
402                         row[range_cols.selected] = true;
403                         row[range_cols.name] = (*it)->name();
404                         row[range_cols.label] = construct_label (*it);
405                         row[range_cols.length] = construct_length (*it);
406
407                         add_range_to_selection (*it);
408
409                         break;
410                 }
411         }
412
413         set_time_format_from_state();
414 }
415
416 /*** ExportTimespanSelectorMultiple ***/
417
418 ExportTimespanSelectorMultiple::ExportTimespanSelectorMultiple (ARDOUR::Session * session, ProfileManagerPtr manager) :
419   ExportTimespanSelector (session, manager)
420 {
421         range_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
422         range_view.append_column_editable ("", range_cols.selected);
423         range_view.append_column_editable (_("Range"), range_cols.name);
424
425         if (Gtk::CellRendererToggle * renderer = dynamic_cast<Gtk::CellRendererToggle *> (range_view.get_column_cell_renderer (0))) {
426                 renderer->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_selection)));
427         }
428         if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (1))) {
429                 renderer->signal_edited().connect (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_range_name));
430         }
431
432         Gtk::CellRendererText * label_render = Gtk::manage (new Gtk::CellRendererText());
433         Gtk::TreeView::Column * label_col = Gtk::manage (new Gtk::TreeView::Column (_("Time Span"), *label_render));
434         label_col->add_attribute (label_render->property_markup(), range_cols.label);
435         range_view.append_column (*label_col);
436
437         range_view.append_column (_("Length"), range_cols.length);
438 }
439
440 void
441 ExportTimespanSelectorMultiple::fill_range_list ()
442 {
443         if (!state) { return; }
444
445         range_list->clear();
446
447         Gtk::TreeModel::iterator iter;
448         Gtk::TreeModel::Row row;
449         for (LocationList::const_iterator it = state->ranges->begin(); it != state->ranges->end(); ++it) {
450
451                 iter = range_list->append();
452                 row = *iter;
453
454                 row[range_cols.location] = *it;
455                 row[range_cols.selected] = false;
456                 row[range_cols.name] = (*it)->name();
457                 row[range_cols.label] = construct_label (*it);
458                 row[range_cols.length] = construct_length (*it);
459         }
460
461         set_selection_from_state ();
462 }
463
464 void
465 ExportTimespanSelectorMultiple::set_selection_from_state ()
466 {
467         Gtk::TreeModel::Children::iterator tree_it;
468
469         for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
470                 string id = (*it)->range_id();
471                 for (tree_it = range_list->children().begin(); tree_it != range_list->children().end(); ++tree_it) {
472                         Location * loc = tree_it->get_value (range_cols.location);
473
474                         if ((id == "selection" && loc == state->selection_range.get()) ||
475                             (id == loc->id().to_s())) {
476                                 tree_it->set_value (range_cols.selected, true);
477                         }
478                 }
479         }
480
481         set_time_format_from_state();
482 }
483
484 void
485 ExportTimespanSelectorMultiple::update_selection ()
486 {
487         update_timespans ();
488         CriticalSelectionChanged ();
489 }
490
491 void
492 ExportTimespanSelectorMultiple::update_timespans ()
493 {
494         state->timespans->clear();
495
496         for (Gtk::TreeStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
497                 if (it->get_value (range_cols.selected)) {
498                         add_range_to_selection (it->get_value (range_cols.location));
499                 }
500         }
501 }
502