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