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

#include <cairo.h>
#include <xcb/xcb.h>
#include <cstddef>
#include <optional>

namespace cru::platform::gui::xcb {
class XcbUiApplication;
class XcbCursor;

class XcbWindow : public XcbResource, public virtual INativeWindow {
  friend XcbUiApplication;

 public:
  explicit XcbWindow(XcbUiApplication* application);
  ~XcbWindow() override;

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

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

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

  String GetTitle() override;
  void SetTitle(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;

  Rect GetWindowRect() override;
  void SetWindowRect(const Rect& rect) override;

  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;

  IEvent<std::nullptr_t>* CreateEvent() override;
  IEvent<std::nullptr_t>* DestroyEvent() override;
  IEvent<std::nullptr_t>* PaintEvent() override;

  IEvent<WindowVisibilityType>* VisibilityChangeEvent() override;
  IEvent<Size>* ResizeEvent() override;
  IEvent<FocusChangeType>* FocusEvent() override;

  IEvent<MouseEnterLeaveType>* MouseEnterLeaveEvent() override;
  IEvent<Point>* MouseMoveEvent() override;
  IEvent<NativeMouseButtonEventArgs>* MouseDownEvent() override;
  IEvent<NativeMouseButtonEventArgs>* MouseUpEvent() override;
  IEvent<NativeMouseWheelEventArgs>* MouseWheelEvent() override;
  IEvent<NativeKeyEventArgs>* KeyDownEvent() override;
  IEvent<NativeKeyEventArgs>* KeyUpEvent() override;

  IInputMethodContext* GetInputMethodContext() override;

 public:
  std::optional<xcb_window_t> GetXcbWindow();

 private:
  xcb_window_t DoCreateWindow();
  void HandleEvent(xcb_generic_event_t* event);
  static std::optional<xcb_window_t> GetEventWindow(xcb_generic_event_t* event);

  void DoSetParent(xcb_window_t window);
  void DoSetStyleFlags(xcb_window_t window);
  void DoSetTitle(xcb_window_t window);
  void DoSetClientRect(xcb_window_t window, const Rect& rect);
  void DoSetCursor(xcb_window_t window, XcbCursor* cursor);

  void* XcbGetProperty(xcb_window_t window, xcb_atom_t property,
                       xcb_atom_t type, std::uint32_t offset,
                       std::uint32_t length,
                       std::uint32_t* out_length = nullptr);

  // Relative to screen lefttop.
  Point GetXcbWindowPosition(xcb_window_t window);

  std::optional<Thickness> Get_NET_FRAME_EXTENTS(xcb_window_t window);

 private:
  XcbUiApplication* application_;
  std::optional<xcb_window_t> xcb_window_;
  cairo_surface_t* cairo_surface_;
  Size current_size_;
  WindowStyleFlag style_;
  std::string title_;
  bool mapped_;
  std::shared_ptr<XcbCursor> cursor_;

  XcbWindow* parent_;

  Event<std::nullptr_t> create_event_;
  Event<std::nullptr_t> destroy_event_;
  Event<std::nullptr_t> paint_event_;

  Event<WindowVisibilityType> visibility_change_event_;
  Event<Size> resize_event_;
  Event<FocusChangeType> focus_event_;
  Event<MouseEnterLeaveType> mouse_enter_leave_event_;
  Event<Point> mouse_move_event_;
  Event<NativeMouseButtonEventArgs> mouse_down_event_;
  Event<NativeMouseButtonEventArgs> mouse_up_event_;
  Event<NativeMouseWheelEventArgs> mouse_wheel_event_;
  Event<NativeKeyEventArgs> key_down_event_;
  Event<NativeKeyEventArgs> key_up_event_;
};
}  // namespace cru::platform::gui::xcb