-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathstartgtk_editor.c
407 lines (330 loc) · 12.2 KB
/
startgtk_editor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "build.h"
#include <gtk/gtk.h>
#include "baselayer.h"
#include "editor.h"
#define TAB_CONFIG 0
#define TAB_MESSAGES 1
extern int gtkenabled;
static GtkWindow *startwin;
static struct {
GtkWidget *startbutton;
GtkWidget *cancelbutton;
GtkWidget *tabs;
GtkWidget *configbox;
GtkWidget *alwaysshowcheck;
GtkWidget *messagestext;
GtkWidget *vmode2dcombo;
GtkListStore *vmode2dlist;
GtkWidget *vmode3dcombo;
GtkListStore *vmode3dlist;
GtkWidget *fullscreencheck;
} controls;
static gboolean startwinloop = FALSE;
static struct startwin_settings *settings;
static gboolean quiteventonclose = FALSE;
static int retval = -1;
// -- SUPPORT FUNCTIONS -------------------------------------------------------
static GObject * get_and_connect_signal(GtkBuilder *builder, const char *name, const char *signal_name, GCallback handler)
{
GObject *object;
object = gtk_builder_get_object(builder, name);
if (!object) {
buildprintf("gtk_builder_get_object: %s not found\n", name);
return 0;
}
g_signal_connect(object, signal_name, handler, NULL);
return object;
}
static void foreach_gtk_widget_set_sensitive(GtkWidget *widget, gpointer data)
{
gtk_widget_set_sensitive(widget, (gboolean)(intptr_t)data);
}
static void populate_video_modes(gboolean firsttime)
{
int i, mode2d = -1, mode3d = -1;
int xdim = 0, ydim = 0, bpp = 0, fullscreen = 0;
int xdim2d = 0, ydim2d = 0;
char modestr[64];
int cd[] = { 32, 24, 16, 15, 8, 0 };
GtkTreeIter iter;
if (firsttime) {
getvalidmodes();
xdim = settings->xdim3d;
ydim = settings->ydim3d;
bpp = settings->bpp3d;
fullscreen = settings->fullscreen;
xdim2d = settings->xdim2d;
ydim2d = settings->ydim2d;
} else {
// Read back the current resolution information selected in the combobox.
fullscreen = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(controls.fullscreencheck));
if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(controls.vmode3dcombo), &iter)) {
gtk_tree_model_get(GTK_TREE_MODEL(controls.vmode3dlist), &iter, 1 /*index*/, &mode3d, -1);
}
if (mode3d >= 0) {
xdim = validmode[mode3d].xdim;
ydim = validmode[mode3d].ydim;
bpp = validmode[mode3d].bpp;
}
if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(controls.vmode2dcombo), &iter)) {
gtk_tree_model_get(GTK_TREE_MODEL(controls.vmode2dlist), &iter, 1 /*index*/, &mode2d, -1);
}
if (mode2d >= 0) {
xdim2d = validmode[mode2d].xdim;
ydim2d = validmode[mode2d].ydim;
}
}
// Find an ideal match.
mode3d = checkvideomode(&xdim, &ydim, bpp, fullscreen, 1);
mode2d = checkvideomode(&xdim2d, &ydim2d, 8, fullscreen, 1);
if (mode2d < 0) {
mode2d = 0;
}
if (mode3d < 0) {
for (i=0; cd[i]; ) { if (cd[i] >= bpp) i++; else break; }
for ( ; cd[i]; i++) {
mode3d = checkvideomode(&xdim, &ydim, cd[i], fullscreen, 1);
if (mode3d < 0) continue;
break;
}
}
// Repopulate the list.
gtk_list_store_clear(controls.vmode3dlist);
gtk_list_store_clear(controls.vmode2dlist);
for (i = 0; i < validmodecnt; i++) {
if (validmode[i].fs != fullscreen) continue;
sprintf(modestr, "%d \xc3\x97 %d %d-bpp",
validmode[i].xdim, validmode[i].ydim, validmode[i].bpp);
gtk_list_store_insert_with_values(controls.vmode3dlist,
&iter, -1,
0, modestr, 1, i, -1);
if (i == mode3d) {
gtk_combo_box_set_active_iter(GTK_COMBO_BOX(controls.vmode3dcombo), &iter);
}
if (validmode[i].bpp == 8 && validmode[i].xdim >= 640 && validmode[i].ydim >= 480) {
sprintf(modestr, "%d \xc3\x97 %d",
validmode[i].xdim, validmode[i].ydim);
gtk_list_store_insert_with_values(controls.vmode2dlist,
&iter, -1,
0, modestr, 1, i, -1);
if (i == mode2d) {
gtk_combo_box_set_active_iter(GTK_COMBO_BOX(controls.vmode2dcombo), &iter);
}
}
}
}
static void set_settings(struct startwin_settings *thesettings)
{
settings = thesettings;
}
static void setup_config_mode(void)
{
gtk_notebook_set_current_page(GTK_NOTEBOOK(controls.tabs), TAB_CONFIG);
// Enable all the controls on the Configuration page.
gtk_container_foreach(GTK_CONTAINER(controls.configbox),
foreach_gtk_widget_set_sensitive, (gpointer)TRUE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(controls.alwaysshowcheck), settings->forcesetup);
gtk_widget_set_sensitive(controls.alwaysshowcheck, TRUE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(controls.fullscreencheck), settings->fullscreen);
gtk_widget_set_sensitive(controls.fullscreencheck, TRUE);
populate_video_modes(TRUE);
gtk_widget_set_sensitive(controls.vmode3dcombo, TRUE);
gtk_widget_set_sensitive(controls.vmode2dcombo, TRUE);
gtk_widget_set_sensitive(controls.cancelbutton, TRUE);
gtk_widget_set_sensitive(controls.startbutton, TRUE);
}
static void setup_messages_mode(gboolean allowcancel)
{
gtk_notebook_set_current_page(GTK_NOTEBOOK(controls.tabs), TAB_MESSAGES);
// Disable all the controls on the Configuration page.
gtk_container_foreach(GTK_CONTAINER(controls.configbox),
foreach_gtk_widget_set_sensitive, (gpointer)FALSE);
gtk_widget_set_sensitive(controls.alwaysshowcheck, FALSE);
gtk_widget_set_sensitive(controls.cancelbutton, allowcancel);
gtk_widget_set_sensitive(controls.startbutton, FALSE);
}
// -- EVENT CALLBACKS AND CREATION STUFF --------------------------------------
static void on_fullscreencheck_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
(void)togglebutton; (void)user_data;
populate_video_modes(FALSE);
}
static void on_cancelbutton_clicked(GtkButton *button, gpointer user_data)
{
(void)button; (void)user_data;
startwinloop = FALSE; // Break the loop.
retval = STARTWIN_CANCEL;
quitevent = quitevent || quiteventonclose;
}
static void on_startbutton_clicked(GtkButton *button, gpointer user_data)
{
int mode = -1;
GtkTreeIter iter;
(void)button; (void)user_data;
if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(controls.vmode3dcombo), &iter)) {
gtk_tree_model_get(GTK_TREE_MODEL(controls.vmode3dlist), &iter, 1 /*index*/, &mode, -1);
}
if (mode >= 0) {
settings->xdim3d = validmode[mode].xdim;
settings->ydim3d = validmode[mode].ydim;
settings->bpp3d = validmode[mode].bpp;
settings->fullscreen = validmode[mode].fs;
}
if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(controls.vmode2dcombo), &iter)) {
gtk_tree_model_get(GTK_TREE_MODEL(controls.vmode2dlist), &iter, 1 /*index*/, &mode, -1);
}
if (mode >= 0) {
settings->xdim2d = validmode[mode].xdim;
settings->ydim2d = validmode[mode].ydim;
}
settings->forcesetup = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(controls.alwaysshowcheck));
startwinloop = FALSE; // Break the loop.
retval = STARTWIN_RUN;
}
static gboolean on_startgtk_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
(void)widget; (void)event; (void)user_data;
startwinloop = FALSE; // Break the loop.
retval = STARTWIN_CANCEL;
quitevent = quitevent || quiteventonclose;
return TRUE; // FALSE would let the event go through. We want the game to decide when to close.
}
static GtkWindow *create_window(void)
{
GtkBuilder *builder = NULL;
GError *error = NULL;
GtkWidget *window = NULL;
GtkImage *appicon = NULL;
builder = gtk_builder_new();
if (!builder) {
goto fail;
}
if (!gtk_builder_add_from_resource(builder, "/startgtk.glade", &error)) {
buildprintf("gtk_builder_add_from_resource error: %s\n", error->message);
goto fail;
}
// Get the window widget.
window = GTK_WIDGET(get_and_connect_signal(builder, "startgtk",
"delete-event", G_CALLBACK(on_startgtk_delete_event)));
if (!window) {
goto fail;
}
// Set the appicon image.
appicon = GTK_IMAGE(gtk_builder_get_object(builder, "appicon"));
if (appicon) {
gtk_image_set_from_resource(appicon, "/appicon.png");
}
// Get the window widgets we need and wire them up as appropriate.
controls.startbutton = GTK_WIDGET(get_and_connect_signal(builder, "startbutton",
"clicked", G_CALLBACK(on_startbutton_clicked)));
controls.cancelbutton = GTK_WIDGET(get_and_connect_signal(builder, "cancelbutton",
"clicked", G_CALLBACK(on_cancelbutton_clicked)));
controls.tabs = GTK_WIDGET(gtk_builder_get_object(builder, "tabs"));
controls.configbox = GTK_WIDGET(gtk_builder_get_object(builder, "configbox"));
controls.alwaysshowcheck = GTK_WIDGET(gtk_builder_get_object(builder, "alwaysshowcheck"));
controls.messagestext = GTK_WIDGET(gtk_builder_get_object(builder, "messagestext"));
controls.vmode2dcombo = GTK_WIDGET(gtk_builder_get_object(builder, "vmode2dcombo"));
controls.vmode2dlist = GTK_LIST_STORE(gtk_builder_get_object(builder, "vmode2dlist"));
controls.vmode3dcombo = GTK_WIDGET(gtk_builder_get_object(builder, "vmode3dcombo"));
controls.vmode3dlist = GTK_LIST_STORE(gtk_builder_get_object(builder, "vmode3dlist"));
controls.fullscreencheck = GTK_WIDGET(get_and_connect_signal(builder, "fullscreencheck",
"toggled", G_CALLBACK(on_fullscreencheck_toggled)));
g_object_unref(G_OBJECT(builder));
return GTK_WINDOW(window);
fail:
if (window) {
gtk_widget_destroy(window);
}
if (builder) {
g_object_unref(G_OBJECT(builder));
}
return 0;
}
// -- BUILD ENTRY POINTS ------------------------------------------------------
int startwin_open(void)
{
if (!gtkenabled) return 0;
if (startwin) return 1;
startwin = create_window();
if (!startwin) {
return -1;
}
quiteventonclose = TRUE;
setup_messages_mode(TRUE);
gtk_widget_show_all(GTK_WIDGET(startwin));
return 0;
}
int startwin_close(void)
{
if (!gtkenabled) return 0;
if (!startwin) return 1;
quiteventonclose = FALSE;
gtk_widget_destroy(GTK_WIDGET(startwin));
startwin = NULL;
while (gtk_events_pending()) {
gtk_main_iteration();
}
return 0;
}
int startwin_puts(const char *str)
{
GtkTextBuffer *textbuffer;
GtkTextIter enditer;
GtkTextMark *mark;
const char *aptr, *bptr;
if (!gtkenabled || !str) return 0;
if (!startwin) return 1;
textbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(controls.messagestext));
gtk_text_buffer_get_end_iter(textbuffer, &enditer);
for (aptr = bptr = str; *aptr != 0; ) {
switch (*bptr) {
case '\b':
if (bptr > aptr) {
// Insert any normal characters seen so far.
gtk_text_buffer_insert(textbuffer, &enditer, (const gchar *)aptr, (gint)(bptr-aptr)-1);
}
gtk_text_buffer_backspace(textbuffer, &enditer, FALSE, TRUE);
aptr = ++bptr;
break;
case 0:
if (bptr > aptr) {
gtk_text_buffer_insert(textbuffer, &enditer, (const gchar *)aptr, (gint)(bptr-aptr));
}
aptr = bptr;
break;
default:
bptr++;
break;
}
}
mark = gtk_text_buffer_create_mark(textbuffer, NULL, &enditer, 1);
gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(controls.messagestext), mark, 0.0, FALSE, 0.0, 1.0);
gtk_text_buffer_delete_mark(textbuffer, mark);
return 0;
}
int startwin_settitle(const char *title)
{
if (!gtkenabled) return 0;
if (startwin) {
gtk_window_set_title(startwin, title);
}
return 0;
}
int startwin_idle(void *s)
{
(void)s;
return 0;
}
int startwin_run(struct startwin_settings *settings)
{
if (!gtkenabled || !startwin) return 0;
set_settings(settings);
setup_config_mode();
startwinloop = TRUE;
while (startwinloop) {
gtk_main_iteration_do(TRUE);
}
setup_messages_mode(FALSE);
set_settings(NULL);
return retval;
}