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 /* ----------------------------------------------------------------------
25 * Error reporting functions used elsewhere.
28 void fatal(char *fmt
, ...)
32 fprintf(stderr
, "fatal error: ");
35 vfprintf(stderr
, fmt
, ap
);
38 fprintf(stderr
, "\n");
42 /* ----------------------------------------------------------------------
43 * GTK front end to puzzles.
53 * This structure holds all the data relevant to a single window.
54 * In principle this would allow us to open multiple independent
55 * puzzle windows, although I can't currently see any real point in
56 * doing so. I'm just coding cleanly because there's no
57 * particularly good reason not to.
71 int bbox_l
, bbox_r
, bbox_u
, bbox_d
;
72 int timer_active
, timer_id
;
73 struct timeval last_time
;
77 int cfg_which
, cfgret
;
82 int pw
, ph
; /* pixmap size (w, h are area size) */
83 int ox
, oy
; /* offset of pixmap in drawing area */
86 void get_random_seed(void **randseed
, int *randseedsize
)
88 struct timeval
*tvp
= snew(struct timeval
);
89 gettimeofday(tvp
, NULL
);
90 *randseed
= (void *)tvp
;
91 *randseedsize
= sizeof(struct timeval
);
94 void frontend_default_colour(frontend
*fe
, float *output
)
96 GdkColor col
= fe
->window
->style
->bg
[GTK_STATE_NORMAL
];
97 output
[0] = col
.red
/ 65535.0;
98 output
[1] = col
.green
/ 65535.0;
99 output
[2] = col
.blue
/ 65535.0;
102 void status_bar(frontend
*fe
, char *text
)
106 assert(fe
->statusbar
);
108 rewritten
= midend_rewrite_statusbar(fe
->me
, text
);
109 if (!fe
->laststatus
|| strcmp(rewritten
, fe
->laststatus
)) {
110 gtk_statusbar_pop(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
);
111 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
, rewritten
);
112 sfree(fe
->laststatus
);
113 fe
->laststatus
= rewritten
;
119 void start_draw(frontend
*fe
)
121 fe
->gc
= gdk_gc_new(fe
->area
->window
);
128 void clip(frontend
*fe
, int x
, int y
, int w
, int h
)
137 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
140 void unclip(frontend
*fe
)
149 gdk_gc_set_clip_rectangle(fe
->gc
, &rect
);
152 void draw_text(frontend
*fe
, int x
, int y
, int fonttype
, int fontsize
,
153 int align
, int colour
, char *text
)
158 * Find or create the font.
160 for (i
= 0; i
< fe
->nfonts
; i
++)
161 if (fe
->fonts
[i
].type
== fonttype
&& fe
->fonts
[i
].size
== fontsize
)
164 if (i
== fe
->nfonts
) {
165 if (fe
->fontsize
<= fe
->nfonts
) {
166 fe
->fontsize
= fe
->nfonts
+ 10;
167 fe
->fonts
= sresize(fe
->fonts
, fe
->fontsize
, struct font
);
172 fe
->fonts
[i
].type
= fonttype
;
173 fe
->fonts
[i
].size
= fontsize
;
175 #if GTK_CHECK_VERSION(2,0,0)
177 * Use Pango to find the closest match to the requested
181 PangoFontDescription
*fd
;
183 fd
= pango_font_description_new();
184 /* `Monospace' and `Sans' are meta-families guaranteed to exist */
185 pango_font_description_set_family(fd
, fonttype
== FONT_FIXED ?
186 "Monospace" : "Sans");
187 pango_font_description_set_weight(fd
, PANGO_WEIGHT_BOLD
);
189 * I found some online Pango documentation which
190 * described a function called
191 * pango_font_description_set_absolute_size(), which is
192 * _exactly_ what I want here. Unfortunately, none of
193 * my local Pango installations have it (presumably
194 * they're too old), so I'm going to have to hack round
195 * it by figuring out the point size myself. This
196 * limits me to X and probably also breaks in later
197 * Pango installations, so ideally I should add another
198 * CHECK_VERSION type ifdef and use set_absolute_size
199 * where available. All very annoying.
201 #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
202 pango_font_description_set_absolute_size(fd
, PANGO_SCALE
*fontsize
);
205 Display
*d
= GDK_DISPLAY();
206 int s
= DefaultScreen(d
);
208 (PANGO_SCALE
* 72.27 / 25.4) *
209 ((double) DisplayWidthMM(d
, s
) / DisplayWidth (d
, s
));
210 pango_font_description_set_size(fd
, resolution
* fontsize
);
213 fe
->fonts
[i
].font
= gdk_font_from_description(fd
);
214 pango_font_description_free(fd
);
217 if (!fe
->fonts
[i
].font
)
220 * In GTK 1.2, I don't know of any plausible way to
221 * pick a suitable font, so I'm just going to be
224 * This is also fallback code called if the Pango
225 * approach fails to find an appropriate font.
227 fe
->fonts
[i
].font
= gdk_font_load(fonttype
== FONT_FIXED ?
228 "fixed" : "variable");
232 * Find string dimensions and process alignment.
235 int lb
, rb
, wid
, asc
, desc
;
238 * Measure vertical string extents with respect to the same
241 gdk_string_extents(fe
->fonts
[i
].font
,
242 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
243 &lb
, &rb
, &wid
, &asc
, &desc
);
244 if (align
& ALIGN_VCENTRE
)
245 y
+= asc
- (asc
+desc
)/2;
248 * ... but horizontal extents with respect to the provided
249 * string. This means that multiple pieces of text centred
250 * on the same y-coordinate don't have different baselines.
252 gdk_string_extents(fe
->fonts
[i
].font
, text
,
253 &lb
, &rb
, &wid
, &asc
, &desc
);
255 if (align
& ALIGN_HCENTRE
)
257 else if (align
& ALIGN_HRIGHT
)
263 * Set colour and actually draw text.
265 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
266 gdk_draw_string(fe
->pixmap
, fe
->fonts
[i
].font
, fe
->gc
, x
, y
, text
);
269 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
)
271 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
272 gdk_draw_rectangle(fe
->pixmap
, fe
->gc
, 1, x
, y
, w
, h
);
275 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
)
277 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
278 gdk_draw_line(fe
->pixmap
, fe
->gc
, x1
, y1
, x2
, y2
);
281 void draw_polygon(frontend
*fe
, int *coords
, int npoints
,
282 int fill
, int colour
)
284 GdkPoint
*points
= snewn(npoints
, GdkPoint
);
287 for (i
= 0; i
< npoints
; i
++) {
288 points
[i
].x
= coords
[i
*2];
289 points
[i
].y
= coords
[i
*2+1];
292 gdk_gc_set_foreground(fe
->gc
, &fe
->colours
[colour
]);
293 gdk_draw_polygon(fe
->pixmap
, fe
->gc
, fill
, points
, npoints
);
298 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
)
300 if (fe
->bbox_l
> x
) fe
->bbox_l
= x
;
301 if (fe
->bbox_r
< x
+w
) fe
->bbox_r
= x
+w
;
302 if (fe
->bbox_u
> y
) fe
->bbox_u
= y
;
303 if (fe
->bbox_d
< y
+h
) fe
->bbox_d
= y
+h
;
306 void end_draw(frontend
*fe
)
308 gdk_gc_unref(fe
->gc
);
311 if (fe
->bbox_l
< fe
->bbox_r
&& fe
->bbox_u
< fe
->bbox_d
) {
312 gdk_draw_pixmap(fe
->area
->window
,
313 fe
->area
->style
->fg_gc
[GTK_WIDGET_STATE(fe
->area
)],
315 fe
->bbox_l
, fe
->bbox_u
,
316 fe
->ox
+ fe
->bbox_l
, fe
->oy
+ fe
->bbox_u
,
317 fe
->bbox_r
- fe
->bbox_l
, fe
->bbox_d
- fe
->bbox_u
);
321 static void destroy(GtkWidget
*widget
, gpointer data
)
323 frontend
*fe
= (frontend
*)data
;
324 deactivate_timer(fe
);
328 static gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
330 frontend
*fe
= (frontend
*)data
;
332 int shift
= (event
->state
& GDK_SHIFT_MASK
) ? MOD_SHFT
: 0;
333 int ctrl
= (event
->state
& GDK_CONTROL_MASK
) ? MOD_CTRL
: 0;
338 if (event
->keyval
== GDK_Up
)
339 keyval
= shift
| ctrl
| CURSOR_UP
;
340 else if (event
->keyval
== GDK_KP_Up
|| event
->keyval
== GDK_KP_8
)
341 keyval
= MOD_NUM_KEYPAD
| '8';
342 else if (event
->keyval
== GDK_Down
)
343 keyval
= shift
| ctrl
| CURSOR_DOWN
;
344 else if (event
->keyval
== GDK_KP_Down
|| event
->keyval
== GDK_KP_2
)
345 keyval
= MOD_NUM_KEYPAD
| '2';
346 else if (event
->keyval
== GDK_Left
)
347 keyval
= shift
| ctrl
| CURSOR_LEFT
;
348 else if (event
->keyval
== GDK_KP_Left
|| event
->keyval
== GDK_KP_4
)
349 keyval
= MOD_NUM_KEYPAD
| '4';
350 else if (event
->keyval
== GDK_Right
)
351 keyval
= shift
| ctrl
| CURSOR_RIGHT
;
352 else if (event
->keyval
== GDK_KP_Right
|| event
->keyval
== GDK_KP_6
)
353 keyval
= MOD_NUM_KEYPAD
| '6';
354 else if (event
->keyval
== GDK_KP_Home
|| event
->keyval
== GDK_KP_7
)
355 keyval
= MOD_NUM_KEYPAD
| '7';
356 else if (event
->keyval
== GDK_KP_End
|| event
->keyval
== GDK_KP_1
)
357 keyval
= MOD_NUM_KEYPAD
| '1';
358 else if (event
->keyval
== GDK_KP_Page_Up
|| event
->keyval
== GDK_KP_9
)
359 keyval
= MOD_NUM_KEYPAD
| '9';
360 else if (event
->keyval
== GDK_KP_Page_Down
|| event
->keyval
== GDK_KP_3
)
361 keyval
= MOD_NUM_KEYPAD
| '3';
362 else if (event
->keyval
== GDK_KP_Insert
|| event
->keyval
== GDK_KP_0
)
363 keyval
= MOD_NUM_KEYPAD
| '0';
364 else if (event
->keyval
== GDK_KP_Begin
|| event
->keyval
== GDK_KP_5
)
365 keyval
= MOD_NUM_KEYPAD
| '5';
366 else if (event
->string
[0] && !event
->string
[1])
367 keyval
= (unsigned char)event
->string
[0];
372 !midend_process_key(fe
->me
, 0, 0, keyval
))
373 gtk_widget_destroy(fe
->window
);
378 static gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
,
381 frontend
*fe
= (frontend
*)data
;
387 if (event
->type
!= GDK_BUTTON_PRESS
&& event
->type
!= GDK_BUTTON_RELEASE
)
390 if (event
->button
== 2 || (event
->state
& GDK_SHIFT_MASK
))
391 button
= MIDDLE_BUTTON
;
392 else if (event
->button
== 3 || (event
->state
& GDK_MOD1_MASK
))
393 button
= RIGHT_BUTTON
;
394 else if (event
->button
== 1)
395 button
= LEFT_BUTTON
;
397 return FALSE
; /* don't even know what button! */
399 if (event
->type
== GDK_BUTTON_RELEASE
)
400 button
+= LEFT_RELEASE
- LEFT_BUTTON
;
402 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
403 event
->y
- fe
->oy
, button
))
404 gtk_widget_destroy(fe
->window
);
409 static gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
,
412 frontend
*fe
= (frontend
*)data
;
418 if (event
->state
& (GDK_BUTTON2_MASK
| GDK_SHIFT_MASK
))
419 button
= MIDDLE_DRAG
;
420 else if (event
->state
& GDK_BUTTON1_MASK
)
422 else if (event
->state
& GDK_BUTTON3_MASK
)
425 return FALSE
; /* don't even know what button! */
427 if (!midend_process_key(fe
->me
, event
->x
- fe
->ox
,
428 event
->y
- fe
->oy
, button
))
429 gtk_widget_destroy(fe
->window
);
434 static gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
,
437 frontend
*fe
= (frontend
*)data
;
440 gdk_draw_pixmap(widget
->window
,
441 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
443 event
->area
.x
- fe
->ox
, event
->area
.y
- fe
->oy
,
444 event
->area
.x
, event
->area
.y
,
445 event
->area
.width
, event
->area
.height
);
450 static gint
map_window(GtkWidget
*widget
, GdkEvent
*event
,
453 frontend
*fe
= (frontend
*)data
;
456 * Apparently we need to do this because otherwise the status
457 * bar will fail to update immediately. Annoying, but there we
460 gtk_widget_queue_draw(fe
->window
);
465 static gint
configure_area(GtkWidget
*widget
,
466 GdkEventConfigure
*event
, gpointer data
)
468 frontend
*fe
= (frontend
*)data
;
473 gdk_pixmap_unref(fe
->pixmap
);
475 x
= fe
->w
= event
->width
;
476 y
= fe
->h
= event
->height
;
477 midend_size(fe
->me
, &x
, &y
, TRUE
);
480 fe
->ox
= (fe
->w
- fe
->pw
) / 2;
481 fe
->oy
= (fe
->h
- fe
->ph
) / 2;
483 fe
->pixmap
= gdk_pixmap_new(widget
->window
, fe
->pw
, fe
->ph
, -1);
485 gc
= gdk_gc_new(fe
->area
->window
);
486 gdk_gc_set_foreground(gc
, &fe
->colours
[0]);
487 gdk_draw_rectangle(fe
->pixmap
, gc
, 1, 0, 0, fe
->pw
, fe
->ph
);
490 midend_force_redraw(fe
->me
);
495 static gint
timer_func(gpointer data
)
497 frontend
*fe
= (frontend
*)data
;
499 if (fe
->timer_active
) {
502 gettimeofday(&now
, NULL
);
503 elapsed
= ((now
.tv_usec
- fe
->last_time
.tv_usec
) * 0.000001F
+
504 (now
.tv_sec
- fe
->last_time
.tv_sec
));
505 midend_timer(fe
->me
, elapsed
); /* may clear timer_active */
509 return fe
->timer_active
;
512 void deactivate_timer(frontend
*fe
)
514 if (fe
->timer_active
)
515 gtk_timeout_remove(fe
->timer_id
);
516 fe
->timer_active
= FALSE
;
519 void activate_timer(frontend
*fe
)
521 if (!fe
->timer_active
) {
522 fe
->timer_id
= gtk_timeout_add(20, timer_func
, fe
);
523 gettimeofday(&fe
->last_time
, NULL
);
525 fe
->timer_active
= TRUE
;
528 static void window_destroy(GtkWidget
*widget
, gpointer data
)
533 static void errmsg_button_clicked(GtkButton
*button
, gpointer data
)
535 gtk_widget_destroy(GTK_WIDGET(data
));
538 static int win_key_press(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
540 GtkObject
*cancelbutton
= GTK_OBJECT(data
);
543 * `Escape' effectively clicks the cancel button
545 if (event
->keyval
== GDK_Escape
) {
546 gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton
), "clicked");
553 void message_box(GtkWidget
*parent
, char *title
, char *msg
, int centre
)
555 GtkWidget
*window
, *hbox
, *text
, *ok
;
557 window
= gtk_dialog_new();
558 text
= gtk_label_new(msg
);
559 gtk_misc_set_alignment(GTK_MISC(text
), 0.0, 0.0);
560 hbox
= gtk_hbox_new(FALSE
, 0);
561 gtk_box_pack_start(GTK_BOX(hbox
), text
, FALSE
, FALSE
, 20);
562 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window
)->vbox
),
563 hbox
, FALSE
, FALSE
, 20);
564 gtk_widget_show(text
);
565 gtk_widget_show(hbox
);
566 gtk_window_set_title(GTK_WINDOW(window
), title
);
567 gtk_label_set_line_wrap(GTK_LABEL(text
), TRUE
);
568 ok
= gtk_button_new_with_label("OK");
569 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window
)->action_area
),
570 ok
, FALSE
, FALSE
, 0);
572 GTK_WIDGET_SET_FLAGS(ok
, GTK_CAN_DEFAULT
);
573 gtk_window_set_default(GTK_WINDOW(window
), ok
);
574 gtk_signal_connect(GTK_OBJECT(ok
), "clicked",
575 GTK_SIGNAL_FUNC(errmsg_button_clicked
), window
);
576 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
577 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
578 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
579 GTK_SIGNAL_FUNC(win_key_press
), ok
);
580 gtk_window_set_modal(GTK_WINDOW(window
), TRUE
);
581 gtk_window_set_transient_for(GTK_WINDOW(window
), GTK_WINDOW(parent
));
582 /* set_transient_window_pos(parent, window); */
583 gtk_widget_show(window
);
587 void error_box(GtkWidget
*parent
, char *msg
)
589 message_box(parent
, "Error", msg
, FALSE
);
592 static void config_ok_button_clicked(GtkButton
*button
, gpointer data
)
594 frontend
*fe
= (frontend
*)data
;
597 err
= midend_set_config(fe
->me
, fe
->cfg_which
, fe
->cfg
);
600 error_box(fe
->cfgbox
, err
);
603 gtk_widget_destroy(fe
->cfgbox
);
607 static void config_cancel_button_clicked(GtkButton
*button
, gpointer data
)
609 frontend
*fe
= (frontend
*)data
;
611 gtk_widget_destroy(fe
->cfgbox
);
614 static int editbox_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
617 * GtkEntry has a nasty habit of eating the Return key, which
618 * is unhelpful since it doesn't actually _do_ anything with it
619 * (it calls gtk_widget_activate, but our edit boxes never need
620 * activating). So I catch Return before GtkEntry sees it, and
621 * pass it straight on to the parent widget. Effect: hitting
622 * Return in an edit box will now activate the default button
623 * in the dialog just like it will everywhere else.
625 if (event
->keyval
== GDK_Return
&& widget
->parent
!= NULL
) {
627 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
628 gtk_signal_emit_by_name(GTK_OBJECT(widget
->parent
), "key_press_event",
635 static void editbox_changed(GtkEditable
*ed
, gpointer data
)
637 config_item
*i
= (config_item
*)data
;
640 i
->sval
= dupstr(gtk_entry_get_text(GTK_ENTRY(ed
)));
643 static void button_toggled(GtkToggleButton
*tb
, gpointer data
)
645 config_item
*i
= (config_item
*)data
;
647 i
->ival
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb
));
650 static void droplist_sel(GtkMenuItem
*item
, gpointer data
)
652 config_item
*i
= (config_item
*)data
;
654 i
->ival
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
),
658 static int get_config(frontend
*fe
, int which
)
660 GtkWidget
*w
, *table
, *cancel
;
665 fe
->cfg
= midend_get_config(fe
->me
, which
, &title
);
666 fe
->cfg_which
= which
;
669 fe
->cfgbox
= gtk_dialog_new();
670 gtk_window_set_title(GTK_WINDOW(fe
->cfgbox
), title
);
673 w
= gtk_button_new_with_label("OK");
674 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
677 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
678 gtk_window_set_default(GTK_WINDOW(fe
->cfgbox
), w
);
679 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
680 GTK_SIGNAL_FUNC(config_ok_button_clicked
), fe
);
682 w
= gtk_button_new_with_label("Cancel");
683 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->action_area
),
686 gtk_signal_connect(GTK_OBJECT(w
), "clicked",
687 GTK_SIGNAL_FUNC(config_cancel_button_clicked
), fe
);
690 table
= gtk_table_new(1, 2, FALSE
);
692 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe
->cfgbox
)->vbox
),
693 table
, FALSE
, FALSE
, 0);
694 gtk_widget_show(table
);
696 for (i
= fe
->cfg
; i
->type
!= C_END
; i
++) {
697 gtk_table_resize(GTK_TABLE(table
), y
+1, 2);
702 * Edit box with a label beside it.
705 w
= gtk_label_new(i
->name
);
706 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
707 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
708 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
709 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
714 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
715 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
716 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
718 gtk_entry_set_text(GTK_ENTRY(w
), i
->sval
);
719 gtk_signal_connect(GTK_OBJECT(w
), "changed",
720 GTK_SIGNAL_FUNC(editbox_changed
), i
);
721 gtk_signal_connect(GTK_OBJECT(w
), "key_press_event",
722 GTK_SIGNAL_FUNC(editbox_key
), NULL
);
731 w
= gtk_check_button_new_with_label(i
->name
);
732 gtk_signal_connect(GTK_OBJECT(w
), "toggled",
733 GTK_SIGNAL_FUNC(button_toggled
), i
);
734 gtk_table_attach(GTK_TABLE(table
), w
, 0, 2, y
, y
+1,
735 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
736 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
738 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w
), i
->ival
);
744 * Drop-down list (GtkOptionMenu).
747 w
= gtk_label_new(i
->name
);
748 gtk_misc_set_alignment(GTK_MISC(w
), 0.0, 0.5);
749 gtk_table_attach(GTK_TABLE(table
), w
, 0, 1, y
, y
+1,
750 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
751 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
755 w
= gtk_option_menu_new();
756 gtk_table_attach(GTK_TABLE(table
), w
, 1, 2, y
, y
+1,
757 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
758 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
766 GtkWidget
*menu
= gtk_menu_new();
768 gtk_option_menu_set_menu(GTK_OPTION_MENU(w
), menu
);
776 while (*q
&& *q
!= c
)
779 name
= snewn(q
-p
+1, char);
780 strncpy(name
, p
, q
-p
);
783 if (*q
) q
++; /* eat delimiter */
785 menuitem
= gtk_menu_item_new_with_label(name
);
786 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
787 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
788 GINT_TO_POINTER(val
));
789 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
790 GTK_SIGNAL_FUNC(droplist_sel
), i
);
791 gtk_widget_show(menuitem
);
798 gtk_option_menu_set_history(GTK_OPTION_MENU(w
), i
->ival
);
807 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "destroy",
808 GTK_SIGNAL_FUNC(window_destroy
), NULL
);
809 gtk_signal_connect(GTK_OBJECT(fe
->cfgbox
), "key_press_event",
810 GTK_SIGNAL_FUNC(win_key_press
), cancel
);
811 gtk_window_set_modal(GTK_WINDOW(fe
->cfgbox
), TRUE
);
812 gtk_window_set_transient_for(GTK_WINDOW(fe
->cfgbox
),
813 GTK_WINDOW(fe
->window
));
814 /* set_transient_window_pos(fe->window, fe->cfgbox); */
815 gtk_widget_show(fe
->cfgbox
);
823 static void menu_key_event(GtkMenuItem
*menuitem
, gpointer data
)
825 frontend
*fe
= (frontend
*)data
;
826 int key
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
828 if (!midend_process_key(fe
->me
, 0, 0, key
))
829 gtk_widget_destroy(fe
->window
);
832 static void get_size(frontend
*fe
, int *px
, int *py
)
837 * Currently I don't want to make the GTK port scale large
838 * puzzles to fit on the screen. This is because X does permit
839 * extremely large windows and many window managers provide a
840 * means of navigating round them, and the users I consulted
841 * before deciding said that they'd rather have enormous puzzle
842 * windows spanning multiple screen pages than have them
843 * shrunk. I could change my mind later or introduce
844 * configurability; this would be the place to do so, by
845 * replacing the initial values of x and y with the screen
850 midend_size(fe
->me
, &x
, &y
, FALSE
);
855 static void menu_preset_event(GtkMenuItem
*menuitem
, gpointer data
)
857 frontend
*fe
= (frontend
*)data
;
858 game_params
*params
=
859 (game_params
*)gtk_object_get_data(GTK_OBJECT(menuitem
), "user-data");
862 midend_set_params(fe
->me
, params
);
863 midend_new_game(fe
->me
);
864 get_size(fe
, &x
, &y
);
867 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
868 gtk_window_resize(GTK_WINDOW(fe
->window
), 1, 1);
871 GdkAtom compound_text_atom
, utf8_string_atom
;
872 int paste_initialised
= FALSE
;
876 unsigned char empty
[] = { 0 };
878 if (paste_initialised
)
881 if (!compound_text_atom
)
882 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
883 if (!utf8_string_atom
)
884 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
887 * Ensure that all the cut buffers exist - according to the
888 * ICCCM, we must do this before we start using cut buffers.
890 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
891 XA_CUT_BUFFER0
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
892 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
893 XA_CUT_BUFFER1
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
894 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
895 XA_CUT_BUFFER2
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
896 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
897 XA_CUT_BUFFER3
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
898 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
899 XA_CUT_BUFFER4
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
900 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
901 XA_CUT_BUFFER5
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
902 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
903 XA_CUT_BUFFER6
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
904 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
905 XA_CUT_BUFFER7
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
908 /* Store data in a cut-buffer. */
909 void store_cutbuffer(char *ptr
, int len
)
911 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
912 XRotateBuffers(GDK_DISPLAY(), 1);
913 XStoreBytes(GDK_DISPLAY(), ptr
, len
);
916 void write_clip(frontend
*fe
, char *data
)
921 sfree(fe
->paste_data
);
924 * For this simple application we can safely assume that the
925 * data passed to this function is pure ASCII, which means we
926 * can return precisely the same stuff for types STRING,
927 * COMPOUND_TEXT or UTF8_STRING.
930 fe
->paste_data
= data
;
931 fe
->paste_data_len
= strlen(data
);
933 store_cutbuffer(fe
->paste_data
, fe
->paste_data_len
);
935 if (gtk_selection_owner_set(fe
->area
, GDK_SELECTION_PRIMARY
,
937 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
938 GDK_SELECTION_TYPE_STRING
, 1);
939 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
940 compound_text_atom
, 1);
941 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
942 utf8_string_atom
, 1);
946 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
947 guint info
, guint time_stamp
, gpointer data
)
949 frontend
*fe
= (frontend
*)data
;
950 gtk_selection_data_set(seldata
, seldata
->target
, 8,
951 fe
->paste_data
, fe
->paste_data_len
);
954 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
957 frontend
*fe
= (frontend
*)data
;
960 sfree(fe
->paste_data
);
961 fe
->paste_data
= NULL
;
962 fe
->paste_data_len
= 0;
966 static void menu_copy_event(GtkMenuItem
*menuitem
, gpointer data
)
968 frontend
*fe
= (frontend
*)data
;
971 text
= midend_text_format(fe
->me
);
974 write_clip(fe
, text
);
980 static void menu_solve_event(GtkMenuItem
*menuitem
, gpointer data
)
982 frontend
*fe
= (frontend
*)data
;
985 msg
= midend_solve(fe
->me
);
988 error_box(fe
->window
, msg
);
991 static void menu_restart_event(GtkMenuItem
*menuitem
, gpointer data
)
993 frontend
*fe
= (frontend
*)data
;
995 midend_restart_game(fe
->me
);
998 static void menu_config_event(GtkMenuItem
*menuitem
, gpointer data
)
1000 frontend
*fe
= (frontend
*)data
;
1001 int which
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1005 if (!get_config(fe
, which
))
1008 midend_new_game(fe
->me
);
1009 get_size(fe
, &x
, &y
);
1012 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1013 gtk_window_resize(GTK_WINDOW(fe
->window
), 1, 1);
1016 static void menu_about_event(GtkMenuItem
*menuitem
, gpointer data
)
1018 frontend
*fe
= (frontend
*)data
;
1022 sprintf(titlebuf
, "About %.200s", thegame
.name
);
1025 "from Simon Tatham's Portable Puzzle Collection\n\n"
1026 "%.500s", thegame
.name
, ver
);
1028 message_box(fe
->window
, titlebuf
, textbuf
, TRUE
);
1031 static GtkWidget
*add_menu_item_with_key(frontend
*fe
, GtkContainer
*cont
,
1032 char *text
, int key
)
1034 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
1035 gtk_container_add(cont
, menuitem
);
1036 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1037 GINT_TO_POINTER(key
));
1038 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1039 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
1040 gtk_widget_show(menuitem
);
1044 static void add_menu_separator(GtkContainer
*cont
)
1046 GtkWidget
*menuitem
= gtk_menu_item_new();
1047 gtk_container_add(cont
, menuitem
);
1048 gtk_widget_show(menuitem
);
1051 static frontend
*new_window(char *game_id
, char **error
)
1055 GtkWidget
*menubar
, *menu
, *menuitem
;
1058 fe
= snew(frontend
);
1060 fe
->timer_active
= FALSE
;
1063 fe
->me
= midend_new(fe
, &thegame
);
1066 *error
= midend_game_id(fe
->me
, game_id
);
1068 midend_free(fe
->me
);
1073 midend_new_game(fe
->me
);
1075 fe
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1076 gtk_window_set_title(GTK_WINDOW(fe
->window
), thegame
.name
);
1078 gtk_window_set_resizable(GTK_WINDOW(fe
->window
), FALSE
);
1080 gtk_window_set_policy(GTK_WINDOW(fe
->window
), FALSE
, FALSE
, TRUE
);
1082 vbox
= GTK_BOX(gtk_vbox_new(FALSE
, 0));
1083 gtk_container_add(GTK_CONTAINER(fe
->window
), GTK_WIDGET(vbox
));
1084 gtk_widget_show(GTK_WIDGET(vbox
));
1086 menubar
= gtk_menu_bar_new();
1087 gtk_box_pack_start(vbox
, menubar
, FALSE
, FALSE
, 0);
1088 gtk_widget_show(menubar
);
1090 menuitem
= gtk_menu_item_new_with_label("Game");
1091 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1092 gtk_widget_show(menuitem
);
1094 menu
= gtk_menu_new();
1095 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1097 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "New", 'n');
1099 menuitem
= gtk_menu_item_new_with_label("Restart");
1100 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1101 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1102 GTK_SIGNAL_FUNC(menu_restart_event
), fe
);
1103 gtk_widget_show(menuitem
);
1105 menuitem
= gtk_menu_item_new_with_label("Specific...");
1106 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1107 GINT_TO_POINTER(CFG_DESC
));
1108 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1109 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1110 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1111 gtk_widget_show(menuitem
);
1113 menuitem
= gtk_menu_item_new_with_label("Random Seed...");
1114 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1115 GINT_TO_POINTER(CFG_SEED
));
1116 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1117 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1118 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1119 gtk_widget_show(menuitem
);
1121 if ((n
= midend_num_presets(fe
->me
)) > 0 || thegame
.can_configure
) {
1125 menuitem
= gtk_menu_item_new_with_label("Type");
1126 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1127 gtk_widget_show(menuitem
);
1129 submenu
= gtk_menu_new();
1130 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), submenu
);
1132 for (i
= 0; i
< n
; i
++) {
1134 game_params
*params
;
1136 midend_fetch_preset(fe
->me
, i
, &name
, ¶ms
);
1138 menuitem
= gtk_menu_item_new_with_label(name
);
1139 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1140 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", params
);
1141 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1142 GTK_SIGNAL_FUNC(menu_preset_event
), fe
);
1143 gtk_widget_show(menuitem
);
1146 if (thegame
.can_configure
) {
1147 menuitem
= gtk_menu_item_new_with_label("Custom...");
1148 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1149 GPOINTER_TO_INT(CFG_SETTINGS
));
1150 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1151 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1152 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1153 gtk_widget_show(menuitem
);
1157 add_menu_separator(GTK_CONTAINER(menu
));
1158 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Undo", 'u');
1159 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Redo", '\x12');
1160 if (thegame
.can_format_as_text
) {
1161 add_menu_separator(GTK_CONTAINER(menu
));
1162 menuitem
= gtk_menu_item_new_with_label("Copy");
1163 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1164 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1165 GTK_SIGNAL_FUNC(menu_copy_event
), fe
);
1166 gtk_widget_show(menuitem
);
1168 if (thegame
.can_solve
) {
1169 add_menu_separator(GTK_CONTAINER(menu
));
1170 menuitem
= gtk_menu_item_new_with_label("Solve");
1171 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1172 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1173 GTK_SIGNAL_FUNC(menu_solve_event
), fe
);
1174 gtk_widget_show(menuitem
);
1176 add_menu_separator(GTK_CONTAINER(menu
));
1177 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Exit", 'q');
1179 menuitem
= gtk_menu_item_new_with_label("Help");
1180 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1181 gtk_widget_show(menuitem
);
1183 menu
= gtk_menu_new();
1184 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1186 menuitem
= gtk_menu_item_new_with_label("About");
1187 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1188 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1189 GTK_SIGNAL_FUNC(menu_about_event
), fe
);
1190 gtk_widget_show(menuitem
);
1197 fe
->colmap
= gdk_colormap_get_system();
1198 colours
= midend_colours(fe
->me
, &ncolours
);
1199 fe
->ncolours
= ncolours
;
1200 fe
->colours
= snewn(ncolours
, GdkColor
);
1201 for (i
= 0; i
< ncolours
; i
++) {
1202 fe
->colours
[i
].red
= colours
[i
*3] * 0xFFFF;
1203 fe
->colours
[i
].green
= colours
[i
*3+1] * 0xFFFF;
1204 fe
->colours
[i
].blue
= colours
[i
*3+2] * 0xFFFF;
1206 success
= snewn(ncolours
, gboolean
);
1207 gdk_colormap_alloc_colors(fe
->colmap
, fe
->colours
, ncolours
,
1208 FALSE
, FALSE
, success
);
1209 for (i
= 0; i
< ncolours
; i
++) {
1211 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
1212 i
, fe
->colours
[i
].red
>> 8,
1213 fe
->colours
[i
].green
>> 8,
1214 fe
->colours
[i
].blue
>> 8);
1218 if (midend_wants_statusbar(fe
->me
)) {
1219 GtkWidget
*viewport
;
1222 viewport
= gtk_viewport_new(NULL
, NULL
);
1223 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport
), GTK_SHADOW_NONE
);
1224 fe
->statusbar
= gtk_statusbar_new();
1225 gtk_container_add(GTK_CONTAINER(viewport
), fe
->statusbar
);
1226 gtk_widget_show(viewport
);
1227 gtk_box_pack_end(vbox
, viewport
, FALSE
, FALSE
, 0);
1228 gtk_widget_show(fe
->statusbar
);
1229 fe
->statusctx
= gtk_statusbar_get_context_id
1230 (GTK_STATUSBAR(fe
->statusbar
), "game");
1231 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
,
1233 gtk_widget_size_request(fe
->statusbar
, &req
);
1235 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
1237 gtk_widget_set_usize(viewport
, -1, req
.height
);
1239 fe
->statusbar
= NULL
;
1241 fe
->area
= gtk_drawing_area_new();
1242 get_size(fe
, &x
, &y
);
1243 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1247 gtk_box_pack_end(vbox
, fe
->area
, TRUE
, TRUE
, 0);
1251 fe
->nfonts
= fe
->fontsize
= 0;
1253 fe
->laststatus
= NULL
;
1255 fe
->paste_data
= NULL
;
1256 fe
->paste_data_len
= 0;
1258 gtk_signal_connect(GTK_OBJECT(fe
->window
), "destroy",
1259 GTK_SIGNAL_FUNC(destroy
), fe
);
1260 gtk_signal_connect(GTK_OBJECT(fe
->window
), "key_press_event",
1261 GTK_SIGNAL_FUNC(key_event
), fe
);
1262 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_press_event",
1263 GTK_SIGNAL_FUNC(button_event
), fe
);
1264 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_release_event",
1265 GTK_SIGNAL_FUNC(button_event
), fe
);
1266 gtk_signal_connect(GTK_OBJECT(fe
->area
), "motion_notify_event",
1267 GTK_SIGNAL_FUNC(motion_event
), fe
);
1268 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_get",
1269 GTK_SIGNAL_FUNC(selection_get
), fe
);
1270 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_clear_event",
1271 GTK_SIGNAL_FUNC(selection_clear
), fe
);
1272 gtk_signal_connect(GTK_OBJECT(fe
->area
), "expose_event",
1273 GTK_SIGNAL_FUNC(expose_area
), fe
);
1274 gtk_signal_connect(GTK_OBJECT(fe
->window
), "map_event",
1275 GTK_SIGNAL_FUNC(map_window
), fe
);
1276 gtk_signal_connect(GTK_OBJECT(fe
->area
), "configure_event",
1277 GTK_SIGNAL_FUNC(configure_area
), fe
);
1279 gtk_widget_add_events(GTK_WIDGET(fe
->area
),
1280 GDK_BUTTON_PRESS_MASK
|
1281 GDK_BUTTON_RELEASE_MASK
|
1282 GDK_BUTTON_MOTION_MASK
);
1284 gtk_widget_show(fe
->area
);
1285 gtk_widget_show(fe
->window
);
1287 gdk_window_set_background(fe
->area
->window
, &fe
->colours
[0]);
1288 gdk_window_set_background(fe
->window
->window
, &fe
->colours
[0]);
1293 int main(int argc
, char **argv
)
1295 char *pname
= argv
[0];
1298 if (argc
> 1 && !strcmp(argv
[1], "--version")) {
1299 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
1305 * Special standalone mode for generating puzzle IDs on the
1306 * command line. Useful for generating puzzles to be printed
1307 * out and solved offline (for puzzles where that even makes
1308 * sense - Solo, for example, is a lot more pencil-and-paper
1309 * friendly than Net!)
1313 * <puzzle-name> --generate [<n> [<params>]]
1315 * <n>, if present, is the number of puzzle IDs to generate.
1316 * <params>, if present, is the same type of parameter string
1317 * you would pass to the puzzle when running it in GUI mode,
1318 * including optional extras such as the expansion factor in
1319 * Rectangles and the difficulty level in Solo.
1321 * If you specify <params>, you must also specify <n> (although
1322 * you may specify it to be 1). Sorry; that was the
1323 * simplest-to-parse command-line syntax I came up with.
1325 if (argc
> 1 && !strcmp(argv
[1], "--generate")) {
1327 char *params
= NULL
, *seed
= NULL
;
1337 par
= thegame
.default_params();
1339 if ( (seed
= strchr(params
, '#')) != NULL
)
1341 thegame
.decode_params(par
, params
);
1343 if ((error
= thegame
.validate_params(par
)) != NULL
) {
1344 fprintf(stderr
, "%s: %s\n", pname
, error
);
1347 parstr
= thegame
.encode_params(par
, FALSE
);
1354 seedlen
= strlen(seed
);
1356 get_random_seed(&seeddata
, &seedlen
);
1358 rs
= random_init(seeddata
, seedlen
);
1362 game_aux_info
*aux
= NULL
;
1363 char *desc
= thegame
.new_desc(par
, rs
, &aux
, FALSE
);
1364 printf("%s:%s\n", parstr
, desc
);
1367 thegame
.free_aux_info(aux
);
1373 gtk_init(&argc
, &argv
);
1375 if (!new_window(argc
> 1 ? argv
[1] : NULL
, &error
)) {
1376 fprintf(stderr
, "%s: %s\n", pname
, error
);