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;
119 int pw, ph; /* pixmap size (w, h are area size) */
120 int ox, oy; /* offset of pixmap in drawing area */
124 void get_random_seed(void **randseed, int *randseedsize)
126 struct timeval *tvp = snew(struct timeval);
127 gettimeofday(tvp, NULL);
128 *randseed = (void *)tvp;
129 *randseedsize = sizeof(struct timeval);
132 void frontend_default_colour(frontend *fe, float *output)
134 GdkColor col = fe->window->style->bg[GTK_STATE_NORMAL];
135 output[0] = col.red / 65535.0;
136 output[1] = col.green / 65535.0;
137 output[2] = col.blue / 65535.0;
140 void gtk_status_bar(void *handle, char *text)
142 frontend *fe = (frontend *)handle;
144 assert(fe->statusbar);
146 gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx);
147 gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, text);
150 void gtk_start_draw(void *handle)
152 frontend *fe = (frontend *)handle;
153 fe->gc = gdk_gc_new(fe->area->window);
160 void gtk_clip(void *handle, int x, int y, int w, int h)
162 frontend *fe = (frontend *)handle;
170 gdk_gc_set_clip_rectangle(fe->gc, &rect);
173 void gtk_unclip(void *handle)
175 frontend *fe = (frontend *)handle;
183 gdk_gc_set_clip_rectangle(fe->gc, &rect);
186 void gtk_draw_text(void *handle, int x, int y, int fonttype, int fontsize,
187 int align, int colour, char *text)
189 frontend *fe = (frontend *)handle;
193 * Find or create the font.
195 for (i = 0; i < fe->nfonts; i++)
196 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
199 if (i == fe->nfonts) {
200 if (fe->fontsize <= fe->nfonts) {
201 fe->fontsize = fe->nfonts + 10;
202 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
207 fe->fonts[i].type = fonttype;
208 fe->fonts[i].size = fontsize;
212 * Use Pango to find the closest match to the requested
216 PangoFontDescription *fd;
218 fd = pango_font_description_new();
219 /* `Monospace' and `Sans' are meta-families guaranteed to exist */
220 pango_font_description_set_family(fd, fonttype == FONT_FIXED ?
221 "Monospace" : "Sans");
222 pango_font_description_set_weight(fd, PANGO_WEIGHT_BOLD);
224 * I found some online Pango documentation which
225 * described a function called
226 * pango_font_description_set_absolute_size(), which is
227 * _exactly_ what I want here. Unfortunately, none of
228 * my local Pango installations have it (presumably
229 * they're too old), so I'm going to have to hack round
230 * it by figuring out the point size myself. This
231 * limits me to X and probably also breaks in later
232 * Pango installations, so ideally I should add another
233 * CHECK_VERSION type ifdef and use set_absolute_size
234 * where available. All very annoying.
236 #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
237 pango_font_description_set_absolute_size(fd, PANGO_SCALE*fontsize);
240 Display *d = GDK_DISPLAY();
241 int s = DefaultScreen(d);
243 (PANGO_SCALE * 72.27 / 25.4) *
244 ((double) DisplayWidthMM(d, s) / DisplayWidth (d, s));
245 pango_font_description_set_size(fd, resolution * fontsize);
248 fe->fonts[i].desc = fd;
253 * In GTK 1.2, I don't know of any plausible way to
254 * pick a suitable font, so I'm just going to be
257 fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ?
258 "fixed" : "variable");
266 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
277 layout = pango_layout_new(gtk_widget_get_pango_context(fe->area));
278 pango_layout_set_font_description(layout, fe->fonts[i].desc);
279 pango_layout_set_text(layout, text, strlen(text));
280 pango_layout_get_pixel_extents(layout, NULL, &rect);
282 if (align & ALIGN_VCENTRE)
283 rect.y -= rect.height / 2;
285 rect.y -= rect.height;
287 if (align & ALIGN_HCENTRE)
288 rect.x -= rect.width / 2;
289 else if (align & ALIGN_HRIGHT)
290 rect.x -= rect.width;
292 gdk_draw_layout(fe->pixmap, fe->gc, rect.x + x, rect.y + y, layout);
294 g_object_unref(layout);
299 * Find string dimensions and process alignment.
302 int lb, rb, wid, asc, desc;
305 * Measure vertical string extents with respect to the same
308 gdk_string_extents(fe->fonts[i].font,
309 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
310 &lb, &rb, &wid, &asc, &desc);
311 if (align & ALIGN_VCENTRE)
312 y += asc - (asc+desc)/2;
317 * ... but horizontal extents with respect to the provided
318 * string. This means that multiple pieces of text centred
319 * on the same y-coordinate don't have different baselines.
321 gdk_string_extents(fe->fonts[i].font, text,
322 &lb, &rb, &wid, &asc, &desc);
324 if (align & ALIGN_HCENTRE)
326 else if (align & ALIGN_HRIGHT)
332 * Actually draw the text.
334 gdk_draw_string(fe->pixmap, fe->fonts[i].font, fe->gc, x, y, text);
339 void gtk_draw_rect(void *handle, int x, int y, int w, int h, int colour)
341 frontend *fe = (frontend *)handle;
342 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
343 gdk_draw_rectangle(fe->pixmap, fe->gc, 1, x, y, w, h);
346 void gtk_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
348 frontend *fe = (frontend *)handle;
349 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
350 gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2);
353 void gtk_draw_poly(void *handle, int *coords, int npoints,
354 int fillcolour, int outlinecolour)
356 frontend *fe = (frontend *)handle;
357 GdkPoint *points = snewn(npoints, GdkPoint);
360 for (i = 0; i < npoints; i++) {
361 points[i].x = coords[i*2];
362 points[i].y = coords[i*2+1];
365 if (fillcolour >= 0) {
366 gdk_gc_set_foreground(fe->gc, &fe->colours[fillcolour]);
367 gdk_draw_polygon(fe->pixmap, fe->gc, TRUE, points, npoints);
369 assert(outlinecolour >= 0);
370 gdk_gc_set_foreground(fe->gc, &fe->colours[outlinecolour]);
373 * In principle we ought to be able to use gdk_draw_polygon for
374 * the outline as well. In fact, it turns out to interact badly
375 * with a clipping region, for no terribly obvious reason, so I
376 * draw the outline as a sequence of lines instead.
378 for (i = 0; i < npoints; i++)
379 gdk_draw_line(fe->pixmap, fe->gc,
380 points[i].x, points[i].y,
381 points[(i+1)%npoints].x, points[(i+1)%npoints].y);
386 void gtk_draw_circle(void *handle, int cx, int cy, int radius,
387 int fillcolour, int outlinecolour)
389 frontend *fe = (frontend *)handle;
390 if (fillcolour >= 0) {
391 gdk_gc_set_foreground(fe->gc, &fe->colours[fillcolour]);
392 gdk_draw_arc(fe->pixmap, fe->gc, TRUE,
393 cx - radius, cy - radius,
394 2 * radius, 2 * radius, 0, 360 * 64);
397 assert(outlinecolour >= 0);
398 gdk_gc_set_foreground(fe->gc, &fe->colours[outlinecolour]);
399 gdk_draw_arc(fe->pixmap, fe->gc, FALSE,
400 cx - radius, cy - radius,
401 2 * radius, 2 * radius, 0, 360 * 64);
409 blitter *gtk_blitter_new(void *handle, int w, int h)
412 * We can't create the pixmap right now, because fe->window
413 * might not yet exist. So we just cache w and h and create it
414 * during the firs call to blitter_save.
416 blitter *bl = snew(blitter);
423 void gtk_blitter_free(void *handle, blitter *bl)
426 gdk_pixmap_unref(bl->pixmap);
430 void gtk_blitter_save(void *handle, blitter *bl, int x, int y)
432 frontend *fe = (frontend *)handle;
434 bl->pixmap = gdk_pixmap_new(fe->area->window, bl->w, bl->h, -1);
437 gdk_draw_pixmap(bl->pixmap,
438 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
440 x, y, 0, 0, bl->w, bl->h);
443 void gtk_blitter_load(void *handle, blitter *bl, int x, int y)
445 frontend *fe = (frontend *)handle;
447 if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) {
451 gdk_draw_pixmap(fe->pixmap,
452 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
454 0, 0, x, y, bl->w, bl->h);
457 void gtk_draw_update(void *handle, int x, int y, int w, int h)
459 frontend *fe = (frontend *)handle;
460 if (fe->bbox_l > x ) fe->bbox_l = x ;
461 if (fe->bbox_r < x+w) fe->bbox_r = x+w;
462 if (fe->bbox_u > y ) fe->bbox_u = y ;
463 if (fe->bbox_d < y+h) fe->bbox_d = y+h;
466 void gtk_end_draw(void *handle)
468 frontend *fe = (frontend *)handle;
469 gdk_gc_unref(fe->gc);
472 if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) {
473 gdk_draw_pixmap(fe->area->window,
474 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
476 fe->bbox_l, fe->bbox_u,
477 fe->ox + fe->bbox_l, fe->oy + fe->bbox_u,
478 fe->bbox_r - fe->bbox_l, fe->bbox_d - fe->bbox_u);
482 const struct drawing_api gtk_drawing = {
498 NULL, NULL, NULL, NULL, NULL, NULL, /* {begin,end}_{doc,page,puzzle} */
499 NULL, /* line_width */
502 static void destroy(GtkWidget *widget, gpointer data)
504 frontend *fe = (frontend *)data;
505 deactivate_timer(fe);
510 static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
512 frontend *fe = (frontend *)data;
514 int shift = (event->state & GDK_SHIFT_MASK) ? MOD_SHFT : 0;
515 int ctrl = (event->state & GDK_CONTROL_MASK) ? MOD_CTRL : 0;
520 if (event->keyval == GDK_Up)
521 keyval = shift | ctrl | CURSOR_UP;
522 else if (event->keyval == GDK_KP_Up || event->keyval == GDK_KP_8)
523 keyval = MOD_NUM_KEYPAD | '8';
524 else if (event->keyval == GDK_Down)
525 keyval = shift | ctrl | CURSOR_DOWN;
526 else if (event->keyval == GDK_KP_Down || event->keyval == GDK_KP_2)
527 keyval = MOD_NUM_KEYPAD | '2';
528 else if (event->keyval == GDK_Left)
529 keyval = shift | ctrl | CURSOR_LEFT;
530 else if (event->keyval == GDK_KP_Left || event->keyval == GDK_KP_4)
531 keyval = MOD_NUM_KEYPAD | '4';
532 else if (event->keyval == GDK_Right)
533 keyval = shift | ctrl | CURSOR_RIGHT;
534 else if (event->keyval == GDK_KP_Right || event->keyval == GDK_KP_6)
535 keyval = MOD_NUM_KEYPAD | '6';
536 else if (event->keyval == GDK_KP_Home || event->keyval == GDK_KP_7)
537 keyval = MOD_NUM_KEYPAD | '7';
538 else if (event->keyval == GDK_KP_End || event->keyval == GDK_KP_1)
539 keyval = MOD_NUM_KEYPAD | '1';
540 else if (event->keyval == GDK_KP_Page_Up || event->keyval == GDK_KP_9)
541 keyval = MOD_NUM_KEYPAD | '9';
542 else if (event->keyval == GDK_KP_Page_Down || event->keyval == GDK_KP_3)
543 keyval = MOD_NUM_KEYPAD | '3';
544 else if (event->keyval == GDK_KP_Insert || event->keyval == GDK_KP_0)
545 keyval = MOD_NUM_KEYPAD | '0';
546 else if (event->keyval == GDK_KP_Begin || event->keyval == GDK_KP_5)
547 keyval = MOD_NUM_KEYPAD | '5';
548 else if (event->keyval == GDK_BackSpace ||
549 event->keyval == GDK_Delete ||
550 event->keyval == GDK_KP_Delete)
552 else if (event->string[0] && !event->string[1])
553 keyval = (unsigned char)event->string[0];
558 !midend_process_key(fe->me, 0, 0, keyval))
559 gtk_widget_destroy(fe->window);
564 static gint button_event(GtkWidget *widget, GdkEventButton *event,
567 frontend *fe = (frontend *)data;
573 if (event->type != GDK_BUTTON_PRESS && event->type != GDK_BUTTON_RELEASE)
576 if (event->button == 2 || (event->state & GDK_SHIFT_MASK))
577 button = MIDDLE_BUTTON;
578 else if (event->button == 3 || (event->state & GDK_MOD1_MASK))
579 button = RIGHT_BUTTON;
580 else if (event->button == 1)
581 button = LEFT_BUTTON;
583 return FALSE; /* don't even know what button! */
585 if (event->type == GDK_BUTTON_RELEASE)
586 button += LEFT_RELEASE - LEFT_BUTTON;
588 if (!midend_process_key(fe->me, event->x - fe->ox,
589 event->y - fe->oy, button))
590 gtk_widget_destroy(fe->window);
595 static gint motion_event(GtkWidget *widget, GdkEventMotion *event,
598 frontend *fe = (frontend *)data;
604 if (event->state & (GDK_BUTTON2_MASK | GDK_SHIFT_MASK))
605 button = MIDDLE_DRAG;
606 else if (event->state & GDK_BUTTON1_MASK)
608 else if (event->state & GDK_BUTTON3_MASK)
611 return FALSE; /* don't even know what button! */
613 if (!midend_process_key(fe->me, event->x - fe->ox,
614 event->y - fe->oy, button))
615 gtk_widget_destroy(fe->window);
620 static gint expose_area(GtkWidget *widget, GdkEventExpose *event,
623 frontend *fe = (frontend *)data;
626 gdk_draw_pixmap(widget->window,
627 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
629 event->area.x - fe->ox, event->area.y - fe->oy,
630 event->area.x, event->area.y,
631 event->area.width, event->area.height);
636 static gint map_window(GtkWidget *widget, GdkEvent *event,
639 frontend *fe = (frontend *)data;
642 * Apparently we need to do this because otherwise the status
643 * bar will fail to update immediately. Annoying, but there we
646 gtk_widget_queue_draw(fe->window);
651 static gint configure_area(GtkWidget *widget,
652 GdkEventConfigure *event, gpointer data)
654 frontend *fe = (frontend *)data;
659 gdk_pixmap_unref(fe->pixmap);
661 x = fe->w = event->width;
662 y = fe->h = event->height;
663 midend_size(fe->me, &x, &y, TRUE);
666 fe->ox = (fe->w - fe->pw) / 2;
667 fe->oy = (fe->h - fe->ph) / 2;
669 fe->pixmap = gdk_pixmap_new(widget->window, fe->pw, fe->ph, -1);
671 gc = gdk_gc_new(fe->area->window);
672 gdk_gc_set_foreground(gc, &fe->colours[0]);
673 gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->pw, fe->ph);
676 midend_force_redraw(fe->me);
681 static gint timer_func(gpointer data)
683 frontend *fe = (frontend *)data;
685 if (fe->timer_active) {
688 gettimeofday(&now, NULL);
689 elapsed = ((now.tv_usec - fe->last_time.tv_usec) * 0.000001F +
690 (now.tv_sec - fe->last_time.tv_sec));
691 midend_timer(fe->me, elapsed); /* may clear timer_active */
695 return fe->timer_active;
698 void deactivate_timer(frontend *fe)
701 return; /* can happen due to --generate */
702 if (fe->timer_active)
703 gtk_timeout_remove(fe->timer_id);
704 fe->timer_active = FALSE;
707 void activate_timer(frontend *fe)
710 return; /* can happen due to --generate */
711 if (!fe->timer_active) {
712 fe->timer_id = gtk_timeout_add(20, timer_func, fe);
713 gettimeofday(&fe->last_time, NULL);
715 fe->timer_active = TRUE;
718 static void window_destroy(GtkWidget *widget, gpointer data)
723 static void msgbox_button_clicked(GtkButton *button, gpointer data)
725 GtkWidget *window = GTK_WIDGET(data);
728 ip = (int *)gtk_object_get_data(GTK_OBJECT(window), "user-data");
729 v = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "user-data"));
732 gtk_widget_destroy(GTK_WIDGET(data));
735 static int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
737 GtkObject *cancelbutton = GTK_OBJECT(data);
740 * `Escape' effectively clicks the cancel button
742 if (event->keyval == GDK_Escape) {
743 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton), "clicked");
750 enum { MB_OK, MB_YESNO };
752 int message_box(GtkWidget *parent, char *title, char *msg, int centre,
755 GtkWidget *window, *hbox, *text, *button;
759 window = gtk_dialog_new();
760 text = gtk_label_new(msg);
761 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
762 hbox = gtk_hbox_new(FALSE, 0);
763 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
764 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
765 hbox, FALSE, FALSE, 20);
766 gtk_widget_show(text);
767 gtk_widget_show(hbox);
768 gtk_window_set_title(GTK_WINDOW(window), title);
769 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
775 assert(type == MB_YESNO);
776 titles = "Yes\0No\0";
783 button = gtk_button_new_with_label(titles);
784 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area),
785 button, FALSE, FALSE, 0);
786 gtk_widget_show(button);
788 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
789 gtk_window_set_default(GTK_WINDOW(window), button);
792 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
793 GTK_SIGNAL_FUNC(win_key_press), button);
795 gtk_signal_connect(GTK_OBJECT(button), "clicked",
796 GTK_SIGNAL_FUNC(msgbox_button_clicked), window);
797 gtk_object_set_data(GTK_OBJECT(button), "user-data",
799 titles += strlen(titles)+1;
802 gtk_object_set_data(GTK_OBJECT(window), "user-data",
803 GINT_TO_POINTER(&i));
804 gtk_signal_connect(GTK_OBJECT(window), "destroy",
805 GTK_SIGNAL_FUNC(window_destroy), NULL);
806 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
807 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent));
808 /* set_transient_window_pos(parent, window); */
809 gtk_widget_show(window);
812 return (type == MB_YESNO ? i == 0 : TRUE);
815 void error_box(GtkWidget *parent, char *msg)
817 message_box(parent, "Error", msg, FALSE, MB_OK);
820 static void config_ok_button_clicked(GtkButton *button, gpointer data)
822 frontend *fe = (frontend *)data;
825 err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
828 error_box(fe->cfgbox, err);
831 gtk_widget_destroy(fe->cfgbox);
835 static void config_cancel_button_clicked(GtkButton *button, gpointer data)
837 frontend *fe = (frontend *)data;
839 gtk_widget_destroy(fe->cfgbox);
842 static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
845 * GtkEntry has a nasty habit of eating the Return key, which
846 * is unhelpful since it doesn't actually _do_ anything with it
847 * (it calls gtk_widget_activate, but our edit boxes never need
848 * activating). So I catch Return before GtkEntry sees it, and
849 * pass it straight on to the parent widget. Effect: hitting
850 * Return in an edit box will now activate the default button
851 * in the dialog just like it will everywhere else.
853 if (event->keyval == GDK_Return && widget->parent != NULL) {
855 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
856 gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event",
863 static void editbox_changed(GtkEditable *ed, gpointer data)
865 config_item *i = (config_item *)data;
868 i->sval = dupstr(gtk_entry_get_text(GTK_ENTRY(ed)));
871 static void button_toggled(GtkToggleButton *tb, gpointer data)
873 config_item *i = (config_item *)data;
875 i->ival = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb));
878 static void droplist_sel(GtkMenuItem *item, gpointer data)
880 config_item *i = (config_item *)data;
882 i->ival = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
886 static int get_config(frontend *fe, int which)
888 GtkWidget *w, *table, *cancel;
893 fe->cfg = midend_get_config(fe->me, which, &title);
894 fe->cfg_which = which;
897 fe->cfgbox = gtk_dialog_new();
898 gtk_window_set_title(GTK_WINDOW(fe->cfgbox), title);
901 w = gtk_button_new_with_label("OK");
902 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area),
905 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
906 gtk_window_set_default(GTK_WINDOW(fe->cfgbox), w);
907 gtk_signal_connect(GTK_OBJECT(w), "clicked",
908 GTK_SIGNAL_FUNC(config_ok_button_clicked), fe);
910 w = gtk_button_new_with_label("Cancel");
911 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area),
914 gtk_signal_connect(GTK_OBJECT(w), "clicked",
915 GTK_SIGNAL_FUNC(config_cancel_button_clicked), fe);
918 table = gtk_table_new(1, 2, FALSE);
920 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->vbox),
921 table, FALSE, FALSE, 0);
922 gtk_widget_show(table);
924 for (i = fe->cfg; i->type != C_END; i++) {
925 gtk_table_resize(GTK_TABLE(table), y+1, 2);
930 * Edit box with a label beside it.
933 w = gtk_label_new(i->name);
934 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5);
935 gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1,
936 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
937 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
942 gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1,
943 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
944 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
946 gtk_entry_set_text(GTK_ENTRY(w), i->sval);
947 gtk_signal_connect(GTK_OBJECT(w), "changed",
948 GTK_SIGNAL_FUNC(editbox_changed), i);
949 gtk_signal_connect(GTK_OBJECT(w), "key_press_event",
950 GTK_SIGNAL_FUNC(editbox_key), NULL);
959 w = gtk_check_button_new_with_label(i->name);
960 gtk_signal_connect(GTK_OBJECT(w), "toggled",
961 GTK_SIGNAL_FUNC(button_toggled), i);
962 gtk_table_attach(GTK_TABLE(table), w, 0, 2, y, y+1,
963 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
964 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
966 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), i->ival);
972 * Drop-down list (GtkOptionMenu).
975 w = gtk_label_new(i->name);
976 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5);
977 gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1,
978 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
979 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
983 w = gtk_option_menu_new();
984 gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1,
985 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
986 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
994 GtkWidget *menu = gtk_menu_new();
996 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), menu);
1004 while (*q && *q != c)
1007 name = snewn(q-p+1, char);
1008 strncpy(name, p, q-p);
1011 if (*q) q++; /* eat delimiter */
1013 menuitem = gtk_menu_item_new_with_label(name);
1014 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1015 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1016 GINT_TO_POINTER(val));
1017 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1018 GTK_SIGNAL_FUNC(droplist_sel), i);
1019 gtk_widget_show(menuitem);
1026 gtk_option_menu_set_history(GTK_OPTION_MENU(w), i->ival);
1035 gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "destroy",
1036 GTK_SIGNAL_FUNC(window_destroy), NULL);
1037 gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "key_press_event",
1038 GTK_SIGNAL_FUNC(win_key_press), cancel);
1039 gtk_window_set_modal(GTK_WINDOW(fe->cfgbox), TRUE);
1040 gtk_window_set_transient_for(GTK_WINDOW(fe->cfgbox),
1041 GTK_WINDOW(fe->window));
1042 /* set_transient_window_pos(fe->window, fe->cfgbox); */
1043 gtk_widget_show(fe->cfgbox);
1051 static void menu_key_event(GtkMenuItem *menuitem, gpointer data)
1053 frontend *fe = (frontend *)data;
1054 int key = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem),
1056 if (!midend_process_key(fe->me, 0, 0, key))
1057 gtk_widget_destroy(fe->window);
1060 static void get_size(frontend *fe, int *px, int *py)
1065 * Currently I don't want to make the GTK port scale large
1066 * puzzles to fit on the screen. This is because X does permit
1067 * extremely large windows and many window managers provide a
1068 * means of navigating round them, and the users I consulted
1069 * before deciding said that they'd rather have enormous puzzle
1070 * windows spanning multiple screen pages than have them
1071 * shrunk. I could change my mind later or introduce
1072 * configurability; this would be the place to do so, by
1073 * replacing the initial values of x and y with the screen
1078 midend_size(fe->me, &x, &y, FALSE);
1083 #if !GTK_CHECK_VERSION(2,0,0)
1084 #define gtk_window_resize(win, x, y) \
1085 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
1088 static void menu_preset_event(GtkMenuItem *menuitem, gpointer data)
1090 frontend *fe = (frontend *)data;
1091 game_params *params =
1092 (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data");
1095 midend_set_params(fe->me, params);
1096 midend_new_game(fe->me);
1097 get_size(fe, &x, &y);
1100 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1103 gtk_widget_size_request(GTK_WIDGET(fe->window), &req);
1104 gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height);
1108 GdkAtom compound_text_atom, utf8_string_atom;
1109 int paste_initialised = FALSE;
1113 unsigned char empty[] = { 0 };
1115 if (paste_initialised)
1118 if (!compound_text_atom)
1119 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1120 if (!utf8_string_atom)
1121 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
1124 * Ensure that all the cut buffers exist - according to the
1125 * ICCCM, we must do this before we start using cut buffers.
1127 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1128 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1129 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1130 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1131 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1132 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1133 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1134 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1135 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1136 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1137 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1138 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1139 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1140 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1141 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1142 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1145 /* Store data in a cut-buffer. */
1146 void store_cutbuffer(char *ptr, int len)
1148 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1149 XRotateBuffers(GDK_DISPLAY(), 1);
1150 XStoreBytes(GDK_DISPLAY(), ptr, len);
1153 void write_clip(frontend *fe, char *data)
1158 sfree(fe->paste_data);
1161 * For this simple application we can safely assume that the
1162 * data passed to this function is pure ASCII, which means we
1163 * can return precisely the same stuff for types STRING,
1164 * COMPOUND_TEXT or UTF8_STRING.
1167 fe->paste_data = data;
1168 fe->paste_data_len = strlen(data);
1170 store_cutbuffer(fe->paste_data, fe->paste_data_len);
1172 if (gtk_selection_owner_set(fe->area, GDK_SELECTION_PRIMARY,
1174 gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
1175 GDK_SELECTION_TYPE_STRING, 1);
1176 gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
1177 compound_text_atom, 1);
1178 gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
1179 utf8_string_atom, 1);
1183 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1184 guint info, guint time_stamp, gpointer data)
1186 frontend *fe = (frontend *)data;
1187 gtk_selection_data_set(seldata, seldata->target, 8,
1188 fe->paste_data, fe->paste_data_len);
1191 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1194 frontend *fe = (frontend *)data;
1197 sfree(fe->paste_data);
1198 fe->paste_data = NULL;
1199 fe->paste_data_len = 0;
1203 static void menu_copy_event(GtkMenuItem *menuitem, gpointer data)
1205 frontend *fe = (frontend *)data;
1208 text = midend_text_format(fe->me);
1211 write_clip(fe, text);
1217 static void filesel_ok(GtkButton *button, gpointer data)
1219 frontend *fe = (frontend *)data;
1221 gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1224 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
1226 fe->filesel_name = dupstr(name);
1229 static char *file_selector(frontend *fe, char *title, int save)
1231 GtkWidget *filesel =
1232 gtk_file_selection_new(title);
1234 fe->filesel_name = NULL;
1236 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1238 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1241 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1242 GTK_SIGNAL_FUNC(filesel_ok), fe);
1243 gtk_signal_connect_object
1244 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1245 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1246 gtk_signal_connect_object
1247 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1248 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1249 gtk_signal_connect(GTK_OBJECT(filesel), "destroy",
1250 GTK_SIGNAL_FUNC(window_destroy), NULL);
1251 gtk_widget_show(filesel);
1252 gtk_window_set_transient_for(GTK_WINDOW(filesel), GTK_WINDOW(fe->window));
1255 return fe->filesel_name;
1258 static void savefile_write(void *wctx, void *buf, int len)
1260 FILE *fp = (FILE *)wctx;
1261 fwrite(buf, 1, len, fp);
1264 static int savefile_read(void *wctx, void *buf, int len)
1266 FILE *fp = (FILE *)wctx;
1269 ret = fread(buf, 1, len, fp);
1270 return (ret == len);
1273 static void menu_save_event(GtkMenuItem *menuitem, gpointer data)
1275 frontend *fe = (frontend *)data;
1278 name = file_selector(fe, "Enter name of game file to save", TRUE);
1283 if ((fp = fopen(name, "r")) != NULL) {
1284 char buf[256 + FILENAME_MAX];
1288 sprintf(buf, "Are you sure you want to overwrite the"
1290 FILENAME_MAX, name);
1291 if (!message_box(fe->window, "Question", buf, TRUE, MB_YESNO))
1295 fp = fopen(name, "w");
1299 error_box(fe->window, "Unable to open save file");
1303 midend_serialise(fe->me, savefile_write, fp);
1309 static void menu_load_event(GtkMenuItem *menuitem, gpointer data)
1311 frontend *fe = (frontend *)data;
1315 name = file_selector(fe, "Enter name of saved game file to load", FALSE);
1318 FILE *fp = fopen(name, "r");
1322 error_box(fe->window, "Unable to open saved game file");
1326 err = midend_deserialise(fe->me, savefile_read, fp);
1331 error_box(fe->window, err);
1335 get_size(fe, &x, &y);
1338 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1341 gtk_widget_size_request(GTK_WIDGET(fe->window), &req);
1342 gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height);
1348 static void menu_solve_event(GtkMenuItem *menuitem, gpointer data)
1350 frontend *fe = (frontend *)data;
1353 msg = midend_solve(fe->me);
1356 error_box(fe->window, msg);
1359 static void menu_restart_event(GtkMenuItem *menuitem, gpointer data)
1361 frontend *fe = (frontend *)data;
1363 midend_restart_game(fe->me);
1366 static void menu_config_event(GtkMenuItem *menuitem, gpointer data)
1368 frontend *fe = (frontend *)data;
1369 int which = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem),
1373 if (!get_config(fe, which))
1376 midend_new_game(fe->me);
1377 get_size(fe, &x, &y);
1380 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1383 gtk_widget_size_request(GTK_WIDGET(fe->window), &req);
1384 gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height);
1388 static void menu_about_event(GtkMenuItem *menuitem, gpointer data)
1390 frontend *fe = (frontend *)data;
1394 sprintf(titlebuf, "About %.200s", thegame.name);
1397 "from Simon Tatham's Portable Puzzle Collection\n\n"
1398 "%.500s", thegame.name, ver);
1400 message_box(fe->window, titlebuf, textbuf, TRUE, MB_OK);
1403 static GtkWidget *add_menu_item_with_key(frontend *fe, GtkContainer *cont,
1404 char *text, int key)
1406 GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
1407 gtk_container_add(cont, menuitem);
1408 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1409 GINT_TO_POINTER(key));
1410 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1411 GTK_SIGNAL_FUNC(menu_key_event), fe);
1412 gtk_widget_show(menuitem);
1416 static void add_menu_separator(GtkContainer *cont)
1418 GtkWidget *menuitem = gtk_menu_item_new();
1419 gtk_container_add(cont, menuitem);
1420 gtk_widget_show(menuitem);
1423 static frontend *new_window(char *arg, char **error)
1427 GtkWidget *menubar, *menu, *menuitem;
1431 fe = snew(frontend);
1433 fe->timer_active = FALSE;
1436 fe->me = midend_new(fe, &thegame, >k_drawing, fe);
1444 * Try treating the argument as a game ID.
1446 err = midend_game_id(fe->me, arg);
1449 * It's a valid game ID.
1451 midend_new_game(fe->me);
1453 FILE *fp = fopen(arg, "r");
1455 sprintf(errbuf, "Supplied argument is neither a game ID (%.400s)"
1456 " nor a save file (%.400s)", err, strerror(errno));
1458 err = midend_deserialise(fe->me, savefile_read, fp);
1460 sprintf(errbuf, "%.800s", err);
1465 *error = dupstr(errbuf);
1466 midend_free(fe->me);
1472 midend_new_game(fe->me);
1475 fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1476 gtk_window_set_title(GTK_WINDOW(fe->window), thegame.name);
1478 gtk_window_set_resizable(GTK_WINDOW(fe->window), FALSE);
1480 gtk_window_set_policy(GTK_WINDOW(fe->window), FALSE, FALSE, TRUE);
1482 vbox = GTK_BOX(gtk_vbox_new(FALSE, 0));
1483 gtk_container_add(GTK_CONTAINER(fe->window), GTK_WIDGET(vbox));
1484 gtk_widget_show(GTK_WIDGET(vbox));
1486 menubar = gtk_menu_bar_new();
1487 gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0);
1488 gtk_widget_show(menubar);
1490 menuitem = gtk_menu_item_new_with_label("Game");
1491 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
1492 gtk_widget_show(menuitem);
1494 menu = gtk_menu_new();
1495 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
1497 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "New", 'n');
1499 menuitem = gtk_menu_item_new_with_label("Restart");
1500 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1501 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1502 GTK_SIGNAL_FUNC(menu_restart_event), fe);
1503 gtk_widget_show(menuitem);
1505 menuitem = gtk_menu_item_new_with_label("Specific...");
1506 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1507 GINT_TO_POINTER(CFG_DESC));
1508 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1509 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1510 GTK_SIGNAL_FUNC(menu_config_event), fe);
1511 gtk_widget_show(menuitem);
1513 menuitem = gtk_menu_item_new_with_label("Random Seed...");
1514 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1515 GINT_TO_POINTER(CFG_SEED));
1516 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1517 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1518 GTK_SIGNAL_FUNC(menu_config_event), fe);
1519 gtk_widget_show(menuitem);
1521 if ((n = midend_num_presets(fe->me)) > 0 || thegame.can_configure) {
1525 menuitem = gtk_menu_item_new_with_label("Type");
1526 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
1527 gtk_widget_show(menuitem);
1529 submenu = gtk_menu_new();
1530 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1532 for (i = 0; i < n; i++) {
1534 game_params *params;
1536 midend_fetch_preset(fe->me, i, &name, ¶ms);
1538 menuitem = gtk_menu_item_new_with_label(name);
1539 gtk_container_add(GTK_CONTAINER(submenu), menuitem);
1540 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", params);
1541 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1542 GTK_SIGNAL_FUNC(menu_preset_event), fe);
1543 gtk_widget_show(menuitem);
1546 if (thegame.can_configure) {
1547 menuitem = gtk_menu_item_new_with_label("Custom...");
1548 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1549 GPOINTER_TO_INT(CFG_SETTINGS));
1550 gtk_container_add(GTK_CONTAINER(submenu), menuitem);
1551 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1552 GTK_SIGNAL_FUNC(menu_config_event), fe);
1553 gtk_widget_show(menuitem);
1557 add_menu_separator(GTK_CONTAINER(menu));
1558 menuitem = gtk_menu_item_new_with_label("Load");
1559 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1560 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1561 GTK_SIGNAL_FUNC(menu_load_event), fe);
1562 gtk_widget_show(menuitem);
1563 menuitem = gtk_menu_item_new_with_label("Save");
1564 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1565 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1566 GTK_SIGNAL_FUNC(menu_save_event), fe);
1567 gtk_widget_show(menuitem);
1568 add_menu_separator(GTK_CONTAINER(menu));
1569 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Undo", 'u');
1570 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Redo", '\x12');
1571 if (thegame.can_format_as_text) {
1572 add_menu_separator(GTK_CONTAINER(menu));
1573 menuitem = gtk_menu_item_new_with_label("Copy");
1574 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1575 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1576 GTK_SIGNAL_FUNC(menu_copy_event), fe);
1577 gtk_widget_show(menuitem);
1579 if (thegame.can_solve) {
1580 add_menu_separator(GTK_CONTAINER(menu));
1581 menuitem = gtk_menu_item_new_with_label("Solve");
1582 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1583 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1584 GTK_SIGNAL_FUNC(menu_solve_event), fe);
1585 gtk_widget_show(menuitem);
1587 add_menu_separator(GTK_CONTAINER(menu));
1588 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Exit", 'q');
1590 menuitem = gtk_menu_item_new_with_label("Help");
1591 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
1592 gtk_widget_show(menuitem);
1594 menu = gtk_menu_new();
1595 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
1597 menuitem = gtk_menu_item_new_with_label("About");
1598 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1599 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1600 GTK_SIGNAL_FUNC(menu_about_event), fe);
1601 gtk_widget_show(menuitem);
1608 fe->colmap = gdk_colormap_get_system();
1609 colours = midend_colours(fe->me, &ncolours);
1610 fe->ncolours = ncolours;
1611 fe->colours = snewn(ncolours, GdkColor);
1612 for (i = 0; i < ncolours; i++) {
1613 fe->colours[i].red = colours[i*3] * 0xFFFF;
1614 fe->colours[i].green = colours[i*3+1] * 0xFFFF;
1615 fe->colours[i].blue = colours[i*3+2] * 0xFFFF;
1617 success = snewn(ncolours, gboolean);
1618 gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours,
1619 FALSE, FALSE, success);
1620 for (i = 0; i < ncolours; i++) {
1622 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
1623 i, fe->colours[i].red >> 8,
1624 fe->colours[i].green >> 8,
1625 fe->colours[i].blue >> 8);
1629 if (midend_wants_statusbar(fe->me)) {
1630 GtkWidget *viewport;
1633 viewport = gtk_viewport_new(NULL, NULL);
1634 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
1635 fe->statusbar = gtk_statusbar_new();
1636 gtk_container_add(GTK_CONTAINER(viewport), fe->statusbar);
1637 gtk_widget_show(viewport);
1638 gtk_box_pack_end(vbox, viewport, FALSE, FALSE, 0);
1639 gtk_widget_show(fe->statusbar);
1640 fe->statusctx = gtk_statusbar_get_context_id
1641 (GTK_STATUSBAR(fe->statusbar), "game");
1642 gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx,
1644 gtk_widget_size_request(fe->statusbar, &req);
1646 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
1648 gtk_widget_set_usize(viewport, -1, req.height);
1650 fe->statusbar = NULL;
1652 fe->area = gtk_drawing_area_new();
1653 get_size(fe, &x, &y);
1654 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1658 gtk_box_pack_end(vbox, fe->area, TRUE, TRUE, 0);
1662 fe->nfonts = fe->fontsize = 0;
1664 fe->paste_data = NULL;
1665 fe->paste_data_len = 0;
1667 gtk_signal_connect(GTK_OBJECT(fe->window), "destroy",
1668 GTK_SIGNAL_FUNC(destroy), fe);
1669 gtk_signal_connect(GTK_OBJECT(fe->window), "key_press_event",
1670 GTK_SIGNAL_FUNC(key_event), fe);
1671 gtk_signal_connect(GTK_OBJECT(fe->area), "button_press_event",
1672 GTK_SIGNAL_FUNC(button_event), fe);
1673 gtk_signal_connect(GTK_OBJECT(fe->area), "button_release_event",
1674 GTK_SIGNAL_FUNC(button_event), fe);
1675 gtk_signal_connect(GTK_OBJECT(fe->area), "motion_notify_event",
1676 GTK_SIGNAL_FUNC(motion_event), fe);
1677 gtk_signal_connect(GTK_OBJECT(fe->area), "selection_get",
1678 GTK_SIGNAL_FUNC(selection_get), fe);
1679 gtk_signal_connect(GTK_OBJECT(fe->area), "selection_clear_event",
1680 GTK_SIGNAL_FUNC(selection_clear), fe);
1681 gtk_signal_connect(GTK_OBJECT(fe->area), "expose_event",
1682 GTK_SIGNAL_FUNC(expose_area), fe);
1683 gtk_signal_connect(GTK_OBJECT(fe->window), "map_event",
1684 GTK_SIGNAL_FUNC(map_window), fe);
1685 gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event",
1686 GTK_SIGNAL_FUNC(configure_area), fe);
1688 gtk_widget_add_events(GTK_WIDGET(fe->area),
1689 GDK_BUTTON_PRESS_MASK |
1690 GDK_BUTTON_RELEASE_MASK |
1691 GDK_BUTTON_MOTION_MASK);
1693 gtk_widget_show(fe->area);
1694 gtk_widget_show(fe->window);
1696 gdk_window_set_background(fe->area->window, &fe->colours[0]);
1697 gdk_window_set_background(fe->window->window, &fe->colours[0]);
1702 char *fgetline(FILE *fp)
1704 char *ret = snewn(512, char);
1705 int size = 512, len = 0;
1706 while (fgets(ret + len, size - len, fp)) {
1707 len += strlen(ret + len);
1708 if (ret[len-1] == '\n')
1709 break; /* got a newline, we're done */
1711 ret = sresize(ret, size, char);
1713 if (len == 0) { /* first fgets returned NULL */
1721 int main(int argc, char **argv)
1723 char *pname = argv[0];
1725 int ngenerate = 0, print = FALSE, px = 1, py = 1;
1726 int soln = FALSE, colour = FALSE;
1729 int doing_opts = TRUE;
1735 * Command line parsing in this function is rather fiddly,
1736 * because GTK wants to have a go at argc/argv _first_ - and
1737 * yet we can't let it, because gtk_init() will bomb out if it
1738 * can't open an X display, whereas in fact we want to permit
1739 * our --generate and --print modes to run without an X
1743 * - we parse the command line ourselves, without modifying
1745 * - if we encounter an error which might plausibly be the
1746 * result of a GTK command line (i.e. not detailed errors in
1747 * particular options of ours) we store the error message
1748 * and terminate parsing.
1749 * - if we got enough out of the command line to know it
1750 * specifies a non-X mode of operation, we either display
1751 * the stored error and return failure, or if there is no
1752 * stored error we do the non-X operation and return
1754 * - otherwise, we go straight to gtk_init().
1760 if (doing_opts && !strcmp(p, "--version")) {
1761 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
1764 } else if (doing_opts && !strcmp(p, "--generate")) {
1766 ngenerate = atoi(*++av);
1768 fprintf(stderr, "%s: '--generate' expected a number\n",
1774 } else if (doing_opts && !strcmp(p, "--print")) {
1775 if (!thegame.can_print) {
1776 fprintf(stderr, "%s: this game does not support printing\n",
1783 if (sscanf(dim, "%dx%d", &px, &py) != 2) {
1784 fprintf(stderr, "%s: unable to parse argument '%s' to "
1785 "'--print'\n", pname, dim);
1791 } else if (doing_opts && !strcmp(p, "--scale")) {
1793 scale = atof(*++av);
1795 fprintf(stderr, "%s: no argument supplied to '--scale'\n",
1799 } else if (doing_opts && (!strcmp(p, "--with-solutions") ||
1800 !strcmp(p, "--with-solution") ||
1801 !strcmp(p, "--with-solns") ||
1802 !strcmp(p, "--with-soln") ||
1803 !strcmp(p, "--solutions") ||
1804 !strcmp(p, "--solution") ||
1805 !strcmp(p, "--solns") ||
1806 !strcmp(p, "--soln"))) {
1808 } else if (doing_opts && !strcmp(p, "--colour")) {
1809 if (!thegame.can_print_in_colour) {
1810 fprintf(stderr, "%s: this game does not support colour"
1811 " printing\n", pname);
1815 } else if (doing_opts && !strcmp(p, "--")) {
1817 } else if (!doing_opts || p[0] != '-') {
1819 fprintf(stderr, "%s: more than one argument supplied\n",
1825 sprintf(errbuf, "%.100s: unrecognised option '%.100s'\n",
1832 * Special standalone mode for generating puzzle IDs on the
1833 * command line. Useful for generating puzzles to be printed
1834 * out and solved offline (for puzzles where that even makes
1835 * sense - Solo, for example, is a lot more pencil-and-paper
1836 * friendly than Twiddle!)
1840 * <puzzle-name> --generate [<n> [<params>]]
1842 * <n>, if present, is the number of puzzle IDs to generate.
1843 * <params>, if present, is the same type of parameter string
1844 * you would pass to the puzzle when running it in GUI mode,
1845 * including optional extras such as the expansion factor in
1846 * Rectangles and the difficulty level in Solo.
1848 * If you specify <params>, you must also specify <n> (although
1849 * you may specify it to be 1). Sorry; that was the
1850 * simplest-to-parse command-line syntax I came up with.
1852 if (ngenerate > 0 || print) {
1856 document *doc = NULL;
1859 fputs(errbuf, stderr);
1865 me = midend_new(NULL, &thegame, NULL, NULL);
1869 doc = document_new(px, py, scale);
1872 * In this loop, we either generate a game ID or read one
1873 * from stdin depending on whether we're in generate mode;
1874 * then we either write it to stdout or print it, depending
1875 * on whether we're in print mode. Thus, this loop handles
1876 * generate-to-stdout, print-from-stdin and generate-and-
1877 * immediately-print modes.
1879 * (It could also handle a copy-stdin-to-stdout mode,
1880 * although there's currently no combination of options
1881 * which will cause this loop to be activated in that mode.
1882 * It wouldn't be _entirely_ pointless, though, because
1883 * stdin could contain bare params strings or random-seed
1884 * IDs, and stdout would contain nothing but fully
1885 * generated descriptive game IDs.)
1887 while (ngenerate == 0 || i < n) {
1890 if (ngenerate == 0) {
1891 pstr = fgetline(stdin);
1894 pstr[strcspn(pstr, "\r\n")] = '\0';
1897 pstr = snewn(strlen(arg) + 40, char);
1900 if (i > 0 && strchr(arg, '#'))
1901 sprintf(pstr + strlen(pstr), "-%d", i);
1907 err = midend_game_id(me, pstr);
1909 fprintf(stderr, "%s: error parsing '%s': %s\n",
1916 midend_new_game(me);
1919 err = midend_print_puzzle(me, doc, soln);
1921 fprintf(stderr, "%s: error in printing: %s\n", pname, err);
1925 id = midend_get_game_id(me);
1934 psdata *ps = ps_init(stdout, colour);
1935 document_print(doc, ps_drawing_api(ps));
1945 gtk_init(&argc, &argv);
1947 if (!new_window(argc > 1 ? argv[1] : NULL, &error)) {
1948 fprintf(stderr, "%s: %s\n", pname, error);