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