From b43a3bf7a7ae8b3221c886d5392498db868d35c8 Mon Sep 17 00:00:00 2001 From: Harishankar Date: Sun, 3 May 2020 18:55:16 +0530 Subject: [PATCH] Continue work on the GUI - added code for drawing Added basic drawing code in the GUI for puzzle grid - still incomplete - only implemented drawing a fixed grid --- Makefile | 4 +-- constantstrings.h | 3 ++ wordblox.c | 1 - wordblox.h | 13 ++++--- wordblox_player.c | 79 +++++++++++++++++++++++++++++++++++++++---- wordblox_player.glade | 22 ++++++++++-- 6 files changed, 104 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 23de2da..81d882d 100644 --- 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` diff --git a/constantstrings.h b/constantstrings.h index 1c0dd49..1aec380 100644 --- a/constantstrings.h +++ b/constantstrings.h @@ -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}; diff --git a/wordblox.c b/wordblox.c index fae8b83..8ab9891 100644 --- a/wordblox.c +++ b/wordblox.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include diff --git a/wordblox.h b/wordblox.h index 22963e4..04adfa0 100644 --- a/wordblox.h +++ b/wordblox.h @@ -2,6 +2,9 @@ #define __WORDBLOX_H #define _XOPEN_SOURCE #include +#include +#include +#include #include #include #include @@ -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); diff --git a/wordblox_player.c b/wordblox_player.c index cf9e0bc..f4ecbb5 100644 --- a/wordblox_player.c +++ b/wordblox_player.c @@ -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; } diff --git a/wordblox_player.glade b/wordblox_player.glade index e27bcc5..4b26971 100644 --- a/wordblox_player.glade +++ b/wordblox_player.glade @@ -18,12 +18,12 @@ - + False Wordblox Player 540 400 - + @@ -142,10 +142,26 @@ True - 70 + 80 + 80 True True in + + + True + False + natural + natural + + + True + False + + + + + True -- 2.20.1