X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/810aa59ea0ad9197a43a14864f5c15d6671b6b4f..3bada052e3dcd53c23e5393165581f880b346e27:/gtk.c diff --git a/gtk.c b/gtk.c index fc99f92..960077a 100644 --- a/gtk.c +++ b/gtk.c @@ -25,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 @@ -123,7 +131,9 @@ struct frontend { int paste_data_len; 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; @@ -1244,6 +1254,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, @@ -1287,6 +1298,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; @@ -1328,10 +1341,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) @@ -1373,9 +1420,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); } } @@ -1895,6 +1954,7 @@ int main(int argc, char **argv) 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; @@ -1943,6 +2003,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", @@ -2073,7 +2150,7 @@ 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; @@ -2084,6 +2161,11 @@ int main(int argc, char **argv) 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); @@ -2140,7 +2222,32 @@ 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); + 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);