2 * gtk.c: GTK front end for my puzzle collection.
17 #include <gdk/gdkkeysyms.h>
19 #include <gdk-pixbuf/gdk-pixbuf.h>
23 #include <X11/Xutil.h>
24 #include <X11/Xatom.h>
28 #if GTK_CHECK_VERSION(2,0,0)
30 # ifdef PANGO_VERSION_CHECK
31 # if PANGO_VERSION_CHECK(1,8,0)
32 # define HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
36 #if !GTK_CHECK_VERSION(2,4,0)
39 #if GTK_CHECK_VERSION(2,8,0)
43 /* #undef USE_CAIRO */
44 /* #define NO_THICK_LINE */
46 static FILE *debug_fp
= NULL
;
51 debug_fp
= fopen("debug.log", "w");
62 void debug_printf(char *fmt
, ...)
68 vsprintf(buf
, fmt
, ap
);
74 /* ----------------------------------------------------------------------
75 * Error reporting functions used elsewhere.
78 void fatal(char *fmt
, ...)
82 fprintf(stderr
, "fatal error: ");
85 vfprintf(stderr
, fmt
, ap
);
88 fprintf(stderr
, "\n");
92 /* ----------------------------------------------------------------------
93 * GTK front end to puzzles.
96 static void changed_preset(frontend
*fe
);
100 PangoFontDescription
*desc
;
109 * This structure holds all the data relevant to a single window.
110 * In principle this would allow us to open multiple independent
111 * puzzle windows, although I can't currently see any real point in
112 * doing so. I'm just coding cleanly because there's no
113 * particularly good reason not to.
117 GtkAccelGroup
*accelgroup
;
119 GtkWidget
*statusbar
;
125 const float *colours
;
127 cairo_surface_t
*image
;
129 GdkColor background
; /* for painting outside puzzle area */
135 int backgroundindex
; /* which of colours[] is background */
138 int bbox_l
, bbox_r
, bbox_u
, bbox_d
;
139 int timer_active
, timer_id
;
140 struct timeval last_time
;
142 int nfonts
, fontsize
;
144 int cfg_which
, cfgret
;
148 int pw
, ph
; /* pixmap size (w, h are area size) */
149 int ox
, oy
; /* offset of pixmap in drawing area */
153 int drawing_area_shrink_pending
;
154 GSList
*preset_radio
;
155 int n_preset_menu_items
;
157 GtkWidget
*preset_custom
;
158 GtkWidget
*copy_menu_item
;
163 cairo_surface_t
*image
;
170 void get_random_seed(void **randseed
, int *randseedsize
)
172 struct timeval
*tvp
= snew(struct timeval
);
173 gettimeofday(tvp
, NULL
);
174 *randseed
= (void *)tvp
;
175 *randseedsize
= sizeof(struct timeval
);
178 void frontend_default_colour(frontend
*fe
, float *output
)
180 GdkColor col
= fe
->window
->style
->bg
[GTK_STATE_NORMAL
];
181 output
[0] = col
.red
/ 65535.0;
182 output
[1] = col
.green
/ 65535.0;
183 output
[2] = col
.blue
/ 65535.0;
186 void gtk_status_bar(void *handle
, char *text
)
188 frontend
*fe
= (frontend
*)handle
;
190 assert(fe
->statusbar
);
192 gtk_statusbar_pop(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
);
193 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
, text
);
196 /* ----------------------------------------------------------------------
197 * Cairo drawing functions.
202 static void setup_drawing(frontend
*fe
)
204 fe
->cr
= cairo_create(fe
->image
);
205 cairo_set_antialias(fe
->cr
, CAIRO_ANTIALIAS_GRAY
);
206 cairo_set_line_width(fe
->cr
, 1.0);
207 cairo_set_line_cap(fe
->cr
, CAIRO_LINE_CAP_SQUARE
);
208 cairo_set_line_join(fe
->cr
, CAIRO_LINE_JOIN_ROUND
);
211 static void teardown_drawing(frontend
*fe
)
215 cairo_destroy(fe
->cr
);
218 cr
= gdk_cairo_create(fe
->pixmap
);
219 cairo_set_source_surface(cr
, fe
->image
, 0, 0);
223 fe
->bbox_r
- fe
->bbox_l
+ 2,
224 fe
->bbox_d
- fe
->bbox_u
+ 2);
229 static void snaffle_colours(frontend
*fe
)
231 fe
->colours
= midend_colours(fe
->me
, &fe
->ncolours
);
234 static void set_colour(frontend
*fe
, int colour
)
236 cairo_set_source_rgb(fe
->cr
,
237 fe
->colours
[3*colour
+ 0],
238 fe
->colours
[3*colour
+ 1],
239 fe
->colours
[3*colour
+ 2]);
242 static void set_window_background(frontend
*fe
, int colour
)
246 colmap
= gdk_colormap_get_system();
247 fe
->background
.red
= fe
->colours
[3*colour
+ 0] * 65535;
248 fe
->background
.green
= fe
->colours
[3*colour
+ 1] * 65535;
249 fe
->background
.blue
= fe
->colours
[3*colour
+ 2] * 65535;
250 if (!gdk_colormap_alloc_color(colmap
, &fe
->background
, FALSE
, FALSE
)) {
251 g_error("couldn't allocate background (#%02x%02x%02x)\n",
252 fe
->background
.red
>> 8, fe
->background
.green
>> 8,
253 fe
->background
.blue
>> 8);
255 gdk_window_set_background(fe
->area
->window
, &fe
->background
);
256 gdk_window_set_background(fe
->window
->window
, &fe
->background
);
259 static PangoLayout
*make_pango_layout(frontend
*fe
)
261 return (pango_cairo_create_layout(fe
->cr
));
264 static void draw_pango_layout(frontend
*fe
, PangoLayout
*layout
,
267 cairo_move_to(fe
->cr
, x
, y
);
268 pango_cairo_show_layout(fe
->cr
, layout
);
271 static void save_screenshot_png(frontend
*fe
, const char *screenshot_file
)
273 cairo_surface_write_to_png(fe
->image
, screenshot_file
);
276 static void do_clip(frontend
*fe
, int x
, int y
, int w
, int h
)
278 cairo_new_path(fe
->cr
);
279 cairo_rectangle(fe
->cr
, x
, y
, w
, h
);
283 static void do_unclip(frontend
*fe
)
285 cairo_reset_clip(fe
->cr
);
288 static void do_draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
)
291 cairo_new_path(fe
->cr
);
292 cairo_set_antialias(fe
->cr
, CAIRO_ANTIALIAS_NONE
);
293 cairo_rectangle(fe
->cr
, x
, y
, w
, h
);
295 cairo_restore(fe
->cr
);
298 static void do_draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
)
300 cairo_new_path(fe
->cr
);
301 cairo_move_to(fe
->cr
, x1
+ 0.5, y1
+ 0.5);
302 cairo_line_to(fe
->cr
, x2
+ 0.5, y2
+ 0.5);
303 cairo_stroke(fe
->cr
);
306 static void do_draw_thick_line(frontend
*fe
, float thickness
,
307 float x1
, float y1
, float x2
, float y2
)
310 cairo_set_line_width(fe
->cr
, thickness
);
311 cairo_new_path(fe
->cr
);
312 cairo_move_to(fe
->cr
, x1
, y1
);
313 cairo_line_to(fe
->cr
, x2
, y2
);
314 cairo_stroke(fe
->cr
);
315 cairo_restore(fe
->cr
);
318 static void do_draw_poly(frontend
*fe
, int *coords
, int npoints
,
319 int fillcolour
, int outlinecolour
)
323 cairo_new_path(fe
->cr
);
324 for (i
= 0; i
< npoints
; i
++)
325 cairo_line_to(fe
->cr
, coords
[i
*2] + 0.5, coords
[i
*2 + 1] + 0.5);
326 cairo_close_path(fe
->cr
);
327 if (fillcolour
>= 0) {
328 set_colour(fe
, fillcolour
);
329 cairo_fill_preserve(fe
->cr
);
331 assert(outlinecolour
>= 0);
332 set_colour(fe
, outlinecolour
);
333 cairo_stroke(fe
->cr
);
336 static void do_draw_circle(frontend
*fe
, int cx
, int cy
, int radius
,
337 int fillcolour
, int outlinecolour
)
339 cairo_new_path(fe
->cr
);
340 cairo_arc(fe
->cr
, cx
+ 0.5, cy
+ 0.5, radius
, 0, 2*PI
);
341 cairo_close_path(fe
->cr
); /* Just in case... */
342 if (fillcolour
>= 0) {
343 set_colour(fe
, fillcolour
);
344 cairo_fill_preserve(fe
->cr
);
346 assert(outlinecolour
>= 0);
347 set_colour(fe
, outlinecolour
);
348 cairo_stroke(fe
->cr
);
351 static void setup_blitter(blitter
*bl
, int w
, int h
)
353 bl
->image
= cairo_image_surface_create(CAIRO_FORMAT_RGB24
, w
, h
);
356 static void teardown_blitter(blitter
*bl
)
358 cairo_surface_destroy(bl
->image
);
361 static void do_blitter_save(frontend
*fe
, blitter
*bl
, int x
, int y
)
363 cairo_t
*cr
= cairo_create(bl
->image
);
365 cairo_set_source_surface(cr
, fe
->image
, -x
, -y
);
370 static void do_blitter_load(frontend
*fe
, blitter
*bl
, int x
, int y
)
372 cairo_set_source_surface(fe
->cr
, bl
->image
, x
, y
);
376 static void clear_backing_store(frontend
*fe
)
381 static void setup_backing_store(frontend
*fe
)
386 fe
->pixmap
= gdk_pixmap_new(fe
->area
->window
, fe
->pw
, fe
->ph
, -1);
387 fe
->image
= cairo_image_surface_create(CAIRO_FORMAT_RGB24
,
390 for (i
= 0; i
< 3; i
++) {
392 case 0: cr
= cairo_create(fe
->image
); break;
393 case 1: cr
= gdk_cairo_create(fe
->pixmap
); break;
394 case 2: cr
= gdk_cairo_create(fe
->area
->window
); break;
396 cairo_set_source_rgb(cr
,
397 fe
->colours
[0], fe
->colours
[1], fe
->colours
[2]);
403 static int backing_store_ok(frontend
*fe
)
405 return (!!fe
->image
);
408 static void teardown_backing_store(frontend
*fe
)
410 cairo_surface_destroy(fe
->image
);
411 gdk_pixmap_unref(fe
->pixmap
);
417 /* ----------------------------------------------------------------------
418 * GDK drawing functions.
423 static void setup_drawing(frontend
*fe
)
425 fe
->gc
= gdk_gc_new(fe
->area
->window
);
428 static void teardown_drawing(frontend
*fe
)
430 gdk_gc_unref(fe
->gc
);
434 static void snaffle_colours(frontend
*fe
)
440 fe
->colmap
= gdk_colormap_get_system();
441 colours
= midend_colours(fe
->me
, &ncolours
);
442 fe
->ncolours
= ncolours
;
443 fe
->colours
= snewn(ncolours
, GdkColor
);
444 for (i
= 0; i
< ncolours
; i
++) {
445 fe
->colours
[i
].red
= colours
[i
*3] * 0xFFFF;
446 fe
->colours
[i
].green
= colours
[i
*3+1] * 0xFFFF;
447 fe
->colours
[i
].blue
= colours
[i
*3+2] * 0xFFFF;
449 success
= snewn(ncolours
, gboolean
);
450 gdk_colormap_alloc_colors(fe
->colmap
, fe
->colours
, ncolours
,
451 FALSE
, FALSE
, success
);
452 for (i
= 0; i
< ncolours
; i
++) {
454 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
455 i
, fe
->colours
[i
].red
>> 8,
456 fe
->colours
[i
].green
>> 8,
457 fe
->colours
[i
].blue
>> 8);
462 static void set_window_background(frontend
*fe
, int colour
)
464 fe
->backgroundindex
= colour
;
465 gdk_window_set_background(fe
->area
->window
, &fe
->colours
[colour
]);
466 gdk_window_set_background(fe
->window
->window
, &fe
->colours
[colour
]);
469 static void set_colour(frontend
*fe
, int colour
)
471 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
475 static PangoLayout
*make_pango_layout(frontend
*fe
)
477 return (pango_layout_new(gtk_widget_get_pango_context(fe
->area
)));
480 static void draw_pango_layout(frontend
*fe
, PangoLayout
*layout
,
483 gdk_draw_layout(fe
->pixmap
, fe
->gc
, x
, y
, layout
);
487 static void save_screenshot_png(frontend
*fe
, const char *screenshot_file
)
490 GError
*gerror
= NULL
;
492 midend_redraw(fe
->me
);
494 pb
= gdk_pixbuf_get_from_drawable(NULL
, fe
->pixmap
,
495 NULL
, 0, 0, 0, 0, -1, -1);
496 gdk_pixbuf_save(pb
, screenshot_file
, "png", &gerror
, NULL
);
499 static void do_clip(frontend
*fe
, int x
, int y
, int w
, int h
)
507 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
510 static void do_unclip(frontend
*fe
)
518 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
521 static void do_draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
)
523 gdk_draw_rectangle(fe
->pixmap
, fe
->gc
, 1, x
, y
, w
, h
);
526 static void do_draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
)
528 gdk_draw_line(fe
->pixmap
, fe
->gc
, x1
, y1
, x2
, y2
);
531 static void do_draw_thick_line(frontend
*fe
, float thickness
,
532 float x1
, float y1
, float x2
, float y2
)
536 gdk_gc_get_values(fe
->gc
, &save
);
537 gdk_gc_set_line_attributes(fe
->gc
,
542 gdk_draw_line(fe
->pixmap
, fe
->gc
, x1
, y1
, x2
, y2
);
543 gdk_gc_set_line_attributes(fe
->gc
,
550 static void do_draw_poly(frontend
*fe
, int *coords
, int npoints
,
551 int fillcolour
, int outlinecolour
)
553 GdkPoint
*points
= snewn(npoints
, GdkPoint
);
556 for (i
= 0; i
< npoints
; i
++) {
557 points
[i
].x
= coords
[i
*2];
558 points
[i
].y
= coords
[i
*2+1];
561 if (fillcolour
>= 0) {
562 set_colour(fe
, fillcolour
);
563 gdk_draw_polygon(fe
->pixmap
, fe
->gc
, TRUE
, points
, npoints
);
565 assert(outlinecolour
>= 0);
566 set_colour(fe
, outlinecolour
);
569 * In principle we ought to be able to use gdk_draw_polygon for
570 * the outline as well. In fact, it turns out to interact badly
571 * with a clipping region, for no terribly obvious reason, so I
572 * draw the outline as a sequence of lines instead.
574 for (i
= 0; i
< npoints
; i
++)
575 gdk_draw_line(fe
->pixmap
, fe
->gc
,
576 points
[i
].x
, points
[i
].y
,
577 points
[(i
+1)%npoints
].x
, points
[(i
+1)%npoints
].y
);
582 static void do_draw_circle(frontend
*fe
, int cx
, int cy
, int radius
,
583 int fillcolour
, int outlinecolour
)
585 if (fillcolour
>= 0) {
586 set_colour(fe
, fillcolour
);
587 gdk_draw_arc(fe
->pixmap
, fe
->gc
, TRUE
,
588 cx
- radius
, cy
- radius
,
589 2 * radius
, 2 * radius
, 0, 360 * 64);
592 assert(outlinecolour
>= 0);
593 set_colour(fe
, outlinecolour
);
594 gdk_draw_arc(fe
->pixmap
, fe
->gc
, FALSE
,
595 cx
- radius
, cy
- radius
,
596 2 * radius
, 2 * radius
, 0, 360 * 64);
599 static void setup_blitter(blitter
*bl
, int w
, int h
)
602 * We can't create the pixmap right now, because fe->window
603 * might not yet exist. So we just cache w and h and create it
604 * during the firs call to blitter_save.
609 static void teardown_blitter(blitter
*bl
)
612 gdk_pixmap_unref(bl
->pixmap
);
615 static void do_blitter_save(frontend
*fe
, blitter
*bl
, int x
, int y
)
618 bl
->pixmap
= gdk_pixmap_new(fe
->area
->window
, bl
->w
, bl
->h
, -1);
619 gdk_draw_pixmap(bl
->pixmap
,
620 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
622 x
, y
, 0, 0, bl
->w
, bl
->h
);
625 static void do_blitter_load(frontend
*fe
, blitter
*bl
, int x
, int y
)
628 gdk_draw_pixmap(fe
->pixmap
,
629 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
631 0, 0, x
, y
, bl
->w
, bl
->h
);
634 static void clear_backing_store(frontend
*fe
)
639 static void setup_backing_store(frontend
*fe
)
643 fe
->pixmap
= gdk_pixmap_new(fe
->area
->window
, fe
->pw
, fe
->ph
, -1);
645 gc
= gdk_gc_new(fe
->area
->window
);
646 gdk_gc_set_foreground(gc
, &fe
->colours
[0]);
647 gdk_draw_rectangle(fe
->pixmap
, gc
, 1, 0, 0, fe
->pw
, fe
->ph
);
648 gdk_draw_rectangle(fe
->area
->window
, gc
, 1, 0, 0, fe
->w
, fe
->h
);
652 static int backing_store_ok(frontend
*fe
)
654 return (!!fe
->pixmap
);
657 static void teardown_backing_store(frontend
*fe
)
659 gdk_pixmap_unref(fe
->pixmap
);
665 static void repaint_rectangle(frontend
*fe
, GtkWidget
*widget
,
666 int x
, int y
, int w
, int h
)
668 GdkGC
*gc
= gdk_gc_new(widget
->window
);
670 gdk_gc_set_foreground(gc
, &fe
->background
);
672 gdk_gc_set_foreground(gc
, &fe
->colours
[fe
->backgroundindex
]);
675 gdk_draw_rectangle(widget
->window
, gc
,
676 TRUE
, x
, y
, fe
->ox
- x
, h
);
681 gdk_draw_rectangle(widget
->window
, gc
,
682 TRUE
, x
, y
, w
, fe
->oy
- y
);
687 gdk_draw_rectangle(widget
->window
, gc
,
688 TRUE
, x
+ fe
->pw
, y
, w
- fe
->pw
, h
);
692 gdk_draw_rectangle(widget
->window
, gc
,
693 TRUE
, x
, y
+ fe
->ph
, w
, h
- fe
->ph
);
696 gdk_draw_pixmap(widget
->window
, gc
, fe
->pixmap
,
697 x
- fe
->ox
, y
- fe
->oy
, x
, y
, w
, h
);
701 /* ----------------------------------------------------------------------
702 * Pango font functions.
707 static void add_font(frontend
*fe
, int index
, int fonttype
, int fontsize
)
710 * Use Pango to find the closest match to the requested
713 PangoFontDescription
*fd
;
715 fd
= pango_font_description_new();
716 /* `Monospace' and `Sans' are meta-families guaranteed to exist */
717 pango_font_description_set_family(fd
, fonttype
== FONT_FIXED ?
718 "Monospace" : "Sans");
719 pango_font_description_set_weight(fd
, PANGO_WEIGHT_BOLD
);
721 * I found some online Pango documentation which
722 * described a function called
723 * pango_font_description_set_absolute_size(), which is
724 * _exactly_ what I want here. Unfortunately, none of
725 * my local Pango installations have it (presumably
726 * they're too old), so I'm going to have to hack round
727 * it by figuring out the point size myself. This
728 * limits me to X and probably also breaks in later
729 * Pango installations, so ideally I should add another
730 * CHECK_VERSION type ifdef and use set_absolute_size
731 * where available. All very annoying.
733 #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
734 pango_font_description_set_absolute_size(fd
, PANGO_SCALE
*fontsize
);
737 Display
*d
= GDK_DISPLAY();
738 int s
= DefaultScreen(d
);
740 (PANGO_SCALE
* 72.27 / 25.4) *
741 ((double) DisplayWidthMM(d
, s
) / DisplayWidth (d
, s
));
742 pango_font_description_set_size(fd
, resolution
* fontsize
);
745 fe
->fonts
[index
].desc
= fd
;
748 static void align_and_draw_text(frontend
*fe
,
749 int index
, int align
, int x
, int y
,
755 layout
= make_pango_layout(fe
);
760 pango_layout_set_font_description(layout
, fe
->fonts
[index
].desc
);
761 pango_layout_set_text(layout
, text
, strlen(text
));
762 pango_layout_get_pixel_extents(layout
, NULL
, &rect
);
764 if (align
& ALIGN_VCENTRE
)
765 rect
.y
-= rect
.height
/ 2;
767 rect
.y
-= rect
.height
;
769 if (align
& ALIGN_HCENTRE
)
770 rect
.x
-= rect
.width
/ 2;
771 else if (align
& ALIGN_HRIGHT
)
772 rect
.x
-= rect
.width
;
774 draw_pango_layout(fe
, layout
, rect
.x
+ x
, rect
.y
+ y
);
776 g_object_unref(layout
);
781 /* ----------------------------------------------------------------------
782 * Old-fashioned font functions.
787 static void add_font(int index
, int fonttype
, int fontsize
)
790 * In GTK 1.2, I don't know of any plausible way to
791 * pick a suitable font, so I'm just going to be
794 fe
->fonts
[i
].font
= gdk_font_load(fonttype
== FONT_FIXED ?
795 "fixed" : "variable");
798 static void align_and_draw_text(int index
, int align
, int x
, int y
,
801 int lb
, rb
, wid
, asc
, desc
;
804 * Measure vertical string extents with respect to the same
807 gdk_string_extents(fe
->fonts
[i
].font
,
808 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
809 &lb
, &rb
, &wid
, &asc
, &desc
);
810 if (align
& ALIGN_VCENTRE
)
811 y
+= asc
- (asc
+desc
)/2;
816 * ... but horizontal extents with respect to the provided
817 * string. This means that multiple pieces of text centred
818 * on the same y-coordinate don't have different baselines.
820 gdk_string_extents(fe
->fonts
[i
].font
, text
,
821 &lb
, &rb
, &wid
, &asc
, &desc
);
823 if (align
& ALIGN_HCENTRE
)
825 else if (align
& ALIGN_HRIGHT
)
829 * Actually draw the text.
831 gdk_draw_string(fe
->pixmap
, fe
->fonts
[i
].font
, fe
->gc
, x
, y
, text
);
836 /* ----------------------------------------------------------------------
837 * The exported drawing functions.
840 void gtk_start_draw(void *handle
)
842 frontend
*fe
= (frontend
*)handle
;
850 void gtk_clip(void *handle
, int x
, int y
, int w
, int h
)
852 frontend
*fe
= (frontend
*)handle
;
853 do_clip(fe
, x
, y
, w
, h
);
856 void gtk_unclip(void *handle
)
858 frontend
*fe
= (frontend
*)handle
;
862 void gtk_draw_text(void *handle
, int x
, int y
, int fonttype
, int fontsize
,
863 int align
, int colour
, char *text
)
865 frontend
*fe
= (frontend
*)handle
;
869 * Find or create the font.
871 for (i
= 0; i
< fe
->nfonts
; i
++)
872 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
875 if (i
== fe
->nfonts
) {
876 if (fe
->fontsize
<= fe
->nfonts
) {
877 fe
->fontsize
= fe
->nfonts
+ 10;
878 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
883 fe
->fonts
[i
].type
= fonttype
;
884 fe
->fonts
[i
].size
= fontsize
;
885 add_font(fe
, i
, fonttype
, fontsize
);
891 set_colour(fe
, colour
);
892 align_and_draw_text(fe
, i
, align
, x
, y
, text
);
895 void gtk_draw_rect(void *handle
, int x
, int y
, int w
, int h
, int colour
)
897 frontend
*fe
= (frontend
*)handle
;
898 set_colour(fe
, colour
);
899 do_draw_rect(fe
, x
, y
, w
, h
);
902 void gtk_draw_line(void *handle
, int x1
, int y1
, int x2
, int y2
, int colour
)
904 frontend
*fe
= (frontend
*)handle
;
905 set_colour(fe
, colour
);
906 do_draw_line(fe
, x1
, y1
, x2
, y2
);
909 void gtk_draw_thick_line(void *handle
, float thickness
,
910 float x1
, float y1
, float x2
, float y2
, int colour
)
912 frontend
*fe
= (frontend
*)handle
;
913 set_colour(fe
, colour
);
914 do_draw_thick_line(fe
, thickness
, x1
, y1
, x2
, y2
);
917 void gtk_draw_poly(void *handle
, int *coords
, int npoints
,
918 int fillcolour
, int outlinecolour
)
920 frontend
*fe
= (frontend
*)handle
;
921 do_draw_poly(fe
, coords
, npoints
, fillcolour
, outlinecolour
);
924 void gtk_draw_circle(void *handle
, int cx
, int cy
, int radius
,
925 int fillcolour
, int outlinecolour
)
927 frontend
*fe
= (frontend
*)handle
;
928 do_draw_circle(fe
, cx
, cy
, radius
, fillcolour
, outlinecolour
);
931 blitter
*gtk_blitter_new(void *handle
, int w
, int h
)
933 blitter
*bl
= snew(blitter
);
934 setup_blitter(bl
, w
, h
);
940 void gtk_blitter_free(void *handle
, blitter
*bl
)
942 teardown_blitter(bl
);
946 void gtk_blitter_save(void *handle
, blitter
*bl
, int x
, int y
)
948 frontend
*fe
= (frontend
*)handle
;
949 do_blitter_save(fe
, bl
, x
, y
);
954 void gtk_blitter_load(void *handle
, blitter
*bl
, int x
, int y
)
956 frontend
*fe
= (frontend
*)handle
;
957 if (x
== BLITTER_FROMSAVED
&& y
== BLITTER_FROMSAVED
) {
961 do_blitter_load(fe
, bl
, x
, y
);
964 void gtk_draw_update(void *handle
, int x
, int y
, int w
, int h
)
966 frontend
*fe
= (frontend
*)handle
;
967 if (fe
->bbox_l
> x
) fe
->bbox_l
= x
;
968 if (fe
->bbox_r
< x
+w
) fe
->bbox_r
= x
+w
;
969 if (fe
->bbox_u
> y
) fe
->bbox_u
= y
;
970 if (fe
->bbox_d
< y
+h
) fe
->bbox_d
= y
+h
;
973 void gtk_end_draw(void *handle
)
975 frontend
*fe
= (frontend
*)handle
;
977 teardown_drawing(fe
);
979 if (fe
->bbox_l
< fe
->bbox_r
&& fe
->bbox_u
< fe
->bbox_d
) {
980 repaint_rectangle(fe
, fe
->area
,
981 fe
->bbox_l
- 1 + fe
->ox
,
982 fe
->bbox_u
- 1 + fe
->oy
,
983 fe
->bbox_r
- fe
->bbox_l
+ 2,
984 fe
->bbox_d
- fe
->bbox_u
+ 2);
989 char *gtk_text_fallback(void *handle
, const char *const *strings
, int nstrings
)
992 * We assume Pango can cope with any UTF-8 likely to be emitted
995 return dupstr(strings
[0]);
999 const struct drawing_api gtk_drawing
= {
1015 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, /* {begin,end}_{doc,page,puzzle} */
1016 NULL
, NULL
, /* line_width, line_dotted */
1022 #ifdef NO_THICK_LINE
1025 gtk_draw_thick_line
,
1029 static void destroy(GtkWidget
*widget
, gpointer data
)
1031 frontend
*fe
= (frontend
*)data
;
1032 deactivate_timer(fe
);
1033 midend_free(fe
->me
);
1037 static gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1039 frontend
*fe
= (frontend
*)data
;
1041 int shift
= (event
->state
& GDK_SHIFT_MASK
) ? MOD_SHFT
: 0;
1042 int ctrl
= (event
->state
& GDK_CONTROL_MASK
) ? MOD_CTRL
: 0;
1044 if (!backing_store_ok(fe
))
1047 #if !GTK_CHECK_VERSION(2,0,0)
1048 /* Gtk 1.2 passes a key event to this function even if it's also
1049 * defined as an accelerator.
1050 * Gtk 2 doesn't do this, and this function appears not to exist there. */
1051 if (fe
->accelgroup
&&
1052 gtk_accel_group_get_entry(fe
->accelgroup
,
1053 event
->keyval
, event
->state
))
1057 /* Handle mnemonics. */
1058 if (gtk_window_activate_key(GTK_WINDOW(fe
->window
), event
))
1061 if (event
->keyval
== GDK_Up
)
1062 keyval
= shift
| ctrl
| CURSOR_UP
;
1063 else if (event
->keyval
== GDK_KP_Up
|| event
->keyval
== GDK_KP_8
)
1064 keyval
= MOD_NUM_KEYPAD
| '8';
1065 else if (event
->keyval
== GDK_Down
)
1066 keyval
= shift
| ctrl
| CURSOR_DOWN
;
1067 else if (event
->keyval
== GDK_KP_Down
|| event
->keyval
== GDK_KP_2
)
1068 keyval
= MOD_NUM_KEYPAD
| '2';
1069 else if (event
->keyval
== GDK_Left
)
1070 keyval
= shift
| ctrl
| CURSOR_LEFT
;
1071 else if (event
->keyval
== GDK_KP_Left
|| event
->keyval
== GDK_KP_4
)
1072 keyval
= MOD_NUM_KEYPAD
| '4';
1073 else if (event
->keyval
== GDK_Right
)
1074 keyval
= shift
| ctrl
| CURSOR_RIGHT
;
1075 else if (event
->keyval
== GDK_KP_Right
|| event
->keyval
== GDK_KP_6
)
1076 keyval
= MOD_NUM_KEYPAD
| '6';
1077 else if (event
->keyval
== GDK_KP_Home
|| event
->keyval
== GDK_KP_7
)
1078 keyval
= MOD_NUM_KEYPAD
| '7';
1079 else if (event
->keyval
== GDK_KP_End
|| event
->keyval
== GDK_KP_1
)
1080 keyval
= MOD_NUM_KEYPAD
| '1';
1081 else if (event
->keyval
== GDK_KP_Page_Up
|| event
->keyval
== GDK_KP_9
)
1082 keyval
= MOD_NUM_KEYPAD
| '9';
1083 else if (event
->keyval
== GDK_KP_Page_Down
|| event
->keyval
== GDK_KP_3
)
1084 keyval
= MOD_NUM_KEYPAD
| '3';
1085 else if (event
->keyval
== GDK_KP_Insert
|| event
->keyval
== GDK_KP_0
)
1086 keyval
= MOD_NUM_KEYPAD
| '0';
1087 else if (event
->keyval
== GDK_KP_Begin
|| event
->keyval
== GDK_KP_5
)
1088 keyval
= MOD_NUM_KEYPAD
| '5';
1089 else if (event
->keyval
== GDK_BackSpace
||
1090 event
->keyval
== GDK_Delete
||
1091 event
->keyval
== GDK_KP_Delete
)
1093 else if (event
->string
[0] && !event
->string
[1])
1094 keyval
= (unsigned char)event
->string
[0];
1099 !midend_process_key(fe
->me
, 0, 0, keyval
))
1100 gtk_widget_destroy(fe
->window
);
1105 static gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
,
1108 frontend
*fe
= (frontend
*)data
;
1111 if (!backing_store_ok(fe
))
1114 if (event
->type
!= GDK_BUTTON_PRESS
&& event
->type
!= GDK_BUTTON_RELEASE
)
1117 if (event
->button
== 2 || (event
->state
& GDK_SHIFT_MASK
))
1118 button
= MIDDLE_BUTTON
;
1119 else if (event
->button
== 3 || (event
->state
& GDK_MOD1_MASK
))
1120 button
= RIGHT_BUTTON
;
1121 else if (event
->button
== 1)
1122 button
= LEFT_BUTTON
;
1124 return FALSE
; /* don't even know what button! */
1126 if (event
->type
== GDK_BUTTON_RELEASE
)
1127 button
+= LEFT_RELEASE
- LEFT_BUTTON
;
1129 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
1130 event
->y
- fe
->oy
, button
))
1131 gtk_widget_destroy(fe
->window
);
1136 static gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
,
1139 frontend
*fe
= (frontend
*)data
;
1142 if (!backing_store_ok(fe
))
1145 if (event
->state
& (GDK_BUTTON2_MASK
| GDK_SHIFT_MASK
))
1146 button
= MIDDLE_DRAG
;
1147 else if (event
->state
& GDK_BUTTON1_MASK
)
1149 else if (event
->state
& GDK_BUTTON3_MASK
)
1150 button
= RIGHT_DRAG
;
1152 return FALSE
; /* don't even know what button! */
1154 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
1155 event
->y
- fe
->oy
, button
))
1156 gtk_widget_destroy(fe
->window
);
1157 #if GTK_CHECK_VERSION(2,12,0)
1158 gdk_event_request_motions(event
);
1160 gdk_window_get_pointer(widget
->window
, NULL
, NULL
, NULL
);
1166 static gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
,
1169 frontend
*fe
= (frontend
*)data
;
1171 if (backing_store_ok(fe
)) {
1172 repaint_rectangle(fe
, widget
,
1173 event
->area
.x
, event
->area
.y
,
1174 event
->area
.width
, event
->area
.height
);
1179 static gint
map_window(GtkWidget
*widget
, GdkEvent
*event
,
1182 frontend
*fe
= (frontend
*)data
;
1185 * Apparently we need to do this because otherwise the status
1186 * bar will fail to update immediately. Annoying, but there we
1189 gtk_widget_queue_draw(fe
->window
);
1194 static gint
configure_area(GtkWidget
*widget
,
1195 GdkEventConfigure
*event
, gpointer data
)
1197 frontend
*fe
= (frontend
*)data
;
1200 if (backing_store_ok(fe
))
1201 teardown_backing_store(fe
);
1203 x
= fe
->w
= event
->width
;
1204 y
= fe
->h
= event
->height
;
1205 midend_size(fe
->me
, &x
, &y
, TRUE
);
1208 fe
->ox
= (fe
->w
- fe
->pw
) / 2;
1209 fe
->oy
= (fe
->h
- fe
->ph
) / 2;
1211 setup_backing_store(fe
);
1212 midend_force_redraw(fe
->me
);
1217 static gint
timer_func(gpointer data
)
1219 frontend
*fe
= (frontend
*)data
;
1221 if (fe
->timer_active
) {
1224 gettimeofday(&now
, NULL
);
1225 elapsed
= ((now
.tv_usec
- fe
->last_time
.tv_usec
) * 0.000001F
+
1226 (now
.tv_sec
- fe
->last_time
.tv_sec
));
1227 midend_timer(fe
->me
, elapsed
); /* may clear timer_active */
1228 fe
->last_time
= now
;
1231 return fe
->timer_active
;
1234 void deactivate_timer(frontend
*fe
)
1237 return; /* can happen due to --generate */
1238 if (fe
->timer_active
)
1239 gtk_timeout_remove(fe
->timer_id
);
1240 fe
->timer_active
= FALSE
;
1243 void activate_timer(frontend
*fe
)
1246 return; /* can happen due to --generate */
1247 if (!fe
->timer_active
) {
1248 fe
->timer_id
= gtk_timeout_add(20, timer_func
, fe
);
1249 gettimeofday(&fe
->last_time
, NULL
);
1251 fe
->timer_active
= TRUE
;
1254 static void window_destroy(GtkWidget
*widget
, gpointer data
)
1259 static void msgbox_button_clicked(GtkButton
*button
, gpointer data
)
1261 GtkWidget
*window
= GTK_WIDGET(data
);
1264 ip
= (int *)gtk_object_get_data(GTK_OBJECT(window
), "user-data");
1265 v
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button
), "user-data"));
1268 gtk_widget_destroy(GTK_WIDGET(data
));
1271 static int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1273 GtkObject
*cancelbutton
= GTK_OBJECT(data
);
1276 * `Escape' effectively clicks the cancel button
1278 if (event
->keyval
== GDK_Escape
) {
1279 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton
), "clicked");
1286 enum { MB_OK
, MB_YESNO
};
1288 int message_box(GtkWidget
*parent
, char *title
, char *msg
, int centre
,
1291 GtkWidget
*window
, *hbox
, *text
, *button
;
1295 window
= gtk_dialog_new();
1296 text
= gtk_label_new(msg
);
1297 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
1298 hbox
= gtk_hbox_new(FALSE
, 0);
1299 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
1300 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
1301 hbox
, FALSE
, FALSE
, 20);
1302 gtk_widget_show(text
);
1303 gtk_widget_show(hbox
);
1304 gtk_window_set_title(GTK_WINDOW(window
), title
);
1305 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
1307 if (type
== MB_OK
) {
1308 titles
= GTK_STOCK_OK
"\0";
1311 assert(type
== MB_YESNO
);
1312 titles
= GTK_STOCK_NO
"\0" GTK_STOCK_YES
"\0";
1319 button
= gtk_button_new_from_stock(titles
);
1320 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
1321 button
, FALSE
, FALSE
, 0);
1322 gtk_widget_show(button
);
1324 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
1325 gtk_window_set_default(GTK_WINDOW(window
), button
);
1328 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
1329 GTK_SIGNAL_FUNC(win_key_press
), button
);
1331 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
1332 GTK_SIGNAL_FUNC(msgbox_button_clicked
), window
);
1333 gtk_object_set_data(GTK_OBJECT(button
), "user-data",
1334 GINT_TO_POINTER(i
));
1335 titles
+= strlen(titles
)+1;
1338 gtk_object_set_data(GTK_OBJECT(window
), "user-data",
1339 GINT_TO_POINTER(&i
));
1340 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
1341 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1342 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
1343 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(parent
));
1344 /* set_transient_window_pos(parent, window); */
1345 gtk_widget_show(window
);
1348 return (type
== MB_YESNO ? i
== 1 : TRUE
);
1351 void error_box(GtkWidget
*parent
, char *msg
)
1353 message_box(parent
, "Error", msg
, FALSE
, MB_OK
);
1356 static void config_ok_button_clicked(GtkButton
*button
, gpointer data
)
1358 frontend
*fe
= (frontend
*)data
;
1361 err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
1364 error_box(fe
->cfgbox
, err
);
1367 gtk_widget_destroy(fe
->cfgbox
);
1372 static void config_cancel_button_clicked(GtkButton
*button
, gpointer data
)
1374 frontend
*fe
= (frontend
*)data
;
1376 gtk_widget_destroy(fe
->cfgbox
);
1379 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1382 * GtkEntry has a nasty habit of eating the Return key, which
1383 * is unhelpful since it doesn't actually _do_ anything with it
1384 * (it calls gtk_widget_activate, but our edit boxes never need
1385 * activating). So I catch Return before GtkEntry sees it, and
1386 * pass it straight on to the parent widget. Effect: hitting
1387 * Return in an edit box will now activate the default button
1388 * in the dialog just like it will everywhere else.
1390 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
1392 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
1393 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
1394 event
, &return_val
);
1400 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
1402 config_item
*i
= (config_item
*)data
;
1405 i
->sval
= dupstr(gtk_entry_get_text(GTK_ENTRY(ed
)));
1408 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
1410 config_item
*i
= (config_item
*)data
;
1412 i
->ival
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb
));
1415 static void droplist_sel(GtkMenuItem
*item
, gpointer data
)
1417 config_item
*i
= (config_item
*)data
;
1419 i
->ival
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
),
1423 static int get_config(frontend
*fe
, int which
)
1425 GtkWidget
*w
, *table
, *cancel
;
1430 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
1431 fe
->cfg_which
= which
;
1434 fe
->cfgbox
= gtk_dialog_new();
1435 gtk_window_set_title(GTK_WINDOW(fe
->cfgbox
), title
);
1438 w
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
1439 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
1440 w
, FALSE
, FALSE
, 0);
1442 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1443 GTK_SIGNAL_FUNC(config_cancel_button_clicked
), fe
);
1446 w
= gtk_button_new_from_stock(GTK_STOCK_OK
);
1447 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
1448 w
, FALSE
, FALSE
, 0);
1450 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
1451 gtk_window_set_default(GTK_WINDOW(fe
->cfgbox
), w
);
1452 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
1453 GTK_SIGNAL_FUNC(config_ok_button_clicked
), fe
);
1455 table
= gtk_table_new(1, 2, FALSE
);
1457 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->vbox
),
1458 table
, FALSE
, FALSE
, 0);
1459 gtk_widget_show(table
);
1461 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
1462 gtk_table_resize(GTK_TABLE(table
), y
+1, 2);
1467 * Edit box with a label beside it.
1470 w
= gtk_label_new(i
->name
);
1471 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
1472 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
1473 GTK_SHRINK
| GTK_FILL
,
1474 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1478 w
= gtk_entry_new();
1479 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
1480 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1481 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1483 gtk_entry_set_text(GTK_ENTRY(w
), i
->sval
);
1484 gtk_signal_connect(GTK_OBJECT(w
), "changed",
1485 GTK_SIGNAL_FUNC(editbox_changed
), i
);
1486 gtk_signal_connect(GTK_OBJECT(w
), "key_press_event",
1487 GTK_SIGNAL_FUNC(editbox_key
), NULL
);
1496 w
= gtk_check_button_new_with_label(i
->name
);
1497 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
1498 GTK_SIGNAL_FUNC(button_toggled
), i
);
1499 gtk_table_attach(GTK_TABLE(table
), w
, 0, 2, y
, y
+1,
1500 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1501 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1503 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w
), i
->ival
);
1509 * Drop-down list (GtkOptionMenu).
1512 w
= gtk_label_new(i
->name
);
1513 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
1514 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
1515 GTK_SHRINK
| GTK_FILL
,
1516 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1520 w
= gtk_option_menu_new();
1521 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
1522 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1523 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1530 GtkWidget
*menuitem
;
1531 GtkWidget
*menu
= gtk_menu_new();
1533 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), menu
);
1541 while (*q
&& *q
!= c
)
1544 name
= snewn(q
-p
+1, char);
1545 strncpy(name
, p
, q
-p
);
1548 if (*q
) q
++; /* eat delimiter */
1550 menuitem
= gtk_menu_item_new_with_label(name
);
1551 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1552 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1553 GINT_TO_POINTER(val
));
1554 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1555 GTK_SIGNAL_FUNC(droplist_sel
), i
);
1556 gtk_widget_show(menuitem
);
1563 gtk_option_menu_set_history(GTK_OPTION_MENU(w
), i
->ival
);
1572 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "destroy",
1573 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1574 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "key_press_event",
1575 GTK_SIGNAL_FUNC(win_key_press
), cancel
);
1576 gtk_window_set_modal(GTK_WINDOW(fe
->cfgbox
), TRUE
);
1577 gtk_window_set_transient_for(GTK_WINDOW(fe
->cfgbox
),
1578 GTK_WINDOW(fe
->window
));
1579 /* set_transient_window_pos(fe->window, fe->cfgbox); */
1580 gtk_widget_show(fe
->cfgbox
);
1588 static void menu_key_event(GtkMenuItem
*menuitem
, gpointer data
)
1590 frontend
*fe
= (frontend
*)data
;
1591 int key
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1593 if (!midend_process_key(fe
->me
, 0, 0, key
))
1594 gtk_widget_destroy(fe
->window
);
1597 static void get_size(frontend
*fe
, int *px
, int *py
)
1602 * Currently I don't want to make the GTK port scale large
1603 * puzzles to fit on the screen. This is because X does permit
1604 * extremely large windows and many window managers provide a
1605 * means of navigating round them, and the users I consulted
1606 * before deciding said that they'd rather have enormous puzzle
1607 * windows spanning multiple screen pages than have them
1608 * shrunk. I could change my mind later or introduce
1609 * configurability; this would be the place to do so, by
1610 * replacing the initial values of x and y with the screen
1615 midend_size(fe
->me
, &x
, &y
, FALSE
);
1620 #if !GTK_CHECK_VERSION(2,0,0)
1621 #define gtk_window_resize(win, x, y) \
1622 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
1626 * Called when any other code in this file has changed the
1627 * selected game parameters.
1629 static void changed_preset(frontend
*fe
)
1631 int n
= midend_which_preset(fe
->me
);
1633 fe
->preset_threaded
= TRUE
;
1634 if (n
< 0 && fe
->preset_custom
) {
1635 gtk_check_menu_item_set_active(
1636 GTK_CHECK_MENU_ITEM(fe
->preset_custom
),
1639 GSList
*gs
= fe
->preset_radio
;
1640 int i
= fe
->n_preset_menu_items
- 1 - n
;
1641 if (fe
->preset_custom
)
1648 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(gs
->data
),
1650 } else for (gs
= fe
->preset_radio
; gs
; gs
= gs
->next
) {
1651 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(gs
->data
),
1655 fe
->preset_threaded
= FALSE
;
1658 * Update the greying on the Copy menu option.
1660 if (fe
->copy_menu_item
) {
1661 int enabled
= midend_can_format_as_text_now(fe
->me
);
1662 gtk_widget_set_sensitive(fe
->copy_menu_item
, enabled
);
1666 static gboolean
not_size_allocated_yet(GtkWidget
*w
)
1669 * This function tests whether a widget has not yet taken up space
1670 * on the screen which it will occupy in future. (Therefore, it
1671 * returns true only if the widget does exist but does not have a
1672 * size allocation. A null widget is already taking up all the
1673 * space it ever will.)
1678 return FALSE
; /* nonexistent widgets aren't a problem */
1680 gtk_widget_get_allocation(w
, &a
);
1681 return a
.height
== 0 || a
.width
== 0;
1684 static void try_shrink_drawing_area(frontend
*fe
)
1686 if (fe
->drawing_area_shrink_pending
&&
1687 !not_size_allocated_yet(fe
->menubar
) &&
1688 !not_size_allocated_yet(fe
->statusbar
)) {
1690 * In order to permit the user to resize the window smaller as
1691 * well as bigger, we call this function after the window size
1692 * has ended up where we want it. This shouldn't shrink the
1693 * window immediately; it just arranges that the next time the
1694 * user tries to shrink it, they can.
1696 * However, at puzzle creation time, we defer the first of
1697 * these operations until after the menu bar and status bar
1698 * are actually visible. On Ubuntu 12.04 I've found that these
1699 * can take a while to be displayed, and that it's a mistake
1700 * to reduce the drawing area's size allocation before they've
1701 * turned up or else the drawing area makes room for them by
1702 * shrinking to less than the size we intended.
1704 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), 1, 1);
1705 fe
->drawing_area_shrink_pending
= FALSE
;
1709 static gint
configure_window(GtkWidget
*widget
,
1710 GdkEventConfigure
*event
, gpointer data
)
1712 frontend
*fe
= (frontend
*)data
;
1714 * When the main puzzle window changes size, it might be because
1715 * the menu bar or status bar has turned up after starting off
1716 * absent, in which case we should have another go at enacting a
1717 * pending shrink of the drawing area.
1719 try_shrink_drawing_area(fe
);
1723 static void resize_fe(frontend
*fe
)
1727 get_size(fe
, &x
, &y
);
1730 fe
->drawing_area_shrink_pending
= FALSE
;
1731 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1734 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1735 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1737 fe
->drawing_area_shrink_pending
= TRUE
;
1738 try_shrink_drawing_area(fe
);
1741 static void menu_preset_event(GtkMenuItem
*menuitem
, gpointer data
)
1743 frontend
*fe
= (frontend
*)data
;
1744 game_params
*params
=
1745 (game_params
*)gtk_object_get_data(GTK_OBJECT(menuitem
), "user-data");
1747 if (fe
->preset_threaded
||
1748 (GTK_IS_CHECK_MENU_ITEM(menuitem
) &&
1749 !gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem
))))
1751 midend_set_params(fe
->me
, params
);
1752 midend_new_game(fe
->me
);
1757 GdkAtom compound_text_atom
, utf8_string_atom
;
1758 int paste_initialised
= FALSE
;
1760 static void set_selection(frontend
*fe
, GdkAtom selection
)
1762 if (!paste_initialised
) {
1763 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
1764 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
1765 paste_initialised
= TRUE
;
1769 * For this simple application we can safely assume that the
1770 * data passed to this function is pure ASCII, which means we
1771 * can return precisely the same stuff for types STRING,
1772 * COMPOUND_TEXT or UTF8_STRING.
1775 if (gtk_selection_owner_set(fe
->area
, selection
, CurrentTime
)) {
1776 gtk_selection_clear_targets(fe
->area
, selection
);
1777 gtk_selection_add_target(fe
->area
, selection
,
1778 GDK_SELECTION_TYPE_STRING
, 1);
1779 gtk_selection_add_target(fe
->area
, selection
, compound_text_atom
, 1);
1780 gtk_selection_add_target(fe
->area
, selection
, utf8_string_atom
, 1);
1784 void write_clip(frontend
*fe
, char *data
)
1787 sfree(fe
->paste_data
);
1789 fe
->paste_data
= data
;
1790 fe
->paste_data_len
= strlen(data
);
1792 set_selection(fe
, GDK_SELECTION_PRIMARY
);
1793 set_selection(fe
, GDK_SELECTION_CLIPBOARD
);
1796 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1797 guint info
, guint time_stamp
, gpointer data
)
1799 frontend
*fe
= (frontend
*)data
;
1800 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1801 fe
->paste_data
, fe
->paste_data_len
);
1804 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1807 frontend
*fe
= (frontend
*)data
;
1810 sfree(fe
->paste_data
);
1811 fe
->paste_data
= NULL
;
1812 fe
->paste_data_len
= 0;
1816 static void menu_copy_event(GtkMenuItem
*menuitem
, gpointer data
)
1818 frontend
*fe
= (frontend
*)data
;
1821 text
= midend_text_format(fe
->me
);
1824 write_clip(fe
, text
);
1832 static void filesel_ok(GtkButton
*button
, gpointer data
)
1834 frontend
*fe
= (frontend
*)data
;
1836 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1839 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1841 fe
->filesel_name
= dupstr(name
);
1844 static char *file_selector(frontend
*fe
, char *title
, int save
)
1846 GtkWidget
*filesel
=
1847 gtk_file_selection_new(title
);
1849 fe
->filesel_name
= NULL
;
1851 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1853 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1856 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1857 GTK_SIGNAL_FUNC(filesel_ok
), fe
);
1858 gtk_signal_connect_object
1859 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1860 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1861 gtk_signal_connect_object
1862 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1863 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1864 gtk_signal_connect(GTK_OBJECT(filesel
), "destroy",
1865 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1866 gtk_widget_show(filesel
);
1867 gtk_window_set_transient_for(GTK_WINDOW(filesel
), GTK_WINDOW(fe
->window
));
1870 return fe
->filesel_name
;
1875 static char *file_selector(frontend
*fe
, char *title
, int save
)
1877 char *filesel_name
= NULL
;
1879 GtkWidget
*filesel
=
1880 gtk_file_chooser_dialog_new(title
,
1881 GTK_WINDOW(fe
->window
),
1882 save ? GTK_FILE_CHOOSER_ACTION_SAVE
:
1883 GTK_FILE_CHOOSER_ACTION_OPEN
,
1884 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1885 save ? GTK_STOCK_SAVE
: GTK_STOCK_OPEN
,
1886 GTK_RESPONSE_ACCEPT
,
1889 if (gtk_dialog_run(GTK_DIALOG(filesel
)) == GTK_RESPONSE_ACCEPT
) {
1890 const char *name
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(filesel
));
1891 filesel_name
= dupstr(name
);
1894 gtk_widget_destroy(filesel
);
1896 return filesel_name
;
1901 struct savefile_write_ctx
{
1906 static void savefile_write(void *wctx
, void *buf
, int len
)
1908 struct savefile_write_ctx
*ctx
= (struct savefile_write_ctx
*)wctx
;
1909 if (fwrite(buf
, 1, len
, ctx
->fp
) < len
)
1913 static int savefile_read(void *wctx
, void *buf
, int len
)
1915 FILE *fp
= (FILE *)wctx
;
1918 ret
= fread(buf
, 1, len
, fp
);
1919 return (ret
== len
);
1922 static void menu_save_event(GtkMenuItem
*menuitem
, gpointer data
)
1924 frontend
*fe
= (frontend
*)data
;
1927 name
= file_selector(fe
, "Enter name of game file to save", TRUE
);
1932 if ((fp
= fopen(name
, "r")) != NULL
) {
1933 char buf
[256 + FILENAME_MAX
];
1937 sprintf(buf
, "Are you sure you want to overwrite the"
1939 FILENAME_MAX
, name
);
1940 if (!message_box(fe
->window
, "Question", buf
, TRUE
, MB_YESNO
))
1944 fp
= fopen(name
, "w");
1948 error_box(fe
->window
, "Unable to open save file");
1953 struct savefile_write_ctx ctx
;
1956 midend_serialise(fe
->me
, savefile_write
, &ctx
);
1960 sprintf(boxmsg
, "Error writing save file: %.400s",
1962 error_box(fe
->window
, boxmsg
);
1970 static void menu_load_event(GtkMenuItem
*menuitem
, gpointer data
)
1972 frontend
*fe
= (frontend
*)data
;
1975 name
= file_selector(fe
, "Enter name of saved game file to load", FALSE
);
1978 FILE *fp
= fopen(name
, "r");
1982 error_box(fe
->window
, "Unable to open saved game file");
1986 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1991 error_box(fe
->window
, err
);
2000 static void menu_solve_event(GtkMenuItem
*menuitem
, gpointer data
)
2002 frontend
*fe
= (frontend
*)data
;
2005 msg
= midend_solve(fe
->me
);
2008 error_box(fe
->window
, msg
);
2011 static void menu_restart_event(GtkMenuItem
*menuitem
, gpointer data
)
2013 frontend
*fe
= (frontend
*)data
;
2015 midend_restart_game(fe
->me
);
2018 static void menu_config_event(GtkMenuItem
*menuitem
, gpointer data
)
2020 frontend
*fe
= (frontend
*)data
;
2021 int which
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
2024 if (fe
->preset_threaded
||
2025 (GTK_IS_CHECK_MENU_ITEM(menuitem
) &&
2026 !gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem
))))
2028 changed_preset(fe
); /* Put the old preset back! */
2029 if (!get_config(fe
, which
))
2032 midend_new_game(fe
->me
);
2036 static void menu_about_event(GtkMenuItem
*menuitem
, gpointer data
)
2038 frontend
*fe
= (frontend
*)data
;
2042 sprintf(titlebuf
, "About %.200s", thegame
.name
);
2045 "from Simon Tatham's Portable Puzzle Collection\n\n"
2046 "%.500s", thegame
.name
, ver
);
2048 message_box(fe
->window
, titlebuf
, textbuf
, TRUE
, MB_OK
);
2051 static GtkWidget
*add_menu_item_with_key(frontend
*fe
, GtkContainer
*cont
,
2052 char *text
, int key
)
2054 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
2056 gtk_container_add(cont
, menuitem
);
2057 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
2058 GINT_TO_POINTER(key
));
2059 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2060 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
2061 switch (key
& ~0x1F) {
2064 keyqual
= GDK_CONTROL_MASK
;
2068 keyqual
= GDK_SHIFT_MASK
;
2074 gtk_widget_add_accelerator(menuitem
,
2075 "activate", fe
->accelgroup
,
2078 gtk_widget_show(menuitem
);
2082 static void add_menu_separator(GtkContainer
*cont
)
2084 GtkWidget
*menuitem
= gtk_menu_item_new();
2085 gtk_container_add(cont
, menuitem
);
2086 gtk_widget_show(menuitem
);
2089 enum { ARG_EITHER
, ARG_SAVE
, ARG_ID
}; /* for argtype */
2091 static frontend
*new_window(char *arg
, int argtype
, char **error
)
2094 GtkBox
*vbox
, *hbox
;
2095 GtkWidget
*menu
, *menuitem
;
2100 extern char *const *const xpm_icons
[];
2101 extern const int n_xpm_icons
;
2103 fe
= snew(frontend
);
2105 fe
->timer_active
= FALSE
;
2108 fe
->me
= midend_new(fe
, &thegame
, >k_drawing
, fe
);
2118 err
= midend_game_id(fe
->me
, arg
);
2120 midend_new_game(fe
->me
);
2122 sprintf(errbuf
, "Invalid game ID: %.800s", err
);
2125 fp
= fopen(arg
, "r");
2127 sprintf(errbuf
, "Error opening file: %.800s", strerror(errno
));
2129 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
2131 sprintf(errbuf
, "Invalid save file: %.800s", err
);
2135 default /*case ARG_EITHER*/:
2137 * First try treating the argument as a game ID.
2139 err
= midend_game_id(fe
->me
, arg
);
2142 * It's a valid game ID.
2144 midend_new_game(fe
->me
);
2146 FILE *fp
= fopen(arg
, "r");
2148 sprintf(errbuf
, "Supplied argument is neither a game ID (%.400s)"
2149 " nor a save file (%.400s)", err
, strerror(errno
));
2151 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
2153 sprintf(errbuf
, "%.800s", err
);
2160 *error
= dupstr(errbuf
);
2161 midend_free(fe
->me
);
2167 midend_new_game(fe
->me
);
2170 fe
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2171 gtk_window_set_title(GTK_WINDOW(fe
->window
), thegame
.name
);
2173 vbox
= GTK_BOX(gtk_vbox_new(FALSE
, 0));
2174 gtk_container_add(GTK_CONTAINER(fe
->window
), GTK_WIDGET(vbox
));
2175 gtk_widget_show(GTK_WIDGET(vbox
));
2177 fe
->accelgroup
= gtk_accel_group_new();
2178 gtk_window_add_accel_group(GTK_WINDOW(fe
->window
), fe
->accelgroup
);
2180 hbox
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
2181 gtk_box_pack_start(vbox
, GTK_WIDGET(hbox
), FALSE
, FALSE
, 0);
2182 gtk_widget_show(GTK_WIDGET(hbox
));
2184 fe
->menubar
= gtk_menu_bar_new();
2185 gtk_box_pack_start(hbox
, fe
->menubar
, TRUE
, TRUE
, 0);
2186 gtk_widget_show(fe
->menubar
);
2188 menuitem
= gtk_menu_item_new_with_mnemonic("_Game");
2189 gtk_container_add(GTK_CONTAINER(fe
->menubar
), menuitem
);
2190 gtk_widget_show(menuitem
);
2192 menu
= gtk_menu_new();
2193 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
2195 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "New", 'n');
2197 menuitem
= gtk_menu_item_new_with_label("Restart");
2198 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
2199 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2200 GTK_SIGNAL_FUNC(menu_restart_event
), fe
);
2201 gtk_widget_show(menuitem
);
2203 menuitem
= gtk_menu_item_new_with_label("Specific...");
2204 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
2205 GINT_TO_POINTER(CFG_DESC
));
2206 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
2207 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2208 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
2209 gtk_widget_show(menuitem
);
2211 menuitem
= gtk_menu_item_new_with_label("Random Seed...");
2212 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
2213 GINT_TO_POINTER(CFG_SEED
));
2214 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
2215 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2216 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
2217 gtk_widget_show(menuitem
);
2219 fe
->preset_radio
= NULL
;
2220 fe
->preset_custom
= NULL
;
2221 fe
->n_preset_menu_items
= 0;
2222 fe
->preset_threaded
= FALSE
;
2223 if ((n
= midend_num_presets(fe
->me
)) > 0 || thegame
.can_configure
) {
2227 menuitem
= gtk_menu_item_new_with_mnemonic("_Type");
2228 gtk_container_add(GTK_CONTAINER(fe
->menubar
), menuitem
);
2229 gtk_widget_show(menuitem
);
2231 submenu
= gtk_menu_new();
2232 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), submenu
);
2234 for (i
= 0; i
< n
; i
++) {
2236 game_params
*params
;
2238 midend_fetch_preset(fe
->me
, i
, &name
, ¶ms
);
2241 gtk_radio_menu_item_new_with_label(fe
->preset_radio
, name
);
2243 gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(menuitem
));
2244 fe
->n_preset_menu_items
++;
2245 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
2246 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", params
);
2247 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2248 GTK_SIGNAL_FUNC(menu_preset_event
), fe
);
2249 gtk_widget_show(menuitem
);
2252 if (thegame
.can_configure
) {
2253 menuitem
= fe
->preset_custom
=
2254 gtk_radio_menu_item_new_with_label(fe
->preset_radio
,
2257 gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(menuitem
));
2258 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
2259 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
2260 GPOINTER_TO_INT(CFG_SETTINGS
));
2261 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2262 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
2263 gtk_widget_show(menuitem
);
2268 add_menu_separator(GTK_CONTAINER(menu
));
2269 menuitem
= gtk_menu_item_new_with_label("Load...");
2270 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
2271 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2272 GTK_SIGNAL_FUNC(menu_load_event
), fe
);
2273 gtk_widget_show(menuitem
);
2274 menuitem
= gtk_menu_item_new_with_label("Save...");
2275 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
2276 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2277 GTK_SIGNAL_FUNC(menu_save_event
), fe
);
2278 gtk_widget_show(menuitem
);
2279 #ifndef STYLUS_BASED
2280 add_menu_separator(GTK_CONTAINER(menu
));
2281 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Undo", 'u');
2282 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Redo", 'r');
2284 if (thegame
.can_format_as_text_ever
) {
2285 add_menu_separator(GTK_CONTAINER(menu
));
2286 menuitem
= gtk_menu_item_new_with_label("Copy");
2287 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
2288 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2289 GTK_SIGNAL_FUNC(menu_copy_event
), fe
);
2290 gtk_widget_show(menuitem
);
2291 fe
->copy_menu_item
= menuitem
;
2293 fe
->copy_menu_item
= NULL
;
2295 if (thegame
.can_solve
) {
2296 add_menu_separator(GTK_CONTAINER(menu
));
2297 menuitem
= gtk_menu_item_new_with_label("Solve");
2298 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
2299 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2300 GTK_SIGNAL_FUNC(menu_solve_event
), fe
);
2301 gtk_widget_show(menuitem
);
2303 add_menu_separator(GTK_CONTAINER(menu
));
2304 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Exit", 'q');
2306 menuitem
= gtk_menu_item_new_with_mnemonic("_Help");
2307 gtk_container_add(GTK_CONTAINER(fe
->menubar
), menuitem
);
2308 gtk_widget_show(menuitem
);
2310 menu
= gtk_menu_new();
2311 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
2313 menuitem
= gtk_menu_item_new_with_label("About");
2314 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
2315 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2316 GTK_SIGNAL_FUNC(menu_about_event
), fe
);
2317 gtk_widget_show(menuitem
);
2320 menuitem
=gtk_button_new_with_mnemonic("_Redo");
2321 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
2322 GINT_TO_POINTER((int)('r')));
2323 gtk_signal_connect(GTK_OBJECT(menuitem
), "clicked",
2324 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
2325 gtk_box_pack_end(hbox
, menuitem
, FALSE
, FALSE
, 0);
2326 gtk_widget_show(menuitem
);
2328 menuitem
=gtk_button_new_with_mnemonic("_Undo");
2329 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
2330 GINT_TO_POINTER((int)('u')));
2331 gtk_signal_connect(GTK_OBJECT(menuitem
), "clicked",
2332 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
2333 gtk_box_pack_end(hbox
, menuitem
, FALSE
, FALSE
, 0);
2334 gtk_widget_show(menuitem
);
2336 if (thegame
.flags
& REQUIRE_NUMPAD
) {
2337 hbox
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
2338 gtk_box_pack_start(vbox
, GTK_WIDGET(hbox
), FALSE
, FALSE
, 0);
2339 gtk_widget_show(GTK_WIDGET(hbox
));
2343 for(errbuf
[0]='0';errbuf
[0]<='9';errbuf
[0]++) {
2344 menuitem
=gtk_button_new_with_label(errbuf
);
2345 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
2346 GINT_TO_POINTER((int)(errbuf
[0])));
2347 gtk_signal_connect(GTK_OBJECT(menuitem
), "clicked",
2348 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
2349 gtk_box_pack_start(hbox
, menuitem
, TRUE
, TRUE
, 0);
2350 gtk_widget_show(menuitem
);
2353 #endif /* STYLUS_BASED */
2357 snaffle_colours(fe
);
2359 if (midend_wants_statusbar(fe
->me
)) {
2360 GtkWidget
*viewport
;
2363 viewport
= gtk_viewport_new(NULL
, NULL
);
2364 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport
), GTK_SHADOW_NONE
);
2365 fe
->statusbar
= gtk_statusbar_new();
2366 gtk_container_add(GTK_CONTAINER(viewport
), fe
->statusbar
);
2367 gtk_widget_show(viewport
);
2368 gtk_box_pack_end(vbox
, viewport
, FALSE
, FALSE
, 0);
2369 gtk_widget_show(fe
->statusbar
);
2370 fe
->statusctx
= gtk_statusbar_get_context_id
2371 (GTK_STATUSBAR(fe
->statusbar
), "game");
2372 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
,
2374 gtk_widget_size_request(fe
->statusbar
, &req
);
2376 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
2378 gtk_widget_set_usize(viewport
, -1, req
.height
);
2380 fe
->statusbar
= NULL
;
2382 fe
->area
= gtk_drawing_area_new();
2383 #if GTK_CHECK_VERSION(2,0,0)
2384 GTK_WIDGET_UNSET_FLAGS(fe
->area
, GTK_DOUBLE_BUFFERED
);
2386 get_size(fe
, &x
, &y
);
2387 fe
->drawing_area_shrink_pending
= FALSE
;
2388 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
2392 gtk_box_pack_end(vbox
, fe
->area
, TRUE
, TRUE
, 0);
2394 clear_backing_store(fe
);
2396 fe
->nfonts
= fe
->fontsize
= 0;
2398 fe
->paste_data
= NULL
;
2399 fe
->paste_data_len
= 0;
2401 gtk_signal_connect(GTK_OBJECT(fe
->window
), "destroy",
2402 GTK_SIGNAL_FUNC(destroy
), fe
);
2403 gtk_signal_connect(GTK_OBJECT(fe
->window
), "key_press_event",
2404 GTK_SIGNAL_FUNC(key_event
), fe
);
2405 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_press_event",
2406 GTK_SIGNAL_FUNC(button_event
), fe
);
2407 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_release_event",
2408 GTK_SIGNAL_FUNC(button_event
), fe
);
2409 gtk_signal_connect(GTK_OBJECT(fe
->area
), "motion_notify_event",
2410 GTK_SIGNAL_FUNC(motion_event
), fe
);
2411 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_get",
2412 GTK_SIGNAL_FUNC(selection_get
), fe
);
2413 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_clear_event",
2414 GTK_SIGNAL_FUNC(selection_clear
), fe
);
2415 gtk_signal_connect(GTK_OBJECT(fe
->area
), "expose_event",
2416 GTK_SIGNAL_FUNC(expose_area
), fe
);
2417 gtk_signal_connect(GTK_OBJECT(fe
->window
), "map_event",
2418 GTK_SIGNAL_FUNC(map_window
), fe
);
2419 gtk_signal_connect(GTK_OBJECT(fe
->area
), "configure_event",
2420 GTK_SIGNAL_FUNC(configure_area
), fe
);
2421 gtk_signal_connect(GTK_OBJECT(fe
->window
), "configure_event",
2422 GTK_SIGNAL_FUNC(configure_window
), fe
);
2424 gtk_widget_add_events(GTK_WIDGET(fe
->area
),
2425 GDK_BUTTON_PRESS_MASK
|
2426 GDK_BUTTON_RELEASE_MASK
|
2427 GDK_BUTTON_MOTION_MASK
|
2428 GDK_POINTER_MOTION_HINT_MASK
);
2431 gtk_widget_realize(fe
->window
);
2432 iconpm
= gdk_pixmap_create_from_xpm_d(fe
->window
->window
, NULL
,
2433 NULL
, (gchar
**)xpm_icons
[0]);
2434 gdk_window_set_icon(fe
->window
->window
, NULL
, iconpm
, NULL
);
2436 for (n
= 0; n
< n_xpm_icons
; n
++) {
2438 g_list_append(iconlist
,
2439 gdk_pixbuf_new_from_xpm_data((const gchar
**)
2442 gdk_window_set_icon_list(fe
->window
->window
, iconlist
);
2445 gtk_widget_show(fe
->area
);
2446 gtk_widget_show(fe
->window
);
2448 fe
->drawing_area_shrink_pending
= TRUE
;
2449 try_shrink_drawing_area(fe
);
2450 set_window_background(fe
, 0);
2455 char *fgetline(FILE *fp
)
2457 char *ret
= snewn(512, char);
2458 int size
= 512, len
= 0;
2459 while (fgets(ret
+ len
, size
- len
, fp
)) {
2460 len
+= strlen(ret
+ len
);
2461 if (ret
[len
-1] == '\n')
2462 break; /* got a newline, we're done */
2464 ret
= sresize(ret
, size
, char);
2466 if (len
== 0) { /* first fgets returned NULL */
2474 int main(int argc
, char **argv
)
2476 char *pname
= argv
[0];
2478 int ngenerate
= 0, print
= FALSE
, px
= 1, py
= 1;
2479 int soln
= FALSE
, colour
= FALSE
;
2481 float redo_proportion
= 0.0F
;
2482 char *savefile
= NULL
, *savesuffix
= NULL
;
2484 int argtype
= ARG_EITHER
;
2485 char *screenshot_file
= NULL
;
2486 int doing_opts
= TRUE
;
2492 * Command line parsing in this function is rather fiddly,
2493 * because GTK wants to have a go at argc/argv _first_ - and
2494 * yet we can't let it, because gtk_init() will bomb out if it
2495 * can't open an X display, whereas in fact we want to permit
2496 * our --generate and --print modes to run without an X
2500 * - we parse the command line ourselves, without modifying
2502 * - if we encounter an error which might plausibly be the
2503 * result of a GTK command line (i.e. not detailed errors in
2504 * particular options of ours) we store the error message
2505 * and terminate parsing.
2506 * - if we got enough out of the command line to know it
2507 * specifies a non-X mode of operation, we either display
2508 * the stored error and return failure, or if there is no
2509 * stored error we do the non-X operation and return
2511 * - otherwise, we go straight to gtk_init().
2517 if (doing_opts
&& !strcmp(p
, "--version")) {
2518 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
2521 } else if (doing_opts
&& !strcmp(p
, "--generate")) {
2523 ngenerate
= atoi(*++av
);
2525 fprintf(stderr
, "%s: '--generate' expected a number\n",
2531 } else if (doing_opts
&& !strcmp(p
, "--save")) {
2535 fprintf(stderr
, "%s: '--save' expected a filename\n",
2539 } else if (doing_opts
&& (!strcmp(p
, "--save-suffix") ||
2540 !strcmp(p
, "--savesuffix"))) {
2544 fprintf(stderr
, "%s: '--save-suffix' expected a filename\n",
2548 } else if (doing_opts
&& !strcmp(p
, "--print")) {
2549 if (!thegame
.can_print
) {
2550 fprintf(stderr
, "%s: this game does not support printing\n",
2557 if (sscanf(dim
, "%dx%d", &px
, &py
) != 2) {
2558 fprintf(stderr
, "%s: unable to parse argument '%s' to "
2559 "'--print'\n", pname
, dim
);
2565 } else if (doing_opts
&& !strcmp(p
, "--scale")) {
2567 scale
= atof(*++av
);
2569 fprintf(stderr
, "%s: no argument supplied to '--scale'\n",
2573 } else if (doing_opts
&& !strcmp(p
, "--redo")) {
2575 * This is an internal option which I don't expect
2576 * users to have any particular use for. The effect of
2577 * --redo is that once the game has been loaded and
2578 * initialised, the next move in the redo chain is
2579 * replayed, and the game screen is redrawn part way
2580 * through the making of the move. This is only
2581 * meaningful if there _is_ a next move in the redo
2582 * chain, which means in turn that this option is only
2583 * useful if you're also passing a save file on the
2586 * This option is used by the script which generates
2587 * the puzzle icons and website screenshots, and I
2588 * don't imagine it's useful for anything else.
2589 * (Unless, I suppose, users don't like my screenshots
2590 * and want to generate their own in the same way for
2591 * some repackaged version of the puzzles.)
2594 redo_proportion
= atof(*++av
);
2596 fprintf(stderr
, "%s: no argument supplied to '--redo'\n",
2600 } else if (doing_opts
&& !strcmp(p
, "--screenshot")) {
2602 * Another internal option for the icon building
2603 * script. This causes a screenshot of the central
2604 * drawing area (i.e. not including the menu bar or
2605 * status bar) to be saved to a PNG file once the
2606 * window has been drawn, and then the application
2607 * quits immediately.
2610 screenshot_file
= *++av
;
2612 fprintf(stderr
, "%s: no argument supplied to '--screenshot'\n",
2616 } else if (doing_opts
&& (!strcmp(p
, "--with-solutions") ||
2617 !strcmp(p
, "--with-solution") ||
2618 !strcmp(p
, "--with-solns") ||
2619 !strcmp(p
, "--with-soln") ||
2620 !strcmp(p
, "--solutions") ||
2621 !strcmp(p
, "--solution") ||
2622 !strcmp(p
, "--solns") ||
2623 !strcmp(p
, "--soln"))) {
2625 } else if (doing_opts
&& !strcmp(p
, "--colour")) {
2626 if (!thegame
.can_print_in_colour
) {
2627 fprintf(stderr
, "%s: this game does not support colour"
2628 " printing\n", pname
);
2632 } else if (doing_opts
&& !strcmp(p
, "--load")) {
2634 } else if (doing_opts
&& !strcmp(p
, "--game")) {
2636 } else if (doing_opts
&& !strcmp(p
, "--")) {
2638 } else if (!doing_opts
|| p
[0] != '-') {
2640 fprintf(stderr
, "%s: more than one argument supplied\n",
2646 sprintf(errbuf
, "%.100s: unrecognised option '%.100s'\n",
2653 fputs(errbuf
, stderr
);
2658 * Special standalone mode for generating puzzle IDs on the
2659 * command line. Useful for generating puzzles to be printed
2660 * out and solved offline (for puzzles where that even makes
2661 * sense - Solo, for example, is a lot more pencil-and-paper
2662 * friendly than Twiddle!)
2666 * <puzzle-name> --generate [<n> [<params>]]
2668 * <n>, if present, is the number of puzzle IDs to generate.
2669 * <params>, if present, is the same type of parameter string
2670 * you would pass to the puzzle when running it in GUI mode,
2671 * including optional extras such as the expansion factor in
2672 * Rectangles and the difficulty level in Solo.
2674 * If you specify <params>, you must also specify <n> (although
2675 * you may specify it to be 1). Sorry; that was the
2676 * simplest-to-parse command-line syntax I came up with.
2678 if (ngenerate
> 0 || print
|| savefile
|| savesuffix
) {
2682 document
*doc
= NULL
;
2686 me
= midend_new(NULL
, &thegame
, NULL
, NULL
);
2689 if (savefile
&& !savesuffix
)
2691 if (!savefile
&& savesuffix
)
2695 doc
= document_new(px
, py
, scale
);
2698 * In this loop, we either generate a game ID or read one
2699 * from stdin depending on whether we're in generate mode;
2700 * then we either write it to stdout or print it, depending
2701 * on whether we're in print mode. Thus, this loop handles
2702 * generate-to-stdout, print-from-stdin and generate-and-
2703 * immediately-print modes.
2705 * (It could also handle a copy-stdin-to-stdout mode,
2706 * although there's currently no combination of options
2707 * which will cause this loop to be activated in that mode.
2708 * It wouldn't be _entirely_ pointless, though, because
2709 * stdin could contain bare params strings or random-seed
2710 * IDs, and stdout would contain nothing but fully
2711 * generated descriptive game IDs.)
2713 while (ngenerate
== 0 || i
< n
) {
2716 if (ngenerate
== 0) {
2717 pstr
= fgetline(stdin
);
2720 pstr
[strcspn(pstr
, "\r\n")] = '\0';
2723 pstr
= snewn(strlen(arg
) + 40, char);
2726 if (i
> 0 && strchr(arg
, '#'))
2727 sprintf(pstr
+ strlen(pstr
), "-%d", i
);
2733 err
= midend_game_id(me
, pstr
);
2735 fprintf(stderr
, "%s: error parsing '%s': %s\n",
2742 midend_new_game(me
);
2745 err
= midend_print_puzzle(me
, doc
, soln
);
2747 fprintf(stderr
, "%s: error in printing: %s\n", pname
, err
);
2752 struct savefile_write_ctx ctx
;
2753 char *realname
= snewn(40 + strlen(savefile
) +
2754 strlen(savesuffix
), char);
2755 sprintf(realname
, "%s%d%s", savefile
, i
, savesuffix
);
2758 char *err
= midend_solve(me
);
2760 fprintf(stderr
, "%s: unable to show solution: %s\n",
2766 ctx
.fp
= fopen(realname
, "w");
2768 fprintf(stderr
, "%s: open: %s\n", realname
,
2773 midend_serialise(me
, savefile_write
, &ctx
);
2775 fprintf(stderr
, "%s: write: %s\n", realname
,
2776 strerror(ctx
.error
));
2779 if (fclose(ctx
.fp
)) {
2780 fprintf(stderr
, "%s: close: %s\n", realname
,
2786 if (!doc
&& !savefile
) {
2787 id
= midend_get_game_id(me
);
2796 psdata
*ps
= ps_init(stdout
, colour
);
2797 document_print(doc
, ps_drawing_api(ps
));
2808 gtk_init(&argc
, &argv
);
2810 fe
= new_window(arg
, argtype
, &error
);
2813 fprintf(stderr
, "%s: %s\n", pname
, error
);
2817 if (screenshot_file
) {
2819 * Some puzzles will not redraw their entire area if
2820 * given a partially completed animation, which means
2821 * we must redraw now and _then_ redraw again after
2822 * freezing the move timer.
2824 midend_force_redraw(fe
->me
);
2827 if (redo_proportion
) {
2829 midend_process_key(fe
->me
, 0, 0, 'r');
2830 /* And freeze the timer at the specified position. */
2831 midend_freeze_timer(fe
->me
, redo_proportion
);
2834 if (screenshot_file
) {
2835 save_screenshot_png(fe
, screenshot_file
);