X-Git-Url: https://harishankar.org/repos/?p=getaclue.git;a=blobdiff_plain;f=player_mainwindow.py;h=8234893d0a6923de42a5e4cf55bb6ba377286cbf;hp=8ee60a763326e6ca85f3def9f8e75b3d150748fe;hb=8e96d1e907d35e47615ff1fe610bca7c88f392cd;hpb=50103f2c3cb70ee8fad1b13e1ab6864a57c36c29 diff --git a/player_mainwindow.py b/player_mainwindow.py index 8ee60a7..8234893 100644 --- a/player_mainwindow.py +++ b/player_mainwindow.py @@ -17,11 +17,8 @@ class MainWindow: ACROSS = 1 DOWN = 2 - def gtk_main_quit (self, *args): - gtk.main_quit () - - # callback for menu item quit activated event - def on_quit_activate (self, menuitem): + # 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 +27,52 @@ class MainWindow: dlg.destroy () return False dlg.destroy () + return True + + # 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)) + + if dlg.run () == gtk.RESPONSE_OK: + puzzlefile = dlg.get_filename () + self.open_file (puzzlefile) + + dlg.destroy () + + # callback for menu item save as activated event + def on_save_as_activate (self, menuitem): + if self.puzzle: + dlg = gtk.FileChooserDialog ("Save GetAClue puzzle as", self.window, + gtk.FILE_CHOOSER_ACTION_SAVE, + (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, + gtk.RESPONSE_OK)) + if dlg.run () == gtk.RESPONSE_OK: + puzzlefile = dlg.get_filename () + self.save_file (puzzlefile) + + dlg.destroy () - self.gtk_main_quit () + # 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): @@ -414,6 +455,18 @@ class MainWindow: down.append ([ str(self.puzzle.data[word[1]][word[2]].numbered), clue]) + def save_file (self, file): + # try to save the file + try: + cPickle.dump (self.puzzle, open (file, "wb"), cPickle.HIGHEST_PROTOCOL) + except (IOError, OSError, cPickle.PicklingError): + dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, + gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, + "Error in saving puzzle") + dlg.run () + dlg.destroy () + + # open a file def open_file (self, file): # try to open the file try: