2 * gtk.c: GTK front end for my puzzle collection.
16 #include <gdk/gdkkeysyms.h>
18 #include <gdk-pixbuf/gdk-pixbuf.h>
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
27 #if GTK_CHECK_VERSION(2,0,0)
32 static FILE *debug_fp = NULL;
37 debug_fp = fopen("debug.log", "w");
48 void debug_printf(char *fmt, ...)
54 vsprintf(buf, fmt, ap);
60 /* ----------------------------------------------------------------------
61 * Error reporting functions used elsewhere.
64 void fatal(char *fmt, ...)
68 fprintf(stderr, "fatal error: ");
71 vfprintf(stderr, fmt, ap);
74 fprintf(stderr, "\n");
78 /* ----------------------------------------------------------------------
79 * GTK front end to puzzles.
82 static void update_preset_tick(frontend *fe);
86 PangoFontDescription *desc;
95 * This structure holds all the data relevant to a single window.
96 * In principle this would allow us to open multiple independent
97 * puzzle windows, although I can't currently see any real point in
98 * doing so. I'm just coding cleanly because there's no
99 * particularly good reason not to.
103 GtkAccelGroup *accelgroup;
105 GtkWidget *statusbar;
114 int bbox_l, bbox_r, bbox_u, bbox_d;
115 int timer_active, timer_id;
116 struct timeval last_time;
118 int nfonts, fontsize;
120 int cfg_which, cfgret;
124 int pw, ph; /* pixmap size (w, h are area size) */
125 int ox, oy; /* offset of pixmap in drawing area */
128 GtkWidget **preset_bullets;
129 GtkWidget *preset_custom_bullet;
132 void get_random_seed(void **randseed, int *randseedsize)
134 struct timeval *tvp = snew(struct timeval);
135 gettimeofday(tvp, NULL);
136 *randseed = (void *)tvp;
137 *randseedsize = sizeof(struct timeval);
140 void frontend_default_colour(frontend *fe, float *output)
142 GdkColor col = fe->window->style->bg[GTK_STATE_NORMAL];
143 output[0] = col.red / 65535.0;
144 output[1] = col.green / 65535.0;
145 output[2] = col.blue / 65535.0;
148 void gtk_status_bar(void *handle, char *text)
150 frontend *fe = (frontend *)handle;
152 assert(fe->statusbar);
154 gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx);
155 gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, text);
158 void gtk_start_draw(void *handle)
160 frontend *fe = (frontend *)handle;
161 fe->gc = gdk_gc_new(fe->area->window);
168 void gtk_clip(void *handle, int x, int y, int w, int h)
170 frontend *fe = (frontend *)handle;
178 gdk_gc_set_clip_rectangle(fe->gc, &rect);
181 void gtk_unclip(void *handle)
183 frontend *fe = (frontend *)handle;
191 gdk_gc_set_clip_rectangle(fe->gc, &rect);
194 void gtk_draw_text(void *handle, int x, int y, int fonttype, int fontsize,
195 int align, int colour, char *text)
197 frontend *fe = (frontend *)handle;
201 * Find or create the font.
203 for (i = 0; i < fe->nfonts; i++)
204 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
207 if (i == fe->nfonts) {
208 if (fe->fontsize <= fe->nfonts) {
209 fe->fontsize = fe->nfonts + 10;
210 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
215 fe->fonts[i].type = fonttype;
216 fe->fonts[i].size = fontsize;
220 * Use Pango to find the closest match to the requested
224 PangoFontDescription *fd;
226 fd = pango_font_description_new();
227 /* `Monospace' and `Sans' are meta-families guaranteed to exist */
228 pango_font_description_set_family(fd, fonttype == FONT_FIXED ?
229 "Monospace" : "Sans");
230 pango_font_description_set_weight(fd, PANGO_WEIGHT_BOLD);
232 * I found some online Pango documentation which
233 * described a function called
234 * pango_font_description_set_absolute_size(), which is
235 * _exactly_ what I want here. Unfortunately, none of
236 * my local Pango installations have it (presumably
237 * they're too old), so I'm going to have to hack round
238 * it by figuring out the point size myself. This
239 * limits me to X and probably also breaks in later
240 * Pango installations, so ideally I should add another
241 * CHECK_VERSION type ifdef and use set_absolute_size
242 * where available. All very annoying.
244 #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
245 pango_font_description_set_absolute_size(fd, PANGO_SCALE*fontsize);
248 Display *d = GDK_DISPLAY();
249 int s = DefaultScreen(d);
251 (PANGO_SCALE * 72.27 / 25.4) *
252 ((double) DisplayWidthMM(d, s) / DisplayWidth (d, s));
253 pango_font_description_set_size(fd, resolution * fontsize);
256 fe->fonts[i].desc = fd;
261 * In GTK 1.2, I don't know of any plausible way to
262 * pick a suitable font, so I'm just going to be
265 fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ?
266 "fixed" : "variable");
274 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
285 layout = pango_layout_new(gtk_widget_get_pango_context(fe->area));
286 pango_layout_set_font_description(layout, fe->fonts[i].desc);
287 pango_layout_set_text(layout, text, strlen(text));
288 pango_layout_get_pixel_extents(layout, NULL, &rect);
290 if (align & ALIGN_VCENTRE)
291 rect.y -= rect.height / 2;
293 rect.y -= rect.height;
295 if (align & ALIGN_HCENTRE)
296 rect.x -= rect.width / 2;
297 else if (align & ALIGN_HRIGHT)
298 rect.x -= rect.width;
300 gdk_draw_layout(fe->pixmap, fe->gc, rect.x + x, rect.y + y, layout);
302 g_object_unref(layout);
307 * Find string dimensions and process alignment.
310 int lb, rb, wid, asc, desc;
313 * Measure vertical string extents with respect to the same
316 gdk_string_extents(fe->fonts[i].font,
317 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
318 &lb, &rb, &wid, &asc, &desc);
319 if (align & ALIGN_VCENTRE)
320 y += asc - (asc+desc)/2;
325 * ... but horizontal extents with respect to the provided
326 * string. This means that multiple pieces of text centred
327 * on the same y-coordinate don't have different baselines.
329 gdk_string_extents(fe->fonts[i].font, text,
330 &lb, &rb, &wid, &asc, &desc);
332 if (align & ALIGN_HCENTRE)
334 else if (align & ALIGN_HRIGHT)
340 * Actually draw the text.
342 gdk_draw_string(fe->pixmap, fe->fonts[i].font, fe->gc, x, y, text);
347 void gtk_draw_rect(void *handle, int x, int y, int w, int h, int colour)
349 frontend *fe = (frontend *)handle;
350 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
351 gdk_draw_rectangle(fe->pixmap, fe->gc, 1, x, y, w, h);
354 void gtk_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
356 frontend *fe = (frontend *)handle;
357 gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
358 gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2);
361 void gtk_draw_poly(void *handle, int *coords, int npoints,
362 int fillcolour, int outlinecolour)
364 frontend *fe = (frontend *)handle;
365 GdkPoint *points = snewn(npoints, GdkPoint);
368 for (i = 0; i < npoints; i++) {
369 points[i].x = coords[i*2];
370 points[i].y = coords[i*2+1];
373 if (fillcolour >= 0) {
374 gdk_gc_set_foreground(fe->gc, &fe->colours[fillcolour]);
375 gdk_draw_polygon(fe->pixmap, fe->gc, TRUE, points, npoints);
377 assert(outlinecolour >= 0);
378 gdk_gc_set_foreground(fe->gc, &fe->colours[outlinecolour]);
381 * In principle we ought to be able to use gdk_draw_polygon for
382 * the outline as well. In fact, it turns out to interact badly
383 * with a clipping region, for no terribly obvious reason, so I
384 * draw the outline as a sequence of lines instead.
386 for (i = 0; i < npoints; i++)
387 gdk_draw_line(fe->pixmap, fe->gc,
388 points[i].x, points[i].y,
389 points[(i+1)%npoints].x, points[(i+1)%npoints].y);
394 void gtk_draw_circle(void *handle, int cx, int cy, int radius,
395 int fillcolour, int outlinecolour)
397 frontend *fe = (frontend *)handle;
398 if (fillcolour >= 0) {
399 gdk_gc_set_foreground(fe->gc, &fe->colours[fillcolour]);
400 gdk_draw_arc(fe->pixmap, fe->gc, TRUE,
401 cx - radius, cy - radius,
402 2 * radius, 2 * radius, 0, 360 * 64);
405 assert(outlinecolour >= 0);
406 gdk_gc_set_foreground(fe->gc, &fe->colours[outlinecolour]);
407 gdk_draw_arc(fe->pixmap, fe->gc, FALSE,
408 cx - radius, cy - radius,
409 2 * radius, 2 * radius, 0, 360 * 64);
417 blitter *gtk_blitter_new(void *handle, int w, int h)
420 * We can't create the pixmap right now, because fe->window
421 * might not yet exist. So we just cache w and h and create it
422 * during the firs call to blitter_save.
424 blitter *bl = snew(blitter);
431 void gtk_blitter_free(void *handle, blitter *bl)
434 gdk_pixmap_unref(bl->pixmap);
438 void gtk_blitter_save(void *handle, blitter *bl, int x, int y)
440 frontend *fe = (frontend *)handle;
442 bl->pixmap = gdk_pixmap_new(fe->area->window, bl->w, bl->h, -1);
445 gdk_draw_pixmap(bl->pixmap,
446 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
448 x, y, 0, 0, bl->w, bl->h);
451 void gtk_blitter_load(void *handle, blitter *bl, int x, int y)
453 frontend *fe = (frontend *)handle;
455 if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) {
459 gdk_draw_pixmap(fe->pixmap,
460 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
462 0, 0, x, y, bl->w, bl->h);
465 void gtk_draw_update(void *handle, int x, int y, int w, int h)
467 frontend *fe = (frontend *)handle;
468 if (fe->bbox_l > x ) fe->bbox_l = x ;
469 if (fe->bbox_r < x+w) fe->bbox_r = x+w;
470 if (fe->bbox_u > y ) fe->bbox_u = y ;
471 if (fe->bbox_d < y+h) fe->bbox_d = y+h;
474 void gtk_end_draw(void *handle)
476 frontend *fe = (frontend *)handle;
477 gdk_gc_unref(fe->gc);
480 if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) {
481 gdk_draw_pixmap(fe->area->window,
482 fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
484 fe->bbox_l, fe->bbox_u,
485 fe->ox + fe->bbox_l, fe->oy + fe->bbox_u,
486 fe->bbox_r - fe->bbox_l, fe->bbox_d - fe->bbox_u);
490 const struct drawing_api gtk_drawing = {
506 NULL, NULL, NULL, NULL, NULL, NULL, /* {begin,end}_{doc,page,puzzle} */
507 NULL, /* line_width */
510 static void destroy(GtkWidget *widget, gpointer data)
512 frontend *fe = (frontend *)data;
513 deactivate_timer(fe);
518 static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
520 frontend *fe = (frontend *)data;
522 int shift = (event->state & GDK_SHIFT_MASK) ? MOD_SHFT : 0;
523 int ctrl = (event->state & GDK_CONTROL_MASK) ? MOD_CTRL : 0;
528 #if !GTK_CHECK_VERSION(2,0,0)
529 /* Gtk 1.2 passes a key event to this function even if it's also
530 * defined as an accelerator.
531 * Gtk 2 doesn't do this, and this function appears not to exist there. */
532 if (fe->accelgroup &&
533 gtk_accel_group_get_entry(fe->accelgroup,
534 event->keyval, event->state))
538 if (event->keyval == GDK_Up)
539 keyval = shift | ctrl | CURSOR_UP;
540 else if (event->keyval == GDK_KP_Up || event->keyval == GDK_KP_8)
541 keyval = MOD_NUM_KEYPAD | '8';
542 else if (event->keyval == GDK_Down)
543 keyval = shift | ctrl | CURSOR_DOWN;
544 else if (event->keyval == GDK_KP_Down || event->keyval == GDK_KP_2)
545 keyval = MOD_NUM_KEYPAD | '2';
546 else if (event->keyval == GDK_Left)
547 keyval = shift | ctrl | CURSOR_LEFT;
548 else if (event->keyval == GDK_KP_Left || event->keyval == GDK_KP_4)
549 keyval = MOD_NUM_KEYPAD | '4';
550 else if (event->keyval == GDK_Right)
551 keyval = shift | ctrl | CURSOR_RIGHT;
552 else if (event->keyval == GDK_KP_Right || event->keyval == GDK_KP_6)
553 keyval = MOD_NUM_KEYPAD | '6';
554 else if (event->keyval == GDK_KP_Home || event->keyval == GDK_KP_7)
555 keyval = MOD_NUM_KEYPAD | '7';
556 else if (event->keyval == GDK_KP_End || event->keyval == GDK_KP_1)
557 keyval = MOD_NUM_KEYPAD | '1';
558 else if (event->keyval == GDK_KP_Page_Up || event->keyval == GDK_KP_9)
559 keyval = MOD_NUM_KEYPAD | '9';
560 else if (event->keyval == GDK_KP_Page_Down || event->keyval == GDK_KP_3)
561 keyval = MOD_NUM_KEYPAD | '3';
562 else if (event->keyval == GDK_KP_Insert || event->keyval == GDK_KP_0)
563 keyval = MOD_NUM_KEYPAD | '0';
564 else if (event->keyval == GDK_KP_Begin || event->keyval == GDK_KP_5)
565 keyval = MOD_NUM_KEYPAD | '5';
566 else if (event->keyval == GDK_BackSpace ||
567 event->keyval == GDK_Delete ||
568 event->keyval == GDK_KP_Delete)
570 else if (event->string[0] && !event->string[1])
571 keyval = (unsigned char)event->string[0];
576 !midend_process_key(fe->me, 0, 0, keyval))
577 gtk_widget_destroy(fe->window);
582 static gint button_event(GtkWidget *widget, GdkEventButton *event,
585 frontend *fe = (frontend *)data;
591 if (event->type != GDK_BUTTON_PRESS && event->type != GDK_BUTTON_RELEASE)
594 if (event->button == 2 || (event->state & GDK_SHIFT_MASK))
595 button = MIDDLE_BUTTON;
596 else if (event->button == 3 || (event->state & GDK_MOD1_MASK))
597 button = RIGHT_BUTTON;
598 else if (event->button == 1)
599 button = LEFT_BUTTON;
601 return FALSE; /* don't even know what button! */
603 if (event->type == GDK_BUTTON_RELEASE)
604 button += LEFT_RELEASE - LEFT_BUTTON;
606 if (!midend_process_key(fe->me, event->x - fe->ox,
607 event->y - fe->oy, button))
608 gtk_widget_destroy(fe->window);
613 static gint motion_event(GtkWidget *widget, GdkEventMotion *event,
616 frontend *fe = (frontend *)data;
622 if (event->state & (GDK_BUTTON2_MASK | GDK_SHIFT_MASK))
623 button = MIDDLE_DRAG;
624 else if (event->state & GDK_BUTTON1_MASK)
626 else if (event->state & GDK_BUTTON3_MASK)
629 return FALSE; /* don't even know what button! */
631 if (!midend_process_key(fe->me, event->x - fe->ox,
632 event->y - fe->oy, button))
633 gtk_widget_destroy(fe->window);
638 static gint expose_area(GtkWidget *widget, GdkEventExpose *event,
641 frontend *fe = (frontend *)data;
644 gdk_draw_pixmap(widget->window,
645 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
647 event->area.x - fe->ox, event->area.y - fe->oy,
648 event->area.x, event->area.y,
649 event->area.width, event->area.height);
654 static gint map_window(GtkWidget *widget, GdkEvent *event,
657 frontend *fe = (frontend *)data;
660 * Apparently we need to do this because otherwise the status
661 * bar will fail to update immediately. Annoying, but there we
664 gtk_widget_queue_draw(fe->window);
669 static gint configure_area(GtkWidget *widget,
670 GdkEventConfigure *event, gpointer data)
672 frontend *fe = (frontend *)data;
677 gdk_pixmap_unref(fe->pixmap);
679 x = fe->w = event->width;
680 y = fe->h = event->height;
681 midend_size(fe->me, &x, &y, TRUE);
684 fe->ox = (fe->w - fe->pw) / 2;
685 fe->oy = (fe->h - fe->ph) / 2;
687 fe->pixmap = gdk_pixmap_new(widget->window, fe->pw, fe->ph, -1);
689 gc = gdk_gc_new(fe->area->window);
690 gdk_gc_set_foreground(gc, &fe->colours[0]);
691 gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->pw, fe->ph);
692 gdk_draw_rectangle(widget->window, gc, 1, 0, 0,
693 event->width, event->height);
696 midend_force_redraw(fe->me);
701 static gint timer_func(gpointer data)
703 frontend *fe = (frontend *)data;
705 if (fe->timer_active) {
708 gettimeofday(&now, NULL);
709 elapsed = ((now.tv_usec - fe->last_time.tv_usec) * 0.000001F +
710 (now.tv_sec - fe->last_time.tv_sec));
711 midend_timer(fe->me, elapsed); /* may clear timer_active */
715 return fe->timer_active;
718 void deactivate_timer(frontend *fe)
721 return; /* can happen due to --generate */
722 if (fe->timer_active)
723 gtk_timeout_remove(fe->timer_id);
724 fe->timer_active = FALSE;
727 void activate_timer(frontend *fe)
730 return; /* can happen due to --generate */
731 if (!fe->timer_active) {
732 fe->timer_id = gtk_timeout_add(20, timer_func, fe);
733 gettimeofday(&fe->last_time, NULL);
735 fe->timer_active = TRUE;
738 static void window_destroy(GtkWidget *widget, gpointer data)
743 static void msgbox_button_clicked(GtkButton *button, gpointer data)
745 GtkWidget *window = GTK_WIDGET(data);
748 ip = (int *)gtk_object_get_data(GTK_OBJECT(window), "user-data");
749 v = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "user-data"));
752 gtk_widget_destroy(GTK_WIDGET(data));
755 static int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
757 GtkObject *cancelbutton = GTK_OBJECT(data);
760 * `Escape' effectively clicks the cancel button
762 if (event->keyval == GDK_Escape) {
763 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton), "clicked");
770 enum { MB_OK, MB_YESNO };
772 int message_box(GtkWidget *parent, char *title, char *msg, int centre,
775 GtkWidget *window, *hbox, *text, *button;
779 window = gtk_dialog_new();
780 text = gtk_label_new(msg);
781 gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
782 hbox = gtk_hbox_new(FALSE, 0);
783 gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
784 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
785 hbox, FALSE, FALSE, 20);
786 gtk_widget_show(text);
787 gtk_widget_show(hbox);
788 gtk_window_set_title(GTK_WINDOW(window), title);
789 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
795 assert(type == MB_YESNO);
796 titles = "Yes\0No\0";
803 button = gtk_button_new_with_label(titles);
804 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area),
805 button, FALSE, FALSE, 0);
806 gtk_widget_show(button);
808 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
809 gtk_window_set_default(GTK_WINDOW(window), button);
812 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
813 GTK_SIGNAL_FUNC(win_key_press), button);
815 gtk_signal_connect(GTK_OBJECT(button), "clicked",
816 GTK_SIGNAL_FUNC(msgbox_button_clicked), window);
817 gtk_object_set_data(GTK_OBJECT(button), "user-data",
819 titles += strlen(titles)+1;
822 gtk_object_set_data(GTK_OBJECT(window), "user-data",
823 GINT_TO_POINTER(&i));
824 gtk_signal_connect(GTK_OBJECT(window), "destroy",
825 GTK_SIGNAL_FUNC(window_destroy), NULL);
826 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
827 gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent));
828 /* set_transient_window_pos(parent, window); */
829 gtk_widget_show(window);
832 return (type == MB_YESNO ? i == 0 : TRUE);
835 void error_box(GtkWidget *parent, char *msg)
837 message_box(parent, "Error", msg, FALSE, MB_OK);
840 static void config_ok_button_clicked(GtkButton *button, gpointer data)
842 frontend *fe = (frontend *)data;
845 err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);
848 error_box(fe->cfgbox, err);
851 gtk_widget_destroy(fe->cfgbox);
852 update_preset_tick(fe);
856 static void config_cancel_button_clicked(GtkButton *button, gpointer data)
858 frontend *fe = (frontend *)data;
860 gtk_widget_destroy(fe->cfgbox);
863 static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
866 * GtkEntry has a nasty habit of eating the Return key, which
867 * is unhelpful since it doesn't actually _do_ anything with it
868 * (it calls gtk_widget_activate, but our edit boxes never need
869 * activating). So I catch Return before GtkEntry sees it, and
870 * pass it straight on to the parent widget. Effect: hitting
871 * Return in an edit box will now activate the default button
872 * in the dialog just like it will everywhere else.
874 if (event->keyval == GDK_Return && widget->parent != NULL) {
876 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
877 gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event",
884 static void editbox_changed(GtkEditable *ed, gpointer data)
886 config_item *i = (config_item *)data;
889 i->sval = dupstr(gtk_entry_get_text(GTK_ENTRY(ed)));
892 static void button_toggled(GtkToggleButton *tb, gpointer data)
894 config_item *i = (config_item *)data;
896 i->ival = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb));
899 static void droplist_sel(GtkMenuItem *item, gpointer data)
901 config_item *i = (config_item *)data;
903 i->ival = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
907 static int get_config(frontend *fe, int which)
909 GtkWidget *w, *table, *cancel;
914 fe->cfg = midend_get_config(fe->me, which, &title);
915 fe->cfg_which = which;
918 fe->cfgbox = gtk_dialog_new();
919 gtk_window_set_title(GTK_WINDOW(fe->cfgbox), title);
922 w = gtk_button_new_with_label("OK");
923 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area),
926 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
927 gtk_window_set_default(GTK_WINDOW(fe->cfgbox), w);
928 gtk_signal_connect(GTK_OBJECT(w), "clicked",
929 GTK_SIGNAL_FUNC(config_ok_button_clicked), fe);
931 w = gtk_button_new_with_label("Cancel");
932 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area),
935 gtk_signal_connect(GTK_OBJECT(w), "clicked",
936 GTK_SIGNAL_FUNC(config_cancel_button_clicked), fe);
939 table = gtk_table_new(1, 2, FALSE);
941 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->vbox),
942 table, FALSE, FALSE, 0);
943 gtk_widget_show(table);
945 for (i = fe->cfg; i->type != C_END; i++) {
946 gtk_table_resize(GTK_TABLE(table), y+1, 2);
951 * Edit box with a label beside it.
954 w = gtk_label_new(i->name);
955 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5);
956 gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1,
957 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
958 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
963 gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1,
964 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
965 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
967 gtk_entry_set_text(GTK_ENTRY(w), i->sval);
968 gtk_signal_connect(GTK_OBJECT(w), "changed",
969 GTK_SIGNAL_FUNC(editbox_changed), i);
970 gtk_signal_connect(GTK_OBJECT(w), "key_press_event",
971 GTK_SIGNAL_FUNC(editbox_key), NULL);
980 w = gtk_check_button_new_with_label(i->name);
981 gtk_signal_connect(GTK_OBJECT(w), "toggled",
982 GTK_SIGNAL_FUNC(button_toggled), i);
983 gtk_table_attach(GTK_TABLE(table), w, 0, 2, y, y+1,
984 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
985 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
987 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), i->ival);
993 * Drop-down list (GtkOptionMenu).
996 w = gtk_label_new(i->name);
997 gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5);
998 gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1,
999 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
1000 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
1004 w = gtk_option_menu_new();
1005 gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1,
1006 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
1007 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
1014 GtkWidget *menuitem;
1015 GtkWidget *menu = gtk_menu_new();
1017 gtk_option_menu_set_menu(GTK_OPTION_MENU(w), menu);
1025 while (*q && *q != c)
1028 name = snewn(q-p+1, char);
1029 strncpy(name, p, q-p);
1032 if (*q) q++; /* eat delimiter */
1034 menuitem = gtk_menu_item_new_with_label(name);
1035 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1036 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1037 GINT_TO_POINTER(val));
1038 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1039 GTK_SIGNAL_FUNC(droplist_sel), i);
1040 gtk_widget_show(menuitem);
1047 gtk_option_menu_set_history(GTK_OPTION_MENU(w), i->ival);
1056 gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "destroy",
1057 GTK_SIGNAL_FUNC(window_destroy), NULL);
1058 gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "key_press_event",
1059 GTK_SIGNAL_FUNC(win_key_press), cancel);
1060 gtk_window_set_modal(GTK_WINDOW(fe->cfgbox), TRUE);
1061 gtk_window_set_transient_for(GTK_WINDOW(fe->cfgbox),
1062 GTK_WINDOW(fe->window));
1063 /* set_transient_window_pos(fe->window, fe->cfgbox); */
1064 gtk_widget_show(fe->cfgbox);
1072 static void menu_key_event(GtkMenuItem *menuitem, gpointer data)
1074 frontend *fe = (frontend *)data;
1075 int key = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem),
1077 if (!midend_process_key(fe->me, 0, 0, key))
1078 gtk_widget_destroy(fe->window);
1081 static void get_size(frontend *fe, int *px, int *py)
1086 * Currently I don't want to make the GTK port scale large
1087 * puzzles to fit on the screen. This is because X does permit
1088 * extremely large windows and many window managers provide a
1089 * means of navigating round them, and the users I consulted
1090 * before deciding said that they'd rather have enormous puzzle
1091 * windows spanning multiple screen pages than have them
1092 * shrunk. I could change my mind later or introduce
1093 * configurability; this would be the place to do so, by
1094 * replacing the initial values of x and y with the screen
1099 midend_size(fe->me, &x, &y, FALSE);
1104 #if !GTK_CHECK_VERSION(2,0,0)
1105 #define gtk_window_resize(win, x, y) \
1106 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
1109 static void update_menuitem_bullet(GtkWidget *label, int visible)
1112 gtk_label_set_text(GTK_LABEL(label), "\xE2\x80\xA2");
1114 gtk_label_set_text(GTK_LABEL(label), "");
1118 static void update_preset_tick(frontend *fe)
1120 int n = midend_which_preset(fe->me);
1123 if (fe->preset_bullets) {
1124 for (i = 0; i < fe->npresets; i++)
1125 update_menuitem_bullet(fe->preset_bullets[i], n == i);
1127 if (fe->preset_custom_bullet) {
1128 update_menuitem_bullet(fe->preset_custom_bullet, n < 0);
1132 static void resize_fe(frontend *fe)
1136 get_size(fe, &x, &y);
1139 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1142 gtk_widget_size_request(GTK_WIDGET(fe->window), &req);
1143 gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height);
1146 * Now that we've established the preferred size of the window,
1147 * reduce the drawing area's size request so the user can shrink
1150 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), 1, 1);
1153 static void menu_preset_event(GtkMenuItem *menuitem, gpointer data)
1155 frontend *fe = (frontend *)data;
1156 game_params *params =
1157 (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data");
1159 midend_set_params(fe->me, params);
1160 midend_new_game(fe->me);
1161 update_preset_tick(fe);
1165 GdkAtom compound_text_atom, utf8_string_atom;
1166 int paste_initialised = FALSE;
1170 unsigned char empty[] = { 0 };
1172 if (paste_initialised)
1175 if (!compound_text_atom)
1176 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1177 if (!utf8_string_atom)
1178 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
1181 * Ensure that all the cut buffers exist - according to the
1182 * ICCCM, we must do this before we start using cut buffers.
1184 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1185 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1186 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1187 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1188 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1189 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1190 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1191 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1192 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1193 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1194 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1195 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1196 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1197 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1198 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1199 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1202 /* Store data in a cut-buffer. */
1203 void store_cutbuffer(char *ptr, int len)
1205 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1206 XRotateBuffers(GDK_DISPLAY(), 1);
1207 XStoreBytes(GDK_DISPLAY(), ptr, len);
1210 void write_clip(frontend *fe, char *data)
1215 sfree(fe->paste_data);
1218 * For this simple application we can safely assume that the
1219 * data passed to this function is pure ASCII, which means we
1220 * can return precisely the same stuff for types STRING,
1221 * COMPOUND_TEXT or UTF8_STRING.
1224 fe->paste_data = data;
1225 fe->paste_data_len = strlen(data);
1227 store_cutbuffer(fe->paste_data, fe->paste_data_len);
1229 if (gtk_selection_owner_set(fe->area, GDK_SELECTION_PRIMARY,
1231 gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
1232 GDK_SELECTION_TYPE_STRING, 1);
1233 gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
1234 compound_text_atom, 1);
1235 gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
1236 utf8_string_atom, 1);
1240 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1241 guint info, guint time_stamp, gpointer data)
1243 frontend *fe = (frontend *)data;
1244 gtk_selection_data_set(seldata, seldata->target, 8,
1245 fe->paste_data, fe->paste_data_len);
1248 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1251 frontend *fe = (frontend *)data;
1254 sfree(fe->paste_data);
1255 fe->paste_data = NULL;
1256 fe->paste_data_len = 0;
1260 static void menu_copy_event(GtkMenuItem *menuitem, gpointer data)
1262 frontend *fe = (frontend *)data;
1265 text = midend_text_format(fe->me);
1268 write_clip(fe, text);
1274 static void filesel_ok(GtkButton *button, gpointer data)
1276 frontend *fe = (frontend *)data;
1278 gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
1281 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
1283 fe->filesel_name = dupstr(name);
1286 static char *file_selector(frontend *fe, char *title, int save)
1288 GtkWidget *filesel =
1289 gtk_file_selection_new(title);
1291 fe->filesel_name = NULL;
1293 gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
1295 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
1298 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1299 GTK_SIGNAL_FUNC(filesel_ok), fe);
1300 gtk_signal_connect_object
1301 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
1302 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1303 gtk_signal_connect_object
1304 (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
1305 GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
1306 gtk_signal_connect(GTK_OBJECT(filesel), "destroy",
1307 GTK_SIGNAL_FUNC(window_destroy), NULL);
1308 gtk_widget_show(filesel);
1309 gtk_window_set_transient_for(GTK_WINDOW(filesel), GTK_WINDOW(fe->window));
1312 return fe->filesel_name;
1315 static void savefile_write(void *wctx, void *buf, int len)
1317 FILE *fp = (FILE *)wctx;
1318 fwrite(buf, 1, len, fp);
1321 static int savefile_read(void *wctx, void *buf, int len)
1323 FILE *fp = (FILE *)wctx;
1326 ret = fread(buf, 1, len, fp);
1327 return (ret == len);
1330 static void menu_save_event(GtkMenuItem *menuitem, gpointer data)
1332 frontend *fe = (frontend *)data;
1335 name = file_selector(fe, "Enter name of game file to save", TRUE);
1340 if ((fp = fopen(name, "r")) != NULL) {
1341 char buf[256 + FILENAME_MAX];
1345 sprintf(buf, "Are you sure you want to overwrite the"
1347 FILENAME_MAX, name);
1348 if (!message_box(fe->window, "Question", buf, TRUE, MB_YESNO))
1352 fp = fopen(name, "w");
1356 error_box(fe->window, "Unable to open save file");
1360 midend_serialise(fe->me, savefile_write, fp);
1366 static void menu_load_event(GtkMenuItem *menuitem, gpointer data)
1368 frontend *fe = (frontend *)data;
1371 name = file_selector(fe, "Enter name of saved game file to load", FALSE);
1374 FILE *fp = fopen(name, "r");
1378 error_box(fe->window, "Unable to open saved game file");
1382 err = midend_deserialise(fe->me, savefile_read, fp);
1387 error_box(fe->window, err);
1391 update_preset_tick(fe);
1396 static void menu_solve_event(GtkMenuItem *menuitem, gpointer data)
1398 frontend *fe = (frontend *)data;
1401 msg = midend_solve(fe->me);
1404 error_box(fe->window, msg);
1407 static void menu_restart_event(GtkMenuItem *menuitem, gpointer data)
1409 frontend *fe = (frontend *)data;
1411 midend_restart_game(fe->me);
1414 static void menu_config_event(GtkMenuItem *menuitem, gpointer data)
1416 frontend *fe = (frontend *)data;
1417 int which = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem),
1420 if (!get_config(fe, which))
1423 midend_new_game(fe->me);
1427 static void menu_about_event(GtkMenuItem *menuitem, gpointer data)
1429 frontend *fe = (frontend *)data;
1433 sprintf(titlebuf, "About %.200s", thegame.name);
1436 "from Simon Tatham's Portable Puzzle Collection\n\n"
1437 "%.500s", thegame.name, ver);
1439 message_box(fe->window, titlebuf, textbuf, TRUE, MB_OK);
1442 static GtkWidget *add_menu_item_with_key(frontend *fe, GtkContainer *cont,
1443 char *text, int key)
1445 GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
1447 gtk_container_add(cont, menuitem);
1448 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1449 GINT_TO_POINTER(key));
1450 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1451 GTK_SIGNAL_FUNC(menu_key_event), fe);
1452 switch (key & ~0x1F) {
1455 keyqual = GDK_CONTROL_MASK;
1459 keyqual = GDK_SHIFT_MASK;
1465 gtk_widget_add_accelerator(menuitem,
1466 "activate", fe->accelgroup,
1469 gtk_widget_show(menuitem);
1473 static void add_menu_separator(GtkContainer *cont)
1475 GtkWidget *menuitem = gtk_menu_item_new();
1476 gtk_container_add(cont, menuitem);
1477 gtk_widget_show(menuitem);
1480 enum { ARG_EITHER, ARG_SAVE, ARG_ID }; /* for argtype */
1482 static GtkWidget *make_preset_menuitem(GtkWidget **bulletlabel,
1485 GtkWidget *hbox, *lab1, *lab2, *menuitem;
1488 hbox = gtk_hbox_new(FALSE, 0);
1489 gtk_widget_show(hbox);
1490 lab1 = gtk_label_new("\xE2\x80\xA2 ");
1491 gtk_widget_show(lab1);
1492 gtk_box_pack_start(GTK_BOX(hbox), lab1, FALSE, FALSE, 0);
1493 gtk_misc_set_alignment(GTK_MISC(lab1), 0.0, 0.0);
1494 lab2 = gtk_label_new(name);
1495 gtk_widget_show(lab2);
1496 gtk_box_pack_start(GTK_BOX(hbox), lab2, TRUE, TRUE, 0);
1497 gtk_misc_set_alignment(GTK_MISC(lab2), 0.0, 0.0);
1499 gtk_widget_size_request(lab1, &req);
1500 gtk_widget_set_usize(lab1, req.width, -1);
1501 gtk_label_set_text(GTK_LABEL(lab1), "");
1503 menuitem = gtk_menu_item_new();
1504 gtk_container_add(GTK_CONTAINER(menuitem), hbox);
1506 *bulletlabel = lab1;
1510 static frontend *new_window(char *arg, int argtype, char **error)
1514 GtkWidget *menubar, *menu, *menuitem;
1519 extern char *const *const xpm_icons[];
1520 extern const int n_xpm_icons;
1522 fe = snew(frontend);
1524 fe->timer_active = FALSE;
1527 fe->me = midend_new(fe, &thegame, >k_drawing, fe);
1537 err = midend_game_id(fe->me, arg);
1539 midend_new_game(fe->me);
1541 sprintf(errbuf, "Invalid game ID: %.800s", err);
1544 fp = fopen(arg, "r");
1546 sprintf(errbuf, "Error opening file: %.800s", strerror(errno));
1548 err = midend_deserialise(fe->me, savefile_read, fp);
1550 sprintf(errbuf, "Invalid save file: %.800s", err);
1554 default /*case ARG_EITHER*/:
1556 * First try treating the argument as a game ID.
1558 err = midend_game_id(fe->me, arg);
1561 * It's a valid game ID.
1563 midend_new_game(fe->me);
1565 FILE *fp = fopen(arg, "r");
1567 sprintf(errbuf, "Supplied argument is neither a game ID (%.400s)"
1568 " nor a save file (%.400s)", err, strerror(errno));
1570 err = midend_deserialise(fe->me, savefile_read, fp);
1572 sprintf(errbuf, "%.800s", err);
1579 *error = dupstr(errbuf);
1580 midend_free(fe->me);
1586 midend_new_game(fe->me);
1589 fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1590 gtk_window_set_title(GTK_WINDOW(fe->window), thegame.name);
1592 vbox = GTK_BOX(gtk_vbox_new(FALSE, 0));
1593 gtk_container_add(GTK_CONTAINER(fe->window), GTK_WIDGET(vbox));
1594 gtk_widget_show(GTK_WIDGET(vbox));
1596 fe->accelgroup = gtk_accel_group_new();
1597 gtk_window_add_accel_group(GTK_WINDOW(fe->window), fe->accelgroup);
1599 menubar = gtk_menu_bar_new();
1600 gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0);
1601 gtk_widget_show(menubar);
1603 menuitem = gtk_menu_item_new_with_label("Game");
1604 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
1605 gtk_widget_show(menuitem);
1607 menu = gtk_menu_new();
1608 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
1610 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "New", 'n');
1612 menuitem = gtk_menu_item_new_with_label("Restart");
1613 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1614 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1615 GTK_SIGNAL_FUNC(menu_restart_event), fe);
1616 gtk_widget_show(menuitem);
1618 menuitem = gtk_menu_item_new_with_label("Specific...");
1619 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1620 GINT_TO_POINTER(CFG_DESC));
1621 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1622 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1623 GTK_SIGNAL_FUNC(menu_config_event), fe);
1624 gtk_widget_show(menuitem);
1626 menuitem = gtk_menu_item_new_with_label("Random Seed...");
1627 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1628 GINT_TO_POINTER(CFG_SEED));
1629 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1630 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1631 GTK_SIGNAL_FUNC(menu_config_event), fe);
1632 gtk_widget_show(menuitem);
1634 if ((n = midend_num_presets(fe->me)) > 0 || thegame.can_configure) {
1638 menuitem = gtk_menu_item_new_with_label("Type");
1639 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
1640 gtk_widget_show(menuitem);
1642 submenu = gtk_menu_new();
1643 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1646 fe->preset_bullets = snewn(n, GtkWidget *);
1648 for (i = 0; i < n; i++) {
1650 game_params *params;
1652 midend_fetch_preset(fe->me, i, &name, ¶ms);
1654 menuitem = make_preset_menuitem(&fe->preset_bullets[i], name);
1656 gtk_container_add(GTK_CONTAINER(submenu), menuitem);
1657 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", params);
1658 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1659 GTK_SIGNAL_FUNC(menu_preset_event), fe);
1660 gtk_widget_show(menuitem);
1663 if (thegame.can_configure) {
1664 menuitem = make_preset_menuitem(&fe->preset_custom_bullet,
1667 gtk_container_add(GTK_CONTAINER(submenu), menuitem);
1668 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
1669 GPOINTER_TO_INT(CFG_SETTINGS));
1670 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1671 GTK_SIGNAL_FUNC(menu_config_event), fe);
1672 gtk_widget_show(menuitem);
1674 fe->preset_custom_bullet = NULL;
1676 update_preset_tick(fe);
1679 fe->preset_bullets = NULL;
1680 fe->preset_custom_bullet = NULL;
1683 add_menu_separator(GTK_CONTAINER(menu));
1684 menuitem = gtk_menu_item_new_with_label("Load...");
1685 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1686 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1687 GTK_SIGNAL_FUNC(menu_load_event), fe);
1688 gtk_widget_show(menuitem);
1689 menuitem = gtk_menu_item_new_with_label("Save...");
1690 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1691 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1692 GTK_SIGNAL_FUNC(menu_save_event), fe);
1693 gtk_widget_show(menuitem);
1694 add_menu_separator(GTK_CONTAINER(menu));
1695 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Undo", 'u');
1696 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Redo", 'r');
1697 if (thegame.can_format_as_text) {
1698 add_menu_separator(GTK_CONTAINER(menu));
1699 menuitem = gtk_menu_item_new_with_label("Copy");
1700 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1701 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1702 GTK_SIGNAL_FUNC(menu_copy_event), fe);
1703 gtk_widget_show(menuitem);
1705 if (thegame.can_solve) {
1706 add_menu_separator(GTK_CONTAINER(menu));
1707 menuitem = gtk_menu_item_new_with_label("Solve");
1708 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1709 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1710 GTK_SIGNAL_FUNC(menu_solve_event), fe);
1711 gtk_widget_show(menuitem);
1713 add_menu_separator(GTK_CONTAINER(menu));
1714 add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Exit", 'q');
1716 menuitem = gtk_menu_item_new_with_label("Help");
1717 gtk_container_add(GTK_CONTAINER(menubar), menuitem);
1718 gtk_widget_show(menuitem);
1720 menu = gtk_menu_new();
1721 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
1723 menuitem = gtk_menu_item_new_with_label("About");
1724 gtk_container_add(GTK_CONTAINER(menu), menuitem);
1725 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
1726 GTK_SIGNAL_FUNC(menu_about_event), fe);
1727 gtk_widget_show(menuitem);
1734 fe->colmap = gdk_colormap_get_system();
1735 colours = midend_colours(fe->me, &ncolours);
1736 fe->ncolours = ncolours;
1737 fe->colours = snewn(ncolours, GdkColor);
1738 for (i = 0; i < ncolours; i++) {
1739 fe->colours[i].red = colours[i*3] * 0xFFFF;
1740 fe->colours[i].green = colours[i*3+1] * 0xFFFF;
1741 fe->colours[i].blue = colours[i*3+2] * 0xFFFF;
1743 success = snewn(ncolours, gboolean);
1744 gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours,
1745 FALSE, FALSE, success);
1746 for (i = 0; i < ncolours; i++) {
1748 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
1749 i, fe->colours[i].red >> 8,
1750 fe->colours[i].green >> 8,
1751 fe->colours[i].blue >> 8);
1755 if (midend_wants_statusbar(fe->me)) {
1756 GtkWidget *viewport;
1759 viewport = gtk_viewport_new(NULL, NULL);
1760 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
1761 fe->statusbar = gtk_statusbar_new();
1762 gtk_container_add(GTK_CONTAINER(viewport), fe->statusbar);
1763 gtk_widget_show(viewport);
1764 gtk_box_pack_end(vbox, viewport, FALSE, FALSE, 0);
1765 gtk_widget_show(fe->statusbar);
1766 fe->statusctx = gtk_statusbar_get_context_id
1767 (GTK_STATUSBAR(fe->statusbar), "game");
1768 gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx,
1770 gtk_widget_size_request(fe->statusbar, &req);
1772 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
1774 gtk_widget_set_usize(viewport, -1, req.height);
1776 fe->statusbar = NULL;
1778 fe->area = gtk_drawing_area_new();
1779 get_size(fe, &x, &y);
1780 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
1784 gtk_box_pack_end(vbox, fe->area, TRUE, TRUE, 0);
1788 fe->nfonts = fe->fontsize = 0;
1790 fe->paste_data = NULL;
1791 fe->paste_data_len = 0;
1793 gtk_signal_connect(GTK_OBJECT(fe->window), "destroy",
1794 GTK_SIGNAL_FUNC(destroy), fe);
1795 gtk_signal_connect(GTK_OBJECT(fe->window), "key_press_event",
1796 GTK_SIGNAL_FUNC(key_event), fe);
1797 gtk_signal_connect(GTK_OBJECT(fe->area), "button_press_event",
1798 GTK_SIGNAL_FUNC(button_event), fe);
1799 gtk_signal_connect(GTK_OBJECT(fe->area), "button_release_event",
1800 GTK_SIGNAL_FUNC(button_event), fe);
1801 gtk_signal_connect(GTK_OBJECT(fe->area), "motion_notify_event",
1802 GTK_SIGNAL_FUNC(motion_event), fe);
1803 gtk_signal_connect(GTK_OBJECT(fe->area), "selection_get",
1804 GTK_SIGNAL_FUNC(selection_get), fe);
1805 gtk_signal_connect(GTK_OBJECT(fe->area), "selection_clear_event",
1806 GTK_SIGNAL_FUNC(selection_clear), fe);
1807 gtk_signal_connect(GTK_OBJECT(fe->area), "expose_event",
1808 GTK_SIGNAL_FUNC(expose_area), fe);
1809 gtk_signal_connect(GTK_OBJECT(fe->window), "map_event",
1810 GTK_SIGNAL_FUNC(map_window), fe);
1811 gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event",
1812 GTK_SIGNAL_FUNC(configure_area), fe);
1814 gtk_widget_add_events(GTK_WIDGET(fe->area),
1815 GDK_BUTTON_PRESS_MASK |
1816 GDK_BUTTON_RELEASE_MASK |
1817 GDK_BUTTON_MOTION_MASK);
1820 gtk_widget_realize(fe->window);
1821 iconpm = gdk_pixmap_create_from_xpm_d(fe->window->window, NULL,
1822 NULL, (gchar **)xpm_icons[0]);
1823 gdk_window_set_icon(fe->window->window, NULL, iconpm, NULL);
1825 for (n = 0; n < n_xpm_icons; n++) {
1827 g_list_append(iconlist,
1828 gdk_pixbuf_new_from_xpm_data((const gchar **)
1831 gdk_window_set_icon_list(fe->window->window, iconlist);
1834 gtk_widget_show(fe->area);
1835 gtk_widget_show(fe->window);
1838 * Now that we've established the preferred size of the window,
1839 * reduce the drawing area's size request so the user can shrink
1842 gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), 1, 1);
1844 gdk_window_set_background(fe->area->window, &fe->colours[0]);
1845 gdk_window_set_background(fe->window->window, &fe->colours[0]);
1850 char *fgetline(FILE *fp)
1852 char *ret = snewn(512, char);
1853 int size = 512, len = 0;
1854 while (fgets(ret + len, size - len, fp)) {
1855 len += strlen(ret + len);
1856 if (ret[len-1] == '\n')
1857 break; /* got a newline, we're done */
1859 ret = sresize(ret, size, char);
1861 if (len == 0) { /* first fgets returned NULL */
1869 int main(int argc, char **argv)
1871 char *pname = argv[0];
1873 int ngenerate = 0, print = FALSE, px = 1, py = 1;
1874 int soln = FALSE, colour = FALSE;
1876 float redo_proportion = 0.0F;
1878 int argtype = ARG_EITHER;
1879 char *screenshot_file = NULL;
1880 int doing_opts = TRUE;
1886 * Command line parsing in this function is rather fiddly,
1887 * because GTK wants to have a go at argc/argv _first_ - and
1888 * yet we can't let it, because gtk_init() will bomb out if it
1889 * can't open an X display, whereas in fact we want to permit
1890 * our --generate and --print modes to run without an X
1894 * - we parse the command line ourselves, without modifying
1896 * - if we encounter an error which might plausibly be the
1897 * result of a GTK command line (i.e. not detailed errors in
1898 * particular options of ours) we store the error message
1899 * and terminate parsing.
1900 * - if we got enough out of the command line to know it
1901 * specifies a non-X mode of operation, we either display
1902 * the stored error and return failure, or if there is no
1903 * stored error we do the non-X operation and return
1905 * - otherwise, we go straight to gtk_init().
1911 if (doing_opts && !strcmp(p, "--version")) {
1912 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
1915 } else if (doing_opts && !strcmp(p, "--generate")) {
1917 ngenerate = atoi(*++av);
1919 fprintf(stderr, "%s: '--generate' expected a number\n",
1925 } else if (doing_opts && !strcmp(p, "--print")) {
1926 if (!thegame.can_print) {
1927 fprintf(stderr, "%s: this game does not support printing\n",
1934 if (sscanf(dim, "%dx%d", &px, &py) != 2) {
1935 fprintf(stderr, "%s: unable to parse argument '%s' to "
1936 "'--print'\n", pname, dim);
1942 } else if (doing_opts && !strcmp(p, "--scale")) {
1944 scale = atof(*++av);
1946 fprintf(stderr, "%s: no argument supplied to '--scale'\n",
1950 } else if (doing_opts && !strcmp(p, "--redo")) {
1952 * This is an internal option which I don't expect
1953 * users to have any particular use for. The effect of
1954 * --redo is that once the game has been loaded and
1955 * initialised, the next move in the redo chain is
1956 * replayed, and the game screen is redrawn part way
1957 * through the making of the move. This is only
1958 * meaningful if there _is_ a next move in the redo
1959 * chain, which means in turn that this option is only
1960 * useful if you're also passing a save file on the
1963 * This option is used by the script which generates
1964 * the puzzle icons and website screenshots, and I
1965 * don't imagine it's useful for anything else.
1966 * (Unless, I suppose, users don't like my screenshots
1967 * and want to generate their own in the same way for
1968 * some repackaged version of the puzzles.)
1971 redo_proportion = atof(*++av);
1973 fprintf(stderr, "%s: no argument supplied to '--redo'\n",
1977 } else if (doing_opts && !strcmp(p, "--screenshot")) {
1979 * Another internal option for the icon building
1980 * script. This causes a screenshot of the central
1981 * drawing area (i.e. not including the menu bar or
1982 * status bar) to be saved to a PNG file once the
1983 * window has been drawn, and then the application
1984 * quits immediately.
1987 screenshot_file = *++av;
1989 fprintf(stderr, "%s: no argument supplied to '--screenshot'\n",
1993 } else if (doing_opts && (!strcmp(p, "--with-solutions") ||
1994 !strcmp(p, "--with-solution") ||
1995 !strcmp(p, "--with-solns") ||
1996 !strcmp(p, "--with-soln") ||
1997 !strcmp(p, "--solutions") ||
1998 !strcmp(p, "--solution") ||
1999 !strcmp(p, "--solns") ||
2000 !strcmp(p, "--soln"))) {
2002 } else if (doing_opts && !strcmp(p, "--colour")) {
2003 if (!thegame.can_print_in_colour) {
2004 fprintf(stderr, "%s: this game does not support colour"
2005 " printing\n", pname);
2009 } else if (doing_opts && !strcmp(p, "--load")) {
2011 } else if (doing_opts && !strcmp(p, "--game")) {
2013 } else if (doing_opts && !strcmp(p, "--")) {
2015 } else if (!doing_opts || p[0] != '-') {
2017 fprintf(stderr, "%s: more than one argument supplied\n",
2023 sprintf(errbuf, "%.100s: unrecognised option '%.100s'\n",
2030 fputs(errbuf, stderr);
2035 * Special standalone mode for generating puzzle IDs on the
2036 * command line. Useful for generating puzzles to be printed
2037 * out and solved offline (for puzzles where that even makes
2038 * sense - Solo, for example, is a lot more pencil-and-paper
2039 * friendly than Twiddle!)
2043 * <puzzle-name> --generate [<n> [<params>]]
2045 * <n>, if present, is the number of puzzle IDs to generate.
2046 * <params>, if present, is the same type of parameter string
2047 * you would pass to the puzzle when running it in GUI mode,
2048 * including optional extras such as the expansion factor in
2049 * Rectangles and the difficulty level in Solo.
2051 * If you specify <params>, you must also specify <n> (although
2052 * you may specify it to be 1). Sorry; that was the
2053 * simplest-to-parse command-line syntax I came up with.
2055 if (ngenerate > 0 || print) {
2059 document *doc = NULL;
2063 me = midend_new(NULL, &thegame, NULL, NULL);
2067 doc = document_new(px, py, scale);
2070 * In this loop, we either generate a game ID or read one
2071 * from stdin depending on whether we're in generate mode;
2072 * then we either write it to stdout or print it, depending
2073 * on whether we're in print mode. Thus, this loop handles
2074 * generate-to-stdout, print-from-stdin and generate-and-
2075 * immediately-print modes.
2077 * (It could also handle a copy-stdin-to-stdout mode,
2078 * although there's currently no combination of options
2079 * which will cause this loop to be activated in that mode.
2080 * It wouldn't be _entirely_ pointless, though, because
2081 * stdin could contain bare params strings or random-seed
2082 * IDs, and stdout would contain nothing but fully
2083 * generated descriptive game IDs.)
2085 while (ngenerate == 0 || i < n) {
2088 if (ngenerate == 0) {
2089 pstr = fgetline(stdin);
2092 pstr[strcspn(pstr, "\r\n")] = '\0';
2095 pstr = snewn(strlen(arg) + 40, char);
2098 if (i > 0 && strchr(arg, '#'))
2099 sprintf(pstr + strlen(pstr), "-%d", i);
2105 err = midend_game_id(me, pstr);
2107 fprintf(stderr, "%s: error parsing '%s': %s\n",
2114 midend_new_game(me);
2117 err = midend_print_puzzle(me, doc, soln);
2119 fprintf(stderr, "%s: error in printing: %s\n", pname, err);
2123 id = midend_get_game_id(me);
2132 psdata *ps = ps_init(stdout, colour);
2133 document_print(doc, ps_drawing_api(ps));
2144 gtk_init(&argc, &argv);
2146 fe = new_window(arg, argtype, &error);
2149 fprintf(stderr, "%s: %s\n", pname, error);
2153 if (screenshot_file) {
2155 * Some puzzles will not redraw their entire area if
2156 * given a partially completed animation, which means
2157 * we must redraw now and _then_ redraw again after
2158 * freezing the move timer.
2160 midend_force_redraw(fe->me);
2163 if (redo_proportion) {
2165 midend_process_key(fe->me, 0, 0, 'r');
2166 /* And freeze the timer at the specified position. */
2167 midend_freeze_timer(fe->me, redo_proportion);
2170 if (screenshot_file) {
2172 GError *gerror = NULL;
2174 midend_redraw(fe->me);
2176 pb = gdk_pixbuf_get_from_drawable(NULL, fe->pixmap,
2177 NULL, 0, 0, 0, 0, -1, -1);
2178 gdk_pixbuf_save(pb, screenshot_file, "png", &gerror, NULL);