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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
// ui - Basic User Interface library to do experiments -*- C++ -*-
// Copyright (C) 2010, 2012 David Capello
//
// Distributed under the terms of the New BSD License,
// see LICENSE.md for more details.
#ifndef UI_WIN32_HEADER_FILE_INCLUDED
#define UI_WIN32_HEADER_FILE_INCLUDED
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400 // From Windows 2000
#endif
#include "mt/thread.h"
#include <exception>
#include <windows.h>
namespace ui {
namespace win32 {
//////////////////////////////////////////////////////////////////////
// win32_details namespace
class win32_details {
template<class WindowType>
static LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
WindowType* wnd = (WindowType*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
if (wnd != NULL) {
LRESULT result;
if (wnd->process_message(msg, wparam, lparam, result))
return result;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
public:
template<class WindowType>
static void register_window_class() {
HINSTANCE hinstance = ::GetModuleHandle(NULL);
LPCTSTR class_name = "ui_window_class";
WNDCLASSEX wcex;
if (!::GetClassInfoEx(hinstance, class_name, &wcex)) {
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = &wnd_proc<WindowType>;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hinstance;
wcex.hIcon = (HICON)NULL;
wcex.hCursor = (HCURSOR)::LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCTSTR)NULL;
wcex.lpszClassName = class_name;
wcex.hIconSm = NULL;
if (RegisterClassEx(&wcex) == 0)
//throw std::exception("Cannot register win32 window class");
throw std::exception();
}
}
template<class WindowType>
static HWND create_window(WindowType* wnd, int width, int height) {
HINSTANCE hinstance = ::GetModuleHandle(NULL);
HWND hwnd = CreateWindowEx(WS_EX_CONTROLPARENT,
"ui_window_class", "",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
// CW_USEDEFAULT, CW_USEDEFAULT,
width, height,
(HWND)NULL,
(HMENU)NULL,
hinstance,
NULL);
if (hwnd != NULL) {
// Set the pointer in the user-data field of the window (TODO: use an ATOM)
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)wnd);
}
return hwnd;
}
};
//////////////////////////////////////////////////////////////////////
// win32_bitmap class
class win32_bitmap {
HBITMAP m_hbitmap;
HDC m_hdc;
public:
win32_bitmap(int width, int height) {
assert(width > 0 && height > 0);
BITMAPINFOHEADER bhdr;
bhdr.biSize = sizeof(BITMAPINFOHEADER);
bhdr.biWidth = width;
bhdr.biHeight = -height;
bhdr.biPlanes = 1;
bhdr.biBitCount = 32; // 32 bpp
bhdr.biCompression = BI_RGB;
bhdr.biSizeImage = 0;
bhdr.biXPelsPerMeter = 0;
bhdr.biYPelsPerMeter = 0;
bhdr.biClrUsed = 0;
bhdr.biClrImportant = 0;
BITMAPINFO binf;
RGBQUAD dummy = { 0, 0, 0, 0 };
binf.bmiColors[0] = dummy;
binf.bmiHeader = bhdr;
char* bits = NULL;
HDC hdc = GetDC(GetDesktopWindow());
m_hbitmap = CreateDIBSection(hdc, &binf, DIB_RGB_COLORS,
reinterpret_cast<void**>(&bits),
NULL, 0);
if (m_hbitmap == NULL)
throw std::exception(); // TODO throw ui_exception
m_hdc = CreateCompatibleDC(hdc);
SelectObject(m_hdc, m_hbitmap);
// Clear the whole bitmap with a white background
{
HGDIOBJ oldPen = SelectObject(m_hdc, ::CreatePen(PS_NULL, 0, 0));
HGDIOBJ oldBrush = SelectObject(m_hdc, ::CreateSolidBrush(RGB(255, 255, 255)));
Rectangle(m_hdc, 0, 0, width, height);
DeleteObject(SelectObject(m_hdc, oldPen));
DeleteObject(SelectObject(m_hdc, oldBrush));
}
}
~win32_bitmap() {
::DeleteDC(m_hdc);
::DeleteObject(m_hbitmap);
}
HBITMAP hbitmap() {
return m_hbitmap;
}
HDC hdc() {
return m_hdc;
}
};
//////////////////////////////////////////////////////////////////////
// win32_window class
class win32_window {
HWND m_handle;
mt::thread* m_thread;
bool m_destroy;
bool m_keypressed;
bool m_closed;
win32_bitmap m_bitmap;
int m_textx, m_texty;
struct create_window_wrapper {
win32_window* m_wnd;
int m_width;
int m_height;
create_window_wrapper(win32_window* wnd, int width, int height)
: m_wnd(wnd)
, m_width(width)
, m_height(height) {
}
void operator()() {
m_wnd->m_handle = win32_details::create_window(m_wnd, m_width, m_height);
MSG msg;
::ShowWindow(m_wnd->m_handle, SW_SHOWNORMAL);
::UpdateWindow(m_wnd->m_handle);
// Message loop while:
// 1) the window is not destroyed
// 2) the window is visible
// 3) the Windows message queue is still alive
while (!m_wnd->m_destroy &&
::IsWindowVisible(m_wnd->m_handle) &&
::GetMessage(&msg, m_wnd->m_handle, 0, 0)) {
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
// Destroy the window
::DestroyWindow(m_wnd->m_handle);
m_wnd->m_handle = NULL;
}
};
friend struct create_window_wrapper;
friend class win32_details;
public:
win32_window(int width, int height)
: m_bitmap(width, height)
, m_textx(0)
, m_texty(0) {
win32_details::register_window_class<win32_window>();
m_destroy = false;
m_keypressed = false;
m_closed = false;
m_thread = new mt::thread(create_window_wrapper(this, width, height));
// TODO wait m_handle != NULL (use a notification, avoid 100% CPU wait)
while (m_handle == NULL)
;
}
~win32_window() {
if (m_thread) {
m_destroy = true;
m_thread->join();
delete m_thread;
}
}
void waitkey() {
// // do something
while (!m_keypressed && !m_closed) // TODO avoid 100% CPU wait
// wait_message();
;
m_keypressed = false;
}
size_t width() {
RECT rc;
::GetWindowRect(m_handle, &rc);
return rc.right - rc.left;
}
size_t height() {
RECT rc;
::GetWindowRect(m_handle, &rc);
return rc.bottom - rc.top;
}
void write(const std::string& text) {
HDC hdc = m_bitmap.hdc();
// Draw the text in the bitmap
TextOut(hdc, m_textx, m_texty, text.c_str(), static_cast<int>(text.size()));
// Calculate the size of the string
RECT rc = { 0, 0, 32000, 0 };
DrawText(hdc, text.c_str(), static_cast<int>(text.size()), &rc, DT_CALCRECT);
m_textx += (rc.right - rc.left); // X = X + Text Width
if (m_textx > width()) {
m_texty += (rc.bottom - rc.top);
m_textx = 0;
}
::InvalidateRect(m_handle, NULL, FALSE);
}
private:
// This function is used to process
bool process_message(UINT msg, WPARAM wparam, LPARAM lparam, LRESULT& result)
{
switch (msg) {
case WM_KEYDOWN:
m_keypressed = true;
break;
case WM_CLOSE:
m_closed = true;
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC window_hdc = ::BeginPaint(m_handle, &ps);
HDC bitmap_hdc = m_bitmap.hdc();
if (!::IsRectEmpty(&ps.rcPaint)) {
BitBlt(window_hdc,
ps.rcPaint.left, ps.rcPaint.top, // X, Y (dst)
ps.rcPaint.right - ps.rcPaint.left, // Width
ps.rcPaint.bottom - ps.rcPaint.top, // Height
bitmap_hdc,
ps.rcPaint.left, ps.rcPaint.top, // X, Y (src)
SRCCOPY);
}
::EndPaint(m_handle, &ps);
result = TRUE;
}
return true;
}
return false;
}
};
} // namespace win32
typedef win32::win32_window window_impl;
} // namespace ui
#define ui_main() \
WINAPI WinMain(HINSTANCE hInstance, \
HINSTANCE hPrevInstance, \
LPSTR lpCmdLine, \
int nCmdShow) \
#endif // UI_WIN32_HEADER_FILE_INCLUDED
|