+#define RACK_DICT_ENTRY_OBSCURE (1<<1)
+#define RACK_DICT_ENTRY_FOUND (1<<2)
+#define MAX_TILES 7
+
+typedef struct _tile
+{
+ char letter;
+ int rack_index;
+ int x, y;
+ GooCanvasItem *item;
+ gboolean guessed;
+} tile_t;
+
+typedef struct _rack
+{
+ tile_t *tiles[MAX_TILES];
+ int num_tiles;
+ char guess[MAX_TILES+1];
+ int guess_length;
+ bag_t bag;
+ dict_t dict;
+ dict_t obscure;
+ dict_t solution;
+ int solution_total;
+ GooCanvasItem *solution_item;
+ gboolean done;
+} rack_t;
+
+#define LETTER_SIZE 60
+#define LETTER_PAD 5
+
+static void
+guess_tile_position (int i, int *x, int *y)
+{
+ *x = 20 + i * (LETTER_SIZE + LETTER_PAD);
+ *y = LETTER_PAD;
+}
+
+static void
+rack_tile_position (int i, int *x, int *y)
+{
+ guess_tile_position (i, x, y);
+ *y += (LETTER_SIZE + LETTER_PAD);
+}
+
+typedef enum dict_paint_cursor_show
+{
+ DICT_PAINT_CURSOR_SHOW_FOUND,
+ DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS,
+ DICT_PAINT_CURSOR_SHOW_ALL
+} dict_paint_cursor_show_t;
+
+typedef struct _dict_paint_cursor
+{
+ cairo_t *cr;
+ int line_height;
+ int x;
+ int y;
+ int max_column_width;
+ int max_y;
+ dict_paint_cursor_show_t show;
+} dict_paint_cursor_t;
+
+static void
+dict_paint_action (void *closure, char *word, dict_entry_t *entry)
+{
+ dict_paint_cursor_t *cursor = closure;
+ cairo_t *cr = cursor->cr;
+ double new_x, new_y;
+ int found, show_blanks = FALSE;
+
+ if (strlen (word) < 5)
+ return;
+
+ found = *entry & RACK_DICT_ENTRY_FOUND;
+
+ cairo_set_source_rgb (cr, 0, 0, 0); /* black */
+
+ switch (cursor->show) {
+ case DICT_PAINT_CURSOR_SHOW_FOUND:
+ if (! found)
+ return;
+ break;
+ case DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS:
+ if (found)
+ return;
+ show_blanks = TRUE;
+ break;
+ case DICT_PAINT_CURSOR_SHOW_ALL:
+ if (! found)
+ cairo_set_source_rgb (cr, 1, 0, 0); /* red */
+ break;
+ }
+
+ /* "Obscure" words get some special coloring. */
+ if (*entry & RACK_DICT_ENTRY_OBSCURE) {
+ if (found) {
+ cairo_set_source_rgb (cr, 0, 1, 0); /* green */
+ } else {
+ if (cursor->show == DICT_PAINT_CURSOR_SHOW_ALL)
+ cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
+ else
+ return;
+ }
+ }
+
+ cairo_move_to (cr, cursor->x, cursor->y);
+ if (show_blanks) {
+ int i, length = strlen (word);
+ for (i = 0; i < length; i++)
+ cairo_show_text (cr, "_");
+ } else {
+ cairo_show_text (cr, word);
+ }
+ cairo_get_current_point (cr, &new_x, &new_y);
+ if (new_x > cursor->max_column_width)
+ cursor->max_column_width = new_x;
+ cursor->y += cursor->line_height;
+ if (cursor->y > cursor->max_y) {
+ cursor->x = cursor->max_column_width + cursor->line_height / 2;
+ cursor->y = cursor->line_height;
+ }
+}
+
+#define SOLUTION_FONT_SIZE 12
+#define SOLUTION_LINE_HEIGHT (1.5 * SOLUTION_FONT_SIZE)
+
+static void
+dict_paint (cairo_t *cr, void *closure, double width, double height)
+{
+ rack_t *rack = closure;
+ dict_paint_cursor_t cursor;
+ int length, count;
+
+ cairo_save (cr);
+ cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
+
+ cursor.cr = cr;
+
+ cairo_select_font_face (cr, "mono", 0, 0);
+ cairo_set_font_size (cr, SOLUTION_FONT_SIZE);
+ cursor.line_height = SOLUTION_LINE_HEIGHT;
+
+ cursor.x = 0;
+ cursor.y = cursor.line_height;
+
+ cursor.max_column_width = 0;
+ cursor.max_y = height;
+
+ length = 1;
+ count = 0;
+ do {
+ if (rack->done)
+ cursor.show = DICT_PAINT_CURSOR_SHOW_ALL;
+ else
+ cursor.show = DICT_PAINT_CURSOR_SHOW_FOUND;
+ count += dict_for_each_of_length (&rack->solution,
+ dict_paint_action, &cursor,
+ length, length);
+ if (! rack->done) {
+ cursor.show = DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS;
+ dict_for_each_of_length (&rack->solution,
+ dict_paint_action, &cursor,
+ length, length);
+ }
+ length++;
+ } while (count < rack->solution_total);
+
+ cairo_restore (cr);
+}
+
+static void
+tile_paint (cairo_t *cr, void *closure, double width, double height)
+{
+ tile_t *tile = closure;
+
+ cairo_pattern_t *gradient;
+ cairo_text_extents_t extents;
+ int rad = (int) MIN (width / 2, height / 2);
+ int cx = width / 2;
+ int cy = cx;
+ int tx, ty;
+ double spot_angle = M_PI / 4.0;
+ double spot_rad = rad / 2.0;
+ char string[2];
+
+ cairo_save (cr);
+
+ gradient = cairo_pattern_create_radial (cx - spot_rad * cos (spot_angle),
+ cy - spot_rad * sin (spot_angle),
+ 0.0,
+ cx - spot_rad * cos (spot_angle),
+ cy - spot_rad * sin (spot_angle),
+ rad + spot_rad);
+ cairo_pattern_add_color_stop_rgb (gradient, 0.0, 1.0, 1.0, 1.0);
+ cairo_pattern_add_color_stop_rgb (gradient, 1.0, 0.33, 0.33, 0.33);
+
+ cairo_set_source (cr, gradient);
+
+ cairo_arc (cr,
+ cx, cy,
+ rad, 0, 2 * M_PI);
+
+ cairo_fill (cr);
+
+ cairo_select_font_face (cr, "mono",
+ CAIRO_FONT_SLANT_NORMAL,
+ CAIRO_FONT_WEIGHT_BOLD);
+ cairo_set_font_size (cr, 1.8 * rad);
+
+ string[0] = tile->letter;
+ string[1] = '\0';
+ cairo_text_extents (cr, string, &extents);
+ tx = cx - extents.width / 2 - extents.x_bearing;
+ ty = cy - extents.height / 2 - extents.y_bearing;
+
+ cairo_set_source_rgb (cr, 0.7, 0.7, 0.7);
+ cairo_move_to (cr, tx + 1, ty + 1);
+ cairo_show_text (cr, string);
+
+ cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
+ cairo_move_to (cr, tx - 1, ty - 1);
+ cairo_show_text (cr, string);
+
+ cairo_set_source_rgb (cr, 0.2, 0.3, 0.8);
+ cairo_move_to (cr, tx, ty);
+ cairo_show_text (cr, string);
+
+ cairo_restore (cr);
+}
+
+static void
+tile_glide_to (tile_t *tile, int x, int y)
+{
+ goo_canvas_item_animate (tile->item, x, y,
+ 1.0, 0,
+ 500, 40,
+ GOO_CANVAS_ANIMATE_FREEZE);
+ tile->x = x;
+ tile->y = y;
+}
+
+static tile_t *
+tile_create (GooCanvasItem *parent,
+ char letter, int rack_index)
+{
+ tile_t *tile;
+
+ tile = g_malloc (sizeof (tile_t));
+ tile->letter = tolower (letter);
+ tile->rack_index = rack_index;
+ rack_tile_position (rack_index, &tile->x, &tile->y);
+ tile->item = goo_demo_item_new (parent,
+ tile->x, tile->y,
+ LETTER_SIZE, LETTER_SIZE,
+ tile_paint,
+ tile, NULL);
+
+ tile->guessed = FALSE;
+
+ return tile;
+}
+