guiTextEditCtrl.cc 40 KB

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