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