Unconditionally save instant.xml on session-close
[ardour.git] / gtk2_ardour / region_layering_order_editor.cc
1 /*
2  * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2010-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2011 David Robillard <d@drobilla.net>
5  * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <gtkmm/table.h>
23 #include <gtkmm/stock.h>
24 #include <gtkmm/alignment.h>
25
26 #include "pbd/stateful_diff_command.h"
27
28 #include "ardour/region.h"
29
30 #include "gui_thread.h"
31 #include "keyboard.h"
32 #include "public_editor.h"
33 #include "region_layering_order_editor.h"
34 #include "region_view.h"
35 #include "utils.h"
36 #include "pbd/i18n.h"
37
38 using namespace std;
39 using namespace Gtk;
40 using namespace ARDOUR;
41 using namespace ARDOUR_UI_UTILS;
42
43 RegionLayeringOrderEditor::RegionLayeringOrderEditor (PublicEditor& pe)
44         : ArdourWindow (_("RegionLayeringOrderEditor"))
45         , position (0)
46         , in_row_change (false)
47         , regions_at_position (0)
48         , layering_order_model (Gtk::ListStore::create (layering_order_columns))
49         , clock ("layer dialog", true, "", false, false, false)
50         , editor (pe)
51         , _time_axis_view (0)
52 {
53         set_name ("RegionLayeringOrderEditorWindow");
54
55         layering_order_display.set_model (layering_order_model);
56
57         layering_order_display.append_column (_("Region Name"), layering_order_columns.name);
58         layering_order_display.set_headers_visible (true);
59         layering_order_display.set_reorderable (false);
60         layering_order_display.set_rules_hint (true);
61
62         scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
63         scroller.add (layering_order_display);
64
65         clock.set_mode (AudioClock::BBT);
66
67         Gtk::Table* scroller_table = manage (new Gtk::Table);
68         scroller_table->set_size_request (300, 250);
69         scroller_table->attach (scroller, 0, 1, 0, 1);
70         scroller_table->set_col_spacings (5);
71         scroller_table->set_row_spacings (5);
72
73         track_label.set_name ("RegionLayeringOrderEditorLabel");
74         track_label.set_text (_("Track:"));
75         track_label.set_alignment (0, 0.5);
76         clock_label.set_name ("RegionLayeringOrderEditorLabel");
77         clock_label.set_text (_("Position:"));
78         clock_label.set_alignment (0, 0.5);
79         track_name_label.set_name ("RegionLayeringOrderEditorNameLabel");
80         track_name_label.set_alignment (0, 0.5);
81         clock.set_mode (AudioClock::BBT);
82
83         Gtk::Table* info_table = manage (new Gtk::Table (2, 2));
84         info_table->set_col_spacings (5);
85         info_table->set_row_spacings (5);
86         info_table->attach (track_label, 0, 1, 0, 1, FILL, FILL);
87         info_table->attach (track_name_label, 1, 2, 0, 1, FILL, FILL);
88         info_table->attach (clock_label, 0, 1, 1, 2, FILL, FILL);
89         info_table->attach (clock, 1, 2, 1, 2, Gtk::AttachOptions(0), FILL);
90
91         Gtk::VBox* vbox = Gtk::manage (new Gtk::VBox ());
92         vbox->set_spacing (12);
93         vbox->pack_start (*info_table, false, false);
94         vbox->pack_start (*scroller_table, true, true);
95         add (*vbox);
96
97         info_table->set_name ("RegionLayeringOrderTable");
98         scroller_table->set_name ("RegionLayeringOrderTable");
99
100         layering_order_display.set_name ("RegionLayeringOrderDisplay");
101         layering_order_display.get_selection()->set_mode (SELECTION_SINGLE);
102         layering_order_display.get_selection()->signal_changed ().connect (mem_fun (*this, &RegionLayeringOrderEditor::row_selected));
103
104         layering_order_display.grab_focus ();
105
106         set_title (_("Choose Top Region"));
107         show_all();
108 }
109
110 RegionLayeringOrderEditor::~RegionLayeringOrderEditor ()
111 {
112
113 }
114
115 void
116 RegionLayeringOrderEditor::row_selected ()
117 {
118         if (in_row_change) {
119                 return;
120         }
121
122         Glib::RefPtr<TreeSelection> selection = layering_order_display.get_selection();
123         TreeModel::iterator iter = selection->get_selected(); // only used with Gtk::SELECTION_SINGLE
124
125         if (!iter) {
126                 return;
127         }
128
129         TreeModel::Row row = *iter;
130         RegionView* rv = row[layering_order_columns.region_view];
131
132         vector<RegionView*> eq;
133         editor.get_equivalent_regions (rv, eq, Properties::group_select.property_id);
134
135         /* XXX this should be reversible, really */
136
137         for (vector<RegionView*>::iterator i = eq.begin(); i != eq.end(); ++i) {
138                 boost::shared_ptr<Playlist> pl = (*i)->region()->playlist();
139                 if (pl) {
140                         pl->raise_region_to_top ((*i)->region());
141                 }
142         }
143 }
144
145 struct RegionViewCompareByLayer {
146         bool operator() (RegionView* a, RegionView* b) const {
147                 return a->region()->layer() > b->region()->layer();
148         }
149 };
150
151 void
152 RegionLayeringOrderEditor::refill ()
153 {
154         assert (_time_axis_view);
155
156         regions_at_position = 0;
157         in_row_change = true;
158         layering_order_model->clear ();
159
160         RegionSelection region_list;
161         TrackViewList ts;
162         ts.push_back (_time_axis_view);
163         editor.get_regions_at (region_list, position, ts);
164
165         regions_at_position = region_list.size ();
166
167         if (regions_at_position < 2) {
168                 playlist_modified_connection.disconnect ();
169                 hide ();
170                 in_row_change = false;
171                 return;
172         }
173
174         RegionViewCompareByLayer cmp;
175         region_list.sort (cmp);
176
177         for (RegionSelection::const_iterator i = region_list.begin(); i != region_list.end(); ++i) {
178                 TreeModel::Row newrow = *(layering_order_model->append());
179                 newrow[layering_order_columns.name] = (*i)->region()->name();
180                 newrow[layering_order_columns.region_view] = *i;
181
182                if (i == region_list.begin()) {
183                        layering_order_display.get_selection()->select(newrow);
184                }
185         }
186
187         in_row_change = false;
188 }
189
190 void
191 RegionLayeringOrderEditor::set_context (const string& a_name, Session* s, TimeAxisView* tav, boost::shared_ptr<Playlist> pl, samplepos_t pos)
192 {
193         track_name_label.set_text (a_name);
194
195         clock.set_session (s);
196         clock.set (pos, true);
197
198         playlist_modified_connection.disconnect ();
199         pl->ContentsChanged.connect (playlist_modified_connection, invalidator (*this), boost::bind
200                                      (&RegionLayeringOrderEditor::playlist_modified, this), gui_context());
201
202         _time_axis_view = tav;
203
204         position = pos;
205         refill ();
206 }
207
208 bool
209 RegionLayeringOrderEditor::on_key_press_event (GdkEventKey* ev)
210 {
211         bool handled = false;
212
213         /* in general, we want shortcuts working while in this
214            dialog. However, we'd like to treat "return" specially
215            since it is used for row activation. So ..
216
217            for return: try normal handling first
218            then try the editor (to get accelerators/shortcuts)
219            then try normal handling (for keys other than return)
220         */
221
222         if (ev->keyval == GDK_Return) {
223                 handled = ArdourWindow::on_key_press_event (ev);
224         }
225
226         if (!handled) {
227                 handled = relay_key_press (ev, this);
228         }
229
230         if (!handled) {
231                 handled = ArdourWindow::on_key_press_event (ev);
232         }
233
234         return handled;
235 }
236
237 void
238 RegionLayeringOrderEditor::maybe_present ()
239 {
240         if (regions_at_position < 2) {
241                 hide ();
242                 return;
243         }
244
245         present ();
246 }
247
248 void
249 RegionLayeringOrderEditor::playlist_modified ()
250 {
251         refill ();
252 }