|
| 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 | + |
0 commit comments