File: ScriptEditorDlg.cpp

package info (click to toggle)
descent3 1.5.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 35,256 kB
  • sloc: cpp: 416,147; ansic: 3,233; sh: 10; makefile: 8
file content (620 lines) | stat: -rw-r--r-- 17,527 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*
* Descent 3
* Copyright (C) 2024 Parallax Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.

--- HISTORICAL COMMENTS FOLLOW ---

 * $Logfile: /DescentIII/Main/editor/ScriptEditorDlg.cpp $
 * $Revision: 1.1.1.1 $
 * $Date: 2003-08-26 03:57:38 $
 * $Author: kevinb $
 *
 *	The script editor dialog.
 *
 * $Log: not supported by cvs2svn $
 *
 * 15    10/08/98 4:24p Kevin
 * Changed code to comply with memory library usage. Always use mem_malloc
 * , mem_free and mem_strdup
 *
 * 14    9/25/97 5:28p Samir
 * Even newer script code due to more changes in ObjCScript.cpp
 *
 * 13    9/04/97 4:39p Matt
 * Added includes needed as a result of removing includes from d3edit.h
 *
 * 12    9/02/97 3:28p Matt
 * Got rid of warnings
 *
 * 11    8/29/97 2:21p Samir
 * Add some rich edit functionality.
 *
 * $NoKeywords: $
 */

// ScriptEditorDlg.cpp : implementation file
//

#include "mfc_compatibility.h"
#include "stdafx.h"
#include "editor.h"
#include "ddio.h"
#include "ScriptEditorDlg.h"
#include <process.h>
#include "CFILE.h"
#include "OsirisStatusDlg.h"
#include "pserror.h"
#include "mem.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define MAX_SCRIPT_LINE_SIZE 800
#define DEFAULT_SCRIPT_LOCATION "C:\\OSIRIS.TXT"

/////////////////////////////////////////////////////////////////////////////
// CScriptEditorDlg dialog

static UINT WM_FINDREPLACE = RegisterWindowMessage(FINDMSGSTRING);
char Default_external_editor[256];

CScriptEditorDlg::CScriptEditorDlg(CWnd *pParent /*=NULL*/) : CDialog(CScriptEditorDlg::IDD, pParent) {
  //{{AFX_DATA_INIT(CScriptEditorDlg)
  m_sScript = _T("");
  //}}AFX_DATA_INIT
  m_ExternalEditor = "None";
}

void CScriptEditorDlg::DoDataExchange(CDataExchange *pDX) {
  /* DDX_Text(pDX, IDC_SCRIPTVIEW, m_sScript);*/

  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CScriptEditorDlg)
  //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CScriptEditorDlg, CDialog)
//{{AFX_MSG_MAP(CScriptEditorDlg)
ON_BN_CLICKED(IDC_SET_EXTERNAL_VIEWER, OnSetExternalViewer)
ON_BN_CLICKED(IDC_USE_EXTERNAL, OnUseExternal)
ON_BN_CLICKED(ID_SCRIPTEDITOR_EXPORT, OnScripteditorExport)
ON_BN_CLICKED(ID_SCRIPTEDITOR_IMPORT, OnScripteditorImport)
ON_BN_CLICKED(IDC_SCRIPT_COMPILE, OnScriptCompile)
ON_COMMAND(ID_OSIRIS_COMPILE_SCRIPT, OnOsirisCompileScript)
ON_COMMAND(ID_OSIRIS_COPY, OnOsirisCopy)
ON_COMMAND(ID_OSIRIS_CUT, OnOsirisCut)
ON_COMMAND(ID_OSIRIS_EXPORT_SCRIPT, OnOsirisExportScript)
ON_COMMAND(ID_OSIRIS_FIND, OnOsirisFind)
ON_COMMAND(ID_OSIRIS_FIND_AND_REPLACE, OnOsirisFindAndReplace)
ON_COMMAND(ID_OSIRIS_IMPORT_SCRIPT, OnOsirisImportScript)
ON_COMMAND(ID_OSIRIS_PASTE, OnOsirisPaste)
ON_COMMAND(ID_OSIRIS_SAVE_EXIT, OnOsirisSaveExit)
ON_COMMAND(ID_OSIRIS_SET_EXTERNAL, OnOsirisSetExternal)
ON_COMMAND(ID_OSIRIS_USE_EXTERNAL, OnOsirisUseExternal)
ON_UPDATE_COMMAND_UI(ID_OSIRIS_PASTE, OnUpdateOsirisPaste)
//}}AFX_MSG_MAP
ON_REGISTERED_MESSAGE(WM_FINDREPLACE, OnFindReplace)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScriptEditorDlg message handlers

