-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdmain.py
244 lines (220 loc) · 9.85 KB
/
hdmain.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import re
import sys
from hdsettings import SettingsDialog
from hdver import verstr, verstrs
from hdeditor import HideEditor
__author__ = 'hyst329'
import wx
import wx.lib.dialogs
import wx.stc
import wx.adv
import json
import subprocess
import os
class MainIDEFrame(wx.Frame):
def __init__(self, parent, title):
super(MainIDEFrame, self).__init__(parent, title=title, size=(800, 600))
# Loading the config
if getattr(sys, 'frozen', False):
path = os.path.dirname(sys.executable)
else:
path = os.path.dirname(os.path.realpath(__file__))
if os.path.isfile(path + "/config.json"):
cfg = open("config.json", "r")
self.config = json.load(cfg)
else:
wx.MessageBox("No config file found. Creating new - please check your settings",
"Warning", wx.ICON_WARNING)
self.config = {"helpath": "C:/f4main/f4main.exe"}
cfg = open("config.json", "w")
json.dump(self.config, cfg)
self.helpath = self.config["helpath"]
menubar = wx.MenuBar()
fileMenu = wx.Menu()
editMenu = wx.Menu()
projectMenu = wx.Menu()
optionsMenu = wx.Menu()
runMenu = wx.Menu()
helpMenu = wx.Menu()
newMenuItem = fileMenu.Append(wx.ID_ANY, "New\tCtrl+N", "Create a new .f4 file")
openMenuItem = fileMenu.Append(wx.ID_ANY, "Open...\tCtrl+O", "Open existing .f4 file")
saveMenuItem = fileMenu.Append(wx.ID_ANY, "Save\tCtrl+S", "Save the file")
saveAsMenuItem = fileMenu.Append(wx.ID_ANY, "Save As...\tCtrl+Shift+S", "Save the file")
quitMenuItem = fileMenu.Append(wx.ID_ANY, "Quit\tAlt+F4", "Quit application")
cutMenuItem = editMenu.Append(wx.ID_ANY, "Cut\tCtrl+X", "Move the selection to clipboard")
copyMenuItem = editMenu.Append(wx.ID_ANY, "Copy\tCtrl+C", "Copy the selection to clipboard")
pasteMenuItem = editMenu.Append(wx.ID_ANY, "Paste\tCtrl+V", "Copy clipboard contents to the selection")
newProjectMenuItem = projectMenu.Append(wx.ID_ANY, "New Project", "Create new project")
openProjectMenuItem = projectMenu.Append(wx.ID_ANY, "Open Project...", "Open an existing project")
saveProjectMenuItem = projectMenu.Append(wx.ID_ANY, "Save Project", "Save the project")
saveProjectAsMenuItem = projectMenu.Append(wx.ID_ANY, "Save Project As...", "Save the project as...")
closeProjectMenuItem = projectMenu.Append(wx.ID_ANY, "Close Project", "Close the project")
preferencesMenuItem = optionsMenu.Append(wx.ID_ANY, "Preferences...\tCtrl+P", "hide preferences")
runMenuItem = runMenu.Append(wx.ID_ANY, "Run\tF9", "Runs the project")
generateMenuItem = runMenu.Append(wx.ID_ANY, "Generate C file\tF10", "Generate C code from project")
genAndCompMenuItem = runMenu.Append(wx.ID_ANY, "Generate and compile\tF11",
"Generate and try to compile it with GCC")
aboutMenuItem = helpMenu.Append(wx.ID_ANY, "About\tCtrl+F1", "Display info about hide")
menubar.Append(fileMenu, "&File")
menubar.Append(editMenu, "&Edit")
menubar.Append(projectMenu, "&Project")
menubar.Append(optionsMenu, "&Options")
menubar.Append(runMenu, "&Run")
menubar.Append(helpMenu, "&Help")
for mi in projectMenu.GetMenuItems():
mi.Enable(False)
self.SetMenuBar(menubar)
font = wx.Font(10, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_NORMAL, False, 'Fixedsys',
wx.FONTENCODING_CP1252)
self.editor = HideEditor(self, font=font)
self.output = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.TE_READONLY)
self.output.SetFont(font)
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.sizer.Add(self.editor, 5, wx.EXPAND)
self.sizer.Add(self.output, 3, wx.EXPAND)
self.SetSizer(self.sizer)
self.sb = self.CreateStatusBar()
self.Bind(wx.EVT_MENU, self.OnNew, newMenuItem)
self.Bind(wx.EVT_MENU, self.OnOpen, openMenuItem)
self.Bind(wx.EVT_MENU, self.OnSave, saveMenuItem)
self.Bind(wx.EVT_MENU, self.OnSaveAs, saveAsMenuItem)
self.Bind(wx.EVT_MENU, self.OnQuit, quitMenuItem)
self.Bind(wx.EVT_MENU, self.OnAbout, aboutMenuItem)
self.Bind(wx.EVT_MENU, self.OnPreferences, preferencesMenuItem)
self.Bind(wx.EVT_MENU, self.OnRun, runMenuItem)
self.Bind(wx.EVT_MENU, self.OnGenerate, generateMenuItem)
self.Bind(wx.EVT_MENU, self.OnGenAndComp, genAndCompMenuItem)
self.timer = wx.Timer(self, wx.ID_ANY)
self.Bind(wx.EVT_TIMER, self.OnTimer)
self.timer.Start(1000)
self.Layout()
self.Centre()
self.Show()
def OnNew(self, e):
if self.editor.modified:
dlg = wx.MessageDialog(self, "File is modified after last save. Should hide save it?",
"Unsaved file", wx.ICON_QUESTION, wx.YES_NO)
if dlg.ShowModal() == wx.YES:
self.OnSave(e)
self.editor.ClearAll()
self.editor.filename = None
self.editor.modified = False
def OnOpen(self, e):
if self.editor.modified:
dlg = wx.MessageDialog(self, "File is modified after last save. Should hide save it?",
"Unsaved file", wx.ICON_QUESTION, wx.YES_NO)
if dlg.ShowModal() == wx.YES:
self.OnSave(e)
dlg = wx.FileDialog(self, message="Open", wildcard="F4/Helen sources (*.f4)|*.f4",
style=wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_CANCEL:
return
fname = dlg.GetPath()
self.editor.filename = fname
f = open(fname, 'r')
self.editor.SetText(f.read())
f.close()
dlg.Destroy()
self.editor.modified = False
pass
def OnSave(self, e):
if not self.editor.filename:
self.OnSaveAs(e)
else:
f = open(self.editor.filename, 'w')
text = self.editor.GetText()
text = re.sub(r'\r[^\n]', '\r\n', text)
f.write(text)
f.close()
self.editor.modified = False
def OnSaveAs(self, e):
if self.editor.modified:
dlg = wx.MessageDialog(self, "File is modified after last save. Should hide save it?",
"Unsaved file", wx.ICON_QUESTION, wx.YES_NO)
if dlg.ShowModal() == wx.YES:
self.OnSave(e)
dlg = wx.FileDialog(self, message="Save As", wildcard="F4/Helen sources (*.f4)|*.f4",
style=wx.FD_SAVE)
if dlg.ShowModal() == wx.ID_CANCEL:
return
fname = dlg.GetPath()
self.editor.filename = fname
f = open(fname, 'w')
text = self.editor.GetText()
text = re.sub(r'\r[^\n]', '\r\n', text)
f.write(text)
f.close()
self.editor.modified = False
dlg.Destroy()
def OnQuit(self, e):
if self.editor.modified:
dlg = wx.MessageDialog(self, "File is modified after last save. Should hide save it?",
"Unsaved file", wx.ICON_QUESTION, wx.YES_NO)
if dlg.ShowModal() == wx.YES:
self.OnSave(e)
cfg = open("config.json", "w")
json.dump(self.config, cfg)
self.Close()
def OnAbout(self, e):
info = wx.adv.AboutDialogInfo()
info.SetName("hide")
info.SetVersion(verstrs)
info.SetDescription("hide is a F4/Helen IDE (source code editor).\nPowered by wxPython toolkit")
info.SetCopyright("(C) 2015 hyst329")
info.SetWebSite("http://github.com/hyst329/hide")
info.AddDeveloper("hyst329")
info.AddTranslator("hyst329")
wx.adv.AboutBox(info)
def OnTimer(self, e):
s = "hide - %s%s" % (self.editor.filename, "*" if self.editor.modified else "")
self.SetTitle(s)
def OnPreferences(self, e):
sdlg = SettingsDialog(self)
sdlg.LoadConfig(self.config)
sdlg.InitUI()
sdlg.ShowModal()
sdlg.Destroy()
def OnRun(self, e):
if not self.editor.filename:
wx.MessageBox("Please save the file first!", "File needs to be saved", wx.ICON_WARNING)
return
elif self.editor.modified:
self.OnSave(e)
cmd = "%s %s -i" % (self.helpath, self.editor.filename)
try:
out = subprocess.check_output(cmd)
self.output.Clear()
self.output.SetValue(out)
except subprocess.CalledProcessError as e:
wx.MessageBox(str(e), "Error!", wx.ICON_ERROR)
def OnGenerate(self, e):
if not self.editor.filename:
wx.MessageBox("Please save the file first!", "File needs to be saved", wx.ICON_WARNING)
return
elif self.editor.modified:
self.OnSave(e)
cmd = "%s %s -g" % (self.helpath, self.editor.filename)
try:
out = subprocess.check_output(cmd)
self.output.Clear()
self.output.SetValue(out)
except subprocess.CalledProcessError as e:
wx.MessageBox(str(e), "Error!", wx.ICON_ERROR)
def OnGenAndComp(self, e):
if not self.editor.filename:
wx.MessageBox("Please save the file first!", "File needs to be saved", wx.ICON_WARNING)
return
elif self.editor.modified:
self.OnSave(e)
cmd = "%s %s -gc" % (self.helpath, self.editor.filename)
try:
out = subprocess.check_output(cmd)
self.output.Clear()
self.output.SetValue(out)
except subprocess.CalledProcessError as e:
wx.MessageBox(str(e), "Error!", wx.ICON_ERROR)
if __name__ == '__main__':
app = wx.App()
MainIDEFrame(None, title='hide')
app.MainLoop()