Prevent region drags with the middle button in internal edit mode (#3869).
[ardour.git] / gtk2_ardour / audio_region_editor.cc
1 /*
2     Copyright (C) 2001 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 "pbd/memento_command.h"
21 #include "pbd/stateful_diff_command.h"
22 #include "pbd/pthread_utils.h"
23
24 #include "ardour/session.h"
25 #include "ardour/audioregion.h"
26 #include "ardour/playlist.h"
27 #include "ardour/utils.h"
28 #include "ardour/dB.h"
29 #include <gtkmm2ext/utils.h>
30 #include <cmath>
31
32 #include "audio_region_editor.h"
33 #include "audio_region_view.h"
34 #include "ardour_ui.h"
35 #include "utils.h"
36 #include "gui_thread.h"
37
38 #include "i18n.h"
39
40 using namespace ARDOUR;
41 using namespace PBD;
42 using namespace std;
43 using namespace Gtkmm2ext;
44
45 static void *
46 _peak_amplitude_thread (void* arg)
47 {
48         SessionEvent::create_per_thread_pool ("peak amplitude events", 64);
49         static_cast<AudioRegionEditor*>(arg)->peak_amplitude_thread ();
50         return 0;
51 }
52
53 AudioRegionEditor::AudioRegionEditor (Session* s, boost::shared_ptr<AudioRegion> r)
54         : RegionEditor (s, r)
55         , _audio_region (r)
56         , gain_adjustment(accurate_coefficient_to_dB(_audio_region->scale_amplitude()), -40.0, +40.0, 0.1, 1.0, 0)        
57         , _peak_amplitude_found (false)
58 {
59
60         Gtk::HBox* b = Gtk::manage (new Gtk::HBox);
61         b->set_spacing (6);
62         b->pack_start (gain_entry);
63         b->pack_start (*Gtk::manage (new Gtk::Label (_("dB"))), false, false);
64
65         gain_label.set_name ("AudioRegionEditorLabel");
66         gain_label.set_text (_("Region gain:"));
67         gain_label.set_alignment (1, 0.5);
68         gain_entry.configure (gain_adjustment, 0.0, 1);
69         _table.attach (gain_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
70         _table.attach (*b, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
71         ++_table_row;
72
73         b = Gtk::manage (new Gtk::HBox);
74         b->set_spacing (6);
75         b->pack_start (_peak_amplitude);
76         b->pack_start (*Gtk::manage (new Gtk::Label (_("dBFS"))), false, false);
77         
78         _peak_amplitude_label.set_name ("AudioRegionEditorLabel");
79         _peak_amplitude_label.set_text (_("Peak amplitude:"));
80         _peak_amplitude_label.set_alignment (1, 0.5);
81         _table.attach (_peak_amplitude_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
82         _table.attach (*b, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
83         ++_table_row;
84         
85         gain_changed ();
86
87         gain_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &AudioRegionEditor::gain_adjustment_changed));
88
89         _peak_amplitude.property_editable() = false;
90         _peak_amplitude.set_text (_("Calculating..."));
91
92         PeakAmplitudeFound.connect (_peak_amplitude_connection, invalidator (*this), boost::bind (&AudioRegionEditor::peak_amplitude_found, this, _1), gui_context ());
93         pthread_create_and_store (X_("peak-amplitude"), &_peak_amplitude_thread_handle, _peak_amplitude_thread, this);
94 }
95
96 AudioRegionEditor::~AudioRegionEditor ()
97 {
98         pthread_cancel_one (_peak_amplitude_thread_handle);
99         void* v;
100         int const r = pthread_join (_peak_amplitude_thread_handle, &v);
101         assert (r == 0);
102 }
103
104 void
105 AudioRegionEditor::region_changed (const PBD::PropertyChange& what_changed)
106 {
107         RegionEditor::region_changed (what_changed);
108         
109         if (what_changed.contains (ARDOUR::Properties::scale_amplitude)) {
110                 gain_changed ();
111         }
112 }
113 void
114 AudioRegionEditor::gain_changed ()
115 {
116         float const region_gain_dB = accurate_coefficient_to_dB (_audio_region->scale_amplitude());
117         if (region_gain_dB != gain_adjustment.get_value()) {
118                 gain_adjustment.set_value(region_gain_dB);
119         }
120 }
121
122 void
123 AudioRegionEditor::gain_adjustment_changed ()
124 {
125         float const gain = dB_to_coefficient (gain_adjustment.get_value());
126         if (_audio_region->scale_amplitude() != gain) {
127                 _audio_region->set_scale_amplitude (gain);
128         }
129 }
130
131 void
132 AudioRegionEditor::peak_amplitude_thread ()
133 {
134         PeakAmplitudeFound (accurate_coefficient_to_dB (_audio_region->maximum_amplitude ())); /* EMIT SIGNAL */
135 }
136
137 void
138 AudioRegionEditor::peak_amplitude_found (double p)
139 {
140         stringstream s;
141         s.precision (2);
142         s.setf (ios::fixed, ios::floatfield);
143         s << p;
144         _peak_amplitude.set_text (s.str ());
145 }
146