Continue work on the GUI - added code for drawing
authorHarishankar <v.harishankar@gmail.com>
Sun, 3 May 2020 13:25:16 +0000 (18:55 +0530)
committerHarishankar <v.harishankar@gmail.com>
Sun, 3 May 2020 13:25:16 +0000 (18:55 +0530)
Added basic drawing code in the GUI for puzzle grid
- still incomplete - only implemented drawing a fixed
grid

Makefile
constantstrings.h
wordblox.c
wordblox.h
wordblox_player.c
wordblox_player.glade

index 23de2da..81d882d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 wordblox: wordblox.c wordblox.h constantstrings.h
        clang wordblox.c -lgd -lz -lcrypt -o wordblox
 
-wordblox_player: wordblox_player.c wordblox_resource.c wordblox.gresource.xml wordblox_player.glade constantstrings.h
+wordblox_player: wordblox_player.c wordblox.h wordblox_resource.c wordblox.gresource.xml wordblox_player.glade constantstrings.h
                glib-compile-resources wordblox.gresource.xml --target wordblox_resource.c --generate-source
-               clang -rdynamic -o wordblox_player wordblox_player.c -Wall `pkg-config --cflags --libs gtk+-3.0`
+               clang -rdynamic -lz -lgd -lcrypt -o wordblox_player wordblox_player.c -Wall `pkg-config --cflags --libs gtk+-3.0`
index 1c0dd49..1aec380 100644 (file)
@@ -42,6 +42,9 @@ columns (warning: existing file name may be overwritten)\n"
 /* for wordblox_player */
 #define ERROR_ICON "Unable to load icon!"
 #define ERROR_WINDOW "Error loading Window!"
+#define OPEN_FILE "Open File"
+#define UNFROZEN_GRID_PLAYER "Puzzle is not finalized/frozen and hence cannot\
+ be played"
 
 /* about box info */
 const char *AUTHOR[] = { "V. Harishankar", NULL};
index fae8b83..8ab9891 100644 (file)
@@ -1,7 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
-#include <stdbool.h>
 #include <string.h>
 #include <ctype.h>
 #include <unistd.h>
index 22963e4..04adfa0 100644 (file)
@@ -2,6 +2,9 @@
 #define __WORDBLOX_H
 #define _XOPEN_SOURCE
 #include <unistd.h>
+#include <stdbool.h>
+#include <string.h>
+#include <ctype.h>
 #include <gd.h>
 #include <gdfontmb.h>
 #include <gdfontg.h>
@@ -391,7 +394,7 @@ void save_puzzle (Puzzle *puzzle, const char* file) {
        gzFile outdestfile = gzopen (file, "wb");
        if (outdestfile == NULL)
        {
-               fprintf (stderr, ERROR_WRITING_FILE);
+               fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
                fclose (outfile);
                exit (1);
        }
@@ -401,7 +404,7 @@ void save_puzzle (Puzzle *puzzle, const char* file) {
                int res = gzwrite (outdestfile, buf, strlen (buf) );
                if (res == 0)
                {
-                       fprintf (stderr, "%s %s", ERROR_WRITING_FILE, COMPRESSED);
+                       fprintf (stderr, "%s %s\n", ERROR_WRITING_FILE, COMPRESSED);
                        fclose (outfile);
                        exit (1);
                }
@@ -417,14 +420,14 @@ Puzzle load_puzzle (const char* file) {
        gzFile insourcefile = gzopen (file, "rb");
        if (insourcefile == NULL)
        {
-               fprintf (stderr, "%s %s", ERROR_READING_FILE, COMPRESSED);
+               fprintf (stderr, "%s %s\n", ERROR_READING_FILE, COMPRESSED);
                exit (1);
        }
        /* Open a temporary file to uncompress the contents */
        FILE *infile = tmpfile ();
        if (infile == NULL)
        {
-               fprintf (stderr, ERROR_READING_FILE);
+               fprintf (stderr, "%s\n", ERROR_READING_FILE);
                exit (1);       
        }
        /* Put the uncompressed content to the temp file */
@@ -434,7 +437,7 @@ Puzzle load_puzzle (const char* file) {
                int res = fwrite (buf, sizeof(char), strlen (buf), infile);
                if (res == 0)
                {
-                       fprintf (stderr, ERROR_READING_FILE);
+                       fprintf (stderr, "%s\n", ERROR_READING_FILE);
                        fclose (infile);
                        gzclose (insourcefile);
                        exit (1);
index cf9e0bc..f4ecbb5 100644 (file)
@@ -2,6 +2,35 @@
 
 #include "constantstrings.h"
 #include "wordblox_resource.c"
+#include "wordblox.h"
+
+GtkWidget *window; 
+
+/* slot for drawing the puzzle */
+gboolean on_puzzle_area_draw (GtkWidget *widget, cairo_t *cr, gpointer data)
+{
+       GdkRGBA colorfore, colorback;
+       gdk_rgba_parse (&colorfore, "#000000"); 
+       gdk_rgba_parse (&colorback, "#ffffff");
+       cairo_set_line_width (cr, 3);
+       gtk_widget_set_size_request (widget, 10*30+5, 10*30+5);
+       for (int i = 0; i < 10; i ++)
+       {
+               for (int j = 0; j < 10; j ++)
+               {
+                       cairo_rectangle (cr, i*30+5, j*30+5, 30, 30);
+                       gdk_cairo_set_source_rgba (cr, &colorfore);
+                       cairo_stroke (cr);
+                       
+                       cairo_rectangle (cr, i*30+5, j*30+5, 30, 30);
+                       gdk_cairo_set_source_rgba (cr, &colorback);
+                       cairo_fill (cr);
+               }
+       }
+       
+       return FALSE;
+
+}
 
 /* slot for exit menu */
 void on_menu_exit_activate (GtkMenuItem *item, gpointer data)
@@ -9,11 +38,49 @@ void on_menu_exit_activate (GtkMenuItem *item, gpointer data)
        gtk_main_quit ();
 }
 
+/* slot for open menu */
+void on_menu_open_activate (GtkMenuItem *item, gpointer data)
+{
+       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));
+               Puzzle p = load_puzzle (filename);
+               /* if the grid is not frozen then the game cannot be played */
+               if (p.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);
+               
+               }
+               
+               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,15 +94,13 @@ 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);
@@ -48,14 +113,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;
                }
index e27bcc5..4b26971 100644 (file)
       <column type="gchararray"/>
     </columns>
   </object>
-  <object class="GtkApplicationWindow" id="main_window">
+  <object class="GtkWindow" id="main_window">
     <property name="can_focus">False</property>
     <property name="title" translatable="yes">Wordblox Player</property>
     <property name="default_width">540</property>
     <property name="default_height">400</property>
-    <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
+    <signal name="destroy" handler="on_menu_exit_activate" swapped="no"/>
     <child type="titlebar">
       <placeholder/>
     </child>
             <property name="can_focus">True</property>
             <child>
               <object class="GtkScrolledWindow">
-                <property name="width_request">70</property>
+                <property name="width_request">80</property>
+                <property name="height_request">80</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
                 <property name="shadow_type">in</property>
+                <child>
+                  <object class="GtkViewport">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="hscroll_policy">natural</property>
+                    <property name="vscroll_policy">natural</property>
+                    <child>
+                      <object class="GtkDrawingArea" id="puzzle_area">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <signal name="draw" handler="on_puzzle_area_draw" swapped="no"/>
+                      </object>
+                    </child>
+                  </object>
+                </child>
               </object>
               <packing>
                 <property name="resize">True</property>