guiTextEditCtrl.cc 43 KB

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