1 # 11/18/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
10 import wx
.html
as html
16 #----------------------------------------------------------------------
18 # This shows how to catch the OnLinkClicked non-event. (It's a virtual
19 # method in the C++ code...)
20 class MyHtmlWindow(html
.HtmlWindow
):
21 def __init__(self
, parent
, id, log
):
22 html
.HtmlWindow
.__init
__(self
, parent
, id, style
=wx
.NO_FULL_REPAINT_ON_RESIZE
)
24 self
.Bind(wx
.EVT_SCROLLWIN
, self
.OnScroll
)
26 def OnScroll( self
, event
):
27 #print 'event.GetOrientation()',event.GetOrientation()
28 #print 'event.GetPosition()',event.GetPosition()
31 def OnLinkClicked(self
, linkinfo
):
32 self
.log
.WriteText('OnLinkClicked: %s\n' % linkinfo
.GetHref())
34 # Virtuals in the base class have been renamed with base_ on the front.
35 self
.base_OnLinkClicked(linkinfo
)
38 def OnSetTitle(self
, title
):
39 self
.log
.WriteText('OnSetTitle: %s\n' % title
)
40 self
.base_OnSetTitle(title
)
42 def OnCellMouseHover(self
, cell
, x
, y
):
43 self
.log
.WriteText('OnCellMouseHover: %s, (%d %d)\n' % (cell
, x
, y
))
44 self
.base_OnCellMouseHover(cell
, x
, y
)
46 def OnCellClicked(self
, cell
, x
, y
, evt
):
47 self
.log
.WriteText('OnCellClicked: %s, (%d %d)\n' % (cell
, x
, y
))
48 self
.base_OnCellClicked(cell
, x
, y
, evt
)
51 # This filter doesn't really do anything but show how to use filters
52 class MyHtmlFilter(html
.HtmlFilter
):
53 def __init__(self
, log
):
54 html
.HtmlFilter
.__init
__(self
)
57 # This method decides if this filter is able to read the file
58 def CanRead(self
, fsfile
):
59 self
.log
.write("CanRead: %s\n" % fsfile
.GetMimeType())
62 # If CanRead returns True then this method is called to actually
63 # read the file and return the contents.
64 def ReadFile(self
, fsfile
):
68 class TestHtmlPanel(wx
.Panel
):
69 def __init__(self
, parent
, frame
, log
):
70 wx
.Panel
.__init
__(self
, parent
, -1, style
=wx
.NO_FULL_REPAINT_ON_RESIZE
)
73 self
.cwd
= os
.path
.split(sys
.argv
[0])[0]
76 self
.cwd
= os
.getcwd()
78 self
.titleBase
= frame
.GetTitle()
80 html
.HtmlWindow_AddFilter(MyHtmlFilter(log
))
82 self
.html
= MyHtmlWindow(self
, -1, log
)
83 self
.html
.SetRelatedFrame(frame
, self
.titleBase
+ " -- %s")
84 self
.html
.SetRelatedStatusBar(0)
86 self
.printer
= html
.HtmlEasyPrinting()
88 self
.box
= wx
.BoxSizer(wx
.VERTICAL
)
89 self
.box
.Add(self
.html
, 1, wx
.GROW
)
91 subbox
= wx
.BoxSizer(wx
.HORIZONTAL
)
93 btn
= wx
.Button(self
, -1, "Load File")
94 self
.Bind(wx
.EVT_BUTTON
, self
.OnLoadFile
, btn
)
95 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
97 btn
= wx
.Button(self
, -1, "Load URL")
98 self
.Bind(wx
.EVT_BUTTON
, self
.OnLoadURL
, btn
)
99 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
101 btn
= wx
.Button(self
, -1, "With Widgets")
102 self
.Bind(wx
.EVT_BUTTON
, self
.OnWithWidgets
, btn
)
103 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
105 btn
= wx
.Button(self
, -1, "Back")
106 self
.Bind(wx
.EVT_BUTTON
, self
.OnBack
, btn
)
107 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
109 btn
= wx
.Button(self
, -1, "Forward")
110 self
.Bind(wx
.EVT_BUTTON
, self
.OnForward
, btn
)
111 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
113 btn
= wx
.Button(self
, -1, "Print")
114 self
.Bind(wx
.EVT_BUTTON
, self
.OnPrint
, btn
)
115 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
117 btn
= wx
.Button(self
, -1, "View Source")
118 self
.Bind(wx
.EVT_BUTTON
, self
.OnViewSource
, btn
)
119 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
121 self
.box
.Add(subbox
, 0, wx
.GROW
)
122 self
.SetSizer(self
.box
)
123 self
.SetAutoLayout(True)
125 # A button with this ID is created on the widget test page.
126 self
.Bind(wx
.EVT_BUTTON
, self
.OnOk
, id=wx
.ID_OK
)
128 self
.OnShowDefault(None)
131 def ShutdownDemo(self
):
132 # put the frame title back
134 self
.frame
.SetTitle(self
.titleBase
)
137 def OnShowDefault(self
, event
):
138 name
= os
.path
.join(self
.cwd
, opj('data/test.htm'))
139 self
.html
.LoadPage(name
)
142 def OnLoadFile(self
, event
):
143 dlg
= wx
.FileDialog(self
, wildcard
= '*.htm*', style
=wx
.OPEN
)
147 self
.html
.LoadPage(path
)
152 def OnLoadURL(self
, event
):
153 dlg
= wx
.TextEntryDialog(self
, "Enter a URL")
157 self
.html
.LoadPage(url
)
162 def OnWithWidgets(self
, event
):
164 name
= os
.path
.join(self
.cwd
, opj('data/widgetTest.htm'))
165 self
.html
.LoadPage(name
)
168 def OnOk(self
, event
):
169 self
.log
.WriteText("It works!\n")
171 def OnBack(self
, event
):
172 if not self
.html
.HistoryBack():
173 wx
.MessageBox("No more items in history!")
176 def OnForward(self
, event
):
177 if not self
.html
.HistoryForward():
178 wx
.MessageBox("No more items in history!")
181 def OnViewSource(self
, event
):
182 import wx
.lib
.dialogs
as dlgs
184 source
= self
.html
.GetParser().GetSource()
186 dlg
= dlgs
.wxScrolledMessageDialog(self
, source
, 'HTML Source')
191 def OnPrint(self
, event
):
192 ##self.printer.GetPageSetupData().SetMarginTopLeft((100,100))
193 self
.printer
.PrintFile(self
.html
.GetOpenedPage())
195 #----------------------------------------------------------------------
197 def runTest(frame
, nb
, log
):
198 win
= TestHtmlPanel(nb
, frame
, log
)
199 print wx
.Window_FindFocus()
203 #----------------------------------------------------------------------
206 overview
= """<html><body>
207 <h2>wxHtmlWindow</h2>
209 <p>wxHtmlWindow is capable of parsing and rendering most
212 <p>It is not intended to be a high-end HTML browser. If you're
213 looking for something like that see the IEHtmlWin class, which
214 wraps the core MSIE HTML viewer.
221 if __name__
== '__main__':
224 run
.main(['', os
.path
.basename(sys
.argv
[0])])