Rendered the grid in player and also fixed bug in save/load
[wordblah.git] / wordblox_player.c
index cf9e0bc..2904929 100644 (file)
@@ -2,6 +2,55 @@
 
 #include "constantstrings.h"
 #include "wordblox_resource.c"
+#include "wordblox.h"
+
+GtkWidget *window; 
+
+struct MainAppData {
+       Puzzle puzzle;
+       char filename[65535];
+       bool is_loaded;
+} app_data;
+
+/* 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;
+               gdk_rgba_parse (&colorfore, "#000000"); 
+               gdk_rgba_parse (&colorback, "#ffffff");
+               cairo_set_line_width (cr, 3);
+               
+               /* set the size of the drawing area */
+               gtk_widget_set_size_request (widget, app_data.puzzle.grid_size*30+5, 
+                                                                                       app_data.puzzle.grid_size*30+5);
+                                                               
+               /* 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 ++)
+                       {
+                               cairo_rectangle (cr, j*30+5, i*30+5, 30, 30);
+                               gdk_cairo_set_source_rgba (cr, &colorfore);
+                               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);
+                               
+                               cairo_rectangle (cr, j*30+5, i*30+5, 30, 30);
+
+                               cairo_fill (cr);
+                       }
+               }
+       }
+                       
+       return FALSE;
+
+}
 
 /* slot for exit menu */
 void on_menu_exit_activate (GtkMenuItem *item, gpointer data)
@@ -9,11 +58,53 @@ 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;
+               }
+               
+               gtk_widget_queue_draw_area (GTK_WIDGET (area), 0, 0, 305, 305);
+               
+               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,
@@ -27,19 +118,19 @@ void on_menu_about_activate (GtkMenuItem *item, gpointer data)
 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);
@@ -48,14 +139,14 @@ int main (int argc, char *argv [])
        }
        else 
        {
-               window = GTK_WINDOW (gtk_builder_get_object (builder, "main_window") );
+               window = GTK_WIDGET (gtk_builder_get_object (builder, "main_window") );
                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;
                }