rlm@1: // LuaOpenDialog.cpp : implementation file rlm@1: // rlm@1: rlm@1: //#include "stdafx.h" rlm@1: //#include "resource.h" rlm@1: #include "LuaOpenDialog.h" rlm@1: #include "MainWnd.h" rlm@1: #include "WinResUtil.h" rlm@1: #include "WinMiscUtil.h" rlm@1: #include "VBA.h" rlm@1: #include "Sound.h" rlm@1: rlm@1: #include "../common/vbalua.h" rlm@1: rlm@1: HWND LuaConsoleHWnd = NULL; rlm@1: HFONT hFont = NULL; rlm@1: LOGFONT LuaConsoleLogFont; rlm@1: rlm@1: struct ControlLayoutInfo rlm@1: { rlm@1: int controlID; rlm@1: rlm@1: enum LayoutType // what to do when the containing window resizes rlm@1: { rlm@1: NONE, // leave the control where it was rlm@1: RESIZE_END, // resize the control rlm@1: MOVE_START, // move the control rlm@1: }; rlm@1: LayoutType horizontalLayout; rlm@1: LayoutType verticalLayout; rlm@1: }; rlm@1: struct ControlLayoutState rlm@1: { rlm@1: int x,y,width,height; rlm@1: bool valid; rlm@1: ControlLayoutState() : valid(false) {} rlm@1: }; rlm@1: rlm@1: static ControlLayoutInfo controlLayoutInfos [] = { rlm@1: {IDC_LUACONSOLE, ControlLayoutInfo::RESIZE_END, ControlLayoutInfo::RESIZE_END}, rlm@1: {IDC_EDIT_LUAPATH, ControlLayoutInfo::RESIZE_END, ControlLayoutInfo::NONE}, rlm@1: {IDC_BUTTON_LUARUN, ControlLayoutInfo::MOVE_START, ControlLayoutInfo::NONE}, rlm@1: {IDC_BUTTON_LUASTOP, ControlLayoutInfo::MOVE_START, ControlLayoutInfo::NONE}, rlm@1: }; rlm@1: static const int numControlLayoutInfos = sizeof(controlLayoutInfos)/sizeof(*controlLayoutInfos); rlm@1: rlm@1: struct { rlm@1: int width; int height; rlm@1: ControlLayoutState layoutState [numControlLayoutInfos]; rlm@1: } windowInfo; rlm@1: rlm@1: void PrintToWindowConsole(int hDlgAsInt, const char* str) rlm@1: { rlm@1: HWND hDlg = (HWND)hDlgAsInt; rlm@1: HWND hConsole = GetDlgItem(hDlg, IDC_LUACONSOLE); rlm@1: rlm@1: int length = GetWindowTextLength(hConsole); rlm@1: if(length >= 250000) rlm@1: { rlm@1: // discard first half of text if it's getting too long rlm@1: SendMessage(hConsole, EM_SETSEL, 0, length/2); rlm@1: SendMessage(hConsole, EM_REPLACESEL, false, (LPARAM)""); rlm@1: length = GetWindowTextLength(hConsole); rlm@1: } rlm@1: SendMessage(hConsole, EM_SETSEL, length, length); rlm@1: rlm@1: //LuaPerWindowInfo& info = LuaWindowInfo[hDlg]; rlm@1: rlm@1: { rlm@1: SendMessage(hConsole, EM_REPLACESEL, false, (LPARAM)str); rlm@1: } rlm@1: } rlm@1: rlm@1: void WinLuaOnStart(int hDlgAsInt) rlm@1: { rlm@1: HWND hDlg = (HWND)hDlgAsInt; rlm@1: //LuaPerWindowInfo& info = LuaWindowInfo[hDlg]; rlm@1: //info.started = true; rlm@1: EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUABROWSE), false); // disable browse while running because it misbehaves if clicked in a frameadvance loop rlm@1: EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUASTOP), true); rlm@1: SetWindowText(GetDlgItem(hDlg, IDC_BUTTON_LUARUN), "Restart"); rlm@1: SetWindowText(GetDlgItem(hDlg, IDC_LUACONSOLE), ""); // clear the console rlm@1: // Show_Genesis_Screen(HWnd); // otherwise we might never show the first thing the script draws rlm@1: } rlm@1: rlm@1: void WinLuaOnStop(int hDlgAsInt) rlm@1: { rlm@1: HWND hDlg = (HWND)hDlgAsInt; rlm@1: //LuaPerWindowInfo& info = LuaWindowInfo[hDlg]; rlm@1: rlm@1: HWND prevWindow = GetActiveWindow(); rlm@1: SetActiveWindow(hDlg); // bring to front among other script/secondary windows, since a stopped script will have some message for the user that would be easier to miss otherwise rlm@1: if(prevWindow == AfxGetMainWnd()->GetSafeHwnd()) SetActiveWindow(prevWindow); rlm@1: rlm@1: //info.started = false; rlm@1: EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUABROWSE), true); rlm@1: EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUASTOP), false); rlm@1: SetWindowText(GetDlgItem(hDlg, IDC_BUTTON_LUARUN), "Run"); rlm@1: // if(statusOK) rlm@1: // Show_Genesis_Screen(MainWindow->getHWnd()); // otherwise we might never show the last thing the script draws rlm@1: //if(info.closeOnStop) rlm@1: // PostMessage(hDlg, WM_CLOSE, 0, 0); rlm@1: } rlm@1: rlm@1: INT_PTR CALLBACK DlgLuaScriptDialog(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) rlm@1: { rlm@1: RECT r; rlm@1: RECT r2; rlm@1: int dx1, dy1, dx2, dy2; rlm@1: rlm@1: switch (msg) { rlm@1: rlm@1: case WM_INITDIALOG: rlm@1: { rlm@1: // remove the 30000 character limit from the console control rlm@1: SendMessage(GetDlgItem(hDlg, IDC_LUACONSOLE),EM_LIMITTEXT,0,0); rlm@1: rlm@1: GetWindowRect(AfxGetMainWnd()->GetSafeHwnd(), &r); rlm@1: dx1 = (r.right - r.left) / 2; rlm@1: dy1 = (r.bottom - r.top) / 2; rlm@1: rlm@1: GetWindowRect(hDlg, &r2); rlm@1: dx2 = (r2.right - r2.left) / 2; rlm@1: dy2 = (r2.bottom - r2.top) / 2; rlm@1: rlm@1: int windowIndex = 0;//std::find(LuaScriptHWnds.begin(), LuaScriptHWnds.end(), hDlg) - LuaScriptHWnds.begin(); rlm@1: int staggerOffset = windowIndex * 24; rlm@1: r.left += staggerOffset; rlm@1: r.right += staggerOffset; rlm@1: r.top += staggerOffset; rlm@1: r.bottom += staggerOffset; rlm@1: rlm@1: // push it away from the main window if we can rlm@1: const int width = (r.right-r.left); rlm@1: const int width2 = (r2.right-r2.left); rlm@1: if(r.left+width2 + width < GetSystemMetrics(SM_CXSCREEN)) rlm@1: { rlm@1: r.right += width; rlm@1: r.left += width; rlm@1: } rlm@1: else if((int)r.left - (int)width2 > 0) rlm@1: { rlm@1: r.right -= width2; rlm@1: r.left -= width2; rlm@1: } rlm@1: rlm@1: SetWindowPos(hDlg, NULL, r.left, r.top, NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW); rlm@1: rlm@1: RECT r3; rlm@1: GetClientRect(hDlg, &r3); rlm@1: windowInfo.width = r3.right - r3.left; rlm@1: windowInfo.height = r3.bottom - r3.top; rlm@1: for(int i = 0; i < numControlLayoutInfos; i++) { rlm@1: ControlLayoutState& layoutState = windowInfo.layoutState[i]; rlm@1: layoutState.valid = false; rlm@1: } rlm@1: rlm@1: DragAcceptFiles(hDlg, true); rlm@1: SetDlgItemText(hDlg, IDC_EDIT_LUAPATH, VBAGetLuaScriptName()); rlm@1: rlm@1: SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &LuaConsoleLogFont, 0); // reset with an acceptable font rlm@1: return true; rlm@1: } break; rlm@1: rlm@1: case WM_SIZE: rlm@1: { rlm@1: // resize or move controls in the window as necessary when the window is resized rlm@1: rlm@1: //LuaPerWindowInfo& windowInfo = LuaWindowInfo[hDlg]; rlm@1: int prevDlgWidth = windowInfo.width; rlm@1: int prevDlgHeight = windowInfo.height; rlm@1: rlm@1: int dlgWidth = LOWORD(lParam); rlm@1: int dlgHeight = HIWORD(lParam); rlm@1: rlm@1: int deltaWidth = dlgWidth - prevDlgWidth; rlm@1: int deltaHeight = dlgHeight - prevDlgHeight; rlm@1: rlm@1: for(int i = 0; i < numControlLayoutInfos; i++) rlm@1: { rlm@1: ControlLayoutInfo layoutInfo = controlLayoutInfos[i]; rlm@1: ControlLayoutState& layoutState = windowInfo.layoutState[i]; rlm@1: rlm@1: HWND hCtrl = GetDlgItem(hDlg,layoutInfo.controlID); rlm@1: rlm@1: int x,y,width,height; rlm@1: if(layoutState.valid) rlm@1: { rlm@1: x = layoutState.x; rlm@1: y = layoutState.y; rlm@1: width = layoutState.width; rlm@1: height = layoutState.height; rlm@1: } rlm@1: else rlm@1: { rlm@1: RECT r; rlm@1: GetWindowRect(hCtrl, &r); rlm@1: POINT p = {r.left, r.top}; rlm@1: ScreenToClient(hDlg, &p); rlm@1: x = p.x; rlm@1: y = p.y; rlm@1: width = r.right - r.left; rlm@1: height = r.bottom - r.top; rlm@1: } rlm@1: rlm@1: switch(layoutInfo.horizontalLayout) rlm@1: { rlm@1: case ControlLayoutInfo::RESIZE_END: width += deltaWidth; break; rlm@1: case ControlLayoutInfo::MOVE_START: x += deltaWidth; break; rlm@1: default: break; rlm@1: } rlm@1: switch(layoutInfo.verticalLayout) rlm@1: { rlm@1: case ControlLayoutInfo::RESIZE_END: height += deltaHeight; break; rlm@1: case ControlLayoutInfo::MOVE_START: y += deltaHeight; break; rlm@1: default: break; rlm@1: } rlm@1: rlm@1: SetWindowPos(hCtrl, 0, x,y, width,height, 0); rlm@1: rlm@1: layoutState.x = x; rlm@1: layoutState.y = y; rlm@1: layoutState.width = width; rlm@1: layoutState.height = height; rlm@1: layoutState.valid = true; rlm@1: } rlm@1: rlm@1: windowInfo.width = dlgWidth; rlm@1: windowInfo.height = dlgHeight; rlm@1: rlm@1: RedrawWindow(hDlg, NULL, NULL, RDW_INVALIDATE); rlm@1: } break; rlm@1: rlm@1: case WM_COMMAND: rlm@1: switch (LOWORD(wParam)) { rlm@1: case IDOK: rlm@1: case IDCANCEL: { rlm@1: EndDialog(hDlg, true); // goto case WM_CLOSE; rlm@1: } break; rlm@1: rlm@1: case IDC_BUTTON_LUARUN: rlm@1: { rlm@1: char filename[MAX_PATH]; rlm@1: GetDlgItemText(hDlg, IDC_EDIT_LUAPATH, filename, MAX_PATH); rlm@1: VBALoadLuaCode(filename); rlm@1: } break; rlm@1: rlm@1: case IDC_BUTTON_LUASTOP: rlm@1: { rlm@1: VBALuaStop(); rlm@1: } break; rlm@1: rlm@1: case IDC_BUTTON_LUAEDIT: rlm@1: { rlm@1: char Str_Tmp [1024]; rlm@1: SendDlgItemMessage(hDlg,IDC_EDIT_LUAPATH,WM_GETTEXT,(WPARAM)512,(LPARAM)Str_Tmp); rlm@1: // tell the OS to open the file with its associated editor, rlm@1: // without blocking on it or leaving a command window open. rlm@1: if((int)ShellExecute(NULL, "edit", Str_Tmp, NULL, NULL, SW_SHOWNORMAL) == SE_ERR_NOASSOC) rlm@1: if((int)ShellExecute(NULL, "open", Str_Tmp, NULL, NULL, SW_SHOWNORMAL) == SE_ERR_NOASSOC) rlm@1: ShellExecute(NULL, NULL, "notepad", Str_Tmp, NULL, SW_SHOWNORMAL); rlm@1: } break; rlm@1: rlm@1: case IDC_BUTTON_LUABROWSE: rlm@1: { rlm@1: systemSoundClearBuffer(); rlm@1: rlm@1: CString filter = winResLoadFilter(IDS_FILTER_LUA); rlm@1: CString title = winResLoadString(IDS_SELECT_LUA_NAME); rlm@1: rlm@1: CString luaName = winGetDestFilename(theApp.gameFilename, IDS_LUA_DIR, ".lua"); rlm@1: CString luaDir = winGetDestDir(IDS_LUA_DIR); rlm@1: rlm@1: filter.Replace('|', '\000'); rlm@1: // char *p = filter.GetBuffer(0); rlm@1: // while ((p = strchr(p, '|')) != NULL) rlm@1: // *p++ = 0; rlm@1: rlm@1: OPENFILENAME ofn; rlm@1: ZeroMemory( (LPVOID)&ofn, sizeof(OPENFILENAME) ); rlm@1: ofn.lpstrFile = luaName.GetBuffer(MAX_PATH); rlm@1: ofn.nMaxFile = MAX_PATH; rlm@1: ofn.lStructSize = sizeof(OPENFILENAME); rlm@1: ofn.hwndOwner = hDlg; rlm@1: ofn.lpstrFilter = filter; rlm@1: ofn.nFilterIndex = 0; rlm@1: ofn.lpstrInitialDir = luaDir; rlm@1: ofn.lpstrTitle = title; rlm@1: ofn.lpstrDefExt = "lua"; rlm@1: ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_ENABLESIZING | OFN_EXPLORER; // hide previously-ignored read-only checkbox (the real read-only box is in the open-movie dialog itself) rlm@1: if(GetOpenFileName( &ofn )) rlm@1: { rlm@1: SetWindowText(GetDlgItem(hDlg, IDC_EDIT_LUAPATH), luaName); rlm@1: } rlm@1: return true; rlm@1: } break; rlm@1: rlm@1: case IDC_EDIT_LUAPATH: rlm@1: { rlm@1: char filename[MAX_PATH]; rlm@1: GetDlgItemText(hDlg, IDC_EDIT_LUAPATH, filename, MAX_PATH); rlm@1: FILE* file = fopen(filename, "rb"); rlm@1: EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUAEDIT), file != NULL); rlm@1: if(file) rlm@1: fclose(file); rlm@1: } break; rlm@1: rlm@1: case IDC_LUACONSOLE_CHOOSEFONT: rlm@1: { rlm@1: CHOOSEFONT cf; rlm@1: rlm@1: ZeroMemory(&cf, sizeof(cf)); rlm@1: cf.lStructSize = sizeof(CHOOSEFONT); rlm@1: cf.hwndOwner = hDlg; rlm@1: cf.lpLogFont = &LuaConsoleLogFont; rlm@1: cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT; rlm@1: if (ChooseFont(&cf)) { rlm@1: if (hFont) { rlm@1: DeleteObject(hFont); rlm@1: hFont = NULL; rlm@1: } rlm@1: hFont = CreateFontIndirect(&LuaConsoleLogFont); rlm@1: if (hFont) rlm@1: SendDlgItemMessage(hDlg, IDC_LUACONSOLE, WM_SETFONT, (WPARAM)hFont, 0); rlm@1: } rlm@1: } break; rlm@1: rlm@1: case IDC_LUACONSOLE_CLEAR: rlm@1: { rlm@1: SetWindowText(GetDlgItem(hDlg, IDC_LUACONSOLE), ""); rlm@1: } break; rlm@1: } rlm@1: break; rlm@1: rlm@1: case WM_CLOSE: { rlm@1: SendMessage(hDlg, WM_DESTROY, 0, 0); rlm@1: } break; rlm@1: rlm@1: case WM_DESTROY: { rlm@1: //VBALuaStop(); rlm@1: DragAcceptFiles(hDlg, FALSE); rlm@1: if (hFont) { rlm@1: DeleteObject(hFont); rlm@1: hFont = NULL; rlm@1: } rlm@1: LuaConsoleHWnd = NULL; rlm@1: } break; rlm@1: rlm@1: case WM_DROPFILES: { rlm@1: HDROP hDrop; rlm@1: //UINT fileNo; rlm@1: UINT fileCount; rlm@1: char filename[_MAX_PATH]; rlm@1: rlm@1: hDrop = (HDROP)wParam; rlm@1: fileCount = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0); rlm@1: if (fileCount > 0) { rlm@1: DragQueryFile(hDrop, 0, filename, sizeof(filename)); rlm@1: SetWindowText(GetDlgItem(hDlg, IDC_EDIT_LUAPATH), filename); rlm@1: } rlm@1: DragFinish(hDrop); rlm@1: return true; rlm@1: } break; rlm@1: rlm@1: } rlm@1: rlm@1: return false; rlm@1: rlm@1: }