]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/ActiveXWrapper_IE.py
reSWIGged
[wxWidgets.git] / wxPython / demo / ActiveXWrapper_IE.py
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 Explorer 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,
38 style=wxCLIP_CHILDREN|wxNO_FULL_REPAINT_ON_RESIZE)
39 self.ie = None
40 self.log = log
41 self.current = "http://wxPython.org/"
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
57 self.ie = theClass(self, -1) ##, style=wxSUNKEN_BORDER)
58
59
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)
63
64 btn = wxButton(self, wxNewId(), "Home", style=wxBU_EXACTFIT)
65 EVT_BUTTON(self, btn.GetId(), self.OnHomeButton)
66 btnSizer.Add(btn, 0, wxEXPAND|wxALL, 2)
67
68 btn = wxButton(self, wxNewId(), "<--", style=wxBU_EXACTFIT)
69 EVT_BUTTON(self, btn.GetId(), self.OnPrevPageButton)
70 btnSizer.Add(btn, 0, wxEXPAND|wxALL, 2)
71
72 btn = wxButton(self, wxNewId(), "-->", style=wxBU_EXACTFIT)
73 EVT_BUTTON(self, btn.GetId(), self.OnNextPageButton)
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)
87
88 txt = wxStaticText(self, -1, "Location:")
89 btnSizer.Add(txt, 0, wxCENTER|wxALL, 2)
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)
94 EVT_CHAR(self.location, self.IgnoreReturn)
95 btnSizer.Add(self.location, 1, wxEXPAND|wxALL, 2)
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
107 EVT_WINDOW_DESTROY(self, self.OnDestroy)
108
109
110 def ShutdownDemo(self):
111 # put the frame title back
112 if self.frame:
113 self.frame.SetTitle(self.titleBase)
114
115
116 def OnDestroy(self, evt):
117 if self.ie:
118 self.ie.Cleanup()
119 self.ie = None
120 self.frame = None
121
122
123 def OnSize(self, evt):
124 self.Layout()
125
126
127 def OnLocationSelect(self, evt):
128 url = self.location.GetStringSelection()
129 self.log.write('OnLocationSelect: %s\n' % url)
130 self.ie.Navigate(url)
131
132 def OnLocationKey(self, evt):
133 if evt.KeyCode() == WXK_RETURN:
134 URL = self.location.GetValue()
135 self.location.Append(URL)
136 self.ie.Navigate(URL)
137 else:
138 evt.Skip()
139
140 def IgnoreReturn(self, evt):
141 print 'IgnoreReturn'
142 if evt.KeyCode() != WXK_RETURN:
143 evt.Skip()
144
145 def OnOpenButton(self, event):
146 dlg = wxTextEntryDialog(self, "Open Location",
147 "Enter a full URL or local path",
148 self.current, wxOK|wxCANCEL)
149 dlg.CentreOnParent()
150 if dlg.ShowModal() == wxID_OK:
151 self.current = dlg.GetValue()
152 self.ie.Navigate(self.current)
153 dlg.Destroy()
154
155 def OnHomeButton(self, event):
156 self.ie.GoHome() ## ET Phone Home!
157
158 def OnPrevPageButton(self, event):
159 self.ie.GoBack()
160
161 def OnNextPageButton(self, event):
162 self.ie.GoForward()
163
164 def OnStopButton(self, evt):
165 self.ie.Stop()
166
167 def OnSearchPageButton(self, evt):
168 self.ie.GoSearch()
169
170 def OnRefreshPageButton(self, evt):
171 self.ie.Refresh2(3)
172
173
174
175 # The following event handlers are called by the web browser COM
176 # control since we passed self to MakeActiveXClass. It will look
177 # here for matching attributes and call them if they exist. See the
178 # module generated by makepy for details of method names, etc.
179 def OnBeforeNavigate2(self, pDisp, URL, *args):
180 self.log.write('OnBeforeNavigate2: %s\n' % URL)
181
182 def OnNavigateComplete2(self, pDisp, URL):
183 self.log.write('OnNavigateComplete2: %s\n' % URL)
184 self.current = URL
185 self.location.SetValue(URL)
186
187 def OnTitleChange(self, text):
188 self.log.write('OnTitleChange: %s\n' % text)
189 if self.frame:
190 self.frame.SetTitle(self.titleBase + ' -- ' + text)
191
192 def OnStatusTextChange(self, text):
193 self.log.write('OnStatusTextChange: %s\n' % text)
194 if self.frame:
195 self.frame.SetStatusText(text)
196
197
198 #----------------------------------------------------------------------
199 # for the demo framework...
200
201 def runTest(frame, nb, log):
202 if wxPlatform == '__WXMSW__':
203 win = TestPanel(nb, log, frame)
204 return win
205 else:
206 dlg = wxMessageDialog(frame, 'This demo only works on MSW.',
207 'Sorry', wxOK | wxICON_INFORMATION)
208 dlg.ShowModal()
209 dlg.Destroy()
210
211
212 overview = __doc__
213
214 #----------------------------------------------------------------------
215
216
217 if __name__ == '__main__':
218 class TestFrame(wxFrame):
219 def __init__(self):
220 wxFrame.__init__(self, None, -1, "ActiveX test -- Internet Explorer",
221 size=(640, 480),
222 style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)
223 self.CreateStatusBar()
224 self.tp = TestPanel(self, sys.stdout, self)
225 EVT_CLOSE(self, self.OnCloseWindow)
226
227 def OnCloseWindow(self, evt):
228 self.tp.Destroy()
229 self.Destroy()
230
231 app = wxPySimpleApp()
232 frame = TestFrame()
233 frame.Show(True)
234 app.MainLoop()
235
236
237