/
window.hpp
871 lines (763 loc) · 15.4 KB
/
window.hpp
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
#pragma once
template<class... Args>
class event
{
public:
#if _MSC_VER >= 1800
using delegate = std::function < void(Args...) >;
#else
typedef std::function < void(Args...) >;
#endif
event &operator=(const delegate& handler)
{
_handlers.clear();
_handlers.push_back(handler);
return *this;
}
event &operator+=(const delegate& handler)
{
_handlers.push_back(handler);
return *this;
}
bool operator== (nullptr_t)
{
return _handlers.size() == 0;
}
bool operator!= (nullptr_t)
{
return _handlers.size() != 0;
}
operator bool()
{
_handlers.size() > 0;
}
bool operator()(Args... args)
{
bool r = (_handlers.size() != 0);
for (auto &handler : _handlers) if (handler) handler(args...);
return r;
}
private:
std::vector<delegate> _handlers;
};
class event_args
{
public:
event_args()
: _wparam(0)
, _lparam(0)
{
}
event_args(WPARAM wparam, LPARAM lparam)
: _wparam(wparam)
, _lparam(lparam)
{
}
WPARAM wparam() const
{
return _wparam;
}
LPARAM lparam() const
{
return _lparam;
}
private:
WPARAM _wparam;
LPARAM _lparam;
};
// for win32 gui app
class console
{
protected:
console()
{
AllocConsole();
SetConsoleCtrlHandler(ctrl_handler, TRUE);
}
public:
static console *instance()
{
if (!_inst)
{
_inst = std::unique_ptr<console>(new console());
}
return _inst.get();
}
static void destroy()
{
_inst.reset();
}
~console()
{
SetConsoleCtrlHandler(ctrl_handler, FALSE);
FreeConsole();
}
void redirect()
{
auto redir = [](DWORD from_handle, FILE *to_handle, char *mode){
auto std_handle = GetStdHandle(from_handle);
auto con_handle = _open_osfhandle((intptr_t)std_handle, _O_TEXT);
auto fp = _fdopen(con_handle, mode);
*to_handle = *fp;
setvbuf(to_handle, nullptr, _IONBF, 0);
};
redir(STD_OUTPUT_HANDLE, stdout, "w"); // redirect stdout
redir(STD_INPUT_HANDLE, stdin, "r"); // redirect stdin
redir(STD_ERROR_HANDLE, stderr, "w"); // redirect stderr
// make std::cout, std::cin, std::cerr, std::wcin, std::wcout, std::wcerr,
// std::clog and std::wclog point to console as well
std::ios::sync_with_stdio();
}
#if _MSC_VER >= 1800
using ctrl_c_event = event <event_args &>;
using ctrl_break_event = event < event_args & >;
using close_event = event < event_args & >;
using logoff_event = event < event_args & >;
using shutdown_event = event < event_args & >;
#else
typedef event<event_args &> ctrl_c_event;
typedef event<event_args &> ctrl_break_event;
typedef event<event_args &> close_event;
typedef event<event_args &> logoff_event;
typedef event<event_args &> shutdown_event;
#endif
ctrl_c_event &ctrl_c_pressed()
{
return _ctrl_c;
}
ctrl_break_event &ctrl_break_pressed()
{
return _ctrl_break;
}
close_event &will_close()
{
return _close;
}
logoff_event &will_logoff()
{
return _logoff;
}
shutdown_event &will_shutdown()
{
return _shutdown;
}
private:
static BOOL WINAPI ctrl_handler(DWORD ctrl_type)
{
event_args args;
bool r = false;
switch (ctrl_type)
{
case CTRL_C_EVENT:
r = _inst->_ctrl_c(args);
break;
case CTRL_BREAK_EVENT:
r = _inst->_ctrl_break(args);
break;
case CTRL_CLOSE_EVENT:
r = _inst->_close(args);
break;
case CTRL_LOGOFF_EVENT:
r = _inst->_logoff(args);
break;
case CTRL_SHUTDOWN_EVENT:
r = _inst->_shutdown(args);
break;
}
return r ? TRUE : FALSE;
}
ctrl_c_event _ctrl_c;
ctrl_break_event _ctrl_break;
close_event _close;
logoff_event _logoff;
shutdown_event _shutdown;
static std::unique_ptr<console> _inst;
};
#include "win32.hpp"
class window;
class size_event_args
: public event_args
{
public:
size_event_args(WPARAM wparam, LPARAM lparam)
: event_args(wparam, lparam)
, _type(wparam)
, _width(LOWORD(lparam))
, _height(HIWORD(lparam))
{
}
int type() const
{
return _type;
}
int width() const
{
return _width;
}
int height() const
{
return _height;
}
private:
int _type;
int _width;
int _height;
};
class sizing_event_args
: public event_args
{
public:
sizing_event_args(WPARAM wparam, LPARAM lparam)
: event_args(wparam, lparam)
, _edge(wparam)
, _rc(*reinterpret_cast<RECT*>(lparam))
{
}
rect &rc()
{
return _rc;
}
int edge() const
{
return _edge;
}
private:
rect _rc;
int _edge;
};
class cancel_event_args : public event_args
{
public:
cancel_event_args(WPARAM wparam, LPARAM lparam)
: event_args(wparam, lparam)
, _canceled(false)
{
}
void cancel()
{
_canceled = true;
}
bool canceled()
{
return _canceled;
}
void set_canceled(bool val)
{
_canceled = val;
}
private:
bool _canceled;
};
enum MouseButtons
{
NoMouseButton,
LeftButton = 1 << 0,
RightButton = 1 << 1,
MiddleButton = 1 << 2,
};
enum KeyModifiers
{
NoKeyModifier,
ShiftKey = 1 << 0,
CtrlKey = 1 << 1,
AltKey = 1 << 2,
};
class mouse_event_args : public event_args
{
public:
mouse_event_args(WPARAM wparam, LPARAM lparam)
: event_args(wparam, lparam)
, _x(GET_X_LPARAM(lparam))
, _y(GET_Y_LPARAM(lparam))
, _z(GET_WHEEL_DELTA_WPARAM(wparam))
{
int b = 0;
int m = 0;
if (wparam & MK_LBUTTON)
b |= LeftButton;
if (wparam & MK_RBUTTON)
b |= RightButton;
if (wparam & MK_MBUTTON)
b |= MiddleButton;
if (wparam & MK_CONTROL)
m |= CtrlKey;
if (wparam & MK_SHIFT)
m |= ShiftKey;
if (GetKeyState(VK_MENU) < 0)
m |= AltKey;
_btns = static_cast<MouseButtons>(b);
_km = static_cast<KeyModifiers>(m);
}
int x() const
{
return _x;
}
int y() const
{
return _y;
}
int z() const
{
return _z;
}
static int wheel_delta()
{
return WHEEL_DELTA;
}
MouseButtons buttons() const
{
return _btns;
}
KeyModifiers modifiers() const
{
return _km;
}
private:
int _x;
int _y;
int _z;
MouseButtons _btns;
KeyModifiers _km;
};
enum class VirtualKey
{
Unknown
, Back = VK_BACK
, Tab = VK_TAB
, Clear = VK_CLEAR
, Return = VK_RETURN
, Shift = VK_SHIFT
, Control = VK_CONTROL
, Menu = VK_MENU
, Pause = VK_PAUSE
, CapsLock = VK_CAPITAL
, Kana = VK_KANA
, Hangeul = VK_HANGEUL
, Hangul = VK_HANGUL
, Junja = VK_JUNJA
, Final = VK_FINAL
, Hanja = VK_HANJA
, Kanji = VK_KANJI
, Escape = VK_ESCAPE
, Convert = VK_CONVERT
, NonConvert = VK_NONCONVERT
, Accept = VK_ACCEPT
, ModeChange = VK_MODECHANGE
, Space = VK_SPACE
, PageUp = VK_PRIOR
, PageDown = VK_NEXT
, End = VK_END
, Home = VK_HOME
, Left = VK_LEFT
, Up = VK_UP
, Right = VK_RIGHT
, Down = VK_DOWN
, Select = VK_SELECT
, Print = VK_PRINT
, Execute = VK_EXECUTE
, Snapshot = VK_SNAPSHOT
, Delete = VK_DELETE
, Help = VK_HELP
, LeftWin = VK_LWIN
, RightWin = VK_RWIN
, Apps = VK_APPS
, Sleep = VK_SLEEP
, NumPad0 = VK_NUMPAD0
, NumPad1 = VK_NUMPAD1
, NumPad2 = VK_NUMPAD2
, NumPad3 = VK_NUMPAD3
, NumPad4 = VK_NUMPAD4
, NumPad5 = VK_NUMPAD5
, NumPad6 = VK_NUMPAD6
, NumPad7 = VK_NUMPAD7
, NumPad8 = VK_NUMPAD8
, NumPad9 = VK_NUMPAD9
, Multiply = VK_MULTIPLY
, Add = VK_ADD
, Separator = VK_SEPARATOR
, Subtract = VK_SUBTRACT
, Decimal = VK_DECIMAL
, Divide = VK_DIVIDE
, F1 = VK_F1
, F2 = VK_F2
, F3 = VK_F3
, F4 = VK_F4
, F5 = VK_F5
, F6 = VK_F6
, F7 = VK_F7
, F8 = VK_F8
, F9 = VK_F9
, F10 = VK_F10
, NumLock = VK_NUMLOCK
, Scroll = VK_SCROLL
, LeftShift = VK_LSHIFT
, RightShift = VK_RSHIFT
, LeftCtrl = VK_LCONTROL
, RightCtrl = VK_RCONTROL
, LeftMenu = VK_LMENU
, RightMenu = VK_RMENU
};
class key_event_args
: public event_args
{
public:
key_event_args(WPARAM wparam, LPARAM lparam)
: event_args(wparam, lparam)
, _key(static_cast<VirtualKey>(wparam))
, _ch(wparam)
, _rpt_cnt(LOWORD(lparam))
{
}
VirtualKey key() const
{
return _key;
}
char ch() const
{
return _ch;
}
int repeat_count() const
{
return _rpt_cnt;
}
private:
VirtualKey _key;
char _ch;
int _rpt_cnt;
};
class window_class
{
public:
window_class(const std::string &name, WNDPROC winproc)
: _name(name)
{
WNDCLASSEXA wcx{ sizeof(wcx) };
wcx.lpfnWndProc = winproc;
wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcx.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcx.lpszClassName = name.c_str();
wcx.style = CS_HREDRAW | CS_VREDRAW;
_atom = RegisterClassExA(&wcx);
}
~window_class()
{
UnregisterClassA(_name.c_str(), nullptr);
}
operator LPSTR()
{
return (LPSTR)_atom;
}
private:
std::string _name;
ATOM _atom;
};
class window
{
public:
window(const std::string &title, int x, int y, int width, int height)
: _hwnd(nullptr)
, _hook(false)
, _org_wproc(nullptr)
{
create(title, x, y, width, height);
}
explicit window(const std::string &title = std::string())
: window(title, CW_USEDEFAULT, 0, 640, 480)
{
}
explicit window(HWND hwnd)
: _hwnd(hwnd)
, _hook(true)
{
_org_wproc = (WNDPROC)GetClassLongPtr(hwnd, GCLP_WNDPROC);
SetClassLongPtr(hwnd, GCLP_WNDPROC, (LONG_PTR)hook_winproc);
}
virtual ~window()
{
DestroyWindow(_hwnd);
}
operator HWND() const
{
return _hwnd;
}
void close()
{
CloseWindow(_hwnd);
}
void show()
{
ShowWindow(_hwnd, SW_SHOW);
}
#if (_MSC_VER >= 1800) || defined(__GNUG__)
using will_resize_event = event <window&, sizing_event_args &>;
using did_resize_event = event < window&, size_event_args & >;
using will_close_event = event < window&, cancel_event_args & >;
using did_close_event = event < window&, event_args & >;
using mouse_move_event = event < window &, mouse_event_args & >;
using mouse_press_event = event < window &, mouse_event_args & >;
using mouse_release_event = event < window &, mouse_event_args & >;
using mouse_dbclick_event = event < window &, mouse_event_args & >;
using mouse_wheel_event = event < window &, mouse_event_args & >;
using key_press_event = event < window &, key_event_args & >;
using key_release_event = event < window &, key_event_args & >;
using key_char_event = event < window &, key_event_args & >;
#else
typedef event<window &, sizing_event_args &> will_resize_event;
typedef event<window &, size_event_args &> did_resize_event;
typedef event<window &, cancel_event_args &> will_close_event;
typedef event<window &, event_args &> did_close_event;
typedef event<window &, mouse_event_args &> mouse_move_event;
typedef event<window &, mouse_event_args &> mouse_press_event;
typedef event<window &, mouse_event_args &> mouse_release_event;
typedef event<window &, mouse_event_args &> mouse_dbclick_event;
typedef event<window &, mouse_event_args &> mouse_wheel_event;
typedef event<window &, key_event_args &> key_press_event;
typedef event<window &, key_event_args &> key_release_event;
typedef event<window &, key_event_args &> key_char_event;
#endif
will_resize_event &will_resize()
{
return _will_resize;
}
did_resize_event &did_resize()
{
return _did_resize;
}
will_close_event &will_close()
{
return _will_close;
}
did_close_event &did_close()
{
return _did_close;
}
mouse_move_event &mouse_move()
{
return _mouse_move;
}
mouse_press_event &mouse_press()
{
return _mouse_press;
}
mouse_release_event &mouse_release()
{
return _mouse_release;
}
mouse_dbclick_event &mouse_dbclick()
{
return _mouse_dbclick;
}
mouse_wheel_event &mouse_wheel()
{
return _mouse_wheel;
}
key_press_event &key_press()
{
return _key_press;
}
key_release_event &key_release()
{
return _key_release;
}
key_char_event &key_char()
{
return _key_char;
}
void update()
{
UpdateWindow(_hwnd);
}
rect client_rect() const
{
RECT rc;
GetClientRect(_hwnd, &rc);
return rc;
}
protected:
virtual LRESULT msgproc(UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_CREATE:
_wcount++;
break;
case WM_DESTROY:
{
event_args args;
_did_close(*this, args);
_wcount--;
if (!_wcount) PostQuitMessage(0);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
auto hdc = BeginPaint(_hwnd, &ps);
paint(hdc, ps);
EndPaint(_hwnd, &ps);
}
break;
case WM_SIZE:
{
size_event_args args(wparam, lparam);
_did_resize(*this, args);
return TRUE;
}
break;
case WM_SIZING:
{
sizing_event_args args(wparam, lparam);
if (_will_resize(*this, args)) return TRUE;
}
break;
case WM_CLOSE:
{
cancel_event_args args(wparam, lparam);
if (_will_close(*this, args))
{
if (!args.canceled()) DestroyWindow(_hwnd);
return 0;
}
}
break;
case WM_MOUSEMOVE:
{
mouse_event_args args(wparam, lparam);
if (_mouse_move(*this, args)) return 0;
}
break;
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
{
mouse_event_args args(wparam, lparam);
if (_mouse_press(*this, args)) return 0;
}
break;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
{
mouse_event_args args(wparam, lparam);
if (_mouse_release(*this, args)) return 0;
}
break;
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
{
mouse_event_args args(wparam, lparam);
if (_mouse_dbclick(*this, args)) return 0;
}
break;
case WM_MOUSEWHEEL:
{
mouse_event_args args(wparam, lparam);
if (_mouse_wheel(*this, args)) return 0;
}
break;
case WM_KEYDOWN:
{
key_event_args args(wparam, lparam);
if (_key_press(*this, args)) return 0;
}
break;
case WM_KEYUP:
{
key_event_args args(wparam, lparam);
if (_key_release(*this, args)) return 0;
}
break;
case WM_CHAR:
{
key_event_args args(wparam, lparam);
if (_key_char(*this, args)) return 0;
}
break;
}
return _hook ? 0 : DefWindowProcA(_hwnd, msg, wparam, lparam);
}
virtual void paint(HDC hdc, const PAINTSTRUCT &ps)
{
}
private:
void create(const std::string &title, int x, int y, int width, int height)
{
RECT rc{ x, y, x + width, y + height };
DWORD style = WS_OVERLAPPEDWINDOW;
AdjustWindowRect(&rc, style, FALSE);
width = rc.right - rc.left;
height = rc.bottom - rc.top;
_hwnd = CreateWindowA(_cls
, title.c_str()
, style
, x
, y
, width
, height
, nullptr
, nullptr
, nullptr
, this);
}
static LRESULT CALLBACK winproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
if (msg == WM_NCCREATE)
{
CREATESTRUCTA *cs = reinterpret_cast<CREATESTRUCTA *>(lparam);
SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)(cs->lpCreateParams));
}
window *w = reinterpret_cast<window *>(GetWindowLongPtrA(hwnd, GWLP_USERDATA));
if (w)
{
w->_hwnd = hwnd;
return w->msgproc(msg, wparam, lparam);
}
return DefWindowProcA(hwnd, msg, wparam, lparam);
}
static LRESULT CALLBACK hook_winproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
window *w = reinterpret_cast<window *>(GetWindowLongPtrA(hwnd, GWLP_USERDATA));
if (w)
{
w->msgproc(msg, wparam, lparam);
if (w->_org_wproc)
return CallWindowProc(w->_org_wproc, hwnd, msg, wparam, lparam);
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
private:
//using msg_translator = std::function < LRESULT(WPARAM, LPARAM) > ;
//std::map<UINT, msg_translator> _msg_trans;
static int _wcount;
static window_class _cls;
HWND _hwnd;
bool _hook;
WNDPROC _org_wproc;
will_resize_event _will_resize;
did_resize_event _did_resize;
will_close_event _will_close;
did_close_event _did_close;
mouse_move_event _mouse_move;
mouse_press_event _mouse_press;
mouse_release_event _mouse_release;
mouse_dbclick_event _mouse_dbclick;
mouse_wheel_event _mouse_wheel;
key_press_event _key_press;
key_release_event _key_release;
key_char_event _key_char;
};
class application
{
public:
int run()
{
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.message == WM_QUIT) ? msg.wParam : 0;
}
};