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.
9 # 12/18/2003 - Jeff Grimmett (grimmtooth@softhome.net)
11 # o wxScrolledMessageDialog -> ScrolledMessageDialog
18 import wx
.html
as html
23 #----------------------------------------------------------------------
25 # This shows how to catch the OnLinkClicked non-event. (It's a virtual
26 # method in the C++ code...)
27 class MyHtmlWindow(html
.HtmlWindow
):
28 def __init__(self
, parent
, id, log
):
29 html
.HtmlWindow
.__init
__(self
, parent
, id, style
=wx
.NO_FULL_REPAINT_ON_RESIZE
)
31 self
.Bind(wx
.EVT_SCROLLWIN
, self
.OnScroll
)
33 def OnScroll( self
, event
):
34 #print 'event.GetOrientation()',event.GetOrientation()
35 #print 'event.GetPosition()',event.GetPosition()
38 def OnLinkClicked(self
, linkinfo
):
39 self
.log
.WriteText('OnLinkClicked: %s\n' % linkinfo
.GetHref())
41 # Virtuals in the base class have been renamed with base_ on the front.
42 self
.base_OnLinkClicked(linkinfo
)
45 def OnSetTitle(self
, title
):
46 self
.log
.WriteText('OnSetTitle: %s\n' % title
)
47 self
.base_OnSetTitle(title
)
49 def OnCellMouseHover(self
, cell
, x
, y
):
50 self
.log
.WriteText('OnCellMouseHover: %s, (%d %d)\n' % (cell
, x
, y
))
51 self
.base_OnCellMouseHover(cell
, x
, y
)
53 def OnCellClicked(self
, cell
, x
, y
, evt
):
54 self
.log
.WriteText('OnCellClicked: %s, (%d %d)\n' % (cell
, x
, y
))
55 self
.base_OnCellClicked(cell
, x
, y
, evt
)
58 # This filter doesn't really do anything but show how to use filters
59 class MyHtmlFilter(html
.HtmlFilter
):
60 def __init__(self
, log
):
61 html
.HtmlFilter
.__init
__(self
)
64 # This method decides if this filter is able to read the file
65 def CanRead(self
, fsfile
):
66 self
.log
.write("CanRead: %s\n" % fsfile
.GetMimeType())
69 # If CanRead returns True then this method is called to actually
70 # read the file and return the contents.
71 def ReadFile(self
, fsfile
):
75 class TestHtmlPanel(wx
.Panel
):
76 def __init__(self
, parent
, frame
, log
):
77 wx
.Panel
.__init
__(self
, parent
, -1, style
=wx
.NO_FULL_REPAINT_ON_RESIZE
)
80 self
.cwd
= os
.path
.split(sys
.argv
[0])[0]
83 self
.cwd
= os
.getcwd()
85 self
.titleBase
= frame
.GetTitle()
87 html
.HtmlWindow_AddFilter(MyHtmlFilter(log
))
89 self
.html
= MyHtmlWindow(self
, -1, log
)
90 self
.html
.SetRelatedFrame(frame
, self
.titleBase
+ " -- %s")
91 self
.html
.SetRelatedStatusBar(0)
93 self
.printer
= html
.HtmlEasyPrinting()
95 self
.box
= wx
.BoxSizer(wx
.VERTICAL
)
96 self
.box
.Add(self
.html
, 1, wx
.GROW
)
98 subbox
= wx
.BoxSizer(wx
.HORIZONTAL
)
100 btn
= wx
.Button(self
, -1, "Load File")
101 self
.Bind(wx
.EVT_BUTTON
, self
.OnLoadFile
, btn
)
102 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
104 btn
= wx
.Button(self
, -1, "Load URL")
105 self
.Bind(wx
.EVT_BUTTON
, self
.OnLoadURL
, btn
)
106 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
108 btn
= wx
.Button(self
, -1, "With Widgets")
109 self
.Bind(wx
.EVT_BUTTON
, self
.OnWithWidgets
, btn
)
110 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
112 btn
= wx
.Button(self
, -1, "Back")
113 self
.Bind(wx
.EVT_BUTTON
, self
.OnBack
, btn
)
114 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
116 btn
= wx
.Button(self
, -1, "Forward")
117 self
.Bind(wx
.EVT_BUTTON
, self
.OnForward
, btn
)
118 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
120 btn
= wx
.Button(self
, -1, "Print")
121 self
.Bind(wx
.EVT_BUTTON
, self
.OnPrint
, btn
)
122 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
124 btn
= wx
.Button(self
, -1, "View Source")
125 self
.Bind(wx
.EVT_BUTTON
, self
.OnViewSource
, btn
)
126 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
128 self
.box
.Add(subbox
, 0, wx
.GROW
)
129 self
.SetSizer(self
.box
)
130 self
.SetAutoLayout(True)
132 # A button with this ID is created on the widget test page.
133 self
.Bind(wx
.EVT_BUTTON
, self
.OnOk
, id=wx
.ID_OK
)
135 self
.OnShowDefault(None)
138 def ShutdownDemo(self
):
139 # put the frame title back
141 self
.frame
.SetTitle(self
.titleBase
)
144 def OnShowDefault(self
, event
):
145 name
= os
.path
.join(self
.cwd
, opj('data/test.htm'))
146 self
.html
.LoadPage(name
)
149 def OnLoadFile(self
, event
):
150 dlg
= wx
.FileDialog(self
, wildcard
= '*.htm*', style
=wx
.OPEN
)
154 self
.html
.LoadPage(path
)
159 def OnLoadURL(self
, event
):
160 dlg
= wx
.TextEntryDialog(self
, "Enter a URL")
164 self
.html
.LoadPage(url
)
169 def OnWithWidgets(self
, event
):
171 name
= os
.path
.join(self
.cwd
, opj('data/widgetTest.htm'))
172 self
.html
.LoadPage(name
)
175 def OnOk(self
, event
):
176 self
.log
.WriteText("It works!\n")
178 def OnBack(self
, event
):
179 if not self
.html
.HistoryBack():
180 wx
.MessageBox("No more items in history!")
183 def OnForward(self
, event
):
184 if not self
.html
.HistoryForward():
185 wx
.MessageBox("No more items in history!")
188 def OnViewSource(self
, event
):
189 import wx
.lib
.dialogs
191 source
= self
.html
.GetParser().GetSource()
193 dlg
= wx
.lib
.dialogs
.ScrolledMessageDialog(self
, source
, 'HTML Source')
198 def OnPrint(self
, event
):
199 ##self.printer.GetPageSetupData().SetMarginTopLeft((100,100))
200 self
.printer
.PrintFile(self
.html
.GetOpenedPage())
202 #----------------------------------------------------------------------
204 def runTest(frame
, nb
, log
):
205 win
= TestHtmlPanel(nb
, frame
, log
)
206 print wx
.Window_FindFocus()
210 #----------------------------------------------------------------------
213 overview
= """<html><body>
214 <h2>wxHtmlWindow</h2>
216 <p>wxHtmlWindow is capable of parsing and rendering most
219 <p>It is not intended to be a high-end HTML browser. If you're
220 looking for something like that see the IEHtmlWin class, which
221 wraps the core MSIE HTML viewer.
228 if __name__
== '__main__':
231 run
.main(['', os
.path
.basename(sys
.argv
[0])])