aboutsummaryrefslogtreecommitdiff
path: root/include/cru/platform/gui/sdl/Window.h
blob: aaa0c7253cf001c8f6b01eed14496c351aa1790b (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
#pragma once
#include "Base.h"

#include <cru/platform/GraphicsBase.h>
#include <cru/platform/gui/Window.h>

#include <SDL3/SDL_events.h>
#include <SDL3/SDL_video.h>

namespace cru::platform::gui::sdl {
class SdlUiApplication;
class SdlInputMethodContext;

class SdlWindow : public SdlResource, public virtual INativeWindow {
  CRU_DEFINE_CLASS_LOG_TAG("cru::platform::gui::xcb::SdlWindow")

  friend SdlUiApplication;

 public:
  explicit SdlWindow(SdlUiApplication* application);
  ~SdlWindow() override;

  bool IsCreated() override;
  void Close() override;

  INativeWindow* GetParent() override;
  void SetParent(INativeWindow* parent) override;

  WindowStyleFlag GetStyleFlag() override;
  void SetStyleFlag(WindowStyleFlag flag) override;

  std::string GetTitle() override;
  void SetTitle(std::string title) override;

  WindowVisibilityType GetVisibility() override;
  void SetVisibility(WindowVisibilityType visibility) override;

  Size GetClientSize() override;
  void SetClientSize(const Size& size) override;

  Rect GetClientRect() override;
  void SetClientRect(const Rect& rect) override;

  /**
   * Note: When the window is not created, this method will return an empty rect
   * regardless of whether the window has a border.
   */
  Rect GetWindowRect() override;

  /**
   * Note: When the window is not created, the border is always considered empty
   * regardless of the window style flag. So, you should always call this method
   * after the window is created.
   */
  void SetWindowRect(const Rect& rect) override;

  /**
   * Note: SDL3 doesn't have an API to solely set focus without raising the
   * window. So this method actually calls SDL_RaiseWindow to raise and set
   * focus to the window.
   */
  bool RequestFocus() override;

  Point GetMousePosition() override;

  bool CaptureMouse() override;
  bool ReleaseMouse() override;

  void SetCursor(std::shared_ptr<ICursor> cursor) override;

  void SetToForeground() override;

  void RequestRepaint() override;

  std::unique_ptr<graphics::IPainter> BeginPaint() override;

  CRU_DEFINE_CRU_PLATFORM_GUI_I_NATIVE_WINDOW_OVERRIDE_EVENTS()

  IInputMethodContext* GetInputMethodContext() override;

 public:
  SDL_Window* GetSdlWindow();
  SDL_WindowID GetSdlWindowId();
  SdlUiApplication* GetSdlUiApplication();
  float GetDisplayScale();
  Thickness GetBorderThickness();

 private:
  void DoCreateWindow();
  void DoUpdateClientRect();
  void DoUpdateParent();
  void DoUpdateStyleFlag();
  void DoUpdateTitle();
  void DoUpdateCursor();

  bool HandleEvent(const SDL_Event* event);

 private:
  SdlUiApplication* application_;
  SDL_Window* sdl_window_;
  SDL_WindowID sdl_window_id_;
  Rect client_rect_;
  SdlWindow* parent_;
  EventHandlerRevokerGuard parent_create_guard_;
  WindowStyleFlag style_;
  std::string title_;
  std::shared_ptr<ICursor> cursor_;

  std::unique_ptr<SdlInputMethodContext> input_context_;
};
}  // namespace cru::platform::gui::sdl