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.
99 GtkAccelGroup *accelgroup;
101 GtkWidget *statusbar;
110 int bbox_l, bbox_r, bbox_u, bbox_d;
111 int timer_active, timer_id;
112 struct timeval last_time;
114 int nfonts, fontsize;
116 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;
145 assert(fe->statusbar);
147 gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx);
148 gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, text);
151 void gtk_start_draw(void *handle)
153 frontend *fe = (frontend *)handle;
154 fe->gc = gdk_gc_new(fe->area->window);
161 void gtk_clip(void *handle, int x, int y, int w, int h)
163 frontend *fe = (frontend *)handle;
171 gdk_gc_set_clip_rectangle(fe->gc, &rect);
174 void gtk_unclip(void *handle)
176 frontend *fe = (frontend *)handle;
184 gdk_gc_set_clip_rectangle(fe->gc, &rect);
187 void gtk_draw_text(void *handle, int x, int y, int fonttype, int fontsize,
188 int align, int colour, char *text)
190 frontend *fe = (frontend *)handle;
194 * Find or create the font.
196 for (i = 0; i < fe->nfonts; i++)
197 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
200 if (i == fe->nfonts) {
201 if (fe->fontsize <= fe->nfonts) {
202 fe->fontsize = fe->nfonts + 10;
203 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
208 fe->fonts[i].type = fonttype;
209 fe->fonts[i].size = fontsize;
213 * Use Pango to find the closest match to the requested
217 PangoFontDescription *fd;
219 fd = pango_font_description_new();
220 /* `Monospace' and `Sans' are meta-families guaranteed to exist */
221 pango_font_description_set_family(fd, fonttype == FONT_FIXED ?
222 "Monospace" : "Sans");
223 pango_font_description_set_weight(fd, PANGO_WEIGHT_BOLD);
225 * I found some online Pango documentation which
226 * described a function called
227 * pango_font_description_set_absolute_size(), which is
228 * _exactly_ what I want here. Unfortunately, none of
229 * my local Pango installations have it (presumably
230 * they're too old), so I'm going to have to hack round
231 * it by figuring out the point size myself. This
232 * limits me to X and probably also breaks in later
233 * Pango installations, so ideally I should add another
234 * CHECK_VERSION type ifdef and use set_absolute_size
235 * where available. All very annoying.
237 #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
238 pango_font_description_set_absolute_size(fd, PANGO_SCALE*fontsize);
241 Display *d = GDK_DISPLAY();
242 int s = DefaultScreen(d);
244 (PANGO_SCALE * 72.27 / 25.4) *
245 ((double) DisplayWidthMM(d, s) / DisplayWidth (d, s));
246 pango_font_description_set_size(fd, resolution * fontsize);
249 fe->fonts[i].desc = fd;
254 * In GTK 1.2, I don't know of any plausible way to
255 * pick a suitable font, so I'm just going to be
258 fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ?
259 "fixed" : "variable");
267 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
278 layout = pango_layout_new(gtk_widget_get_pango_context(fe->area));
279 pango_layout_set_font_description(layout, fe->fonts[i].desc);
280 pango_layout_set_text(layout, text, strlen(text));
281 pango_layout_get_pixel_extents(layout, NULL, &rect);
283 if (align & ALIGN_VCENTRE)
284 rect.y -= rect.height / 2;
286 rect.y -= rect.height;
288 if (align & ALIGN_HCENTRE)
289 rect.x -= rect.width / 2;
290 else if (align & ALIGN_HRIGHT)
291 rect.x -= rect.width;
293 gdk_draw_layout(fe->pixmap, fe->gc, rect.x + x, rect.y + y, layout);
295 g_object_unref(layout);
300 * Find string dimensions and process alignment.
303 int lb, rb, wid, asc, desc;
306 * Measure vertical string extents with respect to the same
309 gdk_string_extents(fe->fonts[i].font,
310 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
311 &lb, &rb, &wid, &asc, &desc);
312 if (align & ALIGN_VCENTRE)
313 y += asc - (asc+desc)/2;
318 * ... but horizontal extents with respect to the provided
319 * string. This means that multiple pieces of text centred
320 * on the same y-coordinate don't have different baselines.
322 gdk_string_extents(fe->fonts[i].font, text,
323 &lb, &rb, &wid, &asc, &desc);
325 if (align & ALIGN_HCENTRE)
327 else if (align & ALIGN_HRIGHT)
333 * Actually draw the text.
335 gdk_draw_string(fe->pixmap, fe->fonts[i].font, fe->gc, x, y, text);
340 void gtk_draw_rect(void *handle, int x, int y, int w, int h, int colour)
342 frontend *fe = (frontend *)handle;
343 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
344 gdk_draw_rectangle(fe->pixmap, fe->gc, 1, x, y, w, h);
347 void gtk_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
349 frontend *fe = (frontend *)handle;
350 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
351 gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2);
354 void gtk_draw_poly(void *handle, int *coords, int npoints,
355 int fillcolour, int outlinecolour)
357 frontend *fe = (frontend *)handle;
358 GdkPoint *points = snewn(npoints, GdkPoint);
361 for (i = 0; i < npoints; i++) {
362 points[i].x = coords[i*2];
363 points[i].y = coords[i*2+1];
366 if (fillcolour >= 0) {
367 gdk_gc_set_foreground(fe->gc, &fe->colours[fillcolour]);
368 gdk_draw_polygon(fe->pixmap, fe->gc, TRUE, points, npoints);
370 assert(outlinecolour >= 0);
371 gdk_gc_set_foreground(fe->gc, &fe->colours[outlinecolour]);
374 * In principle we ought to be able to use gdk_draw_polygon for
375 * the outline as well. In fact, it turns out to interact badly
376 * with a clipping region, for no terribly obvious reason, so I
377 * draw the outline as a sequence of lines instead.
379 for (i = 0; i < npoints; i++)
380 gdk_draw_line(fe->pixmap, fe->gc,
381 points[i].x, points[i].y,
382 points[(i+1)%npoints].x, points[(i+1)%npoints].y);
387 void gtk_draw_circle(void *handle, int cx, int cy, int radius,
388 int fillcolour, int outlinecolour)
390 frontend *fe = (frontend *)handle;
391 if (fillcolour >= 0) {
392 gdk_gc_set_foreground(fe->gc, &fe->colours[fillcolour]);
393 gdk_draw_arc(fe->pixmap, fe->gc, TRUE,
394 cx - radius, cy - radius,
395 2 * radius, 2 * radius, 0, 360 * 64);
398 assert(outlinecolour >= 0);
399 gdk_gc_set_foreground(fe->gc, &fe->colours[outlinecolour]);
400 gdk_draw_arc(fe->pixmap, fe->gc, FALSE,
401 cx - radius, cy - radius,
402 2 * radius, 2 * radius, 0, 360 * 64);
410 blitter *gtk_blitter_new(void *handle, int w, int h)
413 * We can't create the pixmap right now, because fe->window
414 * might not yet exist. So we just cache w and h and create it
415 * during the firs call to blitter_save.
417 blitter *bl = snew(blitter);
424 void gtk_blitter_free(void *handle, blitter *bl)
427 gdk_pixmap_unref(bl->pixmap);
431 void gtk_blitter_save(void *handle, blitter *bl, int x, int y)
433 frontend *fe = (frontend *)handle;
435 bl->pixmap = gdk_pixmap_new(fe->area->window, bl->w, bl->h, -1);
438 gdk_draw_pixmap(bl->pixmap,
439 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
441 x, y, 0, 0, bl->w, bl->h);
444 void gtk_blitter_load(void *handle, blitter *bl, int x, int y)
446 frontend *fe = (frontend *)handle;
448 if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) {
452 gdk_draw_pixmap(fe->pixmap,
453 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
455 0, 0, x, y, bl->w, bl->h);
458 void gtk_draw_update(void *handle, int x, int y, int w, int h)
460 frontend *fe = (frontend *)handle;
461 if (fe->bbox_l > x ) fe->bbox_l = x ;
462 if (fe->bbox_r < x+w) fe->bbox_r = x+w;
463 if (fe->bbox_u > y ) fe->bbox_u = y ;
464 if (fe->bbox_d < y+h) fe->bbox_d = y+h;
467 void gtk_end_draw(void *handle)
469 frontend *fe = (frontend *)handle;
470 gdk_gc_unref(fe->gc);
473 if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) {
474 gdk_draw_pixmap(fe->area->window,
475 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
477 fe->bbox_l, fe->bbox_u,
478 fe->ox + fe->bbox_l, fe->oy + fe->bbox_u,
479 fe->bbox_r - fe->bbox_l, fe->bbox_d - fe->bbox_u);
483 const struct drawing_api gtk_drawing = {
499 NULL, NULL, NULL, NULL, NULL, NULL, /* {begin,end}_{doc,page,puzzle} */
500 NULL, /* line_width */
503 static void destroy(GtkWidget *widget, gpointer data)
505 frontend *fe = (frontend *)data;
506 deactivate_timer(fe);
511 static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
513 frontend *fe = (frontend *)data;
515 int shift = (event->state & GDK_SHIFT_MASK) ? MOD_SHFT : 0;
516 int ctrl = (event->state & GDK_CONTROL_MASK) ? MOD_CTRL : 0;
521 if (event->keyval == GDK_Up)
522 keyval = shift | ctrl | CURSOR_UP;
523 else if (event->keyval == GDK_KP_Up || event->keyval == GDK_KP_8)
524 keyval = MOD_NUM_KEYPAD | '8';
525 else if (event->keyval == GDK_Down)
526 keyval = shift | ctrl | CURSOR_DOWN;
527 else if (event->keyval == GDK_KP_Down || event->keyval == GDK_KP_2)
528 keyval = MOD_NUM_KEYPAD | '2';
529 else if (event->keyval == GDK_Left)
530 keyval = shift | ctrl | CURSOR_LEFT;
531 else if (event->keyval == GDK_KP_Left || event->keyval == GDK_KP_4)
532 keyval = MOD_NUM_KEYPAD | '4';
533 else if (event->keyval == GDK_Right)
534 keyval = shift | ctrl | CURSOR_RIGHT;
535 else if (event->keyval == GDK_KP_Right || event->keyval == GDK_KP_6)
536 keyval = MOD_NUM_KEYPAD | '6';
537 else if (event->keyval == GDK_KP_Home || event->keyval == GDK_KP_7)
538 keyval = MOD_NUM_KEYPAD | '7';
539 else if (event->keyval == GDK_KP_End || event->keyval == GDK_KP_1)
540 keyval = MOD_NUM_KEYPAD | '1';
541 else if (event->keyval == GDK_KP_Page_Up || event->keyval == GDK_KP_9)
542 keyval = MOD_NUM_KEYPAD | '9';
543 else if (event->keyval == GDK_KP_Page_Down || event->keyval == GDK_KP_3)
544 keyval = MOD_NUM_KEYPAD | '3';
545 else if (event->keyval == GDK_KP_Insert || event->keyval == GDK_KP_0)
546 keyval = MOD_NUM_KEYPAD | '0';
547 else if (event->keyval == GDK_KP_Begin || event->keyval == GDK_KP_5)
548 keyval = MOD_NUM_KEYPAD | '5';
549 else if (event->keyval == GDK_BackSpace ||
550 event->keyval == GDK_Delete ||
551 event->keyval == GDK_KP_Delete)
553 else if (event->string[0] && !event->string[1])
554 keyval = (unsigned char)event->string[0];
559 !midend_process_key(fe->me, 0, 0, keyval))
560 gtk_widget_destroy(fe->window);
565 static gint button_event(GtkWidget *widget, GdkEventButton *event,
568 frontend *fe = (frontend *)data;
574 if (event->type != GDK_BUTTON_PRESS && event->type != GDK_BUTTON_RELEASE)
577 if (event->button == 2 || (event->state & GDK_SHIFT_MASK))
578 button = MIDDLE_BUTTON;
579 else if (event->button == 3 || (event->state & GDK_MOD1_MASK))
580 button = RIGHT_BUTTON;
581 else if (event->button == 1)
582 button = LEFT_BUTTON;
584 return FALSE; /* don't even know what button! */
586 if (event->type == GDK_BUTTON_RELEASE)
587 button += LEFT_RELEASE - LEFT_BUTTON;
589 if (!midend_process_key(fe->me, event->x - fe->ox,
590 event->y - fe->oy, button))
591 gtk_widget_destroy(fe->window);
596 static gint motion_event(GtkWidget *widget, GdkEventMotion *event,
599 frontend *fe = (frontend *)data;
605 if (event->state & (GDK_BUTTON2_MASK | GDK_SHIFT_MASK))
606 button = MIDDLE_DRAG;
607 else if (event->state & GDK_BUTTON1_MASK)
609 else if (event->state & GDK_BUTTON3_MASK)
612 return FALSE; /* don't even know what button! */
614 if (!midend_process_key(fe->me, event->x - fe->ox,
615 event->y - fe->oy, button))
616 gtk_widget_destroy(fe->window);
621 static gint expose_area(GtkWidget *widget, GdkEventExpose *event,
624 frontend *fe = (frontend *)data;
627 gdk_draw_pixmap(widget->window,
628 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
630 event->area.x - fe->ox, event->area.y - fe->oy,
631 event->area.x, event->area.y,
632 event->area.width, event->area.height);
637 static gint map_window(GtkWidget *widget, GdkEvent *event,
640 frontend *fe = (frontend *)data;
643 * Apparently we need to do this because otherwise the status
644 * bar will fail to update immediately. Annoying, but there we
647 gtk_widget_queue_draw(fe->window);
652 static gint configure_area(GtkWidget *widget,
653 GdkEventConfigure *event, gpointer data)
655 frontend *fe = (frontend *)data;
660 gdk_pixmap_unref(fe->pixmap);
662 x = fe->w = event->width;
663 y = fe->h = event->height;
664 midend_size(fe->me, &x, &y, TRUE);
667 fe->ox = (fe->w - fe->pw) / 2;
668 fe->oy = (fe->h - fe->ph) / 2;
670 fe->pixmap = gdk_pixmap_new(widget->window, fe->pw, fe->ph, -1);
672 gc = gdk_gc_new(fe->area->window);
673 gdk_gc_set_foreground(gc, &fe->colours[0]);
674 gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->pw, fe->ph);
677 midend_force_redraw(fe->me);
682 static gint timer_func(gpointer data)
684 frontend *fe = (frontend *)data;
686 if (fe->timer_active) {
689 gettimeofday(&now, NULL);
690 elapsed = ((now.tv_usec - fe->last_time.tv_usec) * 0.000001F +
691 (now.tv_sec - fe->last_time.tv_sec));
692 midend_timer(fe->me, elapsed); /* may clear timer_active */
696 return fe->timer_active;
699 void deactivate_timer(frontend *fe)
702 return; /* can happen due to --generate */
703 if (fe->timer_active)
704 gtk_timeout_remove(fe->timer_id);
705 fe->timer_active = FALSE;
708 void activate_timer(frontend *fe)
711 return; /* can happen due to --generate */
712 if (!fe->timer_active) {
713 fe->timer_id = gtk_timeout_add(20, timer_func, fe);
714 gettimeofday(&fe->last_time, NULL);
716 fe->timer_active = TRUE;
719 static void window_destroy(GtkWidget *widget, gpointer data)
724 static void msgbox_button_clicked(GtkButton *button, gpointer data)
726 GtkWidget *window = GTK_WIDGET(data);
729 ip = (int *)gtk_object_get_data(GTK_OBJECT(window), "user-data");
730 v = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "user-data"));
733 gtk_widget_destroy(GTK_WIDGET(data));
736 static int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
738 GtkObject *cancelbutton = GTK_OBJECT(data);
741 * `Escape' effectively clicks the cancel button
743 if (event->keyval == GDK_Escape) {
744 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton), "clicked");
751 enum { MB_OK, MB_YESNO };
753 int message_box(GtkWidget *parent, char *title, char *msg, int centre,
756 GtkWidget *window, *hbox, *text, *button;
760 window = gtk_dialog_new();
761 text = gtk_label_new(msg);
762 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
763 hbox = gtk_hbox_new(FALSE, 0);
764 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
765 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
766 hbox, FALSE, FALSE, 20);
767 gtk_widget_show(text);
768 gtk_widget_show(hbox);
769 gtk_window_set_title(GTK_WINDOW(window), title);
770 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
776 assert(type == MB_YESNO);
777 titles = "Yes\0No\0";
784 button = gtk_button_new_with_label(titles);
785 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area),
786 button, FALSE, FALSE, 0);
787 gtk_widget_show(button);
789 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
790 gtk_window_set_default(GTK_WINDOW(window), button);
793 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
794 GTK_SIGNAL_FUNC(win_key_press), button);
796 gtk_signal_connect(GTK_OBJECT(button), "clicked",
797 GTK_SIGNAL_FUNC(msgbox_button_clicked), window);
798 gtk_object_set_data(GTK_OBJECT(button), "user-data",
800 titles += strlen(titles)+1;
803 gtk_object_set_data(GTK_OBJECT(window), "user-data",
804 GINT_TO_POINTER(&i));
805 gtk_signal_connect(GTK_OBJECT(window), "destroy",
806 GTK_SIGNAL_FUNC(window_destroy), NULL);
807 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
808 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent));
809 /* set_transient_window_pos(parent, window); */
810 gtk_widget_show(window);
813 return (type == MB_YESNO ? i == 0 : TRUE);
816 void error_box(GtkWidget *parent, char *msg)
818 message_box(parent, "Error", msg, FALSE, MB_OK);
821 static void config_ok_button_clicked(GtkButton *button, gpointer data)
823 frontend *fe = (frontend *)data;
826 err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
829 error_box(fe->cfgbox, err);
832 gtk_widget_destroy(fe->cfgbox);
836 static void config_cancel_button_clicked(GtkButton *button, gpointer data)
838 frontend *fe = (frontend *)data;
840 gtk_widget_destroy(fe->cfgbox);
843 static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
846 * GtkEntry has a nasty habit of eating the Return key, which
847 * is unhelpful since it doesn't actually _do_ anything with it
848 * (it calls gtk_widget_activate, but our edit boxes never need
849 * activating). So I catch Return before GtkEntry sees it, and
850 * pass it straight on to the parent widget. Effect: hitting
851 * Return in an edit box will now activate the default button
852 * in the dialog just like it will everywhere else.
854 if (event->keyval == GDK_Return && widget->parent != NULL) {
856 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
857 gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event",
864 static void editbox_changed(GtkEditable *ed, gpointer data)
866 config_item *i = (config_item *)data;
869 i->sval = dupstr(gtk_entry_get_text(GTK_ENTRY(ed)));
872 static void button_toggled(GtkToggleButton *tb, gpointer data)
874 config_item *i = (config_item *)data;
876 i->ival = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb));
879 static void droplist_sel(GtkMenuItem *item, gpointer data)
881 config_item *i = (config_item *)data;
883 i->ival = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
887 static int get_config(frontend *fe, int which)
889 GtkWidget *w, *table, *cancel;
894 fe->cfg = midend_get_config(fe->me, which, &title);
895 fe->cfg_which = which;
898 fe->cfgbox = gtk_dialog_new();
899 gtk_window_set_title(GTK_WINDOW(fe->cfgbox), title);
902 w = gtk_button_new_with_label("OK");
903 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area),
906 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
907 gtk_window_set_default(GTK_WINDOW(fe->cfgbox), w);
908 gtk_signal_connect(GTK_OBJECT(w), "clicked",
909 GTK_SIGNAL_FUNC(config_ok_button_clicked), fe);
911 w = gtk_button_new_with_label("Cancel");
912 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area),
915 gtk_signal_connect(GTK_OBJECT(w), "clicked",
916 GTK_SIGNAL_FUNC(config_cancel_button_clicked), fe);
919 table = gtk_table_new(1, 2, FALSE);
921 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->vbox),
922 table, FALSE, FALSE, 0);
923 gtk_widget_show(table);
925 for (i = fe->cfg; i->type != C_END; i++) {
926 gtk_table_resize(GTK_TABLE(table), y+1, 2);
931 * Edit box with a label beside it.
934 w = gtk_label_new(i->name);
935 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5);
936 gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1,
937 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
938 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
943 gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1,
944 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
945 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
947 gtk_entry_set_text(GTK_ENTRY(w), i->sval);
948 gtk_signal_connect(GTK_OBJECT(w), "changed",
949 GTK_SIGNAL_FUNC(editbox_changed), i);
950 gtk_signal_connect(GTK_OBJECT(w), "key_press_event",
951 GTK_SIGNAL_FUNC(editbox_key), NULL);
960 w = gtk_check_button_new_with_label(i->name);
961 gtk_signal_connect(GTK_OBJECT(w), "toggled",
962 GTK_SIGNAL_FUNC(button_toggled), i);
963 gtk_table_attach(GTK_TABLE(table), w, 0, 2, y, y+1,
964 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
965 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
967 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), i->ival);
973 * Drop-down list (GtkOptionMenu).
976 w = gtk_label_new(i->name);
977 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5);
978 gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1,
979 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
980 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
984 w = gtk_option_menu_new();
985 gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1,
986 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
987 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
995 GtkWidget *menu = gtk_menu_new();
997 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), menu);
1005 while (*q && *q != c)
1008 name = snewn(q-p+1, char);
1009 strncpy(name, p, q-p);
1012 if (*q) q++; /* eat delimiter */
1014 menuitem = gtk_menu_item_new_with_label(name);
1015 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1016 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1017 GINT_TO_POINTER(val));
1018 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1019 GTK_SIGNAL_FUNC(droplist_sel), i);
1020 gtk_widget_show(menuitem);
1027 gtk_option_menu_set_history(GTK_OPTION_MENU(w), i->ival);
1036 gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "destroy",
1037 GTK_SIGNAL_FUNC(window_destroy), NULL);
1038 gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "key_press_event",
1039 GTK_SIGNAL_FUNC(win_key_press), cancel);
1040 gtk_window_set_modal(GTK_WINDOW(fe->cfgbox), TRUE);
1041 gtk_window_set_transient_for(GTK_WINDOW(fe->cfgbox),
1042 GTK_WINDOW(fe->window));
1043 /* set_transient_window_pos(fe->window, fe->cfgbox); */
1044 gtk_widget_show(fe->cfgbox);
1052 static void menu_key_event(GtkMenuItem *menuitem, gpointer data)
1054 frontend *fe = (frontend *)data;
1055 int key = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem),
1057 if (!midend_process_key(fe->me, 0, 0, key))
1058 gtk_widget_destroy(fe->window);
1061 static void get_size(frontend *fe, int *px, int *py)
1066 * Currently I don't want to make the GTK port scale large
1067 * puzzles to fit on the screen. This is because X does permit
1068 * extremely large windows and many window managers provide a
1069 * means of navigating round them, and the users I consulted
1070 * before deciding said that they'd rather have enormous puzzle
1071 * windows spanning multiple screen pages than have them
1072 * shrunk. I could change my mind later or introduce
1073 * configurability; this would be the place to do so, by
1074 * replacing the initial values of x and y with the screen
1079 midend_size(fe->me, &x, &y, FALSE);
1084 #if !GTK_CHECK_VERSION(2,0,0)
1085 #define gtk_window_resize(win, x, y) \
1086 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
1089 static void menu_preset_event(GtkMenuItem *menuitem, gpointer data)
1091 frontend *fe = (frontend *)data;
1092 game_params *params =
1093 (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data");
1096 midend_set_params(fe->me, params);
1097 midend_new_game(fe->me);
1098 get_size(fe, &x, &y);
1101 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1104 gtk_widget_size_request(GTK_WIDGET(fe->window), &req);
1105 gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height);
1109 GdkAtom compound_text_atom, utf8_string_atom;
1110 int paste_initialised = FALSE;
1114 unsigned char empty[] = { 0 };
1116 if (paste_initialised)
1119 if (!compound_text_atom)
1120 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1121 if (!utf8_string_atom)
1122 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
1125 * Ensure that all the cut buffers exist - according to the
1126 * ICCCM, we must do this before we start using cut buffers.
1128 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1129 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1130 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1131 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1132 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1133 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1134 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1135 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1136 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1137 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1138 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1139 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1140 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1141 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1142 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1143 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1146 /* Store data in a cut-buffer. */
1147 void store_cutbuffer(char *ptr, int len)
1149 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1150 XRotateBuffers(GDK_DISPLAY(), 1);
1151 XStoreBytes(GDK_DISPLAY(), ptr, len);
1154 void write_clip(frontend *fe, char *data)
1159 sfree(fe->paste_data);
1162 * For this simple application we can safely assume that the
1163 * data passed to this function is pure ASCII, which means we
1164 * can return precisely the same stuff for types STRING,
1165 * COMPOUND_TEXT or UTF8_STRING.
1168 fe->paste_data = data;
1169 fe->paste_data_len = strlen(data);
1171 store_cutbuffer(fe->paste_data, fe->paste_data_len);
1173 if (gtk_selection_owner_set(fe->area, GDK_SELECTION_PRIMARY,
1175 gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
1176 GDK_SELECTION_TYPE_STRING, 1);
1177 gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
1178 compound_text_atom, 1);
1179 gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
1180 utf8_string_atom, 1);
1184 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1185 guint info, guint time_stamp, gpointer data)
1187 frontend *fe = (frontend *)data;
1188 gtk_selection_data_set(seldata, seldata->target, 8,
1189 fe->paste_data, fe->paste_data_len);
1192 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1195 frontend *fe = (frontend *)data;
1198 sfree(fe->paste_data);
1199 fe->paste_data = NULL;
1200 fe->paste_data_len = 0;
1204 static void menu_copy_event(GtkMenuItem *menuitem, gpointer data)
1206 frontend *fe = (frontend *)data;
1209 text = midend_text_format(fe->me);
1212 write_clip(fe, text);
1218 static void filesel_ok(GtkButton *button, gpointer data)
1220 frontend *fe = (frontend *)data;
1222 gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1225 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
1227 fe->filesel_name = dupstr(name);
1230 static char *file_selector(frontend *fe, char *title, int save)
1232 GtkWidget *filesel =
1233 gtk_file_selection_new(title);
1235 fe->filesel_name = NULL;
1237 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1239 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1242 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1243 GTK_SIGNAL_FUNC(filesel_ok), fe);
1244 gtk_signal_connect_object
1245 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1246 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1247 gtk_signal_connect_object
1248 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1249 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1250 gtk_signal_connect(GTK_OBJECT(filesel), "destroy",
1251 GTK_SIGNAL_FUNC(window_destroy), NULL);
1252 gtk_widget_show(filesel);
1253 gtk_window_set_transient_for(GTK_WINDOW(filesel), GTK_WINDOW(fe->window));
1256 return fe->filesel_name;
1259 static void savefile_write(void *wctx, void *buf, int len)
1261 FILE *fp = (FILE *)wctx;
1262 fwrite(buf, 1, len, fp);
1265 static int savefile_read(void *wctx, void *buf, int len)
1267 FILE *fp = (FILE *)wctx;
1270 ret = fread(buf, 1, len, fp);
1271 return (ret == len);
1274 static void menu_save_event(GtkMenuItem *menuitem, gpointer data)
1276 frontend *fe = (frontend *)data;
1279 name = file_selector(fe, "Enter name of game file to save", TRUE);
1284 if ((fp = fopen(name, "r")) != NULL) {
1285 char buf[256 + FILENAME_MAX];
1289 sprintf(buf, "Are you sure you want to overwrite the"
1291 FILENAME_MAX, name);
1292 if (!message_box(fe->window, "Question", buf, TRUE, MB_YESNO))
1296 fp = fopen(name, "w");
1300 error_box(fe->window, "Unable to open save file");
1304 midend_serialise(fe->me, savefile_write, fp);
1310 static void menu_load_event(GtkMenuItem *menuitem, gpointer data)
1312 frontend *fe = (frontend *)data;
1316 name = file_selector(fe, "Enter name of saved game file to load", FALSE);
1319 FILE *fp = fopen(name, "r");
1323 error_box(fe->window, "Unable to open saved game file");
1327 err = midend_deserialise(fe->me, savefile_read, fp);
1332 error_box(fe->window, err);
1336 get_size(fe, &x, &y);
1339 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1342 gtk_widget_size_request(GTK_WIDGET(fe->window), &req);
1343 gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height);
1349 static void menu_solve_event(GtkMenuItem *menuitem, gpointer data)
1351 frontend *fe = (frontend *)data;
1354 msg = midend_solve(fe->me);
1357 error_box(fe->window, msg);
1360 static void menu_restart_event(GtkMenuItem *menuitem, gpointer data)
1362 frontend *fe = (frontend *)data;
1364 midend_restart_game(fe->me);
1367 static void menu_config_event(GtkMenuItem *menuitem, gpointer data)
1369 frontend *fe = (frontend *)data;
1370 int which = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem),
1374 if (!get_config(fe, which))
1377 midend_new_game(fe->me);
1378 get_size(fe, &x, &y);
1381 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1384 gtk_widget_size_request(GTK_WIDGET(fe->window), &req);
1385 gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height);
1389 static void menu_about_event(GtkMenuItem *menuitem, gpointer data)
1391 frontend *fe = (frontend *)data;
1395 sprintf(titlebuf, "About %.200s", thegame.name);
1398 "from Simon Tatham's Portable Puzzle Collection\n\n"
1399 "%.500s", thegame.name, ver);
1401 message_box(fe->window, titlebuf, textbuf, TRUE, MB_OK);
1404 static GtkWidget *add_menu_item_with_key(frontend *fe, GtkContainer *cont,
1405 char *text, int key)
1407 GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
1409 gtk_container_add(cont, menuitem);
1410 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1411 GINT_TO_POINTER(key));
1412 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1413 GTK_SIGNAL_FUNC(menu_key_event), fe);
1414 switch (key & ~0x1F) {
1417 keyqual = GDK_CONTROL_MASK;
1421 keyqual = GDK_SHIFT_MASK;
1427 gtk_widget_add_accelerator(menuitem,
1428 "activate", fe->accelgroup,
1431 gtk_widget_show(menuitem);
1435 static void add_menu_separator(GtkContainer *cont)
1437 GtkWidget *menuitem = gtk_menu_item_new();
1438 gtk_container_add(cont, menuitem);
1439 gtk_widget_show(menuitem);
1442 static frontend *new_window(char *arg, char **error)
1446 GtkWidget *menubar, *menu, *menuitem;
1450 fe = snew(frontend);
1452 fe->timer_active = FALSE;
1455 fe->me = midend_new(fe, &thegame, >k_drawing, fe);
1463 * Try treating the argument as a game ID.
1465 err = midend_game_id(fe->me, arg);
1468 * It's a valid game ID.
1470 midend_new_game(fe->me);
1472 FILE *fp = fopen(arg, "r");
1474 sprintf(errbuf, "Supplied argument is neither a game ID (%.400s)"
1475 " nor a save file (%.400s)", err, strerror(errno));
1477 err = midend_deserialise(fe->me, savefile_read, fp);
1479 sprintf(errbuf, "%.800s", err);
1484 *error = dupstr(errbuf);
1485 midend_free(fe->me);
1491 midend_new_game(fe->me);
1494 fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1495 gtk_window_set_title(GTK_WINDOW(fe->window), thegame.name);
1497 gtk_window_set_resizable(GTK_WINDOW(fe->window), FALSE);
1499 gtk_window_set_policy(GTK_WINDOW(fe->window), FALSE, FALSE, TRUE);
1501 vbox = GTK_BOX(gtk_vbox_new(FALSE, 0));
1502 gtk_container_add(GTK_CONTAINER(fe->window), GTK_WIDGET(vbox));
1503 gtk_widget_show(GTK_WIDGET(vbox));
1505 fe->accelgroup = gtk_accel_group_new();
1506 gtk_window_add_accel_group(GTK_WINDOW(fe->window), fe->accelgroup);
1508 menubar = gtk_menu_bar_new();
1509 gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0);
1510 gtk_widget_show(menubar);
1512 menuitem = gtk_menu_item_new_with_label("Game");
1513 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
1514 gtk_widget_show(menuitem);
1516 menu = gtk_menu_new();
1517 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
1519 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "New", 'n');
1521 menuitem = gtk_menu_item_new_with_label("Restart");
1522 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1523 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1524 GTK_SIGNAL_FUNC(menu_restart_event), fe);
1525 gtk_widget_show(menuitem);
1527 menuitem = gtk_menu_item_new_with_label("Specific...");
1528 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1529 GINT_TO_POINTER(CFG_DESC));
1530 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1531 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1532 GTK_SIGNAL_FUNC(menu_config_event), fe);
1533 gtk_widget_show(menuitem);
1535 menuitem = gtk_menu_item_new_with_label("Random Seed...");
1536 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1537 GINT_TO_POINTER(CFG_SEED));
1538 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1539 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1540 GTK_SIGNAL_FUNC(menu_config_event), fe);
1541 gtk_widget_show(menuitem);
1543 if ((n = midend_num_presets(fe->me)) > 0 || thegame.can_configure) {
1547 menuitem = gtk_menu_item_new_with_label("Type");
1548 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
1549 gtk_widget_show(menuitem);
1551 submenu = gtk_menu_new();
1552 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1554 for (i = 0; i < n; i++) {
1556 game_params *params;
1558 midend_fetch_preset(fe->me, i, &name, ¶ms);
1560 menuitem = gtk_menu_item_new_with_label(name);
1561 gtk_container_add(GTK_CONTAINER(submenu), menuitem);
1562 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", params);
1563 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1564 GTK_SIGNAL_FUNC(menu_preset_event), fe);
1565 gtk_widget_show(menuitem);
1568 if (thegame.can_configure) {
1569 menuitem = gtk_menu_item_new_with_label("Custom...");
1570 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1571 GPOINTER_TO_INT(CFG_SETTINGS));
1572 gtk_container_add(GTK_CONTAINER(submenu), menuitem);
1573 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1574 GTK_SIGNAL_FUNC(menu_config_event), fe);
1575 gtk_widget_show(menuitem);
1579 add_menu_separator(GTK_CONTAINER(menu));
1580 menuitem = gtk_menu_item_new_with_label("Load");
1581 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1582 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1583 GTK_SIGNAL_FUNC(menu_load_event), fe);
1584 gtk_widget_show(menuitem);
1585 menuitem = gtk_menu_item_new_with_label("Save");
1586 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1587 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1588 GTK_SIGNAL_FUNC(menu_save_event), fe);
1589 gtk_widget_show(menuitem);
1590 add_menu_separator(GTK_CONTAINER(menu));
1591 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Undo", 'u');
1592 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Redo", 'r');
1593 if (thegame.can_format_as_text) {
1594 add_menu_separator(GTK_CONTAINER(menu));
1595 menuitem = gtk_menu_item_new_with_label("Copy");
1596 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1597 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1598 GTK_SIGNAL_FUNC(menu_copy_event), fe);
1599 gtk_widget_show(menuitem);
1601 if (thegame.can_solve) {
1602 add_menu_separator(GTK_CONTAINER(menu));
1603 menuitem = gtk_menu_item_new_with_label("Solve");
1604 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1605 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1606 GTK_SIGNAL_FUNC(menu_solve_event), fe);
1607 gtk_widget_show(menuitem);
1609 add_menu_separator(GTK_CONTAINER(menu));
1610 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Exit", 'q');
1612 menuitem = gtk_menu_item_new_with_label("Help");
1613 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
1614 gtk_widget_show(menuitem);
1616 menu = gtk_menu_new();
1617 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
1619 menuitem = gtk_menu_item_new_with_label("About");
1620 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1621 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1622 GTK_SIGNAL_FUNC(menu_about_event), fe);
1623 gtk_widget_show(menuitem);
1630 fe->colmap = gdk_colormap_get_system();
1631 colours = midend_colours(fe->me, &ncolours);
1632 fe->ncolours = ncolours;
1633 fe->colours = snewn(ncolours, GdkColor);
1634 for (i = 0; i < ncolours; i++) {
1635 fe->colours[i].red = colours[i*3] * 0xFFFF;
1636 fe->colours[i].green = colours[i*3+1] * 0xFFFF;
1637 fe->colours[i].blue = colours[i*3+2] * 0xFFFF;
1639 success = snewn(ncolours, gboolean);
1640 gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours,
1641 FALSE, FALSE, success);
1642 for (i = 0; i < ncolours; i++) {
1644 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
1645 i, fe->colours[i].red >> 8,
1646 fe->colours[i].green >> 8,
1647 fe->colours[i].blue >> 8);
1651 if (midend_wants_statusbar(fe->me)) {
1652 GtkWidget *viewport;
1655 viewport = gtk_viewport_new(NULL, NULL);
1656 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
1657 fe->statusbar = gtk_statusbar_new();
1658 gtk_container_add(GTK_CONTAINER(viewport), fe->statusbar);
1659 gtk_widget_show(viewport);
1660 gtk_box_pack_end(vbox, viewport, FALSE, FALSE, 0);
1661 gtk_widget_show(fe->statusbar);
1662 fe->statusctx = gtk_statusbar_get_context_id
1663 (GTK_STATUSBAR(fe->statusbar), "game");
1664 gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx,
1666 gtk_widget_size_request(fe->statusbar, &req);
1668 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
1670 gtk_widget_set_usize(viewport, -1, req.height);
1672 fe->statusbar = NULL;
1674 fe->area = gtk_drawing_area_new();
1675 get_size(fe, &x, &y);
1676 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1680 gtk_box_pack_end(vbox, fe->area, TRUE, TRUE, 0);
1684 fe->nfonts = fe->fontsize = 0;
1686 fe->paste_data = NULL;
1687 fe->paste_data_len = 0;
1689 gtk_signal_connect(GTK_OBJECT(fe->window), "destroy",
1690 GTK_SIGNAL_FUNC(destroy), fe);
1691 gtk_signal_connect(GTK_OBJECT(fe->window), "key_press_event",
1692 GTK_SIGNAL_FUNC(key_event), fe);
1693 gtk_signal_connect(GTK_OBJECT(fe->area), "button_press_event",
1694 GTK_SIGNAL_FUNC(button_event), fe);
1695 gtk_signal_connect(GTK_OBJECT(fe->area), "button_release_event",
1696 GTK_SIGNAL_FUNC(button_event), fe);
1697 gtk_signal_connect(GTK_OBJECT(fe->area), "motion_notify_event",
1698 GTK_SIGNAL_FUNC(motion_event), fe);
1699 gtk_signal_connect(GTK_OBJECT(fe->area), "selection_get",
1700 GTK_SIGNAL_FUNC(selection_get), fe);
1701 gtk_signal_connect(GTK_OBJECT(fe->area), "selection_clear_event",
1702 GTK_SIGNAL_FUNC(selection_clear), fe);
1703 gtk_signal_connect(GTK_OBJECT(fe->area), "expose_event",
1704 GTK_SIGNAL_FUNC(expose_area), fe);
1705 gtk_signal_connect(GTK_OBJECT(fe->window), "map_event",
1706 GTK_SIGNAL_FUNC(map_window), fe);
1707 gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event",
1708 GTK_SIGNAL_FUNC(configure_area), fe);
1710 gtk_widget_add_events(GTK_WIDGET(fe->area),
1711 GDK_BUTTON_PRESS_MASK |
1712 GDK_BUTTON_RELEASE_MASK |
1713 GDK_BUTTON_MOTION_MASK);
1715 gtk_widget_show(fe->area);
1716 gtk_widget_show(fe->window);
1718 gdk_window_set_background(fe->area->window, &fe->colours[0]);
1719 gdk_window_set_background(fe->window->window, &fe->colours[0]);
1724 char *fgetline(FILE *fp)
1726 char *ret = snewn(512, char);
1727 int size = 512, len = 0;
1728 while (fgets(ret + len, size - len, fp)) {
1729 len += strlen(ret + len);
1730 if (ret[len-1] == '\n')
1731 break; /* got a newline, we're done */
1733 ret = sresize(ret, size, char);
1735 if (len == 0) { /* first fgets returned NULL */
1743 int main(int argc, char **argv)
1745 char *pname = argv[0];
1747 int ngenerate = 0, print = FALSE, px = 1, py = 1;
1748 int soln = FALSE, colour = FALSE;
1751 int doing_opts = TRUE;
1757 * Command line parsing in this function is rather fiddly,
1758 * because GTK wants to have a go at argc/argv _first_ - and
1759 * yet we can't let it, because gtk_init() will bomb out if it
1760 * can't open an X display, whereas in fact we want to permit
1761 * our --generate and --print modes to run without an X
1765 * - we parse the command line ourselves, without modifying
1767 * - if we encounter an error which might plausibly be the
1768 * result of a GTK command line (i.e. not detailed errors in
1769 * particular options of ours) we store the error message
1770 * and terminate parsing.
1771 * - if we got enough out of the command line to know it
1772 * specifies a non-X mode of operation, we either display
1773 * the stored error and return failure, or if there is no
1774 * stored error we do the non-X operation and return
1776 * - otherwise, we go straight to gtk_init().
1782 if (doing_opts && !strcmp(p, "--version")) {
1783 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
1786 } else if (doing_opts && !strcmp(p, "--generate")) {
1788 ngenerate = atoi(*++av);
1790 fprintf(stderr, "%s: '--generate' expected a number\n",
1796 } else if (doing_opts && !strcmp(p, "--print")) {
1797 if (!thegame.can_print) {
1798 fprintf(stderr, "%s: this game does not support printing\n",
1805 if (sscanf(dim, "%dx%d", &px, &py) != 2) {
1806 fprintf(stderr, "%s: unable to parse argument '%s' to "
1807 "'--print'\n", pname, dim);
1813 } else if (doing_opts && !strcmp(p, "--scale")) {
1815 scale = atof(*++av);
1817 fprintf(stderr, "%s: no argument supplied to '--scale'\n",
1821 } else if (doing_opts && (!strcmp(p, "--with-solutions") ||
1822 !strcmp(p, "--with-solution") ||
1823 !strcmp(p, "--with-solns") ||
1824 !strcmp(p, "--with-soln") ||
1825 !strcmp(p, "--solutions") ||
1826 !strcmp(p, "--solution") ||
1827 !strcmp(p, "--solns") ||
1828 !strcmp(p, "--soln"))) {
1830 } else if (doing_opts && !strcmp(p, "--colour")) {
1831 if (!thegame.can_print_in_colour) {
1832 fprintf(stderr, "%s: this game does not support colour"
1833 " printing\n", pname);
1837 } else if (doing_opts && !strcmp(p, "--")) {
1839 } else if (!doing_opts || p[0] != '-') {
1841 fprintf(stderr, "%s: more than one argument supplied\n",
1847 sprintf(errbuf, "%.100s: unrecognised option '%.100s'\n",
1854 * Special standalone mode for generating puzzle IDs on the
1855 * command line. Useful for generating puzzles to be printed
1856 * out and solved offline (for puzzles where that even makes
1857 * sense - Solo, for example, is a lot more pencil-and-paper
1858 * friendly than Twiddle!)
1862 * <puzzle-name> --generate [<n> [<params>]]
1864 * <n>, if present, is the number of puzzle IDs to generate.
1865 * <params>, if present, is the same type of parameter string
1866 * you would pass to the puzzle when running it in GUI mode,
1867 * including optional extras such as the expansion factor in
1868 * Rectangles and the difficulty level in Solo.
1870 * If you specify <params>, you must also specify <n> (although
1871 * you may specify it to be 1). Sorry; that was the
1872 * simplest-to-parse command-line syntax I came up with.
1874 if (ngenerate > 0 || print) {
1878 document *doc = NULL;
1881 fputs(errbuf, stderr);
1887 me = midend_new(NULL, &thegame, NULL, NULL);
1891 doc = document_new(px, py, scale);
1894 * In this loop, we either generate a game ID or read one
1895 * from stdin depending on whether we're in generate mode;
1896 * then we either write it to stdout or print it, depending
1897 * on whether we're in print mode. Thus, this loop handles
1898 * generate-to-stdout, print-from-stdin and generate-and-
1899 * immediately-print modes.
1901 * (It could also handle a copy-stdin-to-stdout mode,
1902 * although there's currently no combination of options
1903 * which will cause this loop to be activated in that mode.
1904 * It wouldn't be _entirely_ pointless, though, because
1905 * stdin could contain bare params strings or random-seed
1906 * IDs, and stdout would contain nothing but fully
1907 * generated descriptive game IDs.)
1909 while (ngenerate == 0 || i < n) {
1912 if (ngenerate == 0) {
1913 pstr = fgetline(stdin);
1916 pstr[strcspn(pstr, "\r\n")] = '\0';
1919 pstr = snewn(strlen(arg) + 40, char);
1922 if (i > 0 && strchr(arg, '#'))
1923 sprintf(pstr + strlen(pstr), "-%d", i);
1929 err = midend_game_id(me, pstr);
1931 fprintf(stderr, "%s: error parsing '%s': %s\n",
1938 midend_new_game(me);
1941 err = midend_print_puzzle(me, doc, soln);
1943 fprintf(stderr, "%s: error in printing: %s\n", pname, err);
1947 id = midend_get_game_id(me);
1956 psdata *ps = ps_init(stdout, colour);
1957 document_print(doc, ps_drawing_api(ps));
1967 gtk_init(&argc, &argv);
1969 if (!new_window(argc > 1 ? argv[1] : NULL, &error)) {
1970 fprintf(stderr, "%s: %s\n", pname, error);