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