Skip to content

Commit 9920379

Browse files
committed
Updated wx-subpackage to make it work on Linux.
1 parent 623e26a commit 9920379

6 files changed

Lines changed: 102 additions & 84 deletions

File tree

cefpython/cef1/wx-subpackage/chromectrl.py

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,12 @@
33

44
#--------------------------------------------------------------------------------
55

6-
import platform
7-
if platform.architecture()[0] != "32bit":
8-
raise Exception("Only 32bit architecture is supported")
9-
10-
import os
11-
import sys
12-
try:
13-
# Import local PYD file (portable zip).
14-
if sys.hexversion >= 0x02070000 and sys.hexversion < 0x03000000:
15-
import cefpython_py27 as cefpython
16-
elif sys.hexversion >= 0x03000000 and sys.hexversion < 0x04000000:
17-
import cefpython_py32 as cefpython
18-
else:
19-
raise Exception("Unsupported python version: %s" % sys.version)
20-
except ImportError:
21-
# Import from package (installer).
22-
from cefpython1 import cefpython
23-
6+
from cefpython1 import cefpython
7+
from cefpython1.wx.utils import ExceptHook
8+
import os, sys, platform
249
import wx
2510
import wx.lib.buttons as buttons
2611

27-
from cefpython1.wx.utils import GetApplicationPath
28-
2912
#-------------------------------------------------------------------------------
3013

3114
# Default timer interval when timer used to service CEF message loop
@@ -111,14 +94,35 @@ def __init__(self, parent, url="", useTimer=False,
11194
*args, **kwargs):
11295
wx.Window.__init__(self, parent, id=wx.ID_ANY, size=size,
11396
*args, **kwargs)
97+
# On Linux absolute file urls need to start with "file://"
98+
# otherwise a path of "/home/some" is converted to "http://home/some".
99+
if platform.system() == "Linux":
100+
if url.startswith("/"):
101+
url = "file://" + url
114102
self.url = url
103+
115104
windowInfo = cefpython.WindowInfo()
116-
windowInfo.SetAsChild(self.GetHandle())
105+
if platform.system() == "Windows":
106+
windowInfo.SetAsChild(self.GetHandle())
107+
elif platform.system() == "Linux":
108+
windowInfo.SetAsChild(self.GetGtkWidget())
109+
else:
110+
raise Exception("Unsupported OS")
111+
112+
# TODO: allow for custom browser settings for the ChromeWindow
113+
browserSettings = {}
114+
if platform.system() == "Linux":
115+
# Disable plugins on Linux as Flash will crash the application
116+
# in CEF 1 on Linux, it's a known problem.
117+
if not "plugins_disabled" in browserSettings:
118+
browserSettings["plugins_disabled"] = True
119+
117120
self.browser = cefpython.CreateBrowserSync(windowInfo,
118-
browserSettings={}, navigateUrl=url)
121+
browserSettings=browserSettings, navigateUrl=url)
119122

120-
self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
121-
self.Bind(wx.EVT_SIZE, self.OnSize)
123+
if platform.system() == "Windows":
124+
self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
125+
self.Bind(wx.EVT_SIZE, self.OnSize)
122126
if useTimer:
123127
self.timerID = 1
124128
self._CreateTimer(timerMillis)
@@ -149,11 +153,12 @@ def OnIdle(self, event):
149153
event.Skip()
150154

151155
def OnSetFocus(self, event):
156+
"""OS_WIN only."""
152157
cefpython.WindowUtils.OnSetFocus(self.GetHandle(), 0, 0, 0)
153158
event.Skip()
154159

155160
def OnSize(self, event):
156-
"""Handle the the size event"""
161+
"""OS_WIN only. Handle the the size event"""
157162
cefpython.WindowUtils.OnSize(self.GetHandle(), 0, 0, 0)
158163
event.Skip()
159164

@@ -246,6 +251,8 @@ def OnLoadStart(self, browser, frame):
246251
browser.GetMainFrame().GetUrl())
247252
self.navigationBar.AddToHistory(browser.GetMainFrame().GetUrl())
248253

254+
def OnLoadEnd(self, browser, frame, httpStatusCode):
255+
pass
249256

250257
class DefaultClientHandler(object):
251258
def __init__(self, parentCtrl):
@@ -259,7 +266,7 @@ def OnLoadEnd(self, browser, frame, httpStatusCode):
259266

