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