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