NOOP, remove trailing tabs/whitespace.
[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 "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         time_format_combo.set_name ("PaddedButton");
82
83         iter = time_format_list->append();
84         row = *iter;
85         row[time_format_cols.format] = ExportProfileManager::Timecode;
86         row[time_format_cols.label] = _("Timecode");
87
88         iter = time_format_list->append();
89         row = *iter;
90         row[time_format_cols.format] = ExportProfileManager::MinSec;
91         row[time_format_cols.label] = _("Minutes:Seconds");
92
93         iter = time_format_list->append();
94         row = *iter;
95         row[time_format_cols.format] = ExportProfileManager::BBT;
96         row[time_format_cols.label] = _("Bars:Beats");
97
98         time_format_combo.pack_start (time_format_cols.label);
99         time_format_combo.set_active (0);
100
101         time_format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportTimespanSelector::change_time_format));
102
103         /* Range view */
104
105         range_list = Gtk::ListStore::create (range_cols);
106         // order by location start times
107         range_list->set_sort_column(range_cols.location, Gtk::SORT_ASCENDING);
108         range_list->set_sort_func(range_cols.location, sigc::mem_fun(*this, &ExportTimespanSelector::location_sorter));
109         range_view.set_model (range_list);
110         range_view.set_headers_visible (true);
111 }
112
113 ExportTimespanSelector::~ExportTimespanSelector ()
114 {
115
116 }
117
118 int
119 ExportTimespanSelector::location_sorter(Gtk::TreeModel::iterator a, Gtk::TreeModel::iterator b)
120 {
121         Location *l1 = (*a)[range_cols.location];
122         Location *l2 = (*b)[range_cols.location];
123         const Location *ls = _session->locations()->session_range_location();
124
125         // always sort session range first
126         if (l1 == ls)
127                 return -1;
128         if (l2 == ls)
129                 return +1;
130
131         return l1->start() - l2->start();
132 }
133
134 void
135 ExportTimespanSelector::add_range_to_selection (ARDOUR::Location const * loc)
136 {
137         ExportTimespanPtr span = _session->get_export_handler()->add_timespan();
138
139         std::string id;
140         if (loc == state->selection_range.get()) {
141                 id = "selection";
142         } else {
143                 id = loc->id().to_s();
144         }
145
146         span->set_range (loc->start(), loc->end());
147         span->set_name (loc->name());
148         span->set_range_id (id);
149         state->timespans->push_back (span);
150 }
151
152 void
153 ExportTimespanSelector::set_time_format_from_state ()
154 {
155         Gtk::TreeModel::Children::iterator tree_it;
156         for (tree_it = time_format_list->children().begin(); tree_it != time_format_list->children().end(); ++tree_it) {
157                 if (tree_it->get_value (time_format_cols.format) == state->time_format) {
158                         time_format_combo.set_active (tree_it);
159                 }
160         }
161 }
162
163 void
164 ExportTimespanSelector::sync_with_manager ()
165 {
166         state = manager->get_timespans().front();
167         fill_range_list ();
168         CriticalSelectionChanged();
169 }
170
171 void
172 ExportTimespanSelector::change_time_format ()
173 {
174         state->time_format = time_format_combo.get_active()->get_value (time_format_cols.format);
175
176         for (Gtk::ListStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
177                 Location * location = it->get_value (range_cols.location);
178                 it->set_value (range_cols.label, construct_label (location));
179                 it->set_value (range_cols.length, construct_length (location));
180         }
181 }
182
183 std::string
184 ExportTimespanSelector::construct_label (ARDOUR::Location const * location) const
185 {
186         std::string label;
187         std::string start;
188         std::string end;
189
190         framepos_t start_frame = location->start();
191         framepos_t end_frame = location->end();
192
193         switch (state->time_format) {
194           case AudioClock::BBT:
195                 start = bbt_str (start_frame);
196                 end = bbt_str (end_frame);
197                 break;
198
199           case AudioClock::Timecode:
200                 start = timecode_str (start_frame);
201                 end = timecode_str (end_frame);
202                 break;
203
204           case AudioClock::MinSec:
205                 start = ms_str (start_frame);
206                 end = ms_str (end_frame);
207                 break;
208
209           case AudioClock::Frames:
210                 start = to_string (start_frame, std::dec);
211                 end = to_string (end_frame, std::dec);
212                 break;
213         }
214
215         // label += _("from ");
216
217         // label += "<span color=\"#7fff7f\">";
218         label += start;
219 //      label += "</span>";
220
221         label += _(" to ");
222
223 //      label += "<span color=\"#7fff7f\">";
224         label += end;
225 //      label += "</span>";
226
227         return label;
228 }
229
230 std::string
231 ExportTimespanSelector::construct_length (ARDOUR::Location const * location) const
232 {
233         if (location->length() == 0) {
234                 return "";
235         }
236
237         std::stringstream s;
238
239         switch (state->time_format) {
240         case AudioClock::BBT:
241                 s << bbt_str (location->length ());
242                 break;
243
244         case AudioClock::Timecode:
245         {
246                 Timecode::Time tc;
247                 _session->timecode_duration (location->length(), tc);
248                 tc.print (s);
249                 break;
250         }
251
252         case AudioClock::MinSec:
253                 s << ms_str (location->length ());
254                 break;
255
256         case AudioClock::Frames:
257                 s << location->length ();
258                 break;
259         }
260
261         return s.str ();
262 }
263
264
265 std::string
266 ExportTimespanSelector::bbt_str (framepos_t frames) const
267 {
268         if (!_session) {
269                 return "Error!";
270         }
271
272         std::ostringstream oss;
273         Timecode::BBT_Time time;
274         _session->bbt_time (frames, time);
275
276         print_padded (oss, time);
277         return oss.str ();
278 }
279
280 std::string
281 ExportTimespanSelector::timecode_str (framecnt_t frames) const
282 {
283         if (!_session) {
284                 return "Error!";
285         }
286
287         std::ostringstream oss;
288         Timecode::Time time;
289
290         _session->timecode_time (frames, time);
291
292         oss << std::setfill('0') << std::right <<
293           std::setw(2) <<
294           time.hours << ":" <<
295           std::setw(2) <<
296           time.minutes << ":" <<
297           std::setw(2) <<
298           time.seconds << ":" <<
299           std::setw(2) <<
300           time.frames;
301
302         return oss.str();
303 }
304
305 std::string
306 ExportTimespanSelector::ms_str (framecnt_t frames) const
307 {
308         if (!_session) {
309                 return "Error!";
310         }
311
312         std::ostringstream oss;
313         framecnt_t left;
314         int hrs;
315         int mins;
316         int secs;
317         int sec_promilles;
318
319         left = frames;
320         hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
321         left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
322         mins = (int) floor (left / (_session->frame_rate() * 60.0f));
323         left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
324         secs = (int) floor (left / (float) _session->frame_rate());
325         left -= (framecnt_t) floor ((double)(secs * _session->frame_rate()));
326         sec_promilles = (int) (left * 1000 / (float) _session->frame_rate() + 0.5);
327
328         oss << std::setfill('0') << std::right <<
329           std::setw(2) <<
330           hrs << ":" <<
331           std::setw(2) <<
332           mins << ":" <<
333           std::setw(2) <<
334           secs << "." <<
335           std::setw(3) <<
336           sec_promilles;
337
338         return oss.str();
339 }
340
341 void
342 ExportTimespanSelector::update_range_name (std::string const & path, std::string const & new_text)
343 {
344         Gtk::TreeStore::iterator it = range_list->get_iter (path);
345         it->get_value (range_cols.location)->set_name (new_text);
346
347         CriticalSelectionChanged();
348 }
349
350 void
351 ExportTimespanSelector::set_selection_state_of_all_timespans (bool s)
352 {
353         for (Gtk::ListStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
354                 it->set_value (range_cols.selected, s);
355         }
356 }
357
358 /*** ExportTimespanSelectorSingle ***/
359
360 ExportTimespanSelectorSingle::ExportTimespanSelectorSingle (ARDOUR::Session * session, ProfileManagerPtr manager, std::string range_id) :
361         ExportTimespanSelector (session, manager),
362         range_id (range_id)
363 {
364         range_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_NEVER);
365         range_view.append_column_editable (_("Range"), range_cols.name);
366
367         if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (0))) {
368                 renderer->signal_edited().connect (sigc::mem_fun (*this, &ExportTimespanSelectorSingle::update_range_name));
369         }
370
371         Gtk::CellRendererText * label_render = Gtk::manage (new Gtk::CellRendererText());
372         Gtk::TreeView::Column * label_col = Gtk::manage (new Gtk::TreeView::Column (_("Time Span"), *label_render));
373         label_col->add_attribute (label_render->property_markup(), range_cols.label);
374         range_view.append_column (*label_col);
375
376         range_view.append_column (_("Length"), range_cols.length);
377 }
378
379 void
380 ExportTimespanSelectorSingle::fill_range_list ()
381 {
382         if (!state) { return; }
383
384         std::string id;
385         if (!range_id.compare (X_("selection"))) {
386                 id = state->selection_range->id().to_s();
387         } else {
388                 id = range_id;
389         }
390
391         range_list->clear();
392         state->timespans->clear();
393
394         Gtk::TreeModel::iterator iter;
395         Gtk::TreeModel::Row row;
396         for (LocationList::const_iterator it = state->ranges->begin(); it != state->ranges->end(); ++it) {
397
398                 if (!(*it)->id().to_s().compare (id)) {
399                         iter = range_list->append();
400                         row = *iter;
401
402                         row[range_cols.location] = *it;
403                         row[range_cols.selected] = true;
404                         row[range_cols.name] = (*it)->name();
405                         row[range_cols.label] = construct_label (*it);
406                         row[range_cols.length] = construct_length (*it);
407
408                         add_range_to_selection (*it);
409
410                         break;
411                 }
412         }
413
414         set_time_format_from_state();
415 }
416
417 /*** ExportTimespanSelectorMultiple ***/
418
419 ExportTimespanSelectorMultiple::ExportTimespanSelectorMultiple (ARDOUR::Session * session, ProfileManagerPtr manager) :
420   ExportTimespanSelector (session, manager)
421 {
422         range_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
423         range_view.append_column_editable ("", range_cols.selected);
424         range_view.append_column_editable (_("Range"), range_cols.name);
425
426         if (Gtk::CellRendererToggle * renderer = dynamic_cast<Gtk::CellRendererToggle *> (range_view.get_column_cell_renderer (0))) {
427                 renderer->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_selection)));
428         }
429         if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (1))) {
430                 renderer->signal_edited().connect (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_range_name));
431         }
432
433         Gtk::CellRendererText * label_render = Gtk::manage (new Gtk::CellRendererText());
434         Gtk::TreeView::Column * label_col = Gtk::manage (new Gtk::TreeView::Column (_("Time Span"), *label_render));
435         label_col->add_attribute (label_render->property_markup(), range_cols.label);
436         range_view.append_column (*label_col);
437
438         range_view.append_column (_("Length"), range_cols.length);
439 }
440
441 void
442 ExportTimespanSelectorMultiple::fill_range_list ()
443 {
444         if (!state) { return; }
445
446         range_list->clear();
447
448         Gtk::TreeModel::iterator iter;
449         Gtk::TreeModel::Row row;
450         for (LocationList::const_iterator it = state->ranges->begin(); it != state->ranges->end(); ++it) {
451
452                 iter = range_list->append();
453                 row = *iter;
454
455                 row[range_cols.location] = *it;
456                 row[range_cols.selected] = false;
457                 row[range_cols.name] = (*it)->name();
458                 row[range_cols.label] = construct_label (*it);
459                 row[range_cols.length] = construct_length (*it);
460         }
461
462         set_selection_from_state ();
463 }
464
465 void
466 ExportTimespanSelectorMultiple::set_selection_from_state ()
467 {
468         Gtk::TreeModel::Children::iterator tree_it;
469
470         for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
471                 string id = (*it)->range_id();
472                 for (tree_it = range_list->children().begin(); tree_it != range_list->children().end(); ++tree_it) {
473                         Location * loc = tree_it->get_value (range_cols.location);
474
475                         if ((id == "selection" && loc == state->selection_range.get()) ||
476                             (id == loc->id().to_s())) {
477                                 tree_it->set_value (range_cols.selected, true);
478                         }
479                 }
480         }
481
482         set_time_format_from_state();
483 }
484
485 void
486 ExportTimespanSelectorMultiple::update_selection ()
487 {
488         update_timespans ();
489         CriticalSelectionChanged ();
490 }
491
492 void
493 ExportTimespanSelectorMultiple::update_timespans ()
494 {
495         state->timespans->clear();
496
497         for (Gtk::TreeStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
498                 if (it->get_value (range_cols.selected)) {
499                         add_range_to_selection (it->get_value (range_cols.location));
500                 }
501         }
502 }
503