2 * gtk.c: GTK front end for my puzzle collection.
16 #include <gdk/gdkkeysyms.h>
18 #include <gdk-pixbuf/gdk-pixbuf.h>
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
27 #if GTK_CHECK_VERSION(2,0,0)
32 static FILE *debug_fp
= NULL
;
37 debug_fp
= fopen("debug.log", "w");
48 void debug_printf(char *fmt
, ...)
54 vsprintf(buf
, fmt
, ap
);
60 /* ----------------------------------------------------------------------
61 * Error reporting functions used elsewhere.
64 void fatal(char *fmt
, ...)
68 fprintf(stderr
, "fatal error: ");
71 vfprintf(stderr
, fmt
, ap
);
74 fprintf(stderr
, "\n");
78 /* ----------------------------------------------------------------------
79 * GTK front end to puzzles.
82 static void changed_preset(frontend
*fe
);
86 PangoFontDescription
*desc
;
95 * This structure holds all the data relevant to a single window.
96 * In principle this would allow us to open multiple independent
97 * puzzle windows, although I can't currently see any real point in
98 * doing so. I'm just coding cleanly because there's no
99 * particularly good reason not to.
103 GtkAccelGroup
*accelgroup
;
105 GtkWidget
*statusbar
;
114 int bbox_l
, bbox_r
, bbox_u
, bbox_d
;
115 int timer_active
, timer_id
;
116 struct timeval last_time
;
118 int nfonts
, fontsize
;
120 int cfg_which
, cfgret
;
124 int pw
, ph
; /* pixmap size (w, h are area size) */
125 int ox
, oy
; /* offset of pixmap in drawing area */
128 GtkWidget
**preset_bullets
;
129 GtkWidget
*preset_custom_bullet
;
130 GtkWidget
*copy_menu_item
;
133 void get_random_seed(void **randseed
, int *randseedsize
)
135 struct timeval
*tvp
= snew(struct timeval
);
136 gettimeofday(tvp
, NULL
);
137 *randseed
= (void *)tvp
;
138 *randseedsize
= sizeof(struct timeval
);
141 void frontend_default_colour(frontend
*fe
, float *output
)
143 GdkColor col
= fe
->window
->style
->bg
[GTK_STATE_NORMAL
];
144 output
[0] = col
.red
/ 65535.0;
145 output
[1] = col
.green
/ 65535.0;
146 output
[2] = col
.blue
/ 65535.0;
149 void gtk_status_bar(void *handle
, char *text
)
151 frontend
*fe
= (frontend
*)handle
;
153 assert(fe
->statusbar
);
155 gtk_statusbar_pop(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
);
156 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
, text
);
159 void gtk_start_draw(void *handle
)
161 frontend
*fe
= (frontend
*)handle
;
162 fe
->gc
= gdk_gc_new(fe
->area
->window
);
169 void gtk_clip(void *handle
, int x
, int y
, int w
, int h
)
171 frontend
*fe
= (frontend
*)handle
;
179 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
182 void gtk_unclip(void *handle
)
184 frontend
*fe
= (frontend
*)handle
;
192 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
195 void gtk_draw_text(void *handle
, int x
, int y
, int fonttype
, int fontsize
,
196 int align
, int colour
, char *text
)
198 frontend
*fe
= (frontend
*)handle
;
202 * Find or create the font.
204 for (i
= 0; i
< fe
->nfonts
; i
++)
205 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
208 if (i
== fe
->nfonts
) {
209 if (fe
->fontsize
<= fe
->nfonts
) {
210 fe
->fontsize
= fe
->nfonts
+ 10;
211 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
216 fe
->fonts
[i
].type
= fonttype
;
217 fe
->fonts
[i
].size
= fontsize
;
221 * Use Pango to find the closest match to the requested
225 PangoFontDescription
*fd
;
227 fd
= pango_font_description_new();
228 /* `Monospace' and `Sans' are meta-families guaranteed to exist */
229 pango_font_description_set_family(fd
, fonttype
== FONT_FIXED ?
230 "Monospace" : "Sans");
231 pango_font_description_set_weight(fd
, PANGO_WEIGHT_BOLD
);
233 * I found some online Pango documentation which
234 * described a function called
235 * pango_font_description_set_absolute_size(), which is
236 * _exactly_ what I want here. Unfortunately, none of
237 * my local Pango installations have it (presumably
238 * they're too old), so I'm going to have to hack round
239 * it by figuring out the point size myself. This
240 * limits me to X and probably also breaks in later
241 * Pango installations, so ideally I should add another
242 * CHECK_VERSION type ifdef and use set_absolute_size
243 * where available. All very annoying.
245 #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
246 pango_font_description_set_absolute_size(fd
, PANGO_SCALE
*fontsize
);
249 Display
*d
= GDK_DISPLAY();
250 int s
= DefaultScreen(d
);
252 (PANGO_SCALE
* 72.27 / 25.4) *
253 ((double) DisplayWidthMM(d
, s
) / DisplayWidth (d
, s
));
254 pango_font_description_set_size(fd
, resolution
* fontsize
);
257 fe
->fonts
[i
].desc
= fd
;
262 * In GTK 1.2, I don't know of any plausible way to
263 * pick a suitable font, so I'm just going to be
266 fe
->fonts
[i
].font
= gdk_font_load(fonttype
== FONT_FIXED ?
267 "fixed" : "variable");
275 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
286 layout
= pango_layout_new(gtk_widget_get_pango_context(fe
->area
));
287 pango_layout_set_font_description(layout
, fe
->fonts
[i
].desc
);
288 pango_layout_set_text(layout
, text
, strlen(text
));
289 pango_layout_get_pixel_extents(layout
, NULL
, &rect
);
291 if (align
& ALIGN_VCENTRE
)
292 rect
.y
-= rect
.height
/ 2;
294 rect
.y
-= rect
.height
;
296 if (align
& ALIGN_HCENTRE
)
297 rect
.x
-= rect
.width
/ 2;
298 else if (align
& ALIGN_HRIGHT
)
299 rect
.x
-= rect
.width
;
301 gdk_draw_layout(fe
->pixmap
, fe
->gc
, rect
.x
+ x
, rect
.y
+ y
, layout
);
303 g_object_unref(layout
);
308 * Find string dimensions and process alignment.
311 int lb
, rb
, wid
, asc
, desc
;
314 * Measure vertical string extents with respect to the same
317 gdk_string_extents(fe
->fonts
[i
].font
,
318 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
319 &lb
, &rb
, &wid
, &asc
, &desc
);
320 if (align
& ALIGN_VCENTRE
)
321 y
+= asc
- (asc
+desc
)/2;
326 * ... but horizontal extents with respect to the provided
327 * string. This means that multiple pieces of text centred
328 * on the same y-coordinate don't have different baselines.
330 gdk_string_extents(fe
->fonts
[i
].font
, text
,
331 &lb
, &rb
, &wid
, &asc
, &desc
);
333 if (align
& ALIGN_HCENTRE
)
335 else if (align
& ALIGN_HRIGHT
)
341 * Actually draw the text.
343 gdk_draw_string(fe
->pixmap
, fe
->fonts
[i
].font
, fe
->gc
, x
, y
, text
);
348 void gtk_draw_rect(void *handle
, int x
, int y
, int w
, int h
, int colour
)
350 frontend
*fe
= (frontend
*)handle
;
351 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
352 gdk_draw_rectangle(fe
->pixmap
, fe
->gc
, 1, x
, y
, w
, h
);
355 void gtk_draw_line(void *handle
, int x1
, int y1
, int x2
, int y2
, int colour
)
357 frontend
*fe
= (frontend
*)handle
;
358 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
359 gdk_draw_line(fe
->pixmap
, fe
->gc
, x1
, y1
, x2
, y2
);
362 void gtk_draw_poly(void *handle
, int *coords
, int npoints
,
363 int fillcolour
, int outlinecolour
)
365 frontend
*fe
= (frontend
*)handle
;
366 GdkPoint
*points
= snewn(npoints
, GdkPoint
);
369 for (i
= 0; i
< npoints
; i
++) {
370 points
[i
].x
= coords
[i
*2];
371 points
[i
].y
= coords
[i
*2+1];
374 if (fillcolour
>= 0) {
375 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[fillcolour
]);
376 gdk_draw_polygon(fe
->pixmap
, fe
->gc
, TRUE
, points
, npoints
);
378 assert(outlinecolour
>= 0);
379 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[outlinecolour
]);
382 * In principle we ought to be able to use gdk_draw_polygon for
383 * the outline as well. In fact, it turns out to interact badly
384 * with a clipping region, for no terribly obvious reason, so I
385 * draw the outline as a sequence of lines instead.
387 for (i
= 0; i
< npoints
; i
++)
388 gdk_draw_line(fe
->pixmap
, fe
->gc
,
389 points
[i
].x
, points
[i
].y
,
390 points
[(i
+1)%npoints
].x
, points
[(i
+1)%npoints
].y
);
395 void gtk_draw_circle(void *handle
, int cx
, int cy
, int radius
,
396 int fillcolour
, int outlinecolour
)
398 frontend
*fe
= (frontend
*)handle
;
399 if (fillcolour
>= 0) {
400 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[fillcolour
]);
401 gdk_draw_arc(fe
->pixmap
, fe
->gc
, TRUE
,
402 cx
- radius
, cy
- radius
,
403 2 * radius
, 2 * radius
, 0, 360 * 64);
406 assert(outlinecolour
>= 0);
407 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[outlinecolour
]);
408 gdk_draw_arc(fe
->pixmap
, fe
->gc
, FALSE
,
409 cx
- radius
, cy
- radius
,
410 2 * radius
, 2 * radius
, 0, 360 * 64);
418 blitter
*gtk_blitter_new(void *handle
, int w
, int h
)
421 * We can't create the pixmap right now, because fe->window
422 * might not yet exist. So we just cache w and h and create it
423 * during the firs call to blitter_save.
425 blitter
*bl
= snew(blitter
);
432 void gtk_blitter_free(void *handle
, blitter
*bl
)
435 gdk_pixmap_unref(bl
->pixmap
);
439 void gtk_blitter_save(void *handle
, blitter
*bl
, int x
, int y
)
441 frontend
*fe
= (frontend
*)handle
;
443 bl
->pixmap
= gdk_pixmap_new(fe
->area
->window
, bl
->w
, bl
->h
, -1);
446 gdk_draw_pixmap(bl
->pixmap
,
447 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
449 x
, y
, 0, 0, bl
->w
, bl
->h
);
452 void gtk_blitter_load(void *handle
, blitter
*bl
, int x
, int y
)
454 frontend
*fe
= (frontend
*)handle
;
456 if (x
== BLITTER_FROMSAVED
&& y
== BLITTER_FROMSAVED
) {
460 gdk_draw_pixmap(fe
->pixmap
,
461 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
463 0, 0, x
, y
, bl
->w
, bl
->h
);
466 void gtk_draw_update(void *handle
, int x
, int y
, int w
, int h
)
468 frontend
*fe
= (frontend
*)handle
;
469 if (fe
->bbox_l
> x
) fe
->bbox_l
= x
;
470 if (fe
->bbox_r
< x
+w
) fe
->bbox_r
= x
+w
;
471 if (fe
->bbox_u
> y
) fe
->bbox_u
= y
;
472 if (fe
->bbox_d
< y
+h
) fe
->bbox_d
= y
+h
;
475 void gtk_end_draw(void *handle
)
477 frontend
*fe
= (frontend
*)handle
;
478 gdk_gc_unref(fe
->gc
);
481 if (fe
->bbox_l
< fe
->bbox_r
&& fe
->bbox_u
< fe
->bbox_d
) {
482 gdk_draw_pixmap(fe
->area
->window
,
483 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
485 fe
->bbox_l
, fe
->bbox_u
,
486 fe
->ox
+ fe
->bbox_l
, fe
->oy
+ fe
->bbox_u
,
487 fe
->bbox_r
- fe
->bbox_l
, fe
->bbox_d
- fe
->bbox_u
);
491 const struct drawing_api gtk_drawing
= {
507 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, /* {begin,end}_{doc,page,puzzle} */
508 NULL
, /* line_width */
511 static void destroy(GtkWidget
*widget
, gpointer data
)
513 frontend
*fe
= (frontend
*)data
;
514 deactivate_timer(fe
);
519 static gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
521 frontend
*fe
= (frontend
*)data
;
523 int shift
= (event
->state
& GDK_SHIFT_MASK
) ? MOD_SHFT
: 0;
524 int ctrl
= (event
->state
& GDK_CONTROL_MASK
) ? MOD_CTRL
: 0;
529 #if !GTK_CHECK_VERSION(2,0,0)
530 /* Gtk 1.2 passes a key event to this function even if it's also
531 * defined as an accelerator.
532 * Gtk 2 doesn't do this, and this function appears not to exist there. */
533 if (fe
->accelgroup
&&
534 gtk_accel_group_get_entry(fe
->accelgroup
,
535 event
->keyval
, event
->state
))
539 if (event
->keyval
== GDK_Up
)
540 keyval
= shift
| ctrl
| CURSOR_UP
;
541 else if (event
->keyval
== GDK_KP_Up
|| event
->keyval
== GDK_KP_8
)
542 keyval
= MOD_NUM_KEYPAD
| '8';
543 else if (event
->keyval
== GDK_Down
)
544 keyval
= shift
| ctrl
| CURSOR_DOWN
;
545 else if (event
->keyval
== GDK_KP_Down
|| event
->keyval
== GDK_KP_2
)
546 keyval
= MOD_NUM_KEYPAD
| '2';
547 else if (event
->keyval
== GDK_Left
)
548 keyval
= shift
| ctrl
| CURSOR_LEFT
;
549 else if (event
->keyval
== GDK_KP_Left
|| event
->keyval
== GDK_KP_4
)
550 keyval
= MOD_NUM_KEYPAD
| '4';
551 else if (event
->keyval
== GDK_Right
)
552 keyval
= shift
| ctrl
| CURSOR_RIGHT
;
553 else if (event
->keyval
== GDK_KP_Right
|| event
->keyval
== GDK_KP_6
)
554 keyval
= MOD_NUM_KEYPAD
| '6';
555 else if (event
->keyval
== GDK_KP_Home
|| event
->keyval
== GDK_KP_7
)
556 keyval
= MOD_NUM_KEYPAD
| '7';
557 else if (event
->keyval
== GDK_KP_End
|| event
->keyval
== GDK_KP_1
)
558 keyval
= MOD_NUM_KEYPAD
| '1';
559 else if (event
->keyval
== GDK_KP_Page_Up
|| event
->keyval
== GDK_KP_9
)
560 keyval
= MOD_NUM_KEYPAD
| '9';
561 else if (event
->keyval
== GDK_KP_Page_Down
|| event
->keyval
== GDK_KP_3
)
562 keyval
= MOD_NUM_KEYPAD
| '3';
563 else if (event
->keyval
== GDK_KP_Insert
|| event
->keyval
== GDK_KP_0
)
564 keyval
= MOD_NUM_KEYPAD
| '0';
565 else if (event
->keyval
== GDK_KP_Begin
|| event
->keyval
== GDK_KP_5
)
566 keyval
= MOD_NUM_KEYPAD
| '5';
567 else if (event
->keyval
== GDK_BackSpace
||
568 event
->keyval
== GDK_Delete
||
569 event
->keyval
== GDK_KP_Delete
)
571 else if (event
->string
[0] && !event
->string
[1])
572 keyval
= (unsigned char)event
->string
[0];
577 !midend_process_key(fe
->me
, 0, 0, keyval
))
578 gtk_widget_destroy(fe
->window
);
583 static gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
,
586 frontend
*fe
= (frontend
*)data
;
592 if (event
->type
!= GDK_BUTTON_PRESS
&& event
->type
!= GDK_BUTTON_RELEASE
)
595 if (event
->button
== 2 || (event
->state
& GDK_SHIFT_MASK
))
596 button
= MIDDLE_BUTTON
;
597 else if (event
->button
== 3 || (event
->state
& GDK_MOD1_MASK
))
598 button
= RIGHT_BUTTON
;
599 else if (event
->button
== 1)
600 button
= LEFT_BUTTON
;
602 return FALSE
; /* don't even know what button! */
604 if (event
->type
== GDK_BUTTON_RELEASE
)
605 button
+= LEFT_RELEASE
- LEFT_BUTTON
;
607 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
608 event
->y
- fe
->oy
, button
))
609 gtk_widget_destroy(fe
->window
);
614 static gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
,
617 frontend
*fe
= (frontend
*)data
;
623 if (event
->state
& (GDK_BUTTON2_MASK
| GDK_SHIFT_MASK
))
624 button
= MIDDLE_DRAG
;
625 else if (event
->state
& GDK_BUTTON1_MASK
)
627 else if (event
->state
& GDK_BUTTON3_MASK
)
630 return FALSE
; /* don't even know what button! */
632 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
633 event
->y
- fe
->oy
, button
))
634 gtk_widget_destroy(fe
->window
);
639 static gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
,
642 frontend
*fe
= (frontend
*)data
;
645 gdk_draw_pixmap(widget
->window
,
646 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
648 event
->area
.x
- fe
->ox
, event
->area
.y
- fe
->oy
,
649 event
->area
.x
, event
->area
.y
,
650 event
->area
.width
, event
->area
.height
);
655 static gint
map_window(GtkWidget
*widget
, GdkEvent
*event
,
658 frontend
*fe
= (frontend
*)data
;
661 * Apparently we need to do this because otherwise the status
662 * bar will fail to update immediately. Annoying, but there we
665 gtk_widget_queue_draw(fe
->window
);
670 static gint
configure_area(GtkWidget
*widget
,
671 GdkEventConfigure
*event
, gpointer data
)
673 frontend
*fe
= (frontend
*)data
;
678 gdk_pixmap_unref(fe
->pixmap
);
680 x
= fe
->w
= event
->width
;
681 y
= fe
->h
= event
->height
;
682 midend_size(fe
->me
, &x
, &y
, TRUE
);
685 fe
->ox
= (fe
->w
- fe
->pw
) / 2;
686 fe
->oy
= (fe
->h
- fe
->ph
) / 2;
688 fe
->pixmap
= gdk_pixmap_new(widget
->window
, fe
->pw
, fe
->ph
, -1);
690 gc
= gdk_gc_new(fe
->area
->window
);
691 gdk_gc_set_foreground(gc
, &fe
->colours
[0]);
692 gdk_draw_rectangle(fe
->pixmap
, gc
, 1, 0, 0, fe
->pw
, fe
->ph
);
693 gdk_draw_rectangle(widget
->window
, gc
, 1, 0, 0,
694 event
->width
, event
->height
);
697 midend_force_redraw(fe
->me
);
702 static gint
timer_func(gpointer data
)
704 frontend
*fe
= (frontend
*)data
;
706 if (fe
->timer_active
) {
709 gettimeofday(&now
, NULL
);
710 elapsed
= ((now
.tv_usec
- fe
->last_time
.tv_usec
) * 0.000001F
+
711 (now
.tv_sec
- fe
->last_time
.tv_sec
));
712 midend_timer(fe
->me
, elapsed
); /* may clear timer_active */
716 return fe
->timer_active
;
719 void deactivate_timer(frontend
*fe
)
722 return; /* can happen due to --generate */
723 if (fe
->timer_active
)
724 gtk_timeout_remove(fe
->timer_id
);
725 fe
->timer_active
= FALSE
;
728 void activate_timer(frontend
*fe
)
731 return; /* can happen due to --generate */
732 if (!fe
->timer_active
) {
733 fe
->timer_id
= gtk_timeout_add(20, timer_func
, fe
);
734 gettimeofday(&fe
->last_time
, NULL
);
736 fe
->timer_active
= TRUE
;
739 static void window_destroy(GtkWidget
*widget
, gpointer data
)
744 static void msgbox_button_clicked(GtkButton
*button
, gpointer data
)
746 GtkWidget
*window
= GTK_WIDGET(data
);
749 ip
= (int *)gtk_object_get_data(GTK_OBJECT(window
), "user-data");
750 v
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button
), "user-data"));
753 gtk_widget_destroy(GTK_WIDGET(data
));
756 static int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
758 GtkObject
*cancelbutton
= GTK_OBJECT(data
);
761 * `Escape' effectively clicks the cancel button
763 if (event
->keyval
== GDK_Escape
) {
764 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton
), "clicked");
771 enum { MB_OK
, MB_YESNO
};
773 int message_box(GtkWidget
*parent
, char *title
, char *msg
, int centre
,
776 GtkWidget
*window
, *hbox
, *text
, *button
;
780 window
= gtk_dialog_new();
781 text
= gtk_label_new(msg
);
782 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
783 hbox
= gtk_hbox_new(FALSE
, 0);
784 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
785 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
786 hbox
, FALSE
, FALSE
, 20);
787 gtk_widget_show(text
);
788 gtk_widget_show(hbox
);
789 gtk_window_set_title(GTK_WINDOW(window
), title
);
790 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
796 assert(type
== MB_YESNO
);
797 titles
= "Yes\0No\0";
804 button
= gtk_button_new_with_label(titles
);
805 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
806 button
, FALSE
, FALSE
, 0);
807 gtk_widget_show(button
);
809 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
810 gtk_window_set_default(GTK_WINDOW(window
), button
);
813 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
814 GTK_SIGNAL_FUNC(win_key_press
), button
);
816 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
817 GTK_SIGNAL_FUNC(msgbox_button_clicked
), window
);
818 gtk_object_set_data(GTK_OBJECT(button
), "user-data",
820 titles
+= strlen(titles
)+1;
823 gtk_object_set_data(GTK_OBJECT(window
), "user-data",
824 GINT_TO_POINTER(&i
));
825 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
826 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
827 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
828 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(parent
));
829 /* set_transient_window_pos(parent, window); */
830 gtk_widget_show(window
);
833 return (type
== MB_YESNO ? i
== 0 : TRUE
);
836 void error_box(GtkWidget
*parent
, char *msg
)
838 message_box(parent
, "Error", msg
, FALSE
, MB_OK
);
841 static void config_ok_button_clicked(GtkButton
*button
, gpointer data
)
843 frontend
*fe
= (frontend
*)data
;
846 err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
849 error_box(fe
->cfgbox
, err
);
852 gtk_widget_destroy(fe
->cfgbox
);
857 static void config_cancel_button_clicked(GtkButton
*button
, gpointer data
)
859 frontend
*fe
= (frontend
*)data
;
861 gtk_widget_destroy(fe
->cfgbox
);
864 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
867 * GtkEntry has a nasty habit of eating the Return key, which
868 * is unhelpful since it doesn't actually _do_ anything with it
869 * (it calls gtk_widget_activate, but our edit boxes never need
870 * activating). So I catch Return before GtkEntry sees it, and
871 * pass it straight on to the parent widget. Effect: hitting
872 * Return in an edit box will now activate the default button
873 * in the dialog just like it will everywhere else.
875 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
877 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
878 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
885 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
887 config_item
*i
= (config_item
*)data
;
890 i
->sval
= dupstr(gtk_entry_get_text(GTK_ENTRY(ed
)));
893 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
895 config_item
*i
= (config_item
*)data
;
897 i
->ival
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb
));
900 static void droplist_sel(GtkMenuItem
*item
, gpointer data
)
902 config_item
*i
= (config_item
*)data
;
904 i
->ival
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
),
908 static int get_config(frontend
*fe
, int which
)
910 GtkWidget
*w
, *table
, *cancel
;
915 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
916 fe
->cfg_which
= which
;
919 fe
->cfgbox
= gtk_dialog_new();
920 gtk_window_set_title(GTK_WINDOW(fe
->cfgbox
), title
);
923 w
= gtk_button_new_with_label("OK");
924 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
927 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
928 gtk_window_set_default(GTK_WINDOW(fe
->cfgbox
), w
);
929 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
930 GTK_SIGNAL_FUNC(config_ok_button_clicked
), fe
);
932 w
= gtk_button_new_with_label("Cancel");
933 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
936 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
937 GTK_SIGNAL_FUNC(config_cancel_button_clicked
), fe
);
940 table
= gtk_table_new(1, 2, FALSE
);
942 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->vbox
),
943 table
, FALSE
, FALSE
, 0);
944 gtk_widget_show(table
);
946 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
947 gtk_table_resize(GTK_TABLE(table
), y
+1, 2);
952 * Edit box with a label beside it.
955 w
= gtk_label_new(i
->name
);
956 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
957 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
958 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
959 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
964 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
965 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
966 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
968 gtk_entry_set_text(GTK_ENTRY(w
), i
->sval
);
969 gtk_signal_connect(GTK_OBJECT(w
), "changed",
970 GTK_SIGNAL_FUNC(editbox_changed
), i
);
971 gtk_signal_connect(GTK_OBJECT(w
), "key_press_event",
972 GTK_SIGNAL_FUNC(editbox_key
), NULL
);
981 w
= gtk_check_button_new_with_label(i
->name
);
982 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
983 GTK_SIGNAL_FUNC(button_toggled
), i
);
984 gtk_table_attach(GTK_TABLE(table
), w
, 0, 2, y
, y
+1,
985 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
986 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
988 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w
), i
->ival
);
994 * Drop-down list (GtkOptionMenu).
997 w
= gtk_label_new(i
->name
);
998 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
999 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
1000 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1001 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1005 w
= gtk_option_menu_new();
1006 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
1007 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1008 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
1015 GtkWidget
*menuitem
;
1016 GtkWidget
*menu
= gtk_menu_new();
1018 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), menu
);
1026 while (*q
&& *q
!= c
)
1029 name
= snewn(q
-p
+1, char);
1030 strncpy(name
, p
, q
-p
);
1033 if (*q
) q
++; /* eat delimiter */
1035 menuitem
= gtk_menu_item_new_with_label(name
);
1036 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1037 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1038 GINT_TO_POINTER(val
));
1039 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1040 GTK_SIGNAL_FUNC(droplist_sel
), i
);
1041 gtk_widget_show(menuitem
);
1048 gtk_option_menu_set_history(GTK_OPTION_MENU(w
), i
->ival
);
1057 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "destroy",
1058 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1059 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "key_press_event",
1060 GTK_SIGNAL_FUNC(win_key_press
), cancel
);
1061 gtk_window_set_modal(GTK_WINDOW(fe
->cfgbox
), TRUE
);
1062 gtk_window_set_transient_for(GTK_WINDOW(fe
->cfgbox
),
1063 GTK_WINDOW(fe
->window
));
1064 /* set_transient_window_pos(fe->window, fe->cfgbox); */
1065 gtk_widget_show(fe
->cfgbox
);
1073 static void menu_key_event(GtkMenuItem
*menuitem
, gpointer data
)
1075 frontend
*fe
= (frontend
*)data
;
1076 int key
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1078 if (!midend_process_key(fe
->me
, 0, 0, key
))
1079 gtk_widget_destroy(fe
->window
);
1082 static void get_size(frontend
*fe
, int *px
, int *py
)
1087 * Currently I don't want to make the GTK port scale large
1088 * puzzles to fit on the screen. This is because X does permit
1089 * extremely large windows and many window managers provide a
1090 * means of navigating round them, and the users I consulted
1091 * before deciding said that they'd rather have enormous puzzle
1092 * windows spanning multiple screen pages than have them
1093 * shrunk. I could change my mind later or introduce
1094 * configurability; this would be the place to do so, by
1095 * replacing the initial values of x and y with the screen
1100 midend_size(fe
->me
, &x
, &y
, FALSE
);
1105 #if !GTK_CHECK_VERSION(2,0,0)
1106 #define gtk_window_resize(win, x, y) \
1107 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
1110 static void update_menuitem_bullet(GtkWidget
*label
, int visible
)
1113 gtk_label_set_text(GTK_LABEL(label
), "\xE2\x80\xA2");
1115 gtk_label_set_text(GTK_LABEL(label
), "");
1120 * Called when any other code in this file has changed the
1121 * selected game parameters.
1123 static void changed_preset(frontend
*fe
)
1125 int n
= midend_which_preset(fe
->me
);
1129 * Update the tick mark in the Type menu.
1131 if (fe
->preset_bullets
) {
1132 for (i
= 0; i
< fe
->npresets
; i
++)
1133 update_menuitem_bullet(fe
->preset_bullets
[i
], n
== i
);
1135 if (fe
->preset_custom_bullet
) {
1136 update_menuitem_bullet(fe
->preset_custom_bullet
, n
< 0);
1140 * Update the greying on the Copy menu option.
1142 if (fe
->copy_menu_item
) {
1143 int enabled
= midend_can_format_as_text_now(fe
->me
);
1144 gtk_widget_set_sensitive(fe
->copy_menu_item
, enabled
);
1148 static void resize_fe(frontend
*fe
)
1152 get_size(fe
, &x
, &y
);
1155 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1158 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1159 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1162 * Now that we've established the preferred size of the window,
1163 * reduce the drawing area's size request so the user can shrink
1166 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), 1, 1);
1169 static void menu_preset_event(GtkMenuItem
*menuitem
, gpointer data
)
1171 frontend
*fe
= (frontend
*)data
;
1172 game_params
*params
=
1173 (game_params
*)gtk_object_get_data(GTK_OBJECT(menuitem
), "user-data");
1175 midend_set_params(fe
->me
, params
);
1176 midend_new_game(fe
->me
);
1181 GdkAtom compound_text_atom
, utf8_string_atom
;
1182 int paste_initialised
= FALSE
;
1186 unsigned char empty
[] = { 0 };
1188 if (paste_initialised
)
1191 if (!compound_text_atom
)
1192 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
1193 if (!utf8_string_atom
)
1194 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
1197 * Ensure that all the cut buffers exist - according to the
1198 * ICCCM, we must do this before we start using cut buffers.
1200 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1201 XA_CUT_BUFFER0
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1202 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1203 XA_CUT_BUFFER1
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1204 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1205 XA_CUT_BUFFER2
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1206 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1207 XA_CUT_BUFFER3
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1208 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1209 XA_CUT_BUFFER4
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1210 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1211 XA_CUT_BUFFER5
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1212 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1213 XA_CUT_BUFFER6
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1214 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1215 XA_CUT_BUFFER7
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1218 /* Store data in a cut-buffer. */
1219 void store_cutbuffer(char *ptr
, int len
)
1221 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1222 XRotateBuffers(GDK_DISPLAY(), 1);
1223 XStoreBytes(GDK_DISPLAY(), ptr
, len
);
1226 void write_clip(frontend
*fe
, char *data
)
1231 sfree(fe
->paste_data
);
1234 * For this simple application we can safely assume that the
1235 * data passed to this function is pure ASCII, which means we
1236 * can return precisely the same stuff for types STRING,
1237 * COMPOUND_TEXT or UTF8_STRING.
1240 fe
->paste_data
= data
;
1241 fe
->paste_data_len
= strlen(data
);
1243 store_cutbuffer(fe
->paste_data
, fe
->paste_data_len
);
1245 if (gtk_selection_owner_set(fe
->area
, GDK_SELECTION_PRIMARY
,
1247 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1248 GDK_SELECTION_TYPE_STRING
, 1);
1249 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1250 compound_text_atom
, 1);
1251 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1252 utf8_string_atom
, 1);
1256 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1257 guint info
, guint time_stamp
, gpointer data
)
1259 frontend
*fe
= (frontend
*)data
;
1260 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1261 fe
->paste_data
, fe
->paste_data_len
);
1264 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1267 frontend
*fe
= (frontend
*)data
;
1270 sfree(fe
->paste_data
);
1271 fe
->paste_data
= NULL
;
1272 fe
->paste_data_len
= 0;
1276 static void menu_copy_event(GtkMenuItem
*menuitem
, gpointer data
)
1278 frontend
*fe
= (frontend
*)data
;
1281 text
= midend_text_format(fe
->me
);
1284 write_clip(fe
, text
);
1290 static void filesel_ok(GtkButton
*button
, gpointer data
)
1292 frontend
*fe
= (frontend
*)data
;
1294 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1297 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1299 fe
->filesel_name
= dupstr(name
);
1302 static char *file_selector(frontend
*fe
, char *title
, int save
)
1304 GtkWidget
*filesel
=
1305 gtk_file_selection_new(title
);
1307 fe
->filesel_name
= NULL
;
1309 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1311 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1314 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1315 GTK_SIGNAL_FUNC(filesel_ok
), fe
);
1316 gtk_signal_connect_object
1317 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1318 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1319 gtk_signal_connect_object
1320 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1321 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1322 gtk_signal_connect(GTK_OBJECT(filesel
), "destroy",
1323 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1324 gtk_widget_show(filesel
);
1325 gtk_window_set_transient_for(GTK_WINDOW(filesel
), GTK_WINDOW(fe
->window
));
1328 return fe
->filesel_name
;
1331 static void savefile_write(void *wctx
, void *buf
, int len
)
1333 FILE *fp
= (FILE *)wctx
;
1334 fwrite(buf
, 1, len
, fp
);
1337 static int savefile_read(void *wctx
, void *buf
, int len
)
1339 FILE *fp
= (FILE *)wctx
;
1342 ret
= fread(buf
, 1, len
, fp
);
1343 return (ret
== len
);
1346 static void menu_save_event(GtkMenuItem
*menuitem
, gpointer data
)
1348 frontend
*fe
= (frontend
*)data
;
1351 name
= file_selector(fe
, "Enter name of game file to save", TRUE
);
1356 if ((fp
= fopen(name
, "r")) != NULL
) {
1357 char buf
[256 + FILENAME_MAX
];
1361 sprintf(buf
, "Are you sure you want to overwrite the"
1363 FILENAME_MAX
, name
);
1364 if (!message_box(fe
->window
, "Question", buf
, TRUE
, MB_YESNO
))
1368 fp
= fopen(name
, "w");
1372 error_box(fe
->window
, "Unable to open save file");
1376 midend_serialise(fe
->me
, savefile_write
, fp
);
1382 static void menu_load_event(GtkMenuItem
*menuitem
, gpointer data
)
1384 frontend
*fe
= (frontend
*)data
;
1387 name
= file_selector(fe
, "Enter name of saved game file to load", FALSE
);
1390 FILE *fp
= fopen(name
, "r");
1394 error_box(fe
->window
, "Unable to open saved game file");
1398 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1403 error_box(fe
->window
, err
);
1412 static void menu_solve_event(GtkMenuItem
*menuitem
, gpointer data
)
1414 frontend
*fe
= (frontend
*)data
;
1417 msg
= midend_solve(fe
->me
);
1420 error_box(fe
->window
, msg
);
1423 static void menu_restart_event(GtkMenuItem
*menuitem
, gpointer data
)
1425 frontend
*fe
= (frontend
*)data
;
1427 midend_restart_game(fe
->me
);
1430 static void menu_config_event(GtkMenuItem
*menuitem
, gpointer data
)
1432 frontend
*fe
= (frontend
*)data
;
1433 int which
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1436 if (!get_config(fe
, which
))
1439 midend_new_game(fe
->me
);
1443 static void menu_about_event(GtkMenuItem
*menuitem
, gpointer data
)
1445 frontend
*fe
= (frontend
*)data
;
1449 sprintf(titlebuf
, "About %.200s", thegame
.name
);
1452 "from Simon Tatham's Portable Puzzle Collection\n\n"
1453 "%.500s", thegame
.name
, ver
);
1455 message_box(fe
->window
, titlebuf
, textbuf
, TRUE
, MB_OK
);
1458 static GtkWidget
*add_menu_item_with_key(frontend
*fe
, GtkContainer
*cont
,
1459 char *text
, int key
)
1461 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
1463 gtk_container_add(cont
, menuitem
);
1464 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1465 GINT_TO_POINTER(key
));
1466 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1467 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
1468 switch (key
& ~0x1F) {
1471 keyqual
= GDK_CONTROL_MASK
;
1475 keyqual
= GDK_SHIFT_MASK
;
1481 gtk_widget_add_accelerator(menuitem
,
1482 "activate", fe
->accelgroup
,
1485 gtk_widget_show(menuitem
);
1489 static void add_menu_separator(GtkContainer
*cont
)
1491 GtkWidget
*menuitem
= gtk_menu_item_new();
1492 gtk_container_add(cont
, menuitem
);
1493 gtk_widget_show(menuitem
);
1496 enum { ARG_EITHER
, ARG_SAVE
, ARG_ID
}; /* for argtype */
1498 static GtkWidget
*make_preset_menuitem(GtkWidget
**bulletlabel
,
1501 GtkWidget
*hbox
, *lab1
, *lab2
, *menuitem
;
1504 hbox
= gtk_hbox_new(FALSE
, 0);
1505 gtk_widget_show(hbox
);
1506 lab1
= gtk_label_new("\xE2\x80\xA2 ");
1507 gtk_widget_show(lab1
);
1508 gtk_box_pack_start(GTK_BOX(hbox
), lab1
, FALSE
, FALSE
, 0);
1509 gtk_misc_set_alignment(GTK_MISC(lab1
), 0.0, 0.0);
1510 lab2
= gtk_label_new(name
);
1511 gtk_widget_show(lab2
);
1512 gtk_box_pack_start(GTK_BOX(hbox
), lab2
, TRUE
, TRUE
, 0);
1513 gtk_misc_set_alignment(GTK_MISC(lab2
), 0.0, 0.0);
1515 gtk_widget_size_request(lab1
, &req
);
1516 gtk_widget_set_usize(lab1
, req
.width
, -1);
1517 gtk_label_set_text(GTK_LABEL(lab1
), "");
1519 menuitem
= gtk_menu_item_new();
1520 gtk_container_add(GTK_CONTAINER(menuitem
), hbox
);
1522 *bulletlabel
= lab1
;
1526 static frontend
*new_window(char *arg
, int argtype
, char **error
)
1530 GtkWidget
*menubar
, *menu
, *menuitem
;
1535 extern char *const *const xpm_icons
[];
1536 extern const int n_xpm_icons
;
1538 fe
= snew(frontend
);
1540 fe
->timer_active
= FALSE
;
1543 fe
->me
= midend_new(fe
, &thegame
, >k_drawing
, fe
);
1553 err
= midend_game_id(fe
->me
, arg
);
1555 midend_new_game(fe
->me
);
1557 sprintf(errbuf
, "Invalid game ID: %.800s", err
);
1560 fp
= fopen(arg
, "r");
1562 sprintf(errbuf
, "Error opening file: %.800s", strerror(errno
));
1564 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1566 sprintf(errbuf
, "Invalid save file: %.800s", err
);
1570 default /*case ARG_EITHER*/:
1572 * First try treating the argument as a game ID.
1574 err
= midend_game_id(fe
->me
, arg
);
1577 * It's a valid game ID.
1579 midend_new_game(fe
->me
);
1581 FILE *fp
= fopen(arg
, "r");
1583 sprintf(errbuf
, "Supplied argument is neither a game ID (%.400s)"
1584 " nor a save file (%.400s)", err
, strerror(errno
));
1586 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1588 sprintf(errbuf
, "%.800s", err
);
1595 *error
= dupstr(errbuf
);
1596 midend_free(fe
->me
);
1602 midend_new_game(fe
->me
);
1605 fe
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1606 gtk_window_set_title(GTK_WINDOW(fe
->window
), thegame
.name
);
1608 vbox
= GTK_BOX(gtk_vbox_new(FALSE
, 0));
1609 gtk_container_add(GTK_CONTAINER(fe
->window
), GTK_WIDGET(vbox
));
1610 gtk_widget_show(GTK_WIDGET(vbox
));
1612 fe
->accelgroup
= gtk_accel_group_new();
1613 gtk_window_add_accel_group(GTK_WINDOW(fe
->window
), fe
->accelgroup
);
1615 menubar
= gtk_menu_bar_new();
1616 gtk_box_pack_start(vbox
, menubar
, FALSE
, FALSE
, 0);
1617 gtk_widget_show(menubar
);
1619 menuitem
= gtk_menu_item_new_with_label("Game");
1620 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1621 gtk_widget_show(menuitem
);
1623 menu
= gtk_menu_new();
1624 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1626 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "New", 'n');
1628 menuitem
= gtk_menu_item_new_with_label("Restart");
1629 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1630 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1631 GTK_SIGNAL_FUNC(menu_restart_event
), fe
);
1632 gtk_widget_show(menuitem
);
1634 menuitem
= gtk_menu_item_new_with_label("Specific...");
1635 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1636 GINT_TO_POINTER(CFG_DESC
));
1637 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1638 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1639 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1640 gtk_widget_show(menuitem
);
1642 menuitem
= gtk_menu_item_new_with_label("Random Seed...");
1643 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1644 GINT_TO_POINTER(CFG_SEED
));
1645 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1646 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1647 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1648 gtk_widget_show(menuitem
);
1650 if ((n
= midend_num_presets(fe
->me
)) > 0 || thegame
.can_configure
) {
1654 menuitem
= gtk_menu_item_new_with_label("Type");
1655 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1656 gtk_widget_show(menuitem
);
1658 submenu
= gtk_menu_new();
1659 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), submenu
);
1662 fe
->preset_bullets
= snewn(n
, GtkWidget
*);
1664 for (i
= 0; i
< n
; i
++) {
1666 game_params
*params
;
1668 midend_fetch_preset(fe
->me
, i
, &name
, ¶ms
);
1670 menuitem
= make_preset_menuitem(&fe
->preset_bullets
[i
], name
);
1672 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1673 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", params
);
1674 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1675 GTK_SIGNAL_FUNC(menu_preset_event
), fe
);
1676 gtk_widget_show(menuitem
);
1679 if (thegame
.can_configure
) {
1680 menuitem
= make_preset_menuitem(&fe
->preset_custom_bullet
,
1683 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1684 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1685 GPOINTER_TO_INT(CFG_SETTINGS
));
1686 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1687 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1688 gtk_widget_show(menuitem
);
1690 fe
->preset_custom_bullet
= NULL
;
1694 fe
->preset_bullets
= NULL
;
1695 fe
->preset_custom_bullet
= NULL
;
1698 add_menu_separator(GTK_CONTAINER(menu
));
1699 menuitem
= gtk_menu_item_new_with_label("Load...");
1700 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1701 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1702 GTK_SIGNAL_FUNC(menu_load_event
), fe
);
1703 gtk_widget_show(menuitem
);
1704 menuitem
= gtk_menu_item_new_with_label("Save...");
1705 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1706 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1707 GTK_SIGNAL_FUNC(menu_save_event
), fe
);
1708 gtk_widget_show(menuitem
);
1709 add_menu_separator(GTK_CONTAINER(menu
));
1710 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Undo", 'u');
1711 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Redo", 'r');
1712 if (thegame
.can_format_as_text_ever
) {
1713 add_menu_separator(GTK_CONTAINER(menu
));
1714 menuitem
= gtk_menu_item_new_with_label("Copy");
1715 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1716 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1717 GTK_SIGNAL_FUNC(menu_copy_event
), fe
);
1718 gtk_widget_show(menuitem
);
1719 fe
->copy_menu_item
= menuitem
;
1721 fe
->copy_menu_item
= NULL
;
1723 if (thegame
.can_solve
) {
1724 add_menu_separator(GTK_CONTAINER(menu
));
1725 menuitem
= gtk_menu_item_new_with_label("Solve");
1726 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1727 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1728 GTK_SIGNAL_FUNC(menu_solve_event
), fe
);
1729 gtk_widget_show(menuitem
);
1731 add_menu_separator(GTK_CONTAINER(menu
));
1732 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Exit", 'q');
1734 menuitem
= gtk_menu_item_new_with_label("Help");
1735 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1736 gtk_widget_show(menuitem
);
1738 menu
= gtk_menu_new();
1739 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1741 menuitem
= gtk_menu_item_new_with_label("About");
1742 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1743 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1744 GTK_SIGNAL_FUNC(menu_about_event
), fe
);
1745 gtk_widget_show(menuitem
);
1754 fe
->colmap
= gdk_colormap_get_system();
1755 colours
= midend_colours(fe
->me
, &ncolours
);
1756 fe
->ncolours
= ncolours
;
1757 fe
->colours
= snewn(ncolours
, GdkColor
);
1758 for (i
= 0; i
< ncolours
; i
++) {
1759 fe
->colours
[i
].red
= colours
[i
*3] * 0xFFFF;
1760 fe
->colours
[i
].green
= colours
[i
*3+1] * 0xFFFF;
1761 fe
->colours
[i
].blue
= colours
[i
*3+2] * 0xFFFF;
1763 success
= snewn(ncolours
, gboolean
);
1764 gdk_colormap_alloc_colors(fe
->colmap
, fe
->colours
, ncolours
,
1765 FALSE
, FALSE
, success
);
1766 for (i
= 0; i
< ncolours
; i
++) {
1768 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
1769 i
, fe
->colours
[i
].red
>> 8,
1770 fe
->colours
[i
].green
>> 8,
1771 fe
->colours
[i
].blue
>> 8);
1775 if (midend_wants_statusbar(fe
->me
)) {
1776 GtkWidget
*viewport
;
1779 viewport
= gtk_viewport_new(NULL
, NULL
);
1780 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport
), GTK_SHADOW_NONE
);
1781 fe
->statusbar
= gtk_statusbar_new();
1782 gtk_container_add(GTK_CONTAINER(viewport
), fe
->statusbar
);
1783 gtk_widget_show(viewport
);
1784 gtk_box_pack_end(vbox
, viewport
, FALSE
, FALSE
, 0);
1785 gtk_widget_show(fe
->statusbar
);
1786 fe
->statusctx
= gtk_statusbar_get_context_id
1787 (GTK_STATUSBAR(fe
->statusbar
), "game");
1788 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
,
1790 gtk_widget_size_request(fe
->statusbar
, &req
);
1792 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
1794 gtk_widget_set_usize(viewport
, -1, req
.height
);
1796 fe
->statusbar
= NULL
;
1798 fe
->area
= gtk_drawing_area_new();
1799 get_size(fe
, &x
, &y
);
1800 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1804 gtk_box_pack_end(vbox
, fe
->area
, TRUE
, TRUE
, 0);
1808 fe
->nfonts
= fe
->fontsize
= 0;
1810 fe
->paste_data
= NULL
;
1811 fe
->paste_data_len
= 0;
1813 gtk_signal_connect(GTK_OBJECT(fe
->window
), "destroy",
1814 GTK_SIGNAL_FUNC(destroy
), fe
);
1815 gtk_signal_connect(GTK_OBJECT(fe
->window
), "key_press_event",
1816 GTK_SIGNAL_FUNC(key_event
), fe
);
1817 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_press_event",
1818 GTK_SIGNAL_FUNC(button_event
), fe
);
1819 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_release_event",
1820 GTK_SIGNAL_FUNC(button_event
), fe
);
1821 gtk_signal_connect(GTK_OBJECT(fe
->area
), "motion_notify_event",
1822 GTK_SIGNAL_FUNC(motion_event
), fe
);
1823 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_get",
1824 GTK_SIGNAL_FUNC(selection_get
), fe
);
1825 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_clear_event",
1826 GTK_SIGNAL_FUNC(selection_clear
), fe
);
1827 gtk_signal_connect(GTK_OBJECT(fe
->area
), "expose_event",
1828 GTK_SIGNAL_FUNC(expose_area
), fe
);
1829 gtk_signal_connect(GTK_OBJECT(fe
->window
), "map_event",
1830 GTK_SIGNAL_FUNC(map_window
), fe
);
1831 gtk_signal_connect(GTK_OBJECT(fe
->area
), "configure_event",
1832 GTK_SIGNAL_FUNC(configure_area
), fe
);
1834 gtk_widget_add_events(GTK_WIDGET(fe
->area
),
1835 GDK_BUTTON_PRESS_MASK
|
1836 GDK_BUTTON_RELEASE_MASK
|
1837 GDK_BUTTON_MOTION_MASK
);
1840 gtk_widget_realize(fe
->window
);
1841 iconpm
= gdk_pixmap_create_from_xpm_d(fe
->window
->window
, NULL
,
1842 NULL
, (gchar
**)xpm_icons
[0]);
1843 gdk_window_set_icon(fe
->window
->window
, NULL
, iconpm
, NULL
);
1845 for (n
= 0; n
< n_xpm_icons
; n
++) {
1847 g_list_append(iconlist
,
1848 gdk_pixbuf_new_from_xpm_data((const gchar
**)
1851 gdk_window_set_icon_list(fe
->window
->window
, iconlist
);
1854 gtk_widget_show(fe
->area
);
1855 gtk_widget_show(fe
->window
);
1858 * Now that we've established the preferred size of the window,
1859 * reduce the drawing area's size request so the user can shrink
1862 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), 1, 1);
1864 gdk_window_set_background(fe
->area
->window
, &fe
->colours
[0]);
1865 gdk_window_set_background(fe
->window
->window
, &fe
->colours
[0]);
1870 char *fgetline(FILE *fp
)
1872 char *ret
= snewn(512, char);
1873 int size
= 512, len
= 0;
1874 while (fgets(ret
+ len
, size
- len
, fp
)) {
1875 len
+= strlen(ret
+ len
);
1876 if (ret
[len
-1] == '\n')
1877 break; /* got a newline, we're done */
1879 ret
= sresize(ret
, size
, char);
1881 if (len
== 0) { /* first fgets returned NULL */
1889 int main(int argc
, char **argv
)
1891 char *pname
= argv
[0];
1893 int ngenerate
= 0, print
= FALSE
, px
= 1, py
= 1;
1894 int soln
= FALSE
, colour
= FALSE
;
1896 float redo_proportion
= 0.0F
;
1898 int argtype
= ARG_EITHER
;
1899 char *screenshot_file
= NULL
;
1900 int doing_opts
= TRUE
;
1906 * Command line parsing in this function is rather fiddly,
1907 * because GTK wants to have a go at argc/argv _first_ - and
1908 * yet we can't let it, because gtk_init() will bomb out if it
1909 * can't open an X display, whereas in fact we want to permit
1910 * our --generate and --print modes to run without an X
1914 * - we parse the command line ourselves, without modifying
1916 * - if we encounter an error which might plausibly be the
1917 * result of a GTK command line (i.e. not detailed errors in
1918 * particular options of ours) we store the error message
1919 * and terminate parsing.
1920 * - if we got enough out of the command line to know it
1921 * specifies a non-X mode of operation, we either display
1922 * the stored error and return failure, or if there is no
1923 * stored error we do the non-X operation and return
1925 * - otherwise, we go straight to gtk_init().
1931 if (doing_opts
&& !strcmp(p
, "--version")) {
1932 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
1935 } else if (doing_opts
&& !strcmp(p
, "--generate")) {
1937 ngenerate
= atoi(*++av
);
1939 fprintf(stderr
, "%s: '--generate' expected a number\n",
1945 } else if (doing_opts
&& !strcmp(p
, "--print")) {
1946 if (!thegame
.can_print
) {
1947 fprintf(stderr
, "%s: this game does not support printing\n",
1954 if (sscanf(dim
, "%dx%d", &px
, &py
) != 2) {
1955 fprintf(stderr
, "%s: unable to parse argument '%s' to "
1956 "'--print'\n", pname
, dim
);
1962 } else if (doing_opts
&& !strcmp(p
, "--scale")) {
1964 scale
= atof(*++av
);
1966 fprintf(stderr
, "%s: no argument supplied to '--scale'\n",
1970 } else if (doing_opts
&& !strcmp(p
, "--redo")) {
1972 * This is an internal option which I don't expect
1973 * users to have any particular use for. The effect of
1974 * --redo is that once the game has been loaded and
1975 * initialised, the next move in the redo chain is
1976 * replayed, and the game screen is redrawn part way
1977 * through the making of the move. This is only
1978 * meaningful if there _is_ a next move in the redo
1979 * chain, which means in turn that this option is only
1980 * useful if you're also passing a save file on the
1983 * This option is used by the script which generates
1984 * the puzzle icons and website screenshots, and I
1985 * don't imagine it's useful for anything else.
1986 * (Unless, I suppose, users don't like my screenshots
1987 * and want to generate their own in the same way for
1988 * some repackaged version of the puzzles.)
1991 redo_proportion
= atof(*++av
);
1993 fprintf(stderr
, "%s: no argument supplied to '--redo'\n",
1997 } else if (doing_opts
&& !strcmp(p
, "--screenshot")) {
1999 * Another internal option for the icon building
2000 * script. This causes a screenshot of the central
2001 * drawing area (i.e. not including the menu bar or
2002 * status bar) to be saved to a PNG file once the
2003 * window has been drawn, and then the application
2004 * quits immediately.
2007 screenshot_file
= *++av
;
2009 fprintf(stderr
, "%s: no argument supplied to '--screenshot'\n",
2013 } else if (doing_opts
&& (!strcmp(p
, "--with-solutions") ||
2014 !strcmp(p
, "--with-solution") ||
2015 !strcmp(p
, "--with-solns") ||
2016 !strcmp(p
, "--with-soln") ||
2017 !strcmp(p
, "--solutions") ||
2018 !strcmp(p
, "--solution") ||
2019 !strcmp(p
, "--solns") ||
2020 !strcmp(p
, "--soln"))) {
2022 } else if (doing_opts
&& !strcmp(p
, "--colour")) {
2023 if (!thegame
.can_print_in_colour
) {
2024 fprintf(stderr
, "%s: this game does not support colour"
2025 " printing\n", pname
);
2029 } else if (doing_opts
&& !strcmp(p
, "--load")) {
2031 } else if (doing_opts
&& !strcmp(p
, "--game")) {
2033 } else if (doing_opts
&& !strcmp(p
, "--")) {
2035 } else if (!doing_opts
|| p
[0] != '-') {
2037 fprintf(stderr
, "%s: more than one argument supplied\n",
2043 sprintf(errbuf
, "%.100s: unrecognised option '%.100s'\n",
2050 fputs(errbuf
, stderr
);
2055 * Special standalone mode for generating puzzle IDs on the
2056 * command line. Useful for generating puzzles to be printed
2057 * out and solved offline (for puzzles where that even makes
2058 * sense - Solo, for example, is a lot more pencil-and-paper
2059 * friendly than Twiddle!)
2063 * <puzzle-name> --generate [<n> [<params>]]
2065 * <n>, if present, is the number of puzzle IDs to generate.
2066 * <params>, if present, is the same type of parameter string
2067 * you would pass to the puzzle when running it in GUI mode,
2068 * including optional extras such as the expansion factor in
2069 * Rectangles and the difficulty level in Solo.
2071 * If you specify <params>, you must also specify <n> (although
2072 * you may specify it to be 1). Sorry; that was the
2073 * simplest-to-parse command-line syntax I came up with.
2075 if (ngenerate
> 0 || print
) {
2079 document
*doc
= NULL
;
2083 me
= midend_new(NULL
, &thegame
, NULL
, NULL
);
2087 doc
= document_new(px
, py
, scale
);
2090 * In this loop, we either generate a game ID or read one
2091 * from stdin depending on whether we're in generate mode;
2092 * then we either write it to stdout or print it, depending
2093 * on whether we're in print mode. Thus, this loop handles
2094 * generate-to-stdout, print-from-stdin and generate-and-
2095 * immediately-print modes.
2097 * (It could also handle a copy-stdin-to-stdout mode,
2098 * although there's currently no combination of options
2099 * which will cause this loop to be activated in that mode.
2100 * It wouldn't be _entirely_ pointless, though, because
2101 * stdin could contain bare params strings or random-seed
2102 * IDs, and stdout would contain nothing but fully
2103 * generated descriptive game IDs.)
2105 while (ngenerate
== 0 || i
< n
) {
2108 if (ngenerate
== 0) {
2109 pstr
= fgetline(stdin
);
2112 pstr
[strcspn(pstr
, "\r\n")] = '\0';
2115 pstr
= snewn(strlen(arg
) + 40, char);
2118 if (i
> 0 && strchr(arg
, '#'))
2119 sprintf(pstr
+ strlen(pstr
), "-%d", i
);
2125 err
= midend_game_id(me
, pstr
);
2127 fprintf(stderr
, "%s: error parsing '%s': %s\n",
2134 midend_new_game(me
);
2137 err
= midend_print_puzzle(me
, doc
, soln
);
2139 fprintf(stderr
, "%s: error in printing: %s\n", pname
, err
);
2143 id
= midend_get_game_id(me
);
2152 psdata
*ps
= ps_init(stdout
, colour
);
2153 document_print(doc
, ps_drawing_api(ps
));
2164 gtk_init(&argc
, &argv
);
2166 fe
= new_window(arg
, argtype
, &error
);
2169 fprintf(stderr
, "%s: %s\n", pname
, error
);
2173 if (screenshot_file
) {
2175 * Some puzzles will not redraw their entire area if
2176 * given a partially completed animation, which means
2177 * we must redraw now and _then_ redraw again after
2178 * freezing the move timer.
2180 midend_force_redraw(fe
->me
);
2183 if (redo_proportion
) {
2185 midend_process_key(fe
->me
, 0, 0, 'r');
2186 /* And freeze the timer at the specified position. */
2187 midend_freeze_timer(fe
->me
, redo_proportion
);
2190 if (screenshot_file
) {
2192 GError
*gerror
= NULL
;
2194 midend_redraw(fe
->me
);
2196 pb
= gdk_pixbuf_get_from_drawable(NULL
, fe
->pixmap
,
2197 NULL
, 0, 0, 0, 0, -1, -1);
2198 gdk_pixbuf_save(pb
, screenshot_file
, "png", &gerror
, NULL
);