Debian Codegraph @

editor/ScriptEditorDlg.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/editor/ScriptEditorDlg.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:38 $
23 * $Author: kevinb $
24 *
25 * The script editor dialog.
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 15 10/08/98 4:24p Kevin
30 * Changed code to comply with memory library usage. Always use mem_malloc
31 * , mem_free and mem_strdup
32 *
33 * 14 9/25/97 5:28p Samir
34 * Even newer script code due to more changes in ObjCScript.cpp
35 *
36 * 13 9/04/97 4:39p Matt
37 * Added includes needed as a result of removing includes from d3edit.h
38 *
39 * 12 9/02/97 3:28p Matt
40 * Got rid of warnings
41 *
42 * 11 8/29/97 2:21p Samir
43 * Add some rich edit functionality.
44 *
45 * $NoKeywords: $
46 */
47
48 // ScriptEditorDlg.cpp : implementation file
49 //
50
51 #include "mfc_compatibility.h"
52 #include "stdafx.h"
53 #include "editor.h"
54 #include "ddio.h"
55 #include "ScriptEditorDlg.h"
56 #include <process.h>
57 #include "CFILE.h"
58 #include "OsirisStatusDlg.h"
59 #include "pserror.h"
60 #include "mem.h"
61
62 #ifdef _DEBUG
63 #define new DEBUG_NEW
64 #undef THIS_FILE
65 static char THIS_FILE[] = __FILE__;
66 #endif
67
68 #define MAX_SCRIPT_LINE_SIZE 800
69 #define DEFAULT_SCRIPT_LOCATION "C:\\OSIRIS.TXT"
70
71 /////////////////////////////////////////////////////////////////////////////
72 // CScriptEditorDlg dialog
73
74 static UINT WM_FINDREPLACE = RegisterWindowMessage(FINDMSGSTRING);
75 char Default_external_editor[256];
76
77 CScriptEditorDlg::CScriptEditorDlg(CWnd *pParent /*=NULL*/) : CDialog(CScriptEditorDlg::IDD, pParent) {
78 //{{AFX_DATA_INIT(CScriptEditorDlg)
79 m_sScript = _T("");
80 //}}AFX_DATA_INIT
81 m_ExternalEditor = "None";
82 }
83
84 void CScriptEditorDlg::DoDataExchange(CDataExchange *pDX) {
85 /* DDX_Text(pDX, IDC_SCRIPTVIEW, m_sScript);*/
86
87 CDialog::DoDataExchange(pDX);
88 //{{AFX_DATA_MAP(CScriptEditorDlg)
89 //}}AFX_DATA_MAP
90 }
91
92 BEGIN_MESSAGE_MAP(CScriptEditorDlg, CDialog)
93 //{{AFX_MSG_MAP(CScriptEditorDlg)
94 ON_BN_CLICKED(IDC_SET_EXTERNAL_VIEWER, OnSetExternalViewer)
95 ON_BN_CLICKED(IDC_USE_EXTERNAL, OnUseExternal)
96 ON_BN_CLICKED(ID_SCRIPTEDITOR_EXPORT, OnScripteditorExport)
97 ON_BN_CLICKED(ID_SCRIPTEDITOR_IMPORT, OnScripteditorImport)
98 ON_BN_CLICKED(IDC_SCRIPT_COMPILE, OnScriptCompile)
99 ON_COMMAND(ID_OSIRIS_COMPILE_SCRIPT, OnOsirisCompileScript)
100 ON_COMMAND(ID_OSIRIS_COPY, OnOsirisCopy)
101 ON_COMMAND(ID_OSIRIS_CUT, OnOsirisCut)
102 ON_COMMAND(ID_OSIRIS_EXPORT_SCRIPT, OnOsirisExportScript)
103 ON_COMMAND(ID_OSIRIS_FIND, OnOsirisFind)
104 ON_COMMAND(ID_OSIRIS_FIND_AND_REPLACE, OnOsirisFindAndReplace)
105 ON_COMMAND(ID_OSIRIS_IMPORT_SCRIPT, OnOsirisImportScript)
106 ON_COMMAND(ID_OSIRIS_PASTE, OnOsirisPaste)
107 ON_COMMAND(ID_OSIRIS_SAVE_EXIT, OnOsirisSaveExit)
108 ON_COMMAND(ID_OSIRIS_SET_EXTERNAL, OnOsirisSetExternal)
109 ON_COMMAND(ID_OSIRIS_USE_EXTERNAL, OnOsirisUseExternal)
110 ON_UPDATE_COMMAND_UI(ID_OSIRIS_PASTE, OnUpdateOsirisPaste)
111 //}}AFX_MSG_MAP
112 ON_REGISTERED_MESSAGE(WM_FINDREPLACE, OnFindReplace)
113 END_MESSAGE_MAP()
114
115 /////////////////////////////////////////////////////////////////////////////
116 // CScriptEditorDlg message handlers
117
118 void CScriptEditorDlg::OnOK() {
119 // TODO: Add extra validation here
120 ddio_KeyFlush();
121 CDialog::OnOK();
122 }
123
124 void CScriptEditorDlg::OnCancel() {
125 // TODO: Add extra cleanup here
126 ddio_KeyFlush();
127 CDialog::OnCancel();
128 }
129
130 // This sets the script text data in the editor. Call this before the DoModal() call
131 void CScriptEditorDlg::SetScript(char *script, tD3XProgram *d3x) {
132 m_Program = d3x;
133 m_sScript = script;
134 }
135
136 // This sets the default external editor. Call this before the DoModal(). If this isn't called, then
137 // The external editor is set to "None"
138 void CScriptEditorDlg::SetExternalEditor(char *external) { m_ExternalEditor = external; }
139
140 // Returns the string of script data
141 char *CScriptEditorDlg::GetScript() { return m_sScript.GetBuffer(1); }
142
143 // Returns the length of the script data (includes end NULL character)
144 int CScriptEditorDlg::GetScriptLength() { return lstrlen(m_sScript.GetBuffer(1)) + 1; }
145
146 // This initializes a Find/Replace dialog box (if bFind==false it sets up a Find and Replace, else it's just Find)
147 void CScriptEditorDlg::InitFindReplace(BOOL bFind) {
148 //
149 // If the dialog has been called already, and then closed,
150 // the pointer is still non-NULL, but the object is already
151 // dead, so reset the pointer!
152 //
153 m_pdlgFindReplace = NULL;
154 m_LastWord = " ";
155
156 // Construct the dialog
157 m_pdlgFindReplace = new CFindReplaceDialog;
158 ASSERT(m_pdlgFindReplace != NULL);
159
160 // Initialize the dialog
161 if (!m_pdlgFindReplace->Create(bFind, 0, 0, FR_DOWN, this)) {
162 mprintf(0, "Error allocating find/replace dialog!");
163 m_pdlgFindReplace = NULL;
164 return;
165 }
166
167 // Display the dialog
168 ASSERT(m_pdlgFindReplace != NULL);
169 m_pdlgFindReplace->SetActiveWindow();
170 m_pdlgFindReplace->ShowWindow(SW_SHOW);
171 }
172
173 // This is the message handler of sorts for the Find/Replace dialog, it interprets the message and passes the control
174 // to the appropriate function
175 LONG CScriptEditorDlg::OnFindReplace(WPARAM wParam, LPARAM lParam) {
176 // Get a pointer to the calling dialog
177 CFindReplaceDialog *pDlg = CFindReplaceDialog::GetNotifier(lParam);
178 ASSERT(pDlg != NULL);
179
180 // See what the user is up to out there...
181 if (pDlg->IsTerminating()) {
182 // Time to kill the dialog box
183 return 0;
184 }
185
186 if (pDlg->ReplaceAll()) {
187 // Put a call to your ReplaceAll() method here...
188 CString find, repl;
189 find = pDlg->GetFindString();
190 repl = pDlg->GetReplaceString();
191 if (pDlg->MatchCase())
192 m_FindMatchCase = true;
193 else
194 m_FindMatchCase = false;
195 ReplaceAll(find.GetBuffer(1), repl.GetBuffer(1));
196 return 0;
197 }
198
199 if (pDlg->ReplaceCurrent()) {
200 // Put a call to your ReplaceCurrent() method here...
201 CString repl;
202 repl = pDlg->GetReplaceString();
203 if (pDlg->MatchCase())
204 m_FindMatchCase = true;
205 else
206 m_FindMatchCase = false;
207 ReplaceCurrent(repl.GetBuffer(1));
208 return 0;
209 }
210
211 CString str;
212 if (pDlg->MatchCase())
213 m_FindMatchCase = true;
214 else
215 m_FindMatchCase = false;
216 str = pDlg->GetFindString();
217
218 FindNext(str.GetBuffer(1));
219
220 return 0;
221 }
222
223 // This will look for the next series of charaters (given by the null terminated w)
224 // If it finds the string, it will highlight/select it and move the caret to the beginning of the word
225 // returns true if it found the string
226 // else false
227 bool CScriptEditorDlg::FindNext(char *w) {
228 int index, max;
229 bool word_found;
230 bool new_word;
231 static int start = 0;
232
233 word_found = false;
234
235 // See if this is a new word to look for (as compared to the last searched word)
236 if (m_LastWord != w) {
237 m_LastWord = w;
238 start = 0;
239 new_word = true;
240 } else
241 new_word = false;
242
243 // This will update the m_sScript string so it has the current script in it
244 UpdateData();
245
246 // Make a copy of the script to work with
247 char *text;
248 text = (char *)mem_malloc(GetScriptLength());
249 strcpy(text, GetScript());
250 max = GetScriptLength();
251
252 // Make sure we aren't gonna try to go past the end of the buffer, start at 0 if we are
253 if (start + (signed)strlen(w) >= max - 1)
254 start = 0;
255
256 // Here we begin the search
257 for (index = start; index < max; index++) {
258 // check the character it's at in the buffer to the first character of the find string, if they are the same
259 // it's a possibility, so Check the word
260 if (toupper(text[index]) == toupper(w[0]))
261 if (CheckWord(index, text, w)) {
262 // We got a match, so move the caret, and select the word
263 SetCurrentIndex(index);
264 SetSelection(index, strlen(w));
265 word_found = true;
266 // adjust start so on the next FindNext() it will start from the next character
267 start = index + 1;
268 index = max;
269 }
270 }
271
272 if (text)
273 mem_free(text);
274
275 if (!word_found) {
276 // This can mean 2 things. We searched to the end of the file and either never found a word, or we did at one time
277
278 // either way, start back at the beginning next time
279 start = 0;
280
281 // if it was a new word then we never found it
282 if (new_word) {
283 OutrageMessageBox("Word Not Found!");
284 m_LastWord = " ";
285 } else {
286 OutrageMessageBox("Searched to end of file");
287 }
288 return false;
289 }
290
291 return true;
292 }
293
294 // This selects a word/string in the edit box, given the starting zero based character index and the length of the
295 // string
296 void CScriptEditorDlg::SetSelection(int index, int length) {
297 CEdit *edit;
298 edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
299 edit->SetActiveWindow();
300 edit->SetFocus();
301 edit->ShowCaret();
302 edit->SetSel(-1, 0, false);
303 edit->SetSel(index, index + length, false);
304 }
305
306 // This function clears all selections
307 void CScriptEditorDlg::RemoveAllSelections() {
308 CEdit *edit;
309 edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
310 edit->SetFocus();
311 edit->ShowCaret();
312 edit->SetSel(-1, 0, false);
313 }
314
315 // Replaces the selected word with the passed word
316 void CScriptEditorDlg::ReplaceSelected(char *replace_word) {
317 UpdateData(false);
318
319 CEdit *edit;
320 int start_index, end_index;
321
322 edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
323 edit->SetFocus();
324 edit->GetSel(start_index, end_index);
325 edit->ReplaceSel(replace_word);
326
327 UpdateData();
328
329 SetCurrentIndex(start_index);
330 }
331
332 // This will start from the beginning and replace all the occurences
333 void CScriptEditorDlg::ReplaceAll(char *find_word, char *replace_word) {
334 SetCurrentIndex(0);
335 m_LastWord = " ";
336 while (FindNext(find_word)) {
337 ReplaceSelected(replace_word);
338 }
339 }
340
341 // This only replaces the selected string with the replace_word
342 void CScriptEditorDlg::ReplaceCurrent(char *replace_word) { ReplaceSelected(replace_word); }
343
344 // This function moves the caret to the position specified in index
345 void CScriptEditorDlg::SetCurrentIndex(int index) {
346 CEdit *edit;
347 CPoint point;
348 edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
349 edit->SetFocus();
350 edit->ShowCaret();
351 point = edit->PosFromChar(index);
352 edit->SetCaretPos(point);
353 }
354
355 // This function checks to see if the string w is the same as the string that begins in text[index]
356 // returns true if it is
357 bool CScriptEditorDlg::CheckWord(int index, char *text, char *w) {
358 int word_length = strlen(w);
359 int buffer_size = strlen(text);
360 int count;
361 if (m_FindMatchCase) {
362 for (count = 0; count < word_length; count++) {
363 if (index + count >= buffer_size)
364 return false;
365 if (text[index + count] != w[count])
366 return false;
367 }
368 } else {
369 for (count = 0; count < word_length; count++) {
370 if (index + count >= buffer_size)
371 return false;
372 if (toupper(text[index + count]) != toupper(w[count]))
373 return false;
374 }
375 }
376
377 return true;
378 }
379
380 // Sets the path to the external editor/viewer
381 void CScriptEditorDlg::OnSetExternalViewer() {
382 // TODO: Add your control notification handler code here
383 CString filter = "Executables (*.exe)|*.exe|All Files (*.*)|*.*||";
384
385 OutrageMessageBox("WARNING: DO NOT USE WORDPAD OR WRITE!");
386
387 CFileDialog dlg(true, 0, 0, OFN_FILEMUSTEXIST, (LPCTSTR)filter, this);
388 if (dlg.DoModal() == IDOK) {
389 m_ExternalEditor = dlg.GetPathName();
390 AdjustExternalName();
391 }
392 }
393
394 // Called when we want to differ to the external editor/viewer
395 void CScriptEditorDlg::OnUseExternal() {
396 // TODO: Add your control notification handler code here
397 int flag;
398 STARTUPINFO si;
399 PROCESS_INFORMATION pi;
400 CString external;
401 bool done = false;
402
403 UpdateData(true);
404 ExportScript(DEFAULT_SCRIPT_LOCATION);
405
406 external = m_ExternalEditor;
407 external += " ";
408 external += DEFAULT_SCRIPT_LOCATION;
409
410 memset(&si, 0, sizeof(si));
411 si.cb = sizeof(si);
412 si.dwFlags = STARTF_USESHOWWINDOW;
413 si.wShowWindow = SW_SHOW;
414
415 OutrageMessageBox(
416 "You are about to enter an external editor. Please remember to save your work back as a text file");
417 flag = CreateProcess(NULL, external.GetBuffer(1), NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
418 if (!flag) {
419 OutrageMessageBox("Unable to start external editor");
420 return;
421 }
422
423 mprintf(0, "External Editor started\n");
424 WaitForSingleObject(pi.hProcess, INFINITE);
425 TerminateProcess(pi.hProcess, 0);
426 Sleep(100);
427 CloseHandle(pi.hThread);
428 CloseHandle(pi.hProcess);
429 mprintf(0, "External Editor finished\n");
430
431 ImportScript(DEFAULT_SCRIPT_LOCATION);
432 UpdateData(false);
433 }
434
435 BOOL CScriptEditorDlg::OnInitDialog() {
436 CDialog::OnInitDialog();
437 // RECT rect;
438 // BOOL res;
439
440 // TODO: Add extra initialization here
441 // CWnd *wnd = GetDlgItem(IDC_SCRMOD_BOX);
442 // wnd->GetWindowRect(&rect);
443 // ScreenToClient(&rect);
444 // res = m_RichEdit.Create(WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_WANTRETURN, rect, this, IDC_SCRIPTVIEW);
445
446 // CEdit *edit;
447 // edit=(CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
448 // edit->SetTabStops(5);
449
450 AdjustExternalName();
451 return TRUE; // return TRUE unless you set the focus to a control
452 // EXCEPTION: OCX Property Pages should return FALSE
453 }
454
455 // This takes m_ExternalEditor string and rips out the name of the external editor and displays it
456 void CScriptEditorDlg::AdjustExternalName() {
457 char temp[20];
458 char temp_name[256];
459 int i, start = 0;
460
461 strcpy(temp_name, m_ExternalEditor.GetBuffer(1));
462 for (i = strlen(temp_name); i >= 0; i--) {
463 if (temp_name[i] == '.') {
464 temp_name[i] = '\0';
465 i = -1;
466 }
467 }
468
469 for (i = strlen(temp_name); i >= 0; i--) {
470 if (temp_name[i] == '\\') {
471 start = i + 1;
472 i = -1;
473 }
474 }
475
476 for (i = 0; i < 20; i++) {
477 temp[i] = temp_name[i + start];
478 }
479
480 CWnd *text;
481 text = GetDlgItem(IDC_SCRIPTEXTERNAL);
482 text->SetWindowText(temp);
483 }
484
485 void CScriptEditorDlg::ExportScript(char *filename) {
486 CFILE *file;
487
488 file = cfopen(filename, "wt");
489 mprintf(0, "Exporting Script to file %s\n", filename);
490 cf_WriteString(file, m_sScript.GetBuffer(1));
491 cfclose(file);
492 }
493
494 void CScriptEditorDlg::ImportScript(char *filename) {
495 CFILE *file;
496 CString temp;
497 int size;
498 char buffer[MAX_SCRIPT_LINE_SIZE];
499
500 memset(buffer, 0, MAX_SCRIPT_LINE_SIZE);
501
502 if (!cfexist(filename)) {
503 OutrageMessageBox("File not found!");
504 return;
505 }
506
507 file = cfopen(filename, "rt");
508 mprintf(0, "Importing Script from file %s\n", filename);
509 do {
510 size = cf_ReadString(buffer, MAX_SCRIPT_LINE_SIZE - 2, file);
511 buffer[size] = '\xd';
512 buffer[size + 1] = '\xa';
513 buffer[size + 2] = '\0';
514 temp += buffer;
515 memset(buffer, 0, size);
516 } while (!cfeof(file));
517
518 m_sScript = temp;
519 cfclose(file);
520 }
521
522 void CScriptEditorDlg::OnScripteditorExport() {
523 // TODO: Add your control notification handler code here
524 }
525
526 void CScriptEditorDlg::OnScripteditorImport() {
527 // TODO: Add your control notification handler code here
528 CString path;
529 CString filter = "All Files (*.*)|*.*||";
530
531 CFileDialog dlg(true, 0, 0, OFN_FILEMUSTEXIST, (LPCTSTR)filter, this);
532 if (dlg.DoModal() == IDOK) {
533 path = dlg.GetPathName();
534 ImportScript(path.GetBuffer(1));
535 }
536
537 UpdateData(false);
538 }
539
540 void CScriptEditorDlg::OnScriptCompile() {
541 // TODO: Add your control notification handler code here
542 COsirisStatusDlg progress_status;
543
544 UpdateData(true);
545
546 progress_status.SetScript(m_sScript.GetBuffer(1), m_Program);
547
548 progress_status.DoModal();
549 }
550
551 void CScriptEditorDlg::OnOsirisCompileScript() {
552 // TODO: Add your command handler code here
553 OnScriptCompile();
554 }
555
556 void CScriptEditorDlg::OnOsirisCopy() {
557 // TODO: Add your command handler code here
558 CEdit *edit;
559 edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
560 edit->Copy();
561 }
562
563 void CScriptEditorDlg::OnOsirisCut() {
564 // TODO: Add your command handler code here
565 CEdit *edit;
566 edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
567 edit->Cut();
568 }
569
570 void CScriptEditorDlg::OnOsirisExportScript() {
571 // TODO: Add your command handler code here
572 OnScripteditorExport();
573 }
574
575 void CScriptEditorDlg::OnOsirisFind() {
576 // TODO: Add your command handler code here
577 InitFindReplace(true);
578 }
579
580 void CScriptEditorDlg::OnOsirisFindAndReplace() {
581 // TODO: Add your command handler code here
582 InitFindReplace(false);
583 }
584
585 void CScriptEditorDlg::OnOsirisImportScript() {
586 // TODO: Add your command handler code here
587 OnScripteditorImport();
588 }
589
590 void CScriptEditorDlg::OnOsirisPaste() {
591 // TODO: Add your command handler code here
592 CEdit *edit;
593 edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
594 edit->Paste();
595 }
596
597 void CScriptEditorDlg::OnOsirisSaveExit() {
598 // TODO: Add your command handler code here
599 OnOK();
600 }
601
602 void CScriptEditorDlg::OnOsirisSetExternal() {
603 // TODO: Add your command handler code here
604 OnSetExternalViewer();
605 }
606
607 void CScriptEditorDlg::OnOsirisUseExternal() {
608 // TODO: Add your command handler code here
609 OnUseExternal();
610 }
611
612 void CScriptEditorDlg::OnUpdateOsirisPaste(CCmdUI *pCmdUI) {
613 // TODO: Add your command update UI handler code here
614
615 // this doesn't work right...don't know why
616 if (IsClipboardFormatAvailable(CF_TEXT))
617 pCmdUI->Enable(true);
618 else
619 pCmdUI->Enable(false);
620 }