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 if isinstance(cell
, html
.HtmlWordCell
):
37 sel
= html
.HtmlSelection()
38 self
.log
.WriteText(' %s\n' % cell
.ConvertToText(sel
))
39 super(MyHtmlWindow
, self
).OnCellClicked(cell
, x
, y
, evt
)
43 # This filter doesn't really do anything but show how to use filters
44 class MyHtmlFilter(html
.HtmlFilter
):
45 def __init__(self
, log
):
46 html
.HtmlFilter
.__init
__(self
)
49 # This method decides if this filter is able to read the file
50 def CanRead(self
, fsfile
):
51 self
.log
.write("CanRead: %s\n" % fsfile
.GetMimeType())
54 # If CanRead returns True then this method is called to actually
55 # read the file and return the contents.
56 def ReadFile(self
, fsfile
):
60 class TestHtmlPanel(wx
.Panel
):
61 def __init__(self
, parent
, frame
, log
):
62 wx
.Panel
.__init
__(self
, parent
, -1, style
=wx
.NO_FULL_REPAINT_ON_RESIZE
)
65 self
.cwd
= os
.path
.split(sys
.argv
[0])[0]
68 self
.cwd
= os
.getcwd()
70 self
.titleBase
= frame
.GetTitle()
72 html
.HtmlWindow_AddFilter(MyHtmlFilter(log
))
74 self
.html
= MyHtmlWindow(self
, -1, log
)
75 self
.html
.SetRelatedFrame(frame
, self
.titleBase
+ " -- %s")
76 self
.html
.SetRelatedStatusBar(0)
78 self
.printer
= html
.HtmlEasyPrinting()
80 self
.box
= wx
.BoxSizer(wx
.VERTICAL
)
81 self
.box
.Add(self
.html
, 1, wx
.GROW
)
83 subbox
= wx
.BoxSizer(wx
.HORIZONTAL
)
85 btn
= wx
.Button(self
, -1, "Load File")
86 self
.Bind(wx
.EVT_BUTTON
, self
.OnLoadFile
, btn
)
87 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
89 btn
= wx
.Button(self
, -1, "Load URL")
90 self
.Bind(wx
.EVT_BUTTON
, self
.OnLoadURL
, btn
)
91 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
93 btn
= wx
.Button(self
, -1, "With Widgets")
94 self
.Bind(wx
.EVT_BUTTON
, self
.OnWithWidgets
, btn
)
95 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
97 btn
= wx
.Button(self
, -1, "Back")
98 self
.Bind(wx
.EVT_BUTTON
, self
.OnBack
, btn
)
99 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
101 btn
= wx
.Button(self
, -1, "Forward")
102 self
.Bind(wx
.EVT_BUTTON
, self
.OnForward
, btn
)
103 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
105 btn
= wx
.Button(self
, -1, "Print")
106 self
.Bind(wx
.EVT_BUTTON
, self
.OnPrint
, btn
)
107 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
109 btn
= wx
.Button(self
, -1, "View Source")
110 self
.Bind(wx
.EVT_BUTTON
, self
.OnViewSource
, btn
)
111 subbox
.Add(btn
, 1, wx
.GROW | wx
.ALL
, 2)
113 self
.box
.Add(subbox
, 0, wx
.GROW
)
114 self
.SetSizer(self
.box
)
115 self
.SetAutoLayout(True)
117 # A button with this ID is created on the widget test page.
118 self
.Bind(wx
.EVT_BUTTON
, self
.OnOk
, id=wx
.ID_OK
)
120 self
.OnShowDefault(None)
123 def ShutdownDemo(self
):
124 # put the frame title back
126 self
.frame
.SetTitle(self
.titleBase
)
129 def OnShowDefault(self
, event
):
130 name
= os
.path
.join(self
.cwd
, opj('data/test.htm'))
131 self
.html
.LoadPage(name
)
134 def OnLoadFile(self
, event
):
135 dlg
= wx
.FileDialog(self
, wildcard
= '*.htm*', style
=wx
.OPEN
)
139 self
.html
.LoadPage(path
)
144 def OnLoadURL(self
, event
):
145 dlg
= wx
.TextEntryDialog(self
, "Enter a URL")
149 self
.html
.LoadPage(url
)
154 def OnWithWidgets(self
, event
):
156 name
= os
.path
.join(self
.cwd
, opj('data/widgetTest.htm'))
157 self
.html
.LoadPage(name
)
160 def OnOk(self
, event
):
161 self
.log
.WriteText("It works!\n")
163 def OnBack(self
, event
):
164 if not self
.html
.HistoryBack():
165 wx
.MessageBox("No more items in history!")
168 def OnForward(self
, event
):
169 if not self
.html
.HistoryForward():
170 wx
.MessageBox("No more items in history!")
173 def OnViewSource(self
, event
):
174 import wx
.lib
.dialogs
176 source
= self
.html
.GetParser().GetSource()
178 dlg
= wx
.lib
.dialogs
.ScrolledMessageDialog(self
, source
, 'HTML Source')
183 def OnPrint(self
, event
):
184 self
.printer
.GetPrintData().SetPaperId(wx
.PAPER_LETTER
)
185 self
.printer
.PrintFile(self
.html
.GetOpenedPage())
187 #----------------------------------------------------------------------
189 def runTest(frame
, nb
, log
):
190 win
= TestHtmlPanel(nb
, frame
, log
)
194 #----------------------------------------------------------------------
197 overview
= """<html><body>
198 <h2>wx.HtmlWindow</h2>
200 <p>wx.HtmlWindow is capable of parsing and rendering most
203 <p>It is not intended to be a high-end HTML browser. If you're
204 looking for something like that see the IEHtmlWin class, which
205 wraps the core MSIE HTML viewer.
212 if __name__
== '__main__':
215 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])