summaryrefslogtreecommitdiff
path: root/Rx/v2/examples/win_text/windows_user.h
blob: 418f73fc31ae1327c7e56cae76fd36b54dffb3b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#pragma once

namespace windows_user {

    EXTERN_C IMAGE_DOS_HEADER __ImageBase;
    inline HINSTANCE GetCurrentInstance(){ return ((HINSTANCE)&__ImageBase); }

    template<typename Type>
    LRESULT CALLBACK WindowCallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) noexcept;

    template<typename Type>
    class window_class
    {
    public:
        static LPCWSTR Name() {
            return Type::class_name();
        }

        static ATOM Register() {
            WNDCLASSEX wcex = {};
            wcex.cbSize = sizeof(WNDCLASSEX);

            // defaults that can be overriden
            wcex.style = CS_HREDRAW | CS_VREDRAW;
            wcex.hInstance = GetCurrentInstance();
            wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
            wcex.style         = 0;
            wcex.hIcon         = NULL;
            wcex.hCursor       = LoadCursor(NULL, IDC_ARROW);
            wcex.lpszMenuName  = NULL;

            Type::change_class(wcex);

            // not overridable
            wcex.lpszClassName = Name();
            wcex.lpfnWndProc = WindowCallback<Type>;

            return RegisterClassEx(&wcex);
        }

    private:
        ~window_class();
        window_class();
        window_class(window_class&);
        window_class& operator=(window_class&);
    };

    namespace detail {
        template<typename Type>
        std::unique_ptr<Type> find(HWND hwnd) {
            return std::unique_ptr<Type>(reinterpret_cast<Type*>(GetWindowLongPtr(hwnd, GWLP_USERDATA)));
        }

        void erase(HWND hwnd) {
            SetWindowLongPtr(hwnd, GWLP_USERDATA, 0);
        }

        template<typename Type>
        std::unique_ptr<Type> insert(HWND hwnd, std::unique_ptr<Type> type) {
            if (!type) {
                return nullptr;
            }

            SetLastError(0);

            ON_UNWIND(unwind_userdata, [&](){erase(hwnd);});
            auto result = SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(type.get()));

            LONG winerror = !result ? GetLastError() : ERROR_SUCCESS;

            if (!!winerror || !!result) {
                return nullptr;
            }

            unwind_userdata.dismiss();
            return type;
        }
    }

    template<typename Type>
    LRESULT CALLBACK WindowCallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) noexcept {
        auto type = detail::find<Type>(hWnd);
        // don't delete type
        ON_UNWIND(unwind_type, [&](){type.release();});

        if (message == WM_NCCREATE) {
            if  (type) {
                // the slot where we would store our type instance is full. abort.
                return FALSE;
            }
            auto cs = reinterpret_cast<LPCREATESTRUCT>(lParam);
            auto param = reinterpret_cast<Type::param_type*>(cs->lpCreateParams);
            type = detail::insert(hWnd, std::unique_ptr<Type>(new (std::nothrow) Type(hWnd, cs, param)));
            if (!type) {
                return FALSE;
            }
        }

        LRESULT lResult = 0;
        bool handled = false;

        if (type) {
            std::tie(handled, lResult) = type->message(hWnd, message, wParam, lParam);
        }

        if (!handled) {
            lResult = DefWindowProc(hWnd, message, wParam, lParam);
        }

        if (message == WM_NCDESTROY) {
            detail::erase(hWnd);
            // let type destruct
            unwind_type.dismiss();
        }

        return lResult;
    }
}