guiTextEditCtrl.cpp 47 KB

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