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