guiTextEditCtrl.cc 40 KB

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