Copy-edit.
[ardour.git] / gtk2_ardour / region_layering_order_editor.cc
1 #include <gtkmm/table.h>
2 #include <gtkmm/stock.h>
3 #include <gtkmm/alignment.h>
4 #include <ardour/region.h>
5
6 #include "gui_thread.h"
7 #include "i18n.h"
8 #include "keyboard.h"
9 #include "public_editor.h"
10 #include "region_layering_order_editor.h"
11 #include "utils.h"
12
13 using namespace std;
14 using namespace Gtk;
15 using namespace ARDOUR;
16
17 RegionLayeringOrderEditor::RegionLayeringOrderEditor (PublicEditor& pe)
18         : ArdourDialog (pe, _("RegionLayeringOrderEditor"), false, false)
19         , playlist ()
20         , position ()
21         , in_row_change (false)
22         , regions_at_position (0)
23         , layering_order_columns ()
24         , layering_order_model (Gtk::ListStore::create (layering_order_columns))
25         , layering_order_display ()
26         , clock ("layer dialog", true, "RegionLayeringOrderEditorClock", false, false, false)
27         , scroller ()
28         , editor (pe)
29 {
30         set_name ("RegionLayeringOrderEditorWindow");
31
32         layering_order_display.set_model (layering_order_model);
33
34         layering_order_display.append_column (_("Region Name"), layering_order_columns.name);
35         layering_order_display.set_headers_visible (true);
36         layering_order_display.set_reorderable (false);
37         layering_order_display.set_rules_hint (true);
38
39         scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
40         scroller.add (layering_order_display);
41
42         clock.set_mode (AudioClock::BBT);
43
44         Gtk::Table* scroller_table = manage (new Gtk::Table);
45         scroller_table->set_size_request (300, 250);
46         scroller_table->attach (scroller, 0, 1, 0, 1);
47         scroller_table->set_col_spacings (5);
48         scroller_table->set_row_spacings (5);
49   
50         track_label.set_name ("RegionLayeringOrderEditorLabel");
51         track_label.set_text (_("Track:"));
52         track_label.set_alignment (0, 0.5);
53         clock_label.set_name ("RegionLayeringOrderEditorLabel");
54         clock_label.set_text (_("Position:"));
55         clock_label.set_alignment (0, 0.5);
56         track_name_label.set_name ("RegionLayeringOrderEditorNameLabel");
57         track_name_label.set_alignment (0, 0.5);
58         clock.set_mode (AudioClock::BBT);
59   
60         Gtk::Table* info_table = manage (new Gtk::Table (2, 2));
61         info_table->set_col_spacings (5);
62         info_table->set_row_spacings (5);
63         info_table->attach (track_label, 0, 1, 0, 1, FILL, FILL);
64         info_table->attach (track_name_label, 1, 2, 0, 1, FILL, FILL);
65         info_table->attach (clock_label, 0, 1, 1, 2, FILL, FILL);
66         info_table->attach (clock, 1, 2, 1, 2, FILL, FILL);
67  
68         get_vbox()->set_spacing (12);
69         get_vbox()->pack_start (*info_table, false, false);
70         get_vbox()->pack_start (*scroller_table, true, true);
71  
72         info_table->set_name ("RegionLayeringOrderTable");
73         scroller_table->set_name ("RegionLayeringOrderTable");
74
75         layering_order_display.set_name ("RegionLayeringOrderDisplay");
76
77         layering_order_display.signal_row_activated ().connect (mem_fun (*this, &RegionLayeringOrderEditor::row_activated));
78
79         layering_order_display.grab_focus ();
80
81         set_title (_("Choose Top Region"));
82         show_all();
83 }
84
85 RegionLayeringOrderEditor::~RegionLayeringOrderEditor ()
86 {
87 }
88
89 void
90 RegionLayeringOrderEditor::row_activated (const TreeModel::Path& path, TreeViewColumn* column)
91 {
92         if (in_row_change) {
93                 return;
94         }
95
96         TreeModel::iterator iter = layering_order_model->get_iter (path);
97
98         if (iter) {
99                 TreeModel::Row row = *iter;
100                 boost::shared_ptr<Region> region = row[layering_order_columns.region];
101
102                 region->raise_to_top ();
103         }
104 }
105
106 typedef boost::shared_ptr<Region> RegionPtr;
107
108 struct RegionCompareByLayer {
109     bool operator() (RegionPtr a, RegionPtr b) const {
110             return a->layer() > b->layer();
111     }
112 };
113
114 void
115 RegionLayeringOrderEditor::refill ()
116 {
117         regions_at_position = 0;
118
119         if (!playlist) {
120                 return;
121         }
122
123         typedef Playlist::RegionList RegionList;
124
125         in_row_change = true;
126
127         layering_order_model->clear ();
128
129         boost::shared_ptr<RegionList> region_list(playlist->regions_at (position));
130
131         regions_at_position = region_list->size();
132
133         if (regions_at_position < 2) {
134                 playlist_modified_connection.disconnect ();
135                 hide ();
136                 in_row_change = false;
137                 return;
138         }
139
140         RegionCompareByLayer cmp;
141         region_list->sort (cmp);
142
143         for (RegionList::const_iterator i = region_list->begin(); i != region_list->end(); ++i) {
144                 TreeModel::Row newrow = *(layering_order_model->append());
145                 newrow[layering_order_columns.name] = (*i)->name();
146                 newrow[layering_order_columns.region] = *i;
147
148                if (i == region_list->begin()) {
149                        layering_order_display.get_selection()->select(newrow);
150                }
151         }
152
153         in_row_change = false;
154 }
155
156 void
157 RegionLayeringOrderEditor::set_context (const string& a_name, Session* s, const boost::shared_ptr<Playlist>  & pl, framepos_t pos)
158 {
159         track_name_label.set_text (a_name);
160
161         clock.set_session (s);
162         clock.set (pos, true, 0, 0);
163
164         playlist_modified_connection.disconnect ();
165         playlist = pl;
166         playlist->ContentsChanged.connect (playlist_modified_connection, invalidator (*this), boost::bind
167                                            (&RegionLayeringOrderEditor::playlist_modified, this), gui_context());
168
169         position = pos;
170         refill ();
171 }
172
173 bool
174 RegionLayeringOrderEditor::on_key_press_event (GdkEventKey* ev)
175 {
176         bool handled = false;
177
178         /* in general, we want shortcuts working while in this
179            dialog. However, we'd like to treat "return" specially
180            since it is used for row activation. So ..
181
182            for return: try normal handling first
183            then try the editor (to get accelerators/shortcuts)
184            then try normal handling (for keys other than return)
185         */
186
187         if (ev->keyval == GDK_Return) {
188                 handled = ArdourDialog::on_key_press_event (ev);
189         }
190         
191         if (!handled) {
192                 handled = key_press_focus_accelerator_handler (editor, ev);
193         }
194
195         if (!handled) {
196                 handled = ArdourDialog::on_key_press_event (ev);
197         }
198         
199         return handled;
200 }
201         
202 void
203 RegionLayeringOrderEditor::maybe_present ()
204 {
205         if (regions_at_position < 2) {
206                 hide ();
207                 return;
208         }
209
210         present ();
211 }
212
213 void
214 RegionLayeringOrderEditor::playlist_modified ()
215 {
216         refill ();
217 }