void CScriptEditorDlg::OnOK() {
  // TODO: Add extra validation here
  ddio_KeyFlush();
  CDialog::OnOK();
}

void CScriptEditorDlg::OnCancel() {
  // TODO: Add extra cleanup here
  ddio_KeyFlush();
  CDialog::OnCancel();
}

// This sets the script text data in the editor.  Call this before the DoModal() call
void CScriptEditorDlg::SetScript(char *script, tD3XProgram *d3x) {
  m_Program = d3x;
  m_sScript = script;
}

// This sets the default external editor.  Call this before the DoModal().  If this isn't called, then
// The external editor is set to "None"
void CScriptEditorDlg::SetExternalEditor(char *external) { m_ExternalEditor = external; }

// Returns the string of script data
char *CScriptEditorDlg::GetScript() { return m_sScript.GetBuffer(1); }

// Returns the length of the script data (includes end NULL character)
int CScriptEditorDlg::GetScriptLength() { return lstrlen(m_sScript.GetBuffer(1)) + 1; }

// This initializes a Find/Replace dialog box (if bFind==false it sets up a Find and Replace, else it's just Find)
void CScriptEditorDlg::InitFindReplace(BOOL bFind) {
  //
  // If the dialog has been called already, and then closed,
  // the pointer is still non-NULL, but the object is already
  // dead, so reset the pointer!
  //
  m_pdlgFindReplace = NULL;
  m_LastWord = " ";

  // Construct the dialog
  m_pdlgFindReplace = new CFindReplaceDialog;
  ASSERT(m_pdlgFindReplace != NULL);

  // Initialize the dialog
  if (!m_pdlgFindReplace->Create(bFind, 0, 0, FR_DOWN, this)) {
    mprintf(0, "Error allocating find/replace dialog!");
    m_pdlgFindReplace = NULL;
    return;
  }

  // Display the dialog
  ASSERT(m_pdlgFindReplace != NULL);
  m_pdlgFindReplace->SetActiveWindow();
  m_pdlgFindReplace->ShowWindow(SW_SHOW);
}

// This is the message handler of sorts for the Find/Replace dialog, it interprets the message and passes the control
// to the appropriate function
LONG CScriptEditorDlg::OnFindReplace(WPARAM wParam, LPARAM lParam) {
  // Get a pointer to the calling dialog
  CFindReplaceDialog *pDlg = CFindReplaceDialog::GetNotifier(lParam);
  ASSERT(pDlg != NULL);

  // See what the user is up to out there...
  if (pDlg->IsTerminating()) {
    // Time to kill the dialog box
    return 0;
  }

  if (pDlg->ReplaceAll()) {
    // Put a call to your ReplaceAll() method here...
    CString find, repl;
    find = pDlg->GetFindString();
    repl = pDlg->GetReplaceString();
    if (pDlg->MatchCase())
      m_FindMatchCase = true;
    else
      m_FindMatchCase = false;
    ReplaceAll(find.GetBuffer(1), repl.GetBuffer(1));
    return 0;
  }

  if (pDlg->ReplaceCurrent()) {
    // Put a call to your ReplaceCurrent() method here...
    CString repl;
    repl = pDlg->GetReplaceString();
    if (pDlg->MatchCase())
      m_FindMatchCase = true;
    else
      m_FindMatchCase = false;
    ReplaceCurrent(repl.GetBuffer(1));
    return 0;
  }

  CString str;
  if (pDlg->MatchCase())
    m_FindMatchCase = true;
  else
    m_FindMatchCase = false;
  str = pDlg->GetFindString();

  FindNext(str.GetBuffer(1));

  return 0;
}

