]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/stxview/stxview.py
formatting changes
[wxWidgets.git] / wxPython / samples / stxview / stxview.py
1 #!/usr/bin/env python
2 #----------------------------------------------------------------------
3
4 import sys, os
5 import StructuredText
6 from wxPython.wx import *
7
8
9 USE_WXHTML = 1
10
11
12 if not USE_WXHTML:
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)
18 except:
19 USE_WXHTML = 1
20
21 if not USE_WXHTML:
22 BrowserClass = MakeActiveXClass(browserModule.WebBrowser)
23
24 class MyHtmlWindow(BrowserClass):
25 def SetPage(self, html):
26 import tempfile
27 filename = tempfile.mktemp('.html')
28 f = open(filename, 'w')
29 f.write(html)
30 f.close()
31 self.Navigate(os.path.abspath(filename))
32 self.filename = filename
33
34 def OnDocumentComplete(self, pDisp=None, URL=None):
35 os.unlink(self.filename)
36
37 else:
38 from wxPython.html import *
39 MyHtmlWindow = wxHtmlWindow
40
41
42
43 class StxFrame(wxFrame):
44 title = "StxViewer"
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)
48
49 ##self.CreateStatusBar()
50
51 menu = wxMenu()
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)
68
69
70 menuBar = wxMenuBar()
71 menuBar.Append(menu, "&File")
72 self.SetMenuBar(menuBar)
73
74
75 nb = wxNotebook(self, -1)
76 EVT_NOTEBOOK_PAGE_CHANGED(self, -1, self.OnPageChanged)
77
78 self.htmlWin = MyHtmlWindow(nb, -1)
79 nb.AddPage(self.htmlWin, "View")
80
81 self.editWin = wxTextCtrl(nb, -1, "", style=wxTE_MULTILINE)
82 self.editWin.SetFont(wxFont(10, wxTELETYPE, wxNORMAL, wxNORMAL))
83 nb.AddPage(self.editWin, "Edit")
84
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")
88
89 self.LoadStxFile(stxFile)
90
91
92 def LoadStxFile(self, stxFile):
93 self.file = stxFile
94 if stxFile is not None:
95 text = open(stxFile).read()
96 self.SetTitle(self.title + ': ' + stxFile)
97 else:
98 text = ""
99 self.SetTitle(self.title)
100 self.LoadStxText(text)
101
102
103 def LoadStxText(self, text):
104 # Old ST
105 #html = str(StructuredText.html_with_references(text))
106
107 # NG Version
108 st = StructuredText.Basic(text)
109 doc = StructuredText.Document(st)
110 html = StructuredText.HTMLNG(doc)
111
112 self.htmlWin.SetPage(html)
113 self.editWin.SetValue(text)
114 self.viewHtml.SetValue(html)
115 self.html = html
116
117
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)
122
123
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 (*.*)|*.*",
129 style=wxOPEN)
130 if dlg.ShowModal() == wxID_OK:
131 self.LoadStxFile(dlg.GetPath())
132 dlg.Destroy()
133
134
135
136 def OnClose(self, evt):
137 self.LoadStxFile(None)
138
139
140 def OnSave(self, evt):
141 if not self.file:
142 self.OnSaveAs(evt)
143 else:
144 text = self.editWin.GetValue()
145 open(self.file, 'w').write(text)
146 self.LoadStxFile(self.file)
147
148
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 (*.*)|*.*",
154 style=wxSAVE)
155 if dlg.ShowModal() == wxID_OK:
156 file = dlg.GetPath()
157 text = self.editWin.GetValue()
158 open(file, 'w').write(text)
159 self.LoadStxFile(file)
160 dlg.Destroy()
161
162
163 def OnSaveAsHTML(self, evt):
164 dlg = wxFileDialog(self, "Save as...", defaultDir=os.getcwd(),
165 wildcard = "HTML files (*.html)|*.html|"
166 "All files (*.*)|*.*",
167 style=wxSAVE)
168 if dlg.ShowModal() == wxID_OK:
169 file = dlg.GetPath()
170 text = self.editWin.GetValue()
171 self.LoadStxText(text)
172 open(file, 'w').write(self.html)
173 dlg.Destroy()
174
175
176
177 def OnRefresh(self, evt):
178 self.LoadStxFile(self.file)
179
180
181 def OnExit(self, evt):
182 self.Close(true)
183
184
185
186
187
188 app = wxPySimpleApp()
189 wxInitAllImageHandlers()
190
191 if len(sys.argv) > 1:
192 filename = sys.argv[1]
193 else:
194 filename = None
195
196 frame = StxFrame(filename)
197 frame.Show(true)
198 app.MainLoop()
199
200
201