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