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