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 if "gtk2" in wx
.PlatformInfo
:
20 self
.SetStandardFonts()
22 def OnLinkClicked(self
, linkinfo
):
23 self
.log
.WriteText('OnLinkClicked: %s\n' % linkinfo
.GetHref())
24 super(MyHtmlWindow
, self
).OnLinkClicked(linkinfo
)
26 def OnSetTitle(self
, title
):
27 self
.log
.WriteText('OnSetTitle: %s\n' % title
)
28 super(MyHtmlWindow
, self
).OnSetTitle(title
)
30 def OnCellMouseHover(self
, cell
, x
, y
):
31 self
.log
.WriteText('OnCellMouseHover: %s, (%d %d)\n' % (cell
, x
, y
))
32 super(MyHtmlWindow
, self
).OnCellMouseHover(cell
, x
, y
)
34 def OnCellClicked(self
, cell
, x
, y
, evt
):
35 self
.log
.WriteText('OnCellClicked: %s, (%d %d)\n' % (cell
, x
, y
))
36 super(MyHtmlWindow
, self
).OnCellClicked(cell
, x
, y
, evt
)
40 # This filter doesn't really do anything but show how to use filters
41 class MyHtmlFilter(html
.HtmlFilter
):
42 def __init__(self
, log
):
43 html
.HtmlFilter
.__init
__(self
)
46 # This method decides if this filter is able to read the file
47 def CanRead(self
, fsfile
):
48 self
.log
.write("CanRead: %s\n" % fsfile
.GetMimeType())
51 # If CanRead returns True then this method is called to actually
52 # read the file and return the contents.
53 def ReadFile(self
, fsfile
):
57 class TestHtmlPanel(wx
.Panel
):
58 def __init__(self
, parent
, frame
, log
):
59 wx
.Panel
.__init
__(self
, parent
, -1, style
=wx
.NO_FULL_REPAINT_ON_RESIZE
)
62 self
.cwd
= os
.path
.split(sys
.argv
[0])[0]
65 self
.cwd
= os
.getcwd()
67 self
.titleBase
= frame
.GetTitle()
69 html
.HtmlWindow_AddFilter(MyHtmlFilter(log
))
71 self
.html
= MyHtmlWindow(self
, -1, log
)
72 self
.html
.SetRelatedFrame(frame
, self
.titleBase
+ " -- %s")
73 self
.html
.SetRelatedStatusBar(0)
75 self
.printer
= html
.HtmlEasyPrinting()
77 self
.box
= wx
.BoxSizer(wx
.VERTICAL
)
78 self
.box
.Add(self
.html
, 1, wx
.GROW
)
80 subbox
= wx
.BoxSizer(wx
.HORIZONTAL
)
82 btn
= wx
.Button(self
, -1, "Load File")
83 self
.Bind(wx
.EVT_BUTTON
, self
.OnLoadFile
, btn
)
84 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
86 btn
= wx
.Button(self
, -1, "Load URL")
87 self
.Bind(wx
.EVT_BUTTON
, self
.OnLoadURL
, btn
)
88 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
90 btn
= wx
.Button(self
, -1, "With Widgets")
91 self
.Bind(wx
.EVT_BUTTON
, self
.OnWithWidgets
, btn
)
92 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
94 btn
= wx
.Button(self
, -1, "Back")
95 self
.Bind(wx
.EVT_BUTTON
, self
.OnBack
, btn
)
96 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
98 btn
= wx
.Button(self
, -1, "Forward")
99 self
.Bind(wx
.EVT_BUTTON
, self
.OnForward
, btn
)
100 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
102 btn
= wx
.Button(self
, -1, "Print")
103 self
.Bind(wx
.EVT_BUTTON
, self
.OnPrint
, btn
)
104 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
106 btn
= wx
.Button(self
, -1, "View Source")
107 self
.Bind(wx
.EVT_BUTTON
, self
.OnViewSource
, btn
)
108 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
110 self
.box
.Add(subbox
, 0, wx
.GROW
)
111 self
.SetSizer(self
.box
)
112 self
.SetAutoLayout(True)
114 # A button with this ID is created on the widget test page.
115 self
.Bind(wx
.EVT_BUTTON
, self
.OnOk
, id=wx
.ID_OK
)
117 self
.OnShowDefault(None)
120 def ShutdownDemo(self
):
121 # put the frame title back
123 self
.frame
.SetTitle(self
.titleBase
)
126 def OnShowDefault(self
, event
):
127 name
= os
.path
.join(self
.cwd
, opj('data/test.htm'))
128 self
.html
.LoadPage(name
)
131 def OnLoadFile(self
, event
):
132 dlg
= wx
.FileDialog(self
, wildcard
= '*.htm*', style
=wx
.OPEN
)
136 self
.html
.LoadPage(path
)
141 def OnLoadURL(self
, event
):
142 dlg
= wx
.TextEntryDialog(self
, "Enter a URL")
146 self
.html
.LoadPage(url
)
151 def OnWithWidgets(self
, event
):
153 name
= os
.path
.join(self
.cwd
, opj('data/widgetTest.htm'))
154 self
.html
.LoadPage(name
)
157 def OnOk(self
, event
):
158 self
.log
.WriteText("It works!\n")
160 def OnBack(self
, event
):
161 if not self
.html
.HistoryBack():
162 wx
.MessageBox("No more items in history!")
165 def OnForward(self
, event
):
166 if not self
.html
.HistoryForward():
167 wx
.MessageBox("No more items in history!")
170 def OnViewSource(self
, event
):
171 import wx
.lib
.dialogs
173 source
= self
.html
.GetParser().GetSource()
175 dlg
= wx
.lib
.dialogs
.ScrolledMessageDialog(self
, source
, 'HTML Source')
180 def OnPrint(self
, event
):
181 self
.printer
.GetPrintData().SetPaperId(wx
.PAPER_LETTER
)
182 self
.printer
.PrintFile(self
.html
.GetOpenedPage())
184 #----------------------------------------------------------------------
186 def runTest(frame
, nb
, log
):
187 win
= TestHtmlPanel(nb
, frame
, log
)
191 #----------------------------------------------------------------------
194 overview
= """<html><body>
195 <h2>wx.HtmlWindow</h2>
197 <p>wx.HtmlWindow is capable of parsing and rendering most
200 <p>It is not intended to be a high-end HTML browser. If you're
201 looking for something like that see the IEHtmlWin class, which
202 wraps the core MSIE HTML viewer.
209 if __name__
== '__main__':
212 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])