2 * gtk.c: GTK front end for my puzzle collection.
17 #include <gdk/gdkkeysyms.h>
19 #include <gdk-pixbuf/gdk-pixbuf.h>
23 #include <X11/Xutil.h>
24 #include <X11/Xatom.h>
28 #if GTK_CHECK_VERSION(2,0,0)
30 # ifdef PANGO_VERSION_CHECK
31 # if PANGO_VERSION_CHECK(1,8,0)
32 # define HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
36 #if !GTK_CHECK_VERSION(2,4,0)
39 #if GTK_CHECK_VERSION(2,8,0)
44 static FILE *debug_fp = NULL;
49 debug_fp = fopen("debug.log", "w");
60 void debug_printf(char *fmt, ...)
66 vsprintf(buf, fmt, ap);
72 /* ----------------------------------------------------------------------
73 * Error reporting functions used elsewhere.
76 void fatal(char *fmt, ...)
80 fprintf(stderr, "fatal error: ");
83 vfprintf(stderr, fmt, ap);
86 fprintf(stderr, "\n");
90 /* ----------------------------------------------------------------------
91 * GTK front end to puzzles.
94 static void changed_preset(frontend *fe);
98 PangoFontDescription *desc;
107 * This structure holds all the data relevant to a single window.
108 * In principle this would allow us to open multiple independent
109 * puzzle windows, although I can't currently see any real point in
110 * doing so. I'm just coding cleanly because there's no
111 * particularly good reason not to.
115 GtkAccelGroup *accelgroup;
117 GtkWidget *statusbar;
122 const float *colours;
124 cairo_surface_t *image;
133 int bbox_l, bbox_r, bbox_u, bbox_d;
134 int timer_active, timer_id;
135 struct timeval last_time;
137 int nfonts, fontsize;
139 int cfg_which, cfgret;
143 int pw, ph; /* pixmap size (w, h are area size) */
144 int ox, oy; /* offset of pixmap in drawing area */
148 GSList *preset_radio;
149 int n_preset_menu_items;
151 GtkWidget *preset_custom;
152 GtkWidget *copy_menu_item;
157 cairo_surface_t *image;
164 void get_random_seed(void **randseed, int *randseedsize)
166 struct timeval *tvp = snew(struct timeval);
167 gettimeofday(tvp, NULL);
168 *randseed = (void *)tvp;
169 *randseedsize = sizeof(struct timeval);
172 void frontend_default_colour(frontend *fe, float *output)
174 GdkColor col = fe->window->style->bg[GTK_STATE_NORMAL];
175 output[0] = col.red / 65535.0;
176 output[1] = col.green / 65535.0;
177 output[2] = col.blue / 65535.0;
180 void gtk_status_bar(void *handle, char *text)
182 frontend *fe = (frontend *)handle;
184 assert(fe->statusbar);
186 gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx);
187 gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, text);
190 /* ----------------------------------------------------------------------
191 * Cairo drawing functions.
196 static void setup_drawing(frontend *fe)
198 fe->cr = cairo_create(fe->image);
199 cairo_set_antialias(fe->cr, CAIRO_ANTIALIAS_GRAY);
200 cairo_set_line_width(fe->cr, 1.0);
201 cairo_set_line_cap(fe->cr, CAIRO_LINE_CAP_SQUARE);
202 cairo_set_line_join(fe->cr, CAIRO_LINE_JOIN_ROUND);
205 static void teardown_drawing(frontend *fe)
209 cairo_destroy(fe->cr);
212 cr = gdk_cairo_create(fe->pixmap);
213 cairo_set_source_surface(cr, fe->image, 0, 0);
217 fe->bbox_r - fe->bbox_l + 2,
218 fe->bbox_d - fe->bbox_u + 2);
223 static void snaffle_colours(frontend *fe)
225 fe->colours = midend_colours(fe->me, &fe->ncolours);
228 static void set_colour(frontend *fe, int colour)
230 cairo_set_source_rgb(fe->cr,
231 fe->colours[3*colour + 0],
232 fe->colours[3*colour + 1],
233 fe->colours[3*colour + 2]);
236 static void set_window_background(frontend *fe, int colour)
241 colmap = gdk_colormap_get_system();
242 backg.red = fe->colours[3*colour + 0] * 65535;
243 backg.green = fe->colours[3*colour + 1] * 65535;
244 backg.blue = fe->colours[3*colour + 2] * 65535;
245 if (!gdk_colormap_alloc_color(colmap, &backg, FALSE, FALSE)) {
246 g_error("couldn't allocate background (#%02x%02x%02x)\n",
247 backg.red >> 8, backg.green >> 8, backg.blue >> 8);
249 gdk_window_set_background(fe->area->window, &backg);
250 gdk_window_set_background(fe->window->window, &backg);
253 static PangoLayout *make_pango_layout(frontend *fe)
255 return (pango_cairo_create_layout(fe->cr));
258 static void draw_pango_layout(frontend *fe, PangoLayout *layout,
261 cairo_move_to(fe->cr, x, y);
262 pango_cairo_show_layout(fe->cr, layout);
265 static void save_screenshot_png(frontend *fe, const char *screenshot_file)
267 cairo_surface_write_to_png(fe->image, screenshot_file);
270 static void do_clip(frontend *fe, int x, int y, int w, int h)
272 cairo_new_path(fe->cr);
273 cairo_rectangle(fe->cr, x, y, w, h);
277 static void do_unclip(frontend *fe)
279 cairo_reset_clip(fe->cr);
282 static void do_draw_rect(frontend *fe, int x, int y, int w, int h)
285 cairo_new_path(fe->cr);
286 cairo_set_antialias(fe->cr, CAIRO_ANTIALIAS_NONE);
287 cairo_rectangle(fe->cr, x, y, w, h);
289 cairo_restore(fe->cr);
292 static void do_draw_line(frontend *fe, int x1, int y1, int x2, int y2)
294 cairo_new_path(fe->cr);
295 cairo_move_to(fe->cr, x1 + 0.5, y1 + 0.5);
296 cairo_line_to(fe->cr, x2 + 0.5, y2 + 0.5);
297 cairo_stroke(fe->cr);
300 static void do_draw_poly(frontend *fe, int *coords, int npoints,
301 int fillcolour, int outlinecolour)
305 cairo_new_path(fe->cr);
306 for (i = 0; i < npoints; i++)
307 cairo_line_to(fe->cr, coords[i*2] + 0.5, coords[i*2 + 1] + 0.5);
308 cairo_close_path(fe->cr);
309 if (fillcolour >= 0) {
310 set_colour(fe, fillcolour);
311 cairo_fill_preserve(fe->cr);
313 assert(outlinecolour >= 0);
314 set_colour(fe, outlinecolour);
315 cairo_stroke(fe->cr);
318 static void do_draw_circle(frontend *fe, int cx, int cy, int radius,
319 int fillcolour, int outlinecolour)
321 cairo_new_path(fe->cr);
322 cairo_arc(fe->cr, cx + 0.5, cy + 0.5, radius, 0, 2*PI);
323 cairo_close_path(fe->cr); /* Just in case... */
324 if (fillcolour >= 0) {
325 set_colour(fe, fillcolour);
326 cairo_fill_preserve(fe->cr);
328 assert(outlinecolour >= 0);
329 set_colour(fe, outlinecolour);
330 cairo_stroke(fe->cr);
333 static void setup_blitter(blitter *bl, int w, int h)
335 bl->image = cairo_image_surface_create(CAIRO_FORMAT_RGB24, w, h);
338 static void teardown_blitter(blitter *bl)
340 cairo_surface_destroy(bl->image);
343 static void do_blitter_save(frontend *fe, blitter *bl, int x, int y)
345 cairo_t *cr = cairo_create(bl->image);
347 cairo_set_source_surface(cr, fe->image, -x, -y);
352 static void do_blitter_load(frontend *fe, blitter *bl, int x, int y)
354 cairo_set_source_surface(fe->cr, bl->image, x, y);
358 static void clear_backing_store(frontend *fe)
363 static void setup_backing_store(frontend *fe)
368 fe->pixmap = gdk_pixmap_new(fe->area->window, fe->pw, fe->ph, -1);
369 fe->image = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
372 for (i = 0; i < 3; i++) {
374 case 0: cr = cairo_create(fe->image); break;
375 case 1: cr = gdk_cairo_create(fe->pixmap); break;
376 case 2: cr = gdk_cairo_create(fe->area->window); break;
378 cairo_set_source_rgb(cr,
379 fe->colours[0], fe->colours[1], fe->colours[2]);
385 static int backing_store_ok(frontend *fe)
387 return (!!fe->image);
390 static void teardown_backing_store(frontend *fe)
392 cairo_surface_destroy(fe->image);
393 gdk_pixmap_unref(fe->pixmap);
397 static void repaint_rectangle(frontend *fe, GtkWidget *widget,
398 int x, int y, int w, int h)
400 gdk_draw_pixmap(widget->window,
401 widget->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
403 x - fe->ox, y - fe->oy, x, y, w, h);
408 /* ----------------------------------------------------------------------
409 * GDK drawing functions.
414 static void setup_drawing(frontend *fe)
416 fe->gc = gdk_gc_new(fe->area->window);
419 static void teardown_drawing(frontend *fe)
421 gdk_gc_unref(fe->gc);
425 static void snaffle_colours(frontend *fe)
431 fe->colmap = gdk_colormap_get_system();
432 colours = midend_colours(fe->me, &ncolours);
433 fe->ncolours = ncolours;
434 fe->colours = snewn(ncolours, GdkColor);
435 for (i = 0; i < ncolours; i++) {
436 fe->colours[i].red = colours[i*3] * 0xFFFF;
437 fe->colours[i].green = colours[i*3+1] * 0xFFFF;
438 fe->colours[i].blue = colours[i*3+2] * 0xFFFF;
440 success = snewn(ncolours, gboolean);
441 gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours,
442 FALSE, FALSE, success);
443 for (i = 0; i < ncolours; i++) {
445 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
446 i, fe->colours[i].red >> 8,
447 fe->colours[i].green >> 8,
448 fe->colours[i].blue >> 8);
453 static void set_window_background(frontend *fe, int colour)
455 gdk_window_set_background(fe->area->window, &fe->colours[colour]);
456 gdk_window_set_background(fe->window->window, &fe->colours[colour]);
459 static void set_colour(frontend *fe, int colour)
461 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
465 static PangoLayout *make_pango_layout(frontend *fe)
467 return (pango_layout_new(gtk_widget_get_pango_context(fe->area)));
470 static void draw_pango_layout(frontend *fe, PangoLayout *layout,
473 gdk_draw_layout(fe->pixmap, fe->gc, x, y, layout);
477 static void save_screenshot_png(frontend *fe, const char *screenshot_file)
480 GError *gerror = NULL;
482 midend_redraw(fe->me);
484 pb = gdk_pixbuf_get_from_drawable(NULL, fe->pixmap,
485 NULL, 0, 0, 0, 0, -1, -1);
486 gdk_pixbuf_save(pb, screenshot_file, "png", &gerror, NULL);
489 static void do_clip(frontend *fe, int x, int y, int w, int h)
497 gdk_gc_set_clip_rectangle(fe->gc, &rect);
500 static void do_unclip(frontend *fe)
508 gdk_gc_set_clip_rectangle(fe->gc, &rect);
511 static void do_draw_rect(frontend *fe, int x, int y, int w, int h)
513 gdk_draw_rectangle(fe->pixmap, fe->gc, 1, x, y, w, h);
516 static void do_draw_line(frontend *fe, int x1, int y1, int x2, int y2)
518 gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2);
521 static void do_draw_poly(frontend *fe, int *coords, int npoints,
522 int fillcolour, int outlinecolour)
524 GdkPoint *points = snewn(npoints, GdkPoint);
527 for (i = 0; i < npoints; i++) {
528 points[i].x = coords[i*2];
529 points[i].y = coords[i*2+1];
532 if (fillcolour >= 0) {
533 set_colour(fe, fillcolour);
534 gdk_draw_polygon(fe->pixmap, fe->gc, TRUE, points, npoints);
536 assert(outlinecolour >= 0);
537 set_colour(fe, outlinecolour);
540 * In principle we ought to be able to use gdk_draw_polygon for
541 * the outline as well. In fact, it turns out to interact badly
542 * with a clipping region, for no terribly obvious reason, so I
543 * draw the outline as a sequence of lines instead.
545 for (i = 0; i < npoints; i++)
546 gdk_draw_line(fe->pixmap, fe->gc,
547 points[i].x, points[i].y,
548 points[(i+1)%npoints].x, points[(i+1)%npoints].y);
553 static void do_draw_circle(frontend *fe, int cx, int cy, int radius,
554 int fillcolour, int outlinecolour)
556 if (fillcolour >= 0) {
557 set_colour(fe, fillcolour);
558 gdk_draw_arc(fe->pixmap, fe->gc, TRUE,
559 cx - radius, cy - radius,
560 2 * radius, 2 * radius, 0, 360 * 64);
563 assert(outlinecolour >= 0);
564 set_colour(fe, outlinecolour);
565 gdk_draw_arc(fe->pixmap, fe->gc, FALSE,
566 cx - radius, cy - radius,
567 2 * radius, 2 * radius, 0, 360 * 64);
570 static void setup_blitter(blitter *bl, int w, int h)
573 * We can't create the pixmap right now, because fe->window
574 * might not yet exist. So we just cache w and h and create it
575 * during the firs call to blitter_save.
580 static void teardown_blitter(blitter *bl)
583 gdk_pixmap_unref(bl->pixmap);
586 static void do_blitter_save(frontend *fe, blitter *bl, int x, int y)
589 bl->pixmap = gdk_pixmap_new(fe->area->window, bl->w, bl->h, -1);
590 gdk_draw_pixmap(bl->pixmap,
591 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
593 x, y, 0, 0, bl->w, bl->h);
596 static void do_blitter_load(frontend *fe, blitter *bl, int x, int y)
599 gdk_draw_pixmap(fe->pixmap,
600 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
602 0, 0, x, y, bl->w, bl->h);
605 static void clear_backing_store(frontend *fe)
610 static void setup_backing_store(frontend *fe)
614 fe->pixmap = gdk_pixmap_new(fe->area->window, fe->pw, fe->ph, -1);
616 gc = gdk_gc_new(fe->area->window);
617 gdk_gc_set_foreground(gc, &fe->colours[0]);
618 gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->pw, fe->ph);
619 gdk_draw_rectangle(fe->area->window, gc, 1, 0, 0, fe->w, fe->h);
623 static int backing_store_ok(frontend *fe)
625 return (!!fe->pixmap);
628 static void teardown_backing_store(frontend *fe)
630 gdk_pixmap_unref(fe->pixmap);
634 static void repaint_rectangle(frontend *fe, GtkWidget *widget,
635 int x, int y, int w, int h)
637 gdk_draw_pixmap(widget->window,
638 widget->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
640 x - fe->ox, y - fe->oy, x, y, w, h);
645 /* ----------------------------------------------------------------------
646 * Pango font functions.
651 static void add_font(frontend *fe, int index, int fonttype, int fontsize)
654 * Use Pango to find the closest match to the requested
657 PangoFontDescription *fd;
659 fd = pango_font_description_new();
660 /* `Monospace' and `Sans' are meta-families guaranteed to exist */
661 pango_font_description_set_family(fd, fonttype == FONT_FIXED ?
662 "Monospace" : "Sans");
663 pango_font_description_set_weight(fd, PANGO_WEIGHT_BOLD);
665 * I found some online Pango documentation which
666 * described a function called
667 * pango_font_description_set_absolute_size(), which is
668 * _exactly_ what I want here. Unfortunately, none of
669 * my local Pango installations have it (presumably
670 * they're too old), so I'm going to have to hack round
671 * it by figuring out the point size myself. This
672 * limits me to X and probably also breaks in later
673 * Pango installations, so ideally I should add another
674 * CHECK_VERSION type ifdef and use set_absolute_size
675 * where available. All very annoying.
677 #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
678 pango_font_description_set_absolute_size(fd, PANGO_SCALE*fontsize);
681 Display *d = GDK_DISPLAY();
682 int s = DefaultScreen(d);
684 (PANGO_SCALE * 72.27 / 25.4) *
685 ((double) DisplayWidthMM(d, s) / DisplayWidth (d, s));
686 pango_font_description_set_size(fd, resolution * fontsize);
689 fe->fonts[index].desc = fd;
692 static void align_and_draw_text(frontend *fe,
693 int index, int align, int x, int y,
699 layout = make_pango_layout(fe);
704 pango_layout_set_font_description(layout, fe->fonts[index].desc);
705 pango_layout_set_text(layout, text, strlen(text));
706 pango_layout_get_pixel_extents(layout, NULL, &rect);
708 if (align & ALIGN_VCENTRE)
709 rect.y -= rect.height / 2;
711 rect.y -= rect.height;
713 if (align & ALIGN_HCENTRE)
714 rect.x -= rect.width / 2;
715 else if (align & ALIGN_HRIGHT)
716 rect.x -= rect.width;
718 draw_pango_layout(fe, layout, rect.x + x, rect.y + y);
720 g_object_unref(layout);
725 /* ----------------------------------------------------------------------
726 * Old-fashioned font functions.
731 static void add_font(int index, int fonttype, int fontsize)
734 * In GTK 1.2, I don't know of any plausible way to
735 * pick a suitable font, so I'm just going to be
738 fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ?
739 "fixed" : "variable");
742 static void align_and_draw_text(int index, int align, int x, int y,
745 int lb, rb, wid, asc, desc;
748 * Measure vertical string extents with respect to the same
751 gdk_string_extents(fe->fonts[i].font,
752 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
753 &lb, &rb, &wid, &asc, &desc);
754 if (align & ALIGN_VCENTRE)
755 y += asc - (asc+desc)/2;
760 * ... but horizontal extents with respect to the provided
761 * string. This means that multiple pieces of text centred
762 * on the same y-coordinate don't have different baselines.
764 gdk_string_extents(fe->fonts[i].font, text,
765 &lb, &rb, &wid, &asc, &desc);
767 if (align & ALIGN_HCENTRE)
769 else if (align & ALIGN_HRIGHT)
773 * Actually draw the text.
775 gdk_draw_string(fe->pixmap, fe->fonts[i].font, fe->gc, x, y, text);
780 /* ----------------------------------------------------------------------
781 * The exported drawing functions.
784 void gtk_start_draw(void *handle)
786 frontend *fe = (frontend *)handle;
794 void gtk_clip(void *handle, int x, int y, int w, int h)
796 frontend *fe = (frontend *)handle;
797 do_clip(fe, x, y, w, h);
800 void gtk_unclip(void *handle)
802 frontend *fe = (frontend *)handle;
806 void gtk_draw_text(void *handle, int x, int y, int fonttype, int fontsize,
807 int align, int colour, char *text)
809 frontend *fe = (frontend *)handle;
813 * Find or create the font.
815 for (i = 0; i < fe->nfonts; i++)
816 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
819 if (i == fe->nfonts) {
820 if (fe->fontsize <= fe->nfonts) {
821 fe->fontsize = fe->nfonts + 10;
822 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
827 fe->fonts[i].type = fonttype;
828 fe->fonts[i].size = fontsize;
829 add_font(fe, i, fonttype, fontsize);
835 set_colour(fe, colour);
836 align_and_draw_text(fe, i, align, x, y, text);
839 void gtk_draw_rect(void *handle, int x, int y, int w, int h, int colour)
841 frontend *fe = (frontend *)handle;
842 set_colour(fe, colour);
843 do_draw_rect(fe, x, y, w, h);
846 void gtk_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
848 frontend *fe = (frontend *)handle;
849 set_colour(fe, colour);
850 do_draw_line(fe, x1, y1, x2, y2);
853 void gtk_draw_poly(void *handle, int *coords, int npoints,
854 int fillcolour, int outlinecolour)
856 frontend *fe = (frontend *)handle;
857 do_draw_poly(fe, coords, npoints, fillcolour, outlinecolour);
860 void gtk_draw_circle(void *handle, int cx, int cy, int radius,
861 int fillcolour, int outlinecolour)
863 frontend *fe = (frontend *)handle;
864 do_draw_circle(fe, cx, cy, radius, fillcolour, outlinecolour);
867 blitter *gtk_blitter_new(void *handle, int w, int h)
869 blitter *bl = snew(blitter);
870 setup_blitter(bl, w, h);
876 void gtk_blitter_free(void *handle, blitter *bl)
878 teardown_blitter(bl);
882 void gtk_blitter_save(void *handle, blitter *bl, int x, int y)
884 frontend *fe = (frontend *)handle;
885 do_blitter_save(fe, bl, x, y);
890 void gtk_blitter_load(void *handle, blitter *bl, int x, int y)
892 frontend *fe = (frontend *)handle;
893 if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) {
897 do_blitter_load(fe, bl, x, y);
900 void gtk_draw_update(void *handle, int x, int y, int w, int h)
902 frontend *fe = (frontend *)handle;
903 if (fe->bbox_l > x ) fe->bbox_l = x ;
904 if (fe->bbox_r < x+w) fe->bbox_r = x+w;
905 if (fe->bbox_u > y ) fe->bbox_u = y ;
906 if (fe->bbox_d < y+h) fe->bbox_d = y+h;
909 void gtk_end_draw(void *handle)
911 frontend *fe = (frontend *)handle;
913 teardown_drawing(fe);
915 if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) {
916 repaint_rectangle(fe, fe->area,
917 fe->bbox_l - 1 + fe->ox,
918 fe->bbox_u - 1 + fe->oy,
919 fe->bbox_r - fe->bbox_l + 2,
920 fe->bbox_d - fe->bbox_u + 2);
925 char *gtk_text_fallback(void *handle, const char *const *strings, int nstrings)
928 * We assume Pango can cope with any UTF-8 likely to be emitted
931 return dupstr(strings[0]);
935 const struct drawing_api gtk_drawing = {
951 NULL, NULL, NULL, NULL, NULL, NULL, /* {begin,end}_{doc,page,puzzle} */
952 NULL, NULL, /* line_width, line_dotted */
960 static void destroy(GtkWidget *widget, gpointer data)
962 frontend *fe = (frontend *)data;
963 deactivate_timer(fe);
968 static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
970 frontend *fe = (frontend *)data;
972 int shift = (event->state & GDK_SHIFT_MASK) ? MOD_SHFT : 0;
973 int ctrl = (event->state & GDK_CONTROL_MASK) ? MOD_CTRL : 0;
975 if (!backing_store_ok(fe))
978 #if !GTK_CHECK_VERSION(2,0,0)
979 /* Gtk 1.2 passes a key event to this function even if it's also
980 * defined as an accelerator.
981 * Gtk 2 doesn't do this, and this function appears not to exist there. */
982 if (fe->accelgroup &&
983 gtk_accel_group_get_entry(fe->accelgroup,
984 event->keyval, event->state))
988 /* Handle mnemonics. */
989 if (gtk_window_activate_key(GTK_WINDOW(fe->window), event))
992 if (event->keyval == GDK_Up)
993 keyval = shift | ctrl | CURSOR_UP;
994 else if (event->keyval == GDK_KP_Up || event->keyval == GDK_KP_8)
995 keyval = MOD_NUM_KEYPAD | '8';
996 else if (event->keyval == GDK_Down)
997 keyval = shift | ctrl | CURSOR_DOWN;
998 else if (event->keyval == GDK_KP_Down || event->keyval == GDK_KP_2)
999 keyval = MOD_NUM_KEYPAD | '2';
1000 else if (event->keyval == GDK_Left)
1001 keyval = shift | ctrl | CURSOR_LEFT;
1002 else if (event->keyval == GDK_KP_Left || event->keyval == GDK_KP_4)
1003 keyval = MOD_NUM_KEYPAD | '4';
1004 else if (event->keyval == GDK_Right)
1005 keyval = shift | ctrl | CURSOR_RIGHT;
1006 else if (event->keyval == GDK_KP_Right || event->keyval == GDK_KP_6)
1007 keyval = MOD_NUM_KEYPAD | '6';
1008 else if (event->keyval == GDK_KP_Home || event->keyval == GDK_KP_7)
1009 keyval = MOD_NUM_KEYPAD | '7';
1010 else if (event->keyval == GDK_KP_End || event->keyval == GDK_KP_1)
1011 keyval = MOD_NUM_KEYPAD | '1';
1012 else if (event->keyval == GDK_KP_Page_Up || event->keyval == GDK_KP_9)
1013 keyval = MOD_NUM_KEYPAD | '9';
1014 else if (event->keyval == GDK_KP_Page_Down || event->keyval == GDK_KP_3)
1015 keyval = MOD_NUM_KEYPAD | '3';
1016 else if (event->keyval == GDK_KP_Insert || event->keyval == GDK_KP_0)
1017 keyval = MOD_NUM_KEYPAD | '0';
1018 else if (event->keyval == GDK_KP_Begin || event->keyval == GDK_KP_5)
1019 keyval = MOD_NUM_KEYPAD | '5';
1020 else if (event->keyval == GDK_BackSpace ||
1021 event->keyval == GDK_Delete ||
1022 event->keyval == GDK_KP_Delete)
1024 else if (event->string[0] && !event->string[1])
1025 keyval = (unsigned char)event->string[0];
1030 !midend_process_key(fe->me, 0, 0, keyval))
1031 gtk_widget_destroy(fe->window);
1036 static gint button_event(GtkWidget *widget, GdkEventButton *event,
1039 frontend *fe = (frontend *)data;
1042 if (!backing_store_ok(fe))
1045 if (event->type != GDK_BUTTON_PRESS && event->type != GDK_BUTTON_RELEASE)
1048 if (event->button == 2 || (event->state & GDK_SHIFT_MASK))
1049 button = MIDDLE_BUTTON;
1050 else if (event->button == 3 || (event->state & GDK_MOD1_MASK))
1051 button = RIGHT_BUTTON;
1052 else if (event->button == 1)
1053 button = LEFT_BUTTON;
1055 return FALSE; /* don't even know what button! */
1057 if (event->type == GDK_BUTTON_RELEASE)
1058 button += LEFT_RELEASE - LEFT_BUTTON;
1060 if (!midend_process_key(fe->me, event->x - fe->ox,
1061 event->y - fe->oy, button))
1062 gtk_widget_destroy(fe->window);
1067 static gint motion_event(GtkWidget *widget, GdkEventMotion *event,
1070 frontend *fe = (frontend *)data;
1073 if (!backing_store_ok(fe))
1076 if (event->state & (GDK_BUTTON2_MASK | GDK_SHIFT_MASK))
1077 button = MIDDLE_DRAG;
1078 else if (event->state & GDK_BUTTON1_MASK)
1080 else if (event->state & GDK_BUTTON3_MASK)
1081 button = RIGHT_DRAG;
1083 return FALSE; /* don't even know what button! */
1085 if (!midend_process_key(fe->me, event->x - fe->ox,
1086 event->y - fe->oy, button))
1087 gtk_widget_destroy(fe->window);
1088 #if GTK_CHECK_VERSION(2,12,0)
1089 gdk_event_request_motions(event);
1091 gdk_window_get_pointer(widget->window, NULL, NULL, NULL);
1097 static gint expose_area(GtkWidget *widget, GdkEventExpose *event,
1100 frontend *fe = (frontend *)data;
1102 if (backing_store_ok(fe)) {
1103 repaint_rectangle(fe, widget,
1104 event->area.x, event->area.y,
1105 event->area.width, event->area.height);
1110 static gint map_window(GtkWidget *widget, GdkEvent *event,
1113 frontend *fe = (frontend *)data;
1116 * Apparently we need to do this because otherwise the status
1117 * bar will fail to update immediately. Annoying, but there we
1120 gtk_widget_queue_draw(fe->window);
1125 static gint configure_area(GtkWidget *widget,
1126 GdkEventConfigure *event, gpointer data)
1128 frontend *fe = (frontend *)data;
1131 if (backing_store_ok(fe))
1132 teardown_backing_store(fe);
1134 x = fe->w = event->width;
1135 y = fe->h = event->height;
1136 midend_size(fe->me, &x, &y, TRUE);
1139 fe->ox = (fe->w - fe->pw) / 2;
1140 fe->oy = (fe->h - fe->ph) / 2;
1142 setup_backing_store(fe);
1143 midend_force_redraw(fe->me);
1148 static gint timer_func(gpointer data)
1150 frontend *fe = (frontend *)data;
1152 if (fe->timer_active) {
1155 gettimeofday(&now, NULL);
1156 elapsed = ((now.tv_usec - fe->last_time.tv_usec) * 0.000001F +
1157 (now.tv_sec - fe->last_time.tv_sec));
1158 midend_timer(fe->me, elapsed); /* may clear timer_active */
1159 fe->last_time = now;
1162 return fe->timer_active;
1165 void deactivate_timer(frontend *fe)
1168 return; /* can happen due to --generate */
1169 if (fe->timer_active)
1170 gtk_timeout_remove(fe->timer_id);
1171 fe->timer_active = FALSE;
1174 void activate_timer(frontend *fe)
1177 return; /* can happen due to --generate */
1178 if (!fe->timer_active) {
1179 fe->timer_id = gtk_timeout_add(20, timer_func, fe);
1180 gettimeofday(&fe->last_time, NULL);
1182 fe->timer_active = TRUE;
1185 static void window_destroy(GtkWidget *widget, gpointer data)
1190 static void msgbox_button_clicked(GtkButton *button, gpointer data)
1192 GtkWidget *window = GTK_WIDGET(data);
1195 ip = (int *)gtk_object_get_data(GTK_OBJECT(window), "user-data");
1196 v = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "user-data"));
1199 gtk_widget_destroy(GTK_WIDGET(data));
1202 static int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
1204 GtkObject *cancelbutton = GTK_OBJECT(data);
1207 * `Escape' effectively clicks the cancel button
1209 if (event->keyval == GDK_Escape) {
1210 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton), "clicked");
1217 enum { MB_OK, MB_YESNO };
1219 int message_box(GtkWidget *parent, char *title, char *msg, int centre,
1222 GtkWidget *window, *hbox, *text, *button;
1226 window = gtk_dialog_new();
1227 text = gtk_label_new(msg);
1228 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
1229 hbox = gtk_hbox_new(FALSE, 0);
1230 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
1231 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
1232 hbox, FALSE, FALSE, 20);
1233 gtk_widget_show(text);
1234 gtk_widget_show(hbox);
1235 gtk_window_set_title(GTK_WINDOW(window), title);
1236 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
1238 if (type == MB_OK) {
1239 titles = GTK_STOCK_OK "\0";
1242 assert(type == MB_YESNO);
1243 titles = GTK_STOCK_NO "\0" GTK_STOCK_YES "\0";
1250 button = gtk_button_new_from_stock(titles);
1251 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area),
1252 button, FALSE, FALSE, 0);
1253 gtk_widget_show(button);
1255 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1256 gtk_window_set_default(GTK_WINDOW(window), button);
1259 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
1260 GTK_SIGNAL_FUNC(win_key_press), button);
1262 gtk_signal_connect(GTK_OBJECT(button), "clicked",
1263 GTK_SIGNAL_FUNC(msgbox_button_clicked), window);
1264 gtk_object_set_data(GTK_OBJECT(button), "user-data",
1265 GINT_TO_POINTER(i));
1266 titles += strlen(titles)+1;
1269 gtk_object_set_data(GTK_OBJECT(window), "user-data",
1270 GINT_TO_POINTER(&i));
1271 gtk_signal_connect(GTK_OBJECT(window), "destroy",
1272 GTK_SIGNAL_FUNC(window_destroy), NULL);
1273 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
1274 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent));
1275 /* set_transient_window_pos(parent, window); */
1276 gtk_widget_show(window);
1279 return (type == MB_YESNO ? i == 1 : TRUE);
1282 void error_box(GtkWidget *parent, char *msg)
1284 message_box(parent, "Error", msg, FALSE, MB_OK);
1287 static void config_ok_button_clicked(GtkButton *button, gpointer data)
1289 frontend *fe = (frontend *)data;
1292 err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
1295 error_box(fe->cfgbox, err);
1298 gtk_widget_destroy(fe->cfgbox);
1303 static void config_cancel_button_clicked(GtkButton *button, gpointer data)
1305 frontend *fe = (frontend *)data;
1307 gtk_widget_destroy(fe->cfgbox);
1310 static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
1313 * GtkEntry has a nasty habit of eating the Return key, which
1314 * is unhelpful since it doesn't actually _do_ anything with it
1315 * (it calls gtk_widget_activate, but our edit boxes never need
1316 * activating). So I catch Return before GtkEntry sees it, and
1317 * pass it straight on to the parent widget. Effect: hitting
1318 * Return in an edit box will now activate the default button
1319 * in the dialog just like it will everywhere else.
1321 if (event->keyval == GDK_Return && widget->parent != NULL) {
1323 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
1324 gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event",
1325 event, &return_val);
1331 static void editbox_changed(GtkEditable *ed, gpointer data)
1333 config_item *i = (config_item *)data;
1336 i->sval = dupstr(gtk_entry_get_text(GTK_ENTRY(ed)));
1339 static void button_toggled(GtkToggleButton *tb, gpointer data)
1341 config_item *i = (config_item *)data;
1343 i->ival = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb));
1346 static void droplist_sel(GtkMenuItem *item, gpointer data)
1348 config_item *i = (config_item *)data;
1350 i->ival = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
1354 static int get_config(frontend *fe, int which)
1356 GtkWidget *w, *table, *cancel;
1361 fe->cfg = midend_get_config(fe->me, which, &title);
1362 fe->cfg_which = which;
1365 fe->cfgbox = gtk_dialog_new();
1366 gtk_window_set_title(GTK_WINDOW(fe->cfgbox), title);
1369 w = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
1370 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area),
1371 w, FALSE, FALSE, 0);
1373 gtk_signal_connect(GTK_OBJECT(w), "clicked",
1374 GTK_SIGNAL_FUNC(config_cancel_button_clicked), fe);
1377 w = gtk_button_new_from_stock(GTK_STOCK_OK);
1378 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area),
1379 w, FALSE, FALSE, 0);
1381 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
1382 gtk_window_set_default(GTK_WINDOW(fe->cfgbox), w);
1383 gtk_signal_connect(GTK_OBJECT(w), "clicked",
1384 GTK_SIGNAL_FUNC(config_ok_button_clicked), fe);
1386 table = gtk_table_new(1, 2, FALSE);
1388 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->vbox),
1389 table, FALSE, FALSE, 0);
1390 gtk_widget_show(table);
1392 for (i = fe->cfg; i->type != C_END; i++) {
1393 gtk_table_resize(GTK_TABLE(table), y+1, 2);
1398 * Edit box with a label beside it.
1401 w = gtk_label_new(i->name);
1402 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5);
1403 gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1,
1404 GTK_SHRINK | GTK_FILL,
1405 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
1409 w = gtk_entry_new();
1410 gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1,
1411 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
1412 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
1414 gtk_entry_set_text(GTK_ENTRY(w), i->sval);
1415 gtk_signal_connect(GTK_OBJECT(w), "changed",
1416 GTK_SIGNAL_FUNC(editbox_changed), i);
1417 gtk_signal_connect(GTK_OBJECT(w), "key_press_event",
1418 GTK_SIGNAL_FUNC(editbox_key), NULL);
1427 w = gtk_check_button_new_with_label(i->name);
1428 gtk_signal_connect(GTK_OBJECT(w), "toggled",
1429 GTK_SIGNAL_FUNC(button_toggled), i);
1430 gtk_table_attach(GTK_TABLE(table), w, 0, 2, y, y+1,
1431 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
1432 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
1434 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), i->ival);
1440 * Drop-down list (GtkOptionMenu).
1443 w = gtk_label_new(i->name);
1444 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5);
1445 gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1,
1446 GTK_SHRINK | GTK_FILL,
1447 GTK_EXPAND | GTK_SHRINK | GTK_FILL ,
1451 w = gtk_option_menu_new();
1452 gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1,
1453 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
1454 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
1461 GtkWidget *menuitem;
1462 GtkWidget *menu = gtk_menu_new();
1464 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), menu);
1472 while (*q && *q != c)
1475 name = snewn(q-p+1, char);
1476 strncpy(name, p, q-p);
1479 if (*q) q++; /* eat delimiter */
1481 menuitem = gtk_menu_item_new_with_label(name);
1482 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1483 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1484 GINT_TO_POINTER(val));
1485 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1486 GTK_SIGNAL_FUNC(droplist_sel), i);
1487 gtk_widget_show(menuitem);
1494 gtk_option_menu_set_history(GTK_OPTION_MENU(w), i->ival);
1503 gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "destroy",
1504 GTK_SIGNAL_FUNC(window_destroy), NULL);
1505 gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "key_press_event",
1506 GTK_SIGNAL_FUNC(win_key_press), cancel);
1507 gtk_window_set_modal(GTK_WINDOW(fe->cfgbox), TRUE);
1508 gtk_window_set_transient_for(GTK_WINDOW(fe->cfgbox),
1509 GTK_WINDOW(fe->window));
1510 /* set_transient_window_pos(fe->window, fe->cfgbox); */
1511 gtk_widget_show(fe->cfgbox);
1519 static void menu_key_event(GtkMenuItem *menuitem, gpointer data)
1521 frontend *fe = (frontend *)data;
1522 int key = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem),
1524 if (!midend_process_key(fe->me, 0, 0, key))
1525 gtk_widget_destroy(fe->window);
1528 static void get_size(frontend *fe, int *px, int *py)
1533 * Currently I don't want to make the GTK port scale large
1534 * puzzles to fit on the screen. This is because X does permit
1535 * extremely large windows and many window managers provide a
1536 * means of navigating round them, and the users I consulted
1537 * before deciding said that they'd rather have enormous puzzle
1538 * windows spanning multiple screen pages than have them
1539 * shrunk. I could change my mind later or introduce
1540 * configurability; this would be the place to do so, by
1541 * replacing the initial values of x and y with the screen
1546 midend_size(fe->me, &x, &y, FALSE);
1551 #if !GTK_CHECK_VERSION(2,0,0)
1552 #define gtk_window_resize(win, x, y) \
1553 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
1557 * Called when any other code in this file has changed the
1558 * selected game parameters.
1560 static void changed_preset(frontend *fe)
1562 int n = midend_which_preset(fe->me);
1564 fe->preset_threaded = TRUE;
1565 if (n < 0 && fe->preset_custom) {
1566 gtk_check_menu_item_set_active(
1567 GTK_CHECK_MENU_ITEM(fe->preset_custom),
1570 GSList *gs = fe->preset_radio;
1571 int i = fe->n_preset_menu_items - 1 - n;
1572 if (fe->preset_custom)
1579 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(gs->data),
1581 } else for (gs = fe->preset_radio; gs; gs = gs->next) {
1582 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(gs->data),
1586 fe->preset_threaded = FALSE;
1589 * Update the greying on the Copy menu option.
1591 if (fe->copy_menu_item) {
1592 int enabled = midend_can_format_as_text_now(fe->me);
1593 gtk_widget_set_sensitive(fe->copy_menu_item, enabled);
1597 static void resize_fe(frontend *fe)
1601 get_size(fe, &x, &y);
1604 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1607 gtk_widget_size_request(GTK_WIDGET(fe->window), &req);
1608 gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height);
1611 * Now that we've established the preferred size of the window,
1612 * reduce the drawing area's size request so the user can shrink
1615 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), 1, 1);
1618 static void menu_preset_event(GtkMenuItem *menuitem, gpointer data)
1620 frontend *fe = (frontend *)data;
1621 game_params *params =
1622 (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data");
1624 if (fe->preset_threaded ||
1625 (GTK_IS_CHECK_MENU_ITEM(menuitem) &&
1626 !gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem))))
1628 midend_set_params(fe->me, params);
1629 midend_new_game(fe->me);
1634 GdkAtom compound_text_atom, utf8_string_atom;
1635 int paste_initialised = FALSE;
1637 static void set_selection(frontend *fe, GdkAtom selection)
1639 if (!paste_initialised) {
1640 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1641 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
1642 paste_initialised = TRUE;
1646 * For this simple application we can safely assume that the
1647 * data passed to this function is pure ASCII, which means we
1648 * can return precisely the same stuff for types STRING,
1649 * COMPOUND_TEXT or UTF8_STRING.
1652 if (gtk_selection_owner_set(fe->area, selection, CurrentTime)) {
1653 gtk_selection_clear_targets(fe->area, selection);
1654 gtk_selection_add_target(fe->area, selection,
1655 GDK_SELECTION_TYPE_STRING, 1);
1656 gtk_selection_add_target(fe->area, selection, compound_text_atom, 1);
1657 gtk_selection_add_target(fe->area, selection, utf8_string_atom, 1);
1661 void write_clip(frontend *fe, char *data)
1664 sfree(fe->paste_data);
1666 fe->paste_data = data;
1667 fe->paste_data_len = strlen(data);
1669 set_selection(fe, GDK_SELECTION_PRIMARY);
1670 set_selection(fe, GDK_SELECTION_CLIPBOARD);
1673 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1674 guint info, guint time_stamp, gpointer data)
1676 frontend *fe = (frontend *)data;
1677 gtk_selection_data_set(seldata, seldata->target, 8,
1678 fe->paste_data, fe->paste_data_len);
1681 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1684 frontend *fe = (frontend *)data;
1687 sfree(fe->paste_data);
1688 fe->paste_data = NULL;
1689 fe->paste_data_len = 0;
1693 static void menu_copy_event(GtkMenuItem *menuitem, gpointer data)
1695 frontend *fe = (frontend *)data;
1698 text = midend_text_format(fe->me);
1701 write_clip(fe, text);
1709 static void filesel_ok(GtkButton *button, gpointer data)
1711 frontend *fe = (frontend *)data;
1713 gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1716 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
1718 fe->filesel_name = dupstr(name);
1721 static char *file_selector(frontend *fe, char *title, int save)
1723 GtkWidget *filesel =
1724 gtk_file_selection_new(title);
1726 fe->filesel_name = NULL;
1728 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1730 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1733 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1734 GTK_SIGNAL_FUNC(filesel_ok), fe);
1735 gtk_signal_connect_object
1736 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1737 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1738 gtk_signal_connect_object
1739 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1740 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1741 gtk_signal_connect(GTK_OBJECT(filesel), "destroy",
1742 GTK_SIGNAL_FUNC(window_destroy), NULL);
1743 gtk_widget_show(filesel);
1744 gtk_window_set_transient_for(GTK_WINDOW(filesel), GTK_WINDOW(fe->window));
1747 return fe->filesel_name;
1752 static char *file_selector(frontend *fe, char *title, int save)
1754 char *filesel_name = NULL;
1756 GtkWidget *filesel =
1757 gtk_file_chooser_dialog_new(title,
1758 GTK_WINDOW(fe->window),
1759 save ? GTK_FILE_CHOOSER_ACTION_SAVE :
1760 GTK_FILE_CHOOSER_ACTION_OPEN,
1761 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1762 save ? GTK_STOCK_SAVE : GTK_STOCK_OPEN,
1763 GTK_RESPONSE_ACCEPT,
1766 if (gtk_dialog_run(GTK_DIALOG(filesel)) == GTK_RESPONSE_ACCEPT) {
1767 const char *name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(filesel));
1768 filesel_name = dupstr(name);
1771 gtk_widget_destroy(filesel);
1773 return filesel_name;
1778 struct savefile_write_ctx {
1783 static void savefile_write(void *wctx, void *buf, int len)
1785 struct savefile_write_ctx *ctx = (struct savefile_write_ctx *)wctx;
1786 if (fwrite(buf, 1, len, ctx->fp) < len)
1790 static int savefile_read(void *wctx, void *buf, int len)
1792 FILE *fp = (FILE *)wctx;
1795 ret = fread(buf, 1, len, fp);
1796 return (ret == len);
1799 static void menu_save_event(GtkMenuItem *menuitem, gpointer data)
1801 frontend *fe = (frontend *)data;
1804 name = file_selector(fe, "Enter name of game file to save", TRUE);
1809 if ((fp = fopen(name, "r")) != NULL) {
1810 char buf[256 + FILENAME_MAX];
1814 sprintf(buf, "Are you sure you want to overwrite the"
1816 FILENAME_MAX, name);
1817 if (!message_box(fe->window, "Question", buf, TRUE, MB_YESNO))
1821 fp = fopen(name, "w");
1825 error_box(fe->window, "Unable to open save file");
1830 struct savefile_write_ctx ctx;
1833 midend_serialise(fe->me, savefile_write, &ctx);
1837 sprintf(boxmsg, "Error writing save file: %.400s",
1839 error_box(fe->window, boxmsg);
1847 static void menu_load_event(GtkMenuItem *menuitem, gpointer data)
1849 frontend *fe = (frontend *)data;
1852 name = file_selector(fe, "Enter name of saved game file to load", FALSE);
1855 FILE *fp = fopen(name, "r");
1859 error_box(fe->window, "Unable to open saved game file");
1863 err = midend_deserialise(fe->me, savefile_read, fp);
1868 error_box(fe->window, err);
1877 static void menu_solve_event(GtkMenuItem *menuitem, gpointer data)
1879 frontend *fe = (frontend *)data;
1882 msg = midend_solve(fe->me);
1885 error_box(fe->window, msg);
1888 static void menu_restart_event(GtkMenuItem *menuitem, gpointer data)
1890 frontend *fe = (frontend *)data;
1892 midend_restart_game(fe->me);
1895 static void menu_config_event(GtkMenuItem *menuitem, gpointer data)
1897 frontend *fe = (frontend *)data;
1898 int which = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem),
1901 if (fe->preset_threaded ||
1902 (GTK_IS_CHECK_MENU_ITEM(menuitem) &&
1903 !gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem))))
1905 changed_preset(fe); /* Put the old preset back! */
1906 if (!get_config(fe, which))
1909 midend_new_game(fe->me);
1913 static void menu_about_event(GtkMenuItem *menuitem, gpointer data)
1915 frontend *fe = (frontend *)data;
1919 sprintf(titlebuf, "About %.200s", thegame.name);
1922 "from Simon Tatham's Portable Puzzle Collection\n\n"
1923 "%.500s", thegame.name, ver);
1925 message_box(fe->window, titlebuf, textbuf, TRUE, MB_OK);
1928 static GtkWidget *add_menu_item_with_key(frontend *fe, GtkContainer *cont,
1929 char *text, int key)
1931 GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
1933 gtk_container_add(cont, menuitem);
1934 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1935 GINT_TO_POINTER(key));
1936 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1937 GTK_SIGNAL_FUNC(menu_key_event), fe);
1938 switch (key & ~0x1F) {
1941 keyqual = GDK_CONTROL_MASK;
1945 keyqual = GDK_SHIFT_MASK;
1951 gtk_widget_add_accelerator(menuitem,
1952 "activate", fe->accelgroup,
1955 gtk_widget_show(menuitem);
1959 static void add_menu_separator(GtkContainer *cont)
1961 GtkWidget *menuitem = gtk_menu_item_new();
1962 gtk_container_add(cont, menuitem);
1963 gtk_widget_show(menuitem);
1966 enum { ARG_EITHER, ARG_SAVE, ARG_ID }; /* for argtype */
1968 static frontend *new_window(char *arg, int argtype, char **error)
1971 GtkBox *vbox, *hbox;
1972 GtkWidget *menubar, *menu, *menuitem;
1977 extern char *const *const xpm_icons[];
1978 extern const int n_xpm_icons;
1980 fe = snew(frontend);
1982 fe->timer_active = FALSE;
1985 fe->me = midend_new(fe, &thegame, >k_drawing, fe);
1995 err = midend_game_id(fe->me, arg);
1997 midend_new_game(fe->me);
1999 sprintf(errbuf, "Invalid game ID: %.800s", err);
2002 fp = fopen(arg, "r");
2004 sprintf(errbuf, "Error opening file: %.800s", strerror(errno));
2006 err = midend_deserialise(fe->me, savefile_read, fp);
2008 sprintf(errbuf, "Invalid save file: %.800s", err);
2012 default /*case ARG_EITHER*/:
2014 * First try treating the argument as a game ID.
2016 err = midend_game_id(fe->me, arg);
2019 * It's a valid game ID.
2021 midend_new_game(fe->me);
2023 FILE *fp = fopen(arg, "r");
2025 sprintf(errbuf, "Supplied argument is neither a game ID (%.400s)"
2026 " nor a save file (%.400s)", err, strerror(errno));
2028 err = midend_deserialise(fe->me, savefile_read, fp);
2030 sprintf(errbuf, "%.800s", err);
2037 *error = dupstr(errbuf);
2038 midend_free(fe->me);
2044 midend_new_game(fe->me);
2047 fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2048 gtk_window_set_title(GTK_WINDOW(fe->window), thegame.name);
2050 vbox = GTK_BOX(gtk_vbox_new(FALSE, 0));
2051 gtk_container_add(GTK_CONTAINER(fe->window), GTK_WIDGET(vbox));
2052 gtk_widget_show(GTK_WIDGET(vbox));
2054 fe->accelgroup = gtk_accel_group_new();
2055 gtk_window_add_accel_group(GTK_WINDOW(fe->window), fe->accelgroup);
2057 hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
2058 gtk_box_pack_start(vbox, GTK_WIDGET(hbox), FALSE, FALSE, 0);
2059 gtk_widget_show(GTK_WIDGET(hbox));
2061 menubar = gtk_menu_bar_new();
2062 gtk_box_pack_start(hbox, menubar, TRUE, TRUE, 0);
2063 gtk_widget_show(menubar);
2065 menuitem = gtk_menu_item_new_with_mnemonic("_Game");
2066 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
2067 gtk_widget_show(menuitem);
2069 menu = gtk_menu_new();
2070 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
2072 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "New", 'n');
2074 menuitem = gtk_menu_item_new_with_label("Restart");
2075 gtk_container_add(GTK_CONTAINER(menu), menuitem);
2076 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2077 GTK_SIGNAL_FUNC(menu_restart_event), fe);
2078 gtk_widget_show(menuitem);
2080 menuitem = gtk_menu_item_new_with_label("Specific...");
2081 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
2082 GINT_TO_POINTER(CFG_DESC));
2083 gtk_container_add(GTK_CONTAINER(menu), menuitem);
2084 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2085 GTK_SIGNAL_FUNC(menu_config_event), fe);
2086 gtk_widget_show(menuitem);
2088 menuitem = gtk_menu_item_new_with_label("Random Seed...");
2089 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
2090 GINT_TO_POINTER(CFG_SEED));
2091 gtk_container_add(GTK_CONTAINER(menu), menuitem);
2092 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2093 GTK_SIGNAL_FUNC(menu_config_event), fe);
2094 gtk_widget_show(menuitem);
2096 fe->preset_radio = NULL;
2097 fe->preset_custom = NULL;
2098 fe->n_preset_menu_items = 0;
2099 fe->preset_threaded = FALSE;
2100 if ((n = midend_num_presets(fe->me)) > 0 || thegame.can_configure) {
2104 menuitem = gtk_menu_item_new_with_mnemonic("_Type");
2105 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
2106 gtk_widget_show(menuitem);
2108 submenu = gtk_menu_new();
2109 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
2111 for (i = 0; i < n; i++) {
2113 game_params *params;
2115 midend_fetch_preset(fe->me, i, &name, ¶ms);
2118 gtk_radio_menu_item_new_with_label(fe->preset_radio, name);
2120 gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(menuitem));
2121 fe->n_preset_menu_items++;
2122 gtk_container_add(GTK_CONTAINER(submenu), menuitem);
2123 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", params);
2124 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2125 GTK_SIGNAL_FUNC(menu_preset_event), fe);
2126 gtk_widget_show(menuitem);
2129 if (thegame.can_configure) {
2130 menuitem = fe->preset_custom =
2131 gtk_radio_menu_item_new_with_label(fe->preset_radio,
2134 gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(menuitem));
2135 gtk_container_add(GTK_CONTAINER(submenu), menuitem);
2136 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
2137 GPOINTER_TO_INT(CFG_SETTINGS));
2138 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2139 GTK_SIGNAL_FUNC(menu_config_event), fe);
2140 gtk_widget_show(menuitem);
2145 add_menu_separator(GTK_CONTAINER(menu));
2146 menuitem = gtk_menu_item_new_with_label("Load...");
2147 gtk_container_add(GTK_CONTAINER(menu), menuitem);
2148 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2149 GTK_SIGNAL_FUNC(menu_load_event), fe);
2150 gtk_widget_show(menuitem);
2151 menuitem = gtk_menu_item_new_with_label("Save...");
2152 gtk_container_add(GTK_CONTAINER(menu), menuitem);
2153 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2154 GTK_SIGNAL_FUNC(menu_save_event), fe);
2155 gtk_widget_show(menuitem);
2156 #ifndef STYLUS_BASED
2157 add_menu_separator(GTK_CONTAINER(menu));
2158 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Undo", 'u');
2159 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Redo", 'r');
2161 if (thegame.can_format_as_text_ever) {
2162 add_menu_separator(GTK_CONTAINER(menu));
2163 menuitem = gtk_menu_item_new_with_label("Copy");
2164 gtk_container_add(GTK_CONTAINER(menu), menuitem);
2165 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2166 GTK_SIGNAL_FUNC(menu_copy_event), fe);
2167 gtk_widget_show(menuitem);
2168 fe->copy_menu_item = menuitem;
2170 fe->copy_menu_item = NULL;
2172 if (thegame.can_solve) {
2173 add_menu_separator(GTK_CONTAINER(menu));
2174 menuitem = gtk_menu_item_new_with_label("Solve");
2175 gtk_container_add(GTK_CONTAINER(menu), menuitem);
2176 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2177 GTK_SIGNAL_FUNC(menu_solve_event), fe);
2178 gtk_widget_show(menuitem);
2180 add_menu_separator(GTK_CONTAINER(menu));
2181 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Exit", 'q');
2183 menuitem = gtk_menu_item_new_with_mnemonic("_Help");
2184 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
2185 gtk_widget_show(menuitem);
2187 menu = gtk_menu_new();
2188 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
2190 menuitem = gtk_menu_item_new_with_label("About");
2191 gtk_container_add(GTK_CONTAINER(menu), menuitem);
2192 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2193 GTK_SIGNAL_FUNC(menu_about_event), fe);
2194 gtk_widget_show(menuitem);
2197 menuitem=gtk_button_new_with_mnemonic("_Redo");
2198 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
2199 GINT_TO_POINTER((int)('r')));
2200 gtk_signal_connect(GTK_OBJECT(menuitem), "clicked",
2201 GTK_SIGNAL_FUNC(menu_key_event), fe);
2202 gtk_box_pack_end(hbox, menuitem, FALSE, FALSE, 0);
2203 gtk_widget_show(menuitem);
2205 menuitem=gtk_button_new_with_mnemonic("_Undo");
2206 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
2207 GINT_TO_POINTER((int)('u')));
2208 gtk_signal_connect(GTK_OBJECT(menuitem), "clicked",
2209 GTK_SIGNAL_FUNC(menu_key_event), fe);
2210 gtk_box_pack_end(hbox, menuitem, FALSE, FALSE, 0);
2211 gtk_widget_show(menuitem);
2213 if (thegame.flags & REQUIRE_NUMPAD) {
2214 hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
2215 gtk_box_pack_start(vbox, GTK_WIDGET(hbox), FALSE, FALSE, 0);
2216 gtk_widget_show(GTK_WIDGET(hbox));
2220 for(errbuf[0]='0';errbuf[0]<='9';errbuf[0]++) {
2221 menuitem=gtk_button_new_with_label(errbuf);
2222 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
2223 GINT_TO_POINTER((int)(errbuf[0])));
2224 gtk_signal_connect(GTK_OBJECT(menuitem), "clicked",
2225 GTK_SIGNAL_FUNC(menu_key_event), fe);
2226 gtk_box_pack_start(hbox, menuitem, TRUE, TRUE, 0);
2227 gtk_widget_show(menuitem);
2230 #endif /* STYLUS_BASED */
2234 snaffle_colours(fe);
2236 if (midend_wants_statusbar(fe->me)) {
2237 GtkWidget *viewport;
2240 viewport = gtk_viewport_new(NULL, NULL);
2241 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
2242 fe->statusbar = gtk_statusbar_new();
2243 gtk_container_add(GTK_CONTAINER(viewport), fe->statusbar);
2244 gtk_widget_show(viewport);
2245 gtk_box_pack_end(vbox, viewport, FALSE, FALSE, 0);
2246 gtk_widget_show(fe->statusbar);
2247 fe->statusctx = gtk_statusbar_get_context_id
2248 (GTK_STATUSBAR(fe->statusbar), "game");
2249 gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx,
2251 gtk_widget_size_request(fe->statusbar, &req);
2253 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
2255 gtk_widget_set_usize(viewport, -1, req.height);
2257 fe->statusbar = NULL;
2259 fe->area = gtk_drawing_area_new();
2260 #if GTK_CHECK_VERSION(2,0,0)
2261 GTK_WIDGET_UNSET_FLAGS(fe->area, GTK_DOUBLE_BUFFERED);
2263 get_size(fe, &x, &y);
2264 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
2268 gtk_box_pack_end(vbox, fe->area, TRUE, TRUE, 0);
2270 clear_backing_store(fe);
2272 fe->nfonts = fe->fontsize = 0;
2274 fe->paste_data = NULL;
2275 fe->paste_data_len = 0;
2277 gtk_signal_connect(GTK_OBJECT(fe->window), "destroy",
2278 GTK_SIGNAL_FUNC(destroy), fe);
2279 gtk_signal_connect(GTK_OBJECT(fe->window), "key_press_event",
2280 GTK_SIGNAL_FUNC(key_event), fe);
2281 gtk_signal_connect(GTK_OBJECT(fe->area), "button_press_event",
2282 GTK_SIGNAL_FUNC(button_event), fe);
2283 gtk_signal_connect(GTK_OBJECT(fe->area), "button_release_event",
2284 GTK_SIGNAL_FUNC(button_event), fe);
2285 gtk_signal_connect(GTK_OBJECT(fe->area), "motion_notify_event",
2286 GTK_SIGNAL_FUNC(motion_event), fe);
2287 gtk_signal_connect(GTK_OBJECT(fe->area), "selection_get",
2288 GTK_SIGNAL_FUNC(selection_get), fe);
2289 gtk_signal_connect(GTK_OBJECT(fe->area), "selection_clear_event",
2290 GTK_SIGNAL_FUNC(selection_clear), fe);
2291 gtk_signal_connect(GTK_OBJECT(fe->area), "expose_event",
2292 GTK_SIGNAL_FUNC(expose_area), fe);
2293 gtk_signal_connect(GTK_OBJECT(fe->window), "map_event",
2294 GTK_SIGNAL_FUNC(map_window), fe);
2295 gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event",
2296 GTK_SIGNAL_FUNC(configure_area), fe);
2298 gtk_widget_add_events(GTK_WIDGET(fe->area),
2299 GDK_BUTTON_PRESS_MASK |
2300 GDK_BUTTON_RELEASE_MASK |
2301 GDK_BUTTON_MOTION_MASK |
2302 GDK_POINTER_MOTION_HINT_MASK);
2305 gtk_widget_realize(fe->window);
2306 iconpm = gdk_pixmap_create_from_xpm_d(fe->window->window, NULL,
2307 NULL, (gchar **)xpm_icons[0]);
2308 gdk_window_set_icon(fe->window->window, NULL, iconpm, NULL);
2310 for (n = 0; n < n_xpm_icons; n++) {
2312 g_list_append(iconlist,
2313 gdk_pixbuf_new_from_xpm_data((const gchar **)
2316 gdk_window_set_icon_list(fe->window->window, iconlist);
2319 gtk_widget_show(fe->area);
2320 gtk_widget_show(fe->window);
2323 * Now that we've established the preferred size of the window,
2324 * reduce the drawing area's size request so the user can shrink
2327 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), 1, 1);
2328 set_window_background(fe, 0);
2333 char *fgetline(FILE *fp)
2335 char *ret = snewn(512, char);
2336 int size = 512, len = 0;
2337 while (fgets(ret + len, size - len, fp)) {
2338 len += strlen(ret + len);
2339 if (ret[len-1] == '\n')
2340 break; /* got a newline, we're done */
2342 ret = sresize(ret, size, char);
2344 if (len == 0) { /* first fgets returned NULL */
2352 int main(int argc, char **argv)
2354 char *pname = argv[0];
2356 int ngenerate = 0, print = FALSE, px = 1, py = 1;
2357 int soln = FALSE, colour = FALSE;
2359 float redo_proportion = 0.0F;
2360 char *savefile = NULL, *savesuffix = NULL;
2362 int argtype = ARG_EITHER;
2363 char *screenshot_file = NULL;
2364 int doing_opts = TRUE;
2370 * Command line parsing in this function is rather fiddly,
2371 * because GTK wants to have a go at argc/argv _first_ - and
2372 * yet we can't let it, because gtk_init() will bomb out if it
2373 * can't open an X display, whereas in fact we want to permit
2374 * our --generate and --print modes to run without an X
2378 * - we parse the command line ourselves, without modifying
2380 * - if we encounter an error which might plausibly be the
2381 * result of a GTK command line (i.e. not detailed errors in
2382 * particular options of ours) we store the error message
2383 * and terminate parsing.
2384 * - if we got enough out of the command line to know it
2385 * specifies a non-X mode of operation, we either display
2386 * the stored error and return failure, or if there is no
2387 * stored error we do the non-X operation and return
2389 * - otherwise, we go straight to gtk_init().
2395 if (doing_opts && !strcmp(p, "--version")) {
2396 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
2399 } else if (doing_opts && !strcmp(p, "--generate")) {
2401 ngenerate = atoi(*++av);
2403 fprintf(stderr, "%s: '--generate' expected a number\n",
2409 } else if (doing_opts && !strcmp(p, "--save")) {
2413 fprintf(stderr, "%s: '--save' expected a filename\n",
2417 } else if (doing_opts && (!strcmp(p, "--save-suffix") ||
2418 !strcmp(p, "--savesuffix"))) {
2422 fprintf(stderr, "%s: '--save-suffix' expected a filename\n",
2426 } else if (doing_opts && !strcmp(p, "--print")) {
2427 if (!thegame.can_print) {
2428 fprintf(stderr, "%s: this game does not support printing\n",
2435 if (sscanf(dim, "%dx%d", &px, &py) != 2) {
2436 fprintf(stderr, "%s: unable to parse argument '%s' to "
2437 "'--print'\n", pname, dim);
2443 } else if (doing_opts && !strcmp(p, "--scale")) {
2445 scale = atof(*++av);
2447 fprintf(stderr, "%s: no argument supplied to '--scale'\n",
2451 } else if (doing_opts && !strcmp(p, "--redo")) {
2453 * This is an internal option which I don't expect
2454 * users to have any particular use for. The effect of
2455 * --redo is that once the game has been loaded and
2456 * initialised, the next move in the redo chain is
2457 * replayed, and the game screen is redrawn part way
2458 * through the making of the move. This is only
2459 * meaningful if there _is_ a next move in the redo
2460 * chain, which means in turn that this option is only
2461 * useful if you're also passing a save file on the
2464 * This option is used by the script which generates
2465 * the puzzle icons and website screenshots, and I
2466 * don't imagine it's useful for anything else.
2467 * (Unless, I suppose, users don't like my screenshots
2468 * and want to generate their own in the same way for
2469 * some repackaged version of the puzzles.)
2472 redo_proportion = atof(*++av);
2474 fprintf(stderr, "%s: no argument supplied to '--redo'\n",
2478 } else if (doing_opts && !strcmp(p, "--screenshot")) {
2480 * Another internal option for the icon building
2481 * script. This causes a screenshot of the central
2482 * drawing area (i.e. not including the menu bar or
2483 * status bar) to be saved to a PNG file once the
2484 * window has been drawn, and then the application
2485 * quits immediately.
2488 screenshot_file = *++av;
2490 fprintf(stderr, "%s: no argument supplied to '--screenshot'\n",
2494 } else if (doing_opts && (!strcmp(p, "--with-solutions") ||
2495 !strcmp(p, "--with-solution") ||
2496 !strcmp(p, "--with-solns") ||
2497 !strcmp(p, "--with-soln") ||
2498 !strcmp(p, "--solutions") ||
2499 !strcmp(p, "--solution") ||
2500 !strcmp(p, "--solns") ||
2501 !strcmp(p, "--soln"))) {
2503 } else if (doing_opts && !strcmp(p, "--colour")) {
2504 if (!thegame.can_print_in_colour) {
2505 fprintf(stderr, "%s: this game does not support colour"
2506 " printing\n", pname);
2510 } else if (doing_opts && !strcmp(p, "--load")) {
2512 } else if (doing_opts && !strcmp(p, "--game")) {
2514 } else if (doing_opts && !strcmp(p, "--")) {
2516 } else if (!doing_opts || p[0] != '-') {
2518 fprintf(stderr, "%s: more than one argument supplied\n",
2524 sprintf(errbuf, "%.100s: unrecognised option '%.100s'\n",
2531 fputs(errbuf, stderr);
2536 * Special standalone mode for generating puzzle IDs on the
2537 * command line. Useful for generating puzzles to be printed
2538 * out and solved offline (for puzzles where that even makes
2539 * sense - Solo, for example, is a lot more pencil-and-paper
2540 * friendly than Twiddle!)
2544 * <puzzle-name> --generate [<n> [<params>]]
2546 * <n>, if present, is the number of puzzle IDs to generate.
2547 * <params>, if present, is the same type of parameter string
2548 * you would pass to the puzzle when running it in GUI mode,
2549 * including optional extras such as the expansion factor in
2550 * Rectangles and the difficulty level in Solo.
2552 * If you specify <params>, you must also specify <n> (although
2553 * you may specify it to be 1). Sorry; that was the
2554 * simplest-to-parse command-line syntax I came up with.
2556 if (ngenerate > 0 || print || savefile || savesuffix) {
2560 document *doc = NULL;
2564 me = midend_new(NULL, &thegame, NULL, NULL);
2567 if (savefile && !savesuffix)
2569 if (!savefile && savesuffix)
2573 doc = document_new(px, py, scale);
2576 * In this loop, we either generate a game ID or read one
2577 * from stdin depending on whether we're in generate mode;
2578 * then we either write it to stdout or print it, depending
2579 * on whether we're in print mode. Thus, this loop handles
2580 * generate-to-stdout, print-from-stdin and generate-and-
2581 * immediately-print modes.
2583 * (It could also handle a copy-stdin-to-stdout mode,
2584 * although there's currently no combination of options
2585 * which will cause this loop to be activated in that mode.
2586 * It wouldn't be _entirely_ pointless, though, because
2587 * stdin could contain bare params strings or random-seed
2588 * IDs, and stdout would contain nothing but fully
2589 * generated descriptive game IDs.)
2591 while (ngenerate == 0 || i < n) {
2594 if (ngenerate == 0) {
2595 pstr = fgetline(stdin);
2598 pstr[strcspn(pstr, "\r\n")] = '\0';
2601 pstr = snewn(strlen(arg) + 40, char);
2604 if (i > 0 && strchr(arg, '#'))
2605 sprintf(pstr + strlen(pstr), "-%d", i);
2611 err = midend_game_id(me, pstr);
2613 fprintf(stderr, "%s: error parsing '%s': %s\n",
2620 midend_new_game(me);
2623 err = midend_print_puzzle(me, doc, soln);
2625 fprintf(stderr, "%s: error in printing: %s\n", pname, err);
2630 struct savefile_write_ctx ctx;
2631 char *realname = snewn(40 + strlen(savefile) +
2632 strlen(savesuffix), char);
2633 sprintf(realname, "%s%d%s", savefile, i, savesuffix);
2634 ctx.fp = fopen(realname, "w");
2636 fprintf(stderr, "%s: open: %s\n", realname,
2641 midend_serialise(me, savefile_write, &ctx);
2643 fprintf(stderr, "%s: write: %s\n", realname,
2644 strerror(ctx.error));
2647 if (fclose(ctx.fp)) {
2648 fprintf(stderr, "%s: close: %s\n", realname,
2653 if (!doc && !savefile) {
2654 id = midend_get_game_id(me);
2663 psdata *ps = ps_init(stdout, colour);
2664 document_print(doc, ps_drawing_api(ps));
2675 gtk_init(&argc, &argv);
2677 fe = new_window(arg, argtype, &error);
2680 fprintf(stderr, "%s: %s\n", pname, error);
2684 if (screenshot_file) {
2686 * Some puzzles will not redraw their entire area if
2687 * given a partially completed animation, which means
2688 * we must redraw now and _then_ redraw again after
2689 * freezing the move timer.
2691 midend_force_redraw(fe->me);
2694 if (redo_proportion) {
2696 midend_process_key(fe->me, 0, 0, 'r');
2697 /* And freeze the timer at the specified position. */
2698 midend_freeze_timer(fe->me, redo_proportion);
2701 if (screenshot_file) {
2702 save_screenshot_png(fe, screenshot_file);