X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/077f3cbef8366f3cd57f07cb3eb5118457ea4f6d..30861651545725962a99ff36dbedddd33b8cd3f6:/gtk.c diff --git a/gtk.c b/gtk.c index 5e51028..b85cfa9 100644 --- a/gtk.c +++ b/gtk.c @@ -9,9 +9,16 @@ #include #include +#include + #include #include +#if GTK_CHECK_VERSION(2,0,0) && !defined HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION +#include +#include +#endif + #include "puzzles.h" /* ---------------------------------------------------------------------- @@ -63,13 +70,22 @@ struct frontend { GdkGC *gc; int bbox_l, bbox_r, bbox_u, bbox_d; int timer_active, timer_id; + struct timeval last_time; struct font *fonts; int nfonts, fontsize; config_item *cfg; - int cfgret; + int cfg_which, cfgret; GtkWidget *cfgbox; }; +void get_random_seed(void **randseed, int *randseedsize) +{ + time_t *tp = snew(time_t); + time(tp); + *randseed = (void *)tp; + *randseedsize = sizeof(time_t); +} + void frontend_default_colour(frontend *fe, float *output) { GdkColor col = fe->window->style->bg[GTK_STATE_NORMAL]; @@ -142,11 +158,60 @@ void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize, fe->fonts[i].type = fonttype; fe->fonts[i].size = fontsize; +#if GTK_CHECK_VERSION(2,0,0) /* - * FIXME: Really I should make at least _some_ effort to - * pick the correct font. + * Use Pango to find the closest match to the requested + * font. */ - fe->fonts[i].font = gdk_font_load("variable"); + { + 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].font = gdk_font_from_description(fd); + pango_font_description_free(fd); + } + + if (!fe->fonts[i].font) +#endif + /* + * 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. + * + * This is also fallback code called if the Pango + * approach fails to find an appropriate font. + */ + fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ? + "fixed" : "variable"); } /* @@ -155,11 +220,24 @@ void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize, { int lb, rb, wid, asc, desc; - gdk_string_extents(fe->fonts[i].font, text, + /* + * 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; + /* + * ... 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) @@ -228,6 +306,8 @@ void end_draw(frontend *fe) static void destroy(GtkWidget *widget, gpointer data) { + frontend *fe = (frontend *)data; + deactivate_timer(fe); gtk_main_quit(); } @@ -280,7 +360,7 @@ static gint button_event(GtkWidget *widget, GdkEventButton *event, if (!fe->pixmap) return TRUE; - if (event->type != GDK_BUTTON_PRESS) + if (event->type != GDK_BUTTON_PRESS && event->type != GDK_BUTTON_RELEASE) return TRUE; if (event->button == 2 || (event->state & GDK_SHIFT_MASK)) @@ -292,6 +372,33 @@ static gint button_event(GtkWidget *widget, GdkEventButton *event, else return FALSE; /* don't even know what button! */ + if (event->type == GDK_BUTTON_RELEASE) + button += LEFT_RELEASE - LEFT_BUTTON; + + if (!midend_process_key(fe->me, event->x, event->y, button)) + gtk_widget_destroy(fe->window); + + return TRUE; +} + +static gint motion_event(GtkWidget *widget, GdkEventMotion *event, + gpointer data) +{ + frontend *fe = (frontend *)data; + int button; + + if (!fe->pixmap) + return TRUE; + + if (event->state & (GDK_BUTTON2_MASK | GDK_SHIFT_MASK)) + button = MIDDLE_DRAG; + else if (event->state & GDK_BUTTON1_MASK) + button = LEFT_DRAG; + else if (event->state & GDK_BUTTON3_MASK) + button = RIGHT_DRAG; + else + return FALSE; /* don't even know what button! */ + if (!midend_process_key(fe->me, event->x, event->y, button)) gtk_widget_destroy(fe->window); @@ -354,8 +461,15 @@ static gint timer_func(gpointer data) { frontend *fe = (frontend *)data; - if (fe->timer_active) - midend_timer(fe->me, 0.02); /* may clear timer_active */ + if (fe->timer_active) { + struct timeval now; + float elapsed; + gettimeofday(&now, NULL); + elapsed = ((now.tv_usec - fe->last_time.tv_usec) * 0.000001F + + (now.tv_sec - fe->last_time.tv_sec)); + midend_timer(fe->me, elapsed); /* may clear timer_active */ + fe->last_time = now; + } return fe->timer_active; } @@ -369,8 +483,10 @@ void deactivate_timer(frontend *fe) void activate_timer(frontend *fe) { - if (!fe->timer_active) + if (!fe->timer_active) { fe->timer_id = gtk_timeout_add(20, timer_func, fe); + gettimeofday(&fe->last_time, NULL); + } fe->timer_active = TRUE; } @@ -384,6 +500,21 @@ static void errmsg_button_clicked(GtkButton *button, gpointer data) gtk_widget_destroy(GTK_WIDGET(data)); } +static int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) +{ + GtkObject *cancelbutton = GTK_OBJECT(data); + + /* + * `Escape' effectively clicks the cancel button + */ + if (event->keyval == GDK_Escape) { + gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton), "clicked"); + return TRUE; + } + + return FALSE; +} + void error_box(GtkWidget *parent, char *msg) { GtkWidget *window, *hbox, *text, *ok; @@ -409,6 +540,8 @@ void error_box(GtkWidget *parent, char *msg) GTK_SIGNAL_FUNC(errmsg_button_clicked), window); gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(window_destroy), NULL); + gtk_signal_connect(GTK_OBJECT(window), "key_press_event", + GTK_SIGNAL_FUNC(win_key_press), ok); gtk_window_set_modal(GTK_WINDOW(window), TRUE); gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent)); //set_transient_window_pos(parent, window); @@ -421,7 +554,7 @@ static void config_ok_button_clicked(GtkButton *button, gpointer data) frontend *fe = (frontend *)data; char *err; - err = midend_set_config(fe->me, fe->cfg); + err = midend_set_config(fe->me, fe->cfg_which, fe->cfg); if (err) error_box(fe->cfgbox, err); @@ -438,6 +571,27 @@ static void config_cancel_button_clicked(GtkButton *button, gpointer data) gtk_widget_destroy(fe->cfgbox); } +static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data) +{ + /* + * GtkEntry has a nasty habit of eating the Return key, which + * is unhelpful since it doesn't actually _do_ anything with it + * (it calls gtk_widget_activate, but our edit boxes never need + * activating). So I catch Return before GtkEntry sees it, and + * pass it straight on to the parent widget. Effect: hitting + * Return in an edit box will now activate the default button + * in the dialog just like it will everywhere else. + */ + if (event->keyval == GDK_Return && widget->parent != NULL) { + gint return_val; + gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event"); + gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event", + event, &return_val); + return return_val; + } + return FALSE; +} + static void editbox_changed(GtkEditable *ed, gpointer data) { config_item *i = (config_item *)data; @@ -461,17 +615,20 @@ static void droplist_sel(GtkMenuItem *item, gpointer data) "user-data")); } -static int get_config(frontend *fe) +static int get_config(frontend *fe, int which) { - GtkWidget *w, *table; + GtkWidget *w, *table, *cancel; + char *title; config_item *i; int y; - fe->cfg = midend_get_config(fe->me); + fe->cfg = midend_get_config(fe->me, which, &title); + fe->cfg_which = which; fe->cfgret = FALSE; fe->cfgbox = gtk_dialog_new(); - gtk_window_set_title(GTK_WINDOW(fe->cfgbox), "Configure"); + gtk_window_set_title(GTK_WINDOW(fe->cfgbox), title); + sfree(title); w = gtk_button_new_with_label("OK"); gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area), @@ -488,6 +645,7 @@ static int get_config(frontend *fe) gtk_widget_show(w); gtk_signal_connect(GTK_OBJECT(w), "clicked", GTK_SIGNAL_FUNC(config_cancel_button_clicked), fe); + cancel = w; table = gtk_table_new(1, 2, FALSE); y = 0; @@ -520,6 +678,8 @@ static int get_config(frontend *fe) gtk_entry_set_text(GTK_ENTRY(w), i->sval); gtk_signal_connect(GTK_OBJECT(w), "changed", GTK_SIGNAL_FUNC(editbox_changed), i); + gtk_signal_connect(GTK_OBJECT(w), "key_press_event", + GTK_SIGNAL_FUNC(editbox_key), NULL); gtk_widget_show(w); break; @@ -606,6 +766,8 @@ static int get_config(frontend *fe) gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "destroy", GTK_SIGNAL_FUNC(window_destroy), NULL); + gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "key_press_event", + GTK_SIGNAL_FUNC(win_key_press), cancel); gtk_window_set_modal(GTK_WINDOW(fe->cfgbox), TRUE); gtk_window_set_transient_for(GTK_WINDOW(fe->cfgbox), GTK_WINDOW(fe->window)); @@ -635,7 +797,7 @@ static void menu_preset_event(GtkMenuItem *menuitem, gpointer data) int x, y; midend_set_params(fe->me, params); - midend_new_game(fe->me, NULL); + midend_new_game(fe->me); midend_size(fe->me, &x, &y); gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); fe->w = x; @@ -645,12 +807,14 @@ static void menu_preset_event(GtkMenuItem *menuitem, gpointer data) 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)) + if (!get_config(fe, which)) return; - midend_new_game(fe->me, NULL); + midend_new_game(fe->me); midend_size(fe->me, &x, &y); gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); fe->w = x; @@ -677,7 +841,7 @@ static void add_menu_separator(GtkContainer *cont) gtk_widget_show(menuitem); } -static frontend *new_window(void) +static frontend *new_window(char *game_id, char **error) { frontend *fe; GtkBox *vbox; @@ -686,11 +850,19 @@ static frontend *new_window(void) fe = snew(frontend); - fe->me = midend_new(fe); - midend_new_game(fe->me, NULL); + fe->me = midend_new(fe, &thegame); + if (game_id) { + *error = midend_game_id(fe->me, game_id, FALSE); + if (*error) { + midend_free(fe->me); + sfree(fe); + return NULL; + } + } + midend_new_game(fe->me); fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); - gtk_window_set_title(GTK_WINDOW(fe->window), game_name); + gtk_window_set_title(GTK_WINDOW(fe->window), thegame.name); #if 0 gtk_window_set_resizable(GTK_WINDOW(fe->window), FALSE); #else @@ -714,7 +886,15 @@ static frontend *new_window(void) add_menu_item_with_key(fe, GTK_CONTAINER(menu), "New", 'n'); add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Restart", 'r'); - if ((n = midend_num_presets(fe->me)) > 0 || game_can_configure) { + menuitem = gtk_menu_item_new_with_label("Specific..."); + gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", + GINT_TO_POINTER(CFG_SEED)); + gtk_container_add(GTK_CONTAINER(menu), menuitem); + gtk_signal_connect(GTK_OBJECT(menuitem), "activate", + GTK_SIGNAL_FUNC(menu_config_event), fe); + gtk_widget_show(menuitem); + + if ((n = midend_num_presets(fe->me)) > 0 || thegame.can_configure) { GtkWidget *submenu; int i; @@ -739,8 +919,10 @@ static frontend *new_window(void) gtk_widget_show(menuitem); } - if (game_can_configure) { + if (thegame.can_configure) { menuitem = gtk_menu_item_new_with_label("Custom..."); + 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); @@ -799,7 +981,7 @@ static frontend *new_window(void) #if 0 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */ #endif - gtk_widget_set_usize(viewport, x, req.height); + gtk_widget_set_usize(viewport, -1, req.height); } else fe->statusbar = NULL; @@ -823,6 +1005,10 @@ static frontend *new_window(void) GTK_SIGNAL_FUNC(key_event), fe); gtk_signal_connect(GTK_OBJECT(fe->area), "button_press_event", GTK_SIGNAL_FUNC(button_event), fe); + gtk_signal_connect(GTK_OBJECT(fe->area), "button_release_event", + GTK_SIGNAL_FUNC(button_event), fe); + gtk_signal_connect(GTK_OBJECT(fe->area), "motion_notify_event", + GTK_SIGNAL_FUNC(motion_event), fe); gtk_signal_connect(GTK_OBJECT(fe->area), "expose_event", GTK_SIGNAL_FUNC(expose_area), fe); gtk_signal_connect(GTK_OBJECT(fe->window), "map_event", @@ -830,7 +1016,10 @@ static frontend *new_window(void) gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event", GTK_SIGNAL_FUNC(configure_area), fe); - gtk_widget_add_events(GTK_WIDGET(fe->area), GDK_BUTTON_PRESS_MASK); + gtk_widget_add_events(GTK_WIDGET(fe->area), + GDK_BUTTON_PRESS_MASK | + GDK_BUTTON_RELEASE_MASK | + GDK_BUTTON_MOTION_MASK); gtk_widget_show(fe->area); gtk_widget_show(fe->window); @@ -840,11 +1029,73 @@ static frontend *new_window(void) int main(int argc, char **argv) { - srand(time(NULL)); + char *pname = argv[0]; + char *error; - gtk_init(&argc, &argv); - (void) new_window(); - gtk_main(); + /* + * Special standalone mode for generating puzzle IDs on the + * 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!) + * + * Usage: + * + * --generate [ []] + * + * , if present, is the number of puzzle IDs to generate. + * , if present, is the same type of parameter string + * you would pass to the puzzle when running it in GUI mode, + * including optional extras such as the expansion factor in + * Rectangles and the difficulty level in Solo. + * + * If you specify , you must also specify (although + * 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; + game_params *par; + random_state *rs; + char *parstr; + + { + void *seed; + int seedlen; + get_random_seed(&seed, &seedlen); + rs = random_init(seed, seedlen); + } + + if (argc > 2) + n = atoi(argv[2]); + if (argc > 3) + params = argv[3]; + + if (params) + par = thegame.decode_params(params); + else + par = thegame.default_params(); + parstr = thegame.encode_params(par); + + while (n-- > 0) { + char *seed = thegame.new_seed(par, rs); + printf("%s:%s\n", parstr, seed); + sfree(seed); + } + + return 0; + } else { + + gtk_init(&argc, &argv); + + if (!new_window(argc > 1 ? argv[1] : NULL, &error)) { + fprintf(stderr, "%s: %s\n", pname, error); + return 1; + } + + gtk_main(); + } return 0; }