Ultralight C++ API 1.3.0
Loading...
Searching...
No Matches
Window.h
Go to the documentation of this file.
1/******************************************************************************
2 * This file is a part of Ultralight, an ultra-portable web-browser engine. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2023 Ultralight, Inc. *
7 *****************************************************************************/
8#pragma once
9#include "Defines.h"
10#include <Ultralight/RefPtr.h>
11#include <Ultralight/Listener.h>
12#include <Ultralight/Bitmap.h>
13#include <Ultralight/KeyEvent.h>
16
17namespace ultralight {
18
19class Monitor;
20class OverlayManager;
21class Surface;
22class Window;
23
24///
25/// Interface for all Window-related events. @see Window::set_listener
26///
28public:
29 virtual ~WindowListener() {}
30
31 ///
32 /// Called when the Window is closed.
33 ///
34 virtual void OnClose(ultralight::Window* window) { }
35
36 ///
37 /// Called when the Window is resized.
38 ///
39 /// @param width The new width (in pixels).
40 ///
41 /// @param height The new height (in pixels).
42 ///
43 virtual void OnResize(ultralight::Window* window, uint32_t width_px, uint32_t height_px) { }
44
45 ///
46 /// Called when a keyboard event is fired.
47 ///
48 /// @param evt Details for the event.
49 ///
50 /// @return Return false to consume the event and prevent it from propagating further.
51 ///
52 virtual bool OnKeyEvent(const ultralight::KeyEvent& evt) { return true; }
53
54 ///
55 /// Called when a mouse event is fired.
56 ///
57 /// @param evt Details for the event.
58 ///
59 /// @return Return false to consume the event and prevent it from propagating further.
60 ///
61 virtual bool OnMouseEvent(const ultralight::MouseEvent& evt) { return true; }
62
63 ///
64 /// Called when a scroll event is fired.
65 ///
66 /// @param evt Details for the event.
67 ///
68 /// @return Return false to consume the event and prevent it from propagating further.
69 ///
70 virtual bool OnScrollEvent(const ultralight::ScrollEvent& evt) { return true; }
71};
72
73///
74/// Window creation flags. @see Window::Create
75///
76enum WindowFlags : uint8_t {
82};
83
84///
85/// Window class, represents a platform window.
86///
87class AExport Window : public RefCounted {
88public:
89 ///
90 /// Create a new Window.
91 ///
92 /// @param monitor The monitor to create the Window on.
93 ///
94 /// @param width The width (in screen coordinates).
95 ///
96 /// @param height The height (in screen coordinates).
97 ///
98 /// @param fullscreen Whether or not the window is fullscreen.
99 ///
100 /// @param window_flags Various window flags.
101 ///
102 /// @note
103 /// \parblock
104 ///
105 /// Windows are immediately shown by default unless kWindowFlags_Hidden is set in the
106 /// window_flags parameter. (They can be shown later via Window::Show())
107 ///
108 /// \endparblock
109 ///
110 /// @note
111 /// \parblock
112 ///
113 /// Screen coordinates are device-scale-independent and have the following relationship
114 /// to pixel coordinates:
115 ///
116 /// \code
117 /// pixel_coordinate = round(screen_coordinate * scale)
118 /// \endcode
119 ///
120 /// \endparblock
121 ///
122 static RefPtr<Window> Create(Monitor* monitor, uint32_t width, uint32_t height,
123 bool fullscreen, unsigned int window_flags);
124
125 ///
126 /// Set a WindowListener to receive callbacks for window-related events.
127 ///
128 /// @note Ownership remains with the caller.
129 ///
130 virtual void set_listener(WindowListener* listener) = 0;
131
132 ///
133 /// Get the WindowListener, if any.
134 ///
135 virtual WindowListener* listener() = 0;
136
137 ///
138 /// Get the window width (in screen coordinates).
139 ///
140 virtual uint32_t screen_width() const = 0;
141
142 ///
143 /// Get the window width (in pixels).
144 ///
145 virtual uint32_t width() const = 0;
146
147 ///
148 /// Get the window height (in screen coordinates).
149 ///
150 virtual uint32_t screen_height() const = 0;
151
152 ///
153 /// Get the window height (in pixels).
154 ///
155 virtual uint32_t height() const = 0;
156
157 ///
158 /// Move the window to a new position (in screen coordinates) relative to the top-left of the
159 /// monitor area.
160 ///
161 virtual void MoveTo(int x, int y) = 0;
162
163 ///
164 /// Move the window to the center of the monitor.
165 ///
166 virtual void MoveToCenter() = 0;
167
168 ///
169 /// Get the x-position of the window (in screen coordinates) relative to the top-left of the
170 /// monitor area.
171 ///
172 virtual int x() const = 0;
173
174 ///
175 /// Get the y-position of the window (in screen coordinates) relative to the top-left of the
176 /// monitor area.
177 ///
178 virtual int y() const = 0;
179
180 ///
181 /// Whether or not the window is fullscreen.
182 ///
183 virtual bool is_fullscreen() const = 0;
184
185 ///
186 /// Whether or not the window is GPU accelerated.
187 ///
188 virtual bool is_accelerated() const = 0;
189
190 ///
191 /// The render buffer id of the the window's backing texture.
192 /// (This will be 0 if the window is not accelerated).
193 ///
194 virtual uint32_t render_buffer_id() const = 0;
195
196 ///
197 /// The DPI scale of the window.
198 ///
199 virtual double scale() const = 0;
200
201 ///
202 /// Set the window title.
203 ///
204 virtual void SetTitle(const char* title) = 0;
205
206 ///
207 /// Set the cursor.
208 ///
209 virtual void SetCursor(ultralight::Cursor cursor) = 0;
210
211 ///
212 /// Show the window (if it was previously hidden).
213 ///
214 virtual void Show() = 0;
215
216 ///
217 /// Hide the window.
218 ///
219 virtual void Hide() = 0;
220
221 ///
222 /// Whether or not the window is currently visible (not hidden).
223 ///
224 virtual bool is_visible() const = 0;
225
226 ///
227 /// Close the window.
228 ///
229 virtual void Close() = 0;
230
231 ///
232 /// Convert screen coordinates to pixels using the current DPI scale.
233 ///
234 virtual int ScreenToPixels(int val) const = 0;
235
236 ///
237 /// Convert pixels to screen coordinates using the current DPI scale.
238 ///
239 virtual int PixelsToScreen(int val) const = 0;
240
241 ///
242 /// Draw a surface directly to window, used only by CPU renderer
243 ///
244 virtual void DrawSurface(int x, int y, Surface* surface) {}
245
246 ///
247 /// Get the underlying native window handle.
248 ///
249 /// @note
250 /// This is:
251 /// - HWND on Windows
252 /// - NSWindow* on macOS
253 /// - GLFWwindow* on Linux
254 ///
255 virtual void* native_handle() const = 0;
256
257protected:
258 virtual ~Window();
259 virtual OverlayManager* overlay_manager() const = 0;
260
261 friend class OverlayImpl;
262};
263
264} // namespace ultralight
#define AExport
Definition Defines.h:42
Generic keyboard event representing a change in keyboard state.
Definition KeyEvent.h:23
Monitor class, represents a platform monitor.
Definition Monitor.h:16
Generic mouse event representing a change in mouse state.
Definition MouseEvent.h:18
Interface for all ref-counted objects that will be managed using the RefPtr<> smart pointer.
Definition RefPtr.h:47
A nullable smart pointer.
Definition RefPtr.h:79
Generic scroll event representing a change in scroll state.
Definition ScrollEvent.h:18
Offscreen pixel buffer surface.
Definition Surface.h:34
Window class, represents a platform window.
Definition Window.h:87
virtual uint32_t screen_width() const =0
Get the window width (in screen coordinates).
virtual bool is_visible() const =0
Whether or not the window is currently visible (not hidden).
virtual int y() const =0
Get the y-position of the window (in screen coordinates) relative to the top-left of the monitor area...
virtual void SetCursor(ultralight::Cursor cursor)=0
Set the cursor.
virtual bool is_accelerated() const =0
Whether or not the window is GPU accelerated.
static RefPtr< Window > Create(Monitor *monitor, uint32_t width, uint32_t height, bool fullscreen, unsigned int window_flags)
Create a new Window.
virtual void Show()=0
Show the window (if it was previously hidden).
virtual void MoveTo(int x, int y)=0
Move the window to a new position (in screen coordinates) relative to the top-left of the monitor are...
virtual void Close()=0
Close the window.
virtual double scale() const =0
The DPI scale of the window.
virtual void set_listener(WindowListener *listener)=0
Set a WindowListener to receive callbacks for window-related events.
virtual uint32_t height() const =0
Get the window height (in pixels).
virtual void SetTitle(const char *title)=0
Set the window title.
virtual int ScreenToPixels(int val) const =0
Convert screen coordinates to pixels using the current DPI scale.
virtual WindowListener * listener()=0
Get the WindowListener, if any.
virtual uint32_t render_buffer_id() const =0
The render buffer id of the the window's backing texture.
virtual bool is_fullscreen() const =0
Whether or not the window is fullscreen.
virtual OverlayManager * overlay_manager() const =0
virtual void DrawSurface(int x, int y, Surface *surface)
Draw a surface directly to window, used only by CPU renderer.
Definition Window.h:244
virtual void * native_handle() const =0
Get the underlying native window handle.
virtual uint32_t screen_height() const =0
Get the window height (in screen coordinates).
virtual int PixelsToScreen(int val) const =0
Convert pixels to screen coordinates using the current DPI scale.
virtual uint32_t width() const =0
Get the window width (in pixels).
virtual int x() const =0
Get the x-position of the window (in screen coordinates) relative to the top-left of the monitor area...
virtual void MoveToCenter()=0
Move the window to the center of the monitor.
virtual void Hide()=0
Hide the window.
Interface for all Window-related events.
Definition Window.h:27
virtual ~WindowListener()
Definition Window.h:29
virtual bool OnKeyEvent(const ultralight::KeyEvent &evt)
Called when a keyboard event is fired.
Definition Window.h:52
virtual bool OnScrollEvent(const ultralight::ScrollEvent &evt)
Called when a scroll event is fired.
Definition Window.h:70
virtual bool OnMouseEvent(const ultralight::MouseEvent &evt)
Called when a mouse event is fired.
Definition Window.h:61
virtual void OnResize(ultralight::Window *window, uint32_t width_px, uint32_t height_px)
Called when the Window is resized.
Definition Window.h:43
virtual void OnClose(ultralight::Window *window)
Called when the Window is closed.
Definition Window.h:34
Definition App.h:14
WindowFlags
Window creation flags.
Definition Window.h:76
@ kWindowFlags_Titled
Definition Window.h:78
@ kWindowFlags_Borderless
Definition Window.h:77
@ kWindowFlags_Maximizable
Definition Window.h:80
@ kWindowFlags_Hidden
Definition Window.h:81
@ kWindowFlags_Resizable
Definition Window.h:79
Cursor
Cursor types,.
Definition Listener.h:49