guiTextEditCtrl.cpp 47 KB

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