Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/yabause/src/gtk/yuiinputentry.c
2 views
1
/* Copyright 2006 Guillaume Duhamel
2
Copyright 2005-2006 Fabien Coulon
3
4
This file is part of Yabause.
5
6
Yabause is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
11
Yabause is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
15
16
You should have received a copy of the GNU General Public License
17
along with Yabause; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
*/
20
21
#include <gtk/gtksignal.h>
22
#include <gtk/gtktable.h>
23
#include <gtk/gtklabel.h>
24
#include <gtk/gtkentry.h>
25
#include <glib/gprintf.h>
26
#include "yuiinputentry.h"
27
#include "settings.h"
28
29
static void yui_input_entry_class_init (YuiInputEntryClass *klass);
30
static void yui_input_entry_init (YuiInputEntry *yie);
31
gboolean yui_input_entry_keypress(GtkWidget * widget, GdkEventKey * event, gpointer name);
32
gboolean yui_input_entry_focus_in(GtkWidget * widget, GdkEventFocus * event, gpointer user_data);
33
gboolean yui_input_entry_focus_out(GtkWidget * widget, GdkEventFocus * event, gpointer user_data);
34
35
GType yui_input_entry_get_type (void) {
36
static GType yie_type = 0;
37
38
if (!yie_type) {
39
static const GTypeInfo yie_info = {
40
sizeof (YuiInputEntryClass),
41
NULL, /* base_init */
42
NULL, /* base_finalize */
43
(GClassInitFunc) yui_input_entry_class_init,
44
NULL, /* class_finalize */
45
NULL, /* class_data */
46
sizeof (YuiInputEntry),
47
0,
48
(GInstanceInitFunc) yui_input_entry_init,
49
NULL,
50
};
51
52
yie_type = g_type_register_static (GTK_TYPE_TABLE, "YuiInputEntry", &yie_info, 0);
53
}
54
55
return yie_type;
56
}
57
58
#define PROP_KEYFILE 1
59
#define PROP_GROUP 2
60
61
static void yui_input_entry_set_property(GObject * object, guint property_id,
62
const GValue * value, GParamSpec * pspec) {
63
switch(property_id) {
64
case PROP_KEYFILE:
65
YUI_INPUT_ENTRY(object)->keyfile = g_value_get_pointer(value);
66
break;
67
case PROP_GROUP:
68
YUI_INPUT_ENTRY(object)->group = g_value_get_pointer(value);
69
break;
70
}
71
}
72
73
static void yui_input_entry_get_property(GObject * object, guint property_id,
74
GValue * value, GParamSpec * pspec) {
75
}
76
77
static void yui_input_entry_class_init (YuiInputEntryClass *klass) {
78
GParamSpec * param;
79
80
G_OBJECT_CLASS(klass)->set_property = yui_input_entry_set_property;
81
G_OBJECT_CLASS(klass)->get_property = yui_input_entry_get_property;
82
83
param = g_param_spec_pointer("key-file", 0, 0, G_PARAM_READABLE | G_PARAM_WRITABLE);
84
g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_KEYFILE, param);
85
86
param = g_param_spec_pointer("group", 0, 0, G_PARAM_READABLE | G_PARAM_WRITABLE);
87
g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_GROUP, param);
88
}
89
90
static void yui_input_entry_init(YuiInputEntry *yie) {
91
gtk_container_set_border_width(GTK_CONTAINER(yie), 0);
92
gtk_table_set_row_spacings(GTK_TABLE(yie), 10);
93
gtk_table_set_col_spacings(GTK_TABLE(yie), 10);
94
}
95
96
GtkWidget* yui_input_entry_new(GKeyFile * keyfile, const gchar * group, const gchar * keys[]) {
97
GtkWidget * widget;
98
GtkWidget * label;
99
GtkWidget * entry;
100
guint keyName;
101
int row = 0;
102
103
widget = GTK_WIDGET(g_object_new(yui_input_entry_get_type(), "key-file", keyfile, "group", group, NULL));
104
105
while(keys[row]) {
106
char tmp[100];
107
gtk_table_resize(GTK_TABLE(widget), row + 1, 2);
108
label = gtk_label_new(keys[row]);
109
110
gtk_table_attach(GTK_TABLE(widget), label, 0, 1, row , row + 1,
111
(GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
112
gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
113
114
entry = gtk_entry_new ();
115
gtk_entry_set_width_chars(GTK_ENTRY(entry), 10);
116
117
sprintf(tmp, "%s.%s.1", group, keys[row]);
118
keyName = g_key_file_get_integer(keyfile, PERCore->Name, tmp, 0);
119
if (keyName > 0) {
120
char buffer[50];
121
#ifdef PERKEYNAME
122
PERCore->KeyName(keyName, buffer, 50);
123
#else
124
sprintf(buffer, "%x", keyName);
125
#endif
126
gtk_entry_set_text(GTK_ENTRY(entry), buffer);
127
}
128
129
if (PERCore) {
130
//if (PERCore->canScan)
131
g_signal_connect(entry, "focus-in-event", G_CALLBACK(yui_input_entry_focus_in), (gpointer) keys[row]);
132
g_signal_connect(entry, "focus-out-event", G_CALLBACK(yui_input_entry_focus_out), NULL);
133
//else
134
g_signal_connect(entry, "key-press-event", G_CALLBACK(yui_input_entry_keypress), (gpointer) keys[row]);
135
} else {
136
gtk_widget_set_sensitive(entry, FALSE);
137
}
138
139
gtk_table_attach(GTK_TABLE(widget), entry, 1, 2, row, row + 1,
140
(GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0), 0, 0);
141
row++;
142
}
143
144
return widget;
145
}
146
147
gboolean yui_input_entry_keypress(GtkWidget * widget, GdkEventKey * event, gpointer name) {
148
char tmp[100];
149
150
if (PERCore->canScan) return FALSE;
151
152
#ifdef PERKEYNAME
153
PERCore->KeyName(event->keyval, tmp, 100);
154
#else
155
sprintf(tmp, "%x", event->keyval);
156
#endif
157
gtk_entry_set_text(GTK_ENTRY(widget), tmp);
158
sprintf(tmp, "%s.%s.1", YUI_INPUT_ENTRY(gtk_widget_get_parent(widget))->group, (char *)name);
159
g_key_file_set_integer(YUI_INPUT_ENTRY(gtk_widget_get_parent(widget))->keyfile,
160
PERCore->Name, tmp, event->keyval);
161
162
return TRUE;
163
}
164
165
gboolean is_watching = FALSE;
166
GtkEntry * entry_hack = NULL;
167
168
static gboolean watch_joy(gpointer name) {
169
u32 i;
170
171
if (! is_watching) return TRUE;
172
173
if (! PERCore->canScan) {
174
is_watching = FALSE;
175
return TRUE;
176
}
177
178
i = PERCore->Scan();
179
180
if (i == 0) {
181
return TRUE;
182
} else {
183
char tmp[100];
184
185
sprintf(tmp, "Pad.%s.1", (char *)name); // should be group.name
186
g_key_file_set_integer(keyfile, PERCore->Name, tmp, i);
187
#ifdef PERKEYNAME
188
PERCore->KeyName(i, tmp, 100);
189
#else
190
sprintf(tmp, "%x", (int)i);
191
#endif
192
gtk_entry_set_text(entry_hack, tmp);
193
is_watching = FALSE;
194
return FALSE;
195
}
196
}
197
198
gboolean yui_input_entry_focus_in(GtkWidget * widget, GdkEventFocus * event, gpointer name) {
199
if (! PERCore->canScan) return TRUE;
200
201
PERCore->Flush();
202
entry_hack = GTK_ENTRY(widget);
203
204
if (!is_watching) {
205
g_timeout_add(100, watch_joy, name);
206
is_watching = TRUE;
207
}
208
209
return FALSE;
210
}
211
212
gboolean yui_input_entry_focus_out(GtkWidget * widget, GdkEventFocus * event, gpointer name) {
213
is_watching = FALSE;
214
215
return FALSE;
216
}
217
218
void yui_input_entry_update(YuiInputEntry * yie) {
219
GList * wlist = gtk_container_get_children(GTK_CONTAINER(yie));
220
u32 key;
221
GtkEntry * entry = NULL;
222
char tmp[100];
223
224
while(wlist) {
225
if (strcmp(gtk_widget_get_name(wlist->data), "GtkEntry") == 0) {
226
entry = wlist->data;
227
}
228
if (strcmp(gtk_widget_get_name(wlist->data), "GtkLabel") == 0) {
229
sprintf(tmp, "%s.%s.1", yie->group, gtk_label_get_text(wlist->data));
230
key = g_key_file_get_integer(yie->keyfile, PERCore->Name, tmp, 0);
231
if (key > 0) {
232
#ifdef PERKEYNAME
233
PERCore->KeyName(key, tmp, 100);
234
#else
235
sprintf(tmp, "%x", (int)key);
236
#endif
237
gtk_entry_set_text(entry, tmp);
238
} else {
239
gtk_entry_set_text(entry, "");
240
}
241
}
242
wlist = g_list_next(wlist);
243
}
244
}
245
246