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