1 /* -----------------------------------------------------------------------------
2  *
3  * Giada - Your Hardcore Loopmachine
4  *
5  * -----------------------------------------------------------------------------
6  *
7  * Copyright (C) 2010-2020 Giovanni A. Zuliani | Monocasual
8  *
9  * This file is part of Giada - Your Hardcore Loopmachine.
10  *
11  * Giada - Your Hardcore Loopmachine is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation, either
14  * version 3 of the License, or (at your option) any later version.
15  *
16  * Giada - Your Hardcore Loopmachine is distributed in the hope that it
17  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
18  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with Giada - Your Hardcore Loopmachine. If not, see
23  * <http://www.gnu.org/licenses/>.
24  *
25  * -------------------------------------------------------------------------- */
26 
27 
28 #include <cassert>
29 #include "utils/gui.h"
30 #include "utils/string.h"
31 #include "glue/io.h"
32 #include "core/model/model.h"
33 #include "core/conf.h"
34 #include "utils/log.h"
35 #include "gui/elems/basics/box.h"
36 #include "gui/elems/mainWindow/keyboard/keyboard.h"
37 #include "gui/elems/mainWindow/keyboard/channel.h"
38 #include "gui/elems/mainWindow/keyboard/channelButton.h"
39 #include "keyGrabber.h"
40 #include "config.h"
41 #include "mainWindow.h"
42 
43 
44 extern giada::v::gdMainWindow* mainWin;
45 
46 
47 namespace giada {
48 namespace v
49 {
gdKeyGrabber(const c::channel::Data & d)50 gdKeyGrabber::gdKeyGrabber(const c::channel::Data& d)
51 : gdWindow(300, 126, "Key configuration")
52 , m_data  (d)
53 {
54 	begin();
55 	m_text   = new geBox(8, 8, 284, 80, "");
56 	m_clear  = new geButton(w()-88, m_text->y()+m_text->h()+8, 80, 20, "Clear");
57 	m_cancel = new geButton(m_clear->x()-88, m_clear->y(), 80, 20, "Close");
58 	end();
59 
60 	m_clear->callback(cb_clear, (void*)this);
61 	m_cancel->callback(cb_cancel, (void*)this);
62 
63 	updateText(m_data.key);
64 
65 	u::gui::setFavicon(this);
66 	set_modal();
67 	show();
68 }
69 
70 
71 /* -------------------------------------------------------------------------- */
72 
73 
rebuild()74 void gdKeyGrabber::rebuild()
75 {
76 	updateText(m_data.key);
77 }
78 
79 
80 /* -------------------------------------------------------------------------- */
81 
82 
cb_clear(Fl_Widget *,void * p)83 void gdKeyGrabber::cb_clear (Fl_Widget* /*w*/, void* p) { ((gdKeyGrabber*)p)->cb_clear(); }
cb_cancel(Fl_Widget *,void * p)84 void gdKeyGrabber::cb_cancel(Fl_Widget* /*w*/, void* p) { ((gdKeyGrabber*)p)->cb_cancel(); }
85 
86 
87 /* -------------------------------------------------------------------------- */
88 
89 
cb_cancel()90 void gdKeyGrabber::cb_cancel()
91 {
92 	do_callback();
93 }
94 
95 
96 /* -------------------------------------------------------------------------- */
97 
98 
cb_clear()99 void gdKeyGrabber::cb_clear()
100 {
101 	updateText(0);
102 	setButtonLabel(0);
103 }
104 
105 
106 /* -------------------------------------------------------------------------- */
107 
108 
setButtonLabel(int key)109 void gdKeyGrabber::setButtonLabel(int key)
110 {
111 	c::io::channel_setKey(m_data.id, key);
112 }
113 
114 /* -------------------------------------------------------------------------- */
115 
116 
updateText(int key)117 void gdKeyGrabber::updateText(int key)
118 {
119 	std::string tmp = "Press a key.\n\nCurrent binding: ";
120 	if (key != 0)
121 		tmp += static_cast<wchar_t>(key);
122 	else
123 		tmp += "[none]";
124 	m_text->copy_label(tmp.c_str());
125 }
126 
127 
128 /* -------------------------------------------------------------------------- */
129 
130 
handle(int e)131 int gdKeyGrabber::handle(int e)
132 {
133 	int ret = Fl_Group::handle(e);
134 	switch(e) {
135 		case FL_KEYUP: {
136 			int x = Fl::event_key();
137 			if (strlen(Fl::event_text()) != 0
138 			    && x != FL_BackSpace
139 			    && x != FL_Enter
140 			    && x != FL_Delete
141 			    && x != FL_Tab
142 			    && x != FL_End
143 			    && x != ' ')
144 			{
145 				u::log::print("set key '%c' (%d) for channel ID=%d\n", x, x, m_data.id);
146 				setButtonLabel(x);
147 				updateText(x);
148 				break;
149 			}
150 			else
151 				u::log::print("invalid key\n");
152 		}
153 	}
154 	return(ret);
155 }
156 
157 }} // giada::v::
158