260267
def OnLoadError(self, browser, frame, errorCode, failedUrl, errorText):
261268
# TODO
262-
print "ERROR LOADING URL : %" % failedUrl
269+
print("ERROR LOADING URL : %s" % failedUrl)
263270

264271
class CallbackClientHandler(object):
265272
def __init__(self, onLoadStart=None, onLoadEnd=None):
@@ -276,7 +283,7 @@ def OnLoadEnd(self, browser, frame, httpStatusCode):
276283

277284
def OnLoadError(self, browser, frame, errorCode, failedUrl, errorText):
278285
# TODO
279-
print "ERROR LOADING URL : %" % failedUrl
286+
print("ERROR LOADING URL : %s" % failedUrl)
280287

281288
#-------------------------------------------------------------------------------
282289

@@ -286,26 +293,21 @@ def Initialize(settings=None):
286293
"""
287294
sys.excepthook = ExceptHook
288295
if not settings:
289-
settings = {
290-
"log_severity": cefpython.LOGSEVERITY_INFO,
291-
"log_file": GetApplicationPath("debug.log"),
292-
"release_dcheck_enabled": True # Enable only when debugging.
293-
}
296+
settings = {}
297+
if not "log_severity" in settings:
298+
settings["log_severity"] = cefpython.LOGSEVERITY_INFO
299+
if not "log_file" in settings:
300+
settings["log_file"] = ""
301+
if platform.system() == "Linux":
302+
# On Linux we need to set locales and resources directories.
303+
if not "locales_dir_path" in settings:
304+
settings["locales_dir_path"] = cefpython.GetModuleDirectory()+(
305+
"/locales")
306+
if not "resources_dir_path" in settings:
307+
settings["resources_dir_path"] = cefpython.GetModuleDirectory()
308+
294309
cefpython.Initialize(settings)
295310

296311
def Shutdown():
297312
"""Shuts down CEF, should be called by app exiting code"""
298313
cefpython.Shutdown()
299-
300-
def ExceptHook(t, value, traceObject):
301-
import traceback, os, time
302-
# This hook does the following: in case of exception display it,
303-
# write to error.log, shutdown CEF and exit application.
304-
error = "\n".join(traceback.format_exception(t, value, traceObject))
305-
with open(GetApplicationPath("error.log"), "a") as f:
306-
f.write("\n[%s] %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), error))
307-
print("\n"+error+"\n")
308-
##cefpython.QuitMessageLoop()
309-
##cefpython.Shutdown()
310-
# So that "finally" does not execute.
311-
##os._exit(1)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset=utf-8>
5+
<title>sample 1</title>
6+
<style>
7+
body { font: 13px Segoe UI, Arial; line-height: 1.4em; }
8+
pre { background: #ddd; font: 12px Consolas, Courier New; }
9+
</style>
10+
</head>
11+
<body>
12+
13+
sample1.py - wxPython example for the CEF Python framework
14+
15+
<h3>Google Search</h3>
16+
17+
<a href="http://www.google.com/">http://www.google.com/</a>
18+
19+
<h3>User agent</h3>
20+
21+
<script>document.write(navigator.userAgent)</script>
22+
23+
<h3>Popup</h3>
24+
25+
<a href="javascript:window.open('sample1.html')">
26+
window.open('sample1.html')</a>
27+
28+
<br><br><br><br><br><br><br><br><br><br><br>
29+
<br><br><br><br><br><br><br><br><br><br><br>
30+
<br><br><br><br><br><br><br><br><br><br><br>
31+
32+
</body>
33+
</html>

cefpython/cef1/wx-subpackage/examples/sample1.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import os
55
import wx
6-
76
import cefpython1.wx.chromectrl as chrome
87

98
class MainFrame(wx.Frame):
@@ -12,10 +11,8 @@ def __init__(self):
1211
title='cefwx example1', size=(600,400))
1312

1413
self.cefWindow = chrome.ChromeWindow(self,
15-
#url=os.path.join(os.path.dirname(os.path.abspath(__file__)),
16-
# "../cefsimple.html"))
1714
url=os.path.join(os.path.dirname(os.path.abspath(__file__)),
18-
"../withpopup.html"))
15+
"sample1.html"))
1916

2017
sizer = wx.BoxSizer()
2118
sizer.Add(self.cefWindow, 1, wx.EXPAND, 0)
@@ -28,11 +25,10 @@ def OnClose(self, event):
2825

2926
if __name__ == '__main__':
3027
chrome.Initialize()
31-
print('wx.version=%s' % wx.version())
28+
print('sample1.py: wx.version=%s' % wx.version())
3229
app = wx.PySimpleApp()
3330
MainFrame().Show()
3431
app.MainLoop()
35-
del app # Let wx.App destructor do the cleanup before calling cefpython.Shutdown().
32+
# Important: do the wx cleanup before calling Shutdown.
33+
del app
3634
chrome.Shutdown()
37-
38-

cefpython/cef1/wx-subpackage/examples/sample2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import wx
55
import wx.lib.agw.flatnotebook as fnb
6-
76
import cefpython1.wx.chromectrl as chrome
87

98
ROOT_NAME = "My Locations"
@@ -21,7 +20,7 @@
2120
class MainFrame(wx.Frame):
2221
def __init__(self):
2322
wx.Frame.__init__(self, parent=None, id=wx.ID_ANY,
24-
title='cefwx example2', size=(600, 400))
23+
title='cefwx example2', size=(1024, 768))
2524

2625
self.initComponents()
2726
self.layoutComponents()
@@ -57,18 +56,19 @@ def OnSelChanged(self, event):
5756
event.Skip()
5857

5958
def OnPageClosing(self, event):
60-
print "One could place some extra closing stuff here"
59+
print("sample2.py: One could place some extra closing stuff here")
6160
event.Skip()
6261

6362
def OnClose(self, event):
6463
self.Destroy()
6564

6665
if __name__ == '__main__':
6766
chrome.Initialize()
68-
print('wx.version=%s' % wx.version())
67+
print('sample2.py: wx.version=%s' % wx.version())
6968
app = wx.PySimpleApp()
7069
MainFrame().Show()
7170
app.MainLoop()
72-
del app # Let wx.App destructor do the cleanup before calling cefpython.Shutdown().
71+
# Important: do the wx cleanup before calling Shutdown.
72+
del app
7373
chrome.Shutdown()
7474

cefpython/cef1/wx-subpackage/examples/sample3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55
import wx
66
import wx.lib.agw.flatnotebook as fnb
7-
87
import cefpython1.wx.chromectrl as chrome
98

109
class MainFrame(wx.Frame):
@@ -70,10 +69,11 @@ def _LayoutComponents(self):
7069

7170
if __name__ == '__main__':
7271
chrome.Initialize()
73-
print('wx.version=%s' % wx.version())
72+
print('sample3.py: wx.version=%s' % wx.version())
7473
app = wx.PySimpleApp()
7574
MainFrame().Show()
7675
app.MainLoop()
77-
del app # Let wx.App destructor do the cleanup before calling cefpython.Shutdown().
76+
# Important: do the wx cleanup before calling Shutdown.
77+
del app
7878
chrome.Shutdown()
7979

cefpython/cef1/wx-subpackage/utils.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,13 @@
33

44
#-------------------------------------------------------------------------------
55

6-
import sys
7-
import os
8-
9-
def GetApplicationPath(myFile=None):
10-
import re, os
11-
# If myFile is None return current directory without trailing slash.
12-
if myFile is None:
13-
myFile = ""
14-
# Only when relative path.
15-
if not myFile.startswith("/") and not myFile.startswith("\\") and not re.search(r"^[\w-]+:", myFile):
16-
if hasattr(sys, "frozen"):
17-
path = os.path.dirname(sys.executable)
18-
elif "__file__" in globals():
19-
path = os.path.dirname(os.path.realpath(__file__))
20-
else:
21-
path = os.getcwd()
22-
path = path + os.sep + myFile
23-
path = re.sub(r"[/\\]+", re.escape(os.sep), path)
24-
path = re.sub(r"[/\\]+$", "", path)
25-
return path
26-
return str(myFile)
27-
28-
6+
def ExceptHook(type, value, traceObject):
7+
import traceback, os
8+
# This hook does the following: in case of exception display it,
9+
# write to error.log, shutdown CEF and exit application.
10+
error = "\n".join(traceback.format_exception(type, value, traceObject))
11+
print("\n"+error+"\n")
12+
#cefpython.QuitMessageLoop()
13+
#cefpython.Shutdown()
14+
# So that "finally" does not execute.
15+
#os._exit(1)

0 commit comments

Comments
 (0)