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