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
37 static FILE *debug_fp
= NULL
;
42 debug_fp
= fopen("debug.log", "w");
53 void debug_printf(char *fmt
, ...)
59 vsprintf(buf
, fmt
, ap
);
65 /* ----------------------------------------------------------------------
66 * Error reporting functions used elsewhere.
69 void fatal(char *fmt
, ...)
73 fprintf(stderr
, "fatal error: ");
76 vfprintf(stderr
, fmt
, ap
);
79 fprintf(stderr
, "\n");
83 /* ----------------------------------------------------------------------
84 * GTK front end to puzzles.
87 static void changed_preset(frontend
*fe
);
91 PangoFontDescription
*desc
;
100 * This structure holds all the data relevant to a single window.
101 * In principle this would allow us to open multiple independent
102 * puzzle windows, although I can't currently see any real point in
103 * doing so. I'm just coding cleanly because there's no
104 * particularly good reason not to.
108 GtkAccelGroup
*accelgroup
;
110 GtkWidget
*statusbar
;
119 int bbox_l
, bbox_r
, bbox_u
, bbox_d
;
120 int timer_active
, timer_id
;
121 struct timeval last_time
;
123 int nfonts
, fontsize
;
125 int cfg_which
, cfgret
;
129 int pw
, ph
; /* pixmap size (w, h are area size) */
130 int ox
, oy
; /* offset of pixmap in drawing area */
133 GtkWidget
**preset_bullets
;
134 GtkWidget
*preset_custom_bullet
;
135 GtkWidget
*copy_menu_item
;
138 void get_random_seed(void **randseed
, int *randseedsize
)
140 struct timeval
*tvp
= snew(struct timeval
);
141 gettimeofday(tvp
, NULL
);
142 *randseed
= (void *)tvp
;
143 *randseedsize
= sizeof(struct timeval
);
146 void frontend_default_colour(frontend
*fe
, float *output
)
148 GdkColor col
= fe
->window
->style
->bg
[GTK_STATE_NORMAL
];
149 output
[0] = col
.red
/ 65535.0;
150 output
[1] = col
.green
/ 65535.0;
151 output
[2] = col
.blue
/ 65535.0;
154 void gtk_status_bar(void *handle
, char *text
)
156 frontend
*fe
= (frontend
*)handle
;
158 assert(fe
->statusbar
);
160 gtk_statusbar_pop(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
);
161 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
, text
);
164 void gtk_start_draw(void *handle
)
166 frontend
*fe
= (frontend
*)handle
;
167 fe
->gc
= gdk_gc_new(fe
->area
->window
);
174 void gtk_clip(void *handle
, int x
, int y
, int w
, int h
)
176 frontend
*fe
= (frontend
*)handle
;
184 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
187 void gtk_unclip(void *handle
)
189 frontend
*fe
= (frontend
*)handle
;
197 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
200 void gtk_draw_text(void *handle
, int x
, int y
, int fonttype
, int fontsize
,
201 int align
, int colour
, char *text
)
203 frontend
*fe
= (frontend
*)handle
;
207 * Find or create the font.
209 for (i
= 0; i
< fe
->nfonts
; i
++)
210 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
213 if (i
== fe
->nfonts
) {
214 if (fe
->fontsize
<= fe
->nfonts
) {
215 fe
->fontsize
= fe
->nfonts
+ 10;
216 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
221 fe
->fonts
[i
].type
= fonttype
;
222 fe
->fonts
[i
].size
= fontsize
;
226 * Use Pango to find the closest match to the requested
230 PangoFontDescription
*fd
;
232 fd
= pango_font_description_new();
233 /* `Monospace' and `Sans' are meta-families guaranteed to exist */
234 pango_font_description_set_family(fd
, fonttype
== FONT_FIXED ?
235 "Monospace" : "Sans");
236 pango_font_description_set_weight(fd
, PANGO_WEIGHT_BOLD
);
238 * I found some online Pango documentation which
239 * described a function called
240 * pango_font_description_set_absolute_size(), which is
241 * _exactly_ what I want here. Unfortunately, none of
242 * my local Pango installations have it (presumably
243 * they're too old), so I'm going to have to hack round
244 * it by figuring out the point size myself. This
245 * limits me to X and probably also breaks in later
246 * Pango installations, so ideally I should add another
247 * CHECK_VERSION type ifdef and use set_absolute_size
248 * where available. All very annoying.
250 #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
251 pango_font_description_set_absolute_size(fd
, PANGO_SCALE
*fontsize
);
254 Display
*d
= GDK_DISPLAY();
255 int s
= DefaultScreen(d
);
257 (PANGO_SCALE
* 72.27 / 25.4) *
258 ((double) DisplayWidthMM(d
, s
) / DisplayWidth (d
, s
));
259 pango_font_description_set_size(fd
, resolution
* fontsize
);
262 fe
->fonts
[i
].desc
= fd
;
267 * In GTK 1.2, I don't know of any plausible way to
268 * pick a suitable font, so I'm just going to be
271 fe
->fonts
[i
].font
= gdk_font_load(fonttype
== FONT_FIXED ?
272 "fixed" : "variable");
280 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
291 layout
= pango_layout_new(gtk_widget_get_pango_context(fe
->area
));
292 pango_layout_set_font_description(layout
, fe
->fonts
[i
].desc
);
293 pango_layout_set_text(layout
, text
, strlen(text
));
294 pango_layout_get_pixel_extents(layout
, NULL
, &rect
);
296 if (align
& ALIGN_VCENTRE
)
297 rect
.y
-= rect
.height
/ 2;
299 rect
.y
-= rect
.height
;
301 if (align
& ALIGN_HCENTRE
)
302 rect
.x
-= rect
.width
/ 2;
303 else if (align
& ALIGN_HRIGHT
)
304 rect
.x
-= rect
.width
;
306 gdk_draw_layout(fe
->pixmap
, fe
->gc
, rect
.x
+ x
, rect
.y
+ y
, layout
);
308 g_object_unref(layout
);
313 * Find string dimensions and process alignment.
316 int lb
, rb
, wid
, asc
, desc
;
319 * Measure vertical string extents with respect to the same
322 gdk_string_extents(fe
->fonts
[i
].font
,
323 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
324 &lb
, &rb
, &wid
, &asc
, &desc
);
325 if (align
& ALIGN_VCENTRE
)
326 y
+= asc
- (asc
+desc
)/2;
331 * ... but horizontal extents with respect to the provided
332 * string. This means that multiple pieces of text centred
333 * on the same y-coordinate don't have different baselines.
335 gdk_string_extents(fe
->fonts
[i
].font
, text
,
336 &lb
, &rb
, &wid
, &asc
, &desc
);
338 if (align
& ALIGN_HCENTRE
)
340 else if (align
& ALIGN_HRIGHT
)
346 * Actually draw the text.
348 gdk_draw_string(fe
->pixmap
, fe
->fonts
[i
].font
, fe
->gc
, x
, y
, text
);
353 void gtk_draw_rect(void *handle
, int x
, int y
, int w
, int h
, int colour
)
355 frontend
*fe
= (frontend
*)handle
;
356 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
357 gdk_draw_rectangle(fe
->pixmap
, fe
->gc
, 1, x
, y
, w
, h
);
360 void gtk_draw_line(void *handle
, int x1
, int y1
, int x2
, int y2
, int colour
)
362 frontend
*fe
= (frontend
*)handle
;
363 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
364 gdk_draw_line(fe
->pixmap
, fe
->gc
, x1
, y1
, x2
, y2
);
367 void gtk_draw_poly(void *handle
, int *coords
, int npoints
,
368 int fillcolour
, int outlinecolour
)
370 frontend
*fe
= (frontend
*)handle
;
371 GdkPoint
*points
= snewn(npoints
, GdkPoint
);
374 for (i
= 0; i
< npoints
; i
++) {
375 points
[i
].x
= coords
[i
*2];
376 points
[i
].y
= coords
[i
*2+1];
379 if (fillcolour
>= 0) {
380 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[fillcolour
]);
381 gdk_draw_polygon(fe
->pixmap
, fe
->gc
, TRUE
, points
, npoints
);
383 assert(outlinecolour
>= 0);
384 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[outlinecolour
]);
387 * In principle we ought to be able to use gdk_draw_polygon for
388 * the outline as well. In fact, it turns out to interact badly
389 * with a clipping region, for no terribly obvious reason, so I
390 * draw the outline as a sequence of lines instead.
392 for (i
= 0; i
< npoints
; i
++)
393 gdk_draw_line(fe
->pixmap
, fe
->gc
,
394 points
[i
].x
, points
[i
].y
,
395 points
[(i
+1)%npoints
].x
, points
[(i
+1)%npoints
].y
);
400 void gtk_draw_circle(void *handle
, int cx
, int cy
, int radius
,
401 int fillcolour
, int outlinecolour
)
403 frontend
*fe
= (frontend
*)handle
;
404 if (fillcolour
>= 0) {
405 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[fillcolour
]);
406 gdk_draw_arc(fe
->pixmap
, fe
->gc
, TRUE
,
407 cx
- radius
, cy
- radius
,
408 2 * radius
, 2 * radius
, 0, 360 * 64);
411 assert(outlinecolour
>= 0);
412 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[outlinecolour
]);
413 gdk_draw_arc(fe
->pixmap
, fe
->gc
, FALSE
,
414 cx
- radius
, cy
- radius
,
415 2 * radius
, 2 * radius
, 0, 360 * 64);
423 blitter
*gtk_blitter_new(void *handle
, int w
, int h
)
426 * We can't create the pixmap right now, because fe->window
427 * might not yet exist. So we just cache w and h and create it
428 * during the firs call to blitter_save.
430 blitter
*bl
= snew(blitter
);
437 void gtk_blitter_free(void *handle
, blitter
*bl
)
440 gdk_pixmap_unref(bl
->pixmap
);
444 void gtk_blitter_save(void *handle
, blitter
*bl
, int x
, int y
)
446 frontend
*fe
= (frontend
*)handle
;
448 bl
->pixmap
= gdk_pixmap_new(fe
->area
->window
, bl
->w
, bl
->h
, -1);
451 gdk_draw_pixmap(bl
->pixmap
,
452 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
454 x
, y
, 0, 0, bl
->w
, bl
->h
);
457 void gtk_blitter_load(void *handle
, blitter
*bl
, int x
, int y
)
459 frontend
*fe
= (frontend
*)handle
;
461 if (x
== BLITTER_FROMSAVED
&& y
== BLITTER_FROMSAVED
) {
465 gdk_draw_pixmap(fe
->pixmap
,
466 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
468 0, 0, x
, y
, bl
->w
, bl
->h
);
471 void gtk_draw_update(void *handle
, int x
, int y
, int w
, int h
)
473 frontend
*fe
= (frontend
*)handle
;
474 if (fe
->bbox_l
> x
) fe
->bbox_l
= x
;
475 if (fe
->bbox_r
< x
+w
) fe
->bbox_r
= x
+w
;
476 if (fe
->bbox_u
> y
) fe
->bbox_u
= y
;
477 if (fe
->bbox_d
< y
+h
) fe
->bbox_d
= y
+h
;
480 void gtk_end_draw(void *handle
)
482 frontend
*fe
= (frontend
*)handle
;
483 gdk_gc_unref(fe
->gc
);
486 if (fe
->bbox_l
< fe
->bbox_r
&& fe
->bbox_u
< fe
->bbox_d
) {
487 gdk_draw_pixmap(fe
->area
->window
,
488 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
490 fe
->bbox_l
, fe
->bbox_u
,
491 fe
->ox
+ fe
->bbox_l
, fe
->oy
+ fe
->bbox_u
,
492 fe
->bbox_r
- fe
->bbox_l
, fe
->bbox_d
- fe
->bbox_u
);
496 const struct drawing_api gtk_drawing
= {
512 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, /* {begin,end}_{doc,page,puzzle} */
513 NULL
, /* line_width */
516 static void destroy(GtkWidget
*widget
, gpointer data
)
518 frontend
*fe
= (frontend
*)data
;
519 deactivate_timer(fe
);
524 static gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
526 frontend
*fe
= (frontend
*)data
;
528 int shift
= (event
->state
& GDK_SHIFT_MASK
) ? MOD_SHFT
: 0;
529 int ctrl
= (event
->state
& GDK_CONTROL_MASK
) ? MOD_CTRL
: 0;
534 #if !GTK_CHECK_VERSION(2,0,0)
535 /* Gtk 1.2 passes a key event to this function even if it's also
536 * defined as an accelerator.
537 * Gtk 2 doesn't do this, and this function appears not to exist there. */
538 if (fe
->accelgroup
&&
539 gtk_accel_group_get_entry(fe
->accelgroup
,
540 event
->keyval
, event
->state
))
544 if (event
->keyval
== GDK_Up
)
545 keyval
= shift
| ctrl
| CURSOR_UP
;
546 else if (event
->keyval
== GDK_KP_Up
|| event
->keyval
== GDK_KP_8
)
547 keyval
= MOD_NUM_KEYPAD
| '8';
548 else if (event
->keyval
== GDK_Down
)
549 keyval
= shift
| ctrl
| CURSOR_DOWN
;
550 else if (event
->keyval
== GDK_KP_Down
|| event
->keyval
== GDK_KP_2
)
551 keyval
= MOD_NUM_KEYPAD
| '2';
552 else if (event
->keyval
== GDK_Left
)
553 keyval
= shift
| ctrl
| CURSOR_LEFT
;
554 else if (event
->keyval
== GDK_KP_Left
|| event
->keyval
== GDK_KP_4
)
555 keyval
= MOD_NUM_KEYPAD
| '4';
556 else if (event
->keyval
== GDK_Right
)
557 keyval
= shift
| ctrl
| CURSOR_RIGHT
;
558 else if (event
->keyval
== GDK_KP_Right
|| event
->keyval
== GDK_KP_6
)
559 keyval
= MOD_NUM_KEYPAD
| '6';
560 else if (event
->keyval
== GDK_KP_Home
|| event
->keyval
== GDK_KP_7
)
561 keyval
= MOD_NUM_KEYPAD
| '7';
562 else if (event
->keyval
== GDK_KP_End
|| event
->keyval
== GDK_KP_1
)
563 keyval
= MOD_NUM_KEYPAD
| '1';
564 else if (event
->keyval
== GDK_KP_Page_Up
|| event
->keyval
== GDK_KP_9
)
565 keyval
= MOD_NUM_KEYPAD
| '9';
566 else if (event
->keyval
== GDK_KP_Page_Down
|| event
->keyval
== GDK_KP_3
)
567 keyval
= MOD_NUM_KEYPAD
| '3';
568 else if (event
->keyval
== GDK_KP_Insert
|| event
->keyval
== GDK_KP_0
)
569 keyval
= MOD_NUM_KEYPAD
| '0';
570 else if (event
->keyval
== GDK_KP_Begin
|| event
->keyval
== GDK_KP_5
)
571 keyval
= MOD_NUM_KEYPAD
| '5';
572 else if (event
->keyval
== GDK_BackSpace
||
573 event
->keyval
== GDK_Delete
||
574 event
->keyval
== GDK_KP_Delete
)
576 else if (event
->string
[0] && !event
->string
[1])
577 keyval
= (unsigned char)event
->string
[0];
582 !midend_process_key(fe
->me
, 0, 0, keyval
))
583 gtk_widget_destroy(fe
->window
);
588 static gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
,
591 frontend
*fe
= (frontend
*)data
;
597 if (event
->type
!= GDK_BUTTON_PRESS
&& event
->type
!= GDK_BUTTON_RELEASE
)
600 if (event
->button
== 2 || (event
->state
& GDK_SHIFT_MASK
))
601 button
= MIDDLE_BUTTON
;
602 else if (event
->button
== 3 || (event
->state
& GDK_MOD1_MASK
))
603 button
= RIGHT_BUTTON
;
604 else if (event
->button
== 1)
605 button
= LEFT_BUTTON
;
607 return FALSE
; /* don't even know what button! */
609 if (event
->type
== GDK_BUTTON_RELEASE
)
610 button
+= LEFT_RELEASE
- LEFT_BUTTON
;
612 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
613 event
->y
- fe
->oy
, button
))
614 gtk_widget_destroy(fe
->window
);
619 static gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
,
622 frontend
*fe
= (frontend
*)data
;
628 if (event
->state
& (GDK_BUTTON2_MASK
| GDK_SHIFT_MASK
))
629 button
= MIDDLE_DRAG
;
630 else if (event
->state
& GDK_BUTTON1_MASK
)
632 else if (event
->state
& GDK_BUTTON3_MASK
)
635 return FALSE
; /* don't even know what button! */
637 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
638 event
->y
- fe
->oy
, button
))
639 gtk_widget_destroy(fe
->window
);
644 static gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
,
647 frontend
*fe
= (frontend
*)data
;
650 gdk_draw_pixmap(widget
->window
,
651 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
653 event
->area
.x
- fe
->ox
, event
->area
.y
- fe
->oy
,
654 event
->area
.x
, event
->area
.y
,
655 event
->area
.width
, event
->area
.height
);
660 static gint
map_window(GtkWidget
*widget
, GdkEvent
*event
,
663 frontend
*fe
= (frontend
*)data
;
666 * Apparently we need to do this because otherwise the status
667 * bar will fail to update immediately. Annoying, but there we
670 gtk_widget_queue_draw(fe
->window
);
675 static gint
configure_area(GtkWidget
*widget
,
676 GdkEventConfigure
*event
, gpointer data
)
678 frontend
*fe
= (frontend
*)data
;
683 gdk_pixmap_unref(fe
->pixmap
);
685 x
= fe
->w
= event
->width
;
686 y
= fe
->h
= event
->height
;
687 midend_size(fe
->me
, &x
, &y
, TRUE
);
690 fe
->ox
= (fe
->w
- fe
->pw
) / 2;
691 fe
->oy
= (fe
->h
- fe
->ph
) / 2;
693 fe
->pixmap
= gdk_pixmap_new(widget
->window
, fe
->pw
, fe
->ph
, -1);
695 gc
= gdk_gc_new(fe
->area
->window
);
696 gdk_gc_set_foreground(gc
, &fe
->colours
[0]);
697 gdk_draw_rectangle(fe
->pixmap
, gc
, 1, 0, 0, fe
->pw
, fe
->ph
);
698 gdk_draw_rectangle(widget
->window
, gc
, 1, 0, 0,
699 event
->width
, event
->height
);
702 midend_force_redraw(fe
->me
);
707 static gint
timer_func(gpointer data
)
709 frontend
*fe
= (frontend
*)data
;
711 if (fe
->timer_active
) {
714 gettimeofday(&now
, NULL
);
715 elapsed
= ((now
.tv_usec
- fe
->last_time
.tv_usec
) * 0.000001F
+
716 (now
.tv_sec
- fe
->last_time
.tv_sec
));
717 midend_timer(fe
->me
, elapsed
); /* may clear timer_active */
721 return fe
->timer_active
;
724 void deactivate_timer(frontend
*fe
)
727 return; /* can happen due to --generate */
728 if (fe
->timer_active
)
729 gtk_timeout_remove(fe
->timer_id
);
730 fe
->timer_active
= FALSE
;
733 void activate_timer(frontend
*fe
)
736 return; /* can happen due to --generate */
737 if (!fe
->timer_active
) {
738 fe
->timer_id
= gtk_timeout_add(20, timer_func
, fe
);
739 gettimeofday(&fe
->last_time
, NULL
);
741 fe
->timer_active
= TRUE
;
744 static void window_destroy(GtkWidget
*widget
, gpointer data
)
749 static void msgbox_button_clicked(GtkButton
*button
, gpointer data
)
751 GtkWidget
*window
= GTK_WIDGET(data
);
754 ip
= (int *)gtk_object_get_data(GTK_OBJECT(window
), "user-data");
755 v
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button
), "user-data"));
758 gtk_widget_destroy(GTK_WIDGET(data
));
761 static int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
763 GtkObject
*cancelbutton
= GTK_OBJECT(data
);
766 * `Escape' effectively clicks the cancel button
768 if (event
->keyval
== GDK_Escape
) {
769 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton
), "clicked");
776 enum { MB_OK
, MB_YESNO
};
778 int message_box(GtkWidget
*parent
, char *title
, char *msg
, int centre
,
781 GtkWidget
*window
, *hbox
, *text
, *button
;
785 window
= gtk_dialog_new();
786 text
= gtk_label_new(msg
);
787 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
788 hbox
= gtk_hbox_new(FALSE
, 0);
789 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
790 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
791 hbox
, FALSE
, FALSE
, 20);
792 gtk_widget_show(text
);
793 gtk_widget_show(hbox
);
794 gtk_window_set_title(GTK_WINDOW(window
), title
);
795 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
801 assert(type
== MB_YESNO
);
802 titles
= "Yes\0No\0";
809 button
= gtk_button_new_with_label(titles
);
810 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
811 button
, FALSE
, FALSE
, 0);
812 gtk_widget_show(button
);
814 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
815 gtk_window_set_default(GTK_WINDOW(window
), button
);
818 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
819 GTK_SIGNAL_FUNC(win_key_press
), button
);
821 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
822 GTK_SIGNAL_FUNC(msgbox_button_clicked
), window
);
823 gtk_object_set_data(GTK_OBJECT(button
), "user-data",
825 titles
+= strlen(titles
)+1;
828 gtk_object_set_data(GTK_OBJECT(window
), "user-data",
829 GINT_TO_POINTER(&i
));
830 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
831 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
832 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
833 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(parent
));
834 /* set_transient_window_pos(parent, window); */
835 gtk_widget_show(window
);
838 return (type
== MB_YESNO ? i
== 0 : TRUE
);
841 void error_box(GtkWidget
*parent
, char *msg
)
843 message_box(parent
, "Error", msg
, FALSE
, MB_OK
);
846 static void config_ok_button_clicked(GtkButton
*button
, gpointer data
)
848 frontend
*fe
= (frontend
*)data
;
851 err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
854 error_box(fe
->cfgbox
, err
);
857 gtk_widget_destroy(fe
->cfgbox
);
862 static void config_cancel_button_clicked(GtkButton
*button
, gpointer data
)
864 frontend
*fe
= (frontend
*)data
;
866 gtk_widget_destroy(fe
->cfgbox
);
869 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
872 * GtkEntry has a nasty habit of eating the Return key, which
873 * is unhelpful since it doesn't actually _do_ anything with it
874 * (it calls gtk_widget_activate, but our edit boxes never need
875 * activating). So I catch Return before GtkEntry sees it, and
876 * pass it straight on to the parent widget. Effect: hitting
877 * Return in an edit box will now activate the default button
878 * in the dialog just like it will everywhere else.
880 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
882 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
883 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
890 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
892 config_item
*i
= (config_item
*)data
;
895 i
->sval
= dupstr(gtk_entry_get_text(GTK_ENTRY(ed
)));
898 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
900 config_item
*i
= (config_item
*)data
;
902 i
->ival
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb
));
905 static void droplist_sel(GtkMenuItem
*item
, gpointer data
)
907 config_item
*i
= (config_item
*)data
;
909 i
->ival
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
),
913 static int get_config(frontend
*fe
, int which
)
915 GtkWidget
*w
, *table
, *cancel
;
920 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
921 fe
->cfg_which
= which
;
924 fe
->cfgbox
= gtk_dialog_new();
925 gtk_window_set_title(GTK_WINDOW(fe
->cfgbox
), title
);
928 w
= gtk_button_new_with_label("OK");
929 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
932 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
933 gtk_window_set_default(GTK_WINDOW(fe
->cfgbox
), w
);
934 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
935 GTK_SIGNAL_FUNC(config_ok_button_clicked
), fe
);
937 w
= gtk_button_new_with_label("Cancel");
938 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
941 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
942 GTK_SIGNAL_FUNC(config_cancel_button_clicked
), fe
);
945 table
= gtk_table_new(1, 2, FALSE
);
947 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->vbox
),
948 table
, FALSE
, FALSE
, 0);
949 gtk_widget_show(table
);
951 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
952 gtk_table_resize(GTK_TABLE(table
), y
+1, 2);
957 * Edit box with a label beside it.
960 w
= gtk_label_new(i
->name
);
961 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
962 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
963 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
964 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
969 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
970 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
971 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
973 gtk_entry_set_text(GTK_ENTRY(w
), i
->sval
);
974 gtk_signal_connect(GTK_OBJECT(w
), "changed",
975 GTK_SIGNAL_FUNC(editbox_changed
), i
);
976 gtk_signal_connect(GTK_OBJECT(w
), "key_press_event",
977 GTK_SIGNAL_FUNC(editbox_key
), NULL
);
986 w
= gtk_check_button_new_with_label(i
->name
);
987 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
988 GTK_SIGNAL_FUNC(button_toggled
), i
);
989 gtk_table_attach(GTK_TABLE(table
), w
, 0, 2, y
, y
+1,
990 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
991 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
993 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w
), i
->ival
);
999 * Drop-down list (GtkOptionMenu).
1002 w
= gtk_label_new(i
->name
);
1003 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
1004 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
1005 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1006 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1010 w
= gtk_option_menu_new();
1011 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
1012 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1013 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1020 GtkWidget
*menuitem
;
1021 GtkWidget
*menu
= gtk_menu_new();
1023 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), menu
);
1031 while (*q
&& *q
!= c
)
1034 name
= snewn(q
-p
+1, char);
1035 strncpy(name
, p
, q
-p
);
1038 if (*q
) q
++; /* eat delimiter */
1040 menuitem
= gtk_menu_item_new_with_label(name
);
1041 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1042 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1043 GINT_TO_POINTER(val
));
1044 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1045 GTK_SIGNAL_FUNC(droplist_sel
), i
);
1046 gtk_widget_show(menuitem
);
1053 gtk_option_menu_set_history(GTK_OPTION_MENU(w
), i
->ival
);
1062 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "destroy",
1063 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1064 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "key_press_event",
1065 GTK_SIGNAL_FUNC(win_key_press
), cancel
);
1066 gtk_window_set_modal(GTK_WINDOW(fe
->cfgbox
), TRUE
);
1067 gtk_window_set_transient_for(GTK_WINDOW(fe
->cfgbox
),
1068 GTK_WINDOW(fe
->window
));
1069 /* set_transient_window_pos(fe->window, fe->cfgbox); */
1070 gtk_widget_show(fe
->cfgbox
);
1078 static void menu_key_event(GtkMenuItem
*menuitem
, gpointer data
)
1080 frontend
*fe
= (frontend
*)data
;
1081 int key
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1083 if (!midend_process_key(fe
->me
, 0, 0, key
))
1084 gtk_widget_destroy(fe
->window
);
1087 static void get_size(frontend
*fe
, int *px
, int *py
)
1092 * Currently I don't want to make the GTK port scale large
1093 * puzzles to fit on the screen. This is because X does permit
1094 * extremely large windows and many window managers provide a
1095 * means of navigating round them, and the users I consulted
1096 * before deciding said that they'd rather have enormous puzzle
1097 * windows spanning multiple screen pages than have them
1098 * shrunk. I could change my mind later or introduce
1099 * configurability; this would be the place to do so, by
1100 * replacing the initial values of x and y with the screen
1105 midend_size(fe
->me
, &x
, &y
, FALSE
);
1110 #if !GTK_CHECK_VERSION(2,0,0)
1111 #define gtk_window_resize(win, x, y) \
1112 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
1115 static void update_menuitem_bullet(GtkWidget
*label
, int visible
)
1118 gtk_label_set_text(GTK_LABEL(label
), "\xE2\x80\xA2");
1120 gtk_label_set_text(GTK_LABEL(label
), "");
1125 * Called when any other code in this file has changed the
1126 * selected game parameters.
1128 static void changed_preset(frontend
*fe
)
1130 int n
= midend_which_preset(fe
->me
);
1134 * Update the tick mark in the Type menu.
1136 if (fe
->preset_bullets
) {
1137 for (i
= 0; i
< fe
->npresets
; i
++)
1138 update_menuitem_bullet(fe
->preset_bullets
[i
], n
== i
);
1140 if (fe
->preset_custom_bullet
) {
1141 update_menuitem_bullet(fe
->preset_custom_bullet
, n
< 0);
1145 * Update the greying on the Copy menu option.
1147 if (fe
->copy_menu_item
) {
1148 int enabled
= midend_can_format_as_text_now(fe
->me
);
1149 gtk_widget_set_sensitive(fe
->copy_menu_item
, enabled
);
1153 static void resize_fe(frontend
*fe
)
1157 get_size(fe
, &x
, &y
);
1160 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1163 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1164 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1167 * Now that we've established the preferred size of the window,
1168 * reduce the drawing area's size request so the user can shrink
1171 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), 1, 1);
1174 static void menu_preset_event(GtkMenuItem
*menuitem
, gpointer data
)
1176 frontend
*fe
= (frontend
*)data
;
1177 game_params
*params
=
1178 (game_params
*)gtk_object_get_data(GTK_OBJECT(menuitem
), "user-data");
1180 midend_set_params(fe
->me
, params
);
1181 midend_new_game(fe
->me
);
1186 GdkAtom compound_text_atom
, utf8_string_atom
;
1187 int paste_initialised
= FALSE
;
1191 unsigned char empty
[] = { 0 };
1193 if (paste_initialised
)
1196 if (!compound_text_atom
)
1197 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
1198 if (!utf8_string_atom
)
1199 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
1202 * Ensure that all the cut buffers exist - according to the
1203 * ICCCM, we must do this before we start using cut buffers.
1205 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1206 XA_CUT_BUFFER0
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1207 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1208 XA_CUT_BUFFER1
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1209 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1210 XA_CUT_BUFFER2
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1211 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1212 XA_CUT_BUFFER3
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1213 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1214 XA_CUT_BUFFER4
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1215 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1216 XA_CUT_BUFFER5
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1217 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1218 XA_CUT_BUFFER6
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1219 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1220 XA_CUT_BUFFER7
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1223 /* Store data in a cut-buffer. */
1224 void store_cutbuffer(char *ptr
, int len
)
1226 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1227 XRotateBuffers(GDK_DISPLAY(), 1);
1228 XStoreBytes(GDK_DISPLAY(), ptr
, len
);
1231 void write_clip(frontend
*fe
, char *data
)
1236 sfree(fe
->paste_data
);
1239 * For this simple application we can safely assume that the
1240 * data passed to this function is pure ASCII, which means we
1241 * can return precisely the same stuff for types STRING,
1242 * COMPOUND_TEXT or UTF8_STRING.
1245 fe
->paste_data
= data
;
1246 fe
->paste_data_len
= strlen(data
);
1248 store_cutbuffer(fe
->paste_data
, fe
->paste_data_len
);
1250 if (gtk_selection_owner_set(fe
->area
, GDK_SELECTION_PRIMARY
,
1252 gtk_selection_clear_targets(fe
->area
, GDK_SELECTION_PRIMARY
);
1253 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1254 GDK_SELECTION_TYPE_STRING
, 1);
1255 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1256 compound_text_atom
, 1);
1257 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1258 utf8_string_atom
, 1);
1262 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1263 guint info
, guint time_stamp
, gpointer data
)
1265 frontend
*fe
= (frontend
*)data
;
1266 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1267 fe
->paste_data
, fe
->paste_data_len
);
1270 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1273 frontend
*fe
= (frontend
*)data
;
1276 sfree(fe
->paste_data
);
1277 fe
->paste_data
= NULL
;
1278 fe
->paste_data_len
= 0;
1282 static void menu_copy_event(GtkMenuItem
*menuitem
, gpointer data
)
1284 frontend
*fe
= (frontend
*)data
;
1287 text
= midend_text_format(fe
->me
);
1290 write_clip(fe
, text
);
1296 static void filesel_ok(GtkButton
*button
, gpointer data
)
1298 frontend
*fe
= (frontend
*)data
;
1300 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1303 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1305 fe
->filesel_name
= dupstr(name
);
1308 static char *file_selector(frontend
*fe
, char *title
, int save
)
1310 GtkWidget
*filesel
=
1311 gtk_file_selection_new(title
);
1313 fe
->filesel_name
= NULL
;
1315 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1317 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1320 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1321 GTK_SIGNAL_FUNC(filesel_ok
), fe
);
1322 gtk_signal_connect_object
1323 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1324 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1325 gtk_signal_connect_object
1326 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1327 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1328 gtk_signal_connect(GTK_OBJECT(filesel
), "destroy",
1329 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1330 gtk_widget_show(filesel
);
1331 gtk_window_set_transient_for(GTK_WINDOW(filesel
), GTK_WINDOW(fe
->window
));
1334 return fe
->filesel_name
;
1337 struct savefile_write_ctx
{
1342 static void savefile_write(void *wctx
, void *buf
, int len
)
1344 struct savefile_write_ctx
*ctx
= (struct savefile_write_ctx
*)wctx
;
1345 if (fwrite(buf
, 1, len
, ctx
->fp
) < len
)
1349 static int savefile_read(void *wctx
, void *buf
, int len
)
1351 FILE *fp
= (FILE *)wctx
;
1354 ret
= fread(buf
, 1, len
, fp
);
1355 return (ret
== len
);
1358 static void menu_save_event(GtkMenuItem
*menuitem
, gpointer data
)
1360 frontend
*fe
= (frontend
*)data
;
1363 name
= file_selector(fe
, "Enter name of game file to save", TRUE
);
1368 if ((fp
= fopen(name
, "r")) != NULL
) {
1369 char buf
[256 + FILENAME_MAX
];
1373 sprintf(buf
, "Are you sure you want to overwrite the"
1375 FILENAME_MAX
, name
);
1376 if (!message_box(fe
->window
, "Question", buf
, TRUE
, MB_YESNO
))
1380 fp
= fopen(name
, "w");
1384 error_box(fe
->window
, "Unable to open save file");
1389 struct savefile_write_ctx ctx
;
1392 midend_serialise(fe
->me
, savefile_write
, &ctx
);
1396 sprintf(boxmsg
, "Error writing save file: %.400s",
1398 error_box(fe
->window
, boxmsg
);
1406 static void menu_load_event(GtkMenuItem
*menuitem
, gpointer data
)
1408 frontend
*fe
= (frontend
*)data
;
1411 name
= file_selector(fe
, "Enter name of saved game file to load", FALSE
);
1414 FILE *fp
= fopen(name
, "r");
1418 error_box(fe
->window
, "Unable to open saved game file");
1422 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1427 error_box(fe
->window
, err
);
1436 static void menu_solve_event(GtkMenuItem
*menuitem
, gpointer data
)
1438 frontend
*fe
= (frontend
*)data
;
1441 msg
= midend_solve(fe
->me
);
1444 error_box(fe
->window
, msg
);
1447 static void menu_restart_event(GtkMenuItem
*menuitem
, gpointer data
)
1449 frontend
*fe
= (frontend
*)data
;
1451 midend_restart_game(fe
->me
);
1454 static void menu_config_event(GtkMenuItem
*menuitem
, gpointer data
)
1456 frontend
*fe
= (frontend
*)data
;
1457 int which
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1460 if (!get_config(fe
, which
))
1463 midend_new_game(fe
->me
);
1467 static void menu_about_event(GtkMenuItem
*menuitem
, gpointer data
)
1469 frontend
*fe
= (frontend
*)data
;
1473 sprintf(titlebuf
, "About %.200s", thegame
.name
);
1476 "from Simon Tatham's Portable Puzzle Collection\n\n"
1477 "%.500s", thegame
.name
, ver
);
1479 message_box(fe
->window
, titlebuf
, textbuf
, TRUE
, MB_OK
);
1482 static GtkWidget
*add_menu_item_with_key(frontend
*fe
, GtkContainer
*cont
,
1483 char *text
, int key
)
1485 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
1487 gtk_container_add(cont
, menuitem
);
1488 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1489 GINT_TO_POINTER(key
));
1490 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1491 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
1492 switch (key
& ~0x1F) {
1495 keyqual
= GDK_CONTROL_MASK
;
1499 keyqual
= GDK_SHIFT_MASK
;
1505 gtk_widget_add_accelerator(menuitem
,
1506 "activate", fe
->accelgroup
,
1509 gtk_widget_show(menuitem
);
1513 static void add_menu_separator(GtkContainer
*cont
)
1515 GtkWidget
*menuitem
= gtk_menu_item_new();
1516 gtk_container_add(cont
, menuitem
);
1517 gtk_widget_show(menuitem
);
1520 enum { ARG_EITHER
, ARG_SAVE
, ARG_ID
}; /* for argtype */
1522 static GtkWidget
*make_preset_menuitem(GtkWidget
**bulletlabel
,
1525 GtkWidget
*hbox
, *lab1
, *lab2
, *menuitem
;
1528 hbox
= gtk_hbox_new(FALSE
, 0);
1529 gtk_widget_show(hbox
);
1530 lab1
= gtk_label_new("\xE2\x80\xA2 ");
1531 gtk_widget_show(lab1
);
1532 gtk_box_pack_start(GTK_BOX(hbox
), lab1
, FALSE
, FALSE
, 0);
1533 gtk_misc_set_alignment(GTK_MISC(lab1
), 0.0, 0.0);
1534 lab2
= gtk_label_new(name
);
1535 gtk_widget_show(lab2
);
1536 gtk_box_pack_start(GTK_BOX(hbox
), lab2
, TRUE
, TRUE
, 0);
1537 gtk_misc_set_alignment(GTK_MISC(lab2
), 0.0, 0.0);
1539 gtk_widget_size_request(lab1
, &req
);
1540 gtk_widget_set_usize(lab1
, req
.width
, -1);
1541 gtk_label_set_text(GTK_LABEL(lab1
), "");
1543 menuitem
= gtk_menu_item_new();
1544 gtk_container_add(GTK_CONTAINER(menuitem
), hbox
);
1546 *bulletlabel
= lab1
;
1550 static frontend
*new_window(char *arg
, int argtype
, char **error
)
1554 GtkWidget
*menubar
, *menu
, *menuitem
;
1559 extern char *const *const xpm_icons
[];
1560 extern const int n_xpm_icons
;
1562 fe
= snew(frontend
);
1564 fe
->timer_active
= FALSE
;
1567 fe
->me
= midend_new(fe
, &thegame
, >k_drawing
, fe
);
1577 err
= midend_game_id(fe
->me
, arg
);
1579 midend_new_game(fe
->me
);
1581 sprintf(errbuf
, "Invalid game ID: %.800s", err
);
1584 fp
= fopen(arg
, "r");
1586 sprintf(errbuf
, "Error opening file: %.800s", strerror(errno
));
1588 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1590 sprintf(errbuf
, "Invalid save file: %.800s", err
);
1594 default /*case ARG_EITHER*/:
1596 * First try treating the argument as a game ID.
1598 err
= midend_game_id(fe
->me
, arg
);
1601 * It's a valid game ID.
1603 midend_new_game(fe
->me
);
1605 FILE *fp
= fopen(arg
, "r");
1607 sprintf(errbuf
, "Supplied argument is neither a game ID (%.400s)"
1608 " nor a save file (%.400s)", err
, strerror(errno
));
1610 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1612 sprintf(errbuf
, "%.800s", err
);
1619 *error
= dupstr(errbuf
);
1620 midend_free(fe
->me
);
1626 midend_new_game(fe
->me
);
1629 fe
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1630 gtk_window_set_title(GTK_WINDOW(fe
->window
), thegame
.name
);
1632 vbox
= GTK_BOX(gtk_vbox_new(FALSE
, 0));
1633 gtk_container_add(GTK_CONTAINER(fe
->window
), GTK_WIDGET(vbox
));
1634 gtk_widget_show(GTK_WIDGET(vbox
));
1636 fe
->accelgroup
= gtk_accel_group_new();
1637 gtk_window_add_accel_group(GTK_WINDOW(fe
->window
), fe
->accelgroup
);
1639 menubar
= gtk_menu_bar_new();
1640 gtk_box_pack_start(vbox
, menubar
, FALSE
, FALSE
, 0);
1641 gtk_widget_show(menubar
);
1643 menuitem
= gtk_menu_item_new_with_label("Game");
1644 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1645 gtk_widget_show(menuitem
);
1647 menu
= gtk_menu_new();
1648 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1650 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "New", 'n');
1652 menuitem
= gtk_menu_item_new_with_label("Restart");
1653 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1654 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1655 GTK_SIGNAL_FUNC(menu_restart_event
), fe
);
1656 gtk_widget_show(menuitem
);
1658 menuitem
= gtk_menu_item_new_with_label("Specific...");
1659 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1660 GINT_TO_POINTER(CFG_DESC
));
1661 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1662 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1663 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1664 gtk_widget_show(menuitem
);
1666 menuitem
= gtk_menu_item_new_with_label("Random Seed...");
1667 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1668 GINT_TO_POINTER(CFG_SEED
));
1669 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1670 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1671 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1672 gtk_widget_show(menuitem
);
1674 if ((n
= midend_num_presets(fe
->me
)) > 0 || thegame
.can_configure
) {
1678 menuitem
= gtk_menu_item_new_with_label("Type");
1679 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1680 gtk_widget_show(menuitem
);
1682 submenu
= gtk_menu_new();
1683 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), submenu
);
1686 fe
->preset_bullets
= snewn(n
, GtkWidget
*);
1688 for (i
= 0; i
< n
; i
++) {
1690 game_params
*params
;
1692 midend_fetch_preset(fe
->me
, i
, &name
, ¶ms
);
1694 menuitem
= make_preset_menuitem(&fe
->preset_bullets
[i
], name
);
1696 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1697 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", params
);
1698 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1699 GTK_SIGNAL_FUNC(menu_preset_event
), fe
);
1700 gtk_widget_show(menuitem
);
1703 if (thegame
.can_configure
) {
1704 menuitem
= make_preset_menuitem(&fe
->preset_custom_bullet
,
1707 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1708 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1709 GPOINTER_TO_INT(CFG_SETTINGS
));
1710 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1711 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1712 gtk_widget_show(menuitem
);
1714 fe
->preset_custom_bullet
= NULL
;
1718 fe
->preset_bullets
= NULL
;
1719 fe
->preset_custom_bullet
= NULL
;
1722 add_menu_separator(GTK_CONTAINER(menu
));
1723 menuitem
= gtk_menu_item_new_with_label("Load...");
1724 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1725 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1726 GTK_SIGNAL_FUNC(menu_load_event
), fe
);
1727 gtk_widget_show(menuitem
);
1728 menuitem
= gtk_menu_item_new_with_label("Save...");
1729 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1730 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1731 GTK_SIGNAL_FUNC(menu_save_event
), fe
);
1732 gtk_widget_show(menuitem
);
1733 add_menu_separator(GTK_CONTAINER(menu
));
1734 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Undo", 'u');
1735 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Redo", 'r');
1736 if (thegame
.can_format_as_text_ever
) {
1737 add_menu_separator(GTK_CONTAINER(menu
));
1738 menuitem
= gtk_menu_item_new_with_label("Copy");
1739 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1740 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1741 GTK_SIGNAL_FUNC(menu_copy_event
), fe
);
1742 gtk_widget_show(menuitem
);
1743 fe
->copy_menu_item
= menuitem
;
1745 fe
->copy_menu_item
= NULL
;
1747 if (thegame
.can_solve
) {
1748 add_menu_separator(GTK_CONTAINER(menu
));
1749 menuitem
= gtk_menu_item_new_with_label("Solve");
1750 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1751 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1752 GTK_SIGNAL_FUNC(menu_solve_event
), fe
);
1753 gtk_widget_show(menuitem
);
1755 add_menu_separator(GTK_CONTAINER(menu
));
1756 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Exit", 'q');
1758 menuitem
= gtk_menu_item_new_with_label("Help");
1759 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1760 gtk_widget_show(menuitem
);
1762 menu
= gtk_menu_new();
1763 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1765 menuitem
= gtk_menu_item_new_with_label("About");
1766 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1767 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1768 GTK_SIGNAL_FUNC(menu_about_event
), fe
);
1769 gtk_widget_show(menuitem
);
1778 fe
->colmap
= gdk_colormap_get_system();
1779 colours
= midend_colours(fe
->me
, &ncolours
);
1780 fe
->ncolours
= ncolours
;
1781 fe
->colours
= snewn(ncolours
, GdkColor
);
1782 for (i
= 0; i
< ncolours
; i
++) {
1783 fe
->colours
[i
].red
= colours
[i
*3] * 0xFFFF;
1784 fe
->colours
[i
].green
= colours
[i
*3+1] * 0xFFFF;
1785 fe
->colours
[i
].blue
= colours
[i
*3+2] * 0xFFFF;
1787 success
= snewn(ncolours
, gboolean
);
1788 gdk_colormap_alloc_colors(fe
->colmap
, fe
->colours
, ncolours
,
1789 FALSE
, FALSE
, success
);
1790 for (i
= 0; i
< ncolours
; i
++) {
1792 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
1793 i
, fe
->colours
[i
].red
>> 8,
1794 fe
->colours
[i
].green
>> 8,
1795 fe
->colours
[i
].blue
>> 8);
1800 if (midend_wants_statusbar(fe
->me
)) {
1801 GtkWidget
*viewport
;
1804 viewport
= gtk_viewport_new(NULL
, NULL
);
1805 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport
), GTK_SHADOW_NONE
);
1806 fe
->statusbar
= gtk_statusbar_new();
1807 gtk_container_add(GTK_CONTAINER(viewport
), fe
->statusbar
);
1808 gtk_widget_show(viewport
);
1809 gtk_box_pack_end(vbox
, viewport
, FALSE
, FALSE
, 0);
1810 gtk_widget_show(fe
->statusbar
);
1811 fe
->statusctx
= gtk_statusbar_get_context_id
1812 (GTK_STATUSBAR(fe
->statusbar
), "game");
1813 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
,
1815 gtk_widget_size_request(fe
->statusbar
, &req
);
1817 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
1819 gtk_widget_set_usize(viewport
, -1, req
.height
);
1821 fe
->statusbar
= NULL
;
1823 fe
->area
= gtk_drawing_area_new();
1824 get_size(fe
, &x
, &y
);
1825 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1829 gtk_box_pack_end(vbox
, fe
->area
, TRUE
, TRUE
, 0);
1833 fe
->nfonts
= fe
->fontsize
= 0;
1835 fe
->paste_data
= NULL
;
1836 fe
->paste_data_len
= 0;
1838 gtk_signal_connect(GTK_OBJECT(fe
->window
), "destroy",
1839 GTK_SIGNAL_FUNC(destroy
), fe
);
1840 gtk_signal_connect(GTK_OBJECT(fe
->window
), "key_press_event",
1841 GTK_SIGNAL_FUNC(key_event
), fe
);
1842 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_press_event",
1843 GTK_SIGNAL_FUNC(button_event
), fe
);
1844 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_release_event",
1845 GTK_SIGNAL_FUNC(button_event
), fe
);
1846 gtk_signal_connect(GTK_OBJECT(fe
->area
), "motion_notify_event",
1847 GTK_SIGNAL_FUNC(motion_event
), fe
);
1848 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_get",
1849 GTK_SIGNAL_FUNC(selection_get
), fe
);
1850 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_clear_event",
1851 GTK_SIGNAL_FUNC(selection_clear
), fe
);
1852 gtk_signal_connect(GTK_OBJECT(fe
->area
), "expose_event",
1853 GTK_SIGNAL_FUNC(expose_area
), fe
);
1854 gtk_signal_connect(GTK_OBJECT(fe
->window
), "map_event",
1855 GTK_SIGNAL_FUNC(map_window
), fe
);
1856 gtk_signal_connect(GTK_OBJECT(fe
->area
), "configure_event",
1857 GTK_SIGNAL_FUNC(configure_area
), fe
);
1859 gtk_widget_add_events(GTK_WIDGET(fe
->area
),
1860 GDK_BUTTON_PRESS_MASK
|
1861 GDK_BUTTON_RELEASE_MASK
|
1862 GDK_BUTTON_MOTION_MASK
);
1865 gtk_widget_realize(fe
->window
);
1866 iconpm
= gdk_pixmap_create_from_xpm_d(fe
->window
->window
, NULL
,
1867 NULL
, (gchar
**)xpm_icons
[0]);
1868 gdk_window_set_icon(fe
->window
->window
, NULL
, iconpm
, NULL
);
1870 for (n
= 0; n
< n_xpm_icons
; n
++) {
1872 g_list_append(iconlist
,
1873 gdk_pixbuf_new_from_xpm_data((const gchar
**)
1876 gdk_window_set_icon_list(fe
->window
->window
, iconlist
);
1879 gtk_widget_show(fe
->area
);
1880 gtk_widget_show(fe
->window
);
1883 * Now that we've established the preferred size of the window,
1884 * reduce the drawing area's size request so the user can shrink
1887 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), 1, 1);
1889 gdk_window_set_background(fe
->area
->window
, &fe
->colours
[0]);
1890 gdk_window_set_background(fe
->window
->window
, &fe
->colours
[0]);
1895 char *fgetline(FILE *fp
)
1897 char *ret
= snewn(512, char);
1898 int size
= 512, len
= 0;
1899 while (fgets(ret
+ len
, size
- len
, fp
)) {
1900 len
+= strlen(ret
+ len
);
1901 if (ret
[len
-1] == '\n')
1902 break; /* got a newline, we're done */
1904 ret
= sresize(ret
, size
, char);
1906 if (len
== 0) { /* first fgets returned NULL */
1914 int main(int argc
, char **argv
)
1916 char *pname
= argv
[0];
1918 int ngenerate
= 0, print
= FALSE
, px
= 1, py
= 1;
1919 int soln
= FALSE
, colour
= FALSE
;
1921 float redo_proportion
= 0.0F
;
1922 char *savefile
= NULL
, *savesuffix
= NULL
;
1924 int argtype
= ARG_EITHER
;
1925 char *screenshot_file
= NULL
;
1926 int doing_opts
= TRUE
;
1932 * Command line parsing in this function is rather fiddly,
1933 * because GTK wants to have a go at argc/argv _first_ - and
1934 * yet we can't let it, because gtk_init() will bomb out if it
1935 * can't open an X display, whereas in fact we want to permit
1936 * our --generate and --print modes to run without an X
1940 * - we parse the command line ourselves, without modifying
1942 * - if we encounter an error which might plausibly be the
1943 * result of a GTK command line (i.e. not detailed errors in
1944 * particular options of ours) we store the error message
1945 * and terminate parsing.
1946 * - if we got enough out of the command line to know it
1947 * specifies a non-X mode of operation, we either display
1948 * the stored error and return failure, or if there is no
1949 * stored error we do the non-X operation and return
1951 * - otherwise, we go straight to gtk_init().
1957 if (doing_opts
&& !strcmp(p
, "--version")) {
1958 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
1961 } else if (doing_opts
&& !strcmp(p
, "--generate")) {
1963 ngenerate
= atoi(*++av
);
1965 fprintf(stderr
, "%s: '--generate' expected a number\n",
1971 } else if (doing_opts
&& !strcmp(p
, "--save")) {
1975 fprintf(stderr
, "%s: '--save' expected a filename\n",
1979 } else if (doing_opts
&& (!strcmp(p
, "--save-suffix") ||
1980 !strcmp(p
, "--savesuffix"))) {
1984 fprintf(stderr
, "%s: '--save-suffix' expected a filename\n",
1988 } else if (doing_opts
&& !strcmp(p
, "--print")) {
1989 if (!thegame
.can_print
) {
1990 fprintf(stderr
, "%s: this game does not support printing\n",
1997 if (sscanf(dim
, "%dx%d", &px
, &py
) != 2) {
1998 fprintf(stderr
, "%s: unable to parse argument '%s' to "
1999 "'--print'\n", pname
, dim
);
2005 } else if (doing_opts
&& !strcmp(p
, "--scale")) {
2007 scale
= atof(*++av
);
2009 fprintf(stderr
, "%s: no argument supplied to '--scale'\n",
2013 } else if (doing_opts
&& !strcmp(p
, "--redo")) {
2015 * This is an internal option which I don't expect
2016 * users to have any particular use for. The effect of
2017 * --redo is that once the game has been loaded and
2018 * initialised, the next move in the redo chain is
2019 * replayed, and the game screen is redrawn part way
2020 * through the making of the move. This is only
2021 * meaningful if there _is_ a next move in the redo
2022 * chain, which means in turn that this option is only
2023 * useful if you're also passing a save file on the
2026 * This option is used by the script which generates
2027 * the puzzle icons and website screenshots, and I
2028 * don't imagine it's useful for anything else.
2029 * (Unless, I suppose, users don't like my screenshots
2030 * and want to generate their own in the same way for
2031 * some repackaged version of the puzzles.)
2034 redo_proportion
= atof(*++av
);
2036 fprintf(stderr
, "%s: no argument supplied to '--redo'\n",
2040 } else if (doing_opts
&& !strcmp(p
, "--screenshot")) {
2042 * Another internal option for the icon building
2043 * script. This causes a screenshot of the central
2044 * drawing area (i.e. not including the menu bar or
2045 * status bar) to be saved to a PNG file once the
2046 * window has been drawn, and then the application
2047 * quits immediately.
2050 screenshot_file
= *++av
;
2052 fprintf(stderr
, "%s: no argument supplied to '--screenshot'\n",
2056 } else if (doing_opts
&& (!strcmp(p
, "--with-solutions") ||
2057 !strcmp(p
, "--with-solution") ||
2058 !strcmp(p
, "--with-solns") ||
2059 !strcmp(p
, "--with-soln") ||
2060 !strcmp(p
, "--solutions") ||
2061 !strcmp(p
, "--solution") ||
2062 !strcmp(p
, "--solns") ||
2063 !strcmp(p
, "--soln"))) {
2065 } else if (doing_opts
&& !strcmp(p
, "--colour")) {
2066 if (!thegame
.can_print_in_colour
) {
2067 fprintf(stderr
, "%s: this game does not support colour"
2068 " printing\n", pname
);
2072 } else if (doing_opts
&& !strcmp(p
, "--load")) {
2074 } else if (doing_opts
&& !strcmp(p
, "--game")) {
2076 } else if (doing_opts
&& !strcmp(p
, "--")) {
2078 } else if (!doing_opts
|| p
[0] != '-') {
2080 fprintf(stderr
, "%s: more than one argument supplied\n",
2086 sprintf(errbuf
, "%.100s: unrecognised option '%.100s'\n",
2093 fputs(errbuf
, stderr
);
2098 * Special standalone mode for generating puzzle IDs on the
2099 * command line. Useful for generating puzzles to be printed
2100 * out and solved offline (for puzzles where that even makes
2101 * sense - Solo, for example, is a lot more pencil-and-paper
2102 * friendly than Twiddle!)
2106 * <puzzle-name> --generate [<n> [<params>]]
2108 * <n>, if present, is the number of puzzle IDs to generate.
2109 * <params>, if present, is the same type of parameter string
2110 * you would pass to the puzzle when running it in GUI mode,
2111 * including optional extras such as the expansion factor in
2112 * Rectangles and the difficulty level in Solo.
2114 * If you specify <params>, you must also specify <n> (although
2115 * you may specify it to be 1). Sorry; that was the
2116 * simplest-to-parse command-line syntax I came up with.
2118 if (ngenerate
> 0 || print
|| savefile
|| savesuffix
) {
2122 document
*doc
= NULL
;
2126 me
= midend_new(NULL
, &thegame
, NULL
, NULL
);
2129 if (savefile
&& !savesuffix
)
2131 if (!savefile
&& savesuffix
)
2135 doc
= document_new(px
, py
, scale
);
2138 * In this loop, we either generate a game ID or read one
2139 * from stdin depending on whether we're in generate mode;
2140 * then we either write it to stdout or print it, depending
2141 * on whether we're in print mode. Thus, this loop handles
2142 * generate-to-stdout, print-from-stdin and generate-and-
2143 * immediately-print modes.
2145 * (It could also handle a copy-stdin-to-stdout mode,
2146 * although there's currently no combination of options
2147 * which will cause this loop to be activated in that mode.
2148 * It wouldn't be _entirely_ pointless, though, because
2149 * stdin could contain bare params strings or random-seed
2150 * IDs, and stdout would contain nothing but fully
2151 * generated descriptive game IDs.)
2153 while (ngenerate
== 0 || i
< n
) {
2156 if (ngenerate
== 0) {
2157 pstr
= fgetline(stdin
);
2160 pstr
[strcspn(pstr
, "\r\n")] = '\0';
2163 pstr
= snewn(strlen(arg
) + 40, char);
2166 if (i
> 0 && strchr(arg
, '#'))
2167 sprintf(pstr
+ strlen(pstr
), "-%d", i
);
2173 err
= midend_game_id(me
, pstr
);
2175 fprintf(stderr
, "%s: error parsing '%s': %s\n",
2182 midend_new_game(me
);
2185 err
= midend_print_puzzle(me
, doc
, soln
);
2187 fprintf(stderr
, "%s: error in printing: %s\n", pname
, err
);
2192 struct savefile_write_ctx ctx
;
2193 char *realname
= snewn(40 + strlen(savefile
) +
2194 strlen(savesuffix
), char);
2195 sprintf(realname
, "%s%d%s", savefile
, i
, savesuffix
);
2196 ctx
.fp
= fopen(realname
, "w");
2198 fprintf(stderr
, "%s: open: %s\n", realname
,
2203 midend_serialise(me
, savefile_write
, &ctx
);
2205 fprintf(stderr
, "%s: write: %s\n", realname
,
2206 strerror(ctx
.error
));
2209 if (fclose(ctx
.fp
)) {
2210 fprintf(stderr
, "%s: close: %s\n", realname
,
2215 if (!doc
&& !savefile
) {
2216 id
= midend_get_game_id(me
);
2225 psdata
*ps
= ps_init(stdout
, colour
);
2226 document_print(doc
, ps_drawing_api(ps
));
2237 gtk_init(&argc
, &argv
);
2239 fe
= new_window(arg
, argtype
, &error
);
2242 fprintf(stderr
, "%s: %s\n", pname
, error
);
2246 if (screenshot_file
) {
2248 * Some puzzles will not redraw their entire area if
2249 * given a partially completed animation, which means
2250 * we must redraw now and _then_ redraw again after
2251 * freezing the move timer.
2253 midend_force_redraw(fe
->me
);
2256 if (redo_proportion
) {
2258 midend_process_key(fe
->me
, 0, 0, 'r');
2259 /* And freeze the timer at the specified position. */
2260 midend_freeze_timer(fe
->me
, redo_proportion
);
2263 if (screenshot_file
) {
2265 GError
*gerror
= NULL
;
2267 midend_redraw(fe
->me
);
2269 pb
= gdk_pixbuf_get_from_drawable(NULL
, fe
->pixmap
,
2270 NULL
, 0, 0, 0, 0, -1, -1);
2271 gdk_pixbuf_save(pb
, screenshot_file
, "png", &gerror
, NULL
);