]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxHtmlWindow.py
Tool tweaks and metadata update
[wxWidgets.git] / wxPython / demo / wxHtmlWindow.py
1 # 11/18/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5 # 12/13/2003 - Jeff Grimmett (grimmtooth@softhome.net)
6 #
7 # o got the wxpTag stuff working right.
8 #
9 # 12/18/2003 - Jeff Grimmett (grimmtooth@softhome.net)
10 #
11 # o wxScrolledMessageDialog -> ScrolledMessageDialog
12 #
13
14 import os
15 import sys
16
17 import wx
18 import wx.html as html
19 import wx.lib.wxpTag
20
21 from Main import opj
22
23 #----------------------------------------------------------------------
24
25 # This shows how to catch the OnLinkClicked non-event. (It's a virtual
26 # method in the C++ code...)
27 class MyHtmlWindow(html.HtmlWindow):
28 def __init__(self, parent, id, log):
29 html.HtmlWindow.__init__(self, parent, id, style=wx.NO_FULL_REPAINT_ON_RESIZE)
30 self.log = log
31 self.Bind(wx.EVT_SCROLLWIN, self.OnScroll )
32
33 def OnScroll( self, event ):
34 #print 'event.GetOrientation()',event.GetOrientation()
35 #print 'event.GetPosition()',event.GetPosition()
36 event.Skip()
37
38 def OnLinkClicked(self, linkinfo):
39 self.log.WriteText('OnLinkClicked: %s\n' % linkinfo.GetHref())
40
41 # Virtuals in the base class have been renamed with base_ on the front.
42 self.base_OnLinkClicked(linkinfo)
43
44
45 def OnSetTitle(self, title):
46 self.log.WriteText('OnSetTitle: %s\n' % title)
47 self.base_OnSetTitle(title)
48
49 def OnCellMouseHover(self, cell, x, y):
50 self.log.WriteText('OnCellMouseHover: %s, (%d %d)\n' % (cell, x, y))
51 self.base_OnCellMouseHover(cell, x, y)
52
53 def OnCellClicked(self, cell, x, y, evt):
54 self.log.WriteText('OnCellClicked: %s, (%d %d)\n' % (cell, x, y))
55 self.base_OnCellClicked(cell, x, y, evt)
56
57
58 # This filter doesn't really do anything but show how to use filters
59 class MyHtmlFilter(html.HtmlFilter):
60 def __init__(self, log):
61 html.HtmlFilter.__init__(self)
62 self.log = log
63
64 # This method decides if this filter is able to read the file
65 def CanRead(self, fsfile):
66 self.log.write("CanRead: %s\n" % fsfile.GetMimeType())
67 return False
68
69 # If CanRead returns True then this method is called to actually
70 # read the file and return the contents.
71 def ReadFile(self, fsfile):
72 return ""
73
74
75 class TestHtmlPanel(wx.Panel):
76 def __init__(self, parent, frame, log):
77 wx.Panel.__init__(self, parent, -1, style=wx.NO_FULL_REPAINT_ON_RESIZE)
78 self.log = log
79 self.frame = frame
80 self.cwd = os.path.split(sys.argv[0])[0]
81
82 if not self.cwd:
83 self.cwd = os.getcwd()
84 if frame:
85 self.titleBase = frame.GetTitle()
86
87 html.HtmlWindow_AddFilter(MyHtmlFilter(log))
88
89 self.html = MyHtmlWindow(self, -1, log)
90 self.html.SetRelatedFrame(frame, self.titleBase + " -- %s")
91 self.html.SetRelatedStatusBar(0)
92
93 self.printer = html.HtmlEasyPrinting()
94
95 self.box = wx.BoxSizer(wx.VERTICAL)
96 self.box.Add(self.html, 1, wx.GROW)
97
98 subbox = wx.BoxSizer(wx.HORIZONTAL)
99
100 btn = wx.Button(self, -1, "Load File")
101 self.Bind(wx.EVT_BUTTON, self.OnLoadFile, btn)
102 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
103
104 btn = wx.Button(self, -1, "Load URL")
105 self.Bind(wx.EVT_BUTTON, self.OnLoadURL, btn)
106 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
107
108 btn = wx.Button(self, -1, "With Widgets")
109 self.Bind(wx.EVT_BUTTON, self.OnWithWidgets, btn)
110 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
111
112 btn = wx.Button(self, -1, "Back")
113 self.Bind(wx.EVT_BUTTON, self.OnBack, btn)
114 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
115
116 btn = wx.Button(self, -1, "Forward")
117 self.Bind(wx.EVT_BUTTON, self.OnForward, btn)
118 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
119
120 btn = wx.Button(self, -1, "Print")
121 self.Bind(wx.EVT_BUTTON, self.OnPrint, btn)
122 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
123
124 btn = wx.Button(self, -1, "View Source")
125 self.Bind(wx.EVT_BUTTON, self.OnViewSource, btn)
126 subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
127
128 self.box.Add(subbox, 0, wx.GROW)
129 self.SetSizer(self.box)
130 self.SetAutoLayout(True)
131
132 # A button with this ID is created on the widget test page.
133 self.Bind(wx.EVT_BUTTON, self.OnOk, id=wx.ID_OK)
134
135 self.OnShowDefault(None)
136
137
138 def ShutdownDemo(self):
139 # put the frame title back
140 if self.frame:
141 self.frame.SetTitle(self.titleBase)
142
143
144 def OnShowDefault(self, event):
145 name = os.path.join(self.cwd, opj('data/test.htm'))
146 self.html.LoadPage(name)
147
148
149 def OnLoadFile(self, event):
150 dlg = wx.FileDialog(self, wildcard = '*.htm*', style=wx.OPEN)
151
152 if dlg.ShowModal():
153 path = dlg.GetPath()
154 self.html.LoadPage(path)
155
156 dlg.Destroy()
157
158
159 def OnLoadURL(self, event):
160 dlg = wx.TextEntryDialog(self, "Enter a URL")
161
162 if dlg.ShowModal():
163 url = dlg.GetValue()
164 self.html.LoadPage(url)
165
166 dlg.Destroy()
167
168
169 def OnWithWidgets(self, event):
170 os.chdir(self.cwd)
171 name = os.path.join(self.cwd, opj('data/widgetTest.htm'))
172 self.html.LoadPage(name)
173
174
175 def OnOk(self, event):
176 self.log.WriteText("It works!\n")
177
178 def OnBack(self, event):
179 if not self.html.HistoryBack():
180 wx.MessageBox("No more items in history!")
181
182
183 def OnForward(self, event):
184 if not self.html.HistoryForward():
185 wx.MessageBox("No more items in history!")
186
187
188 def OnViewSource(self, event):
189 import wx.lib.dialogs
190
191 source = self.html.GetParser().GetSource()
192
193 dlg = wx.lib.dialogs.ScrolledMessageDialog(self, source, 'HTML Source')
194 dlg.ShowModal()
195 dlg.Destroy()
196
197
198 def OnPrint(self, event):
199 ##self.printer.GetPageSetupData().SetMarginTopLeft((100,100))
200 self.printer.PrintFile(self.html.GetOpenedPage())
201
202 #----------------------------------------------------------------------
203
204 def runTest(frame, nb, log):
205 win = TestHtmlPanel(nb, frame, log)
206 print wx.Window_FindFocus()
207 return win
208
209
210 #----------------------------------------------------------------------
211
212
213 overview = """<html><body>
214 <h2>wxHtmlWindow</h2>
215
216 <p>wxHtmlWindow is capable of parsing and rendering most
217 simple HTML tags.
218
219 <p>It is not intended to be a high-end HTML browser. If you're
220 looking for something like that see the IEHtmlWin class, which
221 wraps the core MSIE HTML viewer.
222
223 </body></html>
224 """
225
226
227
228 if __name__ == '__main__':
229 import sys,os
230 import run
231 run.main(['', os.path.basename(sys.argv[0])])
232
233
234
235
236
237
238
239