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