fix incorrect restoration of pane positions on Apple.
[ardour.git] / gtk2_ardour / step_editor.cc
1 /*
2     Copyright (C) 2012 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/midi_track.h"
21 #include "ardour/midi_region.h"
22 #include "ardour/tempo.h"
23 #include "ardour/types.h"
24
25 #include "gui_thread.h"
26 #include "midi_region_view.h"
27 #include "public_editor.h"
28 #include "step_editor.h"
29 #include "step_entry.h"
30
31 using namespace ARDOUR;
32 using namespace Gtk;
33 using namespace std;
34
35 StepEditor::StepEditor (PublicEditor& e, boost::shared_ptr<MidiTrack> t, MidiTimeAxisView& mtv)
36         : _editor (e)
37         , _track (t)
38         , step_editor (0)
39         , _mtv (mtv)
40 {
41         step_edit_insert_position = 0;
42         _step_edit_triplet_countdown = 0;
43         _step_edit_within_chord = 0;
44         _step_edit_chord_duration = Evoral::Beats();
45         step_edit_region_view = 0;
46
47         _track->PlaylistChanged.connect (*this, invalidator (*this),
48                                          boost::bind (&StepEditor::playlist_changed, this),
49                                          gui_context());
50         playlist_changed ();
51 }
52
53 StepEditor::~StepEditor()
54 {
55         delete step_editor;
56 }
57
58 void
59 StepEditor::start_step_editing ()
60 {
61         _step_edit_triplet_countdown = 0;
62         _step_edit_within_chord = 0;
63         _step_edit_chord_duration = Evoral::Beats();
64         step_edit_region.reset ();
65         step_edit_region_view = 0;
66         last_added_pitch = -1;
67         last_added_end = Evoral::Beats();
68
69         resync_step_edit_position ();
70         prepare_step_edit_region ();
71         reset_step_edit_beat_pos ();
72
73         assert (step_edit_region);
74         assert (step_edit_region_view);
75
76         if (step_editor == 0) {
77                 step_editor = new StepEntry (*this);
78                 step_editor->signal_delete_event().connect (sigc::mem_fun (*this, &StepEditor::step_editor_hidden));
79                 step_editor->signal_hide().connect (sigc::mem_fun (*this, &StepEditor::step_editor_hide));
80         }
81
82         step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
83         step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
84
85         step_editor->present ();
86 }
87
88 void
89 StepEditor::resync_step_edit_position ()
90 {
91         step_edit_insert_position = _editor.get_preferred_edit_position (Editing::EDIT_IGNORE_NONE, false, true);
92 }
93
94 void
95 StepEditor::resync_step_edit_to_edit_point ()
96 {
97         resync_step_edit_position ();
98         if (step_edit_region) {
99                 reset_step_edit_beat_pos ();
100         }
101 }
102
103 void
104 StepEditor::prepare_step_edit_region ()
105 {
106         boost::shared_ptr<Region> r = _track->playlist()->top_region_at (step_edit_insert_position);
107
108         if (r) {
109                 step_edit_region = boost::dynamic_pointer_cast<MidiRegion>(r);
110         }
111
112         if (step_edit_region) {
113                 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
114                 step_edit_region_view = dynamic_cast<MidiRegionView*> (rv);
115
116         } else {
117
118                 const Meter& m = _mtv.session()->tempo_map().meter_at_frame (step_edit_insert_position);
119
120                 double baf = _mtv.session()->tempo_map().beat_at_frame (step_edit_insert_position);
121                 double next_bar_in_beats =  baf + m.divisions_per_bar();
122                 framecnt_t next_bar_pos = _mtv.session()->tempo_map().frame_at_beat (next_bar_in_beats);
123                 framecnt_t len = next_bar_pos - step_edit_insert_position;
124
125                 step_edit_region = _mtv.add_region (step_edit_insert_position, len, true, _editor.get_grid_music_divisions (0));
126
127                 RegionView* rv = _mtv.midi_view()->find_view (step_edit_region);
128                 step_edit_region_view = dynamic_cast<MidiRegionView*>(rv);
129         }
130 }
131
132
133 void
134 StepEditor::reset_step_edit_beat_pos ()
135 {
136         assert (step_edit_region);
137         assert (step_edit_region_view);
138
139         framecnt_t frames_from_start = _editor.get_preferred_edit_position() - step_edit_region->position();
140
141         if (frames_from_start < 0) {
142                 /* this can happen with snap enabled, and the edit point == Playhead. we snap the
143                    position of the new region, and it can end up after the edit point.
144                 */
145                 frames_from_start = 0;
146         }
147
148         step_edit_beat_pos = step_edit_region_view->region_frames_to_region_beats (frames_from_start);
149         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
150 }
151
152 bool
153 StepEditor::step_editor_hidden (GdkEventAny*)
154 {
155         step_editor_hide ();
156         return true; // XXX remember position ?!
157 }
158
159 void
160 StepEditor::step_editor_hide ()
161 {
162         /* everything else will follow the change in the model */
163         _track->set_step_editing (false);
164 }
165
166 void
167 StepEditor::stop_step_editing ()
168 {
169         if (step_editor) {
170                 step_editor->hide ();
171         }
172
173         if (step_edit_region_view) {
174                 step_edit_region_view->hide_step_edit_cursor();
175         }
176
177         step_edit_region.reset ();
178 }
179
180 void
181 StepEditor::check_step_edit ()
182 {
183         MidiRingBuffer<framepos_t>& incoming (_track->step_edit_ring_buffer());
184         uint8_t* buf;
185         uint32_t bufsize = 32;
186
187         buf = new uint8_t[bufsize];
188
189         while (incoming.read_space()) {
190                 framepos_t time;
191                 Evoral::EventType type;
192                 uint32_t size;
193
194                 incoming.read_prefix (&time, &type, &size);
195
196                 if (size > bufsize) {
197                         delete [] buf;
198                         bufsize = size;
199                         buf = new uint8_t[bufsize];
200                 }
201
202                 incoming.read_contents (size, buf);
203
204                 if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON) {
205                         step_add_note (buf[0] & 0xf, buf[1], buf[2], Evoral::Beats());
206                 }
207         }
208 }
209
210 int
211 StepEditor::step_add_bank_change (uint8_t /*channel*/, uint8_t /*bank*/)
212 {
213         return 0;
214 }
215
216 int
217 StepEditor::step_add_program_change (uint8_t /*channel*/, uint8_t /*program*/)
218 {
219         return 0;
220 }
221
222 void
223 StepEditor::step_edit_sustain (Evoral::Beats beats)
224 {
225         if (step_edit_region_view) {
226                 step_edit_region_view->step_sustain (beats);
227         }
228 }
229
230 void
231 StepEditor::move_step_edit_beat_pos (Evoral::Beats beats)
232 {
233         if (!step_edit_region_view) {
234                 return;
235         }
236         if (beats > 0.0) {
237                 step_edit_beat_pos = min (step_edit_beat_pos + beats,
238                                           step_edit_region_view->region_frames_to_region_beats (step_edit_region->length()));
239         } else if (beats < 0.0) {
240                 if (-beats < step_edit_beat_pos) {
241                         step_edit_beat_pos += beats; // its negative, remember
242                 } else {
243                         step_edit_beat_pos = Evoral::Beats();
244                 }
245         }
246         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
247 }
248
249 int
250 StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evoral::Beats beat_duration)
251 {
252         /* do these things in case undo removed the step edit region
253          */
254         if (!step_edit_region) {
255                 resync_step_edit_position ();
256                 prepare_step_edit_region ();
257                 reset_step_edit_beat_pos ();
258                 step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
259                 step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
260         }
261
262         assert (step_edit_region);
263         assert (step_edit_region_view);
264
265         if (beat_duration == 0.0 && step_editor) {
266                 beat_duration = step_editor->note_length();
267         } else if (beat_duration == 0.0) {
268                 bool success;
269                 beat_duration = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
270
271                 if (!success) {
272                         return -1;
273                 }
274         }
275
276         MidiStreamView* msv = _mtv.midi_view();
277
278         /* make sure its visible on the vertical axis */
279
280         if (pitch < msv->lowest_note() || pitch > msv->highest_note()) {
281                 msv->update_note_range (pitch);
282                 msv->set_note_range (MidiStreamView::ContentsRange);
283         }
284
285         /* make sure its visible on the horizontal axis */
286
287         framepos_t fpos = step_edit_region_view->region_beats_to_absolute_frames (step_edit_beat_pos + beat_duration);
288
289         if (fpos >= (_editor.leftmost_sample() + _editor.current_page_samples())) {
290                 _editor.reset_x_origin (fpos - (_editor.current_page_samples()/4));
291         }
292
293         Evoral::Beats at = step_edit_beat_pos;
294         Evoral::Beats len = beat_duration;
295
296         if ((last_added_pitch >= 0) && (pitch == last_added_pitch) && (last_added_end == step_edit_beat_pos)) {
297
298                 /* avoid any apparent note overlap - move the start of this note
299                    up by 1 tick from where the last note ended
300                 */
301
302                 at  += Evoral::Beats::ticks(1);
303                 len -= Evoral::Beats::ticks(1);
304         }
305
306         step_edit_region_view->step_add_note (channel, pitch, velocity, at, len);
307
308         last_added_pitch = pitch;
309         last_added_end = at+len;
310
311         if (_step_edit_triplet_countdown > 0) {
312                 _step_edit_triplet_countdown--;
313
314                 if (_step_edit_triplet_countdown == 0) {
315                         _step_edit_triplet_countdown = 3;
316                 }
317         }
318
319         if (!_step_edit_within_chord) {
320                 step_edit_beat_pos += beat_duration;
321                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
322         } else {
323                 step_edit_beat_pos += Evoral::Beats::ticks(1); // tiny, but no longer overlapping
324                 _step_edit_chord_duration = max (_step_edit_chord_duration, beat_duration);
325         }
326
327         step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
328
329         return 0;
330 }
331
332 void
333 StepEditor::set_step_edit_cursor_width (Evoral::Beats beats)
334 {
335         if (step_edit_region_view) {
336                 step_edit_region_view->set_step_edit_cursor_width (beats);
337         }
338 }
339
340 bool
341 StepEditor::step_edit_within_triplet() const
342 {
343         return _step_edit_triplet_countdown > 0;
344 }
345
346 bool
347 StepEditor::step_edit_within_chord() const
348 {
349         return _step_edit_within_chord;
350 }
351
352 void
353 StepEditor::step_edit_toggle_triplet ()
354 {
355         if (_step_edit_triplet_countdown == 0) {
356                 _step_edit_within_chord = false;
357                 _step_edit_triplet_countdown = 3;
358         } else {
359                 _step_edit_triplet_countdown = 0;
360         }
361 }
362
363 void
364 StepEditor::step_edit_toggle_chord ()
365 {
366         if (_step_edit_within_chord) {
367                 _step_edit_within_chord = false;
368                 if (step_edit_region_view) {
369                         step_edit_beat_pos += _step_edit_chord_duration;
370                         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
371                 }
372         } else {
373                 _step_edit_triplet_countdown = 0;
374                 _step_edit_within_chord = true;
375         }
376 }
377
378 void
379 StepEditor::step_edit_rest (Evoral::Beats beats)
380 {
381         bool success;
382
383         if (beats == 0.0) {
384                 beats = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
385         } else {
386                 success = true;
387         }
388
389         if (success && step_edit_region_view) {
390                 step_edit_beat_pos += beats;
391                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
392         }
393 }
394
395 void
396 StepEditor::step_edit_beat_sync ()
397 {
398         step_edit_beat_pos = step_edit_beat_pos.round_up_to_beat();
399         if (step_edit_region_view) {
400                 step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
401         }
402 }
403
404 void
405 StepEditor::step_edit_bar_sync ()
406 {
407         Session* _session = _mtv.session ();
408
409         if (!_session || !step_edit_region_view || !step_edit_region) {
410                 return;
411         }
412
413         framepos_t fpos = step_edit_region_view->region_beats_to_absolute_frames (step_edit_beat_pos);
414         fpos = _session->tempo_map().round_to_bar (fpos, RoundUpAlways);
415         step_edit_beat_pos = step_edit_region_view->region_frames_to_region_beats (fpos - step_edit_region->position()).round_up_to_beat();
416         step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
417 }
418
419 void
420 StepEditor::playlist_changed ()
421 {
422         step_edit_region_connection.disconnect ();
423         _track->playlist()->RegionRemoved.connect (step_edit_region_connection, invalidator (*this),
424                                                    boost::bind (&StepEditor::region_removed, this, _1),
425                                                    gui_context());
426 }
427
428 void
429 StepEditor::region_removed (boost::weak_ptr<Region> wr)
430 {
431         boost::shared_ptr<Region> r (wr.lock());
432
433         if (!r) {
434                 return;
435         }
436
437         if (step_edit_region == r) {
438                 step_edit_region.reset();
439                 step_edit_region_view = 0;
440                 // force a recompute of the insert position
441                 step_edit_beat_pos = Evoral::Beats(-1);
442         }
443 }
444
445 string
446 StepEditor::name() const
447 {
448         return _track->name();
449 }