]>
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> | |
8b9a4190 | 14 | This demo embeds the Internet Explorer WebBrowser control, and shows |
f6bcfd97 BP |
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 | ||
8fa876ca RD |
21 | import sys |
22 | import wx | |
23 | ||
24 | if wx.Platform == '__WXMSW__': | |
25 | import wx.lib.activexwrapper as ax | |
26 | import win32com.client.gencache | |
f6bcfd97 BP |
27 | |
28 | try: | |
8fa876ca RD |
29 | browserModule = win32com.client.gencache.EnsureModule( |
30 | "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1 | |
31 | ) | |
f6bcfd97 BP |
32 | except: |
33 | raise ImportError("IE4 or greater does not appear to be installed.") | |
34 | ||
35 | ||
36 | #---------------------------------------------------------------------- | |
37 | ||
8fa876ca | 38 | class TestPanel(wx.Window): |
f6bcfd97 | 39 | def __init__(self, parent, log, frame=None): |
8fa876ca RD |
40 | wx.Window.__init__( |
41 | self, parent, -1, | |
42 | style=wx.CLIP_CHILDREN|wx.NO_FULL_REPAINT_ON_RESIZE | |
43 | ) | |
44 | ||
f6bcfd97 BP |
45 | self.ie = None |
46 | self.log = log | |
c368d904 | 47 | self.current = "http://wxPython.org/" |
f6bcfd97 | 48 | self.frame = frame |
8fa876ca | 49 | |
f6bcfd97 BP |
50 | if frame: |
51 | self.titleBase = frame.GetTitle() | |
52 | ||
8fa876ca RD |
53 | sizer = wx.BoxSizer(wx.VERTICAL) |
54 | btnSizer = wx.BoxSizer(wx.HORIZONTAL) | |
f6bcfd97 BP |
55 | |
56 | # Make a new class that derives from the WebBrowser class in the | |
57 | # COM module imported above. This class also derives from wxWindow and | |
58 | # implements the machinery needed to integrate the two worlds. | |
8fa876ca RD |
59 | theClass = ax.MakeActiveXClass( |
60 | browserModule.WebBrowser, eventObj = self | |
61 | ) | |
f6bcfd97 BP |
62 | |
63 | # Create an instance of that class | |
c2dac736 | 64 | self.ie = theClass(self, -1) ##, style=wxSUNKEN_BORDER) |
f6bcfd97 BP |
65 | |
66 | ||
8fa876ca RD |
67 | btn = wx.Button(self, wx.NewId(), "Open", style=wx.BU_EXACTFIT) |
68 | self.Bind(wx.EVT_BUTTON, self.OnOpenButton, id=btn.GetId()) | |
69 | btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2) | |
f6bcfd97 | 70 | |
8fa876ca RD |
71 | btn = wx.Button(self, wx.NewId(), "Home", style=wx.BU_EXACTFIT) |
72 | self.Bind(wx.EVT_BUTTON, self.OnHomeButton, id=btn.GetId()) | |
73 | btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2) | |
f6bcfd97 | 74 | |
8fa876ca RD |
75 | btn = wx.Button(self, wx.NewId(), "<--", style=wx.BU_EXACTFIT) |
76 | self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, id=btn.GetId()) | |
77 | btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2) | |
f6bcfd97 | 78 | |
8fa876ca RD |
79 | btn = wx.Button(self, wx.NewId(), "-->", style=wx.BU_EXACTFIT) |
80 | self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, id=btn.GetId()) | |
81 | btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2) | |
c2dac736 | 82 | |
8fa876ca RD |
83 | btn = wx.Button(self, wx.NewId(), "Stop", style=wx.BU_EXACTFIT) |
84 | self.Bind(wx.EVT_BUTTON, self.OnStopButton, id=btn.GetId()) | |
85 | btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2) | |
c2dac736 | 86 | |
8fa876ca RD |
87 | btn = wx.Button(self, wx.NewId(), "Search", style=wx.BU_EXACTFIT) |
88 | self.Bind(wx.EVT_BUTTON, self.OnSearchPageButton, id=btn.GetId()) | |
89 | btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2) | |
c2dac736 | 90 | |
8fa876ca RD |
91 | btn = wx.Button(self, wx.NewId(), "Refresh", style=wx.BU_EXACTFIT) |
92 | self.Bind(wx.EVT_BUTTON, self.OnRefreshPageButton, id=btn.GetId()) | |
93 | btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2) | |
f6bcfd97 | 94 | |
8fa876ca RD |
95 | txt = wx.StaticText(self, -1, "Location:") |
96 | btnSizer.Add(txt, 0, wx.CENTER|wx.ALL, 2) | |
f6bcfd97 | 97 | |
8fa876ca RD |
98 | self.location = wx.ComboBox(self, wx.NewId(), "", style=wx.CB_DROPDOWN) |
99 | self.Bind(wx.EVT_COMBOBOX, self.OnLocationSelect, id=self.location.GetId()) | |
100 | self.Bind(wx.EVT_KEY_UP, self.OnLocationKey, self.location) | |
101 | self.Bind(wx.EVT_CHAR, self.IgnoreReturn, self.location) | |
102 | btnSizer.Add(self.location, 1, wx.EXPAND|wx.ALL, 2) | |
f6bcfd97 | 103 | |
8fa876ca RD |
104 | sizer.Add(btnSizer, 0, wx.EXPAND) |
105 | sizer.Add(self.ie, 1, wx.EXPAND) | |
f6bcfd97 BP |
106 | |
107 | self.ie.Navigate(self.current) | |
108 | self.location.Append(self.current) | |
109 | ||
110 | self.SetSizer(sizer) | |
1e4a197e | 111 | self.SetAutoLayout(True) |
8fa876ca RD |
112 | self.Bind(wx.EVT_SIZE, self.OnSize) |
113 | self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy) | |
f6bcfd97 | 114 | |
f6bcfd97 | 115 | |
1e4a197e RD |
116 | def ShutdownDemo(self): |
117 | # put the frame title back | |
118 | if self.frame: | |
119 | self.frame.SetTitle(self.titleBase) | |
120 | ||
121 | ||
63b6646e | 122 | def OnDestroy(self, evt): |
f6bcfd97 BP |
123 | if self.ie: |
124 | self.ie.Cleanup() | |
125 | self.ie = None | |
47eea71e | 126 | self.frame = None |
f6bcfd97 | 127 | |
63b6646e RD |
128 | |
129 | def OnSize(self, evt): | |
130 | self.Layout() | |
131 | ||
132 | ||
f6bcfd97 BP |
133 | def OnLocationSelect(self, evt): |
134 | url = self.location.GetStringSelection() | |
135 | self.log.write('OnLocationSelect: %s\n' % url) | |
136 | self.ie.Navigate(url) | |
137 | ||
138 | def OnLocationKey(self, evt): | |
8fa876ca | 139 | if evt.KeyCode() == wx.WXK_RETURN: |
f6bcfd97 BP |
140 | URL = self.location.GetValue() |
141 | self.location.Append(URL) | |
142 | self.ie.Navigate(URL) | |
143 | else: | |
144 | evt.Skip() | |
145 | ||
146 | def IgnoreReturn(self, evt): | |
147 | print 'IgnoreReturn' | |
8fa876ca | 148 | if evt.KeyCode() != wx.WXK_RETURN: |
f6bcfd97 BP |
149 | evt.Skip() |
150 | ||
151 | def OnOpenButton(self, event): | |
8fa876ca | 152 | dlg = wx.TextEntryDialog(self, "Open Location", |
f6bcfd97 | 153 | "Enter a full URL or local path", |
8fa876ca RD |
154 | self.current, wx.OK|wx.CANCEL) |
155 | ||
f6bcfd97 | 156 | dlg.CentreOnParent() |
8fa876ca RD |
157 | |
158 | if dlg.ShowModal() == wx.ID_OK: | |
f6bcfd97 BP |
159 | self.current = dlg.GetValue() |
160 | self.ie.Navigate(self.current) | |
8fa876ca | 161 | |
f6bcfd97 BP |
162 | dlg.Destroy() |
163 | ||
164 | def OnHomeButton(self, event): | |
165 | self.ie.GoHome() ## ET Phone Home! | |
166 | ||
167 | def OnPrevPageButton(self, event): | |
168 | self.ie.GoBack() | |
169 | ||
f6bcfd97 BP |
170 | def OnNextPageButton(self, event): |
171 | self.ie.GoForward() | |
172 | ||
c2dac736 RD |
173 | def OnStopButton(self, evt): |
174 | self.ie.Stop() | |
175 | ||
176 | def OnSearchPageButton(self, evt): | |
177 | self.ie.GoSearch() | |
178 | ||
179 | def OnRefreshPageButton(self, evt): | |
180 | self.ie.Refresh2(3) | |
181 | ||
182 | ||
f6bcfd97 BP |
183 | # The following event handlers are called by the web browser COM |
184 | # control since we passed self to MakeActiveXClass. It will look | |
185 | # here for matching attributes and call them if they exist. See the | |
186 | # module generated by makepy for details of method names, etc. | |
187 | def OnBeforeNavigate2(self, pDisp, URL, *args): | |
188 | self.log.write('OnBeforeNavigate2: %s\n' % URL) | |
189 | ||
190 | def OnNavigateComplete2(self, pDisp, URL): | |
191 | self.log.write('OnNavigateComplete2: %s\n' % URL) | |
192 | self.current = URL | |
193 | self.location.SetValue(URL) | |
194 | ||
195 | def OnTitleChange(self, text): | |
196 | self.log.write('OnTitleChange: %s\n' % text) | |
197 | if self.frame: | |
198 | self.frame.SetTitle(self.titleBase + ' -- ' + text) | |
199 | ||
200 | def OnStatusTextChange(self, text): | |
201 | self.log.write('OnStatusTextChange: %s\n' % text) | |
202 | if self.frame: | |
203 | self.frame.SetStatusText(text) | |
204 | ||
205 | ||
206 | #---------------------------------------------------------------------- | |
207 | # for the demo framework... | |
208 | ||
209 | def runTest(frame, nb, log): | |
8fa876ca | 210 | if wx.Platform == '__WXMSW__': |
f6bcfd97 BP |
211 | win = TestPanel(nb, log, frame) |
212 | return win | |
213 | else: | |
8fa876ca RD |
214 | dlg = wx.MessageDialog(frame, 'This demo only works on MSW.', |
215 | 'Sorry', wx.OK | wx.ICON_INFORMATION) | |
f6bcfd97 BP |
216 | dlg.ShowModal() |
217 | dlg.Destroy() | |
218 | ||
219 | ||
220 | overview = __doc__ | |
221 | ||
222 | #---------------------------------------------------------------------- | |
223 | ||
224 | ||
225 | if __name__ == '__main__': | |
8fa876ca | 226 | class TestFrame(wx.Frame): |
f6bcfd97 | 227 | def __init__(self): |
8fa876ca RD |
228 | wx.Frame.__init__( |
229 | self, None, -1, "ActiveX test -- Internet Explorer", | |
230 | size=(640, 480), | |
231 | style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE | |
232 | ) | |
233 | ||
f6bcfd97 BP |
234 | self.CreateStatusBar() |
235 | self.tp = TestPanel(self, sys.stdout, self) | |
8fa876ca | 236 | self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) |
f6bcfd97 | 237 | |
47eea71e RD |
238 | def OnCloseWindow(self, evt): |
239 | self.tp.Destroy() | |
240 | self.Destroy() | |
f6bcfd97 | 241 | |
8fa876ca | 242 | app = wx.PySimpleApp() |
f6bcfd97 | 243 | frame = TestFrame() |
1e4a197e | 244 | frame.Show(True) |
f6bcfd97 BP |
245 | app.MainLoop() |
246 |