EditField.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. static autoComplete_t globalAutoComplete;
  23. /*
  24. ===============
  25. FindMatches
  26. ===============
  27. */
  28. static void FindMatches( const char *s ) {
  29. int i;
  30. if ( idStr::Icmpn( s, globalAutoComplete.completionString, strlen( globalAutoComplete.completionString ) ) != 0 ) {
  31. return;
  32. }
  33. globalAutoComplete.matchCount++;
  34. if ( globalAutoComplete.matchCount == 1 ) {
  35. idStr::Copynz( globalAutoComplete.currentMatch, s, sizeof( globalAutoComplete.currentMatch ) );
  36. return;
  37. }
  38. // cut currentMatch to the amount common with s
  39. for ( i = 0; s[i]; i++ ) {
  40. if ( tolower( globalAutoComplete.currentMatch[i] ) != tolower( s[i] ) ) {
  41. globalAutoComplete.currentMatch[i] = 0;
  42. break;
  43. }
  44. }
  45. globalAutoComplete.currentMatch[i] = 0;
  46. }
  47. /*
  48. ===============
  49. FindIndexMatch
  50. ===============
  51. */
  52. static void FindIndexMatch( const char *s ) {
  53. if ( idStr::Icmpn( s, globalAutoComplete.completionString, strlen( globalAutoComplete.completionString ) ) != 0 ) {
  54. return;
  55. }
  56. if( globalAutoComplete.findMatchIndex == globalAutoComplete.matchIndex ) {
  57. idStr::Copynz( globalAutoComplete.currentMatch, s, sizeof( globalAutoComplete.currentMatch ) );
  58. }
  59. globalAutoComplete.findMatchIndex++;
  60. }
  61. /*
  62. ===============
  63. PrintMatches
  64. ===============
  65. */
  66. static void PrintMatches( const char *s ) {
  67. if ( idStr::Icmpn( s, globalAutoComplete.currentMatch, strlen( globalAutoComplete.currentMatch ) ) == 0 ) {
  68. common->Printf( " %s\n", s );
  69. }
  70. }
  71. /*
  72. ===============
  73. PrintCvarMatches
  74. ===============
  75. */
  76. static void PrintCvarMatches( const char *s ) {
  77. if ( idStr::Icmpn( s, globalAutoComplete.currentMatch, strlen( globalAutoComplete.currentMatch ) ) == 0 ) {
  78. common->Printf( " %s" S_COLOR_WHITE " = \"%s\"\n", s, cvarSystem->GetCVarString( s ) );
  79. }
  80. }
  81. /*
  82. ===============
  83. idEditField::idEditField
  84. ===============
  85. */
  86. idEditField::idEditField() {
  87. widthInChars = 0;
  88. Clear();
  89. }
  90. /*
  91. ===============
  92. idEditField::~idEditField
  93. ===============
  94. */
  95. idEditField::~idEditField() {
  96. }
  97. /*
  98. ===============
  99. idEditField::Clear
  100. ===============
  101. */
  102. void idEditField::Clear( void ) {
  103. buffer[0] = 0;
  104. cursor = 0;
  105. scroll = 0;
  106. autoComplete.length = 0;
  107. autoComplete.valid = false;
  108. }
  109. /*
  110. ===============
  111. idEditField::SetWidthInChars
  112. ===============
  113. */
  114. void idEditField::SetWidthInChars( int w ) {
  115. assert( w <= MAX_EDIT_LINE );
  116. widthInChars = w;
  117. }
  118. /*
  119. ===============
  120. idEditField::SetCursor
  121. ===============
  122. */
  123. void idEditField::SetCursor( int c ) {
  124. assert( c <= MAX_EDIT_LINE );
  125. cursor = c;
  126. }
  127. /*
  128. ===============
  129. idEditField::GetCursor
  130. ===============
  131. */
  132. int idEditField::GetCursor( void ) const {
  133. return cursor;
  134. }
  135. /*
  136. ===============
  137. idEditField::ClearAutoComplete
  138. ===============
  139. */
  140. void idEditField::ClearAutoComplete( void ) {
  141. if ( autoComplete.length > 0 && autoComplete.length <= (int) strlen( buffer ) ) {
  142. buffer[autoComplete.length] = '\0';
  143. if ( cursor > autoComplete.length ) {
  144. cursor = autoComplete.length;
  145. }
  146. }
  147. autoComplete.length = 0;
  148. autoComplete.valid = false;
  149. }
  150. /*
  151. ===============
  152. idEditField::GetAutoCompleteLength
  153. ===============
  154. */
  155. int idEditField::GetAutoCompleteLength( void ) const {
  156. return autoComplete.length;
  157. }
  158. /*
  159. ===============
  160. idEditField::AutoComplete
  161. ===============
  162. */
  163. void idEditField::AutoComplete( void ) {
  164. char completionArgString[MAX_EDIT_LINE];
  165. idCmdArgs args;
  166. if ( !autoComplete.valid ) {
  167. args.TokenizeString( buffer, false );
  168. idStr::Copynz( autoComplete.completionString, args.Argv( 0 ), sizeof( autoComplete.completionString ) );
  169. idStr::Copynz( completionArgString, args.Args(), sizeof( completionArgString ) );
  170. autoComplete.matchCount = 0;
  171. autoComplete.matchIndex = 0;
  172. autoComplete.currentMatch[0] = 0;
  173. if ( strlen( autoComplete.completionString ) == 0 ) {
  174. return;
  175. }
  176. globalAutoComplete = autoComplete;
  177. cmdSystem->CommandCompletion( FindMatches );
  178. cvarSystem->CommandCompletion( FindMatches );
  179. autoComplete = globalAutoComplete;
  180. if ( autoComplete.matchCount == 0 ) {
  181. return; // no matches
  182. }
  183. // when there's only one match or there's an argument
  184. if ( autoComplete.matchCount == 1 || completionArgString[0] != '\0' ) {
  185. /// try completing arguments
  186. idStr::Append( autoComplete.completionString, sizeof( autoComplete.completionString ), " " );
  187. idStr::Append( autoComplete.completionString, sizeof( autoComplete.completionString ), completionArgString );
  188. autoComplete.matchCount = 0;
  189. globalAutoComplete = autoComplete;
  190. cmdSystem->ArgCompletion( autoComplete.completionString, FindMatches );
  191. cvarSystem->ArgCompletion( autoComplete.completionString, FindMatches );
  192. autoComplete = globalAutoComplete;
  193. idStr::snPrintf( buffer, sizeof( buffer ), "%s", autoComplete.currentMatch );
  194. if ( autoComplete.matchCount == 0 ) {
  195. // no argument matches
  196. idStr::Append( buffer, sizeof( buffer ), " " );
  197. idStr::Append( buffer, sizeof( buffer ), completionArgString );
  198. SetCursor( strlen( buffer ) );
  199. return;
  200. }
  201. } else {
  202. // multiple matches, complete to shortest
  203. idStr::snPrintf( buffer, sizeof( buffer ), "%s", autoComplete.currentMatch );
  204. if ( strlen( completionArgString ) ) {
  205. idStr::Append( buffer, sizeof( buffer ), " " );
  206. idStr::Append( buffer, sizeof( buffer ), completionArgString );
  207. }
  208. }
  209. autoComplete.length = strlen( buffer );
  210. autoComplete.valid = ( autoComplete.matchCount != 1 );
  211. SetCursor( autoComplete.length );
  212. common->Printf( "]%s\n", buffer );
  213. // run through again, printing matches
  214. globalAutoComplete = autoComplete;
  215. cmdSystem->CommandCompletion( PrintMatches );
  216. cmdSystem->ArgCompletion( autoComplete.completionString, PrintMatches );
  217. cvarSystem->CommandCompletion( PrintCvarMatches );
  218. cvarSystem->ArgCompletion( autoComplete.completionString, PrintMatches );
  219. } else if ( autoComplete.matchCount != 1 ) {
  220. // get the next match and show instead
  221. autoComplete.matchIndex++;
  222. if ( autoComplete.matchIndex == autoComplete.matchCount ) {
  223. autoComplete.matchIndex = 0;
  224. }
  225. autoComplete.findMatchIndex = 0;
  226. globalAutoComplete = autoComplete;
  227. cmdSystem->CommandCompletion( FindIndexMatch );
  228. cmdSystem->ArgCompletion( autoComplete.completionString, FindIndexMatch );
  229. cvarSystem->CommandCompletion( FindIndexMatch );
  230. cvarSystem->ArgCompletion( autoComplete.completionString, FindIndexMatch );
  231. autoComplete = globalAutoComplete;
  232. // and print it
  233. idStr::snPrintf( buffer, sizeof( buffer ), autoComplete.currentMatch );
  234. if ( autoComplete.length > (int)strlen( buffer ) ) {
  235. autoComplete.length = strlen( buffer );
  236. }
  237. SetCursor( autoComplete.length );
  238. }
  239. }
  240. /*
  241. ===============
  242. idEditField::CharEvent
  243. ===============
  244. */
  245. void idEditField::CharEvent( int ch ) {
  246. int len;
  247. if ( ch == 'v' - 'a' + 1 ) { // ctrl-v is paste
  248. Paste();
  249. return;
  250. }
  251. if ( ch == 'c' - 'a' + 1 ) { // ctrl-c clears the field
  252. Clear();
  253. return;
  254. }
  255. len = strlen( buffer );
  256. if ( ch == 'h' - 'a' + 1 || ch == K_BACKSPACE ) { // ctrl-h is backspace
  257. if ( cursor > 0 ) {
  258. memmove( buffer + cursor - 1, buffer + cursor, len + 1 - cursor );
  259. cursor--;
  260. if ( cursor < scroll ) {
  261. scroll--;
  262. }
  263. }
  264. return;
  265. }
  266. if ( ch == 'a' - 'a' + 1 ) { // ctrl-a is home
  267. cursor = 0;
  268. scroll = 0;
  269. return;
  270. }
  271. if ( ch == 'e' - 'a' + 1 ) { // ctrl-e is end
  272. cursor = len;
  273. scroll = cursor - widthInChars;
  274. return;
  275. }
  276. //
  277. // ignore any other non printable chars
  278. //
  279. if ( ch < 32 ) {
  280. return;
  281. }
  282. if ( idKeyInput::GetOverstrikeMode() ) {
  283. if ( cursor == MAX_EDIT_LINE - 1 ) {
  284. return;
  285. }
  286. buffer[cursor] = ch;
  287. cursor++;
  288. } else { // insert mode
  289. if ( len == MAX_EDIT_LINE - 1 ) {
  290. return; // all full
  291. }
  292. memmove( buffer + cursor + 1, buffer + cursor, len + 1 - cursor );
  293. buffer[cursor] = ch;
  294. cursor++;
  295. }
  296. if ( cursor >= widthInChars ) {
  297. scroll++;
  298. }
  299. if ( cursor == len + 1 ) {
  300. buffer[cursor] = 0;
  301. }
  302. }
  303. /*
  304. ===============
  305. idEditField::KeyDownEvent
  306. ===============
  307. */
  308. void idEditField::KeyDownEvent( int key ) {
  309. int len;
  310. // shift-insert is paste
  311. if ( ( ( key == K_INS ) || ( key == K_KP_INS ) ) && idKeyInput::IsDown( K_SHIFT ) ) {
  312. ClearAutoComplete();
  313. Paste();
  314. return;
  315. }
  316. len = strlen( buffer );
  317. if ( key == K_DEL ) {
  318. if ( autoComplete.length ) {
  319. ClearAutoComplete();
  320. } else if ( cursor < len ) {
  321. memmove( buffer + cursor, buffer + cursor + 1, len - cursor );
  322. }
  323. return;
  324. }
  325. if ( key == K_RIGHTARROW ) {
  326. if ( idKeyInput::IsDown( K_CTRL ) ) {
  327. // skip to next word
  328. while( ( cursor < len ) && ( buffer[ cursor ] != ' ' ) ) {
  329. cursor++;
  330. }
  331. while( ( cursor < len ) && ( buffer[ cursor ] == ' ' ) ) {
  332. cursor++;
  333. }
  334. } else {
  335. cursor++;
  336. }
  337. if ( cursor > len ) {
  338. cursor = len;
  339. }
  340. if ( cursor >= scroll + widthInChars ) {
  341. scroll = cursor - widthInChars + 1;
  342. }
  343. if ( autoComplete.length > 0 ) {
  344. autoComplete.length = cursor;
  345. }
  346. return;
  347. }
  348. if ( key == K_LEFTARROW ) {
  349. if ( idKeyInput::IsDown( K_CTRL ) ) {
  350. // skip to previous word
  351. while( ( cursor > 0 ) && ( buffer[ cursor - 1 ] == ' ' ) ) {
  352. cursor--;
  353. }
  354. while( ( cursor > 0 ) && ( buffer[ cursor - 1 ] != ' ' ) ) {
  355. cursor--;
  356. }
  357. } else {
  358. cursor--;
  359. }
  360. if ( cursor < 0 ) {
  361. cursor = 0;
  362. }
  363. if ( cursor < scroll ) {
  364. scroll = cursor;
  365. }
  366. if ( autoComplete.length ) {
  367. autoComplete.length = cursor;
  368. }
  369. return;
  370. }
  371. if ( key == K_HOME || ( tolower( key ) == 'a' && idKeyInput::IsDown( K_CTRL ) ) ) {
  372. cursor = 0;
  373. scroll = 0;
  374. if ( autoComplete.length ) {
  375. autoComplete.length = cursor;
  376. autoComplete.valid = false;
  377. }
  378. return;
  379. }
  380. if ( key == K_END || ( tolower( key ) == 'e' && idKeyInput::IsDown( K_CTRL ) ) ) {
  381. cursor = len;
  382. if ( cursor >= scroll + widthInChars ) {
  383. scroll = cursor - widthInChars + 1;
  384. }
  385. if ( autoComplete.length ) {
  386. autoComplete.length = cursor;
  387. autoComplete.valid = false;
  388. }
  389. return;
  390. }
  391. if ( key == K_INS ) {
  392. idKeyInput::SetOverstrikeMode( !idKeyInput::GetOverstrikeMode() );
  393. return;
  394. }
  395. // clear autocompletion buffer on normal key input
  396. if ( key != K_CAPSLOCK && key != K_ALT && key != K_CTRL && key != K_SHIFT ) {
  397. ClearAutoComplete();
  398. }
  399. }
  400. /*
  401. ===============
  402. idEditField::Paste
  403. ===============
  404. */
  405. void idEditField::Paste( void ) {
  406. char *cbd;
  407. int pasteLen, i;
  408. cbd = Sys_GetClipboardData();
  409. if ( !cbd ) {
  410. return;
  411. }
  412. // send as if typed, so insert / overstrike works properly
  413. pasteLen = strlen( cbd );
  414. for ( i = 0; i < pasteLen; i++ ) {
  415. CharEvent( cbd[i] );
  416. }
  417. Mem_Free( cbd );
  418. }
  419. /*
  420. ===============
  421. idEditField::GetBuffer
  422. ===============
  423. */
  424. char *idEditField::GetBuffer( void ) {
  425. return buffer;
  426. }
  427. /*
  428. ===============
  429. idEditField::SetBuffer
  430. ===============
  431. */
  432. void idEditField::SetBuffer( const char *buf ) {
  433. Clear();
  434. idStr::Copynz( buffer, buf, sizeof( buffer ) );
  435. SetCursor( strlen( buffer ) );
  436. }
  437. /*
  438. ===============
  439. idEditField::Draw
  440. ===============
  441. */
  442. void idEditField::Draw( int x, int y, int width, bool showCursor, const idMaterial *shader ) {
  443. int len;
  444. int drawLen;
  445. int prestep;
  446. int cursorChar;
  447. char str[MAX_EDIT_LINE];
  448. int size;
  449. size = SMALLCHAR_WIDTH;
  450. drawLen = widthInChars;
  451. len = strlen( buffer ) + 1;
  452. // guarantee that cursor will be visible
  453. if ( len <= drawLen ) {
  454. prestep = 0;
  455. } else {
  456. if ( scroll + drawLen > len ) {
  457. scroll = len - drawLen;
  458. if ( scroll < 0 ) {
  459. scroll = 0;
  460. }
  461. }
  462. prestep = scroll;
  463. // Skip color code
  464. if ( idStr::IsColor( buffer + prestep ) ) {
  465. prestep += 2;
  466. }
  467. if ( prestep > 0 && idStr::IsColor( buffer + prestep - 1 ) ) {
  468. prestep++;
  469. }
  470. }
  471. if ( prestep + drawLen > len ) {
  472. drawLen = len - prestep;
  473. }
  474. // extract <drawLen> characters from the field at <prestep>
  475. if ( drawLen >= MAX_EDIT_LINE ) {
  476. common->Error( "drawLen >= MAX_EDIT_LINE" );
  477. }
  478. memcpy( str, buffer + prestep, drawLen );
  479. str[ drawLen ] = 0;
  480. // draw it
  481. renderSystem->DrawSmallStringExt( x, y, str, colorWhite, false, shader );
  482. // draw the cursor
  483. if ( !showCursor ) {
  484. return;
  485. }
  486. if ( (int)( com_ticNumber >> 4 ) & 1 ) {
  487. return; // off blink
  488. }
  489. if ( idKeyInput::GetOverstrikeMode() ) {
  490. cursorChar = 11;
  491. } else {
  492. cursorChar = 10;
  493. }
  494. // Move the cursor back to account for color codes
  495. for ( int i = 0; i<cursor; i++ ) {
  496. if ( idStr::IsColor( &str[i] ) ) {
  497. i++;
  498. prestep += 2;
  499. }
  500. }
  501. renderSystem->DrawSmallChar( x + ( cursor - prestep ) * size, y, cursorChar, shader );
  502. }