Fix ExportFormatSpecification copy-c'tor
[ardour.git] / gtk2_ardour / latency_gui.cc
1 /*
2     Copyright (C) 2009 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 <inttypes.h>
21 #include <iomanip>
22
23 #include <gtkmm/stock.h>
24
25 #include "pbd/convert.h"
26 #include "pbd/error.h"
27 #include "ardour/latent.h"
28
29 #include "gtkmm2ext/utils.h"
30
31 #include "latency_gui.h"
32
33 #include "pbd/i18n.h"
34
35 using namespace PBD;
36 using namespace Gtk;
37 using namespace Gtkmm2ext;
38 using namespace ARDOUR;
39
40
41 static const gchar *_unit_strings[] = {
42         N_("sample"),
43         N_("msec"),
44         N_("period"),
45         0
46 };
47
48 std::vector<std::string> LatencyGUI::unit_strings;
49
50 std::string
51 LatencyBarController::get_label (double&)
52 {
53         double const nframes = _latency_gui->adjustment.get_value();
54         std::stringstream s;
55
56         if (nframes < (_latency_gui->sample_rate / 1000.0)) {
57                 const samplepos_t nf = (samplepos_t) rint (nframes);
58                 s << string_compose (P_("%1 sample", "%1 samples", nf), nf);
59         } else {
60                 s << std::fixed << std::setprecision (2) << (nframes / (_latency_gui->sample_rate / 1000.0)) << " ms";
61         }
62
63         return s.str ();
64 }
65
66 LatencyGUI::LatencyGUI (Latent& l, samplepos_t sr, samplepos_t psz)
67         : _latent (l),
68           initial_value (_latent.user_latency()),
69           sample_rate (sr),
70           period_size (psz),
71           ignored (new PBD::IgnorableControllable()),
72           /* max 1 second, step by samples, page by msecs */
73           adjustment (initial_value, 0.0, sample_rate, 1.0, sample_rate / 1000.0f),
74           bc (adjustment, this),
75           reset_button (_("Reset"))
76 {
77         Widget* w;
78
79         if (unit_strings.empty()) {
80                 unit_strings = I18N (_unit_strings);
81         }
82
83         set_popdown_strings (units_combo, unit_strings);
84         units_combo.set_active_text (unit_strings.front());
85
86         w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
87         w->show ();
88         plus_button.add (*w);
89         w = manage (new Image (Stock::REMOVE, ICON_SIZE_BUTTON));
90         w->show ();
91         minus_button.add (*w);
92
93         hbox1.pack_start (bc, true, true);
94
95         hbox2.set_homogeneous (false);
96         hbox2.set_spacing (12);
97         hbox2.pack_start (reset_button);
98         hbox2.pack_start (minus_button);
99         hbox2.pack_start (plus_button);
100         hbox2.pack_start (units_combo, true, true);
101
102         minus_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*this, &LatencyGUI::change_latency_from_button), -1));
103         plus_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*this, &LatencyGUI::change_latency_from_button), 1));
104         reset_button.signal_clicked().connect (sigc::mem_fun (*this, &LatencyGUI::reset));
105
106         adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &LatencyGUI::finish));
107
108         bc.set_size_request (-1, 25);
109         bc.set_name (X_("ProcessorControlSlider"));
110
111         set_spacing (12);
112         pack_start (hbox1, true, true);
113         pack_start (hbox2, true, true);
114 }
115
116 void
117 LatencyGUI::finish ()
118 {
119         samplepos_t new_value = (samplepos_t) adjustment.get_value();
120         if (new_value != initial_value) {
121                 _latent.set_user_latency (new_value);
122         }
123 }
124
125 void
126 LatencyGUI::reset ()
127 {
128         _latent.set_user_latency (0);
129         adjustment.set_value (initial_value);
130 }
131
132 void
133 LatencyGUI::refresh ()
134 {
135         initial_value = _latent.signal_latency();
136         adjustment.set_value (initial_value);
137 }
138
139 void
140 LatencyGUI::change_latency_from_button (int dir)
141 {
142         std::string unitstr = units_combo.get_active_text();
143         double shift = 0.0;
144
145         if (unitstr == unit_strings[0]) {
146                 shift = 1;
147         } else if (unitstr == unit_strings[1]) {
148                 shift = (sample_rate / 1000.0);
149         } else if (unitstr == unit_strings[2]) {
150                 shift = period_size;
151         } else {
152                 fatal << string_compose (_("programming error: %1 (%2)"), X_("illegal string in latency GUI units combo"), unitstr)
153                       << endmsg;
154                 abort(); /*NOTREACHED*/
155         }
156
157         if (dir > 0) {
158                 adjustment.set_value (adjustment.get_value() + shift);
159         } else {
160                 adjustment.set_value (adjustment.get_value() - shift);
161         }
162 }
163
164 LatencyDialog::LatencyDialog (const std::string& title, Latent& l, samplepos_t sr, samplepos_t psz)
165         : ArdourDialog (title, false, true),
166           lwidget (l, sr, psz)
167 {
168         get_vbox()->pack_start (lwidget);
169         add_button (Stock::CLOSE, RESPONSE_CLOSE);
170
171         show_all ();
172         run ();
173 }
174
175