720a8fb7 |
1 | /* |
2 | * gtk.c: GTK front end for my puzzle collection. |
3 | */ |
4 | |
5 | #include <stdio.h> |
fd1a1a2b |
6 | #include <assert.h> |
720a8fb7 |
7 | #include <stdlib.h> |
2ef96bd6 |
8 | #include <time.h> |
720a8fb7 |
9 | #include <stdarg.h> |
c8230524 |
10 | #include <string.h> |
720a8fb7 |
11 | |
8c1fd974 |
12 | #include <sys/time.h> |
13 | |
83680571 |
14 | #include <gtk/gtk.h> |
1482ee76 |
15 | #include <gdk/gdkkeysyms.h> |
83680571 |
16 | |
a9668451 |
17 | #if GTK_CHECK_VERSION(2,0,0) && !defined HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION |
18 | #include <gdk/gdkx.h> |
19 | #include <X11/Xlib.h> |
20 | #endif |
21 | |
720a8fb7 |
22 | #include "puzzles.h" |
23 | |
83680571 |
24 | /* ---------------------------------------------------------------------- |
25 | * Error reporting functions used elsewhere. |
26 | */ |
27 | |
720a8fb7 |
28 | void fatal(char *fmt, ...) |
29 | { |
30 | va_list ap; |
31 | |
32 | fprintf(stderr, "fatal error: "); |
33 | |
34 | va_start(ap, fmt); |
35 | vfprintf(stderr, fmt, ap); |
36 | va_end(ap); |
37 | |
38 | fprintf(stderr, "\n"); |
39 | exit(1); |
40 | } |
83680571 |
41 | |
42 | /* ---------------------------------------------------------------------- |
43 | * GTK front end to puzzles. |
44 | */ |
45 | |
4efb3868 |
46 | struct font { |
47 | GdkFont *font; |
48 | int type; |
49 | int size; |
50 | }; |
51 | |
83680571 |
52 | /* |
53 | * This structure holds all the data relevant to a single window. |
54 | * In principle this would allow us to open multiple independent |
55 | * puzzle windows, although I can't currently see any real point in |
56 | * doing so. I'm just coding cleanly because there's no |
57 | * particularly good reason not to. |
58 | */ |
2ef96bd6 |
59 | struct frontend { |
83680571 |
60 | GtkWidget *window; |
7f77ea24 |
61 | GtkWidget *area; |
fd1a1a2b |
62 | GtkWidget *statusbar; |
63 | guint statusctx; |
2ef96bd6 |
64 | GdkPixmap *pixmap; |
65 | GdkColor *colours; |
66 | int ncolours; |
67 | GdkColormap *colmap; |
68 | int w, h; |
7f77ea24 |
69 | midend_data *me; |
2ef96bd6 |
70 | GdkGC *gc; |
71 | int bbox_l, bbox_r, bbox_u, bbox_d; |
20ee89e3 |
72 | int timer_active, timer_id; |
8c1fd974 |
73 | struct timeval last_time; |
4efb3868 |
74 | struct font *fonts; |
75 | int nfonts, fontsize; |
c8230524 |
76 | config_item *cfg; |
5928817c |
77 | int cfg_which, cfgret; |
c8230524 |
78 | GtkWidget *cfgbox; |
83680571 |
79 | }; |
80 | |
cbb5549e |
81 | void get_random_seed(void **randseed, int *randseedsize) |
82 | { |
83 | time_t *tp = snew(time_t); |
84 | time(tp); |
85 | *randseed = (void *)tp; |
86 | *randseedsize = sizeof(time_t); |
87 | } |
88 | |
2ef96bd6 |
89 | void frontend_default_colour(frontend *fe, float *output) |
90 | { |
91 | GdkColor col = fe->window->style->bg[GTK_STATE_NORMAL]; |
92 | output[0] = col.red / 65535.0; |
93 | output[1] = col.green / 65535.0; |
94 | output[2] = col.blue / 65535.0; |
95 | } |
96 | |
fd1a1a2b |
97 | void status_bar(frontend *fe, char *text) |
98 | { |
99 | assert(fe->statusbar); |
100 | |
101 | gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx); |
102 | gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, text); |
103 | } |
104 | |
2ef96bd6 |
105 | void start_draw(frontend *fe) |
106 | { |
107 | fe->gc = gdk_gc_new(fe->area->window); |
108 | fe->bbox_l = fe->w; |
109 | fe->bbox_r = 0; |
110 | fe->bbox_u = fe->h; |
111 | fe->bbox_d = 0; |
112 | } |
113 | |
4efb3868 |
114 | void clip(frontend *fe, int x, int y, int w, int h) |
115 | { |
116 | GdkRectangle rect; |
117 | |
118 | rect.x = x; |
119 | rect.y = y; |
120 | rect.width = w; |
121 | rect.height = h; |
122 | |
123 | gdk_gc_set_clip_rectangle(fe->gc, &rect); |
124 | } |
125 | |
126 | void unclip(frontend *fe) |
127 | { |
128 | GdkRectangle rect; |
129 | |
130 | rect.x = 0; |
131 | rect.y = 0; |
132 | rect.width = fe->w; |
133 | rect.height = fe->h; |
134 | |
135 | gdk_gc_set_clip_rectangle(fe->gc, &rect); |
136 | } |
137 | |
138 | void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize, |
139 | int align, int colour, char *text) |
140 | { |
141 | int i; |
142 | |
143 | /* |
144 | * Find or create the font. |
145 | */ |
146 | for (i = 0; i < fe->nfonts; i++) |
147 | if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize) |
148 | break; |
149 | |
150 | if (i == fe->nfonts) { |
151 | if (fe->fontsize <= fe->nfonts) { |
152 | fe->fontsize = fe->nfonts + 10; |
153 | fe->fonts = sresize(fe->fonts, fe->fontsize, struct font); |
154 | } |
155 | |
156 | fe->nfonts++; |
157 | |
158 | fe->fonts[i].type = fonttype; |
159 | fe->fonts[i].size = fontsize; |
160 | |
a9668451 |
161 | #if GTK_CHECK_VERSION(2,0,0) |
4efb3868 |
162 | /* |
a9668451 |
163 | * Use Pango to find the closest match to the requested |
164 | * font. |
4efb3868 |
165 | */ |
a9668451 |
166 | { |
167 | PangoFontDescription *fd; |
168 | |
169 | fd = pango_font_description_new(); |
170 | /* `Monospace' and `Sans' are meta-families guaranteed to exist */ |
171 | pango_font_description_set_family(fd, fonttype == FONT_FIXED ? |
172 | "Monospace" : "Sans"); |
173 | /* |
174 | * I found some online Pango documentation which |
175 | * described a function called |
176 | * pango_font_description_set_absolute_size(), which is |
177 | * _exactly_ what I want here. Unfortunately, none of |
178 | * my local Pango installations have it (presumably |
179 | * they're too old), so I'm going to have to hack round |
180 | * it by figuring out the point size myself. This |
181 | * limits me to X and probably also breaks in later |
182 | * Pango installations, so ideally I should add another |
183 | * CHECK_VERSION type ifdef and use set_absolute_size |
184 | * where available. All very annoying. |
185 | */ |
186 | #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION |
187 | pango_font_description_set_absolute_size(fd, PANGO_SCALE*fontsize); |
188 | #else |
189 | { |
190 | Display *d = GDK_DISPLAY(); |
191 | int s = DefaultScreen(d); |
192 | double resolution = |
193 | (PANGO_SCALE * 72.27 / 25.4) * |
194 | ((double) DisplayWidthMM(d, s) / DisplayWidth (d, s)); |
195 | pango_font_description_set_size(fd, resolution * fontsize); |
196 | } |
197 | #endif |
198 | fe->fonts[i].font = gdk_font_from_description(fd); |
199 | pango_font_description_free(fd); |
200 | } |
201 | |
202 | if (!fe->fonts[i].font) |
203 | #endif |
204 | /* |
205 | * In GTK 1.2, I don't know of any plausible way to |
206 | * pick a suitable font, so I'm just going to be |
207 | * tedious. |
208 | * |
209 | * This is also fallback code called if the Pango |
210 | * approach fails to find an appropriate font. |
211 | */ |
212 | fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ? |
213 | "fixed" : "variable"); |
4efb3868 |
214 | } |
215 | |
216 | /* |
217 | * Find string dimensions and process alignment. |
218 | */ |
219 | { |
220 | int lb, rb, wid, asc, desc; |
221 | |
3bcf831a |
222 | /* |
223 | * Measure vertical string extents with respect to the same |
224 | * string always... |
225 | */ |
226 | gdk_string_extents(fe->fonts[i].font, |
227 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
4efb3868 |
228 | &lb, &rb, &wid, &asc, &desc); |
229 | if (align & ALIGN_VCENTRE) |
230 | y += asc - (asc+desc)/2; |
231 | |
3bcf831a |
232 | /* |
233 | * ... but horizontal extents with respect to the provided |
234 | * string. This means that multiple pieces of text centred |
235 | * on the same y-coordinate don't have different baselines. |
236 | */ |
237 | gdk_string_extents(fe->fonts[i].font, text, |
238 | &lb, &rb, &wid, &asc, &desc); |
239 | |
4efb3868 |
240 | if (align & ALIGN_HCENTRE) |
241 | x -= wid / 2; |
242 | else if (align & ALIGN_HRIGHT) |
243 | x -= wid; |
244 | |
245 | } |
246 | |
247 | /* |
248 | * Set colour and actually draw text. |
249 | */ |
250 | gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); |
251 | gdk_draw_string(fe->pixmap, fe->fonts[i].font, fe->gc, x, y, text); |
252 | } |
253 | |
2ef96bd6 |
254 | void draw_rect(frontend *fe, int x, int y, int w, int h, int colour) |
255 | { |
256 | gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); |
257 | gdk_draw_rectangle(fe->pixmap, fe->gc, 1, x, y, w, h); |
258 | } |
259 | |
260 | void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour) |
261 | { |
262 | gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); |
263 | gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2); |
264 | } |
265 | |
266 | void draw_polygon(frontend *fe, int *coords, int npoints, |
267 | int fill, int colour) |
268 | { |
269 | GdkPoint *points = snewn(npoints, GdkPoint); |
270 | int i; |
271 | |
272 | for (i = 0; i < npoints; i++) { |
273 | points[i].x = coords[i*2]; |
274 | points[i].y = coords[i*2+1]; |
275 | } |
276 | |
277 | gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); |
278 | gdk_draw_polygon(fe->pixmap, fe->gc, fill, points, npoints); |
279 | |
280 | sfree(points); |
281 | } |
282 | |
283 | void draw_update(frontend *fe, int x, int y, int w, int h) |
284 | { |
285 | if (fe->bbox_l > x ) fe->bbox_l = x ; |
286 | if (fe->bbox_r < x+w) fe->bbox_r = x+w; |
287 | if (fe->bbox_u > y ) fe->bbox_u = y ; |
288 | if (fe->bbox_d < y+h) fe->bbox_d = y+h; |
289 | } |
290 | |
291 | void end_draw(frontend *fe) |
292 | { |
293 | gdk_gc_unref(fe->gc); |
294 | fe->gc = NULL; |
295 | |
296 | if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) { |
297 | gdk_draw_pixmap(fe->area->window, |
298 | fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)], |
299 | fe->pixmap, |
300 | fe->bbox_l, fe->bbox_u, |
301 | fe->bbox_l, fe->bbox_u, |
302 | fe->bbox_r - fe->bbox_l, fe->bbox_d - fe->bbox_u); |
303 | } |
304 | } |
305 | |
83680571 |
306 | static void destroy(GtkWidget *widget, gpointer data) |
307 | { |
f49650a7 |
308 | frontend *fe = (frontend *)data; |
309 | deactivate_timer(fe); |
83680571 |
310 | gtk_main_quit(); |
311 | } |
312 | |
2ef96bd6 |
313 | static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data) |
7f77ea24 |
314 | { |
2ef96bd6 |
315 | frontend *fe = (frontend *)data; |
1482ee76 |
316 | int keyval; |
7f77ea24 |
317 | |
2ef96bd6 |
318 | if (!fe->pixmap) |
319 | return TRUE; |
7f77ea24 |
320 | |
1482ee76 |
321 | if (event->string[0] && !event->string[1]) |
322 | keyval = (unsigned char)event->string[0]; |
c71454c0 |
323 | else if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up || |
324 | event->keyval == GDK_KP_8) |
1482ee76 |
325 | keyval = CURSOR_UP; |
c71454c0 |
326 | else if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down || |
327 | event->keyval == GDK_KP_2) |
1482ee76 |
328 | keyval = CURSOR_DOWN; |
c71454c0 |
329 | else if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left || |
330 | event->keyval == GDK_KP_4) |
1482ee76 |
331 | keyval = CURSOR_LEFT; |
c71454c0 |
332 | else if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right || |
333 | event->keyval == GDK_KP_6) |
1482ee76 |
334 | keyval = CURSOR_RIGHT; |
c71454c0 |
335 | else if (event->keyval == GDK_KP_Home || event->keyval == GDK_KP_7) |
336 | keyval = CURSOR_UP_LEFT; |
337 | else if (event->keyval == GDK_KP_End || event->keyval == GDK_KP_1) |
338 | keyval = CURSOR_DOWN_LEFT; |
339 | else if (event->keyval == GDK_KP_Page_Up || event->keyval == GDK_KP_9) |
340 | keyval = CURSOR_UP_RIGHT; |
341 | else if (event->keyval == GDK_KP_Page_Down || event->keyval == GDK_KP_3) |
342 | keyval = CURSOR_DOWN_RIGHT; |
1482ee76 |
343 | else |
344 | keyval = -1; |
345 | |
346 | if (keyval >= 0 && |
347 | !midend_process_key(fe->me, 0, 0, keyval)) |
2ef96bd6 |
348 | gtk_widget_destroy(fe->window); |
7f77ea24 |
349 | |
350 | return TRUE; |
351 | } |
352 | |
2ef96bd6 |
353 | static gint button_event(GtkWidget *widget, GdkEventButton *event, |
354 | gpointer data) |
7f77ea24 |
355 | { |
2ef96bd6 |
356 | frontend *fe = (frontend *)data; |
357 | int button; |
358 | |
359 | if (!fe->pixmap) |
360 | return TRUE; |
361 | |
74a4e547 |
362 | if (event->type != GDK_BUTTON_PRESS && event->type != GDK_BUTTON_RELEASE) |
2ef96bd6 |
363 | return TRUE; |
7f77ea24 |
364 | |
b0f06719 |
365 | if (event->button == 2 || (event->state & GDK_SHIFT_MASK)) |
2ef96bd6 |
366 | button = MIDDLE_BUTTON; |
b0f06719 |
367 | else if (event->button == 1) |
368 | button = LEFT_BUTTON; |
2ef96bd6 |
369 | else if (event->button == 3) |
370 | button = RIGHT_BUTTON; |
371 | else |
372 | return FALSE; /* don't even know what button! */ |
373 | |
74a4e547 |
374 | if (event->type == GDK_BUTTON_RELEASE) |
375 | button += LEFT_RELEASE - LEFT_BUTTON; |
376 | |
377 | if (!midend_process_key(fe->me, event->x, event->y, button)) |
378 | gtk_widget_destroy(fe->window); |
379 | |
380 | return TRUE; |
381 | } |
382 | |
383 | static gint motion_event(GtkWidget *widget, GdkEventMotion *event, |
384 | gpointer data) |
385 | { |
386 | frontend *fe = (frontend *)data; |
387 | int button; |
388 | |
389 | if (!fe->pixmap) |
390 | return TRUE; |
391 | |
74a4e547 |
392 | if (event->state & (GDK_BUTTON2_MASK | GDK_SHIFT_MASK)) |
393 | button = MIDDLE_DRAG; |
394 | else if (event->state & GDK_BUTTON1_MASK) |
395 | button = LEFT_DRAG; |
396 | else if (event->state & GDK_BUTTON3_MASK) |
397 | button = RIGHT_DRAG; |
398 | else |
399 | return FALSE; /* don't even know what button! */ |
400 | |
2ef96bd6 |
401 | if (!midend_process_key(fe->me, event->x, event->y, button)) |
402 | gtk_widget_destroy(fe->window); |
7f77ea24 |
403 | |
404 | return TRUE; |
405 | } |
406 | |
2ef96bd6 |
407 | static gint expose_area(GtkWidget *widget, GdkEventExpose *event, |
408 | gpointer data) |
83680571 |
409 | { |
2ef96bd6 |
410 | frontend *fe = (frontend *)data; |
411 | |
412 | if (fe->pixmap) { |
413 | gdk_draw_pixmap(widget->window, |
414 | widget->style->fg_gc[GTK_WIDGET_STATE(widget)], |
415 | fe->pixmap, |
416 | event->area.x, event->area.y, |
417 | event->area.x, event->area.y, |
418 | event->area.width, event->area.height); |
419 | } |
420 | return TRUE; |
421 | } |
422 | |
fd1a1a2b |
423 | static gint map_window(GtkWidget *widget, GdkEvent *event, |
424 | gpointer data) |
425 | { |
426 | frontend *fe = (frontend *)data; |
427 | |
428 | /* |
429 | * Apparently we need to do this because otherwise the status |
430 | * bar will fail to update immediately. Annoying, but there we |
431 | * go. |
432 | */ |
433 | gtk_widget_queue_draw(fe->window); |
434 | |
435 | return TRUE; |
436 | } |
437 | |
2ef96bd6 |
438 | static gint configure_area(GtkWidget *widget, |
439 | GdkEventConfigure *event, gpointer data) |
440 | { |
441 | frontend *fe = (frontend *)data; |
442 | GdkGC *gc; |
443 | |
eb2ad6f1 |
444 | if (fe->pixmap) |
445 | gdk_pixmap_unref(fe->pixmap); |
446 | |
2ef96bd6 |
447 | fe->pixmap = gdk_pixmap_new(widget->window, fe->w, fe->h, -1); |
448 | |
449 | gc = gdk_gc_new(fe->area->window); |
450 | gdk_gc_set_foreground(gc, &fe->colours[0]); |
451 | gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->w, fe->h); |
452 | gdk_gc_unref(gc); |
453 | |
454 | midend_redraw(fe->me); |
455 | |
456 | return TRUE; |
457 | } |
458 | |
459 | static gint timer_func(gpointer data) |
460 | { |
461 | frontend *fe = (frontend *)data; |
462 | |
8c1fd974 |
463 | if (fe->timer_active) { |
464 | struct timeval now; |
465 | float elapsed; |
466 | gettimeofday(&now, NULL); |
467 | elapsed = ((now.tv_usec - fe->last_time.tv_usec) * 0.000001F + |
468 | (now.tv_sec - fe->last_time.tv_sec)); |
469 | midend_timer(fe->me, elapsed); /* may clear timer_active */ |
470 | fe->last_time = now; |
471 | } |
2ef96bd6 |
472 | |
473 | return fe->timer_active; |
474 | } |
475 | |
476 | void deactivate_timer(frontend *fe) |
477 | { |
20ee89e3 |
478 | if (fe->timer_active) |
479 | gtk_timeout_remove(fe->timer_id); |
2ef96bd6 |
480 | fe->timer_active = FALSE; |
481 | } |
482 | |
483 | void activate_timer(frontend *fe) |
484 | { |
8c1fd974 |
485 | if (!fe->timer_active) { |
20ee89e3 |
486 | fe->timer_id = gtk_timeout_add(20, timer_func, fe); |
8c1fd974 |
487 | gettimeofday(&fe->last_time, NULL); |
488 | } |
2ef96bd6 |
489 | fe->timer_active = TRUE; |
490 | } |
491 | |
c8230524 |
492 | static void window_destroy(GtkWidget *widget, gpointer data) |
493 | { |
494 | gtk_main_quit(); |
495 | } |
496 | |
497 | static void errmsg_button_clicked(GtkButton *button, gpointer data) |
498 | { |
499 | gtk_widget_destroy(GTK_WIDGET(data)); |
500 | } |
501 | |
a163b7c2 |
502 | static int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) |
503 | { |
504 | GtkObject *cancelbutton = GTK_OBJECT(data); |
505 | |
506 | /* |
507 | * `Escape' effectively clicks the cancel button |
508 | */ |
509 | if (event->keyval == GDK_Escape) { |
510 | gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton), "clicked"); |
511 | return TRUE; |
512 | } |
513 | |
514 | return FALSE; |
515 | } |
516 | |
c8230524 |
517 | void error_box(GtkWidget *parent, char *msg) |
518 | { |
519 | GtkWidget *window, *hbox, *text, *ok; |
520 | |
521 | window = gtk_dialog_new(); |
522 | text = gtk_label_new(msg); |
523 | gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0); |
524 | hbox = gtk_hbox_new(FALSE, 0); |
525 | gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20); |
526 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), |
527 | hbox, FALSE, FALSE, 20); |
528 | gtk_widget_show(text); |
529 | gtk_widget_show(hbox); |
530 | gtk_window_set_title(GTK_WINDOW(window), "Error"); |
531 | gtk_label_set_line_wrap(GTK_LABEL(text), TRUE); |
532 | ok = gtk_button_new_with_label("OK"); |
533 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area), |
534 | ok, FALSE, FALSE, 0); |
535 | gtk_widget_show(ok); |
536 | GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT); |
537 | gtk_window_set_default(GTK_WINDOW(window), ok); |
538 | gtk_signal_connect(GTK_OBJECT(ok), "clicked", |
539 | GTK_SIGNAL_FUNC(errmsg_button_clicked), window); |
a1ed9f0e |
540 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
541 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
a163b7c2 |
542 | gtk_signal_connect(GTK_OBJECT(window), "key_press_event", |
543 | GTK_SIGNAL_FUNC(win_key_press), ok); |
c8230524 |
544 | gtk_window_set_modal(GTK_WINDOW(window), TRUE); |
545 | gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent)); |
546 | //set_transient_window_pos(parent, window); |
547 | gtk_widget_show(window); |
548 | gtk_main(); |
549 | } |
550 | |
551 | static void config_ok_button_clicked(GtkButton *button, gpointer data) |
552 | { |
553 | frontend *fe = (frontend *)data; |
554 | char *err; |
555 | |
5928817c |
556 | err = midend_set_config(fe->me, fe->cfg_which, fe->cfg); |
c8230524 |
557 | |
558 | if (err) |
559 | error_box(fe->cfgbox, err); |
560 | else { |
561 | fe->cfgret = TRUE; |
562 | gtk_widget_destroy(fe->cfgbox); |
563 | } |
564 | } |
565 | |
566 | static void config_cancel_button_clicked(GtkButton *button, gpointer data) |
567 | { |
568 | frontend *fe = (frontend *)data; |
569 | |
570 | gtk_widget_destroy(fe->cfgbox); |
571 | } |
572 | |
a163b7c2 |
573 | static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data) |
574 | { |
575 | /* |
576 | * GtkEntry has a nasty habit of eating the Return key, which |
577 | * is unhelpful since it doesn't actually _do_ anything with it |
578 | * (it calls gtk_widget_activate, but our edit boxes never need |
579 | * activating). So I catch Return before GtkEntry sees it, and |
580 | * pass it straight on to the parent widget. Effect: hitting |
581 | * Return in an edit box will now activate the default button |
582 | * in the dialog just like it will everywhere else. |
583 | */ |
584 | if (event->keyval == GDK_Return && widget->parent != NULL) { |
585 | gint return_val; |
586 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event"); |
587 | gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event", |
588 | event, &return_val); |
589 | return return_val; |
590 | } |
591 | return FALSE; |
592 | } |
593 | |
c8230524 |
594 | static void editbox_changed(GtkEditable *ed, gpointer data) |
595 | { |
596 | config_item *i = (config_item *)data; |
597 | |
598 | sfree(i->sval); |
599 | i->sval = dupstr(gtk_entry_get_text(GTK_ENTRY(ed))); |
600 | } |
601 | |
602 | static void button_toggled(GtkToggleButton *tb, gpointer data) |
603 | { |
604 | config_item *i = (config_item *)data; |
605 | |
606 | i->ival = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb)); |
607 | } |
608 | |
609 | static void droplist_sel(GtkMenuItem *item, gpointer data) |
610 | { |
611 | config_item *i = (config_item *)data; |
612 | |
613 | i->ival = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item), |
614 | "user-data")); |
615 | } |
616 | |
5928817c |
617 | static int get_config(frontend *fe, int which) |
c8230524 |
618 | { |
a163b7c2 |
619 | GtkWidget *w, *table, *cancel; |
5928817c |
620 | char *title; |
c8230524 |
621 | config_item *i; |
622 | int y; |
623 | |
5928817c |
624 | fe->cfg = midend_get_config(fe->me, which, &title); |
625 | fe->cfg_which = which; |
c8230524 |
626 | fe->cfgret = FALSE; |
627 | |
628 | fe->cfgbox = gtk_dialog_new(); |
5928817c |
629 | gtk_window_set_title(GTK_WINDOW(fe->cfgbox), title); |
630 | sfree(title); |
c8230524 |
631 | |
632 | w = gtk_button_new_with_label("OK"); |
633 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area), |
634 | w, FALSE, FALSE, 0); |
635 | gtk_widget_show(w); |
636 | GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT); |
637 | gtk_window_set_default(GTK_WINDOW(fe->cfgbox), w); |
638 | gtk_signal_connect(GTK_OBJECT(w), "clicked", |
639 | GTK_SIGNAL_FUNC(config_ok_button_clicked), fe); |
640 | |
641 | w = gtk_button_new_with_label("Cancel"); |
642 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area), |
643 | w, FALSE, FALSE, 0); |
644 | gtk_widget_show(w); |
645 | gtk_signal_connect(GTK_OBJECT(w), "clicked", |
646 | GTK_SIGNAL_FUNC(config_cancel_button_clicked), fe); |
a163b7c2 |
647 | cancel = w; |
c8230524 |
648 | |
649 | table = gtk_table_new(1, 2, FALSE); |
650 | y = 0; |
651 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->vbox), |
652 | table, FALSE, FALSE, 0); |
653 | gtk_widget_show(table); |
654 | |
95709966 |
655 | for (i = fe->cfg; i->type != C_END; i++) { |
c8230524 |
656 | gtk_table_resize(GTK_TABLE(table), y+1, 2); |
657 | |
658 | switch (i->type) { |
95709966 |
659 | case C_STRING: |
c8230524 |
660 | /* |
661 | * Edit box with a label beside it. |
662 | */ |
663 | |
664 | w = gtk_label_new(i->name); |
665 | gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5); |
666 | gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1, |
667 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
668 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
669 | 3, 3); |
670 | gtk_widget_show(w); |
671 | |
672 | w = gtk_entry_new(); |
673 | gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1, |
674 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
675 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
676 | 3, 3); |
677 | gtk_entry_set_text(GTK_ENTRY(w), i->sval); |
678 | gtk_signal_connect(GTK_OBJECT(w), "changed", |
679 | GTK_SIGNAL_FUNC(editbox_changed), i); |
a163b7c2 |
680 | gtk_signal_connect(GTK_OBJECT(w), "key_press_event", |
681 | GTK_SIGNAL_FUNC(editbox_key), NULL); |
c8230524 |
682 | gtk_widget_show(w); |
683 | |
684 | break; |
685 | |
95709966 |
686 | case C_BOOLEAN: |
c8230524 |
687 | /* |
688 | * Simple checkbox. |
689 | */ |
690 | w = gtk_check_button_new_with_label(i->name); |
691 | gtk_signal_connect(GTK_OBJECT(w), "toggled", |
692 | GTK_SIGNAL_FUNC(button_toggled), i); |
693 | gtk_table_attach(GTK_TABLE(table), w, 0, 2, y, y+1, |
694 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
695 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
696 | 3, 3); |
697 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), i->ival); |
698 | gtk_widget_show(w); |
699 | break; |
700 | |
95709966 |
701 | case C_CHOICES: |
c8230524 |
702 | /* |
703 | * Drop-down list (GtkOptionMenu). |
704 | */ |
705 | |
706 | w = gtk_label_new(i->name); |
707 | gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5); |
708 | gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1, |
709 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
710 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
711 | 3, 3); |
712 | gtk_widget_show(w); |
713 | |
714 | w = gtk_option_menu_new(); |
715 | gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1, |
716 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
717 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
718 | 3, 3); |
719 | gtk_widget_show(w); |
720 | |
721 | { |
722 | int c, val; |
723 | char *p, *q, *name; |
724 | GtkWidget *menuitem; |
725 | GtkWidget *menu = gtk_menu_new(); |
726 | |
727 | gtk_option_menu_set_menu(GTK_OPTION_MENU(w), menu); |
728 | |
729 | c = *i->sval; |
730 | p = i->sval+1; |
731 | val = 0; |
732 | |
733 | while (*p) { |
734 | q = p; |
735 | while (*q && *q != c) |
736 | q++; |
737 | |
738 | name = snewn(q-p+1, char); |
739 | strncpy(name, p, q-p); |
740 | name[q-p] = '\0'; |
741 | |
742 | if (*q) q++; /* eat delimiter */ |
743 | |
744 | menuitem = gtk_menu_item_new_with_label(name); |
745 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
746 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
747 | GINT_TO_POINTER(val)); |
748 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
749 | GTK_SIGNAL_FUNC(droplist_sel), i); |
750 | gtk_widget_show(menuitem); |
751 | |
752 | val++; |
753 | |
754 | p = q; |
755 | } |
756 | |
757 | gtk_option_menu_set_history(GTK_OPTION_MENU(w), i->ival); |
758 | } |
759 | |
760 | break; |
761 | } |
762 | |
763 | y++; |
764 | } |
765 | |
766 | gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "destroy", |
767 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
a163b7c2 |
768 | gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "key_press_event", |
769 | GTK_SIGNAL_FUNC(win_key_press), cancel); |
c8230524 |
770 | gtk_window_set_modal(GTK_WINDOW(fe->cfgbox), TRUE); |
771 | gtk_window_set_transient_for(GTK_WINDOW(fe->cfgbox), |
772 | GTK_WINDOW(fe->window)); |
773 | //set_transient_window_pos(fe->window, fe->cfgbox); |
774 | gtk_widget_show(fe->cfgbox); |
775 | gtk_main(); |
776 | |
077f3cbe |
777 | free_cfg(fe->cfg); |
c8230524 |
778 | |
779 | return fe->cfgret; |
780 | } |
781 | |
eb2ad6f1 |
782 | static void menu_key_event(GtkMenuItem *menuitem, gpointer data) |
783 | { |
784 | frontend *fe = (frontend *)data; |
785 | int key = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem), |
786 | "user-data")); |
787 | if (!midend_process_key(fe->me, 0, 0, key)) |
788 | gtk_widget_destroy(fe->window); |
789 | } |
790 | |
791 | static void menu_preset_event(GtkMenuItem *menuitem, gpointer data) |
792 | { |
793 | frontend *fe = (frontend *)data; |
794 | game_params *params = |
795 | (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data"); |
796 | int x, y; |
797 | |
798 | midend_set_params(fe->me, params); |
5928817c |
799 | midend_new_game(fe->me); |
eb2ad6f1 |
800 | midend_size(fe->me, &x, &y); |
801 | gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); |
802 | fe->w = x; |
803 | fe->h = y; |
804 | } |
805 | |
c8230524 |
806 | static void menu_config_event(GtkMenuItem *menuitem, gpointer data) |
807 | { |
808 | frontend *fe = (frontend *)data; |
5928817c |
809 | int which = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem), |
810 | "user-data")); |
c8230524 |
811 | int x, y; |
812 | |
5928817c |
813 | if (!get_config(fe, which)) |
c8230524 |
814 | return; |
815 | |
5928817c |
816 | midend_new_game(fe->me); |
c8230524 |
817 | midend_size(fe->me, &x, &y); |
818 | gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); |
819 | fe->w = x; |
820 | fe->h = y; |
821 | } |
822 | |
eb2ad6f1 |
823 | static GtkWidget *add_menu_item_with_key(frontend *fe, GtkContainer *cont, |
824 | char *text, int key) |
825 | { |
826 | GtkWidget *menuitem = gtk_menu_item_new_with_label(text); |
827 | gtk_container_add(cont, menuitem); |
828 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
829 | GINT_TO_POINTER(key)); |
830 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
831 | GTK_SIGNAL_FUNC(menu_key_event), fe); |
832 | gtk_widget_show(menuitem); |
833 | return menuitem; |
834 | } |
835 | |
836 | static void add_menu_separator(GtkContainer *cont) |
837 | { |
838 | GtkWidget *menuitem = gtk_menu_item_new(); |
839 | gtk_container_add(cont, menuitem); |
840 | gtk_widget_show(menuitem); |
841 | } |
842 | |
8b7938e7 |
843 | static frontend *new_window(char *game_id, char **error) |
2ef96bd6 |
844 | { |
845 | frontend *fe; |
eb2ad6f1 |
846 | GtkBox *vbox; |
847 | GtkWidget *menubar, *menu, *menuitem; |
848 | int x, y, n; |
83680571 |
849 | |
2ef96bd6 |
850 | fe = snew(frontend); |
83680571 |
851 | |
be8d5aa1 |
852 | fe->me = midend_new(fe, &thegame); |
8b7938e7 |
853 | if (game_id) { |
854 | *error = midend_game_id(fe->me, game_id, FALSE); |
855 | if (*error) { |
856 | midend_free(fe->me); |
857 | sfree(fe); |
858 | return NULL; |
859 | } |
860 | } |
5928817c |
861 | midend_new_game(fe->me); |
7f77ea24 |
862 | |
2ef96bd6 |
863 | fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
be8d5aa1 |
864 | gtk_window_set_title(GTK_WINDOW(fe->window), thegame.name); |
ff2f787b |
865 | #if 0 |
866 | gtk_window_set_resizable(GTK_WINDOW(fe->window), FALSE); |
867 | #else |
868 | gtk_window_set_policy(GTK_WINDOW(fe->window), FALSE, FALSE, TRUE); |
869 | #endif |
eb2ad6f1 |
870 | vbox = GTK_BOX(gtk_vbox_new(FALSE, 0)); |
871 | gtk_container_add(GTK_CONTAINER(fe->window), GTK_WIDGET(vbox)); |
872 | gtk_widget_show(GTK_WIDGET(vbox)); |
873 | |
874 | menubar = gtk_menu_bar_new(); |
875 | gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0); |
876 | gtk_widget_show(menubar); |
877 | |
878 | menuitem = gtk_menu_item_new_with_label("Game"); |
879 | gtk_container_add(GTK_CONTAINER(menubar), menuitem); |
880 | gtk_widget_show(menuitem); |
881 | |
882 | menu = gtk_menu_new(); |
883 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu); |
884 | |
885 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "New", 'n'); |
886 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Restart", 'r'); |
887 | |
5928817c |
888 | menuitem = gtk_menu_item_new_with_label("Specific..."); |
889 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
890 | GINT_TO_POINTER(CFG_SEED)); |
891 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
892 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
893 | GTK_SIGNAL_FUNC(menu_config_event), fe); |
894 | gtk_widget_show(menuitem); |
895 | |
be8d5aa1 |
896 | if ((n = midend_num_presets(fe->me)) > 0 || thegame.can_configure) { |
eb2ad6f1 |
897 | GtkWidget *submenu; |
898 | int i; |
899 | |
900 | menuitem = gtk_menu_item_new_with_label("Type"); |
c8230524 |
901 | gtk_container_add(GTK_CONTAINER(menubar), menuitem); |
eb2ad6f1 |
902 | gtk_widget_show(menuitem); |
903 | |
904 | submenu = gtk_menu_new(); |
905 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu); |
906 | |
907 | for (i = 0; i < n; i++) { |
908 | char *name; |
909 | game_params *params; |
910 | |
911 | midend_fetch_preset(fe->me, i, &name, ¶ms); |
912 | |
913 | menuitem = gtk_menu_item_new_with_label(name); |
914 | gtk_container_add(GTK_CONTAINER(submenu), menuitem); |
915 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", params); |
916 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
917 | GTK_SIGNAL_FUNC(menu_preset_event), fe); |
918 | gtk_widget_show(menuitem); |
919 | } |
c8230524 |
920 | |
be8d5aa1 |
921 | if (thegame.can_configure) { |
c8230524 |
922 | menuitem = gtk_menu_item_new_with_label("Custom..."); |
5928817c |
923 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
924 | GPOINTER_TO_INT(CFG_SETTINGS)); |
c8230524 |
925 | gtk_container_add(GTK_CONTAINER(submenu), menuitem); |
926 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
927 | GTK_SIGNAL_FUNC(menu_config_event), fe); |
928 | gtk_widget_show(menuitem); |
929 | } |
eb2ad6f1 |
930 | } |
931 | |
932 | add_menu_separator(GTK_CONTAINER(menu)); |
933 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Undo", 'u'); |
934 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Redo", '\x12'); |
935 | add_menu_separator(GTK_CONTAINER(menu)); |
936 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Exit", 'q'); |
7f77ea24 |
937 | |
2ef96bd6 |
938 | { |
939 | int i, ncolours; |
940 | float *colours; |
941 | gboolean *success; |
7f77ea24 |
942 | |
2ef96bd6 |
943 | fe->colmap = gdk_colormap_get_system(); |
944 | colours = midend_colours(fe->me, &ncolours); |
945 | fe->ncolours = ncolours; |
946 | fe->colours = snewn(ncolours, GdkColor); |
947 | for (i = 0; i < ncolours; i++) { |
948 | fe->colours[i].red = colours[i*3] * 0xFFFF; |
949 | fe->colours[i].green = colours[i*3+1] * 0xFFFF; |
950 | fe->colours[i].blue = colours[i*3+2] * 0xFFFF; |
951 | } |
952 | success = snewn(ncolours, gboolean); |
953 | gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours, |
954 | FALSE, FALSE, success); |
955 | for (i = 0; i < ncolours; i++) { |
956 | if (!success[i]) |
957 | g_error("couldn't allocate colour %d (#%02x%02x%02x)\n", |
958 | i, fe->colours[i].red >> 8, |
959 | fe->colours[i].green >> 8, |
960 | fe->colours[i].blue >> 8); |
961 | } |
962 | } |
7f77ea24 |
963 | |
fd1a1a2b |
964 | if (midend_wants_statusbar(fe->me)) { |
5725d728 |
965 | GtkWidget *viewport; |
966 | GtkRequisition req; |
967 | |
968 | viewport = gtk_viewport_new(NULL, NULL); |
969 | gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE); |
fd1a1a2b |
970 | fe->statusbar = gtk_statusbar_new(); |
5725d728 |
971 | gtk_container_add(GTK_CONTAINER(viewport), fe->statusbar); |
972 | gtk_widget_show(viewport); |
973 | gtk_box_pack_end(vbox, viewport, FALSE, FALSE, 0); |
fd1a1a2b |
974 | gtk_widget_show(fe->statusbar); |
975 | fe->statusctx = gtk_statusbar_get_context_id |
976 | (GTK_STATUSBAR(fe->statusbar), "game"); |
977 | gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, |
5725d728 |
978 | "test"); |
979 | gtk_widget_size_request(fe->statusbar, &req); |
980 | #if 0 |
981 | /* For GTK 2.0, should we be using gtk_widget_set_size_request? */ |
982 | #endif |
60d42abc |
983 | gtk_widget_set_usize(viewport, -1, req.height); |
fd1a1a2b |
984 | } else |
985 | fe->statusbar = NULL; |
986 | |
2ef96bd6 |
987 | fe->area = gtk_drawing_area_new(); |
988 | midend_size(fe->me, &x, &y); |
989 | gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); |
990 | fe->w = x; |
991 | fe->h = y; |
992 | |
eb2ad6f1 |
993 | gtk_box_pack_end(vbox, fe->area, FALSE, FALSE, 0); |
2ef96bd6 |
994 | |
995 | fe->pixmap = NULL; |
4efb3868 |
996 | fe->fonts = NULL; |
997 | fe->nfonts = fe->fontsize = 0; |
2ef96bd6 |
998 | |
20ee89e3 |
999 | fe->timer_active = FALSE; |
1000 | |
2ef96bd6 |
1001 | gtk_signal_connect(GTK_OBJECT(fe->window), "destroy", |
1002 | GTK_SIGNAL_FUNC(destroy), fe); |
1003 | gtk_signal_connect(GTK_OBJECT(fe->window), "key_press_event", |
1004 | GTK_SIGNAL_FUNC(key_event), fe); |
1005 | gtk_signal_connect(GTK_OBJECT(fe->area), "button_press_event", |
1006 | GTK_SIGNAL_FUNC(button_event), fe); |
74a4e547 |
1007 | gtk_signal_connect(GTK_OBJECT(fe->area), "button_release_event", |
1008 | GTK_SIGNAL_FUNC(button_event), fe); |
1009 | gtk_signal_connect(GTK_OBJECT(fe->area), "motion_notify_event", |
1010 | GTK_SIGNAL_FUNC(motion_event), fe); |
2ef96bd6 |
1011 | gtk_signal_connect(GTK_OBJECT(fe->area), "expose_event", |
1012 | GTK_SIGNAL_FUNC(expose_area), fe); |
fd1a1a2b |
1013 | gtk_signal_connect(GTK_OBJECT(fe->window), "map_event", |
1014 | GTK_SIGNAL_FUNC(map_window), fe); |
2ef96bd6 |
1015 | gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event", |
1016 | GTK_SIGNAL_FUNC(configure_area), fe); |
1017 | |
74a4e547 |
1018 | gtk_widget_add_events(GTK_WIDGET(fe->area), |
1019 | GDK_BUTTON_PRESS_MASK | |
1020 | GDK_BUTTON_RELEASE_MASK | |
1021 | GDK_BUTTON_MOTION_MASK); |
2ef96bd6 |
1022 | |
1023 | gtk_widget_show(fe->area); |
1024 | gtk_widget_show(fe->window); |
1025 | |
1026 | return fe; |
83680571 |
1027 | } |
1028 | |
1029 | int main(int argc, char **argv) |
1030 | { |
8b7938e7 |
1031 | char *pname = argv[0]; |
1032 | char *error; |
1033 | |
83680571 |
1034 | gtk_init(&argc, &argv); |
8b7938e7 |
1035 | |
1036 | if (!new_window(argc > 1 ? argv[1] : NULL, &error)) { |
1037 | fprintf(stderr, "%s: %s\n", pname, error); |
1038 | return 1; |
1039 | } |
1040 | |
83680571 |
1041 | gtk_main(); |
1042 | |
1043 | return 0; |
1044 | } |