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