import wx
import sys
import os
ID_STAT = 2
ID_TOOL = 3
class Styled(wx.stc.StyledTextCtrl):
def __init__(self, parent, id, pos, size, style, validator, name) :
wx.stc.StyledTextCtrl.__init__(self, parent, id, pos, size, style, validator, name)
class Editor(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(550, 580))
menubar = wx.MenuBar()
file = wx.Menu()
new = wx.MenuItem(file, 101, '&New\tCtrl+N', 'Creates a new document')
file.AppendItem(new)
open = wx.MenuItem(file, 102, '&Open\tCtrl+O', 'Open an existing file')
file.AppendItem(open)
save = wx.MenuItem(file, 103, '&Save\tCtrl+S', 'Save the file')
file.AppendItem(save)
file.AppendSeparator()
quit = wx.MenuItem(file, wx.ID_EXIT, '&Quit\tCtrl+Q', 'Exit program')
file.AppendItem(quit)
edit = wx.Menu()
cut = wx.MenuItem(edit, 106, '&Cut\tCtrl+K', 'Cut the Selection')
edit.AppendItem(cut)
copy = wx.MenuItem(edit, 107, '&Copy\tCtrl+C', 'Copy the Selection')
edit.AppendItem(copy)
select = wx.MenuItem(edit, 104, '&Select\tCtrl+E', 'Selact All')
edit.AppendItem(select)
paste = wx.MenuItem(edit, 108, '&Paste\tCtrl+V', 'Paste text from clipboard')
edit.AppendItem(paste)
delete = wx.MenuItem(edit, 109, '&Delete\tCtrl+X', 'Delete the selected text')
edit.AppendItem(delete)
view = wx.Menu()
self.shst = view.Append(ID_STAT, 'Show statusbar', 'Show Statusbar', kind=wx.ITEM_CHECK)
self.shtl = view.Append(ID_TOOL, 'Show toolbar', 'Show Toolbar', kind=wx.ITEM_CHECK)
view.Check(ID_STAT, True)
view.Check(ID_TOOL, True)
self.Bind(wx.EVT_MENU, self.NewApplication, id=101)
self.Bind(wx.EVT_MENU, self.OnOpenFile, id=102)
self.Bind(wx.EVT_MENU, self.OnSaveFile, id=103)
self.Bind(wx.EVT_MENU, self.OnCut, id=106)
self.Bind(wx.EVT_MENU, self.OnCopy, id=107)
self.Bind(wx.EVT_MENU, self.OnPaste, id=108)
self.Bind(wx.EVT_MENU, self.OnDelete, id=109)
self.Bind(wx.EVT_MENU, self.OnSelectAll, id=104)
self.Bind(wx.EVT_MENU, self.ToggleStatusBar, id=ID_STAT)
self.Bind(wx.EVT_MENU, self.ToggleToolBar, id=ID_TOOL)
menubar.Append(file, '&File')
menubar.Append(edit, '&Edit')
menubar.Append(view, '&View')
self.SetMenuBar(menubar)
self.count = 5
self.toolbar = self.CreateToolBar(wx.TB_HORIZONTAL)
self.toolbar.AddLabelTool(101, '', wx.Bitmap('Lamp/fil.png'))
self.toolbar.AddLabelTool(102, '', wx.Bitmap('Lamp/ab.png'))
self.toolbar.AddLabelTool(103, '', wx.Bitmap('Lamp/kep.png'))
self.toolbar.AddLabelTool(104, '', wx.Bitmap('Lamp/sel.png'))
self.toolbar.AddLabelTool(106, '', wx.Bitmap('Lamp/cut.png'))
self.toolbar.AddLabelTool(wx.ID_EXIT, '', wx.Bitmap('Lamp/sar.png'))
self.toolbar.EnableTool(wx.ID_REDO, False)
self.toolbar.Realize()
self.Bind(wx.EVT_TOOL, self.OnExit, id=wx.ID_EXIT)
self.control = wx.stc.StyledTextCtrl(OnInit)
self.dirname = ''
self.statusbar = self.CreateStatusBar()
self.Centre()
self.Show(True)
#aqui
def NewApplication(self, event):
editor = Editor(None, -1, 'Editor')
editor.Centre()
editor.Show()
def OnOpenFile(self,e):
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename=dlg.GetFilename()
self.dirname=dlg.GetDirectory()
filehandle=open(os.path.join(self.dirname, self.filename),'r')
self.control.SetValue(filehandle.read())
filehandle.close()
self.SetTitle("Editing ... "+self.filename)
dlg.Destroy()
def DoOpenFile(self):
wcd = 'All files (*)|*|Editor files (*.ef)|*.ef|'
dir = os.getcwd()
open_dlg = wx.FileDialog(self, message='Choose a file', defaultDir=dir, defaultFile='',
wildcard=wcd, style=wx.OPEN|wx.CHANGE_DIR)
if open_dlg.ShowModal() == wx.ID_OK:
path = open_dlg.GetPath()
try:
file = open(path, 'r')
text = file.read()
file.close()
if self.text.GetLastPosition():
self.text.Clear()
self.text.WriteText(text)
self.last_name_saved = path
self.statusbar.SetStatusText('', 1)
self.modify = False
except IOError, error:
dlg = wx.MessageDialog(self, 'Error opening file\n' + str(error))
dlg.ShowModal()
except UnicodeDecodeError, error:
dlg = wx.MessageDialog(self, 'Error opening file\n' + str(error))
dlg.ShowModal()
open_dlg.Destroy()
def OnSaveFile(self,e):
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", \
wx.SAVE | wx.OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
itcontains = self.control.GetValue()
self.filename=dlg.GetFilename()
self.dirname=dlg.GetDirectory()
filehandle=open(os.path.join(self.dirname, self.filename),'w')
filehandle.write(itcontains)
filehandle.close()
dlg.Destroy()
def OnCut(self, event):
self.control.Cut()
def OnCopy(self, event):
self.control.Copy()
def OnPaste(self, event):
self.control.Paste()
def OnDelete(self, event):
frm, to = self.control.GetSelection()
self.control.Remove(frm, to)
def OnSelectAll(self, event):
self.control.SelectAll()
def OnTextChanged(self, event):
self.modify = True
self.statusbar.SetStatusText(' modified', 1)
event.Skip()
def OnKeyDown(self, event):
keycode = event.GetKeyCode()
if keycode == wx.WXK_INSERT:
if not self.replace:
self.statusbar.SetStatusText('INS', 2)
self.replace = True
else:
self.statusbar.SetStatusText('', 2)
self.replace = False
event.Skip()
def OnAbout(self, event):
dlg = wx.MessageDialog(self, '\tEditor\t\n Another Tutorial\njan bodnar 2005-2006',
'About Editor', wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def OnExit(self, event):
dlg = wx.MessageDialog(self, 'Are you sure to quit editor?', 'Please Confirm', wx.YES_NO |
wx.NO_DEFAULT | wx.ICON_QUESTION)
if dlg.ShowModal() == wx.ID_YES:
self.Close(True)
def ToggleStatusBar(self, event):
if self.shst.IsChecked():
self.statusbar.Show()
else:
self.statusbar.Hide()
def ToggleToolBar(self, event):
if self.shtl.IsChecked():
self.toolbar.Show()
else:
self.toolbar.Hide()
app = wx.App()
Editor(None, 1, 'Editor')
app.MainLoop()