2 * gtk.c: GTK front end for my puzzle collection.
15 #include <gdk/gdkkeysyms.h>
19 #include <X11/Xutil.h>
20 #include <X11/Xatom.h>
24 #if GTK_CHECK_VERSION(2,0,0)
29 static FILE *debug_fp
= NULL
;
34 debug_fp
= fopen("debug.log", "w");
45 void debug_printf(char *fmt
, ...)
51 vsprintf(buf
, fmt
, ap
);
57 /* ----------------------------------------------------------------------
58 * Error reporting functions used elsewhere.
61 void fatal(char *fmt
, ...)
65 fprintf(stderr
, "fatal error: ");
68 vfprintf(stderr
, fmt
, ap
);
71 fprintf(stderr
, "\n");
75 /* ----------------------------------------------------------------------
76 * GTK front end to puzzles.
81 PangoFontDescription
*desc
;
90 * This structure holds all the data relevant to a single window.
91 * In principle this would allow us to open multiple independent
92 * puzzle windows, although I can't currently see any real point in
93 * doing so. I'm just coding cleanly because there's no
94 * particularly good reason not to.
108 int bbox_l
, bbox_r
, bbox_u
, bbox_d
;
109 int timer_active
, timer_id
;
110 struct timeval last_time
;
112 int nfonts
, fontsize
;
114 int cfg_which
, cfgret
;
119 int pw
, ph
; /* pixmap size (w, h are area size) */
120 int ox
, oy
; /* offset of pixmap in drawing area */
124 void get_random_seed(void **randseed
, int *randseedsize
)
126 struct timeval
*tvp
= snew(struct timeval
);
127 gettimeofday(tvp
, NULL
);
128 *randseed
= (void *)tvp
;
129 *randseedsize
= sizeof(struct timeval
);
132 void frontend_default_colour(frontend
*fe
, float *output
)
134 GdkColor col
= fe
->window
->style
->bg
[GTK_STATE_NORMAL
];
135 output
[0] = col
.red
/ 65535.0;
136 output
[1] = col
.green
/ 65535.0;
137 output
[2] = col
.blue
/ 65535.0;
140 void status_bar(frontend
*fe
, char *text
)
144 assert(fe
->statusbar
);
146 rewritten
= midend_rewrite_statusbar(fe
->me
, text
);
147 if (!fe
->laststatus
|| strcmp(rewritten
, fe
->laststatus
)) {
148 gtk_statusbar_pop(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
);
149 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
, rewritten
);
150 sfree(fe
->laststatus
);
151 fe
->laststatus
= rewritten
;
157 void start_draw(frontend
*fe
)
159 fe
->gc
= gdk_gc_new(fe
->area
->window
);
166 void clip(frontend
*fe
, int x
, int y
, int w
, int h
)
175 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
178 void unclip(frontend
*fe
)
187 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
190 void draw_text(frontend
*fe
, int x
, int y
, int fonttype
, int fontsize
,
191 int align
, int colour
, char *text
)
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 if (align
& ALIGN_HCENTRE
)
289 rect
.x
-= rect
.width
/ 2;
290 else if (align
& ALIGN_HRIGHT
)
291 rect
.x
-= rect
.width
;
293 gdk_draw_layout(fe
->pixmap
, fe
->gc
, rect
.x
+ x
, rect
.y
+ y
, layout
);
295 g_object_unref(layout
);
300 * Find string dimensions and process alignment.
303 int lb
, rb
, wid
, asc
, desc
;
306 * Measure vertical string extents with respect to the same
309 gdk_string_extents(fe
->fonts
[i
].font
,
310 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
311 &lb
, &rb
, &wid
, &asc
, &desc
);
312 if (align
& ALIGN_VCENTRE
)
313 y
+= asc
- (asc
+desc
)/2;
316 * ... but horizontal extents with respect to the provided
317 * string. This means that multiple pieces of text centred
318 * on the same y-coordinate don't have different baselines.
320 gdk_string_extents(fe
->fonts
[i
].font
, text
,
321 &lb
, &rb
, &wid
, &asc
, &desc
);
323 if (align
& ALIGN_HCENTRE
)
325 else if (align
& ALIGN_HRIGHT
)
331 * Actually draw the text.
333 gdk_draw_string(fe
->pixmap
, fe
->fonts
[i
].font
, fe
->gc
, x
, y
, text
);
338 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
)
340 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
341 gdk_draw_rectangle(fe
->pixmap
, fe
->gc
, 1, x
, y
, w
, h
);
344 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
)
346 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
347 gdk_draw_line(fe
->pixmap
, fe
->gc
, x1
, y1
, x2
, y2
);
350 void draw_polygon(frontend
*fe
, int *coords
, int npoints
,
351 int fillcolour
, int outlinecolour
)
353 GdkPoint
*points
= snewn(npoints
, GdkPoint
);
356 for (i
= 0; i
< npoints
; i
++) {
357 points
[i
].x
= coords
[i
*2];
358 points
[i
].y
= coords
[i
*2+1];
361 if (fillcolour
>= 0) {
362 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[fillcolour
]);
363 gdk_draw_polygon(fe
->pixmap
, fe
->gc
, TRUE
, points
, npoints
);
365 assert(outlinecolour
>= 0);
366 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[outlinecolour
]);
367 gdk_draw_polygon(fe
->pixmap
, fe
->gc
, FALSE
, points
, npoints
);
372 void draw_circle(frontend
*fe
, int cx
, int cy
, int radius
,
373 int fillcolour
, int outlinecolour
)
375 if (fillcolour
>= 0) {
376 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[fillcolour
]);
377 gdk_draw_arc(fe
->pixmap
, fe
->gc
, TRUE
,
378 cx
- radius
, cy
- radius
,
379 2 * radius
, 2 * radius
, 0, 360 * 64);
382 assert(outlinecolour
>= 0);
383 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[outlinecolour
]);
384 gdk_draw_arc(fe
->pixmap
, fe
->gc
, FALSE
,
385 cx
- radius
, cy
- radius
,
386 2 * radius
, 2 * radius
, 0, 360 * 64);
394 blitter
*blitter_new(int w
, int h
)
397 * We can't create the pixmap right now, because fe->window
398 * might not yet exist. So we just cache w and h and create it
399 * during the firs call to blitter_save.
401 blitter
*bl
= snew(blitter
);
408 void blitter_free(blitter
*bl
)
411 gdk_pixmap_unref(bl
->pixmap
);
415 void blitter_save(frontend
*fe
, blitter
*bl
, int x
, int y
)
418 bl
->pixmap
= gdk_pixmap_new(fe
->area
->window
, bl
->w
, bl
->h
, -1);
421 gdk_draw_pixmap(bl
->pixmap
,
422 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
424 x
, y
, 0, 0, bl
->w
, bl
->h
);
427 void blitter_load(frontend
*fe
, blitter
*bl
, int x
, int y
)
430 if (x
== BLITTER_FROMSAVED
&& y
== BLITTER_FROMSAVED
) {
434 gdk_draw_pixmap(fe
->pixmap
,
435 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
437 0, 0, x
, y
, bl
->w
, bl
->h
);
440 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
)
442 if (fe
->bbox_l
> x
) fe
->bbox_l
= x
;
443 if (fe
->bbox_r
< x
+w
) fe
->bbox_r
= x
+w
;
444 if (fe
->bbox_u
> y
) fe
->bbox_u
= y
;
445 if (fe
->bbox_d
< y
+h
) fe
->bbox_d
= y
+h
;
448 void end_draw(frontend
*fe
)
450 gdk_gc_unref(fe
->gc
);
453 if (fe
->bbox_l
< fe
->bbox_r
&& fe
->bbox_u
< fe
->bbox_d
) {
454 gdk_draw_pixmap(fe
->area
->window
,
455 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
457 fe
->bbox_l
, fe
->bbox_u
,
458 fe
->ox
+ fe
->bbox_l
, fe
->oy
+ fe
->bbox_u
,
459 fe
->bbox_r
- fe
->bbox_l
, fe
->bbox_d
- fe
->bbox_u
);
463 static void destroy(GtkWidget
*widget
, gpointer data
)
465 frontend
*fe
= (frontend
*)data
;
466 deactivate_timer(fe
);
471 static gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
473 frontend
*fe
= (frontend
*)data
;
475 int shift
= (event
->state
& GDK_SHIFT_MASK
) ? MOD_SHFT
: 0;
476 int ctrl
= (event
->state
& GDK_CONTROL_MASK
) ? MOD_CTRL
: 0;
481 if (event
->keyval
== GDK_Up
)
482 keyval
= shift
| ctrl
| CURSOR_UP
;
483 else if (event
->keyval
== GDK_KP_Up
|| event
->keyval
== GDK_KP_8
)
484 keyval
= MOD_NUM_KEYPAD
| '8';
485 else if (event
->keyval
== GDK_Down
)
486 keyval
= shift
| ctrl
| CURSOR_DOWN
;
487 else if (event
->keyval
== GDK_KP_Down
|| event
->keyval
== GDK_KP_2
)
488 keyval
= MOD_NUM_KEYPAD
| '2';
489 else if (event
->keyval
== GDK_Left
)
490 keyval
= shift
| ctrl
| CURSOR_LEFT
;
491 else if (event
->keyval
== GDK_KP_Left
|| event
->keyval
== GDK_KP_4
)
492 keyval
= MOD_NUM_KEYPAD
| '4';
493 else if (event
->keyval
== GDK_Right
)
494 keyval
= shift
| ctrl
| CURSOR_RIGHT
;
495 else if (event
->keyval
== GDK_KP_Right
|| event
->keyval
== GDK_KP_6
)
496 keyval
= MOD_NUM_KEYPAD
| '6';
497 else if (event
->keyval
== GDK_KP_Home
|| event
->keyval
== GDK_KP_7
)
498 keyval
= MOD_NUM_KEYPAD
| '7';
499 else if (event
->keyval
== GDK_KP_End
|| event
->keyval
== GDK_KP_1
)
500 keyval
= MOD_NUM_KEYPAD
| '1';
501 else if (event
->keyval
== GDK_KP_Page_Up
|| event
->keyval
== GDK_KP_9
)
502 keyval
= MOD_NUM_KEYPAD
| '9';
503 else if (event
->keyval
== GDK_KP_Page_Down
|| event
->keyval
== GDK_KP_3
)
504 keyval
= MOD_NUM_KEYPAD
| '3';
505 else if (event
->keyval
== GDK_KP_Insert
|| event
->keyval
== GDK_KP_0
)
506 keyval
= MOD_NUM_KEYPAD
| '0';
507 else if (event
->keyval
== GDK_KP_Begin
|| event
->keyval
== GDK_KP_5
)
508 keyval
= MOD_NUM_KEYPAD
| '5';
509 else if (event
->string
[0] && !event
->string
[1])
510 keyval
= (unsigned char)event
->string
[0];
515 !midend_process_key(fe
->me
, 0, 0, keyval
))
516 gtk_widget_destroy(fe
->window
);
521 static gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
,
524 frontend
*fe
= (frontend
*)data
;
530 if (event
->type
!= GDK_BUTTON_PRESS
&& event
->type
!= GDK_BUTTON_RELEASE
)
533 if (event
->button
== 2 || (event
->state
& GDK_SHIFT_MASK
))
534 button
= MIDDLE_BUTTON
;
535 else if (event
->button
== 3 || (event
->state
& GDK_MOD1_MASK
))
536 button
= RIGHT_BUTTON
;
537 else if (event
->button
== 1)
538 button
= LEFT_BUTTON
;
540 return FALSE
; /* don't even know what button! */
542 if (event
->type
== GDK_BUTTON_RELEASE
)
543 button
+= LEFT_RELEASE
- LEFT_BUTTON
;
545 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
546 event
->y
- fe
->oy
, button
))
547 gtk_widget_destroy(fe
->window
);
552 static gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
,
555 frontend
*fe
= (frontend
*)data
;
561 if (event
->state
& (GDK_BUTTON2_MASK
| GDK_SHIFT_MASK
))
562 button
= MIDDLE_DRAG
;
563 else if (event
->state
& GDK_BUTTON1_MASK
)
565 else if (event
->state
& GDK_BUTTON3_MASK
)
568 return FALSE
; /* don't even know what button! */
570 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
571 event
->y
- fe
->oy
, button
))
572 gtk_widget_destroy(fe
->window
);
577 static gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
,
580 frontend
*fe
= (frontend
*)data
;
583 gdk_draw_pixmap(widget
->window
,
584 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
586 event
->area
.x
- fe
->ox
, event
->area
.y
- fe
->oy
,
587 event
->area
.x
, event
->area
.y
,
588 event
->area
.width
, event
->area
.height
);
593 static gint
map_window(GtkWidget
*widget
, GdkEvent
*event
,
596 frontend
*fe
= (frontend
*)data
;
599 * Apparently we need to do this because otherwise the status
600 * bar will fail to update immediately. Annoying, but there we
603 gtk_widget_queue_draw(fe
->window
);
608 static gint
configure_area(GtkWidget
*widget
,
609 GdkEventConfigure
*event
, gpointer data
)
611 frontend
*fe
= (frontend
*)data
;
616 gdk_pixmap_unref(fe
->pixmap
);
618 x
= fe
->w
= event
->width
;
619 y
= fe
->h
= event
->height
;
620 midend_size(fe
->me
, &x
, &y
, TRUE
);
623 fe
->ox
= (fe
->w
- fe
->pw
) / 2;
624 fe
->oy
= (fe
->h
- fe
->ph
) / 2;
626 fe
->pixmap
= gdk_pixmap_new(widget
->window
, fe
->pw
, fe
->ph
, -1);
628 gc
= gdk_gc_new(fe
->area
->window
);
629 gdk_gc_set_foreground(gc
, &fe
->colours
[0]);
630 gdk_draw_rectangle(fe
->pixmap
, gc
, 1, 0, 0, fe
->pw
, fe
->ph
);
633 midend_force_redraw(fe
->me
);
638 static gint
timer_func(gpointer data
)
640 frontend
*fe
= (frontend
*)data
;
642 if (fe
->timer_active
) {
645 gettimeofday(&now
, NULL
);
646 elapsed
= ((now
.tv_usec
- fe
->last_time
.tv_usec
) * 0.000001F
+
647 (now
.tv_sec
- fe
->last_time
.tv_sec
));
648 midend_timer(fe
->me
, elapsed
); /* may clear timer_active */
652 return fe
->timer_active
;
655 void deactivate_timer(frontend
*fe
)
657 if (fe
->timer_active
)
658 gtk_timeout_remove(fe
->timer_id
);
659 fe
->timer_active
= FALSE
;
662 void activate_timer(frontend
*fe
)
664 if (!fe
->timer_active
) {
665 fe
->timer_id
= gtk_timeout_add(20, timer_func
, fe
);
666 gettimeofday(&fe
->last_time
, NULL
);
668 fe
->timer_active
= TRUE
;
671 static void window_destroy(GtkWidget
*widget
, gpointer data
)
676 static void msgbox_button_clicked(GtkButton
*button
, gpointer data
)
678 GtkWidget
*window
= GTK_WIDGET(data
);
681 ip
= (int *)gtk_object_get_data(GTK_OBJECT(window
), "user-data");
682 v
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button
), "user-data"));
685 gtk_widget_destroy(GTK_WIDGET(data
));
688 static int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
690 GtkObject
*cancelbutton
= GTK_OBJECT(data
);
693 * `Escape' effectively clicks the cancel button
695 if (event
->keyval
== GDK_Escape
) {
696 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton
), "clicked");
703 enum { MB_OK
, MB_YESNO
};
705 int message_box(GtkWidget
*parent
, char *title
, char *msg
, int centre
,
708 GtkWidget
*window
, *hbox
, *text
, *button
;
712 window
= gtk_dialog_new();
713 text
= gtk_label_new(msg
);
714 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
715 hbox
= gtk_hbox_new(FALSE
, 0);
716 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
717 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
718 hbox
, FALSE
, FALSE
, 20);
719 gtk_widget_show(text
);
720 gtk_widget_show(hbox
);
721 gtk_window_set_title(GTK_WINDOW(window
), title
);
722 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
728 assert(type
== MB_YESNO
);
729 titles
= "Yes\0No\0";
736 button
= gtk_button_new_with_label(titles
);
737 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
738 button
, FALSE
, FALSE
, 0);
739 gtk_widget_show(button
);
741 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
742 gtk_window_set_default(GTK_WINDOW(window
), button
);
745 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
746 GTK_SIGNAL_FUNC(win_key_press
), button
);
748 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
749 GTK_SIGNAL_FUNC(msgbox_button_clicked
), window
);
750 gtk_object_set_data(GTK_OBJECT(button
), "user-data",
752 titles
+= strlen(titles
)+1;
755 gtk_object_set_data(GTK_OBJECT(window
), "user-data",
756 GINT_TO_POINTER(&i
));
757 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
758 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
759 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
760 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(parent
));
761 /* set_transient_window_pos(parent, window); */
762 gtk_widget_show(window
);
765 return (type
== MB_YESNO ? i
== 0 : TRUE
);
768 void error_box(GtkWidget
*parent
, char *msg
)
770 message_box(parent
, "Error", msg
, FALSE
, MB_OK
);
773 static void config_ok_button_clicked(GtkButton
*button
, gpointer data
)
775 frontend
*fe
= (frontend
*)data
;
778 err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
781 error_box(fe
->cfgbox
, err
);
784 gtk_widget_destroy(fe
->cfgbox
);
788 static void config_cancel_button_clicked(GtkButton
*button
, gpointer data
)
790 frontend
*fe
= (frontend
*)data
;
792 gtk_widget_destroy(fe
->cfgbox
);
795 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
798 * GtkEntry has a nasty habit of eating the Return key, which
799 * is unhelpful since it doesn't actually _do_ anything with it
800 * (it calls gtk_widget_activate, but our edit boxes never need
801 * activating). So I catch Return before GtkEntry sees it, and
802 * pass it straight on to the parent widget. Effect: hitting
803 * Return in an edit box will now activate the default button
804 * in the dialog just like it will everywhere else.
806 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
808 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
809 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
816 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
818 config_item
*i
= (config_item
*)data
;
821 i
->sval
= dupstr(gtk_entry_get_text(GTK_ENTRY(ed
)));
824 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
826 config_item
*i
= (config_item
*)data
;
828 i
->ival
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb
));
831 static void droplist_sel(GtkMenuItem
*item
, gpointer data
)
833 config_item
*i
= (config_item
*)data
;
835 i
->ival
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
),
839 static int get_config(frontend
*fe
, int which
)
841 GtkWidget
*w
, *table
, *cancel
;
846 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
847 fe
->cfg_which
= which
;
850 fe
->cfgbox
= gtk_dialog_new();
851 gtk_window_set_title(GTK_WINDOW(fe
->cfgbox
), title
);
854 w
= gtk_button_new_with_label("OK");
855 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
858 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
859 gtk_window_set_default(GTK_WINDOW(fe
->cfgbox
), w
);
860 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
861 GTK_SIGNAL_FUNC(config_ok_button_clicked
), fe
);
863 w
= gtk_button_new_with_label("Cancel");
864 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
867 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
868 GTK_SIGNAL_FUNC(config_cancel_button_clicked
), fe
);
871 table
= gtk_table_new(1, 2, FALSE
);
873 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->vbox
),
874 table
, FALSE
, FALSE
, 0);
875 gtk_widget_show(table
);
877 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
878 gtk_table_resize(GTK_TABLE(table
), y
+1, 2);
883 * Edit box with a label beside it.
886 w
= gtk_label_new(i
->name
);
887 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
888 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
889 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
890 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
895 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
896 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
897 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
899 gtk_entry_set_text(GTK_ENTRY(w
), i
->sval
);
900 gtk_signal_connect(GTK_OBJECT(w
), "changed",
901 GTK_SIGNAL_FUNC(editbox_changed
), i
);
902 gtk_signal_connect(GTK_OBJECT(w
), "key_press_event",
903 GTK_SIGNAL_FUNC(editbox_key
), NULL
);
912 w
= gtk_check_button_new_with_label(i
->name
);
913 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
914 GTK_SIGNAL_FUNC(button_toggled
), i
);
915 gtk_table_attach(GTK_TABLE(table
), w
, 0, 2, y
, y
+1,
916 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
917 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
919 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w
), i
->ival
);
925 * Drop-down list (GtkOptionMenu).
928 w
= gtk_label_new(i
->name
);
929 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
930 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
931 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
932 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
936 w
= gtk_option_menu_new();
937 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
938 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
939 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
947 GtkWidget
*menu
= gtk_menu_new();
949 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), menu
);
957 while (*q
&& *q
!= c
)
960 name
= snewn(q
-p
+1, char);
961 strncpy(name
, p
, q
-p
);
964 if (*q
) q
++; /* eat delimiter */
966 menuitem
= gtk_menu_item_new_with_label(name
);
967 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
968 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
969 GINT_TO_POINTER(val
));
970 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
971 GTK_SIGNAL_FUNC(droplist_sel
), i
);
972 gtk_widget_show(menuitem
);
979 gtk_option_menu_set_history(GTK_OPTION_MENU(w
), i
->ival
);
988 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "destroy",
989 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
990 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "key_press_event",
991 GTK_SIGNAL_FUNC(win_key_press
), cancel
);
992 gtk_window_set_modal(GTK_WINDOW(fe
->cfgbox
), TRUE
);
993 gtk_window_set_transient_for(GTK_WINDOW(fe
->cfgbox
),
994 GTK_WINDOW(fe
->window
));
995 /* set_transient_window_pos(fe->window, fe->cfgbox); */
996 gtk_widget_show(fe
->cfgbox
);
1004 static void menu_key_event(GtkMenuItem
*menuitem
, gpointer data
)
1006 frontend
*fe
= (frontend
*)data
;
1007 int key
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1009 if (!midend_process_key(fe
->me
, 0, 0, key
))
1010 gtk_widget_destroy(fe
->window
);
1013 static void get_size(frontend
*fe
, int *px
, int *py
)
1018 * Currently I don't want to make the GTK port scale large
1019 * puzzles to fit on the screen. This is because X does permit
1020 * extremely large windows and many window managers provide a
1021 * means of navigating round them, and the users I consulted
1022 * before deciding said that they'd rather have enormous puzzle
1023 * windows spanning multiple screen pages than have them
1024 * shrunk. I could change my mind later or introduce
1025 * configurability; this would be the place to do so, by
1026 * replacing the initial values of x and y with the screen
1031 midend_size(fe
->me
, &x
, &y
, FALSE
);
1036 #if !GTK_CHECK_VERSION(2,0,0)
1037 #define gtk_window_resize(win, x, y) \
1038 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
1041 static void menu_preset_event(GtkMenuItem
*menuitem
, gpointer data
)
1043 frontend
*fe
= (frontend
*)data
;
1044 game_params
*params
=
1045 (game_params
*)gtk_object_get_data(GTK_OBJECT(menuitem
), "user-data");
1048 midend_set_params(fe
->me
, params
);
1049 midend_new_game(fe
->me
);
1050 get_size(fe
, &x
, &y
);
1053 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1056 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1057 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1061 GdkAtom compound_text_atom
, utf8_string_atom
;
1062 int paste_initialised
= FALSE
;
1066 unsigned char empty
[] = { 0 };
1068 if (paste_initialised
)
1071 if (!compound_text_atom
)
1072 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
1073 if (!utf8_string_atom
)
1074 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
1077 * Ensure that all the cut buffers exist - according to the
1078 * ICCCM, we must do this before we start using cut buffers.
1080 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1081 XA_CUT_BUFFER0
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1082 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1083 XA_CUT_BUFFER1
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1084 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1085 XA_CUT_BUFFER2
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1086 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1087 XA_CUT_BUFFER3
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1088 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1089 XA_CUT_BUFFER4
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1090 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1091 XA_CUT_BUFFER5
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1092 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1093 XA_CUT_BUFFER6
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1094 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1095 XA_CUT_BUFFER7
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1098 /* Store data in a cut-buffer. */
1099 void store_cutbuffer(char *ptr
, int len
)
1101 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1102 XRotateBuffers(GDK_DISPLAY(), 1);
1103 XStoreBytes(GDK_DISPLAY(), ptr
, len
);
1106 void write_clip(frontend
*fe
, char *data
)
1111 sfree(fe
->paste_data
);
1114 * For this simple application we can safely assume that the
1115 * data passed to this function is pure ASCII, which means we
1116 * can return precisely the same stuff for types STRING,
1117 * COMPOUND_TEXT or UTF8_STRING.
1120 fe
->paste_data
= data
;
1121 fe
->paste_data_len
= strlen(data
);
1123 store_cutbuffer(fe
->paste_data
, fe
->paste_data_len
);
1125 if (gtk_selection_owner_set(fe
->area
, GDK_SELECTION_PRIMARY
,
1127 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1128 GDK_SELECTION_TYPE_STRING
, 1);
1129 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1130 compound_text_atom
, 1);
1131 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1132 utf8_string_atom
, 1);
1136 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1137 guint info
, guint time_stamp
, gpointer data
)
1139 frontend
*fe
= (frontend
*)data
;
1140 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1141 fe
->paste_data
, fe
->paste_data_len
);
1144 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1147 frontend
*fe
= (frontend
*)data
;
1150 sfree(fe
->paste_data
);
1151 fe
->paste_data
= NULL
;
1152 fe
->paste_data_len
= 0;
1156 static void menu_copy_event(GtkMenuItem
*menuitem
, gpointer data
)
1158 frontend
*fe
= (frontend
*)data
;
1161 text
= midend_text_format(fe
->me
);
1164 write_clip(fe
, text
);
1170 static void filesel_ok(GtkButton
*button
, gpointer data
)
1172 frontend
*fe
= (frontend
*)data
;
1174 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1177 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1179 fe
->filesel_name
= dupstr(name
);
1182 static char *file_selector(frontend
*fe
, char *title
, int save
)
1184 GtkWidget
*filesel
=
1185 gtk_file_selection_new(title
);
1187 fe
->filesel_name
= NULL
;
1189 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1191 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1194 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1195 GTK_SIGNAL_FUNC(filesel_ok
), fe
);
1196 gtk_signal_connect_object
1197 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1198 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1199 gtk_signal_connect_object
1200 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1201 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1202 gtk_signal_connect(GTK_OBJECT(filesel
), "destroy",
1203 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1204 gtk_widget_show(filesel
);
1205 gtk_window_set_transient_for(GTK_WINDOW(filesel
), GTK_WINDOW(fe
->window
));
1208 return fe
->filesel_name
;
1211 static void savefile_write(void *wctx
, void *buf
, int len
)
1213 FILE *fp
= (FILE *)wctx
;
1214 fwrite(buf
, 1, len
, fp
);
1217 static int savefile_read(void *wctx
, void *buf
, int len
)
1219 FILE *fp
= (FILE *)wctx
;
1222 ret
= fread(buf
, 1, len
, fp
);
1223 return (ret
== len
);
1226 static void menu_save_event(GtkMenuItem
*menuitem
, gpointer data
)
1228 frontend
*fe
= (frontend
*)data
;
1231 name
= file_selector(fe
, "Enter name of game file to save", TRUE
);
1236 if ((fp
= fopen(name
, "r")) != NULL
) {
1237 char buf
[256 + FILENAME_MAX
];
1241 sprintf(buf
, "Are you sure you want to overwrite the"
1243 FILENAME_MAX
, name
);
1244 if (!message_box(fe
->window
, "Question", buf
, TRUE
, MB_YESNO
))
1248 fp
= fopen(name
, "w");
1252 error_box(fe
->window
, "Unable to open save file");
1256 midend_serialise(fe
->me
, savefile_write
, fp
);
1262 static void menu_load_event(GtkMenuItem
*menuitem
, gpointer data
)
1264 frontend
*fe
= (frontend
*)data
;
1268 name
= file_selector(fe
, "Enter name of saved game file to load", FALSE
);
1271 FILE *fp
= fopen(name
, "r");
1275 error_box(fe
->window
, "Unable to open saved game file");
1279 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1284 error_box(fe
->window
, err
);
1288 get_size(fe
, &x
, &y
);
1291 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1294 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1295 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1301 static void menu_solve_event(GtkMenuItem
*menuitem
, gpointer data
)
1303 frontend
*fe
= (frontend
*)data
;
1306 msg
= midend_solve(fe
->me
);
1309 error_box(fe
->window
, msg
);
1312 static void menu_restart_event(GtkMenuItem
*menuitem
, gpointer data
)
1314 frontend
*fe
= (frontend
*)data
;
1316 midend_restart_game(fe
->me
);
1319 static void menu_config_event(GtkMenuItem
*menuitem
, gpointer data
)
1321 frontend
*fe
= (frontend
*)data
;
1322 int which
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1326 if (!get_config(fe
, which
))
1329 midend_new_game(fe
->me
);
1330 get_size(fe
, &x
, &y
);
1333 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1336 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1337 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1341 static void menu_about_event(GtkMenuItem
*menuitem
, gpointer data
)
1343 frontend
*fe
= (frontend
*)data
;
1347 sprintf(titlebuf
, "About %.200s", thegame
.name
);
1350 "from Simon Tatham's Portable Puzzle Collection\n\n"
1351 "%.500s", thegame
.name
, ver
);
1353 message_box(fe
->window
, titlebuf
, textbuf
, TRUE
, MB_OK
);
1356 static GtkWidget
*add_menu_item_with_key(frontend
*fe
, GtkContainer
*cont
,
1357 char *text
, int key
)
1359 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
1360 gtk_container_add(cont
, menuitem
);
1361 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1362 GINT_TO_POINTER(key
));
1363 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1364 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
1365 gtk_widget_show(menuitem
);
1369 static void add_menu_separator(GtkContainer
*cont
)
1371 GtkWidget
*menuitem
= gtk_menu_item_new();
1372 gtk_container_add(cont
, menuitem
);
1373 gtk_widget_show(menuitem
);
1376 static frontend
*new_window(char *game_id
, char **error
)
1380 GtkWidget
*menubar
, *menu
, *menuitem
;
1383 fe
= snew(frontend
);
1385 fe
->timer_active
= FALSE
;
1388 fe
->me
= midend_new(fe
, &thegame
);
1391 *error
= midend_game_id(fe
->me
, game_id
);
1393 midend_free(fe
->me
);
1398 midend_new_game(fe
->me
);
1400 fe
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1401 gtk_window_set_title(GTK_WINDOW(fe
->window
), thegame
.name
);
1403 gtk_window_set_resizable(GTK_WINDOW(fe
->window
), FALSE
);
1405 gtk_window_set_policy(GTK_WINDOW(fe
->window
), FALSE
, FALSE
, TRUE
);
1407 vbox
= GTK_BOX(gtk_vbox_new(FALSE
, 0));
1408 gtk_container_add(GTK_CONTAINER(fe
->window
), GTK_WIDGET(vbox
));
1409 gtk_widget_show(GTK_WIDGET(vbox
));
1411 menubar
= gtk_menu_bar_new();
1412 gtk_box_pack_start(vbox
, menubar
, FALSE
, FALSE
, 0);
1413 gtk_widget_show(menubar
);
1415 menuitem
= gtk_menu_item_new_with_label("Game");
1416 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1417 gtk_widget_show(menuitem
);
1419 menu
= gtk_menu_new();
1420 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1422 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "New", 'n');
1424 menuitem
= gtk_menu_item_new_with_label("Restart");
1425 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1426 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1427 GTK_SIGNAL_FUNC(menu_restart_event
), fe
);
1428 gtk_widget_show(menuitem
);
1430 menuitem
= gtk_menu_item_new_with_label("Specific...");
1431 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1432 GINT_TO_POINTER(CFG_DESC
));
1433 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1434 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1435 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1436 gtk_widget_show(menuitem
);
1438 menuitem
= gtk_menu_item_new_with_label("Random Seed...");
1439 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1440 GINT_TO_POINTER(CFG_SEED
));
1441 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1442 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1443 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1444 gtk_widget_show(menuitem
);
1446 if ((n
= midend_num_presets(fe
->me
)) > 0 || thegame
.can_configure
) {
1450 menuitem
= gtk_menu_item_new_with_label("Type");
1451 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1452 gtk_widget_show(menuitem
);
1454 submenu
= gtk_menu_new();
1455 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), submenu
);
1457 for (i
= 0; i
< n
; i
++) {
1459 game_params
*params
;
1461 midend_fetch_preset(fe
->me
, i
, &name
, ¶ms
);
1463 menuitem
= gtk_menu_item_new_with_label(name
);
1464 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1465 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", params
);
1466 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1467 GTK_SIGNAL_FUNC(menu_preset_event
), fe
);
1468 gtk_widget_show(menuitem
);
1471 if (thegame
.can_configure
) {
1472 menuitem
= gtk_menu_item_new_with_label("Custom...");
1473 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1474 GPOINTER_TO_INT(CFG_SETTINGS
));
1475 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1476 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1477 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1478 gtk_widget_show(menuitem
);
1482 add_menu_separator(GTK_CONTAINER(menu
));
1483 menuitem
= gtk_menu_item_new_with_label("Load");
1484 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1485 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1486 GTK_SIGNAL_FUNC(menu_load_event
), fe
);
1487 gtk_widget_show(menuitem
);
1488 menuitem
= gtk_menu_item_new_with_label("Save");
1489 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1490 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1491 GTK_SIGNAL_FUNC(menu_save_event
), fe
);
1492 gtk_widget_show(menuitem
);
1493 add_menu_separator(GTK_CONTAINER(menu
));
1494 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Undo", 'u');
1495 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Redo", '\x12');
1496 if (thegame
.can_format_as_text
) {
1497 add_menu_separator(GTK_CONTAINER(menu
));
1498 menuitem
= gtk_menu_item_new_with_label("Copy");
1499 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1500 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1501 GTK_SIGNAL_FUNC(menu_copy_event
), fe
);
1502 gtk_widget_show(menuitem
);
1504 if (thegame
.can_solve
) {
1505 add_menu_separator(GTK_CONTAINER(menu
));
1506 menuitem
= gtk_menu_item_new_with_label("Solve");
1507 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1508 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1509 GTK_SIGNAL_FUNC(menu_solve_event
), fe
);
1510 gtk_widget_show(menuitem
);
1512 add_menu_separator(GTK_CONTAINER(menu
));
1513 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Exit", 'q');
1515 menuitem
= gtk_menu_item_new_with_label("Help");
1516 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1517 gtk_widget_show(menuitem
);
1519 menu
= gtk_menu_new();
1520 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1522 menuitem
= gtk_menu_item_new_with_label("About");
1523 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1524 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1525 GTK_SIGNAL_FUNC(menu_about_event
), fe
);
1526 gtk_widget_show(menuitem
);
1533 fe
->colmap
= gdk_colormap_get_system();
1534 colours
= midend_colours(fe
->me
, &ncolours
);
1535 fe
->ncolours
= ncolours
;
1536 fe
->colours
= snewn(ncolours
, GdkColor
);
1537 for (i
= 0; i
< ncolours
; i
++) {
1538 fe
->colours
[i
].red
= colours
[i
*3] * 0xFFFF;
1539 fe
->colours
[i
].green
= colours
[i
*3+1] * 0xFFFF;
1540 fe
->colours
[i
].blue
= colours
[i
*3+2] * 0xFFFF;
1542 success
= snewn(ncolours
, gboolean
);
1543 gdk_colormap_alloc_colors(fe
->colmap
, fe
->colours
, ncolours
,
1544 FALSE
, FALSE
, success
);
1545 for (i
= 0; i
< ncolours
; i
++) {
1547 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
1548 i
, fe
->colours
[i
].red
>> 8,
1549 fe
->colours
[i
].green
>> 8,
1550 fe
->colours
[i
].blue
>> 8);
1554 if (midend_wants_statusbar(fe
->me
)) {
1555 GtkWidget
*viewport
;
1558 viewport
= gtk_viewport_new(NULL
, NULL
);
1559 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport
), GTK_SHADOW_NONE
);
1560 fe
->statusbar
= gtk_statusbar_new();
1561 gtk_container_add(GTK_CONTAINER(viewport
), fe
->statusbar
);
1562 gtk_widget_show(viewport
);
1563 gtk_box_pack_end(vbox
, viewport
, FALSE
, FALSE
, 0);
1564 gtk_widget_show(fe
->statusbar
);
1565 fe
->statusctx
= gtk_statusbar_get_context_id
1566 (GTK_STATUSBAR(fe
->statusbar
), "game");
1567 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
,
1569 gtk_widget_size_request(fe
->statusbar
, &req
);
1571 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
1573 gtk_widget_set_usize(viewport
, -1, req
.height
);
1575 fe
->statusbar
= NULL
;
1577 fe
->area
= gtk_drawing_area_new();
1578 get_size(fe
, &x
, &y
);
1579 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1583 gtk_box_pack_end(vbox
, fe
->area
, TRUE
, TRUE
, 0);
1587 fe
->nfonts
= fe
->fontsize
= 0;
1589 fe
->laststatus
= NULL
;
1591 fe
->paste_data
= NULL
;
1592 fe
->paste_data_len
= 0;
1594 gtk_signal_connect(GTK_OBJECT(fe
->window
), "destroy",
1595 GTK_SIGNAL_FUNC(destroy
), fe
);
1596 gtk_signal_connect(GTK_OBJECT(fe
->window
), "key_press_event",
1597 GTK_SIGNAL_FUNC(key_event
), fe
);
1598 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_press_event",
1599 GTK_SIGNAL_FUNC(button_event
), fe
);
1600 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_release_event",
1601 GTK_SIGNAL_FUNC(button_event
), fe
);
1602 gtk_signal_connect(GTK_OBJECT(fe
->area
), "motion_notify_event",
1603 GTK_SIGNAL_FUNC(motion_event
), fe
);
1604 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_get",
1605 GTK_SIGNAL_FUNC(selection_get
), fe
);
1606 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_clear_event",
1607 GTK_SIGNAL_FUNC(selection_clear
), fe
);
1608 gtk_signal_connect(GTK_OBJECT(fe
->area
), "expose_event",
1609 GTK_SIGNAL_FUNC(expose_area
), fe
);
1610 gtk_signal_connect(GTK_OBJECT(fe
->window
), "map_event",
1611 GTK_SIGNAL_FUNC(map_window
), fe
);
1612 gtk_signal_connect(GTK_OBJECT(fe
->area
), "configure_event",
1613 GTK_SIGNAL_FUNC(configure_area
), fe
);
1615 gtk_widget_add_events(GTK_WIDGET(fe
->area
),
1616 GDK_BUTTON_PRESS_MASK
|
1617 GDK_BUTTON_RELEASE_MASK
|
1618 GDK_BUTTON_MOTION_MASK
);
1620 gtk_widget_show(fe
->area
);
1621 gtk_widget_show(fe
->window
);
1623 gdk_window_set_background(fe
->area
->window
, &fe
->colours
[0]);
1624 gdk_window_set_background(fe
->window
->window
, &fe
->colours
[0]);
1629 int main(int argc
, char **argv
)
1631 char *pname
= argv
[0];
1634 if (argc
> 1 && !strcmp(argv
[1], "--version")) {
1635 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
1641 * Special standalone mode for generating puzzle IDs on the
1642 * command line. Useful for generating puzzles to be printed
1643 * out and solved offline (for puzzles where that even makes
1644 * sense - Solo, for example, is a lot more pencil-and-paper
1645 * friendly than Net!)
1649 * <puzzle-name> --generate [<n> [<params>]]
1651 * <n>, if present, is the number of puzzle IDs to generate.
1652 * <params>, if present, is the same type of parameter string
1653 * you would pass to the puzzle when running it in GUI mode,
1654 * including optional extras such as the expansion factor in
1655 * Rectangles and the difficulty level in Solo.
1657 * If you specify <params>, you must also specify <n> (although
1658 * you may specify it to be 1). Sorry; that was the
1659 * simplest-to-parse command-line syntax I came up with.
1661 if (argc
> 1 && !strcmp(argv
[1], "--generate")) {
1663 char *params
= NULL
, *seed
= NULL
;
1673 par
= thegame
.default_params();
1675 if ( (seed
= strchr(params
, '#')) != NULL
)
1677 thegame
.decode_params(par
, params
);
1679 if ((error
= thegame
.validate_params(par
)) != NULL
) {
1680 fprintf(stderr
, "%s: %s\n", pname
, error
);
1683 parstr
= thegame
.encode_params(par
, FALSE
);
1690 seedlen
= strlen(seed
);
1692 get_random_seed(&seeddata
, &seedlen
);
1694 rs
= random_init(seeddata
, seedlen
);
1699 char *desc
= thegame
.new_desc(par
, rs
, &aux
, FALSE
);
1700 printf("%s:%s\n", parstr
, desc
);
1708 gtk_init(&argc
, &argv
);
1710 if (!new_window(argc
> 1 ? argv
[1] : NULL
, &error
)) {
1711 fprintf(stderr
, "%s: %s\n", pname
, error
);