3 #include "constantstrings.h"
4 #include "wordblox_resource.c"
8 GtkListStore
*across_store
;
9 GtkListStore
*down_store
;
11 MainPlayerData app_data
;
13 /* update the clue items store */
14 void update_clue_items ()
16 gtk_list_store_clear (across_store
);
17 gtk_list_store_clear (down_store
);
19 /* if the puzzle is loaded */
20 if (app_data
.is_loaded
== true)
22 /* iterate through the puzzle grid and gte the clues */
23 for (int i
= 0; i
< app_data
.puzzle
.grid_size
; i
++)
25 for (int j
= 0; j
< app_data
.puzzle
.grid_size
; j
++)
27 /* if it is the start of an across word */
28 if (app_data
.puzzle
.start_across_word
[i
][j
] != -1)
32 gtk_list_store_append (across_store
, &iter
);
33 gtk_list_store_set (across_store
, &iter
, 0,
34 app_data
.puzzle
.start_across_word
[i
][j
],
35 1, app_data
.puzzle
.clue_across
[i
][j
],
38 /* if it is the start of a down word */
39 if (app_data
.puzzle
.start_down_word
[i
][j
] != -1)
42 gtk_list_store_append (down_store
, &iter
);
43 gtk_list_store_set (down_store
, &iter
, 0,
44 app_data
.puzzle
.start_down_word
[i
][j
],
45 1, app_data
.puzzle
.clue_down
[i
][j
],
53 /* slot for handling mouse button event for puzzle drawing area */
54 gboolean
on_puzzle_area_button_press_event (GtkWidget
*widget
,
55 GdkEventButton
*event
, gpointer data
)
57 if (event
->type
== GDK_BUTTON_PRESS
&& event
->button
== 1)
59 int col
= (event
->x
- 5) / GRID_PIXELS
;
60 int row
= (event
->y
- 5) / GRID_PIXELS
;
62 if (app_data
.puzzle
.chars
[row
][col
] != '#')
64 if (row
< app_data
.puzzle
.grid_size
&&
65 col
< app_data
.puzzle
.grid_size
)
67 app_data
.cur_row
= row
;
68 app_data
.cur_col
= col
;
73 gtk_widget_queue_draw_area (widget
, 0, 0,
74 app_data
.puzzle
.grid_size
*GRID_PIXELS
+10,
75 app_data
.puzzle
.grid_size
*GRID_PIXELS
+10);
82 /* slot for handling key press event for puzzle drawing area */
83 gboolean
on_puzzle_area_key_press_event (GtkWidget
*widget
,
84 GdkEventKey
*event
, gpointer data
)
86 /* respond to key events only if the puzzle is loaded */
87 if (app_data
.is_loaded
== true)
89 switch (event
->keyval
)
91 case GDK_KEY_Down
: move_current_row (&app_data
, DIR_FORWARD
);
93 case GDK_KEY_Up
: move_current_row (&app_data
, DIR_BACK
);
95 case GDK_KEY_Right
: move_current_col (&app_data
, DIR_FORWARD
);
97 case GDK_KEY_Left
: move_current_col (&app_data
, DIR_BACK
);
101 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = ' ';
105 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'A';
109 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'B';
113 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'C';
117 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'D';
121 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'E';
125 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'F';
129 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'G';
133 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'H';
137 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'I';
141 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'J';
145 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'K';
149 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'L';
153 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'M';
157 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'N';
161 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'O';
165 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'P';
169 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'Q';
173 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'R';
177 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'S';
181 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'T';
185 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'U';
189 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'V';
193 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'W';
197 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'X';
201 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'Y';
205 app_data
.char_ans
[app_data
.cur_row
][app_data
.cur_col
] = 'Z';
207 default : return FALSE
;
212 gtk_widget_queue_draw_area (widget
, 0, 0,
213 app_data
.puzzle
.grid_size
*GRID_PIXELS
+10,
214 app_data
.puzzle
.grid_size
*GRID_PIXELS
+10);
219 /* slot for drawing the puzzle */
220 gboolean
on_puzzle_area_draw (GtkWidget
*widget
, cairo_t
*cr
, gpointer data
)
222 /* if a puzzle is loaded */
223 if (app_data
.is_loaded
== true)
225 GdkRGBA colorfore
, colorback
, colorblue
, colorbacksel
;
226 gdk_rgba_parse (&colorfore
, "#000000");
227 gdk_rgba_parse (&colorback
, "#ffffff");
228 gdk_rgba_parse (&colorblue
, "#0000dd");
229 gdk_rgba_parse (&colorbacksel
, "#ffffaa");
230 cairo_set_line_width (cr
, 3);
232 /* set the size of the drawing area */
233 gtk_widget_set_size_request (widget
,
234 app_data
.puzzle
.grid_size
*GRID_PIXELS
+10,
235 app_data
.puzzle
.grid_size
*GRID_PIXELS
+10);
238 for (int i
= 0; i
< app_data
.puzzle
.grid_size
; i
++)
240 for (int j
= 0; j
< app_data
.puzzle
.grid_size
; j
++)
242 /* if it is the current selection or if -1 is the current
243 selection then let the current selection be the first word */
244 if (app_data
.cur_col
== -1 && app_data
.cur_row
== -1)
246 if (app_data
.puzzle
.start_across_word
[i
][j
] == 1)
248 app_data
.cur_row
= i
;
249 app_data
.cur_col
= j
;
251 else if (app_data
.puzzle
.start_down_word
[i
][j
] == 1)
253 app_data
.cur_row
= i
;
254 app_data
.cur_col
= j
;
258 gdk_cairo_set_source_rgba (cr
, &colorfore
);
259 cairo_rectangle (cr
, j
*GRID_PIXELS
+5, i
*GRID_PIXELS
+5,
260 GRID_PIXELS
, GRID_PIXELS
);
264 /* if it is not a blank grid then set the background color
266 if (app_data
.puzzle
.chars
[i
][j
] != '#')
267 gdk_cairo_set_source_rgba (cr
, &colorback
);
269 /* if it is a current selection then set the background
271 if (app_data
.cur_row
== i
&& app_data
.cur_col
== j
)
272 gdk_cairo_set_source_rgba (cr
, &colorbacksel
);
274 cairo_rectangle (cr
, j
*GRID_PIXELS
+5, i
*GRID_PIXELS
+5,
275 GRID_PIXELS
, GRID_PIXELS
);
278 /* draw the word number if it is the start of a word */
279 if (app_data
.puzzle
.start_across_word
[i
][j
] != -1 ||
280 app_data
.puzzle
.start_down_word
[i
][j
] != -1)
283 if (app_data
.puzzle
.start_across_word
[i
][j
] != -1)
284 num
= app_data
.puzzle
.start_across_word
[i
][j
];
286 num
= app_data
.puzzle
.start_down_word
[i
][j
];
288 gdk_cairo_set_source_rgba (cr
, &colorblue
);
289 cairo_select_font_face (cr
, "sans serif",
290 CAIRO_FONT_SLANT_NORMAL
,
291 CAIRO_FONT_WEIGHT_NORMAL
);
292 cairo_set_font_size (cr
, 11);
293 cairo_move_to (cr
, j
*GRID_PIXELS
+7, i
*GRID_PIXELS
+15);
295 sprintf (cnum
, "%d", num
);
296 cairo_show_text (cr
, (const char*)cnum
);
299 /* draw the answer if it is there */
300 gdk_cairo_set_source_rgba (cr
, &colorfore
);
301 cairo_select_font_face (cr
, "sans serif",
302 CAIRO_FONT_SLANT_NORMAL
,
303 CAIRO_FONT_WEIGHT_BOLD
);
305 cairo_set_font_size (cr
, 16);
306 cairo_move_to (cr
, j
*GRID_PIXELS
+GRID_PIXELS
/2,
307 i
*GRID_PIXELS
+GRID_PIXELS
-10);
309 sprintf (ctxt
, "%c", app_data
.char_ans
[i
][j
]);
310 cairo_show_text (cr
, (const char*) ctxt
);
320 /* slot for reveal solution menu */
321 void on_menu_reveal_solution_activate (GtkMenuItem
*item
, gpointer
*data
)
326 /* slot for save grid state menu */
327 void on_menu_save_grid_state_activate (GtkMenuItem
*item
, gpointer
*data
)
332 /* slot for exit menu */
333 void on_menu_exit_activate (GtkMenuItem
*item
, gpointer data
)
338 /* slot for open menu */
339 void on_menu_open_activate (GtkMenuItem
*item
, GtkDrawingArea
* area
)
342 GtkFileChooserAction action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
344 dialog
= gtk_file_chooser_dialog_new (OPEN_FILE
, GTK_WINDOW(window
), action
,
350 res
= gtk_dialog_run (GTK_DIALOG (dialog
));
351 if (res
== GTK_RESPONSE_ACCEPT
)
354 filename
= gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(dialog
));
356 reset_player_data (&temp
, filename
);
358 /* if the grid is not frozen then the game cannot be played */
359 if (temp
.is_loaded
== false)
361 GtkWidget
*errordlg
;
362 errordlg
= gtk_message_dialog_new (GTK_WINDOW(window
),
363 GTK_DIALOG_DESTROY_WITH_PARENT
,
366 UNFROZEN_GRID_PLAYER
);
367 gtk_dialog_run (GTK_DIALOG(errordlg
));
368 gtk_widget_destroy (errordlg
);
373 gtk_widget_queue_draw_area (GTK_WIDGET (area
), 0, 0,
374 app_data
.puzzle
.grid_size
*30+10,
375 app_data
.puzzle
.grid_size
*30+10);
378 update_clue_items ();
382 gtk_widget_destroy (dialog
);
385 /* slot for about menu */
386 void on_menu_about_activate (GtkMenuItem
*item
, gpointer data
)
388 const char *AUTHOR
[] = {"V.Harishankar", NULL
};
389 gtk_show_about_dialog (GTK_WINDOW(window
), "authors",AUTHOR
,
390 "program-name", PROGRAM_NAME
,
391 "copyright", COPYRIGHT
,
392 "comments", COMMENTS
,
394 "website-label", WEBSITE_LABEL
,
395 "license-type", GTK_LICENSE_GPL_2_0
,
400 int main (int argc
, char *argv
[])
402 gtk_init (&argc
, &argv
);
404 icon
= gdk_pixbuf_new_from_resource
405 ("/org/harishankar/wordblox/wordblox.svg", NULL
);
407 fprintf (stderr
, ERROR_ICON
);
410 builder
= gtk_builder_new ();
411 guint ret
= gtk_builder_add_from_resource (builder
,
412 "/org/harishankar/wordblox/wordblox_player.glade", NULL
);
414 app_data
.is_loaded
= false;
418 fprintf (stderr
, ERROR_WINDOW
);
419 g_object_unref (builder
);
424 window
= GTK_WIDGET (gtk_builder_get_object (builder
, "main_window") );
425 across_store
= GTK_LIST_STORE (gtk_builder_get_object
426 (builder
, "store_across_clues"));
427 down_store
= GTK_LIST_STORE (gtk_builder_get_object
428 (builder
, "store_down_clues"));
432 gtk_window_set_default_icon (icon
);
434 GtkWidget
*draw_area
= GTK_WIDGET (
435 gtk_builder_get_object(builder
, "puzzle_area"));
437 /* make drawing area respond to mouse event */
438 gtk_widget_set_events(draw_area
,
439 gtk_widget_get_events(draw_area
)
440 | GDK_BUTTON_PRESS_MASK
);
442 gtk_builder_connect_signals (builder
, NULL
);
443 g_object_unref (builder
);
444 gtk_widget_show (window
);
450 g_object_unref (builder
);
451 fprintf (stderr
, ERROR_WINDOW
);