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_audio_region_removed (boost::weak_ptr<AudioRegion> wregion)
56 ENSURE_GUI_THREAD (mem_fun (*this, &Editor::redisplay_regions));
61 Editor::handle_new_audio_region (boost::weak_ptr<AudioRegion> wregion)
63 ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::handle_new_audio_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<AudioRegion> region (wregion.lock());
73 add_audio_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_audio_region_to_region_display (boost::shared_ptr<AudioRegion> 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;
107 parent = *(region_list_model->append());
109 parent[region_list_columns.name] = _("Hidden");
113 if ((*iter)[region_list_columns.name] != _("Hidden")) {
115 parent = *(region_list_model->insert(iter));
116 parent[region_list_columns.name] = _("Hidden");
124 row = *(region_list_model->append (parent.children()));
126 } else if (region->whole_file()) {
128 row = *(region_list_model->append());
129 if (missing_source) {
130 c.set_rgb(65535,0,0); // FIXME: error color from style
132 set_color(c, rgba_from_style ("RegionListWholeFile", 0xff, 0, 0, 0, "fg", Gtk::STATE_NORMAL, false ));
134 row[region_list_columns.color_] = c;
136 if (region->source()->name()[0] == '/') { // external file
138 /* XXX there was old code here to try to show an abbreviated version
139 of the path name for whole file regions.
142 str = region->name();
146 str = region->name();
150 if (region->n_channels() > 1) {
151 std::stringstream foo;
152 foo << region->n_channels ();
158 if (missing_source) {
159 str += _(" (MISSING)");
162 row[region_list_columns.name] = str;
163 row[region_list_columns.region] = region;
169 /* find parent node, add as new child */
171 TreeModel::iterator i;
172 TreeModel::Children rows = region_list_model->children();
173 bool found_parent = false;
175 for (i = rows.begin(); i != rows.end(); ++i) {
177 boost::shared_ptr<Region> rr = (*i)[region_list_columns.region];
178 boost::shared_ptr<AudioRegion> r = boost::dynamic_pointer_cast<AudioRegion>(rr);
180 if (r && r->whole_file()) {
181 if (region->source_equivalent (r)) {
182 row = *(region_list_model->append ((*i).children()));
190 row = *(region_list_model->append());
196 row[region_list_columns.region] = region;
198 if (region->n_channels() > 1) {
199 row[region_list_columns.name] = string_compose("%1 [%2]", region->name(), region->n_channels());
201 row[region_list_columns.name] = region->name();
206 Editor::region_list_selection_changed()
210 if (region_list_display.get_selection()->count_selected_rows() > 0) {
217 TreeView::Selection::ListHandle_Path rows = region_list_display.get_selection()->get_selected_rows ();
218 TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
221 if ((iter = region_list_model->get_iter (*i))) {
222 boost::shared_ptr<Region> r = (*iter)[region_list_columns.region];
224 /* they could have clicked on a row that is just a placeholder, like "Hidden" */
228 /* just set the first selected region (in fact, the selection model might be SINGLE, which
229 means there can only be one.
232 set_selected_regionview_from_region_list (r, Selection::Set);
239 Editor::insert_into_tmp_audio_regionlist(boost::shared_ptr<AudioRegion> region)
241 /* keep all whole files at the beginning */
243 if (region->whole_file()) {
244 tmp_audio_region_list.push_front (region);
246 tmp_audio_region_list.push_back (region);
251 Editor::redisplay_regions ()
255 region_list_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
256 region_list_model->clear ();
258 /* now add everything we have, via a temporary list used to help with
262 tmp_audio_region_list.clear();
263 session->foreach_audio_region (this, &Editor::insert_into_tmp_audio_regionlist);
265 for (list<boost::shared_ptr<AudioRegion> >::iterator r = tmp_audio_region_list.begin(); r != tmp_audio_region_list.end(); ++r) {
266 add_audio_region_to_region_display (*r);
268 tmp_audio_region_list.clear();
270 region_list_display.set_model (region_list_model);
275 Editor::build_region_list_menu ()
277 region_list_menu = dynamic_cast<Menu*>(ActionManager::get_widget ("/RegionListMenu"));
279 /* now grab specific menu items that we need */
281 Glib::RefPtr<Action> act;
283 act = ActionManager::get_action (X_("RegionList"), X_("rlShowAll"));
285 toggle_full_region_list_action = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
288 act = ActionManager::get_action (X_("RegionList"), X_("rlShowAuto"));
290 toggle_show_auto_regions_action = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
295 Editor::toggle_show_auto_regions ()
297 show_automatic_regions_in_region_list = toggle_show_auto_regions_action->get_active();
298 redisplay_regions ();
302 Editor::toggle_full_region_list ()
304 if (toggle_full_region_list_action->get_active()) {
305 region_list_display.expand_all ();
307 region_list_display.collapse_all ();
312 Editor::show_region_list_display_context_menu (int button, int time)
314 if (region_list_menu == 0) {
315 build_region_list_menu ();
318 if (region_list_display.get_selection()->count_selected_rows() > 0) {
319 ActionManager::set_sensitive (ActionManager::region_list_selection_sensitive_actions, true);
321 ActionManager::set_sensitive (ActionManager::region_list_selection_sensitive_actions, false);
324 region_list_menu->popup (button, time);
328 Editor::region_list_display_key_press (GdkEventKey* ev)
334 Editor::region_list_display_key_release (GdkEventKey* ev)
336 switch (ev->keyval) {
338 remove_region_from_region_list ();
349 Editor::region_list_display_button_press (GdkEventButton *ev)
351 boost::shared_ptr<Region> region;
353 TreeModel::Path path;
354 TreeViewColumn* column;
358 cerr << "Button press release, button = " << ev->button << endl;
360 if (region_list_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
361 if ((iter = region_list_model->get_iter (path))) {
362 region = (*iter)[region_list_columns.region];
366 if (Keyboard::is_context_menu_event (ev)) {
367 show_region_list_display_context_menu (ev->button, ev->time);
368 cerr << "\tcontext menu event, event handled\n";
373 cerr << "\tno region, event not handled\n";
377 switch (ev->button) {
382 // audition on middle click (stop audition too)
383 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
384 consider_auditioning (region);
386 cerr << "\taudition, event handled\n";
394 cerr << "\tnot handled\n";
399 Editor::region_list_display_button_release (GdkEventButton *ev)
402 TreeModel::Path path;
403 TreeViewColumn* column;
406 boost::shared_ptr<Region> region;
408 if (region_list_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
409 if ((iter = region_list_model->get_iter (path))) {
410 region = (*iter)[region_list_columns.region];
414 if (region && Keyboard::is_delete_event (ev)) {
415 session->remove_region_from_region_list (region);
423 Editor::consider_auditioning (boost::shared_ptr<Region> region)
425 boost::shared_ptr<AudioRegion> r = boost::dynamic_pointer_cast<AudioRegion> (region);
428 session->cancel_audition ();
432 if (session->is_auditioning()) {
433 session->cancel_audition ();
434 if (r == last_audition_region) {
439 session->audition_region (r);
440 last_audition_region = r;
444 Editor::region_list_sorter (TreeModel::iterator a, TreeModel::iterator b)
448 boost::shared_ptr<Region> r1 = (*a)[region_list_columns.region];
449 boost::shared_ptr<Region> r2 = (*b)[region_list_columns.region];
451 /* handle rows without regions, like "Hidden" */
461 boost::shared_ptr<AudioRegion> region1 = boost::dynamic_pointer_cast<AudioRegion> (r1);
462 boost::shared_ptr<AudioRegion> region2 = boost::dynamic_pointer_cast<AudioRegion> (r2);
464 if (region1 == 0 || region2 == 0) {
467 switch (region_list_sort_type) {
469 s1 = (*a)[region_list_columns.name];
470 s2 = (*b)[region_list_columns.name];
471 return (s1.compare (s2));
477 switch (region_list_sort_type) {
479 cmp = strcasecmp (region1->name().c_str(), region2->name().c_str());
483 cmp = region1->length() - region2->length();
487 cmp = region1->position() - region2->position();
491 cmp = region1->source()->timestamp() - region2->source()->timestamp();
495 cmp = region1->start() - region2->start();
499 cmp = (region1->start() + region1->length()) - (region2->start() + region2->length());
502 case BySourceFileName:
503 cmp = strcasecmp (region1->source()->name().c_str(), region2->source()->name().c_str());
506 case BySourceFileLength:
507 cmp = region1->source()->length() - region2->source()->length();
510 case BySourceFileCreationDate:
511 cmp = region1->source()->timestamp() - region2->source()->timestamp();
515 if (region1->source()->name() == region2->source()->name()) {
516 cmp = strcasecmp (region1->name().c_str(), region2->name().c_str());
518 cmp = strcasecmp (region1->source()->name().c_str(), region2->source()->name().c_str());
525 } else if (cmp > 0) {
533 Editor::reset_region_list_sort_type (RegionListSortType type)
535 if (type != region_list_sort_type) {
536 region_list_sort_type = type;
537 region_list_model->set_sort_func (0, (mem_fun (*this, &Editor::region_list_sorter)));
542 Editor::reset_region_list_sort_direction (bool up)
544 region_list_model->set_sort_column (0, up ? SORT_ASCENDING : SORT_DESCENDING);
548 Editor::region_list_selection_mapover (slot<void,boost::shared_ptr<Region> > sl)
550 Glib::RefPtr<TreeSelection> selection = region_list_display.get_selection();
551 TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
552 TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
554 if (selection->count_selected_rows() == 0 || session == 0) {
558 for (; i != rows.end(); ++i) {
561 if ((iter = region_list_model->get_iter (*i))) {
563 /* some rows don't have a region associated with them, but can still be
564 selected (XXX maybe prevent them from being selected)
567 boost::shared_ptr<Region> r = (*iter)[region_list_columns.region];
577 Editor::hide_a_region (boost::shared_ptr<Region> r)
579 r->set_hidden (true);
583 Editor::remove_a_region (boost::shared_ptr<Region> r)
585 session->remove_region_from_region_list (r);
589 Editor::audition_region_from_region_list ()
591 region_list_selection_mapover (mem_fun (*this, &Editor::consider_auditioning));
595 Editor::hide_region_from_region_list ()
597 region_list_selection_mapover (mem_fun (*this, &Editor::hide_a_region));
601 Editor::remove_region_from_region_list ()
603 region_list_selection_mapover (mem_fun (*this, &Editor::remove_a_region));
607 Editor::region_list_display_drag_data_received (const RefPtr<Gdk::DragContext>& context,
609 const SelectionData& data,
610 guint info, guint time)
612 vector<ustring> paths;
614 if (data.get_target() == "GTK_TREE_MODEL_ROW") {
615 cerr << "Delete drag data drop to treeview\n";
616 region_list_display.on_drag_data_received (context, x, y, data, info, time);
620 if (convert_drop_to_paths (paths, context, x, y, data, info, time) == 0) {
622 do_embed (paths, Editing::ImportDistinctFiles, ImportAsRegion, pos);
623 context->drag_finish (true, false, time);
628 Editor::region_list_selection_filter (const RefPtr<TreeModel>& model, const TreeModel::Path& path, bool yn)
630 /* not possible to select rows that do not represent regions, like "Hidden" */
632 TreeModel::iterator iter = model->get_iter (path);
635 boost::shared_ptr<Region> r =(*iter)[region_list_columns.region];
645 Editor::region_name_edit (const Glib::ustring& path, const Glib::ustring& new_text)
647 boost::shared_ptr<Region> region;
650 if ((iter = region_list_model->get_iter (path))) {
651 region = (*iter)[region_list_columns.region];
652 (*iter)[region_list_columns.name] = new_text;
655 /* now mapover everything */
658 vector<RegionView*> equivalents;
659 get_regions_corresponding_to (region, equivalents);
661 for (vector<RegionView*>::iterator i = equivalents.begin(); i != equivalents.end(); ++i) {
662 if (new_text != (*i)->region()->name()) {
663 (*i)->region()->set_name (new_text);