Implemented opening a puzzle
[getaclue.git] / player_mainwindow.py
index b384b54..7aa3af7 100644 (file)
@@ -17,11 +17,20 @@ class MainWindow:
        ACROSS = 1
        DOWN = 2
 
-       def gtk_main_quit (self, *args):
-               gtk.main_quit ()
+       # callback for menu item open activated event
+       def on_open_activate (self, menuitem):
+               dlg = gtk.FileChooserDialog ("Open a GetAClue puzzle", self.window,
+                               gtk.FILE_CHOOSER_ACTION_OPEN,
+                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
 
-       # callback for menu item quit activated event
-       def on_quit_activate (self, menuitem):
+               if dlg.run () == gtk.RESPONSE_OK:
+                       puzzlefile = dlg.get_filename ()
+                       self.open_file (puzzlefile)
+
+               dlg.destroy ()
+
+       # quit verification
+       def verify_quit (self):
                if self.puzzle:
                        dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL,
                                        gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO,
@@ -30,8 +39,65 @@ class MainWindow:
                                dlg.destroy ()
                                return False
                        dlg.destroy ()
+               return True
+
+
+       # callback for main window destroy
+       def on_mainwindow_destroy (self, args):
+               gtk.main_quit ()
+
+
+       # callback for window closing dialog
+       def on_mainwindow_delete_event (self, window, event):
+               # verify whether really to quit or not if a puzzle is open
+               v = self.verify_quit ()
+               # return False for deleting and True for not deleting
+               return not v
+
+       # callback for menu item quit activated event
+       def on_quit_activate (self, menuitem):
+               # verify whether really to quit or not if a puzzle is open
+               v = self.verify_quit ()
+               # if verified, then quit
+               if v is True:
+                       self.window.destroy ()
+
+       # callback for menu item clear grid activated event
+       def on_cleargrid_activate (self, menuitem):
+               if self.puzzle:
+                       dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL,
+                                       gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO,
+                                       "Are you sure you wish to clear your entries?")
+                       if dlg.run () == gtk.RESPONSE_YES:
+                               # clear the guesses
+                               self.puzzle.clear_guesses ()
+                               # redraw the grid
+                               puzgrid = self.ui.get_object ("puzzlegrid")
+                               puzgrid.queue_draw ()
+                       dlg.destroy()
 
-               self.gtk_main_quit ()
+       # callback for menu item verify board activated event
+       def on_verify_activate (self, menuitem):
+               if self.puzzle:
+                       try:
+                               ans = self.puzzle.is_solution_correct ()
+                               # if the solution is correct
+                               if ans:
+                                       dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL,
+                                                       gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
+                                                       "Success! Your entries are correct.")
+                                       dlg.run ()
+                               else:
+                                       dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL,
+                                                       gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
+                                                       "Your solution has some errors. Fix them and try again")
+                                       dlg.run ()
+                       except crosswordpuzzle.IncompleteSolutionException:
+                               dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL,
+                                               gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE,
+                                               "You've not completed the board yet. Cannot verify")
+                               dlg.run ()
+                       dlg.destroy ()
 
        # callback for menu item hide solution activated event
        def on_hidesolution_activate (self, menuitem):