guiTextEditCtrl.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "platform/platform.h"
  23. #include "gui/controls/guiTextEditCtrl.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/console.h"
  26. #include "gui/core/guiCanvas.h"
  27. #include "gui/controls/guiMLTextCtrl.h"
  28. #include "gui/core/guiDefaultControlRender.h"
  29. #include "gfx/gfxDevice.h"
  30. #include "gfx/gfxDrawUtil.h"
  31. #include "core/frameAllocator.h"
  32. #include "sfx/sfxTrack.h"
  33. #include "sfx/sfxTypes.h"
  34. #include "sfx/sfxSystem.h"
  35. #include "core/strings/unicode.h"
  36. #include "console/engineAPI.h"
  37. IMPLEMENT_CONOBJECT(GuiTextEditCtrl);
  38. ConsoleDocClass( GuiTextEditCtrl,
  39. "@brief A component that places a text entry box on the screen.\n\n"
  40. "Fonts and sizes are changed using profiles. The text value can be set or entered by a user.\n\n"
  41. "@tsexample\n"
  42. " new GuiTextEditCtrl(MessageHud_Edit)\n"
  43. " {\n"
  44. " text = \"Hello World\";\n"
  45. " validate = \"validateCommand();\"\n"
  46. " escapeCommand = \"escapeCommand();\";\n"
  47. " historySize = \"5\";\n"
  48. " tabComplete = \"true\";\n"
  49. " deniedSound = \"DeniedSoundProfile\";\n"
  50. " sinkAllKeyEvents = \"true\";\n"
  51. " password = \"true\";\n"
  52. " passwordMask = \"*\";\n"
  53. " //Properties not specific to this control have been omitted from this example.\n"
  54. " };\n"
  55. "@endtsexample\n\n"
  56. "@see GuiTextCtrl\n"
  57. "@see GuiControl\n\n"
  58. "@ingroup GuiControls\n"
  59. );
  60. IMPLEMENT_CALLBACK( GuiTextEditCtrl, onTabComplete, void, (const char* val),( val ),
  61. "@brief Called if tabComplete is true, and the 'tab' key is pressed.\n\n"
  62. "@param val Input to mimick the '1' sent by the actual tab key button press.\n"
  63. "@tsexample\n"
  64. "// Tab key has been pressed, causing the callback to occur.\n"
  65. "GuiTextEditCtrl::onTabComplete(%this,%val)\n"
  66. " {\n"
  67. " //Code to run when the onTabComplete callback occurs\n"
  68. " }\n"
  69. "@endtsexample\n\n"
  70. "@see GuiTextCtrl\n"
  71. "@see GuiControl\n\n"
  72. );
  73. IMPLEMENT_CALLBACK( GuiTextEditCtrl, onReturn, void, (),(),
  74. "@brief Called when the 'Return' or 'Enter' key is pressed.\n\n"
  75. "@tsexample\n"
  76. "// Return or Enter key was pressed, causing the callback to occur.\n"
  77. "GuiTextEditCtrl::onReturn(%this)\n"
  78. " {\n"
  79. " // Code to run when the onReturn callback occurs\n"
  80. " }\n"
  81. "@endtsexample\n\n"
  82. "@see GuiTextCtrl\n"
  83. "@see GuiControl\n\n"
  84. );
  85. IMPLEMENT_CALLBACK( GuiTextEditCtrl, onValidate, void, (),(),
  86. "@brief Called whenever the control is validated.\n\n"
  87. "@tsexample\n"
  88. "// The control gets validated, causing the callback to occur\n"
  89. "GuiTextEditCtrl::onValidated(%this)\n"
  90. " {\n"
  91. " // Code to run when the control is validated\n"
  92. " }\n"
  93. "@endtsexample\n\n"
  94. "@see GuiTextCtrl\n"
  95. "@see GuiControl\n\n"
  96. );
  97. GuiTextEditCtrl::GuiTextEditCtrl()
  98. {
  99. mInsertOn = true;
  100. mBlockStart = 0;
  101. mBlockEnd = 0;
  102. mCursorPos = 0;
  103. mCursorOn = false;
  104. mNumFramesElapsed = 0;
  105. mDragHit = false;
  106. mTabComplete = false;
  107. mScrollDir = 0;
  108. mUndoBlockStart = 0;
  109. mUndoBlockEnd = 0;
  110. mUndoCursorPos = 0;
  111. mPasswordText = false;
  112. mSinkAllKeyEvents = false;
  113. mActive = true;
  114. mTextValid = true;
  115. mTextOffsetReset = true;
  116. mHistoryDirty = false;
  117. mHistorySize = 0;
  118. mHistoryLast = -1;
  119. mHistoryIndex = 0;
  120. mHistoryBuf = NULL;
  121. mDoubleClickTimeMS = 50;
  122. mMouseUpTime = 0;
  123. mPlaceholderText = StringTable->EmptyString();
  124. #if defined(__MACOSX__)
  125. UTF8 bullet[4] = { UTF8(0xE2), UTF8(0x80), UTF8(0xA2), 0 };
  126. mPasswordMask = StringTable->insert( bullet );
  127. #else
  128. mPasswordMask = StringTable->insert( "*" );
  129. #endif
  130. Sim::findObject( "InputDeniedSound", mDeniedSound );
  131. mValidateCommand = "";
  132. }
  133. GuiTextEditCtrl::~GuiTextEditCtrl()
  134. {
  135. //delete the history buffer if it exists
  136. if (mHistoryBuf)
  137. {
  138. for (S32 i = 0; i < mHistorySize; i++)
  139. delete [] mHistoryBuf[i];
  140. delete [] mHistoryBuf;
  141. }
  142. }
  143. void GuiTextEditCtrl::initPersistFields()
  144. {
  145. docsURL;
  146. addProtectedField("placeholderText", TypeCaseString, Offset(mPlaceholderText, GuiTextEditCtrl), setPlaceholderText, getPlaceholderText,
  147. "The text to show on the control.");
  148. addGroup( "Text Input" );
  149. addField("validate", TypeRealString,Offset(mValidateCommand, GuiTextEditCtrl), "Script command to be called when the first validater is lost.\n");
  150. addField("escapeCommand", TypeRealString,Offset(mEscapeCommand, GuiTextEditCtrl), "Script command to be called when the Escape key is pressed.\n");
  151. addField("historySize", TypeS32, Offset(mHistorySize, GuiTextEditCtrl), "How large of a history buffer to maintain.\n");
  152. addField("tabComplete", TypeBool, Offset(mTabComplete, GuiTextEditCtrl), "If true, when the 'tab' key is pressed, it will act as if the Enter key was pressed on the control.\n");
  153. addField("deniedSound", TypeSFXTrackName, Offset(mDeniedSound, GuiTextEditCtrl), "If the attempted text cannot be entered, this sound effect will be played.\n");
  154. addField("sinkAllKeyEvents", TypeBool, Offset(mSinkAllKeyEvents, GuiTextEditCtrl), "If true, every key event will act as if the Enter key was pressed.\n");
  155. addField("password", TypeBool, Offset(mPasswordText, GuiTextEditCtrl), "If true, all characters entered will be stored in the control, however will display as the character stored in passwordMask.\n");
  156. addField("passwordMask", TypeString, Offset(mPasswordMask, GuiTextEditCtrl), "If 'password' is true, this is the character that will be used to mask the characters in the control.\n");
  157. endGroup( "Text Input" );
  158. Parent::initPersistFields();
  159. }
  160. bool GuiTextEditCtrl::onAdd()
  161. {
  162. if ( ! Parent::onAdd() )
  163. return false;
  164. //create the history buffer
  165. if ( mHistorySize > 0 )
  166. {
  167. mHistoryBuf = new UTF16*[mHistorySize];
  168. for ( S32 i = 0; i < mHistorySize; i++ )
  169. {
  170. mHistoryBuf[i] = new UTF16[GuiTextCtrl::MAX_STRING_LENGTH + 1];
  171. mHistoryBuf[i][0] = '\0';
  172. }
  173. }
  174. if( mText && mText[0] )
  175. {
  176. setText(mText);
  177. }
  178. return true;
  179. }
  180. void GuiTextEditCtrl::onStaticModified(const char* slotName, const char* newValue)
  181. {
  182. if(!dStricmp(slotName, "text"))
  183. setText(mText);
  184. }
  185. void GuiTextEditCtrl::execConsoleCallback()
  186. {
  187. // Execute the console command!
  188. Parent::execConsoleCallback();
  189. // Update the console variable:
  190. if ( mConsoleVariable[0] )
  191. Con::setVariable(mConsoleVariable, mTextBuffer.getPtr8());
  192. }
  193. void GuiTextEditCtrl::updateHistory( StringBuffer *inTxt, bool moveIndex )
  194. {
  195. if(!mHistorySize)
  196. return;
  197. const UTF16* txt = inTxt->getPtr();
  198. // Reject empty strings.
  199. if( !txt || !txt[ 0 ] )
  200. return;
  201. // see if it's already in
  202. if(mHistoryLast == -1 || String::compare(txt, mHistoryBuf[mHistoryLast]))
  203. {
  204. if(mHistoryLast == mHistorySize-1) // we're at the history limit... shuffle the pointers around:
  205. {
  206. UTF16 *first = mHistoryBuf[0];
  207. for(U32 i = 0; i < mHistorySize - 1; i++)
  208. mHistoryBuf[i] = mHistoryBuf[i+1];
  209. mHistoryBuf[mHistorySize-1] = first;
  210. if(mHistoryIndex > 0)
  211. mHistoryIndex--;
  212. }
  213. else
  214. mHistoryLast++;
  215. inTxt->getCopy(mHistoryBuf[mHistoryLast], GuiTextCtrl::MAX_STRING_LENGTH);
  216. mHistoryBuf[mHistoryLast][GuiTextCtrl::MAX_STRING_LENGTH] = '\0';
  217. }
  218. if(moveIndex)
  219. mHistoryIndex = mHistoryLast + 1;
  220. }
  221. void GuiTextEditCtrl::getText( char *dest )
  222. {
  223. if ( dest )
  224. mTextBuffer.getCopy8((UTF8*)dest, GuiTextCtrl::MAX_STRING_LENGTH+1);
  225. }
  226. void GuiTextEditCtrl::getRenderText(char *dest)
  227. {
  228. getText( dest );
  229. }
  230. void GuiTextEditCtrl::setText( const UTF8 *txt )
  231. {
  232. if(txt && txt[0] != 0)
  233. {
  234. Parent::setText(txt);
  235. mTextBuffer.set( txt );
  236. }
  237. else
  238. mTextBuffer.set( "" );
  239. mCursorPos = mTextBuffer.length();
  240. }
  241. void GuiTextEditCtrl::setText( const UTF16* txt)
  242. {
  243. if(txt && txt[0] != 0)
  244. {
  245. UTF8* txt8 = createUTF8string( txt );
  246. Parent::setText( txt8 );
  247. delete[] txt8;
  248. mTextBuffer.set( txt );
  249. }
  250. else
  251. {
  252. Parent::setText("");
  253. mTextBuffer.set("");
  254. }
  255. mCursorPos = mTextBuffer.length();
  256. }
  257. bool GuiTextEditCtrl::isAllTextSelected()
  258. {
  259. if( mBlockStart == 0 && mBlockEnd == mTextBuffer.length() )
  260. return true;
  261. else
  262. return false;
  263. }
  264. void GuiTextEditCtrl::selectAllText()
  265. {
  266. mBlockStart = 0;
  267. mBlockEnd = mTextBuffer.length();
  268. setUpdate();
  269. }
  270. void GuiTextEditCtrl::clearSelectedText()
  271. {
  272. mBlockStart = 0;
  273. mBlockEnd = 0;
  274. setUpdate();
  275. }
  276. void GuiTextEditCtrl::forceValidateText()
  277. {
  278. if( mValidateCommand.isNotEmpty() )
  279. evaluate( mValidateCommand );
  280. }
  281. void GuiTextEditCtrl::setCursorPos( const S32 newPos )
  282. {
  283. S32 charCount = mTextBuffer.length();
  284. S32 realPos = newPos > charCount ? charCount : newPos < 0 ? 0 : newPos;
  285. if ( realPos != mCursorPos )
  286. {
  287. mCursorPos = realPos;
  288. setUpdate();
  289. }
  290. }
  291. S32 GuiTextEditCtrl::calculateCursorPos( const Point2I &globalPos )
  292. {
  293. Point2I ctrlOffset = localToGlobalCoord( Point2I( 0, 0 ) );
  294. S32 charLength = 0;
  295. S32 curX;
  296. curX = globalPos.x - ctrlOffset.x;
  297. setUpdate();
  298. //if the cursor is too far to the left
  299. if ( curX < 0 )
  300. return -1;
  301. //if the cursor is too far to the right
  302. if ( curX >= ctrlOffset.x + getExtent().x )
  303. return -2;
  304. curX = globalPos.x - mTextOffset.x;
  305. S32 count=0;
  306. if(mTextBuffer.length() == 0)
  307. return 0;
  308. for(count=0; count<mTextBuffer.length(); count++)
  309. {
  310. UTF16 c = mTextBuffer.getChar(count);
  311. if(!mPasswordText && !mProfile->mFont->isValidChar(c))
  312. continue;
  313. if(mPasswordText)
  314. charLength += mProfile->mFont->getCharXIncrement( mPasswordMask[0] );
  315. else
  316. charLength += mProfile->mFont->getCharXIncrement( c );
  317. if ( charLength > curX )
  318. break;
  319. }
  320. return count;
  321. }
  322. void GuiTextEditCtrl::onMouseDown( const GuiEvent &event )
  323. {
  324. if(!isActive())
  325. return;
  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 && Platform::getRealMilliseconds() - mMouseUpTime > mDoubleClickTimeMS);
  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 = calculateCursorPos( 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. }
  357. void GuiTextEditCtrl::onMouseDragged( const GuiEvent &event )
  358. {
  359. S32 pos = calculateCursorPos( event.mousePoint );
  360. // if the position is to the left
  361. if ( pos == -1 )
  362. mScrollDir = -1;
  363. else if ( pos == -2 ) // the position is to the right
  364. mScrollDir = 1;
  365. else // set the new cursor position
  366. {
  367. mScrollDir = 0;
  368. mCursorPos = pos;
  369. }
  370. // update the block:
  371. mBlockStart = getMin( mCursorPos, mMouseDragStart );
  372. mBlockEnd = getMax( mCursorPos, mMouseDragStart );
  373. if ( mBlockStart < 0 )
  374. mBlockStart = 0;
  375. if ( mBlockStart == mBlockEnd )
  376. mBlockStart = mBlockEnd = 0;
  377. //let the parent get the event
  378. Parent::onMouseDragged(event);
  379. }
  380. void GuiTextEditCtrl::onMouseUp(const GuiEvent &event)
  381. {
  382. TORQUE_UNUSED(event);
  383. mDragHit = false;
  384. mScrollDir = 0;
  385. mMouseUpTime = Platform::getRealMilliseconds();
  386. mouseUnlock();
  387. }
  388. void GuiTextEditCtrl::saveUndoState()
  389. {
  390. //save the current state
  391. mUndoText.set(&mTextBuffer);
  392. mUndoBlockStart = mBlockStart;
  393. mUndoBlockEnd = mBlockEnd;
  394. mUndoCursorPos = mCursorPos;
  395. }
  396. void GuiTextEditCtrl::onCopy(bool andCut)
  397. {
  398. // Don't copy/cut password field!
  399. if(mPasswordText)
  400. return;
  401. if (mBlockEnd > 0)
  402. {
  403. //save the current state
  404. saveUndoState();
  405. //copy the text to the clipboard
  406. UTF8* clipBuff = mTextBuffer.createSubstring8(mBlockStart, mBlockEnd - mBlockStart);
  407. Platform::setClipboard(clipBuff);
  408. delete[] clipBuff;
  409. //if we pressed the cut shortcut, we need to cut the selected text from the control...
  410. if (andCut)
  411. {
  412. mTextBuffer.cut(mBlockStart, mBlockEnd - mBlockStart);
  413. mCursorPos = mBlockStart;
  414. }
  415. mBlockStart = 0;
  416. mBlockEnd = 0;
  417. }
  418. }
  419. void GuiTextEditCtrl::onPaste()
  420. {
  421. //first, make sure there's something in the clipboard to copy...
  422. const UTF8 *clipboard = Platform::getClipboard();
  423. if(dStrlen(clipboard) <= 0)
  424. return;
  425. //save the current state
  426. saveUndoState();
  427. //delete anything hilited
  428. if (mBlockEnd > 0)
  429. {
  430. mTextBuffer.cut(mBlockStart, mBlockEnd - mBlockStart);
  431. mCursorPos = mBlockStart;
  432. mBlockStart = 0;
  433. mBlockEnd = 0;
  434. }
  435. // We'll be converting to UTF16, and maybe trimming the string,
  436. // so let's use a StringBuffer, for convinience.
  437. StringBuffer pasteText(clipboard);
  438. // Space left after we remove the highlighted text
  439. S32 stringLen = mTextBuffer.length();
  440. // Trim down to fit in a buffer of size mMaxStrLen
  441. S32 pasteLen = pasteText.length();
  442. if(stringLen + pasteLen > mMaxStrLen)
  443. {
  444. pasteLen = mMaxStrLen - stringLen;
  445. pasteText.cut(pasteLen, pasteText.length() - pasteLen);
  446. }
  447. if (mCursorPos == stringLen)
  448. {
  449. mTextBuffer.append(pasteText);
  450. }
  451. else
  452. {
  453. mTextBuffer.insert(mCursorPos, pasteText);
  454. }
  455. mCursorPos += pasteLen;
  456. }
  457. void GuiTextEditCtrl::onUndo()
  458. {
  459. StringBuffer tempBuffer;
  460. S32 tempBlockStart;
  461. S32 tempBlockEnd;
  462. S32 tempCursorPos;
  463. //save the current
  464. tempBuffer.set(&mTextBuffer);
  465. tempBlockStart = mBlockStart;
  466. tempBlockEnd = mBlockEnd;
  467. tempCursorPos = mCursorPos;
  468. //restore the prev
  469. mTextBuffer.set(&mUndoText);
  470. mBlockStart = mUndoBlockStart;
  471. mBlockEnd = mUndoBlockEnd;
  472. mCursorPos = mUndoCursorPos;
  473. //update the undo
  474. mUndoText.set(&tempBuffer);
  475. mUndoBlockStart = tempBlockStart;
  476. mUndoBlockEnd = tempBlockEnd;
  477. mUndoCursorPos = tempCursorPos;
  478. }
  479. bool GuiTextEditCtrl::onKeyDown(const GuiEvent &event)
  480. {
  481. if ( !isActive() || !isAwake() )
  482. return false;
  483. S32 stringLen = mTextBuffer.length();
  484. setUpdate();
  485. // Ugly, but now I'm cool like MarkF.
  486. if(event.keyCode == KEY_BACKSPACE)
  487. goto dealWithBackspace;
  488. if ( event.modifier & SI_SHIFT )
  489. {
  490. // Added support for word jump selection.
  491. if ( event.modifier & SI_CTRL )
  492. {
  493. switch ( event.keyCode )
  494. {
  495. case KEY_LEFT:
  496. {
  497. S32 newpos = findPrevWord();
  498. if ( mBlockStart == mBlockEnd )
  499. {
  500. // There was not already a selection so start a new one.
  501. mBlockStart = newpos;
  502. mBlockEnd = mCursorPos;
  503. }
  504. else
  505. {
  506. // There was a selection already...
  507. // In this case the cursor MUST be at either the
  508. // start or end of that selection.
  509. if ( mCursorPos == mBlockStart )
  510. {
  511. // We are at the start block and traveling left so
  512. // just extend the start block farther left.
  513. mBlockStart = newpos;
  514. }
  515. else
  516. {
  517. // We are at the end block BUT traveling left
  518. // back towards the start block...
  519. if ( newpos > mBlockStart )
  520. {
  521. // We haven't overpassed the existing start block
  522. // so just trim back the end block.
  523. mBlockEnd = newpos;
  524. }
  525. else if ( newpos == mBlockStart )
  526. {
  527. // We are back at the start, so no more selection.
  528. mBlockEnd = mBlockStart = 0;
  529. }
  530. else
  531. {
  532. // Only other option, we just backtracked PAST
  533. // our original start block.
  534. // So the new position becomes the start block
  535. // and the old start block becomes the end block.
  536. mBlockEnd = mBlockStart;
  537. mBlockStart = newpos;
  538. }
  539. }
  540. }
  541. mCursorPos = newpos;
  542. return true;
  543. }
  544. case KEY_RIGHT:
  545. {
  546. S32 newpos = findNextWord();
  547. if ( mBlockStart == mBlockEnd )
  548. {
  549. // There was not already a selection so start a new one.
  550. mBlockStart = mCursorPos;
  551. mBlockEnd = newpos;
  552. }
  553. else
  554. {
  555. // There was a selection already...
  556. // In this case the cursor MUST be at either the
  557. // start or end of that selection.
  558. if ( mCursorPos == mBlockEnd )
  559. {
  560. // We are at the end block and traveling right so
  561. // just extend the end block farther right.
  562. mBlockEnd = newpos;
  563. }
  564. else
  565. {
  566. // We are at the start block BUT traveling right
  567. // back towards the end block...
  568. if ( newpos < mBlockEnd )
  569. {
  570. // We haven't overpassed the existing end block
  571. // so just trim back the start block.
  572. mBlockStart = newpos;
  573. }
  574. else if ( newpos == mBlockEnd )
  575. {
  576. // We are back at the end, so no more selection.
  577. mBlockEnd = mBlockStart = 0;
  578. }
  579. else
  580. {
  581. // Only other option, we just backtracked PAST
  582. // our original end block.
  583. // So the new position becomes the end block
  584. // and the old end block becomes the start block.
  585. mBlockStart = mBlockEnd;
  586. mBlockEnd = newpos;
  587. }
  588. }
  589. }
  590. mCursorPos = newpos;
  591. return true;
  592. }
  593. default:
  594. break;
  595. }
  596. }
  597. // End support for word jump selection.
  598. switch ( event.keyCode )
  599. {
  600. case KEY_TAB:
  601. if ( mTabComplete )
  602. {
  603. onTabComplete_callback("1");
  604. return true;
  605. }
  606. break; // We don't want to fall through if we don't handle the TAB here.
  607. case KEY_HOME:
  608. mBlockStart = 0;
  609. mBlockEnd = mCursorPos;
  610. mCursorPos = 0;
  611. return true;
  612. case KEY_END:
  613. mBlockStart = mCursorPos;
  614. mBlockEnd = stringLen;
  615. mCursorPos = stringLen;
  616. return true;
  617. case KEY_LEFT:
  618. if ((mCursorPos > 0) & (stringLen > 0))
  619. {
  620. //if we already have a selected block
  621. if (mCursorPos == mBlockEnd)
  622. {
  623. mCursorPos--;
  624. mBlockEnd--;
  625. if (mBlockEnd == mBlockStart)
  626. {
  627. mBlockStart = 0;
  628. mBlockEnd = 0;
  629. }
  630. }
  631. else {
  632. mCursorPos--;
  633. mBlockStart = mCursorPos;
  634. if (mBlockEnd == 0)
  635. {
  636. mBlockEnd = mCursorPos + 1;
  637. }
  638. }
  639. }
  640. return true;
  641. case KEY_RIGHT:
  642. if (mCursorPos < stringLen)
  643. {
  644. if ((mCursorPos == mBlockStart) && (mBlockEnd > 0))
  645. {
  646. mCursorPos++;
  647. mBlockStart++;
  648. if (mBlockStart == mBlockEnd)
  649. {
  650. mBlockStart = 0;
  651. mBlockEnd = 0;
  652. }
  653. }
  654. else
  655. {
  656. if (mBlockEnd == 0)
  657. {
  658. mBlockStart = mCursorPos;
  659. mBlockEnd = mCursorPos;
  660. }
  661. mCursorPos++;
  662. mBlockEnd++;
  663. }
  664. }
  665. return true;
  666. case KEY_RETURN:
  667. case KEY_NUMPADENTER:
  668. return dealWithEnter(false);
  669. default:
  670. break;
  671. }
  672. }
  673. else if (event.modifier & SI_CTRL)
  674. {
  675. switch(event.keyCode)
  676. {
  677. // Adding word jump navigation.
  678. case KEY_LEFT:
  679. {
  680. mCursorPos = findPrevWord();
  681. mBlockStart = 0;
  682. mBlockEnd = 0;
  683. return true;
  684. }
  685. case KEY_RIGHT:
  686. {
  687. mCursorPos = findNextWord();
  688. mBlockStart = 0;
  689. mBlockEnd = 0;
  690. return true;
  691. }
  692. // Select all
  693. case KEY_A:
  694. {
  695. selectAllText();
  696. return true;
  697. }
  698. // windows style cut / copy / paste / undo keybinds
  699. case KEY_C:
  700. case KEY_X:
  701. {
  702. // copy, and cut the text if we hit ctrl-x
  703. onCopy( event.keyCode==KEY_X );
  704. return true;
  705. }
  706. case KEY_V:
  707. {
  708. onPaste();
  709. // Execute the console command!
  710. execConsoleCallback();
  711. return true;
  712. }
  713. case KEY_Z:
  714. if (! mDragHit)
  715. {
  716. onUndo();
  717. return true;
  718. }
  719. case KEY_DELETE:
  720. case KEY_BACKSPACE:
  721. //save the current state
  722. saveUndoState();
  723. //delete everything in the field
  724. mTextBuffer.set("");
  725. mCursorPos = 0;
  726. mBlockStart = 0;
  727. mBlockEnd = 0;
  728. execConsoleCallback();
  729. return true;
  730. default:
  731. break;
  732. }
  733. }
  734. #if defined(TORQUE_OS_MAC)
  735. // mac style cut / copy / paste / undo keybinds
  736. else if (event.modifier & SI_ALT)
  737. {
  738. // Mac command key maps to alt in torque.
  739. // Added Mac cut/copy/paste/undo keys
  740. switch(event.keyCode)
  741. {
  742. // Select all
  743. case KEY_A:
  744. {
  745. selectAllText();
  746. return true;
  747. }
  748. case KEY_C:
  749. case KEY_X:
  750. {
  751. // copy, and cut the text if we hit cmd-x
  752. onCopy( event.keyCode==KEY_X );
  753. return true;
  754. }
  755. case KEY_V:
  756. {
  757. onPaste();
  758. // Execute the console command!
  759. execConsoleCallback();
  760. return true;
  761. }
  762. case KEY_Z:
  763. if (! mDragHit)
  764. {
  765. onUndo();
  766. return true;
  767. }
  768. default:
  769. break;
  770. }
  771. }
  772. #endif
  773. else
  774. {
  775. switch(event.keyCode)
  776. {
  777. case KEY_ESCAPE:
  778. if( mEscapeCommand.isNotEmpty() )
  779. {
  780. evaluate( mEscapeCommand );
  781. return( true );
  782. }
  783. return( Parent::onKeyDown( event ) );
  784. case KEY_RETURN:
  785. case KEY_NUMPADENTER:
  786. return dealWithEnter(true);
  787. case KEY_UP:
  788. {
  789. if( mHistorySize > 0 )
  790. {
  791. if(mHistoryDirty)
  792. {
  793. updateHistory(&mTextBuffer, false);
  794. mHistoryDirty = false;
  795. }
  796. mHistoryIndex--;
  797. if(mHistoryIndex >= 0 && mHistoryIndex <= mHistoryLast)
  798. setText(mHistoryBuf[mHistoryIndex]);
  799. else if(mHistoryIndex < 0)
  800. mHistoryIndex = 0;
  801. }
  802. return true;
  803. }
  804. case KEY_DOWN:
  805. {
  806. if( mHistorySize > 0 )
  807. {
  808. if(mHistoryDirty)
  809. {
  810. updateHistory(&mTextBuffer, false);
  811. mHistoryDirty = false;
  812. }
  813. mHistoryIndex++;
  814. if(mHistoryIndex > mHistoryLast)
  815. {
  816. mHistoryIndex = mHistoryLast + 1;
  817. setText("");
  818. }
  819. else
  820. setText(mHistoryBuf[mHistoryIndex]);
  821. }
  822. return true;
  823. }
  824. case KEY_LEFT:
  825. // If we have a selection put the cursor to the left side of it.
  826. if ( mBlockStart != mBlockEnd )
  827. {
  828. mCursorPos = mBlockStart;
  829. mBlockStart = mBlockEnd = 0;
  830. }
  831. else
  832. {
  833. mBlockStart = mBlockEnd = 0;
  834. mCursorPos = getMax( mCursorPos - 1, 0 );
  835. }
  836. return true;
  837. case KEY_RIGHT:
  838. // If we have a selection put the cursor to the right side of it.
  839. if ( mBlockStart != mBlockEnd )
  840. {
  841. mCursorPos = mBlockEnd;
  842. mBlockStart = mBlockEnd = 0;
  843. }
  844. else
  845. {
  846. mBlockStart = mBlockEnd = 0;
  847. mCursorPos = getMin( mCursorPos + 1, stringLen );
  848. }
  849. return true;
  850. case KEY_BACKSPACE:
  851. dealWithBackspace:
  852. //save the current state
  853. saveUndoState();
  854. if (mBlockEnd > 0)
  855. {
  856. mTextBuffer.cut(mBlockStart, mBlockEnd-mBlockStart);
  857. mCursorPos = mBlockStart;
  858. mBlockStart = 0;
  859. mBlockEnd = 0;
  860. mHistoryDirty = true;
  861. // Execute the console command!
  862. execConsoleCallback();
  863. }
  864. else if (mCursorPos > 0)
  865. {
  866. mTextBuffer.cut(mCursorPos-1, 1);
  867. mCursorPos--;
  868. mHistoryDirty = true;
  869. // Execute the console command!
  870. execConsoleCallback();
  871. }
  872. return true;
  873. case KEY_DELETE:
  874. //save the current state
  875. saveUndoState();
  876. if (mBlockEnd > 0)
  877. {
  878. mHistoryDirty = true;
  879. mTextBuffer.cut(mBlockStart, mBlockEnd-mBlockStart);
  880. mCursorPos = mBlockStart;
  881. mBlockStart = 0;
  882. mBlockEnd = 0;
  883. // Execute the console command!
  884. execConsoleCallback();
  885. }
  886. else if (mCursorPos < stringLen)
  887. {
  888. mHistoryDirty = true;
  889. mTextBuffer.cut(mCursorPos, 1);
  890. // Execute the console command!
  891. execConsoleCallback();
  892. }
  893. return true;
  894. case KEY_INSERT:
  895. mInsertOn = !mInsertOn;
  896. return true;
  897. case KEY_HOME:
  898. mBlockStart = 0;
  899. mBlockEnd = 0;
  900. mCursorPos = 0;
  901. return true;
  902. case KEY_END:
  903. mBlockStart = 0;
  904. mBlockEnd = 0;
  905. mCursorPos = stringLen;
  906. return true;
  907. default:
  908. break;
  909. }
  910. }
  911. switch ( event.keyCode )
  912. {
  913. case KEY_TAB:
  914. if ( mTabComplete )
  915. {
  916. onTabComplete_callback("0");
  917. return( true );
  918. }
  919. case KEY_UP:
  920. case KEY_DOWN:
  921. case KEY_ESCAPE:
  922. return Parent::onKeyDown( event );
  923. default:
  924. break;
  925. }
  926. // Handle character input events.
  927. if( mProfile->mFont->isValidChar( event.ascii ) )
  928. {
  929. handleCharInput( event.ascii );
  930. return true;
  931. }
  932. // Or eat it if that's appropriate.
  933. if( mSinkAllKeyEvents )
  934. return true;
  935. // Not handled - pass the event to it's parent.
  936. return Parent::onKeyDown( event );
  937. }
  938. bool GuiTextEditCtrl::dealWithEnter( bool clearResponder )
  939. {
  940. //first validate
  941. if (mProfile->mReturnTab)
  942. {
  943. onLoseFirstResponder();
  944. }
  945. updateHistory(&mTextBuffer, true);
  946. mHistoryDirty = false;
  947. //next exec the alt console command
  948. execAltConsoleCallback();
  949. // Notify of Return
  950. onReturn_callback();
  951. if (mProfile->mReturnTab)
  952. {
  953. GuiCanvas *root = getRoot();
  954. if (root)
  955. {
  956. root->tabNext();
  957. return true;
  958. }
  959. }
  960. if( clearResponder )
  961. clearFirstResponder();
  962. return true;
  963. }
  964. void GuiTextEditCtrl::setFirstResponder()
  965. {
  966. Parent::setFirstResponder();
  967. GuiCanvas *root = getRoot();
  968. if (root != NULL)
  969. {
  970. root->enableKeyboardTranslation();
  971. // If the native OS accelerator keys are not disabled
  972. // then some key events like Delete, ctrl+V, etc may
  973. // not make it down to us.
  974. root->setNativeAcceleratorsEnabled( false );
  975. }
  976. }
  977. void GuiTextEditCtrl::onLoseFirstResponder()
  978. {
  979. GuiCanvas *root = getRoot();
  980. if( root )
  981. {
  982. root->setNativeAcceleratorsEnabled( true );
  983. root->disableKeyboardTranslation();
  984. }
  985. updateHistory(&mTextBuffer, true);
  986. mHistoryDirty = false;
  987. //execute the validate command
  988. if( mValidateCommand.isNotEmpty() )
  989. evaluate( mValidateCommand.c_str() );
  990. onValidate_callback();
  991. // Redraw the control:
  992. setUpdate();
  993. // Lost Responder
  994. Parent::onLoseFirstResponder();
  995. }
  996. void GuiTextEditCtrl::onRender( Point2I offset, const RectI &updateRect )
  997. {
  998. RectI ctrlRect( offset, getExtent() );
  999. //if opaque, fill the update rect with the fill color
  1000. if ( mProfile->mOpaque )
  1001. {
  1002. if ( !mTextValid )
  1003. GFX->getDrawUtil()->drawRectFill( ctrlRect, mProfile->mFillColorERR );
  1004. else if ( isFirstResponder() )
  1005. GFX->getDrawUtil()->drawRectFill( ctrlRect, mProfile->mFillColorHL );
  1006. else
  1007. GFX->getDrawUtil()->drawRectFill( ctrlRect, mProfile->mFillColor );
  1008. }
  1009. //if there's a border, draw the border
  1010. if ( mProfile->mBorder )
  1011. {
  1012. renderBorder( ctrlRect, mProfile );
  1013. if ( !mTextValid )
  1014. GFX->getDrawUtil()->drawRectFill( ctrlRect, mProfile->mFillColorERR );
  1015. }
  1016. drawText( ctrlRect, isFirstResponder() );
  1017. }
  1018. void GuiTextEditCtrl::onPreRender()
  1019. {
  1020. if ( isFirstResponder() )
  1021. {
  1022. U32 timeElapsed = Platform::getVirtualMilliseconds() - mTimeLastCursorFlipped;
  1023. mNumFramesElapsed++;
  1024. if ( ( timeElapsed > 500 ) && ( mNumFramesElapsed > 3 ) )
  1025. {
  1026. mCursorOn = !mCursorOn;
  1027. mTimeLastCursorFlipped = Platform::getVirtualMilliseconds();
  1028. mNumFramesElapsed = 0;
  1029. setUpdate();
  1030. }
  1031. //update the cursor if the text is scrolling
  1032. if ( mDragHit )
  1033. {
  1034. if ( ( mScrollDir < 0 ) && ( mCursorPos > 0 ) )
  1035. mCursorPos--;
  1036. else if ( ( mScrollDir > 0 ) && ( mCursorPos < (S32) mTextBuffer.length() ) )
  1037. mCursorPos++;
  1038. }
  1039. }
  1040. }
  1041. void GuiTextEditCtrl::drawText( const RectI &drawRect, bool isFocused )
  1042. {
  1043. StringBuffer textBuffer;
  1044. Point2I drawPoint = drawRect.point;
  1045. Point2I paddingLeftTop, paddingRightBottom;
  1046. // Or else just copy it over.
  1047. char *renderText = Con::getReturnBuffer( GuiTextEditCtrl::MAX_STRING_LENGTH );
  1048. getRenderText( renderText );
  1049. // Apply password masking (make the masking char optional perhaps?)
  1050. if(mPasswordText)
  1051. {
  1052. const U32 renderLen = dStrlen( renderText );
  1053. for( U32 i = 0; i < renderLen; i++ )
  1054. textBuffer.append(mPasswordMask);
  1055. }
  1056. else
  1057. {
  1058. textBuffer.set( renderText );
  1059. }
  1060. bool usePlaceholder = false;
  1061. if (textBuffer.length() == 0 && !isFocused)
  1062. {
  1063. textBuffer.set(mPlaceholderText);
  1064. usePlaceholder = true;
  1065. }
  1066. // Just a little sanity.
  1067. if(mCursorPos > textBuffer.length())
  1068. mCursorPos = textBuffer.length();
  1069. paddingLeftTop.set(( mProfile->mTextOffset.x != 0 ? mProfile->mTextOffset.x : 3 ), mProfile->mTextOffset.y);
  1070. paddingRightBottom = paddingLeftTop;
  1071. // Center vertically:
  1072. drawPoint.y += ( ( drawRect.extent.y - paddingLeftTop.y - paddingRightBottom.y - S32( mProfile->mFont->getHeight() ) ) / 2 ) + paddingLeftTop.y;
  1073. // Align horizontally:
  1074. S32 textWidth = mProfile->mFont->getStrNWidth(textBuffer.getPtr(), textBuffer.length());
  1075. switch( mProfile->mAlignment )
  1076. {
  1077. case GuiControlProfile::RightJustify:
  1078. drawPoint.x += ( drawRect.extent.x - textWidth - paddingRightBottom.x );
  1079. break;
  1080. case GuiControlProfile::CenterJustify:
  1081. drawPoint.x += ( ( drawRect.extent.x - textWidth ) / 2 );
  1082. break;
  1083. default:
  1084. case GuiControlProfile::LeftJustify :
  1085. drawPoint.x += paddingLeftTop.x;
  1086. break;
  1087. }
  1088. ColorI fontColor = mActive ? mProfile->mFontColor : mProfile->mFontColorNA;
  1089. if (usePlaceholder)
  1090. fontColor = mProfile->mFontColorNA;
  1091. // now draw the text
  1092. Point2I cursorStart, cursorEnd;
  1093. mTextOffset.y = drawPoint.y;
  1094. mTextOffset.x = drawPoint.x;
  1095. if ( drawRect.extent.x - paddingLeftTop.x > textWidth )
  1096. mTextOffset.x = drawPoint.x;
  1097. else
  1098. {
  1099. // Alignment affects large text
  1100. if ( mProfile->mAlignment == GuiControlProfile::RightJustify
  1101. || mProfile->mAlignment == GuiControlProfile::CenterJustify )
  1102. {
  1103. if ( mTextOffset.x + textWidth < (drawRect.point.x + drawRect.extent.x) - paddingRightBottom.x)
  1104. mTextOffset.x = (drawRect.point.x + drawRect.extent.x) - paddingRightBottom.x - textWidth;
  1105. }
  1106. }
  1107. // calculate the cursor
  1108. if( isFocused && mActive )
  1109. {
  1110. // Where in the string are we?
  1111. S32 cursorOffset=0, charWidth=0;
  1112. UTF16 tempChar = textBuffer.getChar(mCursorPos);
  1113. // Alright, we want to terminate things momentarily.
  1114. if(mCursorPos > 0)
  1115. {
  1116. cursorOffset = mProfile->mFont->getStrNWidth(textBuffer.getPtr(), mCursorPos);
  1117. }
  1118. else
  1119. cursorOffset = 0;
  1120. if( tempChar && mProfile->mFont->isValidChar( tempChar ) )
  1121. charWidth = mProfile->mFont->getCharWidth( tempChar );
  1122. else
  1123. charWidth = paddingRightBottom.x;
  1124. if( mTextOffset.x + cursorOffset + 1 >= (drawRect.point.x + drawRect.extent.x) ) // +1 is for the cursor width
  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 * 3;
  1129. if ( cursorOffset + skipForward > textWidth )
  1130. mTextOffset.x = (drawRect.point.x + drawRect.extent.x) - paddingRightBottom.x - textWidth;
  1131. else
  1132. {
  1133. //mTextOffset.x -= skipForward;
  1134. S32 mul = (S32)( mFloor( (cursorOffset-drawRect.extent.x) / skipForward ) );
  1135. mTextOffset.x -= skipForward * mul + drawRect.extent.x - 1; // -1 is for the cursor width
  1136. }
  1137. }
  1138. else if( mTextOffset.x + cursorOffset < drawRect.point.x + paddingLeftTop.x )
  1139. {
  1140. // Cursor somewhere before the textcontrol
  1141. // skip backward roughly 25% of the total width (if possible)
  1142. S32 skipBackward = drawRect.extent.x / 4 * 3;
  1143. if ( cursorOffset - skipBackward < 0 )
  1144. mTextOffset.x = drawRect.point.x + paddingLeftTop.x;
  1145. else
  1146. {
  1147. S32 mul = (S32)( mFloor( cursorOffset / skipBackward ) );
  1148. mTextOffset.x += drawRect.point.x - mTextOffset.x - skipBackward * mul;
  1149. }
  1150. }
  1151. cursorStart.x = mTextOffset.x + cursorOffset;
  1152. #ifdef TORQUE_OS_MAC
  1153. cursorStart.x += charWidth/2;
  1154. #endif
  1155. cursorEnd.x = cursorStart.x;
  1156. S32 cursorHeight = mProfile->mFont->getHeight();
  1157. if ( cursorHeight < drawRect.extent.y )
  1158. {
  1159. cursorStart.y = drawPoint.y;
  1160. cursorEnd.y = cursorStart.y + cursorHeight;
  1161. }
  1162. else
  1163. {
  1164. cursorStart.y = drawRect.point.y;
  1165. cursorEnd.y = cursorStart.y + drawRect.extent.y;
  1166. }
  1167. }
  1168. //draw the text
  1169. if ( !isFocused )
  1170. mBlockStart = mBlockEnd = 0;
  1171. //also verify the block start/end
  1172. if ((mBlockStart > textBuffer.length() || (mBlockEnd > textBuffer.length()) || (mBlockStart > mBlockEnd)))
  1173. mBlockStart = mBlockEnd = 0;
  1174. Point2I tempOffset = mTextOffset;
  1175. //draw the portion before the highlight
  1176. if ( mBlockStart > 0 )
  1177. {
  1178. GFX->getDrawUtil()->setBitmapModulation( fontColor );
  1179. const UTF16* preString2 = textBuffer.getPtr();
  1180. GFX->getDrawUtil()->drawText( mProfile->mFont, tempOffset, preString2, mProfile->mFontColors );
  1181. tempOffset.x += mProfile->mFont->getStrNWidth(preString2, mBlockStart);
  1182. }
  1183. //draw the highlighted portion
  1184. if ( mBlockEnd > 0 )
  1185. {
  1186. const UTF16* highlightBuff = textBuffer.getPtr() + mBlockStart;
  1187. U32 highlightBuffLen = mBlockEnd-mBlockStart;
  1188. S32 highlightWidth = mProfile->mFont->getStrNWidth(highlightBuff, highlightBuffLen);
  1189. GFX->getDrawUtil()->drawRectFill( Point2I( tempOffset.x, drawRect.point.y ),
  1190. Point2I( tempOffset.x + highlightWidth, drawRect.point.y + drawRect.extent.y - 1),
  1191. mProfile->mFontColorSEL );
  1192. GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColorHL );
  1193. GFX->getDrawUtil()->drawTextN( mProfile->mFont, tempOffset, highlightBuff, highlightBuffLen, mProfile->mFontColors );
  1194. tempOffset.x += highlightWidth;
  1195. }
  1196. //draw the portion after the highlight
  1197. if(mBlockEnd < textBuffer.length())
  1198. {
  1199. const UTF16* finalBuff = textBuffer.getPtr() + mBlockEnd;
  1200. U32 finalBuffLen = textBuffer.length() - mBlockEnd;
  1201. GFX->getDrawUtil()->setBitmapModulation( fontColor );
  1202. GFX->getDrawUtil()->drawTextN( mProfile->mFont, tempOffset, finalBuff, finalBuffLen, mProfile->mFontColors );
  1203. }
  1204. //draw the cursor
  1205. if ( isFocused && mCursorOn )
  1206. GFX->getDrawUtil()->drawLine( cursorStart, cursorEnd, mProfile->mCursorColor );
  1207. }
  1208. bool GuiTextEditCtrl::hasText()
  1209. {
  1210. return ( mTextBuffer.length() );
  1211. }
  1212. void GuiTextEditCtrl::invalidText(bool playSound)
  1213. {
  1214. mTextValid = false;
  1215. if ( playSound )
  1216. playDeniedSound();
  1217. }
  1218. void GuiTextEditCtrl::validText()
  1219. {
  1220. mTextValid = true;
  1221. }
  1222. bool GuiTextEditCtrl::isValidText()
  1223. {
  1224. return mTextValid;
  1225. }
  1226. void GuiTextEditCtrl::playDeniedSound()
  1227. {
  1228. if ( mDeniedSound )
  1229. SFX->playOnce( mDeniedSound );
  1230. }
  1231. const char *GuiTextEditCtrl::getScriptValue()
  1232. {
  1233. return StringTable->insert(mTextBuffer.getPtr8());
  1234. }
  1235. void GuiTextEditCtrl::setScriptValue(const char *value)
  1236. {
  1237. mTextBuffer.set(value);
  1238. mCursorPos = mTextBuffer.length();
  1239. }
  1240. void GuiTextEditCtrl::handleCharInput( U16 ascii )
  1241. {
  1242. S32 stringLen = mTextBuffer.length();
  1243. // Get the character ready to add to a UTF8 string.
  1244. UTF16 convertedChar[2] = { ascii, 0 };
  1245. //see if it's a number field
  1246. if ( mProfile->mNumbersOnly )
  1247. {
  1248. if (ascii == '-')
  1249. {
  1250. //a minus sign only exists at the beginning, and only a single minus sign
  1251. if (mCursorPos != 0 && !isAllTextSelected())
  1252. {
  1253. invalidText();
  1254. return;
  1255. }
  1256. if (mInsertOn && (mTextBuffer.getChar(0) == '-'))
  1257. {
  1258. invalidText();
  1259. return;
  1260. }
  1261. }
  1262. // BJTODO: This is probably not unicode safe.
  1263. else if (ascii != '.' && (ascii < '0' || ascii > '9'))
  1264. {
  1265. invalidText();
  1266. return;
  1267. }
  1268. else
  1269. validText();
  1270. }
  1271. //save the current state
  1272. saveUndoState();
  1273. bool alreadyCut = false;
  1274. //delete anything highlighted
  1275. if ( mBlockEnd > 0 )
  1276. {
  1277. mTextBuffer.cut(mBlockStart, mBlockEnd-mBlockStart);
  1278. mCursorPos = mBlockStart;
  1279. mBlockStart = 0;
  1280. mBlockEnd = 0;
  1281. // We just changed the string length!
  1282. // Get its new value.
  1283. stringLen = mTextBuffer.length();
  1284. // If we already had text highlighted, we just want to cut that text.
  1285. // Don't cut the next character even if insert is not on.
  1286. alreadyCut = true;
  1287. }
  1288. if ( ( mInsertOn && ( stringLen < mMaxStrLen ) ) ||
  1289. ( !mInsertOn && ( mCursorPos < mMaxStrLen ) ) )
  1290. {
  1291. if ( mCursorPos == stringLen )
  1292. {
  1293. mTextBuffer.append(convertedChar);
  1294. mCursorPos++;
  1295. }
  1296. else
  1297. {
  1298. if ( mInsertOn || alreadyCut )
  1299. {
  1300. mTextBuffer.insert(mCursorPos, convertedChar);
  1301. mCursorPos++;
  1302. }
  1303. else
  1304. {
  1305. mTextBuffer.cut(mCursorPos, 1);
  1306. mTextBuffer.insert(mCursorPos, convertedChar);
  1307. mCursorPos++;
  1308. }
  1309. }
  1310. }
  1311. else
  1312. playDeniedSound();
  1313. //reset the history index
  1314. mHistoryDirty = true;
  1315. //execute the console command if it exists
  1316. execConsoleCallback();
  1317. }
  1318. S32 GuiTextEditCtrl::findPrevWord()
  1319. {
  1320. // First the first word to the left of the current cursor position
  1321. // and return the positional index of its starting character.
  1322. // We define the first character of a word as any non-whitespace
  1323. // character which has a non-alpha-numeric character to its immediate left.
  1324. const UTF8* text = mTextBuffer.getPtr8();
  1325. for ( S32 i = mCursorPos - 1; i > 0; i-- )
  1326. {
  1327. if ( !dIsspace( text[i] ) )
  1328. {
  1329. if ( !dIsalnum( text[i-1] ) )
  1330. {
  1331. return i;
  1332. }
  1333. }
  1334. }
  1335. return 0;
  1336. }
  1337. S32 GuiTextEditCtrl::findNextWord()
  1338. {
  1339. // First the first word to the right of the current cursor position
  1340. // and return the positional index of its starting character.
  1341. // We define the first character of a word as any non-whitespace
  1342. // character which has a non-alpha-numeric character to its immediate left.
  1343. const UTF8* text = mTextBuffer.getPtr8();
  1344. for ( S32 i = mCursorPos + 1; i < mTextBuffer.length(); i++ )
  1345. {
  1346. if ( !dIsspace( text[i] ) )
  1347. {
  1348. if ( !dIsalnum( text[i-1] ) )
  1349. {
  1350. return i;
  1351. }
  1352. }
  1353. }
  1354. return mTextBuffer.length();
  1355. }
  1356. DefineEngineMethod( GuiTextEditCtrl, getText, const char*, (),,
  1357. "@brief Acquires the current text displayed in this control.\n\n"
  1358. "@tsexample\n"
  1359. "// Acquire the value of the text control.\n"
  1360. "%text = %thisGuiTextEditCtrl.getText();\n"
  1361. "@endtsexample\n\n"
  1362. "@return The current text within the control.\n\n"
  1363. "@see GuiControl")
  1364. {
  1365. if( !object->hasText() )
  1366. return StringTable->EmptyString();
  1367. char *retBuffer = Con::getReturnBuffer( GuiTextEditCtrl::MAX_STRING_LENGTH );
  1368. object->getText( retBuffer );
  1369. return retBuffer;
  1370. }
  1371. DefineEngineMethod( GuiTextEditCtrl, setText, void, (const char* text),,
  1372. "@brief Sets the text in the control.\n\n"
  1373. "@param text Text to place in the control.\n"
  1374. "@tsexample\n"
  1375. "// Define the text to display\n"
  1376. "%text = \"Text!\"\n\n"
  1377. "// Inform the GuiTextEditCtrl to display the defined text\n"
  1378. "%thisGuiTextEditCtrl.setText(%text);\n"
  1379. "@endtsexample\n\n"
  1380. "@see GuiControl")
  1381. {
  1382. object->setText( text );
  1383. }
  1384. DefineEngineMethod( GuiTextEditCtrl, getCursorPos, S32, (),,
  1385. "@brief Returns the current position of the text cursor in the control.\n\n"
  1386. "@tsexample\n"
  1387. "// Acquire the cursor position in the control\n"
  1388. "%position = %thisGuiTextEditCtrl.getCursorPost();\n"
  1389. "@endtsexample\n\n"
  1390. "@return Text cursor position within the control.\n\n"
  1391. "@see GuiControl")
  1392. {
  1393. return( object->getCursorPos() );
  1394. }
  1395. DefineEngineMethod( GuiTextEditCtrl, setCursorPos, void, (S32 position),,
  1396. "@brief Sets the text cursor at the defined position within the control.\n\n"
  1397. "@param position Text position to set the text cursor.\n"
  1398. "@tsexample\n"
  1399. "// Define the cursor position\n"
  1400. "%position = \"12\";\n\n"
  1401. "// Inform the GuiTextEditCtrl control to place the text cursor at the defined position\n"
  1402. "%thisGuiTextEditCtrl.setCursorPos(%position);\n"
  1403. "@endtsexample\n\n"
  1404. "@see GuiControl")
  1405. {
  1406. object->setCursorPos( position );
  1407. }
  1408. DefineEngineMethod( GuiTextEditCtrl, isAllTextSelected, bool, (),,
  1409. "@brief Checks to see if all text in the control has been selected.\n\n"
  1410. "@tsexample\n"
  1411. "// Check to see if all text has been selected or not.\n"
  1412. "%allSelected = %thisGuiTextEditCtrl.isAllTextSelected();\n"
  1413. "@endtsexample\n\n"
  1414. "@return True if all text in the control is selected, otherwise false.\n\n"
  1415. "@see GuiControl")
  1416. {
  1417. return object->isAllTextSelected();
  1418. }
  1419. DefineEngineMethod( GuiTextEditCtrl, selectAllText, void, (),,
  1420. "@brief Selects all text within the control.\n\n"
  1421. "@tsexample\n"
  1422. "// Inform the control to select all of its text.\n"
  1423. "%thisGuiTextEditCtrl.selectAllText();\n"
  1424. "@endtsexample\n\n"
  1425. "@see GuiControl")
  1426. {
  1427. object->selectAllText();
  1428. }
  1429. DefineEngineMethod( GuiTextEditCtrl, clearSelectedText, void, (),,
  1430. "@brief Unselects all selected text in the control.\n\n"
  1431. "@tsexample\n"
  1432. "// Inform the control to unselect all of its selected text\n"
  1433. "%thisGuiTextEditCtrl.clearSelectedText();\n"
  1434. "@endtsexample\n\n"
  1435. "@see GuiControl")
  1436. {
  1437. object->clearSelectedText();
  1438. }
  1439. DefineEngineMethod( GuiTextEditCtrl, forceValidateText, void, (),,
  1440. "@brief Force a validation to occur.\n\n"
  1441. "@tsexample\n"
  1442. "// Inform the control to force a validation of its text.\n"
  1443. "%thisGuiTextEditCtrl.forceValidateText();\n"
  1444. "@endtsexample\n\n"
  1445. "@see GuiControl")
  1446. {
  1447. object->forceValidateText();
  1448. }
  1449. DefineEngineMethod(GuiTextEditCtrl, invalidText, void, (bool playSound), (true),
  1450. "@brief Trigger the invalid sound and make the box red.nn"
  1451. "@param playSound Play the invalid text sound or not.n")
  1452. {
  1453. object->invalidText(playSound);
  1454. }
  1455. DefineEngineMethod(GuiTextEditCtrl, validText, void, (), ,
  1456. "@brief Restores the box to normal color.nn")
  1457. {
  1458. object->validText();
  1459. }
  1460. DefineEngineMethod(GuiTextEditCtrl, isValidText, bool, (), ,
  1461. "@brief Returns if the text is set to valid or not.n"
  1462. "@Return true if text is set to valid, false if not.nn")
  1463. {
  1464. return object->isValidText();
  1465. }