Pango-derived fonts seem to generally look better if I ask for them
[sgt/puzzles] / gtk.c
CommitLineData
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 28void 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 46struct 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 59struct 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 81void 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 89void 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 97void 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 105void 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 114void 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
126void 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
138void 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");
2e837d78 173 pango_font_description_set_weight(fd, PANGO_WEIGHT_BOLD);
a9668451 174 /*
175 * I found some online Pango documentation which
176 * described a function called
177 * pango_font_description_set_absolute_size(), which is
178 * _exactly_ what I want here. Unfortunately, none of
179 * my local Pango installations have it (presumably
180 * they're too old), so I'm going to have to hack round
181 * it by figuring out the point size myself. This
182 * limits me to X and probably also breaks in later
183 * Pango installations, so ideally I should add another
184 * CHECK_VERSION type ifdef and use set_absolute_size
185 * where available. All very annoying.
186 */
187#ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
188 pango_font_description_set_absolute_size(fd, PANGO_SCALE*fontsize);
189#else
190 {
191 Display *d = GDK_DISPLAY();
192 int s = DefaultScreen(d);
193 double resolution =
194 (PANGO_SCALE * 72.27 / 25.4) *
195 ((double) DisplayWidthMM(d, s) / DisplayWidth (d, s));
196 pango_font_description_set_size(fd, resolution * fontsize);
197 }
198#endif
199 fe->fonts[i].font = gdk_font_from_description(fd);
200 pango_font_description_free(fd);
201 }
202
203 if (!fe->fonts[i].font)
204#endif
205 /*
206 * In GTK 1.2, I don't know of any plausible way to
207 * pick a suitable font, so I'm just going to be
208 * tedious.
209 *
210 * This is also fallback code called if the Pango
211 * approach fails to find an appropriate font.
212 */
213 fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ?
214 "fixed" : "variable");
4efb3868 215 }
216
217 /*
218 * Find string dimensions and process alignment.
219 */
220 {
221 int lb, rb, wid, asc, desc;
222
3bcf831a 223 /*
224 * Measure vertical string extents with respect to the same
225 * string always...
226 */
227 gdk_string_extents(fe->fonts[i].font,
228 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
4efb3868 229 &lb, &rb, &wid, &asc, &desc);
230 if (align & ALIGN_VCENTRE)
231 y += asc - (asc+desc)/2;
232
3bcf831a 233 /*
234 * ... but horizontal extents with respect to the provided
235 * string. This means that multiple pieces of text centred
236 * on the same y-coordinate don't have different baselines.
237 */
238 gdk_string_extents(fe->fonts[i].font, text,
239 &lb, &rb, &wid, &asc, &desc);
240
4efb3868 241 if (align & ALIGN_HCENTRE)
242 x -= wid / 2;
243 else if (align & ALIGN_HRIGHT)
244 x -= wid;
245
246 }
247
248 /*
249 * Set colour and actually draw text.
250 */
251 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
252 gdk_draw_string(fe->pixmap, fe->fonts[i].font, fe->gc, x, y, text);
253}
254
2ef96bd6 255void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
256{
257 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
258 gdk_draw_rectangle(fe->pixmap, fe->gc, 1, x, y, w, h);
259}
260
261void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
262{
263 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
264 gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2);
265}
266
267void draw_polygon(frontend *fe, int *coords, int npoints,
268 int fill, int colour)
269{
270 GdkPoint *points = snewn(npoints, GdkPoint);
271 int i;
272
273 for (i = 0; i < npoints; i++) {
274 points[i].x = coords[i*2];
275 points[i].y = coords[i*2+1];
276 }
277
278 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
279 gdk_draw_polygon(fe->pixmap, fe->gc, fill, points, npoints);
280
281 sfree(points);
282}
283
284void draw_update(frontend *fe, int x, int y, int w, int h)
285{
286 if (fe->bbox_l > x ) fe->bbox_l = x ;
287 if (fe->bbox_r < x+w) fe->bbox_r = x+w;
288 if (fe->bbox_u > y ) fe->bbox_u = y ;
289 if (fe->bbox_d < y+h) fe->bbox_d = y+h;
290}
291
292void end_draw(frontend *fe)
293{
294 gdk_gc_unref(fe->gc);
295 fe->gc = NULL;
296
297 if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) {
298 gdk_draw_pixmap(fe->area->window,
299 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
300 fe->pixmap,
301 fe->bbox_l, fe->bbox_u,
302 fe->bbox_l, fe->bbox_u,
303 fe->bbox_r - fe->bbox_l, fe->bbox_d - fe->bbox_u);
304 }
305}
306
83680571 307static void destroy(GtkWidget *widget, gpointer data)
308{
f49650a7 309 frontend *fe = (frontend *)data;
310 deactivate_timer(fe);
83680571 311 gtk_main_quit();
312}
313
2ef96bd6 314static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
7f77ea24 315{
2ef96bd6 316 frontend *fe = (frontend *)data;
1482ee76 317 int keyval;
7f77ea24 318
2ef96bd6 319 if (!fe->pixmap)
320 return TRUE;
7f77ea24 321
1482ee76 322 if (event->string[0] && !event->string[1])
323 keyval = (unsigned char)event->string[0];
c71454c0 324 else if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
325 event->keyval == GDK_KP_8)
1482ee76 326 keyval = CURSOR_UP;
c71454c0 327 else if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down ||
328 event->keyval == GDK_KP_2)
1482ee76 329 keyval = CURSOR_DOWN;
c71454c0 330 else if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left ||
331 event->keyval == GDK_KP_4)
1482ee76 332 keyval = CURSOR_LEFT;
c71454c0 333 else if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right ||
334 event->keyval == GDK_KP_6)
1482ee76 335 keyval = CURSOR_RIGHT;
c71454c0 336 else if (event->keyval == GDK_KP_Home || event->keyval == GDK_KP_7)
337 keyval = CURSOR_UP_LEFT;
338 else if (event->keyval == GDK_KP_End || event->keyval == GDK_KP_1)
339 keyval = CURSOR_DOWN_LEFT;
340 else if (event->keyval == GDK_KP_Page_Up || event->keyval == GDK_KP_9)
341 keyval = CURSOR_UP_RIGHT;
342 else if (event->keyval == GDK_KP_Page_Down || event->keyval == GDK_KP_3)
343 keyval = CURSOR_DOWN_RIGHT;
1482ee76 344 else
345 keyval = -1;
346
347 if (keyval >= 0 &&
348 !midend_process_key(fe->me, 0, 0, keyval))
2ef96bd6 349 gtk_widget_destroy(fe->window);
7f77ea24 350
351 return TRUE;
352}
353
2ef96bd6 354static gint button_event(GtkWidget *widget, GdkEventButton *event,
355 gpointer data)
7f77ea24 356{
2ef96bd6 357 frontend *fe = (frontend *)data;
358 int button;
359
360 if (!fe->pixmap)
361 return TRUE;
362
74a4e547 363 if (event->type != GDK_BUTTON_PRESS && event->type != GDK_BUTTON_RELEASE)
2ef96bd6 364 return TRUE;
7f77ea24 365
b0f06719 366 if (event->button == 2 || (event->state & GDK_SHIFT_MASK))
2ef96bd6 367 button = MIDDLE_BUTTON;
b0f06719 368 else if (event->button == 1)
369 button = LEFT_BUTTON;
2ef96bd6 370 else if (event->button == 3)
371 button = RIGHT_BUTTON;
372 else
373 return FALSE; /* don't even know what button! */
374
74a4e547 375 if (event->type == GDK_BUTTON_RELEASE)
376 button += LEFT_RELEASE - LEFT_BUTTON;
377
378 if (!midend_process_key(fe->me, event->x, event->y, button))
379 gtk_widget_destroy(fe->window);
380
381 return TRUE;
382}
383
384static gint motion_event(GtkWidget *widget, GdkEventMotion *event,
385 gpointer data)
386{
387 frontend *fe = (frontend *)data;
388 int button;
389
390 if (!fe->pixmap)
391 return TRUE;
392
74a4e547 393 if (event->state & (GDK_BUTTON2_MASK | GDK_SHIFT_MASK))
394 button = MIDDLE_DRAG;
395 else if (event->state & GDK_BUTTON1_MASK)
396 button = LEFT_DRAG;
397 else if (event->state & GDK_BUTTON3_MASK)
398 button = RIGHT_DRAG;
399 else
400 return FALSE; /* don't even know what button! */
401
2ef96bd6 402 if (!midend_process_key(fe->me, event->x, event->y, button))
403 gtk_widget_destroy(fe->window);
7f77ea24 404
405 return TRUE;
406}
407
2ef96bd6 408static gint expose_area(GtkWidget *widget, GdkEventExpose *event,
409 gpointer data)
83680571 410{
2ef96bd6 411 frontend *fe = (frontend *)data;
412
413 if (fe->pixmap) {
414 gdk_draw_pixmap(widget->window,
415 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
416 fe->pixmap,
417 event->area.x, event->area.y,
418 event->area.x, event->area.y,
419 event->area.width, event->area.height);
420 }
421 return TRUE;
422}
423
fd1a1a2b 424static gint map_window(GtkWidget *widget, GdkEvent *event,
425 gpointer data)
426{
427 frontend *fe = (frontend *)data;
428
429 /*
430 * Apparently we need to do this because otherwise the status
431 * bar will fail to update immediately. Annoying, but there we
432 * go.
433 */
434 gtk_widget_queue_draw(fe->window);
435
436 return TRUE;
437}
438
2ef96bd6 439static gint configure_area(GtkWidget *widget,
440 GdkEventConfigure *event, gpointer data)
441{
442 frontend *fe = (frontend *)data;
443 GdkGC *gc;
444
eb2ad6f1 445 if (fe->pixmap)
446 gdk_pixmap_unref(fe->pixmap);
447
2ef96bd6 448 fe->pixmap = gdk_pixmap_new(widget->window, fe->w, fe->h, -1);
449
450 gc = gdk_gc_new(fe->area->window);
451 gdk_gc_set_foreground(gc, &fe->colours[0]);
452 gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->w, fe->h);
453 gdk_gc_unref(gc);
454
455 midend_redraw(fe->me);
456
457 return TRUE;
458}
459
460static gint timer_func(gpointer data)
461{
462 frontend *fe = (frontend *)data;
463
8c1fd974 464 if (fe->timer_active) {
465 struct timeval now;
466 float elapsed;
467 gettimeofday(&now, NULL);
468 elapsed = ((now.tv_usec - fe->last_time.tv_usec) * 0.000001F +
469 (now.tv_sec - fe->last_time.tv_sec));
470 midend_timer(fe->me, elapsed); /* may clear timer_active */
471 fe->last_time = now;
472 }
2ef96bd6 473
474 return fe->timer_active;
475}
476
477void deactivate_timer(frontend *fe)
478{
20ee89e3 479 if (fe->timer_active)
480 gtk_timeout_remove(fe->timer_id);
2ef96bd6 481 fe->timer_active = FALSE;
482}
483
484void activate_timer(frontend *fe)
485{
8c1fd974 486 if (!fe->timer_active) {
20ee89e3 487 fe->timer_id = gtk_timeout_add(20, timer_func, fe);
8c1fd974 488 gettimeofday(&fe->last_time, NULL);
489 }
2ef96bd6 490 fe->timer_active = TRUE;
491}
492
c8230524 493static void window_destroy(GtkWidget *widget, gpointer data)
494{
495 gtk_main_quit();
496}
497
498static void errmsg_button_clicked(GtkButton *button, gpointer data)
499{
500 gtk_widget_destroy(GTK_WIDGET(data));
501}
502
a163b7c2 503static int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
504{
505 GtkObject *cancelbutton = GTK_OBJECT(data);
506
507 /*
508 * `Escape' effectively clicks the cancel button
509 */
510 if (event->keyval == GDK_Escape) {
511 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton), "clicked");
512 return TRUE;
513 }
514
515 return FALSE;
516}
517
c8230524 518void error_box(GtkWidget *parent, char *msg)
519{
520 GtkWidget *window, *hbox, *text, *ok;
521
522 window = gtk_dialog_new();
523 text = gtk_label_new(msg);
524 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
525 hbox = gtk_hbox_new(FALSE, 0);
526 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
527 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
528 hbox, FALSE, FALSE, 20);
529 gtk_widget_show(text);
530 gtk_widget_show(hbox);
531 gtk_window_set_title(GTK_WINDOW(window), "Error");
532 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
533 ok = gtk_button_new_with_label("OK");
534 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area),
535 ok, FALSE, FALSE, 0);
536 gtk_widget_show(ok);
537 GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT);
538 gtk_window_set_default(GTK_WINDOW(window), ok);
539 gtk_signal_connect(GTK_OBJECT(ok), "clicked",
540 GTK_SIGNAL_FUNC(errmsg_button_clicked), window);
a1ed9f0e 541 gtk_signal_connect(GTK_OBJECT(window), "destroy",
542 GTK_SIGNAL_FUNC(window_destroy), NULL);
a163b7c2 543 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
544 GTK_SIGNAL_FUNC(win_key_press), ok);
c8230524 545 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
546 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent));
547 //set_transient_window_pos(parent, window);
548 gtk_widget_show(window);
549 gtk_main();
550}
551
552static void config_ok_button_clicked(GtkButton *button, gpointer data)
553{
554 frontend *fe = (frontend *)data;
555 char *err;
556
5928817c 557 err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
c8230524 558
559 if (err)
560 error_box(fe->cfgbox, err);
561 else {
562 fe->cfgret = TRUE;
563 gtk_widget_destroy(fe->cfgbox);
564 }
565}
566
567static void config_cancel_button_clicked(GtkButton *button, gpointer data)
568{
569 frontend *fe = (frontend *)data;
570
571 gtk_widget_destroy(fe->cfgbox);
572}
573
a163b7c2 574static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
575{
576 /*
577 * GtkEntry has a nasty habit of eating the Return key, which
578 * is unhelpful since it doesn't actually _do_ anything with it
579 * (it calls gtk_widget_activate, but our edit boxes never need
580 * activating). So I catch Return before GtkEntry sees it, and
581 * pass it straight on to the parent widget. Effect: hitting
582 * Return in an edit box will now activate the default button
583 * in the dialog just like it will everywhere else.
584 */
585 if (event->keyval == GDK_Return && widget->parent != NULL) {
586 gint return_val;
587 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
588 gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event",
589 event, &return_val);
590 return return_val;
591 }
592 return FALSE;
593}
594
c8230524 595static void editbox_changed(GtkEditable *ed, gpointer data)
596{
597 config_item *i = (config_item *)data;
598
599 sfree(i->sval);
600 i->sval = dupstr(gtk_entry_get_text(GTK_ENTRY(ed)));
601}
602
603static void button_toggled(GtkToggleButton *tb, gpointer data)
604{
605 config_item *i = (config_item *)data;
606
607 i->ival = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb));
608}
609
610static void droplist_sel(GtkMenuItem *item, gpointer data)
611{
612 config_item *i = (config_item *)data;
613
614 i->ival = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
615 "user-data"));
616}
617
5928817c 618static int get_config(frontend *fe, int which)
c8230524 619{
a163b7c2 620 GtkWidget *w, *table, *cancel;
5928817c 621 char *title;
c8230524 622 config_item *i;
623 int y;
624
5928817c 625 fe->cfg = midend_get_config(fe->me, which, &title);
626 fe->cfg_which = which;
c8230524 627 fe->cfgret = FALSE;
628
629 fe->cfgbox = gtk_dialog_new();
5928817c 630 gtk_window_set_title(GTK_WINDOW(fe->cfgbox), title);
631 sfree(title);
c8230524 632
633 w = gtk_button_new_with_label("OK");
634 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area),
635 w, FALSE, FALSE, 0);
636 gtk_widget_show(w);
637 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
638 gtk_window_set_default(GTK_WINDOW(fe->cfgbox), w);
639 gtk_signal_connect(GTK_OBJECT(w), "clicked",
640 GTK_SIGNAL_FUNC(config_ok_button_clicked), fe);
641
642 w = gtk_button_new_with_label("Cancel");
643 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area),
644 w, FALSE, FALSE, 0);
645 gtk_widget_show(w);
646 gtk_signal_connect(GTK_OBJECT(w), "clicked",
647 GTK_SIGNAL_FUNC(config_cancel_button_clicked), fe);
a163b7c2 648 cancel = w;
c8230524 649
650 table = gtk_table_new(1, 2, FALSE);
651 y = 0;
652 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->vbox),
653 table, FALSE, FALSE, 0);
654 gtk_widget_show(table);
655
95709966 656 for (i = fe->cfg; i->type != C_END; i++) {
c8230524 657 gtk_table_resize(GTK_TABLE(table), y+1, 2);
658
659 switch (i->type) {
95709966 660 case C_STRING:
c8230524 661 /*
662 * Edit box with a label beside it.
663 */
664
665 w = gtk_label_new(i->name);
666 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5);
667 gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1,
668 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
669 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
670 3, 3);
671 gtk_widget_show(w);
672
673 w = gtk_entry_new();
674 gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1,
675 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
676 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
677 3, 3);
678 gtk_entry_set_text(GTK_ENTRY(w), i->sval);
679 gtk_signal_connect(GTK_OBJECT(w), "changed",
680 GTK_SIGNAL_FUNC(editbox_changed), i);
a163b7c2 681 gtk_signal_connect(GTK_OBJECT(w), "key_press_event",
682 GTK_SIGNAL_FUNC(editbox_key), NULL);
c8230524 683 gtk_widget_show(w);
684
685 break;
686
95709966 687 case C_BOOLEAN:
c8230524 688 /*
689 * Simple checkbox.
690 */
691 w = gtk_check_button_new_with_label(i->name);
692 gtk_signal_connect(GTK_OBJECT(w), "toggled",
693 GTK_SIGNAL_FUNC(button_toggled), i);
694 gtk_table_attach(GTK_TABLE(table), w, 0, 2, y, y+1,
695 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
696 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
697 3, 3);
698 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), i->ival);
699 gtk_widget_show(w);
700 break;
701
95709966 702 case C_CHOICES:
c8230524 703 /*
704 * Drop-down list (GtkOptionMenu).
705 */
706
707 w = gtk_label_new(i->name);
708 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5);
709 gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1,
710 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
711 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
712 3, 3);
713 gtk_widget_show(w);
714
715 w = gtk_option_menu_new();
716 gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1,
717 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
718 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
719 3, 3);
720 gtk_widget_show(w);
721
722 {
723 int c, val;
724 char *p, *q, *name;
725 GtkWidget *menuitem;
726 GtkWidget *menu = gtk_menu_new();
727
728 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), menu);
729
730 c = *i->sval;
731 p = i->sval+1;
732 val = 0;
733
734 while (*p) {
735 q = p;
736 while (*q && *q != c)
737 q++;
738
739 name = snewn(q-p+1, char);
740 strncpy(name, p, q-p);
741 name[q-p] = '\0';
742
743 if (*q) q++; /* eat delimiter */
744
745 menuitem = gtk_menu_item_new_with_label(name);
746 gtk_container_add(GTK_CONTAINER(menu), menuitem);
747 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
748 GINT_TO_POINTER(val));
749 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
750 GTK_SIGNAL_FUNC(droplist_sel), i);
751 gtk_widget_show(menuitem);
752
753 val++;
754
755 p = q;
756 }
757
758 gtk_option_menu_set_history(GTK_OPTION_MENU(w), i->ival);
759 }
760
761 break;
762 }
763
764 y++;
765 }
766
767 gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "destroy",
768 GTK_SIGNAL_FUNC(window_destroy), NULL);
a163b7c2 769 gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "key_press_event",
770 GTK_SIGNAL_FUNC(win_key_press), cancel);
c8230524 771 gtk_window_set_modal(GTK_WINDOW(fe->cfgbox), TRUE);
772 gtk_window_set_transient_for(GTK_WINDOW(fe->cfgbox),
773 GTK_WINDOW(fe->window));
774 //set_transient_window_pos(fe->window, fe->cfgbox);
775 gtk_widget_show(fe->cfgbox);
776 gtk_main();
777
077f3cbe 778 free_cfg(fe->cfg);
c8230524 779
780 return fe->cfgret;
781}
782
eb2ad6f1 783static void menu_key_event(GtkMenuItem *menuitem, gpointer data)
784{
785 frontend *fe = (frontend *)data;
786 int key = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem),
787 "user-data"));
788 if (!midend_process_key(fe->me, 0, 0, key))
789 gtk_widget_destroy(fe->window);
790}
791
792static void menu_preset_event(GtkMenuItem *menuitem, gpointer data)
793{
794 frontend *fe = (frontend *)data;
795 game_params *params =
796 (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data");
797 int x, y;
798
799 midend_set_params(fe->me, params);
5928817c 800 midend_new_game(fe->me);
eb2ad6f1 801 midend_size(fe->me, &x, &y);
802 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
803 fe->w = x;
804 fe->h = y;
805}
806
c8230524 807static void menu_config_event(GtkMenuItem *menuitem, gpointer data)
808{
809 frontend *fe = (frontend *)data;
5928817c 810 int which = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem),
811 "user-data"));
c8230524 812 int x, y;
813
5928817c 814 if (!get_config(fe, which))
c8230524 815 return;
816
5928817c 817 midend_new_game(fe->me);
c8230524 818 midend_size(fe->me, &x, &y);
819 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
820 fe->w = x;
821 fe->h = y;
822}
823
eb2ad6f1 824static GtkWidget *add_menu_item_with_key(frontend *fe, GtkContainer *cont,
825 char *text, int key)
826{
827 GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
828 gtk_container_add(cont, menuitem);
829 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
830 GINT_TO_POINTER(key));
831 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
832 GTK_SIGNAL_FUNC(menu_key_event), fe);
833 gtk_widget_show(menuitem);
834 return menuitem;
835}
836
837static void add_menu_separator(GtkContainer *cont)
838{
839 GtkWidget *menuitem = gtk_menu_item_new();
840 gtk_container_add(cont, menuitem);
841 gtk_widget_show(menuitem);
842}
843
8b7938e7 844static frontend *new_window(char *game_id, char **error)
2ef96bd6 845{
846 frontend *fe;
eb2ad6f1 847 GtkBox *vbox;
848 GtkWidget *menubar, *menu, *menuitem;
849 int x, y, n;
83680571 850
2ef96bd6 851 fe = snew(frontend);
83680571 852
be8d5aa1 853 fe->me = midend_new(fe, &thegame);
8b7938e7 854 if (game_id) {
855 *error = midend_game_id(fe->me, game_id, FALSE);
856 if (*error) {
857 midend_free(fe->me);
858 sfree(fe);
859 return NULL;
860 }
861 }
5928817c 862 midend_new_game(fe->me);
7f77ea24 863
2ef96bd6 864 fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
be8d5aa1 865 gtk_window_set_title(GTK_WINDOW(fe->window), thegame.name);
ff2f787b 866#if 0
867 gtk_window_set_resizable(GTK_WINDOW(fe->window), FALSE);
868#else
869 gtk_window_set_policy(GTK_WINDOW(fe->window), FALSE, FALSE, TRUE);
870#endif
eb2ad6f1 871 vbox = GTK_BOX(gtk_vbox_new(FALSE, 0));
872 gtk_container_add(GTK_CONTAINER(fe->window), GTK_WIDGET(vbox));
873 gtk_widget_show(GTK_WIDGET(vbox));
874
875 menubar = gtk_menu_bar_new();
876 gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0);
877 gtk_widget_show(menubar);
878
879 menuitem = gtk_menu_item_new_with_label("Game");
880 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
881 gtk_widget_show(menuitem);
882
883 menu = gtk_menu_new();
884 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
885
886 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "New", 'n');
887 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Restart", 'r');
888
5928817c 889 menuitem = gtk_menu_item_new_with_label("Specific...");
890 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
891 GINT_TO_POINTER(CFG_SEED));
892 gtk_container_add(GTK_CONTAINER(menu), menuitem);
893 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
894 GTK_SIGNAL_FUNC(menu_config_event), fe);
895 gtk_widget_show(menuitem);
896
be8d5aa1 897 if ((n = midend_num_presets(fe->me)) > 0 || thegame.can_configure) {
eb2ad6f1 898 GtkWidget *submenu;
899 int i;
900
901 menuitem = gtk_menu_item_new_with_label("Type");
c8230524 902 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
eb2ad6f1 903 gtk_widget_show(menuitem);
904
905 submenu = gtk_menu_new();
906 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
907
908 for (i = 0; i < n; i++) {
909 char *name;
910 game_params *params;
911
912 midend_fetch_preset(fe->me, i, &name, &params);
913
914 menuitem = gtk_menu_item_new_with_label(name);
915 gtk_container_add(GTK_CONTAINER(submenu), menuitem);
916 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", params);
917 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
918 GTK_SIGNAL_FUNC(menu_preset_event), fe);
919 gtk_widget_show(menuitem);
920 }
c8230524 921
be8d5aa1 922 if (thegame.can_configure) {
c8230524 923 menuitem = gtk_menu_item_new_with_label("Custom...");
5928817c 924 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
925 GPOINTER_TO_INT(CFG_SETTINGS));
c8230524 926 gtk_container_add(GTK_CONTAINER(submenu), menuitem);
927 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
928 GTK_SIGNAL_FUNC(menu_config_event), fe);
929 gtk_widget_show(menuitem);
930 }
eb2ad6f1 931 }
932
933 add_menu_separator(GTK_CONTAINER(menu));
934 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Undo", 'u');
935 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Redo", '\x12');
936 add_menu_separator(GTK_CONTAINER(menu));
937 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Exit", 'q');
7f77ea24 938
2ef96bd6 939 {
940 int i, ncolours;
941 float *colours;
942 gboolean *success;
7f77ea24 943
2ef96bd6 944 fe->colmap = gdk_colormap_get_system();
945 colours = midend_colours(fe->me, &ncolours);
946 fe->ncolours = ncolours;
947 fe->colours = snewn(ncolours, GdkColor);
948 for (i = 0; i < ncolours; i++) {
949 fe->colours[i].red = colours[i*3] * 0xFFFF;
950 fe->colours[i].green = colours[i*3+1] * 0xFFFF;
951 fe->colours[i].blue = colours[i*3+2] * 0xFFFF;
952 }
953 success = snewn(ncolours, gboolean);
954 gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours,
955 FALSE, FALSE, success);
956 for (i = 0; i < ncolours; i++) {
957 if (!success[i])
958 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
959 i, fe->colours[i].red >> 8,
960 fe->colours[i].green >> 8,
961 fe->colours[i].blue >> 8);
962 }
963 }
7f77ea24 964
fd1a1a2b 965 if (midend_wants_statusbar(fe->me)) {
5725d728 966 GtkWidget *viewport;
967 GtkRequisition req;
968
969 viewport = gtk_viewport_new(NULL, NULL);
970 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
fd1a1a2b 971 fe->statusbar = gtk_statusbar_new();
5725d728 972 gtk_container_add(GTK_CONTAINER(viewport), fe->statusbar);
973 gtk_widget_show(viewport);
974 gtk_box_pack_end(vbox, viewport, FALSE, FALSE, 0);
fd1a1a2b 975 gtk_widget_show(fe->statusbar);
976 fe->statusctx = gtk_statusbar_get_context_id
977 (GTK_STATUSBAR(fe->statusbar), "game");
978 gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx,
5725d728 979 "test");
980 gtk_widget_size_request(fe->statusbar, &req);
981#if 0
982 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
983#endif
60d42abc 984 gtk_widget_set_usize(viewport, -1, req.height);
fd1a1a2b 985 } else
986 fe->statusbar = NULL;
987
2ef96bd6 988 fe->area = gtk_drawing_area_new();
989 midend_size(fe->me, &x, &y);
990 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
991 fe->w = x;
992 fe->h = y;
993
eb2ad6f1 994 gtk_box_pack_end(vbox, fe->area, FALSE, FALSE, 0);
2ef96bd6 995
996 fe->pixmap = NULL;
4efb3868 997 fe->fonts = NULL;
998 fe->nfonts = fe->fontsize = 0;
2ef96bd6 999
20ee89e3 1000 fe->timer_active = FALSE;
1001
2ef96bd6 1002 gtk_signal_connect(GTK_OBJECT(fe->window), "destroy",
1003 GTK_SIGNAL_FUNC(destroy), fe);
1004 gtk_signal_connect(GTK_OBJECT(fe->window), "key_press_event",
1005 GTK_SIGNAL_FUNC(key_event), fe);
1006 gtk_signal_connect(GTK_OBJECT(fe->area), "button_press_event",
1007 GTK_SIGNAL_FUNC(button_event), fe);
74a4e547 1008 gtk_signal_connect(GTK_OBJECT(fe->area), "button_release_event",
1009 GTK_SIGNAL_FUNC(button_event), fe);
1010 gtk_signal_connect(GTK_OBJECT(fe->area), "motion_notify_event",
1011 GTK_SIGNAL_FUNC(motion_event), fe);
2ef96bd6 1012 gtk_signal_connect(GTK_OBJECT(fe->area), "expose_event",
1013 GTK_SIGNAL_FUNC(expose_area), fe);
fd1a1a2b 1014 gtk_signal_connect(GTK_OBJECT(fe->window), "map_event",
1015 GTK_SIGNAL_FUNC(map_window), fe);
2ef96bd6 1016 gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event",
1017 GTK_SIGNAL_FUNC(configure_area), fe);
1018
74a4e547 1019 gtk_widget_add_events(GTK_WIDGET(fe->area),
1020 GDK_BUTTON_PRESS_MASK |
1021 GDK_BUTTON_RELEASE_MASK |
1022 GDK_BUTTON_MOTION_MASK);
2ef96bd6 1023
1024 gtk_widget_show(fe->area);
1025 gtk_widget_show(fe->window);
1026
1027 return fe;
83680571 1028}
1029
1030int main(int argc, char **argv)
1031{
8b7938e7 1032 char *pname = argv[0];
1033 char *error;
1034
d91e1fc9 1035 /*
1036 * Special standalone mode for generating puzzle IDs on the
1037 * command line. Useful for generating puzzles to be printed
1038 * out and solved offline (for puzzles where that even makes
1039 * sense - Solo, for example, is a lot more pencil-and-paper
1040 * friendly than Net!)
1041 *
1042 * Usage:
1043 *
1044 * <puzzle-name> --generate [<n> [<params>]]
1045 *
1046 * <n>, if present, is the number of puzzle IDs to generate.
1047 * <params>, if present, is the same type of parameter string
1048 * you would pass to the puzzle when running it in GUI mode,
1049 * including optional extras such as the expansion factor in
1050 * Rectangles and the difficulty level in Solo.
1051 *
1052 * If you specify <params>, you must also specify <n> (although
1053 * you may specify it to be 1). Sorry; that was the
1054 * simplest-to-parse command-line syntax I came up with.
1055 */
1056 if (argc > 1 && !strcmp(argv[1], "--generate")) {
1057 int n = 1;
1058 char *params = NULL;
1059 game_params *par;
1060 random_state *rs;
1061 char *parstr;
1062
1063 {
1064 void *seed;
1065 int seedlen;
1066 get_random_seed(&seed, &seedlen);
1067 rs = random_init(seed, seedlen);
1068 }
8b7938e7 1069
d91e1fc9 1070 if (argc > 2)
1071 n = atoi(argv[2]);
1072 if (argc > 3)
1073 params = argv[3];
1074
1075 if (params)
1076 par = thegame.decode_params(params);
1077 else
1078 par = thegame.default_params();
1079 parstr = thegame.encode_params(par);
1080
1081 while (n-- > 0) {
1082 char *seed = thegame.new_seed(par, rs);
1083 printf("%s:%s\n", parstr, seed);
1084 sfree(seed);
1085 }
8b7938e7 1086
d91e1fc9 1087 return 0;
1088 } else {
1089
1090 gtk_init(&argc, &argv);
1091
1092 if (!new_window(argc > 1 ? argv[1] : NULL, &error)) {
1093 fprintf(stderr, "%s: %s\n", pname, error);
1094 return 1;
1095 }
1096
1097 gtk_main();
1098 }
83680571 1099
1100 return 0;
1101}