-
Notifications
You must be signed in to change notification settings - Fork 0
/
BDialog.py
108 lines (90 loc) · 3.91 KB
/
BDialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import wx
import BFrame
import BIcons
import BSettings
class BDialog(BFrame.MyFrame):
def __init__(self, *args, **kwds):
self.MainIcon = BIcons.catalog['JP-chapeau'].GetBitmap()
self.ToolSave = BIcons.catalog['Save'].GetBitmap()
self.ToolOpen = BIcons.catalog['OpenFolder'].GetBitmap()
self.shift_down = False
super().__init__(*args, **kwds)
self.Bind(wx.EVT_CLOSE, self.OnClose, id=wx.ID_ANY)
self.historyIds = [wx.NewId() for x in range(9)]
self.Bind(wx.EVT_MENU, self.onFileId, None, self.historyIds[0], self.historyIds[-1])
self.history = wx.FileHistory(maxFiles=9, idBase=self.historyIds[0])
self.history.UseMenu(self.mnuFile)
def OnClose(self, event):
self.saveHistory()
self.Destroy()
def onFileId(self, event):
fileName = self.history.GetHistoryFile(event.Id - self.historyIds[0])
wx.GetApp().GetDataFromFile(fileName)
self.StatusBar.SetStatusText('File: {}'.format(fileName))
def onFileOpen(self, event): # wxGlade: MyFrame.<event_handler>
wildcard = "Text File (*.txt)|*.txt|" \
"All files (*.*)|*.*"
app = wx.GetApp()
fileName = app.m_fileName if hasattr(app, "m_fileName") else ""
dirName = app.m_dirName if hasattr(app, "m_dirName") else ""
dlg = wx.FileDialog(self, message="Open File",
defaultDir=dirName, defaultFile=fileName, wildcard=wildcard,
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
self.history.AddFileToHistory(dlg.GetPath())
app.m_fileName = dlg.GetFilename()
app.m_dirName = dlg.GetDirectory()
wx.GetApp().GetDataFromFile(dlg.GetPath())
self.StatusBar.SetStatusText('File: {}'.format(dlg.GetPath()))
dlg.Destroy()
def onFileSave(self, event): # wxGlade: MyFrame.<event_handler>
textCtrl = wx.Window.FindWindowById(self.ID_WIN)
if textCtrl.Value == '':
return
wildcard = "Text File (*.txt)|*.txt|" \
"All files (*.*)|*.*"
app = wx.GetApp()
fileName = app.m_fileName if hasattr(app, "m_fileName") else ""
dirName = app.m_dirName if hasattr(app, "m_dirName") else ""
dlg = wx.FileDialog(self, message="Save to File",
defaultDir=dirName, defaultFile=fileName, wildcard=wildcard,
style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
app.m_fileName = dlg.GetFilename()
app.m_dirName = dlg.GetDirectory()
textCtrl.SaveFile(dlg.GetPath())
dlg.Destroy()
def onFileExit(self, event): # wxGlade: MyFrame.<event_handler>
self.Close()
def saveHistory(self):
wx.GetApp().m_settings.history = []
files = self.history.GetCount()
for i in range(files):
wx.GetApp().m_settings.history.append(self.history.GetHistoryFile(i))
wx.GetApp().m_settings.Save()
def loadHistory(self):
files = wx.GetApp().m_settings.history
if files is None:
return
for f in files:
self.history.AddFileToHistory(f)
def onUserAction(self, event): # wxGlade: MyFrame.<event_handler>
print("Event handler 'onUserAction' ID={}".format(event.GetId()))
class MyApp(wx.App):
def OnInit(self):
self.m_settings = BSettings.Settings('BApp', 'BApp.json')
self.m_data = None
self.frame = BDialog(None, wx.ID_ANY, "")
self.frame.loadHistory()
self.SetTopWindow(self.frame)
self.frame.Show()
return True
def GetDataFromFile(self, filename):
with open(filename, 'rb') as fp:
self.m_data = fp.read()
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
v = app;