2 #----------------------------------------------------------------------
6 from wxPython
.wx
import *
13 try: # try to load the IE ActiveX control
14 from wxPython
.lib
.activexwrapper
import MakeActiveXClass
15 import win32com
.client
.gencache
16 browserModule
= win32com
.client
.gencache
.EnsureModule(
17 "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)
22 BrowserClass
= MakeActiveXClass(browserModule
.WebBrowser
)
24 class MyHtmlWindow(BrowserClass
):
25 def SetPage(self
, html
):
27 filename
= tempfile
.mktemp('.html')
28 f
= open(filename
, 'w')
31 self
.Navigate(os
.path
.abspath(filename
))
32 self
.filename
= filename
34 def OnDocumentComplete(self
, pDisp
=None, URL
=None):
35 os
.unlink(self
.filename
)
38 from wxPython
.html
import *
39 MyHtmlWindow
= wxHtmlWindow
43 class StxFrame(wxFrame
):
45 def __init__(self
, stxFile
):
46 wxFrame
.__init
__(self
, None, -1, self
.title
, size
=(650, 700),
47 style
=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE
)
49 ##self.CreateStatusBar()
52 menu
.Append(10, "&Open\tCtrl-O", "Open a Structured Text file")
53 EVT_MENU(self
, 10, self
.OnOpen
)
54 menu
.Append(20, "&Close", "Close the current file")
55 EVT_MENU(self
, 20, self
.OnClose
)
56 menu
.Append(30, "&Save\tCtrl-S", "Save the current file")
57 EVT_MENU(self
, 30, self
.OnSave
)
58 menu
.Append(40, "Save &as", "Save the current file to a new name")
59 EVT_MENU(self
, 40, self
.OnSaveAs
)
60 menu
.Append(45, "Save as &html", "Save the current file as HTML")
61 EVT_MENU(self
, 45, self
.OnSaveAsHTML
)
62 menu
.AppendSeparator()
63 menu
.Append(50, "&Refresh\tCtrl-R", "Reload the file from disk")
64 EVT_MENU(self
, 50, self
.OnRefresh
)
65 menu
.AppendSeparator()
66 menu
.Append(60, "E&xit\tCtrl-X", "Close the application")
67 EVT_MENU(self
, 60, self
.OnExit
)
71 menuBar
.Append(menu
, "&File")
72 self
.SetMenuBar(menuBar
)
75 nb
= wxNotebook(self
, -1)
76 EVT_NOTEBOOK_PAGE_CHANGED(self
, -1, self
.OnPageChanged
)
78 self
.htmlWin
= MyHtmlWindow(nb
, -1)
79 nb
.AddPage(self
.htmlWin
, "View")
81 self
.editWin
= wxTextCtrl(nb
, -1, "", style
=wxTE_MULTILINE
)
82 self
.editWin
.SetFont(wxFont(10, wxTELETYPE
, wxNORMAL
, wxNORMAL
))
83 nb
.AddPage(self
.editWin
, "Edit")
85 self
.viewHtml
= wxTextCtrl(nb
, -1, "", style
=wxTE_MULTILINE|wxTE_READONLY
)
86 self
.viewHtml
.SetFont(wxFont(10, wxTELETYPE
, wxNORMAL
, wxNORMAL
))
87 nb
.AddPage(self
.viewHtml
, "HTML")
89 self
.LoadStxFile(stxFile
)
92 def LoadStxFile(self
, stxFile
):
94 if stxFile
is not None:
95 text
= open(stxFile
).read()
96 self
.SetTitle(self
.title
+ ': ' + stxFile
)
99 self
.SetTitle(self
.title
)
100 self
.LoadStxText(text
)
103 def LoadStxText(self
, text
):
105 #html = str(StructuredText.html_with_references(text))
108 st
= StructuredText
.Basic(text
)
109 doc
= StructuredText
.Document(st
)
110 html
= StructuredText
.HTMLNG(doc
)
112 self
.htmlWin
.SetPage(html
)
113 self
.editWin
.SetValue(text
)
114 self
.viewHtml
.SetValue(html
)
118 def OnPageChanged(self
, evt
):
119 if evt
.GetOldSelection() == 1: # if it was on the edit page
120 text
= self
.editWin
.GetValue()
121 self
.LoadStxText(text
)
124 def OnOpen(self
, evt
):
125 dlg
= wxFileDialog(self
, defaultDir
=os
.getcwd(),
126 wildcard
= "STX files (*.stx)|*.stx|"
127 "Text files (*.txt)|*.txt|"
128 "All files (*.*)|*.*",
130 if dlg
.ShowModal() == wxID_OK
:
131 self
.LoadStxFile(dlg
.GetPath())
136 def OnClose(self
, evt
):
137 self
.LoadStxFile(None)
140 def OnSave(self
, evt
):
144 text
= self
.editWin
.GetValue()
145 open(self
.file, 'w').write(text
)
146 self
.LoadStxFile(self
.file)
149 def OnSaveAs(self
, evt
):
150 dlg
= wxFileDialog(self
, "Save as...", defaultDir
=os
.getcwd(),
151 wildcard
= "STX files (*.stx)|*.stx|"
152 "Text files (*.txt)|*.txt|"
153 "All files (*.*)|*.*",
155 if dlg
.ShowModal() == wxID_OK
:
157 text
= self
.editWin
.GetValue()
158 open(file, 'w').write(text
)
159 self
.LoadStxFile(file)
163 def OnSaveAsHTML(self
, evt
):
164 dlg
= wxFileDialog(self
, "Save as...", defaultDir
=os
.getcwd(),
165 wildcard
= "HTML files (*.html)|*.html|"
166 "All files (*.*)|*.*",
168 if dlg
.ShowModal() == wxID_OK
:
170 text
= self
.editWin
.GetValue()
171 self
.LoadStxText(text
)
172 open(file, 'w').write(self
.html
)
177 def OnRefresh(self
, evt
):
178 self
.LoadStxFile(self
.file)
181 def OnExit(self
, evt
):
188 app
= wxPySimpleApp()
189 wxInitAllImageHandlers()
191 if len(sys
.argv
) > 1:
192 filename
= sys
.argv
[1]
196 frame
= StxFrame(filename
)