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