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