fix up file renaming code a little bit
[ArdourMidi.git] / gtk2_ardour / export_timespan_selector.cc
blob9f39fa556cc182f1585c993563e39253f1bd894f
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/location.h"
26 #include "ardour/types.h"
27 #include "ardour/session.h"
28 #include "ardour/export_handler.h"
29 #include "ardour/export_timespan.h"
31 #include "pbd/enumwriter.h"
32 #include "pbd/convert.h"
34 #include <sstream>
35 #include <iomanip>
37 #include "i18n.h"
39 using namespace Glib;
40 using namespace ARDOUR;
41 using namespace PBD;
43 ExportTimespanSelector::ExportTimespanSelector (ARDOUR::Session * session, ProfileManagerPtr manager) :
44 manager (manager),
45 time_format_label (_("Show Times as:"), Gtk::ALIGN_LEFT)
47 set_session (session);
49 option_hbox.pack_start (time_format_label, false, false, 0);
50 option_hbox.pack_start (time_format_combo, false, false, 6);
52 range_scroller.add (range_view);
54 pack_start (option_hbox, false, false, 0);
55 pack_start (range_scroller, true, true, 6);
57 /*** Combo boxes ***/
59 Gtk::TreeModel::iterator iter;
60 Gtk::TreeModel::Row row;
62 /* Time format combo */
64 time_format_list = Gtk::ListStore::create (time_format_cols);
65 time_format_combo.set_model (time_format_list);
66 time_format_combo.set_name ("PaddedButton");
68 iter = time_format_list->append();
69 row = *iter;
70 row[time_format_cols.format] = ExportProfileManager::Timecode;
71 row[time_format_cols.label] = X_("Timecode");
73 iter = time_format_list->append();
74 row = *iter;
75 row[time_format_cols.format] = ExportProfileManager::MinSec;
76 row[time_format_cols.label] = _("Minutes:Seconds");
78 iter = time_format_list->append();
79 row = *iter;
80 row[time_format_cols.format] = ExportProfileManager::BBT;
81 row[time_format_cols.label] = _("Bars:Beats");
83 time_format_combo.pack_start (time_format_cols.label);
84 time_format_combo.set_active (0);
86 time_format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportTimespanSelector::change_time_format));
88 /* Range view */
90 range_list = Gtk::ListStore::create (range_cols);
91 range_view.set_model (range_list);
92 range_view.set_headers_visible (false);
95 ExportTimespanSelector::~ExportTimespanSelector ()
100 void
101 ExportTimespanSelector::add_range_to_selection (ARDOUR::Location const * loc)
103 TimespanPtr span = _session->get_export_handler()->add_timespan();
105 Glib::ustring id;
106 if (loc == state->session_range.get()) {
107 id = "session";
108 } else if (loc == state->selection_range.get()) {
109 id = "selection";
110 } else {
111 id = loc->id().to_s();
114 span->set_range (loc->start(), loc->end());
115 span->set_name (loc->name());
116 span->set_range_id (id);
117 state->timespans->push_back (span);
120 void
121 ExportTimespanSelector::set_time_format_from_state ()
123 Gtk::TreeModel::Children::iterator tree_it;
124 for (tree_it = time_format_list->children().begin(); tree_it != time_format_list->children().end(); ++tree_it) {
125 if (tree_it->get_value (time_format_cols.format) == state->time_format) {
126 time_format_combo.set_active (tree_it);
131 void
132 ExportTimespanSelector::sync_with_manager ()
134 state = manager->get_timespans().front();
135 fill_range_list ();
136 CriticalSelectionChanged();
139 void
140 ExportTimespanSelector::change_time_format ()
142 state->time_format = time_format_combo.get_active()->get_value (time_format_cols.format);
144 for (Gtk::ListStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
145 Location * location = it->get_value (range_cols.location);
146 it->set_value (range_cols.label, construct_label (location));
150 Glib::ustring
151 ExportTimespanSelector::construct_label (ARDOUR::Location const * location) const
153 Glib::ustring label;
154 Glib::ustring start;
155 Glib::ustring end;
157 nframes_t start_frame = location->start();
158 nframes_t end_frame = location->end();
160 switch (state->time_format) {
161 case AudioClock::BBT:
162 start = bbt_str (start_frame);
163 end = bbt_str (end_frame);
164 break;
166 case AudioClock::Timecode:
167 start = timecode_str (start_frame);
168 end = timecode_str (end_frame);
169 break;
171 case AudioClock::MinSec:
172 start = ms_str (start_frame);
173 end = ms_str (end_frame);
174 break;
176 case AudioClock::Frames:
177 start = to_string (start_frame, std::dec);
178 end = to_string (end_frame, std::dec);
179 break;
181 case AudioClock::Off:
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;
201 Glib::ustring
202 ExportTimespanSelector::bbt_str (nframes_t frames) const
204 if (!_session) {
205 return "Error!";
208 std::ostringstream oss;
209 BBT_Time time;
211 _session->bbt_time (frames, time);
213 oss << std::setfill('0') << std::right <<
214 std::setw(3) <<
215 time.bars << "|" <<
216 std::setw(2) <<
217 time.beats << "|" <<
218 std::setw(4) <<
219 time.ticks;
221 return oss.str();
224 Glib::ustring
225 ExportTimespanSelector::timecode_str (nframes_t frames) const
227 if (!_session) {
228 return "Error!";
231 std::ostringstream oss;
232 Timecode::Time time;
234 _session->timecode_time (frames, time);
236 oss << std::setfill('0') << std::right <<
237 std::setw(2) <<
238 time.hours << ":" <<
239 std::setw(2) <<
240 time.minutes << ":" <<
241 std::setw(2) <<
242 time.seconds << ":" <<
243 std::setw(2) <<
244 time.frames;
246 return oss.str();
249 Glib::ustring
250 ExportTimespanSelector::ms_str (nframes_t frames) const
252 if (!_session) {
253 return "Error!";
256 std::ostringstream oss;
257 nframes_t left;
258 int hrs;
259 int mins;
260 int secs;
261 int sec_promilles;
263 left = frames;
264 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
265 left -= (nframes_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
266 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
267 left -= (nframes_t) floor (mins * _session->frame_rate() * 60.0f);
268 secs = (int) floor (left / (float) _session->frame_rate());
269 left -= (nframes_t) floor (secs * _session->frame_rate());
270 sec_promilles = (int) (left * 1000 / (float) _session->frame_rate() + 0.5);
272 oss << std::setfill('0') << std::right <<
273 std::setw(2) <<
274 hrs << ":" <<
275 std::setw(2) <<
276 mins << ":" <<
277 std::setw(2) <<
278 secs << "." <<
279 std::setw(3) <<
280 sec_promilles;
282 return oss.str();
285 void
286 ExportTimespanSelector::update_range_name (Glib::ustring const & path, Glib::ustring const & new_text)
288 Gtk::TreeStore::iterator it = range_list->get_iter (path);
289 it->get_value (range_cols.location)->set_name (new_text);
291 CriticalSelectionChanged();
294 /*** ExportTimespanSelectorSingle ***/
296 ExportTimespanSelectorSingle::ExportTimespanSelectorSingle (ARDOUR::Session * session, ProfileManagerPtr manager, Glib::ustring range_id) :
297 ExportTimespanSelector (session, manager),
298 range_id (range_id)
300 range_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_NEVER);
301 range_view.append_column_editable ("", range_cols.name);
303 // Adjust selector height
304 int x_offset, y_offset, width, height;
305 Gtk::CellRenderer * renderer = *range_view.get_column(0)->get_cell_renderers().begin();
306 renderer->get_size (range_view, x_offset, y_offset, width, height);
307 range_scroller.set_size_request (-1, height);
309 if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (0))) {
310 renderer->signal_edited().connect (sigc::mem_fun (*this, &ExportTimespanSelectorSingle::update_range_name));
313 Gtk::CellRendererText * label_render = Gtk::manage (new Gtk::CellRendererText());
314 Gtk::TreeView::Column * label_col = Gtk::manage (new Gtk::TreeView::Column ("", *label_render));
315 label_col->add_attribute (label_render->property_markup(), range_cols.label);
316 range_view.append_column (*label_col);
320 void
321 ExportTimespanSelectorSingle::fill_range_list ()
323 if (!state) { return; }
325 Glib::ustring id;
326 if (!range_id.compare (X_("session"))) {
327 id = state->session_range->id().to_s();
328 } else if (!range_id.compare (X_("selection"))) {
329 id = state->selection_range->id().to_s();
330 } else {
331 id = range_id;
334 range_list->clear();
335 state->timespans->clear();
337 Gtk::TreeModel::iterator iter;
338 Gtk::TreeModel::Row row;
339 for (LocationList::const_iterator it = state->ranges->begin(); it != state->ranges->end(); ++it) {
341 if (!(*it)->id().to_s().compare (id)) {
342 iter = range_list->append();
343 row = *iter;
345 row[range_cols.location] = *it;
346 row[range_cols.selected] = true;
347 row[range_cols.name] = (*it)->name();
348 row[range_cols.label] = construct_label (*it);
350 add_range_to_selection (*it);
352 break;
356 set_time_format_from_state();
359 /*** ExportTimespanSelectorMultiple ***/
361 ExportTimespanSelectorMultiple::ExportTimespanSelectorMultiple (ARDOUR::Session * session, ProfileManagerPtr manager) :
362 ExportTimespanSelector (session, manager)
364 range_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
365 range_view.append_column_editable ("", range_cols.selected);
366 range_view.append_column_editable ("", range_cols.name);
368 if (Gtk::CellRendererToggle * renderer = dynamic_cast<Gtk::CellRendererToggle *> (range_view.get_column_cell_renderer (0))) {
369 renderer->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_selection)));
371 if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (1))) {
372 renderer->signal_edited().connect (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_range_name));
375 Gtk::CellRendererText * label_render = Gtk::manage (new Gtk::CellRendererText());
376 Gtk::TreeView::Column * label_col = Gtk::manage (new Gtk::TreeView::Column ("", *label_render));
377 label_col->add_attribute (label_render->property_markup(), range_cols.label);
378 range_view.append_column (*label_col);
382 void
383 ExportTimespanSelectorMultiple::fill_range_list ()
385 if (!state) { return; }
387 range_list->clear();
389 Gtk::TreeModel::iterator iter;
390 Gtk::TreeModel::Row row;
391 for (LocationList::const_iterator it = state->ranges->begin(); it != state->ranges->end(); ++it) {
393 iter = range_list->append();
394 row = *iter;
396 row[range_cols.location] = *it;
397 row[range_cols.selected] = false;
398 row[range_cols.name] = (*it)->name();
399 row[range_cols.label] = construct_label (*it);
402 set_selection_from_state ();
405 void
406 ExportTimespanSelectorMultiple::set_selection_from_state ()
408 Gtk::TreeModel::Children::iterator tree_it;
410 for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
411 ustring id = (*it)->range_id();
412 for (tree_it = range_list->children().begin(); tree_it != range_list->children().end(); ++tree_it) {
413 Location * loc = tree_it->get_value (range_cols.location);
415 if ((!id.compare ("session") && loc == state->session_range.get()) ||
416 (!id.compare ("selection") && loc == state->selection_range.get()) ||
417 (!id.compare (loc->id().to_s()))) {
418 tree_it->set_value (range_cols.selected, true);
423 set_time_format_from_state();
426 void
427 ExportTimespanSelectorMultiple::update_selection ()
429 update_timespans ();
430 CriticalSelectionChanged ();
433 void
434 ExportTimespanSelectorMultiple::update_timespans ()
436 state->timespans->clear();
438 for (Gtk::TreeStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
439 if (it->get_value (range_cols.selected)) {
440 add_range_to_selection (it->get_value (range_cols.location));