more improvements and fixes for the import dialog
[ardour.git] / gtk2_ardour / editor_keyboard.cc
1 /*
2     Copyright (C) 2004 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 <ardour/audioregion.h>
21 #include <ardour/playlist.h>
22 #include <pbd/memento_command.h>
23
24 #include "editor.h"
25 #include "region_view.h"
26 #include "selection.h"
27
28 #include "i18n.h"
29
30 void
31 Editor::kbd_driver (sigc::slot<void,GdkEvent*> theslot, bool use_track_canvas, bool use_time_canvas, bool can_select)
32 {
33         gint x, y;
34         double worldx, worldy;
35         GdkEvent ev;
36         Gdk::ModifierType mask;
37         Glib::RefPtr<Gdk::Window> evw = track_canvas.get_window()->get_pointer (x, y, mask);
38         bool doit = false;
39
40         if (use_track_canvas && track_canvas_event_box.get_window()->get_pointer(x, y, mask) != 0) {
41                 doit = true;
42         } else if (use_time_canvas && time_canvas_event_box.get_window()->get_pointer(x, y, mask)!= 0) {
43                 doit = true;
44         }
45
46         /* any use of "keyboard mouse buttons" invalidates an existing grab
47         */
48         
49         if (drag_info.item) {
50                 drag_info.item->ungrab (GDK_CURRENT_TIME);
51                 drag_info.item = 0;
52         }
53
54         if (doit) {
55
56                 if (entered_regionview && can_select) {
57                         selection->set (entered_regionview);
58                 }
59
60                 track_canvas.window_to_world (x, y, worldx, worldy);
61                 worldx += horizontal_adjustment.get_value();
62                 worldy += vertical_adjustment.get_value();
63
64                 ev.type = GDK_BUTTON_PRESS;
65                 ev.button.x = worldx;
66                 ev.button.y = worldy;
67                 ev.button.state = 0;  /* XXX correct? */
68
69                 theslot (&ev);
70         }
71 }
72
73 void
74 Editor::kbd_set_playhead_cursor ()
75 {
76         kbd_driver (mem_fun(*this, &Editor::set_playhead_cursor), true, true, false);
77 }
78
79 void
80 Editor::kbd_set_edit_cursor ()
81 {
82         kbd_driver (mem_fun(*this, &Editor::set_edit_cursor), true, true, false);
83 }
84
85
86 void
87 Editor::kbd_do_split (GdkEvent* ev)
88 {
89         nframes_t where = event_frame (ev);
90
91         if (entered_regionview) {
92                 if (selection->regions.contains (entered_regionview)) {
93                         split_regions_at (where, selection->regions);
94                 } else {
95                         RegionSelection s;
96                         s.add (entered_regionview);
97                         split_regions_at (where, s);
98                 }
99         }
100 }
101
102 void
103 Editor::kbd_split ()
104 {
105         kbd_driver (mem_fun(*this, &Editor::kbd_do_split), true, true, false);
106 }
107
108 void
109 Editor::kbd_mute_unmute_region ()
110 {
111         if (entered_regionview) {
112                 begin_reversible_command (_("mute region"));
113                 XMLNode &before = entered_regionview->region()->playlist()->get_state();
114                 
115                 entered_regionview->region()->set_muted (!entered_regionview->region()->muted());
116                 
117                 XMLNode &after = entered_regionview->region()->playlist()->get_state();
118                 session->add_command (new MementoCommand<ARDOUR::Playlist>(*(entered_regionview->region()->playlist()), &before, &after));
119                 commit_reversible_command();
120         }
121 }
122
123 void
124 Editor::kbd_set_sync_position ()
125 {
126         kbd_driver (mem_fun(*this, &Editor::kbd_do_set_sync_position), true, true, false);
127 }
128
129 void
130 Editor::kbd_do_set_sync_position (GdkEvent* ev)
131 {
132     nframes_t where = event_frame (ev);
133         snap_to (where);
134
135         if (entered_regionview) {
136           set_a_regions_sync_position (entered_regionview->region(), where);
137         }
138 }
139
140 void
141 Editor::kbd_do_align (GdkEvent* ev, ARDOUR::RegionPoint what)
142 {
143         align (what);
144 }
145
146 void
147 Editor::kbd_align (ARDOUR::RegionPoint what)
148 {
149         kbd_driver (bind (mem_fun(*this, &Editor::kbd_do_align), what));
150 }
151
152 void
153 Editor::kbd_do_align_relative (GdkEvent* ev, ARDOUR::RegionPoint what)
154 {
155         align (what);
156 }
157
158 void
159 Editor::kbd_align_relative (ARDOUR::RegionPoint what)
160 {
161         kbd_driver (bind (mem_fun(*this, &Editor::kbd_do_align), what), true, true, false);
162 }
163
164 void
165 Editor::kbd_do_brush (GdkEvent *ev)
166 {
167         brush (event_frame (ev, 0, 0));
168 }
169
170 void
171 Editor::kbd_brush ()
172 {
173         kbd_driver (mem_fun(*this, &Editor::kbd_do_brush), true, true, false);
174 }
175
176 void
177 Editor::kbd_do_audition (GdkEvent *ignored)
178 {
179         audition_selected_region ();
180 }
181
182 void
183 Editor::kbd_audition ()
184 {
185         kbd_driver (mem_fun(*this, &Editor::kbd_do_audition), true, false, true);
186 }