#include "api.h" #include #include #include static HWND g_console = 0; int main(int argc, char* argv[]) { g_console = FindWindowEx(NULL, NULL, "ConsoleWindowClass", NULL); api_add_path("prog"); api_add_path("cls"); api_add_path("lib"); for (int i = 0; i < argc; i++) { api_add_arg_ikey(i, argv[i]); std::string s(argv[i]); size_t loc = s.find("=", 0); if (loc != std::string::npos) { if (loc == s.size() - 1) { printf("bad argument: %s\n", argv[i]); return 1; } api_add_arg_skey(s.substr(0, loc).c_str(), s.substr(loc+1).c_str()); } } char err[256], *fname = argv[0]; if (argc > 1) { fname = argv[1]; } else { int len = strlen(fname); if (len > 4 && fname[len-4] == '.') fname[len-4] = 0; } void *module = api_parse_file(fname, err); if (!module) { fprintf(stderr, "%s\n", err); } else { #ifdef EMBEDDED_LIBRARY extern void init_embedded_mysql(); init_embedded_mysql(); #endif if (api_exec_module(module, err) <= 0) fprintf(stderr, "%s\n", err); api_delete_module(module); } if (g_console) ShowWindow(g_console, SW_SHOWNOACTIVATE); return 0; } ///////////////////////////////////////////////////////////////////// #define PROG_NAME "zeGraph" static HWND g_hWnd = 0; static void *g_callback = 0; static void* g_caller = 0; static char g_title[256]; class zsRegPrimitive { public: zsRegPrimitive(const char* name, void* func) { api_add_primitive(name, 0, func); } }; void process_message(const char* message, HWND hWnd, WPARAM wParam, LPARAM lParam) { if (!g_callback) return; void *p[8]; p[0] = api_create_user(g_caller, hWnd, 0, 0, 0); p[1] = api_create_string(g_caller, message); p[2] = api_create_integer(g_caller, wParam); p[3] = api_create_integer(g_caller, LOWORD(wParam)); p[4] = api_create_integer(g_caller, HIWORD(wParam)); p[5] = api_create_integer(g_caller, lParam); p[6] = api_create_integer(g_caller, LOWORD(lParam)); p[7] = api_create_integer(g_caller, HIWORD(lParam)); api_call_func(g_callback, 8, p); } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch(message) { case WM_CREATE: if (g_hWnd == 0) g_hWnd = hWnd; process_message("CREATE", hWnd, wParam, lParam); return 0; case WM_COMMAND: process_message("COMMAND", hWnd, wParam, lParam); return 0; case WM_SIZE: process_message("SIZE", hWnd, wParam, lParam); return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); process_message("PAINT", hWnd, wParam, lParam); EndPaint(hWnd, &ps); return 0; case WM_KEYUP: process_message("KEYUP", hWnd, wParam, lParam); return 0; case WM_KEYDOWN: process_message("KEYDOW", hWnd, wParam, lParam); return 0; case WM_LBUTTONUP: process_message("LBUTTONUP", hWnd, wParam, lParam); return 0; case WM_LBUTTONDOWN: process_message("LBUTTONDOWN", hWnd, wParam, lParam); return 0; case WM_LBUTTONDBLCLK: process_message("LBUTTONDBLCLK", hWnd, wParam, lParam); return 0; case WM_MOUSEMOVE: process_message("MOUSEMOVE", hWnd, wParam, lParam); return 0; case WM_RBUTTONUP: process_message("RBUTTONUP", hWnd, wParam, lParam); return 0; case WM_RBUTTONDOWN: process_message("RBUTTONDOWN", hWnd, wParam, lParam); return 0; case WM_RBUTTONDBLCLK: process_message("RBUTTONDBLCLK", hWnd, wParam, lParam); return 0; case WM_TIMER: process_message("TIMER", hWnd, wParam, lParam); return 0; case WM_DESTROY: PostQuitMessage(0); g_hWnd = 0; return 0; default: return DefWindowProc(hWnd, message, wParam, lParam); } } void* main_window(void* caller, int nargs, void **args) { if (nargs < 1) return 0; g_caller = caller; if (api_is_string(args[0]) && g_hWnd != 0) { if (!g_hWnd) return 0; const char *flag = api_get_string(caller, args[0]); if (strcmp(flag, "fullscreen") == 0) { HWND hwnd = GetDesktopWindow(); RECT rect; GetWindowRect(hwnd, &rect); int w = rect.right-rect.left; int h = rect.bottom-rect.top; SetWindowPos(g_hWnd, HWND_TOPMOST, 0, 0, w, h, SWP_SHOWWINDOW); } else if (strcmp(flag, "resize") == 0) { if (nargs < 3) return 0; SetWindowPos(g_hWnd, HWND_TOPMOST, 10, 10, api_get_integer(caller, args[1]), api_get_integer(caller, args[2]), SWP_SHOWWINDOW); } else if (strcmp(flag, "timer") == 0) { if (nargs < 2) return 0; KillTimer(g_hWnd, (int)g_hWnd); int ms = api_get_integer(caller, args[1]); if (ms > 0) SetTimer(g_hWnd, (int)g_hWnd, ms, NULL); } else if (strcmp(flag, "title") == 0) { if (nargs < 2) return 0; strcpy(g_title, api_get_string(caller, args[1])); SetWindowText(g_hWnd, (LPCTSTR)g_title); } return 0; } if (nargs < 3) return 0; int w = api_get_integer(caller, args[0]); if (w < 32) w = 32; int h = api_get_integer(caller, args[1]); if (h < 32) h = 32; g_callback = api_get_func(caller, api_get_string(caller, args[2])); WNDCLASS wndClass; wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; wndClass.lpfnWndProc = WndProc; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = 0; wndClass.hIcon = LoadIcon(NULL, IDI_WINLOGO); wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW); wndClass.lpszMenuName = 0; wndClass.lpszClassName = TEXT(PROG_NAME); RegisterClass(&wndClass); RECT rect = {0, 0, w, h}; AdjustWindowRect(&rect, WS_SYSMENU | WS_THICKFRAME, 1); HWND hWnd = CreateWindow( TEXT(PROG_NAME), TEXT(PROG_NAME), WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, rect.right-rect.left, rect.bottom-rect.top, 0, 0, 0, 0); g_hWnd = hWnd; ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); MSG msg; while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } g_hWnd = 0; return 0; } static zsRegPrimitive main0("window", main_window); void* main_hide(void* caller, int nargs, void **args) { if (g_console) ShowWindow(g_console, SW_HIDE); if (g_hWnd) SetForegroundWindow(g_hWnd); return 0; } static zsRegPrimitive main1("hide", main_hide); void* main_timer(void* caller, int nargs, void **args) { if (nargs < 1) api_runtime_error(caller, "bad arguments"); if (g_hWnd > 0) { KillTimer(g_hWnd, (int)g_hWnd); int ms = api_get_integer(caller, args[0]); if (ms > 0) SetTimer(g_hWnd, (int)g_hWnd, ms, NULL); } return 0; } static zsRegPrimitive main2("timer", main_timer);