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