1 /* abGate - LV2 Noise Gate Plugin
2  *
3  * Copyright 2011 Antanas Bružas
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 3 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free
17  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #include "toggle.h"
21 
22 using namespace std;
23 
toggle(const sigc::slot<void> toggle_slot)24 toggle::toggle(const sigc::slot<void> toggle_slot) :
25 		a_tog(new Adjustment(0, 0, 1, 1, 1, 0)) {
26 
27 	set_events(Gdk::EXPOSURE_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
28 	set_double_buffered(true);
29 
30 	// Setting bypass images
31 	on = Gdk::Pixbuf::create_from_file(BYPASS_ON_PNG);
32 	off = Gdk::Pixbuf::create_from_file(BYPASS_OFF_PNG);
33 	pixbuf = off;
34 
35 	connecting(a_tog, toggle_slot);
36 	set_size_request(100, 100);
37 }
38 
~toggle()39 toggle::~toggle() {
40 }
41 
connecting(Adjustment * tog,const sigc::slot<void> slot)42 void toggle::connecting(Adjustment* tog, const sigc::slot<void> slot) {
43 	a_tog = tog;
44 
45 	// Triggering value_changed method when value of the toggle changes
46 	a_tog->signal_value_changed().connect(mem_fun(*this, &toggle::value_changed));
47 
48 	// Updating LV2 port value when value of bypass changes
49 	a_tog->signal_value_changed().connect(slot);
50 }
51 
value_changed()52 void toggle::value_changed() {
53 
54 	// Setting bypass value to either 1 or 0
55 	float value = a_tog->get_value() > 0 ? 1 : 0;
56 
57 	// Changing the image of the toggle
58 	pixbuf = value > 0 ? on : off;
59 
60 	// Also can be used like this: dis_sig.emit()
61 	dis_sig();
62 }
63 
on_expose_event(GdkEventExpose * event)64 bool toggle::on_expose_event(GdkEventExpose *event) {
65 
66 	if (event && pixbuf) {
67 		// Drawing new pixbuf
68 		m_drawable = get_window();
69 		m_drawable->draw_pixbuf(get_style()->get_black_gc(), pixbuf, get_width(), -1, 0, 0, get_width(), get_height(), Gdk::RGB_DITHER_NONE, 0, 0);
70 		// This one is deprecated: pixbuf->render_to_drawable(get_window(), get_style()->get_black_gc(), get_width(), -1, 0, 0, get_width(), get_height(), Gdk::RGB_DITHER_NONE, 0, 0);
71 	}
72 	return true;
73 }
74 
on_button_press_event(GdkEventButton * event)75 bool toggle::on_button_press_event(GdkEventButton *event) {
76 
77 	// Updating bypass value
78 	float value = a_tog->get_value() > 0 ? 0 : 1;
79 	a_tog->set_value(value);
80 	return true;
81 }
82 
on_button_release_event(GdkEventButton * event)83 bool toggle::on_button_release_event(GdkEventButton *event) {
84 	if (has_grab()) {
85 		remove_modal_grab();
86 	}
87 	return true;
88 }
89 
90 // Returns bypass value
get_toggle_value()91 float toggle::get_toggle_value() {
92 	return a_tog->get_value();
93 }
94 
95 // Sets bypass value
set_toggle_value(float value)96 void toggle::set_toggle_value(float value) {
97 	a_tog->set_value(value);
98 }
99