]> git.tdb.fi Git - libs/gui.git/blobdiff - source/graphics/windows/window.cpp
Split platform-specific parts into separate directories
[libs/gui.git] / source / graphics / windows / window.cpp
diff --git a/source/graphics/windows/window.cpp b/source/graphics/windows/window.cpp
new file mode 100644 (file)
index 0000000..c5cdc62
--- /dev/null
@@ -0,0 +1,174 @@
+#include <windowsx.h>
+#include <msp/core/application.h>
+#include <msp/core/systemerror.h>
+#include "window.h"
+#include "window_private.h"
+
+using namespace std;
+
+namespace {
+
+LRESULT CALLBACK wndproc_(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
+{
+       if(msg==WM_CREATE)
+       {
+               CREATESTRUCT *cs = reinterpret_cast<CREATESTRUCT *>(lparam);
+               SetWindowLong(hwnd, 0, reinterpret_cast<LONG>(cs->lpCreateParams));
+       }
+       else
+       {
+               Msp::Graphics::Window *wnd = reinterpret_cast<Msp::Graphics::Window *>(GetWindowLong(hwnd, 0));
+               Msp::Graphics::Window::Event ev;
+               ev.msg = msg;
+               ev.wparam = wparam;
+               ev.lparam = lparam;
+               if(wnd && wnd->event(ev))
+                       return 0;
+       }
+
+       return DefWindowProc(hwnd, msg, wparam, lparam);
+}
+
+}
+
+namespace Msp {
+namespace Graphics {
+
+void Window::platform_init()
+{
+       static bool wndclass_created = false;
+
+       if(!wndclass_created)
+       {
+               WNDCLASSEX wndcl;
+
+               wndcl.cbSize = sizeof(WNDCLASSEX);
+               wndcl.style = 0;
+               wndcl.lpfnWndProc = &wndproc_;
+               wndcl.cbClsExtra = 0;
+               wndcl.cbWndExtra = sizeof(Window *);
+               wndcl.hInstance = reinterpret_cast<HINSTANCE>(Application::get_data());
+               wndcl.hIcon = 0;
+               wndcl.hCursor = LoadCursor(0, IDC_ARROW);
+               wndcl.hbrBackground = 0;
+               wndcl.lpszMenuName = 0;
+               wndcl.lpszClassName = "mspgui";
+               wndcl.hIconSm = 0;
+
+               if(!RegisterClassEx(&wndcl))
+                       throw system_error("RegisterClassEx");
+
+               wndclass_created = true;
+       }
+
+       RECT rect;
+       SetRect(&rect, 0, 0, options.width, options.height);
+
+       int style = (options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
+       if(!options.resizable)
+               style &= ~WS_THICKFRAME;
+       int exstyle = (options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
+       AdjustWindowRectEx(&rect, style, false, exstyle);
+
+       priv->window = CreateWindowEx(exstyle,
+               "mspgui",
+               "Window",
+               style,
+               CW_USEDEFAULT, CW_USEDEFAULT,
+               rect.right-rect.left, rect.bottom-rect.top,
+               0,
+               0,
+               reinterpret_cast<HINSTANCE>(Application::get_data()),
+               this);
+       if(!priv->window)
+               throw system_error("CreateWindowEx");
+}
+
+void Window::platform_cleanup()
+{
+       if(priv->window)
+               CloseWindow(priv->window);
+}
+
+void Window::set_title(const string &title)
+{
+       SetWindowText(priv->window, title.c_str());
+}
+
+void Window::platform_reconfigure(bool fullscreen_changed)
+{
+       RECT rect;
+       SetRect(&rect, 0, 0, options.width, options.height);
+
+       int style = (options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
+       if(!options.resizable)
+               style &= ~WS_THICKFRAME;
+       int exstyle = (options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
+       AdjustWindowRectEx(&rect, style, false, exstyle);
+
+       if(fullscreen_changed)
+       {
+               hide();
+               SetWindowLong(priv->window, GWL_EXSTYLE, exstyle);
+               SetWindowLong(priv->window, GWL_STYLE, style);
+               show();
+       }
+
+       if(options.fullscreen)
+               SetWindowPos(priv->window, 0, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOZORDER);
+       else
+               SetWindowPos(priv->window, 0, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOMOVE|SWP_NOZORDER);
+}
+
+void Window::show_cursor(bool s)
+{
+       ShowCursor(s);
+}
+
+void Window::warp_pointer(int, int)
+{
+}
+
+void Window::platform_show()
+{
+       ShowWindow(priv->window, SW_SHOWNORMAL);
+}
+
+void Window::platform_hide()
+{
+       ShowWindow(priv->window, SW_HIDE);
+}
+
+bool Window::event(const Event &evnt)
+{
+       switch(evnt.msg)
+       {
+       case WM_KEYDOWN:
+       case WM_KEYUP:
+       case WM_LBUTTONDOWN:
+       case WM_LBUTTONUP:
+       case WM_MBUTTONDOWN:
+       case WM_MBUTTONUP:
+       case WM_RBUTTONDOWN:
+       case WM_RBUTTONUP:
+       case WM_MOUSEWHEEL:
+       case WM_MOUSEMOVE:
+               signal_input_event.emit(evnt);
+               break;
+       case WM_SIZE:
+               options.width = LOWORD(evnt.lparam);
+               options.height = HIWORD(evnt.lparam);
+               signal_resize.emit(options.width, options.height);
+               break;
+       case WM_CLOSE:
+               signal_close.emit();
+               break;
+       default:
+               return false;
+       }
+
+       return true;
+}
+
+} // namespace Graphics
+} // namespace Msp