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