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
)
20 if "gtk2" in wx
.PlatformInfo
:
21 self
.SetStandardFonts()
23 def OnScroll( self
, event
):
24 #print 'event.GetOrientation()',event.GetOrientation()
25 #print 'event.GetPosition()',event.GetPosition()
28 def OnLinkClicked(self
, linkinfo
):
29 self
.log
.WriteText('OnLinkClicked: %s\n' % linkinfo
.GetHref())
31 # Virtuals in the base class have been renamed with base_ on the front.
32 self
.base_OnLinkClicked(linkinfo
)
35 def OnSetTitle(self
, title
):
36 self
.log
.WriteText('OnSetTitle: %s\n' % title
)
37 self
.base_OnSetTitle(title
)
39 def OnCellMouseHover(self
, cell
, x
, y
):
40 self
.log
.WriteText('OnCellMouseHover: %s, (%d %d)\n' % (cell
, x
, y
))
41 self
.base_OnCellMouseHover(cell
, x
, y
)
43 def OnCellClicked(self
, cell
, x
, y
, evt
):
44 self
.log
.WriteText('OnCellClicked: %s, (%d %d)\n' % (cell
, x
, y
))
45 self
.base_OnCellClicked(cell
, x
, y
, evt
)
48 # This filter doesn't really do anything but show how to use filters
49 class MyHtmlFilter(html
.HtmlFilter
):
50 def __init__(self
, log
):
51 html
.HtmlFilter
.__init
__(self
)
54 # This method decides if this filter is able to read the file
55 def CanRead(self
, fsfile
):
56 self
.log
.write("CanRead: %s\n" % fsfile
.GetMimeType())
59 # If CanRead returns True then this method is called to actually
60 # read the file and return the contents.
61 def ReadFile(self
, fsfile
):
65 class TestHtmlPanel(wx
.Panel
):
66 def __init__(self
, parent
, frame
, log
):
67 wx
.Panel
.__init
__(self
, parent
, -1, style
=wx
.NO_FULL_REPAINT_ON_RESIZE
)
70 self
.cwd
= os
.path
.split(sys
.argv
[0])[0]
73 self
.cwd
= os
.getcwd()
75 self
.titleBase
= frame
.GetTitle()
77 html
.HtmlWindow_AddFilter(MyHtmlFilter(log
))
79 self
.html
= MyHtmlWindow(self
, -1, log
)
80 self
.html
.SetRelatedFrame(frame
, self
.titleBase
+ " -- %s")
81 self
.html
.SetRelatedStatusBar(0)
83 self
.printer
= html
.HtmlEasyPrinting()
85 self
.box
= wx
.BoxSizer(wx
.VERTICAL
)
86 self
.box
.Add(self
.html
, 1, wx
.GROW
)
88 subbox
= wx
.BoxSizer(wx
.HORIZONTAL
)
90 btn
= wx
.Button(self
, -1, "Load File")
91 self
.Bind(wx
.EVT_BUTTON
, self
.OnLoadFile
, btn
)
92 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
94 btn
= wx
.Button(self
, -1, "Load URL")
95 self
.Bind(wx
.EVT_BUTTON
, self
.OnLoadURL
, btn
)
96 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
98 btn
= wx
.Button(self
, -1, "With Widgets")
99 self
.Bind(wx
.EVT_BUTTON
, self
.OnWithWidgets
, btn
)
100 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
102 btn
= wx
.Button(self
, -1, "Back")
103 self
.Bind(wx
.EVT_BUTTON
, self
.OnBack
, btn
)
104 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
106 btn
= wx
.Button(self
, -1, "Forward")
107 self
.Bind(wx
.EVT_BUTTON
, self
.OnForward
, btn
)
108 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
110 btn
= wx
.Button(self
, -1, "Print")
111 self
.Bind(wx
.EVT_BUTTON
, self
.OnPrint
, btn
)
112 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
114 btn
= wx
.Button(self
, -1, "View Source")
115 self
.Bind(wx
.EVT_BUTTON
, self
.OnViewSource
, btn
)
116 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
118 self
.box
.Add(subbox
, 0, wx
.GROW
)
119 self
.SetSizer(self
.box
)
120 self
.SetAutoLayout(True)
122 # A button with this ID is created on the widget test page.
123 self
.Bind(wx
.EVT_BUTTON
, self
.OnOk
, id=wx
.ID_OK
)
125 self
.OnShowDefault(None)
128 def ShutdownDemo(self
):
129 # put the frame title back
131 self
.frame
.SetTitle(self
.titleBase
)
134 def OnShowDefault(self
, event
):
135 name
= os
.path
.join(self
.cwd
, opj('data/test.htm'))
136 self
.html
.LoadPage(name
)
139 def OnLoadFile(self
, event
):
140 dlg
= wx
.FileDialog(self
, wildcard
= '*.htm*', style
=wx
.OPEN
)
144 self
.html
.LoadPage(path
)
149 def OnLoadURL(self
, event
):
150 dlg
= wx
.TextEntryDialog(self
, "Enter a URL")
154 self
.html
.LoadPage(url
)
159 def OnWithWidgets(self
, event
):
161 name
= os
.path
.join(self
.cwd
, opj('data/widgetTest.htm'))
162 self
.html
.LoadPage(name
)
165 def OnOk(self
, event
):
166 self
.log
.WriteText("It works!\n")
168 def OnBack(self
, event
):
169 if not self
.html
.HistoryBack():
170 wx
.MessageBox("No more items in history!")
173 def OnForward(self
, event
):
174 if not self
.html
.HistoryForward():
175 wx
.MessageBox("No more items in history!")
178 def OnViewSource(self
, event
):
179 import wx
.lib
.dialogs
181 source
= self
.html
.GetParser().GetSource()
183 dlg
= wx
.lib
.dialogs
.ScrolledMessageDialog(self
, source
, 'HTML Source')
188 def OnPrint(self
, event
):
189 self
.printer
.GetPrintData().SetPaperId(wx
.PAPER_LETTER
)
190 self
.printer
.PrintFile(self
.html
.GetOpenedPage())
192 #----------------------------------------------------------------------
194 def runTest(frame
, nb
, log
):
195 win
= TestHtmlPanel(nb
, frame
, log
)
199 #----------------------------------------------------------------------
202 overview
= """<html><body>
203 <h2>wx.HtmlWindow</h2>
205 <p>wx.HtmlWindow is capable of parsing and rendering most
208 <p>It is not intended to be a high-end HTML browser. If you're
209 looking for something like that see the IEHtmlWin class, which
210 wraps the core MSIE HTML viewer.
217 if __name__
== '__main__':
220 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])