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