2 * gtk.c: GTK front end for my puzzle collection.
16 #include <gdk/gdkkeysyms.h>
18 #include <gdk-pixbuf/gdk-pixbuf.h>
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
27 #if GTK_CHECK_VERSION(2,0,0)
32 static FILE *debug_fp
= NULL
;
37 debug_fp
= fopen("debug.log", "w");
48 void debug_printf(char *fmt
, ...)
54 vsprintf(buf
, fmt
, ap
);
60 /* ----------------------------------------------------------------------
61 * Error reporting functions used elsewhere.
64 void fatal(char *fmt
, ...)
68 fprintf(stderr
, "fatal error: ");
71 vfprintf(stderr
, fmt
, ap
);
74 fprintf(stderr
, "\n");
78 /* ----------------------------------------------------------------------
79 * GTK front end to puzzles.
84 PangoFontDescription
*desc
;
93 * This structure holds all the data relevant to a single window.
94 * In principle this would allow us to open multiple independent
95 * puzzle windows, although I can't currently see any real point in
96 * doing so. I'm just coding cleanly because there's no
97 * particularly good reason not to.
101 GtkAccelGroup
*accelgroup
;
103 GtkWidget
*statusbar
;
112 int bbox_l
, bbox_r
, bbox_u
, bbox_d
;
113 int timer_active
, timer_id
;
114 struct timeval last_time
;
116 int nfonts
, fontsize
;
118 int cfg_which
, cfgret
;
122 int pw
, ph
; /* pixmap size (w, h are area size) */
123 int ox
, oy
; /* offset of pixmap in drawing area */
127 void get_random_seed(void **randseed
, int *randseedsize
)
129 struct timeval
*tvp
= snew(struct timeval
);
130 gettimeofday(tvp
, NULL
);
131 *randseed
= (void *)tvp
;
132 *randseedsize
= sizeof(struct timeval
);
135 void frontend_default_colour(frontend
*fe
, float *output
)
137 GdkColor col
= fe
->window
->style
->bg
[GTK_STATE_NORMAL
];
138 output
[0] = col
.red
/ 65535.0;
139 output
[1] = col
.green
/ 65535.0;
140 output
[2] = col
.blue
/ 65535.0;
143 void gtk_status_bar(void *handle
, char *text
)
145 frontend
*fe
= (frontend
*)handle
;
147 assert(fe
->statusbar
);
149 gtk_statusbar_pop(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
);
150 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
, text
);
153 void gtk_start_draw(void *handle
)
155 frontend
*fe
= (frontend
*)handle
;
156 fe
->gc
= gdk_gc_new(fe
->area
->window
);
163 void gtk_clip(void *handle
, int x
, int y
, int w
, int h
)
165 frontend
*fe
= (frontend
*)handle
;
173 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
176 void gtk_unclip(void *handle
)
178 frontend
*fe
= (frontend
*)handle
;
186 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
189 void gtk_draw_text(void *handle
, int x
, int y
, int fonttype
, int fontsize
,
190 int align
, int colour
, char *text
)
192 frontend
*fe
= (frontend
*)handle
;
196 * Find or create the font.
198 for (i
= 0; i
< fe
->nfonts
; i
++)
199 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
202 if (i
== fe
->nfonts
) {
203 if (fe
->fontsize
<= fe
->nfonts
) {
204 fe
->fontsize
= fe
->nfonts
+ 10;
205 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
210 fe
->fonts
[i
].type
= fonttype
;
211 fe
->fonts
[i
].size
= fontsize
;
215 * Use Pango to find the closest match to the requested
219 PangoFontDescription
*fd
;
221 fd
= pango_font_description_new();
222 /* `Monospace' and `Sans' are meta-families guaranteed to exist */
223 pango_font_description_set_family(fd
, fonttype
== FONT_FIXED ?
224 "Monospace" : "Sans");
225 pango_font_description_set_weight(fd
, PANGO_WEIGHT_BOLD
);
227 * I found some online Pango documentation which
228 * described a function called
229 * pango_font_description_set_absolute_size(), which is
230 * _exactly_ what I want here. Unfortunately, none of
231 * my local Pango installations have it (presumably
232 * they're too old), so I'm going to have to hack round
233 * it by figuring out the point size myself. This
234 * limits me to X and probably also breaks in later
235 * Pango installations, so ideally I should add another
236 * CHECK_VERSION type ifdef and use set_absolute_size
237 * where available. All very annoying.
239 #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
240 pango_font_description_set_absolute_size(fd
, PANGO_SCALE
*fontsize
);
243 Display
*d
= GDK_DISPLAY();
244 int s
= DefaultScreen(d
);
246 (PANGO_SCALE
* 72.27 / 25.4) *
247 ((double) DisplayWidthMM(d
, s
) / DisplayWidth (d
, s
));
248 pango_font_description_set_size(fd
, resolution
* fontsize
);
251 fe
->fonts
[i
].desc
= fd
;
256 * In GTK 1.2, I don't know of any plausible way to
257 * pick a suitable font, so I'm just going to be
260 fe
->fonts
[i
].font
= gdk_font_load(fonttype
== FONT_FIXED ?
261 "fixed" : "variable");
269 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
280 layout
= pango_layout_new(gtk_widget_get_pango_context(fe
->area
));
281 pango_layout_set_font_description(layout
, fe
->fonts
[i
].desc
);
282 pango_layout_set_text(layout
, text
, strlen(text
));
283 pango_layout_get_pixel_extents(layout
, NULL
, &rect
);
285 if (align
& ALIGN_VCENTRE
)
286 rect
.y
-= rect
.height
/ 2;
288 rect
.y
-= rect
.height
;
290 if (align
& ALIGN_HCENTRE
)
291 rect
.x
-= rect
.width
/ 2;
292 else if (align
& ALIGN_HRIGHT
)
293 rect
.x
-= rect
.width
;
295 gdk_draw_layout(fe
->pixmap
, fe
->gc
, rect
.x
+ x
, rect
.y
+ y
, layout
);
297 g_object_unref(layout
);
302 * Find string dimensions and process alignment.
305 int lb
, rb
, wid
, asc
, desc
;
308 * Measure vertical string extents with respect to the same
311 gdk_string_extents(fe
->fonts
[i
].font
,
312 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
313 &lb
, &rb
, &wid
, &asc
, &desc
);
314 if (align
& ALIGN_VCENTRE
)
315 y
+= asc
- (asc
+desc
)/2;
320 * ... but horizontal extents with respect to the provided
321 * string. This means that multiple pieces of text centred
322 * on the same y-coordinate don't have different baselines.
324 gdk_string_extents(fe
->fonts
[i
].font
, text
,
325 &lb
, &rb
, &wid
, &asc
, &desc
);
327 if (align
& ALIGN_HCENTRE
)
329 else if (align
& ALIGN_HRIGHT
)
335 * Actually draw the text.
337 gdk_draw_string(fe
->pixmap
, fe
->fonts
[i
].font
, fe
->gc
, x
, y
, text
);
342 void gtk_draw_rect(void *handle
, int x
, int y
, int w
, int h
, int colour
)
344 frontend
*fe
= (frontend
*)handle
;
345 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
346 gdk_draw_rectangle(fe
->pixmap
, fe
->gc
, 1, x
, y
, w
, h
);
349 void gtk_draw_line(void *handle
, int x1
, int y1
, int x2
, int y2
, int colour
)
351 frontend
*fe
= (frontend
*)handle
;
352 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
353 gdk_draw_line(fe
->pixmap
, fe
->gc
, x1
, y1
, x2
, y2
);
356 void gtk_draw_poly(void *handle
, int *coords
, int npoints
,
357 int fillcolour
, int outlinecolour
)
359 frontend
*fe
= (frontend
*)handle
;
360 GdkPoint
*points
= snewn(npoints
, GdkPoint
);
363 for (i
= 0; i
< npoints
; i
++) {
364 points
[i
].x
= coords
[i
*2];
365 points
[i
].y
= coords
[i
*2+1];
368 if (fillcolour
>= 0) {
369 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[fillcolour
]);
370 gdk_draw_polygon(fe
->pixmap
, fe
->gc
, TRUE
, points
, npoints
);
372 assert(outlinecolour
>= 0);
373 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[outlinecolour
]);
376 * In principle we ought to be able to use gdk_draw_polygon for
377 * the outline as well. In fact, it turns out to interact badly
378 * with a clipping region, for no terribly obvious reason, so I
379 * draw the outline as a sequence of lines instead.
381 for (i
= 0; i
< npoints
; i
++)
382 gdk_draw_line(fe
->pixmap
, fe
->gc
,
383 points
[i
].x
, points
[i
].y
,
384 points
[(i
+1)%npoints
].x
, points
[(i
+1)%npoints
].y
);
389 void gtk_draw_circle(void *handle
, int cx
, int cy
, int radius
,
390 int fillcolour
, int outlinecolour
)
392 frontend
*fe
= (frontend
*)handle
;
393 if (fillcolour
>= 0) {
394 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[fillcolour
]);
395 gdk_draw_arc(fe
->pixmap
, fe
->gc
, TRUE
,
396 cx
- radius
, cy
- radius
,
397 2 * radius
, 2 * radius
, 0, 360 * 64);
400 assert(outlinecolour
>= 0);
401 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[outlinecolour
]);
402 gdk_draw_arc(fe
->pixmap
, fe
->gc
, FALSE
,
403 cx
- radius
, cy
- radius
,
404 2 * radius
, 2 * radius
, 0, 360 * 64);
412 blitter
*gtk_blitter_new(void *handle
, int w
, int h
)
415 * We can't create the pixmap right now, because fe->window
416 * might not yet exist. So we just cache w and h and create it
417 * during the firs call to blitter_save.
419 blitter
*bl
= snew(blitter
);
426 void gtk_blitter_free(void *handle
, blitter
*bl
)
429 gdk_pixmap_unref(bl
->pixmap
);
433 void gtk_blitter_save(void *handle
, blitter
*bl
, int x
, int y
)
435 frontend
*fe
= (frontend
*)handle
;
437 bl
->pixmap
= gdk_pixmap_new(fe
->area
->window
, bl
->w
, bl
->h
, -1);
440 gdk_draw_pixmap(bl
->pixmap
,
441 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
443 x
, y
, 0, 0, bl
->w
, bl
->h
);
446 void gtk_blitter_load(void *handle
, blitter
*bl
, int x
, int y
)
448 frontend
*fe
= (frontend
*)handle
;
450 if (x
== BLITTER_FROMSAVED
&& y
== BLITTER_FROMSAVED
) {
454 gdk_draw_pixmap(fe
->pixmap
,
455 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
457 0, 0, x
, y
, bl
->w
, bl
->h
);
460 void gtk_draw_update(void *handle
, int x
, int y
, int w
, int h
)
462 frontend
*fe
= (frontend
*)handle
;
463 if (fe
->bbox_l
> x
) fe
->bbox_l
= x
;
464 if (fe
->bbox_r
< x
+w
) fe
->bbox_r
= x
+w
;
465 if (fe
->bbox_u
> y
) fe
->bbox_u
= y
;
466 if (fe
->bbox_d
< y
+h
) fe
->bbox_d
= y
+h
;
469 void gtk_end_draw(void *handle
)
471 frontend
*fe
= (frontend
*)handle
;
472 gdk_gc_unref(fe
->gc
);
475 if (fe
->bbox_l
< fe
->bbox_r
&& fe
->bbox_u
< fe
->bbox_d
) {
476 gdk_draw_pixmap(fe
->area
->window
,
477 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
479 fe
->bbox_l
, fe
->bbox_u
,
480 fe
->ox
+ fe
->bbox_l
, fe
->oy
+ fe
->bbox_u
,
481 fe
->bbox_r
- fe
->bbox_l
, fe
->bbox_d
- fe
->bbox_u
);
485 const struct drawing_api gtk_drawing
= {
501 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, /* {begin,end}_{doc,page,puzzle} */
502 NULL
, /* line_width */
505 static void destroy(GtkWidget
*widget
, gpointer data
)
507 frontend
*fe
= (frontend
*)data
;
508 deactivate_timer(fe
);
513 static gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
515 frontend
*fe
= (frontend
*)data
;
517 int shift
= (event
->state
& GDK_SHIFT_MASK
) ? MOD_SHFT
: 0;
518 int ctrl
= (event
->state
& GDK_CONTROL_MASK
) ? MOD_CTRL
: 0;
523 #if !GTK_CHECK_VERSION(2,0,0)
524 /* Gtk 1.2 passes a key event to this function even if it's also
525 * defined as an accelerator.
526 * Gtk 2 doesn't do this, and this function appears not to exist there. */
527 if (fe
->accelgroup
&&
528 gtk_accel_group_get_entry(fe
->accelgroup
,
529 event
->keyval
, event
->state
))
533 if (event
->keyval
== GDK_Up
)
534 keyval
= shift
| ctrl
| CURSOR_UP
;
535 else if (event
->keyval
== GDK_KP_Up
|| event
->keyval
== GDK_KP_8
)
536 keyval
= MOD_NUM_KEYPAD
| '8';
537 else if (event
->keyval
== GDK_Down
)
538 keyval
= shift
| ctrl
| CURSOR_DOWN
;
539 else if (event
->keyval
== GDK_KP_Down
|| event
->keyval
== GDK_KP_2
)
540 keyval
= MOD_NUM_KEYPAD
| '2';
541 else if (event
->keyval
== GDK_Left
)
542 keyval
= shift
| ctrl
| CURSOR_LEFT
;
543 else if (event
->keyval
== GDK_KP_Left
|| event
->keyval
== GDK_KP_4
)
544 keyval
= MOD_NUM_KEYPAD
| '4';
545 else if (event
->keyval
== GDK_Right
)
546 keyval
= shift
| ctrl
| CURSOR_RIGHT
;
547 else if (event
->keyval
== GDK_KP_Right
|| event
->keyval
== GDK_KP_6
)
548 keyval
= MOD_NUM_KEYPAD
| '6';
549 else if (event
->keyval
== GDK_KP_Home
|| event
->keyval
== GDK_KP_7
)
550 keyval
= MOD_NUM_KEYPAD
| '7';
551 else if (event
->keyval
== GDK_KP_End
|| event
->keyval
== GDK_KP_1
)
552 keyval
= MOD_NUM_KEYPAD
| '1';
553 else if (event
->keyval
== GDK_KP_Page_Up
|| event
->keyval
== GDK_KP_9
)
554 keyval
= MOD_NUM_KEYPAD
| '9';
555 else if (event
->keyval
== GDK_KP_Page_Down
|| event
->keyval
== GDK_KP_3
)
556 keyval
= MOD_NUM_KEYPAD
| '3';
557 else if (event
->keyval
== GDK_KP_Insert
|| event
->keyval
== GDK_KP_0
)
558 keyval
= MOD_NUM_KEYPAD
| '0';
559 else if (event
->keyval
== GDK_KP_Begin
|| event
->keyval
== GDK_KP_5
)
560 keyval
= MOD_NUM_KEYPAD
| '5';
561 else if (event
->keyval
== GDK_BackSpace
||
562 event
->keyval
== GDK_Delete
||
563 event
->keyval
== GDK_KP_Delete
)
565 else if (event
->string
[0] && !event
->string
[1])
566 keyval
= (unsigned char)event
->string
[0];
571 !midend_process_key(fe
->me
, 0, 0, keyval
))
572 gtk_widget_destroy(fe
->window
);
577 static gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
,
580 frontend
*fe
= (frontend
*)data
;
586 if (event
->type
!= GDK_BUTTON_PRESS
&& event
->type
!= GDK_BUTTON_RELEASE
)
589 if (event
->button
== 2 || (event
->state
& GDK_SHIFT_MASK
))
590 button
= MIDDLE_BUTTON
;
591 else if (event
->button
== 3 || (event
->state
& GDK_MOD1_MASK
))
592 button
= RIGHT_BUTTON
;
593 else if (event
->button
== 1)
594 button
= LEFT_BUTTON
;
596 return FALSE
; /* don't even know what button! */
598 if (event
->type
== GDK_BUTTON_RELEASE
)
599 button
+= LEFT_RELEASE
- LEFT_BUTTON
;
601 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
602 event
->y
- fe
->oy
, button
))
603 gtk_widget_destroy(fe
->window
);
608 static gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
,
611 frontend
*fe
= (frontend
*)data
;
617 if (event
->state
& (GDK_BUTTON2_MASK
| GDK_SHIFT_MASK
))
618 button
= MIDDLE_DRAG
;
619 else if (event
->state
& GDK_BUTTON1_MASK
)
621 else if (event
->state
& GDK_BUTTON3_MASK
)
624 return FALSE
; /* don't even know what button! */
626 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
627 event
->y
- fe
->oy
, button
))
628 gtk_widget_destroy(fe
->window
);
633 static gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
,
636 frontend
*fe
= (frontend
*)data
;
639 gdk_draw_pixmap(widget
->window
,
640 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
642 event
->area
.x
- fe
->ox
, event
->area
.y
- fe
->oy
,
643 event
->area
.x
, event
->area
.y
,
644 event
->area
.width
, event
->area
.height
);
649 static gint
map_window(GtkWidget
*widget
, GdkEvent
*event
,
652 frontend
*fe
= (frontend
*)data
;
655 * Apparently we need to do this because otherwise the status
656 * bar will fail to update immediately. Annoying, but there we
659 gtk_widget_queue_draw(fe
->window
);
664 static gint
configure_area(GtkWidget
*widget
,
665 GdkEventConfigure
*event
, gpointer data
)
667 frontend
*fe
= (frontend
*)data
;
672 gdk_pixmap_unref(fe
->pixmap
);
674 x
= fe
->w
= event
->width
;
675 y
= fe
->h
= event
->height
;
676 midend_size(fe
->me
, &x
, &y
, TRUE
);
679 fe
->ox
= (fe
->w
- fe
->pw
) / 2;
680 fe
->oy
= (fe
->h
- fe
->ph
) / 2;
682 fe
->pixmap
= gdk_pixmap_new(widget
->window
, fe
->pw
, fe
->ph
, -1);
684 gc
= gdk_gc_new(fe
->area
->window
);
685 gdk_gc_set_foreground(gc
, &fe
->colours
[0]);
686 gdk_draw_rectangle(fe
->pixmap
, gc
, 1, 0, 0, fe
->pw
, fe
->ph
);
687 gdk_draw_rectangle(widget
->window
, gc
, 1, 0, 0,
688 event
->width
, event
->height
);
691 midend_force_redraw(fe
->me
);
696 static gint
timer_func(gpointer data
)
698 frontend
*fe
= (frontend
*)data
;
700 if (fe
->timer_active
) {
703 gettimeofday(&now
, NULL
);
704 elapsed
= ((now
.tv_usec
- fe
->last_time
.tv_usec
) * 0.000001F
+
705 (now
.tv_sec
- fe
->last_time
.tv_sec
));
706 midend_timer(fe
->me
, elapsed
); /* may clear timer_active */
710 return fe
->timer_active
;
713 void deactivate_timer(frontend
*fe
)
716 return; /* can happen due to --generate */
717 if (fe
->timer_active
)
718 gtk_timeout_remove(fe
->timer_id
);
719 fe
->timer_active
= FALSE
;
722 void activate_timer(frontend
*fe
)
725 return; /* can happen due to --generate */
726 if (!fe
->timer_active
) {
727 fe
->timer_id
= gtk_timeout_add(20, timer_func
, fe
);
728 gettimeofday(&fe
->last_time
, NULL
);
730 fe
->timer_active
= TRUE
;
733 static void window_destroy(GtkWidget
*widget
, gpointer data
)
738 static void msgbox_button_clicked(GtkButton
*button
, gpointer data
)
740 GtkWidget
*window
= GTK_WIDGET(data
);
743 ip
= (int *)gtk_object_get_data(GTK_OBJECT(window
), "user-data");
744 v
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button
), "user-data"));
747 gtk_widget_destroy(GTK_WIDGET(data
));
750 static int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
752 GtkObject
*cancelbutton
= GTK_OBJECT(data
);
755 * `Escape' effectively clicks the cancel button
757 if (event
->keyval
== GDK_Escape
) {
758 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton
), "clicked");
765 enum { MB_OK
, MB_YESNO
};
767 int message_box(GtkWidget
*parent
, char *title
, char *msg
, int centre
,
770 GtkWidget
*window
, *hbox
, *text
, *button
;
774 window
= gtk_dialog_new();
775 text
= gtk_label_new(msg
);
776 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
777 hbox
= gtk_hbox_new(FALSE
, 0);
778 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
779 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
780 hbox
, FALSE
, FALSE
, 20);
781 gtk_widget_show(text
);
782 gtk_widget_show(hbox
);
783 gtk_window_set_title(GTK_WINDOW(window
), title
);
784 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
790 assert(type
== MB_YESNO
);
791 titles
= "Yes\0No\0";
798 button
= gtk_button_new_with_label(titles
);
799 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
800 button
, FALSE
, FALSE
, 0);
801 gtk_widget_show(button
);
803 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
804 gtk_window_set_default(GTK_WINDOW(window
), button
);
807 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
808 GTK_SIGNAL_FUNC(win_key_press
), button
);
810 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
811 GTK_SIGNAL_FUNC(msgbox_button_clicked
), window
);
812 gtk_object_set_data(GTK_OBJECT(button
), "user-data",
814 titles
+= strlen(titles
)+1;
817 gtk_object_set_data(GTK_OBJECT(window
), "user-data",
818 GINT_TO_POINTER(&i
));
819 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
820 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
821 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
822 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(parent
));
823 /* set_transient_window_pos(parent, window); */
824 gtk_widget_show(window
);
827 return (type
== MB_YESNO ? i
== 0 : TRUE
);
830 void error_box(GtkWidget
*parent
, char *msg
)
832 message_box(parent
, "Error", msg
, FALSE
, MB_OK
);
835 static void config_ok_button_clicked(GtkButton
*button
, gpointer data
)
837 frontend
*fe
= (frontend
*)data
;
840 err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
843 error_box(fe
->cfgbox
, err
);
846 gtk_widget_destroy(fe
->cfgbox
);
850 static void config_cancel_button_clicked(GtkButton
*button
, gpointer data
)
852 frontend
*fe
= (frontend
*)data
;
854 gtk_widget_destroy(fe
->cfgbox
);
857 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
860 * GtkEntry has a nasty habit of eating the Return key, which
861 * is unhelpful since it doesn't actually _do_ anything with it
862 * (it calls gtk_widget_activate, but our edit boxes never need
863 * activating). So I catch Return before GtkEntry sees it, and
864 * pass it straight on to the parent widget. Effect: hitting
865 * Return in an edit box will now activate the default button
866 * in the dialog just like it will everywhere else.
868 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
870 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
871 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
878 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
880 config_item
*i
= (config_item
*)data
;
883 i
->sval
= dupstr(gtk_entry_get_text(GTK_ENTRY(ed
)));
886 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
888 config_item
*i
= (config_item
*)data
;
890 i
->ival
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb
));
893 static void droplist_sel(GtkMenuItem
*item
, gpointer data
)
895 config_item
*i
= (config_item
*)data
;
897 i
->ival
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
),
901 static int get_config(frontend
*fe
, int which
)
903 GtkWidget
*w
, *table
, *cancel
;
908 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
909 fe
->cfg_which
= which
;
912 fe
->cfgbox
= gtk_dialog_new();
913 gtk_window_set_title(GTK_WINDOW(fe
->cfgbox
), title
);
916 w
= gtk_button_new_with_label("OK");
917 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
920 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
921 gtk_window_set_default(GTK_WINDOW(fe
->cfgbox
), w
);
922 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
923 GTK_SIGNAL_FUNC(config_ok_button_clicked
), fe
);
925 w
= gtk_button_new_with_label("Cancel");
926 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
929 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
930 GTK_SIGNAL_FUNC(config_cancel_button_clicked
), fe
);
933 table
= gtk_table_new(1, 2, FALSE
);
935 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->vbox
),
936 table
, FALSE
, FALSE
, 0);
937 gtk_widget_show(table
);
939 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
940 gtk_table_resize(GTK_TABLE(table
), y
+1, 2);
945 * Edit box with a label beside it.
948 w
= gtk_label_new(i
->name
);
949 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
950 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
951 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
952 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
957 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
958 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
959 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
961 gtk_entry_set_text(GTK_ENTRY(w
), i
->sval
);
962 gtk_signal_connect(GTK_OBJECT(w
), "changed",
963 GTK_SIGNAL_FUNC(editbox_changed
), i
);
964 gtk_signal_connect(GTK_OBJECT(w
), "key_press_event",
965 GTK_SIGNAL_FUNC(editbox_key
), NULL
);
974 w
= gtk_check_button_new_with_label(i
->name
);
975 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
976 GTK_SIGNAL_FUNC(button_toggled
), i
);
977 gtk_table_attach(GTK_TABLE(table
), w
, 0, 2, y
, y
+1,
978 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
979 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
981 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w
), i
->ival
);
987 * Drop-down list (GtkOptionMenu).
990 w
= gtk_label_new(i
->name
);
991 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
992 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
993 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
994 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
998 w
= gtk_option_menu_new();
999 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
1000 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1001 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1008 GtkWidget
*menuitem
;
1009 GtkWidget
*menu
= gtk_menu_new();
1011 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), menu
);
1019 while (*q
&& *q
!= c
)
1022 name
= snewn(q
-p
+1, char);
1023 strncpy(name
, p
, q
-p
);
1026 if (*q
) q
++; /* eat delimiter */
1028 menuitem
= gtk_menu_item_new_with_label(name
);
1029 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1030 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1031 GINT_TO_POINTER(val
));
1032 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1033 GTK_SIGNAL_FUNC(droplist_sel
), i
);
1034 gtk_widget_show(menuitem
);
1041 gtk_option_menu_set_history(GTK_OPTION_MENU(w
), i
->ival
);
1050 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "destroy",
1051 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1052 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "key_press_event",
1053 GTK_SIGNAL_FUNC(win_key_press
), cancel
);
1054 gtk_window_set_modal(GTK_WINDOW(fe
->cfgbox
), TRUE
);
1055 gtk_window_set_transient_for(GTK_WINDOW(fe
->cfgbox
),
1056 GTK_WINDOW(fe
->window
));
1057 /* set_transient_window_pos(fe->window, fe->cfgbox); */
1058 gtk_widget_show(fe
->cfgbox
);
1066 static void menu_key_event(GtkMenuItem
*menuitem
, gpointer data
)
1068 frontend
*fe
= (frontend
*)data
;
1069 int key
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1071 if (!midend_process_key(fe
->me
, 0, 0, key
))
1072 gtk_widget_destroy(fe
->window
);
1075 static void get_size(frontend
*fe
, int *px
, int *py
)
1080 * Currently I don't want to make the GTK port scale large
1081 * puzzles to fit on the screen. This is because X does permit
1082 * extremely large windows and many window managers provide a
1083 * means of navigating round them, and the users I consulted
1084 * before deciding said that they'd rather have enormous puzzle
1085 * windows spanning multiple screen pages than have them
1086 * shrunk. I could change my mind later or introduce
1087 * configurability; this would be the place to do so, by
1088 * replacing the initial values of x and y with the screen
1093 midend_size(fe
->me
, &x
, &y
, FALSE
);
1098 #if !GTK_CHECK_VERSION(2,0,0)
1099 #define gtk_window_resize(win, x, y) \
1100 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
1103 static void resize_fe(frontend
*fe
)
1107 get_size(fe
, &x
, &y
);
1110 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1113 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1114 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1117 * Now that we've established the preferred size of the window,
1118 * reduce the drawing area's size request so the user can shrink
1121 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), 1, 1);
1124 static void menu_preset_event(GtkMenuItem
*menuitem
, gpointer data
)
1126 frontend
*fe
= (frontend
*)data
;
1127 game_params
*params
=
1128 (game_params
*)gtk_object_get_data(GTK_OBJECT(menuitem
), "user-data");
1130 midend_set_params(fe
->me
, params
);
1131 midend_new_game(fe
->me
);
1135 GdkAtom compound_text_atom
, utf8_string_atom
;
1136 int paste_initialised
= FALSE
;
1140 unsigned char empty
[] = { 0 };
1142 if (paste_initialised
)
1145 if (!compound_text_atom
)
1146 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
1147 if (!utf8_string_atom
)
1148 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
1151 * Ensure that all the cut buffers exist - according to the
1152 * ICCCM, we must do this before we start using cut buffers.
1154 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1155 XA_CUT_BUFFER0
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1156 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1157 XA_CUT_BUFFER1
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1158 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1159 XA_CUT_BUFFER2
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1160 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1161 XA_CUT_BUFFER3
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1162 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1163 XA_CUT_BUFFER4
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1164 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1165 XA_CUT_BUFFER5
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1166 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1167 XA_CUT_BUFFER6
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1168 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1169 XA_CUT_BUFFER7
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1172 /* Store data in a cut-buffer. */
1173 void store_cutbuffer(char *ptr
, int len
)
1175 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1176 XRotateBuffers(GDK_DISPLAY(), 1);
1177 XStoreBytes(GDK_DISPLAY(), ptr
, len
);
1180 void write_clip(frontend
*fe
, char *data
)
1185 sfree(fe
->paste_data
);
1188 * For this simple application we can safely assume that the
1189 * data passed to this function is pure ASCII, which means we
1190 * can return precisely the same stuff for types STRING,
1191 * COMPOUND_TEXT or UTF8_STRING.
1194 fe
->paste_data
= data
;
1195 fe
->paste_data_len
= strlen(data
);
1197 store_cutbuffer(fe
->paste_data
, fe
->paste_data_len
);
1199 if (gtk_selection_owner_set(fe
->area
, GDK_SELECTION_PRIMARY
,
1201 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1202 GDK_SELECTION_TYPE_STRING
, 1);
1203 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1204 compound_text_atom
, 1);
1205 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1206 utf8_string_atom
, 1);
1210 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1211 guint info
, guint time_stamp
, gpointer data
)
1213 frontend
*fe
= (frontend
*)data
;
1214 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1215 fe
->paste_data
, fe
->paste_data_len
);
1218 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1221 frontend
*fe
= (frontend
*)data
;
1224 sfree(fe
->paste_data
);
1225 fe
->paste_data
= NULL
;
1226 fe
->paste_data_len
= 0;
1230 static void menu_copy_event(GtkMenuItem
*menuitem
, gpointer data
)
1232 frontend
*fe
= (frontend
*)data
;
1235 text
= midend_text_format(fe
->me
);
1238 write_clip(fe
, text
);
1244 static void filesel_ok(GtkButton
*button
, gpointer data
)
1246 frontend
*fe
= (frontend
*)data
;
1248 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1251 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1253 fe
->filesel_name
= dupstr(name
);
1256 static char *file_selector(frontend
*fe
, char *title
, int save
)
1258 GtkWidget
*filesel
=
1259 gtk_file_selection_new(title
);
1261 fe
->filesel_name
= NULL
;
1263 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1265 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1268 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1269 GTK_SIGNAL_FUNC(filesel_ok
), fe
);
1270 gtk_signal_connect_object
1271 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1272 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1273 gtk_signal_connect_object
1274 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1275 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1276 gtk_signal_connect(GTK_OBJECT(filesel
), "destroy",
1277 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1278 gtk_widget_show(filesel
);
1279 gtk_window_set_transient_for(GTK_WINDOW(filesel
), GTK_WINDOW(fe
->window
));
1282 return fe
->filesel_name
;
1285 static void savefile_write(void *wctx
, void *buf
, int len
)
1287 FILE *fp
= (FILE *)wctx
;
1288 fwrite(buf
, 1, len
, fp
);
1291 static int savefile_read(void *wctx
, void *buf
, int len
)
1293 FILE *fp
= (FILE *)wctx
;
1296 ret
= fread(buf
, 1, len
, fp
);
1297 return (ret
== len
);
1300 static void menu_save_event(GtkMenuItem
*menuitem
, gpointer data
)
1302 frontend
*fe
= (frontend
*)data
;
1305 name
= file_selector(fe
, "Enter name of game file to save", TRUE
);
1310 if ((fp
= fopen(name
, "r")) != NULL
) {
1311 char buf
[256 + FILENAME_MAX
];
1315 sprintf(buf
, "Are you sure you want to overwrite the"
1317 FILENAME_MAX
, name
);
1318 if (!message_box(fe
->window
, "Question", buf
, TRUE
, MB_YESNO
))
1322 fp
= fopen(name
, "w");
1326 error_box(fe
->window
, "Unable to open save file");
1330 midend_serialise(fe
->me
, savefile_write
, fp
);
1336 static void menu_load_event(GtkMenuItem
*menuitem
, gpointer data
)
1338 frontend
*fe
= (frontend
*)data
;
1341 name
= file_selector(fe
, "Enter name of saved game file to load", FALSE
);
1344 FILE *fp
= fopen(name
, "r");
1348 error_box(fe
->window
, "Unable to open saved game file");
1352 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1357 error_box(fe
->window
, err
);
1365 static void menu_solve_event(GtkMenuItem
*menuitem
, gpointer data
)
1367 frontend
*fe
= (frontend
*)data
;
1370 msg
= midend_solve(fe
->me
);
1373 error_box(fe
->window
, msg
);
1376 static void menu_restart_event(GtkMenuItem
*menuitem
, gpointer data
)
1378 frontend
*fe
= (frontend
*)data
;
1380 midend_restart_game(fe
->me
);
1383 static void menu_config_event(GtkMenuItem
*menuitem
, gpointer data
)
1385 frontend
*fe
= (frontend
*)data
;
1386 int which
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1389 if (!get_config(fe
, which
))
1392 midend_new_game(fe
->me
);
1396 static void menu_about_event(GtkMenuItem
*menuitem
, gpointer data
)
1398 frontend
*fe
= (frontend
*)data
;
1402 sprintf(titlebuf
, "About %.200s", thegame
.name
);
1405 "from Simon Tatham's Portable Puzzle Collection\n\n"
1406 "%.500s", thegame
.name
, ver
);
1408 message_box(fe
->window
, titlebuf
, textbuf
, TRUE
, MB_OK
);
1411 static GtkWidget
*add_menu_item_with_key(frontend
*fe
, GtkContainer
*cont
,
1412 char *text
, int key
)
1414 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
1416 gtk_container_add(cont
, menuitem
);
1417 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1418 GINT_TO_POINTER(key
));
1419 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1420 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
1421 switch (key
& ~0x1F) {
1424 keyqual
= GDK_CONTROL_MASK
;
1428 keyqual
= GDK_SHIFT_MASK
;
1434 gtk_widget_add_accelerator(menuitem
,
1435 "activate", fe
->accelgroup
,
1438 gtk_widget_show(menuitem
);
1442 static void add_menu_separator(GtkContainer
*cont
)
1444 GtkWidget
*menuitem
= gtk_menu_item_new();
1445 gtk_container_add(cont
, menuitem
);
1446 gtk_widget_show(menuitem
);
1449 enum { ARG_EITHER
, ARG_SAVE
, ARG_ID
}; /* for argtype */
1451 static frontend
*new_window(char *arg
, int argtype
, char **error
)
1455 GtkWidget
*menubar
, *menu
, *menuitem
;
1460 extern char *const *const xpm_icons
[];
1461 extern const int n_xpm_icons
;
1463 fe
= snew(frontend
);
1465 fe
->timer_active
= FALSE
;
1468 fe
->me
= midend_new(fe
, &thegame
, >k_drawing
, fe
);
1478 err
= midend_game_id(fe
->me
, arg
);
1480 midend_new_game(fe
->me
);
1482 sprintf(errbuf
, "Invalid game ID: %.800s", err
);
1485 fp
= fopen(arg
, "r");
1487 sprintf(errbuf
, "Error opening file: %.800s", strerror(errno
));
1489 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1491 sprintf(errbuf
, "Invalid save file: %.800s", err
);
1495 default /*case ARG_EITHER*/:
1497 * First try treating the argument as a game ID.
1499 err
= midend_game_id(fe
->me
, arg
);
1502 * It's a valid game ID.
1504 midend_new_game(fe
->me
);
1506 FILE *fp
= fopen(arg
, "r");
1508 sprintf(errbuf
, "Supplied argument is neither a game ID (%.400s)"
1509 " nor a save file (%.400s)", err
, strerror(errno
));
1511 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1513 sprintf(errbuf
, "%.800s", err
);
1520 *error
= dupstr(errbuf
);
1521 midend_free(fe
->me
);
1527 midend_new_game(fe
->me
);
1530 fe
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1531 gtk_window_set_title(GTK_WINDOW(fe
->window
), thegame
.name
);
1533 vbox
= GTK_BOX(gtk_vbox_new(FALSE
, 0));
1534 gtk_container_add(GTK_CONTAINER(fe
->window
), GTK_WIDGET(vbox
));
1535 gtk_widget_show(GTK_WIDGET(vbox
));
1537 fe
->accelgroup
= gtk_accel_group_new();
1538 gtk_window_add_accel_group(GTK_WINDOW(fe
->window
), fe
->accelgroup
);
1540 menubar
= gtk_menu_bar_new();
1541 gtk_box_pack_start(vbox
, menubar
, FALSE
, FALSE
, 0);
1542 gtk_widget_show(menubar
);
1544 menuitem
= gtk_menu_item_new_with_label("Game");
1545 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1546 gtk_widget_show(menuitem
);
1548 menu
= gtk_menu_new();
1549 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1551 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "New", 'n');
1553 menuitem
= gtk_menu_item_new_with_label("Restart");
1554 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1555 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1556 GTK_SIGNAL_FUNC(menu_restart_event
), fe
);
1557 gtk_widget_show(menuitem
);
1559 menuitem
= gtk_menu_item_new_with_label("Specific...");
1560 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1561 GINT_TO_POINTER(CFG_DESC
));
1562 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1563 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1564 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1565 gtk_widget_show(menuitem
);
1567 menuitem
= gtk_menu_item_new_with_label("Random Seed...");
1568 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1569 GINT_TO_POINTER(CFG_SEED
));
1570 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1571 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1572 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1573 gtk_widget_show(menuitem
);
1575 if ((n
= midend_num_presets(fe
->me
)) > 0 || thegame
.can_configure
) {
1579 menuitem
= gtk_menu_item_new_with_label("Type");
1580 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1581 gtk_widget_show(menuitem
);
1583 submenu
= gtk_menu_new();
1584 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), submenu
);
1586 for (i
= 0; i
< n
; i
++) {
1588 game_params
*params
;
1590 midend_fetch_preset(fe
->me
, i
, &name
, ¶ms
);
1592 menuitem
= gtk_menu_item_new_with_label(name
);
1593 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1594 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", params
);
1595 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1596 GTK_SIGNAL_FUNC(menu_preset_event
), fe
);
1597 gtk_widget_show(menuitem
);
1600 if (thegame
.can_configure
) {
1601 menuitem
= gtk_menu_item_new_with_label("Custom...");
1602 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1603 GPOINTER_TO_INT(CFG_SETTINGS
));
1604 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1605 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1606 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1607 gtk_widget_show(menuitem
);
1611 add_menu_separator(GTK_CONTAINER(menu
));
1612 menuitem
= gtk_menu_item_new_with_label("Load...");
1613 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1614 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1615 GTK_SIGNAL_FUNC(menu_load_event
), fe
);
1616 gtk_widget_show(menuitem
);
1617 menuitem
= gtk_menu_item_new_with_label("Save...");
1618 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1619 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1620 GTK_SIGNAL_FUNC(menu_save_event
), fe
);
1621 gtk_widget_show(menuitem
);
1622 add_menu_separator(GTK_CONTAINER(menu
));
1623 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Undo", 'u');
1624 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Redo", 'r');
1625 if (thegame
.can_format_as_text
) {
1626 add_menu_separator(GTK_CONTAINER(menu
));
1627 menuitem
= gtk_menu_item_new_with_label("Copy");
1628 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1629 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1630 GTK_SIGNAL_FUNC(menu_copy_event
), fe
);
1631 gtk_widget_show(menuitem
);
1633 if (thegame
.can_solve
) {
1634 add_menu_separator(GTK_CONTAINER(menu
));
1635 menuitem
= gtk_menu_item_new_with_label("Solve");
1636 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1637 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1638 GTK_SIGNAL_FUNC(menu_solve_event
), fe
);
1639 gtk_widget_show(menuitem
);
1641 add_menu_separator(GTK_CONTAINER(menu
));
1642 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Exit", 'q');
1644 menuitem
= gtk_menu_item_new_with_label("Help");
1645 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1646 gtk_widget_show(menuitem
);
1648 menu
= gtk_menu_new();
1649 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1651 menuitem
= gtk_menu_item_new_with_label("About");
1652 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1653 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1654 GTK_SIGNAL_FUNC(menu_about_event
), fe
);
1655 gtk_widget_show(menuitem
);
1662 fe
->colmap
= gdk_colormap_get_system();
1663 colours
= midend_colours(fe
->me
, &ncolours
);
1664 fe
->ncolours
= ncolours
;
1665 fe
->colours
= snewn(ncolours
, GdkColor
);
1666 for (i
= 0; i
< ncolours
; i
++) {
1667 fe
->colours
[i
].red
= colours
[i
*3] * 0xFFFF;
1668 fe
->colours
[i
].green
= colours
[i
*3+1] * 0xFFFF;
1669 fe
->colours
[i
].blue
= colours
[i
*3+2] * 0xFFFF;
1671 success
= snewn(ncolours
, gboolean
);
1672 gdk_colormap_alloc_colors(fe
->colmap
, fe
->colours
, ncolours
,
1673 FALSE
, FALSE
, success
);
1674 for (i
= 0; i
< ncolours
; i
++) {
1676 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
1677 i
, fe
->colours
[i
].red
>> 8,
1678 fe
->colours
[i
].green
>> 8,
1679 fe
->colours
[i
].blue
>> 8);
1683 if (midend_wants_statusbar(fe
->me
)) {
1684 GtkWidget
*viewport
;
1687 viewport
= gtk_viewport_new(NULL
, NULL
);
1688 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport
), GTK_SHADOW_NONE
);
1689 fe
->statusbar
= gtk_statusbar_new();
1690 gtk_container_add(GTK_CONTAINER(viewport
), fe
->statusbar
);
1691 gtk_widget_show(viewport
);
1692 gtk_box_pack_end(vbox
, viewport
, FALSE
, FALSE
, 0);
1693 gtk_widget_show(fe
->statusbar
);
1694 fe
->statusctx
= gtk_statusbar_get_context_id
1695 (GTK_STATUSBAR(fe
->statusbar
), "game");
1696 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
,
1698 gtk_widget_size_request(fe
->statusbar
, &req
);
1700 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
1702 gtk_widget_set_usize(viewport
, -1, req
.height
);
1704 fe
->statusbar
= NULL
;
1706 fe
->area
= gtk_drawing_area_new();
1707 get_size(fe
, &x
, &y
);
1708 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1712 gtk_box_pack_end(vbox
, fe
->area
, TRUE
, TRUE
, 0);
1716 fe
->nfonts
= fe
->fontsize
= 0;
1718 fe
->paste_data
= NULL
;
1719 fe
->paste_data_len
= 0;
1721 gtk_signal_connect(GTK_OBJECT(fe
->window
), "destroy",
1722 GTK_SIGNAL_FUNC(destroy
), fe
);
1723 gtk_signal_connect(GTK_OBJECT(fe
->window
), "key_press_event",
1724 GTK_SIGNAL_FUNC(key_event
), fe
);
1725 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_press_event",
1726 GTK_SIGNAL_FUNC(button_event
), fe
);
1727 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_release_event",
1728 GTK_SIGNAL_FUNC(button_event
), fe
);
1729 gtk_signal_connect(GTK_OBJECT(fe
->area
), "motion_notify_event",
1730 GTK_SIGNAL_FUNC(motion_event
), fe
);
1731 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_get",
1732 GTK_SIGNAL_FUNC(selection_get
), fe
);
1733 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_clear_event",
1734 GTK_SIGNAL_FUNC(selection_clear
), fe
);
1735 gtk_signal_connect(GTK_OBJECT(fe
->area
), "expose_event",
1736 GTK_SIGNAL_FUNC(expose_area
), fe
);
1737 gtk_signal_connect(GTK_OBJECT(fe
->window
), "map_event",
1738 GTK_SIGNAL_FUNC(map_window
), fe
);
1739 gtk_signal_connect(GTK_OBJECT(fe
->area
), "configure_event",
1740 GTK_SIGNAL_FUNC(configure_area
), fe
);
1742 gtk_widget_add_events(GTK_WIDGET(fe
->area
),
1743 GDK_BUTTON_PRESS_MASK
|
1744 GDK_BUTTON_RELEASE_MASK
|
1745 GDK_BUTTON_MOTION_MASK
);
1748 gtk_widget_realize(fe
->window
);
1749 iconpm
= gdk_pixmap_create_from_xpm_d(fe
->window
->window
, NULL
,
1750 NULL
, (gchar
**)xpm_icons
[0]);
1751 gdk_window_set_icon(fe
->window
->window
, NULL
, iconpm
, NULL
);
1753 for (n
= 0; n
< n_xpm_icons
; n
++) {
1755 g_list_append(iconlist
,
1756 gdk_pixbuf_new_from_xpm_data((const gchar
**)
1759 gdk_window_set_icon_list(fe
->window
->window
, iconlist
);
1762 gtk_widget_show(fe
->area
);
1763 gtk_widget_show(fe
->window
);
1766 * Now that we've established the preferred size of the window,
1767 * reduce the drawing area's size request so the user can shrink
1770 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), 1, 1);
1772 gdk_window_set_background(fe
->area
->window
, &fe
->colours
[0]);
1773 gdk_window_set_background(fe
->window
->window
, &fe
->colours
[0]);
1778 char *fgetline(FILE *fp
)
1780 char *ret
= snewn(512, char);
1781 int size
= 512, len
= 0;
1782 while (fgets(ret
+ len
, size
- len
, fp
)) {
1783 len
+= strlen(ret
+ len
);
1784 if (ret
[len
-1] == '\n')
1785 break; /* got a newline, we're done */
1787 ret
= sresize(ret
, size
, char);
1789 if (len
== 0) { /* first fgets returned NULL */
1797 int main(int argc
, char **argv
)
1799 char *pname
= argv
[0];
1801 int ngenerate
= 0, print
= FALSE
, px
= 1, py
= 1;
1802 int soln
= FALSE
, colour
= FALSE
;
1804 float redo_proportion
= 0.0F
;
1806 int argtype
= ARG_EITHER
;
1807 char *screenshot_file
= NULL
;
1808 int doing_opts
= TRUE
;
1814 * Command line parsing in this function is rather fiddly,
1815 * because GTK wants to have a go at argc/argv _first_ - and
1816 * yet we can't let it, because gtk_init() will bomb out if it
1817 * can't open an X display, whereas in fact we want to permit
1818 * our --generate and --print modes to run without an X
1822 * - we parse the command line ourselves, without modifying
1824 * - if we encounter an error which might plausibly be the
1825 * result of a GTK command line (i.e. not detailed errors in
1826 * particular options of ours) we store the error message
1827 * and terminate parsing.
1828 * - if we got enough out of the command line to know it
1829 * specifies a non-X mode of operation, we either display
1830 * the stored error and return failure, or if there is no
1831 * stored error we do the non-X operation and return
1833 * - otherwise, we go straight to gtk_init().
1839 if (doing_opts
&& !strcmp(p
, "--version")) {
1840 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
1843 } else if (doing_opts
&& !strcmp(p
, "--generate")) {
1845 ngenerate
= atoi(*++av
);
1847 fprintf(stderr
, "%s: '--generate' expected a number\n",
1853 } else if (doing_opts
&& !strcmp(p
, "--print")) {
1854 if (!thegame
.can_print
) {
1855 fprintf(stderr
, "%s: this game does not support printing\n",
1862 if (sscanf(dim
, "%dx%d", &px
, &py
) != 2) {
1863 fprintf(stderr
, "%s: unable to parse argument '%s' to "
1864 "'--print'\n", pname
, dim
);
1870 } else if (doing_opts
&& !strcmp(p
, "--scale")) {
1872 scale
= atof(*++av
);
1874 fprintf(stderr
, "%s: no argument supplied to '--scale'\n",
1878 } else if (doing_opts
&& !strcmp(p
, "--redo")) {
1880 * This is an internal option which I don't expect
1881 * users to have any particular use for. The effect of
1882 * --redo is that once the game has been loaded and
1883 * initialised, the next move in the redo chain is
1884 * replayed, and the game screen is redrawn part way
1885 * through the making of the move. This is only
1886 * meaningful if there _is_ a next move in the redo
1887 * chain, which means in turn that this option is only
1888 * useful if you're also passing a save file on the
1891 * This option is used by the script which generates
1892 * the puzzle icons and website screenshots, and I
1893 * don't imagine it's useful for anything else.
1894 * (Unless, I suppose, users don't like my screenshots
1895 * and want to generate their own in the same way for
1896 * some repackaged version of the puzzles.)
1899 redo_proportion
= atof(*++av
);
1901 fprintf(stderr
, "%s: no argument supplied to '--redo'\n",
1905 } else if (doing_opts
&& !strcmp(p
, "--screenshot")) {
1907 * Another internal option for the icon building
1908 * script. This causes a screenshot of the central
1909 * drawing area (i.e. not including the menu bar or
1910 * status bar) to be saved to a PNG file once the
1911 * window has been drawn, and then the application
1912 * quits immediately.
1915 screenshot_file
= *++av
;
1917 fprintf(stderr
, "%s: no argument supplied to '--screenshot'\n",
1921 } else if (doing_opts
&& (!strcmp(p
, "--with-solutions") ||
1922 !strcmp(p
, "--with-solution") ||
1923 !strcmp(p
, "--with-solns") ||
1924 !strcmp(p
, "--with-soln") ||
1925 !strcmp(p
, "--solutions") ||
1926 !strcmp(p
, "--solution") ||
1927 !strcmp(p
, "--solns") ||
1928 !strcmp(p
, "--soln"))) {
1930 } else if (doing_opts
&& !strcmp(p
, "--colour")) {
1931 if (!thegame
.can_print_in_colour
) {
1932 fprintf(stderr
, "%s: this game does not support colour"
1933 " printing\n", pname
);
1937 } else if (doing_opts
&& !strcmp(p
, "--load")) {
1939 } else if (doing_opts
&& !strcmp(p
, "--game")) {
1941 } else if (doing_opts
&& !strcmp(p
, "--")) {
1943 } else if (!doing_opts
|| p
[0] != '-') {
1945 fprintf(stderr
, "%s: more than one argument supplied\n",
1951 sprintf(errbuf
, "%.100s: unrecognised option '%.100s'\n",
1958 fputs(errbuf
, stderr
);
1963 * Special standalone mode for generating puzzle IDs on the
1964 * command line. Useful for generating puzzles to be printed
1965 * out and solved offline (for puzzles where that even makes
1966 * sense - Solo, for example, is a lot more pencil-and-paper
1967 * friendly than Twiddle!)
1971 * <puzzle-name> --generate [<n> [<params>]]
1973 * <n>, if present, is the number of puzzle IDs to generate.
1974 * <params>, if present, is the same type of parameter string
1975 * you would pass to the puzzle when running it in GUI mode,
1976 * including optional extras such as the expansion factor in
1977 * Rectangles and the difficulty level in Solo.
1979 * If you specify <params>, you must also specify <n> (although
1980 * you may specify it to be 1). Sorry; that was the
1981 * simplest-to-parse command-line syntax I came up with.
1983 if (ngenerate
> 0 || print
) {
1987 document
*doc
= NULL
;
1991 me
= midend_new(NULL
, &thegame
, NULL
, NULL
);
1995 doc
= document_new(px
, py
, scale
);
1998 * In this loop, we either generate a game ID or read one
1999 * from stdin depending on whether we're in generate mode;
2000 * then we either write it to stdout or print it, depending
2001 * on whether we're in print mode. Thus, this loop handles
2002 * generate-to-stdout, print-from-stdin and generate-and-
2003 * immediately-print modes.
2005 * (It could also handle a copy-stdin-to-stdout mode,
2006 * although there's currently no combination of options
2007 * which will cause this loop to be activated in that mode.
2008 * It wouldn't be _entirely_ pointless, though, because
2009 * stdin could contain bare params strings or random-seed
2010 * IDs, and stdout would contain nothing but fully
2011 * generated descriptive game IDs.)
2013 while (ngenerate
== 0 || i
< n
) {
2016 if (ngenerate
== 0) {
2017 pstr
= fgetline(stdin
);
2020 pstr
[strcspn(pstr
, "\r\n")] = '\0';
2023 pstr
= snewn(strlen(arg
) + 40, char);
2026 if (i
> 0 && strchr(arg
, '#'))
2027 sprintf(pstr
+ strlen(pstr
), "-%d", i
);
2033 err
= midend_game_id(me
, pstr
);
2035 fprintf(stderr
, "%s: error parsing '%s': %s\n",
2042 midend_new_game(me
);
2045 err
= midend_print_puzzle(me
, doc
, soln
);
2047 fprintf(stderr
, "%s: error in printing: %s\n", pname
, err
);
2051 id
= midend_get_game_id(me
);
2060 psdata
*ps
= ps_init(stdout
, colour
);
2061 document_print(doc
, ps_drawing_api(ps
));
2072 gtk_init(&argc
, &argv
);
2074 fe
= new_window(arg
, argtype
, &error
);
2077 fprintf(stderr
, "%s: %s\n", pname
, error
);
2081 if (screenshot_file
) {
2083 * Some puzzles will not redraw their entire area if
2084 * given a partially completed animation, which means
2085 * we must redraw now and _then_ redraw again after
2086 * freezing the move timer.
2088 midend_force_redraw(fe
->me
);
2091 if (redo_proportion
) {
2093 midend_process_key(fe
->me
, 0, 0, 'r');
2094 /* And freeze the timer at the specified position. */
2095 midend_freeze_timer(fe
->me
, redo_proportion
);
2098 if (screenshot_file
) {
2100 GError
*gerror
= NULL
;
2102 midend_redraw(fe
->me
);
2104 pb
= gdk_pixbuf_get_from_drawable(NULL
, fe
->pixmap
,
2105 NULL
, 0, 0, 0, 0, -1, -1);
2106 gdk_pixbuf_save(pb
, screenshot_file
, "png", &gerror
, NULL
);