Skip to content

Commit

Permalink
save, discard, cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
chigkim committed May 31, 2023
1 parent 31fb616 commit da81433
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions AME.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,18 @@ def open(self):
self.SetTitle(self.filename+" | Markdown Editor")
self.nb.SetSelection(0)

def shouldSave(self):
dlg = wx.MessageDialog(None, "Recent change has not been saved.", 'Save?', wx.YES_NO | wx.CANCEL | wx.ICON_QUESTION)
dlg.SetYesNoLabels("Save", "Discard")
res = dlg.ShowModal()
if res == wx.ID_YES:
if self.onSave(None) == wx.ID_CANCEL:
return wx.ID_CANCEL
return res

def onOpen(self,e):
if self.edited:
if wx.MessageBox("Current content has not been saved! Proceed?", "Please confirm", wx.ICON_QUESTION | wx.YES_NO, self) == wx.NO: return
if self.shouldSave() == wx.ID_CANCEL: return
with wx.FileDialog(self, "Open", self.dirname, "", "*.*", style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as dlg:
if dlg.ShowModal() == wx.ID_CANCEL: return
self.filename = dlg.GetFilename()
Expand All @@ -93,7 +102,7 @@ def onOpen(self,e):

def onNew(self, e):
if self.edited:
if wx.MessageBox("Current content has not been saved! Proceed?", "Please confirm", wx.ICON_QUESTION | wx.YES_NO, self) == wx.NO: return
if self.shouldSave() == wx.ID_CANCEL: return
self.WebPanel.browser.SetPage("", "")
self.mdPanel.control.SetValue("")
self.edited = False
Expand All @@ -108,12 +117,12 @@ def save(self):
self.edited = False

def onSave(self, e):
if self.filename == "": self.onSaveAs(e)
if self.filename == "": return self.onSaveAs(e)
else: self.save()

def onSaveAs(self, e):
with wx.FileDialog(self, "Save", self.dirname, "", "*.md", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as dlg:
if dlg.ShowModal() == wx.ID_CANCEL: return
if dlg.ShowModal() == wx.ID_CANCEL: return wx.ID_CANCEL
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
self.save()
Expand All @@ -139,12 +148,12 @@ def onClipboard(self, e):

def OnExit(self,e):
if self.edited:
if wx.MessageBox("Current content has not been saved! Proceed?", "Please confirm", wx.ICON_QUESTION | wx.YES_NO, self) == wx.NO: return
if self.shouldSave() == wx.ID_CANCEL: return
self.Close(True)

def onClose(self, event):
if event.CanVeto() and self.edited:
if wx.MessageBox("Current content has not been saved! Proceed?", "Please confirm", wx.ICON_QUESTION | wx.YES_NO) != wx.YES:
if self.shouldSave() == wx.ID_CANCEL:
event.Veto()
return
self.Destroy()
Expand Down

0 comments on commit da81433

Please sign in to comment.