chiark / gitweb /
Stop the user being able to resize the window.
[sgt-puzzles.git] / gtk.c
1 /*
2  * gtk.c: GTK front end for my puzzle collection.
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <time.h>
8 #include <stdarg.h>
9
10 #include <gtk/gtk.h>
11 #include <gdk/gdkkeysyms.h>
12
13 #include "puzzles.h"
14
15 /* ----------------------------------------------------------------------
16  * Error reporting functions used elsewhere.
17  */
18
19 void fatal(char *fmt, ...)
20 {
21     va_list ap;
22
23     fprintf(stderr, "fatal error: ");
24
25     va_start(ap, fmt);
26     vfprintf(stderr, fmt, ap);
27     va_end(ap);
28
29     fprintf(stderr, "\n");
30     exit(1);
31 }
32
33 /* ----------------------------------------------------------------------
34  * GTK front end to puzzles.
35  */
36
37 /*
38  * This structure holds all the data relevant to a single window.
39  * In principle this would allow us to open multiple independent
40  * puzzle windows, although I can't currently see any real point in
41  * doing so. I'm just coding cleanly because there's no
42  * particularly good reason not to.
43  */
44 struct frontend {
45     GtkWidget *window;
46     GtkWidget *area;
47     GdkPixmap *pixmap;
48     GdkColor *colours;
49     int ncolours;
50     GdkColormap *colmap;
51     int w, h;
52     midend_data *me;
53     GdkGC *gc;
54     int bbox_l, bbox_r, bbox_u, bbox_d;
55     int timer_active;
56 };
57
58 void frontend_default_colour(frontend *fe, float *output)
59 {
60     GdkColor col = fe->window->style->bg[GTK_STATE_NORMAL];
61     output[0] = col.red / 65535.0;
62     output[1] = col.green / 65535.0;
63     output[2] = col.blue / 65535.0;
64 }
65
66 void start_draw(frontend *fe)
67 {
68     fe->gc = gdk_gc_new(fe->area->window);
69     fe->bbox_l = fe->w;
70     fe->bbox_r = 0;
71     fe->bbox_u = fe->h;
72     fe->bbox_d = 0;
73 }
74
75 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour)
76 {
77     gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
78     gdk_draw_rectangle(fe->pixmap, fe->gc, 1, x, y, w, h);
79 }
80
81 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour)
82 {
83     gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
84     gdk_draw_line(fe->pixmap, fe->gc, x1, y1, x2, y2);
85 }
86
87 void draw_polygon(frontend *fe, int *coords, int npoints,
88                   int fill, int colour)
89 {
90     GdkPoint *points = snewn(npoints, GdkPoint);
91     int i;
92
93     for (i = 0; i < npoints; i++) {
94         points[i].x = coords[i*2];
95         points[i].y = coords[i*2+1];
96     }
97
98     gdk_gc_set_foreground(fe->gc, &fe->colours[colour]);
99     gdk_draw_polygon(fe->pixmap, fe->gc, fill, points, npoints);
100
101     sfree(points);
102 }
103
104 void draw_update(frontend *fe, int x, int y, int w, int h)
105 {
106     if (fe->bbox_l > x  ) fe->bbox_l = x  ;
107     if (fe->bbox_r < x+w) fe->bbox_r = x+w;
108     if (fe->bbox_u > y  ) fe->bbox_u = y  ;
109     if (fe->bbox_d < y+h) fe->bbox_d = y+h;
110 }
111
112 void end_draw(frontend *fe)
113 {
114     gdk_gc_unref(fe->gc);
115     fe->gc = NULL;
116
117     if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) {
118         gdk_draw_pixmap(fe->area->window,
119                         fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
120                         fe->pixmap,
121                         fe->bbox_l, fe->bbox_u,
122                         fe->bbox_l, fe->bbox_u,
123                         fe->bbox_r - fe->bbox_l, fe->bbox_d - fe->bbox_u);
124     }
125 }
126
127 static void destroy(GtkWidget *widget, gpointer data)
128 {
129     gtk_main_quit();
130 }
131
132 static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
133 {
134     frontend *fe = (frontend *)data;
135     int keyval;
136
137     if (!fe->pixmap)
138         return TRUE;
139
140     if (event->string[0] && !event->string[1])
141         keyval = (unsigned char)event->string[0];
142     else if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
143         keyval = CURSOR_UP;
144     else if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
145         keyval = CURSOR_DOWN;
146     else if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left)
147         keyval = CURSOR_LEFT;
148     else if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right)
149         keyval = CURSOR_RIGHT;
150     else
151         keyval = -1;
152
153     if (keyval >= 0 &&
154         !midend_process_key(fe->me, 0, 0, keyval))
155         gtk_widget_destroy(fe->window);
156
157     return TRUE;
158 }
159
160 static gint button_event(GtkWidget *widget, GdkEventButton *event,
161                          gpointer data)
162 {
163     frontend *fe = (frontend *)data;
164     int button;
165
166     if (!fe->pixmap)
167         return TRUE;
168
169     if (event->type != GDK_BUTTON_PRESS)
170         return TRUE;
171
172     if (event->button == 1)
173         button = LEFT_BUTTON;
174     else if (event->button == 2)
175         button = MIDDLE_BUTTON;
176     else if (event->button == 3)
177         button = RIGHT_BUTTON;
178     else
179         return FALSE;                  /* don't even know what button! */
180
181     if (!midend_process_key(fe->me, event->x, event->y, button))
182         gtk_widget_destroy(fe->window);
183
184     return TRUE;
185 }
186
187 static gint expose_area(GtkWidget *widget, GdkEventExpose *event,
188                         gpointer data)
189 {
190     frontend *fe = (frontend *)data;
191
192     if (fe->pixmap) {
193         gdk_draw_pixmap(widget->window,
194                         widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
195                         fe->pixmap,
196                         event->area.x, event->area.y,
197                         event->area.x, event->area.y,
198                         event->area.width, event->area.height);
199     }
200     return TRUE;
201 }
202
203 static gint configure_area(GtkWidget *widget,
204                            GdkEventConfigure *event, gpointer data)
205 {
206     frontend *fe = (frontend *)data;
207     GdkGC *gc;
208
209     fe->pixmap = gdk_pixmap_new(widget->window, fe->w, fe->h, -1);
210
211     gc = gdk_gc_new(fe->area->window);
212     gdk_gc_set_foreground(gc, &fe->colours[0]);
213     gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->w, fe->h);
214     gdk_gc_unref(gc);
215
216     midend_redraw(fe->me);
217
218     return TRUE;
219 }
220
221 static gint timer_func(gpointer data)
222 {
223     frontend *fe = (frontend *)data;
224
225     if (fe->timer_active)
226         midend_timer(fe->me, 0.02);    /* may clear timer_active */
227
228     return fe->timer_active;
229 }
230
231 void deactivate_timer(frontend *fe)
232 {
233     fe->timer_active = FALSE;
234 }
235
236 void activate_timer(frontend *fe)
237 {
238     gtk_timeout_add(20, timer_func, fe);
239     fe->timer_active = TRUE;
240 }
241
242 static frontend *new_window(void)
243 {
244     frontend *fe;
245     int x, y;
246
247     fe = snew(frontend);
248
249     fe->me = midend_new(fe);
250     midend_new_game(fe->me, NULL);
251
252     fe->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
253 #if 0
254     gtk_window_set_resizable(GTK_WINDOW(fe->window), FALSE);
255 #else
256     gtk_window_set_policy(GTK_WINDOW(fe->window), FALSE, FALSE, TRUE);
257 #endif
258
259     {
260         int i, ncolours;
261         float *colours;
262         gboolean *success;
263
264         fe->colmap = gdk_colormap_get_system();
265         colours = midend_colours(fe->me, &ncolours);
266         fe->ncolours = ncolours;
267         fe->colours = snewn(ncolours, GdkColor);
268         for (i = 0; i < ncolours; i++) {
269             fe->colours[i].red = colours[i*3] * 0xFFFF;
270             fe->colours[i].green = colours[i*3+1] * 0xFFFF;
271             fe->colours[i].blue = colours[i*3+2] * 0xFFFF;
272         }
273         success = snewn(ncolours, gboolean);
274         gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours,
275                                   FALSE, FALSE, success);
276         for (i = 0; i < ncolours; i++) {
277             if (!success[i])
278                 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
279                         i, fe->colours[i].red >> 8,
280                         fe->colours[i].green >> 8,
281                         fe->colours[i].blue >> 8);
282         }
283     }
284
285     fe->area = gtk_drawing_area_new();
286     midend_size(fe->me, &x, &y);
287     gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
288     fe->w = x;
289     fe->h = y;
290
291     gtk_container_add(GTK_CONTAINER(fe->window), fe->area);
292
293     fe->pixmap = NULL;
294
295     gtk_signal_connect(GTK_OBJECT(fe->window), "destroy",
296                        GTK_SIGNAL_FUNC(destroy), fe);
297     gtk_signal_connect(GTK_OBJECT(fe->window), "key_press_event",
298                        GTK_SIGNAL_FUNC(key_event), fe);
299     gtk_signal_connect(GTK_OBJECT(fe->area), "button_press_event",
300                        GTK_SIGNAL_FUNC(button_event), fe);
301     gtk_signal_connect(GTK_OBJECT(fe->area), "expose_event",
302                        GTK_SIGNAL_FUNC(expose_area), fe);
303     gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event",
304                        GTK_SIGNAL_FUNC(configure_area), fe);
305
306     gtk_widget_add_events(GTK_WIDGET(fe->area), GDK_BUTTON_PRESS_MASK);
307
308     gtk_widget_show(fe->area);
309     gtk_widget_show(fe->window);
310
311     return fe;
312 }
313
314 int main(int argc, char **argv)
315 {
316     srand(time(NULL));
317
318     gtk_init(&argc, &argv);
319     (void) new_window();
320     gtk_main();
321
322     return 0;
323 }