X Tutup
Skip to content

Commit 5c9b618

Browse files
committed
Initial commit.
This is version 0.11 of CefPython. CEF version used: 1.963.439.0 (include/ - headers, setup/ - dll wrapper lib)
1 parent 2fe695d commit 5c9b618

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+23819
-0
lines changed

cefapi.pyx

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# CURRENTLY there is NO REAL API, just a single main() function exposed.
2+
3+
import os
4+
import sys
5+
import cefwindow
6+
import win32con
7+
import win32gui
8+
import cython
9+
10+
from libcpp cimport bool
11+
from libc.stdlib cimport malloc, free
12+
13+
cdef CefRefPtr[CefBrowser] browser
14+
15+
def main():
16+
print "\n%s" % ("--------" * 8)
17+
print "Welcome to CEF python bindings!"
18+
print "%s\n" % ("--------" * 8)
19+
20+
print "CefInitialize(settings, app)"
21+
22+
cdef CefSettings settings
23+
cdef CefRefPtr[CefApp] app
24+
25+
cdef bool ret = CefInitialize(settings, app)
26+
27+
if ret: print "OK"
28+
else: print "ERROR"
29+
print "lasterror: %s" % cefwindow.lasterror()
30+
31+
wndproc = {
32+
# pywin32 does not send WM_CREATE message
33+
# win32con.WM_CREATE: wm_create, -- we are calling wm_create() manually after window creation
34+
35+
#win32con.WM_PAINT: wm_paint, -- is it really needed?
36+
37+
win32con.WM_SETFOCUS: wm_setfocus,
38+
win32con.WM_SIZE: wm_size,
39+
win32con.WM_ERASEBKGND: wm_erasebkgnd,
40+
win32con.WM_CLOSE: wm_close
41+
}
42+
43+
hwnd = cefwindow.createwindow("Test", "testclass", wndproc)
44+
print "lasterror: %s" % cefwindow.lasterror()
45+
46+
#print "ShowWindow, UpdateWindow"
47+
#WS_VISIBLE in cefwindow.createwindow does the same, so commenting out.
48+
#win32gui.ShowWindow(hwnd, win32con.SW_SHOWDEFAULT)
49+
#win32gui.UpdateWindow(hwnd)
50+
#print "lasterror: %s" % cefwindow.lasterror()
51+
52+
wm_create(hwnd)
53+
54+
print "CefRunMessageLoop()\n"
55+
CefRunMessageLoop()
56+
print "lasterror: %s" % cefwindow.lasterror()
57+
58+
print "CefShutdown()"
59+
CefShutdown()
60+
61+
print "lasterror: %s" % cefwindow.lasterror()
62+
63+
def wm_create(hwnd):
64+
65+
print "wm_create\n"
66+
67+
# real HWND, "hwnd" passed to wm_create() is an "int"
68+
cdef HWND hwnd2 = FindWindowA("testclass", NULL)
69+
70+
if hwnd2 == NULL: print "hwnd2: NULL"
71+
else: print "hwnd2: OK"
72+
print "lasterror: %s" % cefwindow.lasterror()
73+
74+
cdef CefWindowInfo info
75+
cdef CefBrowserSettings browserSettings
76+
77+
print "win32gui.GetClientRect"
78+
rect1 = win32gui.GetClientRect(hwnd)
79+
print "lasterror: %s" % cefwindow.lasterror()
80+
81+
cdef RECT rect2
82+
rect2.left = <int>rect1[0]
83+
rect2.top = <int>rect1[1]
84+
rect2.right = <int>rect1[2]
85+
rect2.bottom = <int>rect1[3]
86+
87+
print "CefWindowInfo.SetAsChild(hwnd2, rect2)"
88+
info.SetAsChild(hwnd2, rect2)
89+
print "lasterror: %s" % cefwindow.lasterror()
90+
91+
print "CefWindowInfo:"
92+
print "m_x(left): %s" % info.m_x
93+
print "m_y(top): %s" % info.m_y
94+
print "m_nWidth: %s" % info.m_nWidth
95+
print "m_nHeight: %s" % info.m_nHeight
96+
print ""
97+
98+
print "Creating url3 - CefString().FromASCII"
99+
cdef CefString *url3 = new CefString()
100+
htmlfile = "%s/example.html" % os.getcwd()
101+
url3.FromASCII(<char*>htmlfile)
102+
103+
print "Converting back url3(CefString) to ascii:"
104+
cdef wchar_t* urlwide = <wchar_t*> url3.c_str()
105+
print "converted to: urlwide"
106+
cdef int urlascii_size = WideCharToMultiByte(CP_UTF8, 0, urlwide, -1, NULL, 0, NULL, NULL)
107+
print "urlascii_size: %s" % urlascii_size
108+
cdef char* urlascii = <char*>malloc(urlascii_size*sizeof(char))
109+
WideCharToMultiByte(CP_UTF8, 0, urlwide, -1, urlascii, urlascii_size, NULL, NULL)
110+
print "urlascii: %s" % urlascii
111+
#free(urlascii)
112+
print "lasterror: %s" % cefwindow.lasterror()
113+
114+
print ""
115+
print "CefCurrentlyOn(UI): %s" % <bool>CefCurrentlyOn(<CefThreadId>0)
116+
print "CefCurrentlyOn(IO): %s" % <bool>CefCurrentlyOn(<CefThreadId>1)
117+
print "CefCurrentlyOn(FILE): %s" % <bool>CefCurrentlyOn(<CefThreadId>2)
118+
print ""
119+
120+
cdef CefRefPtr[CefClient2] cefclient2 = <cefrefptr_cefclient2_t?>new CefClient2() # <...?> means to throw an error if the cast is not allowed
121+
print "CefClient2 instantiated"
122+
123+
#print "CreateBrowser: %s" % <bool>CreateBrowser(info, <cefrefptr_cefclient_t>cefclient2, url3, browserSettings)
124+
125+
global browser
126+
browser = CreateBrowserSync(info, <cefrefptr_cefclient_t?>cefclient2, url3[0], browserSettings)
127+
128+
if <void*>browser == NULL: print "CreateBrowserSync: NULL"
129+
else: print "CreateBrowserSync: OK"
130+
print "lasterror: %s" % cefwindow.lasterror()
131+
132+
return 0
133+
134+
def wm_setfocus(hwnd, msg, wparam, lparam):
135+
#print "wm_setfocus"
136+
# @TODO
137+
pass
138+
139+
def wm_size(hwnd, msg, wparam, lparam):
140+
#print "wm_size"
141+
# @TODO
142+
pass
143+
144+
def wm_erasebkgnd(hwnd, msg, wparam, lparam):
145+
#print "wm_erasebkgnd"
146+
# @TODO
147+
pass
148+
149+
def wm_close(hwnd, msg, wparam, lparam):
150+
global browser
151+
if <void*>browser != NULL:
152+
print "wm_close: browser != NULL"
153+
print "browser.ParentWindowWillClose()"
154+
(<CefBrowser*>(browser.get())).ParentWindowWillClose()
155+
(<CefBrowser*>(browser.get())).CloseBrowser()
156+
win32gui.PostQuitMessage(0)
157+
return 0
158+

