aboutsummaryrefslogtreecommitdiff
path: root/src/platform/gui/sdl/Window.cpp
blob: f5fceb5f0564c4d4960f98990c8cc9825b1a6462 (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
#include "cru/platform/gui/sdl/Window.h"
#include "cru/base/Base.h"
#include "cru/platform/GraphicsBase.h"
#include "cru/platform/graphics/NullPainter.h"
#include "cru/platform/graphics/Painter.h"
#include "cru/platform/gui/Window.h"
#include "cru/platform/gui/sdl/Base.h"
#include "cru/platform/gui/sdl/UiApplication.h"

#include <SDL3/SDL_video.h>
#include <cairo-xcb.h>
#include <cairo.h>
#include <cassert>
#include <memory>
#include <optional>

namespace cru::platform::gui::sdl {

SdlWindow::SdlWindow(SdlUiApplication* application)
    : application_(application),
      client_rect_(100, 100, 400, 200),
      parent_(nullptr) {
  application->RegisterWindow(this);
}

SdlWindow::~SdlWindow() { application_->UnregisterWindow(this); }

bool SdlWindow::IsCreated() { return sdl_window_.has_value(); }

void SdlWindow::Close() {
  if (sdl_window_) {
    SDL_DestroyWindow(*sdl_window_);
  }
}

INativeWindow* SdlWindow::GetParent() { return parent_; }

void SdlWindow::SetParent(INativeWindow* parent) {
  parent_ = CheckPlatform<SdlWindow>(parent, GetPlatformId());
  NotImplemented();
}

WindowStyleFlag SdlWindow::GetStyleFlag() { return style_; }

void SdlWindow::SetStyleFlag(WindowStyleFlag flag) {
  style_ = flag;
  NotImplemented();
}

std::string SdlWindow::GetTitle() { NotImplemented(); }

void SdlWindow::SetTitle(std::string title) { NotImplemented(); }

WindowVisibilityType SdlWindow::GetVisibility() { NotImplemented(); }

void SdlWindow::SetVisibility(WindowVisibilityType visibility) {
  NotImplemented();
}

Size SdlWindow::GetClientSize() { return GetClientRect().GetSize(); }

void SdlWindow::SetClientSize(const Size& size) {
  auto rect = GetClientRect();
  SetClientRect(Rect(rect.GetLeftTop(), size));
}

Rect SdlWindow::GetClientRect() {
  if (!sdl_window_) return {};
  return client_rect_;
}

void SdlWindow::SetClientRect(const Rect& rect) {
  client_rect_ = rect;
  if (sdl_window_) {
    CheckSdlReturn(SDL_SetWindowPosition(*sdl_window_, rect.left, rect.top));
    CheckSdlReturn(SDL_SetWindowSize(*sdl_window_, rect.width, rect.height));
  }
}

Rect SdlWindow::GetWindowRect() {
  if (!sdl_window_) return {};
  return client_rect_.Expand(GetBorderThickness());
}

void SdlWindow::SetWindowRect(const Rect& rect) {
  SetClientRect(rect.Shrink(GetBorderThickness()));
}

bool SdlWindow::RequestFocus() {
  if (!sdl_window_) return false;
  NotImplemented();
}

Point SdlWindow::GetMousePosition() { NotImplemented(); }

bool SdlWindow::CaptureMouse() {
  if (!sdl_window_) return false;
  NotImplemented();
}

bool SdlWindow::ReleaseMouse() {
  if (!sdl_window_) return false;
  NotImplemented();
}

void SdlWindow::SetCursor(std::shared_ptr<ICursor> cursor) {
  if (!sdl_window_) return;
  NotImplemented();
}

void SdlWindow::SetToForeground() {
  SetVisibility(WindowVisibilityType::Show);
  NotImplemented();
}

void SdlWindow::RequestRepaint() {
  if (!sdl_window_) return;
  NotImplemented();
}

std::unique_ptr<graphics::IPainter> SdlWindow::BeginPaint() {
  if (!sdl_window_.has_value()) {
    return std::make_unique<graphics::NullPainter>();
  }

  NotImplemented();
}

IInputMethodContext* SdlWindow::GetInputMethodContext() { NotImplemented(); }

std::optional<SDL_Window*> SdlWindow::GetSdlWindow() { return sdl_window_; }

SdlUiApplication* SdlWindow::GetSdlUiApplication() { return application_; }

float SdlWindow::GetDisplayScale() {
  if (!sdl_window_) return 1.f;
  auto scale = SDL_GetWindowDisplayScale(*sdl_window_);
  if (scale == 0.f) {
    throw SdlException("Failed to get window display scale.");
  }
  return scale;
}

Thickness SdlWindow::GetBorderThickness() {
  if (!sdl_window_) return {};
  int top, left, bottom, right;
  CheckSdlReturn(
      SDL_GetWindowBordersSize(*sdl_window_, &top, &left, &bottom, &right));
  return {static_cast<float>(left), static_cast<float>(top),
          static_cast<float>(right), static_cast<float>(bottom)};
}

}  // namespace cru::platform::gui::sdl