Remove erroneous assert which I added earlier.
[ardour2.git] / gtk2_ardour / export_timespan_selector.cc
blob78e92a22a637507eff68bc5d3b729f4cd3549ad1
1 /*
2 Copyright (C) 2008 Paul Davis
3 Author: Sakari Bergen
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.
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.
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.
21 #include "export_timespan_selector.h"
23 #include "ardour_ui.h"
25 #include "ardour/tempo.h"
26 #include "ardour/location.h"
27 #include "ardour/types.h"
28 #include "ardour/session.h"
29 #include "ardour/export_handler.h"
30 #include "ardour/export_timespan.h"
32 #include "pbd/enumwriter.h"
33 #include "pbd/convert.h"
35 #include <sstream>
36 #include <iomanip>
38 #include "i18n.h"
40 using namespace Glib;
41 using namespace ARDOUR;
42 using namespace PBD;
43 using std::string;
45 ExportTimespanSelector::ExportTimespanSelector (ARDOUR::Session * session, ProfileManagerPtr manager) :
46 manager (manager),
47 time_format_label (_("Show Times as:"), Gtk::ALIGN_LEFT)
49 set_session (session);
51 option_hbox.pack_start (time_format_label, false, false, 0);
52 option_hbox.pack_start (time_format_combo, false, false, 6);
54 range_scroller.add (range_view);
56 pack_start (option_hbox, false, false, 0);
57 pack_start (range_scroller, true, true, 6);
59 /*** Combo boxes ***/
61 Gtk::TreeModel::iterator iter;
62 Gtk::TreeModel::Row row;
64 /* Time format combo */
66 time_format_list = Gtk::ListStore::create (time_format_cols);
67 time_format_combo.set_model (time_format_list);
68 time_format_combo.set_name ("PaddedButton");
70 iter = time_format_list->append();
71 row = *iter;
72 row[time_format_cols.format] = ExportProfileManager::Timecode;
73 row[time_format_cols.label] = _("Timecode");
75 iter = time_format_list->append();
76 row = *iter;
77 row[time_format_cols.format] = ExportProfileManager::MinSec;
78 row[time_format_cols.label] = _("Minutes:Seconds");
80 iter = time_format_list->append();
81 row = *iter;
82 row[time_format_cols.format] = ExportProfileManager::BBT;
83 row[time_format_cols.label] = _("Bars:Beats");
85 time_format_combo.pack_start (time_format_cols.label);
86 time_format_combo.set_active (0);
88 time_format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportTimespanSelector::change_time_format));
90 /* Range view */
92 range_list = Gtk::ListStore::create (range_cols);
93 range_view.set_model (range_list);
94 range_view.set_headers_visible (true);
97 ExportTimespanSelector::~ExportTimespanSelector ()
102 void
103 ExportTimespanSelector::add_range_to_selection (ARDOUR::Location const * loc)
105 ExportTimespanPtr span = _session->get_export_handler()->add_timespan();
107 std::string id;
108 if (loc == state->session_range.get()) {
109 id = "session";
110 } else if (loc == state->selection_range.get()) {
111 id = "selection";
112 } else {
113 id = loc->id().to_s();
116 span->set_range (loc->start(), loc->end());
117 span->set_name (loc->name());
118 span->set_range_id (id);
119 state->timespans->push_back (span);
122 void
123 ExportTimespanSelector::set_time_format_from_state ()
125 Gtk::TreeModel::Children::iterator tree_it;
126 for (tree_it = time_format_list->children().begin(); tree_it != time_format_list->children().end(); ++tree_it) {
127 if (tree_it->get_value (time_format_cols.format) == state->time_format) {
128 time_format_combo.set_active (tree_it);
133 void
134 ExportTimespanSelector::sync_with_manager ()
136 state = manager->get_timespans().front();
137 fill_range_list ();
138 CriticalSelectionChanged();
141 void
142 ExportTimespanSelector::change_time_format ()
144 state->time_format = time_format_combo.get_active()->get_value (time_format_cols.format);
146 for (Gtk::ListStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
147 Location * location = it->get_value (range_cols.location);
148 it->set_value (range_cols.label, construct_label (location));
149 it->set_value (range_cols.length, construct_length (location));
153 std::string
154 ExportTimespanSelector::construct_label (ARDOUR::Location const * location) const
156 std::string label;
157 std::string start;
158 std::string end;
160 framepos_t start_frame = location->start();
161 framepos_t end_frame = location->end();
163 switch (state->time_format) {
164 case AudioClock::BBT:
165 start = bbt_str (start_frame);
166 end = bbt_str (end_frame);
167 break;
169 case AudioClock::Timecode:
170 start = timecode_str (start_frame);
171 end = timecode_str (end_frame);
172 break;
174 case AudioClock::MinSec:
175 start = ms_str (start_frame);
176 end = ms_str (end_frame);
177 break;
179 case AudioClock::Frames:
180 start = to_string (start_frame, std::dec);
181 end = to_string (end_frame, std::dec);
182 break;
185 // label += _("from ");
187 // label += "<span color=\"#7fff7f\">";
188 label += start;
189 // label += "</span>";
191 label += _(" to ");
193 // label += "<span color=\"#7fff7f\">";
194 label += end;
195 // label += "</span>";
197 return label;
200 std::string
201 ExportTimespanSelector::construct_length (ARDOUR::Location const * location) const
203 if (location->length() == 0) {
204 return "";
207 std::stringstream s;
209 switch (state->time_format) {
210 case AudioClock::BBT:
211 s << bbt_str (location->length ());
212 break;
214 case AudioClock::Timecode:
216 Timecode::Time tc;
217 _session->timecode_duration (location->length(), tc);
218 tc.print (s);
219 break;
222 case AudioClock::MinSec:
223 s << ms_str (location->length ());
224 break;
226 case AudioClock::Frames:
227 s << location->length ();
228 break;
231 return s.str ();
235 std::string
236 ExportTimespanSelector::bbt_str (framepos_t frames) const
238 if (!_session) {
239 return "Error!";
242 std::ostringstream oss;
243 Timecode::BBT_Time time;
244 _session->bbt_time (frames, time);
246 print_padded (oss, time);
247 return oss.str ();
250 std::string
251 ExportTimespanSelector::timecode_str (framecnt_t frames) const
253 if (!_session) {
254 return "Error!";
257 std::ostringstream oss;
258 Timecode::Time time;
260 _session->timecode_time (frames, time);
262 oss << std::setfill('0') << std::right <<
263 std::setw(2) <<
264 time.hours << ":" <<
265 std::setw(2) <<
266 time.minutes << ":" <<
267 std::setw(2) <<
268 time.seconds << ":" <<
269 std::setw(2) <<
270 time.frames;
272 return oss.str();
275 std::string
276 ExportTimespanSelector::ms_str (framecnt_t frames) const
278 if (!_session) {
279 return "Error!";
282 std::ostringstream oss;
283 framecnt_t left;
284 int hrs;
285 int mins;
286 int secs;
287 int sec_promilles;
289 left = frames;
290 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
291 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
292 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
293 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
294 secs = (int) floor (left / (float) _session->frame_rate());
295 left -= (framecnt_t) floor (secs * _session->frame_rate());
296 sec_promilles = (int) (left * 1000 / (float) _session->frame_rate() + 0.5);
298 oss << std::setfill('0') << std::right <<
299 std::setw(2) <<
300 hrs << ":" <<
301 std::setw(2) <<
302 mins << ":" <<
303 std::setw(2) <<
304 secs << "." <<
305 std::setw(3) <<
306 sec_promilles;
308 return oss.str();
311 void
312 ExportTimespanSelector::update_range_name (std::string const & path, std::string const & new_text)
314 Gtk::TreeStore::iterator it = range_list->get_iter (path);
315 it->get_value (range_cols.location)->set_name (new_text);
317 CriticalSelectionChanged();
320 /*** ExportTimespanSelectorSingle ***/
322 ExportTimespanSelectorSingle::ExportTimespanSelectorSingle (ARDOUR::Session * session, ProfileManagerPtr manager, std::string range_id) :
323 ExportTimespanSelector (session, manager),
324 range_id (range_id)
326 range_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_NEVER);
327 range_view.append_column_editable (_("Range"), range_cols.name);
329 // Adjust selector height
330 int x_offset, y_offset, width, height;
331 Gtk::CellRenderer * renderer = *range_view.get_column(0)->get_cell_renderers().begin();
332 renderer->get_size (range_view, x_offset, y_offset, width, height);
333 range_scroller.set_size_request (-1, height);
335 if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (0))) {
336 renderer->signal_edited().connect (sigc::mem_fun (*this, &ExportTimespanSelectorSingle::update_range_name));
339 Gtk::CellRendererText * label_render = Gtk::manage (new Gtk::CellRendererText());
340 Gtk::TreeView::Column * label_col = Gtk::manage (new Gtk::TreeView::Column (_("Time Span"), *label_render));
341 label_col->add_attribute (label_render->property_markup(), range_cols.label);
342 range_view.append_column (*label_col);
344 range_view.append_column (_("Length"), range_cols.length);
347 void
348 ExportTimespanSelectorSingle::fill_range_list ()
350 if (!state) { return; }
352 std::string id;
353 if (!range_id.compare (X_("session"))) {
354 id = state->session_range->id().to_s();
355 } else if (!range_id.compare (X_("selection"))) {
356 id = state->selection_range->id().to_s();
357 } else {
358 id = range_id;
361 range_list->clear();
362 state->timespans->clear();
364 Gtk::TreeModel::iterator iter;
365 Gtk::TreeModel::Row row;
366 for (LocationList::const_iterator it = state->ranges->begin(); it != state->ranges->end(); ++it) {
368 if (!(*it)->id().to_s().compare (id)) {
369 iter = range_list->append();
370 row = *iter;
372 row[range_cols.location] = *it;
373 row[range_cols.selected] = true;
374 row[range_cols.name] = (*it)->name();
375 row[range_cols.label] = construct_label (*it);
376 row[range_cols.length] = construct_length (*it);
378 add_range_to_selection (*it);
380 break;
384 set_time_format_from_state();
387 /*** ExportTimespanSelectorMultiple ***/
389 ExportTimespanSelectorMultiple::ExportTimespanSelectorMultiple (ARDOUR::Session * session, ProfileManagerPtr manager) :
390 ExportTimespanSelector (session, manager)
392 range_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
393 range_view.append_column_editable ("", range_cols.selected);
394 range_view.append_column_editable (_("Range"), range_cols.name);
396 if (Gtk::CellRendererToggle * renderer = dynamic_cast<Gtk::CellRendererToggle *> (range_view.get_column_cell_renderer (0))) {
397 renderer->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_selection)));
399 if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (1))) {
400 renderer->signal_edited().connect (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_range_name));
403 Gtk::CellRendererText * label_render = Gtk::manage (new Gtk::CellRendererText());
404 Gtk::TreeView::Column * label_col = Gtk::manage (new Gtk::TreeView::Column (_("Time Span"), *label_render));
405 label_col->add_attribute (label_render->property_markup(), range_cols.label);
406 range_view.append_column (*label_col);
408 range_view.append_column (_("Length"), range_cols.length);
411 void
412 ExportTimespanSelectorMultiple::fill_range_list ()
414 if (!state) { return; }
416 range_list->clear();
418 Gtk::TreeModel::iterator iter;
419 Gtk::TreeModel::Row row;
420 for (LocationList::const_iterator it = state->ranges->begin(); it != state->ranges->end(); ++it) {
422 iter = range_list->append();
423 row = *iter;
425 row[range_cols.location] = *it;
426 row[range_cols.selected] = false;
427 row[range_cols.name] = (*it)->name();
428 row[range_cols.label] = construct_label (*it);
429 row[range_cols.length] = construct_length (*it);
432 set_selection_from_state ();
435 void
436 ExportTimespanSelectorMultiple::set_selection_from_state ()
438 Gtk::TreeModel::Children::iterator tree_it;
440 for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
441 string id = (*it)->range_id();
442 for (tree_it = range_list->children().begin(); tree_it != range_list->children().end(); ++tree_it) {
443 Location * loc = tree_it->get_value (range_cols.location);
445 if ((!id.compare ("session") && loc == state->session_range.get()) ||
446 (!id.compare ("selection") && loc == state->selection_range.get()) ||
447 (!id.compare (loc->id().to_s()))) {
448 tree_it->set_value (range_cols.selected, true);
453 set_time_format_from_state();
456 void
457 ExportTimespanSelectorMultiple::update_selection ()
459 update_timespans ();
460 CriticalSelectionChanged ();
463 void
464 ExportTimespanSelectorMultiple::update_timespans ()
466 state->timespans->clear();
468 for (Gtk::TreeStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
469 if (it->get_value (range_cols.selected)) {
470 add_range_to_selection (it->get_value (range_cols.location));