1#!/usr/bin/env python
2
3import wx
4
5from wx.lib.activexwrapper import MakeActiveXClass
6import win32com.client.gencache
7browserModule = win32com.client.gencache.EnsureModule(
8    "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)
9
10
11#----------------------------------------------------------------------
12
13class TestPanel(wx.Window):
14    def __init__(self, parent, frame=None):
15        wx.Window.__init__(self, parent)
16        self.ie = None
17        self.current = "http://wxPython.org/"
18        self.frame = frame
19        if frame:
20            self.titleBase = frame.GetTitle()
21
22
23        sizer = wx.BoxSizer(wx.VERTICAL)
24        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
25
26        # Make a new class that derives from the WebBrowser class in the
27        # COM module imported above.  This class also derives from wxWindow and
28        # implements the machinery needed to integrate the two worlds.
29        theClass = MakeActiveXClass(browserModule.WebBrowser, eventObj=self)
30
31        # Create an instance of that class
32        self.ie = theClass(self, -1)
33
34
35        btn = wx.Button(self, wx.ID_ANY, "Open", style=wx.BU_EXACTFIT)
36        self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
37        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
38
39        btn = wx.Button(self, wx.ID_ANY, "Home", style=wx.BU_EXACTFIT)
40        self.Bind(wx.EVT_BUTTON, self.OnHomeButton, btn)
41        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
42
43        btn = wx.Button(self, wx.ID_ANY, "<--", style=wx.BU_EXACTFIT)
44        self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn)
45        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
46
47        btn = wx.Button(self, wx.ID_ANY, "-->", style=wx.BU_EXACTFIT)
48        self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn)
49        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
50
51        btn = wx.Button(self, wx.ID_ANY, "Stop", style=wx.BU_EXACTFIT)
52        self.Bind(wx.EVT_BUTTON, self.OnStopButton, btn)
53        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
54
55        btn = wx.Button(self, wx.ID_ANY, "Search", style=wx.BU_EXACTFIT)
56        self.Bind(wx.EVT_BUTTON, self.OnSearchPageButton, btn)
57        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
58
59        btn = wx.Button(self, wx.ID_ANY, "Refresh", style=wx.BU_EXACTFIT)
60        self.Bind(wx.EVT_BUTTON, self.OnRefreshPageButton, btn)
61        btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
62
63        txt = wx.StaticText(self, -1, "Location:")
64        btnSizer.Add(txt, 0, wx.CENTER|wx.ALL, 2)
65
66        self.location = wx.ComboBox(self, wx.ID_ANY, "", style=wx.CB_DROPDOWN)
67        self.Bind(wx.EVT_COMBOBOX, self.OnLocationSelect, self.location)
68        self.location.Bind(wx.EVT_KEY_UP, self.OnLocationKey)
69        self.location.Bind(wx.EVT_CHAR, self.IgnoreReturn)
70        btnSizer.Add(self.location, 1, wx.EXPAND|wx.ALL, 2)
71
72        sizer.Add(btnSizer, 0, wx.EXPAND)
73        sizer.Add(self.ie, 1, wx.EXPAND)
74
75        self.ie.Navigate2(self.current)
76        self.location.Append(self.current)
77
78        self.SetSizer(sizer)
79
80        self.Bind(wx.EVT_SIZE, self.OnSize)
81        self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
82
83
84
85    def OnDestroy(self, evt):
86        if self.ie:
87            self.ie.Cleanup()
88            self.ie = None
89            self.frame = None
90
91
92    def OnSize(self, evt):
93        self.Layout()
94
95
96    def OnLocationSelect(self, evt):
97        url = self.location.GetStringSelection()
98        self.ie.Navigate2(url)
99
100    def OnLocationKey(self, evt):
101        if evt.KeyCode() == wx.WXK_RETURN:
102            URL = self.location.GetValue()
103            self.location.Append(URL)
104            self.ie.Navigate2(URL)
105        else:
106            evt.Skip()
107
108    def IgnoreReturn(self, evt):
109        if evt.KeyCode() != wx.WXK_RETURN:
110            evt.Skip()
111
112    def OnOpenButton(self, event):
113        dlg = wx.TextEntryDialog(self, "Open Location",
114                                "Enter a full URL or local path",
115                                self.current, wx.OK|wx.CANCEL)
116        dlg.CentreOnParent()
117        if dlg.ShowModal() == wx.ID_OK:
118            self.current = dlg.GetValue()
119            self.ie.Navigate2(self.current)
120        dlg.Destroy()
121
122    def OnHomeButton(self, event):
123        self.ie.GoHome()
124
125    def OnPrevPageButton(self, event):
126        self.ie.GoBack()
127
128    def OnNextPageButton(self, event):
129        self.ie.GoForward()
130
131    def OnStopButton(self, evt):
132        self.ie.Stop()
133
134    def OnSearchPageButton(self, evt):
135        self.ie.GoSearch()
136
137    def OnRefreshPageButton(self, evt):
138        self.ie.Refresh2(3)
139
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 OnNavigateComplete2(self, pDisp, URL):
147        self.current = URL
148        self.location.SetValue(URL)
149
150    def OnTitleChange(self, text):
151        if self.frame:
152            self.frame.SetTitle(self.titleBase + ' -- ' + text)
153
154    def OnStatusTextChange(self, text):
155        if self.frame:
156            self.frame.SetStatusText(text)
157
158
159#----------------------------------------------------------------------
160
161
162if __name__ == '__main__':
163    class TestFrame(wx.Frame):
164        def __init__(self):
165            wx.Frame.__init__(self, None, -1, "ActiveX test -- Internet Explorer",
166                             size=(640, 480))
167            self.CreateStatusBar()
168            self.tp = TestPanel(self, frame=self)
169            self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
170
171        def OnCloseWindow(self, evt):
172            self.tp.Destroy()
173            self.Destroy()
174
175    app = wx.App()
176    frame = TestFrame()
177    frame.Show(True)
178
179    import wx.py
180    shell = wx.py.shell.ShellFrame(frame, size=(640,480),
181                                   locals=dict(wx=wx, frame=frame, ie=frame.tp.ie))
182    shell.Show()
183    frame.Raise()
184    app.MainLoop()
185
186
187
188