Fix thinkos in cubasish theme
[ardour.git] / gtk2_ardour / edit_note_dialog.cc
1 /*
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>
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <gtkmm/stock.h>
23 #include <gtkmm/table.h>
24
25 #include "gtkmm2ext/utils.h"
26
27 #include "edit_note_dialog.h"
28 #include "midi_region_view.h"
29 #include "note_base.h"
30
31 #include "pbd/i18n.h"
32
33 using namespace std;
34 using namespace Gtk;
35 using namespace Gtkmm2ext;
36
37 /**
38  *    EditNoteDialog constructor.
39  *
40  *    @param n Notes to edit.
41  */
42
43 EditNoteDialog::EditNoteDialog (MidiRegionView* rv, set<NoteBase*> n)
44         : ArdourDialog (_("Note"))
45         , _region_view (rv)
46         , _events (n)
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"))
54 {
55         Table* table = manage (new Table (4, 2));
56         table->set_spacings (6);
57
58         int r = 0;
59
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);
64         ++r;
65
66         _channel.set_range (1, 16);
67         _channel.set_increments (1, 2);
68         _channel.set_value ((*_events.begin())->note()->channel () + 1);
69
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);
74         ++r;
75
76         _pitch.set_range (0, 127);
77         _pitch.set_increments (1, 10);
78         _pitch.set_value ((*_events.begin())->note()->note());
79
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);
84         ++r;
85
86         _velocity.set_range (0, 127);
87         _velocity.set_increments (1, 10);
88         _velocity.set_value ((*_events.begin())->note()->velocity ());
89
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);
94         ++r;
95
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);
100
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);
105         ++r;
106
107         _length_clock.set_session (_region_view->get_time_axis_view().session ());
108         _length_clock.set_mode (AudioClock::BBT);
109         _length_clock.set (
110                 _region_view->region_relative_time_converter().to ((*_events.begin())->note()->end_time ()) + _region_view->region()->position(),
111                 true,
112                 _region_view->region_relative_time_converter().to ((*_events.begin())->note()->time ()) + _region_view->region()->position());
113
114         /* Set up `set all notes...' buttons' sensitivity */
115
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);
121
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 ();
127
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);
131                 }
132
133                 if ((*i)->note()->note() != test_pitch) {
134                         _pitch_all.set_sensitive (true);
135                 }
136
137                 if ((*i)->note()->velocity() != test_velocity) {
138                         _velocity_all.set_sensitive (true);
139                 }
140
141                 if ((*i)->note()->time () != test_time) {
142                         _time_all.set_sensitive (true);
143                 }
144
145                 if ((*i)->note()->length () != test_length) {
146                         _length_all.set_sensitive (true);
147                 }
148         }
149
150         get_vbox()->pack_start (*table);
151
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);
155 }
156
157 void
158 EditNoteDialog::done (int r)
159 {
160         if (r != RESPONSE_ACCEPT) {
161                 return;
162         }
163
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.
166         */
167         _channel.update ();
168         _pitch.update ();
169         _velocity.update ();
170
171         _region_view->start_note_diff_command (_("edit note"));
172
173         bool had_change = false;
174
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);
179                                 had_change = true;
180                         }
181                 }
182         }
183
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 ());
188                                 had_change = true;
189                         }
190                 }
191         }
192
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 ());
197                                 had_change = true;
198                         }
199                 }
200         }
201
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);
204
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);
209                                 had_change = true;
210                         }
211                 }
212         }
213
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);
220                                 had_change = true;
221                         }
222                 }
223         }
224
225         if (!had_change) {
226                 _region_view->abort_command ();
227         }
228
229         _region_view->apply_diff ();
230
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());
234         }
235
236         _region_view->select_notes (notes);
237 }