guiTextEditCtrl.cpp 49 KB

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