// This will look for the next series of charaters (given by the null terminated w)
// If it finds the string, it will highlight/select it and move the caret to the beginning of the word
// returns true if it found the string
// else false
bool CScriptEditorDlg::FindNext(char *w) {
  int index, max;
  bool word_found;
  bool new_word;
  static int start = 0;

  word_found = false;

  // See if this is a new word to look for (as compared to the last searched word)
  if (m_LastWord != w) {
    m_LastWord = w;
    start = 0;
    new_word = true;
  } else
    new_word = false;

  // This will update the m_sScript string so it has the current script in it
  UpdateData();

  // Make a copy of the script to work with
  char *text;
  text = (char *)mem_malloc(GetScriptLength());
  strcpy(text, GetScript());
  max = GetScriptLength();

  // Make sure we aren't gonna try to go past the end of the buffer, start at 0 if we are
  if (start + (signed)strlen(w) >= max - 1)
    start = 0;

  // Here we begin the search
  for (index = start; index < max; index++) {
    // check the character it's at in the buffer to the first character of the find string, if they are the same
    // it's a possibility, so Check the word
    if (toupper(text[index]) == toupper(w[0]))
      if (CheckWord(index, text, w)) {
        // We got a match, so move the caret, and select the word
        SetCurrentIndex(index);
        SetSelection(index, strlen(w));
        word_found = true;
        // adjust start so on the next FindNext() it will start from the next character
        start = index + 1;
        index = max;
      }
  }

  if (text)
    mem_free(text);

  if (!word_found) {
    // 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

    // either way, start back at the beginning next time
    start = 0;

    // if it was a new word then we never found it
    if (new_word) {
      OutrageMessageBox("Word Not Found!");
      m_LastWord = " ";
    } else {
      OutrageMessageBox("Searched to end of file");
    }
    return false;
  }

  return true;
}

// This selects a word/string in the edit box, given the starting zero based character index and the length of the
// string
void CScriptEditorDlg::SetSelection(int index, int length) {
  CEdit *edit;
  edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
  edit->SetActiveWindow();
  edit->SetFocus();
  edit->ShowCaret();
  edit->SetSel(-1, 0, false);
  edit->SetSel(index, index + length, false);
}

// This function clears all selections
void CScriptEditorDlg::RemoveAllSelections() {
  CEdit *edit;
  edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
  edit->SetFocus();
  edit->ShowCaret();
  edit->SetSel(-1, 0, false);
}

// Replaces the selected word with the passed word
void CScriptEditorDlg::ReplaceSelected(char *replace_word) {
  UpdateData(false);

  CEdit *edit;
  int start_index, end_index;

  edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
  edit->SetFocus();
  edit->GetSel(start_index, end_index);
  edit->ReplaceSel(replace_word);

  UpdateData();

  SetCurrentIndex(start_index);
}

// This will start from the beginning and replace all the occurences
void CScriptEditorDlg::ReplaceAll(char *find_word, char *replace_word) {
  SetCurrentIndex(0);
  m_LastWord = " ";
  while (FindNext(find_word)) {
    ReplaceSelected(replace_word);
  }
}

// This only replaces the selected string with the replace_word
void CScriptEditorDlg::ReplaceCurrent(char *replace_word) { ReplaceSelected(replace_word); }

// This function moves the caret to the position specified in index
void CScriptEditorDlg::SetCurrentIndex(int index) {
  CEdit *edit;
  CPoint point;
  edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
  edit->SetFocus();
  edit->ShowCaret();
  point = edit->PosFromChar(index);
  edit->SetCaretPos(point);
}

