absence_thelab

annotate src/nwt/nucwin.cpp @ 0:1cffe3409164

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