]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/HtmlWindow.py
bash version of wxPython's b script for win32
[wxWidgets.git] / wxPython / demo / HtmlWindow.py
1
2 import os
3 import sys
4
5 import wx
6 import wx.html as html
7 import wx.lib.wxpTag
8
9 from Main import opj
10
11 #----------------------------------------------------------------------
12
13 # This shows how to catch the OnLinkClicked non-event. (It's a virtual
14 # method in the C++ code...)
15 class MyHtmlWindow(html.HtmlWindow):
16 def __init__(self, parent, id, log):
17 html.HtmlWindow.__init__(self, parent, id, style=wx.NO_FULL_REPAINT_ON_RESIZE)
18 self.log = log
19 self.Bind(wx.EVT_SCROLLWIN, self.OnScroll )
20
21 def OnScroll( self, event ):
22 #print 'event.GetOrientation()',event.GetOrientation()
23 #print 'event.GetPosition()',event.GetPosition()
24 event.Skip()
25
26 def OnLinkClicked(self, linkinfo):
27 self.log.WriteText('OnLinkClicked: %s\n' % linkinfo.GetHref())
28
29 # Virtuals in the base class have been renamed with base_ on the front.
30 self.base_OnLinkClicked(linkinfo)
31
32
33 def OnSetTitle(self, title):
34 self.log.WriteText('OnSetTitle: %s\n' % title)
35 self.base_OnSetTitle(title)
36
37 def OnCellMouseHover(self, cell, x, y):
38 self.log.WriteText('OnCellMouseHover: %s, (%d %d)\n' % (cell, x, y))
39 self.base_OnCellMouseHover(cell, x, y)
40
41 def OnCellClicked(self, cell, x, y, evt):
42 self.log.WriteText('OnCellClicked: %s, (%d %d)\n' % (cell, x, y))
43 self.base_OnCellClicked(cell, x, y, evt)
44
45
46 # This filter doesn't really do anything but show how to use filters
47 class MyHtmlFilter(html.HtmlFilter):
48 def __init__(self, log):
49 html.HtmlFilter.__init__(self)
50 self.log = log
51
52 # This method decides if this filter is able to read the file
53 def CanRead(self, fsfile):
54 self.log.write("CanRead: %s\n" % fsfile.GetMimeType())
55 return False
56
57 # If CanRead returns True then this method is called to actually
58 # read the file and return the contents.
59 def ReadFile(self, fsfile):
60 return ""
61
62
63 class TestHtmlPanel(wx.Panel):
64 def __init__(self, parent, frame, log):
65 wx.Panel.__init__(self, parent, -1, style=wx.NO_FULL_REPAINT_ON_RESIZE)
66 self.log = log
67 self.frame = frame
68 self.cwd = os.path.split(sys.argv[0])[0]
69
70 if not self.cwd:
71 self.cwd = os.getcwd()
72 if frame:
73 self.titleBase = frame.GetTitle()
74
75 html.HtmlWindow_AddFilter(MyHtmlFilter(log))
76
77 self.html = MyHtmlWindow(self, -1, log)
78 self.html.SetRelatedFrame(frame, self.titleBase + " -- %s")
79 self.html.SetRelatedStatusBar(0)
80
81 self.printer = html.HtmlEasyPrinting()
82
83 self.box = wx.BoxSizer(wx.VERTICAL)
84 self.box.Add(self.html, 1, wx.GROW)
85
86 subbox = wx.BoxSizer(wx.HORIZONTAL)
87
88 btn = wx.Button(self, -1, "Load File")
89 self.Bind(wx.EVT_BUTTON, self.OnLoadFile, btn)
90 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
91
92 btn = wx.Button(self, -1, "Load URL")
93 self.Bind(wx.EVT_BUTTON, self.OnLoadURL, btn)
94 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
95
96 btn = wx.Button(self, -1, "With Widgets")
97 self.Bind(wx.EVT_BUTTON, self.OnWithWidgets, btn)
98 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
99
100 btn = wx.Button(self, -1, "Back")
101 self.Bind(wx.EVT_BUTTON, self.OnBack, btn)
102 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
103
104 btn = wx.Button(self, -1, "Forward")
105 self.Bind(wx.EVT_BUTTON, self.OnForward, btn)
106 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
107
108 btn = wx.Button(self, -1, "Print")
109 self.Bind(wx.EVT_BUTTON, self.OnPrint, btn)
110 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
111
112 btn = wx.Button(self, -1, "View Source")
113 self.Bind(wx.EVT_BUTTON, self.OnViewSource, btn)
114 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
115
116 self.box.Add(subbox, 0, wx.GROW)
117 self.SetSizer(self.box)
118 self.SetAutoLayout(True)
119
120 # A button with this ID is created on the widget test page.
121 self.Bind(wx.EVT_BUTTON, self.OnOk, id=wx.ID_OK)
122
123 self.OnShowDefault(None)
124
125
126 def ShutdownDemo(self):
127 # put the frame title back
128 if self.frame:
129 self.frame.SetTitle(self.titleBase)
130
131
132 def OnShowDefault(self, event):
133 name = os.path.join(self.cwd, opj('data/test.htm'))
134 self.html.LoadPage(name)
135
136
137 def OnLoadFile(self, event):
138 dlg = wx.FileDialog(self, wildcard = '*.htm*', style=wx.OPEN)
139
140 if dlg.ShowModal():
141 path = dlg.GetPath()
142 self.html.LoadPage(path)
143
144 dlg.Destroy()
145
146
147 def OnLoadURL(self, event):
148 dlg = wx.TextEntryDialog(self, "Enter a URL")
149
150 if dlg.ShowModal():
151 url = dlg.GetValue()
152 self.html.LoadPage(url)
153
154 dlg.Destroy()
155
156
157 def OnWithWidgets(self, event):
158 os.chdir(self.cwd)
159 name = os.path.join(self.cwd, opj('data/widgetTest.htm'))
160 self.html.LoadPage(name)
161
162
163 def OnOk(self, event):
164 self.log.WriteText("It works!\n")
165
166 def OnBack(self, event):
167 if not self.html.HistoryBack():
168 wx.MessageBox("No more items in history!")
169
170
171 def OnForward(self, event):
172 if not self.html.HistoryForward():
173 wx.MessageBox("No more items in history!")
174
175
176 def OnViewSource(self, event):
177 import wx.lib.dialogs
178
179 source = self.html.GetParser().GetSource()
180
181 dlg = wx.lib.dialogs.ScrolledMessageDialog(self, source, 'HTML Source')
182 dlg.ShowModal()
183 dlg.Destroy()
184
185
186 def OnPrint(self, event):
187 self.printer.GetPrintData().SetPaperId(wx.PAPER_LETTER)
188 self.printer.PrintFile(self.html.GetOpenedPage())
189
190 #----------------------------------------------------------------------
191
192 def runTest(frame, nb, log):
193 win = TestHtmlPanel(nb, frame, log)
194 print wx.Window_FindFocus()
195 return win
196
197
198 #----------------------------------------------------------------------
199
200
201 overview = """<html><body>
202 <h2>wx.HtmlWindow</h2>
203
204 <p>wx.HtmlWindow is capable of parsing and rendering most
205 simple HTML tags.
206
207 <p>It is not intended to be a high-end HTML browser. If you're
208 looking for something like that see the IEHtmlWin class, which
209 wraps the core MSIE HTML viewer.
210
211 </body></html>
212 """
213
214
215
216 if __name__ == '__main__':
217 import sys,os
218 import run
219 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
220
221
222
223
224
225
226
227