General further development. Sketched out the mid-end, added more
[sgt/puzzles] / gtk.c
CommitLineData
720a8fb7 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
83680571 9#include <gtk/gtk.h>
10
720a8fb7 11#include "puzzles.h"
12
83680571 13/* ----------------------------------------------------------------------
14 * Error reporting functions used elsewhere.
15 */
16
720a8fb7 17void 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}
83680571 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 */
42struct window_data {
43 GtkWidget *window;
7f77ea24 44 GtkWidget *area;
45 midend_data *me;
83680571 46};
47
48static void destroy(GtkWidget *widget, gpointer data)
49{
50 gtk_main_quit();
51}
52
7f77ea24 53gint 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
65gint 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
83680571 74static struct window_data *new_window(void)
75{
76 struct window_data *wdata;
7f77ea24 77 int x, y;
83680571 78
79 wdata = snew(struct window_data);
80
7f77ea24 81 wdata->me = midend_new();
82 midend_new_game(wdata->me, NULL);
83
83680571 84 wdata->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
7f77ea24 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
83680571 93 gtk_signal_connect(GTK_OBJECT(wdata->window), "destroy",
94 GTK_SIGNAL_FUNC(destroy), wdata);
7f77ea24 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);
83680571 99 gtk_widget_show(wdata->window);
100 return wdata;
101}
102
103int main(int argc, char **argv)
104{
105 gtk_init(&argc, &argv);
106 (void) new_window();
107 gtk_main();
108
109 return 0;
110}