update drobilla's fascistic dir-locals.el to force emacs users into whitespace submis...
[ardour.git] / gtk2_ardour / visual_time_axis.cc
1 /*
2     Copyright (C) 2003 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 <cstdlib>
21 #include <cmath>
22 #include <algorithm>
23 #include <string>
24 #include <vector>
25
26 #include "pbd/error.h"
27 #include "pbd/stl_delete.h"
28 #include "pbd/whitespace.h"
29
30 #include <gtkmm2ext/utils.h>
31 #include <gtkmm2ext/selector.h>
32 #include <gtkmm2ext/gtk_ui.h>
33 #include <gtkmm2ext/choice.h>
34
35 #include "ardour/session.h"
36 #include "ardour/utils.h"
37 #include "ardour/processor.h"
38 #include "ardour/location.h"
39
40 #include "ardour_ui.h"
41 #include "public_editor.h"
42 #include "imageframe_time_axis.h"
43 #include "imageframe_time_axis_view.h"
44 #include "marker_time_axis_view.h"
45 #include "imageframe_view.h"
46 #include "marker_time_axis.h"
47 #include "marker_view.h"
48 #include "utils.h"
49 #include "prompter.h"
50 #include "rgb_macros.h"
51 #include "canvas_impl.h"
52
53 #include "i18n.h"
54
55 using namespace ARDOUR;
56 using namespace PBD;
57 using namespace Gtk;
58
59 /**
60  * Abstract Constructor for base visual time axis classes
61  *
62  * @param name the name/Id of thie TimeAxis
63  * @param ed the Ardour PublicEditor
64  * @param sess the current session
65  * @param canvas the parent canvas object
66  */
67 VisualTimeAxis::VisualTimeAxis(const string & name, PublicEditor& ed, ARDOUR::Session* sess, Canvas& canvas)
68         : AxisView(sess),
69           TimeAxisView(sess,ed,(TimeAxisView*) 0, canvas),
70           visual_button (_("v")),
71           size_button (_("h"))
72 {
73         time_axis_name = name ;
74         _color = unique_random_color() ;
75
76         name_entry.signal_activate().connect(sigc::mem_fun(*this, &VisualTimeAxis::name_entry_changed)) ;
77         name_entry.signal_button_press_event().connect(sigc::mem_fun(*this, &VisualTimeAxis::name_entry_button_press_handler)) ;
78         name_entry.signal_button_release_event().connect(sigc::mem_fun(*this, &VisualTimeAxis::name_entry_button_release_handler)) ;
79         name_entry.signal_key_release_event().connect(sigc::mem_fun(*this, &VisualTimeAxis::name_entry_key_release_handler)) ;
80
81         size_button.set_name("TrackSizeButton") ;
82         visual_button.set_name("TrackVisualButton") ;
83         hide_button.set_name("TrackRemoveButton") ;
84         hide_button.add(*(Gtk::manage(new Gtk::Image(get_xpm("small_x.xpm")))));
85         size_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VisualTimeAxis::size_click)) ;
86         visual_button.signal_clicked().connect (sigc::mem_fun (*this, &VisualTimeAxis::visual_click)) ;
87         hide_button.signal_clicked().connect (sigc::mem_fun (*this, &VisualTimeAxis::hide_click)) ;
88         ARDOUR_UI::instance()->set_tip(size_button,_("Display Height")) ;
89         ARDOUR_UI::instance()->set_tip(visual_button, _("Visual options")) ;
90         ARDOUR_UI::instance()->set_tip(hide_button, _("Hide this track")) ;
91
92         controls_table.attach (hide_button, 0, 1, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
93         controls_table.attach (visual_button, 1, 2, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
94         controls_table.attach (size_button, 2, 3, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
95
96         /* remove focus from the buttons */
97         size_button.unset_flags(Gtk::CAN_FOCUS) ;
98         hide_button.unset_flags(Gtk::CAN_FOCUS) ;
99         visual_button.unset_flags(Gtk::CAN_FOCUS) ;
100
101         set_height (hNormal) ;
102 }
103
104 /**
105  * VisualTimeAxis Destructor
106  *
107  */
108 VisualTimeAxis::~VisualTimeAxis()
109 {
110 }
111
112
113 //---------------------------------------------------------------------------------------//
114 // Name/Id Accessors/Mutators
115
116 void
117 VisualTimeAxis::set_time_axis_name(const string & name, void* src)
118 {
119         std::string old_name = time_axis_name ;
120
121         if(name != time_axis_name)
122         {
123                 time_axis_name = name ;
124                 label_view() ;
125                 editor.route_name_changed(this) ;
126
127                  NameChanged(time_axis_name, old_name, src) ; /* EMIT_SIGNAL */
128         }
129 }
130
131 std::string
132 VisualTimeAxis::name() const
133 {
134         return(time_axis_name) ;
135 }
136
137
138 //---------------------------------------------------------------------------------------//
139 // ui methods & data
140
141 /**
142  * Sets the height of this TrackView to one of the defined TrackHeghts
143  *
144  * @param h
145  */
146 void
147 VisualTimeAxis::set_height(uint32_t h)
148 {
149         TimeAxisView::set_height(h);
150
151         if (h >= hNormal) {
152                 hide_name_label ();
153                 show_name_entry ();
154                 other_button_hbox.show_all() ;
155         } else if (h >= hSmaller) {
156                 hide_name_label ();
157                 show_name_entry ();
158                 other_button_hbox.hide_all() ;
159         } else if (h >= hSmall) {
160                 hide_name_entry ();
161                 show_name_label ();
162                 other_button_hbox.hide_all() ;
163         }
164 }
165
166 /**
167  * Handle the visuals button click
168  *
169  */
170 void
171 VisualTimeAxis::visual_click()
172 {
173         popup_display_menu(0);
174 }
175
176
177 /**
178  * Handle the hide buttons click
179  *
180  */
181 void
182 VisualTimeAxis::hide_click()
183 {
184         // LAME fix for hide_button display refresh
185         hide_button.set_sensitive(false);
186
187         editor.hide_track_in_display (*this);
188
189         hide_button.set_sensitive(true);
190 }
191
192
193 /**
194  * Allows the selection of a new color for this TimeAxis
195  *
196  */
197 void
198 VisualTimeAxis::select_track_color ()
199 {
200         if(choose_time_axis_color())
201         {
202                 //Does nothing at this abstract point
203         }
204 }
205
206 /**
207  * Provides a color chooser for the selection of a new time axis color.
208  *
209  */
210 bool
211 VisualTimeAxis::choose_time_axis_color()
212 {
213         bool picked ;
214         Gdk::Color color ;
215         gdouble current[4] ;
216         Gdk::Color current_color ;
217
218         current[0] = _color.get_red() / 65535.0 ;
219         current[1] = _color.get_green() / 65535.0 ;
220         current[2] = _color.get_blue() / 65535.0 ;
221         current[3] = 1.0 ;
222
223         current_color.set_rgb_p (current[0],current[1],current[2]);
224         color = Gtkmm2ext::UI::instance()->get_color(_("Color Selection"),picked, &current_color) ;
225
226         if (picked)
227         {
228                 set_time_axis_color(color) ;
229         }
230         return(picked) ;
231 }
232
233 /**
234  * Sets the color of this TimeAxis to the specified color c
235  *
236  * @param c the new TimeAxis color
237  */
238 void
239 VisualTimeAxis::set_time_axis_color(Gdk::Color c)
240 {
241         _color = c ;
242 }
243
244 void
245 VisualTimeAxis::set_selected_regionviews (RegionSelection& regions)
246 {
247         // Not handled by purely visual TimeAxis
248 }
249
250 //---------------------------------------------------------------------------------------//
251 // Handle time axis removal
252
253 /**
254  * Handles the Removal of this VisualTimeAxis
255  *
256  * @param src the identity of the object that initiated the change
257  */
258 void
259 VisualTimeAxis::remove_this_time_axis(void* src)
260 {
261         vector<string> choices;
262
263         std::string prompt  = string_compose (_("Do you really want to remove track \"%1\" ?\n\nYou may also lose the playlist used by this track.\n\n(This action cannot be undone, and the session file will be overwritten)"), time_axis_name);
264
265         choices.push_back (_("No, do nothing."));
266         choices.push_back (_("Yes, remove it."));
267
268         Gtkmm2ext::Choice prompter (prompt, choices);
269
270         if (prompter.run () == 1) {
271                 /*
272                   defer to idle loop, otherwise we'll delete this object
273                   while we're still inside this function ...
274                 */
275                 Glib::signal_idle().connect(sigc::bind(sigc::ptr_fun(&VisualTimeAxis::idle_remove_this_time_axis), this, src));
276         }
277 }
278
279 /**
280  * Callback used to remove this time axis during the gtk idle loop
281  * This is used to avoid deleting the obejct while inside the remove_this_time_axis
282  * method
283  *
284  * @param ta the VisualTimeAxis to remove
285  * @param src the identity of the object that initiated the change
286  */
287 gint
288 VisualTimeAxis::idle_remove_this_time_axis(VisualTimeAxis* ta, void* src)
289 {
290          ta->VisualTimeAxisRemoved(ta->name(), src) ; /* EMIT_SIGNAL */
291         delete ta ;
292         ta = 0 ;
293         return(false) ;
294 }
295
296
297
298
299 //---------------------------------------------------------------------------------------//
300 // Handle TimeAxis rename
301
302 /**
303  * Construct a new prompt to receive a new name for this TimeAxis
304  *
305  * @see finish_time_axis_rename()
306  */
307 void
308 VisualTimeAxis::start_time_axis_rename()
309 {
310         ArdourPrompter name_prompter;
311
312         name_prompter.set_prompt (_("new name: ")) ;
313         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
314         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
315         name_prompter.show_all() ;
316
317         switch (name_prompter.run ()) {
318         case Gtk::RESPONSE_ACCEPT:
319           string result;
320           name_prompter.get_result (result);
321           if (result.length()) {
322                   if (editor.get_named_time_axis(result) != 0) {
323                     ARDOUR_UI::instance()->popup_error (_("A track already exists with that name"));
324                     return ;
325                   }
326
327                   set_time_axis_name(result, this) ;
328           }
329         }
330         label_view() ;
331 }
332
333 /**
334  * Handles the new name for this TimeAxis from the name prompt
335  *
336  * @see start_time_axis_rename()
337  */
338
339 void
340 VisualTimeAxis::label_view()
341 {
342         name_label.set_text(time_axis_name) ;
343         name_entry.set_text(time_axis_name) ;
344         ARDOUR_UI::instance()->set_tip(name_entry, time_axis_name) ;
345 }
346
347
348 //---------------------------------------------------------------------------------------//
349 // Handle name entry signals
350
351 void
352 VisualTimeAxis::name_entry_changed()
353 {
354         string x = name_entry.get_text ();
355
356         if (x == time_axis_name) {
357                 return;
358         }
359
360         strip_whitespace_edges(x);
361
362         if (x.length() == 0) {
363                 name_entry.set_text (time_axis_name);
364                 return;
365         }
366
367         if (!editor.get_named_time_axis(x)) {
368                 set_time_axis_name(x, this);
369         } else {
370                 ARDOUR_UI::instance()->popup_error (_("A track already exists with that name"));
371                 name_entry.set_text(time_axis_name);
372         }
373 }
374
375 bool
376 VisualTimeAxis::name_entry_button_press_handler(GdkEventButton *ev)
377 {
378         if (ev->button == 3) {
379                 return true;
380         }
381         return false
382 }
383
384 bool
385 VisualTimeAxis::name_entry_button_release_handler(GdkEventButton *ev)
386 {
387         return false;
388 }
389
390 bool
391 VisualTimeAxis::name_entry_key_release_handler(GdkEventKey* ev)
392 {
393         switch (ev->keyval) {
394         case GDK_Tab:
395         case GDK_Up:
396         case GDK_Down:
397                 name_entry_changed ();
398                 return true;
399
400         default:
401                 break;
402         }
403
404         return false;
405 }
406
407
408 //---------------------------------------------------------------------------------------//
409 // Super class methods not handled by VisualTimeAxis
410
411 void
412 VisualTimeAxis::show_timestretch (framepos_t start, framepos_t end)
413 {
414         // Not handled by purely visual TimeAxis
415 }
416
417 void
418 VisualTimeAxis::hide_timestretch()
419 {
420         // Not handled by purely visual TimeAxis
421 }
422
423