]> git.cworth.org Git - loa/blob - loa.c
48b5b045c3fdf76e4418a03e773eb4157581fd3e
[loa] / loa.c
1 /*
2  * Copyright (C) 2008 Carl Worth
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see http://www.gnu.org/licenses/ .
16  *
17  * Author: Carl Worth <cworth@cworth.org>
18  */
19
20 #include <gtk/gtk.h>
21 #include <math.h>
22
23 #define BOARD_SIZE 8
24
25 typedef struct {
26     int x_offset;
27     int y_offset;
28     int width;
29     int height;
30     int cell_size;
31 } layout_t;
32
33 static gboolean
34 on_delete_event_quit (GtkWidget  *widget,
35                       GdkEvent   *event,
36                       gpointer    user_data)
37 {
38     gtk_main_quit ();
39
40     /* Returning FALSE allows the default handler for delete-event
41      * to proceed to cleanup the widget. */
42     return FALSE;
43 }
44
45 static gboolean
46 on_expose_event_draw (GtkWidget         *widget,
47                       GdkEventExpose    *event,
48                       gpointer           user_data)
49 {
50     cairo_t *cr;
51     layout_t *layout = user_data;
52     int x, y;
53
54     if (layout->width != widget->allocation.width ||
55         layout->height != widget->allocation.height)
56     {
57         int size;
58
59         layout->width = widget->allocation.width;
60         layout->height = widget->allocation.height;
61
62         size = MIN (layout->width, layout->height);
63         /* Size must be a multiple of the integer cell_size */
64         layout->cell_size = size / BOARD_SIZE;
65         size = layout->cell_size * BOARD_SIZE;
66
67         layout->x_offset = (layout->width - size) / 2;
68         layout->y_offset = (layout->height - size) / 2;
69     }
70
71     cr = gdk_cairo_create (widget->window);
72
73     cairo_translate (cr, layout->x_offset, layout->y_offset);
74
75     for (y = 0; y < BOARD_SIZE; y++) {
76         for (x = 0; x < BOARD_SIZE; x++) {
77             if ((x + y) % 2 == 0)
78                 cairo_set_source_rgb (cr, 0.89, 0.70, 0.40);
79             else
80                 cairo_set_source_rgb (cr, 0.26, 0.02, 0.01);
81             cairo_rectangle (cr,
82                              x * layout->cell_size, y * layout->cell_size,
83                              layout->cell_size, layout->cell_size);
84             cairo_fill (cr);
85         }
86     }
87
88     cairo_destroy (cr);
89
90     return TRUE;
91 }
92
93 int
94 main (int argc, char *argv[])
95 {
96     GtkWidget *window;
97     GtkWidget *drawing_area;
98     layout_t layout;
99
100     layout.width = 0;
101     layout.height = 0;
102
103     gtk_init (&argc, &argv);
104
105     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
106
107     gtk_window_set_default_size (GTK_WINDOW (window), 100, 100);
108
109     g_signal_connect (window, "delete-event",
110                       G_CALLBACK (on_delete_event_quit), NULL);
111
112     drawing_area = gtk_drawing_area_new ();
113
114     gtk_container_add (GTK_CONTAINER (window), drawing_area);
115
116     g_signal_connect (drawing_area, "expose-event",  
117                       G_CALLBACK (on_expose_event_draw), &layout);
118
119     gtk_widget_show_all (window);
120     
121     gtk_main ();
122
123     return 0;
124 }