• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "flutter/shell/platform/windows/win32_dpi_helper.h"
15 
16 namespace flutter {
17 
18 // A class abstraction for a high DPI aware Win32 Window.  Intended to be
19 // inherited from by classes that wish to specialize with custom
20 // rendering and input handling
21 class Win32Window {
22  public:
23   Win32Window();
24   ~Win32Window();
25 
26   // Initializes and shows window with |title| and position and size using |x|,
27   // |y|, |width| and |height|
28   void Initialize(const char* title,
29                   const unsigned int x,
30                   const unsigned int y,
31                   const unsigned int width,
32                   const unsigned int height);
33 
34   // Release OS resources asociated with window.
35   virtual void Destroy();
36 
37  protected:
38   // Converts a c string to a wide unicode string.
39   std::wstring NarrowToWide(const char* source);
40 
41   // Registers a window class with default style attributes, cursor and
42   // icon.
43   WNDCLASS ResgisterWindowClass(std::wstring& title);
44 
45   // OS callback called by message pump.  Handles the WM_NCCREATE message which
46   // is passed when the non-client area is being created and enables automatic
47   // non-client DPI scaling so that the non-client area automatically
48   // responsponds to changes in DPI.  All other messages are handled by
49   // MessageHandler.
50   static LRESULT CALLBACK WndProc(HWND const window,
51                                   UINT const message,
52                                   WPARAM const wparam,
53                                   LPARAM const lparam) noexcept;
54 
55   // Processes and route salient window messages for mouse handling,
56   // size change and DPI.  Delegates handling of these to member overloads that
57   // inheriting classes can handle.
58   LRESULT
59   MessageHandler(HWND window,
60                  UINT const message,
61                  WPARAM const wparam,
62                  LPARAM const lparam) noexcept;
63 
64   // When WM_DPICHANGE resizes the window to the new suggested
65   // size and notifies inheriting class.
66   LRESULT
67   HandleDpiChange(HWND hWnd, WPARAM wParam, LPARAM lParam);
68 
69   // Called when the DPI changes either when a
70   // user drags the window between monitors of differing DPI or when the user
71   // manually changes the scale factor.
72   virtual void OnDpiScale(UINT dpi) = 0;
73 
74   // Called when a resize occurs.
75   virtual void OnResize(UINT width, UINT height) = 0;
76 
77   // Called when the pointer moves within the
78   // window bounds.
79   virtual void OnPointerMove(double x, double y) = 0;
80 
81   // Called when the left mouse button goes down
82   virtual void OnPointerDown(double x, double y) = 0;
83 
84   // Called when the left mouse button goes from
85   // down to up
86   virtual void OnPointerUp(double x, double y) = 0;
87 
88   // Called when character input occurs.
89   virtual void OnChar(unsigned int code_point) = 0;
90 
91   // Called when raw keyboard input occurs.
92   virtual void OnKey(int key, int scancode, int action, int mods) = 0;
93 
94   // Called when mouse scrollwheel input occurs.
95   virtual void OnScroll(double delta_x, double delta_y) = 0;
96 
97   // Called when the user closes the Windows
98   virtual void OnClose() = 0;
99 
100   UINT GetCurrentDPI();
101 
102   UINT GetCurrentWidth();
103 
104   UINT GetCurrentHeight();
105 
106   HWND GetWindowHandle();
107 
108  private:
109   // Stores new width and height and calls |OnResize| to notify inheritors
110   void HandleResize(UINT width, UINT height);
111 
112   // Retrieves a class instance pointer for |window|
113   static Win32Window* GetThisFromHandle(HWND const window) noexcept;
114   int current_dpi_ = 0;
115   int current_width_ = 0;
116   int current_height_ = 0;
117 
118   // Member variable to hold window handle.
119   HWND window_handle_ = nullptr;
120 
121   // Member variable to hold the window title.
122   std::wstring window_class_name_;
123 
124   // Member variable referencing an instance of dpi_helper used to abstract some
125   // aspects of win32 High DPI handling across different OS versions.
126   std::unique_ptr<Win32DpiHelper> dpi_helper_ =
127       std::make_unique<Win32DpiHelper>();
128 };
129 
130 }  // namespace flutter
131 
132 #endif  // FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WIN32_WINDOW_H_