cefbindings.pyx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from libcpp cimport bool
2+
3+
cdef extern from *:
4+
ctypedef char* const_char_ptr "const char*"
5+
6+
cdef extern from "stddef.h":
7+
ctypedef void wchar_t
8+
9+
cdef extern from "include/internal/cef_ptr.h":
10+
cdef cppclass CefRefPtr[T]:
11+
T* get()
12+
13+
cdef extern from "include/internal/cef_types_wrappers.h":
14+
ctypedef struct CefSettings:
15+
pass
16+
ctypedef struct CefBrowserSettings:
17+
pass
18+
19+
cdef extern from "include/cef.h":
20+
cdef cppclass CefApp:
21+
pass
22+
cdef int CefInitialize(CefSettings, CefRefPtr[CefApp])
23+
cdef void CefRunMessageLoop()
24+
cdef void CefShutdown()
25+
cdef cppclass CefBase:
26+
pass
27+
cdef cppclass CefClient(CefBase):
28+
pass
29+
ctypedef int CefThreadId
30+
cdef bool CefCurrentlyOn(CefThreadId)
31+
cdef cppclass CefBrowser:
32+
void ParentWindowWillClose()
33+
void CloseBrowser()
34+
35+
cdef extern from "cefclient2.h":
36+
cdef cppclass CefClient2(CefClient):
37+
pass
38+
39+
ctypedef CefRefPtr[CefClient] cefrefptr_cefclient_t
40+
ctypedef CefRefPtr[CefClient2] cefrefptr_cefclient2_t
41+
ctypedef CefRefPtr[CefApp] cefrefptr_cefapp_t
42+
ctypedef CefRefPtr[CefBrowser] cefrefptr_cefbrowser_t
43+
44+
cdef extern from "include/cef.h" namespace "CefBrowser":
45+
cdef bool CreateBrowser(CefWindowInfo, CefRefPtr[CefClient], CefString, CefBrowserSettings)
46+
cdef CefRefPtr[CefBrowser] CreateBrowserSync(CefWindowInfo, CefRefPtr[CefClient], CefString, CefBrowserSettings)
47+
48+
cdef extern from "windows.h":
49+
ctypedef void *HWND
50+
ctypedef struct RECT:
51+
long left
52+
long top
53+
long right
54+
long bottom
55+
ctypedef char* LPCTSTR
56+
cdef HWND FindWindowA(LPCTSTR, LPCTSTR)
57+
cdef int CP_UTF8
58+
cdef int WideCharToMultiByte(int, int, wchar_t*, int, char*, int, char*, int*)
59+
60+
cdef extern from "include/internal/cef_win.h":
61+
ctypedef void* CefWindowHandle
62+
cdef cppclass CefWindowInfo:
63+
void SetAsChild(HWND, RECT)
64+
void SetAsOffScreen(HWND)
65+
HWND m_hWndParent
66+
HWND m_hWnd
67+
int m_x
68+
int m_y
69+
int m_nWidth
70+
int m_nHeight
71+
72+
cdef extern from "include/internal/cef_string.h":
73+
cdef cppclass CefString:
74+
CefString()
75+
bool FromASCII(char*)
76+
bool FromString(wchar_t*, size_t, bool)
77+
wchar_t* ToWString()
78+
char* c_str()

