aboutsummaryrefslogtreecommitdiff
path: root/src/ui/window.hpp
blob: d98e60e26573bbdca606328454297fd71f6a244e (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#pragma once

#include "system_headers.hpp"
#include <map>
#include <list>
#include <memory>

#include "control.hpp"
#include "events/ui_event.hpp"

namespace cru::graph
{
    class WindowRenderTarget;
}

namespace cru::ui
{
    class WindowClass : public Object
    {
    public:
        WindowClass(const String& name, WNDPROC window_proc, HINSTANCE h_instance);
        WindowClass(const WindowClass& other) = delete;
        WindowClass(WindowClass&& other) = delete;
        WindowClass& operator=(const WindowClass& other) = delete;
        WindowClass& operator=(WindowClass&& other) = delete;
        ~WindowClass() override = default;


        const wchar_t* GetName() const
        {
            return name_.c_str();
        }

        ATOM GetAtom() const
        {
            return atom_;
        }

    private:
        String name_;
        ATOM atom_;
    };

    class WindowManager : public Object
    {
    public:
        static WindowManager* GetInstance();
    private:
        WindowManager();
    public:
        WindowManager(const WindowManager& other) = delete;
        WindowManager(WindowManager&& other) = delete;
        WindowManager& operator=(const WindowManager& other) = delete;
        WindowManager& operator=(WindowManager&& other) = delete;
        ~WindowManager() override = default;


        //Get the general window class for creating ordinary window.
        WindowClass* GetGeneralWindowClass() const
        {
            return general_window_class_.get();
        }

        //Register a window newly created.
        //This function adds the hwnd to hwnd-window map.
        //It should be called immediately after a window was created.
        void RegisterWindow(HWND hwnd, Window* window);

        //Unregister a window that is going to be destroyed.
        //This function removes the hwnd from the hwnd-window map.
        //It should be called immediately before a window is going to be destroyed,
        void UnregisterWindow(HWND hwnd);

        //Return a pointer to the Window object related to the HWND or nullptr if the hwnd is not in the map.
        Window* FromHandle(HWND hwnd);

        std::vector<Window*> GetAllWindows() const;

    private:
        std::unique_ptr<WindowClass> general_window_class_;
        std::map<HWND, Window*> window_map_;
    };



    class Window final : public Control
    {
        friend class WindowManager;
    public:
        static constexpr auto control_type = L"Window";

    public:
        static Window* CreateOverlapped();
        static Window* CreatePopup(Window* parent, bool caption = false);

    private:
        struct tag_overlapped_constructor {};
        struct tag_popup_constructor {};

        explicit Window(tag_overlapped_constructor);
        Window(tag_popup_constructor, Window* parent, bool caption);

        void AfterCreateHwnd(WindowManager* window_manager);

    public:
        Window(const Window& other) = delete;
        Window(Window&& other) = delete;
        Window& operator=(const Window& other) = delete;
        Window& operator=(Window&& other) = delete;
        ~Window() override;

    public:
        StringView GetControlType() const override final;

        void SetDeleteThisOnDestroy(bool value);

        //*************** region: handle ***************

        //Get the handle of the window. Return null if window is invalid.
        HWND GetWindowHandle() const
        {
            return hwnd_;
        }

        //Return if the window is still valid, that is, hasn't been closed or destroyed.
        bool IsWindowValid() const
        {
            return hwnd_ != nullptr;
        }


        //*************** region: window operations ***************

        Window* GetParentWindow() const
        {
            return parent_window_;
        }

        //Close and destroy the window if the window is valid.
        void Close();

        //Send a repaint message to the window's message queue which may make the window repaint.
        void InvalidateDraw() override final;

        //Show the window.
        void Show();

        //Hide thw window.
        void Hide();

        //Get the client size.
        Size GetClientSize();

        //Set the client size and repaint.
        void SetClientSize(const Size& size);

        //Get the rect of the window containing frame.
        //The lefttop of the rect is relative to screen lefttop.
        Rect GetWindowRect();

        //Set the rect of the window containing frame.
        //The lefttop of the rect is relative to screen lefttop.
        void SetWindowRect(const Rect& rect);

        //Set the lefttop of the window relative to screen.
        void SetWindowPosition(const Point& position);

        Point PointToScreen(const Point& point);

        Point PointFromScreen(const Point& point);

        //Handle the raw window message.
        //Return true if the message is handled and get the result through "result" argument.
        //Return false if the message is not handled.
        bool HandleWindowMessage(HWND hwnd, int msg, WPARAM w_param, LPARAM l_param, LRESULT& result);

        //*************** region: mouse ***************

        Point GetMousePosition();

        Control* GetMouseHoverControl() const
        {
            return mouse_hover_control_;
        }

        //*************** region: position and size ***************

        //Always return (0, 0) for a window.
        Point GetPositionRelative() override final;

        //This method has no effect for a window.
        void SetPositionRelative(const Point& position) override final;

        //Get the size of client area for a window.
        Size GetSize() override final;

        //This method has no effect for a window. Use SetClientSize instead.
        void SetSize(const Size& size) override final;

        //Override. If point is in client area, it is in window.
        bool IsPointInside(const Point& point) override final;

        //*************** region: layout ***************

        void WindowInvalidateLayout();

        void Relayout();

        void SetSizeFitContent(const Size& max_size = Size(1000, 1000));

        //*************** region: functions ***************

        //Refresh control list.
        //It should be invoked every time a control is added or removed from the tree.
        void RefreshControlList();

        //Get the most top control at "point".
        Control* HitTest(const Point& point);


        //*************** region: focus ***************

        //Request focus for specified control.
        bool RequestFocusFor(Control* control);

        //Get the control that has focus.
        Control* GetFocusControl();


        //*************** region: mouse capture ***************

        Control* CaptureMouseFor(Control* control);
        Control* ReleaseCurrentMouseCapture();

        
        //*************** region: cursor ***************
        void UpdateCursor();

        //*************** region: debug ***************
#ifdef CRU_DEBUG_LAYOUT
        bool IsDebugLayout() const
        {
            return debug_layout_;
        }

        void SetDebugLayout(bool value);
#endif
 
    public:
        //*************** region: events ***************
        events::UiEvent activated_event;
        events::UiEvent deactivated_event;
 
        events::WindowNativeMessageEvent native_message_event;
 
    private:
        //*************** region: native operations ***************
 
        //Get the client rect in pixel.
        RECT GetClientRectPixel();
 
        bool IsMessageInQueue(UINT message);
 
        void SetCursorInternal(HCURSOR cursor);
 
 
        //*************** region: native messages ***************
 
        void OnDestroyInternal();
        void OnPaintInternal();
        void OnResizeInternal(int new_width, int new_height);
 
        void OnSetFocusInternal();
        void OnKillFocusInternal();
 
        void OnMouseMoveInternal(POINT point);
        void OnMouseLeaveInternal();
        void OnMouseDownInternal(MouseButton button, POINT point);
        void OnMouseUpInternal(MouseButton button, POINT point);
 
        void OnKeyDownInternal(int virtual_code);
        void OnKeyUpInternal(int virtual_code);
        void OnCharInternal(wchar_t c);
 
        void OnActivatedInternal();
        void OnDeactivatedInternal();
 
        //*************** region: event dispatcher helper ***************
 
        template<typename EventArgs>
        using EventMethod = void (Control::*)(EventArgs&);
 
        // Dispatch the event.
        // 
        // This will invoke the "event_method" of the control and its parent and parent's
        // parent ... (until "last_receiver" if it's not nullptr) with appropriate args.
        //
        // Args is of type "EventArgs". The first init argument is "sender", which is
        // automatically bound to each receiving control. The second init argument is
        // "original_sender", which is unchanged. And "args" will be perfectly forwarded
        // as the rest arguments.
        template<typename EventArgs, typename... Args>
        void DispatchEvent(Control* original_sender, EventMethod<EventArgs> event_method, Control* last_receiver, Args&&... args)
        {
            auto control = original_sender;
            while (control != nullptr && control != last_receiver)
            {
                EventArgs event_args(control, original_sender, std::forward<Args>(args)...);
                (control->*event_method)(event_args);
                control = control->GetParent();
            }
        }
 
        void DispatchMouseHoverControlChangeEvent(Control* old_control, Control * new_control, const Point& point);
 
    private:
        bool delete_this_on_destroy_ = true;

        HWND hwnd_ = nullptr;
        Window* parent_window_ = nullptr;
        std::shared_ptr<graph::WindowRenderTarget> render_target_{};
 
        std::list<Control*> control_list_{};
 
        Control* mouse_hover_control_ = nullptr;
 
        bool window_focus_ = false;
        Control* focus_control_ = this; // "focus_control_" can't be nullptr
 
        Control* mouse_capture_control_ = nullptr;

        bool is_layout_invalid_ = false;

#ifdef CRU_DEBUG_LAYOUT
        bool debug_layout_ = false;
#endif
    };
}