absence_thelab

view src/nwt/nucwin.cpp @ 1:4d5933c261c3

todo and .hgignore
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Oct 2014 02:18:43 +0300
parents
children
line source
1 #include "nucwin.h"
2 #include <windows.h>
3 #include <winuser.h>
5 #ifndef WM_MOUSEWHEEL
6 #define WM_MOUSEWHEEL 0x020A
7 #endif // WM_MOUSEWHEEL
9 MainLoopMode loopmode;
10 char *Arguments;
12 int (*KeyHandlerFunc)(Widget*, int) = 0;
13 int (*CloseHandlerFunc)(Widget*, int) = 0;
14 int (*MouseHandlerFunc)(Widget*, int, int, bool, bool, bool) = 0;
15 int (*MouseUpHandlerFunc)(Widget*, int, int, bool, bool, bool) = 0;
16 int (*MouseWheelHandlerFunc)(Widget*, int, int, int) = 0;
17 int (*WinMoveHandlerFunc)(Widget*, int, int) = 0;
18 int (*PaintHandlerFunc)(Widget*) = 0;
20 void (*RealTimeLoopFunc)() = 0;
22 void SetHandler(HandlerType htype, int (*Handler)(Widget*, int)) {
23 switch(htype) {
24 case HANDLER_KEY:
25 KeyHandlerFunc = Handler;
26 break;
28 case HANDLER_CLOSE:
29 CloseHandlerFunc = Handler;
30 break;
32 case HANDLER_MOUSE:
33 MouseHandlerFunc = (int (*)(Widget*, int, int, bool, bool, bool))Handler;
34 break;
36 case HANDLER_MOUSEUP:
37 MouseUpHandlerFunc = (int (*)(Widget*, int, int, bool, bool, bool))Handler;
38 break;
40 case HANDLER_WHEEL:
41 MouseWheelHandlerFunc = (int (*)(Widget*, int, int, int))Handler;
42 break;
44 case HANDLER_WINMOVE:
45 WinMoveHandlerFunc = (int (*)(Widget*, int, int))Handler;
46 break;
48 case HANDLER_PAINT:
49 PaintHandlerFunc = (int (*)(Widget*))Handler;
50 break;
52 default: break;
53 }
54 }
56 void SetMainLoopFunc(void (*func)()) {
57 RealTimeLoopFunc = func;
58 }
60 int NWMainLoop(MainLoopMode mode) {
62 MSG msg;
63 loopmode = mode;
65 if(mode == RealTimeLoop) {
66 while(1) {
67 if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
68 if(msg.message == WM_QUIT) break;
69 TranslateMessage(&msg);
70 DispatchMessage(&msg);
71 } else {
72 if(RealTimeLoopFunc) RealTimeLoopFunc();
73 }
74 }
75 } else {
76 while(GetMessage(&msg, 0, 0, 0)) {
77 TranslateMessage(&msg);
78 DispatchMessage(&msg);
79 }
80 }
82 return (int)msg.wParam;
83 }
85 bool NWCheckForMessages() {
86 MSG msg;
87 return (bool)PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE);
88 }
91 LRESULT CALLBACK MainHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
92 switch(msg) {
93 case WM_KEYDOWN:
94 if(KeyHandlerFunc) return KeyHandlerFunc(hWnd, (int)wParam);
95 break;
97 case WM_CLOSE:
98 if(CloseHandlerFunc) CloseHandlerFunc(0, 0);
99 DestroyWindow(hWnd);
100 PostQuitMessage(0);
101 return 0;
102 break;
104 case WM_MOUSEMOVE:
105 if(MouseHandlerFunc) MouseHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), (bool)(wParam & MK_LBUTTON), (bool)(wParam & MK_MBUTTON), (bool)(wParam & MK_RBUTTON));
106 break;
108 case WM_LBUTTONDOWN:
109 if(MouseHandlerFunc) MouseHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), true, false, false);
110 break;
112 case WM_MBUTTONDOWN:
113 if(MouseHandlerFunc) MouseHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), false, true, false);
114 break;
116 case WM_RBUTTONDOWN:
117 if(MouseHandlerFunc) MouseHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), false, false, true);
118 break;
120 case WM_LBUTTONUP:
121 if(MouseUpHandlerFunc) MouseUpHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), true, false, false);
122 break;
124 case WM_MBUTTONUP:
125 if(MouseUpHandlerFunc) MouseUpHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), false, true, false);
126 break;
128 case WM_RBUTTONUP:
129 if(MouseUpHandlerFunc) MouseUpHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), false, false, true);
130 break;
132 case WM_MOUSEWHEEL:
133 if(MouseWheelHandlerFunc) MouseWheelHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), (short)HIWORD(wParam));
134 break;
136 case WM_MOVE:
137 if(WinMoveHandlerFunc) WinMoveHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam));
138 break;
140 case WM_PAINT:
141 if(PaintHandlerFunc) PaintHandlerFunc(hWnd);
142 }
144 return DefWindowProc(hWnd, msg, wParam, lParam);
145 }
147 Point NWGetScreenSize() {
148 return Point(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
149 }
151 void NWCloseWindow(Widget *win) {
152 PostMessage(win, WM_CLOSE, 0, 0);
153 }
155 std::string NWFileSaveChooser(Widget *win, const char *title, const char *filetypes, const char *defext) {
157 char fname[512];
158 fname[0] = 0;
160 OPENFILENAME ofn;
161 memset(&ofn, 0, sizeof(OPENFILENAME));
162 ofn.lStructSize = sizeof(OPENFILENAME);
163 ofn.hwndOwner = win;
164 ofn.lpstrFilter = filetypes;
165 ofn.lpstrFile = fname;
166 ofn.nMaxFile = 512;
167 ofn.lpstrTitle = title;
168 ofn.lpstrDefExt = defext;
169 ofn.Flags = OFN_PATHMUSTEXIST;
171 if(!GetSaveFileName(&ofn)) return "";
173 return fname;