2 * gtk.c: GTK front end for my puzzle collection.
16 #include <gdk/gdkkeysyms.h>
20 #include <X11/Xutil.h>
21 #include <X11/Xatom.h>
25 #if GTK_CHECK_VERSION(2,0,0)
30 static FILE *debug_fp = NULL;
35 debug_fp = fopen("debug.log", "w");
46 void debug_printf(char *fmt, ...)
52 vsprintf(buf, fmt, ap);
58 /* ----------------------------------------------------------------------
59 * Error reporting functions used elsewhere.
62 void fatal(char *fmt, ...)
66 fprintf(stderr, "fatal error: ");
69 vfprintf(stderr, fmt, ap);
72 fprintf(stderr, "\n");
76 /* ----------------------------------------------------------------------
77 * GTK front end to puzzles.
82 PangoFontDescription *desc;
91 * This structure holds all the data relevant to a single window.
92 * In principle this would allow us to open multiple independent
93 * puzzle windows, although I can't currently see any real point in
94 * doing so. I'm just coding cleanly because there's no
95 * particularly good reason not to.
100 GtkWidget *statusbar;
109 int bbox_l, bbox_r, bbox_u, bbox_d;
110 int timer_active, timer_id;
111 struct timeval last_time;
113 int nfonts, fontsize;
115 int cfg_which, cfgret;
120 int pw, ph; /* pixmap size (w, h are area size) */
121 int ox, oy; /* offset of pixmap in drawing area */
125 void get_random_seed(void **randseed, int *randseedsize)
127 struct timeval *tvp = snew(struct timeval);
128 gettimeofday(tvp, NULL);
129 *randseed = (void *)tvp;
130 *randseedsize = sizeof(struct timeval);
133 void frontend_default_colour(frontend *fe, float *output)
135 GdkColor col = fe->window->style->bg[GTK_STATE_NORMAL];
136 output[0] = col.red / 65535.0;
137 output[1] = col.green / 65535.0;
138 output[2] = col.blue / 65535.0;
141 void gtk_status_bar(void *handle, char *text)
143 frontend *fe = (frontend *)handle;
146 assert(fe->statusbar);
148 rewritten = midend_rewrite_statusbar(fe->me, text);
149 if (!fe->laststatus || strcmp(rewritten, fe->laststatus)) {
150 gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx);
151 gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, rewritten);
152 sfree(fe->laststatus);
153 fe->laststatus = rewritten;
159 void gtk_start_draw(void *handle)
161 frontend *fe = (frontend *)handle;
162 fe->gc = gdk_gc_new(fe->area->window);
169 void gtk_clip(void *handle, int x, int y, int w, int h)
171 frontend *fe = (frontend *)handle;
179 gdk_gc_set_clip_rectangle(fe->gc, &rect);
182 void gtk_unclip(void *handle)
184 frontend *fe = (frontend *)handle;
192 gdk_gc_set_clip_rectangle(fe->gc, &rect);
195 void gtk_draw_text(void *handle, int x, int y, int fonttype, int fontsize,
196 int align, int colour, char *text)
198 frontend *fe = (frontend *)handle;
202 * Find or create the font.
204 for (i = 0; i < fe->nfonts; i++)
205 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
208 if (i == fe->nfonts) {
209 if (fe->fontsize <= fe->nfonts) {
210 fe->fontsize = fe->nfonts + 10;
211 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
216 fe->fonts[i].type = fonttype;
217 fe->fonts[i].size = fontsize;
221 * Use Pango to find the closest match to the requested
225 PangoFontDescription *fd;
227 fd = pango_font_description_new();
228 /* `Monospace' and `Sans' are meta-families guaranteed to exist */
229 pango_font_description_set_family(fd, fonttype == FONT_FIXED ?
230 "Monospace" : "Sans");
231 pango_font_description_set_weight(fd, PANGO_WEIGHT_BOLD);
233 * I found some online Pango documentation which
234 * described a function called
235 * pango_font_description_set_absolute_size(), which is
236 * _exactly_ what I want here. Unfortunately, none of
237 * my local Pango installations have it (presumably
238 * they're too old), so I'm going to have to hack round
239 * it by figuring out the point size myself. This
240 * limits me to X and probably also breaks in later
241 * Pango installations, so ideally I should add another
242 * CHECK_VERSION type ifdef and use set_absolute_size
243 * where available. All very annoying.
245 #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
246 pango_font_description_set_absolute_size(fd, PANGO_SCALE*fontsize);
249 Display *d = GDK_DISPLAY();
250 int s = DefaultScreen(d);
252 (PANGO_SCALE * 72.27 / 25.4) *
253 ((double) DisplayWidthMM(d, s) / DisplayWidth (d, s));
254 pango_font_description_set_size(fd, resolution * fontsize);
257 fe->fonts[i].desc = fd;
262 * In GTK 1.2, I don't know of any plausible way to
263 * pick a suitable font, so I'm just going to be
266 fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ?
267 "fixed" : "variable");
275 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
286 layout = pango_layout_new(gtk_widget_get_pango_context(fe->area));
287 pango_layout_set_font_description(layout, fe->fonts[i].desc);
288 pango_layout_set_text(layout, text, strlen(text));
289 pango_layout_get_pixel_extents(layout, NULL, &rect);
291 if (align & ALIGN_VCENTRE)
292 rect.y -= rect.height / 2;
294 rect.y -= rect.height;
296 if (align & ALIGN_HCENTRE)
297 rect.x -= rect.width / 2;
298 else if (align & ALIGN_HRIGHT)
299 rect.x -= rect.width;
301 gdk_draw_layout(fe->pixmap, fe->gc, rect.x + x, rect.y + y, layout);
303 g_object_unref(layout);
308 * Find string dimensions and process alignment.
311 int lb, rb, wid, asc, desc;
314 * Measure vertical string extents with respect to the same
317 gdk_string_extents(fe->fonts[i].font,
318 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
319 &lb, &rb, &wid, &asc, &desc);
320 if (align & ALIGN_VCENTRE)
321 y += asc - (asc+desc)/2;
326 * ... but horizontal extents with respect to the provided
327 * string. This means that multiple pieces of text centred
328 * on the same y-coordinate don't have different baselines.
330 gdk_string_extents(fe->fonts[i].font, text,
331 &lb, &rb, &wid, &asc, &desc);
333 if (align & ALIGN_HCENTRE)
335 else if (align & ALIGN_HRIGHT)
341 * Actually draw the text.
343 gdk_draw_string(fe->pixmap, fe->fonts[i].font, fe->gc, x, y, text);
348 void gtk_draw_rect(void *handle, int x, int y, int w, int h, int colour)
350 frontend *fe = (frontend *)handle;
351 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
352 gdk_draw_rectangle(fe->pixmap, fe->gc, 1, x, y, w, h);
355 void gtk_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
357 frontend *fe = (frontend *)handle;
358 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
359 gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2);
362 void gtk_draw_poly(void *handle, int *coords, int npoints,
363 int fillcolour, int outlinecolour)
365 frontend *fe = (frontend *)handle;
366 GdkPoint *points = snewn(npoints, GdkPoint);
369 for (i = 0; i < npoints; i++) {
370 points[i].x = coords[i*2];
371 points[i].y = coords[i*2+1];
374 if (fillcolour >= 0) {
375 gdk_gc_set_foreground(fe->gc, &fe->colours[fillcolour]);
376 gdk_draw_polygon(fe->pixmap, fe->gc, TRUE, points, npoints);
378 assert(outlinecolour >= 0);
379 gdk_gc_set_foreground(fe->gc, &fe->colours[outlinecolour]);
382 * In principle we ought to be able to use gdk_draw_polygon for
383 * the outline as well. In fact, it turns out to interact badly
384 * with a clipping region, for no terribly obvious reason, so I
385 * draw the outline as a sequence of lines instead.
387 for (i = 0; i < npoints; i++)
388 gdk_draw_line(fe->pixmap, fe->gc,
389 points[i].x, points[i].y,
390 points[(i+1)%npoints].x, points[(i+1)%npoints].y);
395 void gtk_draw_circle(void *handle, int cx, int cy, int radius,
396 int fillcolour, int outlinecolour)
398 frontend *fe = (frontend *)handle;
399 if (fillcolour >= 0) {
400 gdk_gc_set_foreground(fe->gc, &fe->colours[fillcolour]);
401 gdk_draw_arc(fe->pixmap, fe->gc, TRUE,
402 cx - radius, cy - radius,
403 2 * radius, 2 * radius, 0, 360 * 64);
406 assert(outlinecolour >= 0);
407 gdk_gc_set_foreground(fe->gc, &fe->colours[outlinecolour]);
408 gdk_draw_arc(fe->pixmap, fe->gc, FALSE,
409 cx - radius, cy - radius,
410 2 * radius, 2 * radius, 0, 360 * 64);
418 blitter *gtk_blitter_new(void *handle, int w, int h)
421 * We can't create the pixmap right now, because fe->window
422 * might not yet exist. So we just cache w and h and create it
423 * during the firs call to blitter_save.
425 blitter *bl = snew(blitter);
432 void gtk_blitter_free(void *handle, blitter *bl)
435 gdk_pixmap_unref(bl->pixmap);
439 void gtk_blitter_save(void *handle, blitter *bl, int x, int y)
441 frontend *fe = (frontend *)handle;
443 bl->pixmap = gdk_pixmap_new(fe->area->window, bl->w, bl->h, -1);
446 gdk_draw_pixmap(bl->pixmap,
447 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
449 x, y, 0, 0, bl->w, bl->h);
452 void gtk_blitter_load(void *handle, blitter *bl, int x, int y)
454 frontend *fe = (frontend *)handle;
456 if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) {
460 gdk_draw_pixmap(fe->pixmap,
461 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
463 0, 0, x, y, bl->w, bl->h);
466 void gtk_draw_update(void *handle, int x, int y, int w, int h)
468 frontend *fe = (frontend *)handle;
469 if (fe->bbox_l > x ) fe->bbox_l = x ;
470 if (fe->bbox_r < x+w) fe->bbox_r = x+w;
471 if (fe->bbox_u > y ) fe->bbox_u = y ;
472 if (fe->bbox_d < y+h) fe->bbox_d = y+h;
475 void gtk_end_draw(void *handle)
477 frontend *fe = (frontend *)handle;
478 gdk_gc_unref(fe->gc);
481 if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) {
482 gdk_draw_pixmap(fe->area->window,
483 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
485 fe->bbox_l, fe->bbox_u,
486 fe->ox + fe->bbox_l, fe->oy + fe->bbox_u,
487 fe->bbox_r - fe->bbox_l, fe->bbox_d - fe->bbox_u);
491 const struct drawing_api gtk_drawing = {
507 NULL, NULL, NULL, NULL, NULL, NULL, /* {begin,end}_{doc,page,puzzle} */
508 NULL, /* line_width */
511 static void destroy(GtkWidget *widget, gpointer data)
513 frontend *fe = (frontend *)data;
514 deactivate_timer(fe);
519 static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
521 frontend *fe = (frontend *)data;
523 int shift = (event->state & GDK_SHIFT_MASK) ? MOD_SHFT : 0;
524 int ctrl = (event->state & GDK_CONTROL_MASK) ? MOD_CTRL : 0;
529 if (event->keyval == GDK_Up)
530 keyval = shift | ctrl | CURSOR_UP;
531 else if (event->keyval == GDK_KP_Up || event->keyval == GDK_KP_8)
532 keyval = MOD_NUM_KEYPAD | '8';
533 else if (event->keyval == GDK_Down)
534 keyval = shift | ctrl | CURSOR_DOWN;
535 else if (event->keyval == GDK_KP_Down || event->keyval == GDK_KP_2)
536 keyval = MOD_NUM_KEYPAD | '2';
537 else if (event->keyval == GDK_Left)
538 keyval = shift | ctrl | CURSOR_LEFT;
539 else if (event->keyval == GDK_KP_Left || event->keyval == GDK_KP_4)
540 keyval = MOD_NUM_KEYPAD | '4';
541 else if (event->keyval == GDK_Right)
542 keyval = shift | ctrl | CURSOR_RIGHT;
543 else if (event->keyval == GDK_KP_Right || event->keyval == GDK_KP_6)
544 keyval = MOD_NUM_KEYPAD | '6';
545 else if (event->keyval == GDK_KP_Home || event->keyval == GDK_KP_7)
546 keyval = MOD_NUM_KEYPAD | '7';
547 else if (event->keyval == GDK_KP_End || event->keyval == GDK_KP_1)
548 keyval = MOD_NUM_KEYPAD | '1';
549 else if (event->keyval == GDK_KP_Page_Up || event->keyval == GDK_KP_9)
550 keyval = MOD_NUM_KEYPAD | '9';
551 else if (event->keyval == GDK_KP_Page_Down || event->keyval == GDK_KP_3)
552 keyval = MOD_NUM_KEYPAD | '3';
553 else if (event->keyval == GDK_KP_Insert || event->keyval == GDK_KP_0)
554 keyval = MOD_NUM_KEYPAD | '0';
555 else if (event->keyval == GDK_KP_Begin || event->keyval == GDK_KP_5)
556 keyval = MOD_NUM_KEYPAD | '5';
557 else if (event->keyval == GDK_BackSpace ||
558 event->keyval == GDK_Delete ||
559 event->keyval == GDK_KP_Delete)
561 else if (event->string[0] && !event->string[1])
562 keyval = (unsigned char)event->string[0];
567 !midend_process_key(fe->me, 0, 0, keyval))
568 gtk_widget_destroy(fe->window);
573 static gint button_event(GtkWidget *widget, GdkEventButton *event,
576 frontend *fe = (frontend *)data;
582 if (event->type != GDK_BUTTON_PRESS && event->type != GDK_BUTTON_RELEASE)
585 if (event->button == 2 || (event->state & GDK_SHIFT_MASK))
586 button = MIDDLE_BUTTON;
587 else if (event->button == 3 || (event->state & GDK_MOD1_MASK))
588 button = RIGHT_BUTTON;
589 else if (event->button == 1)
590 button = LEFT_BUTTON;
592 return FALSE; /* don't even know what button! */
594 if (event->type == GDK_BUTTON_RELEASE)
595 button += LEFT_RELEASE - LEFT_BUTTON;
597 if (!midend_process_key(fe->me, event->x - fe->ox,
598 event->y - fe->oy, button))
599 gtk_widget_destroy(fe->window);
604 static gint motion_event(GtkWidget *widget, GdkEventMotion *event,
607 frontend *fe = (frontend *)data;
613 if (event->state & (GDK_BUTTON2_MASK | GDK_SHIFT_MASK))
614 button = MIDDLE_DRAG;
615 else if (event->state & GDK_BUTTON1_MASK)
617 else if (event->state & GDK_BUTTON3_MASK)
620 return FALSE; /* don't even know what button! */
622 if (!midend_process_key(fe->me, event->x - fe->ox,
623 event->y - fe->oy, button))
624 gtk_widget_destroy(fe->window);
629 static gint expose_area(GtkWidget *widget, GdkEventExpose *event,
632 frontend *fe = (frontend *)data;
635 gdk_draw_pixmap(widget->window,
636 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
638 event->area.x - fe->ox, event->area.y - fe->oy,
639 event->area.x, event->area.y,
640 event->area.width, event->area.height);
645 static gint map_window(GtkWidget *widget, GdkEvent *event,
648 frontend *fe = (frontend *)data;
651 * Apparently we need to do this because otherwise the status
652 * bar will fail to update immediately. Annoying, but there we
655 gtk_widget_queue_draw(fe->window);
660 static gint configure_area(GtkWidget *widget,
661 GdkEventConfigure *event, gpointer data)
663 frontend *fe = (frontend *)data;
668 gdk_pixmap_unref(fe->pixmap);
670 x = fe->w = event->width;
671 y = fe->h = event->height;
672 midend_size(fe->me, &x, &y, TRUE);
675 fe->ox = (fe->w - fe->pw) / 2;
676 fe->oy = (fe->h - fe->ph) / 2;
678 fe->pixmap = gdk_pixmap_new(widget->window, fe->pw, fe->ph, -1);
680 gc = gdk_gc_new(fe->area->window);
681 gdk_gc_set_foreground(gc, &fe->colours[0]);
682 gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->pw, fe->ph);
685 midend_force_redraw(fe->me);
690 static gint timer_func(gpointer data)
692 frontend *fe = (frontend *)data;
694 if (fe->timer_active) {
697 gettimeofday(&now, NULL);
698 elapsed = ((now.tv_usec - fe->last_time.tv_usec) * 0.000001F +
699 (now.tv_sec - fe->last_time.tv_sec));
700 midend_timer(fe->me, elapsed); /* may clear timer_active */
704 return fe->timer_active;
707 void deactivate_timer(frontend *fe)
710 return; /* can happen due to --generate */
711 if (fe->timer_active)
712 gtk_timeout_remove(fe->timer_id);
713 fe->timer_active = FALSE;
716 void activate_timer(frontend *fe)
719 return; /* can happen due to --generate */
720 if (!fe->timer_active) {
721 fe->timer_id = gtk_timeout_add(20, timer_func, fe);
722 gettimeofday(&fe->last_time, NULL);
724 fe->timer_active = TRUE;
727 static void window_destroy(GtkWidget *widget, gpointer data)
732 static void msgbox_button_clicked(GtkButton *button, gpointer data)
734 GtkWidget *window = GTK_WIDGET(data);
737 ip = (int *)gtk_object_get_data(GTK_OBJECT(window), "user-data");
738 v = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "user-data"));
741 gtk_widget_destroy(GTK_WIDGET(data));
744 static int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
746 GtkObject *cancelbutton = GTK_OBJECT(data);
749 * `Escape' effectively clicks the cancel button
751 if (event->keyval == GDK_Escape) {
752 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton), "clicked");
759 enum { MB_OK, MB_YESNO };
761 int message_box(GtkWidget *parent, char *title, char *msg, int centre,
764 GtkWidget *window, *hbox, *text, *button;
768 window = gtk_dialog_new();
769 text = gtk_label_new(msg);
770 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
771 hbox = gtk_hbox_new(FALSE, 0);
772 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
773 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
774 hbox, FALSE, FALSE, 20);
775 gtk_widget_show(text);
776 gtk_widget_show(hbox);
777 gtk_window_set_title(GTK_WINDOW(window), title);
778 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
784 assert(type == MB_YESNO);
785 titles = "Yes\0No\0";
792 button = gtk_button_new_with_label(titles);
793 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area),
794 button, FALSE, FALSE, 0);
795 gtk_widget_show(button);
797 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
798 gtk_window_set_default(GTK_WINDOW(window), button);
801 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
802 GTK_SIGNAL_FUNC(win_key_press), button);
804 gtk_signal_connect(GTK_OBJECT(button), "clicked",
805 GTK_SIGNAL_FUNC(msgbox_button_clicked), window);
806 gtk_object_set_data(GTK_OBJECT(button), "user-data",
808 titles += strlen(titles)+1;
811 gtk_object_set_data(GTK_OBJECT(window), "user-data",
812 GINT_TO_POINTER(&i));
813 gtk_signal_connect(GTK_OBJECT(window), "destroy",
814 GTK_SIGNAL_FUNC(window_destroy), NULL);
815 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
816 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent));
817 /* set_transient_window_pos(parent, window); */
818 gtk_widget_show(window);
821 return (type == MB_YESNO ? i == 0 : TRUE);
824 void error_box(GtkWidget *parent, char *msg)
826 message_box(parent, "Error", msg, FALSE, MB_OK);
829 static void config_ok_button_clicked(GtkButton *button, gpointer data)
831 frontend *fe = (frontend *)data;
834 err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
837 error_box(fe->cfgbox, err);
840 gtk_widget_destroy(fe->cfgbox);
844 static void config_cancel_button_clicked(GtkButton *button, gpointer data)
846 frontend *fe = (frontend *)data;
848 gtk_widget_destroy(fe->cfgbox);
851 static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
854 * GtkEntry has a nasty habit of eating the Return key, which
855 * is unhelpful since it doesn't actually _do_ anything with it
856 * (it calls gtk_widget_activate, but our edit boxes never need
857 * activating). So I catch Return before GtkEntry sees it, and
858 * pass it straight on to the parent widget. Effect: hitting
859 * Return in an edit box will now activate the default button
860 * in the dialog just like it will everywhere else.
862 if (event->keyval == GDK_Return && widget->parent != NULL) {
864 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
865 gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event",
872 static void editbox_changed(GtkEditable *ed, gpointer data)
874 config_item *i = (config_item *)data;
877 i->sval = dupstr(gtk_entry_get_text(GTK_ENTRY(ed)));
880 static void button_toggled(GtkToggleButton *tb, gpointer data)
882 config_item *i = (config_item *)data;
884 i->ival = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb));
887 static void droplist_sel(GtkMenuItem *item, gpointer data)
889 config_item *i = (config_item *)data;
891 i->ival = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
895 static int get_config(frontend *fe, int which)
897 GtkWidget *w, *table, *cancel;
902 fe->cfg = midend_get_config(fe->me, which, &title);
903 fe->cfg_which = which;
906 fe->cfgbox = gtk_dialog_new();
907 gtk_window_set_title(GTK_WINDOW(fe->cfgbox), title);
910 w = gtk_button_new_with_label("OK");
911 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area),
914 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
915 gtk_window_set_default(GTK_WINDOW(fe->cfgbox), w);
916 gtk_signal_connect(GTK_OBJECT(w), "clicked",
917 GTK_SIGNAL_FUNC(config_ok_button_clicked), fe);
919 w = gtk_button_new_with_label("Cancel");
920 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area),
923 gtk_signal_connect(GTK_OBJECT(w), "clicked",
924 GTK_SIGNAL_FUNC(config_cancel_button_clicked), fe);
927 table = gtk_table_new(1, 2, FALSE);
929 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->vbox),
930 table, FALSE, FALSE, 0);
931 gtk_widget_show(table);
933 for (i = fe->cfg; i->type != C_END; i++) {
934 gtk_table_resize(GTK_TABLE(table), y+1, 2);
939 * Edit box with a label beside it.
942 w = gtk_label_new(i->name);
943 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5);
944 gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1,
945 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
946 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
951 gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1,
952 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
953 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
955 gtk_entry_set_text(GTK_ENTRY(w), i->sval);
956 gtk_signal_connect(GTK_OBJECT(w), "changed",
957 GTK_SIGNAL_FUNC(editbox_changed), i);
958 gtk_signal_connect(GTK_OBJECT(w), "key_press_event",
959 GTK_SIGNAL_FUNC(editbox_key), NULL);
968 w = gtk_check_button_new_with_label(i->name);
969 gtk_signal_connect(GTK_OBJECT(w), "toggled",
970 GTK_SIGNAL_FUNC(button_toggled), i);
971 gtk_table_attach(GTK_TABLE(table), w, 0, 2, y, y+1,
972 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
973 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
975 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), i->ival);
981 * Drop-down list (GtkOptionMenu).
984 w = gtk_label_new(i->name);
985 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5);
986 gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1,
987 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
988 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
992 w = gtk_option_menu_new();
993 gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1,
994 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
995 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
1002 GtkWidget *menuitem;
1003 GtkWidget *menu = gtk_menu_new();
1005 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), menu);
1013 while (*q && *q != c)
1016 name = snewn(q-p+1, char);
1017 strncpy(name, p, q-p);
1020 if (*q) q++; /* eat delimiter */
1022 menuitem = gtk_menu_item_new_with_label(name);
1023 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1024 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1025 GINT_TO_POINTER(val));
1026 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1027 GTK_SIGNAL_FUNC(droplist_sel), i);
1028 gtk_widget_show(menuitem);
1035 gtk_option_menu_set_history(GTK_OPTION_MENU(w), i->ival);
1044 gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "destroy",
1045 GTK_SIGNAL_FUNC(window_destroy), NULL);
1046 gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "key_press_event",
1047 GTK_SIGNAL_FUNC(win_key_press), cancel);
1048 gtk_window_set_modal(GTK_WINDOW(fe->cfgbox), TRUE);
1049 gtk_window_set_transient_for(GTK_WINDOW(fe->cfgbox),
1050 GTK_WINDOW(fe->window));
1051 /* set_transient_window_pos(fe->window, fe->cfgbox); */
1052 gtk_widget_show(fe->cfgbox);
1060 static void menu_key_event(GtkMenuItem *menuitem, gpointer data)
1062 frontend *fe = (frontend *)data;
1063 int key = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem),
1065 if (!midend_process_key(fe->me, 0, 0, key))
1066 gtk_widget_destroy(fe->window);
1069 static void get_size(frontend *fe, int *px, int *py)
1074 * Currently I don't want to make the GTK port scale large
1075 * puzzles to fit on the screen. This is because X does permit
1076 * extremely large windows and many window managers provide a
1077 * means of navigating round them, and the users I consulted
1078 * before deciding said that they'd rather have enormous puzzle
1079 * windows spanning multiple screen pages than have them
1080 * shrunk. I could change my mind later or introduce
1081 * configurability; this would be the place to do so, by
1082 * replacing the initial values of x and y with the screen
1087 midend_size(fe->me, &x, &y, FALSE);
1092 #if !GTK_CHECK_VERSION(2,0,0)
1093 #define gtk_window_resize(win, x, y) \
1094 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
1097 static void menu_preset_event(GtkMenuItem *menuitem, gpointer data)
1099 frontend *fe = (frontend *)data;
1100 game_params *params =
1101 (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data");
1104 midend_set_params(fe->me, params);
1105 midend_new_game(fe->me);
1106 get_size(fe, &x, &y);
1109 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1112 gtk_widget_size_request(GTK_WIDGET(fe->window), &req);
1113 gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height);
1117 GdkAtom compound_text_atom, utf8_string_atom;
1118 int paste_initialised = FALSE;
1122 unsigned char empty[] = { 0 };
1124 if (paste_initialised)
1127 if (!compound_text_atom)
1128 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1129 if (!utf8_string_atom)
1130 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
1133 * Ensure that all the cut buffers exist - according to the
1134 * ICCCM, we must do this before we start using cut buffers.
1136 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1137 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1138 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1139 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1140 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1141 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1142 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1143 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1144 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1145 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1146 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1147 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1148 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1149 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1150 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1151 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1154 /* Store data in a cut-buffer. */
1155 void store_cutbuffer(char *ptr, int len)
1157 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1158 XRotateBuffers(GDK_DISPLAY(), 1);
1159 XStoreBytes(GDK_DISPLAY(), ptr, len);
1162 void write_clip(frontend *fe, char *data)
1167 sfree(fe->paste_data);
1170 * For this simple application we can safely assume that the
1171 * data passed to this function is pure ASCII, which means we
1172 * can return precisely the same stuff for types STRING,
1173 * COMPOUND_TEXT or UTF8_STRING.
1176 fe->paste_data = data;
1177 fe->paste_data_len = strlen(data);
1179 store_cutbuffer(fe->paste_data, fe->paste_data_len);
1181 if (gtk_selection_owner_set(fe->area, GDK_SELECTION_PRIMARY,
1183 gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
1184 GDK_SELECTION_TYPE_STRING, 1);
1185 gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
1186 compound_text_atom, 1);
1187 gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
1188 utf8_string_atom, 1);
1192 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1193 guint info, guint time_stamp, gpointer data)
1195 frontend *fe = (frontend *)data;
1196 gtk_selection_data_set(seldata, seldata->target, 8,
1197 fe->paste_data, fe->paste_data_len);
1200 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1203 frontend *fe = (frontend *)data;
1206 sfree(fe->paste_data);
1207 fe->paste_data = NULL;
1208 fe->paste_data_len = 0;
1212 static void menu_copy_event(GtkMenuItem *menuitem, gpointer data)
1214 frontend *fe = (frontend *)data;
1217 text = midend_text_format(fe->me);
1220 write_clip(fe, text);
1226 static void filesel_ok(GtkButton *button, gpointer data)
1228 frontend *fe = (frontend *)data;
1230 gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1233 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
1235 fe->filesel_name = dupstr(name);
1238 static char *file_selector(frontend *fe, char *title, int save)
1240 GtkWidget *filesel =
1241 gtk_file_selection_new(title);
1243 fe->filesel_name = NULL;
1245 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1247 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1250 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1251 GTK_SIGNAL_FUNC(filesel_ok), fe);
1252 gtk_signal_connect_object
1253 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1254 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1255 gtk_signal_connect_object
1256 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1257 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1258 gtk_signal_connect(GTK_OBJECT(filesel), "destroy",
1259 GTK_SIGNAL_FUNC(window_destroy), NULL);
1260 gtk_widget_show(filesel);
1261 gtk_window_set_transient_for(GTK_WINDOW(filesel), GTK_WINDOW(fe->window));
1264 return fe->filesel_name;
1267 static void savefile_write(void *wctx, void *buf, int len)
1269 FILE *fp = (FILE *)wctx;
1270 fwrite(buf, 1, len, fp);
1273 static int savefile_read(void *wctx, void *buf, int len)
1275 FILE *fp = (FILE *)wctx;
1278 ret = fread(buf, 1, len, fp);
1279 return (ret == len);
1282 static void menu_save_event(GtkMenuItem *menuitem, gpointer data)
1284 frontend *fe = (frontend *)data;
1287 name = file_selector(fe, "Enter name of game file to save", TRUE);
1292 if ((fp = fopen(name, "r")) != NULL) {
1293 char buf[256 + FILENAME_MAX];
1297 sprintf(buf, "Are you sure you want to overwrite the"
1299 FILENAME_MAX, name);
1300 if (!message_box(fe->window, "Question", buf, TRUE, MB_YESNO))
1304 fp = fopen(name, "w");
1308 error_box(fe->window, "Unable to open save file");
1312 midend_serialise(fe->me, savefile_write, fp);
1318 static void menu_load_event(GtkMenuItem *menuitem, gpointer data)
1320 frontend *fe = (frontend *)data;
1324 name = file_selector(fe, "Enter name of saved game file to load", FALSE);
1327 FILE *fp = fopen(name, "r");
1331 error_box(fe->window, "Unable to open saved game file");
1335 err = midend_deserialise(fe->me, savefile_read, fp);
1340 error_box(fe->window, err);
1344 get_size(fe, &x, &y);
1347 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1350 gtk_widget_size_request(GTK_WIDGET(fe->window), &req);
1351 gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height);
1357 static void menu_solve_event(GtkMenuItem *menuitem, gpointer data)
1359 frontend *fe = (frontend *)data;
1362 msg = midend_solve(fe->me);
1365 error_box(fe->window, msg);
1368 static void menu_restart_event(GtkMenuItem *menuitem, gpointer data)
1370 frontend *fe = (frontend *)data;
1372 midend_restart_game(fe->me);
1375 static void menu_config_event(GtkMenuItem *menuitem, gpointer data)
1377 frontend *fe = (frontend *)data;
1378 int which = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem),
1382 if (!get_config(fe, which))
1385 midend_new_game(fe->me);
1386 get_size(fe, &x, &y);
1389 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1392 gtk_widget_size_request(GTK_WIDGET(fe->window), &req);
1393 gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height);
1397 static void menu_about_event(GtkMenuItem *menuitem, gpointer data)
1399 frontend *fe = (frontend *)data;
1403 sprintf(titlebuf, "About %.200s", thegame.name);
1406 "from Simon Tatham's Portable Puzzle Collection\n\n"
1407 "%.500s", thegame.name, ver);
1409 message_box(fe->window, titlebuf, textbuf, TRUE, MB_OK);
1412 static GtkWidget *add_menu_item_with_key(frontend *fe, GtkContainer *cont,
1413 char *text, int key)
1415 GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
1416 gtk_container_add(cont, menuitem);
1417 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1418 GINT_TO_POINTER(key));
1419 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1420 GTK_SIGNAL_FUNC(menu_key_event), fe);
1421 gtk_widget_show(menuitem);
1425 static void add_menu_separator(GtkContainer *cont)
1427 GtkWidget *menuitem = gtk_menu_item_new();
1428 gtk_container_add(cont, menuitem);
1429 gtk_widget_show(menuitem);
1432 static frontend *new_window(char *arg, char **error)
1436 GtkWidget *menubar, *menu, *menuitem;
1440 fe = snew(frontend);
1442 fe->timer_active = FALSE;
1445 fe->me = midend_new(fe, &thegame, >k_drawing, fe);
1453 * Try treating the argument as a game ID.
1455 err = midend_game_id(fe->me, arg);
1458 * It's a valid game ID.
1460 midend_new_game(fe->me);
1462 FILE *fp = fopen(arg, "r");
1464 sprintf(errbuf, "Supplied argument is neither a game ID (%.400s)"
1465 " nor a save file (%.400s)", err, strerror(errno));
1467 err = midend_deserialise(fe->me, savefile_read, fp);
1469 sprintf(errbuf, "%.800s", err);
1474 *error = dupstr(errbuf);
1475 midend_free(fe->me);
1481 midend_new_game(fe->me);
1484 fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1485 gtk_window_set_title(GTK_WINDOW(fe->window), thegame.name);
1487 gtk_window_set_resizable(GTK_WINDOW(fe->window), FALSE);
1489 gtk_window_set_policy(GTK_WINDOW(fe->window), FALSE, FALSE, TRUE);
1491 vbox = GTK_BOX(gtk_vbox_new(FALSE, 0));
1492 gtk_container_add(GTK_CONTAINER(fe->window), GTK_WIDGET(vbox));
1493 gtk_widget_show(GTK_WIDGET(vbox));
1495 menubar = gtk_menu_bar_new();
1496 gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0);
1497 gtk_widget_show(menubar);
1499 menuitem = gtk_menu_item_new_with_label("Game");
1500 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
1501 gtk_widget_show(menuitem);
1503 menu = gtk_menu_new();
1504 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
1506 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "New", 'n');
1508 menuitem = gtk_menu_item_new_with_label("Restart");
1509 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1510 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1511 GTK_SIGNAL_FUNC(menu_restart_event), fe);
1512 gtk_widget_show(menuitem);
1514 menuitem = gtk_menu_item_new_with_label("Specific...");
1515 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1516 GINT_TO_POINTER(CFG_DESC));
1517 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1518 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1519 GTK_SIGNAL_FUNC(menu_config_event), fe);
1520 gtk_widget_show(menuitem);
1522 menuitem = gtk_menu_item_new_with_label("Random Seed...");
1523 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1524 GINT_TO_POINTER(CFG_SEED));
1525 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1526 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1527 GTK_SIGNAL_FUNC(menu_config_event), fe);
1528 gtk_widget_show(menuitem);
1530 if ((n = midend_num_presets(fe->me)) > 0 || thegame.can_configure) {
1534 menuitem = gtk_menu_item_new_with_label("Type");
1535 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
1536 gtk_widget_show(menuitem);
1538 submenu = gtk_menu_new();
1539 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1541 for (i = 0; i < n; i++) {
1543 game_params *params;
1545 midend_fetch_preset(fe->me, i, &name, ¶ms);
1547 menuitem = gtk_menu_item_new_with_label(name);
1548 gtk_container_add(GTK_CONTAINER(submenu), menuitem);
1549 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", params);
1550 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1551 GTK_SIGNAL_FUNC(menu_preset_event), fe);
1552 gtk_widget_show(menuitem);
1555 if (thegame.can_configure) {
1556 menuitem = gtk_menu_item_new_with_label("Custom...");
1557 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1558 GPOINTER_TO_INT(CFG_SETTINGS));
1559 gtk_container_add(GTK_CONTAINER(submenu), menuitem);
1560 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1561 GTK_SIGNAL_FUNC(menu_config_event), fe);
1562 gtk_widget_show(menuitem);
1566 add_menu_separator(GTK_CONTAINER(menu));
1567 menuitem = gtk_menu_item_new_with_label("Load");
1568 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1569 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1570 GTK_SIGNAL_FUNC(menu_load_event), fe);
1571 gtk_widget_show(menuitem);
1572 menuitem = gtk_menu_item_new_with_label("Save");
1573 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1574 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1575 GTK_SIGNAL_FUNC(menu_save_event), fe);
1576 gtk_widget_show(menuitem);
1577 add_menu_separator(GTK_CONTAINER(menu));
1578 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Undo", 'u');
1579 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Redo", '\x12');
1580 if (thegame.can_format_as_text) {
1581 add_menu_separator(GTK_CONTAINER(menu));
1582 menuitem = gtk_menu_item_new_with_label("Copy");
1583 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1584 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1585 GTK_SIGNAL_FUNC(menu_copy_event), fe);
1586 gtk_widget_show(menuitem);
1588 if (thegame.can_solve) {
1589 add_menu_separator(GTK_CONTAINER(menu));
1590 menuitem = gtk_menu_item_new_with_label("Solve");
1591 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1592 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1593 GTK_SIGNAL_FUNC(menu_solve_event), fe);
1594 gtk_widget_show(menuitem);
1596 add_menu_separator(GTK_CONTAINER(menu));
1597 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Exit", 'q');
1599 menuitem = gtk_menu_item_new_with_label("Help");
1600 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
1601 gtk_widget_show(menuitem);
1603 menu = gtk_menu_new();
1604 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
1606 menuitem = gtk_menu_item_new_with_label("About");
1607 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1608 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1609 GTK_SIGNAL_FUNC(menu_about_event), fe);
1610 gtk_widget_show(menuitem);
1617 fe->colmap = gdk_colormap_get_system();
1618 colours = midend_colours(fe->me, &ncolours);
1619 fe->ncolours = ncolours;
1620 fe->colours = snewn(ncolours, GdkColor);
1621 for (i = 0; i < ncolours; i++) {
1622 fe->colours[i].red = colours[i*3] * 0xFFFF;
1623 fe->colours[i].green = colours[i*3+1] * 0xFFFF;
1624 fe->colours[i].blue = colours[i*3+2] * 0xFFFF;
1626 success = snewn(ncolours, gboolean);
1627 gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours,
1628 FALSE, FALSE, success);
1629 for (i = 0; i < ncolours; i++) {
1631 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
1632 i, fe->colours[i].red >> 8,
1633 fe->colours[i].green >> 8,
1634 fe->colours[i].blue >> 8);
1638 if (midend_wants_statusbar(fe->me)) {
1639 GtkWidget *viewport;
1642 viewport = gtk_viewport_new(NULL, NULL);
1643 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
1644 fe->statusbar = gtk_statusbar_new();
1645 gtk_container_add(GTK_CONTAINER(viewport), fe->statusbar);
1646 gtk_widget_show(viewport);
1647 gtk_box_pack_end(vbox, viewport, FALSE, FALSE, 0);
1648 gtk_widget_show(fe->statusbar);
1649 fe->statusctx = gtk_statusbar_get_context_id
1650 (GTK_STATUSBAR(fe->statusbar), "game");
1651 gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx,
1653 gtk_widget_size_request(fe->statusbar, &req);
1655 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
1657 gtk_widget_set_usize(viewport, -1, req.height);
1659 fe->statusbar = NULL;
1661 fe->area = gtk_drawing_area_new();
1662 get_size(fe, &x, &y);
1663 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1667 gtk_box_pack_end(vbox, fe->area, TRUE, TRUE, 0);
1671 fe->nfonts = fe->fontsize = 0;
1673 fe->laststatus = NULL;
1675 fe->paste_data = NULL;
1676 fe->paste_data_len = 0;
1678 gtk_signal_connect(GTK_OBJECT(fe->window), "destroy",
1679 GTK_SIGNAL_FUNC(destroy), fe);
1680 gtk_signal_connect(GTK_OBJECT(fe->window), "key_press_event",
1681 GTK_SIGNAL_FUNC(key_event), fe);
1682 gtk_signal_connect(GTK_OBJECT(fe->area), "button_press_event",
1683 GTK_SIGNAL_FUNC(button_event), fe);
1684 gtk_signal_connect(GTK_OBJECT(fe->area), "button_release_event",
1685 GTK_SIGNAL_FUNC(button_event), fe);
1686 gtk_signal_connect(GTK_OBJECT(fe->area), "motion_notify_event",
1687 GTK_SIGNAL_FUNC(motion_event), fe);
1688 gtk_signal_connect(GTK_OBJECT(fe->area), "selection_get",
1689 GTK_SIGNAL_FUNC(selection_get), fe);
1690 gtk_signal_connect(GTK_OBJECT(fe->area), "selection_clear_event",
1691 GTK_SIGNAL_FUNC(selection_clear), fe);
1692 gtk_signal_connect(GTK_OBJECT(fe->area), "expose_event",
1693 GTK_SIGNAL_FUNC(expose_area), fe);
1694 gtk_signal_connect(GTK_OBJECT(fe->window), "map_event",
1695 GTK_SIGNAL_FUNC(map_window), fe);
1696 gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event",
1697 GTK_SIGNAL_FUNC(configure_area), fe);
1699 gtk_widget_add_events(GTK_WIDGET(fe->area),
1700 GDK_BUTTON_PRESS_MASK |
1701 GDK_BUTTON_RELEASE_MASK |
1702 GDK_BUTTON_MOTION_MASK);
1704 gtk_widget_show(fe->area);
1705 gtk_widget_show(fe->window);
1707 gdk_window_set_background(fe->area->window, &fe->colours[0]);
1708 gdk_window_set_background(fe->window->window, &fe->colours[0]);
1713 char *fgetline(FILE *fp)
1715 char *ret = snewn(512, char);
1716 int size = 512, len = 0;
1717 while (fgets(ret + len, size - len, fp)) {
1718 len += strlen(ret + len);
1719 if (ret[len-1] == '\n')
1720 break; /* got a newline, we're done */
1722 ret = sresize(ret, size, char);
1724 if (len == 0) { /* first fgets returned NULL */
1732 int main(int argc, char **argv)
1734 char *pname = argv[0];
1736 int ngenerate = 0, print = FALSE, px = 1, py = 1;
1737 int soln = FALSE, colour = FALSE;
1740 int doing_opts = TRUE;
1746 * Command line parsing in this function is rather fiddly,
1747 * because GTK wants to have a go at argc/argv _first_ - and
1748 * yet we can't let it, because gtk_init() will bomb out if it
1749 * can't open an X display, whereas in fact we want to permit
1750 * our --generate and --print modes to run without an X
1754 * - we parse the command line ourselves, without modifying
1756 * - if we encounter an error which might plausibly be the
1757 * result of a GTK command line (i.e. not detailed errors in
1758 * particular options of ours) we store the error message
1759 * and terminate parsing.
1760 * - if we got enough out of the command line to know it
1761 * specifies a non-X mode of operation, we either display
1762 * the stored error and return failure, or if there is no
1763 * stored error we do the non-X operation and return
1765 * - otherwise, we go straight to gtk_init().
1771 if (doing_opts && !strcmp(p, "--version")) {
1772 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
1775 } else if (doing_opts && !strcmp(p, "--generate")) {
1777 ngenerate = atoi(*++av);
1779 fprintf(stderr, "%s: '--generate' expected a number\n",
1785 } else if (doing_opts && !strcmp(p, "--print")) {
1786 if (!thegame.can_print) {
1787 fprintf(stderr, "%s: this game does not support printing\n",
1794 if (sscanf(dim, "%dx%d", &px, &py) != 2) {
1795 fprintf(stderr, "%s: unable to parse argument '%s' to "
1796 "'--print'\n", pname, dim);
1802 } else if (doing_opts && !strcmp(p, "--scale")) {
1804 scale = atof(*++av);
1806 fprintf(stderr, "%s: no argument supplied to '--scale'\n",
1810 } else if (doing_opts && (!strcmp(p, "--with-solutions") ||
1811 !strcmp(p, "--with-solution") ||
1812 !strcmp(p, "--with-solns") ||
1813 !strcmp(p, "--with-soln") ||
1814 !strcmp(p, "--solutions") ||
1815 !strcmp(p, "--solution") ||
1816 !strcmp(p, "--solns") ||
1817 !strcmp(p, "--soln"))) {
1819 } else if (doing_opts && !strcmp(p, "--colour")) {
1820 if (!thegame.can_print_in_colour) {
1821 fprintf(stderr, "%s: this game does not support colour"
1822 " printing\n", pname);
1826 } else if (doing_opts && !strcmp(p, "--")) {
1828 } else if (!doing_opts || p[0] != '-') {
1830 fprintf(stderr, "%s: more than one argument supplied\n",
1836 sprintf(errbuf, "%.100s: unrecognised option '%.100s'\n",
1843 * Special standalone mode for generating puzzle IDs on the
1844 * command line. Useful for generating puzzles to be printed
1845 * out and solved offline (for puzzles where that even makes
1846 * sense - Solo, for example, is a lot more pencil-and-paper
1847 * friendly than Twiddle!)
1851 * <puzzle-name> --generate [<n> [<params>]]
1853 * <n>, if present, is the number of puzzle IDs to generate.
1854 * <params>, if present, is the same type of parameter string
1855 * you would pass to the puzzle when running it in GUI mode,
1856 * including optional extras such as the expansion factor in
1857 * Rectangles and the difficulty level in Solo.
1859 * If you specify <params>, you must also specify <n> (although
1860 * you may specify it to be 1). Sorry; that was the
1861 * simplest-to-parse command-line syntax I came up with.
1863 if (ngenerate > 0 || print) {
1867 document *doc = NULL;
1870 fputs(errbuf, stderr);
1876 me = midend_new(NULL, &thegame, NULL, NULL);
1880 doc = document_new(px, py, scale);
1883 * In this loop, we either generate a game ID or read one
1884 * from stdin depending on whether we're in generate mode;
1885 * then we either write it to stdout or print it, depending
1886 * on whether we're in print mode. Thus, this loop handles
1887 * generate-to-stdout, print-from-stdin and generate-and-
1888 * immediately-print modes.
1890 * (It could also handle a copy-stdin-to-stdout mode,
1891 * although there's currently no combination of options
1892 * which will cause this loop to be activated in that mode.
1893 * It wouldn't be _entirely_ pointless, though, because
1894 * stdin could contain bare params strings or random-seed
1895 * IDs, and stdout would contain nothing but fully
1896 * generated descriptive game IDs.)
1898 while (ngenerate == 0 || i < n) {
1901 if (ngenerate == 0) {
1902 pstr = fgetline(stdin);
1905 pstr[strcspn(pstr, "\r\n")] = '\0';
1908 pstr = snewn(strlen(arg) + 40, char);
1911 if (i > 0 && strchr(arg, '#'))
1912 sprintf(pstr + strlen(pstr), "-%d", i);
1918 err = midend_game_id(me, pstr);
1920 fprintf(stderr, "%s: error parsing '%s': %s\n",
1927 midend_new_game(me);
1930 err = midend_print_puzzle(me, doc, soln);
1932 fprintf(stderr, "%s: error in printing: %s\n", pname, err);
1936 id = midend_get_game_id(me);
1945 psdata *ps = ps_init(stdout, colour);
1946 document_print(doc, ps_drawing_api(ps));
1956 gtk_init(&argc, &argv);
1958 if (!new_window(argc > 1 ? argv[1] : NULL, &error)) {
1959 fprintf(stderr, "%s: %s\n", pname, error);