Flutter Engine
win32_window.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WIN32_WINDOW_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WIN32_WINDOW_H_
7 
8 #include <Windows.h>
9 #include <Windowsx.h>
10 
11 #include <memory>
12 #include <string>
13 
14 namespace flutter {
15 
16 // A class abstraction for a high DPI aware Win32 Window. Intended to be
17 // inherited from by classes that wish to specialize with custom
18 // rendering and input handling.
19 class Win32Window {
20  public:
21  Win32Window();
22  virtual ~Win32Window();
23 
24  // Initializes as a child window with size using |width| and |height| and
25  // |title| to identify the windowclass. Does not show window, window must be
26  // parented into window hierarchy by caller.
27  void InitializeChild(const char* title,
28  unsigned int width,
29  unsigned int height);
30 
31  HWND GetWindowHandle();
32 
33  protected:
34  // Converts a c string to a wide unicode string.
35  std::wstring NarrowToWide(const char* source);
36 
37  // Registers a window class with default style attributes, cursor and
38  // icon.
39  WNDCLASS RegisterWindowClass(std::wstring& title);
40 
41  // OS callback called by message pump. Handles the WM_NCCREATE message which
42  // is passed when the non-client area is being created and enables automatic
43  // non-client DPI scaling so that the non-client area automatically
44  // responsponds to changes in DPI. All other messages are handled by
45  // MessageHandler.
46  static LRESULT CALLBACK WndProc(HWND const window,
47  UINT const message,
48  WPARAM const wparam,
49  LPARAM const lparam) noexcept;
50 
51  // Processes and route salient window messages for mouse handling,
52  // size change and DPI. Delegates handling of these to member overloads that
53  // inheriting classes can handle.
54  LRESULT
55  HandleMessage(UINT const message,
56  WPARAM const wparam,
57  LPARAM const lparam) noexcept;
58 
59  // When WM_DPICHANGE process it using |hWnd|, |wParam|. If
60  // |top_level| is set, extract the suggested new size from |lParam| and resize
61  // the window to the new suggested size. If |top_level| is not set, the
62  // |lParam| will not contain a suggested size hence ignore it.
63  LRESULT
64  HandleDpiChange(HWND hWnd, WPARAM wParam, LPARAM lParam, bool top_level);
65 
66  // Called when the DPI changes either when a
67  // user drags the window between monitors of differing DPI or when the user
68  // manually changes the scale factor.
69  virtual void OnDpiScale(UINT dpi) = 0;
70 
71  // Called when a resize occurs.
72  virtual void OnResize(UINT width, UINT height) = 0;
73 
74  // Called when the pointer moves within the
75  // window bounds.
76  virtual void OnPointerMove(double x, double y) = 0;
77 
78  // Called when the a mouse button, determined by |button|, goes down.
79  virtual void OnPointerDown(double x, double y, UINT button) = 0;
80 
81  // Called when the a mouse button, determined by |button|, goes from
82  // down to up
83  virtual void OnPointerUp(double x, double y, UINT button) = 0;
84 
85  // Called when the mouse leaves the window.
86  virtual void OnPointerLeave() = 0;
87 
88  // Called when the cursor should be set for the client area.
89  virtual void OnSetCursor() = 0;
90 
91  // Called when text input occurs.
92  virtual void OnText(const std::u16string& text) = 0;
93 
94  // Called when raw keyboard input occurs.
95  virtual void OnKey(int key, int scancode, int action, char32_t character) = 0;
96 
97  // Called when mouse scrollwheel input occurs.
98  virtual void OnScroll(double delta_x, double delta_y) = 0;
99 
100  UINT GetCurrentDPI();
101 
102  UINT GetCurrentWidth();
103 
104  UINT GetCurrentHeight();
105 
106  private:
107  // Release OS resources asociated with window.
108  void Destroy();
109 
110  // Activates tracking for a "mouse leave" event.
111  void TrackMouseLeaveEvent(HWND hwnd);
112 
113  // Stores new width and height and calls |OnResize| to notify inheritors
114  void HandleResize(UINT width, UINT height);
115 
116  // Retrieves a class instance pointer for |window|
117  static Win32Window* GetThisFromHandle(HWND const window) noexcept;
118  int current_dpi_ = 0;
119  int current_width_ = 0;
120  int current_height_ = 0;
121 
122  // WM_DPICHANGED_BEFOREPARENT defined in more recent Windows
123  // SDK
124  const static long kWmDpiChangedBeforeParent = 0x02E2;
125 
126  // Member variable to hold window handle.
127  HWND window_handle_ = nullptr;
128 
129  // Member variable to hold the window title.
130  std::wstring window_class_name_;
131 
132  // Set to true to be notified when the mouse leaves the window.
133  bool tracking_mouse_leave_ = false;
134 
135  // Keeps track of the last key code produced by a WM_KEYDOWN or WM_SYSKEYDOWN
136  // message.
137  int keycode_for_char_message_ = 0;
138 };
139 
140 } // namespace flutter
141 
142 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WIN32_WINDOW_H_
WNDCLASS RegisterWindowClass(std::wstring &title)
Definition: win32_window.cc:65
virtual void OnDpiScale(UINT dpi)=0
virtual void OnSetCursor()=0
virtual void OnText(const std::u16string &text)=0
void InitializeChild(const char *title, unsigned int width, unsigned int height)
Definition: win32_window.cc:31
virtual void OnKey(int key, int scancode, int action, char32_t character)=0
virtual void OnPointerUp(double x, double y, UINT button)=0
static LRESULT CALLBACK WndProc(HWND const window, UINT const message, WPARAM const wparam, LPARAM const lparam) noexcept
Definition: win32_window.cc:83
virtual void OnScroll(double delta_x, double delta_y)=0
SemanticsAction action
int32_t height
int32_t width
virtual void OnPointerLeave()=0
std::wstring NarrowToWide(const char *source)
Definition: win32_window.cc:57
LRESULT HandleDpiChange(HWND hWnd, WPARAM wParam, LPARAM lParam, bool top_level)
virtual void OnPointerDown(double x, double y, UINT button)=0
LRESULT HandleMessage(UINT const message, WPARAM const wparam, LPARAM const lparam) noexcept
virtual void OnResize(UINT width, UINT height)=0
virtual void OnPointerMove(double x, double y)=0