1 # 11/18/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
5 # 12/13/2003 - Jeff Grimmett (grimmtooth@softhome.net)
7 # o got the wxpTag stuff working right.
14 import wx
.html
as html
19 #----------------------------------------------------------------------
21 # This shows how to catch the OnLinkClicked non-event. (It's a virtual
22 # method in the C++ code...)
23 class MyHtmlWindow(html
.HtmlWindow
):
24 def __init__(self
, parent
, id, log
):
25 html
.HtmlWindow
.__init
__(self
, parent
, id, style
=wx
.NO_FULL_REPAINT_ON_RESIZE
)
27 self
.Bind(wx
.EVT_SCROLLWIN
, self
.OnScroll
)
29 def OnScroll( self
, event
):
30 #print 'event.GetOrientation()',event.GetOrientation()
31 #print 'event.GetPosition()',event.GetPosition()
34 def OnLinkClicked(self
, linkinfo
):
35 self
.log
.WriteText('OnLinkClicked: %s\n' % linkinfo
.GetHref())
37 # Virtuals in the base class have been renamed with base_ on the front.
38 self
.base_OnLinkClicked(linkinfo
)
41 def OnSetTitle(self
, title
):
42 self
.log
.WriteText('OnSetTitle: %s\n' % title
)
43 self
.base_OnSetTitle(title
)
45 def OnCellMouseHover(self
, cell
, x
, y
):
46 self
.log
.WriteText('OnCellMouseHover: %s, (%d %d)\n' % (cell
, x
, y
))
47 self
.base_OnCellMouseHover(cell
, x
, y
)
49 def OnCellClicked(self
, cell
, x
, y
, evt
):
50 self
.log
.WriteText('OnCellClicked: %s, (%d %d)\n' % (cell
, x
, y
))
51 self
.base_OnCellClicked(cell
, x
, y
, evt
)
54 # This filter doesn't really do anything but show how to use filters
55 class MyHtmlFilter(html
.HtmlFilter
):
56 def __init__(self
, log
):
57 html
.HtmlFilter
.__init
__(self
)
60 # This method decides if this filter is able to read the file
61 def CanRead(self
, fsfile
):
62 self
.log
.write("CanRead: %s\n" % fsfile
.GetMimeType())
65 # If CanRead returns True then this method is called to actually
66 # read the file and return the contents.
67 def ReadFile(self
, fsfile
):
71 class TestHtmlPanel(wx
.Panel
):
72 def __init__(self
, parent
, frame
, log
):
73 wx
.Panel
.__init
__(self
, parent
, -1, style
=wx
.NO_FULL_REPAINT_ON_RESIZE
)
76 self
.cwd
= os
.path
.split(sys
.argv
[0])[0]
79 self
.cwd
= os
.getcwd()
81 self
.titleBase
= frame
.GetTitle()
83 html
.HtmlWindow_AddFilter(MyHtmlFilter(log
))
85 self
.html
= MyHtmlWindow(self
, -1, log
)
86 self
.html
.SetRelatedFrame(frame
, self
.titleBase
+ " -- %s")
87 self
.html
.SetRelatedStatusBar(0)
89 self
.printer
= html
.HtmlEasyPrinting()
91 self
.box
= wx
.BoxSizer(wx
.VERTICAL
)
92 self
.box
.Add(self
.html
, 1, wx
.GROW
)
94 subbox
= wx
.BoxSizer(wx
.HORIZONTAL
)
96 btn
= wx
.Button(self
, -1, "Load File")
97 self
.Bind(wx
.EVT_BUTTON
, self
.OnLoadFile
, btn
)
98 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
100 btn
= wx
.Button(self
, -1, "Load URL")
101 self
.Bind(wx
.EVT_BUTTON
, self
.OnLoadURL
, btn
)
102 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
104 btn
= wx
.Button(self
, -1, "With Widgets")
105 self
.Bind(wx
.EVT_BUTTON
, self
.OnWithWidgets
, btn
)
106 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
108 btn
= wx
.Button(self
, -1, "Back")
109 self
.Bind(wx
.EVT_BUTTON
, self
.OnBack
, btn
)
110 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
112 btn
= wx
.Button(self
, -1, "Forward")
113 self
.Bind(wx
.EVT_BUTTON
, self
.OnForward
, btn
)
114 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
116 btn
= wx
.Button(self
, -1, "Print")
117 self
.Bind(wx
.EVT_BUTTON
, self
.OnPrint
, btn
)
118 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
120 btn
= wx
.Button(self
, -1, "View Source")
121 self
.Bind(wx
.EVT_BUTTON
, self
.OnViewSource
, btn
)
122 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
124 self
.box
.Add(subbox
, 0, wx
.GROW
)
125 self
.SetSizer(self
.box
)
126 self
.SetAutoLayout(True)
128 # A button with this ID is created on the widget test page.
129 self
.Bind(wx
.EVT_BUTTON
, self
.OnOk
, id=wx
.ID_OK
)
131 self
.OnShowDefault(None)
134 def ShutdownDemo(self
):
135 # put the frame title back
137 self
.frame
.SetTitle(self
.titleBase
)
140 def OnShowDefault(self
, event
):
141 name
= os
.path
.join(self
.cwd
, opj('data/test.htm'))
142 self
.html
.LoadPage(name
)
145 def OnLoadFile(self
, event
):
146 dlg
= wx
.FileDialog(self
, wildcard
= '*.htm*', style
=wx
.OPEN
)
150 self
.html
.LoadPage(path
)
155 def OnLoadURL(self
, event
):
156 dlg
= wx
.TextEntryDialog(self
, "Enter a URL")
160 self
.html
.LoadPage(url
)
165 def OnWithWidgets(self
, event
):
167 name
= os
.path
.join(self
.cwd
, opj('data/widgetTest.htm'))
168 self
.html
.LoadPage(name
)
171 def OnOk(self
, event
):
172 self
.log
.WriteText("It works!\n")
174 def OnBack(self
, event
):
175 if not self
.html
.HistoryBack():
176 wx
.MessageBox("No more items in history!")
179 def OnForward(self
, event
):
180 if not self
.html
.HistoryForward():
181 wx
.MessageBox("No more items in history!")
184 def OnViewSource(self
, event
):
185 import wx
.lib
.dialogs
as dlgs
187 source
= self
.html
.GetParser().GetSource()
189 dlg
= dlgs
.wxScrolledMessageDialog(self
, source
, 'HTML Source')
194 def OnPrint(self
, event
):
195 ##self.printer.GetPageSetupData().SetMarginTopLeft((100,100))
196 self
.printer
.PrintFile(self
.html
.GetOpenedPage())
198 #----------------------------------------------------------------------
200 def runTest(frame
, nb
, log
):
201 win
= TestHtmlPanel(nb
, frame
, log
)
202 print wx
.Window_FindFocus()
206 #----------------------------------------------------------------------
209 overview
= """<html><body>
210 <h2>wxHtmlWindow</h2>
212 <p>wxHtmlWindow is capable of parsing and rendering most
215 <p>It is not intended to be a high-end HTML browser. If you're
216 looking for something like that see the IEHtmlWin class, which
217 wraps the core MSIE HTML viewer.
224 if __name__
== '__main__':
227 run
.main(['', os
.path
.basename(sys
.argv
[0])])