4 from wxPython
.wx
import *
5 from wxPython
.html
import *
6 import wxPython
.lib
.wxpTag
10 #----------------------------------------------------------------------
12 # This shows how to catch the OnLinkClicked non-event. (It's a virtual
13 # method in the C++ code...)
14 class MyHtmlWindow(wxHtmlWindow
):
15 def __init__(self
, parent
, id, log
):
16 wxHtmlWindow
.__init
__(self
, parent
, id, style
=wxNO_FULL_REPAINT_ON_RESIZE
)
18 EVT_SCROLLWIN( self
, self
.OnScroll
)
20 def OnScroll( self
, event
):
21 #print 'event.GetOrientation()',event.GetOrientation()
22 #print 'event.GetPosition()',event.GetPosition()
26 def OnLinkClicked(self
, linkinfo
):
27 self
.log
.WriteText('OnLinkClicked: %s\n' % linkinfo
.GetHref())
29 # Virtuals in the base class have been renamed with base_ on the front.
30 self
.base_OnLinkClicked(linkinfo
)
33 def OnSetTitle(self
, title
):
34 self
.log
.WriteText('OnSetTitle: %s\n' % title
)
35 self
.base_OnSetTitle(title
)
37 def OnCellMouseHover(self
, cell
, x
, y
):
38 self
.log
.WriteText('OnCellMouseHover: %s, (%d %d)\n' % (cell
, x
, y
))
39 self
.base_OnCellMouseHover(cell
, x
, y
)
41 def OnCellClicked(self
, cell
, x
, y
, evt
):
42 self
.log
.WriteText('OnCellClicked: %s, (%d %d)\n' % (cell
, x
, y
))
43 self
.base_OnCellClicked(cell
, x
, y
, evt
)
46 # This filter doesn't really do anything but show how to use filters
47 class MyHtmlFilter(wxHtmlFilter
):
48 def __init__(self
, log
):
49 wxHtmlFilter
.__init
__(self
)
52 # This method decides if this filter is able to read the file
53 def CanRead(self
, fsfile
):
54 self
.log
.write("CanRead: %s\n" % fsfile
.GetMimeType())
57 # If CanRead returns True then this method is called to actually
58 # read the file and return the contents.
59 def ReadFile(self
, fsfile
):
63 class TestHtmlPanel(wxPanel
):
64 def __init__(self
, parent
, frame
, log
):
65 wxPanel
.__init
__(self
, parent
, -1, style
=wxNO_FULL_REPAINT_ON_RESIZE
)
68 self
.cwd
= os
.path
.split(sys
.argv
[0])[0]
70 self
.cwd
= os
.getcwd()
72 self
.titleBase
= frame
.GetTitle()
74 wxHtmlWindow_AddFilter(MyHtmlFilter(log
))
76 self
.html
= MyHtmlWindow(self
, -1, log
)
77 self
.html
.SetRelatedFrame(frame
, self
.titleBase
+ " -- %s")
78 self
.html
.SetRelatedStatusBar(0)
80 self
.printer
= wxHtmlEasyPrinting()
82 self
.box
= wxBoxSizer(wxVERTICAL
)
83 self
.box
.Add(self
.html
, 1, wxGROW
)
85 subbox
= wxBoxSizer(wxHORIZONTAL
)
87 btn
= wxButton(self
, -1, "Load File")
88 EVT_BUTTON(self
, btn
.GetId(), self
.OnLoadFile
)
89 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
91 btn
= wxButton(self
, -1, "Load URL")
92 EVT_BUTTON(self
, btn
.GetId(), self
.OnLoadURL
)
93 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
95 btn
= wxButton(self
, -1, "With Widgets")
96 EVT_BUTTON(self
, btn
.GetId(), self
.OnWithWidgets
)
97 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
99 btn
= wxButton(self
, -1, "Back")
100 EVT_BUTTON(self
, btn
.GetId(), self
.OnBack
)
101 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
103 btn
= wxButton(self
, -1, "Forward")
104 EVT_BUTTON(self
, btn
.GetId(), self
.OnForward
)
105 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
107 btn
= wxButton(self
, -1, "Print")
108 EVT_BUTTON(self
, btn
.GetId(), self
.OnPrint
)
109 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
111 btn
= wxButton(self
, -1, "View Source")
112 EVT_BUTTON(self
, btn
.GetId(), self
.OnViewSource
)
113 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
115 self
.box
.Add(subbox
, 0, wxGROW
)
116 self
.SetSizer(self
.box
)
117 self
.SetAutoLayout(True)
119 # A button with this ID is created on the widget test page.
120 EVT_BUTTON(self
, wxID_OK
, self
.OnOk
)
122 self
.OnShowDefault(None)
125 def ShutdownDemo(self
):
126 # put the frame title back
128 self
.frame
.SetTitle(self
.titleBase
)
131 def OnShowDefault(self
, event
):
132 name
= os
.path
.join(self
.cwd
, opj('data/test.htm'))
133 self
.html
.LoadPage(name
)
136 def OnLoadFile(self
, event
):
137 dlg
= wxFileDialog(self
, wildcard
= '*.htm*', style
=wxOPEN
)
140 self
.html
.LoadPage(path
)
144 def OnLoadURL(self
, event
):
145 dlg
= wxTextEntryDialog(self
, "Enter a URL")
148 self
.html
.LoadPage(url
)
152 def OnWithWidgets(self
, event
):
154 name
= os
.path
.join(self
.cwd
, opj('data/widgetTest.htm'))
155 self
.html
.LoadPage(name
)
158 def OnOk(self
, event
):
159 self
.log
.WriteText("It works!\n")
161 def OnBack(self
, event
):
162 if not self
.html
.HistoryBack():
163 wxMessageBox("No more items in history!")
166 def OnForward(self
, event
):
167 if not self
.html
.HistoryForward():
168 wxMessageBox("No more items in history!")
171 def OnViewSource(self
, event
):
172 from wxPython
.lib
.dialogs
import wxScrolledMessageDialog
173 source
= self
.html
.GetParser().GetSource()
174 dlg
= wxScrolledMessageDialog(self
, source
, 'HTML Source')
179 def OnPrint(self
, event
):
180 ##self.printer.GetPageSetupData().SetMarginTopLeft((100,100))
181 self
.printer
.PrintFile(self
.html
.GetOpenedPage())
183 #----------------------------------------------------------------------
185 def runTest(frame
, nb
, log
):
186 win
= TestHtmlPanel(nb
, frame
, log
)
187 print wxWindow_FindFocus()
191 #----------------------------------------------------------------------
197 overview
= """<html><body>
198 <h2>wxHtmlWindow</h2>
200 <p>wxHtmlWindow 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 try http://www.mozilla.org - there's a
205 chance you'll be able to make their widget wxWindows-compatible. I'm
206 sure everyone will enjoy your work in that case...
213 if __name__
== '__main__':
216 run
.main(['', os
.path
.basename(sys
.argv
[0])])