2 Copyright (C) 2000-2005 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <pbd/basename.h>
28 #include <ardour/audioregion.h>
29 #include <ardour/audiofilesource.h>
30 #include <ardour/silentfilesource.h>
31 #include <ardour/session_region.h>
33 #include <gtkmm2ext/stop_signal.h>
38 #include "ardour_ui.h"
39 #include "gui_thread.h"
41 #include "region_view.h"
47 using namespace ARDOUR;
51 using namespace Editing;
54 Editor::handle_region_removed (boost::weak_ptr<Region> wregion)
56 ENSURE_GUI_THREAD (mem_fun (*this, &Editor::redisplay_regions));
61 Editor::handle_new_region (boost::weak_ptr<Region> wregion)
63 ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::handle_new_region), wregion));
65 /* don't copy region - the one we are being notified
66 about belongs to the session, and so it will
70 boost::shared_ptr<Region> region (wregion.lock());
73 add_region_to_region_display (region);
78 Editor::region_hidden (boost::shared_ptr<Region> r)
80 ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::region_hidden), r));
86 Editor::add_region_to_region_display (boost::shared_ptr<Region> region)
93 missing_source = boost::dynamic_pointer_cast<SilentFileSource>(region->source());
95 if (!show_automatic_regions_in_region_list && region->automatic()) {
99 if (region->hidden()) {
101 TreeModel::iterator iter = region_list_model->get_iter ("0");
102 TreeModel::Row parent;
103 TreeModel::Row child;
108 parent = *(region_list_model->append());
110 parent[region_list_columns.name] = _("Hidden");
111 boost::shared_ptr<Region> proxy = parent[region_list_columns.region];
116 if ((*iter)[region_list_columns.name] != _("Hidden")) {
118 parent = *(region_list_model->insert(iter));
119 parent[region_list_columns.name] = _("Hidden");
120 boost::shared_ptr<Region> proxy = parent[region_list_columns.region];
130 row = *(region_list_model->append (parent.children()));
132 } else if (region->whole_file()) {
134 row = *(region_list_model->append());
135 if (missing_source) {
136 c.set_rgb(65535,0,0); // FIXME: error color from style
138 set_color(c, rgba_from_style ("RegionListWholeFile", 0xff, 0, 0, 0, "fg", Gtk::STATE_NORMAL, false ));
140 row[region_list_columns.color_] = c;
142 if (region->source()->name()[0] == '/') { // external file
144 if (region->whole_file()) {
146 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(region->source());
151 str = region_name_from_path (afs->path(), region->n_channels() > 1);
153 str += region->source()->name();
157 str = region->name();
162 str = region->name();
166 if (region->n_channels() > 1) {
167 std::stringstream foo;
168 foo << region->n_channels ();
174 if (missing_source) {
175 str += _(" (MISSING)");
178 row[region_list_columns.name] = str;
179 row[region_list_columns.region] = region;
185 /* find parent node, add as new child */
187 TreeModel::iterator i;
188 TreeModel::Children rows = region_list_model->children();
189 bool found_parent = false;
191 for (i = rows.begin(); i != rows.end(); ++i) {
193 boost::shared_ptr<Region> rr = (*i)[region_list_columns.region];
194 boost::shared_ptr<AudioRegion> r = boost::dynamic_pointer_cast<AudioRegion>(rr);
196 if (r && r->whole_file()) {
197 if (region->source_equivalent (r)) {
198 row = *(region_list_model->append ((*i).children()));
206 row = *(region_list_model->append());
212 row[region_list_columns.region] = region;
214 if (region->n_channels() > 1) {
215 row[region_list_columns.name] = string_compose("%1 [%2]", region->name(), region->n_channels());
217 row[region_list_columns.name] = region->name();
223 Editor::region_list_region_changed (Change what_changed, boost::weak_ptr<Region> region)
225 ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::region_list_region_changed), what_changed, region));
227 boost::shared_ptr<Region> r = region.lock ();
233 if (what_changed & ARDOUR::NameChanged) {
234 /* find the region in our model and change its name */
235 TreeModel::Children rows = region_list_model->children ();
236 TreeModel::iterator i = rows.begin ();
237 while (i != rows.end ()) {
238 TreeModel::Children children = (*i)->children ();
239 TreeModel::iterator j = children.begin ();
240 while (j != children.end()) {
241 boost::shared_ptr<Region> c = (*j)[region_list_columns.region];
248 if (j != children.end()) {
249 (*j)[region_list_columns.name] = r->name ();
260 Editor::region_list_selection_changed()
264 if (region_list_display.get_selection()->count_selected_rows() > 0) {
271 TreeView::Selection::ListHandle_Path rows = region_list_display.get_selection()->get_selected_rows ();
272 TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
275 if ((iter = region_list_model->get_iter (*i))) {
276 boost::shared_ptr<Region> r = (*iter)[region_list_columns.region];
278 /* they could have clicked on a row that is just a placeholder, like "Hidden" */
282 /* just set the first selected region (in fact, the selection model might be SINGLE, which
283 means there can only be one.
286 set_selected_regionview_from_region_list (r, Selection::Set);
293 Editor::insert_into_tmp_regionlist(boost::shared_ptr<Region> region)
295 /* keep all whole files at the beginning */
297 if (region->whole_file()) {
298 tmp_region_list.push_front (region);
300 tmp_region_list.push_back (region);
305 Editor::redisplay_regions ()
309 region_list_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
310 region_list_model->clear ();
312 /* now add everything we have, via a temporary list used to help with
316 tmp_region_list.clear();
317 session->foreach_region (this, &Editor::insert_into_tmp_regionlist);
319 for (list<boost::shared_ptr<Region> >::iterator r = tmp_region_list.begin(); r != tmp_region_list.end(); ++r) {
320 add_region_to_region_display (*r);
322 tmp_region_list.clear();
324 region_list_display.set_model (region_list_model);
329 Editor::build_region_list_menu ()
331 region_list_menu = dynamic_cast<Menu*>(ActionManager::get_widget ("/RegionListMenu"));
333 /* now grab specific menu items that we need */
335 Glib::RefPtr<Action> act;
337 act = ActionManager::get_action (X_("RegionList"), X_("rlShowAll"));
339 toggle_full_region_list_action = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
342 act = ActionManager::get_action (X_("RegionList"), X_("rlShowAuto"));
344 toggle_show_auto_regions_action = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
349 Editor::toggle_show_auto_regions ()
351 show_automatic_regions_in_region_list = toggle_show_auto_regions_action->get_active();
352 redisplay_regions ();
356 Editor::toggle_full_region_list ()
358 if (toggle_full_region_list_action->get_active()) {
359 region_list_display.expand_all ();
361 region_list_display.collapse_all ();
366 Editor::show_region_list_display_context_menu (int button, int time)
368 if (region_list_menu == 0) {
369 build_region_list_menu ();
372 if (region_list_display.get_selection()->count_selected_rows() > 0) {
373 ActionManager::set_sensitive (ActionManager::region_list_selection_sensitive_actions, true);
375 ActionManager::set_sensitive (ActionManager::region_list_selection_sensitive_actions, false);
378 region_list_menu->popup (button, time);
382 Editor::region_list_display_key_press (GdkEventKey* ev)
388 Editor::region_list_display_key_release (GdkEventKey* ev)
390 switch (ev->keyval) {
392 remove_region_from_region_list ();
403 Editor::region_list_display_button_press (GdkEventButton *ev)
405 boost::shared_ptr<Region> region;
407 TreeModel::Path path;
408 TreeViewColumn* column;
412 cerr << "Button press release, button = " << ev->button << endl;
414 if (region_list_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
415 if ((iter = region_list_model->get_iter (path))) {
416 region = (*iter)[region_list_columns.region];
420 if (Keyboard::is_context_menu_event (ev)) {
421 show_region_list_display_context_menu (ev->button, ev->time);
422 cerr << "\tcontext menu event, event handled\n";
427 cerr << "\tno region, event not handled\n";
431 switch (ev->button) {
436 // audition on middle click (stop audition too)
437 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
438 consider_auditioning (region);
440 cerr << "\taudition, event handled\n";
448 cerr << "\tnot handled\n";
453 Editor::region_list_display_button_release (GdkEventButton *ev)
456 TreeModel::Path path;
457 TreeViewColumn* column;
460 boost::shared_ptr<Region> region;
462 if (region_list_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
463 if ((iter = region_list_model->get_iter (path))) {
464 region = (*iter)[region_list_columns.region];
468 if (region && Keyboard::is_delete_event (ev)) {
469 session->remove_region_from_region_list (region);
477 Editor::consider_auditioning (boost::shared_ptr<Region> region)
479 boost::shared_ptr<AudioRegion> r = boost::dynamic_pointer_cast<AudioRegion> (region);
482 session->cancel_audition ();
486 if (session->is_auditioning()) {
487 session->cancel_audition ();
488 if (r == last_audition_region) {
493 session->audition_region (r);
494 last_audition_region = r;
498 Editor::region_list_sorter (TreeModel::iterator a, TreeModel::iterator b)
502 boost::shared_ptr<Region> r1 = (*a)[region_list_columns.region];
503 boost::shared_ptr<Region> r2 = (*b)[region_list_columns.region];
505 /* handle rows without regions, like "Hidden" */
515 boost::shared_ptr<AudioRegion> region1 = boost::dynamic_pointer_cast<AudioRegion> (r1);
516 boost::shared_ptr<AudioRegion> region2 = boost::dynamic_pointer_cast<AudioRegion> (r2);
518 if (region1 == 0 || region2 == 0) {
521 switch (region_list_sort_type) {
523 s1 = (*a)[region_list_columns.name];
524 s2 = (*b)[region_list_columns.name];
525 return (s1.compare (s2));
531 switch (region_list_sort_type) {
533 cmp = strcasecmp (region1->name().c_str(), region2->name().c_str());
537 cmp = region1->length() - region2->length();
541 cmp = region1->position() - region2->position();
545 cmp = region1->source()->timestamp() - region2->source()->timestamp();
549 cmp = region1->start() - region2->start();
553 cmp = (region1->start() + region1->length()) - (region2->start() + region2->length());
556 case BySourceFileName:
557 cmp = strcasecmp (region1->source()->name().c_str(), region2->source()->name().c_str());
560 case BySourceFileLength:
561 cmp = region1->source()->length() - region2->source()->length();
564 case BySourceFileCreationDate:
565 cmp = region1->source()->timestamp() - region2->source()->timestamp();
569 if (region1->source()->name() == region2->source()->name()) {
570 cmp = strcasecmp (region1->name().c_str(), region2->name().c_str());
572 cmp = strcasecmp (region1->source()->name().c_str(), region2->source()->name().c_str());
579 } else if (cmp > 0) {
587 Editor::reset_region_list_sort_type (RegionListSortType type)
589 if (type != region_list_sort_type) {
590 region_list_sort_type = type;
591 region_list_model->set_sort_func (0, (mem_fun (*this, &Editor::region_list_sorter)));
596 Editor::reset_region_list_sort_direction (bool up)
598 region_list_model->set_sort_column (0, up ? SORT_ASCENDING : SORT_DESCENDING);
602 Editor::region_list_selection_mapover (slot<void,boost::shared_ptr<Region> > sl)
604 Glib::RefPtr<TreeSelection> selection = region_list_display.get_selection();
605 TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
606 TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
608 if (selection->count_selected_rows() == 0 || session == 0) {
612 for (; i != rows.end(); ++i) {
615 if ((iter = region_list_model->get_iter (*i))) {
617 /* some rows don't have a region associated with them, but can still be
618 selected (XXX maybe prevent them from being selected)
621 boost::shared_ptr<Region> r = (*iter)[region_list_columns.region];
631 Editor::hide_a_region (boost::shared_ptr<Region> r)
633 r->set_hidden (true);
637 Editor::remove_a_region (boost::shared_ptr<Region> r)
639 session->remove_region_from_region_list (r);
643 Editor::audition_region_from_region_list ()
645 region_list_selection_mapover (mem_fun (*this, &Editor::consider_auditioning));
649 Editor::hide_region_from_region_list ()
651 region_list_selection_mapover (mem_fun (*this, &Editor::hide_a_region));
655 Editor::remove_region_from_region_list ()
657 region_list_selection_mapover (mem_fun (*this, &Editor::remove_a_region));
661 Editor::region_list_display_drag_data_received (const RefPtr<Gdk::DragContext>& context,
663 const SelectionData& data,
664 guint info, guint time)
666 vector<ustring> paths;
668 if (data.get_target() == "GTK_TREE_MODEL_ROW") {
669 cerr << "Delete drag data drop to treeview\n";
670 region_list_display.on_drag_data_received (context, x, y, data, info, time);
674 if (convert_drop_to_paths (paths, context, x, y, data, info, time) == 0) {
676 do_embed (paths, Editing::ImportDistinctFiles, ImportAsRegion, pos);
677 context->drag_finish (true, false, time);
682 Editor::region_list_selection_filter (const RefPtr<TreeModel>& model, const TreeModel::Path& path, bool yn)
684 /* not possible to select rows that do not represent regions, like "Hidden" */
686 TreeModel::iterator iter = model->get_iter (path);
689 boost::shared_ptr<Region> r =(*iter)[region_list_columns.region];
699 Editor::region_name_edit (const Glib::ustring& path, const Glib::ustring& new_text)
701 boost::shared_ptr<Region> region;
704 if ((iter = region_list_model->get_iter (path))) {
705 region = (*iter)[region_list_columns.region];
706 (*iter)[region_list_columns.name] = new_text;
709 /* now mapover everything */
712 vector<RegionView*> equivalents;
713 get_regions_corresponding_to (region, equivalents);
715 for (vector<RegionView*>::iterator i = equivalents.begin(); i != equivalents.end(); ++i) {
716 if (new_text != (*i)->region()->name()) {
717 (*i)->region()->set_name (new_text);