diff options
Diffstat (limited to 'plugingui/nativewindow_win32.cc')
-rw-r--r-- | plugingui/nativewindow_win32.cc | 547 |
1 files changed, 297 insertions, 250 deletions
diff --git a/plugingui/nativewindow_win32.cc b/plugingui/nativewindow_win32.cc index 871bafe..cf39010 100644 --- a/plugingui/nativewindow_win32.cc +++ b/plugingui/nativewindow_win32.cc @@ -26,49 +26,56 @@ */ #include "nativewindow_win32.h" -#ifdef WIN32 - #include "window.h" -LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) +#include <cstring> + +namespace GUI { + +LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg, + WPARAM wp, LPARAM lp) { - GUI::NativeWindowWin32 *native = - (GUI::NativeWindowWin32 *)GetWindowLongPtr(hwnd, GWLP_USERDATA); + NativeWindowWin32* native = + (NativeWindowWin32*)GetWindowLongPtr(hwnd, GWLP_USERDATA); - // NOTE: 'native' is NULL intil the WM_CREATE message has been handled. - if(!native) return DefWindowProc(hwnd, msg, wp, lp); + // NOTE: 'native' is nullptr intil the WM_CREATE message has been handled. + if(!native) + { + return DefWindowProc(hwnd, msg, wp, lp); + } - GUI::Window *window = native->window; + Window& window = native->window; switch(msg) { case WM_SIZE: - { - static bool first = true; - if(!first) { - GUI::ResizeEvent *e = new GUI::ResizeEvent(); - e->width = LOWORD(lp); - e->height = HIWORD(lp); - native->event = e; - first = false; - } - } + { + static bool first = true; + if(!first) + { + ResizeEvent* resizeEvent = new ResizeEvent(); + resizeEvent->width = LOWORD(lp); + resizeEvent->height = HIWORD(lp); + native->event = resizeEvent; + first = false; + } + } break; case WM_MOVE: - { -// GUI::MoveEvent *e = new GUI::MoveEvent(); -// e->x = (int)(short) LOWORD(lp); -// e->y = (int)(short) HIWORD(lp); -// native->event = e; - } + { +// MoveEvent* moveEvent = new MoveEvent(); +// moveEvent->x = (short)LOWORD(lp); +// moveEvent->y = (short)HIWORD(lp); +// native->event = moveEvent; + } break; case WM_CLOSE: - { - GUI::CloseEvent *e = new GUI::CloseEvent(); - native->event = e; - } - break; + { + CloseEvent* closeEvent = new CloseEvent(); + native->event = closeEvent; + } + break; // HWND child, old; // old = 0; @@ -83,31 +90,30 @@ LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) // else PostQuitMessage(0); // return 0; case WM_MOUSEMOVE: - { - - GUI::MouseMoveEvent *e = new GUI::MouseMoveEvent(); - e->x = (int)(short) LOWORD(lp); - e->y = (int)(short) HIWORD(lp); - native->event = e; - } + { + MouseMoveEvent* mouseMoveEvent = new MouseMoveEvent(); + mouseMoveEvent->x = (short)LOWORD(lp); + mouseMoveEvent->y = (short)HIWORD(lp); + native->event = mouseMoveEvent; + } break; case WM_MOUSEWHEEL: - { - GUI::ScrollEvent *e = new GUI::ScrollEvent(); - - // NOTE: lp is coordinates in screen space, not client space. - POINT p; - p.x = (int)(short) LOWORD(lp); - p.y = (int)(short) HIWORD(lp); - ScreenToClient(hwnd, &p); - - e->x = p.x; - e->y = p.y; - e->delta = -1 * (short)HIWORD(wp) / 60; - native->event = e; - } - break; + { + ScrollEvent* scrollEvent = new ScrollEvent(); + + // NOTE: lp is coordinates in screen space, not client space. + POINT p; + p.x = (short)LOWORD(lp); + p.y = (short)HIWORD(lp); + ScreenToClient(hwnd, &p); + + scrollEvent->x = p.x; + scrollEvent->y = p.y; + scrollEvent->delta = -1 * (short)HIWORD(wp) / 60; + native->event = scrollEvent; + } + break; case WM_LBUTTONUP: case WM_LBUTTONDBLCLK: @@ -118,283 +124,324 @@ LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) case WM_MBUTTONUP: case WM_MBUTTONDBLCLK: case WM_MBUTTONDOWN: - { - GUI::ButtonEvent *e = new GUI::ButtonEvent(); - e->x = (int)(short) LOWORD(lp); - e->y = (int)(short) HIWORD(lp); - - if(msg == WM_LBUTTONUP || - msg == WM_LBUTTONDBLCLK || - msg == WM_LBUTTONDOWN) e->button = 0; - - if(msg == WM_RBUTTONUP || - msg == WM_RBUTTONDBLCLK || - msg == WM_RBUTTONDOWN) e->button = 1; - - if(msg == WM_MBUTTONUP || - msg == WM_MBUTTONDBLCLK || - msg == WM_MBUTTONDOWN) e->button = 2; - - e->direction = 0; - if(msg == WM_LBUTTONUP || - msg == WM_RBUTTONUP || - msg == WM_MBUTTONUP) e->direction = -1; - - if(msg == WM_LBUTTONDOWN || - msg == WM_RBUTTONDOWN || - msg == WM_MBUTTONDOWN) e->direction = 1; - - e->doubleclick = (msg == WM_LBUTTONDBLCLK || - msg == WM_RBUTTONDBLCLK || - msg == WM_MBUTTONDBLCLK); - - native->event = e; - } + { + ButtonEvent* buttonEvent = new ButtonEvent(); + + buttonEvent->x = (short)LOWORD(lp); + buttonEvent->y = (short)HIWORD(lp); + + if(msg == WM_LBUTTONUP || + msg == WM_LBUTTONDBLCLK || + msg == WM_LBUTTONDOWN) + { + buttonEvent->button = MouseButton::left; + } + else if(msg == WM_RBUTTONUP || + msg == WM_RBUTTONDBLCLK || + msg == WM_RBUTTONDOWN) + { + buttonEvent->button = MouseButton::middle; + } + else if(msg == WM_MBUTTONUP || + msg == WM_MBUTTONDBLCLK || + msg == WM_MBUTTONDOWN) + { + buttonEvent->button = MouseButton::right; + } + else + { + delete buttonEvent; + break; // unknown button + } + + if(msg == WM_LBUTTONUP || + msg == WM_RBUTTONUP || + msg == WM_MBUTTONUP) + { + buttonEvent->direction = Direction::up; + } + else if(msg == WM_LBUTTONDOWN || + msg == WM_RBUTTONDOWN || + msg == WM_MBUTTONDOWN) + { + buttonEvent->direction = Direction::down; + } + + buttonEvent->doubleClick = (msg == WM_LBUTTONDBLCLK || + msg == WM_RBUTTONDBLCLK || + msg == WM_MBUTTONDBLCLK); + + native->event = buttonEvent; + } break; case WM_KEYDOWN: - { - GUI::KeyEvent *e = new GUI::KeyEvent(); - //printf("wp: %d\n", wp); - switch(wp) { - case 37: e->keycode = GUI::KeyEvent::KEY_LEFT; break; - case 39: e->keycode = GUI::KeyEvent::KEY_RIGHT; break; - case 38: e->keycode = GUI::KeyEvent::KEY_UP; break; - case 40: e->keycode = GUI::KeyEvent::KEY_DOWN; break; - case 8: e->keycode = GUI::KeyEvent::KEY_BACKSPACE; break; - case 46: e->keycode = GUI::KeyEvent::KEY_DELETE; break; - case 36: e->keycode = GUI::KeyEvent::KEY_HOME; break; - case 35: e->keycode = GUI::KeyEvent::KEY_END; break; - case 33: e->keycode = GUI::KeyEvent::KEY_PGUP; break; - case 34: e->keycode = GUI::KeyEvent::KEY_PGDOWN; break; - case 13: e->keycode = GUI::KeyEvent::KEY_ENTER; break; - default: e->keycode = GUI::KeyEvent::KEY_UNKNOWN; break; - } - e->text = ""; - e->direction = -1; - native->event = e; - } + case WM_KEYUP: + { + KeyEvent* keyEvent = new KeyEvent(); + + switch(wp) { + case VK_LEFT: keyEvent->keycode = Key::left; break; + case VK_RIGHT: keyEvent->keycode = Key::right; break; + case VK_UP: keyEvent->keycode = Key::up; break; + case VK_DOWN: keyEvent->keycode = Key::down; break; + case VK_BACK: keyEvent->keycode = Key::backspace; break; + case VK_DELETE: keyEvent->keycode = Key::deleteKey; break; + case VK_HOME: keyEvent->keycode = Key::home; break; + case VK_END: keyEvent->keycode = Key::end; break; + case VK_PRIOR: keyEvent->keycode = Key::pageUp; break; + case VK_NEXT: keyEvent->keycode = Key::pageDown; break; + case VK_RETURN: keyEvent->keycode = Key::enter; break; + default: keyEvent->keycode = Key::unknown; break; + } + + keyEvent->text = ""; + keyEvent->direction = + (msg == WM_KEYDOWN) ? Direction::down : Direction::up; + + native->event = keyEvent; + } break; case WM_CHAR: - { - //printf("WM_CHAR %d %d\n", (int)lp, (int)wp); - if(wp >= ' ') { // Filter control chars. - GUI::KeyEvent *e = new GUI::KeyEvent(); - e->keycode = GUI::KeyEvent::KEY_CHARACTER; - e->text += (char)wp; - e->direction = -1; - native->event = e; - } - } + { + if(wp >= ' ') // Filter control chars. + { + KeyEvent* keyEvent = new KeyEvent(); + keyEvent->keycode = Key::character; + keyEvent->text += (char)wp; + keyEvent->direction = Direction::up; + native->event = keyEvent; + } + } break; case WM_PAINT: - { - GUI::RepaintEvent *e = new GUI::RepaintEvent(); - e->x = 0; - e->y = 0; - e->width = 100; - e->height = 100; - native->event = e; - - // Move to window.h (in class) - HDC pDC; - HBITMAP old; - HBITMAP ourbitmap; - int * framebuf; - GUI::PixelBuffer &px = window->wpixbuf; - - { // Create bitmap (move to window.cc) - HDC hDC; - BITMAPINFO bitmapinfo; - hDC = CreateCompatibleDC(NULL); - bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); - bitmapinfo.bmiHeader.biWidth = px.width; - bitmapinfo.bmiHeader.biHeight = -px.height; /* top-down */ - bitmapinfo.bmiHeader.biPlanes = 1; - bitmapinfo.bmiHeader.biBitCount = 32; - bitmapinfo.bmiHeader.biCompression = BI_RGB; - bitmapinfo.bmiHeader.biSizeImage = 0; - bitmapinfo.bmiHeader.biClrUsed = 256; - bitmapinfo.bmiHeader.biClrImportant = 256; - ourbitmap=CreateDIBSection(hDC, &bitmapinfo, - DIB_RGB_COLORS, (void**)&framebuf, 0, 0); - pDC=CreateCompatibleDC(NULL); - old = (HBITMAP__*)SelectObject(pDC, ourbitmap); - DeleteDC(hDC); - } - - { // Copy GUI::PixelBuffer to framebuffer (move to window.cc) - int i,j,k; - for (k=0,i=0;i<(int)px.height;i++) { - for (j=0;j<(int)px.width;j++,k++) { - *(framebuf+k)=RGB(px.buf[(j + i * px.width) * 3 + 2], - px.buf[(j + i * px.width) * 3 + 1], - px.buf[(j + i * px.width) * 3 + 0]); - } - } - } - - PAINTSTRUCT ps; - HDC hdc = BeginPaint(native->m_hwnd, &ps); - BitBlt(hdc, 0, 0, px.width, px.height, pDC, 0, 0, SRCCOPY); - EndPaint(native->m_hwnd, &ps); - - { // Destroy bitmap (move to window.cc) - SelectObject(pDC,old); - DeleteDC(pDC); - DeleteObject(ourbitmap); - - } - } + { + RepaintEvent* repaintEvent = new RepaintEvent(); + repaintEvent->x = 0; + repaintEvent->y = 0; + repaintEvent->width = 100; + repaintEvent->height = 100; + native->event = repaintEvent; + + // Move to window.h (in class) + HDC pDC; + HBITMAP old; + HBITMAP ourbitmap; + int* framebuf; + PixelBuffer& px = window.wpixbuf; + + { // Create bitmap + HDC hDC; + BITMAPINFO bitmapinfo; + hDC = CreateCompatibleDC(nullptr); + bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bitmapinfo.bmiHeader.biWidth = px.width; + bitmapinfo.bmiHeader.biHeight = -px.height; // top-down + bitmapinfo.bmiHeader.biPlanes = 1; + bitmapinfo.bmiHeader.biBitCount = 32; + bitmapinfo.bmiHeader.biCompression = BI_RGB; + bitmapinfo.bmiHeader.biSizeImage = 0; + bitmapinfo.bmiHeader.biClrUsed = 256; + bitmapinfo.bmiHeader.biClrImportant = 256; + ourbitmap = CreateDIBSection(hDC, &bitmapinfo, + DIB_RGB_COLORS, (void**)&framebuf, 0, 0); + pDC=CreateCompatibleDC(nullptr); + old = (HBITMAP__*)SelectObject(pDC, ourbitmap); + DeleteDC(hDC); + } + + { // Copy PixelBuffer to framebuffer + int i, j, k; + for(k = 0, i = 0; i < (int)px.height; ++i) + { + for(j = 0; j < (int)px.width; ++j, ++k) + { + *(framebuf + k) = RGB(px.buf[(j + i * px.width) * 3 + 2], + px.buf[(j + i * px.width) * 3 + 1], + px.buf[(j + i * px.width) * 3 + 0]); + } + } + } + + PAINTSTRUCT ps; + HDC hdc = BeginPaint(native->m_hwnd, &ps); + BitBlt(hdc, 0, 0, px.width, px.height, pDC, 0, 0, SRCCOPY); + EndPaint(native->m_hwnd, &ps); + + { // Destroy bitmap (move to window.cc) + SelectObject(pDC,old); + DeleteDC(pDC); + DeleteObject(ourbitmap); + } + } + return DefWindowProc(hwnd, msg, wp, lp); } return DefWindowProc(hwnd, msg, wp, lp); } -// Delared in eventhandler.cc -LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp); - -GUI::NativeWindowWin32::NativeWindowWin32(GUI::Window *window) - : GUI::NativeWindow() +NativeWindowWin32::NativeWindowWin32(Window& window) + : window(window) { - this->window = window; - WNDCLASSEX wcex; WNDID wndId; - m_hwnd = 0; - m_className = NULL; - event = NULL; + std::memset(&wcex, 0, sizeof(wcex)); - memset(&wcex, 0, sizeof(wcex)); - //Time to register a window class. - //Generic flags and everything. cbWndExtra is the size of a pointer to an - // object - we need this in the wndproc handler. - + //Generic flags and everything. cbWndExtra is the size of a pointer to an + // object - we need this in the wndproc handler. + wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_DBLCLKS;//class_style; wcex.lpfnWndProc = (WNDPROC)dialogProc; - wcex.hCursor = LoadCursor(NULL, IDC_ARROW); - // Set data: + wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); + // Set data: wcex.cbWndExtra = sizeof(NativeWindowWin32*); // Size of data. - wcex.hInstance = GetModuleHandle(NULL); + wcex.hInstance = GetModuleHandle(nullptr); - // if(ex_style && WS_EX_TRANSPARENT == WS_EX_TRANSPARENT) { - // wcex.hbrBackground = NULL; - // } else { - wcex.hbrBackground = NULL;//(HBRUSH) COLOR_BACKGROUND + 1; - // } - - wcex.lpszClassName = m_className = strdup("DrumGizmoClass"); + // if(ex_style && WS_EX_TRANSPARENT == WS_EX_TRANSPARENT) { + // wcex.hbrBackground = nullptr; + // } else { + wcex.hbrBackground = nullptr;//(HBRUSH) COLOR_BACKGROUND + 1; + // } + + const char* name = "DrumGizmoClass"; + char* c_name = (char*)malloc(strlen(name) + 1); + strcpy(c_name, name); + wcex.lpszClassName = m_className = c_name; RegisterClassEx(&wcex); - /* + /* if(parent) { style = style | WS_CHILD; wndId = parent->getWndId(); } else { - */ - //style = style | WS_OVERLAPPEDWINDOW; - wndId = 0; - // } + */ + //style = style | WS_OVERLAPPEDWINDOW; + wndId = 0; + // } m_hwnd = CreateWindowEx(0/*ex_style*/, m_className, - "DGBasisWidget", - (WS_OVERLAPPEDWINDOW | WS_VISIBLE), - window->x(), window->y(), - window->width(), window->height(), - wndId, NULL, - GetModuleHandle(NULL), NULL); + "DGBasisWidget", + (WS_OVERLAPPEDWINDOW | WS_VISIBLE), + window.x(), window.y(), + window.width(), window.height(), + wndId, nullptr, + GetModuleHandle(nullptr), nullptr); SetWindowLongPtr(m_hwnd, GWLP_USERDATA, (LONG_PTR)this); } -GUI::NativeWindowWin32::~NativeWindowWin32() +NativeWindowWin32::~NativeWindowWin32() { - UnregisterClass(m_className, GetModuleHandle(NULL)); + UnregisterClass(m_className, GetModuleHandle(nullptr)); free(m_className); } -void GUI::NativeWindowWin32::setFixedSize(int width, int height) +void NativeWindowWin32::setFixedSize(int width, int height) { - resize(width, height); - LONG style = GetWindowLong(m_hwnd, GWL_STYLE); - style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX); - SetWindowLong(m_hwnd, GWL_STYLE, style); + resize(width, height); + LONG style = GetWindowLong(m_hwnd, GWL_STYLE); + style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX); + SetWindowLong(m_hwnd, GWL_STYLE, style); } -void GUI::NativeWindowWin32::resize(int width, int height) +void NativeWindowWin32::resize(int width, int height) { - SetWindowPos(m_hwnd, NULL, -1, -1, (int)width, (int)height, SWP_NOMOVE); - RECT r; - GetClientRect(m_hwnd, &r); - int w = width - r.right; - int h = height - r.bottom; + SetWindowPos(m_hwnd, nullptr, -1, -1, (int)width, (int)height, SWP_NOMOVE); + RECT r; + GetClientRect(m_hwnd, &r); + int w = width - r.right; + int h = height - r.bottom; - SetWindowPos(m_hwnd, NULL, -1, -1, width + w, height + h, SWP_NOMOVE); + SetWindowPos(m_hwnd, nullptr, -1, -1, width + w, height + h, SWP_NOMOVE); } -void GUI::NativeWindowWin32::move(int x, int y) +void NativeWindowWin32::move(int x, int y) { - SetWindowPos(m_hwnd, NULL, (int)x, (int)y, -1, -1, SWP_NOSIZE); + SetWindowPos(m_hwnd, nullptr, (int)x, (int)y, -1, -1, SWP_NOSIZE); } -void GUI::NativeWindowWin32::show() +void NativeWindowWin32::show() { - ShowWindow(m_hwnd, SW_SHOW); + ShowWindow(m_hwnd, SW_SHOW); } -void GUI::NativeWindowWin32::handleBuffer() +void NativeWindowWin32::handleBuffer() { } -void GUI::NativeWindowWin32::hide() +void NativeWindowWin32::hide() { - ShowWindow(m_hwnd, SW_HIDE); + ShowWindow(m_hwnd, SW_HIDE); } -void GUI::NativeWindowWin32::redraw() +void NativeWindowWin32::redraw() { - RedrawWindow(m_hwnd, NULL, NULL, RDW_ERASE|RDW_INVALIDATE); - UpdateWindow(m_hwnd); + RedrawWindow(m_hwnd, nullptr, nullptr, RDW_ERASE|RDW_INVALIDATE); + UpdateWindow(m_hwnd); } -void GUI::NativeWindowWin32::setCaption(const std::string &caption) +void NativeWindowWin32::setCaption(const std::string &caption) { - SetWindowText(m_hwnd, caption.c_str()); + SetWindowText(m_hwnd, caption.c_str()); +} + +void NativeWindowWin32::grabMouse(bool grab) +{ + if(grab) + { + SetCapture(m_hwnd); + } + else + { + ReleaseCapture(); + } } -void GUI::NativeWindowWin32::grabMouse(bool grab) +bool NativeWindowWin32::hasEvent() { - if(grab) SetCapture(m_hwnd); - else ReleaseCapture(); + MSG msg; + return PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE) != 0; } -bool GUI::NativeWindowWin32::hasEvent() +Event* NativeWindowWin32::getNextEvent() { - MSG msg; - return PeekMessage(&msg, NULL, 0, 0, 0) != 0; + Event* event = nullptr; + + MSG msg; + if(GetMessage(&msg, nullptr, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + event = this->event; + this->event = nullptr; + + return event; } -GUI::Event *GUI::NativeWindowWin32::getNextEvent() +Event* NativeWindowWin32::peekNextEvent() { - Event *event = NULL; + Event* event = nullptr; - MSG msg; - if(GetMessage(&msg, NULL, 0, 0)) { - TranslateMessage(&msg); - DispatchMessage(&msg); + MSG msg; + if(PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); } - event = this->event; - this->event = NULL; + event = this->event; + this->event = nullptr; - return event; + return event; } -#endif/*WIN32*/ +} // GUI:: |