diff options
author | crupest <crupest@outlook.com> | 2019-03-19 16:21:54 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-03-19 16:21:54 +0800 |
commit | 5dc738a57930271194bd86673eb86f149096a7b2 (patch) | |
tree | 71174aba0d1c0918cc7d7a1be0b86ec0d5c20401 /src/application.cpp | |
parent | 06edefebe8dfb138404397fb2c46732da6cd733a (diff) | |
download | cru-5dc738a57930271194bd86673eb86f149096a7b2.tar.gz cru-5dc738a57930271194bd86673eb86f149096a7b2.tar.bz2 cru-5dc738a57930271194bd86673eb86f149096a7b2.zip |
...
Diffstat (limited to 'src/application.cpp')
-rw-r--r-- | src/application.cpp | 192 |
1 files changed, 85 insertions, 107 deletions
diff --git a/src/application.cpp b/src/application.cpp index c3669f72..e580b56b 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -2,135 +2,113 @@ #include "exception.hpp" #include "timer.hpp" -#include "ui/window.hpp" #include "ui/cursor.hpp" +#include "ui/window_class.hpp" namespace cru { - constexpr auto god_window_class_name = L"GodWindowClass"; - constexpr int invoke_later_message_id = WM_USER + 2000; - - - LRESULT CALLBACK GodWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) - { - const auto app = Application::GetInstance(); - - if (app) - { - const auto result = app->GetGodWindow()->HandleGodWindowMessage(hWnd, uMsg, wParam, lParam); - if (result.has_value()) - return result.value(); - else - return DefWindowProc(hWnd, uMsg, wParam, lParam); - } - else - return DefWindowProc(hWnd, uMsg, wParam, lParam); - } +constexpr auto god_window_class_name = L"GodWindowClass"; +constexpr int invoke_later_message_id = WM_USER + 2000; + +LRESULT CALLBACK GodWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, + LPARAM lParam) { + const auto app = Application::GetInstance(); + + if (app) { + const auto result = + app->GetGodWindow()->HandleGodWindowMessage(hWnd, uMsg, wParam, lParam); + if (result.has_value()) + return result.value(); + else + return DefWindowProc(hWnd, uMsg, wParam, lParam); + } else + return DefWindowProc(hWnd, uMsg, wParam, lParam); +} - GodWindow::GodWindow(Application* application) - { - const auto h_instance = application->GetInstanceHandle(); +GodWindow::GodWindow(Application* application) { + const auto h_instance = application->GetInstanceHandle(); - god_window_class_ = std::make_unique<ui::WindowClass>(god_window_class_name, GodWndProc, h_instance); + god_window_class_ = std::make_unique<ui::WindowClass>(god_window_class_name, + GodWndProc, h_instance); - hwnd_ = CreateWindowEx(0, - god_window_class_name, - L"", 0, - CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, - HWND_MESSAGE, nullptr, h_instance, nullptr - ); + hwnd_ = CreateWindowEx(0, god_window_class_name, L"", 0, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, + HWND_MESSAGE, nullptr, h_instance, nullptr); - if (hwnd_ == nullptr) - throw std::runtime_error("Failed to create window."); - } + if (hwnd_ == nullptr) throw std::runtime_error("Failed to create window."); +} - GodWindow::~GodWindow() - { - ::DestroyWindow(hwnd_); +GodWindow::~GodWindow() { ::DestroyWindow(hwnd_); } + +std::optional<LRESULT> GodWindow::HandleGodWindowMessage(HWND hwnd, int msg, + WPARAM w_param, + LPARAM l_param) { + switch (msg) { + case invoke_later_message_id: { + const auto p_action = reinterpret_cast<std::function<void()>*>(w_param); + (*p_action)(); + delete p_action; + return 0; } - - std::optional<LRESULT> GodWindow::HandleGodWindowMessage(HWND hwnd, int msg, WPARAM w_param, LPARAM l_param) - { - switch (msg) - { - case invoke_later_message_id: - { - const auto p_action = reinterpret_cast<std::function<void()>*>(w_param); - (*p_action)(); - delete p_action; - return 0; - } - case WM_TIMER: - { - const auto id = static_cast<UINT_PTR>(w_param); - const auto action = TimerManager::GetInstance()->GetAction(id); - if (action.has_value()) - { - (action.value().second)(); - if (!action.value().first) - TimerManager::GetInstance()->KillTimer(id); - return 0; - } - break; - } - default: - return std::nullopt; - } - return std::nullopt; + case WM_TIMER: { + const auto id = static_cast<UINT_PTR>(w_param); + const auto action = TimerManager::GetInstance()->GetAction(id); + if (action.has_value()) { + (action.value().second)(); + if (!action.value().first) TimerManager::GetInstance()->KillTimer(id); + return 0; + } + break; } + default: + return std::nullopt; + } + return std::nullopt; +} +Application* Application::instance_ = nullptr; +Application* Application::GetInstance() { return instance_; } - Application* Application::instance_ = nullptr; - - Application * Application::GetInstance() { - return instance_; - } - - Application::Application(HINSTANCE h_instance) - : h_instance_(h_instance) { - - if (instance_) - throw std::runtime_error("A application instance already exists."); +Application::Application(HINSTANCE h_instance) : h_instance_(h_instance) { + if (instance_) + throw std::runtime_error("A application instance already exists."); - instance_ = this; + instance_ = this; - if (!::IsWindows8OrGreater()) - throw std::runtime_error("Must run on Windows 8 or later."); + if (!::IsWindows8OrGreater()) + throw std::runtime_error("Must run on Windows 8 or later."); - god_window_ = std::make_unique<GodWindow>(this); + god_window_ = std::make_unique<GodWindow>(this); - ui::cursors::LoadSystemCursors(); - } + ui::cursors::LoadSystemCursors(); +} - Application::~Application() - { - for (auto i = singleton_list_.crbegin(); i != singleton_list_.crend(); ++i) - delete *i; - instance_ = nullptr; - } +Application::~Application() { + for (auto i = singleton_list_.crbegin(); i != singleton_list_.crend(); ++i) + delete *i; + instance_ = nullptr; +} - int Application::Run() - { - MSG msg; +int Application::Run() { + MSG msg; - while (GetMessage(&msg, nullptr, 0, 0)) - { - TranslateMessage(&msg); - DispatchMessage(&msg); - } + while (GetMessage(&msg, nullptr, 0, 0)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } - return static_cast<int>(msg.wParam); - } + return static_cast<int>(msg.wParam); +} - void Application::Quit(const int quit_code) { - ::PostQuitMessage(quit_code); - } +void Application::Quit(const int quit_code) { ::PostQuitMessage(quit_code); } - void InvokeLater(const std::function<void()>& action) { - //copy the action to a safe place - auto p_action_copy = new std::function<void()>(action); +void InvokeLater(const std::function<void()>& action) { + // copy the action to a safe place + auto p_action_copy = new std::function<void()>(action); - if (PostMessageW(Application::GetInstance()->GetGodWindow()->GetHandle(), invoke_later_message_id, reinterpret_cast<WPARAM>(p_action_copy), 0) == 0) - throw Win32Error(::GetLastError(), "InvokeLater failed to post message."); - } + if (PostMessageW(Application::GetInstance()->GetGodWindow()->GetHandle(), + invoke_later_message_id, + reinterpret_cast<WPARAM>(p_action_copy), 0) == 0) + throw Win32Error(::GetLastError(), "InvokeLater failed to post message."); } +} // namespace cru |