// This function checks to see if the string w is the same as the string that begins in text[index]
// returns true if it is
bool CScriptEditorDlg::CheckWord(int index, char *text, char *w) {
  int word_length = strlen(w);
  int buffer_size = strlen(text);
  int count;
  if (m_FindMatchCase) {
    for (count = 0; count < word_length; count++) {
      if (index + count >= buffer_size)
        return false;
      if (text[index + count] != w[count])
        return false;
    }
  } else {
    for (count = 0; count < word_length; count++) {
      if (index + count >= buffer_size)
        return false;
      if (toupper(text[index + count]) != toupper(w[count]))
        return false;
    }
  }

  return true;
}

// Sets the path to the external editor/viewer
void CScriptEditorDlg::OnSetExternalViewer() {
  // TODO: Add your control notification handler code here
  CString filter = "Executables (*.exe)|*.exe|All Files (*.*)|*.*||";

  OutrageMessageBox("WARNING: DO NOT USE WORDPAD OR WRITE!");

  CFileDialog dlg(true, 0, 0, OFN_FILEMUSTEXIST, (LPCTSTR)filter, this);
  if (dlg.DoModal() == IDOK) {
    m_ExternalEditor = dlg.GetPathName();
    AdjustExternalName();
  }
}

// Called when we want to differ to the external editor/viewer
void CScriptEditorDlg::OnUseExternal() {
  // TODO: Add your control notification handler code here
  int flag;
  STARTUPINFO si;
  PROCESS_INFORMATION pi;
  CString external;
  bool done = false;

  UpdateData(true);
  ExportScript(DEFAULT_SCRIPT_LOCATION);

  external = m_ExternalEditor;
  external += " ";
  external += DEFAULT_SCRIPT_LOCATION;

  memset(&si, 0, sizeof(si));
  si.cb = sizeof(si);
  si.dwFlags = STARTF_USESHOWWINDOW;
  si.wShowWindow = SW_SHOW;

  OutrageMessageBox(
      "You are about to enter an external editor.  Please remember to save your work back as a text file");
  flag = CreateProcess(NULL, external.GetBuffer(1), NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
  if (!flag) {
    OutrageMessageBox("Unable to start external editor");
    return;
  }

  mprintf(0, "External Editor started\n");
  WaitForSingleObject(pi.hProcess, INFINITE);
  TerminateProcess(pi.hProcess, 0);
  Sleep(100);
  CloseHandle(pi.hThread);
  CloseHandle(pi.hProcess);
  mprintf(0, "External Editor finished\n");

  ImportScript(DEFAULT_SCRIPT_LOCATION);
  UpdateData(false);
}

BOOL CScriptEditorDlg::OnInitDialog() {
  CDialog::OnInitDialog();
  //	RECT rect;
  //	BOOL res;

  // TODO: Add extra initialization here
  //	CWnd *wnd = GetDlgItem(IDC_SCRMOD_BOX);
  //	wnd->GetWindowRect(&rect);
  //	ScreenToClient(&rect);
  //	res = m_RichEdit.Create(WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_WANTRETURN, rect, this, IDC_SCRIPTVIEW);

  //	CEdit *edit;
  //	edit=(CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
  //	edit->SetTabStops(5);

  AdjustExternalName();
  return TRUE; // return TRUE unless you set the focus to a control
               // EXCEPTION: OCX Property Pages should return FALSE
}

// This takes m_ExternalEditor string and rips out the name of the external editor and displays it
void CScriptEditorDlg::AdjustExternalName() {
  char temp[20];
  char temp_name[256];
  int i, start = 0;

  strcpy(temp_name, m_ExternalEditor.GetBuffer(1));
  for (i = strlen(temp_name); i >= 0; i--) {
    if (temp_name[i] == '.') {
      temp_name[i] = '\0';
      i = -1;
    }
  }

  for (i = strlen(temp_name); i >= 0; i--) {
    if (temp_name[i] == '\\') {
      start = i + 1;
      i = -1;
    }
  }

  for (i = 0; i < 20; i++) {
    temp[i] = temp_name[i + start];
  }

  CWnd *text;
  text = GetDlgItem(IDC_SCRIPTEXTERNAL);
  text->SetWindowText(temp);
}

void CScriptEditorDlg::ExportScript(char *filename) {
  CFILE *file;

  file = cfopen(filename, "wt");
  mprintf(0, "Exporting Script to file %s\n", filename);
  cf_WriteString(file, m_sScript.GetBuffer(1));
  cfclose(file);
}

void CScriptEditorDlg::ImportScript(char *filename) {
  CFILE *file;
  CString temp;
  int size;
  char buffer[MAX_SCRIPT_LINE_SIZE];

  memset(buffer, 0, MAX_SCRIPT_LINE_SIZE);

  if (!cfexist(filename)) {
    OutrageMessageBox("File not found!");
    return;
  }

  file = cfopen(filename, "rt");
  mprintf(0, "Importing Script from file %s\n", filename);
  do {
    size = cf_ReadString(buffer, MAX_SCRIPT_LINE_SIZE - 2, file);
    buffer[size] = '\xd';
    buffer[size + 1] = '\xa';
    buffer[size + 2] = '\0';
    temp += buffer;
    memset(buffer, 0, size);
  } while (!cfeof(file));

  m_sScript = temp;
  cfclose(file);
}

void CScriptEditorDlg::OnScripteditorExport() {
  // TODO: Add your control notification handler code here
}

void CScriptEditorDlg::OnScripteditorImport() {
  // TODO: Add your control notification handler code here
  CString path;
  CString filter = "All Files (*.*)|*.*||";

  CFileDialog dlg(true, 0, 0, OFN_FILEMUSTEXIST, (LPCTSTR)filter, this);
  if (dlg.DoModal() == IDOK) {
    path = dlg.GetPathName();
    ImportScript(path.GetBuffer(1));
  }

  UpdateData(false);
}

void CScriptEditorDlg::OnScriptCompile() {
  // TODO: Add your control notification handler code here
  COsirisStatusDlg progress_status;

  UpdateData(true);

  progress_status.SetScript(m_sScript.GetBuffer(1), m_Program);

  progress_status.DoModal();
}

void CScriptEditorDlg::OnOsirisCompileScript() {
  // TODO: Add your command handler code here
  OnScriptCompile();
}

void CScriptEditorDlg::OnOsirisCopy() {
  // TODO: Add your command handler code here
  CEdit *edit;
  edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
  edit->Copy();
}

void CScriptEditorDlg::OnOsirisCut() {
  // TODO: Add your command handler code here
  CEdit *edit;
  edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
  edit->Cut();
}

void CScriptEditorDlg::OnOsirisExportScript() {
  // TODO: Add your command handler code here
  OnScripteditorExport();
}

void CScriptEditorDlg::OnOsirisFind() {
  // TODO: Add your command handler code here
  InitFindReplace(true);
}

void CScriptEditorDlg::OnOsirisFindAndReplace() {
  // TODO: Add your command handler code here
  InitFindReplace(false);
}

void CScriptEditorDlg::OnOsirisImportScript() {
  // TODO: Add your command handler code here
  OnScripteditorImport();
}

void CScriptEditorDlg::OnOsirisPaste() {
  // TODO: Add your command handler code here
  CEdit *edit;
  edit = (CEdit *)GetDlgItem(IDC_SCRIPTVIEW);
  edit->Paste();
}

void CScriptEditorDlg::OnOsirisSaveExit() {
  // TODO: Add your command handler code here
  OnOK();
}

void CScriptEditorDlg::OnOsirisSetExternal() {
  // TODO: Add your command handler code here
  OnSetExternalViewer();
}

void CScriptEditorDlg::OnOsirisUseExternal() {
  // TODO: Add your command handler code here
  OnUseExternal();
}

void CScriptEditorDlg::OnUpdateOsirisPaste(CCmdUI *pCmdUI) {
  // TODO: Add your command update UI handler code here

  // this doesn't work right...don't know why
  if (IsClipboardFormatAvailable(CF_TEXT))
    pCmdUI->Enable(true);
  else
    pCmdUI->Enable(false);
}