DialogScriptEditor.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "../../sys/win32/rc/Common_resource.h"
  23. #include "../../sys/win32/rc/ScriptEditor_resource.h"
  24. #include "../comafx/DialogGoToLine.h"
  25. #include "DialogScriptEditor.h"
  26. #ifdef ID_DEBUG_MEMORY
  27. #undef new
  28. #undef DEBUG_NEW
  29. #define DEBUG_NEW new
  30. #endif
  31. typedef struct scriptEventInfo_s {
  32. idStr name;
  33. idStr parms;
  34. idStr help;
  35. } scriptEventInfo_t;
  36. static idList<scriptEventInfo_t> scriptEvents;
  37. static DialogScriptEditor *g_ScriptDialog = NULL;
  38. // DialogScriptEditor dialog
  39. static UINT FindDialogMessage = ::RegisterWindowMessage( FINDMSGSTRING );
  40. toolTip_t DialogScriptEditor::toolTips[] = {
  41. { IDOK, "save" },
  42. { IDCANCEL, "cancel" },
  43. { 0, NULL }
  44. };
  45. IMPLEMENT_DYNAMIC(DialogScriptEditor, CDialog)
  46. /*
  47. ================
  48. DialogScriptEditor::DialogScriptEditor
  49. ================
  50. */
  51. DialogScriptEditor::DialogScriptEditor( CWnd* pParent /*=NULL*/ )
  52. : CDialog(DialogScriptEditor::IDD, pParent)
  53. , findDlg(NULL)
  54. , matchCase(false)
  55. , matchWholeWords(false)
  56. , firstLine(0)
  57. {
  58. }
  59. /*
  60. ================
  61. DialogScriptEditor::~DialogScriptEditor
  62. ================
  63. */
  64. DialogScriptEditor::~DialogScriptEditor() {
  65. }
  66. /*
  67. ================
  68. DialogScriptEditor::DoDataExchange
  69. ================
  70. */
  71. void DialogScriptEditor::DoDataExchange(CDataExchange* pDX) {
  72. CDialog::DoDataExchange(pDX);
  73. //{{AFX_DATA_MAP(DialogScriptEditor)
  74. DDX_Control(pDX, IDC_SCRIPTEDITOR_EDIT_TEXT, scriptEdit);
  75. DDX_Control(pDX, IDOK, okButton);
  76. DDX_Control(pDX, IDCANCEL, cancelButton);
  77. //}}AFX_DATA_MAP
  78. }
  79. /*
  80. ================
  81. DialogScriptEditor::PreTranslateMessage
  82. ================
  83. */
  84. BOOL DialogScriptEditor::PreTranslateMessage( MSG* pMsg ) {
  85. if ( WM_KEYFIRST <= pMsg->message && pMsg->message <= WM_KEYLAST ) {
  86. if ( m_hAccel && ::TranslateAccelerator( m_hWnd, m_hAccel, pMsg ) ) {
  87. return TRUE;
  88. }
  89. }
  90. return CWnd::PreTranslateMessage(pMsg);
  91. }
  92. /*
  93. ================
  94. DialogScriptEditor::UpdateStatusBar
  95. ================
  96. */
  97. void DialogScriptEditor::UpdateStatusBar( void ) {
  98. int line, column, character;
  99. scriptEdit.GetCursorPos( line, column, character );
  100. statusBar.SetWindowText( va( "Line: %d, Column: %d, Character: %d", line, column, character ) );
  101. }
  102. /*
  103. ================
  104. DialogScriptEditor::InitScriptEvents
  105. ================
  106. */
  107. void DialogScriptEditor::InitScriptEvents( void ) {
  108. int index;
  109. idParser src;
  110. idToken token;
  111. idStr whiteSpace;
  112. scriptEventInfo_t info;
  113. if ( !src.LoadFile( "script/doom_events.script" ) ) {
  114. return;
  115. }
  116. scriptEvents.Clear();
  117. while( src.ReadToken( &token ) ) {
  118. if ( token == "scriptEvent" ) {
  119. src.GetLastWhiteSpace( whiteSpace );
  120. index = whiteSpace.Find( "//" );
  121. if ( index != -1 ) {
  122. info.help = whiteSpace.Right( whiteSpace.Length() - index );
  123. info.help.Replace( "\r", "" );
  124. info.help.Replace( "\n", "\r\n" );
  125. } else {
  126. info.help = "";
  127. }
  128. src.ExpectTokenType( TT_NAME, 0, &token );
  129. info.parms = token;
  130. src.ExpectTokenType( TT_NAME, 0, &token );
  131. info.name = token;
  132. src.ExpectTokenString( "(" );
  133. info.parms += " " + info.name + "(";
  134. while( src.ReadToken( &token ) && token != ";" ) {
  135. info.parms.Append( " " + token );
  136. }
  137. scriptEvents.Append( info );
  138. }
  139. }
  140. }
  141. /*
  142. ================
  143. GetScriptEvents
  144. ================
  145. */
  146. bool GetScriptEvents( const char *objectName, CListBox &listBox ) {
  147. for ( int i = 0; i < scriptEvents.Num(); i++ ) {
  148. listBox.AddString( scriptEvents[i].name );
  149. }
  150. return true;
  151. }
  152. /*
  153. ================
  154. GetFunctionParms
  155. ================
  156. */
  157. bool GetFunctionParms( const char *funcName, CString &parmString ) {
  158. for ( int i = 0; i < scriptEvents.Num(); i++ ) {
  159. if ( scriptEvents[i].name.Cmp( funcName ) == 0 ) {
  160. parmString = scriptEvents[i].parms;
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. /*
  167. ================
  168. GetToolTip
  169. ================
  170. */
  171. bool GetToolTip( const char *name, CString &string ) {
  172. for ( int i = 0; i < scriptEvents.Num(); i++ ) {
  173. if ( scriptEvents[i].name.Cmp( name ) == 0 ) {
  174. string = scriptEvents[i].help + scriptEvents[i].parms;
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. /*
  181. ================
  182. DialogScriptEditor::OpenFile
  183. ================
  184. */
  185. void DialogScriptEditor::OpenFile( const char *fileName ) {
  186. int numLines = 0;
  187. int numCharsPerLine = 0;
  188. int maxCharsPerLine = 0;
  189. idStr scriptText, extension;
  190. CRect rect;
  191. void *buffer;
  192. scriptEdit.Init();
  193. scriptEdit.AllowPathNames( false );
  194. idStr( fileName ).ExtractFileExtension( extension );
  195. if ( extension.Icmp( "script" ) == 0 ) {
  196. InitScriptEvents();
  197. scriptEdit.SetCaseSensitive( true );
  198. scriptEdit.LoadKeyWordsFromFile( "editors/script.def" );
  199. scriptEdit.SetObjectMemberCallback( GetScriptEvents );
  200. scriptEdit.SetFunctionParmCallback( GetFunctionParms );
  201. scriptEdit.SetToolTipCallback( GetToolTip );
  202. } else if ( extension.Icmp( "gui" ) == 0 ) {
  203. scriptEdit.SetStringColor( SRE_COLOR_DARK_CYAN, SRE_COLOR_LIGHT_BROWN );
  204. scriptEdit.LoadKeyWordsFromFile( "editors/gui.def" );
  205. }
  206. if ( fileSystem->ReadFile( fileName, &buffer ) == -1 ) {
  207. return;
  208. }
  209. scriptText = (char *) buffer;
  210. fileSystem->FreeFile( buffer );
  211. this->fileName = fileName;
  212. // clean up new-line crapola
  213. scriptText.Replace( "\r", "" );
  214. scriptText.Replace( "\n", "\r" );
  215. scriptText.Replace( "\v", "\r" );
  216. scriptEdit.SetText( scriptText );
  217. for( const char *ptr = scriptText.c_str(); *ptr; ptr++ ) {
  218. if ( *ptr == '\r' ) {
  219. if ( numCharsPerLine > maxCharsPerLine ) {
  220. maxCharsPerLine = numCharsPerLine;
  221. }
  222. numCharsPerLine = 0;
  223. numLines++;
  224. } else if ( *ptr == '\t' ) {
  225. numCharsPerLine += TAB_SIZE;
  226. } else {
  227. numCharsPerLine++;
  228. }
  229. }
  230. SetWindowText( va( "Script Editor (%s)", fileName ) );
  231. rect.left = initialRect.left;
  232. rect.right = rect.left + maxCharsPerLine * FONT_WIDTH + 32;
  233. rect.top = initialRect.top;
  234. rect.bottom = rect.top + numLines * (FONT_HEIGHT+8) + 24 + 56;
  235. if ( rect.right < initialRect.right ) {
  236. rect.right = initialRect.right;
  237. } else if ( rect.right - rect.left > 1024 ) {
  238. rect.right = rect.left + 1024;
  239. }
  240. if ( rect.bottom < initialRect.bottom ) {
  241. rect.bottom = initialRect.bottom;
  242. } else if ( rect.bottom - rect.top > 768 ) {
  243. rect.bottom = rect.top + 768;
  244. }
  245. MoveWindow( rect );
  246. okButton.EnableWindow( FALSE );
  247. UpdateStatusBar();
  248. scriptEdit.SetFocus();
  249. }
  250. /*
  251. ================
  252. DialogScriptEditor::OnInitDialog
  253. ================
  254. */
  255. BOOL DialogScriptEditor::OnInitDialog() {
  256. com_editors |= EDITOR_SCRIPT;
  257. CDialog::OnInitDialog();
  258. // load accelerator table
  259. m_hAccel = ::LoadAccelerators( AfxGetResourceHandle(), MAKEINTRESOURCE( IDR_ACCELERATOR_SCRIPTEDITOR ) );
  260. // create status bar
  261. statusBar.CreateEx( SBARS_SIZEGRIP, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM, initialRect, this, AFX_IDW_STATUS_BAR );
  262. scriptEdit.LimitText( 1024 * 1024 );
  263. GetClientRect( initialRect );
  264. SetWindowText( "Script Editor" );
  265. EnableToolTips( TRUE );
  266. okButton.EnableWindow( FALSE );
  267. UpdateStatusBar();
  268. return FALSE; // return TRUE unless you set the focus to a control
  269. // EXCEPTION: OCX Property Pages should return FALSE
  270. }
  271. BEGIN_MESSAGE_MAP(DialogScriptEditor, CDialog)
  272. ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipNotify)
  273. ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipNotify)
  274. ON_WM_DESTROY()
  275. ON_WM_ACTIVATE()
  276. ON_WM_MOVE()
  277. ON_WM_SIZE()
  278. ON_WM_SIZING()
  279. ON_WM_SETFOCUS()
  280. ON_COMMAND(ID_EDIT_FIND, OnEditFind)
  281. ON_COMMAND(ID_EDIT_REPLACE, OnEditReplace)
  282. ON_COMMAND(ID_SCRIPTEDITOR_FIND_NEXT, OnEditFindNext)
  283. ON_COMMAND(ID_SCRIPTEDITOR_GOTOLINE, OnEditGoToLine)
  284. ON_REGISTERED_MESSAGE(FindDialogMessage, OnFindDialogMessage)
  285. ON_NOTIFY(EN_CHANGE, IDC_SCRIPTEDITOR_EDIT_TEXT, OnEnChangeEdit)
  286. ON_NOTIFY(EN_MSGFILTER, IDC_SCRIPTEDITOR_EDIT_TEXT, OnEnInputEdit)
  287. ON_BN_CLICKED(IDOK, OnBnClickedOk)
  288. ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
  289. END_MESSAGE_MAP()
  290. /*
  291. ================
  292. ScriptEditorInit
  293. ================
  294. */
  295. void ScriptEditorInit( const idDict *spawnArgs ) {
  296. if ( renderSystem->IsFullScreen() ) {
  297. common->Printf( "Cannot run the script editor in fullscreen mode.\n"
  298. "Set r_fullscreen to 0 and vid_restart.\n" );
  299. return;
  300. }
  301. if ( g_ScriptDialog == NULL ) {
  302. InitAfx();
  303. g_ScriptDialog = new DialogScriptEditor();
  304. }
  305. if ( g_ScriptDialog->GetSafeHwnd() == NULL) {
  306. g_ScriptDialog->Create( IDD_DIALOG_SCRIPTEDITOR );
  307. /*
  308. // FIXME: restore position
  309. CRect rct;
  310. g_ScriptDialog->SetWindowPos( NULL, rct.left, rct.top, 0, 0, SWP_NOSIZE );
  311. */
  312. }
  313. idKeyInput::ClearStates();
  314. g_ScriptDialog->ShowWindow( SW_SHOW );
  315. g_ScriptDialog->SetFocus();
  316. if ( spawnArgs ) {
  317. }
  318. }
  319. /*
  320. ================
  321. ScriptEditorRun
  322. ================
  323. */
  324. void ScriptEditorRun( void ) {
  325. #if _MSC_VER >= 1300
  326. MSG *msg = AfxGetCurrentMessage(); // TODO Robert fix me!!
  327. #else
  328. MSG *msg = &m_msgCur;
  329. #endif
  330. while( ::PeekMessage(msg, NULL, NULL, NULL, PM_NOREMOVE) ) {
  331. // pump message
  332. if ( !AfxGetApp()->PumpMessage() ) {
  333. }
  334. }
  335. }
  336. /*
  337. ================
  338. ScriptEditorShutdown
  339. ================
  340. */
  341. void ScriptEditorShutdown( void ) {
  342. delete g_ScriptDialog;
  343. g_ScriptDialog = NULL;
  344. scriptEvents.Clear();
  345. }
  346. // DialogScriptEditor message handlers
  347. /*
  348. ================
  349. DialogScriptEditor::OnActivate
  350. ================
  351. */
  352. void DialogScriptEditor::OnActivate( UINT nState, CWnd *pWndOther, BOOL bMinimized ) {
  353. CDialog::OnActivate( nState, pWndOther, bMinimized );
  354. }
  355. /*
  356. ================
  357. DialogScriptEditor::OnToolTipNotify
  358. ================
  359. */
  360. BOOL DialogScriptEditor::OnToolTipNotify( UINT id, NMHDR *pNMHDR, LRESULT *pResult ) {
  361. return DefaultOnToolTipNotify( toolTips, id, pNMHDR, pResult );
  362. }
  363. /*
  364. ================
  365. DialogScriptEditor::OnSetFocus
  366. ================
  367. */
  368. void DialogScriptEditor::OnSetFocus( CWnd *pOldWnd ) {
  369. CDialog::OnSetFocus( pOldWnd );
  370. }
  371. /*
  372. ================
  373. DialogScriptEditor::OnDestroy
  374. ================
  375. */
  376. void DialogScriptEditor::OnDestroy() {
  377. return CDialog::OnDestroy();
  378. }
  379. /*
  380. ================
  381. DialogScriptEditor::OnMove
  382. ================
  383. */
  384. void DialogScriptEditor::OnMove( int x, int y ) {
  385. if ( GetSafeHwnd() ) {
  386. CRect rct;
  387. GetWindowRect( rct );
  388. // FIXME: save position
  389. }
  390. CDialog::OnMove( x, y );
  391. }
  392. /*
  393. ================
  394. DialogScriptEditor::OnSize
  395. ================
  396. */
  397. #define BORDER_SIZE 0
  398. #define BUTTON_SPACE 4
  399. #define TOOLBAR_HEIGHT 24
  400. void DialogScriptEditor::OnSize( UINT nType, int cx, int cy ) {
  401. CRect clientRect, rect;
  402. LockWindowUpdate();
  403. CDialog::OnSize( nType, cx, cy );
  404. GetClientRect( clientRect );
  405. if ( scriptEdit.GetSafeHwnd() ) {
  406. rect.left = BORDER_SIZE;
  407. rect.top = BORDER_SIZE;
  408. rect.right = clientRect.Width() - BORDER_SIZE;
  409. rect.bottom = clientRect.Height() - 56;
  410. scriptEdit.MoveWindow( rect.left, rect.top, rect.Width(), rect.Height() );
  411. }
  412. if ( okButton.GetSafeHwnd() ) {
  413. okButton.GetClientRect( rect );
  414. int width = rect.Width();
  415. int height = rect.Height();
  416. rect.left = clientRect.Width() - BORDER_SIZE - BUTTON_SPACE - 2 * width;
  417. rect.top = clientRect.Height() - TOOLBAR_HEIGHT - height;
  418. rect.right = clientRect.Width() - BORDER_SIZE - BUTTON_SPACE - width;
  419. rect.bottom = clientRect.Height() - TOOLBAR_HEIGHT;
  420. okButton.MoveWindow( rect.left, rect.top, rect.Width(), rect.Height() );
  421. }
  422. if ( cancelButton.GetSafeHwnd() ) {
  423. cancelButton.GetClientRect( rect );
  424. int width = rect.Width();
  425. int height = rect.Height();
  426. rect.left = clientRect.Width() - BORDER_SIZE - width;
  427. rect.top = clientRect.Height() - TOOLBAR_HEIGHT - height;
  428. rect.right = clientRect.Width() - BORDER_SIZE;
  429. rect.bottom = clientRect.Height() - TOOLBAR_HEIGHT;
  430. cancelButton.MoveWindow( rect.left, rect.top, rect.Width(), rect.Height() );
  431. }
  432. if ( statusBar.GetSafeHwnd() ) {
  433. rect.left = clientRect.Width() - 2;
  434. rect.top = clientRect.Height() - 2;
  435. rect.right = clientRect.Width() - 2;
  436. rect.bottom = clientRect.Height() - 2;
  437. statusBar.MoveWindow( rect.left, rect.top, rect.Width(), rect.Height() );
  438. }
  439. UnlockWindowUpdate();
  440. }
  441. /*
  442. ================
  443. DialogScriptEditor::OnSizing
  444. ================
  445. */
  446. void DialogScriptEditor::OnSizing( UINT nSide, LPRECT lpRect ) {
  447. /*
  448. 1 = left
  449. 2 = right
  450. 3 = top
  451. 4 = left - top
  452. 5 = right - top
  453. 6 = bottom
  454. 7 = left - bottom
  455. 8 = right - bottom
  456. */
  457. CDialog::OnSizing( nSide, lpRect );
  458. if ( ( nSide - 1 ) % 3 == 0 ) {
  459. if ( lpRect->right - lpRect->left < initialRect.Width() ) {
  460. lpRect->left = lpRect->right - initialRect.Width();
  461. }
  462. } else if ( ( nSide - 2 ) % 3 == 0 ) {
  463. if ( lpRect->right - lpRect->left < initialRect.Width() ) {
  464. lpRect->right = lpRect->left + initialRect.Width();
  465. }
  466. }
  467. if ( nSide >= 3 && nSide <= 5 ) {
  468. if ( lpRect->bottom - lpRect->top < initialRect.Height() ) {
  469. lpRect->top = lpRect->bottom - initialRect.Height();
  470. }
  471. } else if ( nSide >= 6 && nSide <= 9 ) {
  472. if ( lpRect->bottom - lpRect->top < initialRect.Height() ) {
  473. lpRect->bottom = lpRect->top + initialRect.Height();
  474. }
  475. }
  476. }
  477. /*
  478. ================
  479. DialogScriptEditor::OnEditGoToLine
  480. ================
  481. */
  482. void DialogScriptEditor::OnEditGoToLine() {
  483. DialogGoToLine goToLineDlg;
  484. goToLineDlg.SetRange( firstLine, firstLine + scriptEdit.GetLineCount() - 1 );
  485. if ( goToLineDlg.DoModal() != IDOK ) {
  486. return;
  487. }
  488. scriptEdit.GoToLine( goToLineDlg.GetLine() - firstLine );
  489. }
  490. /*
  491. ================
  492. DialogScriptEditor::OnEditFind
  493. ================
  494. */
  495. void DialogScriptEditor::OnEditFind() {
  496. CString selText = scriptEdit.GetSelText();
  497. if ( selText.GetLength() ) {
  498. findStr = selText;
  499. }
  500. // create find/replace dialog
  501. if ( !findDlg ) {
  502. findDlg = new CFindReplaceDialog(); // Must be created on the heap
  503. findDlg->Create( TRUE, findStr, "", FR_DOWN, this );
  504. }
  505. }
  506. /*
  507. ================
  508. DialogScriptEditor::OnEditFindNext
  509. ================
  510. */
  511. void DialogScriptEditor::OnEditFindNext() {
  512. if ( scriptEdit.FindNext( findStr, matchCase, matchWholeWords, searchForward ) ) {
  513. scriptEdit.SetFocus();
  514. } else {
  515. AfxMessageBox( "The specified text was not found.", MB_OK | MB_ICONINFORMATION, 0 );
  516. }
  517. }
  518. /*
  519. ================
  520. DialogScriptEditor::OnEditReplace
  521. ================
  522. */
  523. void DialogScriptEditor::OnEditReplace() {
  524. CString selText = scriptEdit.GetSelText();
  525. if ( selText.GetLength() ) {
  526. findStr = selText;
  527. }
  528. // create find/replace dialog
  529. if ( !findDlg ) {
  530. findDlg = new CFindReplaceDialog(); // Must be created on the heap
  531. findDlg->Create( FALSE, findStr, "", FR_DOWN, this );
  532. }
  533. }
  534. /*
  535. ================
  536. DialogScriptEditor::OnFindDialogMessage
  537. ================
  538. */
  539. LRESULT DialogScriptEditor::OnFindDialogMessage( WPARAM wParam, LPARAM lParam ) {
  540. if ( findDlg == NULL ) {
  541. return 0;
  542. }
  543. if ( findDlg->IsTerminating() ) {
  544. findDlg = NULL;
  545. return 0;
  546. }
  547. if( findDlg->FindNext() ) {
  548. findStr = findDlg->GetFindString();
  549. matchCase = findDlg->MatchCase() != FALSE;
  550. matchWholeWords = findDlg->MatchWholeWord() != FALSE;
  551. searchForward = findDlg->SearchDown() != FALSE;
  552. OnEditFindNext();
  553. }
  554. if ( findDlg->ReplaceCurrent() ) {
  555. long selStart, selEnd;
  556. replaceStr = findDlg->GetReplaceString();
  557. scriptEdit.GetSel( selStart, selEnd );
  558. if ( selEnd > selStart ) {
  559. scriptEdit.ReplaceSel( replaceStr, TRUE );
  560. }
  561. }
  562. if ( findDlg->ReplaceAll() ) {
  563. replaceStr = findDlg->GetReplaceString();
  564. findStr = findDlg->GetFindString();
  565. matchCase = findDlg->MatchCase() != FALSE;
  566. matchWholeWords = findDlg->MatchWholeWord() != FALSE;
  567. int numReplaces = scriptEdit.ReplaceAll( findStr, replaceStr, matchCase, matchWholeWords );
  568. if ( numReplaces == 0 ) {
  569. AfxMessageBox( "The specified text was not found.", MB_OK | MB_ICONINFORMATION, 0 );
  570. } else {
  571. AfxMessageBox( va( "Replaced %d occurances.", numReplaces ), MB_OK | MB_ICONINFORMATION, 0 );
  572. }
  573. }
  574. return 0;
  575. }
  576. /*
  577. ================
  578. DialogScriptEditor::OnEnChangeEdit
  579. ================
  580. */
  581. void DialogScriptEditor::OnEnChangeEdit( NMHDR *pNMHDR, LRESULT *pResult ) {
  582. okButton.EnableWindow( TRUE );
  583. }
  584. /*
  585. ================
  586. DialogScriptEditor::OnEnInputEdit
  587. ================
  588. */
  589. void DialogScriptEditor::OnEnInputEdit( NMHDR *pNMHDR, LRESULT *pResult ) {
  590. MSGFILTER *msgFilter = (MSGFILTER *)pNMHDR;
  591. if ( msgFilter->msg != 512 && msgFilter->msg != 33 ) {
  592. UpdateStatusBar();
  593. }
  594. *pResult = 0;
  595. }
  596. /*
  597. ================
  598. DialogScriptEditor::OnBnClickedOk
  599. ================
  600. */
  601. void DialogScriptEditor::OnBnClickedOk() {
  602. idStr scriptText;
  603. common->Printf( "Writing \'%s\'...\n", fileName.c_str() );
  604. scriptEdit.GetText( scriptText );
  605. // clean up new-line crapola
  606. scriptText.Replace( "\n", "" );
  607. scriptText.Replace( "\r", "\r\n" );
  608. scriptText.Replace( "\v", "\r\n" );
  609. if ( fileSystem->WriteFile( fileName, scriptText, scriptText.Length(), "fs_devpath" ) == -1 ) {
  610. MessageBox( va( "Couldn't save: %s", fileName.c_str() ), va( "Error saving: %s", fileName.c_str() ), MB_OK | MB_ICONERROR );
  611. return;
  612. }
  613. okButton.EnableWindow( FALSE );
  614. }
  615. /*
  616. ================
  617. DialogScriptEditor::OnBnClickedCancel
  618. ================
  619. */
  620. void DialogScriptEditor::OnBnClickedCancel() {
  621. if ( okButton.IsWindowEnabled() ) {
  622. if ( MessageBox( "Cancel changes?", "Cancel", MB_YESNO | MB_ICONQUESTION ) != IDYES ) {
  623. return;
  624. }
  625. }
  626. OnCancel();
  627. }