diff src/win32/LuaOpenDialog.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/LuaOpenDialog.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,369 @@
     1.4 +// LuaOpenDialog.cpp : implementation file
     1.5 +//
     1.6 +
     1.7 +//#include "stdafx.h"
     1.8 +//#include "resource.h"
     1.9 +#include "LuaOpenDialog.h"
    1.10 +#include "MainWnd.h"
    1.11 +#include "WinResUtil.h"
    1.12 +#include "WinMiscUtil.h"
    1.13 +#include "VBA.h"
    1.14 +#include "Sound.h"
    1.15 +
    1.16 +#include "../common/vbalua.h"
    1.17 +
    1.18 +HWND LuaConsoleHWnd = NULL;
    1.19 +HFONT hFont = NULL;
    1.20 +LOGFONT LuaConsoleLogFont;
    1.21 +
    1.22 +struct ControlLayoutInfo
    1.23 +{
    1.24 +	int controlID;
    1.25 +	
    1.26 +	enum LayoutType // what to do when the containing window resizes
    1.27 +	{
    1.28 +		NONE, // leave the control where it was
    1.29 +		RESIZE_END, // resize the control
    1.30 +		MOVE_START, // move the control
    1.31 +	};
    1.32 +	LayoutType horizontalLayout;
    1.33 +	LayoutType verticalLayout;
    1.34 +};
    1.35 +struct ControlLayoutState
    1.36 +{
    1.37 +	int x,y,width,height;
    1.38 +	bool valid;
    1.39 +	ControlLayoutState() : valid(false) {}
    1.40 +};
    1.41 +
    1.42 +static ControlLayoutInfo controlLayoutInfos [] = {
    1.43 +	{IDC_LUACONSOLE, ControlLayoutInfo::RESIZE_END, ControlLayoutInfo::RESIZE_END},
    1.44 +	{IDC_EDIT_LUAPATH, ControlLayoutInfo::RESIZE_END, ControlLayoutInfo::NONE},
    1.45 +	{IDC_BUTTON_LUARUN, ControlLayoutInfo::MOVE_START, ControlLayoutInfo::NONE},
    1.46 +	{IDC_BUTTON_LUASTOP, ControlLayoutInfo::MOVE_START, ControlLayoutInfo::NONE},
    1.47 +};
    1.48 +static const int numControlLayoutInfos = sizeof(controlLayoutInfos)/sizeof(*controlLayoutInfos);
    1.49 +
    1.50 +struct {
    1.51 +	int width; int height;
    1.52 +	ControlLayoutState layoutState [numControlLayoutInfos];
    1.53 +} windowInfo;
    1.54 +
    1.55 +void PrintToWindowConsole(int hDlgAsInt, const char* str)
    1.56 +{
    1.57 +	HWND hDlg = (HWND)hDlgAsInt;
    1.58 +	HWND hConsole = GetDlgItem(hDlg, IDC_LUACONSOLE);
    1.59 +
    1.60 +	int length = GetWindowTextLength(hConsole);
    1.61 +	if(length >= 250000)
    1.62 +	{
    1.63 +		// discard first half of text if it's getting too long
    1.64 +		SendMessage(hConsole, EM_SETSEL, 0, length/2);
    1.65 +		SendMessage(hConsole, EM_REPLACESEL, false, (LPARAM)"");
    1.66 +		length = GetWindowTextLength(hConsole);
    1.67 +	}
    1.68 +	SendMessage(hConsole, EM_SETSEL, length, length);
    1.69 +
    1.70 +	//LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
    1.71 +
    1.72 +	{
    1.73 +		SendMessage(hConsole, EM_REPLACESEL, false, (LPARAM)str);
    1.74 +	}
    1.75 +}
    1.76 +
    1.77 +void WinLuaOnStart(int hDlgAsInt)
    1.78 +{
    1.79 +	HWND hDlg = (HWND)hDlgAsInt;
    1.80 +	//LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
    1.81 +	//info.started = true;
    1.82 +	EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUABROWSE), false); // disable browse while running because it misbehaves if clicked in a frameadvance loop
    1.83 +	EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUASTOP), true);
    1.84 +	SetWindowText(GetDlgItem(hDlg, IDC_BUTTON_LUARUN), "Restart");
    1.85 +	SetWindowText(GetDlgItem(hDlg, IDC_LUACONSOLE), ""); // clear the console
    1.86 +//	Show_Genesis_Screen(HWnd); // otherwise we might never show the first thing the script draws
    1.87 +}
    1.88 +
    1.89 +void WinLuaOnStop(int hDlgAsInt)
    1.90 +{
    1.91 +	HWND hDlg = (HWND)hDlgAsInt;
    1.92 +	//LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
    1.93 +
    1.94 +	HWND prevWindow = GetActiveWindow();
    1.95 +	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
    1.96 +	if(prevWindow == AfxGetMainWnd()->GetSafeHwnd()) SetActiveWindow(prevWindow);
    1.97 +
    1.98 +	//info.started = false;
    1.99 +	EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUABROWSE), true);
   1.100 +	EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUASTOP), false);
   1.101 +	SetWindowText(GetDlgItem(hDlg, IDC_BUTTON_LUARUN), "Run");
   1.102 +//	if(statusOK)
   1.103 +//		Show_Genesis_Screen(MainWindow->getHWnd()); // otherwise we might never show the last thing the script draws
   1.104 +	//if(info.closeOnStop)
   1.105 +	//	PostMessage(hDlg, WM_CLOSE, 0, 0);
   1.106 +}
   1.107 +
   1.108 +INT_PTR CALLBACK DlgLuaScriptDialog(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
   1.109 +{
   1.110 +	RECT r;
   1.111 +	RECT r2;
   1.112 +	int dx1, dy1, dx2, dy2;
   1.113 +
   1.114 +	switch (msg) {
   1.115 +
   1.116 +	case WM_INITDIALOG:
   1.117 +	{
   1.118 +		// remove the 30000 character limit from the console control
   1.119 +		SendMessage(GetDlgItem(hDlg, IDC_LUACONSOLE),EM_LIMITTEXT,0,0);
   1.120 +
   1.121 +		GetWindowRect(AfxGetMainWnd()->GetSafeHwnd(), &r);
   1.122 +		dx1 = (r.right - r.left) / 2;
   1.123 +		dy1 = (r.bottom - r.top) / 2;
   1.124 +
   1.125 +		GetWindowRect(hDlg, &r2);
   1.126 +		dx2 = (r2.right - r2.left) / 2;
   1.127 +		dy2 = (r2.bottom - r2.top) / 2;
   1.128 +
   1.129 +		int windowIndex = 0;//std::find(LuaScriptHWnds.begin(), LuaScriptHWnds.end(), hDlg) - LuaScriptHWnds.begin();
   1.130 +		int staggerOffset = windowIndex * 24;
   1.131 +		r.left += staggerOffset;
   1.132 +		r.right += staggerOffset;
   1.133 +		r.top += staggerOffset;
   1.134 +		r.bottom += staggerOffset;
   1.135 +
   1.136 +		// push it away from the main window if we can
   1.137 +		const int width = (r.right-r.left); 
   1.138 +		const int width2 = (r2.right-r2.left); 
   1.139 +		if(r.left+width2 + width < GetSystemMetrics(SM_CXSCREEN))
   1.140 +		{
   1.141 +			r.right += width;
   1.142 +			r.left += width;
   1.143 +		}
   1.144 +		else if((int)r.left - (int)width2 > 0)
   1.145 +		{
   1.146 +			r.right -= width2;
   1.147 +			r.left -= width2;
   1.148 +		}
   1.149 +
   1.150 +		SetWindowPos(hDlg, NULL, r.left, r.top, NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
   1.151 +
   1.152 +		RECT r3;
   1.153 +		GetClientRect(hDlg, &r3);
   1.154 +		windowInfo.width = r3.right - r3.left;
   1.155 +		windowInfo.height = r3.bottom - r3.top;
   1.156 +		for(int i = 0; i < numControlLayoutInfos; i++) {
   1.157 +			ControlLayoutState& layoutState = windowInfo.layoutState[i];
   1.158 +			layoutState.valid = false;
   1.159 +		}
   1.160 +
   1.161 +		DragAcceptFiles(hDlg, true);
   1.162 +		SetDlgItemText(hDlg, IDC_EDIT_LUAPATH, VBAGetLuaScriptName());
   1.163 +
   1.164 +		SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &LuaConsoleLogFont, 0); // reset with an acceptable font
   1.165 +		return true;
   1.166 +	}	break;
   1.167 +
   1.168 +	case WM_SIZE:
   1.169 +	{
   1.170 +		// resize or move controls in the window as necessary when the window is resized
   1.171 +
   1.172 +		//LuaPerWindowInfo& windowInfo = LuaWindowInfo[hDlg];
   1.173 +		int prevDlgWidth = windowInfo.width;
   1.174 +		int prevDlgHeight = windowInfo.height;
   1.175 +
   1.176 +		int dlgWidth = LOWORD(lParam);
   1.177 +		int dlgHeight = HIWORD(lParam);
   1.178 +
   1.179 +		int deltaWidth = dlgWidth - prevDlgWidth;
   1.180 +		int deltaHeight = dlgHeight - prevDlgHeight;
   1.181 +
   1.182 +		for(int i = 0; i < numControlLayoutInfos; i++)
   1.183 +		{
   1.184 +			ControlLayoutInfo layoutInfo = controlLayoutInfos[i];
   1.185 +			ControlLayoutState& layoutState = windowInfo.layoutState[i];
   1.186 +
   1.187 +			HWND hCtrl = GetDlgItem(hDlg,layoutInfo.controlID);
   1.188 +
   1.189 +			int x,y,width,height;
   1.190 +			if(layoutState.valid)
   1.191 +			{
   1.192 +				x = layoutState.x;
   1.193 +				y = layoutState.y;
   1.194 +				width = layoutState.width;
   1.195 +				height = layoutState.height;
   1.196 +			}
   1.197 +			else
   1.198 +			{
   1.199 +				RECT r;
   1.200 +				GetWindowRect(hCtrl, &r);
   1.201 +				POINT p = {r.left, r.top};
   1.202 +				ScreenToClient(hDlg, &p);
   1.203 +				x = p.x;
   1.204 +				y = p.y;
   1.205 +				width = r.right - r.left;
   1.206 +				height = r.bottom - r.top;
   1.207 +			}
   1.208 +
   1.209 +			switch(layoutInfo.horizontalLayout)
   1.210 +			{
   1.211 +				case ControlLayoutInfo::RESIZE_END: width += deltaWidth; break;
   1.212 +				case ControlLayoutInfo::MOVE_START: x += deltaWidth; break;
   1.213 +				default: break;
   1.214 +			}
   1.215 +			switch(layoutInfo.verticalLayout)
   1.216 +			{
   1.217 +				case ControlLayoutInfo::RESIZE_END: height += deltaHeight; break;
   1.218 +				case ControlLayoutInfo::MOVE_START: y += deltaHeight; break;
   1.219 +				default: break;
   1.220 +			}
   1.221 +
   1.222 +			SetWindowPos(hCtrl, 0, x,y, width,height, 0);
   1.223 +
   1.224 +			layoutState.x = x;
   1.225 +			layoutState.y = y;
   1.226 +			layoutState.width = width;
   1.227 +			layoutState.height = height;
   1.228 +			layoutState.valid = true;
   1.229 +		}
   1.230 +
   1.231 +		windowInfo.width = dlgWidth;
   1.232 +		windowInfo.height = dlgHeight;
   1.233 +
   1.234 +		RedrawWindow(hDlg, NULL, NULL, RDW_INVALIDATE);
   1.235 +	}	break;
   1.236 +
   1.237 +	case WM_COMMAND:
   1.238 +		switch (LOWORD(wParam)) {
   1.239 +			case IDOK:
   1.240 +			case IDCANCEL: {
   1.241 +				EndDialog(hDlg, true); // goto case WM_CLOSE;
   1.242 +			}	break;
   1.243 +
   1.244 +			case IDC_BUTTON_LUARUN:
   1.245 +			{
   1.246 +				char filename[MAX_PATH];
   1.247 +				GetDlgItemText(hDlg, IDC_EDIT_LUAPATH, filename, MAX_PATH);
   1.248 +				VBALoadLuaCode(filename);
   1.249 +			}	break;
   1.250 +
   1.251 +			case IDC_BUTTON_LUASTOP:
   1.252 +			{
   1.253 +				VBALuaStop();
   1.254 +			}	break;
   1.255 +
   1.256 +			case IDC_BUTTON_LUAEDIT:
   1.257 +			{
   1.258 +				char Str_Tmp [1024];
   1.259 +				SendDlgItemMessage(hDlg,IDC_EDIT_LUAPATH,WM_GETTEXT,(WPARAM)512,(LPARAM)Str_Tmp);
   1.260 +				// tell the OS to open the file with its associated editor,
   1.261 +				// without blocking on it or leaving a command window open.
   1.262 +				if((int)ShellExecute(NULL, "edit", Str_Tmp, NULL, NULL, SW_SHOWNORMAL) == SE_ERR_NOASSOC)
   1.263 +					if((int)ShellExecute(NULL, "open", Str_Tmp, NULL, NULL, SW_SHOWNORMAL) == SE_ERR_NOASSOC)
   1.264 +						ShellExecute(NULL, NULL, "notepad", Str_Tmp, NULL, SW_SHOWNORMAL);
   1.265 +			}	break;
   1.266 +
   1.267 +			case IDC_BUTTON_LUABROWSE:
   1.268 +			{
   1.269 +				systemSoundClearBuffer();
   1.270 +
   1.271 +				CString filter = winResLoadFilter(IDS_FILTER_LUA);
   1.272 +				CString title  = winResLoadString(IDS_SELECT_LUA_NAME);
   1.273 +
   1.274 +				CString luaName = winGetDestFilename(theApp.gameFilename, IDS_LUA_DIR, ".lua");
   1.275 +				CString luaDir = winGetDestDir(IDS_LUA_DIR);
   1.276 +
   1.277 +				filter.Replace('|', '\000');
   1.278 +//				char *p = filter.GetBuffer(0);
   1.279 +//				while ((p = strchr(p, '|')) != NULL)
   1.280 +//					*p++ = 0;
   1.281 +
   1.282 +				OPENFILENAME  ofn;
   1.283 +				ZeroMemory( (LPVOID)&ofn, sizeof(OPENFILENAME) );
   1.284 +				ofn.lpstrFile       = luaName.GetBuffer(MAX_PATH);
   1.285 +				ofn.nMaxFile        = MAX_PATH;
   1.286 +				ofn.lStructSize     = sizeof(OPENFILENAME);
   1.287 +				ofn.hwndOwner       = hDlg;
   1.288 +				ofn.lpstrFilter     = filter;
   1.289 +				ofn.nFilterIndex    = 0;
   1.290 +				ofn.lpstrInitialDir = luaDir;
   1.291 +				ofn.lpstrTitle      = title;
   1.292 +				ofn.lpstrDefExt     = "lua";
   1.293 +				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)
   1.294 +				if(GetOpenFileName( &ofn ))
   1.295 +				{
   1.296 +					SetWindowText(GetDlgItem(hDlg, IDC_EDIT_LUAPATH), luaName);
   1.297 +				}
   1.298 +				return true;
   1.299 +			}	break;
   1.300 +
   1.301 +			case IDC_EDIT_LUAPATH:
   1.302 +			{
   1.303 +				char filename[MAX_PATH];
   1.304 +				GetDlgItemText(hDlg, IDC_EDIT_LUAPATH, filename, MAX_PATH);
   1.305 +				FILE* file = fopen(filename, "rb");
   1.306 +				EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUAEDIT), file != NULL);
   1.307 +				if(file)
   1.308 +					fclose(file);
   1.309 +			}	break;
   1.310 +
   1.311 +			case IDC_LUACONSOLE_CHOOSEFONT:
   1.312 +			{
   1.313 +				CHOOSEFONT cf;
   1.314 +
   1.315 +				ZeroMemory(&cf, sizeof(cf));
   1.316 +				cf.lStructSize = sizeof(CHOOSEFONT);
   1.317 +				cf.hwndOwner = hDlg;
   1.318 +				cf.lpLogFont = &LuaConsoleLogFont;
   1.319 +				cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
   1.320 +				if (ChooseFont(&cf)) {
   1.321 +					if (hFont) {
   1.322 +						DeleteObject(hFont);
   1.323 +						hFont = NULL;
   1.324 +					}
   1.325 +					hFont = CreateFontIndirect(&LuaConsoleLogFont);
   1.326 +					if (hFont)
   1.327 +						SendDlgItemMessage(hDlg, IDC_LUACONSOLE, WM_SETFONT, (WPARAM)hFont, 0);
   1.328 +				}
   1.329 +			}	break;
   1.330 +
   1.331 +			case IDC_LUACONSOLE_CLEAR:
   1.332 +			{
   1.333 +				SetWindowText(GetDlgItem(hDlg, IDC_LUACONSOLE), "");
   1.334 +			}	break;
   1.335 +		}
   1.336 +		break;
   1.337 +
   1.338 +	case WM_CLOSE: {
   1.339 +		SendMessage(hDlg, WM_DESTROY, 0, 0);
   1.340 +	}	break;
   1.341 +
   1.342 +	case WM_DESTROY: {
   1.343 +		//VBALuaStop();
   1.344 +		DragAcceptFiles(hDlg, FALSE);
   1.345 +		if (hFont) {
   1.346 +			DeleteObject(hFont);
   1.347 +			hFont = NULL;
   1.348 +		}
   1.349 +		LuaConsoleHWnd = NULL;
   1.350 +	}	break;
   1.351 +
   1.352 +	case WM_DROPFILES: {
   1.353 +		HDROP hDrop;
   1.354 +		//UINT fileNo;
   1.355 +		UINT fileCount;
   1.356 +		char filename[_MAX_PATH];
   1.357 +
   1.358 +		hDrop = (HDROP)wParam;
   1.359 +		fileCount = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
   1.360 +		if (fileCount > 0) {
   1.361 +			DragQueryFile(hDrop, 0, filename, sizeof(filename));
   1.362 +			SetWindowText(GetDlgItem(hDlg, IDC_EDIT_LUAPATH), filename);
   1.363 +		}
   1.364 +		DragFinish(hDrop);
   1.365 +		return true;
   1.366 +	}	break;
   1.367 +
   1.368 +	}
   1.369 +
   1.370 +	return false;
   1.371 +
   1.372 +}