func (wi *windowInternal) initializeFromExisting(window WindowHandle, settings ContextSettings) ThreadError { wi.window = window // We change the event procedure of the control (it is important to save the old one) C.__SetWindowLongPtr(wi.window.Handle, C.GWLP_USERDATA, unsafe.Pointer(wi)) callback := C.__SetWindowLongPtr(wi.window.Handle, C.GWLP_WNDPROC, unsafe.Pointer(C.pGlobalOnEvent)) wi.callback = unsafe.Pointer(uintptr(callback)) return nil }
//export globalOnEvent func globalOnEvent(handle C.HWND, message C.UINT, wParam C.WPARAM, lParam C.LPARAM) C.LRESULT { // Associate handle and Window instance when the creation message is received if message == C.WM_CREATE { // Get WindowImplWin32 instance (it was passed as the last argument of CreateWindow) window := (*C.CREATESTRUCT)(unsafe.Pointer(uintptr(lParam))).lpCreateParams // Set as the "user data" parameter of the window C.__SetWindowLongPtr(handle, C.GWLP_USERDATA, unsafe.Pointer(window)) } // Get the WindowImpl instance corresponding to the window handle // Forward the event to the appropriate function if wi := (*windowInternal)(C.__GetWindowLongPtr(handle, C.GWLP_USERDATA)); wi != nil { events, errors := wi.processEvent(message, wParam, lParam) wi.events = append(wi.events, events...) wi.eventErrors = append(wi.eventErrors, errors...) if callback := C.WNDPROC(wi.callback); callback != nil { return C.CallWindowProc(callback, handle, message, wParam, lParam) } } // We don't forward the WM_CLOSE message to prevent the OS from automatically destroying the window if message == C.WM_CLOSE { return 0 } return C.DefWindowProcW(handle, message, wParam, lParam) }
func (wi *windowInternal) close() ThreadError { // Destroy the custom icon, if any if wi.icon != nil { C.DestroyIcon(wi.icon) } if wi.callback == nil { // Destroy the window if wi.window.IsValid() { C.DestroyWindow(wi.window.Handle) } } else { // The window is external : remove the hook on its message callback C.__SetWindowLongPtr(wi.window.Handle, C.GWLP_WNDPROC, wi.callback) } return nil }