guiTextEditCtrl.cc 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/consoleTypes.h"
  23. #include "console/console.h"
  24. #include "graphics/dgl.h"
  25. #include "gui/guiCanvas.h"
  26. #include "gui/guiMLTextCtrl.h"
  27. #include "gui/guiTextEditCtrl.h"
  28. #include "gui/guiDefaultControlRender.h"
  29. #include "memory/frameAllocator.h"
  30. #include "string/unicode.h"
  31. IMPLEMENT_CONOBJECT(GuiTextEditCtrl);
  32. U32 GuiTextEditCtrl::smNumAwake = 0;
  33. GuiTextEditCtrl::GuiTextEditCtrl()
  34. {
  35. mInsertOn = true;
  36. mBlockStart = 0;
  37. mBlockEnd = 0;
  38. mCursorPos = 0;
  39. mCursorOn = false;
  40. mNumFramesElapsed = 0;
  41. mDragHit = false;
  42. mTabComplete = false;
  43. mScrollDir = 0;
  44. mUndoBlockStart = 0;
  45. mUndoBlockEnd = 0;
  46. mUndoCursorPos = 0;
  47. mPasswordText = false;
  48. mSinkAllKeyEvents = false;
  49. mActive = true;
  50. mTextOffsetReset = true;
  51. mHistoryDirty = false;
  52. mHistorySize = 0;
  53. mHistoryLast = -1;
  54. mHistoryIndex = 0;
  55. mHistoryBuf = NULL;
  56. mValidateCommand = StringTable->EmptyString;
  57. mEscapeCommand = StringTable->EmptyString;
  58. #ifdef TORQUE_OS_OSX
  59. UTF8 bullet[4] = { static_cast<UTF8>(0xE2), static_cast<UTF8>(0x80), static_cast<UTF8>(0xA2), 0 };
  60. mPasswordMask = StringTable->insert( bullet );
  61. #else
  62. mPasswordMask = StringTable->insert( "*" );
  63. #endif
  64. mEditCursor = NULL;
  65. }
  66. GuiTextEditCtrl::~GuiTextEditCtrl()
  67. {
  68. //delete the history buffer if it exists
  69. if (mHistoryBuf)
  70. {
  71. for (S32 i = 0; i < mHistorySize; i++)
  72. delete [] mHistoryBuf[i];
  73. delete [] mHistoryBuf;
  74. }
  75. }
  76. void GuiTextEditCtrl::initPersistFields()
  77. {
  78. Parent::initPersistFields();
  79. addField("validate", TypeString, Offset(mValidateCommand, GuiTextEditCtrl));
  80. addField("escapeCommand", TypeString, Offset(mEscapeCommand, GuiTextEditCtrl));
  81. addField("historySize", TypeS32, Offset(mHistorySize, GuiTextEditCtrl));
  82. addField("tabComplete", TypeBool, Offset(mTabComplete, GuiTextEditCtrl));
  83. addField("deniedSound", TypeAudioAssetPtr, Offset(mDeniedSound, GuiTextEditCtrl));
  84. addField("sinkAllKeyEvents", TypeBool, Offset(mSinkAllKeyEvents, GuiTextEditCtrl));
  85. addField("password", TypeBool, Offset(mPasswordText, GuiTextEditCtrl));
  86. addField("passwordMask", TypeString, Offset(mPasswordMask, GuiTextEditCtrl));
  87. }
  88. bool GuiTextEditCtrl::onAdd()
  89. {
  90. if ( ! Parent::onAdd() )
  91. return false;
  92. //create the history buffer
  93. if ( mHistorySize > 0 )
  94. {
  95. mHistoryBuf = new UTF16*[mHistorySize];
  96. for ( S32 i = 0; i < mHistorySize; i++ )
  97. {
  98. mHistoryBuf[i] = new UTF16[GuiTextCtrl::MAX_STRING_LENGTH + 1];
  99. mHistoryBuf[i][0] = '\0';
  100. }
  101. }
  102. if( mText && mText[0] )
  103. {
  104. setText(mText);
  105. }
  106. return true;
  107. }
  108. void GuiTextEditCtrl::onStaticModified(const char* slotName)
  109. {
  110. if(!dStricmp(slotName, "text"))
  111. setText(mText);
  112. }
  113. bool GuiTextEditCtrl::onWake()
  114. {
  115. if (! Parent::onWake())
  116. return false;
  117. // If this is the first awake text edit control, enable keyboard translation
  118. if (smNumAwake == 0)
  119. Platform::enableKeyboardTranslation();
  120. ++smNumAwake;
  121. return true;
  122. }
  123. void GuiTextEditCtrl::onSleep()
  124. {
  125. Parent::onSleep();
  126. // If this is the last awake text edit control, disable keyboard translation
  127. --smNumAwake;
  128. if (smNumAwake == 0)
  129. Platform::disableKeyboardTranslation();
  130. }
  131. void GuiTextEditCtrl::execConsoleCallback()
  132. {
  133. // Execute the console command!
  134. if ( mConsoleCommand[0] )
  135. {
  136. char buf[16];
  137. dSprintf( buf, sizeof( buf ), "%d", getId() );
  138. Con::setVariable( "$ThisControl", buf );
  139. Con::evaluate( mConsoleCommand, false );
  140. }
  141. // Update the console variable:
  142. if ( mConsoleVariable[0] )
  143. Con::setVariable( mConsoleVariable, mTextBuffer.getPtr8() );
  144. }
  145. void GuiTextEditCtrl::updateHistory( StringBuffer *inTxt, bool moveIndex )
  146. {
  147. const UTF16* txt = inTxt->getPtr();
  148. if(!txt)
  149. return;
  150. if(!mHistorySize)
  151. return;
  152. // see if it's already in
  153. if(mHistoryLast == -1 || dStrcmp(txt, mHistoryBuf[mHistoryLast]))
  154. {
  155. if(mHistoryLast == mHistorySize-1) // we're at the history limit... shuffle the pointers around:
  156. {
  157. UTF16 *first = mHistoryBuf[0];
  158. for(U32 i = 0; i < (U32)(mHistorySize - 1); i++)
  159. mHistoryBuf[i] = mHistoryBuf[i+1];
  160. mHistoryBuf[mHistorySize-1] = first;
  161. if(mHistoryIndex > 0)
  162. mHistoryIndex--;
  163. }
  164. else
  165. mHistoryLast++;
  166. inTxt->getCopy(mHistoryBuf[mHistoryLast], GuiTextCtrl::MAX_STRING_LENGTH);
  167. mHistoryBuf[mHistoryLast][GuiTextCtrl::MAX_STRING_LENGTH] = '\0';
  168. }
  169. if(moveIndex)
  170. mHistoryIndex = mHistoryLast + 1;
  171. }
  172. void GuiTextEditCtrl::getText( char *dest )
  173. {
  174. if ( dest )
  175. mTextBuffer.getCopy8((UTF8*)dest, GuiTextCtrl::MAX_STRING_LENGTH+1);
  176. }
  177. void GuiTextEditCtrl::setText( const UTF8 *txt )
  178. {
  179. if(txt && txt[0] != 0)
  180. {
  181. Parent::setText(txt);
  182. mTextBuffer.set( txt );
  183. }
  184. else
  185. mTextBuffer.set( "" );
  186. //respect the max size
  187. int diff = mTextBuffer.length() - mMaxStrLen;
  188. if( diff > 0 ) {
  189. mTextBuffer.cut( mMaxStrLen, diff );
  190. }
  191. mCursorPos = mTextBuffer.length();
  192. }
  193. void GuiTextEditCtrl::setText( const UTF16* txt)
  194. {
  195. if(txt && txt[0] != 0)
  196. {
  197. UTF8* txt8 = convertUTF16toUTF8( txt );
  198. Parent::setText( txt8 );
  199. delete[] txt8;
  200. mTextBuffer.set( txt );
  201. }
  202. else
  203. {
  204. Parent::setText("");
  205. mTextBuffer.set("");
  206. }
  207. //respect the max size
  208. int diff = mTextBuffer.length() - mMaxStrLen;
  209. if( diff > 0 ) {
  210. mTextBuffer.cut( mMaxStrLen, diff );
  211. }
  212. mCursorPos = mTextBuffer.length();
  213. }
  214. void GuiTextEditCtrl::selectAllText()
  215. {
  216. mBlockStart = 0;
  217. mBlockEnd = mTextBuffer.length();
  218. setUpdate();
  219. }
  220. void GuiTextEditCtrl::forceValidateText()
  221. {
  222. if ( mValidateCommand[0] )
  223. {
  224. char buf[16];
  225. dSprintf(buf, sizeof(buf), "%d", getId());
  226. Con::setVariable("$ThisControl", buf);
  227. Con::evaluate( mValidateCommand, false );
  228. }
  229. }
  230. void GuiTextEditCtrl::reallySetCursorPos( const S32 newPos )
  231. {
  232. S32 charCount = mTextBuffer.length();
  233. S32 realPos = newPos > charCount ? charCount : newPos < 0 ? 0 : newPos;
  234. if ( realPos != mCursorPos )
  235. {
  236. mCursorPos = realPos;
  237. setUpdate();
  238. }
  239. }
  240. S32 GuiTextEditCtrl::setCursorPos( const Point2I &offset )
  241. {
  242. Point2I ctrlOffset = localToGlobalCoord( Point2I( 0, 0 ) );
  243. S32 charLength = 0;
  244. S32 curX;
  245. curX = offset.x - ctrlOffset.x;
  246. setUpdate();
  247. //if the cursor is too far to the left
  248. if ( curX < 0 )
  249. return -1;
  250. //if the cursor is too far to the right
  251. if ( curX >= ctrlOffset.x + mBounds.extent.x )
  252. return -2;
  253. curX = offset.x - mTextOffset.x;
  254. S32 count=0;
  255. if(mTextBuffer.length() == 0)
  256. return 0;
  257. for(count=0; count< (S32)mTextBuffer.length(); count++)
  258. {
  259. UTF16 c = mTextBuffer.getChar(count);
  260. if(!mPasswordText && !mFont->isValidChar(c))
  261. continue;
  262. if(mPasswordText)
  263. charLength += mFont->getCharXIncrement( mPasswordMask[0] );
  264. else
  265. charLength += mFont->getCharXIncrement( c );
  266. if ( charLength > curX )
  267. break;
  268. }
  269. return count;
  270. }
  271. void GuiTextEditCtrl::onMouseDown( const GuiEvent &event )
  272. {
  273. mDragHit = false;
  274. // If we have a double click, select all text. Otherwise
  275. // act as before by clearing any selection.
  276. bool doubleClick = (event.mouseClickCount > 1);
  277. if(doubleClick)
  278. {
  279. selectAllText();
  280. } else
  281. {
  282. //undo any block function
  283. mBlockStart = 0;
  284. mBlockEnd = 0;
  285. }
  286. //find out where the cursor should be
  287. S32 pos = setCursorPos( event.mousePoint );
  288. // if the position is to the left
  289. if ( pos == -1 )
  290. mCursorPos = 0;
  291. else if ( pos == -2 ) //else if the position is to the right
  292. mCursorPos = mTextBuffer.length();
  293. else //else set the mCursorPos
  294. mCursorPos = pos;
  295. //save the mouseDragPos
  296. mMouseDragStart = mCursorPos;
  297. // lock the mouse
  298. mouseLock();
  299. //set the drag var
  300. mDragHit = true;
  301. //let the parent get the event
  302. setFirstResponder();
  303. // Notify Script.
  304. if( isMethod("onMouseDown") )
  305. {
  306. char buf[3][32];
  307. dSprintf(buf[0], 32, "%d", event.modifier);
  308. dSprintf(buf[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
  309. dSprintf(buf[2], 32, "%d", event.mouseClickCount);
  310. Con::executef(this, 4, "onMouseDown", buf[0], buf[1], buf[2]);
  311. }
  312. }
  313. void GuiTextEditCtrl::onMouseDragged( const GuiEvent &event )
  314. {
  315. S32 pos = setCursorPos( event.mousePoint );
  316. // if the position is to the left
  317. if ( pos == -1 )
  318. mScrollDir = -1;
  319. else if ( pos == -2 ) // the position is to the right
  320. mScrollDir = 1;
  321. else // set the new cursor position
  322. {
  323. mScrollDir = 0;
  324. mCursorPos = pos;
  325. }
  326. // update the block:
  327. mBlockStart = getMin( mCursorPos, mMouseDragStart );
  328. mBlockEnd = getMax( mCursorPos, mMouseDragStart );
  329. if ( mBlockStart < 0 )
  330. mBlockStart = 0;
  331. if ( mBlockStart == mBlockEnd )
  332. mBlockStart = mBlockEnd = 0;
  333. //let the parent get the event
  334. Parent::onMouseDragged(event);
  335. // Notify Script.
  336. if( isMethod("onMouseDragged") )
  337. {
  338. char buf[3][32];
  339. dSprintf(buf[0], 32, "%d", event.modifier);
  340. dSprintf(buf[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
  341. dSprintf(buf[2], 32, "%d", event.mouseClickCount);
  342. Con::executef(this, 4, "onMouseDragged", buf[0], buf[1], buf[2]);
  343. }
  344. }
  345. void GuiTextEditCtrl::onMouseUp(const GuiEvent &event)
  346. {
  347. mDragHit = false;
  348. mScrollDir = 0;
  349. mouseUnlock();
  350. // Notify Script.
  351. if( isMethod("onMouseUp") )
  352. {
  353. char buf[3][32];
  354. dSprintf(buf[0], 32, "%d", event.modifier);
  355. dSprintf(buf[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
  356. dSprintf(buf[2], 32, "%d", event.mouseClickCount);
  357. Con::executef(this, 4, "onMouseUp", buf[0], buf[1], buf[2]);
  358. }
  359. }
  360. void GuiTextEditCtrl::saveUndoState()
  361. {
  362. //save the current state
  363. mUndoText.set(&mTextBuffer);
  364. mUndoBlockStart = mBlockStart;
  365. mUndoBlockEnd = mBlockEnd;
  366. mUndoCursorPos = mCursorPos;
  367. }
  368. void GuiTextEditCtrl::onCopy(bool andCut)
  369. {
  370. // Don't copy/cut password field!
  371. if(mPasswordText)
  372. return;
  373. if (mBlockEnd > 0)
  374. {
  375. //save the current state
  376. saveUndoState();
  377. //copy the text to the clipboard
  378. UTF8* clipBuff = mTextBuffer.createSubstring8(mBlockStart, mBlockEnd - mBlockStart);
  379. Platform::setClipboard(clipBuff);
  380. delete[] clipBuff;
  381. //if we pressed the cut shortcut, we need to cut the selected text from the control...
  382. if (andCut)
  383. {
  384. mTextBuffer.cut(mBlockStart, mBlockEnd - mBlockStart);
  385. mCursorPos = mBlockStart;
  386. }
  387. mBlockStart = 0;
  388. mBlockEnd = 0;
  389. }
  390. }
  391. void GuiTextEditCtrl::onPaste()
  392. {
  393. //first, make sure there's something in the clipboard to copy...
  394. const UTF8 *clipboard = Platform::getClipboard();
  395. if(dStrlen(clipboard) <= 0)
  396. return;
  397. //save the current state
  398. saveUndoState();
  399. //delete anything hilited
  400. if (mBlockEnd > 0)
  401. {
  402. mTextBuffer.cut(mBlockStart, mBlockEnd - mBlockStart);
  403. mCursorPos = mBlockStart;
  404. mBlockStart = 0;
  405. mBlockEnd = 0;
  406. }
  407. // We'll be converting to UTF16, and maybe trimming the string,
  408. // so let's use a StringBuffer, for convinience.
  409. StringBuffer pasteText(clipboard);
  410. // Space left after we remove the highlighted text
  411. S32 stringLen = mTextBuffer.length();
  412. // Trim down to fit in a buffer of size mMaxStrLen
  413. S32 pasteLen = pasteText.length();
  414. if(stringLen + pasteLen > mMaxStrLen)
  415. {
  416. pasteLen = mMaxStrLen - stringLen;
  417. pasteText.cut(pasteLen, pasteText.length() - pasteLen);
  418. }
  419. if (mCursorPos == stringLen)
  420. {
  421. mTextBuffer.append(pasteText);
  422. }
  423. else
  424. {
  425. mTextBuffer.insert(mCursorPos, pasteText);
  426. }
  427. mCursorPos += pasteLen;
  428. }
  429. void GuiTextEditCtrl::onUndo()
  430. {
  431. StringBuffer tempBuffer;
  432. S32 tempBlockStart;
  433. S32 tempBlockEnd;
  434. S32 tempCursorPos;
  435. //save the current
  436. tempBuffer.set(&mTextBuffer);
  437. tempBlockStart = mBlockStart;
  438. tempBlockEnd = mBlockEnd;
  439. tempCursorPos = mCursorPos;
  440. //restore the prev
  441. mTextBuffer.set(&mUndoText);
  442. mBlockStart = mUndoBlockStart;
  443. mBlockEnd = mUndoBlockEnd;
  444. mCursorPos = mUndoCursorPos;
  445. //update the undo
  446. mUndoText.set(&tempBuffer);
  447. mUndoBlockStart = tempBlockStart;
  448. mUndoBlockEnd = tempBlockEnd;
  449. mUndoCursorPos = tempCursorPos;
  450. }
  451. bool GuiTextEditCtrl::onKeyDown(const GuiEvent &event)
  452. {
  453. if(! isActive())
  454. return false;
  455. S32 stringLen = mTextBuffer.length();
  456. setUpdate();
  457. // Ugly, but now I'm cool like MarkF.
  458. if(event.keyCode == KEY_BACKSPACE)
  459. goto dealWithBackspace;
  460. if (event.modifier & SI_SHIFT)
  461. {
  462. switch (event.keyCode)
  463. {
  464. case KEY_TAB:
  465. if ( mTabComplete )
  466. {
  467. Con::executef( this, 2, "onTabComplete", "1" );
  468. return( true );
  469. }
  470. break; //*** DAW: We don't want to fall through if we don't handle the TAB here.
  471. case KEY_HOME:
  472. mBlockStart = 0;
  473. mBlockEnd = mCursorPos;
  474. mCursorPos = 0;
  475. return true;
  476. case KEY_END:
  477. mBlockStart = mCursorPos;
  478. mBlockEnd = stringLen;
  479. mCursorPos = stringLen;
  480. return true;
  481. case KEY_LEFT:
  482. if ((mCursorPos > 0) & (stringLen > 0))
  483. {
  484. //if we already have a selected block
  485. if (mCursorPos == mBlockEnd)
  486. {
  487. mCursorPos--;
  488. mBlockEnd--;
  489. if (mBlockEnd == mBlockStart)
  490. {
  491. mBlockStart = 0;
  492. mBlockEnd = 0;
  493. }
  494. }
  495. else {
  496. mCursorPos--;
  497. mBlockStart = mCursorPos;
  498. if (mBlockEnd == 0)
  499. {
  500. mBlockEnd = mCursorPos + 1;
  501. }
  502. }
  503. }
  504. return true;
  505. case KEY_RIGHT:
  506. if (mCursorPos < stringLen)
  507. {
  508. if ((mCursorPos == mBlockStart) && (mBlockEnd > 0))
  509. {
  510. mCursorPos++;
  511. mBlockStart++;
  512. if (mBlockStart == mBlockEnd)
  513. {
  514. mBlockStart = 0;
  515. mBlockEnd = 0;
  516. }
  517. }
  518. else
  519. {
  520. if (mBlockEnd == 0)
  521. {
  522. mBlockStart = mCursorPos;
  523. mBlockEnd = mCursorPos;
  524. }
  525. mCursorPos++;
  526. mBlockEnd++;
  527. }
  528. }
  529. return true;
  530. }
  531. }
  532. else if (event.modifier & SI_CTRL)
  533. {
  534. switch(event.keyCode)
  535. {
  536. // Added UNIX emacs key bindings - just a little hack here...
  537. // BJGTODO: Add vi bindings.
  538. // Ctrl-B - move one character back
  539. case KEY_B:
  540. {
  541. GuiEvent new_event;
  542. new_event.modifier = 0;
  543. new_event.keyCode = KEY_LEFT;
  544. return(onKeyDown(new_event));
  545. }
  546. // Ctrl-F - move one character forward
  547. case KEY_F:
  548. {
  549. GuiEvent new_event;
  550. new_event.modifier = 0;
  551. new_event.keyCode = KEY_RIGHT;
  552. return(onKeyDown(new_event));
  553. }
  554. // Ctrl-A - move to the beginning of the line
  555. case KEY_A:
  556. {
  557. GuiEvent new_event;
  558. new_event.modifier = 0;
  559. new_event.keyCode = KEY_HOME;
  560. return(onKeyDown(new_event));
  561. }
  562. // Ctrl-E - move to the end of the line
  563. case KEY_E:
  564. {
  565. GuiEvent new_event;
  566. new_event.modifier = 0;
  567. new_event.keyCode = KEY_END;
  568. return(onKeyDown(new_event));
  569. }
  570. // Ctrl-P - move backward in history
  571. case KEY_P:
  572. {
  573. GuiEvent new_event;
  574. new_event.modifier = 0;
  575. new_event.keyCode = KEY_UP;
  576. return(onKeyDown(new_event));
  577. }
  578. // Ctrl-N - move forward in history
  579. case KEY_N:
  580. {
  581. GuiEvent new_event;
  582. new_event.modifier = 0;
  583. new_event.keyCode = KEY_DOWN;
  584. return(onKeyDown(new_event));
  585. }
  586. // Ctrl-D - delete under cursor
  587. case KEY_D:
  588. {
  589. GuiEvent new_event;
  590. new_event.modifier = 0;
  591. new_event.keyCode = KEY_DELETE;
  592. return(onKeyDown(new_event));
  593. }
  594. case KEY_U:
  595. {
  596. GuiEvent new_event;
  597. new_event.modifier = SI_CTRL;
  598. new_event.keyCode = KEY_DELETE;
  599. return(onKeyDown(new_event));
  600. }
  601. // End added UNIX emacs key bindings
  602. #if !(defined(TORQUE_OS_OSX) || defined(TORQUE_OS_IOS))
  603. // windows style cut / copy / paste / undo keybinds
  604. case KEY_C:
  605. case KEY_X:
  606. {
  607. // copy, and cut the text if we hit ctrl-x
  608. onCopy( event.keyCode==KEY_X );
  609. return true;
  610. }
  611. case KEY_V:
  612. {
  613. onPaste();
  614. // Execute the console command!
  615. execConsoleCallback();
  616. return true;
  617. }
  618. case KEY_Z:
  619. if (! mDragHit)
  620. {
  621. onUndo();
  622. return true;
  623. }
  624. #endif
  625. case KEY_DELETE:
  626. case KEY_BACKSPACE:
  627. //save the current state
  628. saveUndoState();
  629. //delete everything in the field
  630. mTextBuffer.set("");
  631. mCursorPos = 0;
  632. mBlockStart = 0;
  633. mBlockEnd = 0;
  634. execConsoleCallback();
  635. return true;
  636. // [neo, 5/24/2007 - #2986]
  637. // We don't want to embed control characters in the text, so just return false
  638. // so that any that any other consumer can have a bash at the input.
  639. default:
  640. return false;
  641. }
  642. }
  643. #if (defined(TORQUE_OS_OSX) || defined(TORQUE_OS_IOS))
  644. // mac style cut / copy / paste / undo keybinds
  645. else if (event.modifier & SI_ALT)
  646. {
  647. // Added Mac cut/copy/paste/undo keys
  648. // Mac command key maps to alt in torque.
  649. switch(event.keyCode)
  650. {
  651. case KEY_C:
  652. case KEY_X:
  653. {
  654. // copy, and cut the text if we hit cmd-x
  655. onCopy( event.keyCode==KEY_X );
  656. return true;
  657. }
  658. case KEY_V:
  659. {
  660. onPaste();
  661. // Execute the console command!
  662. execConsoleCallback();
  663. return true;
  664. }
  665. case KEY_Z:
  666. if (! mDragHit)
  667. {
  668. onUndo();
  669. return true;
  670. }
  671. }
  672. }
  673. #endif
  674. else
  675. {
  676. switch(event.keyCode)
  677. {
  678. case KEY_ESCAPE:
  679. if ( mEscapeCommand[0] )
  680. {
  681. Con::evaluate( mEscapeCommand );
  682. return( true );
  683. }
  684. return( Parent::onKeyDown( event ) );
  685. case KEY_RETURN:
  686. case KEY_NUMPADENTER:
  687. //first validate
  688. if (mProfile->mReturnTab)
  689. {
  690. //execute the validate command
  691. if ( mValidateCommand[0] )
  692. {
  693. char buf[16];
  694. dSprintf(buf, sizeof(buf), "%d", getId());
  695. Con::setVariable("$ThisControl", buf);
  696. Con::evaluate( mValidateCommand, false );
  697. }
  698. if( isMethod( "onValidate" ) )
  699. Con::executef( this, 2, "onValidate" );
  700. }
  701. updateHistory(&mTextBuffer, true);
  702. mHistoryDirty = false;
  703. //next exec the alt console command
  704. execAltConsoleCallback();
  705. // Notify of Return
  706. if ( isMethod("onReturn") )
  707. Con::executef( this, 2, "onReturn" );
  708. if (mProfile->mReturnTab)
  709. {
  710. GuiCanvas *root = getRoot();
  711. if (root)
  712. {
  713. root->tabNext();
  714. return true;
  715. }
  716. }
  717. return true;
  718. case KEY_UP:
  719. {
  720. if(mHistoryDirty)
  721. {
  722. updateHistory(&mTextBuffer, false);
  723. mHistoryDirty = false;
  724. }
  725. mHistoryIndex--;
  726. if(mHistoryIndex >= 0 && mHistoryIndex <= mHistoryLast)
  727. setText(mHistoryBuf[mHistoryIndex]);
  728. else if(mHistoryIndex < 0)
  729. mHistoryIndex = 0;
  730. return true;
  731. }
  732. case KEY_DOWN:
  733. if(mHistoryDirty)
  734. {
  735. updateHistory(&mTextBuffer, false);
  736. mHistoryDirty = false;
  737. }
  738. mHistoryIndex++;
  739. if(mHistoryIndex > mHistoryLast)
  740. {
  741. mHistoryIndex = mHistoryLast + 1;
  742. setText("");
  743. }
  744. else
  745. setText(mHistoryBuf[mHistoryIndex]);
  746. return true;
  747. case KEY_LEFT:
  748. mBlockStart = 0;
  749. mBlockEnd = 0;
  750. if (mCursorPos > 0)
  751. {
  752. mCursorPos--;
  753. }
  754. return true;
  755. case KEY_RIGHT:
  756. mBlockStart = 0;
  757. mBlockEnd = 0;
  758. if (mCursorPos < stringLen)
  759. {
  760. mCursorPos++;
  761. }
  762. return true;
  763. case KEY_BACKSPACE:
  764. dealWithBackspace:
  765. //save the current state
  766. saveUndoState();
  767. if (mBlockEnd > 0)
  768. {
  769. mTextBuffer.cut(mBlockStart, mBlockEnd-mBlockStart);
  770. mCursorPos = mBlockStart;
  771. mBlockStart = 0;
  772. mBlockEnd = 0;
  773. mHistoryDirty = true;
  774. // Execute the console command!
  775. execConsoleCallback();
  776. }
  777. else if (mCursorPos > 0)
  778. {
  779. mTextBuffer.cut(mCursorPos-1, 1);
  780. mCursorPos--;
  781. mHistoryDirty = true;
  782. // Execute the console command!
  783. execConsoleCallback();
  784. }
  785. return true;
  786. case KEY_DELETE:
  787. //save the current state
  788. saveUndoState();
  789. if (mBlockEnd > 0)
  790. {
  791. mHistoryDirty = true;
  792. mTextBuffer.cut(mBlockStart, mBlockEnd-mBlockStart);
  793. mCursorPos = mBlockStart;
  794. mBlockStart = 0;
  795. mBlockEnd = 0;
  796. // Execute the console command!
  797. execConsoleCallback();
  798. }
  799. else if (mCursorPos < stringLen)
  800. {
  801. mHistoryDirty = true;
  802. mTextBuffer.cut(mCursorPos, 1);
  803. // Execute the console command!
  804. execConsoleCallback();
  805. }
  806. return true;
  807. case KEY_INSERT:
  808. mInsertOn = !mInsertOn;
  809. return true;
  810. case KEY_HOME:
  811. mBlockStart = 0;
  812. mBlockEnd = 0;
  813. mCursorPos = 0;
  814. return true;
  815. case KEY_END:
  816. mBlockStart = 0;
  817. mBlockEnd = 0;
  818. mCursorPos = stringLen;
  819. return true;
  820. }
  821. }
  822. switch ( event.keyCode )
  823. {
  824. case KEY_TAB:
  825. if ( mTabComplete )
  826. {
  827. Con::executef( this, 2, "onTabComplete", "0" );
  828. return( true );
  829. }
  830. case KEY_UP:
  831. case KEY_DOWN:
  832. case KEY_ESCAPE:
  833. return Parent::onKeyDown( event );
  834. }
  835. if(mFont.isNull())
  836. return false;
  837. if ( mFont->isValidChar( event.ascii ) )
  838. {
  839. // Get the character ready to add to a UTF8 string.
  840. UTF16 convertedChar[2] = { event.ascii, 0 };
  841. //see if it's a number field
  842. if ( mProfile->mNumbersOnly )
  843. {
  844. if (event.ascii == '-')
  845. {
  846. //a minus sign only exists at the beginning, and only a single minus sign
  847. if ( mCursorPos != 0 )
  848. {
  849. playDeniedSound();
  850. return true;
  851. }
  852. if ( mInsertOn && ( mTextBuffer.getChar(0) == '-' ) )
  853. {
  854. playDeniedSound();
  855. return true;
  856. }
  857. }
  858. // BJTODO: This is probably not unicode safe.
  859. else if ( event.ascii < '0' || event.ascii > '9' )
  860. {
  861. playDeniedSound();
  862. return true;
  863. }
  864. }
  865. //save the current state
  866. saveUndoState();
  867. //delete anything highlighted
  868. if ( mBlockEnd > 0 )
  869. {
  870. mTextBuffer.cut(mBlockStart, mBlockEnd-mBlockStart);
  871. mCursorPos = mBlockStart;
  872. mBlockStart = 0;
  873. mBlockEnd = 0;
  874. }
  875. if ( ( mInsertOn && ( stringLen < mMaxStrLen ) ) ||
  876. ( !mInsertOn && ( mCursorPos < mMaxStrLen ) ) )
  877. {
  878. if ( mCursorPos == stringLen )
  879. {
  880. mTextBuffer.append(convertedChar);
  881. mCursorPos++;
  882. }
  883. else
  884. {
  885. if ( mInsertOn )
  886. {
  887. mTextBuffer.insert(mCursorPos, convertedChar);
  888. mCursorPos++;
  889. }
  890. else
  891. {
  892. mTextBuffer.cut(mCursorPos, 1);
  893. mTextBuffer.insert(mCursorPos, convertedChar);
  894. mCursorPos++;
  895. }
  896. }
  897. }
  898. else
  899. playDeniedSound();
  900. //reset the history index
  901. mHistoryDirty = true;
  902. //execute the console command if it exists
  903. execConsoleCallback();
  904. return true;
  905. }
  906. //not handled - pass the event to it's parent
  907. // Or eat it if that's appropriate.
  908. if (mSinkAllKeyEvents)
  909. return true;
  910. return Parent::onKeyDown( event );
  911. }
  912. void GuiTextEditCtrl::setFirstResponder()
  913. {
  914. Parent::setFirstResponder();
  915. #ifndef TORQUE_OS_IOS
  916. Platform::enableKeyboardTranslation();
  917. #endif
  918. }
  919. void GuiTextEditCtrl::onLoseFirstResponder()
  920. {
  921. Platform::disableKeyboardTranslation();
  922. //first, update the history
  923. updateHistory( &mTextBuffer, true );
  924. //execute the validate command
  925. if ( mValidateCommand[0] )
  926. {
  927. char buf[16];
  928. dSprintf(buf, sizeof(buf), "%d", getId());
  929. Con::setVariable("$ThisControl", buf);
  930. Con::evaluate( mValidateCommand, false );
  931. }
  932. if( isMethod( "onValidate" ) )
  933. Con::executef( this, 2, "onValidate" );
  934. if( isMethod( "onLoseFirstResponder" ) )
  935. Con::executef( this, 2, "onLoseFirstResponder" );
  936. // Redraw the control:
  937. setUpdate();
  938. }
  939. void GuiTextEditCtrl::parentResized(const Point2I &oldParentExtent, const Point2I &newParentExtent)
  940. {
  941. Parent::parentResized( oldParentExtent, newParentExtent );
  942. mTextOffsetReset = true;
  943. }
  944. void GuiTextEditCtrl::onRender(Point2I offset, const RectI & updateRect)
  945. {
  946. RectI ctrlRect( offset, mBounds.extent );
  947. //if opaque, fill the update rect with the fill color
  948. if ( mProfile->mOpaque )
  949. {
  950. if(isFirstResponder())
  951. dglDrawRectFill( ctrlRect, mActive ? mProfile->mFillColor : mProfile->mFillColorNA );
  952. else
  953. dglDrawRectFill( ctrlRect, mActive ? mProfile->mFillColor : mProfile->mFillColorNA );
  954. }
  955. //if there's a border, draw the border
  956. if ( mProfile->mBorder )
  957. {
  958. dglSetBitmapModulation( mActive ? mProfile->mFillColor : mProfile->mFillColorNA );
  959. renderBorder( ctrlRect, mProfile );
  960. }
  961. drawText( ctrlRect, isFirstResponder() );
  962. renderChildControls(offset, updateRect);
  963. }
  964. void GuiTextEditCtrl::onPreRender()
  965. {
  966. if ( isFirstResponder() )
  967. {
  968. U32 timeElapsed = Platform::getVirtualMilliseconds() - mTimeLastCursorFlipped;
  969. mNumFramesElapsed++;
  970. if ( ( timeElapsed > 500 ) && ( mNumFramesElapsed > 3 ) )
  971. {
  972. mCursorOn = !mCursorOn;
  973. mTimeLastCursorFlipped = Platform::getVirtualMilliseconds();
  974. mNumFramesElapsed = 0;
  975. setUpdate();
  976. }
  977. //update the cursor if the text is scrolling
  978. if ( mDragHit )
  979. {
  980. if ( ( mScrollDir < 0 ) && ( mCursorPos > 0 ) )
  981. mCursorPos--;
  982. else if ( ( mScrollDir > 0 ) && ( mCursorPos < (S32) mTextBuffer.length() ) )
  983. mCursorPos++;
  984. }
  985. }
  986. }
  987. void GuiTextEditCtrl::drawText( const RectI &drawRect, bool isFocused )
  988. {
  989. StringBuffer textBuffer;
  990. Point2I drawPoint = drawRect.point;
  991. Point2I paddingLeftTop, paddingRightBottom;
  992. // Just a little sanity.
  993. if(mCursorPos > (S32)mTextBuffer.length())
  994. mCursorPos = (S32)mTextBuffer.length();
  995. if(mCursorPos < 0)
  996. mCursorPos = 0;
  997. // Apply password masking (make the masking char optional perhaps?)
  998. if(mPasswordText)
  999. {
  1000. for(U32 i = 0; i<mTextBuffer.length(); i++)
  1001. textBuffer.append(mPasswordMask);
  1002. }
  1003. else
  1004. {
  1005. // Or else just copy it over.
  1006. textBuffer.set(&mTextBuffer);
  1007. }
  1008. paddingLeftTop.set(( mProfile->mTextOffset.x != 0 ? mProfile->mTextOffset.x : 3 ), mProfile->mTextOffset.y);
  1009. paddingRightBottom = paddingLeftTop;
  1010. // Center vertically:
  1011. drawPoint.y += ( ( drawRect.extent.y - paddingLeftTop.y - paddingRightBottom.y - mFont->getHeight() ) / 2 ) + paddingLeftTop.y;
  1012. // Align horizontally:
  1013. S32 textWidth = mFont->getStrNWidth(textBuffer.getPtr(), textBuffer.length());
  1014. if ( drawRect.extent.x - paddingLeftTop.x > textWidth )
  1015. {
  1016. switch( mProfile->mAlignment )
  1017. {
  1018. case GuiControlProfile::RightJustify:
  1019. drawPoint.x += ( drawRect.extent.x - textWidth - paddingRightBottom.x );
  1020. break;
  1021. case GuiControlProfile::CenterJustify:
  1022. drawPoint.x += ( ( drawRect.extent.x - textWidth ) / 2 );
  1023. break;
  1024. default:
  1025. case GuiControlProfile::LeftJustify :
  1026. drawPoint.x += paddingLeftTop.x;
  1027. break;
  1028. }
  1029. }
  1030. else
  1031. drawPoint.x += paddingLeftTop.x;
  1032. ColorI fontColor = mActive ? mProfile->mFontColor : mProfile->mFontColorNA;
  1033. // now draw the text
  1034. Point2I cursorStart, cursorEnd;
  1035. mTextOffset = drawPoint;
  1036. if ( mTextOffsetReset )
  1037. {
  1038. mTextOffset.x = drawPoint.x;
  1039. mTextOffsetReset = false;
  1040. }
  1041. if ( drawRect.extent.x - paddingLeftTop.x > textWidth )
  1042. mTextOffset.x = drawPoint.x;
  1043. else
  1044. {
  1045. // Alignment affects large text
  1046. if ( mProfile->mAlignment == GuiControlProfile::RightJustify
  1047. || mProfile->mAlignment == GuiControlProfile::CenterJustify )
  1048. {
  1049. if ( mTextOffset.x + textWidth < (drawRect.point.x + drawRect.extent.x) - paddingRightBottom.x)
  1050. mTextOffset.x = (drawRect.point.x + drawRect.extent.x) - paddingRightBottom.x - textWidth;
  1051. }
  1052. }
  1053. // calculate the cursor
  1054. if ( isFocused )
  1055. {
  1056. // Where in the string are we?
  1057. S32 cursorOffset=0, charWidth=0;
  1058. UTF16 tempChar = mTextBuffer.getChar(mCursorPos);
  1059. // Alright, we want to terminate things momentarily.
  1060. if(mCursorPos > 0)
  1061. {
  1062. cursorOffset = mFont->getStrNWidth(textBuffer.getPtr(), mCursorPos);
  1063. }
  1064. else
  1065. cursorOffset = 0;
  1066. if ( tempChar )
  1067. charWidth = mFont->getCharWidth( tempChar );
  1068. else
  1069. charWidth = paddingRightBottom.x;
  1070. if( mTextOffset.x + cursorOffset + charWidth >= (drawRect.point.x + drawRect.extent.x) - paddingLeftTop.x )
  1071. {
  1072. // Cursor somewhere beyond the textcontrol,
  1073. // skip forward roughly 25% of the total width (if possible)
  1074. S32 skipForward = drawRect.extent.x / 4;
  1075. if ( cursorOffset + skipForward > textWidth )
  1076. mTextOffset.x = (drawRect.point.x + drawRect.extent.x) - paddingRightBottom.x - textWidth;
  1077. else
  1078. mTextOffset.x -= skipForward;
  1079. }
  1080. else if( mTextOffset.x + cursorOffset < drawRect.point.x + paddingLeftTop.x )
  1081. {
  1082. // Cursor somewhere before the textcontrol
  1083. // skip backward roughly 25% of the total width (if possible)
  1084. S32 skipBackward = drawRect.extent.x / 4;
  1085. if ( cursorOffset - skipBackward < 0 )
  1086. mTextOffset.x = drawRect.point.x + paddingLeftTop.x;
  1087. else
  1088. mTextOffset.x += skipBackward;
  1089. }
  1090. cursorStart.x = mTextOffset.x + cursorOffset;
  1091. cursorEnd.x = cursorStart.x;
  1092. S32 cursorHeight = mFont->getHeight();
  1093. if ( cursorHeight < drawRect.extent.y )
  1094. {
  1095. cursorStart.y = drawPoint.y;
  1096. cursorEnd.y = cursorStart.y + cursorHeight;
  1097. }
  1098. else
  1099. {
  1100. cursorStart.y = drawRect.point.y;
  1101. cursorEnd.y = cursorStart.y + drawRect.extent.y;
  1102. }
  1103. }
  1104. //draw the text
  1105. if ( !isFocused )
  1106. mBlockStart = mBlockEnd = 0;
  1107. //also verify the block start/end
  1108. if ((mBlockStart > (S32)textBuffer.length() || (mBlockEnd > (S32)textBuffer.length()) || (mBlockStart > mBlockEnd)))
  1109. mBlockStart = mBlockEnd = 0;
  1110. Point2I tempOffset = mTextOffset;
  1111. //draw the portion before the highlight
  1112. if ( mBlockStart > 0 )
  1113. {
  1114. dglSetBitmapModulation( fontColor );
  1115. const UTF16* preString2 = textBuffer.getPtr();
  1116. dglDrawTextN( mFont, tempOffset, preString2, mBlockStart, mProfile->mFontColors);
  1117. tempOffset.x += mFont->getStrNWidth(preString2, mBlockStart);
  1118. }
  1119. //draw the hilighted portion
  1120. if ( mBlockEnd > 0 )
  1121. {
  1122. const UTF16* highlightBuff = textBuffer.getPtr() + mBlockStart;
  1123. U32 highlightBuffLen = mBlockEnd-mBlockStart;
  1124. S32 highlightWidth = mFont->getStrNWidth(highlightBuff, highlightBuffLen);
  1125. dglDrawRectFill( Point2I( tempOffset.x, drawRect.point.y + 1 ),
  1126. Point2I( tempOffset.x + highlightWidth, drawRect.point.y + drawRect.extent.y - 1),
  1127. mProfile->mFillColorHL );
  1128. dglSetBitmapModulation( mProfile->mFontColorHL );
  1129. dglDrawTextN( mFont, tempOffset, highlightBuff, highlightBuffLen, mProfile->mFontColors );
  1130. tempOffset.x += highlightWidth;
  1131. }
  1132. //draw the portion after the highlite
  1133. if(mBlockEnd < (S32)mTextBuffer.length())
  1134. {
  1135. // Special handling if we are truncating when the ctrl is unfocused
  1136. if ( !isFocused && mTruncateWhenUnfocused)
  1137. {
  1138. StringBuffer terminationString = "...";
  1139. S32 width = mBounds.extent.x;
  1140. StringBuffer truncatedBuffer = truncate(textBuffer, terminationString, width);
  1141. const UTF16* truncatedBufferPtr = truncatedBuffer.getPtr();
  1142. U32 finalBuffLen = truncatedBuffer.length();
  1143. dglSetBitmapModulation( fontColor );
  1144. dglDrawTextN( mFont, tempOffset, truncatedBufferPtr, finalBuffLen, mProfile->mFontColors );
  1145. }
  1146. else
  1147. {
  1148. const UTF16* finalBuff = textBuffer.getPtr() + mBlockEnd;
  1149. U32 finalBuffLen = mTextBuffer.length() - mBlockEnd;
  1150. dglSetBitmapModulation( fontColor );
  1151. dglDrawTextN( mFont, tempOffset, finalBuff, finalBuffLen, mProfile->mFontColors );
  1152. }
  1153. }
  1154. //draw the cursor
  1155. if ( isFocused && mCursorOn )
  1156. dglDrawLine( cursorStart, cursorEnd, mProfile->mCursorColor );
  1157. }
  1158. bool GuiTextEditCtrl::hasText()
  1159. {
  1160. return (mTextBuffer.length());
  1161. }
  1162. void GuiTextEditCtrl::playDeniedSound()
  1163. {
  1164. if ( mDeniedSound.notNull() )
  1165. {
  1166. AUDIOHANDLE handle = alxCreateSource( mDeniedSound );
  1167. alxPlay( handle );
  1168. }
  1169. }
  1170. const char *GuiTextEditCtrl::getScriptValue()
  1171. {
  1172. return StringTable->insert(mTextBuffer.getPtr8());
  1173. }
  1174. void GuiTextEditCtrl::setScriptValue(const char *value)
  1175. {
  1176. mTextBuffer.set(value);
  1177. mCursorPos = getMin((S32)(mTextBuffer.length() - 1), 0);
  1178. }
  1179. ConsoleMethod( GuiTextEditCtrl, getText, const char*, 2, 2, "() Get the contents of the textedit control\n"
  1180. "@return Returns the current textedit buffer.")
  1181. {
  1182. if( !object->hasText() )
  1183. return StringTable->EmptyString;
  1184. char *retBuffer = Con::getReturnBuffer( GuiTextEditCtrl::MAX_STRING_LENGTH );
  1185. object->getText( retBuffer );
  1186. return retBuffer;
  1187. }
  1188. ConsoleMethod( GuiTextEditCtrl, getCursorPos, S32, 2, 2, "() Use the getCursorPos method to get the current position of the text cursor in the control.\n"
  1189. "@return Returns the current position of the text cursor in the control, where 0 is at the beginning of the line, 1 is after the first letter, and so on.\n"
  1190. "@sa setCursorPos")
  1191. {
  1192. return( object->getCursorPos() );
  1193. }
  1194. ConsoleMethod( GuiTextEditCtrl, setCursorPos, void, 3, 3, "( newPos ) Use the setCursorPos method to set the current position of text cursor to newPos.\n"
  1195. "If the requested position is beyond the end of text, the cursor will be placed after the last letter. If the value is less than zero, the cursor will be placed at the front of the entry.\n"
  1196. "@param newPos The new position to place the cursor at, where 0 is at the beginning of the line, 1 is after the first letter, and so on.\n"
  1197. "@return No return value.\n"
  1198. "@sa getCursorPos")
  1199. {
  1200. object->reallySetCursorPos( dAtoi( argv[2] ) );
  1201. }
  1202. ConsoleMethod( GuiTextEditCtrl, selectAllText, void, 2, 2, "textEditCtrl.selectAllText()" )
  1203. {
  1204. object->selectAllText();
  1205. }
  1206. ConsoleMethod( GuiTextEditCtrl, forceValidateText, void, 2, 2, "textEditCtrl.forceValidateText()" )
  1207. {
  1208. object->forceValidateText();
  1209. }