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)
29 # ifdef PANGO_VERSION_CHECK
30 # if PANGO_VERSION_CHECK(1,8,0)
31 # define HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
35 #if !GTK_CHECK_VERSION(2,4,0)
40 static FILE *debug_fp
= NULL
;
45 debug_fp
= fopen("debug.log", "w");
56 void debug_printf(char *fmt
, ...)
62 vsprintf(buf
, fmt
, ap
);
68 /* ----------------------------------------------------------------------
69 * Error reporting functions used elsewhere.
72 void fatal(char *fmt
, ...)
76 fprintf(stderr
, "fatal error: ");
79 vfprintf(stderr
, fmt
, ap
);
82 fprintf(stderr
, "\n");
86 /* ----------------------------------------------------------------------
87 * GTK front end to puzzles.
90 static void changed_preset(frontend
*fe
);
94 PangoFontDescription
*desc
;
103 * This structure holds all the data relevant to a single window.
104 * In principle this would allow us to open multiple independent
105 * puzzle windows, although I can't currently see any real point in
106 * doing so. I'm just coding cleanly because there's no
107 * particularly good reason not to.
111 GtkAccelGroup
*accelgroup
;
113 GtkWidget
*statusbar
;
122 int bbox_l
, bbox_r
, bbox_u
, bbox_d
;
123 int timer_active
, timer_id
;
124 struct timeval last_time
;
126 int nfonts
, fontsize
;
128 int cfg_which
, cfgret
;
132 int pw
, ph
; /* pixmap size (w, h are area size) */
133 int ox
, oy
; /* offset of pixmap in drawing area */
137 GSList
*preset_radio
;
138 int n_preset_menu_items
;
140 GtkWidget
*preset_custom
;
141 GtkWidget
*copy_menu_item
;
144 void get_random_seed(void **randseed
, int *randseedsize
)
146 struct timeval
*tvp
= snew(struct timeval
);
147 gettimeofday(tvp
, NULL
);
148 *randseed
= (void *)tvp
;
149 *randseedsize
= sizeof(struct timeval
);
152 void frontend_default_colour(frontend
*fe
, float *output
)
154 GdkColor col
= fe
->window
->style
->bg
[GTK_STATE_NORMAL
];
155 output
[0] = col
.red
/ 65535.0;
156 output
[1] = col
.green
/ 65535.0;
157 output
[2] = col
.blue
/ 65535.0;
160 void gtk_status_bar(void *handle
, char *text
)
162 frontend
*fe
= (frontend
*)handle
;
164 assert(fe
->statusbar
);
166 gtk_statusbar_pop(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
);
167 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
, text
);
170 void gtk_start_draw(void *handle
)
172 frontend
*fe
= (frontend
*)handle
;
173 fe
->gc
= gdk_gc_new(fe
->area
->window
);
180 void gtk_clip(void *handle
, int x
, int y
, int w
, int h
)
182 frontend
*fe
= (frontend
*)handle
;
190 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
193 void gtk_unclip(void *handle
)
195 frontend
*fe
= (frontend
*)handle
;
203 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
206 void gtk_draw_text(void *handle
, int x
, int y
, int fonttype
, int fontsize
,
207 int align
, int colour
, char *text
)
209 frontend
*fe
= (frontend
*)handle
;
213 * Find or create the font.
215 for (i
= 0; i
< fe
->nfonts
; i
++)
216 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
219 if (i
== fe
->nfonts
) {
220 if (fe
->fontsize
<= fe
->nfonts
) {
221 fe
->fontsize
= fe
->nfonts
+ 10;
222 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
227 fe
->fonts
[i
].type
= fonttype
;
228 fe
->fonts
[i
].size
= fontsize
;
232 * Use Pango to find the closest match to the requested
236 PangoFontDescription
*fd
;
238 fd
= pango_font_description_new();
239 /* `Monospace' and `Sans' are meta-families guaranteed to exist */
240 pango_font_description_set_family(fd
, fonttype
== FONT_FIXED ?
241 "Monospace" : "Sans");
242 pango_font_description_set_weight(fd
, PANGO_WEIGHT_BOLD
);
244 * I found some online Pango documentation which
245 * described a function called
246 * pango_font_description_set_absolute_size(), which is
247 * _exactly_ what I want here. Unfortunately, none of
248 * my local Pango installations have it (presumably
249 * they're too old), so I'm going to have to hack round
250 * it by figuring out the point size myself. This
251 * limits me to X and probably also breaks in later
252 * Pango installations, so ideally I should add another
253 * CHECK_VERSION type ifdef and use set_absolute_size
254 * where available. All very annoying.
256 #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
257 pango_font_description_set_absolute_size(fd
, PANGO_SCALE
*fontsize
);
260 Display
*d
= GDK_DISPLAY();
261 int s
= DefaultScreen(d
);
263 (PANGO_SCALE
* 72.27 / 25.4) *
264 ((double) DisplayWidthMM(d
, s
) / DisplayWidth (d
, s
));
265 pango_font_description_set_size(fd
, resolution
* fontsize
);
268 fe
->fonts
[i
].desc
= fd
;
273 * In GTK 1.2, I don't know of any plausible way to
274 * pick a suitable font, so I'm just going to be
277 fe
->fonts
[i
].font
= gdk_font_load(fonttype
== FONT_FIXED ?
278 "fixed" : "variable");
286 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
297 layout
= pango_layout_new(gtk_widget_get_pango_context(fe
->area
));
298 pango_layout_set_font_description(layout
, fe
->fonts
[i
].desc
);
299 pango_layout_set_text(layout
, text
, strlen(text
));
300 pango_layout_get_pixel_extents(layout
, NULL
, &rect
);
302 if (align
& ALIGN_VCENTRE
)
303 rect
.y
-= rect
.height
/ 2;
305 rect
.y
-= rect
.height
;
307 if (align
& ALIGN_HCENTRE
)
308 rect
.x
-= rect
.width
/ 2;
309 else if (align
& ALIGN_HRIGHT
)
310 rect
.x
-= rect
.width
;
312 gdk_draw_layout(fe
->pixmap
, fe
->gc
, rect
.x
+ x
, rect
.y
+ y
, layout
);
314 g_object_unref(layout
);
319 * Find string dimensions and process alignment.
322 int lb
, rb
, wid
, asc
, desc
;
325 * Measure vertical string extents with respect to the same
328 gdk_string_extents(fe
->fonts
[i
].font
,
329 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
330 &lb
, &rb
, &wid
, &asc
, &desc
);
331 if (align
& ALIGN_VCENTRE
)
332 y
+= asc
- (asc
+desc
)/2;
337 * ... but horizontal extents with respect to the provided
338 * string. This means that multiple pieces of text centred
339 * on the same y-coordinate don't have different baselines.
341 gdk_string_extents(fe
->fonts
[i
].font
, text
,
342 &lb
, &rb
, &wid
, &asc
, &desc
);
344 if (align
& ALIGN_HCENTRE
)
346 else if (align
& ALIGN_HRIGHT
)
352 * Actually draw the text.
354 gdk_draw_string(fe
->pixmap
, fe
->fonts
[i
].font
, fe
->gc
, x
, y
, text
);
359 void gtk_draw_rect(void *handle
, int x
, int y
, int w
, int h
, int colour
)
361 frontend
*fe
= (frontend
*)handle
;
362 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
363 gdk_draw_rectangle(fe
->pixmap
, fe
->gc
, 1, x
, y
, w
, h
);
366 void gtk_draw_line(void *handle
, int x1
, int y1
, int x2
, int y2
, int colour
)
368 frontend
*fe
= (frontend
*)handle
;
369 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
370 gdk_draw_line(fe
->pixmap
, fe
->gc
, x1
, y1
, x2
, y2
);
373 void gtk_draw_poly(void *handle
, int *coords
, int npoints
,
374 int fillcolour
, int outlinecolour
)
376 frontend
*fe
= (frontend
*)handle
;
377 GdkPoint
*points
= snewn(npoints
, GdkPoint
);
380 for (i
= 0; i
< npoints
; i
++) {
381 points
[i
].x
= coords
[i
*2];
382 points
[i
].y
= coords
[i
*2+1];
385 if (fillcolour
>= 0) {
386 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[fillcolour
]);
387 gdk_draw_polygon(fe
->pixmap
, fe
->gc
, TRUE
, points
, npoints
);
389 assert(outlinecolour
>= 0);
390 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[outlinecolour
]);
393 * In principle we ought to be able to use gdk_draw_polygon for
394 * the outline as well. In fact, it turns out to interact badly
395 * with a clipping region, for no terribly obvious reason, so I
396 * draw the outline as a sequence of lines instead.
398 for (i
= 0; i
< npoints
; i
++)
399 gdk_draw_line(fe
->pixmap
, fe
->gc
,
400 points
[i
].x
, points
[i
].y
,
401 points
[(i
+1)%npoints
].x
, points
[(i
+1)%npoints
].y
);
406 void gtk_draw_circle(void *handle
, int cx
, int cy
, int radius
,
407 int fillcolour
, int outlinecolour
)
409 frontend
*fe
= (frontend
*)handle
;
410 if (fillcolour
>= 0) {
411 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[fillcolour
]);
412 gdk_draw_arc(fe
->pixmap
, fe
->gc
, TRUE
,
413 cx
- radius
, cy
- radius
,
414 2 * radius
, 2 * radius
, 0, 360 * 64);
417 assert(outlinecolour
>= 0);
418 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[outlinecolour
]);
419 gdk_draw_arc(fe
->pixmap
, fe
->gc
, FALSE
,
420 cx
- radius
, cy
- radius
,
421 2 * radius
, 2 * radius
, 0, 360 * 64);
429 blitter
*gtk_blitter_new(void *handle
, int w
, int h
)
432 * We can't create the pixmap right now, because fe->window
433 * might not yet exist. So we just cache w and h and create it
434 * during the firs call to blitter_save.
436 blitter
*bl
= snew(blitter
);
443 void gtk_blitter_free(void *handle
, blitter
*bl
)
446 gdk_pixmap_unref(bl
->pixmap
);
450 void gtk_blitter_save(void *handle
, blitter
*bl
, int x
, int y
)
452 frontend
*fe
= (frontend
*)handle
;
454 bl
->pixmap
= gdk_pixmap_new(fe
->area
->window
, bl
->w
, bl
->h
, -1);
457 gdk_draw_pixmap(bl
->pixmap
,
458 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
460 x
, y
, 0, 0, bl
->w
, bl
->h
);
463 void gtk_blitter_load(void *handle
, blitter
*bl
, int x
, int y
)
465 frontend
*fe
= (frontend
*)handle
;
467 if (x
== BLITTER_FROMSAVED
&& y
== BLITTER_FROMSAVED
) {
471 gdk_draw_pixmap(fe
->pixmap
,
472 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
474 0, 0, x
, y
, bl
->w
, bl
->h
);
477 void gtk_draw_update(void *handle
, int x
, int y
, int w
, int h
)
479 frontend
*fe
= (frontend
*)handle
;
480 if (fe
->bbox_l
> x
) fe
->bbox_l
= x
;
481 if (fe
->bbox_r
< x
+w
) fe
->bbox_r
= x
+w
;
482 if (fe
->bbox_u
> y
) fe
->bbox_u
= y
;
483 if (fe
->bbox_d
< y
+h
) fe
->bbox_d
= y
+h
;
486 void gtk_end_draw(void *handle
)
488 frontend
*fe
= (frontend
*)handle
;
489 gdk_gc_unref(fe
->gc
);
492 if (fe
->bbox_l
< fe
->bbox_r
&& fe
->bbox_u
< fe
->bbox_d
) {
493 gdk_draw_pixmap(fe
->area
->window
,
494 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
496 fe
->bbox_l
, fe
->bbox_u
,
497 fe
->ox
+ fe
->bbox_l
, fe
->oy
+ fe
->bbox_u
,
498 fe
->bbox_r
- fe
->bbox_l
, fe
->bbox_d
- fe
->bbox_u
);
503 char *gtk_text_fallback(void *handle
, const char *const *strings
, int nstrings
)
506 * We assume Pango can cope with any UTF-8 likely to be emitted
509 return dupstr(strings
[0]);
513 const struct drawing_api gtk_drawing
= {
529 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, /* {begin,end}_{doc,page,puzzle} */
530 NULL
, NULL
, /* line_width, line_dotted */
538 static void destroy(GtkWidget
*widget
, gpointer data
)
540 frontend
*fe
= (frontend
*)data
;
541 deactivate_timer(fe
);
546 static gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
548 frontend
*fe
= (frontend
*)data
;
550 int shift
= (event
->state
& GDK_SHIFT_MASK
) ? MOD_SHFT
: 0;
551 int ctrl
= (event
->state
& GDK_CONTROL_MASK
) ? MOD_CTRL
: 0;
556 #if !GTK_CHECK_VERSION(2,0,0)
557 /* Gtk 1.2 passes a key event to this function even if it's also
558 * defined as an accelerator.
559 * Gtk 2 doesn't do this, and this function appears not to exist there. */
560 if (fe
->accelgroup
&&
561 gtk_accel_group_get_entry(fe
->accelgroup
,
562 event
->keyval
, event
->state
))
566 /* Handle mnemonics. */
567 if (gtk_window_activate_key(GTK_WINDOW(fe
->window
), event
))
570 if (event
->keyval
== GDK_Up
)
571 keyval
= shift
| ctrl
| CURSOR_UP
;
572 else if (event
->keyval
== GDK_KP_Up
|| event
->keyval
== GDK_KP_8
)
573 keyval
= MOD_NUM_KEYPAD
| '8';
574 else if (event
->keyval
== GDK_Down
)
575 keyval
= shift
| ctrl
| CURSOR_DOWN
;
576 else if (event
->keyval
== GDK_KP_Down
|| event
->keyval
== GDK_KP_2
)
577 keyval
= MOD_NUM_KEYPAD
| '2';
578 else if (event
->keyval
== GDK_Left
)
579 keyval
= shift
| ctrl
| CURSOR_LEFT
;
580 else if (event
->keyval
== GDK_KP_Left
|| event
->keyval
== GDK_KP_4
)
581 keyval
= MOD_NUM_KEYPAD
| '4';
582 else if (event
->keyval
== GDK_Right
)
583 keyval
= shift
| ctrl
| CURSOR_RIGHT
;
584 else if (event
->keyval
== GDK_KP_Right
|| event
->keyval
== GDK_KP_6
)
585 keyval
= MOD_NUM_KEYPAD
| '6';
586 else if (event
->keyval
== GDK_KP_Home
|| event
->keyval
== GDK_KP_7
)
587 keyval
= MOD_NUM_KEYPAD
| '7';
588 else if (event
->keyval
== GDK_KP_End
|| event
->keyval
== GDK_KP_1
)
589 keyval
= MOD_NUM_KEYPAD
| '1';
590 else if (event
->keyval
== GDK_KP_Page_Up
|| event
->keyval
== GDK_KP_9
)
591 keyval
= MOD_NUM_KEYPAD
| '9';
592 else if (event
->keyval
== GDK_KP_Page_Down
|| event
->keyval
== GDK_KP_3
)
593 keyval
= MOD_NUM_KEYPAD
| '3';
594 else if (event
->keyval
== GDK_KP_Insert
|| event
->keyval
== GDK_KP_0
)
595 keyval
= MOD_NUM_KEYPAD
| '0';
596 else if (event
->keyval
== GDK_KP_Begin
|| event
->keyval
== GDK_KP_5
)
597 keyval
= MOD_NUM_KEYPAD
| '5';
598 else if (event
->keyval
== GDK_BackSpace
||
599 event
->keyval
== GDK_Delete
||
600 event
->keyval
== GDK_KP_Delete
)
602 else if (event
->string
[0] && !event
->string
[1])
603 keyval
= (unsigned char)event
->string
[0];
608 !midend_process_key(fe
->me
, 0, 0, keyval
))
609 gtk_widget_destroy(fe
->window
);
614 static gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
,
617 frontend
*fe
= (frontend
*)data
;
623 if (event
->type
!= GDK_BUTTON_PRESS
&& event
->type
!= GDK_BUTTON_RELEASE
)
626 if (event
->button
== 2 || (event
->state
& GDK_SHIFT_MASK
))
627 button
= MIDDLE_BUTTON
;
628 else if (event
->button
== 3 || (event
->state
& GDK_MOD1_MASK
))
629 button
= RIGHT_BUTTON
;
630 else if (event
->button
== 1)
631 button
= LEFT_BUTTON
;
633 return FALSE
; /* don't even know what button! */
635 if (event
->type
== GDK_BUTTON_RELEASE
)
636 button
+= LEFT_RELEASE
- LEFT_BUTTON
;
638 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
639 event
->y
- fe
->oy
, button
))
640 gtk_widget_destroy(fe
->window
);
645 static gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
,
648 frontend
*fe
= (frontend
*)data
;
654 if (event
->state
& (GDK_BUTTON2_MASK
| GDK_SHIFT_MASK
))
655 button
= MIDDLE_DRAG
;
656 else if (event
->state
& GDK_BUTTON1_MASK
)
658 else if (event
->state
& GDK_BUTTON3_MASK
)
661 return FALSE
; /* don't even know what button! */
663 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
664 event
->y
- fe
->oy
, button
))
665 gtk_widget_destroy(fe
->window
);
666 #if GTK_CHECK_VERSION(2,12,0)
667 gdk_event_request_motions(event
);
669 gdk_window_get_pointer(widget
->window
, NULL
, NULL
, NULL
);
675 static gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
,
678 frontend
*fe
= (frontend
*)data
;
681 gdk_draw_pixmap(widget
->window
,
682 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
684 event
->area
.x
- fe
->ox
, event
->area
.y
- fe
->oy
,
685 event
->area
.x
, event
->area
.y
,
686 event
->area
.width
, event
->area
.height
);
691 static gint
map_window(GtkWidget
*widget
, GdkEvent
*event
,
694 frontend
*fe
= (frontend
*)data
;
697 * Apparently we need to do this because otherwise the status
698 * bar will fail to update immediately. Annoying, but there we
701 gtk_widget_queue_draw(fe
->window
);
706 static gint
configure_area(GtkWidget
*widget
,
707 GdkEventConfigure
*event
, gpointer data
)
709 frontend
*fe
= (frontend
*)data
;
714 gdk_pixmap_unref(fe
->pixmap
);
716 x
= fe
->w
= event
->width
;
717 y
= fe
->h
= event
->height
;
718 midend_size(fe
->me
, &x
, &y
, TRUE
);
721 fe
->ox
= (fe
->w
- fe
->pw
) / 2;
722 fe
->oy
= (fe
->h
- fe
->ph
) / 2;
724 fe
->pixmap
= gdk_pixmap_new(widget
->window
, fe
->pw
, fe
->ph
, -1);
726 gc
= gdk_gc_new(fe
->area
->window
);
727 gdk_gc_set_foreground(gc
, &fe
->colours
[0]);
728 gdk_draw_rectangle(fe
->pixmap
, gc
, 1, 0, 0, fe
->pw
, fe
->ph
);
729 gdk_draw_rectangle(widget
->window
, gc
, 1, 0, 0,
730 event
->width
, event
->height
);
733 midend_force_redraw(fe
->me
);
738 static gint
timer_func(gpointer data
)
740 frontend
*fe
= (frontend
*)data
;
742 if (fe
->timer_active
) {
745 gettimeofday(&now
, NULL
);
746 elapsed
= ((now
.tv_usec
- fe
->last_time
.tv_usec
) * 0.000001F
+
747 (now
.tv_sec
- fe
->last_time
.tv_sec
));
748 midend_timer(fe
->me
, elapsed
); /* may clear timer_active */
752 return fe
->timer_active
;
755 void deactivate_timer(frontend
*fe
)
758 return; /* can happen due to --generate */
759 if (fe
->timer_active
)
760 gtk_timeout_remove(fe
->timer_id
);
761 fe
->timer_active
= FALSE
;
764 void activate_timer(frontend
*fe
)
767 return; /* can happen due to --generate */
768 if (!fe
->timer_active
) {
769 fe
->timer_id
= gtk_timeout_add(20, timer_func
, fe
);
770 gettimeofday(&fe
->last_time
, NULL
);
772 fe
->timer_active
= TRUE
;
775 static void window_destroy(GtkWidget
*widget
, gpointer data
)
780 static void msgbox_button_clicked(GtkButton
*button
, gpointer data
)
782 GtkWidget
*window
= GTK_WIDGET(data
);
785 ip
= (int *)gtk_object_get_data(GTK_OBJECT(window
), "user-data");
786 v
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button
), "user-data"));
789 gtk_widget_destroy(GTK_WIDGET(data
));
792 static int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
794 GtkObject
*cancelbutton
= GTK_OBJECT(data
);
797 * `Escape' effectively clicks the cancel button
799 if (event
->keyval
== GDK_Escape
) {
800 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton
), "clicked");
807 enum { MB_OK
, MB_YESNO
};
809 int message_box(GtkWidget
*parent
, char *title
, char *msg
, int centre
,
812 GtkWidget
*window
, *hbox
, *text
, *button
;
816 window
= gtk_dialog_new();
817 text
= gtk_label_new(msg
);
818 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
819 hbox
= gtk_hbox_new(FALSE
, 0);
820 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
821 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
822 hbox
, FALSE
, FALSE
, 20);
823 gtk_widget_show(text
);
824 gtk_widget_show(hbox
);
825 gtk_window_set_title(GTK_WINDOW(window
), title
);
826 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
832 assert(type
== MB_YESNO
);
833 titles
= "Yes\0No\0";
840 button
= gtk_button_new_with_label(titles
);
841 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
842 button
, FALSE
, FALSE
, 0);
843 gtk_widget_show(button
);
845 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
846 gtk_window_set_default(GTK_WINDOW(window
), button
);
849 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
850 GTK_SIGNAL_FUNC(win_key_press
), button
);
852 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
853 GTK_SIGNAL_FUNC(msgbox_button_clicked
), window
);
854 gtk_object_set_data(GTK_OBJECT(button
), "user-data",
856 titles
+= strlen(titles
)+1;
859 gtk_object_set_data(GTK_OBJECT(window
), "user-data",
860 GINT_TO_POINTER(&i
));
861 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
862 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
863 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
864 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(parent
));
865 /* set_transient_window_pos(parent, window); */
866 gtk_widget_show(window
);
869 return (type
== MB_YESNO ? i
== 0 : TRUE
);
872 void error_box(GtkWidget
*parent
, char *msg
)
874 message_box(parent
, "Error", msg
, FALSE
, MB_OK
);
877 static void config_ok_button_clicked(GtkButton
*button
, gpointer data
)
879 frontend
*fe
= (frontend
*)data
;
882 err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
885 error_box(fe
->cfgbox
, err
);
888 gtk_widget_destroy(fe
->cfgbox
);
893 static void config_cancel_button_clicked(GtkButton
*button
, gpointer data
)
895 frontend
*fe
= (frontend
*)data
;
897 gtk_widget_destroy(fe
->cfgbox
);
900 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
903 * GtkEntry has a nasty habit of eating the Return key, which
904 * is unhelpful since it doesn't actually _do_ anything with it
905 * (it calls gtk_widget_activate, but our edit boxes never need
906 * activating). So I catch Return before GtkEntry sees it, and
907 * pass it straight on to the parent widget. Effect: hitting
908 * Return in an edit box will now activate the default button
909 * in the dialog just like it will everywhere else.
911 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
913 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
914 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
921 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
923 config_item
*i
= (config_item
*)data
;
926 i
->sval
= dupstr(gtk_entry_get_text(GTK_ENTRY(ed
)));
929 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
931 config_item
*i
= (config_item
*)data
;
933 i
->ival
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb
));
936 static void droplist_sel(GtkMenuItem
*item
, gpointer data
)
938 config_item
*i
= (config_item
*)data
;
940 i
->ival
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
),
944 static int get_config(frontend
*fe
, int which
)
946 GtkWidget
*w
, *table
, *cancel
;
951 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
952 fe
->cfg_which
= which
;
955 fe
->cfgbox
= gtk_dialog_new();
956 gtk_window_set_title(GTK_WINDOW(fe
->cfgbox
), title
);
959 w
= gtk_button_new_with_label("OK");
960 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
963 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
964 gtk_window_set_default(GTK_WINDOW(fe
->cfgbox
), w
);
965 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
966 GTK_SIGNAL_FUNC(config_ok_button_clicked
), fe
);
968 w
= gtk_button_new_with_label("Cancel");
969 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
972 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
973 GTK_SIGNAL_FUNC(config_cancel_button_clicked
), fe
);
976 table
= gtk_table_new(1, 2, FALSE
);
978 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->vbox
),
979 table
, FALSE
, FALSE
, 0);
980 gtk_widget_show(table
);
982 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
983 gtk_table_resize(GTK_TABLE(table
), y
+1, 2);
988 * Edit box with a label beside it.
991 w
= gtk_label_new(i
->name
);
992 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
993 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
994 GTK_SHRINK
| GTK_FILL
,
995 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1000 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
1001 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1002 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1004 gtk_entry_set_text(GTK_ENTRY(w
), i
->sval
);
1005 gtk_signal_connect(GTK_OBJECT(w
), "changed",
1006 GTK_SIGNAL_FUNC(editbox_changed
), i
);
1007 gtk_signal_connect(GTK_OBJECT(w
), "key_press_event",
1008 GTK_SIGNAL_FUNC(editbox_key
), NULL
);
1017 w
= gtk_check_button_new_with_label(i
->name
);
1018 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1019 GTK_SIGNAL_FUNC(button_toggled
), i
);
1020 gtk_table_attach(GTK_TABLE(table
), w
, 0, 2, y
, y
+1,
1021 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1022 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1024 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w
), i
->ival
);
1030 * Drop-down list (GtkOptionMenu).
1033 w
= gtk_label_new(i
->name
);
1034 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
1035 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
1036 GTK_SHRINK
| GTK_FILL
,
1037 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1041 w
= gtk_option_menu_new();
1042 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
1043 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1044 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1051 GtkWidget
*menuitem
;
1052 GtkWidget
*menu
= gtk_menu_new();
1054 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), menu
);
1062 while (*q
&& *q
!= c
)
1065 name
= snewn(q
-p
+1, char);
1066 strncpy(name
, p
, q
-p
);
1069 if (*q
) q
++; /* eat delimiter */
1071 menuitem
= gtk_menu_item_new_with_label(name
);
1072 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1073 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1074 GINT_TO_POINTER(val
));
1075 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1076 GTK_SIGNAL_FUNC(droplist_sel
), i
);
1077 gtk_widget_show(menuitem
);
1084 gtk_option_menu_set_history(GTK_OPTION_MENU(w
), i
->ival
);
1093 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "destroy",
1094 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1095 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "key_press_event",
1096 GTK_SIGNAL_FUNC(win_key_press
), cancel
);
1097 gtk_window_set_modal(GTK_WINDOW(fe
->cfgbox
), TRUE
);
1098 gtk_window_set_transient_for(GTK_WINDOW(fe
->cfgbox
),
1099 GTK_WINDOW(fe
->window
));
1100 /* set_transient_window_pos(fe->window, fe->cfgbox); */
1101 gtk_widget_show(fe
->cfgbox
);
1109 static void menu_key_event(GtkMenuItem
*menuitem
, gpointer data
)
1111 frontend
*fe
= (frontend
*)data
;
1112 int key
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1114 if (!midend_process_key(fe
->me
, 0, 0, key
))
1115 gtk_widget_destroy(fe
->window
);
1118 static void get_size(frontend
*fe
, int *px
, int *py
)
1123 * Currently I don't want to make the GTK port scale large
1124 * puzzles to fit on the screen. This is because X does permit
1125 * extremely large windows and many window managers provide a
1126 * means of navigating round them, and the users I consulted
1127 * before deciding said that they'd rather have enormous puzzle
1128 * windows spanning multiple screen pages than have them
1129 * shrunk. I could change my mind later or introduce
1130 * configurability; this would be the place to do so, by
1131 * replacing the initial values of x and y with the screen
1136 midend_size(fe
->me
, &x
, &y
, FALSE
);
1141 #if !GTK_CHECK_VERSION(2,0,0)
1142 #define gtk_window_resize(win, x, y) \
1143 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
1147 * Called when any other code in this file has changed the
1148 * selected game parameters.
1150 static void changed_preset(frontend
*fe
)
1152 int n
= midend_which_preset(fe
->me
);
1154 fe
->preset_threaded
= TRUE
;
1155 if (n
< 0 && fe
->preset_custom
) {
1156 gtk_check_menu_item_set_active(
1157 GTK_CHECK_MENU_ITEM(fe
->preset_custom
),
1160 GSList
*gs
= fe
->preset_radio
;
1161 int i
= fe
->n_preset_menu_items
- 1 - n
;
1162 if (fe
->preset_custom
)
1169 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(gs
->data
),
1171 } else for (gs
= fe
->preset_radio
; gs
; gs
= gs
->next
) {
1172 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(gs
->data
),
1176 fe
->preset_threaded
= FALSE
;
1179 * Update the greying on the Copy menu option.
1181 if (fe
->copy_menu_item
) {
1182 int enabled
= midend_can_format_as_text_now(fe
->me
);
1183 gtk_widget_set_sensitive(fe
->copy_menu_item
, enabled
);
1187 static void resize_fe(frontend
*fe
)
1191 get_size(fe
, &x
, &y
);
1194 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1197 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1198 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1201 * Now that we've established the preferred size of the window,
1202 * reduce the drawing area's size request so the user can shrink
1205 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), 1, 1);
1208 static void menu_preset_event(GtkMenuItem
*menuitem
, gpointer data
)
1210 frontend
*fe
= (frontend
*)data
;
1211 game_params
*params
=
1212 (game_params
*)gtk_object_get_data(GTK_OBJECT(menuitem
), "user-data");
1214 if (fe
->preset_threaded
||
1215 (GTK_IS_CHECK_MENU_ITEM(menuitem
) &&
1216 !gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem
))))
1218 midend_set_params(fe
->me
, params
);
1219 midend_new_game(fe
->me
);
1224 GdkAtom compound_text_atom
, utf8_string_atom
;
1225 int paste_initialised
= FALSE
;
1229 unsigned char empty
[] = { 0 };
1231 if (paste_initialised
)
1234 if (!compound_text_atom
)
1235 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
1236 if (!utf8_string_atom
)
1237 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
1240 * Ensure that all the cut buffers exist - according to the
1241 * ICCCM, we must do this before we start using cut buffers.
1243 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1244 XA_CUT_BUFFER0
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1245 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1246 XA_CUT_BUFFER1
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1247 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1248 XA_CUT_BUFFER2
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1249 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1250 XA_CUT_BUFFER3
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1251 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1252 XA_CUT_BUFFER4
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1253 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1254 XA_CUT_BUFFER5
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1255 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1256 XA_CUT_BUFFER6
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1257 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1258 XA_CUT_BUFFER7
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1261 /* Store data in a cut-buffer. */
1262 void store_cutbuffer(char *ptr
, int len
)
1264 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1265 XRotateBuffers(GDK_DISPLAY(), 1);
1266 XStoreBytes(GDK_DISPLAY(), ptr
, len
);
1269 void write_clip(frontend
*fe
, char *data
)
1274 sfree(fe
->paste_data
);
1277 * For this simple application we can safely assume that the
1278 * data passed to this function is pure ASCII, which means we
1279 * can return precisely the same stuff for types STRING,
1280 * COMPOUND_TEXT or UTF8_STRING.
1283 fe
->paste_data
= data
;
1284 fe
->paste_data_len
= strlen(data
);
1286 store_cutbuffer(fe
->paste_data
, fe
->paste_data_len
);
1288 if (gtk_selection_owner_set(fe
->area
, GDK_SELECTION_PRIMARY
,
1290 gtk_selection_clear_targets(fe
->area
, GDK_SELECTION_PRIMARY
);
1291 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1292 GDK_SELECTION_TYPE_STRING
, 1);
1293 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1294 compound_text_atom
, 1);
1295 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1296 utf8_string_atom
, 1);
1300 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1301 guint info
, guint time_stamp
, gpointer data
)
1303 frontend
*fe
= (frontend
*)data
;
1304 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1305 fe
->paste_data
, fe
->paste_data_len
);
1308 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1311 frontend
*fe
= (frontend
*)data
;
1314 sfree(fe
->paste_data
);
1315 fe
->paste_data
= NULL
;
1316 fe
->paste_data_len
= 0;
1320 static void menu_copy_event(GtkMenuItem
*menuitem
, gpointer data
)
1322 frontend
*fe
= (frontend
*)data
;
1325 text
= midend_text_format(fe
->me
);
1328 write_clip(fe
, text
);
1336 static void filesel_ok(GtkButton
*button
, gpointer data
)
1338 frontend
*fe
= (frontend
*)data
;
1340 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1343 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1345 fe
->filesel_name
= dupstr(name
);
1348 static char *file_selector(frontend
*fe
, char *title
, int save
)
1350 GtkWidget
*filesel
=
1351 gtk_file_selection_new(title
);
1353 fe
->filesel_name
= NULL
;
1355 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1357 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1360 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1361 GTK_SIGNAL_FUNC(filesel_ok
), fe
);
1362 gtk_signal_connect_object
1363 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1364 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1365 gtk_signal_connect_object
1366 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1367 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1368 gtk_signal_connect(GTK_OBJECT(filesel
), "destroy",
1369 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1370 gtk_widget_show(filesel
);
1371 gtk_window_set_transient_for(GTK_WINDOW(filesel
), GTK_WINDOW(fe
->window
));
1374 return fe
->filesel_name
;
1379 static char *file_selector(frontend
*fe
, char *title
, int save
)
1381 char *filesel_name
= NULL
;
1383 GtkWidget
*filesel
=
1384 gtk_file_chooser_dialog_new(title
,
1385 GTK_WINDOW(fe
->window
),
1386 save ? GTK_FILE_CHOOSER_ACTION_SAVE
:
1387 GTK_FILE_CHOOSER_ACTION_OPEN
,
1388 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1389 save ? GTK_STOCK_SAVE
: GTK_STOCK_OPEN
,
1390 GTK_RESPONSE_ACCEPT
,
1393 if (gtk_dialog_run(GTK_DIALOG(filesel
)) == GTK_RESPONSE_ACCEPT
) {
1394 const char *name
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(filesel
));
1395 filesel_name
= dupstr(name
);
1398 gtk_widget_destroy(filesel
);
1400 return filesel_name
;
1405 struct savefile_write_ctx
{
1410 static void savefile_write(void *wctx
, void *buf
, int len
)
1412 struct savefile_write_ctx
*ctx
= (struct savefile_write_ctx
*)wctx
;
1413 if (fwrite(buf
, 1, len
, ctx
->fp
) < len
)
1417 static int savefile_read(void *wctx
, void *buf
, int len
)
1419 FILE *fp
= (FILE *)wctx
;
1422 ret
= fread(buf
, 1, len
, fp
);
1423 return (ret
== len
);
1426 static void menu_save_event(GtkMenuItem
*menuitem
, gpointer data
)
1428 frontend
*fe
= (frontend
*)data
;
1431 name
= file_selector(fe
, "Enter name of game file to save", TRUE
);
1436 if ((fp
= fopen(name
, "r")) != NULL
) {
1437 char buf
[256 + FILENAME_MAX
];
1441 sprintf(buf
, "Are you sure you want to overwrite the"
1443 FILENAME_MAX
, name
);
1444 if (!message_box(fe
->window
, "Question", buf
, TRUE
, MB_YESNO
))
1448 fp
= fopen(name
, "w");
1452 error_box(fe
->window
, "Unable to open save file");
1457 struct savefile_write_ctx ctx
;
1460 midend_serialise(fe
->me
, savefile_write
, &ctx
);
1464 sprintf(boxmsg
, "Error writing save file: %.400s",
1466 error_box(fe
->window
, boxmsg
);
1474 static void menu_load_event(GtkMenuItem
*menuitem
, gpointer data
)
1476 frontend
*fe
= (frontend
*)data
;
1479 name
= file_selector(fe
, "Enter name of saved game file to load", FALSE
);
1482 FILE *fp
= fopen(name
, "r");
1486 error_box(fe
->window
, "Unable to open saved game file");
1490 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1495 error_box(fe
->window
, err
);
1504 static void menu_solve_event(GtkMenuItem
*menuitem
, gpointer data
)
1506 frontend
*fe
= (frontend
*)data
;
1509 msg
= midend_solve(fe
->me
);
1512 error_box(fe
->window
, msg
);
1515 static void menu_restart_event(GtkMenuItem
*menuitem
, gpointer data
)
1517 frontend
*fe
= (frontend
*)data
;
1519 midend_restart_game(fe
->me
);
1522 static void menu_config_event(GtkMenuItem
*menuitem
, gpointer data
)
1524 frontend
*fe
= (frontend
*)data
;
1525 int which
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1528 if (fe
->preset_threaded
||
1529 (GTK_IS_CHECK_MENU_ITEM(menuitem
) &&
1530 !gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem
))))
1532 changed_preset(fe
); /* Put the old preset back! */
1533 if (!get_config(fe
, which
))
1536 midend_new_game(fe
->me
);
1540 static void menu_about_event(GtkMenuItem
*menuitem
, gpointer data
)
1542 frontend
*fe
= (frontend
*)data
;
1546 sprintf(titlebuf
, "About %.200s", thegame
.name
);
1549 "from Simon Tatham's Portable Puzzle Collection\n\n"
1550 "%.500s", thegame
.name
, ver
);
1552 message_box(fe
->window
, titlebuf
, textbuf
, TRUE
, MB_OK
);
1555 static GtkWidget
*add_menu_item_with_key(frontend
*fe
, GtkContainer
*cont
,
1556 char *text
, int key
)
1558 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
1560 gtk_container_add(cont
, menuitem
);
1561 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1562 GINT_TO_POINTER(key
));
1563 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1564 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
1565 switch (key
& ~0x1F) {
1568 keyqual
= GDK_CONTROL_MASK
;
1572 keyqual
= GDK_SHIFT_MASK
;
1578 gtk_widget_add_accelerator(menuitem
,
1579 "activate", fe
->accelgroup
,
1582 gtk_widget_show(menuitem
);
1586 static void add_menu_separator(GtkContainer
*cont
)
1588 GtkWidget
*menuitem
= gtk_menu_item_new();
1589 gtk_container_add(cont
, menuitem
);
1590 gtk_widget_show(menuitem
);
1593 enum { ARG_EITHER
, ARG_SAVE
, ARG_ID
}; /* for argtype */
1595 static frontend
*new_window(char *arg
, int argtype
, char **error
)
1598 GtkBox
*vbox
, *hbox
;
1599 GtkWidget
*menubar
, *menu
, *menuitem
;
1604 extern char *const *const xpm_icons
[];
1605 extern const int n_xpm_icons
;
1607 fe
= snew(frontend
);
1609 fe
->timer_active
= FALSE
;
1612 fe
->me
= midend_new(fe
, &thegame
, >k_drawing
, fe
);
1622 err
= midend_game_id(fe
->me
, arg
);
1624 midend_new_game(fe
->me
);
1626 sprintf(errbuf
, "Invalid game ID: %.800s", err
);
1629 fp
= fopen(arg
, "r");
1631 sprintf(errbuf
, "Error opening file: %.800s", strerror(errno
));
1633 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1635 sprintf(errbuf
, "Invalid save file: %.800s", err
);
1639 default /*case ARG_EITHER*/:
1641 * First try treating the argument as a game ID.
1643 err
= midend_game_id(fe
->me
, arg
);
1646 * It's a valid game ID.
1648 midend_new_game(fe
->me
);
1650 FILE *fp
= fopen(arg
, "r");
1652 sprintf(errbuf
, "Supplied argument is neither a game ID (%.400s)"
1653 " nor a save file (%.400s)", err
, strerror(errno
));
1655 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1657 sprintf(errbuf
, "%.800s", err
);
1664 *error
= dupstr(errbuf
);
1665 midend_free(fe
->me
);
1671 midend_new_game(fe
->me
);
1674 fe
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1675 gtk_window_set_title(GTK_WINDOW(fe
->window
), thegame
.name
);
1677 vbox
= GTK_BOX(gtk_vbox_new(FALSE
, 0));
1678 gtk_container_add(GTK_CONTAINER(fe
->window
), GTK_WIDGET(vbox
));
1679 gtk_widget_show(GTK_WIDGET(vbox
));
1681 fe
->accelgroup
= gtk_accel_group_new();
1682 gtk_window_add_accel_group(GTK_WINDOW(fe
->window
), fe
->accelgroup
);
1684 hbox
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
1685 gtk_box_pack_start(vbox
, GTK_WIDGET(hbox
), FALSE
, FALSE
, 0);
1686 gtk_widget_show(GTK_WIDGET(hbox
));
1688 menubar
= gtk_menu_bar_new();
1689 gtk_box_pack_start(hbox
, menubar
, TRUE
, TRUE
, 0);
1690 gtk_widget_show(menubar
);
1692 menuitem
= gtk_menu_item_new_with_mnemonic("_Game");
1693 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1694 gtk_widget_show(menuitem
);
1696 menu
= gtk_menu_new();
1697 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1699 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "New", 'n');
1701 menuitem
= gtk_menu_item_new_with_label("Restart");
1702 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1703 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1704 GTK_SIGNAL_FUNC(menu_restart_event
), fe
);
1705 gtk_widget_show(menuitem
);
1707 menuitem
= gtk_menu_item_new_with_label("Specific...");
1708 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1709 GINT_TO_POINTER(CFG_DESC
));
1710 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1711 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1712 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1713 gtk_widget_show(menuitem
);
1715 menuitem
= gtk_menu_item_new_with_label("Random Seed...");
1716 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1717 GINT_TO_POINTER(CFG_SEED
));
1718 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1719 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1720 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1721 gtk_widget_show(menuitem
);
1723 fe
->preset_radio
= NULL
;
1724 fe
->preset_custom
= NULL
;
1725 fe
->n_preset_menu_items
= 0;
1726 fe
->preset_threaded
= FALSE
;
1727 if ((n
= midend_num_presets(fe
->me
)) > 0 || thegame
.can_configure
) {
1731 menuitem
= gtk_menu_item_new_with_mnemonic("_Type");
1732 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1733 gtk_widget_show(menuitem
);
1735 submenu
= gtk_menu_new();
1736 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), submenu
);
1738 for (i
= 0; i
< n
; i
++) {
1740 game_params
*params
;
1742 midend_fetch_preset(fe
->me
, i
, &name
, ¶ms
);
1745 gtk_radio_menu_item_new_with_label(fe
->preset_radio
, name
);
1747 gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(menuitem
));
1748 fe
->n_preset_menu_items
++;
1749 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1750 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", params
);
1751 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1752 GTK_SIGNAL_FUNC(menu_preset_event
), fe
);
1753 gtk_widget_show(menuitem
);
1756 if (thegame
.can_configure
) {
1757 menuitem
= fe
->preset_custom
=
1758 gtk_radio_menu_item_new_with_label(fe
->preset_radio
,
1761 gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(menuitem
));
1762 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1763 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1764 GPOINTER_TO_INT(CFG_SETTINGS
));
1765 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1766 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1767 gtk_widget_show(menuitem
);
1772 add_menu_separator(GTK_CONTAINER(menu
));
1773 menuitem
= gtk_menu_item_new_with_label("Load...");
1774 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1775 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1776 GTK_SIGNAL_FUNC(menu_load_event
), fe
);
1777 gtk_widget_show(menuitem
);
1778 menuitem
= gtk_menu_item_new_with_label("Save...");
1779 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1780 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1781 GTK_SIGNAL_FUNC(menu_save_event
), fe
);
1782 gtk_widget_show(menuitem
);
1783 #ifndef STYLUS_BASED
1784 add_menu_separator(GTK_CONTAINER(menu
));
1785 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Undo", 'u');
1786 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Redo", 'r');
1788 if (thegame
.can_format_as_text_ever
) {
1789 add_menu_separator(GTK_CONTAINER(menu
));
1790 menuitem
= gtk_menu_item_new_with_label("Copy");
1791 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1792 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1793 GTK_SIGNAL_FUNC(menu_copy_event
), fe
);
1794 gtk_widget_show(menuitem
);
1795 fe
->copy_menu_item
= menuitem
;
1797 fe
->copy_menu_item
= NULL
;
1799 if (thegame
.can_solve
) {
1800 add_menu_separator(GTK_CONTAINER(menu
));
1801 menuitem
= gtk_menu_item_new_with_label("Solve");
1802 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1803 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1804 GTK_SIGNAL_FUNC(menu_solve_event
), fe
);
1805 gtk_widget_show(menuitem
);
1807 add_menu_separator(GTK_CONTAINER(menu
));
1808 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Exit", 'q');
1810 menuitem
= gtk_menu_item_new_with_mnemonic("_Help");
1811 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1812 gtk_widget_show(menuitem
);
1814 menu
= gtk_menu_new();
1815 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1817 menuitem
= gtk_menu_item_new_with_label("About");
1818 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1819 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1820 GTK_SIGNAL_FUNC(menu_about_event
), fe
);
1821 gtk_widget_show(menuitem
);
1824 menuitem
=gtk_button_new_with_mnemonic("_Redo");
1825 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1826 GINT_TO_POINTER((int)('r')));
1827 gtk_signal_connect(GTK_OBJECT(menuitem
), "clicked",
1828 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
1829 gtk_box_pack_end(hbox
, menuitem
, FALSE
, FALSE
, 0);
1830 gtk_widget_show(menuitem
);
1832 menuitem
=gtk_button_new_with_mnemonic("_Undo");
1833 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1834 GINT_TO_POINTER((int)('u')));
1835 gtk_signal_connect(GTK_OBJECT(menuitem
), "clicked",
1836 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
1837 gtk_box_pack_end(hbox
, menuitem
, FALSE
, FALSE
, 0);
1838 gtk_widget_show(menuitem
);
1840 if (thegame
.flags
& REQUIRE_NUMPAD
) {
1841 hbox
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
1842 gtk_box_pack_start(vbox
, GTK_WIDGET(hbox
), FALSE
, FALSE
, 0);
1843 gtk_widget_show(GTK_WIDGET(hbox
));
1847 for(errbuf
[0]='0';errbuf
[0]<='9';errbuf
[0]++) {
1848 menuitem
=gtk_button_new_with_label(errbuf
);
1849 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1850 GINT_TO_POINTER((int)(errbuf
[0])));
1851 gtk_signal_connect(GTK_OBJECT(menuitem
), "clicked",
1852 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
1853 gtk_box_pack_start(hbox
, menuitem
, TRUE
, TRUE
, 0);
1854 gtk_widget_show(menuitem
);
1857 #endif /* STYLUS_BASED */
1866 fe
->colmap
= gdk_colormap_get_system();
1867 colours
= midend_colours(fe
->me
, &ncolours
);
1868 fe
->ncolours
= ncolours
;
1869 fe
->colours
= snewn(ncolours
, GdkColor
);
1870 for (i
= 0; i
< ncolours
; i
++) {
1871 fe
->colours
[i
].red
= colours
[i
*3] * 0xFFFF;
1872 fe
->colours
[i
].green
= colours
[i
*3+1] * 0xFFFF;
1873 fe
->colours
[i
].blue
= colours
[i
*3+2] * 0xFFFF;
1875 success
= snewn(ncolours
, gboolean
);
1876 gdk_colormap_alloc_colors(fe
->colmap
, fe
->colours
, ncolours
,
1877 FALSE
, FALSE
, success
);
1878 for (i
= 0; i
< ncolours
; i
++) {
1880 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
1881 i
, fe
->colours
[i
].red
>> 8,
1882 fe
->colours
[i
].green
>> 8,
1883 fe
->colours
[i
].blue
>> 8);
1888 if (midend_wants_statusbar(fe
->me
)) {
1889 GtkWidget
*viewport
;
1892 viewport
= gtk_viewport_new(NULL
, NULL
);
1893 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport
), GTK_SHADOW_NONE
);
1894 fe
->statusbar
= gtk_statusbar_new();
1895 gtk_container_add(GTK_CONTAINER(viewport
), fe
->statusbar
);
1896 gtk_widget_show(viewport
);
1897 gtk_box_pack_end(vbox
, viewport
, FALSE
, FALSE
, 0);
1898 gtk_widget_show(fe
->statusbar
);
1899 fe
->statusctx
= gtk_statusbar_get_context_id
1900 (GTK_STATUSBAR(fe
->statusbar
), "game");
1901 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
,
1903 gtk_widget_size_request(fe
->statusbar
, &req
);
1905 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
1907 gtk_widget_set_usize(viewport
, -1, req
.height
);
1909 fe
->statusbar
= NULL
;
1911 fe
->area
= gtk_drawing_area_new();
1912 get_size(fe
, &x
, &y
);
1913 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1917 gtk_box_pack_end(vbox
, fe
->area
, TRUE
, TRUE
, 0);
1921 fe
->nfonts
= fe
->fontsize
= 0;
1923 fe
->paste_data
= NULL
;
1924 fe
->paste_data_len
= 0;
1926 gtk_signal_connect(GTK_OBJECT(fe
->window
), "destroy",
1927 GTK_SIGNAL_FUNC(destroy
), fe
);
1928 gtk_signal_connect(GTK_OBJECT(fe
->window
), "key_press_event",
1929 GTK_SIGNAL_FUNC(key_event
), fe
);
1930 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_press_event",
1931 GTK_SIGNAL_FUNC(button_event
), fe
);
1932 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_release_event",
1933 GTK_SIGNAL_FUNC(button_event
), fe
);
1934 gtk_signal_connect(GTK_OBJECT(fe
->area
), "motion_notify_event",
1935 GTK_SIGNAL_FUNC(motion_event
), fe
);
1936 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_get",
1937 GTK_SIGNAL_FUNC(selection_get
), fe
);
1938 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_clear_event",
1939 GTK_SIGNAL_FUNC(selection_clear
), fe
);
1940 gtk_signal_connect(GTK_OBJECT(fe
->area
), "expose_event",
1941 GTK_SIGNAL_FUNC(expose_area
), fe
);
1942 gtk_signal_connect(GTK_OBJECT(fe
->window
), "map_event",
1943 GTK_SIGNAL_FUNC(map_window
), fe
);
1944 gtk_signal_connect(GTK_OBJECT(fe
->area
), "configure_event",
1945 GTK_SIGNAL_FUNC(configure_area
), fe
);
1947 gtk_widget_add_events(GTK_WIDGET(fe
->area
),
1948 GDK_BUTTON_PRESS_MASK
|
1949 GDK_BUTTON_RELEASE_MASK
|
1950 GDK_BUTTON_MOTION_MASK
|
1951 GDK_POINTER_MOTION_HINT_MASK
);
1954 gtk_widget_realize(fe
->window
);
1955 iconpm
= gdk_pixmap_create_from_xpm_d(fe
->window
->window
, NULL
,
1956 NULL
, (gchar
**)xpm_icons
[0]);
1957 gdk_window_set_icon(fe
->window
->window
, NULL
, iconpm
, NULL
);
1959 for (n
= 0; n
< n_xpm_icons
; n
++) {
1961 g_list_append(iconlist
,
1962 gdk_pixbuf_new_from_xpm_data((const gchar
**)
1965 gdk_window_set_icon_list(fe
->window
->window
, iconlist
);
1968 gtk_widget_show(fe
->area
);
1969 gtk_widget_show(fe
->window
);
1972 * Now that we've established the preferred size of the window,
1973 * reduce the drawing area's size request so the user can shrink
1976 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), 1, 1);
1978 gdk_window_set_background(fe
->area
->window
, &fe
->colours
[0]);
1979 gdk_window_set_background(fe
->window
->window
, &fe
->colours
[0]);
1984 char *fgetline(FILE *fp
)
1986 char *ret
= snewn(512, char);
1987 int size
= 512, len
= 0;
1988 while (fgets(ret
+ len
, size
- len
, fp
)) {
1989 len
+= strlen(ret
+ len
);
1990 if (ret
[len
-1] == '\n')
1991 break; /* got a newline, we're done */
1993 ret
= sresize(ret
, size
, char);
1995 if (len
== 0) { /* first fgets returned NULL */
2003 int main(int argc
, char **argv
)
2005 char *pname
= argv
[0];
2007 int ngenerate
= 0, print
= FALSE
, px
= 1, py
= 1;
2008 int soln
= FALSE
, colour
= FALSE
;
2010 float redo_proportion
= 0.0F
;
2011 char *savefile
= NULL
, *savesuffix
= NULL
;
2013 int argtype
= ARG_EITHER
;
2014 char *screenshot_file
= NULL
;
2015 int doing_opts
= TRUE
;
2021 * Command line parsing in this function is rather fiddly,
2022 * because GTK wants to have a go at argc/argv _first_ - and
2023 * yet we can't let it, because gtk_init() will bomb out if it
2024 * can't open an X display, whereas in fact we want to permit
2025 * our --generate and --print modes to run without an X
2029 * - we parse the command line ourselves, without modifying
2031 * - if we encounter an error which might plausibly be the
2032 * result of a GTK command line (i.e. not detailed errors in
2033 * particular options of ours) we store the error message
2034 * and terminate parsing.
2035 * - if we got enough out of the command line to know it
2036 * specifies a non-X mode of operation, we either display
2037 * the stored error and return failure, or if there is no
2038 * stored error we do the non-X operation and return
2040 * - otherwise, we go straight to gtk_init().
2046 if (doing_opts
&& !strcmp(p
, "--version")) {
2047 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
2050 } else if (doing_opts
&& !strcmp(p
, "--generate")) {
2052 ngenerate
= atoi(*++av
);
2054 fprintf(stderr
, "%s: '--generate' expected a number\n",
2060 } else if (doing_opts
&& !strcmp(p
, "--save")) {
2064 fprintf(stderr
, "%s: '--save' expected a filename\n",
2068 } else if (doing_opts
&& (!strcmp(p
, "--save-suffix") ||
2069 !strcmp(p
, "--savesuffix"))) {
2073 fprintf(stderr
, "%s: '--save-suffix' expected a filename\n",
2077 } else if (doing_opts
&& !strcmp(p
, "--print")) {
2078 if (!thegame
.can_print
) {
2079 fprintf(stderr
, "%s: this game does not support printing\n",
2086 if (sscanf(dim
, "%dx%d", &px
, &py
) != 2) {
2087 fprintf(stderr
, "%s: unable to parse argument '%s' to "
2088 "'--print'\n", pname
, dim
);
2094 } else if (doing_opts
&& !strcmp(p
, "--scale")) {
2096 scale
= atof(*++av
);
2098 fprintf(stderr
, "%s: no argument supplied to '--scale'\n",
2102 } else if (doing_opts
&& !strcmp(p
, "--redo")) {
2104 * This is an internal option which I don't expect
2105 * users to have any particular use for. The effect of
2106 * --redo is that once the game has been loaded and
2107 * initialised, the next move in the redo chain is
2108 * replayed, and the game screen is redrawn part way
2109 * through the making of the move. This is only
2110 * meaningful if there _is_ a next move in the redo
2111 * chain, which means in turn that this option is only
2112 * useful if you're also passing a save file on the
2115 * This option is used by the script which generates
2116 * the puzzle icons and website screenshots, and I
2117 * don't imagine it's useful for anything else.
2118 * (Unless, I suppose, users don't like my screenshots
2119 * and want to generate their own in the same way for
2120 * some repackaged version of the puzzles.)
2123 redo_proportion
= atof(*++av
);
2125 fprintf(stderr
, "%s: no argument supplied to '--redo'\n",
2129 } else if (doing_opts
&& !strcmp(p
, "--screenshot")) {
2131 * Another internal option for the icon building
2132 * script. This causes a screenshot of the central
2133 * drawing area (i.e. not including the menu bar or
2134 * status bar) to be saved to a PNG file once the
2135 * window has been drawn, and then the application
2136 * quits immediately.
2139 screenshot_file
= *++av
;
2141 fprintf(stderr
, "%s: no argument supplied to '--screenshot'\n",
2145 } else if (doing_opts
&& (!strcmp(p
, "--with-solutions") ||
2146 !strcmp(p
, "--with-solution") ||
2147 !strcmp(p
, "--with-solns") ||
2148 !strcmp(p
, "--with-soln") ||
2149 !strcmp(p
, "--solutions") ||
2150 !strcmp(p
, "--solution") ||
2151 !strcmp(p
, "--solns") ||
2152 !strcmp(p
, "--soln"))) {
2154 } else if (doing_opts
&& !strcmp(p
, "--colour")) {
2155 if (!thegame
.can_print_in_colour
) {
2156 fprintf(stderr
, "%s: this game does not support colour"
2157 " printing\n", pname
);
2161 } else if (doing_opts
&& !strcmp(p
, "--load")) {
2163 } else if (doing_opts
&& !strcmp(p
, "--game")) {
2165 } else if (doing_opts
&& !strcmp(p
, "--")) {
2167 } else if (!doing_opts
|| p
[0] != '-') {
2169 fprintf(stderr
, "%s: more than one argument supplied\n",
2175 sprintf(errbuf
, "%.100s: unrecognised option '%.100s'\n",
2182 fputs(errbuf
, stderr
);
2187 * Special standalone mode for generating puzzle IDs on the
2188 * command line. Useful for generating puzzles to be printed
2189 * out and solved offline (for puzzles where that even makes
2190 * sense - Solo, for example, is a lot more pencil-and-paper
2191 * friendly than Twiddle!)
2195 * <puzzle-name> --generate [<n> [<params>]]
2197 * <n>, if present, is the number of puzzle IDs to generate.
2198 * <params>, if present, is the same type of parameter string
2199 * you would pass to the puzzle when running it in GUI mode,
2200 * including optional extras such as the expansion factor in
2201 * Rectangles and the difficulty level in Solo.
2203 * If you specify <params>, you must also specify <n> (although
2204 * you may specify it to be 1). Sorry; that was the
2205 * simplest-to-parse command-line syntax I came up with.
2207 if (ngenerate
> 0 || print
|| savefile
|| savesuffix
) {
2211 document
*doc
= NULL
;
2215 me
= midend_new(NULL
, &thegame
, NULL
, NULL
);
2218 if (savefile
&& !savesuffix
)
2220 if (!savefile
&& savesuffix
)
2224 doc
= document_new(px
, py
, scale
);
2227 * In this loop, we either generate a game ID or read one
2228 * from stdin depending on whether we're in generate mode;
2229 * then we either write it to stdout or print it, depending
2230 * on whether we're in print mode. Thus, this loop handles
2231 * generate-to-stdout, print-from-stdin and generate-and-
2232 * immediately-print modes.
2234 * (It could also handle a copy-stdin-to-stdout mode,
2235 * although there's currently no combination of options
2236 * which will cause this loop to be activated in that mode.
2237 * It wouldn't be _entirely_ pointless, though, because
2238 * stdin could contain bare params strings or random-seed
2239 * IDs, and stdout would contain nothing but fully
2240 * generated descriptive game IDs.)
2242 while (ngenerate
== 0 || i
< n
) {
2245 if (ngenerate
== 0) {
2246 pstr
= fgetline(stdin
);
2249 pstr
[strcspn(pstr
, "\r\n")] = '\0';
2252 pstr
= snewn(strlen(arg
) + 40, char);
2255 if (i
> 0 && strchr(arg
, '#'))
2256 sprintf(pstr
+ strlen(pstr
), "-%d", i
);
2262 err
= midend_game_id(me
, pstr
);
2264 fprintf(stderr
, "%s: error parsing '%s': %s\n",
2271 midend_new_game(me
);
2274 err
= midend_print_puzzle(me
, doc
, soln
);
2276 fprintf(stderr
, "%s: error in printing: %s\n", pname
, err
);
2281 struct savefile_write_ctx ctx
;
2282 char *realname
= snewn(40 + strlen(savefile
) +
2283 strlen(savesuffix
), char);
2284 sprintf(realname
, "%s%d%s", savefile
, i
, savesuffix
);
2285 ctx
.fp
= fopen(realname
, "w");
2287 fprintf(stderr
, "%s: open: %s\n", realname
,
2292 midend_serialise(me
, savefile_write
, &ctx
);
2294 fprintf(stderr
, "%s: write: %s\n", realname
,
2295 strerror(ctx
.error
));
2298 if (fclose(ctx
.fp
)) {
2299 fprintf(stderr
, "%s: close: %s\n", realname
,
2304 if (!doc
&& !savefile
) {
2305 id
= midend_get_game_id(me
);
2314 psdata
*ps
= ps_init(stdout
, colour
);
2315 document_print(doc
, ps_drawing_api(ps
));
2326 gtk_init(&argc
, &argv
);
2328 fe
= new_window(arg
, argtype
, &error
);
2331 fprintf(stderr
, "%s: %s\n", pname
, error
);
2335 if (screenshot_file
) {
2337 * Some puzzles will not redraw their entire area if
2338 * given a partially completed animation, which means
2339 * we must redraw now and _then_ redraw again after
2340 * freezing the move timer.
2342 midend_force_redraw(fe
->me
);
2345 if (redo_proportion
) {
2347 midend_process_key(fe
->me
, 0, 0, 'r');
2348 /* And freeze the timer at the specified position. */
2349 midend_freeze_timer(fe
->me
, redo_proportion
);
2352 if (screenshot_file
) {
2354 GError
*gerror
= NULL
;
2356 midend_redraw(fe
->me
);
2358 pb
= gdk_pixbuf_get_from_drawable(NULL
, fe
->pixmap
,
2359 NULL
, 0, 0, 0, 0, -1, -1);
2360 gdk_pixbuf_save(pb
, screenshot_file
, "png", &gerror
, NULL
);