]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxHtmlWindow.py
reworked vertical scrolling code to correspond better to what I think is correct...
[wxWidgets.git] / wxPython / demo / wxHtmlWindow.py
1
2 import sys, os
3
4 from wxPython.wx import *
5 from wxPython.html import *
6 import wxPython.lib.wxpTag
7
8 from Main import opj
9
10 #----------------------------------------------------------------------
11
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)
17 self.log = log
18 EVT_SCROLLWIN( self, self.OnScroll )
19
20 def OnScroll( self, event ):
21 #print 'event.GetOrientation()',event.GetOrientation()
22 #print 'event.GetPosition()',event.GetPosition()
23 event.Skip()
24
25
26 def OnLinkClicked(self, linkinfo):
27 self.log.WriteText('OnLinkClicked: %s\n' % linkinfo.GetHref())
28
29 # Virtuals in the base class have been renamed with base_ on the front.
30 self.base_OnLinkClicked(linkinfo)
31
32
33 def OnSetTitle(self, title):
34 self.log.WriteText('OnSetTitle: %s\n' % title)
35 self.base_OnSetTitle(title)
36
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)
40
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)
44
45
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)
50 self.log = log
51
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())
55 return False
56
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):
60 return ""
61
62
63 class TestHtmlPanel(wxPanel):
64 def __init__(self, parent, frame, log):
65 wxPanel.__init__(self, parent, -1, style=wxNO_FULL_REPAINT_ON_RESIZE)
66 self.log = log
67 self.frame = frame
68 self.cwd = os.path.split(sys.argv[0])[0]
69 if not self.cwd:
70 self.cwd = os.getcwd()
71 if frame:
72 self.titleBase = frame.GetTitle()
73
74 wxHtmlWindow_AddFilter(MyHtmlFilter(log))
75
76 self.html = MyHtmlWindow(self, -1, log)
77 self.html.SetRelatedFrame(frame, self.titleBase + " -- %s")
78 self.html.SetRelatedStatusBar(0)
79
80 self.printer = wxHtmlEasyPrinting()
81
82 self.box = wxBoxSizer(wxVERTICAL)
83 self.box.Add(self.html, 1, wxGROW)
84
85 subbox = wxBoxSizer(wxHORIZONTAL)
86
87 btn = wxButton(self, -1, "Load File")
88 EVT_BUTTON(self, btn.GetId(), self.OnLoadFile)
89 subbox.Add(btn, 1, wxGROW | wxALL, 2)
90
91 btn = wxButton(self, -1, "Load URL")
92 EVT_BUTTON(self, btn.GetId(), self.OnLoadURL)
93 subbox.Add(btn, 1, wxGROW | wxALL, 2)
94
95 btn = wxButton(self, -1, "With Widgets")
96 EVT_BUTTON(self, btn.GetId(), self.OnWithWidgets)
97 subbox.Add(btn, 1, wxGROW | wxALL, 2)
98
99 btn = wxButton(self, -1, "Back")
100 EVT_BUTTON(self, btn.GetId(), self.OnBack)
101 subbox.Add(btn, 1, wxGROW | wxALL, 2)
102
103 btn = wxButton(self, -1, "Forward")
104 EVT_BUTTON(self, btn.GetId(), self.OnForward)
105 subbox.Add(btn, 1, wxGROW | wxALL, 2)
106
107 btn = wxButton(self, -1, "Print")
108 EVT_BUTTON(self, btn.GetId(), self.OnPrint)
109 subbox.Add(btn, 1, wxGROW | wxALL, 2)
110
111 btn = wxButton(self, -1, "View Source")
112 EVT_BUTTON(self, btn.GetId(), self.OnViewSource)
113 subbox.Add(btn, 1, wxGROW | wxALL, 2)
114
115 self.box.Add(subbox, 0, wxGROW)
116 self.SetSizer(self.box)
117 self.SetAutoLayout(True)
118
119 # A button with this ID is created on the widget test page.
120 EVT_BUTTON(self, wxID_OK, self.OnOk)
121
122 self.OnShowDefault(None)
123
124
125 def ShutdownDemo(self):
126 # put the frame title back
127 if self.frame:
128 self.frame.SetTitle(self.titleBase)
129
130
131 def OnShowDefault(self, event):
132 name = os.path.join(self.cwd, opj('data/test.htm'))
133 self.html.LoadPage(name)
134
135
136 def OnLoadFile(self, event):
137 dlg = wxFileDialog(self, wildcard = '*.htm*', style=wxOPEN)
138 if dlg.ShowModal():
139 path = dlg.GetPath()
140 self.html.LoadPage(path)
141 dlg.Destroy()
142
143
144 def OnLoadURL(self, event):
145 dlg = wxTextEntryDialog(self, "Enter a URL")
146 if dlg.ShowModal():
147 url = dlg.GetValue()
148 self.html.LoadPage(url)
149 dlg.Destroy()
150
151
152 def OnWithWidgets(self, event):
153 os.chdir(self.cwd)
154 name = os.path.join(self.cwd, opj('data/widgetTest.htm'))
155 self.html.LoadPage(name)
156
157
158 def OnOk(self, event):
159 self.log.WriteText("It works!\n")
160
161 def OnBack(self, event):
162 if not self.html.HistoryBack():
163 wxMessageBox("No more items in history!")
164
165
166 def OnForward(self, event):
167 if not self.html.HistoryForward():
168 wxMessageBox("No more items in history!")
169
170
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')
175 dlg.ShowModal()
176 dlg.Destroy()
177
178
179 def OnPrint(self, event):
180 ##self.printer.GetPageSetupData().SetMarginTopLeft((100,100))
181 self.printer.PrintFile(self.html.GetOpenedPage())
182
183 #----------------------------------------------------------------------
184
185 def runTest(frame, nb, log):
186 win = TestHtmlPanel(nb, frame, log)
187 print wxWindow_FindFocus()
188 return win
189
190
191 #----------------------------------------------------------------------
192
193
194
195
196
197 overview = """<html><body>
198 <h2>wxHtmlWindow</h2>
199
200 <p>wxHtmlWindow is capable of parsing and rendering most
201 simple HTML tags.
202
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...
207
208 </body></html>
209 """
210
211
212
213 if __name__ == '__main__':
214 import sys,os
215 import run
216 run.main(['', os.path.basename(sys.argv[0])])
217
218
219
220
221
222
223
224