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