2 * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2010-2015 David Robillard <d@drobilla.net>
4 * Copyright (C) 2011-2017 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2016-2017 Nick Mainsbridge <mainsbridge@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <gtkmm/stock.h>
23 #include <gtkmm/table.h>
25 #include "gtkmm2ext/utils.h"
27 #include "edit_note_dialog.h"
28 #include "midi_region_view.h"
29 #include "note_base.h"
35 using namespace Gtkmm2ext;
38 * EditNoteDialog constructor.
40 * @param n Notes to edit.
43 EditNoteDialog::EditNoteDialog (MidiRegionView* rv, set<NoteBase*> n)
44 : ArdourDialog (_("Note"))
47 , _channel_all (_("Set selected notes to this channel"))
48 , _pitch_all (_("Set selected notes to this pitch"))
49 , _velocity_all (_("Set selected notes to this velocity"))
50 , _time_clock (X_("notetime"), true, "", true, false)
51 , _time_all (_("Set selected notes to this time"))
52 , _length_clock (X_("notelength"), true, "", true, false, true)
53 , _length_all (_("Set selected notes to this length"))
55 Table* table = manage (new Table (4, 2));
56 table->set_spacings (6);
60 Label* l = manage (left_aligned_label (_("Channel")));
61 table->attach (*l, 0, 1, r, r + 1);
62 table->attach (_channel, 1, 2, r, r + 1);
63 table->attach (_channel_all, 2, 3, r, r + 1);
66 _channel.set_range (1, 16);
67 _channel.set_increments (1, 2);
68 _channel.set_value ((*_events.begin())->note()->channel () + 1);
70 l = manage (left_aligned_label (_("Pitch")));
71 table->attach (*l, 0, 1, r, r + 1);
72 table->attach (_pitch, 1, 2, r, r + 1);
73 table->attach (_pitch_all, 2, 3, r, r + 1);
76 _pitch.set_range (0, 127);
77 _pitch.set_increments (1, 10);
78 _pitch.set_value ((*_events.begin())->note()->note());
80 l = manage (left_aligned_label (_("Velocity")));
81 table->attach (*l, 0, 1, r, r + 1);
82 table->attach (_velocity, 1, 2, r, r + 1);
83 table->attach (_velocity_all, 2, 3, r, r + 1);
86 _velocity.set_range (0, 127);
87 _velocity.set_increments (1, 10);
88 _velocity.set_value ((*_events.begin())->note()->velocity ());
90 l = manage (left_aligned_label (_("Time")));
91 table->attach (*l, 0, 1, r, r + 1);
92 table->attach (_time_clock, 1, 2, r, r + 1);
93 table->attach (_time_all, 2, 3, r, r + 1);
96 _time_clock.set_session (_region_view->get_time_axis_view().session ());
97 _time_clock.set_mode (AudioClock::BBT);
98 _time_clock.set (_region_view->source_relative_time_converter().to
99 ((*_events.begin())->note()->time()) + (_region_view->region()->position() - _region_view->region()->start()), true);
101 l = manage (left_aligned_label (_("Length")));
102 table->attach (*l, 0, 1, r, r + 1);
103 table->attach (_length_clock, 1, 2, r, r + 1);
104 table->attach (_length_all, 2, 3, r, r + 1);
107 _length_clock.set_session (_region_view->get_time_axis_view().session ());
108 _length_clock.set_mode (AudioClock::BBT);
110 _region_view->region_relative_time_converter().to ((*_events.begin())->note()->end_time ()) + _region_view->region()->position(),
112 _region_view->region_relative_time_converter().to ((*_events.begin())->note()->time ()) + _region_view->region()->position());
114 /* Set up `set all notes...' buttons' sensitivity */
116 _channel_all.set_sensitive (false);
117 _pitch_all.set_sensitive (false);
118 _velocity_all.set_sensitive (false);
119 _time_all.set_sensitive (false);
120 _length_all.set_sensitive (false);
122 int test_channel = (*_events.begin())->note()->channel ();
123 int test_pitch = (*_events.begin())->note()->note ();
124 int test_velocity = (*_events.begin())->note()->velocity ();
125 Temporal::Beats test_time = (*_events.begin())->note()->time ();
126 Temporal::Beats test_length = (*_events.begin())->note()->length ();
128 for (set<NoteBase*>::iterator i = _events.begin(); i != _events.end(); ++i) {
129 if ((*i)->note()->channel() != test_channel) {
130 _channel_all.set_sensitive (true);
133 if ((*i)->note()->note() != test_pitch) {
134 _pitch_all.set_sensitive (true);
137 if ((*i)->note()->velocity() != test_velocity) {
138 _velocity_all.set_sensitive (true);
141 if ((*i)->note()->time () != test_time) {
142 _time_all.set_sensitive (true);
145 if ((*i)->note()->length () != test_length) {
146 _length_all.set_sensitive (true);
150 get_vbox()->pack_start (*table);
152 add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
153 add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_ACCEPT);
154 set_default_response (Gtk::RESPONSE_ACCEPT);
158 EditNoteDialog::done (int r)
160 if (r != RESPONSE_ACCEPT) {
164 /* These calls mean that if a value is entered using the keyboard
165 it will be returned by the get_value_as_int()s below.
171 _region_view->start_note_diff_command (_("edit note"));
173 bool had_change = false;
175 if (!_channel_all.get_sensitive() || _channel_all.get_active ()) {
176 for (set<NoteBase*>::iterator i = _events.begin(); i != _events.end(); ++i) {
177 if (_channel.get_value_as_int() - 1 != (*i)->note()->channel()) {
178 _region_view->change_note_channel (*i, _channel.get_value_as_int () - 1);
184 if (!_pitch_all.get_sensitive() || _pitch_all.get_active ()) {
185 for (set<NoteBase*>::iterator i = _events.begin(); i != _events.end(); ++i) {
186 if (_pitch.get_value_as_int() != (*i)->note()->note()) {
187 _region_view->change_note_note (*i, _pitch.get_value_as_int ());
193 if (!_velocity_all.get_sensitive() || _velocity_all.get_active ()) {
194 for (set<NoteBase*>::iterator i = _events.begin(); i != _events.end(); ++i) {
195 if (_velocity.get_value_as_int() != (*i)->note()->velocity()) {
196 _region_view->change_note_velocity (*i, _velocity.get_value_as_int ());
202 samplecnt_t const region_samples = _time_clock.current_time() - (_region_view->region()->position() - _region_view->region()->start());
203 Temporal::Beats const t = _region_view->source_relative_time_converter().from (region_samples);
205 if (!_time_all.get_sensitive() || _time_all.get_active ()) {
206 for (set<NoteBase*>::iterator i = _events.begin(); i != _events.end(); ++i) {
207 if (t != (*i)->note()->time()) {
208 _region_view->change_note_time (*i, t);
214 if (!_length_all.get_sensitive() || _length_all.get_active ()) {
215 for (set<NoteBase*>::iterator i = _events.begin(); i != _events.end(); ++i) {
216 samplepos_t const note_end_sample = region_samples + _length_clock.current_duration (_time_clock.current_time());
217 Temporal::Beats const d = _region_view->source_relative_time_converter().from (note_end_sample) - (*i)->note()->time();
218 if (d != (*i)->note()->length()) {
219 _region_view->change_note_length (*i, d);
226 _region_view->abort_command ();
229 _region_view->apply_diff ();
231 list<Evoral::event_id_t> notes;
232 for (set<NoteBase*>::iterator i = _events.begin(); i != _events.end(); ++i) {
233 notes.push_back ((*i)->note()->id());
236 _region_view->select_notes (notes);