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