fix keyboard event handling for host-provided plugin GUIs
[ardour2.git] / gtk2_ardour / visual_time_axis.cc
blobfdbd378607b91ece0d4b3a2b3e52a54b4217c5ae
1 /*
2 Copyright (C) 2003 Paul Davis
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.
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.
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.
20 #include <cstdlib>
21 #include <cmath>
22 #include <algorithm>
23 #include <string>
24 #include <vector>
26 #include <pbd/error.h>
27 #include <pbd/stl_delete.h>
28 #include <pbd/whitespace.h>
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>
36 #include <ardour/session.h>
37 #include <ardour/utils.h>
38 #include <ardour/insert.h>
39 #include <ardour/location.h>
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"
54 #include "i18n.h"
56 using namespace ARDOUR;
57 using namespace PBD;
58 using namespace sigc;
59 using namespace Gtk;
61 /**
62 * Abstract Constructor for base visual time axis classes
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
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"))
75 time_axis_name = name ;
76 _color = unique_random_color() ;
77 _marked_for_display = true;
79 name_entry.signal_activate().connect(mem_fun(*this, &VisualTimeAxis::name_entry_changed)) ;
80 name_entry.signal_button_press_event().connect(mem_fun(*this, &VisualTimeAxis::name_entry_button_press_handler)) ;
81 name_entry.signal_button_release_event().connect(mem_fun(*this, &VisualTimeAxis::name_entry_button_release_handler)) ;
82 name_entry.signal_key_release_event().connect(mem_fun(*this, &VisualTimeAxis::name_entry_key_release_handler)) ;
84 size_button.set_name("TrackSizeButton") ;
85 visual_button.set_name("TrackVisualButton") ;
86 hide_button.set_name("TrackRemoveButton") ;
87 hide_button.add (*(manage (new Image (::get_icon("hide")))));
88 hide_button.show_all ();
90 size_button.signal_button_release_event().connect (mem_fun (*this, &VisualTimeAxis::size_click)) ;
91 visual_button.signal_clicked().connect (mem_fun (*this, &VisualTimeAxis::visual_click)) ;
92 hide_button.signal_clicked().connect (mem_fun (*this, &VisualTimeAxis::hide_click)) ;
93 ARDOUR_UI::instance()->tooltips().set_tip(size_button,_("Display Height")) ;
94 ARDOUR_UI::instance()->tooltips().set_tip(visual_button, _("Visual options")) ;
95 ARDOUR_UI::instance()->tooltips().set_tip(hide_button, _("Hide this track")) ;
97 if (0) {
98 /* old school - when we used to put an extra row of buttons in place */
99 controls_table.attach (hide_button, 0, 1, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
100 controls_table.attach (visual_button, 1, 2, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
101 controls_table.attach (size_button, 2, 3, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
103 /* remove focus from the buttons */
104 size_button.unset_flags(Gtk::CAN_FOCUS) ;
105 hide_button.unset_flags(Gtk::CAN_FOCUS) ;
106 visual_button.unset_flags(Gtk::CAN_FOCUS) ;
108 set_height (hNormal) ;
112 * VisualTimeAxis Destructor
115 VisualTimeAxis::~VisualTimeAxis()
120 //---------------------------------------------------------------------------------------//
121 // Name/Id Accessors/Mutators
123 void
124 VisualTimeAxis::set_time_axis_name(const string & name, void* src)
126 std::string old_name = time_axis_name ;
128 if(name != time_axis_name)
130 time_axis_name = name ;
131 label_view() ;
132 editor.route_name_changed(this) ;
134 NameChanged(time_axis_name, old_name, src) ; /* EMIT_SIGNAL */
138 std::string
139 VisualTimeAxis::name() const
141 return(time_axis_name) ;
145 //---------------------------------------------------------------------------------------//
146 // ui methods & data
149 * Sets the height of this TrackView to one of the defined TrackHeghts
151 * @param h
153 void
154 VisualTimeAxis::set_height(uint32_t h)
156 TimeAxisView::set_height(h);
158 if (h >= hNormal) {
159 hide_name_label ();
160 show_name_entry ();
161 visual_button.show();
162 size_button.show();
163 hide_button.show();
164 } else if (h >= hSmaller) {
165 hide_name_label ();
166 show_name_entry ();
167 visual_button.hide ();
168 size_button.hide ();
169 hide_button.hide();
170 } else if (h >= hSmall) {
171 hide_name_entry ();
172 show_name_label ();
173 visual_button.hide ();
174 size_button.hide ();
175 hide_button.hide ();
180 * Handle the visuals button click
183 void
184 VisualTimeAxis::visual_click()
186 popup_display_menu(0);
191 * Handle the hide buttons click
194 void
195 VisualTimeAxis::hide_click()
197 // LAME fix for hide_button display refresh
198 hide_button.set_sensitive(false);
200 editor.hide_track_in_display (*this);
202 hide_button.set_sensitive(true);
207 * Allows the selection of a new color for this TimeAxis
210 void
211 VisualTimeAxis::select_track_color ()
213 if(choose_time_axis_color())
215 //Does nothing at this abstract point
220 * Provides a color chooser for the selection of a new time axis color.
223 bool
224 VisualTimeAxis::choose_time_axis_color()
226 bool picked ;
227 Gdk::Color color ;
228 gdouble current[4] ;
229 Gdk::Color current_color ;
231 current[0] = _color.get_red() / 65535.0 ;
232 current[1] = _color.get_green() / 65535.0 ;
233 current[2] = _color.get_blue() / 65535.0 ;
234 current[3] = 1.0 ;
236 current_color.set_rgb_p (current[0],current[1],current[2]);
237 color = Gtkmm2ext::UI::instance()->get_color(_("ardour: color selection"),picked, &current_color) ;
239 if (picked)
241 set_time_axis_color(color) ;
243 return(picked) ;
247 * Sets the color of this TimeAxis to the specified color c
249 * @param c the new TimeAxis color
251 void
252 VisualTimeAxis::set_time_axis_color(Gdk::Color c)
254 _color = c ;
257 void
258 VisualTimeAxis::set_selected_regionviews (RegionSelection& regions)
260 // Not handled by purely visual TimeAxis
263 //---------------------------------------------------------------------------------------//
264 // Handle time axis removal
267 * Handles the Removal of this VisualTimeAxis
269 * @param src the identity of the object that initiated the change
271 void
272 VisualTimeAxis::remove_this_time_axis(void* src)
274 vector<string> choices;
276 std::string prompt = string_compose (_("Do you really want to remove track \"%1\" ?\n\n(This action cannot be undone, and the session file will be overwritten!)"), time_axis_name);
278 choices.push_back (_("No, do nothing."));
279 choices.push_back (_("Yes, remove it."));
281 Gtkmm2ext::Choice prompter (prompt, choices);
283 if (prompter.run () == 1) {
285 defer to idle loop, otherwise we'll delete this object
286 while we're still inside this function ...
288 Glib::signal_idle().connect(bind(sigc::ptr_fun(&VisualTimeAxis::idle_remove_this_time_axis), this, src));
293 * Callback used to remove this time axis during the gtk idle loop
294 * This is used to avoid deleting the obejct while inside the remove_this_time_axis
295 * method
297 * @param ta the VisualTimeAxis to remove
298 * @param src the identity of the object that initiated the change
300 gint
301 VisualTimeAxis::idle_remove_this_time_axis(VisualTimeAxis* ta, void* src)
303 ta->VisualTimeAxisRemoved(ta->name(), src) ; /* EMIT_SIGNAL */
304 delete ta ;
305 ta = 0 ;
306 return(false) ;
312 //---------------------------------------------------------------------------------------//
313 // Handle TimeAxis rename
316 * Construct a new prompt to receive a new name for this TimeAxis
318 * @see finish_time_axis_rename()
320 void
321 VisualTimeAxis::start_time_axis_rename()
323 ArdourPrompter name_prompter;
325 name_prompter.set_prompt (_("new name: ")) ;
326 name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
327 name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
328 name_prompter.show_all() ;
330 switch (name_prompter.run ()) {
331 case Gtk::RESPONSE_ACCEPT:
332 string result;
333 name_prompter.get_result (result);
334 if (result.length()) {
335 if (editor.get_named_time_axis(result) != 0) {
336 ARDOUR_UI::instance()->popup_error (_("A track already exists with that name"));
337 return ;
340 set_time_axis_name(result, this) ;
343 label_view() ;
347 * Handles the new name for this TimeAxis from the name prompt
349 * @see start_time_axis_rename()
352 void
353 VisualTimeAxis::label_view()
355 name_label.set_text(time_axis_name) ;
356 name_entry.set_text(time_axis_name) ;
357 ARDOUR_UI::instance()->tooltips().set_tip(name_entry, time_axis_name) ;
361 //---------------------------------------------------------------------------------------//
362 // Handle name entry signals
364 void
365 VisualTimeAxis::name_entry_changed()
367 string x = name_entry.get_text ();
369 if (x == time_axis_name) {
370 return;
373 strip_whitespace_edges(x);
375 if (x.length() == 0) {
376 name_entry.set_text (time_axis_name);
377 return;
380 if (!editor.get_named_time_axis(x)) {
381 set_time_axis_name(x, this);
382 } else {
383 ARDOUR_UI::instance()->popup_error (_("A track already exists with that name"));
384 name_entry.set_text(time_axis_name);
388 gint
389 VisualTimeAxis::name_entry_button_press_handler(GdkEventButton *ev)
391 if (ev->button == 3) {
392 return stop_signal (name_entry, "button_press_event");
394 return FALSE;
397 gint
398 VisualTimeAxis::name_entry_button_release_handler(GdkEventButton *ev)
400 return FALSE;
403 gint
404 VisualTimeAxis::name_entry_key_release_handler(GdkEventKey* ev)
406 switch (ev->keyval) {
407 case GDK_Tab:
408 case GDK_Up:
409 case GDK_Down:
410 name_entry_changed ();
411 return TRUE;
413 default:
414 return FALSE;
419 //---------------------------------------------------------------------------------------//
420 // Super class methods not handled by VisualTimeAxis
422 void
423 VisualTimeAxis::show_timestretch (nframes_t start, nframes_t end)
425 // Not handled by purely visual TimeAxis
428 void
429 VisualTimeAxis::hide_timestretch()
431 // Not handled by purely visual TimeAxis