1 #!/usr/bin/python 2 3 # Copyright (C) 2007 Kevin Ollivier All rights reserved. 4 # 5 # Redistribution and use in source and binary forms, with or without 6 # modification, are permitted provided that the following conditions 7 # are met: 8 # 1. Redistributions of source code must retain the above copyright 9 # notice, this list of conditions and the following disclaimer. 10 # 2. Redistributions in binary form must reproduce the above copyright 11 # notice, this list of conditions and the following disclaimer in the 12 # documentation and/or other materials provided with the distribution. 13 # 14 # THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 26 27 import wx 28 import wx.webview 29 30 class TestPanel(wx.Panel): 31 def __init__(self, parent, log, frame=None): 32 wx.Panel.__init__( 33 self, parent, -1, 34 style=wx.TAB_TRAVERSAL|wx.CLIP_CHILDREN|wx.NO_FULL_REPAINT_ON_RESIZE 35 ) 36 37 self.log = log 38 self.current = "http://wxPython.org/" 39 self.frame = frame 40 41 if frame: 42 self.titleBase = frame.GetTitle() 43 44 sizer = wx.BoxSizer(wx.VERTICAL) 45 btnSizer = wx.BoxSizer(wx.HORIZONTAL) 46 47 self.webview = wx.webview.WebView(self, -1) 48 49 50 btn = wx.Button(self, -1, "Open", style=wx.BU_EXACTFIT) 51 self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn) 52 btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2) 53 54 btn = wx.Button(self, -1, "<--", style=wx.BU_EXACTFIT) 55 self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn) 56 btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2) 57 58 btn = wx.Button(self, -1, "-->", style=wx.BU_EXACTFIT) 59 self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn) 60 btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2) 61 62 btn = wx.Button(self, -1, "Stop", style=wx.BU_EXACTFIT) 63 self.Bind(wx.EVT_BUTTON, self.OnStopButton, btn) 64 btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2) 65 66 btn = wx.Button(self, -1, "Refresh", style=wx.BU_EXACTFIT) 67 self.Bind(wx.EVT_BUTTON, self.OnRefreshPageButton, btn) 68 btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2) 69 70 txt = wx.StaticText(self, -1, "Location:") 71 btnSizer.Add(txt, 0, wx.CENTER|wx.ALL, 2) 72 73 self.location = wx.ComboBox( 74 self, -1, "", style=wx.CB_DROPDOWN|wx.PROCESS_ENTER 75 ) 76 77 self.Bind(wx.EVT_COMBOBOX, self.OnLocationSelect, self.location) 78 self.location.Bind(wx.EVT_KEY_UP, self.OnLocationKey) 79 self.location.Bind(wx.EVT_CHAR, self.IgnoreReturn) 80 btnSizer.Add(self.location, 1, wx.EXPAND|wx.ALL, 2) 81 82 sizer.Add(btnSizer, 0, wx.EXPAND) 83 sizer.Add(self.webview, 1, wx.EXPAND) 84 85 self.webview.LoadURL(self.current) 86 self.location.Append(self.current) 87 88 self.webview.Bind(wx.webview.EVT_WEBVIEW_LOAD, self.OnStateChanged) 89 90 self.SetSizer(sizer) 91 92 def OnStateChanged(self, event): 93 statusbar = self.GetParent().GetStatusBar() 94 if statusbar: 95 if event.GetState() == wx.webview.WEBVIEW_LOAD_NEGOTIATING: 96 statusbar.SetStatusText("Contacting " + event.GetURL()) 97 elif event.GetState() == wx.webview.WEBVIEW_LOAD_TRANSFERRING: 98 statusbar.SetStatusText("Loading " + event.GetURL()) 99 elif event.GetState() == wx.webview.WEBVIEW_LOAD_DOC_COMPLETED: 100 statusbar.SetStatusText("") 101 self.location.SetValue(event.GetURL()) 102 self.GetParent().SetTitle("wxWebView - " + self.webview.GetPageTitle()) 103 104 def OnLocationKey(self, evt): 105 if evt.GetKeyCode() == wx.WXK_RETURN: 106 URL = self.location.GetValue() 107 self.location.Append(URL) 108 self.webview.LoadURL(URL) 109 else: 110 evt.Skip() 111 112 def IgnoreReturn(self, evt): 113 if evt.GetKeyCode() != wx.WXK_RETURN: 114 evt.Skip() 115 116 def OnLocationSelect(self, evt): 117 url = self.location.GetStringSelection() 118 self.webview.LoadURL(url) 119 120 def OnOpenButton(self, event): 121 dlg = wx.TextEntryDialog(self, "Open Location", 122 "Enter a full URL or local path", 123 self.current, wx.OK|wx.CANCEL) 124 dlg.CentreOnParent() 125 126 if dlg.ShowModal() == wx.ID_OK: 127 self.current = dlg.GetValue() 128 self.webview.LoadURL(self.current) 129 130 dlg.Destroy() 131 132 def OnPrevPageButton(self, event): 133 self.webview.GoBack() 134 135 def OnNextPageButton(self, event): 136 self.webview.GoForward() 137 138 def OnStopButton(self, evt): 139 self.webview.Stop() 140 141 def OnRefreshPageButton(self, evt): 142 self.webview.Reload() 143 144 145 class wkFrame(wx.Frame): 146 def __init__(self): 147 wx.Frame.__init__(self, None, -1, "WebKit in wxPython!") 148 149 self.panel = TestPanel(self, -1) 150 self.panel.webview.LoadURL("http://www.wxwidgets.org/") 151 self.CreateStatusBar() 152 153 class wkApp(wx.App): 154 def OnInit(self): 155 self.webFrame = wkFrame() 156 self.SetTopWindow(self.webFrame) 157 self.webFrame.Show() 158 159 return True 160 161 app = wkApp(redirect=False) 162 app.MainLoop() 163