open un-writable sessions without complaining, and desensitize all/most actions that...
[ardour.git] / gtk2_ardour / gain_meter.cc
1 /*
2   Copyright (C) 2002 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 <limits.h>
21
22 #include <ardour/io.h>
23 #include <ardour/route.h>
24 #include <ardour/route_group.h>
25 #include <ardour/session.h>
26 #include <ardour/session_route.h>
27 #include <ardour/dB.h>
28
29 #include <gtkmm2ext/utils.h>
30 #include <gtkmm2ext/fastmeter.h>
31 #include <gtkmm2ext/stop_signal.h>
32 #include <gtkmm2ext/barcontroller.h>
33 #include <midi++/manager.h>
34 #include <pbd/fastlog.h>
35
36 #include "ardour_ui.h"
37 #include "gain_meter.h"
38 #include "utils.h"
39 #include "logmeter.h"
40 #include "gui_thread.h"
41 #include "keyboard.h"
42 #include "public_editor.h"
43
44 #include <ardour/session.h>
45 #include <ardour/route.h>
46
47 #include "i18n.h"
48
49 using namespace ARDOUR;
50 using namespace PBD;
51 using namespace Gtkmm2ext;
52 using namespace Gtk;
53 using namespace sigc;
54 using namespace std;
55
56 sigc::signal<void> GainMeterBase::ResetAllPeakDisplays;
57 sigc::signal<void,RouteGroup*> GainMeterBase::ResetGroupPeakDisplays;
58
59 map<string,Glib::RefPtr<Gdk::Pixmap> > GainMeter::metric_pixmaps;
60 Glib::RefPtr<Gdk::Pixbuf> GainMeter::slider;
61
62
63 void
64 GainMeter::setup_slider_pix ()
65 {
66         if ((slider = ::get_icon ("fader_belt")) == 0) {
67                 throw failed_constructor();
68         }
69 }
70
71 GainMeterBase::GainMeterBase (Session& s, 
72                               const Glib::RefPtr<Gdk::Pixbuf>& pix,
73                               bool horizontal)
74         : _session (s),
75           // 0.781787 is the value needed for gain to be set to 0.
76           gain_adjustment (0.781787, 0.0, 1.0, 0.01, 0.1),
77           gain_automation_style_button (""),
78           gain_automation_state_button ("")
79         
80 {
81         using namespace Menu_Helpers;
82
83         ignore_toggle = false;
84         meter_menu = 0;
85         next_release_selects = false;
86         style_changed = true;
87         _width = Wide;
88
89         if (horizontal) {
90                 gain_slider = manage (new HSliderController (pix,
91                                                              &gain_adjustment,
92                                                              false));
93         } else {
94                 gain_slider = manage (new VSliderController (pix,
95                                                              &gain_adjustment,
96                                                              false));
97         }
98
99         level_meter = new LevelMeter(_session);
100
101         gain_slider->signal_button_press_event().connect (mem_fun(*this, &GainMeter::start_gain_touch));
102         gain_slider->signal_button_release_event().connect (mem_fun(*this, &GainMeter::end_gain_touch));
103         gain_slider->set_name ("GainFader");
104
105         gain_display.set_name ("MixerStripGainDisplay");
106         gain_display.set_has_frame (false);
107         set_size_request_to_display_given_text (gain_display, "-80.g", 2, 6); /* note the descender */
108         gain_display.signal_activate().connect (mem_fun (*this, &GainMeter::gain_activated));
109         gain_display.signal_focus_in_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
110         gain_display.signal_focus_out_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
111
112         peak_display.set_name ("MixerStripPeakDisplay");
113 //      peak_display.set_has_frame (false);
114 //      peak_display.set_editable (false);
115         set_size_request_to_display_given_text  (peak_display, "-80.g", 2, 6); /* note the descender */
116         max_peak = minus_infinity();
117         peak_display.set_label (_("-inf"));
118         peak_display.unset_flags (Gtk::CAN_FOCUS);
119
120         gain_automation_style_button.set_name ("MixerAutomationModeButton");
121         gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
122
123         ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
124         ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
125
126         gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
127         gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
128
129         gain_automation_state_button.set_size_request(15, 15);
130         gain_automation_style_button.set_size_request(15, 15);
131
132         gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
133         gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
134         
135         gain_astate_menu.set_name ("ArdourContextMenu");
136         gain_astyle_menu.set_name ("ArdourContextMenu");
137
138         gain_adjustment.signal_value_changed().connect (mem_fun(*this, &GainMeterBase::gain_adjusted));
139         peak_display.signal_button_release_event().connect (mem_fun(*this, &GainMeterBase::peak_button_release), false);
140         gain_display.signal_key_press_event().connect (mem_fun(*this, &GainMeterBase::gain_key_press), false);
141
142         ResetAllPeakDisplays.connect (mem_fun(*this, &GainMeterBase::reset_peak_display));
143         ResetGroupPeakDisplays.connect (mem_fun(*this, &GainMeterBase::reset_group_peak_display));
144
145         UI::instance()->theme_changed.connect (mem_fun(*this, &GainMeterBase::on_theme_changed));
146         ColorsChanged.connect (bind(mem_fun (*this, &GainMeterBase::color_handler), false));
147         DPIReset.connect (bind(mem_fun (*this, &GainMeterBase::color_handler), true));
148
149 }
150
151 GainMeterBase::~GainMeterBase ()
152 {
153         if (meter_menu) {
154                 delete meter_menu;
155         }
156
157         if (level_meter) {
158                 delete level_meter;
159         }
160 }
161
162 void
163 GainMeterBase::set_io (boost::shared_ptr<IO> io)
164 {
165         connections.clear ();
166
167         _io = io;
168
169         level_meter->set_io (_io);
170         gain_slider->set_controllable (&_io->gain_control());
171
172         boost::shared_ptr<Route> r;
173         
174         if ((r = boost::dynamic_pointer_cast<Route> (_io)) != 0) {
175
176                 if (!r->hidden()) {
177
178                         using namespace Menu_Helpers;
179
180                         gain_astate_menu.items().clear ();
181                         
182                         gain_astate_menu.items().push_back (MenuElem (_("Manual"), 
183                                                                       bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Off)));
184                         gain_astate_menu.items().push_back (MenuElem (_("Play"),
185                                                                       bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Play)));
186                         gain_astate_menu.items().push_back (MenuElem (_("Write"),
187                                                                       bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Write)));
188                         gain_astate_menu.items().push_back (MenuElem (_("Touch"),
189                                                                       bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Touch)));
190                         
191                         connections.push_back (gain_automation_style_button.signal_button_press_event().connect (mem_fun(*this, &GainMeterBase::gain_automation_style_button_event), false));
192                         connections.push_back (gain_automation_state_button.signal_button_press_event().connect (mem_fun(*this, &GainMeterBase::gain_automation_state_button_event), false));
193                         
194                         connections.push_back (r->gain_automation_curve().automation_state_changed.connect (mem_fun(*this, &GainMeterBase::gain_automation_state_changed)));
195                         connections.push_back (r->gain_automation_curve().automation_style_changed.connect (mem_fun(*this, &GainMeterBase::gain_automation_style_changed)));
196                         
197                         gain_automation_state_changed ();
198                 }
199         }
200
201         connections.push_back (_io->gain_changed.connect (mem_fun(*this, &GainMeterBase::gain_changed)));
202
203         gain_changed (0);
204         show_gain ();
205
206         update_gain_sensitive ();
207 }       
208
209 void
210 GainMeterBase::hide_all_meters ()
211 {
212         level_meter->hide_meters();
213 }
214
215 void
216 GainMeter::hide_all_meters ()
217 {
218         bool remove_metric_area = false;
219
220         GainMeterBase::hide_all_meters ();
221
222         if (remove_metric_area) {
223                 if (meter_metric_area.get_parent()) {
224                         level_meter->remove (meter_metric_area);
225                 }
226         }
227 }
228
229 void
230 GainMeterBase::setup_meters (int len)
231 {
232         level_meter->setup_meters(len, 5);
233 }
234
235 void 
236 GainMeter::setup_meters (int len)
237 {
238         if (!meter_metric_area.get_parent()) {
239                 level_meter->pack_end (meter_metric_area, false, false);
240                 meter_metric_area.show_all ();
241         }
242         GainMeterBase::setup_meters (len);
243 }
244
245 bool
246 GainMeterBase::gain_key_press (GdkEventKey* ev)
247 {
248         if (key_is_legal_for_numeric_entry (ev->keyval)) {
249                 /* drop through to normal handling */
250                 return false;
251         }
252         /* illegal key for gain entry */
253         return true;
254 }
255
256 bool
257 GainMeterBase::peak_button_release (GdkEventButton* ev)
258 {
259         /* reset peak label */
260
261         if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) {
262                 ResetAllPeakDisplays ();
263         } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
264                 boost::shared_ptr<Route> r;
265
266                 if ((r = boost::dynamic_pointer_cast<Route> (_io)) != 0) {
267                         ResetGroupPeakDisplays (r->mix_group());
268                 }
269         } else {
270                 reset_peak_display ();
271         }
272
273         return true;
274 }
275
276 void
277 GainMeterBase::reset_peak_display ()
278 {
279         boost::shared_ptr<Route> r;
280
281         if ((r = boost::dynamic_pointer_cast<Route> (_io)) != 0) {
282                 r->reset_max_peak_meters();
283         }
284
285         level_meter->clear_meters();
286         max_peak = -INFINITY;
287         peak_display.set_label (_("-Inf"));
288         peak_display.set_name ("MixerStripPeakDisplay");
289 }
290
291 void
292 GainMeterBase::reset_group_peak_display (RouteGroup* group)
293 {
294         boost::shared_ptr<Route> r;
295         
296         if ((r = boost::dynamic_pointer_cast<Route> (_io)) != 0) {
297                 if (group == r->mix_group()) {
298                         reset_peak_display ();
299                 }
300         }
301 }
302
303 void
304 GainMeterBase::popup_meter_menu (GdkEventButton *ev)
305 {
306         using namespace Menu_Helpers;
307
308         if (meter_menu == 0) {
309                 meter_menu = new Gtk::Menu;
310                 MenuList& items = meter_menu->items();
311
312                 items.push_back (MenuElem ("-inf .. +0dBFS"));
313                 items.push_back (MenuElem ("-10dB .. +0dBFS"));
314                 items.push_back (MenuElem ("-4 .. +0dBFS"));
315                 items.push_back (SeparatorElem());
316                 items.push_back (MenuElem ("-inf .. -2dBFS"));
317                 items.push_back (MenuElem ("-10dB .. -2dBFS"));
318                 items.push_back (MenuElem ("-4 .. -2dBFS"));
319         }
320
321         meter_menu->popup (1, ev->time);
322 }
323
324 bool
325 GainMeterBase::gain_focused (GdkEventFocus* ev)
326 {
327         if (ev->in) {
328                 gain_display.select_region (0, -1);
329         } else {
330                 gain_display.select_region (0, 0);
331         }
332         return false;
333 }
334
335 void
336 GainMeterBase::gain_activated ()
337 {
338         float f;
339
340         if (sscanf (gain_display.get_text().c_str(), "%f", &f) == 1) {
341
342                 /* clamp to displayable values */
343
344                 f = min (f, 6.0f);
345
346                 _io->set_gain (dB_to_coefficient(f), this);
347
348                 if (gain_display.has_focus()) {
349                         PublicEditor::instance().reset_focus();
350                 }
351         }
352 }
353
354 void
355 GainMeterBase::show_gain ()
356 {
357         char buf[32];
358
359         float v = gain_adjustment.get_value();
360         
361         if (v == 0.0) {
362                 strcpy (buf, _("-inf"));
363         } else {
364                 snprintf (buf, 32, "%.1f", coefficient_to_dB (slider_position_to_gain (v)));
365         }
366         
367         gain_display.set_text (buf);
368 }
369
370 void
371 GainMeterBase::gain_adjusted ()
372 {
373         if (!ignore_toggle) {
374                 _io->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
375         }
376         show_gain ();
377 }
378
379 void
380 GainMeterBase::effective_gain_display ()
381 {
382         gfloat value = gain_to_slider_position (_io->effective_gain());
383
384         if (gain_adjustment.get_value() != value) {
385                 ignore_toggle = true; 
386                 gain_adjustment.set_value (value);
387                 ignore_toggle = false;
388         }
389 }
390
391 void
392 GainMeterBase::gain_changed (void *src)
393 {
394         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeterBase::effective_gain_display));
395 }
396
397 void
398 GainMeterBase::set_meter_strip_name (const char * name)
399 {
400         meter_metric_area.set_name (name);
401         style_changed = true;
402 }
403
404 void
405 GainMeterBase::set_fader_name (const char * name)
406 {
407         gain_slider->set_name (name);
408 }
409
410 void
411 GainMeterBase::update_gain_sensitive ()
412 {
413         static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (!(_io->gain_automation_state() & Play));
414 }
415
416
417 static MeterPoint
418 next_meter_point (MeterPoint mp)
419 {
420         switch (mp) {
421         case MeterInput:
422                 return MeterPreFader;
423                 break;
424                 
425         case MeterPreFader:
426                 return MeterPostFader;
427                 break;
428                 
429         case MeterPostFader:
430                 return MeterInput;
431                 break;
432         }
433         /*NOTREACHED*/
434         return MeterInput;
435 }
436
437 gint
438 GainMeterBase::meter_press(GdkEventButton* ev)
439 {
440         boost::shared_ptr<Route> _route;
441
442         wait_for_release = false;
443         
444         if ((_route = boost::dynamic_pointer_cast<Route>(_io)) == 0) {
445                 return FALSE;
446         }
447
448         if (!ignore_toggle) {
449
450                 if (Keyboard::is_context_menu_event (ev)) {
451                         
452                         // no menu at this time.
453
454                 } else {
455
456                         if (Keyboard::is_button2_event(ev)) {
457
458                                 // Primary-button2 click is the midi binding click
459                                 // button2-click is "momentary"
460                                 
461                                 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier))) {
462                                         wait_for_release = true;
463                                         old_meter_point = _route->meter_point ();
464                                 }
465                         }
466
467                         if (ev->button == 1 || Keyboard::is_button2_event (ev)) {
468
469                                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
470
471                                         /* Primary+Tertiary-click applies change to all routes */
472
473                                         _session.begin_reversible_command (_("meter point change"));
474                                         Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
475                                         _session.foreach_route (this, &GainMeterBase::set_meter_point, next_meter_point (_route->meter_point()));
476                                         cmd->mark();
477                                         _session.add_command (cmd);
478                                         _session.commit_reversible_command ();
479                                         
480                                         
481                                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
482
483                                         /* Primary-click: solo mix group.
484                                            NOTE: Primary-button2 is MIDI learn.
485                                         */
486                                         
487                                         if (ev->button == 1) {
488                                                 _session.begin_reversible_command (_("meter point change"));
489                                                 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
490                                                 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
491                                                 cmd->mark();
492                                                 _session.add_command (cmd);
493                                                 _session.commit_reversible_command ();
494                                         }
495                                         
496                                 } else {
497                                         
498                                         /* click: change just this route */
499
500                                         // XXX no undo yet
501                                         
502                                         _route->set_meter_point (next_meter_point (_route->meter_point()), this);
503                                 }
504                         }
505                 }
506         }
507
508         return true;
509
510 }
511
512 gint
513 GainMeterBase::meter_release(GdkEventButton* ev)
514 {
515         if(!ignore_toggle){
516                 if (wait_for_release){
517                         wait_for_release = false;
518                         
519                         boost::shared_ptr<Route> r;
520                         
521                         if ((r = boost::dynamic_pointer_cast<Route>(_io)) != 0) {
522                                 set_meter_point (*r, old_meter_point);
523                         }
524                 }
525         }
526
527         return true;
528 }
529
530 void
531 GainMeterBase::set_meter_point (Route& route, MeterPoint mp)
532 {
533         route.set_meter_point (mp, this);
534 }
535
536 void
537 GainMeterBase::set_mix_group_meter_point (Route& route, MeterPoint mp)
538 {
539         RouteGroup* mix_group;
540
541         if((mix_group = route.mix_group()) != 0){
542                 mix_group->apply (&Route::set_meter_point, mp, this);
543         } else {
544                 route.set_meter_point (mp, this);
545         }
546 }
547
548 void
549 GainMeterBase::meter_point_clicked ()
550 {
551         boost::shared_ptr<Route> r;
552
553         if ((r = boost::dynamic_pointer_cast<Route> (_io)) != 0) {
554                 /* WHAT? */
555         }
556 }
557
558 gint
559 GainMeterBase::start_gain_touch (GdkEventButton* ev)
560 {
561         _io->start_gain_touch ();
562         return FALSE;
563 }
564
565 gint
566 GainMeterBase::end_gain_touch (GdkEventButton* ev)
567 {
568         _io->end_gain_touch ();
569         return FALSE;
570 }
571
572 gint
573 GainMeterBase::gain_automation_state_button_event (GdkEventButton *ev)
574 {
575         if (ev->type == GDK_BUTTON_RELEASE) {
576                 return TRUE;
577         }
578         
579         switch (ev->button) {
580                 case 1:
581                         gain_astate_menu.popup (1, ev->time);
582                         break;
583                 default:
584                         break;
585         }
586
587         return TRUE;
588 }
589
590 gint
591 GainMeterBase::gain_automation_style_button_event (GdkEventButton *ev)
592 {
593         if (ev->type == GDK_BUTTON_RELEASE) {
594                 return TRUE;
595         }
596
597         switch (ev->button) {
598         case 1:
599                 gain_astyle_menu.popup (1, ev->time);
600                 break;
601         default:
602                 break;
603         }
604         return TRUE;
605 }
606
607 string
608 GainMeterBase::astate_string (AutoState state)
609 {
610         return _astate_string (state, false);
611 }
612
613 string
614 GainMeterBase::short_astate_string (AutoState state)
615 {
616         return _astate_string (state, true);
617 }
618
619 string
620 GainMeterBase::_astate_string (AutoState state, bool shrt)
621 {
622         string sstr;
623
624         switch (state) {
625         case Off:
626                 sstr = (shrt ? "M" : _("M"));
627                 break;
628         case Play:
629                 sstr = (shrt ? "P" : _("P"));
630                 break;
631         case Touch:
632                 sstr = (shrt ? "T" : _("T"));
633                 break;
634         case Write:
635                 sstr = (shrt ? "W" : _("W"));
636                 break;
637         }
638
639         return sstr;
640 }
641
642 string
643 GainMeterBase::astyle_string (AutoStyle style)
644 {
645         return _astyle_string (style, false);
646 }
647
648 string
649 GainMeterBase::short_astyle_string (AutoStyle style)
650 {
651         return _astyle_string (style, true);
652 }
653
654 string
655 GainMeterBase::_astyle_string (AutoStyle style, bool shrt)
656 {
657         if (style & Trim) {
658                 return _("Trim");
659         } else {
660                 /* XXX it might different in different languages */
661
662                 return (shrt ? _("Abs") : _("Abs"));
663         }
664 }
665
666 void
667 GainMeterBase::gain_automation_style_changed ()
668 {
669         switch (_width) {
670         case Wide:
671                 gain_automation_style_button.set_label (astyle_string(_io->gain_automation_curve().automation_style()));
672                 break;
673         case Narrow:
674                 gain_automation_style_button.set_label  (short_astyle_string(_io->gain_automation_curve().automation_style()));
675                 break;
676         }
677 }
678
679 void
680 GainMeterBase::gain_automation_state_changed ()
681 {
682         ENSURE_GUI_THREAD(mem_fun(*this, &GainMeterBase::gain_automation_state_changed));
683         
684         bool x;
685
686         switch (_width) {
687         case Wide:
688                 gain_automation_state_button.set_label (astate_string(_io->gain_automation_curve().automation_state()));
689                 break;
690         case Narrow:
691                 gain_automation_state_button.set_label (short_astate_string(_io->gain_automation_curve().automation_state()));
692                 break;
693         }
694
695         x = (_io->gain_automation_state() != Off);
696         
697         if (gain_automation_state_button.get_active() != x) {
698                 ignore_toggle = true;
699                 gain_automation_state_button.set_active (x);
700                 ignore_toggle = false;
701         }
702
703         update_gain_sensitive ();
704         
705         /* start watching automation so that things move */
706         
707         gain_watching.disconnect();
708
709         if (x) {
710                 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeterBase::effective_gain_display));
711         }
712 }
713
714 void
715 GainMeterBase::update_meters()
716 {
717         char buf[32];
718         float mpeak = level_meter->update_meters();
719
720         if (mpeak > max_peak) {
721                 max_peak = mpeak;
722                 if (mpeak <= -200.0f) {
723                         peak_display.set_label (_("-inf"));
724                 } else {
725                         snprintf (buf, sizeof(buf), "%.1f", mpeak);
726                         peak_display.set_label (buf);
727                 }
728
729                 if (mpeak >= 0.0f) {
730                         peak_display.set_name ("MixerStripPeakDisplayPeak");
731                 }
732         }
733 }
734
735 void GainMeterBase::color_handler(bool dpi)
736 {
737         color_changed = true;
738         dpi_changed = (dpi) ? true : false;
739         setup_meters();
740 }
741
742 void
743 GainMeterBase::set_width (Width w, int len)
744 {
745         _width = w;
746         level_meter->setup_meters (len);
747 }
748
749
750 void
751 GainMeterBase::on_theme_changed()
752 {
753         style_changed = true;
754 }
755
756 GainMeter::GainMeter (Session& s)
757         : GainMeterBase (s, slider, false)
758 {
759         gain_display_box.set_homogeneous (true);
760         gain_display_box.set_spacing (2);
761         gain_display_box.pack_start (gain_display, true, true);
762
763         meter_metric_area.set_name ("AudioTrackMetrics");
764         set_size_request_to_display_given_text (meter_metric_area, "-50", 0, 0);
765
766         gain_automation_style_button.set_name ("MixerAutomationModeButton");
767         gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
768
769         ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
770         ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
771
772         gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
773         gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
774
775         gain_automation_state_button.set_size_request(15, 15);
776         gain_automation_style_button.set_size_request(15, 15);
777
778         HBox* fader_centering_box = manage (new HBox);
779         fader_centering_box->pack_start (*gain_slider, true, false);
780
781         fader_vbox = manage (new Gtk::VBox());
782         fader_vbox->set_spacing (0);
783         fader_vbox->pack_start (*fader_centering_box, false, false, 0);
784
785         hbox.set_spacing (2);
786         hbox.pack_start (*fader_vbox, true, true);
787
788         set_spacing (2);
789
790         pack_start (gain_display_box, Gtk::PACK_SHRINK);
791         pack_start (hbox, Gtk::PACK_SHRINK);
792
793         meter_metric_area.signal_expose_event().connect (mem_fun(*this, &GainMeter::meter_metrics_expose));
794 }
795
796 void
797 GainMeter::set_io (boost::shared_ptr<IO> io)
798 {
799         if (level_meter->get_parent()) {
800                 hbox.remove (*level_meter);
801         }
802
803         if (peak_display.get_parent()) {
804                 gain_display_box.remove (peak_display);
805         }
806
807         if (gain_automation_state_button.get_parent()) {
808                 fader_vbox->remove (gain_automation_state_button);
809         }
810
811         GainMeterBase::set_io (io);
812
813         boost::shared_ptr<Route> r;
814
815         if ((r = boost::dynamic_pointer_cast<Route> (_io)) != 0) {
816                 
817                 /* 
818                    if we have a non-hidden route (ie. we're not the click or the auditioner), 
819                    pack some route-dependent stuff.
820                 */
821
822                 gain_display_box.pack_end (peak_display, true, true);
823                 hbox.pack_end (*level_meter, true, true);
824
825                 if (!r->hidden()) {
826                         fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
827                 }
828         }
829 }
830
831 int
832 GainMeter::get_gm_width ()
833 {
834         Gtk::Requisition sz;
835         hbox.size_request (sz);
836         return sz.width;
837 }
838
839 Glib::RefPtr<Gdk::Pixmap>
840 GainMeter::render_metrics (Gtk::Widget& w)
841 {
842         Glib::RefPtr<Gdk::Window> win (w.get_window());
843         Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
844         Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
845         gint width, height;
846         int  db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
847         char buf[32];
848
849         win->get_size (width, height);
850         
851         Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
852
853         metric_pixmaps[w.get_name()] = pixmap;
854
855         pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
856
857         Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
858
859         for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
860
861                 float fraction = log_meter (db_points[i]);
862                 gint pos = height - (gint) floor (height * fraction);
863
864                 snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
865
866                 layout->set_text (buf);
867
868                 /* we want logical extents, not ink extents here */
869
870                 int width, height;
871                 layout->get_pixel_size (width, height);
872
873                 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
874                 pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
875         }
876
877         return pixmap;
878 }
879
880 gint
881 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
882 {
883         static Glib::RefPtr<Gtk::Style> meter_style;
884         if (style_changed) {
885                 meter_style = meter_metric_area.get_style();
886         }
887         Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
888         Glib::RefPtr<Gdk::GC> bg_gc (meter_style->get_bg_gc (Gtk::STATE_INSENSITIVE));
889         GdkRectangle base_rect;
890         GdkRectangle draw_rect;
891         gint width, height;
892
893         win->get_size (width, height);
894         
895         base_rect.width = width;
896         base_rect.height = height;
897         base_rect.x = 0;
898         base_rect.y = 0;
899
900         Glib::RefPtr<Gdk::Pixmap> pixmap;
901         std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
902
903         if (i == metric_pixmaps.end() || style_changed || dpi_changed) {
904                 pixmap = render_metrics (meter_metric_area);
905         } else {
906                 pixmap = i->second;
907         }
908
909         gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
910         win->draw_drawable (bg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
911         style_changed = false;
912         return true;
913 }
914