Remove most using declarations from header files.
[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
43 ExportTimespanSelector::ExportTimespanSelector (ARDOUR::Session * session, ProfileManagerPtr manager) :
44   session (session),
45   manager (manager),
46   time_format_label (_("Show Times as:"), Gtk::ALIGN_LEFT)
47 {
48
49         option_hbox.pack_start (time_format_label, false, false, 0);
50         option_hbox.pack_start (time_format_combo, false, false, 6);
51         
52         range_scroller.add (range_view);
53         
54         pack_start (option_hbox, false, false, 0);
55         pack_start (range_scroller, true, true, 6);
56         
57         /*** Combo boxes ***/
58         
59         Gtk::TreeModel::iterator iter;
60         Gtk::TreeModel::Row row;
61         
62         /* Time format combo */
63         
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");
67         
68         iter = time_format_list->append();
69         row = *iter;
70         row[time_format_cols.format] = ExportProfileManager::SMPTE;
71         row[time_format_cols.label] = X_("Timecode");
72         
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");
77         
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");
82         
83         time_format_combo.pack_start (time_format_cols.label);
84         time_format_combo.set_active (0);
85         
86         time_format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportTimespanSelector::change_time_format));
87         
88         /* Range view */
89         
90         range_list = Gtk::ListStore::create (range_cols);
91         range_view.set_model (range_list);
92         range_view.set_headers_visible (false);
93 }
94
95 ExportTimespanSelector::~ExportTimespanSelector ()
96 {
97
98 }
99
100 void
101 ExportTimespanSelector::add_range_to_selection (ARDOUR::Location const * loc)
102 {
103         TimespanPtr span = session->get_export_handler()->add_timespan();
104         
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();
112         }
113         
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);
118 }
119
120 void
121 ExportTimespanSelector::set_time_format_from_state ()
122 {
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);
127                 }
128         }
129 }
130
131 void
132 ExportTimespanSelector::sync_with_manager ()
133 {
134         state = manager->get_timespans().front();
135         fill_range_list ();
136         CriticalSelectionChanged();
137 }
138
139 void
140 ExportTimespanSelector::change_time_format ()
141 {
142         state->time_format = time_format_combo.get_active()->get_value (time_format_cols.format);
143
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));
147         }
148 }
149
150 Glib::ustring
151 ExportTimespanSelector::construct_label (ARDOUR::Location const * location) const
152 {
153         Glib::ustring label;
154         Glib::ustring start;
155         Glib::ustring end;
156         
157         nframes_t start_frame = location->start();
158         nframes_t end_frame = location->end();
159         
160         switch (state->time_format) {
161           case AudioClock::BBT:
162                 start = bbt_str (start_frame);
163                 end = bbt_str (end_frame);
164                 break;
165         
166           case AudioClock::SMPTE:
167                 start = smpte_str (start_frame);
168                 end = smpte_str (end_frame);
169                 break;
170         
171           case AudioClock::MinSec:
172                 start = ms_str (start_frame);
173                 end = ms_str (end_frame);
174                 break;
175         
176           case AudioClock::Frames:
177                 start = to_string (start_frame, std::dec);
178                 end = to_string (end_frame, std::dec);
179                 break;
180         
181           case AudioClock::Off:
182                 break;
183         }
184         
185         // label += _("from ");
186         
187         // label += "<span color=\"#7fff7f\">";
188         label += start;
189 //      label += "</span>";
190         
191         label += _(" to ");
192         
193 //      label += "<span color=\"#7fff7f\">";
194         label += end;
195 //      label += "</span>";
196         
197         return label;
198 }
199
200
201 Glib::ustring
202 ExportTimespanSelector::bbt_str (nframes_t frames) const
203 {
204         if (!session) {
205                 return "Error!";
206         }
207         
208         std::ostringstream oss;
209         BBT_Time time;
210         
211         session->bbt_time (frames, time);
212         
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;
220         
221         return oss.str();
222 }
223
224 Glib::ustring
225 ExportTimespanSelector::smpte_str (nframes_t frames) const
226 {
227         if (!session) {
228                 return "Error!";
229         }
230         
231         std::ostringstream oss;
232         SMPTE::Time time;
233         
234         session->smpte_time (frames, time);
235         
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;
245         
246         return oss.str();
247 }
248
249 Glib::ustring
250 ExportTimespanSelector::ms_str (nframes_t frames) const
251 {
252         if (!session) {
253                 return "Error!";
254         }
255         
256         std::ostringstream oss;
257         nframes_t left;
258         int hrs;
259         int mins;
260         int secs;
261         int sec_promilles;
262         
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);
271         
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;
281         
282         return oss.str();
283 }
284
285 void
286 ExportTimespanSelector::update_range_name (Glib::ustring const & path, Glib::ustring const & new_text)
287 {
288         Gtk::TreeStore::iterator it = range_list->get_iter (path);
289         it->get_value (range_cols.location)->set_name (new_text);
290         
291         CriticalSelectionChanged();
292 }
293
294 /*** ExportTimespanSelectorSingle ***/
295
296 ExportTimespanSelectorSingle::ExportTimespanSelectorSingle (ARDOUR::Session * session, ProfileManagerPtr manager, Glib::ustring range_id) :
297   ExportTimespanSelector (session, manager),
298   range_id (range_id)
299 {
300         range_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_NEVER);
301         range_view.append_column_editable ("", range_cols.name);
302         
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);
308         
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));
311         }
312         
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);
317
318 }
319
320 void
321 ExportTimespanSelectorSingle::fill_range_list ()
322 {
323         if (!state) { return; }
324         
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;
332         }
333
334         range_list->clear();
335         state->timespans->clear();
336
337         Gtk::TreeModel::iterator iter;
338         Gtk::TreeModel::Row row;
339         for (LocationList::const_iterator it = state->ranges->begin(); it != state->ranges->end(); ++it) {
340         
341                 if (!(*it)->id().to_s().compare (id)) {
342                         iter = range_list->append();
343                         row = *iter;
344                         
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);
349                         
350                         add_range_to_selection (*it);
351                         
352                         break;
353                 }
354         }
355         
356         set_time_format_from_state();
357 }
358
359 /*** ExportTimespanSelectorMultiple ***/
360
361 ExportTimespanSelectorMultiple::ExportTimespanSelectorMultiple (ARDOUR::Session * session, ProfileManagerPtr manager) :
362   ExportTimespanSelector (session, manager)
363 {
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);
367         
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)));
370         }
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));
373         }
374         
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);
379
380 }
381
382 void
383 ExportTimespanSelectorMultiple::fill_range_list ()
384 {
385         if (!state) { return; }
386
387         range_list->clear();
388
389         Gtk::TreeModel::iterator iter;
390         Gtk::TreeModel::Row row;
391         for (LocationList::const_iterator it = state->ranges->begin(); it != state->ranges->end(); ++it) {
392         
393                 iter = range_list->append();
394                 row = *iter;
395                 
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);
400         }
401         
402         set_selection_from_state ();
403 }
404
405 void
406 ExportTimespanSelectorMultiple::set_selection_from_state ()
407 {
408         Gtk::TreeModel::Children::iterator tree_it;
409         
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);
414                         
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);
419                         }
420                 }
421         }
422         
423         set_time_format_from_state();
424 }
425
426 void
427 ExportTimespanSelectorMultiple::update_selection ()
428 {
429         update_timespans ();
430         CriticalSelectionChanged ();
431 }
432
433 void
434 ExportTimespanSelectorMultiple::update_timespans ()
435 {
436         state->timespans->clear();
437         
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));
441                 }
442         }
443 }
444