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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
* nativewindow_win32.cc
*
* Fri Dec 28 18:45:52 CET 2012
* Copyright 2012 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of DrumGizmo.
*
* DrumGizmo is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* DrumGizmo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with DrumGizmo; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "nativewindow_win32.h"
#include "window.h"
#include <cstring>
namespace GUI {
LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg,
WPARAM wp, LPARAM lp)
{
NativeWindowWin32* native =
(NativeWindowWin32*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
// NOTE: 'native' is nullptr intil the WM_CREATE message has been handled.
if(!native)
{
return DefWindowProc(hwnd, msg, wp, lp);
}
Window& window = native->window;
switch(msg) {
case WM_SIZE:
{
static bool first = true;
if(!first)
{
auto resizeEvent = std::make_shared<ResizeEvent>();
resizeEvent->width = LOWORD(lp);
resizeEvent->height = HIWORD(lp);
native->event_queue.push(resizeEvent);
first = false;
}
}
break;
case WM_MOVE:
{
// auto moveEvent = std::make_shared<MoveEvent>();
// moveEvent->x = (short)LOWORD(lp);
// moveEvent->y = (short)HIWORD(lp);
// native->event_queue.push(moveEvent);
}
break;
case WM_CLOSE:
{
auto closeEvent = std::make_shared<CloseEvent>();
native->event_queue.push(closeEvent);
}
break;
// HWND child, old;
// old = 0;
// numDialogs--;
// while(old != (child = GetNextDlgGroupItem(hwnd, hwnd, false))) {
// old = child;
// EndDialog(child, 0);
// }
// if(numDialogs) EndDialog(hwnd, 0);
// else PostQuitMessage(0);
// return 0;
case WM_MOUSEMOVE:
{
auto mouseMoveEvent = std::make_shared<MouseMoveEvent>();
mouseMoveEvent->x = (short)LOWORD(lp);
mouseMoveEvent->y = (short)HIWORD(lp);
native->event_queue.push(mouseMoveEvent);
}
break;
case WM_MOUSEWHEEL:
{
auto scrollEvent = std::make_shared<ScrollEvent>();
// NOTE: lp is coordinates in screen space, not client space.
POINT p;
p.x = (short)LOWORD(lp);
p.y = (short)HIWORD(lp);
ScreenToClient(hwnd, &p);
scrollEvent->x = p.x;
scrollEvent->y = p.y;
scrollEvent->delta = -1 * (short)HIWORD(wp) / 60;
native->event_queue.push(scrollEvent);
}
break;
case WM_LBUTTONUP:
case WM_LBUTTONDBLCLK:
case WM_LBUTTONDOWN:
case WM_RBUTTONUP:
case WM_RBUTTONDBLCLK:
case WM_RBUTTONDOWN:
case WM_MBUTTONUP:
case WM_MBUTTONDBLCLK:
case WM_MBUTTONDOWN:
{
auto buttonEvent = std::make_shared<ButtonEvent>();
buttonEvent->x = (short)LOWORD(lp);
buttonEvent->y = (short)HIWORD(lp);
if(msg == WM_LBUTTONUP ||
msg == WM_LBUTTONDBLCLK ||
msg == WM_LBUTTONDOWN)
{
buttonEvent->button = MouseButton::left;
}
else if(msg == WM_RBUTTONUP ||
msg == WM_RBUTTONDBLCLK ||
msg == WM_RBUTTONDOWN)
{
buttonEvent->button = MouseButton::middle;
}
else if(msg == WM_MBUTTONUP ||
msg == WM_MBUTTONDBLCLK ||
msg == WM_MBUTTONDOWN)
{
buttonEvent->button = MouseButton::right;
}
else
{
break; // unknown button
}
// Double-clicking the a mouse button actually generates a sequence
// of four messages: WM_xBUTTONDOWN, WM_xBUTTONUP, WM_xBUTTONDBLCLK, and
// WM_xBUTTONUP. In other words the second WM_xBUTTONDOWN is replaced by a
// WM_xBUTTONDBLCLK. We simply 'return it' as a WM_xBUTTONDOWN but set the
// doubleClick boolean hint accordingly.
if(msg == WM_LBUTTONUP ||
msg == WM_RBUTTONUP ||
msg == WM_MBUTTONUP)
{
buttonEvent->direction = Direction::up;
}
else if(msg == WM_LBUTTONDOWN ||
msg == WM_RBUTTONDOWN ||
msg == WM_MBUTTONDOWN ||
msg == WM_LBUTTONDBLCLK ||
msg == WM_RBUTTONDBLCLK ||
msg == WM_MBUTTONDBLCLK)
{
buttonEvent->direction = Direction::down;
}
buttonEvent->doubleClick = (msg == WM_LBUTTONDBLCLK ||
msg == WM_RBUTTONDBLCLK ||
msg == WM_MBUTTONDBLCLK);
native->event_queue.push(buttonEvent);
}
break;
case WM_KEYDOWN:
case WM_KEYUP:
{
auto keyEvent = std::make_shared<KeyEvent>();
switch(wp) {
case VK_LEFT: keyEvent->keycode = Key::left; break;
case VK_RIGHT: keyEvent->keycode = Key::right; break;
case VK_UP: keyEvent->keycode = Key::up; break;
case VK_DOWN: keyEvent->keycode = Key::down; break;
case VK_BACK: keyEvent->keycode = Key::backspace; break;
case VK_DELETE: keyEvent->keycode = Key::deleteKey; break;
case VK_HOME: keyEvent->keycode = Key::home; break;
case VK_END: keyEvent->keycode = Key::end; break;
case VK_PRIOR: keyEvent->keycode = Key::pageUp; break;
case VK_NEXT: keyEvent->keycode = Key::pageDown; break;
case VK_RETURN: keyEvent->keycode = Key::enter; break;
default: keyEvent->keycode = Key::unknown; break;
}
keyEvent->text = "";
keyEvent->direction =
(msg == WM_KEYDOWN) ? Direction::down : Direction::up;
native->event_queue.push(keyEvent);
}
break;
case WM_CHAR:
{
if(wp >= ' ') // Filter control chars.
{
auto keyEvent = std::make_shared<KeyEvent>();
keyEvent->keycode = Key::character;
keyEvent->text += (char)wp;
keyEvent->direction = Direction::up;
native->event_queue.push(keyEvent);
}
}
break;
case WM_PAINT:
{
auto repaintEvent = std::make_shared<RepaintEvent>();
repaintEvent->x = 0;
repaintEvent->y = 0;
repaintEvent->width = 100;
repaintEvent->height = 100;
native->event_queue.push(repaintEvent);
// Move to window.h (in class)
HDC pDC;
HBITMAP old;
HBITMAP ourbitmap;
int* framebuf;
PixelBuffer& px = window.wpixbuf;
{ // Create bitmap
HDC hDC;
BITMAPINFO bitmapinfo;
hDC = CreateCompatibleDC(nullptr);
bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapinfo.bmiHeader.biWidth = px.width;
bitmapinfo.bmiHeader.biHeight = -px.height; // top-down
bitmapinfo.bmiHeader.biPlanes = 1;
bitmapinfo.bmiHeader.biBitCount = 32;
bitmapinfo.bmiHeader.biCompression = BI_RGB;
bitmapinfo.bmiHeader.biSizeImage = 0;
bitmapinfo.bmiHeader.biClrUsed = 256;
bitmapinfo.bmiHeader.biClrImportant = 256;
ourbitmap = CreateDIBSection(hDC, &bitmapinfo,
DIB_RGB_COLORS, (void**)&framebuf, 0, 0);
pDC=CreateCompatibleDC(nullptr);
old = (HBITMAP__*)SelectObject(pDC, ourbitmap);
DeleteDC(hDC);
}
{ // Copy PixelBuffer to framebuffer
int i, j, k;
for(k = 0, i = 0; i < (int)px.height; ++i)
{
for(j = 0; j < (int)px.width; ++j, ++k)
{
*(framebuf + k) = RGB(px.buf[(j + i * px.width) * 3 + 2],
px.buf[(j + i * px.width) * 3 + 1],
px.buf[(j + i * px.width) * 3 + 0]);
}
}
}
PAINTSTRUCT ps;
HDC hdc = BeginPaint(native->m_hwnd, &ps);
BitBlt(hdc, 0, 0, px.width, px.height, pDC, 0, 0, SRCCOPY);
EndPaint(native->m_hwnd, &ps);
{ // Destroy bitmap (move to window.cc)
SelectObject(pDC,old);
DeleteDC(pDC);
DeleteObject(ourbitmap);
}
}
return DefWindowProc(hwnd, msg, wp, lp);
}
return DefWindowProc(hwnd, msg, wp, lp);
}
NativeWindowWin32::NativeWindowWin32(void* native_window, Window& window)
: window(window)
{
WNDCLASSEX wcex{};
//Time to register a window class.
//Generic flags and everything. cbWndExtra is the size of a pointer to an
// object - we need this in the wndproc handler.
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_DBLCLKS;//class_style;
wcex.lpfnWndProc = (WNDPROC)dialogProc;
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
// Set data:
wcex.cbWndExtra = sizeof(NativeWindowWin32*); // Size of data.
wcex.hInstance = GetModuleHandle(nullptr);
// if(ex_style && WS_EX_TRANSPARENT == WS_EX_TRANSPARENT) {
// wcex.hbrBackground = nullptr;
// } else {
wcex.hbrBackground = nullptr;//(HBRUSH) COLOR_BACKGROUND + 1;
// }
const char* name = "DrumGizmoClass";
char* c_name = (char*)malloc(strlen(name) + 1);
strcpy(c_name, name);
wcex.lpszClassName = m_className = c_name;
RegisterClassEx(&wcex);
parent_window = (HWND)native_window;
m_hwnd = CreateWindowEx(0/*ex_style*/, m_className,
"DGBasisWidget",
(native_window?WS_CHILD:WS_OVERLAPPEDWINDOW) | WS_VISIBLE,
window.x(), window.y(),
window.width(), window.height(),
parent_window, nullptr,
GetModuleHandle(nullptr), nullptr);
SetWindowLongPtr(m_hwnd, GWLP_USERDATA, (LONG_PTR)this);
}
NativeWindowWin32::~NativeWindowWin32()
{
UnregisterClass(m_className, GetModuleHandle(nullptr));
free(m_className);
}
void NativeWindowWin32::setFixedSize(int width, int height)
{
resize(width, height);
LONG style = GetWindowLong(m_hwnd, GWL_STYLE);
style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
SetWindowLong(m_hwnd, GWL_STYLE, style);
}
void NativeWindowWin32::resize(int width, int height)
{
SetWindowPos(m_hwnd, nullptr, -1, -1, (int)width, (int)height, SWP_NOMOVE);
RECT r;
GetClientRect(m_hwnd, &r);
int w = width - r.right;
int h = height - r.bottom;
SetWindowPos(m_hwnd, nullptr, -1, -1, width + w, height + h, SWP_NOMOVE);
}
void NativeWindowWin32::move(int x, int y)
{
SetWindowPos(m_hwnd, nullptr, (int)x, (int)y, -1, -1, SWP_NOSIZE);
}
void NativeWindowWin32::show()
{
ShowWindow(m_hwnd, SW_SHOW);
}
void NativeWindowWin32::handleBuffer()
{
}
void NativeWindowWin32::hide()
{
ShowWindow(m_hwnd, SW_HIDE);
}
void NativeWindowWin32::redraw()
{
if(parent_window == nullptr)
{
RedrawWindow(m_hwnd, nullptr, nullptr, RDW_ERASE|RDW_INVALIDATE);
UpdateWindow(m_hwnd);
}
else
{
InvalidateRect(m_hwnd, 0, TRUE);
}
}
void NativeWindowWin32::setCaption(const std::string &caption)
{
SetWindowText(m_hwnd, caption.c_str());
}
void NativeWindowWin32::grabMouse(bool grab)
{
if(grab)
{
SetCapture(m_hwnd);
}
else
{
ReleaseCapture();
}
}
bool NativeWindowWin32::hasEvent()
{
if(!event_queue.empty())
{
return true;
}
// Parented windows have their event loop somewhere else.
if(parent_window == nullptr)
{
MSG msg;
return PeekMessage(&msg, m_hwnd, 0, 0, PM_NOREMOVE) != 0;
}
return false;
}
std::shared_ptr<Event> NativeWindowWin32::getNextEvent()
{
if(!event_queue.empty())
{
auto event = event_queue.front();
event_queue.pop();
return event;
}
// Parented windows have their event loop somewhere else.
if(parent_window == nullptr)
{
MSG msg;
if(GetMessage(&msg, m_hwnd, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if(event_queue.empty())
{
return nullptr;
}
auto event = event_queue.front();
event_queue.pop();
return event;
}
std::shared_ptr<Event> NativeWindowWin32::peekNextEvent()
{
if(!event_queue.empty())
{
auto event = event_queue.front();
return event;
}
// Parented windows have their event loop somewhere else.
if(parent_window == nullptr)
{
MSG msg;
if(PeekMessage(&msg, m_hwnd, 0, 0, PM_NOREMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if(event_queue.empty())
{
return nullptr;
}
auto event = event_queue.front();
return event;
}
} // GUI::
|