1/*
2 * LegacyClonk
3 *
4 * Copyright (c) RedWolf Design
5 * Copyright (c) 2005, Günther
6 * Copyright (c) 2017-2021, The LegacyClonk Team and contributors
7 *
8 * Distributed under the terms of the ISC license; see accompanying file
9 * "COPYING" for details.
10 *
11 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12 * See accompanying file "TRADEMARK" for details.
13 *
14 * To redistribute this file separately, substitute the full license texts
15 * for the above references.
16 */
17
18/* A wrapper class to OS dependent event and window interfaces */
19
20#pragma once
21
22#include "C4Rect.h"
23#include <Standard.h>
24
25#include "C4Rect.h"
26#include <StdBuf.h>
27
28#include <tuple>
29
30#ifdef _WIN32
31#include "C4Com.h"
32#include "C4WinRT.h"
33#include <shobjidl.h>
34#endif
35
36class CStdApp;
37#ifdef USE_X11
38// Forward declarations because xlib.h is evil
39typedef union _XEvent XEvent;
40typedef struct _XDisplay Display;
41#elif defined(USE_SDL_MAINLOOP)
42struct SDL_Window;
43union SDL_Event;
44#endif
45
46enum class DisplayMode
47{
48 Fullscreen,
49 Window
50};
51
52class CStdWindow
53{
54public:
55 CStdWindow() = default;
56 virtual ~CStdWindow();
57 bool Active{false};
58 virtual void Clear();
59 // Only when the wm requests a close
60 // For example, when the user clicks the little x in the corner or uses Alt-F4
61 virtual void Close() = 0;
62 // Keypress(es) translated to a char
63 virtual void CharIn(const char *c) {}
64 virtual bool Init(CStdApp *app, const char *title, const C4Rect &bounds = DefaultBounds, CStdWindow *parent = nullptr);
65 void StorePosition();
66 void RestorePosition();
67 bool GetSize(C4Rect &rect);
68
69#ifdef _WIN32
70 virtual
71#endif
72 void SetSize(unsigned int cx, unsigned int cy); // resiz
73 void SetTitle(const char *Title);
74 void FlashWindow();
75 void SetDisplayMode(DisplayMode mode);
76 void SetProgress(uint32_t progress); // progress 100 disables the progress bar
77
78protected:
79 virtual void Sec1Timer() {}
80
81#ifdef _WIN32
82
83public:
84 static constexpr C4Rect DefaultBounds{CW_USEDEFAULT, CW_USEDEFAULT, 0, 0};
85
86 HWND hWindow{nullptr};
87 void Maximize();
88 void SetPosition(int x, int y);
89 virtual HWND GetRenderWindow() const { return hWindow; }
90
91 static LRESULT DefaultWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
92
93protected:
94 virtual WNDCLASSEX GetWindowClass(HINSTANCE instance) const = 0;
95 virtual bool Win32DialogMessageHandling(MSG *msg) { return false; };
96 virtual bool GetPositionData(std::string &id, std::string &subKey, bool &storeSize) const { return {}; }
97 virtual std::pair<DWORD, DWORD> GetWindowStyle() const { return {WS_OVERLAPPEDWINDOW, 0}; }
98 virtual bool SupportsDarkMode() const { return true; }
99
100private:
101 C4Com com;
102 DWORD style = WS_OVERLAPPEDWINDOW;
103 DWORD styleEx = 0;
104 winrt::com_ptr<ITaskbarList3> taskBarList{nullptr};
105
106#elif defined(USE_X11)
107
108public:
109 static constexpr C4Rect DefaultBounds{0, 0, 640, 480};
110
111protected:
112 bool FindInfo();
113 virtual bool HideCursor() const { return false; }
114
115 unsigned long wnd{0};
116 unsigned long renderwnd{0};
117 Display *dpy{nullptr};
118 virtual void HandleMessage(XEvent &);
119 // The currently set window hints
120 void *Hints{nullptr};
121 bool HasFocus{false}; // To clear urgency hint
122 // The XVisualInfo the window was created with
123 void *Info{nullptr};
124
125#elif defined(USE_SDL_MAINLOOP)
126public:
127 static constexpr C4Rect DefaultBounds{0, 0, 100, 100};
128
129public:
130 float GetInputScale();
131
132private:
133 int width, height;
134 CStdApp *app;
135 DisplayMode displayMode;
136
137protected:
138 SDL_Window *sdlWindow;
139 virtual void HandleMessage(SDL_Event &) {}
140#elif defined(USE_CONSOLE)
141public:
142 static constexpr C4Rect DefaultBounds{0, 0, 100, 100};
143#endif
144
145 friend class CStdDDraw;
146 friend class CStdGL;
147 friend class CStdGLCtx;
148 friend class CStdApp;
149 friend class CStdGtkWindow;
150};
151