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