Fix thinkos in cubasish theme
[ardour.git] / gtk2_ardour / step_editor.cc
index 8afc0ed8261cbe176cb2fb1d15ecf47972a2b517..e2b230de276fb8d21ccad00acdba8db380ea9ced 100644 (file)
@@ -1,21 +1,26 @@
 /*
-    Copyright (C) 2012 Paul Davis
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-*/
+ * Copyright (C) 2010-2011 Carl Hetherington <carl@carlh.net>
+ * Copyright (C) 2010-2018 Paul Davis <paul@linuxaudiosystems.com>
+ * Copyright (C) 2011-2015 David Robillard <d@drobilla.net>
+ * Copyright (C) 2013-2018 Robin Gareus <robin@gareus.org>
+ * Copyright (C) 2015-2017 Nick Mainsbridge <mainsbridge@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "pbd/stacktrace.h"
 
 #include "ardour/midi_track.h"
 #include "ardour/midi_region.h"
@@ -35,13 +40,12 @@ using namespace std;
 StepEditor::StepEditor (PublicEditor& e, boost::shared_ptr<MidiTrack> t, MidiTimeAxisView& mtv)
        : _editor (e)
        , _track (t)
-       , step_editor (0)
        , _mtv (mtv)
 {
        step_edit_insert_position = 0;
        _step_edit_triplet_countdown = 0;
        _step_edit_within_chord = 0;
-       _step_edit_chord_duration = Evoral::Beats();
+       _step_edit_chord_duration = Temporal::Beats();
        step_edit_region_view = 0;
 
        _track->PlaylistChanged.connect (*this, invalidator (*this),
@@ -52,7 +56,7 @@ StepEditor::StepEditor (PublicEditor& e, boost::shared_ptr<MidiTrack> t, MidiTim
 
 StepEditor::~StepEditor()
 {
-       delete step_editor;
+       StepEntry::instance().set_step_editor (0);
 }
 
 void
@@ -60,11 +64,11 @@ StepEditor::start_step_editing ()
 {
        _step_edit_triplet_countdown = 0;
        _step_edit_within_chord = 0;
-       _step_edit_chord_duration = Evoral::Beats();
+       _step_edit_chord_duration = Temporal::Beats();
        step_edit_region.reset ();
        step_edit_region_view = 0;
        last_added_pitch = -1;
-       last_added_end = Evoral::Beats();
+       last_added_end = Temporal::Beats();
 
        resync_step_edit_position ();
        prepare_step_edit_region ();
@@ -73,16 +77,14 @@ StepEditor::start_step_editing ()
        assert (step_edit_region);
        assert (step_edit_region_view);
 
-       if (step_editor == 0) {
-               step_editor = new StepEntry (*this);
-               step_editor->signal_delete_event().connect (sigc::mem_fun (*this, &StepEditor::step_editor_hidden));
-               step_editor->signal_hide().connect (sigc::mem_fun (*this, &StepEditor::step_editor_hide));
-       }
+       StepEntry::instance().set_step_editor (this);
+       delete_connection = StepEntry::instance().signal_delete_event().connect (sigc::mem_fun (*this, &StepEditor::step_entry_hidden));
+       hide_connection = StepEntry::instance(). signal_hide().connect (sigc::mem_fun (*this, &StepEditor::step_entry_done));
 
        step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
-       step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
+       step_edit_region_view->set_step_edit_cursor_width (StepEntry::instance().note_length());
 
-       step_editor->present ();
+       StepEntry::instance().present ();
 }
 
 void
@@ -115,12 +117,11 @@ StepEditor::prepare_step_edit_region ()
 
        } else {
 
-               const Meter& m = _mtv.session()->tempo_map().meter_at_frame (step_edit_insert_position);
-
-               double baf = _mtv.session()->tempo_map().beat_at_frame (step_edit_insert_position);
+               const Meter& m = _mtv.session()->tempo_map().meter_at_sample (step_edit_insert_position);
+               double baf = max (0.0, _mtv.session()->tempo_map().beat_at_sample (step_edit_insert_position));
                double next_bar_in_beats =  baf + m.divisions_per_bar();
-               framecnt_t next_bar_pos = _mtv.session()->tempo_map().frame_at_beat (next_bar_in_beats);
-               framecnt_t len = next_bar_pos - step_edit_insert_position;
+               samplecnt_t next_bar_pos = _mtv.session()->tempo_map().sample_at_beat (next_bar_in_beats);
+               samplecnt_t len = next_bar_pos - step_edit_insert_position;
 
                step_edit_region = _mtv.add_region (step_edit_insert_position, len, true);
 
@@ -136,29 +137,32 @@ StepEditor::reset_step_edit_beat_pos ()
        assert (step_edit_region);
        assert (step_edit_region_view);
 
-       framecnt_t frames_from_start = _editor.get_preferred_edit_position() - step_edit_region->position();
+       samplecnt_t samples_from_start = _editor.get_preferred_edit_position() - step_edit_region->position();
 
-       if (frames_from_start < 0) {
+       if (samples_from_start < 0) {
                /* this can happen with snap enabled, and the edit point == Playhead. we snap the
                   position of the new region, and it can end up after the edit point.
                */
-               frames_from_start = 0;
+               samples_from_start = 0;
        }
 
-       step_edit_beat_pos = step_edit_region_view->region_frames_to_region_beats (frames_from_start);
+       step_edit_beat_pos = step_edit_region_view->region_samples_to_region_beats (samples_from_start);
        step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
 }
 
 bool
-StepEditor::step_editor_hidden (GdkEventAny*)
+StepEditor::step_entry_hidden (GdkEventAny*)
 {
-       step_editor_hide ();
-       return true; // XXX remember position ?!
+       step_entry_done ();
+       return true;
 }
 
 void
-StepEditor::step_editor_hide ()
+StepEditor::step_entry_done ()
 {
+       hide_connection.disconnect ();
+       delete_connection.disconnect ();
+
        /* everything else will follow the change in the model */
        _track->set_step_editing (false);
 }
@@ -166,9 +170,7 @@ StepEditor::step_editor_hide ()
 void
 StepEditor::stop_step_editing ()
 {
-       if (step_editor) {
-               step_editor->hide ();
-       }
+       StepEntry::instance().hide ();
 
        if (step_edit_region_view) {
                step_edit_region_view->hide_step_edit_cursor();
@@ -180,18 +182,20 @@ StepEditor::stop_step_editing ()
 void
 StepEditor::check_step_edit ()
 {
-       MidiRingBuffer<framepos_t>& incoming (_track->step_edit_ring_buffer());
+       MidiRingBuffer<samplepos_t>& incoming (_track->step_edit_ring_buffer());
        uint8_t* buf;
        uint32_t bufsize = 32;
 
        buf = new uint8_t[bufsize];
 
        while (incoming.read_space()) {
-               framepos_t time;
+               samplepos_t time;
                Evoral::EventType type;
                uint32_t size;
 
-               incoming.read_prefix (&time, &type, &size);
+               if (!incoming.read_prefix (&time, &type, &size)) {
+                       break;
+               }
 
                if (size > bufsize) {
                        delete [] buf;
@@ -199,12 +203,15 @@ StepEditor::check_step_edit ()
                        buf = new uint8_t[bufsize];
                }
 
-               incoming.read_contents (size, buf);
+               if (!incoming.read_contents (size, buf)) {
+                       break;
+               }
 
-               if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON) {
-                       step_add_note (buf[0] & 0xf, buf[1], buf[2], Evoral::Beats());
+               if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON && size == 3) {
+                       step_add_note (buf[0] & 0xf, buf[1], buf[2], Temporal::Beats());
                }
        }
+       delete [] buf;
 }
 
 int
@@ -220,7 +227,7 @@ StepEditor::step_add_program_change (uint8_t /*channel*/, uint8_t /*program*/)
 }
 
 void
-StepEditor::step_edit_sustain (Evoral::Beats beats)
+StepEditor::step_edit_sustain (Temporal::Beats beats)
 {
        if (step_edit_region_view) {
                step_edit_region_view->step_sustain (beats);
@@ -228,26 +235,26 @@ StepEditor::step_edit_sustain (Evoral::Beats beats)
 }
 
 void
-StepEditor::move_step_edit_beat_pos (Evoral::Beats beats)
+StepEditor::move_step_edit_beat_pos (Temporal::Beats beats)
 {
        if (!step_edit_region_view) {
                return;
        }
        if (beats > 0.0) {
                step_edit_beat_pos = min (step_edit_beat_pos + beats,
-                                         step_edit_region_view->region_frames_to_region_beats (step_edit_region->length()));
+                                         step_edit_region_view->region_samples_to_region_beats (step_edit_region->length()));
        } else if (beats < 0.0) {
                if (-beats < step_edit_beat_pos) {
                        step_edit_beat_pos += beats; // its negative, remember
                } else {
-                       step_edit_beat_pos = Evoral::Beats();
+                       step_edit_beat_pos = Temporal::Beats();
                }
        }
        step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
 }
 
 int
-StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evoral::Beats beat_duration)
+StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Temporal::Beats beat_duration)
 {
        /* do these things in case undo removed the step edit region
         */
@@ -256,14 +263,14 @@ StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evo
                prepare_step_edit_region ();
                reset_step_edit_beat_pos ();
                step_edit_region_view->show_step_edit_cursor (step_edit_beat_pos);
-               step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
+               step_edit_region_view->set_step_edit_cursor_width (StepEntry::instance().note_length());
        }
 
        assert (step_edit_region);
        assert (step_edit_region_view);
 
-       if (beat_duration == 0.0 && step_editor) {
-               beat_duration = step_editor->note_length();
+       if (beat_duration == 0.0) {
+               beat_duration = StepEntry::instance().note_length();
        } else if (beat_duration == 0.0) {
                bool success;
                beat_duration = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
@@ -284,14 +291,14 @@ StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evo
 
        /* make sure its visible on the horizontal axis */
 
-       framepos_t fpos = step_edit_region_view->region_beats_to_absolute_frames (step_edit_beat_pos + beat_duration);
+       samplepos_t fpos = step_edit_region_view->region_beats_to_absolute_samples (step_edit_beat_pos + beat_duration);
 
        if (fpos >= (_editor.leftmost_sample() + _editor.current_page_samples())) {
                _editor.reset_x_origin (fpos - (_editor.current_page_samples()/4));
        }
 
-       Evoral::Beats at = step_edit_beat_pos;
-       Evoral::Beats len = beat_duration;
+       Temporal::Beats at = step_edit_beat_pos;
+       Temporal::Beats len = beat_duration;
 
        if ((last_added_pitch >= 0) && (pitch == last_added_pitch) && (last_added_end == step_edit_beat_pos)) {
 
@@ -299,8 +306,8 @@ StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evo
                   up by 1 tick from where the last note ended
                */
 
-               at  += Evoral::Beats::ticks(1);
-               len -= Evoral::Beats::ticks(1);
+               at  += Temporal::Beats::ticks(1);
+               len -= Temporal::Beats::ticks(1);
        }
 
        step_edit_region_view->step_add_note (channel, pitch, velocity, at, len);
@@ -320,17 +327,17 @@ StepEditor::step_add_note (uint8_t channel, uint8_t pitch, uint8_t velocity, Evo
                step_edit_beat_pos += beat_duration;
                step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
        } else {
-               step_edit_beat_pos += Evoral::Beats::ticks(1); // tiny, but no longer overlapping
+               step_edit_beat_pos += Temporal::Beats::ticks(1); // tiny, but no longer overlapping
                _step_edit_chord_duration = max (_step_edit_chord_duration, beat_duration);
        }
 
-       step_edit_region_view->set_step_edit_cursor_width (step_editor->note_length());
+       step_edit_region_view->set_step_edit_cursor_width (StepEntry::instance().note_length());
 
        return 0;
 }
 
 void
-StepEditor::set_step_edit_cursor_width (Evoral::Beats beats)
+StepEditor::set_step_edit_cursor_width (Temporal::Beats beats)
 {
        if (step_edit_region_view) {
                step_edit_region_view->set_step_edit_cursor_width (beats);
@@ -376,7 +383,7 @@ StepEditor::step_edit_toggle_chord ()
 }
 
 void
-StepEditor::step_edit_rest (Evoral::Beats beats)
+StepEditor::step_edit_rest (Temporal::Beats beats)
 {
        bool success;
 
@@ -410,9 +417,9 @@ StepEditor::step_edit_bar_sync ()
                return;
        }
 
-       framepos_t fpos = step_edit_region_view->region_beats_to_absolute_frames (step_edit_beat_pos);
-       fpos = _session->tempo_map().round_to_bar (fpos, RoundUpAlways);
-       step_edit_beat_pos = step_edit_region_view->region_frames_to_region_beats (fpos - step_edit_region->position()).round_up_to_beat();
+       samplepos_t fpos = step_edit_region_view->region_beats_to_absolute_samples (step_edit_beat_pos);
+       fpos = _session->tempo_map().round_to_bar (fpos, RoundUpAlways).sample;
+       step_edit_beat_pos = step_edit_region_view->region_samples_to_region_beats (fpos - step_edit_region->position()).round_up_to_beat();
        step_edit_region_view->move_step_edit_cursor (step_edit_beat_pos);
 }
 
@@ -438,7 +445,7 @@ StepEditor::region_removed (boost::weak_ptr<Region> wr)
                step_edit_region.reset();
                step_edit_region_view = 0;
                // force a recompute of the insert position
-               step_edit_beat_pos = Evoral::Beats(-1);
+               step_edit_beat_pos = Temporal::Beats(-1);
        }
 }