cefclient.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import cefapi
2+
cefapi.main()

cefclient2.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "include/cef.h"
2+
3+
class CefClient2 : public CefClient
4+
5+
{
6+
public:
7+
CefClient2()
8+
{
9+
}
10+
virtual ~CefClient2()
11+
{
12+
}
13+
14+
// CefClient methods
15+
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE
16+
{ return NULL; }
17+
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE
18+
{ return NULL; }
19+
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() OVERRIDE
20+
{ return NULL; }
21+
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE
22+
{ return NULL; }
23+
virtual CefRefPtr<CefFocusHandler> GetFocusHandler() OVERRIDE
24+
{ return NULL; }
25+
virtual CefRefPtr<CefKeyboardHandler> GetKeyboardHandler() OVERRIDE
26+
{ return NULL; }
27+
virtual CefRefPtr<CefPrintHandler> GetPrintHandler() OVERRIDE
28+
{ return NULL; }
29+
virtual CefRefPtr<CefV8ContextHandler> GetV8ContextHandler() OVERRIDE
30+
{ return NULL; }
31+
virtual CefRefPtr<CefDragHandler> GetDragHandler() OVERRIDE
32+
{ return NULL; }
33+
34+
protected:
35+
// Include the default reference counting implementation.
36+
IMPLEMENT_REFCOUNTING(CefClient2);
37+
// Include the default locking implementation.
38+
IMPLEMENT_LOCKING(CefClient2);
39+
};

cefpython.jpg

81 KB
Loading

cefwindow.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import win32gui
2+
import win32con
3+
import win32api
4+
import time
5+
6+
def onclose(hwnd, msg, wparam, lparam):
7+
win32gui.DestroyWindow(hwnd)
8+
9+
def ondestroy(hwnd, msg, wparam, lparam):
10+
win32gui.PostQuitMessage(0)
11+
12+
def lasterror():
13+
code = win32api.GetLastError()
14+
return "(%d) %s" % (code, win32api.FormatMessage(code))
15+
16+
def createwindow(title, classname, wndproc=None):
17+
if not wndproc:
18+
wndproc = {
19+
win32con.WM_CLOSE: onclose,
20+
win32con.WM_DESTROY: ondestroy
21+
}
22+
wc = win32gui.WNDCLASS()
23+
wc.hInstance = win32api.GetModuleHandle(None)
24+
wc.lpszClassName = classname
25+
wc.style = win32con.CS_GLOBALCLASS | win32con.CS_VREDRAW | win32con.CS_HREDRAW
26+
wc.hbrBackground = win32con.COLOR_WINDOW # + 1
27+
wc.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
28+
wc.lpfnWndProc = wndproc
29+
30+
#wc.cbClsExtra = 0
31+
#wc.cbWndExtra = 0
32+
33+
atomclass = win32gui.RegisterClass(wc)
34+
print "win32gui.RegisterClass(wc)"
35+
print lasterror()
36+
if not atomclass:
37+
print lasterror() # this is probably unneeded, as win32gui.RegisterClass automatically handles errors
38+
return
39+
hwnd = win32gui.CreateWindow(classname, title,
40+
win32con.WS_OVERLAPPEDWINDOW | win32con.WS_CLIPCHILDREN | win32con.WS_VISIBLE,
41+
200, 200, 600, 400, # x pos, y pos, width, height
42+
0, 0, wc.hInstance, None)
43+
print "hwnd=%s" % hwnd
44+
return hwnd
45+
46+
def messageloop(classname):
47+
while win32gui.PumpWaitingMessages() == 0:
48+
time.sleep(0.001)
49+
win32gui.UnregisterClass(classname, None)
50+
51+
if __name__ == "__main__":
52+
hwnd = createwindow("Test window", "test_class")
53+
messageloop("test_class")

cefwindow.pyc

2.14 KB
Binary file not shown.

0 commit comments

Comments
 (0)
X Tutup