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