11 #----------------------------------------------------------------------
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
)
19 self
.Bind(wx
.EVT_SCROLLWIN
, self
.OnScroll
)
21 def OnScroll( self
, event
):
22 #print 'event.GetOrientation()',event.GetOrientation()
23 #print 'event.GetPosition()',event.GetPosition()
26 def OnLinkClicked(self
, linkinfo
):
27 self
.log
.WriteText('OnLinkClicked: %s\n' % linkinfo
.GetHref())
29 # Virtuals in the base class have been renamed with base_ on the front.
30 self
.base_OnLinkClicked(linkinfo
)
33 def OnSetTitle(self
, title
):
34 self
.log
.WriteText('OnSetTitle: %s\n' % title
)
35 self
.base_OnSetTitle(title
)
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
)
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
)
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
)
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())
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
):
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
)
68 self
.cwd
= os
.path
.split(sys
.argv
[0])[0]
71 self
.cwd
= os
.getcwd()
73 self
.titleBase
= frame
.GetTitle()
75 html
.HtmlWindow_AddFilter(MyHtmlFilter(log
))
77 self
.html
= MyHtmlWindow(self
, -1, log
)
78 self
.html
.SetRelatedFrame(frame
, self
.titleBase
+ " -- %s")
79 self
.html
.SetRelatedStatusBar(0)
81 self
.printer
= html
.HtmlEasyPrinting()
83 self
.box
= wx
.BoxSizer(wx
.VERTICAL
)
84 self
.box
.Add(self
.html
, 1, wx
.GROW
)
86 subbox
= wx
.BoxSizer(wx
.HORIZONTAL
)
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)
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)
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)
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)
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)
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)
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)
116 self
.box
.Add(subbox
, 0, wx
.GROW
)
117 self
.SetSizer(self
.box
)
118 self
.SetAutoLayout(True)
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
)
123 self
.OnShowDefault(None)
126 def ShutdownDemo(self
):
127 # put the frame title back
129 self
.frame
.SetTitle(self
.titleBase
)
132 def OnShowDefault(self
, event
):
133 name
= os
.path
.join(self
.cwd
, opj('data/test.htm'))
134 self
.html
.LoadPage(name
)
137 def OnLoadFile(self
, event
):
138 dlg
= wx
.FileDialog(self
, wildcard
= '*.htm*', style
=wx
.OPEN
)
142 self
.html
.LoadPage(path
)
147 def OnLoadURL(self
, event
):
148 dlg
= wx
.TextEntryDialog(self
, "Enter a URL")
152 self
.html
.LoadPage(url
)
157 def OnWithWidgets(self
, event
):
159 name
= os
.path
.join(self
.cwd
, opj('data/widgetTest.htm'))
160 self
.html
.LoadPage(name
)
163 def OnOk(self
, event
):
164 self
.log
.WriteText("It works!\n")
166 def OnBack(self
, event
):
167 if not self
.html
.HistoryBack():
168 wx
.MessageBox("No more items in history!")
171 def OnForward(self
, event
):
172 if not self
.html
.HistoryForward():
173 wx
.MessageBox("No more items in history!")
176 def OnViewSource(self
, event
):
177 import wx
.lib
.dialogs
179 source
= self
.html
.GetParser().GetSource()
181 dlg
= wx
.lib
.dialogs
.ScrolledMessageDialog(self
, source
, 'HTML Source')
186 def OnPrint(self
, event
):
187 self
.printer
.GetPrintData().SetPaperId(wx
.PAPER_LETTER
)
188 self
.printer
.PrintFile(self
.html
.GetOpenedPage())
190 #----------------------------------------------------------------------
192 def runTest(frame
, nb
, log
):
193 win
= TestHtmlPanel(nb
, frame
, log
)
194 print wx
.Window_FindFocus()
198 #----------------------------------------------------------------------
201 overview
= """<html><body>
202 <h2>wx.HtmlWindow</h2>
204 <p>wx.HtmlWindow is capable of parsing and rendering most
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.
216 if __name__
== '__main__':
219 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])