123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "platform/platform.h"
- #include "gui/controls/guiTextEditCtrl.h"
- #include "console/consoleTypes.h"
- #include "console/console.h"
- #include "gui/core/guiCanvas.h"
- #include "gui/controls/guiMLTextCtrl.h"
- #include "gui/core/guiDefaultControlRender.h"
- #include "gfx/gfxDevice.h"
- #include "gfx/gfxDrawUtil.h"
- #include "core/frameAllocator.h"
- #include "sfx/sfxTrack.h"
- #include "sfx/sfxTypes.h"
- #include "sfx/sfxSystem.h"
- #include "core/strings/unicode.h"
- #include "console/engineAPI.h"
- IMPLEMENT_CONOBJECT(GuiTextEditCtrl);
- ConsoleDocClass( GuiTextEditCtrl,
- "@brief A component that places a text entry box on the screen.\n\n"
- "Fonts and sizes are changed using profiles. The text value can be set or entered by a user.\n\n"
- "@tsexample\n"
- " new GuiTextEditCtrl(MessageHud_Edit)\n"
- " {\n"
- " text = \"Hello World\";\n"
- " validate = \"validateCommand();\"\n"
- " escapeCommand = \"escapeCommand();\";\n"
- " historySize = \"5\";\n"
- " tabComplete = \"true\";\n"
- " deniedSound = \"DeniedSoundProfile\";\n"
- " sinkAllKeyEvents = \"true\";\n"
- " password = \"true\";\n"
- " passwordMask = \"*\";\n"
- " //Properties not specific to this control have been omitted from this example.\n"
- " };\n"
- "@endtsexample\n\n"
- "@see GuiTextCtrl\n"
- "@see GuiControl\n\n"
- "@ingroup GuiControls\n"
- );
- IMPLEMENT_CALLBACK( GuiTextEditCtrl, onTabComplete, void, (const char* val),( val ),
- "@brief Called if tabComplete is true, and the 'tab' key is pressed.\n\n"
- "@param val Input to mimick the '1' sent by the actual tab key button press.\n"
- "@tsexample\n"
- "// Tab key has been pressed, causing the callback to occur.\n"
- "GuiTextEditCtrl::onTabComplete(%this,%val)\n"
- " {\n"
- " //Code to run when the onTabComplete callback occurs\n"
- " }\n"
- "@endtsexample\n\n"
- "@see GuiTextCtrl\n"
- "@see GuiControl\n\n"
- );
- IMPLEMENT_CALLBACK( GuiTextEditCtrl, onReturn, void, (),(),
- "@brief Called when the 'Return' or 'Enter' key is pressed.\n\n"
- "@tsexample\n"
- "// Return or Enter key was pressed, causing the callback to occur.\n"
- "GuiTextEditCtrl::onReturn(%this)\n"
- " {\n"
- " // Code to run when the onReturn callback occurs\n"
- " }\n"
- "@endtsexample\n\n"
- "@see GuiTextCtrl\n"
- "@see GuiControl\n\n"
- );
- IMPLEMENT_CALLBACK( GuiTextEditCtrl, onValidate, void, (),(),
- "@brief Called whenever the control is validated.\n\n"
- "@tsexample\n"
- "// The control gets validated, causing the callback to occur\n"
- "GuiTextEditCtrl::onValidated(%this)\n"
- " {\n"
- " // Code to run when the control is validated\n"
- " }\n"
- "@endtsexample\n\n"
- "@see GuiTextCtrl\n"
- "@see GuiControl\n\n"
- );
- GuiTextEditCtrl::GuiTextEditCtrl()
- {
- mInsertOn = true;
- mBlockStart = 0;
- mBlockEnd = 0;
- mCursorPos = 0;
- mCursorOn = false;
- mNumFramesElapsed = 0;
- mDragHit = false;
- mTabComplete = false;
- mScrollDir = 0;
- mUndoBlockStart = 0;
- mUndoBlockEnd = 0;
- mUndoCursorPos = 0;
- mPasswordText = false;
- mSinkAllKeyEvents = false;
- mActive = true;
- mTextValid = true;
- mTextOffsetReset = true;
- mHistoryDirty = false;
- mHistorySize = 0;
- mHistoryLast = -1;
- mHistoryIndex = 0;
- mHistoryBuf = NULL;
- #if defined(__MACOSX__)
- UTF8 bullet[4] = { 0xE2, 0x80, 0xA2, 0 };
-
- mPasswordMask = StringTable->insert( bullet );
- #else
- mPasswordMask = StringTable->insert( "*" );
- #endif
- Sim::findObject( "InputDeniedSound", mDeniedSound );
- }
- GuiTextEditCtrl::~GuiTextEditCtrl()
- {
- //delete the history buffer if it exists
- if (mHistoryBuf)
- {
- for (S32 i = 0; i < mHistorySize; i++)
- delete [] mHistoryBuf[i];
- delete [] mHistoryBuf;
- }
- }
- void GuiTextEditCtrl::initPersistFields()
- {
- addGroup( "Text Input" );
-
- addField("validate", TypeRealString,Offset(mValidateCommand, GuiTextEditCtrl), "Script command to be called when the first validater is lost.\n");
- addField("escapeCommand", TypeRealString,Offset(mEscapeCommand, GuiTextEditCtrl), "Script command to be called when the Escape key is pressed.\n");
- addField("historySize", TypeS32, Offset(mHistorySize, GuiTextEditCtrl), "How large of a history buffer to maintain.\n");
- 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");
- addField("deniedSound", TypeSFXTrackName, Offset(mDeniedSound, GuiTextEditCtrl), "If the attempted text cannot be entered, this sound effect will be played.\n");
- addField("sinkAllKeyEvents", TypeBool, Offset(mSinkAllKeyEvents, GuiTextEditCtrl), "If true, every key event will act as if the Enter key was pressed.\n");
- 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");
- 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");
-
- endGroup( "Text Input" );
- Parent::initPersistFields();
- }
- bool GuiTextEditCtrl::onAdd()
- {
- if ( ! Parent::onAdd() )
- return false;
- //create the history buffer
- if ( mHistorySize > 0 )
- {
- mHistoryBuf = new UTF16*[mHistorySize];
- for ( S32 i = 0; i < mHistorySize; i++ )
- {
- mHistoryBuf[i] = new UTF16[GuiTextCtrl::MAX_STRING_LENGTH + 1];
- mHistoryBuf[i][0] = '\0';
- }
- }
- if( mText && mText[0] )
- {
- setText(mText);
- }
- return true;
- }
- void GuiTextEditCtrl::onStaticModified(const char* slotName, const char* newValue)
- {
- if(!dStricmp(slotName, "text"))
- setText(mText);
- }
- void GuiTextEditCtrl::execConsoleCallback()
- {
- // Execute the console command!
- Parent::execConsoleCallback();
- // Update the console variable:
- if ( mConsoleVariable[0] )
- Con::setVariable( mConsoleVariable, mTextBuffer.getPtr8() );
- }
- void GuiTextEditCtrl::updateHistory( StringBuffer *inTxt, bool moveIndex )
- {
- if(!mHistorySize)
- return;
- const UTF16* txt = inTxt->getPtr();
-
- // Reject empty strings.
- if( !txt || !txt[ 0 ] )
- return;
- // see if it's already in
- if(mHistoryLast == -1 || dStrcmp(txt, mHistoryBuf[mHistoryLast]))
- {
- if(mHistoryLast == mHistorySize-1) // we're at the history limit... shuffle the pointers around:
- {
- UTF16 *first = mHistoryBuf[0];
- for(U32 i = 0; i < mHistorySize - 1; i++)
- mHistoryBuf[i] = mHistoryBuf[i+1];
- mHistoryBuf[mHistorySize-1] = first;
- if(mHistoryIndex > 0)
- mHistoryIndex--;
- }
- else
- mHistoryLast++;
- inTxt->getCopy(mHistoryBuf[mHistoryLast], GuiTextCtrl::MAX_STRING_LENGTH);
- mHistoryBuf[mHistoryLast][GuiTextCtrl::MAX_STRING_LENGTH] = '\0';
- }
- if(moveIndex)
- mHistoryIndex = mHistoryLast + 1;
- }
- void GuiTextEditCtrl::getText( char *dest )
- {
- if ( dest )
- mTextBuffer.getCopy8((UTF8*)dest, GuiTextCtrl::MAX_STRING_LENGTH+1);
- }
- void GuiTextEditCtrl::getRenderText(char *dest)
- {
- getText( dest );
- }
-
- void GuiTextEditCtrl::setText( const UTF8 *txt )
- {
- if(txt && txt[0] != 0)
- {
- Parent::setText(txt);
- mTextBuffer.set( txt );
- }
- else
- mTextBuffer.set( "" );
- mCursorPos = mTextBuffer.length();
- }
- void GuiTextEditCtrl::setText( const UTF16* txt)
- {
- if(txt && txt[0] != 0)
- {
- UTF8* txt8 = createUTF8string( txt );
- Parent::setText( txt8 );
- delete[] txt8;
- mTextBuffer.set( txt );
- }
- else
- {
- Parent::setText("");
- mTextBuffer.set("");
- }
-
- mCursorPos = mTextBuffer.length();
- }
- bool GuiTextEditCtrl::isAllTextSelected()
- {
- if( mBlockStart == 0 && mBlockEnd == mTextBuffer.length() )
- return true;
- else
- return false;
- }
- void GuiTextEditCtrl::selectAllText()
- {
- mBlockStart = 0;
- mBlockEnd = mTextBuffer.length();
- setUpdate();
- }
- void GuiTextEditCtrl::clearSelectedText()
- {
- mBlockStart = 0;
- mBlockEnd = 0;
- setUpdate();
- }
- void GuiTextEditCtrl::forceValidateText()
- {
- if( mValidateCommand.isNotEmpty() )
- evaluate( mValidateCommand );
- }
- void GuiTextEditCtrl::setCursorPos( const S32 newPos )
- {
- S32 charCount = mTextBuffer.length();
- S32 realPos = newPos > charCount ? charCount : newPos < 0 ? 0 : newPos;
- if ( realPos != mCursorPos )
- {
- mCursorPos = realPos;
- setUpdate();
- }
- }
- S32 GuiTextEditCtrl::calculateCursorPos( const Point2I &globalPos )
- {
- Point2I ctrlOffset = localToGlobalCoord( Point2I( 0, 0 ) );
- S32 charLength = 0;
- S32 curX;
- curX = globalPos.x - ctrlOffset.x;
- setUpdate();
- //if the cursor is too far to the left
- if ( curX < 0 )
- return -1;
- //if the cursor is too far to the right
- if ( curX >= ctrlOffset.x + getExtent().x )
- return -2;
- curX = globalPos.x - mTextOffset.x;
- S32 count=0;
- if(mTextBuffer.length() == 0)
- return 0;
- for(count=0; count<mTextBuffer.length(); count++)
- {
- UTF16 c = mTextBuffer.getChar(count);
- if(!mPasswordText && !mProfile->mFont->isValidChar(c))
- continue;
-
- if(mPasswordText)
- charLength += mProfile->mFont->getCharXIncrement( mPasswordMask[0] );
- else
- charLength += mProfile->mFont->getCharXIncrement( c );
- if ( charLength > curX )
- break;
- }
- return count;
- }
- void GuiTextEditCtrl::onMouseDown( const GuiEvent &event )
- {
- mDragHit = false;
- // If we have a double click, select all text. Otherwise
- // act as before by clearing any selection.
- bool doubleClick = (event.mouseClickCount > 1);
- if(doubleClick)
- {
- selectAllText();
- } else
- {
- //undo any block function
- mBlockStart = 0;
- mBlockEnd = 0;
- }
- //find out where the cursor should be
- S32 pos = calculateCursorPos( event.mousePoint );
- // if the position is to the left
- if ( pos == -1 )
- mCursorPos = 0;
- else if ( pos == -2 ) //else if the position is to the right
- mCursorPos = mTextBuffer.length();
- else //else set the mCursorPos
- mCursorPos = pos;
- //save the mouseDragPos
- mMouseDragStart = mCursorPos;
- // lock the mouse
- mouseLock();
- //set the drag var
- mDragHit = true;
- //let the parent get the event
- setFirstResponder();
- }
- void GuiTextEditCtrl::onMouseDragged( const GuiEvent &event )
- {
- S32 pos = calculateCursorPos( event.mousePoint );
- // if the position is to the left
- if ( pos == -1 )
- mScrollDir = -1;
- else if ( pos == -2 ) // the position is to the right
- mScrollDir = 1;
- else // set the new cursor position
- {
- mScrollDir = 0;
- mCursorPos = pos;
- }
- // update the block:
- mBlockStart = getMin( mCursorPos, mMouseDragStart );
- mBlockEnd = getMax( mCursorPos, mMouseDragStart );
- if ( mBlockStart < 0 )
- mBlockStart = 0;
- if ( mBlockStart == mBlockEnd )
- mBlockStart = mBlockEnd = 0;
- //let the parent get the event
- Parent::onMouseDragged(event);
- }
- void GuiTextEditCtrl::onMouseUp(const GuiEvent &event)
- {
- TORQUE_UNUSED(event);
- mDragHit = false;
- mScrollDir = 0;
- mouseUnlock();
- }
- void GuiTextEditCtrl::saveUndoState()
- {
- //save the current state
- mUndoText.set(&mTextBuffer);
- mUndoBlockStart = mBlockStart;
- mUndoBlockEnd = mBlockEnd;
- mUndoCursorPos = mCursorPos;
- }
- void GuiTextEditCtrl::onCopy(bool andCut)
- {
- // Don't copy/cut password field!
- if(mPasswordText)
- return;
- if (mBlockEnd > 0)
- {
- //save the current state
- saveUndoState();
- //copy the text to the clipboard
- UTF8* clipBuff = mTextBuffer.createSubstring8(mBlockStart, mBlockEnd - mBlockStart);
- Platform::setClipboard(clipBuff);
- delete[] clipBuff;
- //if we pressed the cut shortcut, we need to cut the selected text from the control...
- if (andCut)
- {
- mTextBuffer.cut(mBlockStart, mBlockEnd - mBlockStart);
- mCursorPos = mBlockStart;
- }
- mBlockStart = 0;
- mBlockEnd = 0;
- }
- }
- void GuiTextEditCtrl::onPaste()
- {
- //first, make sure there's something in the clipboard to copy...
- const UTF8 *clipboard = Platform::getClipboard();
- if(dStrlen(clipboard) <= 0)
- return;
- //save the current state
- saveUndoState();
- //delete anything hilited
- if (mBlockEnd > 0)
- {
- mTextBuffer.cut(mBlockStart, mBlockEnd - mBlockStart);
- mCursorPos = mBlockStart;
- mBlockStart = 0;
- mBlockEnd = 0;
- }
- // We'll be converting to UTF16, and maybe trimming the string,
- // so let's use a StringBuffer, for convinience.
- StringBuffer pasteText(clipboard);
- // Space left after we remove the highlighted text
- S32 stringLen = mTextBuffer.length();
- // Trim down to fit in a buffer of size mMaxStrLen
- S32 pasteLen = pasteText.length();
- if(stringLen + pasteLen > mMaxStrLen)
- {
- pasteLen = mMaxStrLen - stringLen;
- pasteText.cut(pasteLen, pasteText.length() - pasteLen);
- }
- if (mCursorPos == stringLen)
- {
- mTextBuffer.append(pasteText);
- }
- else
- {
- mTextBuffer.insert(mCursorPos, pasteText);
- }
- mCursorPos += pasteLen;
- }
- void GuiTextEditCtrl::onUndo()
- {
- StringBuffer tempBuffer;
- S32 tempBlockStart;
- S32 tempBlockEnd;
- S32 tempCursorPos;
- //save the current
- tempBuffer.set(&mTextBuffer);
- tempBlockStart = mBlockStart;
- tempBlockEnd = mBlockEnd;
- tempCursorPos = mCursorPos;
- //restore the prev
- mTextBuffer.set(&mUndoText);
- mBlockStart = mUndoBlockStart;
- mBlockEnd = mUndoBlockEnd;
- mCursorPos = mUndoCursorPos;
- //update the undo
- mUndoText.set(&tempBuffer);
- mUndoBlockStart = tempBlockStart;
- mUndoBlockEnd = tempBlockEnd;
- mUndoCursorPos = tempCursorPos;
- }
- bool GuiTextEditCtrl::onKeyDown(const GuiEvent &event)
- {
- if ( !isActive() || !isAwake() )
- return false;
- S32 stringLen = mTextBuffer.length();
- setUpdate();
- // Ugly, but now I'm cool like MarkF.
- if(event.keyCode == KEY_BACKSPACE)
- goto dealWithBackspace;
-
- if ( event.modifier & SI_SHIFT )
- {
- // Added support for word jump selection.
- if ( event.modifier & SI_CTRL )
- {
- switch ( event.keyCode )
- {
- case KEY_LEFT:
- {
- S32 newpos = findPrevWord();
- if ( mBlockStart == mBlockEnd )
- {
- // There was not already a selection so start a new one.
- mBlockStart = newpos;
- mBlockEnd = mCursorPos;
- }
- else
- {
- // There was a selection already...
- // In this case the cursor MUST be at either the
- // start or end of that selection.
- if ( mCursorPos == mBlockStart )
- {
- // We are at the start block and traveling left so
- // just extend the start block farther left.
- mBlockStart = newpos;
- }
- else
- {
- // We are at the end block BUT traveling left
- // back towards the start block...
- if ( newpos > mBlockStart )
- {
- // We haven't overpassed the existing start block
- // so just trim back the end block.
- mBlockEnd = newpos;
- }
- else if ( newpos == mBlockStart )
- {
- // We are back at the start, so no more selection.
- mBlockEnd = mBlockStart = 0;
- }
- else
- {
- // Only other option, we just backtracked PAST
- // our original start block.
- // So the new position becomes the start block
- // and the old start block becomes the end block.
- mBlockEnd = mBlockStart;
- mBlockStart = newpos;
- }
- }
- }
-
- mCursorPos = newpos;
- return true;
- }
- case KEY_RIGHT:
- {
- S32 newpos = findNextWord();
- if ( mBlockStart == mBlockEnd )
- {
- // There was not already a selection so start a new one.
- mBlockStart = mCursorPos;
- mBlockEnd = newpos;
- }
- else
- {
- // There was a selection already...
- // In this case the cursor MUST be at either the
- // start or end of that selection.
- if ( mCursorPos == mBlockEnd )
- {
- // We are at the end block and traveling right so
- // just extend the end block farther right.
- mBlockEnd = newpos;
- }
- else
- {
- // We are at the start block BUT traveling right
- // back towards the end block...
- if ( newpos < mBlockEnd )
- {
- // We haven't overpassed the existing end block
- // so just trim back the start block.
- mBlockStart = newpos;
- }
- else if ( newpos == mBlockEnd )
- {
- // We are back at the end, so no more selection.
- mBlockEnd = mBlockStart = 0;
- }
- else
- {
- // Only other option, we just backtracked PAST
- // our original end block.
- // So the new position becomes the end block
- // and the old end block becomes the start block.
- mBlockStart = mBlockEnd;
- mBlockEnd = newpos;
- }
- }
- }
- mCursorPos = newpos;
- return true;
- }
-
- default:
- break;
- }
- }
- // End support for word jump selection.
- switch ( event.keyCode )
- {
- case KEY_TAB:
- if ( mTabComplete )
- {
- onTabComplete_callback("1");
- return true;
- }
- break; // We don't want to fall through if we don't handle the TAB here.
- case KEY_HOME:
- mBlockStart = 0;
- mBlockEnd = mCursorPos;
- mCursorPos = 0;
- return true;
- case KEY_END:
- mBlockStart = mCursorPos;
- mBlockEnd = stringLen;
- mCursorPos = stringLen;
- return true;
- case KEY_LEFT:
- if ((mCursorPos > 0) & (stringLen > 0))
- {
- //if we already have a selected block
- if (mCursorPos == mBlockEnd)
- {
- mCursorPos--;
- mBlockEnd--;
- if (mBlockEnd == mBlockStart)
- {
- mBlockStart = 0;
- mBlockEnd = 0;
- }
- }
- else {
- mCursorPos--;
- mBlockStart = mCursorPos;
- if (mBlockEnd == 0)
- {
- mBlockEnd = mCursorPos + 1;
- }
- }
- }
- return true;
- case KEY_RIGHT:
- if (mCursorPos < stringLen)
- {
- if ((mCursorPos == mBlockStart) && (mBlockEnd > 0))
- {
- mCursorPos++;
- mBlockStart++;
- if (mBlockStart == mBlockEnd)
- {
- mBlockStart = 0;
- mBlockEnd = 0;
- }
- }
- else
- {
- if (mBlockEnd == 0)
- {
- mBlockStart = mCursorPos;
- mBlockEnd = mCursorPos;
- }
- mCursorPos++;
- mBlockEnd++;
- }
- }
- return true;
- case KEY_RETURN:
- case KEY_NUMPADENTER:
-
- return dealWithEnter(false);
- default:
- break;
- }
- }
- else if (event.modifier & SI_CTRL)
- {
- switch(event.keyCode)
- {
- #if defined(TORQUE_OS_MAC)
- // Added UNIX emacs key bindings - just a little hack here...
- // Ctrl-B - move one character back
- case KEY_B:
- {
- GuiEvent new_event;
- new_event.modifier = 0;
- new_event.keyCode = KEY_LEFT;
- return(onKeyDown(new_event));
- }
- // Ctrl-F - move one character forward
- case KEY_F:
- {
- GuiEvent new_event;
- new_event.modifier = 0;
- new_event.keyCode = KEY_RIGHT;
- return(onKeyDown(new_event));
- }
- // Ctrl-A - move to the beginning of the line
- case KEY_A:
- {
- GuiEvent new_event;
- new_event.modifier = 0;
- new_event.keyCode = KEY_HOME;
- return(onKeyDown(new_event));
- }
- // Ctrl-E - move to the end of the line
- case KEY_E:
- {
- GuiEvent new_event;
- new_event.modifier = 0;
- new_event.keyCode = KEY_END;
- return(onKeyDown(new_event));
- }
- // Ctrl-P - move backward in history
- case KEY_P:
- {
- GuiEvent new_event;
- new_event.modifier = 0;
- new_event.keyCode = KEY_UP;
- return(onKeyDown(new_event));
- }
- // Ctrl-N - move forward in history
- case KEY_N:
- {
- GuiEvent new_event;
- new_event.modifier = 0;
- new_event.keyCode = KEY_DOWN;
- return(onKeyDown(new_event));
- }
- // Ctrl-D - delete under cursor
- case KEY_D:
- {
- GuiEvent new_event;
- new_event.modifier = 0;
- new_event.keyCode = KEY_DELETE;
- return(onKeyDown(new_event));
- }
- case KEY_U:
- {
- GuiEvent new_event;
- new_event.modifier = SI_CTRL;
- new_event.keyCode = KEY_DELETE;
- return(onKeyDown(new_event));
- }
- // End added UNIX emacs key bindings
- #endif
- // Adding word jump navigation.
- case KEY_LEFT:
- {
- mCursorPos = findPrevWord();
- mBlockStart = 0;
- mBlockEnd = 0;
- return true;
- }
- case KEY_RIGHT:
- {
- mCursorPos = findNextWord();
- mBlockStart = 0;
- mBlockEnd = 0;
- return true;
- }
-
- #if !defined(TORQUE_OS_MAC)
- // Select all
- case KEY_A:
- {
- selectAllText();
- return true;
- }
- // windows style cut / copy / paste / undo keybinds
- case KEY_C:
- case KEY_X:
- {
- // copy, and cut the text if we hit ctrl-x
- onCopy( event.keyCode==KEY_X );
- return true;
- }
- case KEY_V:
- {
- onPaste();
- // Execute the console command!
- execConsoleCallback();
- return true;
- }
- case KEY_Z:
- if (! mDragHit)
- {
- onUndo();
- return true;
- }
- #endif
- case KEY_DELETE:
- case KEY_BACKSPACE:
- //save the current state
- saveUndoState();
- //delete everything in the field
- mTextBuffer.set("");
- mCursorPos = 0;
- mBlockStart = 0;
- mBlockEnd = 0;
- execConsoleCallback();
- return true;
- default:
- break;
- }
- }
- #if defined(TORQUE_OS_MAC)
- // mac style cut / copy / paste / undo keybinds
- else if (event.modifier & SI_ALT)
- {
- // Mac command key maps to alt in torque.
- // Added Mac cut/copy/paste/undo keys
- switch(event.keyCode)
- {
- // Select all
- case KEY_A:
- {
- selectAllText();
- return true;
- }
- case KEY_C:
- case KEY_X:
- {
- // copy, and cut the text if we hit cmd-x
- onCopy( event.keyCode==KEY_X );
- return true;
- }
- case KEY_V:
- {
- onPaste();
- // Execute the console command!
- execConsoleCallback();
- return true;
- }
-
- case KEY_Z:
- if (! mDragHit)
- {
- onUndo();
- return true;
- }
-
- default:
- break;
- }
- }
- #endif
- else
- {
- switch(event.keyCode)
- {
- case KEY_ESCAPE:
- if( mEscapeCommand.isNotEmpty() )
- {
- evaluate( mEscapeCommand );
- return( true );
- }
- return( Parent::onKeyDown( event ) );
- case KEY_RETURN:
- case KEY_NUMPADENTER:
-
- return dealWithEnter(true);
- case KEY_UP:
- {
- if( mHistorySize > 0 )
- {
- if(mHistoryDirty)
- {
- updateHistory(&mTextBuffer, false);
- mHistoryDirty = false;
- }
- mHistoryIndex--;
-
- if(mHistoryIndex >= 0 && mHistoryIndex <= mHistoryLast)
- setText(mHistoryBuf[mHistoryIndex]);
- else if(mHistoryIndex < 0)
- mHistoryIndex = 0;
- }
-
- return true;
- }
- case KEY_DOWN:
- {
- if( mHistorySize > 0 )
- {
- if(mHistoryDirty)
- {
- updateHistory(&mTextBuffer, false);
- mHistoryDirty = false;
- }
- mHistoryIndex++;
- if(mHistoryIndex > mHistoryLast)
- {
- mHistoryIndex = mHistoryLast + 1;
- setText("");
- }
- else
- setText(mHistoryBuf[mHistoryIndex]);
- }
- return true;
- }
- case KEY_LEFT:
-
- // If we have a selection put the cursor to the left side of it.
- if ( mBlockStart != mBlockEnd )
- {
- mCursorPos = mBlockStart;
- mBlockStart = mBlockEnd = 0;
- }
- else
- {
- mBlockStart = mBlockEnd = 0;
- mCursorPos = getMax( mCursorPos - 1, 0 );
- }
- return true;
- case KEY_RIGHT:
- // If we have a selection put the cursor to the right side of it.
- if ( mBlockStart != mBlockEnd )
- {
- mCursorPos = mBlockEnd;
- mBlockStart = mBlockEnd = 0;
- }
- else
- {
- mBlockStart = mBlockEnd = 0;
- mCursorPos = getMin( mCursorPos + 1, stringLen );
- }
- return true;
- case KEY_BACKSPACE:
- dealWithBackspace:
- //save the current state
- saveUndoState();
- if (mBlockEnd > 0)
- {
- mTextBuffer.cut(mBlockStart, mBlockEnd-mBlockStart);
- mCursorPos = mBlockStart;
- mBlockStart = 0;
- mBlockEnd = 0;
- mHistoryDirty = true;
- // Execute the console command!
- execConsoleCallback();
- }
- else if (mCursorPos > 0)
- {
- mTextBuffer.cut(mCursorPos-1, 1);
- mCursorPos--;
- mHistoryDirty = true;
- // Execute the console command!
- execConsoleCallback();
- }
- return true;
- case KEY_DELETE:
- //save the current state
- saveUndoState();
- if (mBlockEnd > 0)
- {
- mHistoryDirty = true;
- mTextBuffer.cut(mBlockStart, mBlockEnd-mBlockStart);
- mCursorPos = mBlockStart;
- mBlockStart = 0;
- mBlockEnd = 0;
- // Execute the console command!
- execConsoleCallback();
- }
- else if (mCursorPos < stringLen)
- {
- mHistoryDirty = true;
- mTextBuffer.cut(mCursorPos, 1);
- // Execute the console command!
- execConsoleCallback();
- }
- return true;
- case KEY_INSERT:
- mInsertOn = !mInsertOn;
- return true;
- case KEY_HOME:
- mBlockStart = 0;
- mBlockEnd = 0;
- mCursorPos = 0;
- return true;
- case KEY_END:
- mBlockStart = 0;
- mBlockEnd = 0;
- mCursorPos = stringLen;
- return true;
-
- default:
- break;
- }
- }
- switch ( event.keyCode )
- {
- case KEY_TAB:
- if ( mTabComplete )
- {
- onTabComplete_callback("0");
- return( true );
- }
- case KEY_UP:
- case KEY_DOWN:
- case KEY_ESCAPE:
- return Parent::onKeyDown( event );
- default:
- break;
- }
-
- // Handle character input events.
- if( mProfile->mFont->isValidChar( event.ascii ) )
- {
- handleCharInput( event.ascii );
- return true;
- }
- // Or eat it if that's appropriate.
- if( mSinkAllKeyEvents )
- return true;
- // Not handled - pass the event to it's parent.
- return Parent::onKeyDown( event );
- }
- bool GuiTextEditCtrl::dealWithEnter( bool clearResponder )
- {
- //first validate
- if (mProfile->mReturnTab)
- {
- onLoseFirstResponder();
- }
- updateHistory(&mTextBuffer, true);
- mHistoryDirty = false;
- //next exec the alt console command
- execAltConsoleCallback();
- // Notify of Return
- onReturn_callback();
- if (mProfile->mReturnTab)
- {
- GuiCanvas *root = getRoot();
- if (root)
- {
- root->tabNext();
- return true;
- }
- }
-
- if( clearResponder )
- clearFirstResponder();
- return true;
- }
- void GuiTextEditCtrl::setFirstResponder()
- {
- Parent::setFirstResponder();
- GuiCanvas *root = getRoot();
- if (root != NULL)
- {
- root->enableKeyboardTranslation();
-
- // If the native OS accelerator keys are not disabled
- // then some key events like Delete, ctrl+V, etc may
- // not make it down to us.
- root->setNativeAcceleratorsEnabled( false );
- }
- }
- void GuiTextEditCtrl::onLoseFirstResponder()
- {
- GuiCanvas *root = getRoot();
- if( root )
- {
- root->setNativeAcceleratorsEnabled( true );
- root->disableKeyboardTranslation();
- }
- //execute the validate command
- if( mValidateCommand.isNotEmpty() )
- evaluate( mValidateCommand );
- onValidate_callback();
- // Redraw the control:
- setUpdate();
- // Lost Responder
- Parent::onLoseFirstResponder();
- }
- void GuiTextEditCtrl::onRender(Point2I offset, const RectI &updateRect)
- {
- RectI ctrlRect( offset, getExtent() );
- //if opaque, fill the update rect with the fill color
- if ( mProfile->mOpaque )
- {
- if (!mTextValid)
- GFX->getDrawUtil()->drawRectFill(ctrlRect, mProfile->mFillColorERR);
- else if (isFirstResponder())
- GFX->getDrawUtil()->drawRectFill(ctrlRect, mProfile->mFillColorHL);
- else
- GFX->getDrawUtil()->drawRectFill(ctrlRect, mProfile->mFillColor);
- }
- //if there's a border, draw the border
- if (mProfile->mBorder)
- {
- renderBorder(ctrlRect, mProfile);
- if (!mTextValid)
- GFX->getDrawUtil()->drawRectFill(ctrlRect, mProfile->mFillColorERR);
- }
- drawText( ctrlRect, isFirstResponder() );
- }
- void GuiTextEditCtrl::onPreRender()
- {
- if ( isFirstResponder() )
- {
- U32 timeElapsed = Platform::getVirtualMilliseconds() - mTimeLastCursorFlipped;
- mNumFramesElapsed++;
- if ( ( timeElapsed > 500 ) && ( mNumFramesElapsed > 3 ) )
- {
- mCursorOn = !mCursorOn;
- mTimeLastCursorFlipped = Platform::getVirtualMilliseconds();
- mNumFramesElapsed = 0;
- setUpdate();
- }
- //update the cursor if the text is scrolling
- if ( mDragHit )
- {
- if ( ( mScrollDir < 0 ) && ( mCursorPos > 0 ) )
- mCursorPos--;
- else if ( ( mScrollDir > 0 ) && ( mCursorPos < (S32) mTextBuffer.length() ) )
- mCursorPos++;
- }
- }
- }
- void GuiTextEditCtrl::drawText( const RectI &drawRect, bool isFocused )
- {
- StringBuffer textBuffer;
- Point2I drawPoint = drawRect.point;
- Point2I paddingLeftTop, paddingRightBottom;
- // Or else just copy it over.
- char *renderText = Con::getReturnBuffer( GuiTextEditCtrl::MAX_STRING_LENGTH );
- getRenderText( renderText );
- // Apply password masking (make the masking char optional perhaps?)
- if(mPasswordText)
- {
- const U32 renderLen = dStrlen( renderText );
- for( U32 i = 0; i < renderLen; i++ )
- textBuffer.append(mPasswordMask);
- }
- else
- {
- textBuffer.set( renderText );
- }
- // Just a little sanity.
- if(mCursorPos > textBuffer.length())
- mCursorPos = textBuffer.length();
- paddingLeftTop.set(( mProfile->mTextOffset.x != 0 ? mProfile->mTextOffset.x : 3 ), mProfile->mTextOffset.y);
- paddingRightBottom = paddingLeftTop;
- // Center vertically:
- drawPoint.y += ( ( drawRect.extent.y - paddingLeftTop.y - paddingRightBottom.y - S32( mProfile->mFont->getHeight() ) ) / 2 ) + paddingLeftTop.y;
- // Align horizontally:
-
- S32 textWidth = mProfile->mFont->getStrNWidth(textBuffer.getPtr(), textBuffer.length());
- switch( mProfile->mAlignment )
- {
- case GuiControlProfile::RightJustify:
- drawPoint.x += ( drawRect.extent.x - textWidth - paddingRightBottom.x );
- break;
- case GuiControlProfile::CenterJustify:
- drawPoint.x += ( ( drawRect.extent.x - textWidth ) / 2 );
- break;
- default:
- case GuiControlProfile::LeftJustify :
- drawPoint.x += paddingLeftTop.x;
- break;
- }
- ColorI fontColor = mActive ? mProfile->mFontColor : mProfile->mFontColorNA;
- // now draw the text
- Point2I cursorStart, cursorEnd;
- mTextOffset.y = drawPoint.y;
- mTextOffset.x = drawPoint.x;
- if ( drawRect.extent.x - paddingLeftTop.x > textWidth )
- mTextOffset.x = drawPoint.x;
- else
- {
- // Alignment affects large text
- if ( mProfile->mAlignment == GuiControlProfile::RightJustify
- || mProfile->mAlignment == GuiControlProfile::CenterJustify )
- {
- if ( mTextOffset.x + textWidth < (drawRect.point.x + drawRect.extent.x) - paddingRightBottom.x)
- mTextOffset.x = (drawRect.point.x + drawRect.extent.x) - paddingRightBottom.x - textWidth;
- }
- }
- // calculate the cursor
- if( isFocused && mActive )
- {
- // Where in the string are we?
- S32 cursorOffset=0, charWidth=0;
- UTF16 tempChar = textBuffer.getChar(mCursorPos);
- // Alright, we want to terminate things momentarily.
- if(mCursorPos > 0)
- {
- cursorOffset = mProfile->mFont->getStrNWidth(textBuffer.getPtr(), mCursorPos);
- }
- else
- cursorOffset = 0;
- if( tempChar && mProfile->mFont->isValidChar( tempChar ) )
- charWidth = mProfile->mFont->getCharWidth( tempChar );
- else
- charWidth = paddingRightBottom.x;
- if( mTextOffset.x + cursorOffset + 1 >= (drawRect.point.x + drawRect.extent.x) ) // +1 is for the cursor width
- {
- // Cursor somewhere beyond the textcontrol,
- // skip forward roughly 25% of the total width (if possible)
- S32 skipForward = drawRect.extent.x / 4 * 3;
- if ( cursorOffset + skipForward > textWidth )
- mTextOffset.x = (drawRect.point.x + drawRect.extent.x) - paddingRightBottom.x - textWidth;
- else
- {
- //mTextOffset.x -= skipForward;
- S32 mul = (S32)( mFloor( (cursorOffset-drawRect.extent.x) / skipForward ) );
- mTextOffset.x -= skipForward * mul + drawRect.extent.x - 1; // -1 is for the cursor width
- }
- }
- else if( mTextOffset.x + cursorOffset < drawRect.point.x + paddingLeftTop.x )
- {
- // Cursor somewhere before the textcontrol
- // skip backward roughly 25% of the total width (if possible)
- S32 skipBackward = drawRect.extent.x / 4 * 3;
- if ( cursorOffset - skipBackward < 0 )
- mTextOffset.x = drawRect.point.x + paddingLeftTop.x;
- else
- {
- S32 mul = (S32)( mFloor( cursorOffset / skipBackward ) );
- mTextOffset.x += drawRect.point.x - mTextOffset.x - skipBackward * mul;
- }
- }
- cursorStart.x = mTextOffset.x + cursorOffset;
- #ifdef TORQUE_OS_MAC
- cursorStart.x += charWidth/2;
- #endif
-
- cursorEnd.x = cursorStart.x;
- S32 cursorHeight = mProfile->mFont->getHeight();
- if ( cursorHeight < drawRect.extent.y )
- {
- cursorStart.y = drawPoint.y;
- cursorEnd.y = cursorStart.y + cursorHeight;
- }
- else
- {
- cursorStart.y = drawRect.point.y;
- cursorEnd.y = cursorStart.y + drawRect.extent.y;
- }
- }
- //draw the text
- if ( !isFocused )
- mBlockStart = mBlockEnd = 0;
- //also verify the block start/end
- if ((mBlockStart > textBuffer.length() || (mBlockEnd > textBuffer.length()) || (mBlockStart > mBlockEnd)))
- mBlockStart = mBlockEnd = 0;
- Point2I tempOffset = mTextOffset;
- //draw the portion before the highlight
- if ( mBlockStart > 0 )
- {
- GFX->getDrawUtil()->setBitmapModulation( fontColor );
- const UTF16* preString2 = textBuffer.getPtr();
- GFX->getDrawUtil()->drawText( mProfile->mFont, tempOffset, preString2, mProfile->mFontColors );
- tempOffset.x += mProfile->mFont->getStrNWidth(preString2, mBlockStart);
- }
- //draw the highlighted portion
- if ( mBlockEnd > 0 )
- {
- const UTF16* highlightBuff = textBuffer.getPtr() + mBlockStart;
- U32 highlightBuffLen = mBlockEnd-mBlockStart;
- S32 highlightWidth = mProfile->mFont->getStrNWidth(highlightBuff, highlightBuffLen);
- GFX->getDrawUtil()->drawRectFill( Point2I( tempOffset.x, drawRect.point.y ),
- Point2I( tempOffset.x + highlightWidth, drawRect.point.y + drawRect.extent.y - 1),
- mProfile->mFontColorSEL );
- GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColorHL );
- GFX->getDrawUtil()->drawTextN( mProfile->mFont, tempOffset, highlightBuff, highlightBuffLen, mProfile->mFontColors );
- tempOffset.x += highlightWidth;
- }
- //draw the portion after the highlight
- if(mBlockEnd < textBuffer.length())
- {
- const UTF16* finalBuff = textBuffer.getPtr() + mBlockEnd;
- U32 finalBuffLen = textBuffer.length() - mBlockEnd;
- GFX->getDrawUtil()->setBitmapModulation( fontColor );
- GFX->getDrawUtil()->drawTextN( mProfile->mFont, tempOffset, finalBuff, finalBuffLen, mProfile->mFontColors );
- }
- //draw the cursor
- if ( isFocused && mCursorOn )
- GFX->getDrawUtil()->drawLine( cursorStart, cursorEnd, mProfile->mCursorColor );
- }
- bool GuiTextEditCtrl::hasText()
- {
- return (mTextBuffer.length());
- }
- void GuiTextEditCtrl::invalidText(bool playSound)
- {
- mTextValid = false;
- if (playSound)
- playDeniedSound();
- }
- void GuiTextEditCtrl::validText()
- {
- mTextValid = true;
- }
- bool GuiTextEditCtrl::isValidText()
- {
- return mTextValid;
- }
- void GuiTextEditCtrl::playDeniedSound()
- {
- if ( mDeniedSound )
- SFX->playOnce( mDeniedSound );
- }
- const char *GuiTextEditCtrl::getScriptValue()
- {
- return StringTable->insert(mTextBuffer.getPtr8());
- }
- void GuiTextEditCtrl::setScriptValue(const char *value)
- {
- mTextBuffer.set(value);
- mCursorPos = mTextBuffer.length();
- }
- void GuiTextEditCtrl::handleCharInput( U16 ascii )
- {
- S32 stringLen = mTextBuffer.length();
- // Get the character ready to add to a UTF8 string.
- UTF16 convertedChar[2] = { ascii, 0 };
- //see if it's a number field
- if ( mProfile->mNumbersOnly )
- {
- if (ascii == '-')
- {
- //a minus sign only exists at the beginning, and only a single minus sign
- if (mCursorPos != 0 && !isAllTextSelected())
- {
- invalidText();
- return;
- }
- if (mInsertOn && (mTextBuffer.getChar(0) == '-'))
- {
- invalidText();
- return;
- }
- }
- // BJTODO: This is probably not unicode safe.
- else if (ascii != '.' && (ascii < '0' || ascii > '9'))
- {
- invalidText();
- return;
- }
- else
- validText();
- }
- //save the current state
- saveUndoState();
- bool alreadyCut = false;
- //delete anything highlighted
- if ( mBlockEnd > 0 )
- {
- mTextBuffer.cut(mBlockStart, mBlockEnd-mBlockStart);
- mCursorPos = mBlockStart;
- mBlockStart = 0;
- mBlockEnd = 0;
- // We just changed the string length!
- // Get its new value.
- stringLen = mTextBuffer.length();
- // If we already had text highlighted, we just want to cut that text.
- // Don't cut the next character even if insert is not on.
- alreadyCut = true;
- }
- if ( ( mInsertOn && ( stringLen < mMaxStrLen ) ) ||
- ( !mInsertOn && ( mCursorPos < mMaxStrLen ) ) )
- {
- if ( mCursorPos == stringLen )
- {
- mTextBuffer.append(convertedChar);
- mCursorPos++;
- }
- else
- {
- if ( mInsertOn || alreadyCut )
- {
- mTextBuffer.insert(mCursorPos, convertedChar);
- mCursorPos++;
- }
- else
- {
- mTextBuffer.cut(mCursorPos, 1);
- mTextBuffer.insert(mCursorPos, convertedChar);
- mCursorPos++;
- }
- }
- }
- else
- playDeniedSound();
- //reset the history index
- mHistoryDirty = true;
- //execute the console command if it exists
- execConsoleCallback();
- }
- S32 GuiTextEditCtrl::findPrevWord()
- {
- // First the first word to the left of the current cursor position
- // and return the positional index of its starting character.
- // We define the first character of a word as any non-whitespace
- // character which has a non-alpha-numeric character to its immediate left.
- const UTF8* text = mTextBuffer.getPtr8();
- for ( S32 i = mCursorPos - 1; i > 0; i-- )
- {
- if ( !dIsspace( text[i] ) )
- {
- if ( !dIsalnum( text[i-1] ) )
- {
- return i;
- }
- }
- }
- return 0;
- }
- S32 GuiTextEditCtrl::findNextWord()
- {
- // First the first word to the right of the current cursor position
- // and return the positional index of its starting character.
- // We define the first character of a word as any non-whitespace
- // character which has a non-alpha-numeric character to its immediate left.
-
- const UTF8* text = mTextBuffer.getPtr8();
- for ( S32 i = mCursorPos + 1; i < mTextBuffer.length(); i++ )
- {
- if ( !dIsspace( text[i] ) )
- {
- if ( !dIsalnum( text[i-1] ) )
- {
- return i;
- }
- }
- }
- return mTextBuffer.length();
- }
- DefineEngineMethod( GuiTextEditCtrl, getText, const char*, (),,
- "@brief Acquires the current text displayed in this control.\n\n"
- "@tsexample\n"
- "// Acquire the value of the text control.\n"
- "%text = %thisGuiTextEditCtrl.getText();\n"
- "@endtsexample\n\n"
- "@return The current text within the control.\n\n"
- "@see GuiControl")
- {
- if( !object->hasText() )
- return StringTable->insert("");
- char *retBuffer = Con::getReturnBuffer( GuiTextEditCtrl::MAX_STRING_LENGTH );
- object->getText( retBuffer );
- return retBuffer;
- }
- DefineEngineMethod( GuiTextEditCtrl, setText, void, (const char* text),,
- "@brief Sets the text in the control.\n\n"
- "@param text Text to place in the control.\n"
- "@tsexample\n"
- "// Define the text to display\n"
- "%text = \"Text!\"\n\n"
- "// Inform the GuiTextEditCtrl to display the defined text\n"
- "%thisGuiTextEditCtrl.setText(%text);\n"
- "@endtsexample\n\n"
- "@see GuiControl")
- {
- object->setText( text );
- }
- DefineEngineMethod( GuiTextEditCtrl, getCursorPos, S32, (),,
- "@brief Returns the current position of the text cursor in the control.\n\n"
- "@tsexample\n"
- "// Acquire the cursor position in the control\n"
- "%position = %thisGuiTextEditCtrl.getCursorPost();\n"
- "@endtsexample\n\n"
- "@return Text cursor position within the control.\n\n"
- "@see GuiControl")
- {
- return( object->getCursorPos() );
- }
- DefineEngineMethod( GuiTextEditCtrl, setCursorPos, void, (S32 position),,
- "@brief Sets the text cursor at the defined position within the control.\n\n"
- "@param position Text position to set the text cursor.\n"
- "@tsexample\n"
- "// Define the cursor position\n"
- "%position = \"12\";\n\n"
- "// Inform the GuiTextEditCtrl control to place the text cursor at the defined position\n"
- "%thisGuiTextEditCtrl.setCursorPos(%position);\n"
- "@endtsexample\n\n"
- "@see GuiControl")
- {
- object->setCursorPos( position );
- }
- DefineEngineMethod( GuiTextEditCtrl, isAllTextSelected, bool, (),,
- "@brief Checks to see if all text in the control has been selected.\n\n"
- "@tsexample\n"
- "// Check to see if all text has been selected or not.\n"
- "%allSelected = %thisGuiTextEditCtrl.isAllTextSelected();\n"
- "@endtsexample\n\n"
- "@return True if all text in the control is selected, otherwise false.\n\n"
- "@see GuiControl")
- {
- return object->isAllTextSelected();
- }
- DefineEngineMethod( GuiTextEditCtrl, selectAllText, void, (),,
- "@brief Selects all text within the control.\n\n"
- "@tsexample\n"
- "// Inform the control to select all of its text.\n"
- "%thisGuiTextEditCtrl.selectAllText();\n"
- "@endtsexample\n\n"
- "@see GuiControl")
- {
- object->selectAllText();
- }
- DefineEngineMethod( GuiTextEditCtrl, clearSelectedText, void, (),,
- "@brief Unselects all selected text in the control.\n\n"
- "@tsexample\n"
- "// Inform the control to unselect all of its selected text\n"
- "%thisGuiTextEditCtrl.clearSelectedText();\n"
- "@endtsexample\n\n"
- "@see GuiControl")
- {
- object->clearSelectedText();
- }
- DefineEngineMethod( GuiTextEditCtrl, forceValidateText, void, (),,
- "@brief Force a validation to occur.\n\n"
- "@tsexample\n"
- "// Inform the control to force a validation of its text.\n"
- "%thisGuiTextEditCtrl.forceValidateText();\n"
- "@endtsexample\n\n"
- "@see GuiControl")
- {
- object->forceValidateText();
- }
- DefineEngineMethod(GuiTextEditCtrl, invalidText, void, (bool playSound), (true),
- "@brief Trigger the invalid sound and make the box red.nn"
- "@param playSound Play the invalid text sound or not.n")
- {
- object->invalidText(playSound);
- }
- DefineEngineMethod(GuiTextEditCtrl, validText, void, (), ,
- "@brief Restores the box to normal color.nn")
- {
- object->validText();
- }
- DefineEngineMethod(GuiTextEditCtrl, isValidText, bool, (), ,
- "@brief Returns if the text is set to valid or not.n"
- "@Return true if text is set to valid, false if not.nn")
- {
- return object->isValidText();
- }
|