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