Import upstream Source
[gamazons.git] / src / callbacks.c
blobdf046b1e383787a42b0eb5e4bf4d23d37798947b
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
5 #include <gnome.h>
6 #include <libgen.h>
8 #include "callbacks.h"
9 #include "interface.h"
10 #include "support.h"
11 #include "amazons.h"
12 #include "board.h"
13 #include "bstate.h"
15 extern Board *board;
16 extern struct options options;
17 extern struct game_states states;
18 extern Square legal_moves[100];
19 extern int ok;
20 extern GtkWidget *main_window;
21 extern state_hash;
22 int grabbed_queen;
23 GtkWidget *PlayerSettingsWindow;
24 extern int moving_ai;
26 GtkWidget *file_selector;
27 gchar *selected_filename;
29 /* Local Prototypes */
30 void load_new_theme (GtkFileSelection *selector, gpointer user_data);
31 void save_game_history(GtkFileSelection *selector, gpointer user_data);
32 void on_ThemeCancelButton_clicked(GtkButton *button, gpointer user_data);
33 void on_save_as1CancelButton_clicked(GtkButton *button, gpointer user_data);
34 void on_OpenCancelButton_clicked(GtkButton *button, gpointer user_data);
35 void load_saved_game(GtkFileSelection *selector, gpointer user_data);
38 void
39 on_new1_activate (GtkMenuItem *menuitem,
40 gpointer user_data)
43 if (bstate_get_moving_piece())
45 gnome_warning_dialog("Unable to start a new game while a piece is being moved. Please try again.");
46 return;
49 free_all_memory();
50 init_engine();
51 destroy_board();
52 init_game_board(main_window);
53 load_values_from_file();
55 bstate_set_just_finished(NEW_GAME);
56 while (bstate_get_what_next() == WAIT_FOR_AI)
57 move_ai(); //if both players are AI, keeps on running
63 void
64 on_quit1_activate (GtkMenuItem *menuitem,
65 gpointer user_data)
68 state_hash = 0;
69 bstate_set_just_finished(QUIT_GAME);
70 gtk_main_quit();
76 void
77 on_clear1_activate (GtkMenuItem *menuitem,
78 gpointer user_data)
84 void
85 on_preferences1_activate (GtkMenuItem *menuitem,
86 gpointer user_data)
92 void
93 on_about1_activate (GtkMenuItem *menuitem,
94 gpointer user_data)
96 static GtkWidget *about;
97 GdkPixbuf *pixbuf = NULL;
99 const gchar *authors[] = {"Ron Yorgason", "Sean McMillen", NULL};
100 gchar *documenters[] = {
101 NULL
103 /* Translator credits */
104 gchar *translator_credits = _("translator_credits");
106 if (about != NULL) {
107 gdk_window_raise (about->window);
108 gdk_window_show (about->window);
109 return;
113 char filename[1024];
115 strcpy(filename, PACKAGE_DATA_DIR "/pixmaps/gnome-gamazons.png");
116 pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
119 about = gnome_about_new (_("Gamazons"), VERSION,
120 "(C) 2002 Ronald Yorgason and Sean McMillen",
121 _("Send comments and bug reports to: "
122 "yorgasor@cs.pdx.edu"),
123 (const char **)authors,
124 (const char **)documenters,
125 strcmp (translator_credits, "translator_credits") != 0 ? translator_credits : NULL,
126 pixbuf);
127 g_signal_connect (G_OBJECT (about), "destroy", G_CALLBACK
128 (gtk_widget_destroyed), &about);
129 gtk_widget_show (about);
133 void
134 on_network1_activate (GtkMenuItem *menuitem,
135 gpointer user_data)
141 void
142 on_player1_activate (GtkMenuItem *menuitem,
143 gpointer user_data)
145 GtkWidget *time, *width, *depth;
146 GtkWidget *white_ai, *white_h, *black_ai, *black_h;
149 if (bstate_get_open_dialog())
150 return;
152 bstate_set_open_dialog(TRUE);
153 PlayerSettingsWindow = create_PlayerSettings();
155 /* grab all the widgets */
156 time = lookup_widget(PlayerSettingsWindow, "TimeSpinner");
157 width = lookup_widget(PlayerSettingsWindow, "WidthSpinner");
158 depth = lookup_widget(PlayerSettingsWindow, "DepthSpinner");
159 white_ai = lookup_widget(PlayerSettingsWindow, "WhiteAIRadio");
160 white_h = lookup_widget(PlayerSettingsWindow, "WhiteHumanRadio");
161 black_ai = lookup_widget(PlayerSettingsWindow, "BlackAIRadio");
162 black_h = lookup_widget(PlayerSettingsWindow, "BlackHumanRadio");
167 /* Load current values */
168 load_values_from_file();
169 gtk_spin_button_set_value( (GtkSpinButton *)time, options.engine.timeout);
170 gtk_spin_button_set_value( (GtkSpinButton *)width, options.engine.maxwidth);
171 gtk_spin_button_set_value( (GtkSpinButton *)depth, options.engine.maxdepth);
173 if (options.white_player == HUMAN)
174 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(white_h), TRUE);
175 else
176 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(white_ai), TRUE);
178 if (options.black_player == HUMAN)
179 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(black_h), TRUE);
180 else
181 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(black_ai), TRUE);
184 gtk_widget_show(PlayerSettingsWindow);
189 void
190 on_how_to_play1_activate (GtkMenuItem *menuitem,
191 gpointer user_data)
193 GError *error = NULL;
194 if (gnome_help_display("gamazons",NULL,&error))
196 #ifdef DEBUG
197 printf("I should pop up the help display now\n");
198 #endif
200 else
202 #ifdef DEBUG
203 printf("I couldn't display the help screen, sorry.\n");
204 #endif
205 printf("%s\n", error->message );
210 void
211 on_help2_activate (GtkMenuItem *menuitem,
212 gpointer user_data)
217 void
218 on_BT_UNDO_clicked (GtkButton *button,
219 gpointer user_data)
221 int temp;
222 GtkWidget *undo_button;
223 int i;
225 if (bstate_get_moving_piece())
226 return;
227 if (bstate_get_open_dialog())
228 return;
230 if (bstate_get_what_next() == FIRE_ARROW) //Undo during mid-move
232 int to_col, from_col, to_row, from_row;
233 Square to, from;
235 from = bstate_get_move_to();
236 to = bstate_get_move_from();
238 bstate_set_move_to(to);
239 bstate_set_move_from(from);
241 move_piece_to (to, board->selected_queen);
243 //roll back the state of the board
244 from_col = get_x_int_from_square(bstate_get_move_from());
245 from_row = get_y_int_from_square(bstate_get_move_from());
247 to_col = get_x_int_from_square(bstate_get_move_to());
248 to_row = get_y_int_from_square(bstate_get_move_to());
250 board->squares[to_row][to_col] = board->squares[from_row][from_col];
251 board->squares[from_row][from_col] = NOTHING;
252 gen_legal_moves(bstate_get_move_to());
254 if (board->squares[to_row][to_col] == WHITE)
256 bstate_set_just_finished(UNDO);
257 for (i=0; i<4; i++) //update queen to square mapping
259 if (board->square_to_wh_queen_map[i] == to_row*10 + to_col)
261 board->square_to_wh_queen_map[i] = from_row*10 + from_col;
262 break;
266 else
268 bstate_set_just_finished(UNDO);
269 for (i=0; i<4; i++) //update queen to square mapping
271 if (board->square_to_bl_queen_map[i] == to_row*10 + to_col)
273 board->square_to_bl_queen_map[i] = from_row*10 + from_col;
274 break;
285 /*==============================================================================
286 * on_BT_FORCEMOVE_clicked
288 * This sets the global timeout variable, forcing the AI to take the best move
289 * currently available.
291 void
292 on_BT_FORCEMOVE_clicked (GtkButton *button,
293 gpointer user_data)
295 if (bstate_get_moving_piece())
296 return;
297 if (bstate_get_open_dialog())
298 return;
299 bstate_set_just_finished(FORCE_MOVE);
300 ok = 0;
303 /*==============================================================================
304 * on_BT_AUTOFINISH_clicked
306 * This starts the auto-finish process if allowed. This can't be done inbetween
307 * a human player moving & firing an arrow, or while an AI is moving.
310 void
311 on_BT_AUTOFINISH_clicked (GtkButton *button,
312 gpointer user_data)
314 if (bstate_get_moving_piece())
315 return;
316 if (bstate_get_open_dialog())
317 return;
318 options.white_player = AI;
319 options.black_player = AI;
320 options.engine.timeout = 1;
321 options.engine.maxdepth = 5;
322 options.engine.maxwidth = 15;
323 bstate_set_just_finished(AUTO_FINISH);
324 while (bstate_get_what_next() == WAIT_FOR_AI)
325 move_ai(); //if both players are AI, keeps on running
330 void
331 on_GamazonsMain_destroy (GtkObject *object,
332 gpointer user_data)
335 state_hash = 0;
336 bstate_set_just_finished(QUIT_GAME);
337 gtk_main_quit();
342 /*==============================================================================
343 * board_press_cb
345 * This is the main board handling routine. Determines what the user is clicking
346 * on and what a click means.
348 int board_press_cb (GnomeCanvasItem *item, GdkEvent *event, gpointer data)
350 double drop_x, drop_y;
351 double new_x, new_y;
352 int from_row, from_col, to_row, to_col;
353 // int from;
354 int i,j,k;
355 GtkWidget *auto_button, *undo_button;
356 static Square to;
357 static Square from;
359 if (bstate_get_moving_piece())
361 //printf("Can't do that while a piece is moving\n");
362 return;
364 if (bstate_get_open_dialog())
366 //printf("Can't do that while another window is open\n");
367 return;
369 if (bstate_get_what_next() == NEW_GAME)
371 //printf("Can't do that while I'm expecting a new game to start\n");
372 return;
374 if (bstate_get_replay_mode())
375 return 0;
377 switch (event->type) {
378 case GDK_BUTTON_PRESS:
379 if (event->button.button != 1)
380 break;
382 #ifdef DEBUG
383 printf("what_next = %d\n", bstate_get_what_next());
384 #endif
385 //Set where a piece is coming from
386 board->orig_x = board->curr_x = event->button.x;
387 board->orig_y = board->curr_y = event->button.y;
388 from = get_square (board->curr_x, board->curr_y);
389 from_col = get_x_int_from_square(from);
390 from_row = get_y_int_from_square(from);
392 #ifdef DEBUG
393 print_board();
394 #endif
396 //Make sure it's time to move a queen before letting the user do it,
397 //as well as make sure he's moving his own queen. Also make sure it's
398 //a human's turn!
399 #ifdef DEBUG
400 printf("checking row %d, col %d\n", from_row, from_col);
401 square_contains(from);
402 printf("board->squares = %d\n",board->squares[from_row][from_col]);
403 #endif
404 if ((board->squares[from_row][from_col] == WHITE &&
405 bstate_get_what_next() == MOVE_WHITE_QUEEN &&
406 options.white_player == HUMAN) ||
407 (board->squares[from_row][from_col] == BLACK &&
408 bstate_get_what_next() == MOVE_BLACK_QUEEN &&
409 options.black_player == HUMAN))
411 //Human is moving a queen
412 #ifdef DEBUG
413 printf("board->squares = %d, what_next = %d\n", board->squares[from_row][from_col], bstate_get_what_next());
414 #endif
415 grabbed_queen = TRUE;
416 gen_legal_moves(from);
417 #ifdef DEBUG
418 printf("grabbing queen\n");
419 #endif
420 gnome_canvas_item_raise_to_top (item);
421 gnome_canvas_item_grab (item,
422 GDK_POINTER_MOTION_MASK |
423 GDK_BUTTON_RELEASE_MASK,
424 NULL,
425 event->button.time);
427 board->selected_queen = item;
429 from = get_square (board->curr_x, board->curr_y);
430 to = from;
431 #ifdef DEBUG
432 printf("GDK_BUTTON_PRESS: to = %d from = %d\n", to, from);
433 #endif
434 break;
436 case GDK_MOTION_NOTIFY:
437 if (event && (event->motion.state & GDK_BUTTON1_MASK))
439 if (!grabbed_queen)
440 break;
441 new_x = event->motion.x;
442 new_y = event->motion.y;
443 gnome_canvas_item_raise_to_top (item);
444 gnome_canvas_item_move (item,
445 new_x - board->curr_x,
446 new_y - board->curr_y);
449 board->curr_x = new_x;
450 board->curr_y = new_y;
452 break;
454 case GDK_BUTTON_RELEASE:
455 if (event->button.button != 1)
456 break;
457 #ifdef DEBUG
458 printf("what_next = %d\n", bstate_get_what_next());
459 #endif
460 if (grabbed_queen == FALSE)
461 break;
462 else
463 grabbed_queen = FALSE;
465 if (item != NULL)
466 gnome_canvas_item_ungrab (item, event->button.time);
469 #ifdef DEBUG
470 printf("GDK_BUTTON_RELEASE: to = %d from = %d\n", to, from);
471 #endif
472 drop_x = event->button.x;
473 drop_y = event->button.y;
475 //something funny here?
476 if (is_queen_square(get_square (drop_x, drop_y)))
478 move_piece_to (to, item);
479 break;
482 #ifdef DEBUG
483 printf("Queen is dropped at coords %f, %f\n", drop_x, drop_y);
484 #endif
485 to = get_square (drop_x, drop_y);
486 #ifdef DEBUG
487 printf("new square is %d\n", to);
488 #endif
490 /* Register the queen move with the board */
491 from_col = get_x_int_from_square(from);
492 from_row = get_y_int_from_square(from);
494 to_col = get_x_int_from_square(to);
495 to_row = get_y_int_from_square(to);
496 #ifdef DEBUG
497 printf("setting square %d%d to hold %d\n", to_row, to_col, board->squares[from_row][from_col]);
498 #endif
500 //Queen is being moved to a different square
501 if (to != from)
503 //put the piece back if it's not a legal move
504 if (!is_move_legal(to))
506 //bstate_set_move_to(from);
507 move_piece_to(from, item);
508 break;
510 else
512 bstate_set_move_to(to);
513 bstate_set_move_from(from);
514 bstate_set_just_finished(MOVE_WHITE_QUEEN);
516 board->squares[to_row][to_col] = board->squares[from_row][from_col];
517 for (k=0; k<4; k++) //update square to queen mapping
519 if (board->square_to_wh_queen_map[k] == from_row*10 + from_col)
521 board->square_to_wh_queen_map[k] = to_row*10 + to_col;
522 break;
524 if (board->square_to_bl_queen_map[k] == from_row*10 + from_col)
526 board->square_to_bl_queen_map[k] = to_row*10 + to_col;
527 break;
530 board->squares[from_row][from_col] = NOTHING;
531 gen_legal_moves(to); //prepare moves for arrow
535 #ifdef DEBUG
536 square_contains(to);
537 square_contains(from);
538 #endif
541 move_piece_to(to, item);
542 #ifdef DEBUG
543 printf("what_next = %d\n", bstate_get_what_next());
544 #endif
545 break;
546 //default:
549 return 1;
552 /*==============================================================================
553 * arrow_fire_cb
555 * Determines the square the arrow is fired at and marks it as such.
556 * Also checks that this is the appropriate time to fire an arrow and that the
557 * given square is in the list of legal moves.
559 int arrow_fire_cb(GnomeCanvasItem *item, GdkEvent *event, gpointer data)
561 double x_coord, y_coord;
562 int sq;
563 int row, col;
564 state *s;
565 //GtkWidget *auto_button, *undo_button;
567 if (bstate_get_moving_piece())
568 return 0;
569 if (bstate_get_open_dialog())
570 return 0;
571 if (bstate_get_what_next() == NEW_GAME)
572 return 0;
573 if (bstate_get_replay_mode())
574 return 0;
576 count_queens();
577 switch (event->type)
579 case GDK_BUTTON_PRESS:
580 if (event->button.button != 1)
581 break;
582 if (bstate_get_what_next() != FIRE_ARROW)
583 break;
585 x_coord = event->button.x-4;
586 y_coord = event->button.y-4;
588 sq = get_square(x_coord, y_coord);
589 if (!is_move_legal(sq))
590 break;
592 /* Make sure the square is empty first */
593 col = get_x_int_from_square(sq);
594 row = get_y_int_from_square(sq);
596 #ifdef DEBUG
597 printf("what_next = %d\n", bstate_get_what_next());
598 printf("arrow fire: ");
599 square_contains(sq);
600 #endif
601 if (board->squares[row][col] != NOTHING)
602 break;
604 fire_arrow(sq);
605 s = states.s[states.current_state];
606 register_move_with_engine(sq);
608 bstate_set_just_finished(FIRE_ARROW);
611 //check for gameover
612 if (game_over())
613 break;
615 dup_state(s, states.s[++(states.current_state)]);
616 if (bstate_get_what_next() == WAIT_FOR_AI)
617 move_ai();
618 //default:
621 return 0;
625 /*==============================================================================
626 * on_PlayerOKButton_clicked
628 * This processes the information from the Player settings window. If a change
629 * is made, sometimes the state of the game must reflect this change. For
630 * instance, if it was a humans turn, and this player was set to AI, the game
631 * state must stop waiting for the human to move and must instead instruct the
632 * AI to start thinking.
634 void
635 on_PlayerOKButton_clicked (GtkButton *button,
636 gpointer user_data)
638 GtkWidget *time, *width, *depth;
639 GtkWidget *white_ai, *white_h, *black_ai, *black_h;
641 load_values_from_file();
643 /* grab all the widgets */
644 time = lookup_widget(PlayerSettingsWindow, "TimeSpinner");
645 width = lookup_widget(PlayerSettingsWindow, "WidthSpinner");
646 depth = lookup_widget(PlayerSettingsWindow, "DepthSpinner");
647 white_ai = lookup_widget(PlayerSettingsWindow, "WhiteAIRadio");
648 white_h = lookup_widget(PlayerSettingsWindow, "WhiteHumanRadio");
649 black_ai = lookup_widget(PlayerSettingsWindow, "BlackAIRadio");
650 black_h = lookup_widget(PlayerSettingsWindow, "BlackHumanRadio");
653 /* Store new values */
654 options.engine.timeout = gtk_spin_button_get_value_as_int( (GtkSpinButton *)time);
655 options.engine.maxwidth = gtk_spin_button_get_value_as_int( (GtkSpinButton *)width);
656 options.engine.maxdepth = gtk_spin_button_get_value_as_int( (GtkSpinButton *)depth);
658 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(white_h)))
659 options.white_player = HUMAN;
660 else
661 options.white_player = AI;
663 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(black_h)))
664 options.black_player = HUMAN;
665 else
666 options.black_player = AI;
668 store_values_in_file();
669 gtk_widget_destroy(PlayerSettingsWindow);
671 bstate_update_player_settings();
672 bstate_set_open_dialog(FALSE);
673 //checks to see if the AI is supposed to move after changes to the player have been made
674 //if the ai is already moving, don't muck things up by calling it again.
675 if (!bstate_get_moving_ai())
677 while (bstate_get_what_next() == WAIT_FOR_AI)
679 move_ai();
686 void
687 on_PlayerCancelButton_clicked (GtkButton *button,
688 gpointer user_data)
690 gtk_widget_destroy(PlayerSettingsWindow);
691 bstate_set_open_dialog(FALSE);
695 void
696 on_theme1_activate (GtkMenuItem *menuitem,
697 gpointer user_data)
700 if (bstate_get_open_dialog())
701 return;
702 bstate_set_open_dialog(TRUE);
703 file_selector = gtk_file_selection_new ("Pick your theme file.");
705 gtk_file_selection_set_filename (GTK_FILE_SELECTION (file_selector), PACKAGE_DATA_DIR "/gamazons/");
706 gtk_file_selection_complete (GTK_FILE_SELECTION (file_selector), "*.theme");
707 g_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->ok_button),
708 "clicked",
709 G_CALLBACK (load_new_theme),
710 NULL);
711 g_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->cancel_button),
712 "clicked",
713 G_CALLBACK (on_ThemeCancelButton_clicked),
714 NULL);
716 /* Ensure that the dialog box is destroyed when the user clicks a button. */
718 g_signal_connect_swapped (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->ok_button),
719 "clicked",
720 G_CALLBACK (gtk_widget_destroy),
721 (gpointer) file_selector);
723 g_signal_connect_swapped (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->cancel_button),
724 "clicked",
725 G_CALLBACK (gtk_widget_destroy),
726 (gpointer) file_selector);
728 /* Display that dialog */
730 gtk_widget_show (file_selector);
735 void load_new_theme (GtkFileSelection *selector, gpointer user_data)
737 if (bstate_get_moving_piece())
739 gnome_warning_dialog("Warning: Theme can't be updated while a game piece is moving. Please try again.");
740 bstate_set_open_dialog(FALSE);
741 return;
744 selected_filename = (gchar *) gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_selector));
745 load_values_from_file(); //Get current settings
746 if (!load_images_from_theme (selected_filename)) //load new theme settings
748 gnome_error_dialog("Theme file doesn't exist!\n");
749 bstate_set_open_dialog(FALSE);
750 return;
752 store_values_in_file(); //store new settings
753 destroy_board();
754 draw_board();
755 bstate_set_open_dialog(FALSE);
759 void on_ThemeCancelButton_clicked(GtkButton *button, gpointer user_data)
761 bstate_set_open_dialog(FALSE);
765 void
766 on_save_as1_activate (GtkMenuItem *menuitem,
767 gpointer user_data)
769 //char home[256];
770 char *home_env;
772 if (bstate_get_open_dialog())
773 return;
774 bstate_set_open_dialog(TRUE);
776 file_selector = gtk_file_selection_new ("Save your move history to a file");
778 gtk_file_selection_set_filename (GTK_FILE_SELECTION (file_selector), options.hist_dir);
779 g_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->ok_button),
780 "clicked",
781 G_CALLBACK (save_game_history),
782 NULL);
783 g_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->cancel_button),
784 "clicked",
785 G_CALLBACK (on_save_as1CancelButton_clicked),
786 NULL);
788 /* Ensure that the dialog box is destroyed when the user clicks a button. */
790 g_signal_connect_swapped (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->ok_button),
791 "clicked",
792 G_CALLBACK (gtk_widget_destroy),
793 (gpointer) file_selector);
795 g_signal_connect_swapped (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->cancel_button),
796 "clicked",
797 G_CALLBACK (gtk_widget_destroy),
798 (gpointer) file_selector);
800 /* Display that dialog */
802 gtk_widget_show (file_selector);
807 void save_game_history(GtkFileSelection *selector, gpointer user_data)
809 FILE *history_fd;
810 GtkTextView *view;
811 GtkTextBuffer *buffer;
812 GtkTextIter *iter_start = (GtkTextIter *) malloc(sizeof(GtkTextIter));
813 GtkTextIter *iter_end = (GtkTextIter *) malloc(sizeof(GtkTextIter));
814 gchar *text;
815 char *temp_dir;
817 selected_filename = (gchar *)gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_selector));
818 if (strcmp(options.hist_dir, selected_filename) == 0)
820 bstate_set_open_dialog(FALSE);
821 return;
824 load_values_from_file(); //Get current settings
825 strcpy(options.hist_dir, selected_filename);
826 temp_dir = dirname(options.hist_dir);
827 strcpy(options.hist_dir, temp_dir);
828 options.hist_dir[strlen(options.hist_dir)+1] = '\0';
829 options.hist_dir[strlen(options.hist_dir)] = '/';
830 store_values_in_file(); //save new dir
832 view = (GtkTextView *) lookup_widget(main_window, "textview1");
833 buffer = gtk_text_view_get_buffer (view);
834 gtk_text_buffer_get_start_iter(buffer, iter_start);
835 gtk_text_buffer_get_end_iter(buffer, iter_end);
837 text = gtk_text_buffer_get_text(buffer, iter_start, iter_end, FALSE);
838 history_fd = fopen(selected_filename, "w");
840 if (history_fd == NULL)
842 gnome_error_dialog("Unable to save move history");
843 bstate_set_open_dialog(FALSE);
844 return;
847 fprintf(history_fd, "%s", text);
848 fclose(history_fd);
849 bstate_set_open_dialog(FALSE);
852 void on_save_as1CancelButton_clicked(GtkButton *button, gpointer user_data)
854 bstate_set_open_dialog(FALSE);
858 void
859 on_open_activate (GtkMenuItem *menuitem,
860 gpointer user_data)
862 //char home[256];
863 char *home_env;
865 if (bstate_get_open_dialog())
866 return;
867 bstate_set_open_dialog(TRUE);
869 file_selector = gtk_file_selection_new ("Load saved Gamazons history file you wish to continue");
871 gtk_file_selection_set_filename (GTK_FILE_SELECTION (file_selector), options.hist_dir);
872 g_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->ok_button),
873 "clicked",
874 G_CALLBACK (load_saved_game),
875 NULL);
876 g_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->cancel_button),
877 "clicked",
878 G_CALLBACK (on_OpenCancelButton_clicked),
879 NULL);
881 /* Ensure that the dialog box is destroyed when the user clicks a button. */
883 g_signal_connect_swapped (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->ok_button),
884 "clicked",
885 G_CALLBACK (gtk_widget_destroy),
886 (gpointer) file_selector);
888 g_signal_connect_swapped (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->cancel_button),
889 "clicked",
890 G_CALLBACK (gtk_widget_destroy),
891 (gpointer) file_selector);
893 /* Display that dialog */
895 gtk_widget_show (file_selector);
900 void load_saved_game(GtkFileSelection *selector, gpointer user_data)
903 FILE *history_fd;
904 char *temp_dir;
906 selected_filename = (gchar *)gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_selector));
907 if (strcmp(options.hist_dir, selected_filename) == 0)
909 bstate_set_open_dialog(FALSE);
910 return;
913 load_values_from_file(); //Get current settings
914 strcpy(options.hist_dir, selected_filename);
915 temp_dir = dirname(options.hist_dir);
916 strcpy(options.hist_dir, temp_dir);
917 options.hist_dir[strlen(options.hist_dir)+1] = '\0';
918 options.hist_dir[strlen(options.hist_dir)] = '/';
919 store_values_in_file();
921 history_fd = fopen(selected_filename, "ro");
922 if (history_fd == NULL)
924 gnome_error_dialog("Error opening history file\n");
925 bstate_set_open_dialog(FALSE);
926 return;
928 gtk_widget_destroy(file_selector);
929 bstate_set_open_dialog(FALSE);
930 bstate_set_replay_mode(FALSE);
932 free_all_memory();
933 init_engine();
934 destroy_board();
935 init_game_board(main_window);
937 if (read_in_moves(history_fd))
938 bstate_set_just_finished(LOAD_GAME);
939 else
940 gnome_error_dialog("Error: Corrupted history file\n");
943 //printf("finished loading history file\n");
944 fclose(history_fd);
946 if (bstate_get_what_next() == NEW_GAME) //game over
947 return;
948 while (bstate_get_what_next() == WAIT_FOR_AI)
949 move_ai(); //if both players are AI, keeps on running
953 void on_OpenCancelButton_clicked(GtkButton *button, gpointer user_data)
955 bstate_set_open_dialog(FALSE);
958 void
959 on_BT_REPLAY_clicked (GtkButton *button,
960 gpointer user_data)
962 //char home[256];
963 char *home_env;
965 if (bstate_get_open_dialog())
966 return;
967 bstate_set_open_dialog(TRUE);
969 file_selector = gtk_file_selection_new ("Load saved Gamazons history file you wish to replay");
971 gtk_file_selection_set_filename (GTK_FILE_SELECTION (file_selector), options.hist_dir);
972 g_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->ok_button),
973 "clicked",
974 G_CALLBACK (replay_saved_game),
975 NULL);
976 g_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->cancel_button),
977 "clicked",
978 G_CALLBACK (on_OpenCancelButton_clicked),
979 NULL);
981 /* Ensure that the dialog box is destroyed when the user clicks a button. */
983 g_signal_connect_swapped (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->ok_button),
984 "clicked",
985 G_CALLBACK (gtk_widget_destroy),
986 (gpointer) file_selector);
988 g_signal_connect_swapped (GTK_OBJECT (GTK_FILE_SELECTION (file_selector)->cancel_button),
989 "clicked",
990 G_CALLBACK (gtk_widget_destroy),
991 (gpointer) file_selector);
993 /* Display that dialog */
995 gtk_widget_show (file_selector);
1000 void replay_saved_game(GtkFileSelection *selector, gpointer user_data)
1003 FILE *history_fd;
1004 char *temp_dir;
1005 int game_life = bstate_get_game_life();
1007 selected_filename = (gchar *)gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_selector));
1008 if (strcmp(options.hist_dir, selected_filename) == 0)
1010 bstate_set_open_dialog(FALSE);
1011 return;
1014 load_values_from_file(); //Get current settings
1015 strcpy(options.hist_dir, selected_filename);
1016 temp_dir = dirname(options.hist_dir);
1017 strcpy(options.hist_dir, temp_dir);
1018 options.hist_dir[strlen(options.hist_dir)+1] = '\0';
1019 options.hist_dir[strlen(options.hist_dir)] = '/';
1020 store_values_in_file();
1022 history_fd = fopen(selected_filename, "ro");
1023 if (history_fd == NULL)
1025 gnome_error_dialog("Error opening history file\n");
1026 bstate_set_open_dialog(FALSE);
1027 return;
1029 gtk_widget_destroy(file_selector);
1030 bstate_set_open_dialog(FALSE);
1031 bstate_set_just_finished(START_REPLAY);
1033 free_all_memory();
1034 init_engine();
1035 destroy_board();
1036 init_game_board(main_window);
1038 //bstate_set_replay_mode(TRUE);
1039 if (read_in_moves(history_fd))
1042 else if (bstate_get_replay_mode()) //if still in replay mode, there must have been an error. Otherwise
1043 { //the stop button was pressed and all is well.
1044 gnome_error_dialog("Error: Corrupted history file\n");
1046 else if (game_life != bstate_get_game_life())
1048 return;
1051 bstate_set_just_finished(STOP_REPLAY); //cleans up replay mode if successfully finished
1052 //bstate_set_replay_mode(FALSE);
1055 //printf("finished loading history file\n");
1056 fclose(history_fd);
1058 if (bstate_get_what_next() == NEW_GAME) //game over
1059 return;
1060 while (bstate_get_what_next() == WAIT_FOR_AI)
1061 move_ai(); //if both players are AI, keeps on running
1066 void
1067 on_BT_REPLAY_STOP_clicked (GtkButton *button,
1068 gpointer user_data)
1070 //bstate_set_replay_mode(FALSE);
1071 bstate_set_just_finished(STOP_REPLAY);