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