LRESULT CALLBACK WindowProc::wndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { MSG msg { hwnd, uMsg, wParam, lParam }; // We dispatch certain messages back to the child widget, so that their // potential callbacks will die along with the child when the time comes HWND handler = getHandler(msg); // Try to get the this pointer Widget* w = hwnd_cast<Widget*>(handler); if(w) { if(uMsg == WM_NCDESTROY) { w->kill(); return returnUnknown(msg); } LRESULT res = 0; if(w->handleMessage(msg, res)) { return res; } } if(handler != hwnd) { w = hwnd_cast<Widget*>(hwnd); } // If this fails there's something wrong dwtassert(w, "Expected to get a pointer to a widget - something's wrong"); if(!w) { return returnUnknown(msg); } return w->getDispatcher().chain(msg); }
void Widget::setHandle(HWND hwnd) { if(itsHandle) { throw DWTException("You may not attach to a widget that's already attached"); } itsHandle = hwnd; dwtassert((::GetWindowLongPtr(hwnd, GWLP_USERDATA) == 0), "Userdata already set"); ::SetLastError(0); LONG_PTR ret = ::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); if(ret == 0 && ::GetLastError() != 0) { throw Win32Exception("Error while setting pointer"); } }