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 fill
, int colour
)
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 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
362 gdk_draw_polygon(fe
->pixmap
, fe
->gc
, fill
, points
, npoints
);
367 void draw_circle(frontend
*fe
, int cx
, int cy
, int radius
,
368 int fill
, int colour
)
370 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
371 gdk_draw_arc(fe
->pixmap
, fe
->gc
, fill
,
372 cx
- radius
, cy
- radius
,
373 2 * radius
, 2 * radius
, 0, 360 * 64);
381 blitter
*blitter_new(int w
, int h
)
384 * We can't create the pixmap right now, because fe->window
385 * might not yet exist. So we just cache w and h and create it
386 * during the firs call to blitter_save.
388 blitter
*bl
= snew(blitter
);
395 void blitter_free(blitter
*bl
)
398 gdk_pixmap_unref(bl
->pixmap
);
402 void blitter_save(frontend
*fe
, blitter
*bl
, int x
, int y
)
405 bl
->pixmap
= gdk_pixmap_new(fe
->area
->window
, bl
->w
, bl
->h
, -1);
408 gdk_draw_pixmap(bl
->pixmap
,
409 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
411 x
, y
, 0, 0, bl
->w
, bl
->h
);
414 void blitter_load(frontend
*fe
, blitter
*bl
, int x
, int y
)
417 if (x
== BLITTER_FROMSAVED
&& y
== BLITTER_FROMSAVED
) {
421 gdk_draw_pixmap(fe
->pixmap
,
422 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
424 0, 0, x
, y
, bl
->w
, bl
->h
);
427 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
)
429 if (fe
->bbox_l
> x
) fe
->bbox_l
= x
;
430 if (fe
->bbox_r
< x
+w
) fe
->bbox_r
= x
+w
;
431 if (fe
->bbox_u
> y
) fe
->bbox_u
= y
;
432 if (fe
->bbox_d
< y
+h
) fe
->bbox_d
= y
+h
;
435 void end_draw(frontend
*fe
)
437 gdk_gc_unref(fe
->gc
);
440 if (fe
->bbox_l
< fe
->bbox_r
&& fe
->bbox_u
< fe
->bbox_d
) {
441 gdk_draw_pixmap(fe
->area
->window
,
442 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
444 fe
->bbox_l
, fe
->bbox_u
,
445 fe
->ox
+ fe
->bbox_l
, fe
->oy
+ fe
->bbox_u
,
446 fe
->bbox_r
- fe
->bbox_l
, fe
->bbox_d
- fe
->bbox_u
);
450 static void destroy(GtkWidget
*widget
, gpointer data
)
452 frontend
*fe
= (frontend
*)data
;
453 deactivate_timer(fe
);
458 static gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
460 frontend
*fe
= (frontend
*)data
;
462 int shift
= (event
->state
& GDK_SHIFT_MASK
) ? MOD_SHFT
: 0;
463 int ctrl
= (event
->state
& GDK_CONTROL_MASK
) ? MOD_CTRL
: 0;
468 if (event
->keyval
== GDK_Up
)
469 keyval
= shift
| ctrl
| CURSOR_UP
;
470 else if (event
->keyval
== GDK_KP_Up
|| event
->keyval
== GDK_KP_8
)
471 keyval
= MOD_NUM_KEYPAD
| '8';
472 else if (event
->keyval
== GDK_Down
)
473 keyval
= shift
| ctrl
| CURSOR_DOWN
;
474 else if (event
->keyval
== GDK_KP_Down
|| event
->keyval
== GDK_KP_2
)
475 keyval
= MOD_NUM_KEYPAD
| '2';
476 else if (event
->keyval
== GDK_Left
)
477 keyval
= shift
| ctrl
| CURSOR_LEFT
;
478 else if (event
->keyval
== GDK_KP_Left
|| event
->keyval
== GDK_KP_4
)
479 keyval
= MOD_NUM_KEYPAD
| '4';
480 else if (event
->keyval
== GDK_Right
)
481 keyval
= shift
| ctrl
| CURSOR_RIGHT
;
482 else if (event
->keyval
== GDK_KP_Right
|| event
->keyval
== GDK_KP_6
)
483 keyval
= MOD_NUM_KEYPAD
| '6';
484 else if (event
->keyval
== GDK_KP_Home
|| event
->keyval
== GDK_KP_7
)
485 keyval
= MOD_NUM_KEYPAD
| '7';
486 else if (event
->keyval
== GDK_KP_End
|| event
->keyval
== GDK_KP_1
)
487 keyval
= MOD_NUM_KEYPAD
| '1';
488 else if (event
->keyval
== GDK_KP_Page_Up
|| event
->keyval
== GDK_KP_9
)
489 keyval
= MOD_NUM_KEYPAD
| '9';
490 else if (event
->keyval
== GDK_KP_Page_Down
|| event
->keyval
== GDK_KP_3
)
491 keyval
= MOD_NUM_KEYPAD
| '3';
492 else if (event
->keyval
== GDK_KP_Insert
|| event
->keyval
== GDK_KP_0
)
493 keyval
= MOD_NUM_KEYPAD
| '0';
494 else if (event
->keyval
== GDK_KP_Begin
|| event
->keyval
== GDK_KP_5
)
495 keyval
= MOD_NUM_KEYPAD
| '5';
496 else if (event
->string
[0] && !event
->string
[1])
497 keyval
= (unsigned char)event
->string
[0];
502 !midend_process_key(fe
->me
, 0, 0, keyval
))
503 gtk_widget_destroy(fe
->window
);
508 static gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
,
511 frontend
*fe
= (frontend
*)data
;
517 if (event
->type
!= GDK_BUTTON_PRESS
&& event
->type
!= GDK_BUTTON_RELEASE
)
520 if (event
->button
== 2 || (event
->state
& GDK_SHIFT_MASK
))
521 button
= MIDDLE_BUTTON
;
522 else if (event
->button
== 3 || (event
->state
& GDK_MOD1_MASK
))
523 button
= RIGHT_BUTTON
;
524 else if (event
->button
== 1)
525 button
= LEFT_BUTTON
;
527 return FALSE
; /* don't even know what button! */
529 if (event
->type
== GDK_BUTTON_RELEASE
)
530 button
+= LEFT_RELEASE
- LEFT_BUTTON
;
532 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
533 event
->y
- fe
->oy
, button
))
534 gtk_widget_destroy(fe
->window
);
539 static gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
,
542 frontend
*fe
= (frontend
*)data
;
548 if (event
->state
& (GDK_BUTTON2_MASK
| GDK_SHIFT_MASK
))
549 button
= MIDDLE_DRAG
;
550 else if (event
->state
& GDK_BUTTON1_MASK
)
552 else if (event
->state
& GDK_BUTTON3_MASK
)
555 return FALSE
; /* don't even know what button! */
557 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
558 event
->y
- fe
->oy
, button
))
559 gtk_widget_destroy(fe
->window
);
564 static gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
,
567 frontend
*fe
= (frontend
*)data
;
570 gdk_draw_pixmap(widget
->window
,
571 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
573 event
->area
.x
- fe
->ox
, event
->area
.y
- fe
->oy
,
574 event
->area
.x
, event
->area
.y
,
575 event
->area
.width
, event
->area
.height
);
580 static gint
map_window(GtkWidget
*widget
, GdkEvent
*event
,
583 frontend
*fe
= (frontend
*)data
;
586 * Apparently we need to do this because otherwise the status
587 * bar will fail to update immediately. Annoying, but there we
590 gtk_widget_queue_draw(fe
->window
);
595 static gint
configure_area(GtkWidget
*widget
,
596 GdkEventConfigure
*event
, gpointer data
)
598 frontend
*fe
= (frontend
*)data
;
603 gdk_pixmap_unref(fe
->pixmap
);
605 x
= fe
->w
= event
->width
;
606 y
= fe
->h
= event
->height
;
607 midend_size(fe
->me
, &x
, &y
, TRUE
);
610 fe
->ox
= (fe
->w
- fe
->pw
) / 2;
611 fe
->oy
= (fe
->h
- fe
->ph
) / 2;
613 fe
->pixmap
= gdk_pixmap_new(widget
->window
, fe
->pw
, fe
->ph
, -1);
615 gc
= gdk_gc_new(fe
->area
->window
);
616 gdk_gc_set_foreground(gc
, &fe
->colours
[0]);
617 gdk_draw_rectangle(fe
->pixmap
, gc
, 1, 0, 0, fe
->pw
, fe
->ph
);
620 midend_force_redraw(fe
->me
);
625 static gint
timer_func(gpointer data
)
627 frontend
*fe
= (frontend
*)data
;
629 if (fe
->timer_active
) {
632 gettimeofday(&now
, NULL
);
633 elapsed
= ((now
.tv_usec
- fe
->last_time
.tv_usec
) * 0.000001F
+
634 (now
.tv_sec
- fe
->last_time
.tv_sec
));
635 midend_timer(fe
->me
, elapsed
); /* may clear timer_active */
639 return fe
->timer_active
;
642 void deactivate_timer(frontend
*fe
)
644 if (fe
->timer_active
)
645 gtk_timeout_remove(fe
->timer_id
);
646 fe
->timer_active
= FALSE
;
649 void activate_timer(frontend
*fe
)
651 if (!fe
->timer_active
) {
652 fe
->timer_id
= gtk_timeout_add(20, timer_func
, fe
);
653 gettimeofday(&fe
->last_time
, NULL
);
655 fe
->timer_active
= TRUE
;
658 static void window_destroy(GtkWidget
*widget
, gpointer data
)
663 static void msgbox_button_clicked(GtkButton
*button
, gpointer data
)
665 GtkWidget
*window
= GTK_WIDGET(data
);
668 ip
= (int *)gtk_object_get_data(GTK_OBJECT(window
), "user-data");
669 v
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button
), "user-data"));
672 gtk_widget_destroy(GTK_WIDGET(data
));
675 static int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
677 GtkObject
*cancelbutton
= GTK_OBJECT(data
);
680 * `Escape' effectively clicks the cancel button
682 if (event
->keyval
== GDK_Escape
) {
683 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton
), "clicked");
690 enum { MB_OK
, MB_YESNO
};
692 int message_box(GtkWidget
*parent
, char *title
, char *msg
, int centre
,
695 GtkWidget
*window
, *hbox
, *text
, *button
;
699 window
= gtk_dialog_new();
700 text
= gtk_label_new(msg
);
701 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
702 hbox
= gtk_hbox_new(FALSE
, 0);
703 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
704 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
705 hbox
, FALSE
, FALSE
, 20);
706 gtk_widget_show(text
);
707 gtk_widget_show(hbox
);
708 gtk_window_set_title(GTK_WINDOW(window
), title
);
709 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
715 assert(type
== MB_YESNO
);
716 titles
= "Yes\0No\0";
723 button
= gtk_button_new_with_label(titles
);
724 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
725 button
, FALSE
, FALSE
, 0);
726 gtk_widget_show(button
);
728 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
729 gtk_window_set_default(GTK_WINDOW(window
), button
);
732 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
733 GTK_SIGNAL_FUNC(win_key_press
), button
);
735 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
736 GTK_SIGNAL_FUNC(msgbox_button_clicked
), window
);
737 gtk_object_set_data(GTK_OBJECT(button
), "user-data",
739 titles
+= strlen(titles
)+1;
742 gtk_object_set_data(GTK_OBJECT(window
), "user-data",
743 GINT_TO_POINTER(&i
));
744 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
745 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
746 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
747 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(parent
));
748 /* set_transient_window_pos(parent, window); */
749 gtk_widget_show(window
);
752 return (type
== MB_YESNO ? i
== 0 : TRUE
);
755 void error_box(GtkWidget
*parent
, char *msg
)
757 message_box(parent
, "Error", msg
, FALSE
, MB_OK
);
760 static void config_ok_button_clicked(GtkButton
*button
, gpointer data
)
762 frontend
*fe
= (frontend
*)data
;
765 err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
768 error_box(fe
->cfgbox
, err
);
771 gtk_widget_destroy(fe
->cfgbox
);
775 static void config_cancel_button_clicked(GtkButton
*button
, gpointer data
)
777 frontend
*fe
= (frontend
*)data
;
779 gtk_widget_destroy(fe
->cfgbox
);
782 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
785 * GtkEntry has a nasty habit of eating the Return key, which
786 * is unhelpful since it doesn't actually _do_ anything with it
787 * (it calls gtk_widget_activate, but our edit boxes never need
788 * activating). So I catch Return before GtkEntry sees it, and
789 * pass it straight on to the parent widget. Effect: hitting
790 * Return in an edit box will now activate the default button
791 * in the dialog just like it will everywhere else.
793 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
795 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
796 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
803 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
805 config_item
*i
= (config_item
*)data
;
808 i
->sval
= dupstr(gtk_entry_get_text(GTK_ENTRY(ed
)));
811 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
813 config_item
*i
= (config_item
*)data
;
815 i
->ival
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb
));
818 static void droplist_sel(GtkMenuItem
*item
, gpointer data
)
820 config_item
*i
= (config_item
*)data
;
822 i
->ival
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
),
826 static int get_config(frontend
*fe
, int which
)
828 GtkWidget
*w
, *table
, *cancel
;
833 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
834 fe
->cfg_which
= which
;
837 fe
->cfgbox
= gtk_dialog_new();
838 gtk_window_set_title(GTK_WINDOW(fe
->cfgbox
), title
);
841 w
= gtk_button_new_with_label("OK");
842 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
845 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
846 gtk_window_set_default(GTK_WINDOW(fe
->cfgbox
), w
);
847 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
848 GTK_SIGNAL_FUNC(config_ok_button_clicked
), fe
);
850 w
= gtk_button_new_with_label("Cancel");
851 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
854 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
855 GTK_SIGNAL_FUNC(config_cancel_button_clicked
), fe
);
858 table
= gtk_table_new(1, 2, FALSE
);
860 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->vbox
),
861 table
, FALSE
, FALSE
, 0);
862 gtk_widget_show(table
);
864 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
865 gtk_table_resize(GTK_TABLE(table
), y
+1, 2);
870 * Edit box with a label beside it.
873 w
= gtk_label_new(i
->name
);
874 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
875 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
876 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
877 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
882 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
883 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
884 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
886 gtk_entry_set_text(GTK_ENTRY(w
), i
->sval
);
887 gtk_signal_connect(GTK_OBJECT(w
), "changed",
888 GTK_SIGNAL_FUNC(editbox_changed
), i
);
889 gtk_signal_connect(GTK_OBJECT(w
), "key_press_event",
890 GTK_SIGNAL_FUNC(editbox_key
), NULL
);
899 w
= gtk_check_button_new_with_label(i
->name
);
900 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
901 GTK_SIGNAL_FUNC(button_toggled
), i
);
902 gtk_table_attach(GTK_TABLE(table
), w
, 0, 2, y
, y
+1,
903 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
904 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
906 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w
), i
->ival
);
912 * Drop-down list (GtkOptionMenu).
915 w
= gtk_label_new(i
->name
);
916 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
917 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
918 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
919 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
923 w
= gtk_option_menu_new();
924 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
925 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
926 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
934 GtkWidget
*menu
= gtk_menu_new();
936 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), menu
);
944 while (*q
&& *q
!= c
)
947 name
= snewn(q
-p
+1, char);
948 strncpy(name
, p
, q
-p
);
951 if (*q
) q
++; /* eat delimiter */
953 menuitem
= gtk_menu_item_new_with_label(name
);
954 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
955 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
956 GINT_TO_POINTER(val
));
957 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
958 GTK_SIGNAL_FUNC(droplist_sel
), i
);
959 gtk_widget_show(menuitem
);
966 gtk_option_menu_set_history(GTK_OPTION_MENU(w
), i
->ival
);
975 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "destroy",
976 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
977 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "key_press_event",
978 GTK_SIGNAL_FUNC(win_key_press
), cancel
);
979 gtk_window_set_modal(GTK_WINDOW(fe
->cfgbox
), TRUE
);
980 gtk_window_set_transient_for(GTK_WINDOW(fe
->cfgbox
),
981 GTK_WINDOW(fe
->window
));
982 /* set_transient_window_pos(fe->window, fe->cfgbox); */
983 gtk_widget_show(fe
->cfgbox
);
991 static void menu_key_event(GtkMenuItem
*menuitem
, gpointer data
)
993 frontend
*fe
= (frontend
*)data
;
994 int key
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
996 if (!midend_process_key(fe
->me
, 0, 0, key
))
997 gtk_widget_destroy(fe
->window
);
1000 static void get_size(frontend
*fe
, int *px
, int *py
)
1005 * Currently I don't want to make the GTK port scale large
1006 * puzzles to fit on the screen. This is because X does permit
1007 * extremely large windows and many window managers provide a
1008 * means of navigating round them, and the users I consulted
1009 * before deciding said that they'd rather have enormous puzzle
1010 * windows spanning multiple screen pages than have them
1011 * shrunk. I could change my mind later or introduce
1012 * configurability; this would be the place to do so, by
1013 * replacing the initial values of x and y with the screen
1018 midend_size(fe
->me
, &x
, &y
, FALSE
);
1023 #if !GTK_CHECK_VERSION(2,0,0)
1024 #define gtk_window_resize(win, x, y) \
1025 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
1028 static void menu_preset_event(GtkMenuItem
*menuitem
, gpointer data
)
1030 frontend
*fe
= (frontend
*)data
;
1031 game_params
*params
=
1032 (game_params
*)gtk_object_get_data(GTK_OBJECT(menuitem
), "user-data");
1035 midend_set_params(fe
->me
, params
);
1036 midend_new_game(fe
->me
);
1037 get_size(fe
, &x
, &y
);
1040 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1043 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1044 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1048 GdkAtom compound_text_atom
, utf8_string_atom
;
1049 int paste_initialised
= FALSE
;
1053 unsigned char empty
[] = { 0 };
1055 if (paste_initialised
)
1058 if (!compound_text_atom
)
1059 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
1060 if (!utf8_string_atom
)
1061 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
1064 * Ensure that all the cut buffers exist - according to the
1065 * ICCCM, we must do this before we start using cut buffers.
1067 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1068 XA_CUT_BUFFER0
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1069 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1070 XA_CUT_BUFFER1
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1071 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1072 XA_CUT_BUFFER2
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1073 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1074 XA_CUT_BUFFER3
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1075 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1076 XA_CUT_BUFFER4
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1077 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1078 XA_CUT_BUFFER5
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1079 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1080 XA_CUT_BUFFER6
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1081 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1082 XA_CUT_BUFFER7
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1085 /* Store data in a cut-buffer. */
1086 void store_cutbuffer(char *ptr
, int len
)
1088 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1089 XRotateBuffers(GDK_DISPLAY(), 1);
1090 XStoreBytes(GDK_DISPLAY(), ptr
, len
);
1093 void write_clip(frontend
*fe
, char *data
)
1098 sfree(fe
->paste_data
);
1101 * For this simple application we can safely assume that the
1102 * data passed to this function is pure ASCII, which means we
1103 * can return precisely the same stuff for types STRING,
1104 * COMPOUND_TEXT or UTF8_STRING.
1107 fe
->paste_data
= data
;
1108 fe
->paste_data_len
= strlen(data
);
1110 store_cutbuffer(fe
->paste_data
, fe
->paste_data_len
);
1112 if (gtk_selection_owner_set(fe
->area
, GDK_SELECTION_PRIMARY
,
1114 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1115 GDK_SELECTION_TYPE_STRING
, 1);
1116 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1117 compound_text_atom
, 1);
1118 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1119 utf8_string_atom
, 1);
1123 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1124 guint info
, guint time_stamp
, gpointer data
)
1126 frontend
*fe
= (frontend
*)data
;
1127 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1128 fe
->paste_data
, fe
->paste_data_len
);
1131 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1134 frontend
*fe
= (frontend
*)data
;
1137 sfree(fe
->paste_data
);
1138 fe
->paste_data
= NULL
;
1139 fe
->paste_data_len
= 0;
1143 static void menu_copy_event(GtkMenuItem
*menuitem
, gpointer data
)
1145 frontend
*fe
= (frontend
*)data
;
1148 text
= midend_text_format(fe
->me
);
1151 write_clip(fe
, text
);
1157 static void filesel_ok(GtkButton
*button
, gpointer data
)
1159 frontend
*fe
= (frontend
*)data
;
1161 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1164 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1166 fe
->filesel_name
= dupstr(name
);
1169 static char *file_selector(frontend
*fe
, char *title
, int save
)
1171 GtkWidget
*filesel
=
1172 gtk_file_selection_new(title
);
1174 fe
->filesel_name
= NULL
;
1176 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1178 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1181 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1182 GTK_SIGNAL_FUNC(filesel_ok
), fe
);
1183 gtk_signal_connect_object
1184 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1185 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1186 gtk_signal_connect_object
1187 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1188 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1189 gtk_signal_connect(GTK_OBJECT(filesel
), "destroy",
1190 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1191 gtk_widget_show(filesel
);
1192 gtk_window_set_transient_for(GTK_WINDOW(filesel
), GTK_WINDOW(fe
->window
));
1195 return fe
->filesel_name
;
1198 static void savefile_write(void *wctx
, void *buf
, int len
)
1200 FILE *fp
= (FILE *)wctx
;
1201 fwrite(buf
, 1, len
, fp
);
1204 static int savefile_read(void *wctx
, void *buf
, int len
)
1206 FILE *fp
= (FILE *)wctx
;
1209 ret
= fread(buf
, 1, len
, fp
);
1210 return (ret
== len
);
1213 static void menu_save_event(GtkMenuItem
*menuitem
, gpointer data
)
1215 frontend
*fe
= (frontend
*)data
;
1218 name
= file_selector(fe
, "Enter name of game file to save", TRUE
);
1223 if ((fp
= fopen(name
, "r")) != NULL
) {
1224 char buf
[256 + FILENAME_MAX
];
1228 sprintf(buf
, "Are you sure you want to overwrite the"
1230 FILENAME_MAX
, name
);
1231 if (!message_box(fe
->window
, "Question", buf
, TRUE
, MB_YESNO
))
1235 fp
= fopen(name
, "w");
1239 error_box(fe
->window
, "Unable to open save file");
1243 midend_serialise(fe
->me
, savefile_write
, fp
);
1249 static void menu_load_event(GtkMenuItem
*menuitem
, gpointer data
)
1251 frontend
*fe
= (frontend
*)data
;
1255 name
= file_selector(fe
, "Enter name of saved game file to load", FALSE
);
1258 FILE *fp
= fopen(name
, "r");
1262 error_box(fe
->window
, "Unable to open saved game file");
1266 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1271 error_box(fe
->window
, err
);
1275 get_size(fe
, &x
, &y
);
1278 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1281 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1282 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1288 static void menu_solve_event(GtkMenuItem
*menuitem
, gpointer data
)
1290 frontend
*fe
= (frontend
*)data
;
1293 msg
= midend_solve(fe
->me
);
1296 error_box(fe
->window
, msg
);
1299 static void menu_restart_event(GtkMenuItem
*menuitem
, gpointer data
)
1301 frontend
*fe
= (frontend
*)data
;
1303 midend_restart_game(fe
->me
);
1306 static void menu_config_event(GtkMenuItem
*menuitem
, gpointer data
)
1308 frontend
*fe
= (frontend
*)data
;
1309 int which
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1313 if (!get_config(fe
, which
))
1316 midend_new_game(fe
->me
);
1317 get_size(fe
, &x
, &y
);
1320 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1323 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1324 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1328 static void menu_about_event(GtkMenuItem
*menuitem
, gpointer data
)
1330 frontend
*fe
= (frontend
*)data
;
1334 sprintf(titlebuf
, "About %.200s", thegame
.name
);
1337 "from Simon Tatham's Portable Puzzle Collection\n\n"
1338 "%.500s", thegame
.name
, ver
);
1340 message_box(fe
->window
, titlebuf
, textbuf
, TRUE
, MB_OK
);
1343 static GtkWidget
*add_menu_item_with_key(frontend
*fe
, GtkContainer
*cont
,
1344 char *text
, int key
)
1346 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
1347 gtk_container_add(cont
, menuitem
);
1348 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1349 GINT_TO_POINTER(key
));
1350 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1351 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
1352 gtk_widget_show(menuitem
);
1356 static void add_menu_separator(GtkContainer
*cont
)
1358 GtkWidget
*menuitem
= gtk_menu_item_new();
1359 gtk_container_add(cont
, menuitem
);
1360 gtk_widget_show(menuitem
);
1363 static frontend
*new_window(char *game_id
, char **error
)
1367 GtkWidget
*menubar
, *menu
, *menuitem
;
1370 fe
= snew(frontend
);
1372 fe
->timer_active
= FALSE
;
1375 fe
->me
= midend_new(fe
, &thegame
);
1378 *error
= midend_game_id(fe
->me
, game_id
);
1380 midend_free(fe
->me
);
1385 midend_new_game(fe
->me
);
1387 fe
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1388 gtk_window_set_title(GTK_WINDOW(fe
->window
), thegame
.name
);
1390 gtk_window_set_resizable(GTK_WINDOW(fe
->window
), FALSE
);
1392 gtk_window_set_policy(GTK_WINDOW(fe
->window
), FALSE
, FALSE
, TRUE
);
1394 vbox
= GTK_BOX(gtk_vbox_new(FALSE
, 0));
1395 gtk_container_add(GTK_CONTAINER(fe
->window
), GTK_WIDGET(vbox
));
1396 gtk_widget_show(GTK_WIDGET(vbox
));
1398 menubar
= gtk_menu_bar_new();
1399 gtk_box_pack_start(vbox
, menubar
, FALSE
, FALSE
, 0);
1400 gtk_widget_show(menubar
);
1402 menuitem
= gtk_menu_item_new_with_label("Game");
1403 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1404 gtk_widget_show(menuitem
);
1406 menu
= gtk_menu_new();
1407 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1409 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "New", 'n');
1411 menuitem
= gtk_menu_item_new_with_label("Restart");
1412 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1413 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1414 GTK_SIGNAL_FUNC(menu_restart_event
), fe
);
1415 gtk_widget_show(menuitem
);
1417 menuitem
= gtk_menu_item_new_with_label("Specific...");
1418 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1419 GINT_TO_POINTER(CFG_DESC
));
1420 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1421 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1422 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1423 gtk_widget_show(menuitem
);
1425 menuitem
= gtk_menu_item_new_with_label("Random Seed...");
1426 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1427 GINT_TO_POINTER(CFG_SEED
));
1428 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1429 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1430 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1431 gtk_widget_show(menuitem
);
1433 if ((n
= midend_num_presets(fe
->me
)) > 0 || thegame
.can_configure
) {
1437 menuitem
= gtk_menu_item_new_with_label("Type");
1438 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1439 gtk_widget_show(menuitem
);
1441 submenu
= gtk_menu_new();
1442 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), submenu
);
1444 for (i
= 0; i
< n
; i
++) {
1446 game_params
*params
;
1448 midend_fetch_preset(fe
->me
, i
, &name
, ¶ms
);
1450 menuitem
= gtk_menu_item_new_with_label(name
);
1451 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1452 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", params
);
1453 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1454 GTK_SIGNAL_FUNC(menu_preset_event
), fe
);
1455 gtk_widget_show(menuitem
);
1458 if (thegame
.can_configure
) {
1459 menuitem
= gtk_menu_item_new_with_label("Custom...");
1460 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1461 GPOINTER_TO_INT(CFG_SETTINGS
));
1462 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1463 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1464 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1465 gtk_widget_show(menuitem
);
1469 add_menu_separator(GTK_CONTAINER(menu
));
1470 menuitem
= gtk_menu_item_new_with_label("Load");
1471 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1472 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1473 GTK_SIGNAL_FUNC(menu_load_event
), fe
);
1474 gtk_widget_show(menuitem
);
1475 menuitem
= gtk_menu_item_new_with_label("Save");
1476 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1477 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1478 GTK_SIGNAL_FUNC(menu_save_event
), fe
);
1479 gtk_widget_show(menuitem
);
1480 add_menu_separator(GTK_CONTAINER(menu
));
1481 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Undo", 'u');
1482 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Redo", '\x12');
1483 if (thegame
.can_format_as_text
) {
1484 add_menu_separator(GTK_CONTAINER(menu
));
1485 menuitem
= gtk_menu_item_new_with_label("Copy");
1486 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1487 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1488 GTK_SIGNAL_FUNC(menu_copy_event
), fe
);
1489 gtk_widget_show(menuitem
);
1491 if (thegame
.can_solve
) {
1492 add_menu_separator(GTK_CONTAINER(menu
));
1493 menuitem
= gtk_menu_item_new_with_label("Solve");
1494 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1495 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1496 GTK_SIGNAL_FUNC(menu_solve_event
), fe
);
1497 gtk_widget_show(menuitem
);
1499 add_menu_separator(GTK_CONTAINER(menu
));
1500 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Exit", 'q');
1502 menuitem
= gtk_menu_item_new_with_label("Help");
1503 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1504 gtk_widget_show(menuitem
);
1506 menu
= gtk_menu_new();
1507 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1509 menuitem
= gtk_menu_item_new_with_label("About");
1510 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1511 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1512 GTK_SIGNAL_FUNC(menu_about_event
), fe
);
1513 gtk_widget_show(menuitem
);
1520 fe
->colmap
= gdk_colormap_get_system();
1521 colours
= midend_colours(fe
->me
, &ncolours
);
1522 fe
->ncolours
= ncolours
;
1523 fe
->colours
= snewn(ncolours
, GdkColor
);
1524 for (i
= 0; i
< ncolours
; i
++) {
1525 fe
->colours
[i
].red
= colours
[i
*3] * 0xFFFF;
1526 fe
->colours
[i
].green
= colours
[i
*3+1] * 0xFFFF;
1527 fe
->colours
[i
].blue
= colours
[i
*3+2] * 0xFFFF;
1529 success
= snewn(ncolours
, gboolean
);
1530 gdk_colormap_alloc_colors(fe
->colmap
, fe
->colours
, ncolours
,
1531 FALSE
, FALSE
, success
);
1532 for (i
= 0; i
< ncolours
; i
++) {
1534 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
1535 i
, fe
->colours
[i
].red
>> 8,
1536 fe
->colours
[i
].green
>> 8,
1537 fe
->colours
[i
].blue
>> 8);
1541 if (midend_wants_statusbar(fe
->me
)) {
1542 GtkWidget
*viewport
;
1545 viewport
= gtk_viewport_new(NULL
, NULL
);
1546 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport
), GTK_SHADOW_NONE
);
1547 fe
->statusbar
= gtk_statusbar_new();
1548 gtk_container_add(GTK_CONTAINER(viewport
), fe
->statusbar
);
1549 gtk_widget_show(viewport
);
1550 gtk_box_pack_end(vbox
, viewport
, FALSE
, FALSE
, 0);
1551 gtk_widget_show(fe
->statusbar
);
1552 fe
->statusctx
= gtk_statusbar_get_context_id
1553 (GTK_STATUSBAR(fe
->statusbar
), "game");
1554 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
,
1556 gtk_widget_size_request(fe
->statusbar
, &req
);
1558 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
1560 gtk_widget_set_usize(viewport
, -1, req
.height
);
1562 fe
->statusbar
= NULL
;
1564 fe
->area
= gtk_drawing_area_new();
1565 get_size(fe
, &x
, &y
);
1566 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1570 gtk_box_pack_end(vbox
, fe
->area
, TRUE
, TRUE
, 0);
1574 fe
->nfonts
= fe
->fontsize
= 0;
1576 fe
->laststatus
= NULL
;
1578 fe
->paste_data
= NULL
;
1579 fe
->paste_data_len
= 0;
1581 gtk_signal_connect(GTK_OBJECT(fe
->window
), "destroy",
1582 GTK_SIGNAL_FUNC(destroy
), fe
);
1583 gtk_signal_connect(GTK_OBJECT(fe
->window
), "key_press_event",
1584 GTK_SIGNAL_FUNC(key_event
), fe
);
1585 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_press_event",
1586 GTK_SIGNAL_FUNC(button_event
), fe
);
1587 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_release_event",
1588 GTK_SIGNAL_FUNC(button_event
), fe
);
1589 gtk_signal_connect(GTK_OBJECT(fe
->area
), "motion_notify_event",
1590 GTK_SIGNAL_FUNC(motion_event
), fe
);
1591 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_get",
1592 GTK_SIGNAL_FUNC(selection_get
), fe
);
1593 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_clear_event",
1594 GTK_SIGNAL_FUNC(selection_clear
), fe
);
1595 gtk_signal_connect(GTK_OBJECT(fe
->area
), "expose_event",
1596 GTK_SIGNAL_FUNC(expose_area
), fe
);
1597 gtk_signal_connect(GTK_OBJECT(fe
->window
), "map_event",
1598 GTK_SIGNAL_FUNC(map_window
), fe
);
1599 gtk_signal_connect(GTK_OBJECT(fe
->area
), "configure_event",
1600 GTK_SIGNAL_FUNC(configure_area
), fe
);
1602 gtk_widget_add_events(GTK_WIDGET(fe
->area
),
1603 GDK_BUTTON_PRESS_MASK
|
1604 GDK_BUTTON_RELEASE_MASK
|
1605 GDK_BUTTON_MOTION_MASK
);
1607 gtk_widget_show(fe
->area
);
1608 gtk_widget_show(fe
->window
);
1610 gdk_window_set_background(fe
->area
->window
, &fe
->colours
[0]);
1611 gdk_window_set_background(fe
->window
->window
, &fe
->colours
[0]);
1616 int main(int argc
, char **argv
)
1618 char *pname
= argv
[0];
1621 if (argc
> 1 && !strcmp(argv
[1], "--version")) {
1622 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
1628 * Special standalone mode for generating puzzle IDs on the
1629 * command line. Useful for generating puzzles to be printed
1630 * out and solved offline (for puzzles where that even makes
1631 * sense - Solo, for example, is a lot more pencil-and-paper
1632 * friendly than Net!)
1636 * <puzzle-name> --generate [<n> [<params>]]
1638 * <n>, if present, is the number of puzzle IDs to generate.
1639 * <params>, if present, is the same type of parameter string
1640 * you would pass to the puzzle when running it in GUI mode,
1641 * including optional extras such as the expansion factor in
1642 * Rectangles and the difficulty level in Solo.
1644 * If you specify <params>, you must also specify <n> (although
1645 * you may specify it to be 1). Sorry; that was the
1646 * simplest-to-parse command-line syntax I came up with.
1648 if (argc
> 1 && !strcmp(argv
[1], "--generate")) {
1650 char *params
= NULL
, *seed
= NULL
;
1660 par
= thegame
.default_params();
1662 if ( (seed
= strchr(params
, '#')) != NULL
)
1664 thegame
.decode_params(par
, params
);
1666 if ((error
= thegame
.validate_params(par
)) != NULL
) {
1667 fprintf(stderr
, "%s: %s\n", pname
, error
);
1670 parstr
= thegame
.encode_params(par
, FALSE
);
1677 seedlen
= strlen(seed
);
1679 get_random_seed(&seeddata
, &seedlen
);
1681 rs
= random_init(seeddata
, seedlen
);
1686 char *desc
= thegame
.new_desc(par
, rs
, &aux
, FALSE
);
1687 printf("%s:%s\n", parstr
, desc
);
1695 gtk_init(&argc
, &argv
);
1697 if (!new_window(argc
> 1 ? argv
[1] : NULL
, &error
)) {
1698 fprintf(stderr
, "%s: %s\n", pname
, error
);