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 #if !GTK_CHECK_VERSION(2,0,0)
856 #define gtk_window_resize(win, x, y) \
857 gdk_window_resize(GTK_WIDGET(win)->window, x, y)
860 static void menu_preset_event(GtkMenuItem
*menuitem
, gpointer data
)
862 frontend
*fe
= (frontend
*)data
;
863 game_params
*params
=
864 (game_params
*)gtk_object_get_data(GTK_OBJECT(menuitem
), "user-data");
867 midend_set_params(fe
->me
, params
);
868 midend_new_game(fe
->me
);
869 get_size(fe
, &x
, &y
);
872 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
873 gtk_window_resize(GTK_WINDOW(fe
->window
), 1, 1);
876 GdkAtom compound_text_atom
, utf8_string_atom
;
877 int paste_initialised
= FALSE
;
881 unsigned char empty
[] = { 0 };
883 if (paste_initialised
)
886 if (!compound_text_atom
)
887 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
888 if (!utf8_string_atom
)
889 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
892 * Ensure that all the cut buffers exist - according to the
893 * ICCCM, we must do this before we start using cut buffers.
895 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
896 XA_CUT_BUFFER0
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
897 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
898 XA_CUT_BUFFER1
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
899 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
900 XA_CUT_BUFFER2
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
901 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
902 XA_CUT_BUFFER3
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
903 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
904 XA_CUT_BUFFER4
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
905 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
906 XA_CUT_BUFFER5
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
907 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
908 XA_CUT_BUFFER6
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
909 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
910 XA_CUT_BUFFER7
, XA_STRING
, 8, PropModeAppend
, empty
, 0);
913 /* Store data in a cut-buffer. */
914 void store_cutbuffer(char *ptr
, int len
)
916 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
917 XRotateBuffers(GDK_DISPLAY(), 1);
918 XStoreBytes(GDK_DISPLAY(), ptr
, len
);
921 void write_clip(frontend
*fe
, char *data
)
926 sfree(fe
->paste_data
);
929 * For this simple application we can safely assume that the
930 * data passed to this function is pure ASCII, which means we
931 * can return precisely the same stuff for types STRING,
932 * COMPOUND_TEXT or UTF8_STRING.
935 fe
->paste_data
= data
;
936 fe
->paste_data_len
= strlen(data
);
938 store_cutbuffer(fe
->paste_data
, fe
->paste_data_len
);
940 if (gtk_selection_owner_set(fe
->area
, GDK_SELECTION_PRIMARY
,
942 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
943 GDK_SELECTION_TYPE_STRING
, 1);
944 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
945 compound_text_atom
, 1);
946 gtk_selection_add_target(fe
->area
, GDK_SELECTION_PRIMARY
,
947 utf8_string_atom
, 1);
951 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
952 guint info
, guint time_stamp
, gpointer data
)
954 frontend
*fe
= (frontend
*)data
;
955 gtk_selection_data_set(seldata
, seldata
->target
, 8,
956 fe
->paste_data
, fe
->paste_data_len
);
959 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
962 frontend
*fe
= (frontend
*)data
;
965 sfree(fe
->paste_data
);
966 fe
->paste_data
= NULL
;
967 fe
->paste_data_len
= 0;
971 static void menu_copy_event(GtkMenuItem
*menuitem
, gpointer data
)
973 frontend
*fe
= (frontend
*)data
;
976 text
= midend_text_format(fe
->me
);
979 write_clip(fe
, text
);
985 static void menu_solve_event(GtkMenuItem
*menuitem
, gpointer data
)
987 frontend
*fe
= (frontend
*)data
;
990 msg
= midend_solve(fe
->me
);
993 error_box(fe
->window
, msg
);
996 static void menu_restart_event(GtkMenuItem
*menuitem
, gpointer data
)
998 frontend
*fe
= (frontend
*)data
;
1000 midend_restart_game(fe
->me
);
1003 static void menu_config_event(GtkMenuItem
*menuitem
, gpointer data
)
1005 frontend
*fe
= (frontend
*)data
;
1006 int which
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem
),
1010 if (!get_config(fe
, which
))
1013 midend_new_game(fe
->me
);
1014 get_size(fe
, &x
, &y
);
1017 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1018 gtk_window_resize(GTK_WINDOW(fe
->window
), 1, 1);
1021 static void menu_about_event(GtkMenuItem
*menuitem
, gpointer data
)
1023 frontend
*fe
= (frontend
*)data
;
1027 sprintf(titlebuf
, "About %.200s", thegame
.name
);
1030 "from Simon Tatham's Portable Puzzle Collection\n\n"
1031 "%.500s", thegame
.name
, ver
);
1033 message_box(fe
->window
, titlebuf
, textbuf
, TRUE
);
1036 static GtkWidget
*add_menu_item_with_key(frontend
*fe
, GtkContainer
*cont
,
1037 char *text
, int key
)
1039 GtkWidget
*menuitem
= gtk_menu_item_new_with_label(text
);
1040 gtk_container_add(cont
, menuitem
);
1041 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1042 GINT_TO_POINTER(key
));
1043 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1044 GTK_SIGNAL_FUNC(menu_key_event
), fe
);
1045 gtk_widget_show(menuitem
);
1049 static void add_menu_separator(GtkContainer
*cont
)
1051 GtkWidget
*menuitem
= gtk_menu_item_new();
1052 gtk_container_add(cont
, menuitem
);
1053 gtk_widget_show(menuitem
);
1056 static frontend
*new_window(char *game_id
, char **error
)
1060 GtkWidget
*menubar
, *menu
, *menuitem
;
1063 fe
= snew(frontend
);
1065 fe
->timer_active
= FALSE
;
1068 fe
->me
= midend_new(fe
, &thegame
);
1071 *error
= midend_game_id(fe
->me
, game_id
);
1073 midend_free(fe
->me
);
1078 midend_new_game(fe
->me
);
1080 fe
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1081 gtk_window_set_title(GTK_WINDOW(fe
->window
), thegame
.name
);
1083 gtk_window_set_resizable(GTK_WINDOW(fe
->window
), FALSE
);
1085 gtk_window_set_policy(GTK_WINDOW(fe
->window
), FALSE
, FALSE
, TRUE
);
1087 vbox
= GTK_BOX(gtk_vbox_new(FALSE
, 0));
1088 gtk_container_add(GTK_CONTAINER(fe
->window
), GTK_WIDGET(vbox
));
1089 gtk_widget_show(GTK_WIDGET(vbox
));
1091 menubar
= gtk_menu_bar_new();
1092 gtk_box_pack_start(vbox
, menubar
, FALSE
, FALSE
, 0);
1093 gtk_widget_show(menubar
);
1095 menuitem
= gtk_menu_item_new_with_label("Game");
1096 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1097 gtk_widget_show(menuitem
);
1099 menu
= gtk_menu_new();
1100 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1102 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "New", 'n');
1104 menuitem
= gtk_menu_item_new_with_label("Restart");
1105 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1106 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1107 GTK_SIGNAL_FUNC(menu_restart_event
), fe
);
1108 gtk_widget_show(menuitem
);
1110 menuitem
= gtk_menu_item_new_with_label("Specific...");
1111 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1112 GINT_TO_POINTER(CFG_DESC
));
1113 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1114 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1115 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1116 gtk_widget_show(menuitem
);
1118 menuitem
= gtk_menu_item_new_with_label("Random Seed...");
1119 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1120 GINT_TO_POINTER(CFG_SEED
));
1121 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1122 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1123 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1124 gtk_widget_show(menuitem
);
1126 if ((n
= midend_num_presets(fe
->me
)) > 0 || thegame
.can_configure
) {
1130 menuitem
= gtk_menu_item_new_with_label("Type");
1131 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1132 gtk_widget_show(menuitem
);
1134 submenu
= gtk_menu_new();
1135 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), submenu
);
1137 for (i
= 0; i
< n
; i
++) {
1139 game_params
*params
;
1141 midend_fetch_preset(fe
->me
, i
, &name
, ¶ms
);
1143 menuitem
= gtk_menu_item_new_with_label(name
);
1144 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1145 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data", params
);
1146 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1147 GTK_SIGNAL_FUNC(menu_preset_event
), fe
);
1148 gtk_widget_show(menuitem
);
1151 if (thegame
.can_configure
) {
1152 menuitem
= gtk_menu_item_new_with_label("Custom...");
1153 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
1154 GPOINTER_TO_INT(CFG_SETTINGS
));
1155 gtk_container_add(GTK_CONTAINER(submenu
), menuitem
);
1156 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1157 GTK_SIGNAL_FUNC(menu_config_event
), fe
);
1158 gtk_widget_show(menuitem
);
1162 add_menu_separator(GTK_CONTAINER(menu
));
1163 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Undo", 'u');
1164 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Redo", '\x12');
1165 if (thegame
.can_format_as_text
) {
1166 add_menu_separator(GTK_CONTAINER(menu
));
1167 menuitem
= gtk_menu_item_new_with_label("Copy");
1168 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1169 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1170 GTK_SIGNAL_FUNC(menu_copy_event
), fe
);
1171 gtk_widget_show(menuitem
);
1173 if (thegame
.can_solve
) {
1174 add_menu_separator(GTK_CONTAINER(menu
));
1175 menuitem
= gtk_menu_item_new_with_label("Solve");
1176 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1177 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1178 GTK_SIGNAL_FUNC(menu_solve_event
), fe
);
1179 gtk_widget_show(menuitem
);
1181 add_menu_separator(GTK_CONTAINER(menu
));
1182 add_menu_item_with_key(fe
, GTK_CONTAINER(menu
), "Exit", 'q');
1184 menuitem
= gtk_menu_item_new_with_label("Help");
1185 gtk_container_add(GTK_CONTAINER(menubar
), menuitem
);
1186 gtk_widget_show(menuitem
);
1188 menu
= gtk_menu_new();
1189 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), menu
);
1191 menuitem
= gtk_menu_item_new_with_label("About");
1192 gtk_container_add(GTK_CONTAINER(menu
), menuitem
);
1193 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
1194 GTK_SIGNAL_FUNC(menu_about_event
), fe
);
1195 gtk_widget_show(menuitem
);
1202 fe
->colmap
= gdk_colormap_get_system();
1203 colours
= midend_colours(fe
->me
, &ncolours
);
1204 fe
->ncolours
= ncolours
;
1205 fe
->colours
= snewn(ncolours
, GdkColor
);
1206 for (i
= 0; i
< ncolours
; i
++) {
1207 fe
->colours
[i
].red
= colours
[i
*3] * 0xFFFF;
1208 fe
->colours
[i
].green
= colours
[i
*3+1] * 0xFFFF;
1209 fe
->colours
[i
].blue
= colours
[i
*3+2] * 0xFFFF;
1211 success
= snewn(ncolours
, gboolean
);
1212 gdk_colormap_alloc_colors(fe
->colmap
, fe
->colours
, ncolours
,
1213 FALSE
, FALSE
, success
);
1214 for (i
= 0; i
< ncolours
; i
++) {
1216 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
1217 i
, fe
->colours
[i
].red
>> 8,
1218 fe
->colours
[i
].green
>> 8,
1219 fe
->colours
[i
].blue
>> 8);
1223 if (midend_wants_statusbar(fe
->me
)) {
1224 GtkWidget
*viewport
;
1227 viewport
= gtk_viewport_new(NULL
, NULL
);
1228 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport
), GTK_SHADOW_NONE
);
1229 fe
->statusbar
= gtk_statusbar_new();
1230 gtk_container_add(GTK_CONTAINER(viewport
), fe
->statusbar
);
1231 gtk_widget_show(viewport
);
1232 gtk_box_pack_end(vbox
, viewport
, FALSE
, FALSE
, 0);
1233 gtk_widget_show(fe
->statusbar
);
1234 fe
->statusctx
= gtk_statusbar_get_context_id
1235 (GTK_STATUSBAR(fe
->statusbar
), "game");
1236 gtk_statusbar_push(GTK_STATUSBAR(fe
->statusbar
), fe
->statusctx
,
1238 gtk_widget_size_request(fe
->statusbar
, &req
);
1240 /* For GTK 2.0, should we be using gtk_widget_set_size_request? */
1242 gtk_widget_set_usize(viewport
, -1, req
.height
);
1244 fe
->statusbar
= NULL
;
1246 fe
->area
= gtk_drawing_area_new();
1247 get_size(fe
, &x
, &y
);
1248 gtk_drawing_area_size(GTK_DRAWING_AREA(fe
->area
), x
, y
);
1252 gtk_box_pack_end(vbox
, fe
->area
, TRUE
, TRUE
, 0);
1256 fe
->nfonts
= fe
->fontsize
= 0;
1258 fe
->laststatus
= NULL
;
1260 fe
->paste_data
= NULL
;
1261 fe
->paste_data_len
= 0;
1263 gtk_signal_connect(GTK_OBJECT(fe
->window
), "destroy",
1264 GTK_SIGNAL_FUNC(destroy
), fe
);
1265 gtk_signal_connect(GTK_OBJECT(fe
->window
), "key_press_event",
1266 GTK_SIGNAL_FUNC(key_event
), fe
);
1267 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_press_event",
1268 GTK_SIGNAL_FUNC(button_event
), fe
);
1269 gtk_signal_connect(GTK_OBJECT(fe
->area
), "button_release_event",
1270 GTK_SIGNAL_FUNC(button_event
), fe
);
1271 gtk_signal_connect(GTK_OBJECT(fe
->area
), "motion_notify_event",
1272 GTK_SIGNAL_FUNC(motion_event
), fe
);
1273 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_get",
1274 GTK_SIGNAL_FUNC(selection_get
), fe
);
1275 gtk_signal_connect(GTK_OBJECT(fe
->area
), "selection_clear_event",
1276 GTK_SIGNAL_FUNC(selection_clear
), fe
);
1277 gtk_signal_connect(GTK_OBJECT(fe
->area
), "expose_event",
1278 GTK_SIGNAL_FUNC(expose_area
), fe
);
1279 gtk_signal_connect(GTK_OBJECT(fe
->window
), "map_event",
1280 GTK_SIGNAL_FUNC(map_window
), fe
);
1281 gtk_signal_connect(GTK_OBJECT(fe
->area
), "configure_event",
1282 GTK_SIGNAL_FUNC(configure_area
), fe
);
1284 gtk_widget_add_events(GTK_WIDGET(fe
->area
),
1285 GDK_BUTTON_PRESS_MASK
|
1286 GDK_BUTTON_RELEASE_MASK
|
1287 GDK_BUTTON_MOTION_MASK
);
1289 gtk_widget_show(fe
->area
);
1290 gtk_widget_show(fe
->window
);
1292 gdk_window_set_background(fe
->area
->window
, &fe
->colours
[0]);
1293 gdk_window_set_background(fe
->window
->window
, &fe
->colours
[0]);
1298 int main(int argc
, char **argv
)
1300 char *pname
= argv
[0];
1303 if (argc
> 1 && !strcmp(argv
[1], "--version")) {
1304 printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
1310 * Special standalone mode for generating puzzle IDs on the
1311 * command line. Useful for generating puzzles to be printed
1312 * out and solved offline (for puzzles where that even makes
1313 * sense - Solo, for example, is a lot more pencil-and-paper
1314 * friendly than Net!)
1318 * <puzzle-name> --generate [<n> [<params>]]
1320 * <n>, if present, is the number of puzzle IDs to generate.
1321 * <params>, if present, is the same type of parameter string
1322 * you would pass to the puzzle when running it in GUI mode,
1323 * including optional extras such as the expansion factor in
1324 * Rectangles and the difficulty level in Solo.
1326 * If you specify <params>, you must also specify <n> (although
1327 * you may specify it to be 1). Sorry; that was the
1328 * simplest-to-parse command-line syntax I came up with.
1330 if (argc
> 1 && !strcmp(argv
[1], "--generate")) {
1332 char *params
= NULL
, *seed
= NULL
;
1342 par
= thegame
.default_params();
1344 if ( (seed
= strchr(params
, '#')) != NULL
)
1346 thegame
.decode_params(par
, params
);
1348 if ((error
= thegame
.validate_params(par
)) != NULL
) {
1349 fprintf(stderr
, "%s: %s\n", pname
, error
);
1352 parstr
= thegame
.encode_params(par
, FALSE
);
1359 seedlen
= strlen(seed
);
1361 get_random_seed(&seeddata
, &seedlen
);
1363 rs
= random_init(seeddata
, seedlen
);
1367 game_aux_info
*aux
= NULL
;
1368 char *desc
= thegame
.new_desc(par
, rs
, &aux
, FALSE
);
1369 printf("%s:%s\n", parstr
, desc
);
1372 thegame
.free_aux_info(aux
);
1378 gtk_init(&argc
, &argv
);
1380 if (!new_window(argc
> 1 ? argv
[1] : NULL
, &error
)) {
1381 fprintf(stderr
, "%s: %s\n", pname
, error
);