Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/yabause/src/gtk/yuifileentry.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/gtk.h>
22
23
#include "yuifileentry.h"
24
25
static void yui_file_entry_class_init (YuiFileEntryClass * klass);
26
static void yui_file_entry_init (YuiFileEntry * yfe);
27
static void yui_file_entry_browse (GtkWidget * widget, gpointer user_data);
28
static void yui_file_entry_changed (GtkWidget * widget, YuiFileEntry * yfe);
29
30
GType yui_file_entry_get_type (void) {
31
static GType yfe_type = 0;
32
33
if (!yfe_type)
34
{
35
static const GTypeInfo yfe_info =
36
{
37
sizeof (YuiFileEntryClass),
38
NULL, /* base_init */
39
NULL, /* base_finalize */
40
(GClassInitFunc) yui_file_entry_class_init,
41
NULL, /* class_finalize */
42
NULL, /* class_data */
43
sizeof (YuiFileEntry),
44
0,
45
(GInstanceInitFunc) yui_file_entry_init,
46
NULL,
47
};
48
49
yfe_type = g_type_register_static(GTK_TYPE_HBOX, "YuiFileEntry", &yfe_info, 0);
50
}
51
52
return yfe_type;
53
}
54
55
#define PROP_KEYFILE 1
56
#define PROP_GROUP 2
57
#define PROP_KEY 3
58
59
static void yui_file_entry_set_property(GObject * object, guint property_id,
60
const GValue * value, GParamSpec * pspec) {
61
switch(property_id) {
62
case PROP_KEYFILE:
63
YUI_FILE_ENTRY(object)->keyfile = g_value_get_pointer(value);
64
break;
65
case PROP_GROUP:
66
YUI_FILE_ENTRY(object)->group = g_value_get_pointer(value);
67
break;
68
case PROP_KEY:
69
YUI_FILE_ENTRY(object)->key = g_value_get_pointer(value);
70
break;
71
}
72
}
73
74
static void yui_file_entry_get_property(GObject * object, guint property_id,
75
GValue * value, GParamSpec * pspec) {
76
}
77
78
static void yui_file_entry_class_init (YuiFileEntryClass * klass) {
79
GParamSpec * param;
80
81
G_OBJECT_CLASS(klass)->set_property = yui_file_entry_set_property;
82
G_OBJECT_CLASS(klass)->get_property = yui_file_entry_get_property;
83
84
param = g_param_spec_pointer("key-file", 0, 0, G_PARAM_READABLE | G_PARAM_WRITABLE);
85
g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_KEYFILE, param);
86
87
param = g_param_spec_pointer("group", 0, 0, G_PARAM_READABLE | G_PARAM_WRITABLE);
88
g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_GROUP, param);
89
90
param = g_param_spec_pointer("key", 0, 0, G_PARAM_READABLE | G_PARAM_WRITABLE);
91
g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_KEY, param);
92
}
93
94
static void yui_file_entry_init (YuiFileEntry * yfe) {
95
gtk_container_set_border_width(GTK_CONTAINER(yfe), 10);
96
}
97
98
GtkWidget * yui_file_entry_new(GKeyFile * keyfile, const gchar * group, const gchar * key, gint flags, const gchar * label) {
99
GtkWidget * entry;
100
YuiFileEntry * yfe;
101
gchar * entryText;
102
103
entry = GTK_WIDGET(g_object_new(yui_file_entry_get_type(), "spacing", 10,
104
"key-file", keyfile, "group", group, "key", key, NULL));
105
yfe = YUI_FILE_ENTRY(entry);
106
107
yfe->flags = flags;
108
109
if (label) {
110
gtk_box_pack_start(GTK_BOX(yfe), gtk_label_new_with_mnemonic(label), FALSE, FALSE, 0);
111
}
112
113
yfe->entry = gtk_entry_new ();
114
gtk_box_pack_start(GTK_BOX(yfe), yfe->entry, TRUE, TRUE, 0);
115
116
if (flags & YUI_FILE_ENTRY_BROWSE) {
117
yfe->button = gtk_button_new_with_mnemonic ("Browse");
118
g_signal_connect(yfe->button, "clicked", G_CALLBACK(yui_file_entry_browse), yfe);
119
gtk_box_pack_start(GTK_BOX(yfe), yfe->button, FALSE, FALSE, 0);
120
}
121
122
entryText = g_key_file_get_value(yfe->keyfile, yfe->group, yfe->key, 0);
123
if ( !entryText ) entryText = "";
124
gtk_entry_set_text(GTK_ENTRY(yfe->entry), entryText );
125
g_signal_connect(GTK_ENTRY(yfe->entry), "changed", G_CALLBACK(yui_file_entry_changed), yfe);
126
127
return entry;
128
}
129
130
static void yui_file_entry_browse(GtkWidget * widget, gpointer user_data) {
131
GtkWidget * file_selector;
132
gint result;
133
const gchar * filename;
134
YuiFileEntry * yfe = user_data;
135
GtkFileChooserAction action;
136
137
if (yfe->flags & YUI_FILE_ENTRY_DIRECTORY) {
138
action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
139
} else {
140
action = GTK_FILE_CHOOSER_ACTION_OPEN;
141
}
142
143
file_selector = gtk_file_chooser_dialog_new ("Please choose a file", NULL, action,
144
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
145
filename = gtk_entry_get_text(GTK_ENTRY(yfe->entry));
146
if (filename[0] != '\0')
147
gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(file_selector), filename);
148
149
gtk_widget_show(file_selector);
150
151
result = gtk_dialog_run(GTK_DIALOG(file_selector));
152
153
switch(result) {
154
case GTK_RESPONSE_ACCEPT:
155
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_selector));
156
gtk_entry_set_text(GTK_ENTRY(yfe->entry), filename);
157
break;
158
case GTK_RESPONSE_CANCEL:
159
break;
160
}
161
162
gtk_widget_destroy(file_selector);
163
}
164
165
static void yui_file_entry_changed(GtkWidget * entry, YuiFileEntry * yfe) {
166
g_key_file_set_value(yfe->keyfile, yfe->group, yfe->key, gtk_entry_get_text(GTK_ENTRY(yfe->entry)));
167
}
168
169