#include "constantstrings.h"
#include "wordblox_resource.c"
+#include "wordblox.h"
+
+GtkWidget *window;
+GtkListStore *across_store;
+GtkListStore *down_store;
+
+struct MainAppData {
+ Puzzle puzzle;
+ char filename[65535];
+ bool is_loaded;
+ int cur_row;
+ int cur_col;
+} app_data;
+
+/* update the clue items */
+void update_clue_items ()
+{
+ gtk_list_store_clear (across_store);
+ gtk_list_store_clear (down_store);
+
+ /* if the puzzle is loaded */
+ if (app_data.is_loaded == true)
+ {
+ /* iterate through the puzzle grid and gte the clues */
+ for (int i = 0; i < app_data.puzzle.grid_size; i ++)
+ {
+ for (int j = 0; j < app_data.puzzle.grid_size; j ++)
+ {
+ /* if it is the start of an across word */
+ if (app_data.puzzle.start_across_word[i][j] != -1)
+ {
+ GtkTreeIter iter;
+
+ gtk_list_store_append (across_store, &iter);
+ gtk_list_store_set (across_store, &iter, 0,
+ app_data.puzzle.start_across_word[i][j],
+ 1, app_data.puzzle.clue_across[i][j],
+ -1);
+ }
+ /* if it is the start of a down word */
+ if (app_data.puzzle.start_down_word[i][j] != -1)
+ {
+ GtkTreeIter iter;
+ gtk_list_store_append (down_store, &iter);
+ gtk_list_store_set (down_store, &iter, 0,
+ app_data.puzzle.start_down_word[i][j],
+ 1, app_data.puzzle.clue_down[i][j],
+ -1);
+ }
+ }
+ }
+ }
+}
+
+/* move the current selection index left or right */
+void move_current_col (enum DIRECTION dir)
+{
+ int r = app_data.cur_row;
+ int c = app_data.cur_col;
+ if (dir == DIR_FORWARD)
+ {
+ c ++;
+ while (c < app_data.puzzle.grid_size)
+ {
+ if (app_data.puzzle.chars[r][c] == '#')
+ c ++;
+ else
+ break;
+ }
+ if (c < app_data.puzzle.grid_size)
+ app_data.cur_col = c;
+ }
+ else
+ {
+ c --;
+ while (c >= 0)
+ {
+ if (app_data.puzzle.chars[r][c] == '#')
+ c --;
+ else
+ break;
+ }
+ if (c >= 0)
+ app_data.cur_col = c;
+ }
+}
+
+/* move the current selection index up or down */
+void move_current_row (enum DIRECTION dir)
+{
+ int r = app_data.cur_row;
+ int c = app_data.cur_col;
+ if (dir == DIR_FORWARD)
+ {
+ r ++;
+ while (r < app_data.puzzle.grid_size)
+ {
+ if (app_data.puzzle.chars[r][c] == '#')
+ r ++;
+ else
+ break;
+ }
+ if (r < app_data.puzzle.grid_size)
+ app_data.cur_row = r;
+ }
+ else
+ {
+ r --;
+ while (r >= 0)
+ {
+ if (app_data.puzzle.chars[r][c] == '#')
+ r --;
+ else
+ break;
+ }
+ if (r >= 0)
+ app_data.cur_row = r;
+ }
+}
+
+
+/* slot for handling key press event for puzzle drawing area */
+gboolean on_puzzle_area_key_press_event (GtkWidget *widget,
+ GdkEventKey *event, gpointer data)
+{
+ /* respond to key events only if the puzzle is loaded */
+ if (app_data.is_loaded == true)
+ {
+ switch (event->keyval)
+ {
+ case GDK_KEY_Down : move_current_row (DIR_FORWARD);
+ gtk_widget_queue_draw_area (widget,
+ 0, 0,
+ app_data.puzzle.grid_size*GRID_PIXELS+10,
+ app_data.puzzle.grid_size*GRID_PIXELS+10);
+ break;
+ case GDK_KEY_Up : move_current_row (DIR_BACK);
+ gtk_widget_queue_draw_area (widget,
+ 0, 0,
+ app_data.puzzle.grid_size*GRID_PIXELS+10,
+ app_data.puzzle.grid_size*GRID_PIXELS+10);
+ break;
+ case GDK_KEY_Right: move_current_col (DIR_FORWARD);
+ gtk_widget_queue_draw_area (widget,
+ 0, 0,
+ app_data.puzzle.grid_size*GRID_PIXELS+10,
+ app_data.puzzle.grid_size*GRID_PIXELS+10);
+ break;
+ case GDK_KEY_Left : move_current_col (DIR_BACK);
+ gtk_widget_queue_draw_area (widget,
+ 0, 0,
+ app_data.puzzle.grid_size*GRID_PIXELS+10,
+ app_data.puzzle.grid_size*GRID_PIXELS+10);
+ }
+ }
+
+ return FALSE;
+}
+
+/* slot for drawing the puzzle */
+gboolean on_puzzle_area_draw (GtkWidget *widget, cairo_t *cr, gpointer data)
+{
+ /* if a puzzle is loaded */
+ if (app_data.is_loaded == true)
+ {
+ GdkRGBA colorfore, colorback, colorblue, colorbacksel;
+ gdk_rgba_parse (&colorfore, "#000000");
+ gdk_rgba_parse (&colorback, "#ffffff");
+ gdk_rgba_parse (&colorblue, "#0000dd");
+ gdk_rgba_parse (&colorbacksel, "#ffffaa");
+ cairo_set_line_width (cr, 3);
+
+ /* set the size of the drawing area */
+ gtk_widget_set_size_request (widget,
+ app_data.puzzle.grid_size*GRID_PIXELS+10,
+ app_data.puzzle.grid_size*GRID_PIXELS+10);
+
+ /* Draw the grid */
+ for (int i = 0; i < app_data.puzzle.grid_size; i ++)
+ {
+ for (int j = 0; j < app_data.puzzle.grid_size; j ++)
+ {
+ /* if it is the current selection or if -1 is the current
+ selection then let the current selection be the first word */
+ if (app_data.cur_col == -1 && app_data.cur_row == -1)
+ {
+ if (app_data.puzzle.start_across_word[i][j] == 1)
+ {
+ app_data.cur_row = i;
+ app_data.cur_col = j;
+ }
+ else if (app_data.puzzle.start_down_word[i][j] == 1)
+ {
+ app_data.cur_row = i;
+ app_data.cur_col = j;
+ }
+ }
+
+ gdk_cairo_set_source_rgba (cr, &colorfore);
+ cairo_rectangle (cr, j*GRID_PIXELS+5, i*GRID_PIXELS+5,
+ GRID_PIXELS, GRID_PIXELS);
+
+ cairo_stroke (cr);
+
+ /* if it is not a blank grid then set the background color
+ to black */
+ if (app_data.puzzle.chars[i][j] != '#')
+ gdk_cairo_set_source_rgba (cr, &colorback);
+
+ /* if it is a current selection then set the background
+ to yellow */
+ if (app_data.cur_row == i && app_data.cur_col == j)
+ gdk_cairo_set_source_rgba (cr, &colorbacksel);
+
+ cairo_rectangle (cr, j*GRID_PIXELS+5, i*GRID_PIXELS+5,
+ GRID_PIXELS, GRID_PIXELS);
+ cairo_fill (cr);
+
+ /* draw the word number if it is the start of a word */
+ if (app_data.puzzle.start_across_word[i][j] != -1 ||
+ app_data.puzzle.start_down_word[i][j] != -1)
+ {
+ int num;
+ if (app_data.puzzle.start_across_word[i][j] != -1)
+ num = app_data.puzzle.start_across_word[i][j];
+ else
+ num = app_data.puzzle.start_down_word[i][j];
+
+ gdk_cairo_set_source_rgba (cr, &colorblue);
+ cairo_select_font_face (cr, "sans serif",
+ CAIRO_FONT_SLANT_NORMAL,
+ CAIRO_FONT_WEIGHT_NORMAL);
+ cairo_set_font_size (cr, 11);
+ cairo_move_to (cr, j*GRID_PIXELS+7, i*GRID_PIXELS+15);
+ char cnum[3];
+ sprintf (cnum, "%d", num);
+ cairo_show_text (cr, (const char*)cnum);
+ }
+
+
+ }
+ }
+ }
+
+ return FALSE;
+
+}
+
+/* slot for reveal solution menu */
+void on_menu_reveal_solution_activate (GtkMenuItem *item, gpointer *data)
+{
+ /* TODO */
+}
+
+/* slot for save grid state menu */
+void on_menu_save_grid_state_activate (GtkMenuItem *item, gpointer *data)
+{
+ /* TODO */
+}
/* slot for exit menu */
void on_menu_exit_activate (GtkMenuItem *item, gpointer data)
gtk_main_quit ();
}
+/* slot for open menu */
+void on_menu_open_activate (GtkMenuItem *item, GtkDrawingArea* area)
+{
+ GtkWidget *dialog;
+ GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
+ gint res;
+ dialog = gtk_file_chooser_dialog_new (OPEN_FILE, GTK_WINDOW(window), action,
+ "_Cancel",
+ GTK_RESPONSE_CANCEL,
+ "_Open",
+ GTK_RESPONSE_ACCEPT,
+ NULL);
+ res = gtk_dialog_run (GTK_DIALOG (dialog));
+ if (res == GTK_RESPONSE_ACCEPT)
+ {
+ char *filename;
+ filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(dialog));
+ app_data.puzzle = load_puzzle (filename);
+ app_data.is_loaded = true;
+ strcpy (app_data.filename, filename);
+ /* if the grid is not frozen then the game cannot be played */
+ if (app_data.puzzle.grid_frozen == false)
+ {
+ GtkWidget *errordlg ;
+ errordlg = gtk_message_dialog_new (GTK_WINDOW(window),
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_ERROR,
+ GTK_BUTTONS_CLOSE,
+ UNFROZEN_GRID_PLAYER);
+ gtk_dialog_run (GTK_DIALOG(errordlg));
+ gtk_widget_destroy (errordlg);
+ app_data.is_loaded = false;
+ app_data.cur_col = -1;
+ app_data.cur_row = -1;
+ }
+ else
+ {
+ gtk_widget_queue_draw_area (GTK_WIDGET (area), 0, 0,
+ app_data.puzzle.grid_size*30+10,
+ app_data.puzzle.grid_size*30+10);
+ update_clue_items ();
+ }
+ g_free (filename);
+ }
+
+ gtk_widget_destroy (dialog);
+}
+
/* slot for about menu */
void on_menu_about_activate (GtkMenuItem *item, gpointer data)
{
const char *AUTHOR[] = {"V.Harishankar", NULL};
- gtk_show_about_dialog (NULL, "authors",AUTHOR,
+ gtk_show_about_dialog (GTK_WINDOW(window), "authors",AUTHOR,
"program-name", PROGRAM_NAME,
"copyright", COPYRIGHT,
"comments", COMMENTS,
int main (int argc, char *argv [])
{
gtk_init (&argc, &argv);
- GtkBuilder *builder;
- GtkWindow *window;
GdkPixbuf *icon;
-
icon = gdk_pixbuf_new_from_resource
("/org/harishankar/wordblox/wordblox.svg", NULL);
if (icon == NULL)
fprintf (stderr, ERROR_ICON);
+ GtkBuilder *builder;
builder = gtk_builder_new ();
guint ret = gtk_builder_add_from_resource (builder,
"/org/harishankar/wordblox/wordblox_player.glade", NULL);
+ app_data.is_loaded = false;
+
if (ret == 0)
{
fprintf (stderr, ERROR_WINDOW);
}
else
{
- window = GTK_WINDOW (gtk_builder_get_object (builder, "main_window") );
+ window = GTK_WIDGET (gtk_builder_get_object (builder, "main_window") );
+ across_store = GTK_LIST_STORE (gtk_builder_get_object
+ (builder, "store_across_clues"));
+ down_store = GTK_LIST_STORE (gtk_builder_get_object
+ (builder, "store_down_clues"));
+
if (window != NULL)
{
gtk_window_set_default_icon (icon);
gtk_builder_connect_signals (builder, NULL);
g_object_unref (builder);
- gtk_widget_show (GTK_WIDGET(window));
+ gtk_widget_show (window);
gtk_main ();
return 0;
}