]>
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): | |
37 | wxWindow.__init__(self, parent, -1)#, style=wxCLIP_CHILDREN) | |
38 | self.ie = None | |
39 | self.log = log | |
c368d904 | 40 | self.current = "http://wxPython.org/" |
f6bcfd97 BP |
41 | self.frame = frame |
42 | if frame: | |
43 | self.titleBase = frame.GetTitle() | |
44 | ||
45 | ||
46 | sizer = wxBoxSizer(wxVERTICAL) | |
47 | btnSizer = wxBoxSizer(wxHORIZONTAL) | |
48 | ||
49 | # Make a new class that derives from the WebBrowser class in the | |
50 | # COM module imported above. This class also derives from wxWindow and | |
51 | # implements the machinery needed to integrate the two worlds. | |
52 | theClass = MakeActiveXClass(browserModule.WebBrowser, | |
53 | eventObj = self) | |
54 | ||
55 | # Create an instance of that class | |
56 | self.ie = theClass(self, -1, style=wxSUNKEN_BORDER) | |
57 | ||
58 | ||
59 | #btn = wxButton(self, wxNewId(), " Open ") | |
60 | #EVT_BUTTON(self, btn.GetId(), self.OnOpenButton) | |
61 | #btnSizer.Add(btn, 0, wxEXPAND|wxALL, 5) | |
62 | ||
63 | btn = wxButton(self, wxNewId(), " Home ") | |
64 | EVT_BUTTON(self, btn.GetId(), self.OnHomeButton) | |
65 | btnSizer.Add(btn, 0, wxEXPAND|wxALL, 5) | |
66 | ||
67 | btn = wxButton(self, wxNewId(), " <-- ") | |
68 | EVT_BUTTON(self, btn.GetId(), self.OnPrevPageButton) | |
69 | btnSizer.Add(btn, 0, wxEXPAND|wxALL, 5) | |
70 | ||
71 | btn = wxButton(self, wxNewId(), " --> ") | |
72 | EVT_BUTTON(self, btn.GetId(), self.OnNextPageButton) | |
73 | btnSizer.Add(btn, 0, wxEXPAND|wxALL, 5) | |
74 | ||
75 | txt = wxStaticText(self, -1, "Location:") | |
76 | btnSizer.Add(txt, 0, wxCENTER|wxALL, 5) | |
77 | ||
78 | self.location = wxComboBox(self, wxNewId(), "", style=wxCB_DROPDOWN) | |
79 | EVT_COMBOBOX(self, self.location.GetId(), self.OnLocationSelect) | |
80 | EVT_KEY_UP(self.location, self.OnLocationKey) | |
342639eb | 81 | EVT_CHAR(self.location, self.IgnoreReturn) |
f6bcfd97 BP |
82 | btnSizer.Add(self.location, 1, wxEXPAND|wxALL, 5) |
83 | ||
84 | sizer.Add(btnSizer, 0, wxEXPAND) | |
85 | sizer.Add(self.ie, 1, wxEXPAND) | |
86 | ||
87 | self.ie.Navigate(self.current) | |
88 | self.location.Append(self.current) | |
89 | ||
90 | self.SetSizer(sizer) | |
91 | self.SetAutoLayout(true) | |
92 | EVT_SIZE(self, self.OnSize) | |
93 | ||
94 | ||
95 | def OnSize(self, evt): | |
96 | self.Layout() | |
97 | ||
98 | def __del__(self): | |
99 | if self.ie: | |
100 | self.ie.Cleanup() | |
101 | self.ie = None | |
102 | ||
103 | def OnLocationSelect(self, evt): | |
104 | url = self.location.GetStringSelection() | |
105 | self.log.write('OnLocationSelect: %s\n' % url) | |
106 | self.ie.Navigate(url) | |
107 | ||
108 | def OnLocationKey(self, evt): | |
109 | if evt.KeyCode() == WXK_RETURN: | |
110 | URL = self.location.GetValue() | |
111 | self.location.Append(URL) | |
112 | self.ie.Navigate(URL) | |
113 | else: | |
114 | evt.Skip() | |
115 | ||
116 | def IgnoreReturn(self, evt): | |
117 | print 'IgnoreReturn' | |
118 | if evt.KeyCode() != WXK_RETURN: | |
119 | evt.Skip() | |
120 | ||
121 | def OnOpenButton(self, event): | |
122 | dlg = wxTextEntryDialog(self, "Open Location", | |
123 | "Enter a full URL or local path", | |
124 | self.current, wxOK|wxCANCEL) | |
125 | dlg.CentreOnParent() | |
126 | if dlg.ShowModal() == wxID_OK: | |
127 | self.current = dlg.GetValue() | |
128 | self.ie.Navigate(self.current) | |
129 | dlg.Destroy() | |
130 | ||
131 | def OnHomeButton(self, event): | |
132 | self.ie.GoHome() ## ET Phone Home! | |
133 | ||
134 | def OnPrevPageButton(self, event): | |
135 | self.ie.GoBack() | |
136 | ||
137 | ||
138 | def OnNextPageButton(self, event): | |
139 | self.ie.GoForward() | |
140 | ||
141 | ||
142 | # The following event handlers are called by the web browser COM | |
143 | # control since we passed self to MakeActiveXClass. It will look | |
144 | # here for matching attributes and call them if they exist. See the | |
145 | # module generated by makepy for details of method names, etc. | |
146 | def OnBeforeNavigate2(self, pDisp, URL, *args): | |
147 | self.log.write('OnBeforeNavigate2: %s\n' % URL) | |
148 | ||
149 | def OnNavigateComplete2(self, pDisp, URL): | |
150 | self.log.write('OnNavigateComplete2: %s\n' % URL) | |
151 | self.current = URL | |
152 | self.location.SetValue(URL) | |
153 | ||
154 | def OnTitleChange(self, text): | |
155 | self.log.write('OnTitleChange: %s\n' % text) | |
156 | if self.frame: | |
157 | self.frame.SetTitle(self.titleBase + ' -- ' + text) | |
158 | ||
159 | def OnStatusTextChange(self, text): | |
160 | self.log.write('OnStatusTextChange: %s\n' % text) | |
161 | if self.frame: | |
162 | self.frame.SetStatusText(text) | |
163 | ||
164 | ||
165 | #---------------------------------------------------------------------- | |
166 | # for the demo framework... | |
167 | ||
168 | def runTest(frame, nb, log): | |
169 | if wxPlatform == '__WXMSW__': | |
170 | win = TestPanel(nb, log, frame) | |
171 | return win | |
172 | else: | |
173 | dlg = wxMessageDialog(frame, 'This demo only works on MSW.', | |
174 | 'Sorry', wxOK | wxICON_INFORMATION) | |
175 | dlg.ShowModal() | |
176 | dlg.Destroy() | |
177 | ||
178 | ||
179 | overview = __doc__ | |
180 | ||
181 | #---------------------------------------------------------------------- | |
182 | ||
183 | ||
184 | if __name__ == '__main__': | |
185 | class TestFrame(wxFrame): | |
186 | def __init__(self): | |
187 | wxFrame.__init__(self, None, -1, "ActiveX test -- Internet Explorer", | |
188 | size=(640, 480), | |
189 | style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE) | |
190 | self.CreateStatusBar() | |
191 | self.tp = TestPanel(self, sys.stdout, self) | |
192 | EVT_CLOSE(self, self.OnCloseWindow) | |
193 | ||
194 | def OnCloseWindow(self, event): | |
195 | self.tp.ie.Cleanup() | |
196 | self.Destroy() | |
197 | ||
198 | ||
199 | app = wxPySimpleApp() | |
200 | frame = TestFrame() | |
201 | frame.Show(true) | |
202 | app.MainLoop() | |
203 | ||
204 | ||
205 |