]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/HtmlWindow.py
The great wxVScrolledWindow refactoring: allow using it both horizontal and
[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))
f0879a0a
RD
36 if isinstance(cell, html.HtmlWordCell):
37 sel = html.HtmlSelection()
38 self.log.WriteText(' %s\n' % cell.ConvertToText(sel))
a7a01418
RD
39 super(MyHtmlWindow, self).OnCellClicked(cell, x, y, evt)
40
ec3e670f
RD
41
42
1e4a197e 43# This filter doesn't really do anything but show how to use filters
8fa876ca 44class MyHtmlFilter(html.HtmlFilter):
1e4a197e 45 def __init__(self, log):
8fa876ca 46 html.HtmlFilter.__init__(self)
1e4a197e
RD
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
8fa876ca 60class TestHtmlPanel(wx.Panel):
ec3e670f 61 def __init__(self, parent, frame, log):
8fa876ca 62 wx.Panel.__init__(self, parent, -1, style=wx.NO_FULL_REPAINT_ON_RESIZE)
ec3e670f
RD
63 self.log = log
64 self.frame = frame
ae920857 65 self.cwd = os.path.split(sys.argv[0])[0]
8fa876ca 66
ae920857
RD
67 if not self.cwd:
68 self.cwd = os.getcwd()
1e4a197e
RD
69 if frame:
70 self.titleBase = frame.GetTitle()
71
8fa876ca 72 html.HtmlWindow_AddFilter(MyHtmlFilter(log))
ec3e670f 73
ec3e670f 74 self.html = MyHtmlWindow(self, -1, log)
1e4a197e 75 self.html.SetRelatedFrame(frame, self.titleBase + " -- %s")
ec3e670f
RD
76 self.html.SetRelatedStatusBar(0)
77
8fa876ca 78 self.printer = html.HtmlEasyPrinting()
65dd82cb 79
8fa876ca
RD
80 self.box = wx.BoxSizer(wx.VERTICAL)
81 self.box.Add(self.html, 1, wx.GROW)
ec3e670f 82
8fa876ca 83 subbox = wx.BoxSizer(wx.HORIZONTAL)
ec3e670f 84
8fa876ca
RD
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)
ec3e670f 88
8fa876ca
RD
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)
ec3e670f 92
8fa876ca
RD
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)
ec3e670f 96
8fa876ca
RD
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)
ec3e670f 100
8fa876ca
RD
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)
65dd82cb 104
8fa876ca
RD
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)
a1bfae9d 108
8fa876ca
RD
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)
e166644c 112
8fa876ca 113 self.box.Add(subbox, 0, wx.GROW)
2f90df85 114 self.SetSizer(self.box)
1e4a197e 115 self.SetAutoLayout(True)
e166644c
RD
116
117 # A button with this ID is created on the widget test page.
8fa876ca 118 self.Bind(wx.EVT_BUTTON, self.OnOk, id=wx.ID_OK)
e166644c 119
ec3e670f
RD
120 self.OnShowDefault(None)
121
122
1e4a197e
RD
123 def ShutdownDemo(self):
124 # put the frame title back
125 if self.frame:
126 self.frame.SetTitle(self.titleBase)
127
ec3e670f
RD
128
129 def OnShowDefault(self, event):
6c5ae2d2 130 name = os.path.join(self.cwd, opj('data/test.htm'))
e166644c 131 self.html.LoadPage(name)
ec3e670f
RD
132
133
134 def OnLoadFile(self, event):
8fa876ca
RD
135 dlg = wx.FileDialog(self, wildcard = '*.htm*', style=wx.OPEN)
136
e166644c
RD
137 if dlg.ShowModal():
138 path = dlg.GetPath()
139 self.html.LoadPage(path)
8fa876ca 140
e166644c 141 dlg.Destroy()
ec3e670f
RD
142
143
a1bfae9d 144 def OnLoadURL(self, event):
8fa876ca
RD
145 dlg = wx.TextEntryDialog(self, "Enter a URL")
146
a1bfae9d
RD
147 if dlg.ShowModal():
148 url = dlg.GetValue()
149 self.html.LoadPage(url)
8fa876ca 150
a1bfae9d
RD
151 dlg.Destroy()
152
153
ec3e670f 154 def OnWithWidgets(self, event):
ae920857 155 os.chdir(self.cwd)
6c5ae2d2 156 name = os.path.join(self.cwd, opj('data/widgetTest.htm'))
e166644c 157 self.html.LoadPage(name)
ae920857 158
e166644c
RD
159
160 def OnOk(self, event):
161 self.log.WriteText("It works!\n")
ec3e670f
RD
162
163 def OnBack(self, event):
164 if not self.html.HistoryBack():
8fa876ca 165 wx.MessageBox("No more items in history!")
ec3e670f 166
e166644c 167
ec3e670f
RD
168 def OnForward(self, event):
169 if not self.html.HistoryForward():
8fa876ca 170 wx.MessageBox("No more items in history!")
ec3e670f
RD
171
172
e166644c 173 def OnViewSource(self, event):
33785d9f 174 import wx.lib.dialogs
8fa876ca 175
e166644c 176 source = self.html.GetParser().GetSource()
8fa876ca 177
33785d9f 178 dlg = wx.lib.dialogs.ScrolledMessageDialog(self, source, 'HTML Source')
e166644c
RD
179 dlg.ShowModal()
180 dlg.Destroy()
181
182
65dd82cb 183 def OnPrint(self, event):
72d3f316 184 self.printer.GetPrintData().SetPaperId(wx.PAPER_LETTER)
65dd82cb
RD
185 self.printer.PrintFile(self.html.GetOpenedPage())
186
ec3e670f
RD
187#----------------------------------------------------------------------
188
189def runTest(frame, nb, log):
190 win = TestHtmlPanel(nb, frame, log)
191 return win
192
193
194#----------------------------------------------------------------------
195
196
1e4a197e 197overview = """<html><body>
95bfd958 198<h2>wx.HtmlWindow</h2>
ec3e670f 199
95bfd958 200<p>wx.HtmlWindow is capable of parsing and rendering most
1e4a197e 201simple HTML tags.
ec3e670f 202
1e4a197e 203<p>It is not intended to be a high-end HTML browser. If you're
8fa876ca
RD
204looking for something like that see the IEHtmlWin class, which
205wraps the core MSIE HTML viewer.
1e4a197e
RD
206
207</body></html>
ec3e670f
RD
208"""
209
e166644c
RD
210
211
1e4a197e
RD
212if __name__ == '__main__':
213 import sys,os
214 import run
8eca4fef 215 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
1e4a197e 216
e166644c
RD
217
218
e166644c
RD
219
220
221
222
223