720a8fb7 |
1 | /* |
2 | * gtk.c: GTK front end for my puzzle collection. |
3 | */ |
4 | |
5 | #include <stdio.h> |
fd1a1a2b |
6 | #include <assert.h> |
720a8fb7 |
7 | #include <stdlib.h> |
2ef96bd6 |
8 | #include <time.h> |
720a8fb7 |
9 | #include <stdarg.h> |
c8230524 |
10 | #include <string.h> |
720a8fb7 |
11 | |
8c1fd974 |
12 | #include <sys/time.h> |
13 | |
83680571 |
14 | #include <gtk/gtk.h> |
1482ee76 |
15 | #include <gdk/gdkkeysyms.h> |
83680571 |
16 | |
a9668451 |
17 | #include <gdk/gdkx.h> |
18 | #include <X11/Xlib.h> |
9b4b03d3 |
19 | #include <X11/Xutil.h> |
20 | #include <X11/Xatom.h> |
a9668451 |
21 | |
720a8fb7 |
22 | #include "puzzles.h" |
23 | |
83680571 |
24 | /* ---------------------------------------------------------------------- |
25 | * Error reporting functions used elsewhere. |
26 | */ |
27 | |
720a8fb7 |
28 | void fatal(char *fmt, ...) |
29 | { |
30 | va_list ap; |
31 | |
32 | fprintf(stderr, "fatal error: "); |
33 | |
34 | va_start(ap, fmt); |
35 | vfprintf(stderr, fmt, ap); |
36 | va_end(ap); |
37 | |
38 | fprintf(stderr, "\n"); |
39 | exit(1); |
40 | } |
83680571 |
41 | |
42 | /* ---------------------------------------------------------------------- |
43 | * GTK front end to puzzles. |
44 | */ |
45 | |
4efb3868 |
46 | struct font { |
47 | GdkFont *font; |
48 | int type; |
49 | int size; |
50 | }; |
51 | |
83680571 |
52 | /* |
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. |
58 | */ |
2ef96bd6 |
59 | struct frontend { |
83680571 |
60 | GtkWidget *window; |
7f77ea24 |
61 | GtkWidget *area; |
fd1a1a2b |
62 | GtkWidget *statusbar; |
63 | guint statusctx; |
2ef96bd6 |
64 | GdkPixmap *pixmap; |
65 | GdkColor *colours; |
66 | int ncolours; |
67 | GdkColormap *colmap; |
68 | int w, h; |
7f77ea24 |
69 | midend_data *me; |
2ef96bd6 |
70 | GdkGC *gc; |
71 | int bbox_l, bbox_r, bbox_u, bbox_d; |
20ee89e3 |
72 | int timer_active, timer_id; |
8c1fd974 |
73 | struct timeval last_time; |
4efb3868 |
74 | struct font *fonts; |
75 | int nfonts, fontsize; |
c8230524 |
76 | config_item *cfg; |
5928817c |
77 | int cfg_which, cfgret; |
c8230524 |
78 | GtkWidget *cfgbox; |
9b4b03d3 |
79 | char *paste_data; |
80 | int paste_data_len; |
83680571 |
81 | }; |
82 | |
cbb5549e |
83 | void get_random_seed(void **randseed, int *randseedsize) |
84 | { |
85 | time_t *tp = snew(time_t); |
86 | time(tp); |
87 | *randseed = (void *)tp; |
88 | *randseedsize = sizeof(time_t); |
89 | } |
90 | |
2ef96bd6 |
91 | void frontend_default_colour(frontend *fe, float *output) |
92 | { |
93 | GdkColor col = fe->window->style->bg[GTK_STATE_NORMAL]; |
94 | output[0] = col.red / 65535.0; |
95 | output[1] = col.green / 65535.0; |
96 | output[2] = col.blue / 65535.0; |
97 | } |
98 | |
fd1a1a2b |
99 | void status_bar(frontend *fe, char *text) |
100 | { |
101 | assert(fe->statusbar); |
102 | |
103 | gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx); |
104 | gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, text); |
105 | } |
106 | |
2ef96bd6 |
107 | void start_draw(frontend *fe) |
108 | { |
109 | fe->gc = gdk_gc_new(fe->area->window); |
110 | fe->bbox_l = fe->w; |
111 | fe->bbox_r = 0; |
112 | fe->bbox_u = fe->h; |
113 | fe->bbox_d = 0; |
114 | } |
115 | |
4efb3868 |
116 | void clip(frontend *fe, int x, int y, int w, int h) |
117 | { |
118 | GdkRectangle rect; |
119 | |
120 | rect.x = x; |
121 | rect.y = y; |
122 | rect.width = w; |
123 | rect.height = h; |
124 | |
125 | gdk_gc_set_clip_rectangle(fe->gc, &rect); |
126 | } |
127 | |
128 | void unclip(frontend *fe) |
129 | { |
130 | GdkRectangle rect; |
131 | |
132 | rect.x = 0; |
133 | rect.y = 0; |
134 | rect.width = fe->w; |
135 | rect.height = fe->h; |
136 | |
137 | gdk_gc_set_clip_rectangle(fe->gc, &rect); |
138 | } |
139 | |
140 | void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize, |
141 | int align, int colour, char *text) |
142 | { |
143 | int i; |
144 | |
145 | /* |
146 | * Find or create the font. |
147 | */ |
148 | for (i = 0; i < fe->nfonts; i++) |
149 | if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize) |
150 | break; |
151 | |
152 | if (i == fe->nfonts) { |
153 | if (fe->fontsize <= fe->nfonts) { |
154 | fe->fontsize = fe->nfonts + 10; |
155 | fe->fonts = sresize(fe->fonts, fe->fontsize, struct font); |
156 | } |
157 | |
158 | fe->nfonts++; |
159 | |
160 | fe->fonts[i].type = fonttype; |
161 | fe->fonts[i].size = fontsize; |
162 | |
a9668451 |
163 | #if GTK_CHECK_VERSION(2,0,0) |
4efb3868 |
164 | /* |
a9668451 |
165 | * Use Pango to find the closest match to the requested |
166 | * font. |
4efb3868 |
167 | */ |
a9668451 |
168 | { |
169 | PangoFontDescription *fd; |
170 | |
171 | fd = pango_font_description_new(); |
172 | /* `Monospace' and `Sans' are meta-families guaranteed to exist */ |
173 | pango_font_description_set_family(fd, fonttype == FONT_FIXED ? |
174 | "Monospace" : "Sans"); |
2e837d78 |
175 | pango_font_description_set_weight(fd, PANGO_WEIGHT_BOLD); |
a9668451 |
176 | /* |
177 | * I found some online Pango documentation which |
178 | * described a function called |
179 | * pango_font_description_set_absolute_size(), which is |
180 | * _exactly_ what I want here. Unfortunately, none of |
181 | * my local Pango installations have it (presumably |
182 | * they're too old), so I'm going to have to hack round |
183 | * it by figuring out the point size myself. This |
184 | * limits me to X and probably also breaks in later |
185 | * Pango installations, so ideally I should add another |
186 | * CHECK_VERSION type ifdef and use set_absolute_size |
187 | * where available. All very annoying. |
188 | */ |
189 | #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION |
190 | pango_font_description_set_absolute_size(fd, PANGO_SCALE*fontsize); |
191 | #else |
192 | { |
193 | Display *d = GDK_DISPLAY(); |
194 | int s = DefaultScreen(d); |
195 | double resolution = |
196 | (PANGO_SCALE * 72.27 / 25.4) * |
197 | ((double) DisplayWidthMM(d, s) / DisplayWidth (d, s)); |
198 | pango_font_description_set_size(fd, resolution * fontsize); |
199 | } |
200 | #endif |
201 | fe->fonts[i].font = gdk_font_from_description(fd); |
202 | pango_font_description_free(fd); |
203 | } |
204 | |
205 | if (!fe->fonts[i].font) |
206 | #endif |
207 | /* |
208 | * In GTK 1.2, I don't know of any plausible way to |
209 | * pick a suitable font, so I'm just going to be |
210 | * tedious. |
211 | * |
212 | * This is also fallback code called if the Pango |
213 | * approach fails to find an appropriate font. |
214 | */ |
215 | fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ? |
216 | "fixed" : "variable"); |
4efb3868 |
217 | } |
218 | |
219 | /* |
220 | * Find string dimensions and process alignment. |
221 | */ |
222 | { |
223 | int lb, rb, wid, asc, desc; |
224 | |
3bcf831a |
225 | /* |
226 | * Measure vertical string extents with respect to the same |
227 | * string always... |
228 | */ |
229 | gdk_string_extents(fe->fonts[i].font, |
230 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
4efb3868 |
231 | &lb, &rb, &wid, &asc, &desc); |
232 | if (align & ALIGN_VCENTRE) |
233 | y += asc - (asc+desc)/2; |
234 | |
3bcf831a |
235 | /* |
236 | * ... but horizontal extents with respect to the provided |
237 | * string. This means that multiple pieces of text centred |
238 | * on the same y-coordinate don't have different baselines. |
239 | */ |
240 | gdk_string_extents(fe->fonts[i].font, text, |
241 | &lb, &rb, &wid, &asc, &desc); |
242 | |
4efb3868 |
243 | if (align & ALIGN_HCENTRE) |
244 | x -= wid / 2; |
245 | else if (align & ALIGN_HRIGHT) |
246 | x -= wid; |
247 | |
248 | } |
249 | |
250 | /* |
251 | * Set colour and actually draw text. |
252 | */ |
253 | gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); |
254 | gdk_draw_string(fe->pixmap, fe->fonts[i].font, fe->gc, x, y, text); |
255 | } |
256 | |
2ef96bd6 |
257 | void draw_rect(frontend *fe, int x, int y, int w, int h, int colour) |
258 | { |
259 | gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); |
260 | gdk_draw_rectangle(fe->pixmap, fe->gc, 1, x, y, w, h); |
261 | } |
262 | |
263 | void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour) |
264 | { |
265 | gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); |
266 | gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2); |
267 | } |
268 | |
269 | void draw_polygon(frontend *fe, int *coords, int npoints, |
270 | int fill, int colour) |
271 | { |
272 | GdkPoint *points = snewn(npoints, GdkPoint); |
273 | int i; |
274 | |
275 | for (i = 0; i < npoints; i++) { |
276 | points[i].x = coords[i*2]; |
277 | points[i].y = coords[i*2+1]; |
278 | } |
279 | |
280 | gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); |
281 | gdk_draw_polygon(fe->pixmap, fe->gc, fill, points, npoints); |
282 | |
283 | sfree(points); |
284 | } |
285 | |
286 | void draw_update(frontend *fe, int x, int y, int w, int h) |
287 | { |
288 | if (fe->bbox_l > x ) fe->bbox_l = x ; |
289 | if (fe->bbox_r < x+w) fe->bbox_r = x+w; |
290 | if (fe->bbox_u > y ) fe->bbox_u = y ; |
291 | if (fe->bbox_d < y+h) fe->bbox_d = y+h; |
292 | } |
293 | |
294 | void end_draw(frontend *fe) |
295 | { |
296 | gdk_gc_unref(fe->gc); |
297 | fe->gc = NULL; |
298 | |
299 | if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) { |
300 | gdk_draw_pixmap(fe->area->window, |
301 | fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)], |
302 | fe->pixmap, |
303 | fe->bbox_l, fe->bbox_u, |
304 | fe->bbox_l, fe->bbox_u, |
305 | fe->bbox_r - fe->bbox_l, fe->bbox_d - fe->bbox_u); |
306 | } |
307 | } |
308 | |
83680571 |
309 | static void destroy(GtkWidget *widget, gpointer data) |
310 | { |
f49650a7 |
311 | frontend *fe = (frontend *)data; |
312 | deactivate_timer(fe); |
83680571 |
313 | gtk_main_quit(); |
314 | } |
315 | |
2ef96bd6 |
316 | static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data) |
7f77ea24 |
317 | { |
2ef96bd6 |
318 | frontend *fe = (frontend *)data; |
1482ee76 |
319 | int keyval; |
7f77ea24 |
320 | |
2ef96bd6 |
321 | if (!fe->pixmap) |
322 | return TRUE; |
7f77ea24 |
323 | |
3c833d45 |
324 | if (event->keyval == GDK_Up) |
1482ee76 |
325 | keyval = CURSOR_UP; |
3c833d45 |
326 | else if (event->keyval == GDK_KP_Up || event->keyval == GDK_KP_8) |
327 | keyval = MOD_NUM_KEYPAD | '8'; |
328 | else if (event->keyval == GDK_Down) |
1482ee76 |
329 | keyval = CURSOR_DOWN; |
3c833d45 |
330 | else if (event->keyval == GDK_KP_Down || event->keyval == GDK_KP_2) |
331 | keyval = MOD_NUM_KEYPAD | '2'; |
332 | else if (event->keyval == GDK_Left) |
1482ee76 |
333 | keyval = CURSOR_LEFT; |
3c833d45 |
334 | else if (event->keyval == GDK_KP_Left || event->keyval == GDK_KP_4) |
335 | keyval = MOD_NUM_KEYPAD | '4'; |
336 | else if (event->keyval == GDK_Right) |
1482ee76 |
337 | keyval = CURSOR_RIGHT; |
3c833d45 |
338 | else if (event->keyval == GDK_KP_Right || event->keyval == GDK_KP_6) |
339 | keyval = MOD_NUM_KEYPAD | '6'; |
c71454c0 |
340 | else if (event->keyval == GDK_KP_Home || event->keyval == GDK_KP_7) |
3c833d45 |
341 | keyval = MOD_NUM_KEYPAD | '7'; |
c71454c0 |
342 | else if (event->keyval == GDK_KP_End || event->keyval == GDK_KP_1) |
3c833d45 |
343 | keyval = MOD_NUM_KEYPAD | '1'; |
c71454c0 |
344 | else if (event->keyval == GDK_KP_Page_Up || event->keyval == GDK_KP_9) |
3c833d45 |
345 | keyval = MOD_NUM_KEYPAD | '9'; |
c71454c0 |
346 | else if (event->keyval == GDK_KP_Page_Down || event->keyval == GDK_KP_3) |
3c833d45 |
347 | keyval = MOD_NUM_KEYPAD | '3'; |
348 | else if (event->keyval == GDK_KP_Insert || event->keyval == GDK_KP_0) |
349 | keyval = MOD_NUM_KEYPAD | '0'; |
350 | else if (event->keyval == GDK_KP_Begin || event->keyval == GDK_KP_5) |
351 | keyval = MOD_NUM_KEYPAD | '5'; |
352 | else if (event->string[0] && !event->string[1]) |
353 | keyval = (unsigned char)event->string[0]; |
1482ee76 |
354 | else |
355 | keyval = -1; |
356 | |
357 | if (keyval >= 0 && |
358 | !midend_process_key(fe->me, 0, 0, keyval)) |
2ef96bd6 |
359 | gtk_widget_destroy(fe->window); |
7f77ea24 |
360 | |
361 | return TRUE; |
362 | } |
363 | |
2ef96bd6 |
364 | static gint button_event(GtkWidget *widget, GdkEventButton *event, |
365 | gpointer data) |
7f77ea24 |
366 | { |
2ef96bd6 |
367 | frontend *fe = (frontend *)data; |
368 | int button; |
369 | |
370 | if (!fe->pixmap) |
371 | return TRUE; |
372 | |
74a4e547 |
373 | if (event->type != GDK_BUTTON_PRESS && event->type != GDK_BUTTON_RELEASE) |
2ef96bd6 |
374 | return TRUE; |
7f77ea24 |
375 | |
b0f06719 |
376 | if (event->button == 2 || (event->state & GDK_SHIFT_MASK)) |
2ef96bd6 |
377 | button = MIDDLE_BUTTON; |
b0f06719 |
378 | else if (event->button == 1) |
379 | button = LEFT_BUTTON; |
2ef96bd6 |
380 | else if (event->button == 3) |
381 | button = RIGHT_BUTTON; |
382 | else |
383 | return FALSE; /* don't even know what button! */ |
384 | |
74a4e547 |
385 | if (event->type == GDK_BUTTON_RELEASE) |
386 | button += LEFT_RELEASE - LEFT_BUTTON; |
387 | |
388 | if (!midend_process_key(fe->me, event->x, event->y, button)) |
389 | gtk_widget_destroy(fe->window); |
390 | |
391 | return TRUE; |
392 | } |
393 | |
394 | static gint motion_event(GtkWidget *widget, GdkEventMotion *event, |
395 | gpointer data) |
396 | { |
397 | frontend *fe = (frontend *)data; |
398 | int button; |
399 | |
400 | if (!fe->pixmap) |
401 | return TRUE; |
402 | |
74a4e547 |
403 | if (event->state & (GDK_BUTTON2_MASK | GDK_SHIFT_MASK)) |
404 | button = MIDDLE_DRAG; |
405 | else if (event->state & GDK_BUTTON1_MASK) |
406 | button = LEFT_DRAG; |
407 | else if (event->state & GDK_BUTTON3_MASK) |
408 | button = RIGHT_DRAG; |
409 | else |
410 | return FALSE; /* don't even know what button! */ |
411 | |
2ef96bd6 |
412 | if (!midend_process_key(fe->me, event->x, event->y, button)) |
413 | gtk_widget_destroy(fe->window); |
7f77ea24 |
414 | |
415 | return TRUE; |
416 | } |
417 | |
2ef96bd6 |
418 | static gint expose_area(GtkWidget *widget, GdkEventExpose *event, |
419 | gpointer data) |
83680571 |
420 | { |
2ef96bd6 |
421 | frontend *fe = (frontend *)data; |
422 | |
423 | if (fe->pixmap) { |
424 | gdk_draw_pixmap(widget->window, |
425 | widget->style->fg_gc[GTK_WIDGET_STATE(widget)], |
426 | fe->pixmap, |
427 | event->area.x, event->area.y, |
428 | event->area.x, event->area.y, |
429 | event->area.width, event->area.height); |
430 | } |
431 | return TRUE; |
432 | } |
433 | |
fd1a1a2b |
434 | static gint map_window(GtkWidget *widget, GdkEvent *event, |
435 | gpointer data) |
436 | { |
437 | frontend *fe = (frontend *)data; |
438 | |
439 | /* |
440 | * Apparently we need to do this because otherwise the status |
441 | * bar will fail to update immediately. Annoying, but there we |
442 | * go. |
443 | */ |
444 | gtk_widget_queue_draw(fe->window); |
445 | |
446 | return TRUE; |
447 | } |
448 | |
2ef96bd6 |
449 | static gint configure_area(GtkWidget *widget, |
450 | GdkEventConfigure *event, gpointer data) |
451 | { |
452 | frontend *fe = (frontend *)data; |
453 | GdkGC *gc; |
454 | |
eb2ad6f1 |
455 | if (fe->pixmap) |
456 | gdk_pixmap_unref(fe->pixmap); |
457 | |
2ef96bd6 |
458 | fe->pixmap = gdk_pixmap_new(widget->window, fe->w, fe->h, -1); |
459 | |
460 | gc = gdk_gc_new(fe->area->window); |
461 | gdk_gc_set_foreground(gc, &fe->colours[0]); |
462 | gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->w, fe->h); |
463 | gdk_gc_unref(gc); |
464 | |
465 | midend_redraw(fe->me); |
466 | |
467 | return TRUE; |
468 | } |
469 | |
470 | static gint timer_func(gpointer data) |
471 | { |
472 | frontend *fe = (frontend *)data; |
473 | |
8c1fd974 |
474 | if (fe->timer_active) { |
475 | struct timeval now; |
476 | float elapsed; |
477 | gettimeofday(&now, NULL); |
478 | elapsed = ((now.tv_usec - fe->last_time.tv_usec) * 0.000001F + |
479 | (now.tv_sec - fe->last_time.tv_sec)); |
480 | midend_timer(fe->me, elapsed); /* may clear timer_active */ |
481 | fe->last_time = now; |
482 | } |
2ef96bd6 |
483 | |
484 | return fe->timer_active; |
485 | } |
486 | |
487 | void deactivate_timer(frontend *fe) |
488 | { |
20ee89e3 |
489 | if (fe->timer_active) |
490 | gtk_timeout_remove(fe->timer_id); |
2ef96bd6 |
491 | fe->timer_active = FALSE; |
492 | } |
493 | |
494 | void activate_timer(frontend *fe) |
495 | { |
8c1fd974 |
496 | if (!fe->timer_active) { |
20ee89e3 |
497 | fe->timer_id = gtk_timeout_add(20, timer_func, fe); |
8c1fd974 |
498 | gettimeofday(&fe->last_time, NULL); |
499 | } |
2ef96bd6 |
500 | fe->timer_active = TRUE; |
501 | } |
502 | |
c8230524 |
503 | static void window_destroy(GtkWidget *widget, gpointer data) |
504 | { |
505 | gtk_main_quit(); |
506 | } |
507 | |
508 | static void errmsg_button_clicked(GtkButton *button, gpointer data) |
509 | { |
510 | gtk_widget_destroy(GTK_WIDGET(data)); |
511 | } |
512 | |
a163b7c2 |
513 | static int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) |
514 | { |
515 | GtkObject *cancelbutton = GTK_OBJECT(data); |
516 | |
517 | /* |
518 | * `Escape' effectively clicks the cancel button |
519 | */ |
520 | if (event->keyval == GDK_Escape) { |
521 | gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton), "clicked"); |
522 | return TRUE; |
523 | } |
524 | |
525 | return FALSE; |
526 | } |
527 | |
97098757 |
528 | void message_box(GtkWidget *parent, char *title, char *msg, int centre) |
c8230524 |
529 | { |
530 | GtkWidget *window, *hbox, *text, *ok; |
531 | |
532 | window = gtk_dialog_new(); |
533 | text = gtk_label_new(msg); |
534 | gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0); |
535 | hbox = gtk_hbox_new(FALSE, 0); |
536 | gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20); |
537 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), |
538 | hbox, FALSE, FALSE, 20); |
539 | gtk_widget_show(text); |
540 | gtk_widget_show(hbox); |
97098757 |
541 | gtk_window_set_title(GTK_WINDOW(window), title); |
c8230524 |
542 | gtk_label_set_line_wrap(GTK_LABEL(text), TRUE); |
543 | ok = gtk_button_new_with_label("OK"); |
544 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area), |
545 | ok, FALSE, FALSE, 0); |
546 | gtk_widget_show(ok); |
547 | GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT); |
548 | gtk_window_set_default(GTK_WINDOW(window), ok); |
549 | gtk_signal_connect(GTK_OBJECT(ok), "clicked", |
550 | GTK_SIGNAL_FUNC(errmsg_button_clicked), window); |
a1ed9f0e |
551 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
552 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
a163b7c2 |
553 | gtk_signal_connect(GTK_OBJECT(window), "key_press_event", |
554 | GTK_SIGNAL_FUNC(win_key_press), ok); |
c8230524 |
555 | gtk_window_set_modal(GTK_WINDOW(window), TRUE); |
556 | gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent)); |
557 | //set_transient_window_pos(parent, window); |
558 | gtk_widget_show(window); |
559 | gtk_main(); |
560 | } |
561 | |
97098757 |
562 | void error_box(GtkWidget *parent, char *msg) |
563 | { |
564 | message_box(parent, "Error", msg, FALSE); |
565 | } |
566 | |
c8230524 |
567 | static void config_ok_button_clicked(GtkButton *button, gpointer data) |
568 | { |
569 | frontend *fe = (frontend *)data; |
570 | char *err; |
571 | |
5928817c |
572 | err = midend_set_config(fe->me, fe->cfg_which, fe->cfg); |
c8230524 |
573 | |
574 | if (err) |
575 | error_box(fe->cfgbox, err); |
576 | else { |
577 | fe->cfgret = TRUE; |
578 | gtk_widget_destroy(fe->cfgbox); |
579 | } |
580 | } |
581 | |
582 | static void config_cancel_button_clicked(GtkButton *button, gpointer data) |
583 | { |
584 | frontend *fe = (frontend *)data; |
585 | |
586 | gtk_widget_destroy(fe->cfgbox); |
587 | } |
588 | |
a163b7c2 |
589 | static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data) |
590 | { |
591 | /* |
592 | * GtkEntry has a nasty habit of eating the Return key, which |
593 | * is unhelpful since it doesn't actually _do_ anything with it |
594 | * (it calls gtk_widget_activate, but our edit boxes never need |
595 | * activating). So I catch Return before GtkEntry sees it, and |
596 | * pass it straight on to the parent widget. Effect: hitting |
597 | * Return in an edit box will now activate the default button |
598 | * in the dialog just like it will everywhere else. |
599 | */ |
600 | if (event->keyval == GDK_Return && widget->parent != NULL) { |
601 | gint return_val; |
602 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event"); |
603 | gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event", |
604 | event, &return_val); |
605 | return return_val; |
606 | } |
607 | return FALSE; |
608 | } |
609 | |
c8230524 |
610 | static void editbox_changed(GtkEditable *ed, gpointer data) |
611 | { |
612 | config_item *i = (config_item *)data; |
613 | |
614 | sfree(i->sval); |
615 | i->sval = dupstr(gtk_entry_get_text(GTK_ENTRY(ed))); |
616 | } |
617 | |
618 | static void button_toggled(GtkToggleButton *tb, gpointer data) |
619 | { |
620 | config_item *i = (config_item *)data; |
621 | |
622 | i->ival = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb)); |
623 | } |
624 | |
625 | static void droplist_sel(GtkMenuItem *item, gpointer data) |
626 | { |
627 | config_item *i = (config_item *)data; |
628 | |
629 | i->ival = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item), |
630 | "user-data")); |
631 | } |
632 | |
5928817c |
633 | static int get_config(frontend *fe, int which) |
c8230524 |
634 | { |
a163b7c2 |
635 | GtkWidget *w, *table, *cancel; |
5928817c |
636 | char *title; |
c8230524 |
637 | config_item *i; |
638 | int y; |
639 | |
5928817c |
640 | fe->cfg = midend_get_config(fe->me, which, &title); |
641 | fe->cfg_which = which; |
c8230524 |
642 | fe->cfgret = FALSE; |
643 | |
644 | fe->cfgbox = gtk_dialog_new(); |
5928817c |
645 | gtk_window_set_title(GTK_WINDOW(fe->cfgbox), title); |
646 | sfree(title); |
c8230524 |
647 | |
648 | w = gtk_button_new_with_label("OK"); |
649 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area), |
650 | w, FALSE, FALSE, 0); |
651 | gtk_widget_show(w); |
652 | GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT); |
653 | gtk_window_set_default(GTK_WINDOW(fe->cfgbox), w); |
654 | gtk_signal_connect(GTK_OBJECT(w), "clicked", |
655 | GTK_SIGNAL_FUNC(config_ok_button_clicked), fe); |
656 | |
657 | w = gtk_button_new_with_label("Cancel"); |
658 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area), |
659 | w, FALSE, FALSE, 0); |
660 | gtk_widget_show(w); |
661 | gtk_signal_connect(GTK_OBJECT(w), "clicked", |
662 | GTK_SIGNAL_FUNC(config_cancel_button_clicked), fe); |
a163b7c2 |
663 | cancel = w; |
c8230524 |
664 | |
665 | table = gtk_table_new(1, 2, FALSE); |
666 | y = 0; |
667 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->vbox), |
668 | table, FALSE, FALSE, 0); |
669 | gtk_widget_show(table); |
670 | |
95709966 |
671 | for (i = fe->cfg; i->type != C_END; i++) { |
c8230524 |
672 | gtk_table_resize(GTK_TABLE(table), y+1, 2); |
673 | |
674 | switch (i->type) { |
95709966 |
675 | case C_STRING: |
c8230524 |
676 | /* |
677 | * Edit box with a label beside it. |
678 | */ |
679 | |
680 | w = gtk_label_new(i->name); |
681 | gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5); |
682 | gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1, |
683 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
684 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
685 | 3, 3); |
686 | gtk_widget_show(w); |
687 | |
688 | w = gtk_entry_new(); |
689 | gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1, |
690 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
691 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
692 | 3, 3); |
693 | gtk_entry_set_text(GTK_ENTRY(w), i->sval); |
694 | gtk_signal_connect(GTK_OBJECT(w), "changed", |
695 | GTK_SIGNAL_FUNC(editbox_changed), i); |
a163b7c2 |
696 | gtk_signal_connect(GTK_OBJECT(w), "key_press_event", |
697 | GTK_SIGNAL_FUNC(editbox_key), NULL); |
c8230524 |
698 | gtk_widget_show(w); |
699 | |
700 | break; |
701 | |
95709966 |
702 | case C_BOOLEAN: |
c8230524 |
703 | /* |
704 | * Simple checkbox. |
705 | */ |
706 | w = gtk_check_button_new_with_label(i->name); |
707 | gtk_signal_connect(GTK_OBJECT(w), "toggled", |
708 | GTK_SIGNAL_FUNC(button_toggled), i); |
709 | gtk_table_attach(GTK_TABLE(table), w, 0, 2, y, y+1, |
710 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
711 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
712 | 3, 3); |
713 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), i->ival); |
714 | gtk_widget_show(w); |
715 | break; |
716 | |
95709966 |
717 | case C_CHOICES: |
c8230524 |
718 | /* |
719 | * Drop-down list (GtkOptionMenu). |
720 | */ |
721 | |
722 | w = gtk_label_new(i->name); |
723 | gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5); |
724 | gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1, |
725 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
726 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
727 | 3, 3); |
728 | gtk_widget_show(w); |
729 | |
730 | w = gtk_option_menu_new(); |
731 | gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1, |
732 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
733 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
734 | 3, 3); |
735 | gtk_widget_show(w); |
736 | |
737 | { |
738 | int c, val; |
739 | char *p, *q, *name; |
740 | GtkWidget *menuitem; |
741 | GtkWidget *menu = gtk_menu_new(); |
742 | |
743 | gtk_option_menu_set_menu(GTK_OPTION_MENU(w), menu); |
744 | |
745 | c = *i->sval; |
746 | p = i->sval+1; |
747 | val = 0; |
748 | |
749 | while (*p) { |
750 | q = p; |
751 | while (*q && *q != c) |
752 | q++; |
753 | |
754 | name = snewn(q-p+1, char); |
755 | strncpy(name, p, q-p); |
756 | name[q-p] = '\0'; |
757 | |
758 | if (*q) q++; /* eat delimiter */ |
759 | |
760 | menuitem = gtk_menu_item_new_with_label(name); |
761 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
762 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
763 | GINT_TO_POINTER(val)); |
764 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
765 | GTK_SIGNAL_FUNC(droplist_sel), i); |
766 | gtk_widget_show(menuitem); |
767 | |
768 | val++; |
769 | |
770 | p = q; |
771 | } |
772 | |
773 | gtk_option_menu_set_history(GTK_OPTION_MENU(w), i->ival); |
774 | } |
775 | |
776 | break; |
777 | } |
778 | |
779 | y++; |
780 | } |
781 | |
782 | gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "destroy", |
783 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
a163b7c2 |
784 | gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "key_press_event", |
785 | GTK_SIGNAL_FUNC(win_key_press), cancel); |
c8230524 |
786 | gtk_window_set_modal(GTK_WINDOW(fe->cfgbox), TRUE); |
787 | gtk_window_set_transient_for(GTK_WINDOW(fe->cfgbox), |
788 | GTK_WINDOW(fe->window)); |
789 | //set_transient_window_pos(fe->window, fe->cfgbox); |
790 | gtk_widget_show(fe->cfgbox); |
791 | gtk_main(); |
792 | |
077f3cbe |
793 | free_cfg(fe->cfg); |
c8230524 |
794 | |
795 | return fe->cfgret; |
796 | } |
797 | |
eb2ad6f1 |
798 | static void menu_key_event(GtkMenuItem *menuitem, gpointer data) |
799 | { |
800 | frontend *fe = (frontend *)data; |
801 | int key = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem), |
802 | "user-data")); |
803 | if (!midend_process_key(fe->me, 0, 0, key)) |
804 | gtk_widget_destroy(fe->window); |
805 | } |
806 | |
807 | static void menu_preset_event(GtkMenuItem *menuitem, gpointer data) |
808 | { |
809 | frontend *fe = (frontend *)data; |
810 | game_params *params = |
811 | (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data"); |
812 | int x, y; |
813 | |
814 | midend_set_params(fe->me, params); |
5928817c |
815 | midend_new_game(fe->me); |
eb2ad6f1 |
816 | midend_size(fe->me, &x, &y); |
817 | gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); |
818 | fe->w = x; |
819 | fe->h = y; |
820 | } |
821 | |
9b4b03d3 |
822 | GdkAtom compound_text_atom, utf8_string_atom; |
823 | int paste_initialised = FALSE; |
824 | |
825 | void init_paste() |
826 | { |
827 | if (paste_initialised) |
828 | return; |
829 | |
830 | if (!compound_text_atom) |
831 | compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE); |
832 | if (!utf8_string_atom) |
833 | utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE); |
834 | |
835 | /* |
836 | * Ensure that all the cut buffers exist - according to the |
837 | * ICCCM, we must do this before we start using cut buffers. |
838 | */ |
839 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
840 | XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, "", 0); |
841 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
842 | XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, "", 0); |
843 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
844 | XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, "", 0); |
845 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
846 | XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, "", 0); |
847 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
848 | XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, "", 0); |
849 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
850 | XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, "", 0); |
851 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
852 | XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, "", 0); |
853 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
854 | XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, "", 0); |
855 | } |
856 | |
857 | /* Store data in a cut-buffer. */ |
858 | void store_cutbuffer(char *ptr, int len) |
859 | { |
860 | /* ICCCM says we must rotate the buffers before storing to buffer 0. */ |
861 | XRotateBuffers(GDK_DISPLAY(), 1); |
862 | XStoreBytes(GDK_DISPLAY(), ptr, len); |
863 | } |
864 | |
865 | void write_clip(frontend *fe, char *data) |
866 | { |
867 | init_paste(); |
868 | |
869 | if (fe->paste_data) |
870 | sfree(fe->paste_data); |
871 | |
872 | /* |
873 | * For this simple application we can safely assume that the |
874 | * data passed to this function is pure ASCII, which means we |
875 | * can return precisely the same stuff for types STRING, |
876 | * COMPOUND_TEXT or UTF8_STRING. |
877 | */ |
878 | |
879 | fe->paste_data = data; |
880 | fe->paste_data_len = strlen(data); |
881 | |
882 | store_cutbuffer(fe->paste_data, fe->paste_data_len); |
883 | |
884 | if (gtk_selection_owner_set(fe->area, GDK_SELECTION_PRIMARY, |
885 | CurrentTime)) { |
886 | gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY, |
887 | GDK_SELECTION_TYPE_STRING, 1); |
888 | gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY, |
889 | compound_text_atom, 1); |
890 | gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY, |
891 | utf8_string_atom, 1); |
892 | } |
893 | } |
894 | |
895 | void selection_get(GtkWidget *widget, GtkSelectionData *seldata, |
896 | guint info, guint time_stamp, gpointer data) |
897 | { |
898 | frontend *fe = (frontend *)data; |
899 | gtk_selection_data_set(seldata, seldata->target, 8, |
900 | fe->paste_data, fe->paste_data_len); |
901 | } |
902 | |
903 | gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata, |
904 | gpointer data) |
905 | { |
906 | frontend *fe = (frontend *)data; |
907 | |
908 | if (fe->paste_data) |
909 | sfree(fe->paste_data); |
910 | fe->paste_data = NULL; |
911 | fe->paste_data_len = 0; |
912 | return TRUE; |
913 | } |
914 | |
915 | static void menu_copy_event(GtkMenuItem *menuitem, gpointer data) |
916 | { |
917 | frontend *fe = (frontend *)data; |
918 | char *text; |
919 | |
920 | text = midend_text_format(fe->me); |
921 | |
922 | if (text) { |
923 | write_clip(fe, text); |
924 | } else { |
925 | gdk_beep(); |
926 | } |
927 | } |
928 | |
2ac6d24e |
929 | static void menu_solve_event(GtkMenuItem *menuitem, gpointer data) |
930 | { |
931 | frontend *fe = (frontend *)data; |
932 | char *msg; |
933 | |
934 | msg = midend_solve(fe->me); |
935 | |
936 | if (msg) |
937 | error_box(fe->window, msg); |
938 | } |
939 | |
c8230524 |
940 | static void menu_config_event(GtkMenuItem *menuitem, gpointer data) |
941 | { |
942 | frontend *fe = (frontend *)data; |
5928817c |
943 | int which = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem), |
944 | "user-data")); |
c8230524 |
945 | int x, y; |
946 | |
5928817c |
947 | if (!get_config(fe, which)) |
c8230524 |
948 | return; |
949 | |
5928817c |
950 | midend_new_game(fe->me); |
c8230524 |
951 | midend_size(fe->me, &x, &y); |
952 | gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); |
953 | fe->w = x; |
954 | fe->h = y; |
955 | } |
956 | |
97098757 |
957 | static void menu_about_event(GtkMenuItem *menuitem, gpointer data) |
958 | { |
959 | frontend *fe = (frontend *)data; |
960 | char titlebuf[256]; |
961 | char textbuf[1024]; |
962 | |
963 | sprintf(titlebuf, "About %.200s", thegame.name); |
964 | sprintf(textbuf, |
965 | "%.200s\n\n" |
966 | "from Simon Tatham's Portable Puzzle Collection\n\n" |
967 | "%.500s", thegame.name, ver); |
968 | |
969 | message_box(fe->window, titlebuf, textbuf, TRUE); |
970 | } |
971 | |
eb2ad6f1 |
972 | static GtkWidget *add_menu_item_with_key(frontend *fe, GtkContainer *cont, |
973 | char *text, int key) |
974 | { |
975 | GtkWidget *menuitem = gtk_menu_item_new_with_label(text); |
976 | gtk_container_add(cont, menuitem); |
977 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
978 | GINT_TO_POINTER(key)); |
979 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
980 | GTK_SIGNAL_FUNC(menu_key_event), fe); |
981 | gtk_widget_show(menuitem); |
982 | return menuitem; |
983 | } |
984 | |
985 | static void add_menu_separator(GtkContainer *cont) |
986 | { |
987 | GtkWidget *menuitem = gtk_menu_item_new(); |
988 | gtk_container_add(cont, menuitem); |
989 | gtk_widget_show(menuitem); |
990 | } |
991 | |
8b7938e7 |
992 | static frontend *new_window(char *game_id, char **error) |
2ef96bd6 |
993 | { |
994 | frontend *fe; |
eb2ad6f1 |
995 | GtkBox *vbox; |
996 | GtkWidget *menubar, *menu, *menuitem; |
997 | int x, y, n; |
83680571 |
998 | |
2ef96bd6 |
999 | fe = snew(frontend); |
83680571 |
1000 | |
be8d5aa1 |
1001 | fe->me = midend_new(fe, &thegame); |
8b7938e7 |
1002 | if (game_id) { |
1185e3c5 |
1003 | *error = midend_game_id(fe->me, game_id); |
8b7938e7 |
1004 | if (*error) { |
1005 | midend_free(fe->me); |
1006 | sfree(fe); |
1007 | return NULL; |
1008 | } |
1009 | } |
5928817c |
1010 | midend_new_game(fe->me); |
7f77ea24 |
1011 | |
2ef96bd6 |
1012 | fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
be8d5aa1 |
1013 | gtk_window_set_title(GTK_WINDOW(fe->window), thegame.name); |
ff2f787b |
1014 | #if 0 |
1015 | gtk_window_set_resizable(GTK_WINDOW(fe->window), FALSE); |
1016 | #else |
1017 | gtk_window_set_policy(GTK_WINDOW(fe->window), FALSE, FALSE, TRUE); |
1018 | #endif |
eb2ad6f1 |
1019 | vbox = GTK_BOX(gtk_vbox_new(FALSE, 0)); |
1020 | gtk_container_add(GTK_CONTAINER(fe->window), GTK_WIDGET(vbox)); |
1021 | gtk_widget_show(GTK_WIDGET(vbox)); |
1022 | |
1023 | menubar = gtk_menu_bar_new(); |
1024 | gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0); |
1025 | gtk_widget_show(menubar); |
1026 | |
1027 | menuitem = gtk_menu_item_new_with_label("Game"); |
1028 | gtk_container_add(GTK_CONTAINER(menubar), menuitem); |
1029 | gtk_widget_show(menuitem); |
1030 | |
1031 | menu = gtk_menu_new(); |
1032 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu); |
1033 | |
1034 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "New", 'n'); |
1035 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Restart", 'r'); |
1036 | |
5928817c |
1037 | menuitem = gtk_menu_item_new_with_label("Specific..."); |
1038 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
1185e3c5 |
1039 | GINT_TO_POINTER(CFG_DESC)); |
1040 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1041 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1042 | GTK_SIGNAL_FUNC(menu_config_event), fe); |
1043 | gtk_widget_show(menuitem); |
1044 | |
1045 | menuitem = gtk_menu_item_new_with_label("Random Seed..."); |
1046 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
5928817c |
1047 | GINT_TO_POINTER(CFG_SEED)); |
1048 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1049 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1050 | GTK_SIGNAL_FUNC(menu_config_event), fe); |
1051 | gtk_widget_show(menuitem); |
1052 | |
be8d5aa1 |
1053 | if ((n = midend_num_presets(fe->me)) > 0 || thegame.can_configure) { |
eb2ad6f1 |
1054 | GtkWidget *submenu; |
1055 | int i; |
1056 | |
1057 | menuitem = gtk_menu_item_new_with_label("Type"); |
c8230524 |
1058 | gtk_container_add(GTK_CONTAINER(menubar), menuitem); |
eb2ad6f1 |
1059 | gtk_widget_show(menuitem); |
1060 | |
1061 | submenu = gtk_menu_new(); |
1062 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu); |
1063 | |
1064 | for (i = 0; i < n; i++) { |
1065 | char *name; |
1066 | game_params *params; |
1067 | |
1068 | midend_fetch_preset(fe->me, i, &name, ¶ms); |
1069 | |
1070 | menuitem = gtk_menu_item_new_with_label(name); |
1071 | gtk_container_add(GTK_CONTAINER(submenu), menuitem); |
1072 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", params); |
1073 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1074 | GTK_SIGNAL_FUNC(menu_preset_event), fe); |
1075 | gtk_widget_show(menuitem); |
1076 | } |
c8230524 |
1077 | |
be8d5aa1 |
1078 | if (thegame.can_configure) { |
c8230524 |
1079 | menuitem = gtk_menu_item_new_with_label("Custom..."); |
5928817c |
1080 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
1081 | GPOINTER_TO_INT(CFG_SETTINGS)); |
c8230524 |
1082 | gtk_container_add(GTK_CONTAINER(submenu), menuitem); |
1083 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1084 | GTK_SIGNAL_FUNC(menu_config_event), fe); |
1085 | gtk_widget_show(menuitem); |
1086 | } |
eb2ad6f1 |
1087 | } |
1088 | |
1089 | add_menu_separator(GTK_CONTAINER(menu)); |
1090 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Undo", 'u'); |
1091 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Redo", '\x12'); |
9b4b03d3 |
1092 | if (thegame.can_format_as_text) { |
1093 | add_menu_separator(GTK_CONTAINER(menu)); |
1094 | menuitem = gtk_menu_item_new_with_label("Copy"); |
1095 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1096 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1097 | GTK_SIGNAL_FUNC(menu_copy_event), fe); |
1098 | gtk_widget_show(menuitem); |
1099 | } |
2ac6d24e |
1100 | if (thegame.can_solve) { |
1101 | add_menu_separator(GTK_CONTAINER(menu)); |
1102 | menuitem = gtk_menu_item_new_with_label("Solve"); |
1103 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1104 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1105 | GTK_SIGNAL_FUNC(menu_solve_event), fe); |
1106 | gtk_widget_show(menuitem); |
1107 | } |
eb2ad6f1 |
1108 | add_menu_separator(GTK_CONTAINER(menu)); |
1109 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Exit", 'q'); |
7f77ea24 |
1110 | |
97098757 |
1111 | menuitem = gtk_menu_item_new_with_label("Help"); |
1112 | gtk_container_add(GTK_CONTAINER(menubar), menuitem); |
1113 | gtk_widget_show(menuitem); |
1114 | |
1115 | menu = gtk_menu_new(); |
1116 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu); |
1117 | |
1118 | menuitem = gtk_menu_item_new_with_label("About"); |
1119 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1120 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1121 | GTK_SIGNAL_FUNC(menu_about_event), fe); |
1122 | gtk_widget_show(menuitem); |
1123 | |
2ef96bd6 |
1124 | { |
1125 | int i, ncolours; |
1126 | float *colours; |
1127 | gboolean *success; |
7f77ea24 |
1128 | |
2ef96bd6 |
1129 | fe->colmap = gdk_colormap_get_system(); |
1130 | colours = midend_colours(fe->me, &ncolours); |
1131 | fe->ncolours = ncolours; |
1132 | fe->colours = snewn(ncolours, GdkColor); |
1133 | for (i = 0; i < ncolours; i++) { |
1134 | fe->colours[i].red = colours[i*3] * 0xFFFF; |
1135 | fe->colours[i].green = colours[i*3+1] * 0xFFFF; |
1136 | fe->colours[i].blue = colours[i*3+2] * 0xFFFF; |
1137 | } |
1138 | success = snewn(ncolours, gboolean); |
1139 | gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours, |
1140 | FALSE, FALSE, success); |
1141 | for (i = 0; i < ncolours; i++) { |
1142 | if (!success[i]) |
1143 | g_error("couldn't allocate colour %d (#%02x%02x%02x)\n", |
1144 | i, fe->colours[i].red >> 8, |
1145 | fe->colours[i].green >> 8, |
1146 | fe->colours[i].blue >> 8); |
1147 | } |
1148 | } |
7f77ea24 |
1149 | |
fd1a1a2b |
1150 | if (midend_wants_statusbar(fe->me)) { |
5725d728 |
1151 | GtkWidget *viewport; |
1152 | GtkRequisition req; |
1153 | |
1154 | viewport = gtk_viewport_new(NULL, NULL); |
1155 | gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE); |
fd1a1a2b |
1156 | fe->statusbar = gtk_statusbar_new(); |
5725d728 |
1157 | gtk_container_add(GTK_CONTAINER(viewport), fe->statusbar); |
1158 | gtk_widget_show(viewport); |
1159 | gtk_box_pack_end(vbox, viewport, FALSE, FALSE, 0); |
fd1a1a2b |
1160 | gtk_widget_show(fe->statusbar); |
1161 | fe->statusctx = gtk_statusbar_get_context_id |
1162 | (GTK_STATUSBAR(fe->statusbar), "game"); |
1163 | gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, |
5725d728 |
1164 | "test"); |
1165 | gtk_widget_size_request(fe->statusbar, &req); |
1166 | #if 0 |
1167 | /* For GTK 2.0, should we be using gtk_widget_set_size_request? */ |
1168 | #endif |
60d42abc |
1169 | gtk_widget_set_usize(viewport, -1, req.height); |
fd1a1a2b |
1170 | } else |
1171 | fe->statusbar = NULL; |
1172 | |
2ef96bd6 |
1173 | fe->area = gtk_drawing_area_new(); |
1174 | midend_size(fe->me, &x, &y); |
1175 | gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); |
1176 | fe->w = x; |
1177 | fe->h = y; |
1178 | |
eb2ad6f1 |
1179 | gtk_box_pack_end(vbox, fe->area, FALSE, FALSE, 0); |
2ef96bd6 |
1180 | |
1181 | fe->pixmap = NULL; |
4efb3868 |
1182 | fe->fonts = NULL; |
1183 | fe->nfonts = fe->fontsize = 0; |
2ef96bd6 |
1184 | |
20ee89e3 |
1185 | fe->timer_active = FALSE; |
1186 | |
9b4b03d3 |
1187 | fe->paste_data = NULL; |
1188 | fe->paste_data_len = 0; |
1189 | |
2ef96bd6 |
1190 | gtk_signal_connect(GTK_OBJECT(fe->window), "destroy", |
1191 | GTK_SIGNAL_FUNC(destroy), fe); |
1192 | gtk_signal_connect(GTK_OBJECT(fe->window), "key_press_event", |
1193 | GTK_SIGNAL_FUNC(key_event), fe); |
1194 | gtk_signal_connect(GTK_OBJECT(fe->area), "button_press_event", |
1195 | GTK_SIGNAL_FUNC(button_event), fe); |
74a4e547 |
1196 | gtk_signal_connect(GTK_OBJECT(fe->area), "button_release_event", |
1197 | GTK_SIGNAL_FUNC(button_event), fe); |
1198 | gtk_signal_connect(GTK_OBJECT(fe->area), "motion_notify_event", |
1199 | GTK_SIGNAL_FUNC(motion_event), fe); |
9b4b03d3 |
1200 | gtk_signal_connect(GTK_OBJECT(fe->area), "selection_get", |
1201 | GTK_SIGNAL_FUNC(selection_get), fe); |
1202 | gtk_signal_connect(GTK_OBJECT(fe->area), "selection_clear_event", |
1203 | GTK_SIGNAL_FUNC(selection_clear), fe); |
2ef96bd6 |
1204 | gtk_signal_connect(GTK_OBJECT(fe->area), "expose_event", |
1205 | GTK_SIGNAL_FUNC(expose_area), fe); |
fd1a1a2b |
1206 | gtk_signal_connect(GTK_OBJECT(fe->window), "map_event", |
1207 | GTK_SIGNAL_FUNC(map_window), fe); |
2ef96bd6 |
1208 | gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event", |
1209 | GTK_SIGNAL_FUNC(configure_area), fe); |
1210 | |
74a4e547 |
1211 | gtk_widget_add_events(GTK_WIDGET(fe->area), |
1212 | GDK_BUTTON_PRESS_MASK | |
1213 | GDK_BUTTON_RELEASE_MASK | |
1214 | GDK_BUTTON_MOTION_MASK); |
2ef96bd6 |
1215 | |
1216 | gtk_widget_show(fe->area); |
1217 | gtk_widget_show(fe->window); |
1218 | |
1219 | return fe; |
83680571 |
1220 | } |
1221 | |
1222 | int main(int argc, char **argv) |
1223 | { |
8b7938e7 |
1224 | char *pname = argv[0]; |
1225 | char *error; |
1226 | |
d91e1fc9 |
1227 | /* |
1228 | * Special standalone mode for generating puzzle IDs on the |
1229 | * command line. Useful for generating puzzles to be printed |
1230 | * out and solved offline (for puzzles where that even makes |
1231 | * sense - Solo, for example, is a lot more pencil-and-paper |
1232 | * friendly than Net!) |
1233 | * |
1234 | * Usage: |
1235 | * |
1236 | * <puzzle-name> --generate [<n> [<params>]] |
1237 | * |
1238 | * <n>, if present, is the number of puzzle IDs to generate. |
1239 | * <params>, if present, is the same type of parameter string |
1240 | * you would pass to the puzzle when running it in GUI mode, |
1241 | * including optional extras such as the expansion factor in |
1242 | * Rectangles and the difficulty level in Solo. |
1243 | * |
1244 | * If you specify <params>, you must also specify <n> (although |
1245 | * you may specify it to be 1). Sorry; that was the |
1246 | * simplest-to-parse command-line syntax I came up with. |
1247 | */ |
1248 | if (argc > 1 && !strcmp(argv[1], "--generate")) { |
1249 | int n = 1; |
1185e3c5 |
1250 | char *params = NULL, *seed = NULL; |
d91e1fc9 |
1251 | game_params *par; |
1252 | random_state *rs; |
1253 | char *parstr; |
1254 | |
d91e1fc9 |
1255 | if (argc > 2) |
1256 | n = atoi(argv[2]); |
1257 | if (argc > 3) |
1258 | params = argv[3]; |
1259 | |
1185e3c5 |
1260 | par = thegame.default_params(); |
1261 | if (params) { |
1262 | if ( (seed = strchr(params, '#')) != NULL ) |
1263 | *seed++ = '\0'; |
1264 | thegame.decode_params(par, params); |
1265 | } else { |
1266 | } |
1267 | parstr = thegame.encode_params(par, FALSE); |
1268 | |
1269 | { |
1270 | void *seeddata; |
1271 | int seedlen; |
1272 | if (seed) { |
1273 | seeddata = seed; |
1274 | seedlen = strlen(seed); |
1275 | } else { |
1276 | get_random_seed(&seeddata, &seedlen); |
1277 | } |
1278 | rs = random_init(seeddata, seedlen); |
1279 | } |
d91e1fc9 |
1280 | |
1281 | while (n-- > 0) { |
6f2d8d7c |
1282 | game_aux_info *aux = NULL; |
1185e3c5 |
1283 | char *desc = thegame.new_desc(par, rs, &aux); |
1284 | printf("%s:%s\n", parstr, desc); |
1285 | sfree(desc); |
6f2d8d7c |
1286 | if (aux) |
1287 | thegame.free_aux_info(aux); |
d91e1fc9 |
1288 | } |
8b7938e7 |
1289 | |
d91e1fc9 |
1290 | return 0; |
1291 | } else { |
1292 | |
1293 | gtk_init(&argc, &argv); |
1294 | |
1295 | if (!new_window(argc > 1 ? argv[1] : NULL, &error)) { |
1296 | fprintf(stderr, "%s: %s\n", pname, error); |
1297 | return 1; |
1298 | } |
1299 | |
1300 | gtk_main(); |
1301 | } |
83680571 |
1302 | |
1303 | return 0; |
1304 | } |