absence_thelab

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/nwt/nucwin.cpp	Thu Oct 23 01:46:07 2014 +0300
     1.3 @@ -0,0 +1,174 @@
     1.4 +#include "nucwin.h"
     1.5 +#include <windows.h>
     1.6 +#include <winuser.h>
     1.7 +
     1.8 +#ifndef WM_MOUSEWHEEL
     1.9 +#define WM_MOUSEWHEEL 0x020A
    1.10 +#endif	// WM_MOUSEWHEEL
    1.11 +
    1.12 +MainLoopMode loopmode;
    1.13 +char *Arguments;
    1.14 +
    1.15 +int (*KeyHandlerFunc)(Widget*, int) = 0;
    1.16 +int (*CloseHandlerFunc)(Widget*, int) = 0;
    1.17 +int (*MouseHandlerFunc)(Widget*, int, int, bool, bool, bool) = 0;
    1.18 +int (*MouseUpHandlerFunc)(Widget*, int, int, bool, bool, bool) = 0;
    1.19 +int (*MouseWheelHandlerFunc)(Widget*, int, int, int) = 0;
    1.20 +int (*WinMoveHandlerFunc)(Widget*, int, int) = 0;
    1.21 +int (*PaintHandlerFunc)(Widget*) = 0;
    1.22 +
    1.23 +void (*RealTimeLoopFunc)() = 0;
    1.24 +
    1.25 +void SetHandler(HandlerType htype, int (*Handler)(Widget*, int)) {
    1.26 +	switch(htype) {
    1.27 +	case HANDLER_KEY:
    1.28 +		KeyHandlerFunc = Handler;
    1.29 +		break;
    1.30 +
    1.31 +	case HANDLER_CLOSE:
    1.32 +		CloseHandlerFunc = Handler;
    1.33 +		break;
    1.34 +
    1.35 +	case HANDLER_MOUSE:
    1.36 +		MouseHandlerFunc = (int (*)(Widget*, int, int, bool, bool, bool))Handler;
    1.37 +		break;
    1.38 +
    1.39 +	case HANDLER_MOUSEUP:
    1.40 +		MouseUpHandlerFunc = (int (*)(Widget*, int, int, bool, bool, bool))Handler;
    1.41 +		break;
    1.42 +
    1.43 +	case HANDLER_WHEEL:
    1.44 +		MouseWheelHandlerFunc = (int (*)(Widget*, int, int, int))Handler;
    1.45 +		break;
    1.46 +
    1.47 +	case HANDLER_WINMOVE:
    1.48 +		WinMoveHandlerFunc = (int (*)(Widget*, int, int))Handler;
    1.49 +		break;
    1.50 +
    1.51 +	case HANDLER_PAINT:
    1.52 +		PaintHandlerFunc = (int (*)(Widget*))Handler;
    1.53 +		break;
    1.54 +
    1.55 +	default: break;
    1.56 +	}
    1.57 +}
    1.58 +
    1.59 +void SetMainLoopFunc(void (*func)()) {
    1.60 +	RealTimeLoopFunc = func;
    1.61 +}
    1.62 +
    1.63 +int NWMainLoop(MainLoopMode mode) {
    1.64 +
    1.65 +	MSG msg;
    1.66 +	loopmode = mode;
    1.67 +
    1.68 +	if(mode == RealTimeLoop) {
    1.69 +		while(1) {
    1.70 +			if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
    1.71 +				if(msg.message == WM_QUIT) break;
    1.72 +				TranslateMessage(&msg);
    1.73 +				DispatchMessage(&msg);
    1.74 +			} else {
    1.75 +				if(RealTimeLoopFunc) RealTimeLoopFunc();
    1.76 +			}
    1.77 +		}
    1.78 +	} else {
    1.79 +		while(GetMessage(&msg, 0, 0, 0)) {
    1.80 +			TranslateMessage(&msg);
    1.81 +			DispatchMessage(&msg);
    1.82 +		}
    1.83 +	}
    1.84 +
    1.85 +	return (int)msg.wParam;
    1.86 +}
    1.87 +
    1.88 +bool NWCheckForMessages() {
    1.89 +	MSG msg;
    1.90 +	return (bool)PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE);
    1.91 +}
    1.92 +
    1.93 +
    1.94 +LRESULT CALLBACK MainHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    1.95 +	switch(msg) {
    1.96 +	case WM_KEYDOWN:
    1.97 +		if(KeyHandlerFunc) return KeyHandlerFunc(hWnd, (int)wParam);
    1.98 +		break;
    1.99 +
   1.100 +	case WM_CLOSE:
   1.101 +		if(CloseHandlerFunc) CloseHandlerFunc(0, 0);
   1.102 +		DestroyWindow(hWnd);
   1.103 +		PostQuitMessage(0);
   1.104 +		return 0;
   1.105 +		break;
   1.106 +
   1.107 +	case WM_MOUSEMOVE:
   1.108 +		if(MouseHandlerFunc) MouseHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), (bool)(wParam & MK_LBUTTON), (bool)(wParam & MK_MBUTTON), (bool)(wParam & MK_RBUTTON));
   1.109 +		break;
   1.110 +
   1.111 +	case WM_LBUTTONDOWN:
   1.112 +		if(MouseHandlerFunc) MouseHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), true, false, false);
   1.113 +		break;
   1.114 +
   1.115 +	case WM_MBUTTONDOWN:
   1.116 +		if(MouseHandlerFunc) MouseHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), false, true, false);
   1.117 +		break;
   1.118 +
   1.119 +	case WM_RBUTTONDOWN:
   1.120 +		if(MouseHandlerFunc) MouseHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), false, false, true);
   1.121 +		break;
   1.122 +
   1.123 +	case WM_LBUTTONUP:
   1.124 +		if(MouseUpHandlerFunc) MouseUpHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), true, false, false);
   1.125 +		break;
   1.126 +
   1.127 +	case WM_MBUTTONUP:
   1.128 +		if(MouseUpHandlerFunc) MouseUpHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), false, true, false);
   1.129 +		break;
   1.130 +
   1.131 +	case WM_RBUTTONUP:
   1.132 +		if(MouseUpHandlerFunc) MouseUpHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), false, false, true);
   1.133 +		break;
   1.134 +
   1.135 +	case WM_MOUSEWHEEL:
   1.136 +		if(MouseWheelHandlerFunc) MouseWheelHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam), (short)HIWORD(wParam));
   1.137 +		break;
   1.138 +
   1.139 +	case WM_MOVE:
   1.140 +		if(WinMoveHandlerFunc) WinMoveHandlerFunc(hWnd, LOWORD(lParam), HIWORD(lParam));
   1.141 +		break;
   1.142 +
   1.143 +	case WM_PAINT:
   1.144 +		if(PaintHandlerFunc) PaintHandlerFunc(hWnd);
   1.145 +	}
   1.146 +
   1.147 +	return DefWindowProc(hWnd, msg, wParam, lParam);
   1.148 +}
   1.149 +
   1.150 +Point NWGetScreenSize() {
   1.151 +	return Point(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
   1.152 +}
   1.153 +
   1.154 +void NWCloseWindow(Widget *win) {
   1.155 +	PostMessage(win, WM_CLOSE, 0, 0);
   1.156 +}
   1.157 +
   1.158 +std::string NWFileSaveChooser(Widget *win, const char *title, const char *filetypes, const char *defext) {
   1.159 +	
   1.160 +	char fname[512];
   1.161 +	fname[0] = 0;
   1.162 +
   1.163 +	OPENFILENAME ofn;
   1.164 +	memset(&ofn, 0, sizeof(OPENFILENAME));
   1.165 +	ofn.lStructSize = sizeof(OPENFILENAME);
   1.166 +	ofn.hwndOwner = win;
   1.167 +	ofn.lpstrFilter = filetypes;
   1.168 +	ofn.lpstrFile = fname;
   1.169 +	ofn.nMaxFile = 512;
   1.170 +	ofn.lpstrTitle = title;
   1.171 +	ofn.lpstrDefExt = defext;
   1.172 +	ofn.Flags = OFN_PATHMUSTEXIST;
   1.173 +
   1.174 +	if(!GetSaveFileName(&ofn)) return "";
   1.175 +
   1.176 +	return fname;
   1.177 +}
   1.178 \ No newline at end of file