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> |
dafd6cf6 |
11 | #include <errno.h> |
720a8fb7 |
12 | |
8c1fd974 |
13 | #include <sys/time.h> |
14 | |
83680571 |
15 | #include <gtk/gtk.h> |
1482ee76 |
16 | #include <gdk/gdkkeysyms.h> |
83680571 |
17 | |
f12cdd72 |
18 | #include <gdk-pixbuf/gdk-pixbuf.h> |
19 | |
a9668451 |
20 | #include <gdk/gdkx.h> |
21 | #include <X11/Xlib.h> |
9b4b03d3 |
22 | #include <X11/Xutil.h> |
23 | #include <X11/Xatom.h> |
a9668451 |
24 | |
720a8fb7 |
25 | #include "puzzles.h" |
26 | |
1fad7a3b |
27 | #if GTK_CHECK_VERSION(2,0,0) |
28 | #define USE_PANGO |
29 | #endif |
30 | |
81ff8295 |
31 | #ifdef DEBUGGING |
32 | static FILE *debug_fp = NULL; |
33 | |
34 | void dputs(char *buf) |
35 | { |
36 | if (!debug_fp) { |
37 | debug_fp = fopen("debug.log", "w"); |
38 | } |
39 | |
40 | fputs(buf, stderr); |
41 | |
42 | if (debug_fp) { |
43 | fputs(buf, debug_fp); |
44 | fflush(debug_fp); |
45 | } |
46 | } |
47 | |
48 | void debug_printf(char *fmt, ...) |
49 | { |
50 | char buf[4096]; |
51 | va_list ap; |
52 | |
53 | va_start(ap, fmt); |
54 | vsprintf(buf, fmt, ap); |
55 | dputs(buf); |
56 | va_end(ap); |
57 | } |
58 | #endif |
59 | |
83680571 |
60 | /* ---------------------------------------------------------------------- |
61 | * Error reporting functions used elsewhere. |
62 | */ |
63 | |
720a8fb7 |
64 | void fatal(char *fmt, ...) |
65 | { |
66 | va_list ap; |
67 | |
68 | fprintf(stderr, "fatal error: "); |
69 | |
70 | va_start(ap, fmt); |
71 | vfprintf(stderr, fmt, ap); |
72 | va_end(ap); |
73 | |
74 | fprintf(stderr, "\n"); |
75 | exit(1); |
76 | } |
83680571 |
77 | |
78 | /* ---------------------------------------------------------------------- |
79 | * GTK front end to puzzles. |
80 | */ |
81 | |
fa3abef5 |
82 | static void changed_preset(frontend *fe); |
f92acd1a |
83 | |
4efb3868 |
84 | struct font { |
1fad7a3b |
85 | #ifdef USE_PANGO |
86 | PangoFontDescription *desc; |
87 | #else |
4efb3868 |
88 | GdkFont *font; |
1fad7a3b |
89 | #endif |
4efb3868 |
90 | int type; |
91 | int size; |
92 | }; |
93 | |
83680571 |
94 | /* |
95 | * This structure holds all the data relevant to a single window. |
96 | * In principle this would allow us to open multiple independent |
97 | * puzzle windows, although I can't currently see any real point in |
98 | * doing so. I'm just coding cleanly because there's no |
99 | * particularly good reason not to. |
100 | */ |
2ef96bd6 |
101 | struct frontend { |
83680571 |
102 | GtkWidget *window; |
a383a9be |
103 | GtkAccelGroup *accelgroup; |
7f77ea24 |
104 | GtkWidget *area; |
fd1a1a2b |
105 | GtkWidget *statusbar; |
106 | guint statusctx; |
2ef96bd6 |
107 | GdkPixmap *pixmap; |
108 | GdkColor *colours; |
109 | int ncolours; |
110 | GdkColormap *colmap; |
111 | int w, h; |
dafd6cf6 |
112 | midend *me; |
2ef96bd6 |
113 | GdkGC *gc; |
114 | int bbox_l, bbox_r, bbox_u, bbox_d; |
20ee89e3 |
115 | int timer_active, timer_id; |
8c1fd974 |
116 | struct timeval last_time; |
4efb3868 |
117 | struct font *fonts; |
118 | int nfonts, fontsize; |
c8230524 |
119 | config_item *cfg; |
5928817c |
120 | int cfg_which, cfgret; |
c8230524 |
121 | GtkWidget *cfgbox; |
23e8c9fd |
122 | void *paste_data; |
9b4b03d3 |
123 | int paste_data_len; |
1e3e152d |
124 | int pw, ph; /* pixmap size (w, h are area size) */ |
125 | int ox, oy; /* offset of pixmap in drawing area */ |
a4393230 |
126 | char *filesel_name; |
f92acd1a |
127 | int npresets; |
128 | GtkWidget **preset_bullets; |
129 | GtkWidget *preset_custom_bullet; |
fa3abef5 |
130 | GtkWidget *copy_menu_item; |
83680571 |
131 | }; |
132 | |
cbb5549e |
133 | void get_random_seed(void **randseed, int *randseedsize) |
134 | { |
102d0d14 |
135 | struct timeval *tvp = snew(struct timeval); |
136 | gettimeofday(tvp, NULL); |
137 | *randseed = (void *)tvp; |
138 | *randseedsize = sizeof(struct timeval); |
cbb5549e |
139 | } |
140 | |
2ef96bd6 |
141 | void frontend_default_colour(frontend *fe, float *output) |
142 | { |
143 | GdkColor col = fe->window->style->bg[GTK_STATE_NORMAL]; |
144 | output[0] = col.red / 65535.0; |
145 | output[1] = col.green / 65535.0; |
146 | output[2] = col.blue / 65535.0; |
147 | } |
148 | |
dafd6cf6 |
149 | void gtk_status_bar(void *handle, char *text) |
fd1a1a2b |
150 | { |
dafd6cf6 |
151 | frontend *fe = (frontend *)handle; |
48dcdd62 |
152 | |
fd1a1a2b |
153 | assert(fe->statusbar); |
154 | |
83c0438f |
155 | gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx); |
156 | gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, text); |
fd1a1a2b |
157 | } |
158 | |
dafd6cf6 |
159 | void gtk_start_draw(void *handle) |
2ef96bd6 |
160 | { |
dafd6cf6 |
161 | frontend *fe = (frontend *)handle; |
2ef96bd6 |
162 | fe->gc = gdk_gc_new(fe->area->window); |
163 | fe->bbox_l = fe->w; |
164 | fe->bbox_r = 0; |
165 | fe->bbox_u = fe->h; |
166 | fe->bbox_d = 0; |
167 | } |
168 | |
dafd6cf6 |
169 | void gtk_clip(void *handle, int x, int y, int w, int h) |
4efb3868 |
170 | { |
dafd6cf6 |
171 | frontend *fe = (frontend *)handle; |
4efb3868 |
172 | GdkRectangle rect; |
173 | |
174 | rect.x = x; |
175 | rect.y = y; |
176 | rect.width = w; |
177 | rect.height = h; |
178 | |
179 | gdk_gc_set_clip_rectangle(fe->gc, &rect); |
180 | } |
181 | |
dafd6cf6 |
182 | void gtk_unclip(void *handle) |
4efb3868 |
183 | { |
dafd6cf6 |
184 | frontend *fe = (frontend *)handle; |
4efb3868 |
185 | GdkRectangle rect; |
186 | |
187 | rect.x = 0; |
188 | rect.y = 0; |
189 | rect.width = fe->w; |
190 | rect.height = fe->h; |
191 | |
192 | gdk_gc_set_clip_rectangle(fe->gc, &rect); |
193 | } |
194 | |
dafd6cf6 |
195 | void gtk_draw_text(void *handle, int x, int y, int fonttype, int fontsize, |
196 | int align, int colour, char *text) |
4efb3868 |
197 | { |
dafd6cf6 |
198 | frontend *fe = (frontend *)handle; |
4efb3868 |
199 | int i; |
200 | |
201 | /* |
202 | * Find or create the font. |
203 | */ |
204 | for (i = 0; i < fe->nfonts; i++) |
205 | if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize) |
206 | break; |
207 | |
208 | if (i == fe->nfonts) { |
209 | if (fe->fontsize <= fe->nfonts) { |
210 | fe->fontsize = fe->nfonts + 10; |
211 | fe->fonts = sresize(fe->fonts, fe->fontsize, struct font); |
212 | } |
213 | |
214 | fe->nfonts++; |
215 | |
216 | fe->fonts[i].type = fonttype; |
217 | fe->fonts[i].size = fontsize; |
218 | |
1fad7a3b |
219 | #ifdef USE_PANGO |
4efb3868 |
220 | /* |
a9668451 |
221 | * Use Pango to find the closest match to the requested |
222 | * font. |
4efb3868 |
223 | */ |
a9668451 |
224 | { |
225 | PangoFontDescription *fd; |
226 | |
227 | fd = pango_font_description_new(); |
228 | /* `Monospace' and `Sans' are meta-families guaranteed to exist */ |
229 | pango_font_description_set_family(fd, fonttype == FONT_FIXED ? |
230 | "Monospace" : "Sans"); |
2e837d78 |
231 | pango_font_description_set_weight(fd, PANGO_WEIGHT_BOLD); |
a9668451 |
232 | /* |
233 | * I found some online Pango documentation which |
234 | * described a function called |
235 | * pango_font_description_set_absolute_size(), which is |
236 | * _exactly_ what I want here. Unfortunately, none of |
237 | * my local Pango installations have it (presumably |
238 | * they're too old), so I'm going to have to hack round |
239 | * it by figuring out the point size myself. This |
240 | * limits me to X and probably also breaks in later |
241 | * Pango installations, so ideally I should add another |
242 | * CHECK_VERSION type ifdef and use set_absolute_size |
243 | * where available. All very annoying. |
244 | */ |
245 | #ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION |
246 | pango_font_description_set_absolute_size(fd, PANGO_SCALE*fontsize); |
247 | #else |
248 | { |
249 | Display *d = GDK_DISPLAY(); |
250 | int s = DefaultScreen(d); |
251 | double resolution = |
252 | (PANGO_SCALE * 72.27 / 25.4) * |
253 | ((double) DisplayWidthMM(d, s) / DisplayWidth (d, s)); |
254 | pango_font_description_set_size(fd, resolution * fontsize); |
255 | } |
256 | #endif |
1fad7a3b |
257 | fe->fonts[i].desc = fd; |
a9668451 |
258 | } |
259 | |
1fad7a3b |
260 | #else |
261 | /* |
262 | * In GTK 1.2, I don't know of any plausible way to |
263 | * pick a suitable font, so I'm just going to be |
264 | * tedious. |
265 | */ |
266 | fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ? |
267 | "fixed" : "variable"); |
a9668451 |
268 | #endif |
1fad7a3b |
269 | |
270 | } |
271 | |
272 | /* |
273 | * Set the colour. |
274 | */ |
275 | gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); |
276 | |
277 | #ifdef USE_PANGO |
278 | |
279 | { |
280 | PangoLayout *layout; |
281 | PangoRectangle rect; |
282 | |
283 | /* |
284 | * Create a layout. |
285 | */ |
286 | layout = pango_layout_new(gtk_widget_get_pango_context(fe->area)); |
287 | pango_layout_set_font_description(layout, fe->fonts[i].desc); |
288 | pango_layout_set_text(layout, text, strlen(text)); |
289 | pango_layout_get_pixel_extents(layout, NULL, &rect); |
290 | |
291 | if (align & ALIGN_VCENTRE) |
292 | rect.y -= rect.height / 2; |
86e60e3d |
293 | else |
294 | rect.y -= rect.height; |
1fad7a3b |
295 | |
296 | if (align & ALIGN_HCENTRE) |
297 | rect.x -= rect.width / 2; |
298 | else if (align & ALIGN_HRIGHT) |
299 | rect.x -= rect.width; |
300 | |
301 | gdk_draw_layout(fe->pixmap, fe->gc, rect.x + x, rect.y + y, layout); |
302 | |
303 | g_object_unref(layout); |
4efb3868 |
304 | } |
305 | |
1fad7a3b |
306 | #else |
4efb3868 |
307 | /* |
308 | * Find string dimensions and process alignment. |
309 | */ |
310 | { |
311 | int lb, rb, wid, asc, desc; |
312 | |
3bcf831a |
313 | /* |
314 | * Measure vertical string extents with respect to the same |
315 | * string always... |
316 | */ |
317 | gdk_string_extents(fe->fonts[i].font, |
318 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
4efb3868 |
319 | &lb, &rb, &wid, &asc, &desc); |
320 | if (align & ALIGN_VCENTRE) |
321 | y += asc - (asc+desc)/2; |
86e60e3d |
322 | else |
323 | y += asc; |
4efb3868 |
324 | |
3bcf831a |
325 | /* |
326 | * ... but horizontal extents with respect to the provided |
327 | * string. This means that multiple pieces of text centred |
328 | * on the same y-coordinate don't have different baselines. |
329 | */ |
330 | gdk_string_extents(fe->fonts[i].font, text, |
331 | &lb, &rb, &wid, &asc, &desc); |
332 | |
4efb3868 |
333 | if (align & ALIGN_HCENTRE) |
334 | x -= wid / 2; |
335 | else if (align & ALIGN_HRIGHT) |
336 | x -= wid; |
337 | |
338 | } |
339 | |
340 | /* |
1fad7a3b |
341 | * Actually draw the text. |
4efb3868 |
342 | */ |
4efb3868 |
343 | gdk_draw_string(fe->pixmap, fe->fonts[i].font, fe->gc, x, y, text); |
1fad7a3b |
344 | #endif |
345 | |
4efb3868 |
346 | } |
347 | |
dafd6cf6 |
348 | void gtk_draw_rect(void *handle, int x, int y, int w, int h, int colour) |
2ef96bd6 |
349 | { |
dafd6cf6 |
350 | frontend *fe = (frontend *)handle; |
2ef96bd6 |
351 | gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); |
352 | gdk_draw_rectangle(fe->pixmap, fe->gc, 1, x, y, w, h); |
353 | } |
354 | |
dafd6cf6 |
355 | void gtk_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour) |
2ef96bd6 |
356 | { |
dafd6cf6 |
357 | frontend *fe = (frontend *)handle; |
2ef96bd6 |
358 | gdk_gc_set_foreground(fe->gc, &fe->colours[colour]); |
359 | gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2); |
360 | } |
361 | |
dafd6cf6 |
362 | void gtk_draw_poly(void *handle, int *coords, int npoints, |
363 | int fillcolour, int outlinecolour) |
2ef96bd6 |
364 | { |
dafd6cf6 |
365 | frontend *fe = (frontend *)handle; |
2ef96bd6 |
366 | GdkPoint *points = snewn(npoints, GdkPoint); |
367 | int i; |
368 | |
369 | for (i = 0; i < npoints; i++) { |
370 | points[i].x = coords[i*2]; |
371 | points[i].y = coords[i*2+1]; |
372 | } |
373 | |
28b5987d |
374 | if (fillcolour >= 0) { |
375 | gdk_gc_set_foreground(fe->gc, &fe->colours[fillcolour]); |
376 | gdk_draw_polygon(fe->pixmap, fe->gc, TRUE, points, npoints); |
377 | } |
378 | assert(outlinecolour >= 0); |
379 | gdk_gc_set_foreground(fe->gc, &fe->colours[outlinecolour]); |
dddad8fb |
380 | |
381 | /* |
382 | * In principle we ought to be able to use gdk_draw_polygon for |
383 | * the outline as well. In fact, it turns out to interact badly |
384 | * with a clipping region, for no terribly obvious reason, so I |
385 | * draw the outline as a sequence of lines instead. |
386 | */ |
387 | for (i = 0; i < npoints; i++) |
388 | gdk_draw_line(fe->pixmap, fe->gc, |
389 | points[i].x, points[i].y, |
390 | points[(i+1)%npoints].x, points[(i+1)%npoints].y); |
2ef96bd6 |
391 | |
392 | sfree(points); |
393 | } |
394 | |
dafd6cf6 |
395 | void gtk_draw_circle(void *handle, int cx, int cy, int radius, |
396 | int fillcolour, int outlinecolour) |
550efd7f |
397 | { |
dafd6cf6 |
398 | frontend *fe = (frontend *)handle; |
28b5987d |
399 | if (fillcolour >= 0) { |
400 | gdk_gc_set_foreground(fe->gc, &fe->colours[fillcolour]); |
401 | gdk_draw_arc(fe->pixmap, fe->gc, TRUE, |
402 | cx - radius, cy - radius, |
403 | 2 * radius, 2 * radius, 0, 360 * 64); |
404 | } |
405 | |
406 | assert(outlinecolour >= 0); |
407 | gdk_gc_set_foreground(fe->gc, &fe->colours[outlinecolour]); |
408 | gdk_draw_arc(fe->pixmap, fe->gc, FALSE, |
409 | cx - radius, cy - radius, |
410 | 2 * radius, 2 * radius, 0, 360 * 64); |
550efd7f |
411 | } |
412 | |
3161048d |
413 | struct blitter { |
414 | GdkPixmap *pixmap; |
415 | int w, h, x, y; |
416 | }; |
417 | |
dafd6cf6 |
418 | blitter *gtk_blitter_new(void *handle, int w, int h) |
3161048d |
419 | { |
420 | /* |
421 | * We can't create the pixmap right now, because fe->window |
422 | * might not yet exist. So we just cache w and h and create it |
423 | * during the firs call to blitter_save. |
424 | */ |
425 | blitter *bl = snew(blitter); |
426 | bl->pixmap = NULL; |
427 | bl->w = w; |
428 | bl->h = h; |
429 | return bl; |
430 | } |
431 | |
dafd6cf6 |
432 | void gtk_blitter_free(void *handle, blitter *bl) |
3161048d |
433 | { |
434 | if (bl->pixmap) |
435 | gdk_pixmap_unref(bl->pixmap); |
436 | sfree(bl); |
437 | } |
438 | |
dafd6cf6 |
439 | void gtk_blitter_save(void *handle, blitter *bl, int x, int y) |
3161048d |
440 | { |
dafd6cf6 |
441 | frontend *fe = (frontend *)handle; |
3161048d |
442 | if (!bl->pixmap) |
443 | bl->pixmap = gdk_pixmap_new(fe->area->window, bl->w, bl->h, -1); |
444 | bl->x = x; |
445 | bl->y = y; |
446 | gdk_draw_pixmap(bl->pixmap, |
447 | fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)], |
448 | fe->pixmap, |
449 | x, y, 0, 0, bl->w, bl->h); |
450 | } |
451 | |
dafd6cf6 |
452 | void gtk_blitter_load(void *handle, blitter *bl, int x, int y) |
3161048d |
453 | { |
dafd6cf6 |
454 | frontend *fe = (frontend *)handle; |
3161048d |
455 | assert(bl->pixmap); |
456 | if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) { |
457 | x = bl->x; |
458 | y = bl->y; |
459 | } |
460 | gdk_draw_pixmap(fe->pixmap, |
461 | fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)], |
462 | bl->pixmap, |
463 | 0, 0, x, y, bl->w, bl->h); |
464 | } |
465 | |
dafd6cf6 |
466 | void gtk_draw_update(void *handle, int x, int y, int w, int h) |
2ef96bd6 |
467 | { |
dafd6cf6 |
468 | frontend *fe = (frontend *)handle; |
2ef96bd6 |
469 | if (fe->bbox_l > x ) fe->bbox_l = x ; |
470 | if (fe->bbox_r < x+w) fe->bbox_r = x+w; |
471 | if (fe->bbox_u > y ) fe->bbox_u = y ; |
472 | if (fe->bbox_d < y+h) fe->bbox_d = y+h; |
473 | } |
474 | |
dafd6cf6 |
475 | void gtk_end_draw(void *handle) |
2ef96bd6 |
476 | { |
dafd6cf6 |
477 | frontend *fe = (frontend *)handle; |
2ef96bd6 |
478 | gdk_gc_unref(fe->gc); |
479 | fe->gc = NULL; |
480 | |
481 | if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) { |
482 | gdk_draw_pixmap(fe->area->window, |
483 | fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)], |
484 | fe->pixmap, |
485 | fe->bbox_l, fe->bbox_u, |
1e3e152d |
486 | fe->ox + fe->bbox_l, fe->oy + fe->bbox_u, |
2ef96bd6 |
487 | fe->bbox_r - fe->bbox_l, fe->bbox_d - fe->bbox_u); |
488 | } |
489 | } |
490 | |
dafd6cf6 |
491 | const struct drawing_api gtk_drawing = { |
492 | gtk_draw_text, |
493 | gtk_draw_rect, |
494 | gtk_draw_line, |
495 | gtk_draw_poly, |
496 | gtk_draw_circle, |
497 | gtk_draw_update, |
498 | gtk_clip, |
499 | gtk_unclip, |
500 | gtk_start_draw, |
501 | gtk_end_draw, |
502 | gtk_status_bar, |
503 | gtk_blitter_new, |
504 | gtk_blitter_free, |
505 | gtk_blitter_save, |
506 | gtk_blitter_load, |
507 | NULL, NULL, NULL, NULL, NULL, NULL, /* {begin,end}_{doc,page,puzzle} */ |
508 | NULL, /* line_width */ |
509 | }; |
510 | |
83680571 |
511 | static void destroy(GtkWidget *widget, gpointer data) |
512 | { |
f49650a7 |
513 | frontend *fe = (frontend *)data; |
514 | deactivate_timer(fe); |
a4393230 |
515 | midend_free(fe->me); |
83680571 |
516 | gtk_main_quit(); |
517 | } |
518 | |
2ef96bd6 |
519 | static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data) |
7f77ea24 |
520 | { |
2ef96bd6 |
521 | frontend *fe = (frontend *)data; |
1482ee76 |
522 | int keyval; |
f0ee053c |
523 | int shift = (event->state & GDK_SHIFT_MASK) ? MOD_SHFT : 0; |
524 | int ctrl = (event->state & GDK_CONTROL_MASK) ? MOD_CTRL : 0; |
7f77ea24 |
525 | |
2ef96bd6 |
526 | if (!fe->pixmap) |
527 | return TRUE; |
7f77ea24 |
528 | |
08d7c4ba |
529 | #if !GTK_CHECK_VERSION(2,0,0) |
530 | /* Gtk 1.2 passes a key event to this function even if it's also |
531 | * defined as an accelerator. |
532 | * Gtk 2 doesn't do this, and this function appears not to exist there. */ |
533 | if (fe->accelgroup && |
534 | gtk_accel_group_get_entry(fe->accelgroup, |
535 | event->keyval, event->state)) |
536 | return TRUE; |
537 | #endif |
538 | |
3c833d45 |
539 | if (event->keyval == GDK_Up) |
f0ee053c |
540 | keyval = shift | ctrl | CURSOR_UP; |
3c833d45 |
541 | else if (event->keyval == GDK_KP_Up || event->keyval == GDK_KP_8) |
542 | keyval = MOD_NUM_KEYPAD | '8'; |
543 | else if (event->keyval == GDK_Down) |
f0ee053c |
544 | keyval = shift | ctrl | CURSOR_DOWN; |
3c833d45 |
545 | else if (event->keyval == GDK_KP_Down || event->keyval == GDK_KP_2) |
546 | keyval = MOD_NUM_KEYPAD | '2'; |
547 | else if (event->keyval == GDK_Left) |
f0ee053c |
548 | keyval = shift | ctrl | CURSOR_LEFT; |
3c833d45 |
549 | else if (event->keyval == GDK_KP_Left || event->keyval == GDK_KP_4) |
550 | keyval = MOD_NUM_KEYPAD | '4'; |
551 | else if (event->keyval == GDK_Right) |
f0ee053c |
552 | keyval = shift | ctrl | CURSOR_RIGHT; |
3c833d45 |
553 | else if (event->keyval == GDK_KP_Right || event->keyval == GDK_KP_6) |
554 | keyval = MOD_NUM_KEYPAD | '6'; |
c71454c0 |
555 | else if (event->keyval == GDK_KP_Home || event->keyval == GDK_KP_7) |
3c833d45 |
556 | keyval = MOD_NUM_KEYPAD | '7'; |
c71454c0 |
557 | else if (event->keyval == GDK_KP_End || event->keyval == GDK_KP_1) |
3c833d45 |
558 | keyval = MOD_NUM_KEYPAD | '1'; |
c71454c0 |
559 | else if (event->keyval == GDK_KP_Page_Up || event->keyval == GDK_KP_9) |
3c833d45 |
560 | keyval = MOD_NUM_KEYPAD | '9'; |
c71454c0 |
561 | else if (event->keyval == GDK_KP_Page_Down || event->keyval == GDK_KP_3) |
3c833d45 |
562 | keyval = MOD_NUM_KEYPAD | '3'; |
563 | else if (event->keyval == GDK_KP_Insert || event->keyval == GDK_KP_0) |
564 | keyval = MOD_NUM_KEYPAD | '0'; |
565 | else if (event->keyval == GDK_KP_Begin || event->keyval == GDK_KP_5) |
566 | keyval = MOD_NUM_KEYPAD | '5'; |
0eb4d76e |
567 | else if (event->keyval == GDK_BackSpace || |
568 | event->keyval == GDK_Delete || |
569 | event->keyval == GDK_KP_Delete) |
570 | keyval = '\177'; |
3c833d45 |
571 | else if (event->string[0] && !event->string[1]) |
572 | keyval = (unsigned char)event->string[0]; |
1482ee76 |
573 | else |
574 | keyval = -1; |
575 | |
576 | if (keyval >= 0 && |
577 | !midend_process_key(fe->me, 0, 0, keyval)) |
2ef96bd6 |
578 | gtk_widget_destroy(fe->window); |
7f77ea24 |
579 | |
580 | return TRUE; |
581 | } |
582 | |
2ef96bd6 |
583 | static gint button_event(GtkWidget *widget, GdkEventButton *event, |
584 | gpointer data) |
7f77ea24 |
585 | { |
2ef96bd6 |
586 | frontend *fe = (frontend *)data; |
587 | int button; |
588 | |
589 | if (!fe->pixmap) |
590 | return TRUE; |
591 | |
74a4e547 |
592 | if (event->type != GDK_BUTTON_PRESS && event->type != GDK_BUTTON_RELEASE) |
2ef96bd6 |
593 | return TRUE; |
7f77ea24 |
594 | |
b0f06719 |
595 | if (event->button == 2 || (event->state & GDK_SHIFT_MASK)) |
2ef96bd6 |
596 | button = MIDDLE_BUTTON; |
97c2790f |
597 | else if (event->button == 3 || (event->state & GDK_MOD1_MASK)) |
598 | button = RIGHT_BUTTON; |
b0f06719 |
599 | else if (event->button == 1) |
600 | button = LEFT_BUTTON; |
2ef96bd6 |
601 | else |
602 | return FALSE; /* don't even know what button! */ |
603 | |
74a4e547 |
604 | if (event->type == GDK_BUTTON_RELEASE) |
605 | button += LEFT_RELEASE - LEFT_BUTTON; |
606 | |
1e3e152d |
607 | if (!midend_process_key(fe->me, event->x - fe->ox, |
608 | event->y - fe->oy, button)) |
74a4e547 |
609 | gtk_widget_destroy(fe->window); |
610 | |
611 | return TRUE; |
612 | } |
613 | |
614 | static gint motion_event(GtkWidget *widget, GdkEventMotion *event, |
615 | gpointer data) |
616 | { |
617 | frontend *fe = (frontend *)data; |
618 | int button; |
619 | |
620 | if (!fe->pixmap) |
621 | return TRUE; |
622 | |
74a4e547 |
623 | if (event->state & (GDK_BUTTON2_MASK | GDK_SHIFT_MASK)) |
624 | button = MIDDLE_DRAG; |
625 | else if (event->state & GDK_BUTTON1_MASK) |
626 | button = LEFT_DRAG; |
627 | else if (event->state & GDK_BUTTON3_MASK) |
628 | button = RIGHT_DRAG; |
629 | else |
630 | return FALSE; /* don't even know what button! */ |
631 | |
1e3e152d |
632 | if (!midend_process_key(fe->me, event->x - fe->ox, |
633 | event->y - fe->oy, button)) |
2ef96bd6 |
634 | gtk_widget_destroy(fe->window); |
7f77ea24 |
635 | |
636 | return TRUE; |
637 | } |
638 | |
2ef96bd6 |
639 | static gint expose_area(GtkWidget *widget, GdkEventExpose *event, |
640 | gpointer data) |
83680571 |
641 | { |
2ef96bd6 |
642 | frontend *fe = (frontend *)data; |
643 | |
644 | if (fe->pixmap) { |
645 | gdk_draw_pixmap(widget->window, |
646 | widget->style->fg_gc[GTK_WIDGET_STATE(widget)], |
647 | fe->pixmap, |
1e3e152d |
648 | event->area.x - fe->ox, event->area.y - fe->oy, |
2ef96bd6 |
649 | event->area.x, event->area.y, |
650 | event->area.width, event->area.height); |
651 | } |
652 | return TRUE; |
653 | } |
654 | |
fd1a1a2b |
655 | static gint map_window(GtkWidget *widget, GdkEvent *event, |
656 | gpointer data) |
657 | { |
658 | frontend *fe = (frontend *)data; |
659 | |
660 | /* |
661 | * Apparently we need to do this because otherwise the status |
662 | * bar will fail to update immediately. Annoying, but there we |
663 | * go. |
664 | */ |
665 | gtk_widget_queue_draw(fe->window); |
666 | |
667 | return TRUE; |
668 | } |
669 | |
2ef96bd6 |
670 | static gint configure_area(GtkWidget *widget, |
671 | GdkEventConfigure *event, gpointer data) |
672 | { |
673 | frontend *fe = (frontend *)data; |
674 | GdkGC *gc; |
1e3e152d |
675 | int x, y; |
2ef96bd6 |
676 | |
eb2ad6f1 |
677 | if (fe->pixmap) |
678 | gdk_pixmap_unref(fe->pixmap); |
679 | |
1e3e152d |
680 | x = fe->w = event->width; |
681 | y = fe->h = event->height; |
682 | midend_size(fe->me, &x, &y, TRUE); |
683 | fe->pw = x; |
684 | fe->ph = y; |
685 | fe->ox = (fe->w - fe->pw) / 2; |
686 | fe->oy = (fe->h - fe->ph) / 2; |
687 | |
688 | fe->pixmap = gdk_pixmap_new(widget->window, fe->pw, fe->ph, -1); |
2ef96bd6 |
689 | |
690 | gc = gdk_gc_new(fe->area->window); |
691 | gdk_gc_set_foreground(gc, &fe->colours[0]); |
1e3e152d |
692 | gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->pw, fe->ph); |
47432032 |
693 | gdk_draw_rectangle(widget->window, gc, 1, 0, 0, |
694 | event->width, event->height); |
2ef96bd6 |
695 | gdk_gc_unref(gc); |
696 | |
008b4378 |
697 | midend_force_redraw(fe->me); |
2ef96bd6 |
698 | |
699 | return TRUE; |
700 | } |
701 | |
702 | static gint timer_func(gpointer data) |
703 | { |
704 | frontend *fe = (frontend *)data; |
705 | |
8c1fd974 |
706 | if (fe->timer_active) { |
707 | struct timeval now; |
708 | float elapsed; |
709 | gettimeofday(&now, NULL); |
710 | elapsed = ((now.tv_usec - fe->last_time.tv_usec) * 0.000001F + |
711 | (now.tv_sec - fe->last_time.tv_sec)); |
712 | midend_timer(fe->me, elapsed); /* may clear timer_active */ |
713 | fe->last_time = now; |
714 | } |
2ef96bd6 |
715 | |
716 | return fe->timer_active; |
717 | } |
718 | |
719 | void deactivate_timer(frontend *fe) |
720 | { |
dafd6cf6 |
721 | if (!fe) |
722 | return; /* can happen due to --generate */ |
20ee89e3 |
723 | if (fe->timer_active) |
724 | gtk_timeout_remove(fe->timer_id); |
2ef96bd6 |
725 | fe->timer_active = FALSE; |
726 | } |
727 | |
728 | void activate_timer(frontend *fe) |
729 | { |
dafd6cf6 |
730 | if (!fe) |
731 | return; /* can happen due to --generate */ |
8c1fd974 |
732 | if (!fe->timer_active) { |
20ee89e3 |
733 | fe->timer_id = gtk_timeout_add(20, timer_func, fe); |
8c1fd974 |
734 | gettimeofday(&fe->last_time, NULL); |
735 | } |
2ef96bd6 |
736 | fe->timer_active = TRUE; |
737 | } |
738 | |
c8230524 |
739 | static void window_destroy(GtkWidget *widget, gpointer data) |
740 | { |
741 | gtk_main_quit(); |
742 | } |
743 | |
668be019 |
744 | static void msgbox_button_clicked(GtkButton *button, gpointer data) |
c8230524 |
745 | { |
668be019 |
746 | GtkWidget *window = GTK_WIDGET(data); |
747 | int v, *ip; |
748 | |
749 | ip = (int *)gtk_object_get_data(GTK_OBJECT(window), "user-data"); |
750 | v = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "user-data")); |
751 | *ip = v; |
752 | |
c8230524 |
753 | gtk_widget_destroy(GTK_WIDGET(data)); |
754 | } |
755 | |
a163b7c2 |
756 | static int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) |
757 | { |
758 | GtkObject *cancelbutton = GTK_OBJECT(data); |
759 | |
760 | /* |
761 | * `Escape' effectively clicks the cancel button |
762 | */ |
763 | if (event->keyval == GDK_Escape) { |
764 | gtk_signal_emit_by_name(GTK_OBJECT(cancelbutton), "clicked"); |
765 | return TRUE; |
766 | } |
767 | |
768 | return FALSE; |
769 | } |
770 | |
668be019 |
771 | enum { MB_OK, MB_YESNO }; |
772 | |
773 | int message_box(GtkWidget *parent, char *title, char *msg, int centre, |
774 | int type) |
c8230524 |
775 | { |
668be019 |
776 | GtkWidget *window, *hbox, *text, *button; |
777 | char *titles; |
778 | int i, def, cancel; |
c8230524 |
779 | |
780 | window = gtk_dialog_new(); |
781 | text = gtk_label_new(msg); |
782 | gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0); |
783 | hbox = gtk_hbox_new(FALSE, 0); |
784 | gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20); |
785 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), |
786 | hbox, FALSE, FALSE, 20); |
787 | gtk_widget_show(text); |
788 | gtk_widget_show(hbox); |
97098757 |
789 | gtk_window_set_title(GTK_WINDOW(window), title); |
c8230524 |
790 | gtk_label_set_line_wrap(GTK_LABEL(text), TRUE); |
668be019 |
791 | |
792 | if (type == MB_OK) { |
793 | titles = "OK\0"; |
794 | def = cancel = 0; |
795 | } else { |
796 | assert(type == MB_YESNO); |
797 | titles = "Yes\0No\0"; |
798 | def = 0; |
799 | cancel = 1; |
800 | } |
801 | i = 0; |
802 | |
803 | while (*titles) { |
804 | button = gtk_button_new_with_label(titles); |
805 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area), |
806 | button, FALSE, FALSE, 0); |
807 | gtk_widget_show(button); |
808 | if (i == def) { |
809 | GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT); |
810 | gtk_window_set_default(GTK_WINDOW(window), button); |
811 | } |
812 | if (i == cancel) { |
813 | gtk_signal_connect(GTK_OBJECT(window), "key_press_event", |
814 | GTK_SIGNAL_FUNC(win_key_press), button); |
815 | } |
816 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
817 | GTK_SIGNAL_FUNC(msgbox_button_clicked), window); |
818 | gtk_object_set_data(GTK_OBJECT(button), "user-data", |
819 | GINT_TO_POINTER(i)); |
820 | titles += strlen(titles)+1; |
821 | i++; |
822 | } |
823 | gtk_object_set_data(GTK_OBJECT(window), "user-data", |
824 | GINT_TO_POINTER(&i)); |
a1ed9f0e |
825 | gtk_signal_connect(GTK_OBJECT(window), "destroy", |
826 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
c8230524 |
827 | gtk_window_set_modal(GTK_WINDOW(window), TRUE); |
828 | gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent)); |
23e8c9fd |
829 | /* set_transient_window_pos(parent, window); */ |
c8230524 |
830 | gtk_widget_show(window); |
668be019 |
831 | i = -1; |
c8230524 |
832 | gtk_main(); |
668be019 |
833 | return (type == MB_YESNO ? i == 0 : TRUE); |
c8230524 |
834 | } |
835 | |
97098757 |
836 | void error_box(GtkWidget *parent, char *msg) |
837 | { |
668be019 |
838 | message_box(parent, "Error", msg, FALSE, MB_OK); |
97098757 |
839 | } |
840 | |
c8230524 |
841 | static void config_ok_button_clicked(GtkButton *button, gpointer data) |
842 | { |
843 | frontend *fe = (frontend *)data; |
844 | char *err; |
845 | |
5928817c |
846 | err = midend_set_config(fe->me, fe->cfg_which, fe->cfg); |
c8230524 |
847 | |
848 | if (err) |
849 | error_box(fe->cfgbox, err); |
850 | else { |
851 | fe->cfgret = TRUE; |
852 | gtk_widget_destroy(fe->cfgbox); |
fa3abef5 |
853 | changed_preset(fe); |
c8230524 |
854 | } |
855 | } |
856 | |
857 | static void config_cancel_button_clicked(GtkButton *button, gpointer data) |
858 | { |
859 | frontend *fe = (frontend *)data; |
860 | |
861 | gtk_widget_destroy(fe->cfgbox); |
862 | } |
863 | |
a163b7c2 |
864 | static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data) |
865 | { |
866 | /* |
867 | * GtkEntry has a nasty habit of eating the Return key, which |
868 | * is unhelpful since it doesn't actually _do_ anything with it |
869 | * (it calls gtk_widget_activate, but our edit boxes never need |
870 | * activating). So I catch Return before GtkEntry sees it, and |
871 | * pass it straight on to the parent widget. Effect: hitting |
872 | * Return in an edit box will now activate the default button |
873 | * in the dialog just like it will everywhere else. |
874 | */ |
875 | if (event->keyval == GDK_Return && widget->parent != NULL) { |
876 | gint return_val; |
877 | gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event"); |
878 | gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event", |
879 | event, &return_val); |
880 | return return_val; |
881 | } |
882 | return FALSE; |
883 | } |
884 | |
c8230524 |
885 | static void editbox_changed(GtkEditable *ed, gpointer data) |
886 | { |
887 | config_item *i = (config_item *)data; |
888 | |
889 | sfree(i->sval); |
890 | i->sval = dupstr(gtk_entry_get_text(GTK_ENTRY(ed))); |
891 | } |
892 | |
893 | static void button_toggled(GtkToggleButton *tb, gpointer data) |
894 | { |
895 | config_item *i = (config_item *)data; |
896 | |
897 | i->ival = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tb)); |
898 | } |
899 | |
900 | static void droplist_sel(GtkMenuItem *item, gpointer data) |
901 | { |
902 | config_item *i = (config_item *)data; |
903 | |
904 | i->ival = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item), |
905 | "user-data")); |
906 | } |
907 | |
5928817c |
908 | static int get_config(frontend *fe, int which) |
c8230524 |
909 | { |
a163b7c2 |
910 | GtkWidget *w, *table, *cancel; |
5928817c |
911 | char *title; |
c8230524 |
912 | config_item *i; |
913 | int y; |
914 | |
5928817c |
915 | fe->cfg = midend_get_config(fe->me, which, &title); |
916 | fe->cfg_which = which; |
c8230524 |
917 | fe->cfgret = FALSE; |
918 | |
919 | fe->cfgbox = gtk_dialog_new(); |
5928817c |
920 | gtk_window_set_title(GTK_WINDOW(fe->cfgbox), title); |
921 | sfree(title); |
c8230524 |
922 | |
923 | w = gtk_button_new_with_label("OK"); |
924 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area), |
925 | w, FALSE, FALSE, 0); |
926 | gtk_widget_show(w); |
927 | GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT); |
928 | gtk_window_set_default(GTK_WINDOW(fe->cfgbox), w); |
929 | gtk_signal_connect(GTK_OBJECT(w), "clicked", |
930 | GTK_SIGNAL_FUNC(config_ok_button_clicked), fe); |
931 | |
932 | w = gtk_button_new_with_label("Cancel"); |
933 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->action_area), |
934 | w, FALSE, FALSE, 0); |
935 | gtk_widget_show(w); |
936 | gtk_signal_connect(GTK_OBJECT(w), "clicked", |
937 | GTK_SIGNAL_FUNC(config_cancel_button_clicked), fe); |
a163b7c2 |
938 | cancel = w; |
c8230524 |
939 | |
940 | table = gtk_table_new(1, 2, FALSE); |
941 | y = 0; |
942 | gtk_box_pack_end(GTK_BOX(GTK_DIALOG(fe->cfgbox)->vbox), |
943 | table, FALSE, FALSE, 0); |
944 | gtk_widget_show(table); |
945 | |
95709966 |
946 | for (i = fe->cfg; i->type != C_END; i++) { |
c8230524 |
947 | gtk_table_resize(GTK_TABLE(table), y+1, 2); |
948 | |
949 | switch (i->type) { |
95709966 |
950 | case C_STRING: |
c8230524 |
951 | /* |
952 | * Edit box with a label beside it. |
953 | */ |
954 | |
955 | w = gtk_label_new(i->name); |
956 | gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5); |
957 | gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1, |
958 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
959 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
960 | 3, 3); |
961 | gtk_widget_show(w); |
962 | |
963 | w = gtk_entry_new(); |
964 | gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1, |
965 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
966 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
967 | 3, 3); |
968 | gtk_entry_set_text(GTK_ENTRY(w), i->sval); |
969 | gtk_signal_connect(GTK_OBJECT(w), "changed", |
970 | GTK_SIGNAL_FUNC(editbox_changed), i); |
a163b7c2 |
971 | gtk_signal_connect(GTK_OBJECT(w), "key_press_event", |
972 | GTK_SIGNAL_FUNC(editbox_key), NULL); |
c8230524 |
973 | gtk_widget_show(w); |
974 | |
975 | break; |
976 | |
95709966 |
977 | case C_BOOLEAN: |
c8230524 |
978 | /* |
979 | * Simple checkbox. |
980 | */ |
981 | w = gtk_check_button_new_with_label(i->name); |
982 | gtk_signal_connect(GTK_OBJECT(w), "toggled", |
983 | GTK_SIGNAL_FUNC(button_toggled), i); |
984 | gtk_table_attach(GTK_TABLE(table), w, 0, 2, y, y+1, |
985 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
986 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
987 | 3, 3); |
988 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), i->ival); |
989 | gtk_widget_show(w); |
990 | break; |
991 | |
95709966 |
992 | case C_CHOICES: |
c8230524 |
993 | /* |
994 | * Drop-down list (GtkOptionMenu). |
995 | */ |
996 | |
997 | w = gtk_label_new(i->name); |
998 | gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.5); |
999 | gtk_table_attach(GTK_TABLE(table), w, 0, 1, y, y+1, |
1000 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
1001 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
1002 | 3, 3); |
1003 | gtk_widget_show(w); |
1004 | |
1005 | w = gtk_option_menu_new(); |
1006 | gtk_table_attach(GTK_TABLE(table), w, 1, 2, y, y+1, |
1007 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
1008 | GTK_EXPAND | GTK_SHRINK | GTK_FILL, |
1009 | 3, 3); |
1010 | gtk_widget_show(w); |
1011 | |
1012 | { |
1013 | int c, val; |
1014 | char *p, *q, *name; |
1015 | GtkWidget *menuitem; |
1016 | GtkWidget *menu = gtk_menu_new(); |
1017 | |
1018 | gtk_option_menu_set_menu(GTK_OPTION_MENU(w), menu); |
1019 | |
1020 | c = *i->sval; |
1021 | p = i->sval+1; |
1022 | val = 0; |
1023 | |
1024 | while (*p) { |
1025 | q = p; |
1026 | while (*q && *q != c) |
1027 | q++; |
1028 | |
1029 | name = snewn(q-p+1, char); |
1030 | strncpy(name, p, q-p); |
1031 | name[q-p] = '\0'; |
1032 | |
1033 | if (*q) q++; /* eat delimiter */ |
1034 | |
1035 | menuitem = gtk_menu_item_new_with_label(name); |
1036 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1037 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
1038 | GINT_TO_POINTER(val)); |
1039 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1040 | GTK_SIGNAL_FUNC(droplist_sel), i); |
1041 | gtk_widget_show(menuitem); |
1042 | |
1043 | val++; |
1044 | |
1045 | p = q; |
1046 | } |
1047 | |
1048 | gtk_option_menu_set_history(GTK_OPTION_MENU(w), i->ival); |
1049 | } |
1050 | |
1051 | break; |
1052 | } |
1053 | |
1054 | y++; |
1055 | } |
1056 | |
1057 | gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "destroy", |
1058 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
a163b7c2 |
1059 | gtk_signal_connect(GTK_OBJECT(fe->cfgbox), "key_press_event", |
1060 | GTK_SIGNAL_FUNC(win_key_press), cancel); |
c8230524 |
1061 | gtk_window_set_modal(GTK_WINDOW(fe->cfgbox), TRUE); |
1062 | gtk_window_set_transient_for(GTK_WINDOW(fe->cfgbox), |
1063 | GTK_WINDOW(fe->window)); |
23e8c9fd |
1064 | /* set_transient_window_pos(fe->window, fe->cfgbox); */ |
c8230524 |
1065 | gtk_widget_show(fe->cfgbox); |
1066 | gtk_main(); |
1067 | |
077f3cbe |
1068 | free_cfg(fe->cfg); |
c8230524 |
1069 | |
1070 | return fe->cfgret; |
1071 | } |
1072 | |
eb2ad6f1 |
1073 | static void menu_key_event(GtkMenuItem *menuitem, gpointer data) |
1074 | { |
1075 | frontend *fe = (frontend *)data; |
1076 | int key = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem), |
1077 | "user-data")); |
1078 | if (!midend_process_key(fe->me, 0, 0, key)) |
1079 | gtk_widget_destroy(fe->window); |
1080 | } |
1081 | |
1e3e152d |
1082 | static void get_size(frontend *fe, int *px, int *py) |
1083 | { |
1084 | int x, y; |
1085 | |
1086 | /* |
1087 | * Currently I don't want to make the GTK port scale large |
1088 | * puzzles to fit on the screen. This is because X does permit |
1089 | * extremely large windows and many window managers provide a |
1090 | * means of navigating round them, and the users I consulted |
1091 | * before deciding said that they'd rather have enormous puzzle |
1092 | * windows spanning multiple screen pages than have them |
1093 | * shrunk. I could change my mind later or introduce |
1094 | * configurability; this would be the place to do so, by |
1095 | * replacing the initial values of x and y with the screen |
1096 | * dimensions. |
1097 | */ |
1098 | x = INT_MAX; |
1099 | y = INT_MAX; |
1100 | midend_size(fe->me, &x, &y, FALSE); |
1101 | *px = x; |
1102 | *py = y; |
1103 | } |
1104 | |
ef429a6c |
1105 | #if !GTK_CHECK_VERSION(2,0,0) |
1106 | #define gtk_window_resize(win, x, y) \ |
1107 | gdk_window_resize(GTK_WIDGET(win)->window, x, y) |
1108 | #endif |
1109 | |
f92acd1a |
1110 | static void update_menuitem_bullet(GtkWidget *label, int visible) |
1111 | { |
1112 | if (visible) { |
1113 | gtk_label_set_text(GTK_LABEL(label), "\xE2\x80\xA2"); |
1114 | } else { |
1115 | gtk_label_set_text(GTK_LABEL(label), ""); |
1116 | } |
1117 | } |
1118 | |
fa3abef5 |
1119 | /* |
1120 | * Called when any other code in this file has changed the |
1121 | * selected game parameters. |
1122 | */ |
1123 | static void changed_preset(frontend *fe) |
f92acd1a |
1124 | { |
1125 | int n = midend_which_preset(fe->me); |
1126 | int i; |
1127 | |
fa3abef5 |
1128 | /* |
1129 | * Update the tick mark in the Type menu. |
1130 | */ |
f92acd1a |
1131 | if (fe->preset_bullets) { |
1132 | for (i = 0; i < fe->npresets; i++) |
1133 | update_menuitem_bullet(fe->preset_bullets[i], n == i); |
1134 | } |
1135 | if (fe->preset_custom_bullet) { |
1136 | update_menuitem_bullet(fe->preset_custom_bullet, n < 0); |
1137 | } |
fa3abef5 |
1138 | |
1139 | /* |
1140 | * Update the greying on the Copy menu option. |
1141 | */ |
1142 | if (fe->copy_menu_item) { |
1143 | int enabled = midend_can_format_as_text_now(fe->me); |
1144 | gtk_widget_set_sensitive(fe->copy_menu_item, enabled); |
1145 | } |
f92acd1a |
1146 | } |
1147 | |
854a4f51 |
1148 | static void resize_fe(frontend *fe) |
eb2ad6f1 |
1149 | { |
eb2ad6f1 |
1150 | int x, y; |
1151 | |
1e3e152d |
1152 | get_size(fe, &x, &y); |
eb2ad6f1 |
1153 | fe->w = x; |
1154 | fe->h = y; |
1e3e152d |
1155 | gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); |
4e1f57ba |
1156 | { |
1157 | GtkRequisition req; |
1158 | gtk_widget_size_request(GTK_WIDGET(fe->window), &req); |
cbdf34a0 |
1159 | gtk_window_resize(GTK_WINDOW(fe->window), req.width, req.height); |
4e1f57ba |
1160 | } |
8b629657 |
1161 | /* |
1162 | * Now that we've established the preferred size of the window, |
1163 | * reduce the drawing area's size request so the user can shrink |
1164 | * the window. |
1165 | */ |
1166 | gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), 1, 1); |
eb2ad6f1 |
1167 | } |
1168 | |
854a4f51 |
1169 | static void menu_preset_event(GtkMenuItem *menuitem, gpointer data) |
1170 | { |
1171 | frontend *fe = (frontend *)data; |
1172 | game_params *params = |
1173 | (game_params *)gtk_object_get_data(GTK_OBJECT(menuitem), "user-data"); |
1174 | |
1175 | midend_set_params(fe->me, params); |
1176 | midend_new_game(fe->me); |
fa3abef5 |
1177 | changed_preset(fe); |
854a4f51 |
1178 | resize_fe(fe); |
1179 | } |
1180 | |
9b4b03d3 |
1181 | GdkAtom compound_text_atom, utf8_string_atom; |
1182 | int paste_initialised = FALSE; |
1183 | |
1184 | void init_paste() |
1185 | { |
23e8c9fd |
1186 | unsigned char empty[] = { 0 }; |
1187 | |
9b4b03d3 |
1188 | if (paste_initialised) |
1189 | return; |
1190 | |
1191 | if (!compound_text_atom) |
1192 | compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE); |
1193 | if (!utf8_string_atom) |
1194 | utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE); |
1195 | |
1196 | /* |
1197 | * Ensure that all the cut buffers exist - according to the |
1198 | * ICCCM, we must do this before we start using cut buffers. |
1199 | */ |
1200 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
23e8c9fd |
1201 | XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0); |
9b4b03d3 |
1202 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
23e8c9fd |
1203 | XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0); |
9b4b03d3 |
1204 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
23e8c9fd |
1205 | XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0); |
9b4b03d3 |
1206 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
23e8c9fd |
1207 | XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0); |
9b4b03d3 |
1208 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
23e8c9fd |
1209 | XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0); |
9b4b03d3 |
1210 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
23e8c9fd |
1211 | XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0); |
9b4b03d3 |
1212 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
23e8c9fd |
1213 | XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0); |
9b4b03d3 |
1214 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
23e8c9fd |
1215 | XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0); |
9b4b03d3 |
1216 | } |
1217 | |
1218 | /* Store data in a cut-buffer. */ |
1219 | void store_cutbuffer(char *ptr, int len) |
1220 | { |
1221 | /* ICCCM says we must rotate the buffers before storing to buffer 0. */ |
1222 | XRotateBuffers(GDK_DISPLAY(), 1); |
1223 | XStoreBytes(GDK_DISPLAY(), ptr, len); |
1224 | } |
1225 | |
1226 | void write_clip(frontend *fe, char *data) |
1227 | { |
1228 | init_paste(); |
1229 | |
1230 | if (fe->paste_data) |
1231 | sfree(fe->paste_data); |
1232 | |
1233 | /* |
1234 | * For this simple application we can safely assume that the |
1235 | * data passed to this function is pure ASCII, which means we |
1236 | * can return precisely the same stuff for types STRING, |
1237 | * COMPOUND_TEXT or UTF8_STRING. |
1238 | */ |
1239 | |
1240 | fe->paste_data = data; |
1241 | fe->paste_data_len = strlen(data); |
1242 | |
1243 | store_cutbuffer(fe->paste_data, fe->paste_data_len); |
1244 | |
1245 | if (gtk_selection_owner_set(fe->area, GDK_SELECTION_PRIMARY, |
1246 | CurrentTime)) { |
1247 | gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY, |
1248 | GDK_SELECTION_TYPE_STRING, 1); |
1249 | gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY, |
1250 | compound_text_atom, 1); |
1251 | gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY, |
1252 | utf8_string_atom, 1); |
1253 | } |
1254 | } |
1255 | |
1256 | void selection_get(GtkWidget *widget, GtkSelectionData *seldata, |
1257 | guint info, guint time_stamp, gpointer data) |
1258 | { |
1259 | frontend *fe = (frontend *)data; |
1260 | gtk_selection_data_set(seldata, seldata->target, 8, |
1261 | fe->paste_data, fe->paste_data_len); |
1262 | } |
1263 | |
1264 | gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata, |
1265 | gpointer data) |
1266 | { |
1267 | frontend *fe = (frontend *)data; |
1268 | |
1269 | if (fe->paste_data) |
1270 | sfree(fe->paste_data); |
1271 | fe->paste_data = NULL; |
1272 | fe->paste_data_len = 0; |
1273 | return TRUE; |
1274 | } |
1275 | |
1276 | static void menu_copy_event(GtkMenuItem *menuitem, gpointer data) |
1277 | { |
1278 | frontend *fe = (frontend *)data; |
1279 | char *text; |
1280 | |
1281 | text = midend_text_format(fe->me); |
1282 | |
1283 | if (text) { |
1284 | write_clip(fe, text); |
1285 | } else { |
1286 | gdk_beep(); |
1287 | } |
1288 | } |
1289 | |
a4393230 |
1290 | static void filesel_ok(GtkButton *button, gpointer data) |
1291 | { |
1292 | frontend *fe = (frontend *)data; |
1293 | |
1294 | gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data"); |
1295 | |
1296 | const char *name = |
1297 | gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel)); |
1298 | |
1299 | fe->filesel_name = dupstr(name); |
1300 | } |
1301 | |
1302 | static char *file_selector(frontend *fe, char *title, int save) |
1303 | { |
1304 | GtkWidget *filesel = |
1305 | gtk_file_selection_new(title); |
1306 | |
1307 | fe->filesel_name = NULL; |
1308 | |
1309 | gtk_window_set_modal(GTK_WINDOW(filesel), TRUE); |
1310 | gtk_object_set_data |
1311 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data", |
1312 | (gpointer)filesel); |
1313 | gtk_signal_connect |
1314 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked", |
1315 | GTK_SIGNAL_FUNC(filesel_ok), fe); |
1316 | gtk_signal_connect_object |
1317 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked", |
1318 | GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel); |
1319 | gtk_signal_connect_object |
1320 | (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked", |
1321 | GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel); |
1322 | gtk_signal_connect(GTK_OBJECT(filesel), "destroy", |
1323 | GTK_SIGNAL_FUNC(window_destroy), NULL); |
1324 | gtk_widget_show(filesel); |
1325 | gtk_window_set_transient_for(GTK_WINDOW(filesel), GTK_WINDOW(fe->window)); |
1326 | gtk_main(); |
1327 | |
1328 | return fe->filesel_name; |
1329 | } |
1330 | |
f7692dd8 |
1331 | struct savefile_write_ctx { |
1332 | FILE *fp; |
1333 | int error; |
1334 | }; |
1335 | |
a4393230 |
1336 | static void savefile_write(void *wctx, void *buf, int len) |
1337 | { |
f7692dd8 |
1338 | struct savefile_write_ctx *ctx = (struct savefile_write_ctx *)wctx; |
1339 | if (fwrite(buf, 1, len, ctx->fp) < len) |
1340 | ctx->error = 1; |
a4393230 |
1341 | } |
1342 | |
1343 | static int savefile_read(void *wctx, void *buf, int len) |
1344 | { |
1345 | FILE *fp = (FILE *)wctx; |
1346 | int ret; |
1347 | |
1348 | ret = fread(buf, 1, len, fp); |
1349 | return (ret == len); |
1350 | } |
1351 | |
1352 | static void menu_save_event(GtkMenuItem *menuitem, gpointer data) |
1353 | { |
1354 | frontend *fe = (frontend *)data; |
1355 | char *name; |
1356 | |
1357 | name = file_selector(fe, "Enter name of game file to save", TRUE); |
1358 | |
1359 | if (name) { |
668be019 |
1360 | FILE *fp; |
1361 | |
1362 | if ((fp = fopen(name, "r")) != NULL) { |
1363 | char buf[256 + FILENAME_MAX]; |
1364 | fclose(fp); |
1365 | /* file exists */ |
1366 | |
1367 | sprintf(buf, "Are you sure you want to overwrite the" |
1368 | " file \"%.*s\"?", |
1369 | FILENAME_MAX, name); |
1370 | if (!message_box(fe->window, "Question", buf, TRUE, MB_YESNO)) |
1371 | return; |
1372 | } |
1373 | |
1374 | fp = fopen(name, "w"); |
a4393230 |
1375 | sfree(name); |
1376 | |
1377 | if (!fp) { |
1378 | error_box(fe->window, "Unable to open save file"); |
1379 | return; |
1380 | } |
1381 | |
f7692dd8 |
1382 | { |
1383 | struct savefile_write_ctx ctx; |
1384 | ctx.fp = fp; |
1385 | ctx.error = 0; |
1386 | midend_serialise(fe->me, savefile_write, &ctx); |
1387 | fclose(fp); |
1388 | if (ctx.error) { |
1389 | error_box(fe->window, "Error writing save file"); |
1390 | return; |
1391 | } |
1392 | } |
a4393230 |
1393 | |
a4393230 |
1394 | } |
1395 | } |
1396 | |
1397 | static void menu_load_event(GtkMenuItem *menuitem, gpointer data) |
1398 | { |
1399 | frontend *fe = (frontend *)data; |
1400 | char *name, *err; |
a4393230 |
1401 | |
1402 | name = file_selector(fe, "Enter name of saved game file to load", FALSE); |
1403 | |
1404 | if (name) { |
1405 | FILE *fp = fopen(name, "r"); |
1406 | sfree(name); |
1407 | |
1408 | if (!fp) { |
1409 | error_box(fe->window, "Unable to open saved game file"); |
1410 | return; |
1411 | } |
1412 | |
1413 | err = midend_deserialise(fe->me, savefile_read, fp); |
1414 | |
1415 | fclose(fp); |
1416 | |
1417 | if (err) { |
1418 | error_box(fe->window, err); |
1419 | return; |
1420 | } |
1421 | |
fa3abef5 |
1422 | changed_preset(fe); |
854a4f51 |
1423 | resize_fe(fe); |
a4393230 |
1424 | } |
1425 | } |
1426 | |
2ac6d24e |
1427 | static void menu_solve_event(GtkMenuItem *menuitem, gpointer data) |
1428 | { |
1429 | frontend *fe = (frontend *)data; |
1430 | char *msg; |
1431 | |
1432 | msg = midend_solve(fe->me); |
1433 | |
1434 | if (msg) |
1435 | error_box(fe->window, msg); |
1436 | } |
1437 | |
7f89707c |
1438 | static void menu_restart_event(GtkMenuItem *menuitem, gpointer data) |
1439 | { |
1440 | frontend *fe = (frontend *)data; |
1441 | |
1442 | midend_restart_game(fe->me); |
1443 | } |
1444 | |
c8230524 |
1445 | static void menu_config_event(GtkMenuItem *menuitem, gpointer data) |
1446 | { |
1447 | frontend *fe = (frontend *)data; |
5928817c |
1448 | int which = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(menuitem), |
1449 | "user-data")); |
c8230524 |
1450 | |
5928817c |
1451 | if (!get_config(fe, which)) |
c8230524 |
1452 | return; |
1453 | |
5928817c |
1454 | midend_new_game(fe->me); |
854a4f51 |
1455 | resize_fe(fe); |
c8230524 |
1456 | } |
1457 | |
97098757 |
1458 | static void menu_about_event(GtkMenuItem *menuitem, gpointer data) |
1459 | { |
1460 | frontend *fe = (frontend *)data; |
1461 | char titlebuf[256]; |
1462 | char textbuf[1024]; |
1463 | |
1464 | sprintf(titlebuf, "About %.200s", thegame.name); |
1465 | sprintf(textbuf, |
1466 | "%.200s\n\n" |
1467 | "from Simon Tatham's Portable Puzzle Collection\n\n" |
1468 | "%.500s", thegame.name, ver); |
1469 | |
668be019 |
1470 | message_box(fe->window, titlebuf, textbuf, TRUE, MB_OK); |
97098757 |
1471 | } |
1472 | |
eb2ad6f1 |
1473 | static GtkWidget *add_menu_item_with_key(frontend *fe, GtkContainer *cont, |
1474 | char *text, int key) |
1475 | { |
1476 | GtkWidget *menuitem = gtk_menu_item_new_with_label(text); |
a383a9be |
1477 | int keyqual; |
eb2ad6f1 |
1478 | gtk_container_add(cont, menuitem); |
1479 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
1480 | GINT_TO_POINTER(key)); |
1481 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1482 | GTK_SIGNAL_FUNC(menu_key_event), fe); |
a383a9be |
1483 | switch (key & ~0x1F) { |
1484 | case 0x00: |
1485 | key += 0x60; |
1486 | keyqual = GDK_CONTROL_MASK; |
1487 | break; |
1488 | case 0x40: |
1489 | key += 0x20; |
1490 | keyqual = GDK_SHIFT_MASK; |
1491 | break; |
1492 | default: |
1493 | keyqual = 0; |
1494 | break; |
1495 | } |
1496 | gtk_widget_add_accelerator(menuitem, |
1497 | "activate", fe->accelgroup, |
1498 | key, keyqual, |
1499 | GTK_ACCEL_VISIBLE); |
eb2ad6f1 |
1500 | gtk_widget_show(menuitem); |
1501 | return menuitem; |
1502 | } |
1503 | |
1504 | static void add_menu_separator(GtkContainer *cont) |
1505 | { |
1506 | GtkWidget *menuitem = gtk_menu_item_new(); |
1507 | gtk_container_add(cont, menuitem); |
1508 | gtk_widget_show(menuitem); |
1509 | } |
1510 | |
afc306fc |
1511 | enum { ARG_EITHER, ARG_SAVE, ARG_ID }; /* for argtype */ |
1512 | |
f92acd1a |
1513 | static GtkWidget *make_preset_menuitem(GtkWidget **bulletlabel, |
1514 | const char *name) |
1515 | { |
1516 | GtkWidget *hbox, *lab1, *lab2, *menuitem; |
1517 | GtkRequisition req; |
1518 | |
1519 | hbox = gtk_hbox_new(FALSE, 0); |
1520 | gtk_widget_show(hbox); |
1521 | lab1 = gtk_label_new("\xE2\x80\xA2 "); |
1522 | gtk_widget_show(lab1); |
1523 | gtk_box_pack_start(GTK_BOX(hbox), lab1, FALSE, FALSE, 0); |
1524 | gtk_misc_set_alignment(GTK_MISC(lab1), 0.0, 0.0); |
1525 | lab2 = gtk_label_new(name); |
1526 | gtk_widget_show(lab2); |
1527 | gtk_box_pack_start(GTK_BOX(hbox), lab2, TRUE, TRUE, 0); |
1528 | gtk_misc_set_alignment(GTK_MISC(lab2), 0.0, 0.0); |
1529 | |
1530 | gtk_widget_size_request(lab1, &req); |
1531 | gtk_widget_set_usize(lab1, req.width, -1); |
1532 | gtk_label_set_text(GTK_LABEL(lab1), ""); |
1533 | |
1534 | menuitem = gtk_menu_item_new(); |
1535 | gtk_container_add(GTK_CONTAINER(menuitem), hbox); |
1536 | |
1537 | *bulletlabel = lab1; |
1538 | return menuitem; |
1539 | } |
1540 | |
afc306fc |
1541 | static frontend *new_window(char *arg, int argtype, char **error) |
2ef96bd6 |
1542 | { |
1543 | frontend *fe; |
eb2ad6f1 |
1544 | GtkBox *vbox; |
1545 | GtkWidget *menubar, *menu, *menuitem; |
f12cdd72 |
1546 | GdkPixmap *iconpm; |
1547 | GList *iconlist; |
eb2ad6f1 |
1548 | int x, y, n; |
dafd6cf6 |
1549 | char errbuf[1024]; |
f12cdd72 |
1550 | extern char *const *const xpm_icons[]; |
1551 | extern const int n_xpm_icons; |
83680571 |
1552 | |
2ef96bd6 |
1553 | fe = snew(frontend); |
83680571 |
1554 | |
171fbdaa |
1555 | fe->timer_active = FALSE; |
1556 | fe->timer_id = -1; |
1557 | |
dafd6cf6 |
1558 | fe->me = midend_new(fe, &thegame, >k_drawing, fe); |
171fbdaa |
1559 | |
7d1cfbb7 |
1560 | if (arg) { |
1561 | char *err; |
afc306fc |
1562 | FILE *fp; |
dafd6cf6 |
1563 | |
1564 | errbuf[0] = '\0'; |
1565 | |
afc306fc |
1566 | switch (argtype) { |
1567 | case ARG_ID: |
1568 | err = midend_game_id(fe->me, arg); |
1569 | if (!err) |
1570 | midend_new_game(fe->me); |
1571 | else |
1572 | sprintf(errbuf, "Invalid game ID: %.800s", err); |
1573 | break; |
1574 | case ARG_SAVE: |
1575 | fp = fopen(arg, "r"); |
7d1cfbb7 |
1576 | if (!fp) { |
afc306fc |
1577 | sprintf(errbuf, "Error opening file: %.800s", strerror(errno)); |
7d1cfbb7 |
1578 | } else { |
1579 | err = midend_deserialise(fe->me, savefile_read, fp); |
93548721 |
1580 | if (err) |
afc306fc |
1581 | sprintf(errbuf, "Invalid save file: %.800s", err); |
93548721 |
1582 | fclose(fp); |
7d1cfbb7 |
1583 | } |
afc306fc |
1584 | break; |
1585 | default /*case ARG_EITHER*/: |
1586 | /* |
1587 | * First try treating the argument as a game ID. |
1588 | */ |
1589 | err = midend_game_id(fe->me, arg); |
1590 | if (!err) { |
1591 | /* |
1592 | * It's a valid game ID. |
1593 | */ |
1594 | midend_new_game(fe->me); |
1595 | } else { |
1596 | FILE *fp = fopen(arg, "r"); |
1597 | if (!fp) { |
1598 | sprintf(errbuf, "Supplied argument is neither a game ID (%.400s)" |
1599 | " nor a save file (%.400s)", err, strerror(errno)); |
1600 | } else { |
1601 | err = midend_deserialise(fe->me, savefile_read, fp); |
1602 | if (err) |
1603 | sprintf(errbuf, "%.800s", err); |
1604 | fclose(fp); |
1605 | } |
1606 | } |
1607 | break; |
1608 | } |
dafd6cf6 |
1609 | if (*errbuf) { |
1610 | *error = dupstr(errbuf); |
7d1cfbb7 |
1611 | midend_free(fe->me); |
1612 | sfree(fe); |
1613 | return NULL; |
1614 | } |
1615 | |
1616 | } else { |
1617 | midend_new_game(fe->me); |
8b7938e7 |
1618 | } |
7f77ea24 |
1619 | |
2ef96bd6 |
1620 | fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
be8d5aa1 |
1621 | gtk_window_set_title(GTK_WINDOW(fe->window), thegame.name); |
47432032 |
1622 | |
eb2ad6f1 |
1623 | vbox = GTK_BOX(gtk_vbox_new(FALSE, 0)); |
1624 | gtk_container_add(GTK_CONTAINER(fe->window), GTK_WIDGET(vbox)); |
1625 | gtk_widget_show(GTK_WIDGET(vbox)); |
1626 | |
a383a9be |
1627 | fe->accelgroup = gtk_accel_group_new(); |
1628 | gtk_window_add_accel_group(GTK_WINDOW(fe->window), fe->accelgroup); |
1629 | |
eb2ad6f1 |
1630 | menubar = gtk_menu_bar_new(); |
1631 | gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0); |
1632 | gtk_widget_show(menubar); |
1633 | |
1634 | menuitem = gtk_menu_item_new_with_label("Game"); |
1635 | gtk_container_add(GTK_CONTAINER(menubar), menuitem); |
1636 | gtk_widget_show(menuitem); |
1637 | |
1638 | menu = gtk_menu_new(); |
1639 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu); |
1640 | |
1641 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "New", 'n'); |
7f89707c |
1642 | |
1643 | menuitem = gtk_menu_item_new_with_label("Restart"); |
1644 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1645 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1646 | GTK_SIGNAL_FUNC(menu_restart_event), fe); |
1647 | gtk_widget_show(menuitem); |
eb2ad6f1 |
1648 | |
5928817c |
1649 | menuitem = gtk_menu_item_new_with_label("Specific..."); |
1650 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
1185e3c5 |
1651 | GINT_TO_POINTER(CFG_DESC)); |
1652 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1653 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1654 | GTK_SIGNAL_FUNC(menu_config_event), fe); |
1655 | gtk_widget_show(menuitem); |
1656 | |
1657 | menuitem = gtk_menu_item_new_with_label("Random Seed..."); |
1658 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
5928817c |
1659 | GINT_TO_POINTER(CFG_SEED)); |
1660 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1661 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1662 | GTK_SIGNAL_FUNC(menu_config_event), fe); |
1663 | gtk_widget_show(menuitem); |
1664 | |
be8d5aa1 |
1665 | if ((n = midend_num_presets(fe->me)) > 0 || thegame.can_configure) { |
eb2ad6f1 |
1666 | GtkWidget *submenu; |
1667 | int i; |
1668 | |
1669 | menuitem = gtk_menu_item_new_with_label("Type"); |
c8230524 |
1670 | gtk_container_add(GTK_CONTAINER(menubar), menuitem); |
eb2ad6f1 |
1671 | gtk_widget_show(menuitem); |
1672 | |
1673 | submenu = gtk_menu_new(); |
1674 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu); |
1675 | |
f92acd1a |
1676 | fe->npresets = n; |
1677 | fe->preset_bullets = snewn(n, GtkWidget *); |
1678 | |
eb2ad6f1 |
1679 | for (i = 0; i < n; i++) { |
1680 | char *name; |
1681 | game_params *params; |
1682 | |
1683 | midend_fetch_preset(fe->me, i, &name, ¶ms); |
1684 | |
f92acd1a |
1685 | menuitem = make_preset_menuitem(&fe->preset_bullets[i], name); |
1686 | |
eb2ad6f1 |
1687 | gtk_container_add(GTK_CONTAINER(submenu), menuitem); |
1688 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", params); |
1689 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1690 | GTK_SIGNAL_FUNC(menu_preset_event), fe); |
1691 | gtk_widget_show(menuitem); |
1692 | } |
c8230524 |
1693 | |
be8d5aa1 |
1694 | if (thegame.can_configure) { |
f92acd1a |
1695 | menuitem = make_preset_menuitem(&fe->preset_custom_bullet, |
1696 | "Custom..."); |
1697 | |
1698 | gtk_container_add(GTK_CONTAINER(submenu), menuitem); |
5928817c |
1699 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
1700 | GPOINTER_TO_INT(CFG_SETTINGS)); |
c8230524 |
1701 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1702 | GTK_SIGNAL_FUNC(menu_config_event), fe); |
1703 | gtk_widget_show(menuitem); |
f92acd1a |
1704 | } else |
1705 | fe->preset_custom_bullet = NULL; |
1706 | |
f92acd1a |
1707 | } else { |
1708 | fe->npresets = 0; |
1709 | fe->preset_bullets = NULL; |
1710 | fe->preset_custom_bullet = NULL; |
eb2ad6f1 |
1711 | } |
1712 | |
668be019 |
1713 | add_menu_separator(GTK_CONTAINER(menu)); |
cccb7f09 |
1714 | menuitem = gtk_menu_item_new_with_label("Load..."); |
668be019 |
1715 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1716 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1717 | GTK_SIGNAL_FUNC(menu_load_event), fe); |
1718 | gtk_widget_show(menuitem); |
cccb7f09 |
1719 | menuitem = gtk_menu_item_new_with_label("Save..."); |
668be019 |
1720 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1721 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1722 | GTK_SIGNAL_FUNC(menu_save_event), fe); |
1723 | gtk_widget_show(menuitem); |
eb2ad6f1 |
1724 | add_menu_separator(GTK_CONTAINER(menu)); |
1725 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Undo", 'u'); |
a383a9be |
1726 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Redo", 'r'); |
fa3abef5 |
1727 | if (thegame.can_format_as_text_ever) { |
9b4b03d3 |
1728 | add_menu_separator(GTK_CONTAINER(menu)); |
1729 | menuitem = gtk_menu_item_new_with_label("Copy"); |
1730 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1731 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1732 | GTK_SIGNAL_FUNC(menu_copy_event), fe); |
1733 | gtk_widget_show(menuitem); |
fa3abef5 |
1734 | fe->copy_menu_item = menuitem; |
1735 | } else { |
1736 | fe->copy_menu_item = NULL; |
9b4b03d3 |
1737 | } |
2ac6d24e |
1738 | if (thegame.can_solve) { |
1739 | add_menu_separator(GTK_CONTAINER(menu)); |
1740 | menuitem = gtk_menu_item_new_with_label("Solve"); |
1741 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1742 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1743 | GTK_SIGNAL_FUNC(menu_solve_event), fe); |
1744 | gtk_widget_show(menuitem); |
1745 | } |
eb2ad6f1 |
1746 | add_menu_separator(GTK_CONTAINER(menu)); |
1747 | add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Exit", 'q'); |
7f77ea24 |
1748 | |
97098757 |
1749 | menuitem = gtk_menu_item_new_with_label("Help"); |
1750 | gtk_container_add(GTK_CONTAINER(menubar), menuitem); |
1751 | gtk_widget_show(menuitem); |
1752 | |
1753 | menu = gtk_menu_new(); |
1754 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu); |
1755 | |
1756 | menuitem = gtk_menu_item_new_with_label("About"); |
1757 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
1758 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
1759 | GTK_SIGNAL_FUNC(menu_about_event), fe); |
1760 | gtk_widget_show(menuitem); |
1761 | |
f1a5cc88 |
1762 | changed_preset(fe); |
1763 | |
2ef96bd6 |
1764 | { |
1765 | int i, ncolours; |
1766 | float *colours; |
1767 | gboolean *success; |
7f77ea24 |
1768 | |
2ef96bd6 |
1769 | fe->colmap = gdk_colormap_get_system(); |
1770 | colours = midend_colours(fe->me, &ncolours); |
1771 | fe->ncolours = ncolours; |
1772 | fe->colours = snewn(ncolours, GdkColor); |
1773 | for (i = 0; i < ncolours; i++) { |
813593cc |
1774 | fe->colours[i].red = colours[i*3] * 0xFFFF; |
1775 | fe->colours[i].green = colours[i*3+1] * 0xFFFF; |
1776 | fe->colours[i].blue = colours[i*3+2] * 0xFFFF; |
2ef96bd6 |
1777 | } |
1778 | success = snewn(ncolours, gboolean); |
1779 | gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours, |
1780 | FALSE, FALSE, success); |
1781 | for (i = 0; i < ncolours; i++) { |
810aa59e |
1782 | if (!success[i]) { |
2ef96bd6 |
1783 | g_error("couldn't allocate colour %d (#%02x%02x%02x)\n", |
1784 | i, fe->colours[i].red >> 8, |
1785 | fe->colours[i].green >> 8, |
1786 | fe->colours[i].blue >> 8); |
810aa59e |
1787 | } |
2ef96bd6 |
1788 | } |
1789 | } |
7f77ea24 |
1790 | |
fd1a1a2b |
1791 | if (midend_wants_statusbar(fe->me)) { |
5725d728 |
1792 | GtkWidget *viewport; |
1793 | GtkRequisition req; |
1794 | |
1795 | viewport = gtk_viewport_new(NULL, NULL); |
1796 | gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE); |
fd1a1a2b |
1797 | fe->statusbar = gtk_statusbar_new(); |
5725d728 |
1798 | gtk_container_add(GTK_CONTAINER(viewport), fe->statusbar); |
1799 | gtk_widget_show(viewport); |
1800 | gtk_box_pack_end(vbox, viewport, FALSE, FALSE, 0); |
fd1a1a2b |
1801 | gtk_widget_show(fe->statusbar); |
1802 | fe->statusctx = gtk_statusbar_get_context_id |
1803 | (GTK_STATUSBAR(fe->statusbar), "game"); |
1804 | gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, |
5725d728 |
1805 | "test"); |
1806 | gtk_widget_size_request(fe->statusbar, &req); |
1807 | #if 0 |
1808 | /* For GTK 2.0, should we be using gtk_widget_set_size_request? */ |
1809 | #endif |
60d42abc |
1810 | gtk_widget_set_usize(viewport, -1, req.height); |
fd1a1a2b |
1811 | } else |
1812 | fe->statusbar = NULL; |
1813 | |
2ef96bd6 |
1814 | fe->area = gtk_drawing_area_new(); |
1e3e152d |
1815 | get_size(fe, &x, &y); |
2ef96bd6 |
1816 | gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y); |
1817 | fe->w = x; |
1818 | fe->h = y; |
1819 | |
1e3e152d |
1820 | gtk_box_pack_end(vbox, fe->area, TRUE, TRUE, 0); |
2ef96bd6 |
1821 | |
1822 | fe->pixmap = NULL; |
4efb3868 |
1823 | fe->fonts = NULL; |
1824 | fe->nfonts = fe->fontsize = 0; |
2ef96bd6 |
1825 | |
9b4b03d3 |
1826 | fe->paste_data = NULL; |
1827 | fe->paste_data_len = 0; |
1828 | |
2ef96bd6 |
1829 | gtk_signal_connect(GTK_OBJECT(fe->window), "destroy", |
1830 | GTK_SIGNAL_FUNC(destroy), fe); |
1831 | gtk_signal_connect(GTK_OBJECT(fe->window), "key_press_event", |
1832 | GTK_SIGNAL_FUNC(key_event), fe); |
1833 | gtk_signal_connect(GTK_OBJECT(fe->area), "button_press_event", |
1834 | GTK_SIGNAL_FUNC(button_event), fe); |
74a4e547 |
1835 | gtk_signal_connect(GTK_OBJECT(fe->area), "button_release_event", |
1836 | GTK_SIGNAL_FUNC(button_event), fe); |
1837 | gtk_signal_connect(GTK_OBJECT(fe->area), "motion_notify_event", |
1838 | GTK_SIGNAL_FUNC(motion_event), fe); |
9b4b03d3 |
1839 | gtk_signal_connect(GTK_OBJECT(fe->area), "selection_get", |
1840 | GTK_SIGNAL_FUNC(selection_get), fe); |
1841 | gtk_signal_connect(GTK_OBJECT(fe->area), "selection_clear_event", |
1842 | GTK_SIGNAL_FUNC(selection_clear), fe); |
2ef96bd6 |
1843 | gtk_signal_connect(GTK_OBJECT(fe->area), "expose_event", |
1844 | GTK_SIGNAL_FUNC(expose_area), fe); |
fd1a1a2b |
1845 | gtk_signal_connect(GTK_OBJECT(fe->window), "map_event", |
1846 | GTK_SIGNAL_FUNC(map_window), fe); |
2ef96bd6 |
1847 | gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event", |
1848 | GTK_SIGNAL_FUNC(configure_area), fe); |
1849 | |
74a4e547 |
1850 | gtk_widget_add_events(GTK_WIDGET(fe->area), |
1851 | GDK_BUTTON_PRESS_MASK | |
1852 | GDK_BUTTON_RELEASE_MASK | |
1853 | GDK_BUTTON_MOTION_MASK); |
2ef96bd6 |
1854 | |
f12cdd72 |
1855 | if (n_xpm_icons) { |
1856 | gtk_widget_realize(fe->window); |
1857 | iconpm = gdk_pixmap_create_from_xpm_d(fe->window->window, NULL, |
1858 | NULL, (gchar **)xpm_icons[0]); |
1859 | gdk_window_set_icon(fe->window->window, NULL, iconpm, NULL); |
1860 | iconlist = NULL; |
1861 | for (n = 0; n < n_xpm_icons; n++) { |
1862 | iconlist = |
1863 | g_list_append(iconlist, |
1864 | gdk_pixbuf_new_from_xpm_data((const gchar **) |
1865 | xpm_icons[n])); |
1866 | } |
1867 | gdk_window_set_icon_list(fe->window->window, iconlist); |
1868 | } |
1869 | |
2ef96bd6 |
1870 | gtk_widget_show(fe->area); |
1871 | gtk_widget_show(fe->window); |
1872 | |
8b629657 |
1873 | /* |
1874 | * Now that we've established the preferred size of the window, |
1875 | * reduce the drawing area's size request so the user can shrink |
1876 | * the window. |
1877 | */ |
1878 | gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), 1, 1); |
1879 | |
1e3e152d |
1880 | gdk_window_set_background(fe->area->window, &fe->colours[0]); |
1881 | gdk_window_set_background(fe->window->window, &fe->colours[0]); |
1882 | |
2ef96bd6 |
1883 | return fe; |
83680571 |
1884 | } |
1885 | |
dafd6cf6 |
1886 | char *fgetline(FILE *fp) |
1887 | { |
1888 | char *ret = snewn(512, char); |
1889 | int size = 512, len = 0; |
1890 | while (fgets(ret + len, size - len, fp)) { |
1891 | len += strlen(ret + len); |
1892 | if (ret[len-1] == '\n') |
1893 | break; /* got a newline, we're done */ |
1894 | size = len + 512; |
1895 | ret = sresize(ret, size, char); |
1896 | } |
1897 | if (len == 0) { /* first fgets returned NULL */ |
1898 | sfree(ret); |
1899 | return NULL; |
1900 | } |
1901 | ret[len] = '\0'; |
1902 | return ret; |
1903 | } |
1904 | |
83680571 |
1905 | int main(int argc, char **argv) |
1906 | { |
8b7938e7 |
1907 | char *pname = argv[0]; |
1908 | char *error; |
dafd6cf6 |
1909 | int ngenerate = 0, print = FALSE, px = 1, py = 1; |
1910 | int soln = FALSE, colour = FALSE; |
1911 | float scale = 1.0F; |
afc306fc |
1912 | float redo_proportion = 0.0F; |
dafd6cf6 |
1913 | char *arg = NULL; |
afc306fc |
1914 | int argtype = ARG_EITHER; |
9edb9f35 |
1915 | char *screenshot_file = NULL; |
dafd6cf6 |
1916 | int doing_opts = TRUE; |
1917 | int ac = argc; |
1918 | char **av = argv; |
1919 | char errbuf[500]; |
8b7938e7 |
1920 | |
dafd6cf6 |
1921 | /* |
1922 | * Command line parsing in this function is rather fiddly, |
1923 | * because GTK wants to have a go at argc/argv _first_ - and |
1924 | * yet we can't let it, because gtk_init() will bomb out if it |
1925 | * can't open an X display, whereas in fact we want to permit |
1926 | * our --generate and --print modes to run without an X |
1927 | * display. |
1928 | * |
1929 | * So what we do is: |
1930 | * - we parse the command line ourselves, without modifying |
1931 | * argc/argv |
1932 | * - if we encounter an error which might plausibly be the |
1933 | * result of a GTK command line (i.e. not detailed errors in |
1934 | * particular options of ours) we store the error message |
1935 | * and terminate parsing. |
1936 | * - if we got enough out of the command line to know it |
1937 | * specifies a non-X mode of operation, we either display |
1938 | * the stored error and return failure, or if there is no |
1939 | * stored error we do the non-X operation and return |
1940 | * success. |
1941 | * - otherwise, we go straight to gtk_init(). |
1942 | */ |
1943 | |
1944 | errbuf[0] = '\0'; |
1945 | while (--ac > 0) { |
1946 | char *p = *++av; |
1947 | if (doing_opts && !strcmp(p, "--version")) { |
1948 | printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n", |
1949 | thegame.name, ver); |
1950 | return 0; |
1951 | } else if (doing_opts && !strcmp(p, "--generate")) { |
1952 | if (--ac > 0) { |
1953 | ngenerate = atoi(*++av); |
1954 | if (!ngenerate) { |
1955 | fprintf(stderr, "%s: '--generate' expected a number\n", |
1956 | pname); |
1957 | return 1; |
1958 | } |
1959 | } else |
1960 | ngenerate = 1; |
1961 | } else if (doing_opts && !strcmp(p, "--print")) { |
1962 | if (!thegame.can_print) { |
1963 | fprintf(stderr, "%s: this game does not support printing\n", |
1964 | pname); |
1965 | return 1; |
1966 | } |
1967 | print = TRUE; |
1968 | if (--ac > 0) { |
1969 | char *dim = *++av; |
1970 | if (sscanf(dim, "%dx%d", &px, &py) != 2) { |
1971 | fprintf(stderr, "%s: unable to parse argument '%s' to " |
1972 | "'--print'\n", pname, dim); |
1973 | return 1; |
1974 | } |
1975 | } else { |
1976 | px = py = 1; |
1977 | } |
1978 | } else if (doing_opts && !strcmp(p, "--scale")) { |
1979 | if (--ac > 0) { |
1980 | scale = atof(*++av); |
1981 | } else { |
1982 | fprintf(stderr, "%s: no argument supplied to '--scale'\n", |
1983 | pname); |
1984 | return 1; |
1985 | } |
afc306fc |
1986 | } else if (doing_opts && !strcmp(p, "--redo")) { |
1987 | /* |
1988 | * This is an internal option which I don't expect |
1989 | * users to have any particular use for. The effect of |
1990 | * --redo is that once the game has been loaded and |
1991 | * initialised, the next move in the redo chain is |
1992 | * replayed, and the game screen is redrawn part way |
1993 | * through the making of the move. This is only |
1994 | * meaningful if there _is_ a next move in the redo |
1995 | * chain, which means in turn that this option is only |
1996 | * useful if you're also passing a save file on the |
1997 | * command line. |
1998 | * |
1999 | * This option is used by the script which generates |
2000 | * the puzzle icons and website screenshots, and I |
2001 | * don't imagine it's useful for anything else. |
2002 | * (Unless, I suppose, users don't like my screenshots |
2003 | * and want to generate their own in the same way for |
2004 | * some repackaged version of the puzzles.) |
2005 | */ |
2006 | if (--ac > 0) { |
2007 | redo_proportion = atof(*++av); |
2008 | } else { |
2009 | fprintf(stderr, "%s: no argument supplied to '--redo'\n", |
2010 | pname); |
2011 | return 1; |
2012 | } |
9edb9f35 |
2013 | } else if (doing_opts && !strcmp(p, "--screenshot")) { |
afc306fc |
2014 | /* |
2015 | * Another internal option for the icon building |
9edb9f35 |
2016 | * script. This causes a screenshot of the central |
afc306fc |
2017 | * drawing area (i.e. not including the menu bar or |
9edb9f35 |
2018 | * status bar) to be saved to a PNG file once the |
2019 | * window has been drawn, and then the application |
2020 | * quits immediately. |
afc306fc |
2021 | */ |
9edb9f35 |
2022 | if (--ac > 0) { |
2023 | screenshot_file = *++av; |
2024 | } else { |
2025 | fprintf(stderr, "%s: no argument supplied to '--screenshot'\n", |
2026 | pname); |
2027 | return 1; |
2028 | } |
dafd6cf6 |
2029 | } else if (doing_opts && (!strcmp(p, "--with-solutions") || |
2030 | !strcmp(p, "--with-solution") || |
2031 | !strcmp(p, "--with-solns") || |
2032 | !strcmp(p, "--with-soln") || |
2033 | !strcmp(p, "--solutions") || |
2034 | !strcmp(p, "--solution") || |
2035 | !strcmp(p, "--solns") || |
2036 | !strcmp(p, "--soln"))) { |
2037 | soln = TRUE; |
2038 | } else if (doing_opts && !strcmp(p, "--colour")) { |
2039 | if (!thegame.can_print_in_colour) { |
2040 | fprintf(stderr, "%s: this game does not support colour" |
2041 | " printing\n", pname); |
2042 | return 1; |
2043 | } |
2044 | colour = TRUE; |
afc306fc |
2045 | } else if (doing_opts && !strcmp(p, "--load")) { |
2046 | argtype = ARG_SAVE; |
2047 | } else if (doing_opts && !strcmp(p, "--game")) { |
2048 | argtype = ARG_ID; |
dafd6cf6 |
2049 | } else if (doing_opts && !strcmp(p, "--")) { |
2050 | doing_opts = FALSE; |
2051 | } else if (!doing_opts || p[0] != '-') { |
2052 | if (arg) { |
2053 | fprintf(stderr, "%s: more than one argument supplied\n", |
2054 | pname); |
2055 | return 1; |
2056 | } |
2057 | arg = p; |
2058 | } else { |
2059 | sprintf(errbuf, "%.100s: unrecognised option '%.100s'\n", |
2060 | pname, p); |
2061 | break; |
2062 | } |
45f953cc |
2063 | } |
2064 | |
95c76d7c |
2065 | if (*errbuf) { |
2066 | fputs(errbuf, stderr); |
2067 | return 1; |
2068 | } |
2069 | |
d91e1fc9 |
2070 | /* |
2071 | * Special standalone mode for generating puzzle IDs on the |
2072 | * command line. Useful for generating puzzles to be printed |
2073 | * out and solved offline (for puzzles where that even makes |
2074 | * sense - Solo, for example, is a lot more pencil-and-paper |
dafd6cf6 |
2075 | * friendly than Twiddle!) |
d91e1fc9 |
2076 | * |
2077 | * Usage: |
2078 | * |
2079 | * <puzzle-name> --generate [<n> [<params>]] |
2080 | * |
2081 | * <n>, if present, is the number of puzzle IDs to generate. |
2082 | * <params>, if present, is the same type of parameter string |
2083 | * you would pass to the puzzle when running it in GUI mode, |
2084 | * including optional extras such as the expansion factor in |
2085 | * Rectangles and the difficulty level in Solo. |
2086 | * |
2087 | * If you specify <params>, you must also specify <n> (although |
2088 | * you may specify it to be 1). Sorry; that was the |
2089 | * simplest-to-parse command-line syntax I came up with. |
2090 | */ |
dafd6cf6 |
2091 | if (ngenerate > 0 || print) { |
2092 | int i, n = 1; |
2093 | midend *me; |
2094 | char *id; |
2095 | document *doc = NULL; |
2096 | |
dafd6cf6 |
2097 | n = ngenerate; |
2098 | |
2099 | me = midend_new(NULL, &thegame, NULL, NULL); |
2100 | i = 0; |
2101 | |
2102 | if (print) |
2103 | doc = document_new(px, py, scale); |
2104 | |
2105 | /* |
2106 | * In this loop, we either generate a game ID or read one |
2107 | * from stdin depending on whether we're in generate mode; |
2108 | * then we either write it to stdout or print it, depending |
2109 | * on whether we're in print mode. Thus, this loop handles |
2110 | * generate-to-stdout, print-from-stdin and generate-and- |
2111 | * immediately-print modes. |
2112 | * |
2113 | * (It could also handle a copy-stdin-to-stdout mode, |
2114 | * although there's currently no combination of options |
2115 | * which will cause this loop to be activated in that mode. |
2116 | * It wouldn't be _entirely_ pointless, though, because |
2117 | * stdin could contain bare params strings or random-seed |
2118 | * IDs, and stdout would contain nothing but fully |
2119 | * generated descriptive game IDs.) |
2120 | */ |
2121 | while (ngenerate == 0 || i < n) { |
2122 | char *pstr, *err; |
2123 | |
2124 | if (ngenerate == 0) { |
2125 | pstr = fgetline(stdin); |
2126 | if (!pstr) |
2127 | break; |
2128 | pstr[strcspn(pstr, "\r\n")] = '\0'; |
2129 | } else { |
2130 | if (arg) { |
2131 | pstr = snewn(strlen(arg) + 40, char); |
2132 | |
2133 | strcpy(pstr, arg); |
2134 | if (i > 0 && strchr(arg, '#')) |
2135 | sprintf(pstr + strlen(pstr), "-%d", i); |
2136 | } else |
2137 | pstr = NULL; |
2138 | } |
2139 | |
2140 | if (pstr) { |
2141 | err = midend_game_id(me, pstr); |
2142 | if (err) { |
2143 | fprintf(stderr, "%s: error parsing '%s': %s\n", |
2144 | pname, pstr, err); |
2145 | return 1; |
2146 | } |
2147 | } |
2148 | sfree(pstr); |
2149 | |
2150 | midend_new_game(me); |
2151 | |
2152 | if (doc) { |
2153 | err = midend_print_puzzle(me, doc, soln); |
2154 | if (err) { |
2155 | fprintf(stderr, "%s: error in printing: %s\n", pname, err); |
2156 | return 1; |
2157 | } |
2158 | } else { |
2159 | id = midend_get_game_id(me); |
2160 | puts(id); |
2161 | sfree(id); |
2162 | } |
2163 | |
2164 | i++; |
d91e1fc9 |
2165 | } |
8b7938e7 |
2166 | |
dafd6cf6 |
2167 | if (doc) { |
2168 | psdata *ps = ps_init(stdout, colour); |
2169 | document_print(doc, ps_drawing_api(ps)); |
2170 | document_free(doc); |
2171 | ps_free(ps); |
2172 | } |
2173 | |
2174 | midend_free(me); |
2175 | |
d91e1fc9 |
2176 | return 0; |
2177 | } else { |
afc306fc |
2178 | frontend *fe; |
d91e1fc9 |
2179 | |
2180 | gtk_init(&argc, &argv); |
2181 | |
afc306fc |
2182 | fe = new_window(arg, argtype, &error); |
2183 | |
2184 | if (!fe) { |
d91e1fc9 |
2185 | fprintf(stderr, "%s: %s\n", pname, error); |
2186 | return 1; |
2187 | } |
2188 | |
9edb9f35 |
2189 | if (screenshot_file) { |
afc306fc |
2190 | /* |
2191 | * Some puzzles will not redraw their entire area if |
2192 | * given a partially completed animation, which means |
2193 | * we must redraw now and _then_ redraw again after |
2194 | * freezing the move timer. |
2195 | */ |
2196 | midend_force_redraw(fe->me); |
2197 | } |
2198 | |
2199 | if (redo_proportion) { |
2200 | /* Start a redo. */ |
2201 | midend_process_key(fe->me, 0, 0, 'r'); |
2202 | /* And freeze the timer at the specified position. */ |
2203 | midend_freeze_timer(fe->me, redo_proportion); |
2204 | } |
2205 | |
9edb9f35 |
2206 | if (screenshot_file) { |
2207 | GdkPixbuf *pb; |
851e6f7c |
2208 | GError *gerror = NULL; |
9edb9f35 |
2209 | |
afc306fc |
2210 | midend_redraw(fe->me); |
9edb9f35 |
2211 | |
2212 | pb = gdk_pixbuf_get_from_drawable(NULL, fe->pixmap, |
2213 | NULL, 0, 0, 0, 0, -1, -1); |
5136fa6a |
2214 | gdk_pixbuf_save(pb, screenshot_file, "png", &gerror, NULL); |
9edb9f35 |
2215 | |
2216 | exit(0); |
afc306fc |
2217 | } |
2218 | |
d91e1fc9 |
2219 | gtk_main(); |
2220 | } |
83680571 |
2221 | |
2222 | return 0; |
2223 | } |