}
/* save the puzzle to a file */
-void save_puzzle (Puzzle *puzzle, const char* file) {
+void save_puzzle (Puzzle *puzzle, const char* file)
+{
FILE *outfile;
/* First output the uncompressed contents to a temp file */
outfile = tmpfile ();
}
/* read the puzzle from a file */
-Puzzle load_puzzle (const char* file) {
+Puzzle load_puzzle (const char* file)
+{
/* First open the GZip file */
gzFile insourcefile = gzopen (file, "rb");
if (insourcefile == NULL)
}
+/* save the user grid to a file */
+void save_user_data (MainPlayerData *app_data, const char *filename)
+{
+ FILE *outfile;
+ outfile = fopen (filename, "wb");
+ if (outfile == NULL)
+ {
+ fprintf (stderr, ERROR_WRITING_FILE);
+ return;
+ }
+ fprintf (outfile, "%s\n", app_data->filename);
+ for (int i = 0; i < app_data->puzzle.grid_size; i ++)
+ {
+ for (int j = 0; j < app_data->puzzle.grid_size; j ++)
+ fprintf (outfile, "%c", app_data->char_ans[i][j]);
+ fprintf (outfile, "\n");
+ }
+
+ fclose (outfile);
+}
+
+/* load the user grid from a file */
+void load_user_data (MainPlayerData *app_data, const char *filename)
+{
+ FILE *infile;
+ infile = fopen (filename, "rb");
+ if (infile == NULL)
+ {
+ fprintf (stderr, "%s\n", ERROR_READING_FILE);
+ return;
+ }
+
+ char puzzle_file_name[65535];
+ fgets (puzzle_file_name, 65535, infile);
+ reset_player_data (app_data, strtok (puzzle_file_name, "\n"));
+
+ char line[MAX_PUZZLE_SIZE+10];
+ for (int i = 0; i < app_data->puzzle.grid_size; i ++)
+ {
+ fgets (line, MAX_PUZZLE_SIZE+10, infile);
+ for (int j = 0; j < app_data->puzzle.grid_size; j ++)
+ app_data->char_ans[i][j] = line[j];
+
+ }
+ fclose (infile);
+}
+
/* in the player app, move the current selection index left or right */
void move_current_col (MainPlayerData *app_data, enum DIRECTION dir)
{
}
/* slot for load grid state menu */
-void on_menu_load_grid_state_activate (GtkMenuItem *item, gpointer *data)
+void on_menu_load_grid_state_activate (GtkMenuItem *item, GtkDrawingArea *area)
{
- /* TODO */
+ GtkWidget *dialog;
+ GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
+ gint res;
+ dialog = gtk_file_chooser_dialog_new (OPEN_FILE, GTK_WINDOW (main_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));
+ load_user_data (&app_data, filename);
+ g_free (filename);
+ }
+
+ gtk_widget_destroy (dialog);
+
+ gtk_widget_queue_draw_area (GTK_WIDGET(area), 0, 0,
+ app_data.puzzle.grid_size*GRID_PIXELS+10,
+ app_data.puzzle.grid_size*GRID_PIXELS+10);
+
+ update_clue_items ();
}
/* slot for save grid state menu */
void on_menu_save_grid_state_activate (GtkMenuItem *item, gpointer *data)
{
- /* TODO */
+ if (app_data.is_loaded == false)
+ return;
+
+ GtkWidget *dialog;
+ GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_SAVE;
+ gint res;
+ dialog = gtk_file_chooser_dialog_new (SAVE_FILE, GTK_WINDOW(main_window),
+ action,
+ "_Cancel",
+ GTK_RESPONSE_CANCEL,
+ "_Save",
+ 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));
+ save_user_data (&app_data, filename);
+ g_free (filename);
+ }
+
+ gtk_widget_destroy (dialog);
}
/* slot for exit menu */