X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/dafd6cf6826f9bbd27ddd780fab48221d4706556..HEAD:/gtk.c diff --git a/gtk.c b/gtk.c index 6fef7b8..4222bd4 100644 --- a/gtk.c +++ b/gtk.c @@ -9,12 +9,15 @@ #include #include #include +#include #include #include #include +#include + #include #include #include @@ -23,9 +26,22 @@ #include "puzzles.h" #if GTK_CHECK_VERSION(2,0,0) -#define USE_PANGO +# define USE_PANGO +# ifdef PANGO_VERSION_CHECK +# if PANGO_VERSION_CHECK(1,8,0) +# define HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION +# endif +# endif +#endif +#if !GTK_CHECK_VERSION(2,4,0) +# define OLD_FILESEL +#endif +#if GTK_CHECK_VERSION(2,8,0) +# define USE_CAIRO #endif +/* #undef USE_CAIRO */ +/* #define NO_THICK_LINE */ #ifdef DEBUGGING static FILE *debug_fp = NULL; @@ -77,6 +93,8 @@ void fatal(char *fmt, ...) * GTK front end to puzzles. */ +static void changed_preset(frontend *fe); + struct font { #ifdef USE_PANGO PangoFontDescription *desc; @@ -96,16 +114,27 @@ struct font { */ struct frontend { GtkWidget *window; + GtkAccelGroup *accelgroup; GtkWidget *area; GtkWidget *statusbar; + GtkWidget *menubar; guint statusctx; - GdkPixmap *pixmap; - GdkColor *colours; - int ncolours; - GdkColormap *colmap; int w, h; midend *me; +#ifdef USE_CAIRO + const float *colours; + cairo_t *cr; + cairo_surface_t *image; + GdkPixmap *pixmap; + GdkColor background; /* for painting outside puzzle area */ +#else + GdkPixmap *pixmap; GdkGC *gc; + GdkColor *colours; + GdkColormap *colmap; + int backgroundindex; /* which of colours[] is background */ +#endif + int ncolours; int bbox_l, bbox_r, bbox_u, bbox_d; int timer_active, timer_id; struct timeval last_time; @@ -116,10 +145,26 @@ struct frontend { GtkWidget *cfgbox; void *paste_data; int paste_data_len; - char *laststatus; int pw, ph; /* pixmap size (w, h are area size) */ int ox, oy; /* offset of pixmap in drawing area */ +#ifdef OLD_FILESEL char *filesel_name; +#endif + int drawing_area_shrink_pending; + GSList *preset_radio; + int n_preset_menu_items; + int preset_threaded; + GtkWidget *preset_custom; + GtkWidget *copy_menu_item; +}; + +struct blitter { +#ifdef USE_CAIRO + cairo_surface_t *image; +#else + GdkPixmap *pixmap; +#endif + int w, h, x, y; }; void get_random_seed(void **randseed, int *randseedsize) @@ -141,275 +186,752 @@ void frontend_default_colour(frontend *fe, float *output) void gtk_status_bar(void *handle, char *text) { frontend *fe = (frontend *)handle; - char *rewritten; assert(fe->statusbar); - rewritten = midend_rewrite_statusbar(fe->me, text); - if (!fe->laststatus || strcmp(rewritten, fe->laststatus)) { - gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx); - gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, rewritten); - sfree(fe->laststatus); - fe->laststatus = rewritten; - } else { - sfree(rewritten); + gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx); + gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, text); +} + +/* ---------------------------------------------------------------------- + * Cairo drawing functions. + */ + +#ifdef USE_CAIRO + +static void setup_drawing(frontend *fe) +{ + fe->cr = cairo_create(fe->image); + cairo_set_antialias(fe->cr, CAIRO_ANTIALIAS_GRAY); + cairo_set_line_width(fe->cr, 1.0); + cairo_set_line_cap(fe->cr, CAIRO_LINE_CAP_SQUARE); + cairo_set_line_join(fe->cr, CAIRO_LINE_JOIN_ROUND); +} + +static void teardown_drawing(frontend *fe) +{ + cairo_t *cr; + + cairo_destroy(fe->cr); + fe->cr = NULL; + + cr = gdk_cairo_create(fe->pixmap); + cairo_set_source_surface(cr, fe->image, 0, 0); + cairo_rectangle(cr, + fe->bbox_l - 1, + fe->bbox_u - 1, + fe->bbox_r - fe->bbox_l + 2, + fe->bbox_d - fe->bbox_u + 2); + cairo_fill(cr); + cairo_destroy(cr); +} + +static void snaffle_colours(frontend *fe) +{ + fe->colours = midend_colours(fe->me, &fe->ncolours); +} + +static void set_colour(frontend *fe, int colour) +{ + cairo_set_source_rgb(fe->cr, + fe->colours[3*colour + 0], + fe->colours[3*colour + 1], + fe->colours[3*colour + 2]); +} + +static void set_window_background(frontend *fe, int colour) +{ + GdkColormap *colmap; + + colmap = gdk_colormap_get_system(); + fe->background.red = fe->colours[3*colour + 0] * 65535; + fe->background.green = fe->colours[3*colour + 1] * 65535; + fe->background.blue = fe->colours[3*colour + 2] * 65535; + if (!gdk_colormap_alloc_color(colmap, &fe->background, FALSE, FALSE)) { + g_error("couldn't allocate background (#%02x%02x%02x)\n", + fe->background.red >> 8, fe->background.green >> 8, + fe->background.blue >> 8); } + gdk_window_set_background(fe->area->window, &fe->background); + gdk_window_set_background(fe->window->window, &fe->background); } -void gtk_start_draw(void *handle) +static PangoLayout *make_pango_layout(frontend *fe) +{ + return (pango_cairo_create_layout(fe->cr)); +} + +static void draw_pango_layout(frontend *fe, PangoLayout *layout, + int x, int y) +{ + cairo_move_to(fe->cr, x, y); + pango_cairo_show_layout(fe->cr, layout); +} + +static void save_screenshot_png(frontend *fe, const char *screenshot_file) +{ + cairo_surface_write_to_png(fe->image, screenshot_file); +} + +static void do_clip(frontend *fe, int x, int y, int w, int h) +{ + cairo_new_path(fe->cr); + cairo_rectangle(fe->cr, x, y, w, h); + cairo_clip(fe->cr); +} + +static void do_unclip(frontend *fe) +{ + cairo_reset_clip(fe->cr); +} + +static void do_draw_rect(frontend *fe, int x, int y, int w, int h) +{ + cairo_save(fe->cr); + cairo_new_path(fe->cr); + cairo_set_antialias(fe->cr, CAIRO_ANTIALIAS_NONE); + cairo_rectangle(fe->cr, x, y, w, h); + cairo_fill(fe->cr); + cairo_restore(fe->cr); +} + +static void do_draw_line(frontend *fe, int x1, int y1, int x2, int y2) +{ + cairo_new_path(fe->cr); + cairo_move_to(fe->cr, x1 + 0.5, y1 + 0.5); + cairo_line_to(fe->cr, x2 + 0.5, y2 + 0.5); + cairo_stroke(fe->cr); +} + +static void do_draw_thick_line(frontend *fe, float thickness, + float x1, float y1, float x2, float y2) +{ + cairo_save(fe->cr); + cairo_set_line_width(fe->cr, thickness); + cairo_new_path(fe->cr); + cairo_move_to(fe->cr, x1, y1); + cairo_line_to(fe->cr, x2, y2); + cairo_stroke(fe->cr); + cairo_restore(fe->cr); +} + +static void do_draw_poly(frontend *fe, int *coords, int npoints, + int fillcolour, int outlinecolour) +{ + int i; + + cairo_new_path(fe->cr); + for (i = 0; i < npoints; i++) + cairo_line_to(fe->cr, coords[i*2] + 0.5, coords[i*2 + 1] + 0.5); + cairo_close_path(fe->cr); + if (fillcolour >= 0) { + set_colour(fe, fillcolour); + cairo_fill_preserve(fe->cr); + } + assert(outlinecolour >= 0); + set_colour(fe, outlinecolour); + cairo_stroke(fe->cr); +} + +static void do_draw_circle(frontend *fe, int cx, int cy, int radius, + int fillcolour, int outlinecolour) +{ + cairo_new_path(fe->cr); + cairo_arc(fe->cr, cx + 0.5, cy + 0.5, radius, 0, 2*PI); + cairo_close_path(fe->cr); /* Just in case... */ + if (fillcolour >= 0) { + set_colour(fe, fillcolour); + cairo_fill_preserve(fe->cr); + } + assert(outlinecolour >= 0); + set_colour(fe, outlinecolour); + cairo_stroke(fe->cr); +} + +static void setup_blitter(blitter *bl, int w, int h) +{ + bl->image = cairo_image_surface_create(CAIRO_FORMAT_RGB24, w, h); +} + +static void teardown_blitter(blitter *bl) +{ + cairo_surface_destroy(bl->image); +} + +static void do_blitter_save(frontend *fe, blitter *bl, int x, int y) +{ + cairo_t *cr = cairo_create(bl->image); + + cairo_set_source_surface(cr, fe->image, -x, -y); + cairo_paint(cr); + cairo_destroy(cr); +} + +static void do_blitter_load(frontend *fe, blitter *bl, int x, int y) +{ + cairo_set_source_surface(fe->cr, bl->image, x, y); + cairo_paint(fe->cr); +} + +static void clear_backing_store(frontend *fe) +{ + fe->image = NULL; +} + +static void setup_backing_store(frontend *fe) +{ + cairo_t *cr; + int i; + + fe->pixmap = gdk_pixmap_new(fe->area->window, fe->pw, fe->ph, -1); + fe->image = cairo_image_surface_create(CAIRO_FORMAT_RGB24, + fe->pw, fe->ph); + + for (i = 0; i < 3; i++) { + switch (i) { + case 0: cr = cairo_create(fe->image); break; + case 1: cr = gdk_cairo_create(fe->pixmap); break; + case 2: cr = gdk_cairo_create(fe->area->window); break; + } + cairo_set_source_rgb(cr, + fe->colours[0], fe->colours[1], fe->colours[2]); + cairo_paint(cr); + cairo_destroy(cr); + } +} + +static int backing_store_ok(frontend *fe) +{ + return (!!fe->image); +} + +static void teardown_backing_store(frontend *fe) +{ + cairo_surface_destroy(fe->image); + gdk_pixmap_unref(fe->pixmap); + fe->image = NULL; +} + +#endif + +/* ---------------------------------------------------------------------- + * GDK drawing functions. + */ + +#ifndef USE_CAIRO + +static void setup_drawing(frontend *fe) { - frontend *fe = (frontend *)handle; fe->gc = gdk_gc_new(fe->area->window); - fe->bbox_l = fe->w; - fe->bbox_r = 0; - fe->bbox_u = fe->h; - fe->bbox_d = 0; } -void gtk_clip(void *handle, int x, int y, int w, int h) +static void teardown_drawing(frontend *fe) +{ + gdk_gc_unref(fe->gc); + fe->gc = NULL; +} + +static void snaffle_colours(frontend *fe) +{ + int i, ncolours; + float *colours; + gboolean *success; + + fe->colmap = gdk_colormap_get_system(); + colours = midend_colours(fe->me, &ncolours); + fe->ncolours = ncolours; + fe->colours = snewn(ncolours, GdkColor); + for (i = 0; i < ncolours; i++) { + fe->colours[i].red = colours[i*3] * 0xFFFF; + fe->colours[i].green = colours[i*3+1] * 0xFFFF; + fe->colours[i].blue = colours[i*3+2] * 0xFFFF; + } + success = snewn(ncolours, gboolean); + gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours, + FALSE, FALSE, success); + for (i = 0; i < ncolours; i++) { + if (!success[i]) { + g_error("couldn't allocate colour %d (#%02x%02x%02x)\n", + i, fe->colours[i].red >> 8, + fe->colours[i].green >> 8, + fe->colours[i].blue >> 8); + } + } +} + +static void set_window_background(frontend *fe, int colour) +{ + fe->backgroundindex = colour; + gdk_window_set_background(fe->area->window, &fe->colours[colour]); + gdk_window_set_background(fe->window->window, &fe->colours[colour]); +} + +static void set_colour(frontend *fe, int colour) +{ + gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); +} + +#ifdef USE_PANGO +static PangoLayout *make_pango_layout(frontend *fe) +{ + return (pango_layout_new(gtk_widget_get_pango_context(fe->area))); +} + +static void draw_pango_layout(frontend *fe, PangoLayout *layout, + int x, int y) +{ + gdk_draw_layout(fe->pixmap, fe->gc, x, y, layout); +} +#endif + +static void save_screenshot_png(frontend *fe, const char *screenshot_file) +{ + GdkPixbuf *pb; + GError *gerror = NULL; + + midend_redraw(fe->me); + + pb = gdk_pixbuf_get_from_drawable(NULL, fe->pixmap, + NULL, 0, 0, 0, 0, -1, -1); + gdk_pixbuf_save(pb, screenshot_file, "png", &gerror, NULL); +} + +static void do_clip(frontend *fe, int x, int y, int w, int h) { - frontend *fe = (frontend *)handle; GdkRectangle rect; rect.x = x; rect.y = y; rect.width = w; rect.height = h; - gdk_gc_set_clip_rectangle(fe->gc, &rect); } -void gtk_unclip(void *handle) +static void do_unclip(frontend *fe) { - frontend *fe = (frontend *)handle; GdkRectangle rect; rect.x = 0; rect.y = 0; rect.width = fe->w; rect.height = fe->h; - gdk_gc_set_clip_rectangle(fe->gc, &rect); } -void gtk_draw_text(void *handle, int x, int y, int fonttype, int fontsize, - int align, int colour, char *text) +static void do_draw_rect(frontend *fe, int x, int y, int w, int h) { - frontend *fe = (frontend *)handle; + gdk_draw_rectangle(fe->pixmap, fe->gc, 1, x, y, w, h); +} + +static void do_draw_line(frontend *fe, int x1, int y1, int x2, int y2) +{ + gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2); +} + +static void do_draw_thick_line(frontend *fe, float thickness, + float x1, float y1, float x2, float y2) +{ + GdkGCValues save; + + gdk_gc_get_values(fe->gc, &save); + gdk_gc_set_line_attributes(fe->gc, + thickness, + GDK_LINE_SOLID, + GDK_CAP_BUTT, + GDK_JOIN_BEVEL); + gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2); + gdk_gc_set_line_attributes(fe->gc, + save.line_width, + save.line_style, + save.cap_style, + save.join_style); +} + +static void do_draw_poly(frontend *fe, int *coords, int npoints, + int fillcolour, int outlinecolour) +{ + GdkPoint *points = snewn(npoints, GdkPoint); int i; + for (i = 0; i < npoints; i++) { + points[i].x = coords[i*2]; + points[i].y = coords[i*2+1]; + } + + if (fillcolour >= 0) { + set_colour(fe, fillcolour); + gdk_draw_polygon(fe->pixmap, fe->gc, TRUE, points, npoints); + } + assert(outlinecolour >= 0); + set_colour(fe, outlinecolour); + /* - * Find or create the font. + * In principle we ought to be able to use gdk_draw_polygon for + * the outline as well. In fact, it turns out to interact badly + * with a clipping region, for no terribly obvious reason, so I + * draw the outline as a sequence of lines instead. */ - for (i = 0; i < fe->nfonts; i++) - if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize) - break; + for (i = 0; i < npoints; i++) + gdk_draw_line(fe->pixmap, fe->gc, + points[i].x, points[i].y, + points[(i+1)%npoints].x, points[(i+1)%npoints].y); - if (i == fe->nfonts) { - if (fe->fontsize <= fe->nfonts) { - fe->fontsize = fe->nfonts + 10; - fe->fonts = sresize(fe->fonts, fe->fontsize, struct font); - } + sfree(points); +} - fe->nfonts++; +static void do_draw_circle(frontend *fe, int cx, int cy, int radius, + int fillcolour, int outlinecolour) +{ + if (fillcolour >= 0) { + set_colour(fe, fillcolour); + gdk_draw_arc(fe->pixmap, fe->gc, TRUE, + cx - radius, cy - radius, + 2 * radius, 2 * radius, 0, 360 * 64); + } - fe->fonts[i].type = fonttype; - fe->fonts[i].size = fontsize; + assert(outlinecolour >= 0); + set_colour(fe, outlinecolour); + gdk_draw_arc(fe->pixmap, fe->gc, FALSE, + cx - radius, cy - radius, + 2 * radius, 2 * radius, 0, 360 * 64); +} + +static void setup_blitter(blitter *bl, int w, int h) +{ + /* + * We can't create the pixmap right now, because fe->window + * might not yet exist. So we just cache w and h and create it + * during the firs call to blitter_save. + */ + bl->pixmap = NULL; +} + +static void teardown_blitter(blitter *bl) +{ + if (bl->pixmap) + gdk_pixmap_unref(bl->pixmap); +} + +static void do_blitter_save(frontend *fe, blitter *bl, int x, int y) +{ + if (!bl->pixmap) + bl->pixmap = gdk_pixmap_new(fe->area->window, bl->w, bl->h, -1); + gdk_draw_pixmap(bl->pixmap, + fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)], + fe->pixmap, + x, y, 0, 0, bl->w, bl->h); +} + +static void do_blitter_load(frontend *fe, blitter *bl, int x, int y) +{ + assert(bl->pixmap); + gdk_draw_pixmap(fe->pixmap, + fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)], + bl->pixmap, + 0, 0, x, y, bl->w, bl->h); +} + +static void clear_backing_store(frontend *fe) +{ + fe->pixmap = NULL; +} + +static void setup_backing_store(frontend *fe) +{ + GdkGC *gc; + + fe->pixmap = gdk_pixmap_new(fe->area->window, fe->pw, fe->ph, -1); + + gc = gdk_gc_new(fe->area->window); + gdk_gc_set_foreground(gc, &fe->colours[0]); + gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->pw, fe->ph); + gdk_draw_rectangle(fe->area->window, gc, 1, 0, 0, fe->w, fe->h); + gdk_gc_unref(gc); +} + +static int backing_store_ok(frontend *fe) +{ + return (!!fe->pixmap); +} + +static void teardown_backing_store(frontend *fe) +{ + gdk_pixmap_unref(fe->pixmap); + fe->pixmap = NULL; +} -#ifdef USE_PANGO - /* - * Use Pango to find the closest match to the requested - * font. - */ - { - PangoFontDescription *fd; - - fd = pango_font_description_new(); - /* `Monospace' and `Sans' are meta-families guaranteed to exist */ - pango_font_description_set_family(fd, fonttype == FONT_FIXED ? - "Monospace" : "Sans"); - pango_font_description_set_weight(fd, PANGO_WEIGHT_BOLD); - /* - * I found some online Pango documentation which - * described a function called - * pango_font_description_set_absolute_size(), which is - * _exactly_ what I want here. Unfortunately, none of - * my local Pango installations have it (presumably - * they're too old), so I'm going to have to hack round - * it by figuring out the point size myself. This - * limits me to X and probably also breaks in later - * Pango installations, so ideally I should add another - * CHECK_VERSION type ifdef and use set_absolute_size - * where available. All very annoying. - */ -#ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION - pango_font_description_set_absolute_size(fd, PANGO_SCALE*fontsize); -#else - { - Display *d = GDK_DISPLAY(); - int s = DefaultScreen(d); - double resolution = - (PANGO_SCALE * 72.27 / 25.4) * - ((double) DisplayWidthMM(d, s) / DisplayWidth (d, s)); - pango_font_description_set_size(fd, resolution * fontsize); - } #endif - fe->fonts[i].desc = fd; - } +static void repaint_rectangle(frontend *fe, GtkWidget *widget, + int x, int y, int w, int h) +{ + GdkGC *gc = gdk_gc_new(widget->window); +#ifdef USE_CAIRO + gdk_gc_set_foreground(gc, &fe->background); #else - /* - * In GTK 1.2, I don't know of any plausible way to - * pick a suitable font, so I'm just going to be - * tedious. - */ - fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ? - "fixed" : "variable"); + gdk_gc_set_foreground(gc, &fe->colours[fe->backgroundindex]); #endif + if (x < fe->ox) { + gdk_draw_rectangle(widget->window, gc, + TRUE, x, y, fe->ox - x, h); + w -= (fe->ox - x); + x = fe->ox; + } + if (y < fe->oy) { + gdk_draw_rectangle(widget->window, gc, + TRUE, x, y, w, fe->oy - y); + h -= (fe->oy - y); + y = fe->oy; + } + if (w > fe->pw) { + gdk_draw_rectangle(widget->window, gc, + TRUE, x + fe->pw, y, w - fe->pw, h); + w = fe->pw; + } + if (h > fe->ph) { + gdk_draw_rectangle(widget->window, gc, + TRUE, x, y + fe->ph, w, h - fe->ph); + h = fe->ph; + } + gdk_draw_pixmap(widget->window, gc, fe->pixmap, + x - fe->ox, y - fe->oy, x, y, w, h); + gdk_gc_unref(gc); +} + +/* ---------------------------------------------------------------------- + * Pango font functions. + */ +#ifdef USE_PANGO + +static void add_font(frontend *fe, int index, int fonttype, int fontsize) +{ + /* + * Use Pango to find the closest match to the requested + * font. + */ + PangoFontDescription *fd; + + fd = pango_font_description_new(); + /* `Monospace' and `Sans' are meta-families guaranteed to exist */ + pango_font_description_set_family(fd, fonttype == FONT_FIXED ? + "Monospace" : "Sans"); + pango_font_description_set_weight(fd, PANGO_WEIGHT_BOLD); + /* + * I found some online Pango documentation which + * described a function called + * pango_font_description_set_absolute_size(), which is + * _exactly_ what I want here. Unfortunately, none of + * my local Pango installations have it (presumably + * they're too old), so I'm going to have to hack round + * it by figuring out the point size myself. This + * limits me to X and probably also breaks in later + * Pango installations, so ideally I should add another + * CHECK_VERSION type ifdef and use set_absolute_size + * where available. All very annoying. + */ +#ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION + pango_font_description_set_absolute_size(fd, PANGO_SCALE*fontsize); +#else + { + Display *d = GDK_DISPLAY(); + int s = DefaultScreen(d); + double resolution = + (PANGO_SCALE * 72.27 / 25.4) * + ((double) DisplayWidthMM(d, s) / DisplayWidth (d, s)); + pango_font_description_set_size(fd, resolution * fontsize); } +#endif + fe->fonts[index].desc = fd; +} + +static void align_and_draw_text(frontend *fe, + int index, int align, int x, int y, + const char *text) +{ + PangoLayout *layout; + PangoRectangle rect; + + layout = make_pango_layout(fe); /* - * Set the colour. + * Create a layout. */ - gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); + pango_layout_set_font_description(layout, fe->fonts[index].desc); + pango_layout_set_text(layout, text, strlen(text)); + pango_layout_get_pixel_extents(layout, NULL, &rect); -#ifdef USE_PANGO + if (align & ALIGN_VCENTRE) + rect.y -= rect.height / 2; + else + rect.y -= rect.height; - { - PangoLayout *layout; - PangoRectangle rect; + if (align & ALIGN_HCENTRE) + rect.x -= rect.width / 2; + else if (align & ALIGN_HRIGHT) + rect.x -= rect.width; - /* - * Create a layout. - */ - layout = pango_layout_new(gtk_widget_get_pango_context(fe->area)); - pango_layout_set_font_description(layout, fe->fonts[i].desc); - pango_layout_set_text(layout, text, strlen(text)); - pango_layout_get_pixel_extents(layout, NULL, &rect); + draw_pango_layout(fe, layout, rect.x + x, rect.y + y); - if (align & ALIGN_VCENTRE) - rect.y -= rect.height / 2; + g_object_unref(layout); +} - if (align & ALIGN_HCENTRE) - rect.x -= rect.width / 2; - else if (align & ALIGN_HRIGHT) - rect.x -= rect.width; +#endif - gdk_draw_layout(fe->pixmap, fe->gc, rect.x + x, rect.y + y, layout); +/* ---------------------------------------------------------------------- + * Old-fashioned font functions. + */ - g_object_unref(layout); - } +#ifndef USE_PANGO -#else +static void add_font(int index, int fonttype, int fontsize) +{ /* - * Find string dimensions and process alignment. + * In GTK 1.2, I don't know of any plausible way to + * pick a suitable font, so I'm just going to be + * tedious. */ - { - int lb, rb, wid, asc, desc; + fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ? + "fixed" : "variable"); +} - /* - * Measure vertical string extents with respect to the same - * string always... - */ - gdk_string_extents(fe->fonts[i].font, - "ABCDEFGHIJKLMNOPQRSTUVWXYZ", - &lb, &rb, &wid, &asc, &desc); - if (align & ALIGN_VCENTRE) - y += asc - (asc+desc)/2; +static void align_and_draw_text(int index, int align, int x, int y, + const char *text) +{ + int lb, rb, wid, asc, desc; - /* - * ... but horizontal extents with respect to the provided - * string. This means that multiple pieces of text centred - * on the same y-coordinate don't have different baselines. - */ - gdk_string_extents(fe->fonts[i].font, text, - &lb, &rb, &wid, &asc, &desc); + /* + * Measure vertical string extents with respect to the same + * string always... + */ + gdk_string_extents(fe->fonts[i].font, + "ABCDEFGHIJKLMNOPQRSTUVWXYZ", + &lb, &rb, &wid, &asc, &desc); + if (align & ALIGN_VCENTRE) + y += asc - (asc+desc)/2; + else + y += asc; - if (align & ALIGN_HCENTRE) - x -= wid / 2; - else if (align & ALIGN_HRIGHT) - x -= wid; + /* + * ... but horizontal extents with respect to the provided + * string. This means that multiple pieces of text centred + * on the same y-coordinate don't have different baselines. + */ + gdk_string_extents(fe->fonts[i].font, text, + &lb, &rb, &wid, &asc, &desc); - } + if (align & ALIGN_HCENTRE) + x -= wid / 2; + else if (align & ALIGN_HRIGHT) + x -= wid; /* * Actually draw the text. */ gdk_draw_string(fe->pixmap, fe->fonts[i].font, fe->gc, x, y, text); +} + #endif +/* ---------------------------------------------------------------------- + * The exported drawing functions. + */ + +void gtk_start_draw(void *handle) +{ + frontend *fe = (frontend *)handle; + fe->bbox_l = fe->w; + fe->bbox_r = 0; + fe->bbox_u = fe->h; + fe->bbox_d = 0; + setup_drawing(fe); } -void gtk_draw_rect(void *handle, int x, int y, int w, int h, int colour) +void gtk_clip(void *handle, int x, int y, int w, int h) { frontend *fe = (frontend *)handle; - gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); - gdk_draw_rectangle(fe->pixmap, fe->gc, 1, x, y, w, h); + do_clip(fe, x, y, w, h); } -void gtk_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour) +void gtk_unclip(void *handle) { frontend *fe = (frontend *)handle; - gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); - gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2); + do_unclip(fe); } -void gtk_draw_poly(void *handle, int *coords, int npoints, - int fillcolour, int outlinecolour) +void gtk_draw_text(void *handle, int x, int y, int fonttype, int fontsize, + int align, int colour, char *text) { frontend *fe = (frontend *)handle; - GdkPoint *points = snewn(npoints, GdkPoint); int i; - for (i = 0; i < npoints; i++) { - points[i].x = coords[i*2]; - points[i].y = coords[i*2+1]; - } + /* + * Find or create the font. + */ + for (i = 0; i < fe->nfonts; i++) + if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize) + break; - if (fillcolour >= 0) { - gdk_gc_set_foreground(fe->gc, &fe->colours[fillcolour]); - gdk_draw_polygon(fe->pixmap, fe->gc, TRUE, points, npoints); + if (i == fe->nfonts) { + if (fe->fontsize <= fe->nfonts) { + fe->fontsize = fe->nfonts + 10; + fe->fonts = sresize(fe->fonts, fe->fontsize, struct font); + } + + fe->nfonts++; + + fe->fonts[i].type = fonttype; + fe->fonts[i].size = fontsize; + add_font(fe, i, fonttype, fontsize); } - assert(outlinecolour >= 0); - gdk_gc_set_foreground(fe->gc, &fe->colours[outlinecolour]); - gdk_draw_polygon(fe->pixmap, fe->gc, FALSE, points, npoints); - sfree(points); + /* + * Do the job. + */ + set_colour(fe, colour); + align_and_draw_text(fe, i, align, x, y, text); } -void gtk_draw_circle(void *handle, int cx, int cy, int radius, - int fillcolour, int outlinecolour) +void gtk_draw_rect(void *handle, int x, int y, int w, int h, int colour) { frontend *fe = (frontend *)handle; - if (fillcolour >= 0) { - gdk_gc_set_foreground(fe->gc, &fe->colours[fillcolour]); - gdk_draw_arc(fe->pixmap, fe->gc, TRUE, - cx - radius, cy - radius, - 2 * radius, 2 * radius, 0, 360 * 64); - } + set_colour(fe, colour); + do_draw_rect(fe, x, y, w, h); +} - assert(outlinecolour >= 0); - gdk_gc_set_foreground(fe->gc, &fe->colours[outlinecolour]); - gdk_draw_arc(fe->pixmap, fe->gc, FALSE, - cx - radius, cy - radius, - 2 * radius, 2 * radius, 0, 360 * 64); +void gtk_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour) +{ + frontend *fe = (frontend *)handle; + set_colour(fe, colour); + do_draw_line(fe, x1, y1, x2, y2); } -struct blitter { - GdkPixmap *pixmap; - int w, h, x, y; -}; +void gtk_draw_thick_line(void *handle, float thickness, + float x1, float y1, float x2, float y2, int colour) +{ + frontend *fe = (frontend *)handle; + set_colour(fe, colour); + do_draw_thick_line(fe, thickness, x1, y1, x2, y2); +} + +void gtk_draw_poly(void *handle, int *coords, int npoints, + int fillcolour, int outlinecolour) +{ + frontend *fe = (frontend *)handle; + do_draw_poly(fe, coords, npoints, fillcolour, outlinecolour); +} + +void gtk_draw_circle(void *handle, int cx, int cy, int radius, + int fillcolour, int outlinecolour) +{ + frontend *fe = (frontend *)handle; + do_draw_circle(fe, cx, cy, radius, fillcolour, outlinecolour); +} blitter *gtk_blitter_new(void *handle, int w, int h) { - /* - * We can't create the pixmap right now, because fe->window - * might not yet exist. So we just cache w and h and create it - * during the firs call to blitter_save. - */ blitter *bl = snew(blitter); - bl->pixmap = NULL; + setup_blitter(bl, w, h); bl->w = w; bl->h = h; return bl; @@ -417,36 +939,26 @@ blitter *gtk_blitter_new(void *handle, int w, int h) void gtk_blitter_free(void *handle, blitter *bl) { - if (bl->pixmap) - gdk_pixmap_unref(bl->pixmap); + teardown_blitter(bl); sfree(bl); } void gtk_blitter_save(void *handle, blitter *bl, int x, int y) { frontend *fe = (frontend *)handle; - if (!bl->pixmap) - bl->pixmap = gdk_pixmap_new(fe->area->window, bl->w, bl->h, -1); + do_blitter_save(fe, bl, x, y); bl->x = x; bl->y = y; - gdk_draw_pixmap(bl->pixmap, - fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)], - fe->pixmap, - x, y, 0, 0, bl->w, bl->h); } void gtk_blitter_load(void *handle, blitter *bl, int x, int y) { frontend *fe = (frontend *)handle; - assert(bl->pixmap); if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) { x = bl->x; y = bl->y; } - gdk_draw_pixmap(fe->pixmap, - fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)], - bl->pixmap, - 0, 0, x, y, bl->w, bl->h); + do_blitter_load(fe, bl, x, y); } void gtk_draw_update(void *handle, int x, int y, int w, int h) @@ -461,19 +973,29 @@ void gtk_draw_update(void *handle, int x, int y, int w, int h) void gtk_end_draw(void *handle) { frontend *fe = (frontend *)handle; - gdk_gc_unref(fe->gc); - fe->gc = NULL; + + teardown_drawing(fe); if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) { - gdk_draw_pixmap(fe->area->window, - fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)], - fe->pixmap, - fe->bbox_l, fe->bbox_u, - fe->ox + fe->bbox_l, fe->oy + fe->bbox_u, - fe->bbox_r - fe->bbox_l, fe->bbox_d - fe->bbox_u); + repaint_rectangle(fe, fe->area, + fe->bbox_l - 1 + fe->ox, + fe->bbox_u - 1 + fe->oy, + fe->bbox_r - fe->bbox_l + 2, + fe->bbox_d - fe->bbox_u + 2); } } +#ifdef USE_PANGO +char *gtk_text_fallback(void *handle, const char *const *strings, int nstrings) +{ + /* + * We assume Pango can cope with any UTF-8 likely to be emitted + * by a puzzle. + */ + return dupstr(strings[0]); +} +#endif + const struct drawing_api gtk_drawing = { gtk_draw_text, gtk_draw_rect, @@ -491,7 +1013,17 @@ const struct drawing_api gtk_drawing = { gtk_blitter_save, gtk_blitter_load, NULL, NULL, NULL, NULL, NULL, NULL, /* {begin,end}_{doc,page,puzzle} */ - NULL, /* line_width */ + NULL, NULL, /* line_width, line_dotted */ +#ifdef USE_PANGO + gtk_text_fallback, +#else + NULL, +#endif +#ifdef NO_THICK_LINE + NULL, +#else + gtk_draw_thick_line, +#endif }; static void destroy(GtkWidget *widget, gpointer data) @@ -509,7 +1041,21 @@ static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data) int shift = (event->state & GDK_SHIFT_MASK) ? MOD_SHFT : 0; int ctrl = (event->state & GDK_CONTROL_MASK) ? MOD_CTRL : 0; - if (!fe->pixmap) + if (!backing_store_ok(fe)) + return TRUE; + +#if !GTK_CHECK_VERSION(2,0,0) + /* Gtk 1.2 passes a key event to this function even if it's also + * defined as an accelerator. + * Gtk 2 doesn't do this, and this function appears not to exist there. */ + if (fe->accelgroup && + gtk_accel_group_get_entry(fe->accelgroup, + event->keyval, event->state)) + return TRUE; +#endif + + /* Handle mnemonics. */ + if (gtk_window_activate_key(GTK_WINDOW(fe->window), event)) return TRUE; if (event->keyval == GDK_Up) @@ -540,6 +1086,10 @@ static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data) keyval = MOD_NUM_KEYPAD | '0'; else if (event->keyval == GDK_KP_Begin || event->keyval == GDK_KP_5) keyval = MOD_NUM_KEYPAD | '5'; + else if (event->keyval == GDK_BackSpace || + event->keyval == GDK_Delete || + event->keyval == GDK_KP_Delete) + keyval = '\177'; else if (event->string[0] && !event->string[1]) keyval = (unsigned char)event->string[0]; else @@ -558,7 +1108,7 @@ static gint button_event(GtkWidget *widget, GdkEventButton *event, frontend *fe = (frontend *)data; int button; - if (!fe->pixmap) + if (!backing_store_ok(fe)) return TRUE; if (event->type != GDK_BUTTON_PRESS && event->type != GDK_BUTTON_RELEASE) @@ -589,7 +1139,7 @@ static gint motion_event(GtkWidget *widget, GdkEventMotion *event, frontend *fe = (frontend *)data; int button; - if (!fe->pixmap) + if (!backing_store_ok(fe)) return TRUE; if (event->state & (GDK_BUTTON2_MASK | GDK_SHIFT_MASK)) @@ -604,6 +1154,11 @@ static gint motion_event(GtkWidget *widget, GdkEventMotion *event, if (!midend_process_key(fe->me, event->x - fe->ox, event->y - fe->oy, button)) gtk_widget_destroy(fe->window); +#if GTK_CHECK_VERSION(2,12,0) + gdk_event_request_motions(event); +#else + gdk_window_get_pointer(widget->window, NULL, NULL, NULL); +#endif return TRUE; } @@ -613,13 +1168,10 @@ static gint expose_area(GtkWidget *widget, GdkEventExpose *event, { frontend *fe = (frontend *)data; - if (fe->pixmap) { - gdk_draw_pixmap(widget->window, - widget->style->fg_gc[GTK_WIDGET_STATE(widget)], - fe->pixmap, - event->area.x - fe->ox, event->area.y - fe->oy, - event->area.x, event->area.y, - event->area.width, event->area.height); + if (backing_store_ok(fe)) { + repaint_rectangle(fe, widget, + event->area.x, event->area.y, + event->area.width, event->area.height); } return TRUE; } @@ -643,11 +1195,10 @@ static gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data) { frontend *fe = (frontend *)data; - GdkGC *gc; int x, y; - if (fe->pixmap) - gdk_pixmap_unref(fe->pixmap); + if (backing_store_ok(fe)) + teardown_backing_store(fe); x = fe->w = event->width; y = fe->h = event->height; @@ -657,13 +1208,7 @@ static gint configure_area(GtkWidget *widget, fe->ox = (fe->w - fe->pw) / 2; fe->oy = (fe->h - fe->ph) / 2; - fe->pixmap = gdk_pixmap_new(widget->window, fe->pw, fe->ph, -1); - - gc = gdk_gc_new(fe->area->window); - gdk_gc_set_foreground(gc, &fe->colours[0]); - gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->pw, fe->ph); - gdk_gc_unref(gc); - + setup_backing_store(fe); midend_force_redraw(fe->me); return TRUE; @@ -760,18 +1305,18 @@ int message_box(GtkWidget *parent, char *title, char *msg, int centre, gtk_label_set_line_wrap(GTK_LABEL(text), TRUE); if (type == MB_OK) { - titles = "OK\0"; + titles = GTK_STOCK_OK "\0"; def = cancel = 0; } else { assert(type == MB_YESNO); - titles = "Yes\0No\0"; - def = 0; - cancel = 1; + titles = GTK_STOCK_NO "\0" GTK_STOCK_YES "\0"; + def = 1; + cancel = 0; } i = 0; while (*titles) { - button = gtk_button_new_with_label(titles); + button = gtk_button_new_from_stock(titles); gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area), button, FALSE, FALSE, 0); gtk_widget_show(button); @@ -800,7 +1345,7 @@ int message_box(GtkWidget *parent, char *title, char *msg, int centre, gtk_widget_show(window); i = -1; gtk_main(); - return (type == MB_YESNO ? i == 0 : TRUE); + return (type == MB_YESNO ? i == 1 : TRUE); } void error_box(GtkWidget *parent, char *msg) @@ -820,6 +1365,7 @@ static void config_ok_button_clicked(GtkButton *button, gpointer data) else { fe->cfgret = TRUE; gtk_widget_destroy(fe->cfgbox); + changed_preset(fe); } } @@ -889,22 +1435,22 @@ static int get_config(frontend *fe, int which) gtk_window_set_title(GTK_WINDOW(fe->cfgbox), title); sfree(title); - w = gtk_button_new_with_label("OK"); + w = gtk_button_new_from_stock(GTK_STOCK_CANCEL); gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area), w, FALSE, FALSE, 0); gtk_widget_show(w); - GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT); - gtk_window_set_default(GTK_WINDOW(fe->cfgbox), w); gtk_signal_connect(GTK_OBJECT(w), "clicked", - GTK_SIGNAL_FUNC(config_ok_button_clicked), fe); + GTK_SIGNAL_FUNC(config_cancel_button_clicked), fe); + cancel = w; - w = gtk_button_new_with_label("Cancel"); + w = gtk_button_new_from_stock(GTK_STOCK_OK); gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area), w, FALSE, FALSE, 0); gtk_widget_show(w); + GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT); + gtk_window_set_default(GTK_WINDOW(fe->cfgbox), w); gtk_signal_connect(GTK_OBJECT(w), "clicked", - GTK_SIGNAL_FUNC(config_cancel_button_clicked), fe); - cancel = w; + GTK_SIGNAL_FUNC(config_ok_button_clicked), fe); table = gtk_table_new(1, 2, FALSE); y = 0; @@ -924,7 +1470,7 @@ static int get_config(frontend *fe, int which) w = gtk_label_new(i->name); gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5); gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1, - GTK_EXPAND | GTK_SHRINK | GTK_FILL, + GTK_SHRINK | GTK_FILL, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 3, 3); gtk_widget_show(w); @@ -966,8 +1512,8 @@ static int get_config(frontend *fe, int which) w = gtk_label_new(i->name); gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5); gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1, - GTK_EXPAND | GTK_SHRINK | GTK_FILL, - GTK_EXPAND | GTK_SHRINK | GTK_FILL, + GTK_SHRINK | GTK_FILL, + GTK_EXPAND | GTK_SHRINK | GTK_FILL , 3, 3); gtk_widget_show(w); @@ -1076,77 +1622,154 @@ static void get_size(frontend *fe, int *px, int *py) gdk_window_resize(GTK_WIDGET(win)->window, x, y) #endif -static void menu_preset_event(GtkMenuItem *menuitem, gpointer data) +/* + * Called when any other code in this file has changed the + * selected game parameters. + */ +static void changed_preset(frontend *fe) +{ + int n = midend_which_preset(fe->me); + + fe->preset_threaded = TRUE; + if (n < 0 && fe->preset_custom) { + gtk_check_menu_item_set_active( + GTK_CHECK_MENU_ITEM(fe->preset_custom), + TRUE); + } else { + GSList *gs = fe->preset_radio; + int i = fe->n_preset_menu_items - 1 - n; + if (fe->preset_custom) + gs = gs->next; + while (i && gs) { + i--; + gs = gs->next; + } + if (gs) { + gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(gs->data), + TRUE); + } else for (gs = fe->preset_radio; gs; gs = gs->next) { + gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(gs->data), + FALSE); + } + } + fe->preset_threaded = FALSE; + + /* + * Update the greying on the Copy menu option. + */ + if (fe->copy_menu_item) { + int enabled = midend_can_format_as_text_now(fe->me); + gtk_widget_set_sensitive(fe->copy_menu_item, enabled); + } +} + +static gboolean not_size_allocated_yet(GtkWidget *w) +{ + /* + * This function tests whether a widget has not yet taken up space + * on the screen which it will occupy in future. (Therefore, it + * returns true only if the widget does exist but does not have a + * size allocation. A null widget is already taking up all the + * space it ever will.) + */ + if (!w) + return FALSE; /* nonexistent widgets aren't a problem */ + +#if GTK_CHECK_VERSION(2,18,0) /* skip if no gtk_widget_get_allocation */ + { + GtkAllocation a; + gtk_widget_get_allocation(w, &a); + if (a.height == 0 || a.width == 0) + return TRUE; /* widget exists but has no size yet */ + } +#endif + + return FALSE; +} + +static void try_shrink_drawing_area(frontend *fe) +{ + if (fe->drawing_area_shrink_pending && + !not_size_allocated_yet(fe->menubar) && + !not_size_allocated_yet(fe->statusbar)) { + /* + * In order to permit the user to resize the window smaller as + * well as bigger, we call this function after the window size + * has ended up where we want it. This shouldn't shrink the + * window immediately; it just arranges that the next time the + * user tries to shrink it, they can. + * + * However, at puzzle creation time, we defer the first of + * these operations until after the menu bar and status bar + * are actually visible. On Ubuntu 12.04 I've found that these + * can take a while to be displayed, and that it's a mistake + * to reduce the drawing area's size allocation before they've + * turned up or else the drawing area makes room for them by + * shrinking to less than the size we intended. + */ + gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), 1, 1); + fe->drawing_area_shrink_pending = FALSE; + } +} + +static gint configure_window(GtkWidget *widget, + GdkEventConfigure *event, gpointer data) { frontend *fe = (frontend *)data; - game_params *params = - (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data"); + /* + * When the main puzzle window changes size, it might be because + * the menu bar or status bar has turned up after starting off + * absent, in which case we should have another go at enacting a + * pending shrink of the drawing area. + */ + try_shrink_drawing_area(fe); + return FALSE; +} + +static void resize_fe(frontend *fe) +{ int x, y; - midend_set_params(fe->me, params); - midend_new_game(fe->me); get_size(fe, &x, &y); fe->w = x; fe->h = y; + fe->drawing_area_shrink_pending = FALSE; gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); { GtkRequisition req; gtk_widget_size_request(GTK_WIDGET(fe->window), &req); gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height); } + fe->drawing_area_shrink_pending = TRUE; + try_shrink_drawing_area(fe); } -GdkAtom compound_text_atom, utf8_string_atom; -int paste_initialised = FALSE; - -void init_paste() +static void menu_preset_event(GtkMenuItem *menuitem, gpointer data) { - unsigned char empty[] = { 0 }; + frontend *fe = (frontend *)data; + game_params *params = + (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data"); - if (paste_initialised) + if (fe->preset_threaded || + (GTK_IS_CHECK_MENU_ITEM(menuitem) && + !gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem)))) return; - - if (!compound_text_atom) - compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE); - if (!utf8_string_atom) - utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE); - - /* - * Ensure that all the cut buffers exist - according to the - * ICCCM, we must do this before we start using cut buffers. - */ - XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0); - XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0); - XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0); - XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0); - XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0); - XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0); - XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0); - XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0); -} - -/* Store data in a cut-buffer. */ -void store_cutbuffer(char *ptr, int len) -{ - /* ICCCM says we must rotate the buffers before storing to buffer 0. */ - XRotateBuffers(GDK_DISPLAY(), 1); - XStoreBytes(GDK_DISPLAY(), ptr, len); + midend_set_params(fe->me, params); + midend_new_game(fe->me); + changed_preset(fe); + resize_fe(fe); } -void write_clip(frontend *fe, char *data) -{ - init_paste(); +GdkAtom compound_text_atom, utf8_string_atom; +int paste_initialised = FALSE; - if (fe->paste_data) - sfree(fe->paste_data); +static void set_selection(frontend *fe, GdkAtom selection) +{ + if (!paste_initialised) { + compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE); + utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE); + paste_initialised = TRUE; + } /* * For this simple application we can safely assume that the @@ -1155,20 +1778,25 @@ void write_clip(frontend *fe, char *data) * COMPOUND_TEXT or UTF8_STRING. */ + if (gtk_selection_owner_set(fe->area, selection, CurrentTime)) { + gtk_selection_clear_targets(fe->area, selection); + gtk_selection_add_target(fe->area, selection, + GDK_SELECTION_TYPE_STRING, 1); + gtk_selection_add_target(fe->area, selection, compound_text_atom, 1); + gtk_selection_add_target(fe->area, selection, utf8_string_atom, 1); + } +} + +void write_clip(frontend *fe, char *data) +{ + if (fe->paste_data) + sfree(fe->paste_data); + fe->paste_data = data; fe->paste_data_len = strlen(data); - store_cutbuffer(fe->paste_data, fe->paste_data_len); - - if (gtk_selection_owner_set(fe->area, GDK_SELECTION_PRIMARY, - CurrentTime)) { - gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY, - GDK_SELECTION_TYPE_STRING, 1); - gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY, - compound_text_atom, 1); - gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY, - utf8_string_atom, 1); - } + set_selection(fe, GDK_SELECTION_PRIMARY); + set_selection(fe, GDK_SELECTION_CLIPBOARD); } void selection_get(GtkWidget *widget, GtkSelectionData *seldata, @@ -1205,6 +1833,8 @@ static void menu_copy_event(GtkMenuItem *menuitem, gpointer data) } } +#ifdef OLD_FILESEL + static void filesel_ok(GtkButton *button, gpointer data) { frontend *fe = (frontend *)data; @@ -1246,10 +1876,44 @@ static char *file_selector(frontend *fe, char *title, int save) return fe->filesel_name; } +#else + +static char *file_selector(frontend *fe, char *title, int save) +{ + char *filesel_name = NULL; + + GtkWidget *filesel = + gtk_file_chooser_dialog_new(title, + GTK_WINDOW(fe->window), + save ? GTK_FILE_CHOOSER_ACTION_SAVE : + GTK_FILE_CHOOSER_ACTION_OPEN, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + save ? GTK_STOCK_SAVE : GTK_STOCK_OPEN, + GTK_RESPONSE_ACCEPT, + NULL); + + if (gtk_dialog_run(GTK_DIALOG(filesel)) == GTK_RESPONSE_ACCEPT) { + const char *name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(filesel)); + filesel_name = dupstr(name); + } + + gtk_widget_destroy(filesel); + + return filesel_name; +} + +#endif + +struct savefile_write_ctx { + FILE *fp; + int error; +}; + static void savefile_write(void *wctx, void *buf, int len) { - FILE *fp = (FILE *)wctx; - fwrite(buf, 1, len, fp); + struct savefile_write_ctx *ctx = (struct savefile_write_ctx *)wctx; + if (fwrite(buf, 1, len, ctx->fp) < len) + ctx->error = errno; } static int savefile_read(void *wctx, void *buf, int len) @@ -1291,9 +1955,21 @@ static void menu_save_event(GtkMenuItem *menuitem, gpointer data) return; } - midend_serialise(fe->me, savefile_write, fp); + { + struct savefile_write_ctx ctx; + ctx.fp = fp; + ctx.error = 0; + midend_serialise(fe->me, savefile_write, &ctx); + fclose(fp); + if (ctx.error) { + char boxmsg[512]; + sprintf(boxmsg, "Error writing save file: %.400s", + strerror(errno)); + error_box(fe->window, boxmsg); + return; + } + } - fclose(fp); } } @@ -1301,7 +1977,6 @@ static void menu_load_event(GtkMenuItem *menuitem, gpointer data) { frontend *fe = (frontend *)data; char *name, *err; - int x, y; name = file_selector(fe, "Enter name of saved game file to load", FALSE); @@ -1323,16 +1998,8 @@ static void menu_load_event(GtkMenuItem *menuitem, gpointer data) return; } - get_size(fe, &x, &y); - fe->w = x; - fe->h = y; - gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); - { - GtkRequisition req; - gtk_widget_size_request(GTK_WIDGET(fe->window), &req); - gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height); - } - + changed_preset(fe); + resize_fe(fe); } } @@ -1359,21 +2026,17 @@ static void menu_config_event(GtkMenuItem *menuitem, gpointer data) frontend *fe = (frontend *)data; int which = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem), "user-data")); - int x, y; + if (fe->preset_threaded || + (GTK_IS_CHECK_MENU_ITEM(menuitem) && + !gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem)))) + return; + changed_preset(fe); /* Put the old preset back! */ if (!get_config(fe, which)) return; midend_new_game(fe->me); - get_size(fe, &x, &y); - fe->w = x; - fe->h = y; - gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); - { - GtkRequisition req; - gtk_widget_size_request(GTK_WIDGET(fe->window), &req); - gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height); - } + resize_fe(fe); } static void menu_about_event(GtkMenuItem *menuitem, gpointer data) @@ -1395,11 +2058,29 @@ static GtkWidget *add_menu_item_with_key(frontend *fe, GtkContainer *cont, char *text, int key) { GtkWidget *menuitem = gtk_menu_item_new_with_label(text); + int keyqual; gtk_container_add(cont, menuitem); gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", GINT_TO_POINTER(key)); gtk_signal_connect(GTK_OBJECT(menuitem), "activate", GTK_SIGNAL_FUNC(menu_key_event), fe); + switch (key & ~0x1F) { + case 0x00: + key += 0x60; + keyqual = GDK_CONTROL_MASK; + break; + case 0x40: + key += 0x20; + keyqual = GDK_SHIFT_MASK; + break; + default: + keyqual = 0; + break; + } + gtk_widget_add_accelerator(menuitem, + "activate", fe->accelgroup, + key, keyqual, + GTK_ACCEL_VISIBLE); gtk_widget_show(menuitem); return menuitem; } @@ -1411,13 +2092,19 @@ static void add_menu_separator(GtkContainer *cont) gtk_widget_show(menuitem); } -static frontend *new_window(char *arg, char **error) +enum { ARG_EITHER, ARG_SAVE, ARG_ID }; /* for argtype */ + +static frontend *new_window(char *arg, int argtype, char **error) { frontend *fe; - GtkBox *vbox; - GtkWidget *menubar, *menu, *menuitem; + GtkBox *vbox, *hbox; + GtkWidget *menu, *menuitem; + GdkPixmap *iconpm; + GList *iconlist; int x, y, n; char errbuf[1024]; + extern char *const *const xpm_icons[]; + extern const int n_xpm_icons; fe = snew(frontend); @@ -1428,29 +2115,53 @@ static frontend *new_window(char *arg, char **error) if (arg) { char *err; + FILE *fp; errbuf[0] = '\0'; - /* - * Try treating the argument as a game ID. - */ - err = midend_game_id(fe->me, arg); - if (!err) { - /* - * It's a valid game ID. - */ - midend_new_game(fe->me); - } else { - FILE *fp = fopen(arg, "r"); + switch (argtype) { + case ARG_ID: + err = midend_game_id(fe->me, arg); + if (!err) + midend_new_game(fe->me); + else + sprintf(errbuf, "Invalid game ID: %.800s", err); + break; + case ARG_SAVE: + fp = fopen(arg, "r"); if (!fp) { - sprintf(errbuf, "Supplied argument is neither a game ID (%.400s)" - " nor a save file (%.400s)", err, strerror(errno)); + sprintf(errbuf, "Error opening file: %.800s", strerror(errno)); } else { err = midend_deserialise(fe->me, savefile_read, fp); - sprintf(errbuf, "%.800s", err); - fclose(fp); + if (err) + sprintf(errbuf, "Invalid save file: %.800s", err); + fclose(fp); } - } + break; + default /*case ARG_EITHER*/: + /* + * First try treating the argument as a game ID. + */ + err = midend_game_id(fe->me, arg); + if (!err) { + /* + * It's a valid game ID. + */ + midend_new_game(fe->me); + } else { + FILE *fp = fopen(arg, "r"); + if (!fp) { + sprintf(errbuf, "Supplied argument is neither a game ID (%.400s)" + " nor a save file (%.400s)", err, strerror(errno)); + } else { + err = midend_deserialise(fe->me, savefile_read, fp); + if (err) + sprintf(errbuf, "%.800s", err); + fclose(fp); + } + } + break; + } if (*errbuf) { *error = dupstr(errbuf); midend_free(fe->me); @@ -1464,21 +2175,24 @@ static frontend *new_window(char *arg, char **error) fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(fe->window), thegame.name); -#if 0 - gtk_window_set_resizable(GTK_WINDOW(fe->window), FALSE); -#else - gtk_window_set_policy(GTK_WINDOW(fe->window), FALSE, FALSE, TRUE); -#endif + vbox = GTK_BOX(gtk_vbox_new(FALSE, 0)); gtk_container_add(GTK_CONTAINER(fe->window), GTK_WIDGET(vbox)); gtk_widget_show(GTK_WIDGET(vbox)); - menubar = gtk_menu_bar_new(); - gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0); - gtk_widget_show(menubar); + fe->accelgroup = gtk_accel_group_new(); + gtk_window_add_accel_group(GTK_WINDOW(fe->window), fe->accelgroup); - menuitem = gtk_menu_item_new_with_label("Game"); - gtk_container_add(GTK_CONTAINER(menubar), menuitem); + hbox = GTK_BOX(gtk_hbox_new(FALSE, 0)); + gtk_box_pack_start(vbox, GTK_WIDGET(hbox), FALSE, FALSE, 0); + gtk_widget_show(GTK_WIDGET(hbox)); + + fe->menubar = gtk_menu_bar_new(); + gtk_box_pack_start(hbox, fe->menubar, TRUE, TRUE, 0); + gtk_widget_show(fe->menubar); + + menuitem = gtk_menu_item_new_with_mnemonic("_Game"); + gtk_container_add(GTK_CONTAINER(fe->menubar), menuitem); gtk_widget_show(menuitem); menu = gtk_menu_new(); @@ -1508,12 +2222,16 @@ static frontend *new_window(char *arg, char **error) GTK_SIGNAL_FUNC(menu_config_event), fe); gtk_widget_show(menuitem); + fe->preset_radio = NULL; + fe->preset_custom = NULL; + fe->n_preset_menu_items = 0; + fe->preset_threaded = FALSE; if ((n = midend_num_presets(fe->me)) > 0 || thegame.can_configure) { GtkWidget *submenu; int i; - menuitem = gtk_menu_item_new_with_label("Type"); - gtk_container_add(GTK_CONTAINER(menubar), menuitem); + menuitem = gtk_menu_item_new_with_mnemonic("_Type"); + gtk_container_add(GTK_CONTAINER(fe->menubar), menuitem); gtk_widget_show(menuitem); submenu = gtk_menu_new(); @@ -1525,7 +2243,11 @@ static frontend *new_window(char *arg, char **error) midend_fetch_preset(fe->me, i, &name, ¶ms); - menuitem = gtk_menu_item_new_with_label(name); + menuitem = + gtk_radio_menu_item_new_with_label(fe->preset_radio, name); + fe->preset_radio = + gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(menuitem)); + fe->n_preset_menu_items++; gtk_container_add(GTK_CONTAINER(submenu), menuitem); gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", params); gtk_signal_connect(GTK_OBJECT(menuitem), "activate", @@ -1534,37 +2256,47 @@ static frontend *new_window(char *arg, char **error) } if (thegame.can_configure) { - menuitem = gtk_menu_item_new_with_label("Custom..."); + menuitem = fe->preset_custom = + gtk_radio_menu_item_new_with_label(fe->preset_radio, + "Custom..."); + fe->preset_radio = + gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(menuitem)); + gtk_container_add(GTK_CONTAINER(submenu), menuitem); gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", GPOINTER_TO_INT(CFG_SETTINGS)); - gtk_container_add(GTK_CONTAINER(submenu), menuitem); gtk_signal_connect(GTK_OBJECT(menuitem), "activate", GTK_SIGNAL_FUNC(menu_config_event), fe); gtk_widget_show(menuitem); } + } add_menu_separator(GTK_CONTAINER(menu)); - menuitem = gtk_menu_item_new_with_label("Load"); + menuitem = gtk_menu_item_new_with_label("Load..."); gtk_container_add(GTK_CONTAINER(menu), menuitem); gtk_signal_connect(GTK_OBJECT(menuitem), "activate", GTK_SIGNAL_FUNC(menu_load_event), fe); gtk_widget_show(menuitem); - menuitem = gtk_menu_item_new_with_label("Save"); + menuitem = gtk_menu_item_new_with_label("Save..."); gtk_container_add(GTK_CONTAINER(menu), menuitem); gtk_signal_connect(GTK_OBJECT(menuitem), "activate", GTK_SIGNAL_FUNC(menu_save_event), fe); gtk_widget_show(menuitem); +#ifndef STYLUS_BASED add_menu_separator(GTK_CONTAINER(menu)); add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Undo", 'u'); - add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Redo", '\x12'); - if (thegame.can_format_as_text) { + add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Redo", 'r'); +#endif + if (thegame.can_format_as_text_ever) { add_menu_separator(GTK_CONTAINER(menu)); menuitem = gtk_menu_item_new_with_label("Copy"); gtk_container_add(GTK_CONTAINER(menu), menuitem); gtk_signal_connect(GTK_OBJECT(menuitem), "activate", GTK_SIGNAL_FUNC(menu_copy_event), fe); gtk_widget_show(menuitem); + fe->copy_menu_item = menuitem; + } else { + fe->copy_menu_item = NULL; } if (thegame.can_solve) { add_menu_separator(GTK_CONTAINER(menu)); @@ -1577,8 +2309,8 @@ static frontend *new_window(char *arg, char **error) add_menu_separator(GTK_CONTAINER(menu)); add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Exit", 'q'); - menuitem = gtk_menu_item_new_with_label("Help"); - gtk_container_add(GTK_CONTAINER(menubar), menuitem); + menuitem = gtk_menu_item_new_with_mnemonic("_Help"); + gtk_container_add(GTK_CONTAINER(fe->menubar), menuitem); gtk_widget_show(menuitem); menu = gtk_menu_new(); @@ -1590,31 +2322,45 @@ static frontend *new_window(char *arg, char **error) GTK_SIGNAL_FUNC(menu_about_event), fe); gtk_widget_show(menuitem); - { - int i, ncolours; - float *colours; - gboolean *success; - - fe->colmap = gdk_colormap_get_system(); - colours = midend_colours(fe->me, &ncolours); - fe->ncolours = ncolours; - fe->colours = snewn(ncolours, GdkColor); - for (i = 0; i < ncolours; i++) { - fe->colours[i].red = colours[i*3] * 0xFFFF; - fe->colours[i].green = colours[i*3+1] * 0xFFFF; - fe->colours[i].blue = colours[i*3+2] * 0xFFFF; - } - success = snewn(ncolours, gboolean); - gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours, - FALSE, FALSE, success); - for (i = 0; i < ncolours; i++) { - if (!success[i]) - g_error("couldn't allocate colour %d (#%02x%02x%02x)\n", - i, fe->colours[i].red >> 8, - fe->colours[i].green >> 8, - fe->colours[i].blue >> 8); - } +#ifdef STYLUS_BASED + menuitem=gtk_button_new_with_mnemonic("_Redo"); + gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", + GINT_TO_POINTER((int)('r'))); + gtk_signal_connect(GTK_OBJECT(menuitem), "clicked", + GTK_SIGNAL_FUNC(menu_key_event), fe); + gtk_box_pack_end(hbox, menuitem, FALSE, FALSE, 0); + gtk_widget_show(menuitem); + + menuitem=gtk_button_new_with_mnemonic("_Undo"); + gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", + GINT_TO_POINTER((int)('u'))); + gtk_signal_connect(GTK_OBJECT(menuitem), "clicked", + GTK_SIGNAL_FUNC(menu_key_event), fe); + gtk_box_pack_end(hbox, menuitem, FALSE, FALSE, 0); + gtk_widget_show(menuitem); + + if (thegame.flags & REQUIRE_NUMPAD) { + hbox = GTK_BOX(gtk_hbox_new(FALSE, 0)); + gtk_box_pack_start(vbox, GTK_WIDGET(hbox), FALSE, FALSE, 0); + gtk_widget_show(GTK_WIDGET(hbox)); + + *((int*)errbuf)=0; + errbuf[1]='\0'; + for(errbuf[0]='0';errbuf[0]<='9';errbuf[0]++) { + menuitem=gtk_button_new_with_label(errbuf); + gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", + GINT_TO_POINTER((int)(errbuf[0]))); + gtk_signal_connect(GTK_OBJECT(menuitem), "clicked", + GTK_SIGNAL_FUNC(menu_key_event), fe); + gtk_box_pack_start(hbox, menuitem, TRUE, TRUE, 0); + gtk_widget_show(menuitem); + } } +#endif /* STYLUS_BASED */ + + changed_preset(fe); + + snaffle_colours(fe); if (midend_wants_statusbar(fe->me)) { GtkWidget *viewport; @@ -1640,19 +2386,21 @@ static frontend *new_window(char *arg, char **error) fe->statusbar = NULL; fe->area = gtk_drawing_area_new(); +#if GTK_CHECK_VERSION(2,0,0) + GTK_WIDGET_UNSET_FLAGS(fe->area, GTK_DOUBLE_BUFFERED); +#endif get_size(fe, &x, &y); + fe->drawing_area_shrink_pending = FALSE; gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); fe->w = x; fe->h = y; gtk_box_pack_end(vbox, fe->area, TRUE, TRUE, 0); - fe->pixmap = NULL; + clear_backing_store(fe); fe->fonts = NULL; fe->nfonts = fe->fontsize = 0; - fe->laststatus = NULL; - fe->paste_data = NULL; fe->paste_data_len = 0; @@ -1676,17 +2424,36 @@ static frontend *new_window(char *arg, char **error) GTK_SIGNAL_FUNC(map_window), fe); gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event", GTK_SIGNAL_FUNC(configure_area), fe); + gtk_signal_connect(GTK_OBJECT(fe->window), "configure_event", + GTK_SIGNAL_FUNC(configure_window), fe); gtk_widget_add_events(GTK_WIDGET(fe->area), GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | - GDK_BUTTON_MOTION_MASK); + GDK_BUTTON_MOTION_MASK | + GDK_POINTER_MOTION_HINT_MASK); + + if (n_xpm_icons) { + gtk_widget_realize(fe->window); + iconpm = gdk_pixmap_create_from_xpm_d(fe->window->window, NULL, + NULL, (gchar **)xpm_icons[0]); + gdk_window_set_icon(fe->window->window, NULL, iconpm, NULL); + iconlist = NULL; + for (n = 0; n < n_xpm_icons; n++) { + iconlist = + g_list_append(iconlist, + gdk_pixbuf_new_from_xpm_data((const gchar **) + xpm_icons[n])); + } + gdk_window_set_icon_list(fe->window->window, iconlist); + } gtk_widget_show(fe->area); gtk_widget_show(fe->window); - gdk_window_set_background(fe->area->window, &fe->colours[0]); - gdk_window_set_background(fe->window->window, &fe->colours[0]); + fe->drawing_area_shrink_pending = TRUE; + try_shrink_drawing_area(fe); + set_window_background(fe, 0); return fe; } @@ -1717,7 +2484,11 @@ int main(int argc, char **argv) int ngenerate = 0, print = FALSE, px = 1, py = 1; int soln = FALSE, colour = FALSE; float scale = 1.0F; + float redo_proportion = 0.0F; + char *savefile = NULL, *savesuffix = NULL; char *arg = NULL; + int argtype = ARG_EITHER; + char *screenshot_file = NULL; int doing_opts = TRUE; int ac = argc; char **av = argv; @@ -1763,6 +2534,23 @@ int main(int argc, char **argv) } } else ngenerate = 1; + } else if (doing_opts && !strcmp(p, "--save")) { + if (--ac > 0) { + savefile = *++av; + } else { + fprintf(stderr, "%s: '--save' expected a filename\n", + pname); + return 1; + } + } else if (doing_opts && (!strcmp(p, "--save-suffix") || + !strcmp(p, "--savesuffix"))) { + if (--ac > 0) { + savesuffix = *++av; + } else { + fprintf(stderr, "%s: '--save-suffix' expected a filename\n", + pname); + return 1; + } } else if (doing_opts && !strcmp(p, "--print")) { if (!thegame.can_print) { fprintf(stderr, "%s: this game does not support printing\n", @@ -1788,6 +2576,49 @@ int main(int argc, char **argv) pname); return 1; } + } else if (doing_opts && !strcmp(p, "--redo")) { + /* + * This is an internal option which I don't expect + * users to have any particular use for. The effect of + * --redo is that once the game has been loaded and + * initialised, the next move in the redo chain is + * replayed, and the game screen is redrawn part way + * through the making of the move. This is only + * meaningful if there _is_ a next move in the redo + * chain, which means in turn that this option is only + * useful if you're also passing a save file on the + * command line. + * + * This option is used by the script which generates + * the puzzle icons and website screenshots, and I + * don't imagine it's useful for anything else. + * (Unless, I suppose, users don't like my screenshots + * and want to generate their own in the same way for + * some repackaged version of the puzzles.) + */ + if (--ac > 0) { + redo_proportion = atof(*++av); + } else { + fprintf(stderr, "%s: no argument supplied to '--redo'\n", + pname); + return 1; + } + } else if (doing_opts && !strcmp(p, "--screenshot")) { + /* + * Another internal option for the icon building + * script. This causes a screenshot of the central + * drawing area (i.e. not including the menu bar or + * status bar) to be saved to a PNG file once the + * window has been drawn, and then the application + * quits immediately. + */ + if (--ac > 0) { + screenshot_file = *++av; + } else { + fprintf(stderr, "%s: no argument supplied to '--screenshot'\n", + pname); + return 1; + } } else if (doing_opts && (!strcmp(p, "--with-solutions") || !strcmp(p, "--with-solution") || !strcmp(p, "--with-solns") || @@ -1804,6 +2635,10 @@ int main(int argc, char **argv) return 1; } colour = TRUE; + } else if (doing_opts && !strcmp(p, "--load")) { + argtype = ARG_SAVE; + } else if (doing_opts && !strcmp(p, "--game")) { + argtype = ARG_ID; } else if (doing_opts && !strcmp(p, "--")) { doing_opts = FALSE; } else if (!doing_opts || p[0] != '-') { @@ -1841,22 +2676,32 @@ int main(int argc, char **argv) * you may specify it to be 1). Sorry; that was the * simplest-to-parse command-line syntax I came up with. */ - if (ngenerate > 0 || print) { + if (ngenerate > 0 || print || savefile || savesuffix) { int i, n = 1; midend *me; char *id; document *doc = NULL; - if (*errbuf) { - fputs(errbuf, stderr); - return 1; - } + /* + * If we're in this branch, we should display any pending + * error message from the command line, since GTK isn't going + * to take another crack at making sense of it. + */ + if (*errbuf) { + fputs(errbuf, stderr); + return 1; + } n = ngenerate; me = midend_new(NULL, &thegame, NULL, NULL); i = 0; + if (savefile && !savesuffix) + savesuffix = ""; + if (!savefile && savesuffix) + savefile = ""; + if (print) doc = document_new(px, py, scale); @@ -1913,7 +2758,43 @@ int main(int argc, char **argv) fprintf(stderr, "%s: error in printing: %s\n", pname, err); return 1; } - } else { + } + if (savefile) { + struct savefile_write_ctx ctx; + char *realname = snewn(40 + strlen(savefile) + + strlen(savesuffix), char); + sprintf(realname, "%s%d%s", savefile, i, savesuffix); + + if (soln) { + char *err = midend_solve(me); + if (err) { + fprintf(stderr, "%s: unable to show solution: %s\n", + realname, err); + return 1; + } + } + + ctx.fp = fopen(realname, "w"); + if (!ctx.fp) { + fprintf(stderr, "%s: open: %s\n", realname, + strerror(errno)); + return 1; + } + ctx.error = 0; + midend_serialise(me, savefile_write, &ctx); + if (ctx.error) { + fprintf(stderr, "%s: write: %s\n", realname, + strerror(ctx.error)); + return 1; + } + if (fclose(ctx.fp)) { + fprintf(stderr, "%s: close: %s\n", realname, + strerror(errno)); + return 1; + } + sfree(realname); + } + if (!doc && !savefile) { id = midend_get_game_id(me); puts(id); sfree(id); @@ -1933,14 +2814,39 @@ int main(int argc, char **argv) return 0; } else { + frontend *fe; gtk_init(&argc, &argv); - if (!new_window(argc > 1 ? argv[1] : NULL, &error)) { + fe = new_window(arg, argtype, &error); + + if (!fe) { fprintf(stderr, "%s: %s\n", pname, error); return 1; } + if (screenshot_file) { + /* + * Some puzzles will not redraw their entire area if + * given a partially completed animation, which means + * we must redraw now and _then_ redraw again after + * freezing the move timer. + */ + midend_force_redraw(fe->me); + } + + if (redo_proportion) { + /* Start a redo. */ + midend_process_key(fe->me, 0, 0, 'r'); + /* And freeze the timer at the specified position. */ + midend_freeze_timer(fe->me, redo_proportion); + } + + if (screenshot_file) { + save_screenshot_png(fe, screenshot_file); + exit(0); + } + gtk_main(); }