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 errmsg_button_clicked(GtkButton
*button
, gpointer data
)
665 gtk_widget_destroy(GTK_WIDGET(data
));
668 static int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
670 GtkObject
*cancelbutton
= GTK_OBJECT(data
);
673 * `Escape' effectively clicks the cancel button
675 if (event
->keyval
== GDK_Escape
) {
676 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton
), "clicked");
683 void message_box(GtkWidget
*parent
, char *title
, char *msg
, int centre
)
685 GtkWidget
*window
, *hbox
, *text
, *ok
;
687 window
= gtk_dialog_new();
688 text
= gtk_label_new(msg
);
689 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
690 hbox
= gtk_hbox_new(FALSE
, 0);
691 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
692 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
693 hbox
, FALSE
, FALSE
, 20);
694 gtk_widget_show(text
);
695 gtk_widget_show(hbox
);
696 gtk_window_set_title(GTK_WINDOW(window
), title
);
697 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
698 ok
= gtk_button_new_with_label("OK");
699 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
700 ok
, FALSE
, FALSE
, 0);
702 GTK_WIDGET_SET_FLAGS(ok
, GTK_CAN_DEFAULT
);
703 gtk_window_set_default(GTK_WINDOW(window
), ok
);
704 gtk_signal_connect(GTK_OBJECT(ok
), "clicked",
705 GTK_SIGNAL_FUNC(errmsg_button_clicked
), window
);
706 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
707 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
708 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
709 GTK_SIGNAL_FUNC(win_key_press
), ok
);
710 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
711 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(parent
));
712 /* set_transient_window_pos(parent, window); */
713 gtk_widget_show(window
);
717 void error_box(GtkWidget
*parent
, char *msg
)
719 message_box(parent
, "Error", msg
, FALSE
);
722 static void config_ok_button_clicked(GtkButton
*button
, gpointer data
)
724 frontend
*fe
= (frontend
*)data
;
727 err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
730 error_box(fe
->cfgbox
, err
);
733 gtk_widget_destroy(fe
->cfgbox
);
737 static void config_cancel_button_clicked(GtkButton
*button
, gpointer data
)
739 frontend
*fe
= (frontend
*)data
;
741 gtk_widget_destroy(fe
->cfgbox
);
744 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
747 * GtkEntry has a nasty habit of eating the Return key, which
748 * is unhelpful since it doesn't actually _do_ anything with it
749 * (it calls gtk_widget_activate, but our edit boxes never need
750 * activating). So I catch Return before GtkEntry sees it, and
751 * pass it straight on to the parent widget. Effect: hitting
752 * Return in an edit box will now activate the default button
753 * in the dialog just like it will everywhere else.
755 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
757 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
758 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
765 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
767 config_item
*i
= (config_item
*)data
;
770 i
->sval
= dupstr(gtk_entry_get_text(GTK_ENTRY(ed
)));
773 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
775 config_item
*i
= (config_item
*)data
;
777 i
->ival
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb
));
780 static void droplist_sel(GtkMenuItem
*item
, gpointer data
)
782 config_item
*i
= (config_item
*)data
;
784 i
->ival
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
),
788 static int get_config(frontend
*fe
, int which
)
790 GtkWidget
*w
, *table
, *cancel
;
795 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
796 fe
->cfg_which
= which
;
799 fe
->cfgbox
= gtk_dialog_new();
800 gtk_window_set_title(GTK_WINDOW(fe
->cfgbox
), title
);
803 w
= gtk_button_new_with_label("OK");
804 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
807 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
808 gtk_window_set_default(GTK_WINDOW(fe
->cfgbox
), w
);
809 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
810 GTK_SIGNAL_FUNC(config_ok_button_clicked
), fe
);
812 w
= gtk_button_new_with_label("Cancel");
813 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
816 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
817 GTK_SIGNAL_FUNC(config_cancel_button_clicked
), fe
);
820 table
= gtk_table_new(1, 2, FALSE
);
822 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->vbox
),
823 table
, FALSE
, FALSE
, 0);
824 gtk_widget_show(table
);
826 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
827 gtk_table_resize(GTK_TABLE(table
), y
+1, 2);
832 * Edit box with a label beside it.
835 w
= gtk_label_new(i
->name
);
836 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
837 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
838 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
839 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
844 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
845 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
846 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
848 gtk_entry_set_text(GTK_ENTRY(w
), i
->sval
);
849 gtk_signal_connect(GTK_OBJECT(w
), "changed",
850 GTK_SIGNAL_FUNC(editbox_changed
), i
);
851 gtk_signal_connect(GTK_OBJECT(w
), "key_press_event",
852 GTK_SIGNAL_FUNC(editbox_key
), NULL
);
861 w
= gtk_check_button_new_with_label(i
->name
);
862 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
863 GTK_SIGNAL_FUNC(button_toggled
), i
);
864 gtk_table_attach(GTK_TABLE(table
), w
, 0, 2, y
, y
+1,
865 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
866 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
868 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w
), i
->ival
);
874 * Drop-down list (GtkOptionMenu).
877 w
= gtk_label_new(i
->name
);
878 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
879 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
880 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
881 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
885 w
= gtk_option_menu_new();
886 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
887 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
888 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
896 GtkWidget
*menu
= gtk_menu_new();
898 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), menu
);
906 while (*q
&& *q
!= c
)
909 name
= snewn(q
-p
+1, char);
910 strncpy(name
, p
, q
-p
);
913 if (*q
) q
++; /* eat delimiter */
915 menuitem
= gtk_menu_item_new_with_label(name
);
916 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
917 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
918 GINT_TO_POINTER(val
));
919 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
920 GTK_SIGNAL_FUNC(droplist_sel
), i
);
921 gtk_widget_show(menuitem
);
928 gtk_option_menu_set_history(GTK_OPTION_MENU(w
), i
->ival
);
937 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "destroy",
938 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
939 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "key_press_event",
940 GTK_SIGNAL_FUNC(win_key_press
), cancel
);
941 gtk_window_set_modal(GTK_WINDOW(fe
->cfgbox
), TRUE
);
942 gtk_window_set_transient_for(GTK_WINDOW(fe
->cfgbox
),
943 GTK_WINDOW(fe
->window
));
944 /* set_transient_window_pos(fe->window, fe->cfgbox); */
945 gtk_widget_show(fe
->cfgbox
);
953 static void menu_key_event(GtkMenuItem
*menuitem
, gpointer data
)
955 frontend
*fe
= (frontend
*)data
;
956 int key
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
958 if (!midend_process_key(fe
->me
, 0, 0, key
))
959 gtk_widget_destroy(fe
->window
);
962 static void get_size(frontend
*fe
, int *px
, int *py
)
967 * Currently I don't want to make the GTK port scale large
968 * puzzles to fit on the screen. This is because X does permit
969 * extremely large windows and many window managers provide a
970 * means of navigating round them, and the users I consulted
971 * before deciding said that they'd rather have enormous puzzle
972 * windows spanning multiple screen pages than have them
973 * shrunk. I could change my mind later or introduce
974 * configurability; this would be the place to do so, by
975 * replacing the initial values of x and y with the screen
980 midend_size(fe
->me
, &x
, &y
, FALSE
);
985 #if !GTK_CHECK_VERSION(2,0,0)
986 #define gtk_window_resize(win, x, y) \
987 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
990 static void menu_preset_event(GtkMenuItem
*menuitem
, gpointer data
)
992 frontend
*fe
= (frontend
*)data
;
993 game_params
*params
=
994 (game_params
*)gtk_object_get_data(GTK_OBJECT(menuitem
), "user-data");
997 midend_set_params(fe
->me
, params
);
998 midend_new_game(fe
->me
);
999 get_size(fe
, &x
, &y
);
1002 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1005 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1006 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1010 GdkAtom compound_text_atom
, utf8_string_atom
;
1011 int paste_initialised
= FALSE
;
1015 unsigned char empty
[] = { 0 };
1017 if (paste_initialised
)
1020 if (!compound_text_atom
)
1021 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
1022 if (!utf8_string_atom
)
1023 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
1026 * Ensure that all the cut buffers exist - according to the
1027 * ICCCM, we must do this before we start using cut buffers.
1029 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1030 XA_CUT_BUFFER0
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1031 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1032 XA_CUT_BUFFER1
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1033 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1034 XA_CUT_BUFFER2
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1035 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1036 XA_CUT_BUFFER3
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1037 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1038 XA_CUT_BUFFER4
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1039 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1040 XA_CUT_BUFFER5
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1041 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1042 XA_CUT_BUFFER6
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1043 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1044 XA_CUT_BUFFER7
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
1047 /* Store data in a cut-buffer. */
1048 void store_cutbuffer(char *ptr
, int len
)
1050 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1051 XRotateBuffers(GDK_DISPLAY(), 1);
1052 XStoreBytes(GDK_DISPLAY(), ptr
, len
);
1055 void write_clip(frontend
*fe
, char *data
)
1060 sfree(fe
->paste_data
);
1063 * For this simple application we can safely assume that the
1064 * data passed to this function is pure ASCII, which means we
1065 * can return precisely the same stuff for types STRING,
1066 * COMPOUND_TEXT or UTF8_STRING.
1069 fe
->paste_data
= data
;
1070 fe
->paste_data_len
= strlen(data
);
1072 store_cutbuffer(fe
->paste_data
, fe
->paste_data_len
);
1074 if (gtk_selection_owner_set(fe
->area
, GDK_SELECTION_PRIMARY
,
1076 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1077 GDK_SELECTION_TYPE_STRING
, 1);
1078 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1079 compound_text_atom
, 1);
1080 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
1081 utf8_string_atom
, 1);
1085 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1086 guint info
, guint time_stamp
, gpointer data
)
1088 frontend
*fe
= (frontend
*)data
;
1089 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1090 fe
->paste_data
, fe
->paste_data_len
);
1093 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1096 frontend
*fe
= (frontend
*)data
;
1099 sfree(fe
->paste_data
);
1100 fe
->paste_data
= NULL
;
1101 fe
->paste_data_len
= 0;
1105 static void menu_copy_event(GtkMenuItem
*menuitem
, gpointer data
)
1107 frontend
*fe
= (frontend
*)data
;
1110 text
= midend_text_format(fe
->me
);
1113 write_clip(fe
, text
);
1119 static void filesel_ok(GtkButton
*button
, gpointer data
)
1121 frontend
*fe
= (frontend
*)data
;
1123 gpointer filesel
= gtk_object_get_data(GTK_OBJECT(button
), "user-data");
1126 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel
));
1128 fe
->filesel_name
= dupstr(name
);
1131 static char *file_selector(frontend
*fe
, char *title
, int save
)
1133 GtkWidget
*filesel
=
1134 gtk_file_selection_new(title
);
1136 fe
->filesel_name
= NULL
;
1138 gtk_window_set_modal(GTK_WINDOW(filesel
), TRUE
);
1140 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "user-data",
1143 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1144 GTK_SIGNAL_FUNC(filesel_ok
), fe
);
1145 gtk_signal_connect_object
1146 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->ok_button
), "clicked",
1147 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1148 gtk_signal_connect_object
1149 (GTK_OBJECT(GTK_FILE_SELECTION(filesel
)->cancel_button
), "clicked",
1150 GTK_SIGNAL_FUNC(gtk_widget_destroy
), (gpointer
)filesel
);
1151 gtk_signal_connect(GTK_OBJECT(filesel
), "destroy",
1152 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
1153 gtk_widget_show(filesel
);
1154 gtk_window_set_transient_for(GTK_WINDOW(filesel
), GTK_WINDOW(fe
->window
));
1157 return fe
->filesel_name
;
1160 static void savefile_write(void *wctx
, void *buf
, int len
)
1162 FILE *fp
= (FILE *)wctx
;
1163 fwrite(buf
, 1, len
, fp
);
1166 static int savefile_read(void *wctx
, void *buf
, int len
)
1168 FILE *fp
= (FILE *)wctx
;
1171 ret
= fread(buf
, 1, len
, fp
);
1172 return (ret
== len
);
1175 static void menu_save_event(GtkMenuItem
*menuitem
, gpointer data
)
1177 frontend
*fe
= (frontend
*)data
;
1180 name
= file_selector(fe
, "Enter name of game file to save", TRUE
);
1183 FILE *fp
= fopen(name
, "w");
1187 error_box(fe
->window
, "Unable to open save file");
1191 midend_serialise(fe
->me
, savefile_write
, fp
);
1197 static void menu_load_event(GtkMenuItem
*menuitem
, gpointer data
)
1199 frontend
*fe
= (frontend
*)data
;
1203 name
= file_selector(fe
, "Enter name of saved game file to load", FALSE
);
1206 FILE *fp
= fopen(name
, "r");
1210 error_box(fe
->window
, "Unable to open saved game file");
1214 err
= midend_deserialise(fe
->me
, savefile_read
, fp
);
1219 error_box(fe
->window
, err
);
1223 get_size(fe
, &x
, &y
);
1226 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1229 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1230 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1236 static void menu_solve_event(GtkMenuItem
*menuitem
, gpointer data
)
1238 frontend
*fe
= (frontend
*)data
;
1241 msg
= midend_solve(fe
->me
);
1244 error_box(fe
->window
, msg
);
1247 static void menu_restart_event(GtkMenuItem
*menuitem
, gpointer data
)
1249 frontend
*fe
= (frontend
*)data
;
1251 midend_restart_game(fe
->me
);
1254 static void menu_config_event(GtkMenuItem
*menuitem
, gpointer data
)
1256 frontend
*fe
= (frontend
*)data
;
1257 int which
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1261 if (!get_config(fe
, which
))
1264 midend_new_game(fe
->me
);
1265 get_size(fe
, &x
, &y
);
1268 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1271 gtk_widget_size_request(GTK_WIDGET(fe
->window
), &req
);
1272 gtk_window_resize(GTK_WINDOW(fe
->window
), req
.width
, req
.height
);
1276 static void menu_about_event(GtkMenuItem
*menuitem
, gpointer data
)
1278 frontend
*fe
= (frontend
*)data
;
1282 sprintf(titlebuf
, "About %.200s", thegame
.name
);
1285 "from Simon Tatham's Portable Puzzle Collection\n\n"
1286 "%.500s", thegame
.name
, ver
);
1288 message_box(fe
->window
, titlebuf
, textbuf
, TRUE
);
1291 static GtkWidget
*add_menu_item_with_key(frontend
*fe
, GtkContainer
*cont
,
1292 char *text
, int key
)
1294 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
1295 gtk_container_add(cont
, menuitem
);
1296 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1297 GINT_TO_POINTER(key
));
1298 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1299 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
1300 gtk_widget_show(menuitem
);
1304 static void add_menu_separator(GtkContainer
*cont
)
1306 GtkWidget
*menuitem
= gtk_menu_item_new();
1307 gtk_container_add(cont
, menuitem
);
1308 gtk_widget_show(menuitem
);
1311 static frontend
*new_window(char *game_id
, char **error
)
1315 GtkWidget
*menubar
, *menu
, *menuitem
;
1318 fe
= snew(frontend
);
1320 fe
->timer_active
= FALSE
;
1323 fe
->me
= midend_new(fe
, &thegame
);
1326 *error
= midend_game_id(fe
->me
, game_id
);
1328 midend_free(fe
->me
);
1333 midend_new_game(fe
->me
);
1335 fe
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1336 gtk_window_set_title(GTK_WINDOW(fe
->window
), thegame
.name
);
1338 gtk_window_set_resizable(GTK_WINDOW(fe
->window
), FALSE
);
1340 gtk_window_set_policy(GTK_WINDOW(fe
->window
), FALSE
, FALSE
, TRUE
);
1342 vbox
= GTK_BOX(gtk_vbox_new(FALSE
, 0));
1343 gtk_container_add(GTK_CONTAINER(fe
->window
), GTK_WIDGET(vbox
));
1344 gtk_widget_show(GTK_WIDGET(vbox
));
1346 menubar
= gtk_menu_bar_new();
1347 gtk_box_pack_start(vbox
, menubar
, FALSE
, FALSE
, 0);
1348 gtk_widget_show(menubar
);
1350 menuitem
= gtk_menu_item_new_with_label("Game");
1351 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1352 gtk_widget_show(menuitem
);
1354 menu
= gtk_menu_new();
1355 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1357 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "New", 'n');
1359 menuitem
= gtk_menu_item_new_with_label("Restart");
1360 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1361 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1362 GTK_SIGNAL_FUNC(menu_restart_event
), fe
);
1363 gtk_widget_show(menuitem
);
1365 menuitem
= gtk_menu_item_new_with_label("Specific...");
1366 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1367 GINT_TO_POINTER(CFG_DESC
));
1368 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1369 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1370 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1371 gtk_widget_show(menuitem
);
1373 menuitem
= gtk_menu_item_new_with_label("Random Seed...");
1374 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1375 GINT_TO_POINTER(CFG_SEED
));
1376 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1377 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1378 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1379 gtk_widget_show(menuitem
);
1381 if ((n
= midend_num_presets(fe
->me
)) > 0 || thegame
.can_configure
) {
1385 menuitem
= gtk_menu_item_new_with_label("Type");
1386 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1387 gtk_widget_show(menuitem
);
1389 submenu
= gtk_menu_new();
1390 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), submenu
);
1392 for (i
= 0; i
< n
; i
++) {
1394 game_params
*params
;
1396 midend_fetch_preset(fe
->me
, i
, &name
, ¶ms
);
1398 menuitem
= gtk_menu_item_new_with_label(name
);
1399 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1400 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", params
);
1401 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1402 GTK_SIGNAL_FUNC(menu_preset_event
), fe
);
1403 gtk_widget_show(menuitem
);
1406 if (thegame
.can_configure
) {
1407 menuitem
= gtk_menu_item_new_with_label("Custom...");
1408 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1409 GPOINTER_TO_INT(CFG_SETTINGS
));
1410 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1411 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1412 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1413 gtk_widget_show(menuitem
);
1417 if (getenv("PUZZLES_EXPERIMENTAL_SAVE") != NULL
) {
1418 add_menu_separator(GTK_CONTAINER(menu
));
1419 menuitem
= gtk_menu_item_new_with_label("Load");
1420 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1421 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1422 GTK_SIGNAL_FUNC(menu_load_event
), fe
);
1423 gtk_widget_show(menuitem
);
1424 menuitem
= gtk_menu_item_new_with_label("Save");
1425 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1426 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1427 GTK_SIGNAL_FUNC(menu_save_event
), fe
);
1428 gtk_widget_show(menuitem
);
1430 add_menu_separator(GTK_CONTAINER(menu
));
1431 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Undo", 'u');
1432 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Redo", '\x12');
1433 if (thegame
.can_format_as_text
) {
1434 add_menu_separator(GTK_CONTAINER(menu
));
1435 menuitem
= gtk_menu_item_new_with_label("Copy");
1436 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1437 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1438 GTK_SIGNAL_FUNC(menu_copy_event
), fe
);
1439 gtk_widget_show(menuitem
);
1441 if (thegame
.can_solve
) {
1442 add_menu_separator(GTK_CONTAINER(menu
));
1443 menuitem
= gtk_menu_item_new_with_label("Solve");
1444 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1445 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1446 GTK_SIGNAL_FUNC(menu_solve_event
), fe
);
1447 gtk_widget_show(menuitem
);
1449 add_menu_separator(GTK_CONTAINER(menu
));
1450 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Exit", 'q');
1452 menuitem
= gtk_menu_item_new_with_label("Help");
1453 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1454 gtk_widget_show(menuitem
);
1456 menu
= gtk_menu_new();
1457 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1459 menuitem
= gtk_menu_item_new_with_label("About");
1460 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1461 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1462 GTK_SIGNAL_FUNC(menu_about_event
), fe
);
1463 gtk_widget_show(menuitem
);
1470 fe
->colmap
= gdk_colormap_get_system();
1471 colours
= midend_colours(fe
->me
, &ncolours
);
1472 fe
->ncolours
= ncolours
;
1473 fe
->colours
= snewn(ncolours
, GdkColor
);
1474 for (i
= 0; i
< ncolours
; i
++) {
1475 fe
->colours
[i
].red
= colours
[i
*3] * 0xFFFF;
1476 fe
->colours
[i
].green
= colours
[i
*3+1] * 0xFFFF;
1477 fe
->colours
[i
].blue
= colours
[i
*3+2] * 0xFFFF;
1479 success
= snewn(ncolours
, gboolean
);
1480 gdk_colormap_alloc_colors(fe
->colmap
, fe
->colours
, ncolours
,
1481 FALSE
, FALSE
, success
);
1482 for (i
= 0; i
< ncolours
; i
++) {
1484 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
1485 i
, fe
->colours
[i
].red
>> 8,
1486 fe
->colours
[i
].green
>> 8,
1487 fe
->colours
[i
].blue
>> 8);
1491 if (midend_wants_statusbar(fe
->me
)) {
1492 GtkWidget
*viewport
;
1495 viewport
= gtk_viewport_new(NULL
, NULL
);
1496 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport
), GTK_SHADOW_NONE
);
1497 fe
->statusbar
= gtk_statusbar_new();
1498 gtk_container_add(GTK_CONTAINER(viewport
), fe
->statusbar
);
1499 gtk_widget_show(viewport
);
1500 gtk_box_pack_end(vbox
, viewport
, FALSE
, FALSE
, 0);
1501 gtk_widget_show(fe
->statusbar
);
1502 fe
->statusctx
= gtk_statusbar_get_context_id
1503 (GTK_STATUSBAR(fe
->statusbar
), "game");
1504 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
,
1506 gtk_widget_size_request(fe
->statusbar
, &req
);
1508 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
1510 gtk_widget_set_usize(viewport
, -1, req
.height
);
1512 fe
->statusbar
= NULL
;
1514 fe
->area
= gtk_drawing_area_new();
1515 get_size(fe
, &x
, &y
);
1516 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1520 gtk_box_pack_end(vbox
, fe
->area
, TRUE
, TRUE
, 0);
1524 fe
->nfonts
= fe
->fontsize
= 0;
1526 fe
->laststatus
= NULL
;
1528 fe
->paste_data
= NULL
;
1529 fe
->paste_data_len
= 0;
1531 gtk_signal_connect(GTK_OBJECT(fe
->window
), "destroy",
1532 GTK_SIGNAL_FUNC(destroy
), fe
);
1533 gtk_signal_connect(GTK_OBJECT(fe
->window
), "key_press_event",
1534 GTK_SIGNAL_FUNC(key_event
), fe
);
1535 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_press_event",
1536 GTK_SIGNAL_FUNC(button_event
), fe
);
1537 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_release_event",
1538 GTK_SIGNAL_FUNC(button_event
), fe
);
1539 gtk_signal_connect(GTK_OBJECT(fe
->area
), "motion_notify_event",
1540 GTK_SIGNAL_FUNC(motion_event
), fe
);
1541 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_get",
1542 GTK_SIGNAL_FUNC(selection_get
), fe
);
1543 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_clear_event",
1544 GTK_SIGNAL_FUNC(selection_clear
), fe
);
1545 gtk_signal_connect(GTK_OBJECT(fe
->area
), "expose_event",
1546 GTK_SIGNAL_FUNC(expose_area
), fe
);
1547 gtk_signal_connect(GTK_OBJECT(fe
->window
), "map_event",
1548 GTK_SIGNAL_FUNC(map_window
), fe
);
1549 gtk_signal_connect(GTK_OBJECT(fe
->area
), "configure_event",
1550 GTK_SIGNAL_FUNC(configure_area
), fe
);
1552 gtk_widget_add_events(GTK_WIDGET(fe
->area
),
1553 GDK_BUTTON_PRESS_MASK
|
1554 GDK_BUTTON_RELEASE_MASK
|
1555 GDK_BUTTON_MOTION_MASK
);
1557 gtk_widget_show(fe
->area
);
1558 gtk_widget_show(fe
->window
);
1560 gdk_window_set_background(fe
->area
->window
, &fe
->colours
[0]);
1561 gdk_window_set_background(fe
->window
->window
, &fe
->colours
[0]);
1566 int main(int argc
, char **argv
)
1568 char *pname
= argv
[0];
1571 if (argc
> 1 && !strcmp(argv
[1], "--version")) {
1572 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
1578 * Special standalone mode for generating puzzle IDs on the
1579 * command line. Useful for generating puzzles to be printed
1580 * out and solved offline (for puzzles where that even makes
1581 * sense - Solo, for example, is a lot more pencil-and-paper
1582 * friendly than Net!)
1586 * <puzzle-name> --generate [<n> [<params>]]
1588 * <n>, if present, is the number of puzzle IDs to generate.
1589 * <params>, if present, is the same type of parameter string
1590 * you would pass to the puzzle when running it in GUI mode,
1591 * including optional extras such as the expansion factor in
1592 * Rectangles and the difficulty level in Solo.
1594 * If you specify <params>, you must also specify <n> (although
1595 * you may specify it to be 1). Sorry; that was the
1596 * simplest-to-parse command-line syntax I came up with.
1598 if (argc
> 1 && !strcmp(argv
[1], "--generate")) {
1600 char *params
= NULL
, *seed
= NULL
;
1610 par
= thegame
.default_params();
1612 if ( (seed
= strchr(params
, '#')) != NULL
)
1614 thegame
.decode_params(par
, params
);
1616 if ((error
= thegame
.validate_params(par
)) != NULL
) {
1617 fprintf(stderr
, "%s: %s\n", pname
, error
);
1620 parstr
= thegame
.encode_params(par
, FALSE
);
1627 seedlen
= strlen(seed
);
1629 get_random_seed(&seeddata
, &seedlen
);
1631 rs
= random_init(seeddata
, seedlen
);
1636 char *desc
= thegame
.new_desc(par
, rs
, &aux
, FALSE
);
1637 printf("%s:%s\n", parstr
, desc
);
1645 gtk_init(&argc
, &argv
);
1647 if (!new_window(argc
> 1 ? argv
[1] : NULL
, &error
)) {
1648 fprintf(stderr
, "%s: %s\n", pname
, error
);