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