X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/7f89707cfefc99bd87511cb5d1f48a217c85e8fa..f467efadd6d03a2b7d3a7dfcd9ffa4a6eb144c5a:/gtk.c diff --git a/gtk.c b/gtk.c index 6de9fcf..2cbbde4 100644 --- a/gtk.c +++ b/gtk.c @@ -21,6 +21,10 @@ #include "puzzles.h" +#if GTK_CHECK_VERSION(2,0,0) +#define USE_PANGO +#endif + /* ---------------------------------------------------------------------- * Error reporting functions used elsewhere. */ @@ -44,7 +48,11 @@ void fatal(char *fmt, ...) */ struct font { +#ifdef USE_PANGO + PangoFontDescription *desc; +#else GdkFont *font; +#endif int type; int size; }; @@ -76,16 +84,19 @@ struct frontend { config_item *cfg; int cfg_which, cfgret; GtkWidget *cfgbox; - char *paste_data; + 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 */ }; void get_random_seed(void **randseed, int *randseedsize) { - time_t *tp = snew(time_t); - time(tp); - *randseed = (void *)tp; - *randseedsize = sizeof(time_t); + struct timeval *tvp = snew(struct timeval); + gettimeofday(tvp, NULL); + *randseed = (void *)tvp; + *randseedsize = sizeof(struct timeval); } void frontend_default_colour(frontend *fe, float *output) @@ -98,10 +109,19 @@ void frontend_default_colour(frontend *fe, float *output) void status_bar(frontend *fe, char *text) { + char *rewritten; + assert(fe->statusbar); - gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx); - gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, text); + 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); + } } void start_draw(frontend *fe) @@ -160,7 +180,7 @@ 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) +#ifdef USE_PANGO /* * Use Pango to find the closest match to the requested * font. @@ -198,25 +218,55 @@ void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize, pango_font_description_set_size(fd, resolution * fontsize); } #endif - fe->fonts[i].font = gdk_font_from_description(fd); - pango_font_description_free(fd); + fe->fonts[i].desc = fd; } - if (!fe->fonts[i].font) +#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"); #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"); + } /* + * Set the colour. + */ + gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); + +#ifdef USE_PANGO + + { + PangoLayout *layout; + PangoRectangle rect; + + /* + * 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); + + if (align & ALIGN_VCENTRE) + rect.y -= rect.height / 2; + + if (align & ALIGN_HCENTRE) + rect.x -= rect.width / 2; + else if (align & ALIGN_HRIGHT) + rect.x -= rect.width; + + gdk_draw_layout(fe->pixmap, fe->gc, rect.x + x, rect.y + y, layout); + + g_object_unref(layout); + } + +#else + /* * Find string dimensions and process alignment. */ { @@ -248,10 +298,11 @@ void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize, } /* - * Set colour and actually draw text. + * Actually draw the text. */ - gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); gdk_draw_string(fe->pixmap, fe->fonts[i].font, fe->gc, x, y, text); +#endif + } void draw_rect(frontend *fe, int x, int y, int w, int h, int colour) @@ -283,6 +334,66 @@ void draw_polygon(frontend *fe, int *coords, int npoints, sfree(points); } +void draw_circle(frontend *fe, int cx, int cy, int radius, + int fill, int colour) +{ + gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); + gdk_draw_arc(fe->pixmap, fe->gc, fill, + cx - radius, cy - radius, + 2 * radius, 2 * radius, 0, 360 * 64); +} + +struct blitter { + GdkPixmap *pixmap; + int w, h, x, y; +}; + +blitter *blitter_new(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; + bl->w = w; + bl->h = h; + return bl; +} + +void blitter_free(blitter *bl) +{ + if (bl->pixmap) + gdk_pixmap_unref(bl->pixmap); + sfree(bl); +} + +void 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); + 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 blitter_load(frontend *fe, blitter *bl, int x, int y) +{ + 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); +} + void draw_update(frontend *fe, int x, int y, int w, int h) { if (fe->bbox_l > x ) fe->bbox_l = x ; @@ -301,7 +412,7 @@ void end_draw(frontend *fe) fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)], fe->pixmap, fe->bbox_l, fe->bbox_u, - 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); } } @@ -317,24 +428,26 @@ static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data) { frontend *fe = (frontend *)data; int keyval; + int shift = (event->state & GDK_SHIFT_MASK) ? MOD_SHFT : 0; + int ctrl = (event->state & GDK_CONTROL_MASK) ? MOD_CTRL : 0; if (!fe->pixmap) return TRUE; if (event->keyval == GDK_Up) - keyval = CURSOR_UP; + keyval = shift | ctrl | CURSOR_UP; else if (event->keyval == GDK_KP_Up || event->keyval == GDK_KP_8) keyval = MOD_NUM_KEYPAD | '8'; else if (event->keyval == GDK_Down) - keyval = CURSOR_DOWN; + keyval = shift | ctrl | CURSOR_DOWN; else if (event->keyval == GDK_KP_Down || event->keyval == GDK_KP_2) keyval = MOD_NUM_KEYPAD | '2'; else if (event->keyval == GDK_Left) - keyval = CURSOR_LEFT; + keyval = shift | ctrl | CURSOR_LEFT; else if (event->keyval == GDK_KP_Left || event->keyval == GDK_KP_4) keyval = MOD_NUM_KEYPAD | '4'; else if (event->keyval == GDK_Right) - keyval = CURSOR_RIGHT; + keyval = shift | ctrl | CURSOR_RIGHT; else if (event->keyval == GDK_KP_Right || event->keyval == GDK_KP_6) keyval = MOD_NUM_KEYPAD | '6'; else if (event->keyval == GDK_KP_Home || event->keyval == GDK_KP_7) @@ -375,17 +488,18 @@ static gint button_event(GtkWidget *widget, GdkEventButton *event, if (event->button == 2 || (event->state & GDK_SHIFT_MASK)) button = MIDDLE_BUTTON; + else if (event->button == 3 || (event->state & GDK_MOD1_MASK)) + button = RIGHT_BUTTON; else if (event->button == 1) button = LEFT_BUTTON; - else if (event->button == 3) - button = RIGHT_BUTTON; 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)) + if (!midend_process_key(fe->me, event->x - fe->ox, + event->y - fe->oy, button)) gtk_widget_destroy(fe->window); return TRUE; @@ -409,7 +523,8 @@ static gint motion_event(GtkWidget *widget, GdkEventMotion *event, else return FALSE; /* don't even know what button! */ - if (!midend_process_key(fe->me, event->x, event->y, button)) + if (!midend_process_key(fe->me, event->x - fe->ox, + event->y - fe->oy, button)) gtk_widget_destroy(fe->window); return TRUE; @@ -424,7 +539,7 @@ static gint expose_area(GtkWidget *widget, GdkEventExpose *event, gdk_draw_pixmap(widget->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)], fe->pixmap, - event->area.x, event->area.y, + event->area.x - fe->ox, event->area.y - fe->oy, event->area.x, event->area.y, event->area.width, event->area.height); } @@ -451,18 +566,27 @@ static gint configure_area(GtkWidget *widget, { frontend *fe = (frontend *)data; GdkGC *gc; + int x, y; if (fe->pixmap) gdk_pixmap_unref(fe->pixmap); - fe->pixmap = gdk_pixmap_new(widget->window, fe->w, fe->h, -1); + x = fe->w = event->width; + y = fe->h = event->height; + midend_size(fe->me, &x, &y, TRUE); + fe->pw = x; + fe->ph = y; + 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->w, fe->h); + gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->pw, fe->ph); gdk_gc_unref(gc); - midend_redraw(fe->me); + midend_force_redraw(fe->me); return TRUE; } @@ -554,7 +678,7 @@ void message_box(GtkWidget *parent, char *title, char *msg, int centre) 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); + /* set_transient_window_pos(parent, window); */ gtk_widget_show(window); gtk_main(); } @@ -786,7 +910,7 @@ static int get_config(frontend *fe, int which) gtk_window_set_modal(GTK_WINDOW(fe->cfgbox), TRUE); gtk_window_set_transient_for(GTK_WINDOW(fe->cfgbox), GTK_WINDOW(fe->window)); - //set_transient_window_pos(fe->window, fe->cfgbox); + /* set_transient_window_pos(fe->window, fe->cfgbox); */ gtk_widget_show(fe->cfgbox); gtk_main(); @@ -804,6 +928,34 @@ static void menu_key_event(GtkMenuItem *menuitem, gpointer data) gtk_widget_destroy(fe->window); } +static void get_size(frontend *fe, int *px, int *py) +{ + int x, y; + + /* + * Currently I don't want to make the GTK port scale large + * puzzles to fit on the screen. This is because X does permit + * extremely large windows and many window managers provide a + * means of navigating round them, and the users I consulted + * before deciding said that they'd rather have enormous puzzle + * windows spanning multiple screen pages than have them + * shrunk. I could change my mind later or introduce + * configurability; this would be the place to do so, by + * replacing the initial values of x and y with the screen + * dimensions. + */ + x = INT_MAX; + y = INT_MAX; + midend_size(fe->me, &x, &y, FALSE); + *px = x; + *py = y; +} + +#if !GTK_CHECK_VERSION(2,0,0) +#define gtk_window_resize(win, x, y) \ + gdk_window_resize(GTK_WIDGET(win)->window, x, y) +#endif + static void menu_preset_event(GtkMenuItem *menuitem, gpointer data) { frontend *fe = (frontend *)data; @@ -813,10 +965,11 @@ static void menu_preset_event(GtkMenuItem *menuitem, gpointer data) midend_set_params(fe->me, params); midend_new_game(fe->me); - midend_size(fe->me, &x, &y); - gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); + get_size(fe, &x, &y); fe->w = x; fe->h = y; + gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); + gtk_window_resize(GTK_WINDOW(fe->window), 1, 1); } GdkAtom compound_text_atom, utf8_string_atom; @@ -824,6 +977,8 @@ int paste_initialised = FALSE; void init_paste() { + unsigned char empty[] = { 0 }; + if (paste_initialised) return; @@ -837,21 +992,21 @@ void init_paste() * ICCCM, we must do this before we start using cut buffers. */ XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, "", 0); + XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0); XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, "", 0); + XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0); XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, "", 0); + XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0); XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, "", 0); + XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0); XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, "", 0); + XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0); XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, "", 0); + XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0); XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, "", 0); + XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0); XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, "", 0); + XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0); } /* Store data in a cut-buffer. */ @@ -955,10 +1110,11 @@ static void menu_config_event(GtkMenuItem *menuitem, gpointer data) return; midend_new_game(fe->me); - midend_size(fe->me, &x, &y); - gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); + get_size(fe, &x, &y); fe->w = x; fe->h = y; + gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); + gtk_window_resize(GTK_WINDOW(fe->window), 1, 1); } static void menu_about_event(GtkMenuItem *menuitem, gpointer data) @@ -1005,7 +1161,11 @@ static frontend *new_window(char *game_id, char **error) fe = snew(frontend); + fe->timer_active = FALSE; + fe->timer_id = -1; + fe->me = midend_new(fe, &thegame); + if (game_id) { *error = midend_game_id(fe->me, game_id); if (*error) { @@ -1143,24 +1303,9 @@ static frontend *new_window(char *game_id, char **error) fe->ncolours = ncolours; fe->colours = snewn(ncolours, GdkColor); for (i = 0; i < ncolours; i++) { - /* - * Just for Gareth: if you dislike any of the standard - * colours, here's your chance to configure them in a - * really hacky way. - */ - char buf[80], *e; - unsigned int r, g, b; - sprintf(buf, "PUZZLE_COLOUR_%d", i); - if ((e = getenv(buf)) != NULL && - sscanf(e, "%2x%2x%2x", &r, &g, &b) == 3) { - fe->colours[i].red = r * 0x101; - fe->colours[i].green = g * 0x101; - fe->colours[i].blue = b * 0x101; - } else { - 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; - } + 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, @@ -1198,18 +1343,18 @@ static frontend *new_window(char *game_id, char **error) fe->statusbar = NULL; fe->area = gtk_drawing_area_new(); - midend_size(fe->me, &x, &y); + get_size(fe, &x, &y); gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); fe->w = x; fe->h = y; - gtk_box_pack_end(vbox, fe->area, FALSE, FALSE, 0); + gtk_box_pack_end(vbox, fe->area, TRUE, TRUE, 0); fe->pixmap = NULL; fe->fonts = NULL; fe->nfonts = fe->fontsize = 0; - fe->timer_active = FALSE; + fe->laststatus = NULL; fe->paste_data = NULL; fe->paste_data_len = 0; @@ -1243,6 +1388,9 @@ static frontend *new_window(char *game_id, char **error) 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]); + return fe; } @@ -1251,6 +1399,12 @@ int main(int argc, char **argv) char *pname = argv[0]; char *error; + if (argc > 1 && !strcmp(argv[1], "--version")) { + printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n", + thegame.name, ver); + return 0; + } + /* * Special standalone mode for generating puzzle IDs on the * command line. Useful for generating puzzles to be printed @@ -1289,7 +1443,10 @@ int main(int argc, char **argv) if ( (seed = strchr(params, '#')) != NULL ) *seed++ = '\0'; thegame.decode_params(par, params); - } else { + } + if ((error = thegame.validate_params(par)) != NULL) { + fprintf(stderr, "%s: %s\n", pname, error); + return 1; } parstr = thegame.encode_params(par, FALSE); @@ -1307,7 +1464,7 @@ int main(int argc, char **argv) while (n-- > 0) { game_aux_info *aux = NULL; - char *desc = thegame.new_desc(par, rs, &aux); + char *desc = thegame.new_desc(par, rs, &aux, FALSE); printf("%s:%s\n", parstr, desc); sfree(desc); if (aux)