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