]>
Commit | Line | Data |
---|---|---|
ec3e670f | 1 | |
e166644c RD |
2 | import sys, os |
3 | ||
4 | from wxPython.wx import * | |
5 | from wxPython.html import * | |
e166644c | 6 | import wxPython.lib.wxpTag |
ec3e670f | 7 | |
6c5ae2d2 RD |
8 | from Main import opj |
9 | ||
ec3e670f RD |
10 | #---------------------------------------------------------------------- |
11 | ||
12 | # This shows how to catch the OnLinkClicked non-event. (It's a virtual | |
13 | # method in the C++ code...) | |
14 | class MyHtmlWindow(wxHtmlWindow): | |
15 | def __init__(self, parent, id, log): | |
fe953afb | 16 | wxHtmlWindow.__init__(self, parent, id, style=wxNO_FULL_REPAINT_ON_RESIZE) |
ec3e670f | 17 | self.log = log |
c368d904 RD |
18 | EVT_SCROLLWIN( self, self.OnScroll ) |
19 | ||
20 | def OnScroll( self, event ): | |
a1bfae9d RD |
21 | #print 'event.GetOrientation()',event.GetOrientation() |
22 | #print 'event.GetPosition()',event.GetPosition() | |
c368d904 | 23 | event.Skip() |
ec3e670f | 24 | |
9c00cfa3 RD |
25 | |
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 RD |
46 | # This filter doesn't really do anything but show how to use filters |
47 | class MyHtmlFilter(wxHtmlFilter): | |
48 | def __init__(self, log): | |
49 | wxHtmlFilter.__init__(self) | |
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 | ||
ec3e670f RD |
63 | class TestHtmlPanel(wxPanel): |
64 | def __init__(self, parent, frame, log): | |
fe953afb | 65 | wxPanel.__init__(self, parent, -1, style=wxNO_FULL_REPAINT_ON_RESIZE) |
ec3e670f RD |
66 | self.log = log |
67 | self.frame = frame | |
ae920857 RD |
68 | self.cwd = os.path.split(sys.argv[0])[0] |
69 | if not self.cwd: | |
70 | self.cwd = os.getcwd() | |
1e4a197e RD |
71 | if frame: |
72 | self.titleBase = frame.GetTitle() | |
73 | ||
74 | wxHtmlWindow_AddFilter(MyHtmlFilter(log)) | |
ec3e670f | 75 | |
ec3e670f | 76 | self.html = MyHtmlWindow(self, -1, log) |
1e4a197e | 77 | self.html.SetRelatedFrame(frame, self.titleBase + " -- %s") |
ec3e670f RD |
78 | self.html.SetRelatedStatusBar(0) |
79 | ||
65dd82cb RD |
80 | self.printer = wxHtmlEasyPrinting() |
81 | ||
2f90df85 RD |
82 | self.box = wxBoxSizer(wxVERTICAL) |
83 | self.box.Add(self.html, 1, wxGROW) | |
ec3e670f RD |
84 | |
85 | subbox = wxBoxSizer(wxHORIZONTAL) | |
ec3e670f | 86 | |
a1bfae9d RD |
87 | btn = wxButton(self, -1, "Load File") |
88 | EVT_BUTTON(self, btn.GetId(), self.OnLoadFile) | |
2f90df85 | 89 | subbox.Add(btn, 1, wxGROW | wxALL, 2) |
ec3e670f | 90 | |
a1bfae9d RD |
91 | btn = wxButton(self, -1, "Load URL") |
92 | EVT_BUTTON(self, btn.GetId(), self.OnLoadURL) | |
2f90df85 | 93 | subbox.Add(btn, 1, wxGROW | wxALL, 2) |
ec3e670f | 94 | |
a1bfae9d RD |
95 | btn = wxButton(self, -1, "With Widgets") |
96 | EVT_BUTTON(self, btn.GetId(), self.OnWithWidgets) | |
2f90df85 | 97 | subbox.Add(btn, 1, wxGROW | wxALL, 2) |
ec3e670f | 98 | |
a1bfae9d RD |
99 | btn = wxButton(self, -1, "Back") |
100 | EVT_BUTTON(self, btn.GetId(), self.OnBack) | |
2f90df85 | 101 | subbox.Add(btn, 1, wxGROW | wxALL, 2) |
ec3e670f | 102 | |
a1bfae9d RD |
103 | btn = wxButton(self, -1, "Forward") |
104 | EVT_BUTTON(self, btn.GetId(), self.OnForward) | |
65dd82cb RD |
105 | subbox.Add(btn, 1, wxGROW | wxALL, 2) |
106 | ||
a1bfae9d RD |
107 | btn = wxButton(self, -1, "Print") |
108 | EVT_BUTTON(self, btn.GetId(), self.OnPrint) | |
109 | subbox.Add(btn, 1, wxGROW | wxALL, 2) | |
110 | ||
111 | btn = wxButton(self, -1, "View Source") | |
112 | EVT_BUTTON(self, btn.GetId(), self.OnViewSource) | |
2f90df85 | 113 | subbox.Add(btn, 1, wxGROW | wxALL, 2) |
e166644c | 114 | |
2f90df85 RD |
115 | self.box.Add(subbox, 0, wxGROW) |
116 | self.SetSizer(self.box) | |
1e4a197e | 117 | self.SetAutoLayout(True) |
e166644c RD |
118 | |
119 | # A button with this ID is created on the widget test page. | |
120 | EVT_BUTTON(self, wxID_OK, self.OnOk) | |
121 | ||
ec3e670f RD |
122 | self.OnShowDefault(None) |
123 | ||
124 | ||
1e4a197e RD |
125 | def ShutdownDemo(self): |
126 | # put the frame title back | |
127 | if self.frame: | |
128 | self.frame.SetTitle(self.titleBase) | |
129 | ||
ec3e670f RD |
130 | |
131 | def OnShowDefault(self, event): | |
6c5ae2d2 | 132 | name = os.path.join(self.cwd, opj('data/test.htm')) |
e166644c | 133 | self.html.LoadPage(name) |
ec3e670f RD |
134 | |
135 | ||
136 | def OnLoadFile(self, event): | |
e166644c RD |
137 | dlg = wxFileDialog(self, wildcard = '*.htm*', style=wxOPEN) |
138 | if dlg.ShowModal(): | |
139 | path = dlg.GetPath() | |
140 | self.html.LoadPage(path) | |
141 | dlg.Destroy() | |
ec3e670f RD |
142 | |
143 | ||
a1bfae9d RD |
144 | def OnLoadURL(self, event): |
145 | dlg = wxTextEntryDialog(self, "Enter a URL") | |
146 | if dlg.ShowModal(): | |
147 | url = dlg.GetValue() | |
148 | self.html.LoadPage(url) | |
149 | dlg.Destroy() | |
150 | ||
151 | ||
ec3e670f | 152 | def OnWithWidgets(self, event): |
ae920857 | 153 | os.chdir(self.cwd) |
6c5ae2d2 | 154 | name = os.path.join(self.cwd, opj('data/widgetTest.htm')) |
e166644c | 155 | self.html.LoadPage(name) |
ae920857 | 156 | |
e166644c RD |
157 | |
158 | def OnOk(self, event): | |
159 | self.log.WriteText("It works!\n") | |
ec3e670f RD |
160 | |
161 | def OnBack(self, event): | |
162 | if not self.html.HistoryBack(): | |
163 | wxMessageBox("No more items in history!") | |
164 | ||
e166644c | 165 | |
ec3e670f RD |
166 | def OnForward(self, event): |
167 | if not self.html.HistoryForward(): | |
168 | wxMessageBox("No more items in history!") | |
169 | ||
170 | ||
e166644c RD |
171 | def OnViewSource(self, event): |
172 | from wxPython.lib.dialogs import wxScrolledMessageDialog | |
173 | source = self.html.GetParser().GetSource() | |
174 | dlg = wxScrolledMessageDialog(self, source, 'HTML Source') | |
175 | dlg.ShowModal() | |
176 | dlg.Destroy() | |
177 | ||
178 | ||
65dd82cb | 179 | def OnPrint(self, event): |
2f9be787 | 180 | ##self.printer.GetPageSetupData().SetMarginTopLeft((100,100)) |
65dd82cb RD |
181 | self.printer.PrintFile(self.html.GetOpenedPage()) |
182 | ||
ec3e670f RD |
183 | #---------------------------------------------------------------------- |
184 | ||
185 | def runTest(frame, nb, log): | |
186 | win = TestHtmlPanel(nb, frame, log) | |
f9b24f07 | 187 | print wxWindow_FindFocus() |
ec3e670f RD |
188 | return win |
189 | ||
190 | ||
191 | #---------------------------------------------------------------------- | |
192 | ||
193 | ||
194 | ||
195 | ||
196 | ||
1e4a197e RD |
197 | overview = """<html><body> |
198 | <h2>wxHtmlWindow</h2> | |
ec3e670f | 199 | |
1e4a197e RD |
200 | <p>wxHtmlWindow is capable of parsing and rendering most |
201 | simple HTML tags. | |
ec3e670f | 202 | |
1e4a197e RD |
203 | <p>It is not intended to be a high-end HTML browser. If you're |
204 | looking for something like that try http://www.mozilla.org - there's a | |
205 | chance you'll be able to make their widget wxWindows-compatible. I'm | |
206 | sure everyone will enjoy your work in that case... | |
207 | ||
208 | </body></html> | |
ec3e670f RD |
209 | """ |
210 | ||
e166644c RD |
211 | |
212 | ||
1e4a197e RD |
213 | if __name__ == '__main__': |
214 | import sys,os | |
215 | import run | |
216 | run.main(['', os.path.basename(sys.argv[0])]) | |
217 | ||
e166644c RD |
218 | |
219 | ||
e166644c RD |
220 | |
221 | ||
222 | ||
223 | ||
224 |