Marginal cleanup.
[ardour.git] / gtk2_ardour / edit_note_dialog.cc
1 /*
2     Copyright (C) 2010 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 <gtkmm/stock.h>
21 #include <gtkmm/table.h>
22
23 #include "gtkmm2ext/utils.h"
24
25 #include "canvas-note-event.h"
26 #include "edit_note_dialog.h"
27 #include "midi_region_view.h"
28
29 #include "i18n.h"
30
31 using namespace Gtk;
32 using namespace Gtkmm2ext;
33
34 /**
35  *    EditNoteDialog constructor.
36  *
37  *    @param n Note to edit.
38  */
39
40 EditNoteDialog::EditNoteDialog (MidiRegionView* rv, Gnome::Canvas::CanvasNoteEvent* ev)
41         : ArdourDialog (_("Note"))
42         , _region_view (rv)
43         , _event (ev)
44         , _time_clock (X_("notetime"), true, "", true, false)
45         , _length_clock (X_("notelength"), true, "", true, false, true)
46 {
47         Table* table = manage (new Table (4, 2));
48         table->set_spacings (6);
49
50         int r = 0;
51
52         Label* l = manage (left_aligned_label (_("Channel")));
53         table->attach (*l, 0, 1, r, r + 1);
54         table->attach (_channel, 1, 2, r, r + 1);
55         ++r;
56
57         _channel.set_range (1, 16);
58         _channel.set_increments (1, 2);
59         _channel.set_value (ev->note()->channel () + 1);
60
61         l = manage (left_aligned_label (_("Pitch")));
62         table->attach (*l, 0, 1, r, r + 1);
63         table->attach (_pitch, 1, 2, r, r + 1);
64         ++r;
65
66         _pitch.set_range (0, 127);
67         _pitch.set_increments (1, 10);
68         _pitch.set_value (ev->note()->note ());
69
70         l = manage (left_aligned_label (_("Velocity")));
71         table->attach (*l, 0, 1, r, r + 1);
72         table->attach (_velocity, 1, 2, r, r + 1);
73         ++r;
74
75         _velocity.set_range (0, 127);
76         _velocity.set_increments (1, 10);
77         _velocity.set_value (ev->note()->velocity ());
78
79         l = manage (left_aligned_label (_("Time")));
80         table->attach (*l, 0, 1, r, r + 1);
81         table->attach (_time_clock, 1, 2, r, r + 1);
82         ++r;
83
84         _time_clock.set_session (_region_view->get_time_axis_view().session ());
85         _time_clock.set_mode (AudioClock::BBT);
86         _time_clock.set (_region_view->source_relative_time_converter().to (ev->note()->time ()), true);
87
88         l = manage (left_aligned_label (_("Length")));
89         table->attach (*l, 0, 1, r, r + 1);
90         table->attach (_length_clock, 1, 2, r, r + 1);
91         ++r;
92
93         _length_clock.set_session (_region_view->get_time_axis_view().session ());
94         _length_clock.set_mode (AudioClock::BBT);
95         _length_clock.set (_region_view->region_relative_time_converter().to (ev->note()->length ()), true);
96
97         get_vbox()->pack_start (*table);
98
99         add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
100         add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_ACCEPT);
101         set_default_response (Gtk::RESPONSE_ACCEPT);
102
103         show_all ();
104 }
105
106 int
107 EditNoteDialog::run ()
108 {
109         int const r = Dialog::run ();
110         if (r != RESPONSE_ACCEPT) {
111                 return r;
112         }
113
114         /* These calls mean that if a value is entered using the keyboard
115            it will be returned by the get_value_as_int()s below.
116         */
117         _channel.update ();
118         _pitch.update ();
119         _velocity.update ();
120
121         _region_view->start_note_diff_command (_("edit note"));
122
123         bool had_change = false;
124
125         if (_channel.get_value_as_int() - 1 != _event->note()->channel()) {
126                 _region_view->change_note_channel (_event, _channel.get_value_as_int () - 1);
127                 had_change = true;
128         }
129
130         if (_pitch.get_value_as_int() != _event->note()->note()) {
131                 _region_view->change_note_note (_event, _pitch.get_value_as_int (), false);
132                 had_change = true;
133         }
134
135         if (_velocity.get_value_as_int() != _event->note()->velocity()) {
136                 _region_view->change_note_velocity (_event, _velocity.get_value_as_int (), false);
137                 had_change = true;
138         }
139
140         double const t = _region_view->source_relative_time_converter().from (_time_clock.current_time ());
141
142         if (t != _event->note()->time()) {
143                 _region_view->change_note_time (_event, t);
144                 had_change = true;
145         }
146
147         double const d = _region_view->region_relative_time_converter().from (_length_clock.current_duration ());
148
149         if (d != _event->note()->length()) {
150                 _region_view->change_note_length (_event, d);
151                 had_change = true;
152         }
153
154         if (!had_change) {
155                 _region_view->abort_command ();
156         }
157
158         _region_view->apply_diff ();
159
160         _event->set_selected (_event->selected()); // change color
161
162         return r;
163 }