s/stringcr_t/const string &/
[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 "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 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(const string & name, PublicEditor& ed, ARDOUR::Session& sess, Canvas& 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.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         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(const 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         Gdk::Color color ;
251         gdouble current[4] ;
252         Gdk::Color current_color ;
253         
254         current[0] = _color.get_red() / 65535.0 ;
255         current[1] = _color.get_green() / 65535.0 ;
256         current[2] = _color.get_blue() / 65535.0 ;
257         current[3] = 1.0 ;
258
259         current_color.set_rgb_p (current[0],current[1],current[2]);
260         color = Gtkmm2ext::UI::instance()->get_color(_("ardour: color selection"),picked, &current_color) ;
261         
262         if (picked)
263         {
264                 set_time_axis_color(color) ;
265         }
266         return(picked) ;
267 }
268
269 /**
270  * Sets the color of this TimeAxis to the specified color c
271  *
272  * @param c the new TimeAxis color
273  */
274 void
275 VisualTimeAxis::set_time_axis_color(Gdk::Color c)
276 {
277         _color = c ;
278 }
279
280 void
281 VisualTimeAxis::set_selected_regionviews (AudioRegionSelection& regions)
282 {
283         // Not handled by purely visual TimeAxis
284 }
285
286 //---------------------------------------------------------------------------------------//
287 // Handle time axis removal
288
289 /**
290  * Handles the Removal of this VisualTimeAxis
291  *
292  * @param src the identity of the object that initiated the change
293  */
294 void
295 VisualTimeAxis::remove_this_time_axis(void* src)
296 {
297         vector<string> choices;
298
299         std::string prompt  = string_compose (_("Do you really want to remove track \"%1\" ?\n(cannot be undone)"), time_axis_name);
300
301         choices.push_back (_("Yes, remove it."));
302         choices.push_back (_("No, do nothing."));
303
304         Gtkmm2ext::Choice prompter (prompt, choices);
305
306         prompter.chosen.connect(sigc::ptr_fun(Gtk::Main::quit));
307         prompter.show_all ();
308
309         Gtk::Main::run ();
310
311         if (prompter.get_choice() == 0)
312         {
313                 /*
314                    defer to idle loop, otherwise we'll delete this object
315                    while we're still inside this function ...
316                 */
317           Glib::signal_idle().connect(bind(sigc::ptr_fun(&VisualTimeAxis::idle_remove_this_time_axis), this, src));
318         }
319 }
320
321 /**
322  * Callback used to remove this time axis during the gtk idle loop
323  * This is used to avoid deleting the obejct while inside the remove_this_time_axis
324  * method
325  *
326  * @param ta the VisualTimeAxis to remove
327  * @param src the identity of the object that initiated the change
328  */
329 gint
330 VisualTimeAxis::idle_remove_this_time_axis(VisualTimeAxis* ta, void* src)
331 {
332          ta->VisualTimeAxisRemoved(ta->name(), src) ; /* EMIT_SIGNAL */
333         delete ta ;
334         ta = 0 ;
335         return(false) ;
336 }
337
338
339
340
341 //---------------------------------------------------------------------------------------//
342 // Handle TimeAxis rename
343                 
344 /**
345  * Construct a new prompt to receive a new name for this TimeAxis
346  *
347  * @see finish_time_axis_rename()
348  */
349 void
350 VisualTimeAxis::start_time_axis_rename()
351 {
352         if(name_prompter)
353         {
354                 delete name_prompter ;
355                 name_prompter = 0 ;
356         }
357
358         name_prompter = new ArdourPrompter() ;
359
360         name_prompter->set_prompt (_("new name: ")) ;
361         name_prompter->show_all() ;
362
363         switch (name_prompter->run ()) {
364         case GTK_RESPONSE_ACCEPT:
365           string result;
366           name_prompter->get_result (result);
367           if (editor.get_named_time_axis(result) != 0) {
368             ARDOUR_UI::instance()->popup_error (_("A track already exists with that name"));
369             return ;
370           }
371           
372           set_time_axis_name(result, this) ;
373         }
374         delete name_prompter ;
375         name_prompter = 0 ;
376         label_view() ;
377
378
379 }
380
381 /**
382  * Handles the new name for this TimeAxis from the name prompt
383  *
384  * @see start_time_axis_rename()
385  */
386
387 void
388 VisualTimeAxis::label_view()
389 {
390         name_label.set_text(time_axis_name) ;
391         name_entry.set_text(time_axis_name) ;
392         ARDOUR_UI::instance()->tooltips().set_tip(name_entry, time_axis_name) ;
393 }
394
395
396 //---------------------------------------------------------------------------------------//
397 // Handle name entry signals 
398
399 void
400 VisualTimeAxis::name_entry_changed()
401 {
402         string x = name_entry.get_text ();
403         
404         if (x == time_axis_name) {
405                 return;
406         }
407
408         if (x.length() == 0) {
409                 name_entry.set_text (time_axis_name);
410                 return;
411         }
412
413         strip_whitespace_edges(x);
414
415         if (!editor.get_named_time_axis(x)) {
416                 set_time_axis_name(x, this);
417         } else {
418                 ARDOUR_UI::instance()->popup_error (_("a track already exists with that name"));
419                 name_entry.set_text(time_axis_name);
420         }
421 }
422
423 gint 
424 VisualTimeAxis::name_entry_button_press_handler(GdkEventButton *ev)
425 {
426         if (ev->button == 3) {
427                 return stop_signal (name_entry, "button_press_event");
428         }
429         return FALSE;
430 }
431
432 gint 
433 VisualTimeAxis::name_entry_button_release_handler(GdkEventButton *ev)
434 {
435         return FALSE;
436 }
437
438 gint
439 VisualTimeAxis::name_entry_key_release_handler(GdkEventKey* ev)
440 {
441         switch (ev->keyval) {
442         case GDK_Tab:
443         case GDK_Up:
444         case GDK_Down:
445                 name_entry_changed ();
446                 return TRUE;
447
448         default:
449                 return FALSE;
450         }
451 }
452
453
454 //---------------------------------------------------------------------------------------//
455 // Super class methods not handled by VisualTimeAxis
456                 
457 void
458 VisualTimeAxis::show_timestretch (jack_nframes_t start, jack_nframes_t end)
459 {
460   // Not handled by purely visual TimeAxis
461 }
462
463 void
464 VisualTimeAxis::hide_timestretch()
465 {
466   // Not handled by purely visual TimeAxis
467 }
468
469