]>
Commit | Line | Data |
---|---|---|
f6bcfd97 BP |
1 | """ |
2 | <html><body> | |
3 | This demo shows how to embed an ActiveX control in a wxPython | |
4 | application, (Win32 only.) | |
5 | ||
6 | <p> | |
7 | The MakeActiveXClass function dynamically builds a new Class on the | |
8 | fly, that has the same signature and semantics as wxWindow. This | |
9 | means that when you call the function you get back a new class that | |
10 | you can use just like wxWindow, (set the size and position, use in a | |
11 | sizer, etc.) except its contents will be the COM control. | |
12 | ||
13 | <p> | |
14 | This demo embeds the Internet Exploer WebBrowser control, and shows | |
15 | how to receive events from the COM control. (The title bar and status | |
16 | bar are updated as pages change, in addition to the log messages being | |
17 | shown.) | |
18 | </body></html> | |
19 | """ | |
20 | ||
21 | from wxPython.wx import * | |
22 | ||
23 | if wxPlatform == '__WXMSW__': | |
24 | from wxPython.lib.activexwrapper import MakeActiveXClass | |
25 | import win32com.client.gencache | |
26 | ||
27 | try: | |
28 | browserModule = win32com.client.gencache.EnsureModule("{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1) | |
29 | except: | |
30 | raise ImportError("IE4 or greater does not appear to be installed.") | |
31 | ||
32 | ||
33 | #---------------------------------------------------------------------- | |
34 | ||
35 | class TestPanel(wxWindow): | |
36 | def __init__(self, parent, log, frame=None): | |
c2dac736 RD |
37 | wxWindow.__init__(self, parent, -1, |
38 | style=wxCLIP_CHILDREN|wxNO_FULL_REPAINT_ON_RESIZE) | |
f6bcfd97 BP |
39 | self.ie = None |
40 | self.log = log | |
c368d904 | 41 | self.current = "http://wxPython.org/" |
f6bcfd97 BP |
42 | self.frame = frame |
43 | if frame: | |
44 | self.titleBase = frame.GetTitle() | |
45 | ||
46 | ||
47 | sizer = wxBoxSizer(wxVERTICAL) | |
48 | btnSizer = wxBoxSizer(wxHORIZONTAL) | |
49 | ||
50 | # Make a new class that derives from the WebBrowser class in the | |
51 | # COM module imported above. This class also derives from wxWindow and | |
52 | # implements the machinery needed to integrate the two worlds. | |
53 | theClass = MakeActiveXClass(browserModule.WebBrowser, | |
54 | eventObj = self) | |
55 | ||
56 | # Create an instance of that class | |
c2dac736 | 57 | self.ie = theClass(self, -1) ##, style=wxSUNKEN_BORDER) |
f6bcfd97 BP |
58 | |
59 | ||
c2dac736 RD |
60 | btn = wxButton(self, wxNewId(), "Open", style=wxBU_EXACTFIT) |
61 | EVT_BUTTON(self, btn.GetId(), self.OnOpenButton) | |
62 | btnSizer.Add(btn, 0, wxEXPAND|wxALL, 2) | |
f6bcfd97 | 63 | |
c2dac736 | 64 | btn = wxButton(self, wxNewId(), "Home", style=wxBU_EXACTFIT) |
f6bcfd97 | 65 | EVT_BUTTON(self, btn.GetId(), self.OnHomeButton) |
c2dac736 | 66 | btnSizer.Add(btn, 0, wxEXPAND|wxALL, 2) |
f6bcfd97 | 67 | |
c2dac736 | 68 | btn = wxButton(self, wxNewId(), "<--", style=wxBU_EXACTFIT) |
f6bcfd97 | 69 | EVT_BUTTON(self, btn.GetId(), self.OnPrevPageButton) |
c2dac736 | 70 | btnSizer.Add(btn, 0, wxEXPAND|wxALL, 2) |
f6bcfd97 | 71 | |
c2dac736 | 72 | btn = wxButton(self, wxNewId(), "-->", style=wxBU_EXACTFIT) |
f6bcfd97 | 73 | EVT_BUTTON(self, btn.GetId(), self.OnNextPageButton) |
c2dac736 RD |
74 | btnSizer.Add(btn, 0, wxEXPAND|wxALL, 2) |
75 | ||
76 | btn = wxButton(self, wxNewId(), "Stop", style=wxBU_EXACTFIT) | |
77 | EVT_BUTTON(self, btn.GetId(), self.OnStopButton) | |
78 | btnSizer.Add(btn, 0, wxEXPAND|wxALL, 2) | |
79 | ||
80 | btn = wxButton(self, wxNewId(), "Search", style=wxBU_EXACTFIT) | |
81 | EVT_BUTTON(self, btn.GetId(), self.OnSearchPageButton) | |
82 | btnSizer.Add(btn, 0, wxEXPAND|wxALL, 2) | |
83 | ||
84 | btn = wxButton(self, wxNewId(), "Refresh", style=wxBU_EXACTFIT) | |
85 | EVT_BUTTON(self, btn.GetId(), self.OnRefreshPageButton) | |
86 | btnSizer.Add(btn, 0, wxEXPAND|wxALL, 2) | |
f6bcfd97 BP |
87 | |
88 | txt = wxStaticText(self, -1, "Location:") | |
c2dac736 | 89 | btnSizer.Add(txt, 0, wxCENTER|wxALL, 2) |
f6bcfd97 BP |
90 | |
91 | self.location = wxComboBox(self, wxNewId(), "", style=wxCB_DROPDOWN) | |
92 | EVT_COMBOBOX(self, self.location.GetId(), self.OnLocationSelect) | |
93 | EVT_KEY_UP(self.location, self.OnLocationKey) | |
342639eb | 94 | EVT_CHAR(self.location, self.IgnoreReturn) |
c2dac736 | 95 | btnSizer.Add(self.location, 1, wxEXPAND|wxALL, 2) |
f6bcfd97 BP |
96 | |
97 | sizer.Add(btnSizer, 0, wxEXPAND) | |
98 | sizer.Add(self.ie, 1, wxEXPAND) | |
99 | ||
100 | self.ie.Navigate(self.current) | |
101 | self.location.Append(self.current) | |
102 | ||
103 | self.SetSizer(sizer) | |
104 | self.SetAutoLayout(true) | |
105 | EVT_SIZE(self, self.OnSize) | |
106 | ||
63b6646e | 107 | EVT_WINDOW_DESTROY(self, self.OnDestroy) |
f6bcfd97 | 108 | |
f6bcfd97 | 109 | |
63b6646e | 110 | def OnDestroy(self, evt): |
f6bcfd97 BP |
111 | if self.ie: |
112 | self.ie.Cleanup() | |
113 | self.ie = None | |
47eea71e | 114 | self.frame = None |
f6bcfd97 | 115 | |
63b6646e RD |
116 | |
117 | def OnSize(self, evt): | |
118 | self.Layout() | |
119 | ||
120 | ||
f6bcfd97 BP |
121 | def OnLocationSelect(self, evt): |
122 | url = self.location.GetStringSelection() | |
123 | self.log.write('OnLocationSelect: %s\n' % url) | |
124 | self.ie.Navigate(url) | |
125 | ||
126 | def OnLocationKey(self, evt): | |
127 | if evt.KeyCode() == WXK_RETURN: | |
128 | URL = self.location.GetValue() | |
129 | self.location.Append(URL) | |
130 | self.ie.Navigate(URL) | |
131 | else: | |
132 | evt.Skip() | |
133 | ||
134 | def IgnoreReturn(self, evt): | |
135 | print 'IgnoreReturn' | |
136 | if evt.KeyCode() != WXK_RETURN: | |
137 | evt.Skip() | |
138 | ||
139 | def OnOpenButton(self, event): | |
140 | dlg = wxTextEntryDialog(self, "Open Location", | |
141 | "Enter a full URL or local path", | |
142 | self.current, wxOK|wxCANCEL) | |
143 | dlg.CentreOnParent() | |
144 | if dlg.ShowModal() == wxID_OK: | |
145 | self.current = dlg.GetValue() | |
146 | self.ie.Navigate(self.current) | |
147 | dlg.Destroy() | |
148 | ||
149 | def OnHomeButton(self, event): | |
150 | self.ie.GoHome() ## ET Phone Home! | |
151 | ||
152 | def OnPrevPageButton(self, event): | |
153 | self.ie.GoBack() | |
154 | ||
f6bcfd97 BP |
155 | def OnNextPageButton(self, event): |
156 | self.ie.GoForward() | |
157 | ||
c2dac736 RD |
158 | def OnStopButton(self, evt): |
159 | self.ie.Stop() | |
160 | ||
161 | def OnSearchPageButton(self, evt): | |
162 | self.ie.GoSearch() | |
163 | ||
164 | def OnRefreshPageButton(self, evt): | |
165 | self.ie.Refresh2(3) | |
166 | ||
167 | ||
f6bcfd97 BP |
168 | |
169 | # The following event handlers are called by the web browser COM | |
170 | # control since we passed self to MakeActiveXClass. It will look | |
171 | # here for matching attributes and call them if they exist. See the | |
172 | # module generated by makepy for details of method names, etc. | |
173 | def OnBeforeNavigate2(self, pDisp, URL, *args): | |
174 | self.log.write('OnBeforeNavigate2: %s\n' % URL) | |
175 | ||
176 | def OnNavigateComplete2(self, pDisp, URL): | |
177 | self.log.write('OnNavigateComplete2: %s\n' % URL) | |
178 | self.current = URL | |
179 | self.location.SetValue(URL) | |
180 | ||
181 | def OnTitleChange(self, text): | |
182 | self.log.write('OnTitleChange: %s\n' % text) | |
183 | if self.frame: | |
184 | self.frame.SetTitle(self.titleBase + ' -- ' + text) | |
185 | ||
186 | def OnStatusTextChange(self, text): | |
187 | self.log.write('OnStatusTextChange: %s\n' % text) | |
188 | if self.frame: | |
189 | self.frame.SetStatusText(text) | |
190 | ||
191 | ||
192 | #---------------------------------------------------------------------- | |
193 | # for the demo framework... | |
194 | ||
195 | def runTest(frame, nb, log): | |
196 | if wxPlatform == '__WXMSW__': | |
197 | win = TestPanel(nb, log, frame) | |
198 | return win | |
199 | else: | |
200 | dlg = wxMessageDialog(frame, 'This demo only works on MSW.', | |
201 | 'Sorry', wxOK | wxICON_INFORMATION) | |
202 | dlg.ShowModal() | |
203 | dlg.Destroy() | |
204 | ||
205 | ||
206 | overview = __doc__ | |
207 | ||
208 | #---------------------------------------------------------------------- | |
209 | ||
210 | ||
211 | if __name__ == '__main__': | |
212 | class TestFrame(wxFrame): | |
213 | def __init__(self): | |
214 | wxFrame.__init__(self, None, -1, "ActiveX test -- Internet Explorer", | |
215 | size=(640, 480), | |
216 | style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE) | |
217 | self.CreateStatusBar() | |
218 | self.tp = TestPanel(self, sys.stdout, self) | |
47eea71e | 219 | EVT_CLOSE(self, self.OnCloseWindow) |
f6bcfd97 | 220 | |
47eea71e RD |
221 | def OnCloseWindow(self, evt): |
222 | self.tp.Destroy() | |
223 | self.Destroy() | |
f6bcfd97 BP |
224 | |
225 | app = wxPySimpleApp() | |
226 | frame = TestFrame() | |
227 | frame.Show(true) | |
228 | app.MainLoop() | |
229 | ||
230 | ||
231 |