annotate src/win32/LuaOpenDialog.cpp @ 6:458a4f18f3cd

working on lua generation
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:48:33 -0600
parents f9f4f1b99eed
children
rev   line source
rlm@1 1 // LuaOpenDialog.cpp : implementation file
rlm@1 2 //
rlm@1 3
rlm@1 4 //#include "stdafx.h"
rlm@1 5 //#include "resource.h"
rlm@1 6 #include "LuaOpenDialog.h"
rlm@1 7 #include "MainWnd.h"
rlm@1 8 #include "WinResUtil.h"
rlm@1 9 #include "WinMiscUtil.h"
rlm@1 10 #include "VBA.h"
rlm@1 11 #include "Sound.h"
rlm@1 12
rlm@1 13 #include "../common/vbalua.h"
rlm@1 14
rlm@1 15 HWND LuaConsoleHWnd = NULL;
rlm@1 16 HFONT hFont = NULL;
rlm@1 17 LOGFONT LuaConsoleLogFont;
rlm@1 18
rlm@1 19 struct ControlLayoutInfo
rlm@1 20 {
rlm@1 21 int controlID;
rlm@1 22
rlm@1 23 enum LayoutType // what to do when the containing window resizes
rlm@1 24 {
rlm@1 25 NONE, // leave the control where it was
rlm@1 26 RESIZE_END, // resize the control
rlm@1 27 MOVE_START, // move the control
rlm@1 28 };
rlm@1 29 LayoutType horizontalLayout;
rlm@1 30 LayoutType verticalLayout;
rlm@1 31 };
rlm@1 32 struct ControlLayoutState
rlm@1 33 {
rlm@1 34 int x,y,width,height;
rlm@1 35 bool valid;
rlm@1 36 ControlLayoutState() : valid(false) {}
rlm@1 37 };
rlm@1 38
rlm@1 39 static ControlLayoutInfo controlLayoutInfos [] = {
rlm@1 40 {IDC_LUACONSOLE, ControlLayoutInfo::RESIZE_END, ControlLayoutInfo::RESIZE_END},
rlm@1 41 {IDC_EDIT_LUAPATH, ControlLayoutInfo::RESIZE_END, ControlLayoutInfo::NONE},
rlm@1 42 {IDC_BUTTON_LUARUN, ControlLayoutInfo::MOVE_START, ControlLayoutInfo::NONE},
rlm@1 43 {IDC_BUTTON_LUASTOP, ControlLayoutInfo::MOVE_START, ControlLayoutInfo::NONE},
rlm@1 44 };
rlm@1 45 static const int numControlLayoutInfos = sizeof(controlLayoutInfos)/sizeof(*controlLayoutInfos);
rlm@1 46
rlm@1 47 struct {
rlm@1 48 int width; int height;
rlm@1 49 ControlLayoutState layoutState [numControlLayoutInfos];
rlm@1 50 } windowInfo;
rlm@1 51
rlm@1 52 void PrintToWindowConsole(int hDlgAsInt, const char* str)
rlm@1 53 {
rlm@1 54 HWND hDlg = (HWND)hDlgAsInt;
rlm@1 55 HWND hConsole = GetDlgItem(hDlg, IDC_LUACONSOLE);
rlm@1 56
rlm@1 57 int length = GetWindowTextLength(hConsole);
rlm@1 58 if(length >= 250000)
rlm@1 59 {
rlm@1 60 // discard first half of text if it's getting too long
rlm@1 61 SendMessage(hConsole, EM_SETSEL, 0, length/2);
rlm@1 62 SendMessage(hConsole, EM_REPLACESEL, false, (LPARAM)"");
rlm@1 63 length = GetWindowTextLength(hConsole);
rlm@1 64 }
rlm@1 65 SendMessage(hConsole, EM_SETSEL, length, length);
rlm@1 66
rlm@1 67 //LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
rlm@1 68
rlm@1 69 {
rlm@1 70 SendMessage(hConsole, EM_REPLACESEL, false, (LPARAM)str);
rlm@1 71 }
rlm@1 72 }
rlm@1 73
rlm@1 74 void WinLuaOnStart(int hDlgAsInt)
rlm@1 75 {
rlm@1 76 HWND hDlg = (HWND)hDlgAsInt;
rlm@1 77 //LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
rlm@1 78 //info.started = true;
rlm@1 79 EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUABROWSE), false); // disable browse while running because it misbehaves if clicked in a frameadvance loop
rlm@1 80 EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUASTOP), true);
rlm@1 81 SetWindowText(GetDlgItem(hDlg, IDC_BUTTON_LUARUN), "Restart");
rlm@1 82 SetWindowText(GetDlgItem(hDlg, IDC_LUACONSOLE), ""); // clear the console
rlm@1 83 // Show_Genesis_Screen(HWnd); // otherwise we might never show the first thing the script draws
rlm@1 84 }
rlm@1 85
rlm@1 86 void WinLuaOnStop(int hDlgAsInt)
rlm@1 87 {
rlm@1 88 HWND hDlg = (HWND)hDlgAsInt;
rlm@1 89 //LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
rlm@1 90
rlm@1 91 HWND prevWindow = GetActiveWindow();
rlm@1 92 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 93 if(prevWindow == AfxGetMainWnd()->GetSafeHwnd()) SetActiveWindow(prevWindow);
rlm@1 94
rlm@1 95 //info.started = false;
rlm@1 96 EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUABROWSE), true);
rlm@1 97 EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUASTOP), false);
rlm@1 98 SetWindowText(GetDlgItem(hDlg, IDC_BUTTON_LUARUN), "Run");
rlm@1 99 // if(statusOK)
rlm@1 100 // Show_Genesis_Screen(MainWindow->getHWnd()); // otherwise we might never show the last thing the script draws
rlm@1 101 //if(info.closeOnStop)
rlm@1 102 // PostMessage(hDlg, WM_CLOSE, 0, 0);
rlm@1 103 }
rlm@1 104
rlm@1 105 INT_PTR CALLBACK DlgLuaScriptDialog(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
rlm@1 106 {
rlm@1 107 RECT r;
rlm@1 108 RECT r2;
rlm@1 109 int dx1, dy1, dx2, dy2;
rlm@1 110
rlm@1 111 switch (msg) {
rlm@1 112
rlm@1 113 case WM_INITDIALOG:
rlm@1 114 {
rlm@1 115 // remove the 30000 character limit from the console control
rlm@1 116 SendMessage(GetDlgItem(hDlg, IDC_LUACONSOLE),EM_LIMITTEXT,0,0);
rlm@1 117
rlm@1 118 GetWindowRect(AfxGetMainWnd()->GetSafeHwnd(), &r);
rlm@1 119 dx1 = (r.right - r.left) / 2;
rlm@1 120 dy1 = (r.bottom - r.top) / 2;
rlm@1 121
rlm@1 122 GetWindowRect(hDlg, &r2);
rlm@1 123 dx2 = (r2.right - r2.left) / 2;
rlm@1 124 dy2 = (r2.bottom - r2.top) / 2;
rlm@1 125
rlm@1 126 int windowIndex = 0;//std::find(LuaScriptHWnds.begin(), LuaScriptHWnds.end(), hDlg) - LuaScriptHWnds.begin();
rlm@1 127 int staggerOffset = windowIndex * 24;
rlm@1 128 r.left += staggerOffset;
rlm@1 129 r.right += staggerOffset;
rlm@1 130 r.top += staggerOffset;
rlm@1 131 r.bottom += staggerOffset;
rlm@1 132
rlm@1 133 // push it away from the main window if we can
rlm@1 134 const int width = (r.right-r.left);
rlm@1 135 const int width2 = (r2.right-r2.left);
rlm@1 136 if(r.left+width2 + width < GetSystemMetrics(SM_CXSCREEN))
rlm@1 137 {
rlm@1 138 r.right += width;
rlm@1 139 r.left += width;
rlm@1 140 }
rlm@1 141 else if((int)r.left - (int)width2 > 0)
rlm@1 142 {
rlm@1 143 r.right -= width2;
rlm@1 144 r.left -= width2;
rlm@1 145 }
rlm@1 146
rlm@1 147 SetWindowPos(hDlg, NULL, r.left, r.top, NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
rlm@1 148
rlm@1 149 RECT r3;
rlm@1 150 GetClientRect(hDlg, &r3);
rlm@1 151 windowInfo.width = r3.right - r3.left;
rlm@1 152 windowInfo.height = r3.bottom - r3.top;
rlm@1 153 for(int i = 0; i < numControlLayoutInfos; i++) {
rlm@1 154 ControlLayoutState& layoutState = windowInfo.layoutState[i];
rlm@1 155 layoutState.valid = false;
rlm@1 156 }
rlm@1 157
rlm@1 158 DragAcceptFiles(hDlg, true);
rlm@1 159 SetDlgItemText(hDlg, IDC_EDIT_LUAPATH, VBAGetLuaScriptName());
rlm@1 160
rlm@1 161 SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &LuaConsoleLogFont, 0); // reset with an acceptable font
rlm@1 162 return true;
rlm@1 163 } break;
rlm@1 164
rlm@1 165 case WM_SIZE:
rlm@1 166 {
rlm@1 167 // resize or move controls in the window as necessary when the window is resized
rlm@1 168
rlm@1 169 //LuaPerWindowInfo& windowInfo = LuaWindowInfo[hDlg];
rlm@1 170 int prevDlgWidth = windowInfo.width;
rlm@1 171 int prevDlgHeight = windowInfo.height;
rlm@1 172
rlm@1 173 int dlgWidth = LOWORD(lParam);
rlm@1 174 int dlgHeight = HIWORD(lParam);
rlm@1 175
rlm@1 176 int deltaWidth = dlgWidth - prevDlgWidth;
rlm@1 177 int deltaHeight = dlgHeight - prevDlgHeight;
rlm@1 178
rlm@1 179 for(int i = 0; i < numControlLayoutInfos; i++)
rlm@1 180 {
rlm@1 181 ControlLayoutInfo layoutInfo = controlLayoutInfos[i];
rlm@1 182 ControlLayoutState& layoutState = windowInfo.layoutState[i];
rlm@1 183
rlm@1 184 HWND hCtrl = GetDlgItem(hDlg,layoutInfo.controlID);
rlm@1 185
rlm@1 186 int x,y,width,height;
rlm@1 187 if(layoutState.valid)
rlm@1 188 {
rlm@1 189 x = layoutState.x;
rlm@1 190 y = layoutState.y;
rlm@1 191 width = layoutState.width;
rlm@1 192 height = layoutState.height;
rlm@1 193 }
rlm@1 194 else
rlm@1 195 {
rlm@1 196 RECT r;
rlm@1 197 GetWindowRect(hCtrl, &r);
rlm@1 198 POINT p = {r.left, r.top};
rlm@1 199 ScreenToClient(hDlg, &p);
rlm@1 200 x = p.x;
rlm@1 201 y = p.y;
rlm@1 202 width = r.right - r.left;
rlm@1 203 height = r.bottom - r.top;
rlm@1 204 }
rlm@1 205
rlm@1 206 switch(layoutInfo.horizontalLayout)
rlm@1 207 {
rlm@1 208 case ControlLayoutInfo::RESIZE_END: width += deltaWidth; break;
rlm@1 209 case ControlLayoutInfo::MOVE_START: x += deltaWidth; break;
rlm@1 210 default: break;
rlm@1 211 }
rlm@1 212 switch(layoutInfo.verticalLayout)
rlm@1 213 {
rlm@1 214 case ControlLayoutInfo::RESIZE_END: height += deltaHeight; break;
rlm@1 215 case ControlLayoutInfo::MOVE_START: y += deltaHeight; break;
rlm@1 216 default: break;
rlm@1 217 }
rlm@1 218
rlm@1 219 SetWindowPos(hCtrl, 0, x,y, width,height, 0);
rlm@1 220
rlm@1 221 layoutState.x = x;
rlm@1 222 layoutState.y = y;
rlm@1 223 layoutState.width = width;
rlm@1 224 layoutState.height = height;
rlm@1 225 layoutState.valid = true;
rlm@1 226 }
rlm@1 227
rlm@1 228 windowInfo.width = dlgWidth;
rlm@1 229 windowInfo.height = dlgHeight;
rlm@1 230
rlm@1 231 RedrawWindow(hDlg, NULL, NULL, RDW_INVALIDATE);
rlm@1 232 } break;
rlm@1 233
rlm@1 234 case WM_COMMAND:
rlm@1 235 switch (LOWORD(wParam)) {
rlm@1 236 case IDOK:
rlm@1 237 case IDCANCEL: {
rlm@1 238 EndDialog(hDlg, true); // goto case WM_CLOSE;
rlm@1 239 } break;
rlm@1 240
rlm@1 241 case IDC_BUTTON_LUARUN:
rlm@1 242 {
rlm@1 243 char filename[MAX_PATH];
rlm@1 244 GetDlgItemText(hDlg, IDC_EDIT_LUAPATH, filename, MAX_PATH);
rlm@1 245 VBALoadLuaCode(filename);
rlm@1 246 } break;
rlm@1 247
rlm@1 248 case IDC_BUTTON_LUASTOP:
rlm@1 249 {
rlm@1 250 VBALuaStop();
rlm@1 251 } break;
rlm@1 252
rlm@1 253 case IDC_BUTTON_LUAEDIT:
rlm@1 254 {
rlm@1 255 char Str_Tmp [1024];
rlm@1 256 SendDlgItemMessage(hDlg,IDC_EDIT_LUAPATH,WM_GETTEXT,(WPARAM)512,(LPARAM)Str_Tmp);
rlm@1 257 // tell the OS to open the file with its associated editor,
rlm@1 258 // without blocking on it or leaving a command window open.
rlm@1 259 if((int)ShellExecute(NULL, "edit", Str_Tmp, NULL, NULL, SW_SHOWNORMAL) == SE_ERR_NOASSOC)
rlm@1 260 if((int)ShellExecute(NULL, "open", Str_Tmp, NULL, NULL, SW_SHOWNORMAL) == SE_ERR_NOASSOC)
rlm@1 261 ShellExecute(NULL, NULL, "notepad", Str_Tmp, NULL, SW_SHOWNORMAL);
rlm@1 262 } break;
rlm@1 263
rlm@1 264 case IDC_BUTTON_LUABROWSE:
rlm@1 265 {
rlm@1 266 systemSoundClearBuffer();
rlm@1 267
rlm@1 268 CString filter = winResLoadFilter(IDS_FILTER_LUA);
rlm@1 269 CString title = winResLoadString(IDS_SELECT_LUA_NAME);
rlm@1 270
rlm@1 271 CString luaName = winGetDestFilename(theApp.gameFilename, IDS_LUA_DIR, ".lua");
rlm@1 272 CString luaDir = winGetDestDir(IDS_LUA_DIR);
rlm@1 273
rlm@1 274 filter.Replace('|', '\000');
rlm@1 275 // char *p = filter.GetBuffer(0);
rlm@1 276 // while ((p = strchr(p, '|')) != NULL)
rlm@1 277 // *p++ = 0;
rlm@1 278
rlm@1 279 OPENFILENAME ofn;
rlm@1 280 ZeroMemory( (LPVOID)&ofn, sizeof(OPENFILENAME) );
rlm@1 281 ofn.lpstrFile = luaName.GetBuffer(MAX_PATH);
rlm@1 282 ofn.nMaxFile = MAX_PATH;
rlm@1 283 ofn.lStructSize = sizeof(OPENFILENAME);
rlm@1 284 ofn.hwndOwner = hDlg;
rlm@1 285 ofn.lpstrFilter = filter;
rlm@1 286 ofn.nFilterIndex = 0;
rlm@1 287 ofn.lpstrInitialDir = luaDir;
rlm@1 288 ofn.lpstrTitle = title;
rlm@1 289 ofn.lpstrDefExt = "lua";
rlm@1 290 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 291 if(GetOpenFileName( &ofn ))
rlm@1 292 {
rlm@1 293 SetWindowText(GetDlgItem(hDlg, IDC_EDIT_LUAPATH), luaName);
rlm@1 294 }
rlm@1 295 return true;
rlm@1 296 } break;
rlm@1 297
rlm@1 298 case IDC_EDIT_LUAPATH:
rlm@1 299 {
rlm@1 300 char filename[MAX_PATH];
rlm@1 301 GetDlgItemText(hDlg, IDC_EDIT_LUAPATH, filename, MAX_PATH);
rlm@1 302 FILE* file = fopen(filename, "rb");
rlm@1 303 EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUAEDIT), file != NULL);
rlm@1 304 if(file)
rlm@1 305 fclose(file);
rlm@1 306 } break;
rlm@1 307
rlm@1 308 case IDC_LUACONSOLE_CHOOSEFONT:
rlm@1 309 {
rlm@1 310 CHOOSEFONT cf;
rlm@1 311
rlm@1 312 ZeroMemory(&cf, sizeof(cf));
rlm@1 313 cf.lStructSize = sizeof(CHOOSEFONT);
rlm@1 314 cf.hwndOwner = hDlg;
rlm@1 315 cf.lpLogFont = &LuaConsoleLogFont;
rlm@1 316 cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
rlm@1 317 if (ChooseFont(&cf)) {
rlm@1 318 if (hFont) {
rlm@1 319 DeleteObject(hFont);
rlm@1 320 hFont = NULL;
rlm@1 321 }
rlm@1 322 hFont = CreateFontIndirect(&LuaConsoleLogFont);
rlm@1 323 if (hFont)
rlm@1 324 SendDlgItemMessage(hDlg, IDC_LUACONSOLE, WM_SETFONT, (WPARAM)hFont, 0);
rlm@1 325 }
rlm@1 326 } break;
rlm@1 327
rlm@1 328 case IDC_LUACONSOLE_CLEAR:
rlm@1 329 {
rlm@1 330 SetWindowText(GetDlgItem(hDlg, IDC_LUACONSOLE), "");
rlm@1 331 } break;
rlm@1 332 }
rlm@1 333 break;
rlm@1 334
rlm@1 335 case WM_CLOSE: {
rlm@1 336 SendMessage(hDlg, WM_DESTROY, 0, 0);
rlm@1 337 } break;
rlm@1 338
rlm@1 339 case WM_DESTROY: {
rlm@1 340 //VBALuaStop();
rlm@1 341 DragAcceptFiles(hDlg, FALSE);
rlm@1 342 if (hFont) {
rlm@1 343 DeleteObject(hFont);
rlm@1 344 hFont = NULL;
rlm@1 345 }
rlm@1 346 LuaConsoleHWnd = NULL;
rlm@1 347 } break;
rlm@1 348
rlm@1 349 case WM_DROPFILES: {
rlm@1 350 HDROP hDrop;
rlm@1 351 //UINT fileNo;
rlm@1 352 UINT fileCount;
rlm@1 353 char filename[_MAX_PATH];
rlm@1 354
rlm@1 355 hDrop = (HDROP)wParam;
rlm@1 356 fileCount = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
rlm@1 357 if (fileCount > 0) {
rlm@1 358 DragQueryFile(hDrop, 0, filename, sizeof(filename));
rlm@1 359 SetWindowText(GetDlgItem(hDlg, IDC_EDIT_LUAPATH), filename);
rlm@1 360 }
rlm@1 361 DragFinish(hDrop);
rlm@1 362 return true;
rlm@1 363 } break;
rlm@1 364
rlm@1 365 }
rlm@1 366
rlm@1 367 return false;
rlm@1 368
rlm@1 369 }