4 from wxPython
.wx
import *
5 from wxPython
.html
import *
6 import wxPython
.lib
.wxpTag
12 #----------------------------------------------------------------------
14 # This shows how to catch the OnLinkClicked non-event. (It's a virtual
15 # method in the C++ code...)
16 class MyHtmlWindow(wxHtmlWindow
):
17 def __init__(self
, parent
, id, log
):
18 wxHtmlWindow
.__init
__(self
, parent
, id, style
=wxNO_FULL_REPAINT_ON_RESIZE
)
20 EVT_SCROLLWIN( self
, self
.OnScroll
)
22 def OnScroll( self
, event
):
23 #print 'event.GetOrientation()',event.GetOrientation()
24 #print 'event.GetPosition()',event.GetPosition()
28 def OnLinkClicked(self
, linkinfo
):
29 self
.log
.WriteText('OnLinkClicked: %s\n' % linkinfo
.GetHref())
31 # Virtuals in the base class have been renamed with base_ on the front.
32 self
.base_OnLinkClicked(linkinfo
)
35 def OnSetTitle(self
, title
):
36 self
.log
.WriteText('OnSetTitle: %s\n' % title
)
37 self
.base_OnSetTitle(title
)
39 def OnCellMouseHover(self
, cell
, x
, y
):
40 self
.log
.WriteText('OnCellMouseHover: %s, (%d %d)\n' % (cell
, x
, y
))
41 self
.base_OnCellMouseHover(cell
, x
, y
)
43 def OnCellClicked(self
, cell
, x
, y
, evt
):
44 self
.log
.WriteText('OnCellClicked: %s, (%d %d)\n' % (cell
, x
, y
))
45 self
.base_OnCellClicked(cell
, x
, y
, evt
)
48 # This filter doesn't really do anything but show how to use filters
49 class MyHtmlFilter(wxHtmlFilter
):
50 def __init__(self
, log
):
51 wxHtmlFilter
.__init
__(self
)
54 # This method decides if this filter is able to read the file
55 def CanRead(self
, fsfile
):
56 self
.log
.write("CanRead: %s\n" % fsfile
.GetMimeType())
59 # If CanRead returns True then this method is called to actually
60 # read the file and return the contents.
61 def ReadFile(self
, fsfile
):
65 class TestHtmlPanel(wxPanel
):
66 def __init__(self
, parent
, frame
, log
):
67 wxPanel
.__init
__(self
, parent
, -1, style
=wxNO_FULL_REPAINT_ON_RESIZE
)
70 self
.cwd
= os
.path
.split(sys
.argv
[0])[0]
72 self
.cwd
= os
.getcwd()
74 self
.titleBase
= frame
.GetTitle()
76 wxHtmlWindow_AddFilter(MyHtmlFilter(log
))
78 self
.html
= MyHtmlWindow(self
, -1, log
)
79 self
.html
.SetRelatedFrame(frame
, self
.titleBase
+ " -- %s")
80 self
.html
.SetRelatedStatusBar(0)
82 self
.printer
= wxHtmlEasyPrinting()
84 self
.box
= wxBoxSizer(wxVERTICAL
)
85 self
.box
.Add(self
.html
, 1, wxGROW
)
87 subbox
= wxBoxSizer(wxHORIZONTAL
)
89 btn
= wxButton(self
, -1, "Load File")
90 EVT_BUTTON(self
, btn
.GetId(), self
.OnLoadFile
)
91 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
93 btn
= wxButton(self
, -1, "Load URL")
94 EVT_BUTTON(self
, btn
.GetId(), self
.OnLoadURL
)
95 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
97 btn
= wxButton(self
, -1, "With Widgets")
98 EVT_BUTTON(self
, btn
.GetId(), self
.OnWithWidgets
)
99 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
101 btn
= wxButton(self
, -1, "Back")
102 EVT_BUTTON(self
, btn
.GetId(), self
.OnBack
)
103 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
105 btn
= wxButton(self
, -1, "Forward")
106 EVT_BUTTON(self
, btn
.GetId(), self
.OnForward
)
107 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
109 btn
= wxButton(self
, -1, "Print")
110 EVT_BUTTON(self
, btn
.GetId(), self
.OnPrint
)
111 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
113 btn
= wxButton(self
, -1, "View Source")
114 EVT_BUTTON(self
, btn
.GetId(), self
.OnViewSource
)
115 subbox
.Add(btn
, 1, wxGROW | wxALL
, 2)
117 self
.box
.Add(subbox
, 0, wxGROW
)
118 self
.SetSizer(self
.box
)
119 self
.SetAutoLayout(True)
121 # A button with this ID is created on the widget test page.
122 EVT_BUTTON(self
, wxID_OK
, self
.OnOk
)
124 self
.OnShowDefault(None)
127 def ShutdownDemo(self
):
128 # put the frame title back
130 self
.frame
.SetTitle(self
.titleBase
)
133 def OnShowDefault(self
, event
):
134 name
= os
.path
.join(self
.cwd
, opj('data/test.htm'))
135 self
.html
.LoadPage(name
)
138 def OnLoadFile(self
, event
):
139 dlg
= wxFileDialog(self
, wildcard
= '*.htm*', style
=wxOPEN
)
142 self
.html
.LoadPage(path
)
146 def OnLoadURL(self
, event
):
147 dlg
= wxTextEntryDialog(self
, "Enter a URL")
150 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 wxMessageBox("No more items in history!")
168 def OnForward(self
, event
):
169 if not self
.html
.HistoryForward():
170 wxMessageBox("No more items in history!")
173 def OnViewSource(self
, event
):
174 from wxPython
.lib
.dialogs
import wxScrolledMessageDialog
175 source
= self
.html
.GetParser().GetSource()
176 dlg
= wxScrolledMessageDialog(self
, source
, 'HTML Source')
181 def OnPrint(self
, event
):
182 ##self.printer.GetPageSetupData().SetMarginTopLeft((100,100))
183 self
.printer
.PrintFile(self
.html
.GetOpenedPage())
185 #----------------------------------------------------------------------
187 def runTest(frame
, nb
, log
):
188 win
= TestHtmlPanel(nb
, frame
, log
)
189 print wxWindow_FindFocus()
193 #----------------------------------------------------------------------
199 overview
= """<html><body>
200 <h2>wxHtmlWindow</h2>
202 <p>wxHtmlWindow is capable of parsing and rendering most
205 <p>It is not intended to be a high-end HTML browser. If you're
206 looking for something like that try http://www.mozilla.org - there's a
207 chance you'll be able to make their widget wxWindows-compatible. I'm
208 sure everyone will enjoy your work in that case...
215 if __name__
== '__main__':
218 run
.main(['', os
.path
.basename(sys
.argv
[0])])