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