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