| 1 | /* |
| 2 | * gtk.c: GTK front end for my puzzle collection. |
| 3 | */ |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <stdarg.h> |
| 8 | |
| 9 | #include <gtk/gtk.h> |
| 10 | |
| 11 | #include "puzzles.h" |
| 12 | |
| 13 | /* ---------------------------------------------------------------------- |
| 14 | * Error reporting functions used elsewhere. |
| 15 | */ |
| 16 | |
| 17 | void fatal(char *fmt, ...) |
| 18 | { |
| 19 | va_list ap; |
| 20 | |
| 21 | fprintf(stderr, "fatal error: "); |
| 22 | |
| 23 | va_start(ap, fmt); |
| 24 | vfprintf(stderr, fmt, ap); |
| 25 | va_end(ap); |
| 26 | |
| 27 | fprintf(stderr, "\n"); |
| 28 | exit(1); |
| 29 | } |
| 30 | |
| 31 | /* ---------------------------------------------------------------------- |
| 32 | * GTK front end to puzzles. |
| 33 | */ |
| 34 | |
| 35 | /* |
| 36 | * This structure holds all the data relevant to a single window. |
| 37 | * In principle this would allow us to open multiple independent |
| 38 | * puzzle windows, although I can't currently see any real point in |
| 39 | * doing so. I'm just coding cleanly because there's no |
| 40 | * particularly good reason not to. |
| 41 | */ |
| 42 | struct window_data { |
| 43 | GtkWidget *window; |
| 44 | GtkWidget *area; |
| 45 | midend_data *me; |
| 46 | }; |
| 47 | |
| 48 | static void destroy(GtkWidget *widget, gpointer data) |
| 49 | { |
| 50 | gtk_main_quit(); |
| 51 | } |
| 52 | |
| 53 | gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data) |
| 54 | { |
| 55 | struct window_data *wdata = (struct window_data *)data; |
| 56 | |
| 57 | IGNORE(wdata); |
| 58 | |
| 59 | if (!midend_process_key(wdata->me, 0, 0, event->keyval)) |
| 60 | gtk_widget_destroy(wdata->window); |
| 61 | |
| 62 | return TRUE; |
| 63 | } |
| 64 | |
| 65 | gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data) |
| 66 | { |
| 67 | struct window_data *wdata = (struct window_data *)data; |
| 68 | |
| 69 | IGNORE(wdata); |
| 70 | |
| 71 | return TRUE; |
| 72 | } |
| 73 | |
| 74 | static struct window_data *new_window(void) |
| 75 | { |
| 76 | struct window_data *wdata; |
| 77 | int x, y; |
| 78 | |
| 79 | wdata = snew(struct window_data); |
| 80 | |
| 81 | wdata->me = midend_new(); |
| 82 | midend_new_game(wdata->me, NULL); |
| 83 | |
| 84 | wdata->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 85 | |
| 86 | wdata->area = gtk_drawing_area_new(); |
| 87 | midend_size(wdata->me, &x, &y); |
| 88 | gtk_drawing_area_size(GTK_DRAWING_AREA(wdata->area), x, y); |
| 89 | |
| 90 | gtk_container_add(GTK_CONTAINER(wdata->window), wdata->area); |
| 91 | gtk_widget_show(wdata->area); |
| 92 | |
| 93 | gtk_signal_connect(GTK_OBJECT(wdata->window), "destroy", |
| 94 | GTK_SIGNAL_FUNC(destroy), wdata); |
| 95 | gtk_signal_connect(GTK_OBJECT(wdata->window), "key_press_event", |
| 96 | GTK_SIGNAL_FUNC(key_event), wdata); |
| 97 | gtk_signal_connect(GTK_OBJECT(wdata->area), "button_press_event", |
| 98 | GTK_SIGNAL_FUNC(button_event), wdata); |
| 99 | gtk_widget_show(wdata->window); |
| 100 | return wdata; |
| 101 | } |
| 102 | |
| 103 | int main(int argc, char **argv) |
| 104 | { |
| 105 | gtk_init(&argc, &argv); |
| 106 | (void) new_window(); |
| 107 | gtk_main(); |
| 108 | |
| 109 | return 0; |
| 110 | } |