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