X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/28b5987d5dbdcb396c07b937a638590e5361361e..dbdfc6eadd04c4c91c9ff11c57f0559fb12611ca:/gtk.c diff --git a/gtk.c b/gtk.c index 1a79f0e..5761e8d 100644 --- a/gtk.c +++ b/gtk.c @@ -8,12 +8,15 @@ #include #include #include +#include #include #include #include +#include + #include #include #include @@ -22,7 +25,15 @@ #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 #ifdef DEBUGGING @@ -76,6 +87,8 @@ void fatal(char *fmt, ...) * GTK front end to puzzles. */ +static void changed_preset(frontend *fe); + struct font { #ifdef USE_PANGO PangoFontDescription *desc; @@ -95,6 +108,7 @@ struct font { */ struct frontend { GtkWidget *window; + GtkAccelGroup *accelgroup; GtkWidget *area; GtkWidget *statusbar; guint statusctx; @@ -103,7 +117,7 @@ struct frontend { int ncolours; GdkColormap *colmap; int w, h; - midend_data *me; + midend *me; GdkGC *gc; int bbox_l, bbox_r, bbox_u, bbox_d; int timer_active, timer_id; @@ -115,10 +129,15 @@ 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 npresets; + GtkWidget **preset_bullets; + GtkWidget *preset_custom_bullet; + GtkWidget *copy_menu_item; }; void get_random_seed(void **randseed, int *randseedsize) @@ -137,25 +156,19 @@ void frontend_default_colour(frontend *fe, float *output) output[2] = col.blue / 65535.0; } -void status_bar(frontend *fe, char *text) +void gtk_status_bar(void *handle, char *text) { - char *rewritten; + frontend *fe = (frontend *)handle; 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); } -void start_draw(frontend *fe) +void gtk_start_draw(void *handle) { + frontend *fe = (frontend *)handle; fe->gc = gdk_gc_new(fe->area->window); fe->bbox_l = fe->w; fe->bbox_r = 0; @@ -163,8 +176,9 @@ void start_draw(frontend *fe) fe->bbox_d = 0; } -void clip(frontend *fe, int x, int y, int w, int h) +void gtk_clip(void *handle, int x, int y, int w, int h) { + frontend *fe = (frontend *)handle; GdkRectangle rect; rect.x = x; @@ -175,8 +189,9 @@ void clip(frontend *fe, int x, int y, int w, int h) gdk_gc_set_clip_rectangle(fe->gc, &rect); } -void unclip(frontend *fe) +void gtk_unclip(void *handle) { + frontend *fe = (frontend *)handle; GdkRectangle rect; rect.x = 0; @@ -187,9 +202,10 @@ void unclip(frontend *fe) gdk_gc_set_clip_rectangle(fe->gc, &rect); } -void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize, - int align, int colour, char *text) +void gtk_draw_text(void *handle, int x, int y, int fonttype, int fontsize, + int align, int colour, char *text) { + frontend *fe = (frontend *)handle; int i; /* @@ -284,6 +300,8 @@ void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize, if (align & ALIGN_VCENTRE) rect.y -= rect.height / 2; + else + rect.y -= rect.height; if (align & ALIGN_HCENTRE) rect.x -= rect.width / 2; @@ -311,6 +329,8 @@ void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize, &lb, &rb, &wid, &asc, &desc); if (align & ALIGN_VCENTRE) y += asc - (asc+desc)/2; + else + y += asc; /* * ... but horizontal extents with respect to the provided @@ -335,21 +355,24 @@ void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize, } -void draw_rect(frontend *fe, int x, int y, int w, int h, int colour) +void gtk_draw_rect(void *handle, int x, int y, int w, int h, int colour) { + 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); } -void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour) +void gtk_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour) { + frontend *fe = (frontend *)handle; gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2); } -void draw_polygon(frontend *fe, int *coords, int npoints, - int fillcolour, int outlinecolour) +void gtk_draw_poly(void *handle, int *coords, int npoints, + int fillcolour, int outlinecolour) { + frontend *fe = (frontend *)handle; GdkPoint *points = snewn(npoints, GdkPoint); int i; @@ -364,14 +387,25 @@ void draw_polygon(frontend *fe, int *coords, int npoints, } assert(outlinecolour >= 0); gdk_gc_set_foreground(fe->gc, &fe->colours[outlinecolour]); - gdk_draw_polygon(fe->pixmap, fe->gc, FALSE, points, npoints); + + /* + * 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 < 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); sfree(points); } -void draw_circle(frontend *fe, int cx, int cy, int radius, - int fillcolour, int outlinecolour) +void gtk_draw_circle(void *handle, int cx, int cy, int radius, + int fillcolour, int outlinecolour) { + frontend *fe = (frontend *)handle; if (fillcolour >= 0) { gdk_gc_set_foreground(fe->gc, &fe->colours[fillcolour]); gdk_draw_arc(fe->pixmap, fe->gc, TRUE, @@ -391,7 +425,7 @@ struct blitter { int w, h, x, y; }; -blitter *blitter_new(int w, int h) +blitter *gtk_blitter_new(void *handle, int w, int h) { /* * We can't create the pixmap right now, because fe->window @@ -405,15 +439,16 @@ blitter *blitter_new(int w, int h) return bl; } -void blitter_free(blitter *bl) +void gtk_blitter_free(void *handle, blitter *bl) { if (bl->pixmap) gdk_pixmap_unref(bl->pixmap); sfree(bl); } -void blitter_save(frontend *fe, blitter *bl, int x, int y) +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); bl->x = x; @@ -424,8 +459,9 @@ void blitter_save(frontend *fe, blitter *bl, int x, int y) x, y, 0, 0, bl->w, bl->h); } -void blitter_load(frontend *fe, blitter *bl, int x, int y) +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; @@ -437,16 +473,18 @@ void blitter_load(frontend *fe, blitter *bl, int x, int y) 0, 0, x, y, bl->w, bl->h); } -void draw_update(frontend *fe, int x, int y, int w, int h) +void gtk_draw_update(void *handle, int x, int y, int w, int h) { + frontend *fe = (frontend *)handle; if (fe->bbox_l > x ) fe->bbox_l = x ; if (fe->bbox_r < x+w) fe->bbox_r = x+w; if (fe->bbox_u > y ) fe->bbox_u = y ; if (fe->bbox_d < y+h) fe->bbox_d = y+h; } -void end_draw(frontend *fe) +void gtk_end_draw(void *handle) { + frontend *fe = (frontend *)handle; gdk_gc_unref(fe->gc); fe->gc = NULL; @@ -460,6 +498,26 @@ void end_draw(frontend *fe) } } +const struct drawing_api gtk_drawing = { + gtk_draw_text, + gtk_draw_rect, + gtk_draw_line, + gtk_draw_poly, + gtk_draw_circle, + gtk_draw_update, + gtk_clip, + gtk_unclip, + gtk_start_draw, + gtk_end_draw, + gtk_status_bar, + gtk_blitter_new, + gtk_blitter_free, + gtk_blitter_save, + gtk_blitter_load, + NULL, NULL, NULL, NULL, NULL, NULL, /* {begin,end}_{doc,page,puzzle} */ + NULL, /* line_width */ +}; + static void destroy(GtkWidget *widget, gpointer data) { frontend *fe = (frontend *)data; @@ -478,6 +536,20 @@ static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data) if (!fe->pixmap) 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) keyval = shift | ctrl | CURSOR_UP; else if (event->keyval == GDK_KP_Up || event->keyval == GDK_KP_8) @@ -506,6 +578,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 @@ -628,6 +704,8 @@ static gint configure_area(GtkWidget *widget, 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(widget->window, gc, 1, 0, 0, + event->width, event->height); gdk_gc_unref(gc); midend_force_redraw(fe->me); @@ -654,6 +732,8 @@ static gint timer_func(gpointer data) void deactivate_timer(frontend *fe) { + if (!fe) + return; /* can happen due to --generate */ if (fe->timer_active) gtk_timeout_remove(fe->timer_id); fe->timer_active = FALSE; @@ -661,6 +741,8 @@ void deactivate_timer(frontend *fe) void activate_timer(frontend *fe) { + if (!fe) + return; /* can happen due to --generate */ if (!fe->timer_active) { fe->timer_id = gtk_timeout_add(20, timer_func, fe); gettimeofday(&fe->last_time, NULL); @@ -782,6 +864,7 @@ static void config_ok_button_clicked(GtkButton *button, gpointer data) else { fe->cfgret = TRUE; gtk_widget_destroy(fe->cfgbox); + changed_preset(fe); } } @@ -1038,15 +1121,48 @@ 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) +static void update_menuitem_bullet(GtkWidget *label, int visible) +{ + if (visible) { + gtk_label_set_text(GTK_LABEL(label), "\xE2\x80\xA2"); + } else { + gtk_label_set_text(GTK_LABEL(label), ""); + } +} + +/* + * 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); + int i; + + /* + * Update the tick mark in the Type menu. + */ + if (fe->preset_bullets) { + for (i = 0; i < fe->npresets; i++) + update_menuitem_bullet(fe->preset_bullets[i], n == i); + } + if (fe->preset_custom_bullet) { + update_menuitem_bullet(fe->preset_custom_bullet, n < 0); + } + + /* + * 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 void resize_fe(frontend *fe) { - frontend *fe = (frontend *)data; - game_params *params = - (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data"); 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; @@ -1056,6 +1172,24 @@ static void menu_preset_event(GtkMenuItem *menuitem, gpointer data) gtk_widget_size_request(GTK_WIDGET(fe->window), &req); gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height); } + /* + * Now that we've established the preferred size of the window, + * reduce the drawing area's size request so the user can shrink + * the window. + */ + gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), 1, 1); +} + +static void menu_preset_event(GtkMenuItem *menuitem, gpointer data) +{ + frontend *fe = (frontend *)data; + game_params *params = + (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data"); + + midend_set_params(fe->me, params); + midend_new_game(fe->me); + changed_preset(fe); + resize_fe(fe); } GdkAtom compound_text_atom, utf8_string_atom; @@ -1124,6 +1258,7 @@ void write_clip(frontend *fe, char *data) if (gtk_selection_owner_set(fe->area, GDK_SELECTION_PRIMARY, CurrentTime)) { + gtk_selection_clear_targets(fe->area, GDK_SELECTION_PRIMARY); gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY, GDK_SELECTION_TYPE_STRING, 1); gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY, @@ -1167,6 +1302,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; @@ -1208,10 +1345,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) @@ -1253,9 +1424,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); } } @@ -1263,7 +1446,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); @@ -1285,16 +1467,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); } } @@ -1321,21 +1495,12 @@ 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 (!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) @@ -1357,11 +1522,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; } @@ -1373,46 +1556,130 @@ static void add_menu_separator(GtkContainer *cont) gtk_widget_show(menuitem); } -static frontend *new_window(char *game_id, char **error) +enum { ARG_EITHER, ARG_SAVE, ARG_ID }; /* for argtype */ + +static GtkWidget *make_preset_menuitem(GtkWidget **bulletlabel, + const char *name) +{ + GtkWidget *hbox, *lab1, *lab2, *menuitem; + GtkRequisition req; + + hbox = gtk_hbox_new(FALSE, 0); + gtk_widget_show(hbox); + lab1 = gtk_label_new("\xE2\x80\xA2 "); + gtk_widget_show(lab1); + gtk_box_pack_start(GTK_BOX(hbox), lab1, FALSE, FALSE, 0); + gtk_misc_set_alignment(GTK_MISC(lab1), 0.0, 0.0); + lab2 = gtk_label_new(name); + gtk_widget_show(lab2); + gtk_box_pack_start(GTK_BOX(hbox), lab2, TRUE, TRUE, 0); + gtk_misc_set_alignment(GTK_MISC(lab2), 0.0, 0.0); + + gtk_widget_size_request(lab1, &req); + gtk_widget_set_usize(lab1, req.width, -1); + gtk_label_set_text(GTK_LABEL(lab1), ""); + + menuitem = gtk_menu_item_new(); + gtk_container_add(GTK_CONTAINER(menuitem), hbox); + + *bulletlabel = lab1; + return menuitem; +} + +static frontend *new_window(char *arg, int argtype, char **error) { frontend *fe; GtkBox *vbox; GtkWidget *menubar, *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); fe->timer_active = FALSE; fe->timer_id = -1; - fe->me = midend_new(fe, &thegame); + fe->me = midend_new(fe, &thegame, >k_drawing, fe); - if (game_id) { - *error = midend_game_id(fe->me, game_id); - if (*error) { - midend_free(fe->me); - sfree(fe); - return NULL; - } + if (arg) { + char *err; + FILE *fp; + + errbuf[0] = '\0'; + + 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, "Error opening file: %.800s", strerror(errno)); + } else { + err = midend_deserialise(fe->me, savefile_read, 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); + sfree(fe); + return NULL; + } + + } else { + midend_new_game(fe->me); } - midend_new_game(fe->me); 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)); + fe->accelgroup = gtk_accel_group_new(); + gtk_window_add_accel_group(GTK_WINDOW(fe->window), fe->accelgroup); + menubar = gtk_menu_bar_new(); gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0); gtk_widget_show(menubar); - menuitem = gtk_menu_item_new_with_label("Game"); + menuitem = gtk_menu_item_new_with_mnemonic("_Game"); gtk_container_add(GTK_CONTAINER(menubar), menuitem); gtk_widget_show(menuitem); @@ -1447,20 +1714,24 @@ static frontend *new_window(char *game_id, char **error) GtkWidget *submenu; int i; - menuitem = gtk_menu_item_new_with_label("Type"); + menuitem = gtk_menu_item_new_with_mnemonic("_Type"); gtk_container_add(GTK_CONTAINER(menubar), menuitem); gtk_widget_show(menuitem); submenu = gtk_menu_new(); gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu); + fe->npresets = n; + fe->preset_bullets = snewn(n, GtkWidget *); + for (i = 0; i < n; i++) { char *name; game_params *params; midend_fetch_preset(fe->me, i, &name, ¶ms); - menuitem = gtk_menu_item_new_with_label(name); + menuitem = make_preset_menuitem(&fe->preset_bullets[i], name); + gtk_container_add(GTK_CONTAINER(submenu), menuitem); gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", params); gtk_signal_connect(GTK_OBJECT(menuitem), "activate", @@ -1469,37 +1740,48 @@ static frontend *new_window(char *game_id, char **error) } if (thegame.can_configure) { - menuitem = gtk_menu_item_new_with_label("Custom..."); + menuitem = make_preset_menuitem(&fe->preset_custom_bullet, + "Custom..."); + + 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); - } + } else + fe->preset_custom_bullet = NULL; + + } else { + fe->npresets = 0; + fe->preset_bullets = NULL; + fe->preset_custom_bullet = NULL; } 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); 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'); + 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)); @@ -1512,7 +1794,7 @@ static frontend *new_window(char *game_id, 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"); + menuitem = gtk_menu_item_new_with_mnemonic("_Help"); gtk_container_add(GTK_CONTAINER(menubar), menuitem); gtk_widget_show(menuitem); @@ -1525,6 +1807,8 @@ static frontend *new_window(char *game_id, char **error) GTK_SIGNAL_FUNC(menu_about_event), fe); gtk_widget_show(menuitem); + changed_preset(fe); + { int i, ncolours; float *colours; @@ -1543,11 +1827,12 @@ static frontend *new_window(char *game_id, char **error) gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours, FALSE, FALSE, success); for (i = 0; i < ncolours; i++) { - if (!success[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); + } } } @@ -1586,8 +1871,6 @@ static frontend *new_window(char *game_id, char **error) fe->fonts = NULL; fe->nfonts = fe->fontsize = 0; - fe->laststatus = NULL; - fe->paste_data = NULL; fe->paste_data_len = 0; @@ -1617,24 +1900,237 @@ static frontend *new_window(char *game_id, char **error) GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_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); + /* + * Now that we've established the preferred size of the window, + * reduce the drawing area's size request so the user can shrink + * the window. + */ + gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), 1, 1); + gdk_window_set_background(fe->area->window, &fe->colours[0]); gdk_window_set_background(fe->window->window, &fe->colours[0]); return fe; } +char *fgetline(FILE *fp) +{ + char *ret = snewn(512, char); + int size = 512, len = 0; + while (fgets(ret + len, size - len, fp)) { + len += strlen(ret + len); + if (ret[len-1] == '\n') + break; /* got a newline, we're done */ + size = len + 512; + ret = sresize(ret, size, char); + } + if (len == 0) { /* first fgets returned NULL */ + sfree(ret); + return NULL; + } + ret[len] = '\0'; + return ret; +} + int main(int argc, char **argv) { char *pname = argv[0]; char *error; + 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; + char errbuf[500]; - if (argc > 1 && !strcmp(argv[1], "--version")) { - printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n", - thegame.name, ver); - return 0; + /* + * Command line parsing in this function is rather fiddly, + * because GTK wants to have a go at argc/argv _first_ - and + * yet we can't let it, because gtk_init() will bomb out if it + * can't open an X display, whereas in fact we want to permit + * our --generate and --print modes to run without an X + * display. + * + * So what we do is: + * - we parse the command line ourselves, without modifying + * argc/argv + * - if we encounter an error which might plausibly be the + * result of a GTK command line (i.e. not detailed errors in + * particular options of ours) we store the error message + * and terminate parsing. + * - if we got enough out of the command line to know it + * specifies a non-X mode of operation, we either display + * the stored error and return failure, or if there is no + * stored error we do the non-X operation and return + * success. + * - otherwise, we go straight to gtk_init(). + */ + + errbuf[0] = '\0'; + while (--ac > 0) { + char *p = *++av; + if (doing_opts && !strcmp(p, "--version")) { + printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n", + thegame.name, ver); + return 0; + } else if (doing_opts && !strcmp(p, "--generate")) { + if (--ac > 0) { + ngenerate = atoi(*++av); + if (!ngenerate) { + fprintf(stderr, "%s: '--generate' expected a number\n", + pname); + return 1; + } + } 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", + pname); + return 1; + } + print = TRUE; + if (--ac > 0) { + char *dim = *++av; + if (sscanf(dim, "%dx%d", &px, &py) != 2) { + fprintf(stderr, "%s: unable to parse argument '%s' to " + "'--print'\n", pname, dim); + return 1; + } + } else { + px = py = 1; + } + } else if (doing_opts && !strcmp(p, "--scale")) { + if (--ac > 0) { + scale = atof(*++av); + } else { + fprintf(stderr, "%s: no argument supplied to '--scale'\n", + 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") || + !strcmp(p, "--with-soln") || + !strcmp(p, "--solutions") || + !strcmp(p, "--solution") || + !strcmp(p, "--solns") || + !strcmp(p, "--soln"))) { + soln = TRUE; + } else if (doing_opts && !strcmp(p, "--colour")) { + if (!thegame.can_print_in_colour) { + fprintf(stderr, "%s: this game does not support colour" + " printing\n", pname); + 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] != '-') { + if (arg) { + fprintf(stderr, "%s: more than one argument supplied\n", + pname); + return 1; + } + arg = p; + } else { + sprintf(errbuf, "%.100s: unrecognised option '%.100s'\n", + pname, p); + break; + } + } + + if (*errbuf) { + fputs(errbuf, stderr); + return 1; } /* @@ -1642,7 +2138,7 @@ int main(int argc, char **argv) * command line. Useful for generating puzzles to be printed * out and solved offline (for puzzles where that even makes * sense - Solo, for example, is a lot more pencil-and-paper - * friendly than Net!) + * friendly than Twiddle!) * * Usage: * @@ -1658,60 +2154,164 @@ 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 (argc > 1 && !strcmp(argv[1], "--generate")) { - int n = 1; - char *params = NULL, *seed = NULL; - game_params *par; - random_state *rs; - char *parstr; - - if (argc > 2) - n = atoi(argv[2]); - if (argc > 3) - params = argv[3]; - - par = thegame.default_params(); - if (params) { - if ( (seed = strchr(params, '#')) != NULL ) - *seed++ = '\0'; - thegame.decode_params(par, params); - } - if ((error = thegame.validate_params(par)) != NULL) { - fprintf(stderr, "%s: %s\n", pname, error); - return 1; - } - parstr = thegame.encode_params(par, FALSE); + if (ngenerate > 0 || print || savefile || savesuffix) { + int i, n = 1; + midend *me; + char *id; + document *doc = NULL; - { - void *seeddata; - int seedlen; - if (seed) { - seeddata = seed; - seedlen = strlen(seed); - } else { - get_random_seed(&seeddata, &seedlen); - } - rs = random_init(seeddata, seedlen); + 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); + + /* + * In this loop, we either generate a game ID or read one + * from stdin depending on whether we're in generate mode; + * then we either write it to stdout or print it, depending + * on whether we're in print mode. Thus, this loop handles + * generate-to-stdout, print-from-stdin and generate-and- + * immediately-print modes. + * + * (It could also handle a copy-stdin-to-stdout mode, + * although there's currently no combination of options + * which will cause this loop to be activated in that mode. + * It wouldn't be _entirely_ pointless, though, because + * stdin could contain bare params strings or random-seed + * IDs, and stdout would contain nothing but fully + * generated descriptive game IDs.) + */ + while (ngenerate == 0 || i < n) { + char *pstr, *err; + + if (ngenerate == 0) { + pstr = fgetline(stdin); + if (!pstr) + break; + pstr[strcspn(pstr, "\r\n")] = '\0'; + } else { + if (arg) { + pstr = snewn(strlen(arg) + 40, char); + + strcpy(pstr, arg); + if (i > 0 && strchr(arg, '#')) + sprintf(pstr + strlen(pstr), "-%d", i); + } else + pstr = NULL; + } + + if (pstr) { + err = midend_game_id(me, pstr); + if (err) { + fprintf(stderr, "%s: error parsing '%s': %s\n", + pname, pstr, err); + return 1; + } + } + sfree(pstr); + + midend_new_game(me); + + if (doc) { + err = midend_print_puzzle(me, doc, soln); + if (err) { + fprintf(stderr, "%s: error in printing: %s\n", pname, err); + return 1; + } + } + if (savefile) { + struct savefile_write_ctx ctx; + char *realname = snewn(40 + strlen(savefile) + + strlen(savesuffix), char); + sprintf(realname, "%s%d%s", savefile, i, savesuffix); + ctx.fp = fopen(realname, "w"); + if (!ctx.fp) { + fprintf(stderr, "%s: open: %s\n", realname, + strerror(errno)); + return 1; + } + sfree(realname); + 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; + } + } + if (!doc && !savefile) { + id = midend_get_game_id(me); + puts(id); + sfree(id); + } + + i++; } - while (n-- > 0) { - char *aux = NULL; - char *desc = thegame.new_desc(par, rs, &aux, FALSE); - printf("%s:%s\n", parstr, desc); - sfree(desc); - sfree(aux); + if (doc) { + psdata *ps = ps_init(stdout, colour); + document_print(doc, ps_drawing_api(ps)); + document_free(doc); + ps_free(ps); } + midend_free(me); + 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) { + 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); + + exit(0); + } + gtk_main(); }