1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2013 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 "console/consoleTypes.h"
- #include "console/console.h"
- #include "graphics/dgl.h"
- #include "gui/guiCanvas.h"
- #include "gui/guiMLTextCtrl.h"
- #include "gui/guiTextEditCtrl.h"
- #include "gui/guiDefaultControlRender.h"
- #include "memory/frameAllocator.h"
- #include "string/unicode.h"
- IMPLEMENT_CONOBJECT(GuiTextEditCtrl);
- U32 GuiTextEditCtrl::smNumAwake = 0;
- 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;
- mTextOffsetReset = true;
- mHistoryDirty = false;
- mHistorySize = 0;
- mHistoryLast = -1;
- mHistoryIndex = 0;
- mHistoryBuf = NULL;
- mValidateCommand = StringTable->EmptyString;
- mEscapeCommand = StringTable->EmptyString;
- mPasswordMask = StringTable->insert( "*" );
- mEditCursor = NULL;
- }
- 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()
- {
- Parent::initPersistFields();
- addField("validate", TypeString, Offset(mValidateCommand, GuiTextEditCtrl));
- addField("escapeCommand", TypeString, Offset(mEscapeCommand, GuiTextEditCtrl));
- addField("historySize", TypeS32, Offset(mHistorySize, GuiTextEditCtrl));
- addField("tabComplete", TypeBool, Offset(mTabComplete, GuiTextEditCtrl));
- addField("deniedSound", TypeAudioAssetPtr, Offset(mDeniedSound, GuiTextEditCtrl));
- addField("sinkAllKeyEvents", TypeBool, Offset(mSinkAllKeyEvents, GuiTextEditCtrl));
- addField("password", TypeBool, Offset(mPasswordText, GuiTextEditCtrl));
- addField("passwordMask", TypeString, Offset(mPasswordMask, GuiTextEditCtrl));
- }
- 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[0] )
- {
- setText(mText);
- }
- return true;
- }
- void GuiTextEditCtrl::onStaticModified(const char* slotName)
- {
- if(!dStricmp(slotName, "text"))
- setText(mText);
- }
- bool GuiTextEditCtrl::onWake()
- {
- if (! Parent::onWake())
- return false;
- // If this is the first awake text edit control, enable keyboard translation
- if (smNumAwake == 0)
- Platform::enableKeyboardTranslation();
- ++smNumAwake;
- return true;
- }
- void GuiTextEditCtrl::onSleep()
- {
- Parent::onSleep();
- // If this is the last awake text edit control, disable keyboard translation
- --smNumAwake;
- if (smNumAwake == 0)
- Platform::disableKeyboardTranslation();
- }
- void GuiTextEditCtrl::execConsoleCallback()
- {
- // Execute the console command!
- if ( mConsoleCommand[0] )
- {
- char buf[16];
- dSprintf( buf, sizeof( buf ), "%d", getId() );
- Con::setVariable( "$ThisControl", buf );
- Con::evaluate( mConsoleCommand, false );
- }
- // Update the console variable:
- if ( mConsoleVariable[0] )
- Con::setVariable( mConsoleVariable, mTextBuffer.getPtr8() );
- }
- void GuiTextEditCtrl::updateHistory( StringBuffer *inTxt, bool moveIndex )
- {
- const UTF16* txt = inTxt->getPtr();
- if(!txt)
- return;
- if(!mHistorySize)
- 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 < (U32)(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::setText( const UTF8 *txt )
- {
- if(txt && txt[0] != 0)
- {
- Parent::setText(txt);
- mTextBuffer.set( txt );
- }
- else
- mTextBuffer.set( "" );
- //respect the max size
- int diff = mTextBuffer.length() - mMaxStrLen;
- if( diff > 0 ) {
- mTextBuffer.cut( mMaxStrLen, diff );
- }
- mCursorPos = mTextBuffer.length();
- }
- void GuiTextEditCtrl::setText( const UTF16* txt)
- {
- if(txt && txt[0] != 0)
- {
- UTF8* txt8 = convertUTF16toUTF8( txt );
- Parent::setText( txt8 );
- delete[] txt8;
- mTextBuffer.set( txt );
- }
- else
- {
- Parent::setText("");
- mTextBuffer.set("");
- }
-
- //respect the max size
- int diff = mTextBuffer.length() - mMaxStrLen;
- if( diff > 0 ) {
- mTextBuffer.cut( mMaxStrLen, diff );
- }
- mCursorPos = mTextBuffer.length();
- }
- void GuiTextEditCtrl::selectAllText()
- {
- mBlockStart = 0;
- mBlockEnd = mTextBuffer.length();
- setUpdate();
- }
- void GuiTextEditCtrl::forceValidateText()
- {
- if ( mValidateCommand[0] )
- {
- char buf[16];
- dSprintf(buf, sizeof(buf), "%d", getId());
- Con::setVariable("$ThisControl", buf);
- Con::evaluate( mValidateCommand, false );
- }
- }
- void GuiTextEditCtrl::reallySetCursorPos( const S32 newPos )
- {
- S32 charCount = mTextBuffer.length();
- S32 realPos = newPos > charCount ? charCount : newPos < 0 ? 0 : newPos;
- if ( realPos != mCursorPos )
- {
- mCursorPos = realPos;
- setUpdate();
- }
- }
- S32 GuiTextEditCtrl::setCursorPos( const Point2I &offset )
- {
- Point2I ctrlOffset = localToGlobalCoord( Point2I( 0, 0 ) );
- S32 charLength = 0;
- S32 curX;
- curX = offset.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 + mBounds.extent.x )
- return -2;
- curX = offset.x - mTextOffset.x;
- S32 count=0;
- if(mTextBuffer.length() == 0)
- return 0;
- for(count=0; count< (S32)mTextBuffer.length(); count++)
- {
- UTF16 c = mTextBuffer.getChar(count);
- if(!mPasswordText && !mFont->isValidChar(c))
- continue;
-
- if(mPasswordText)
- charLength += mFont->getCharXIncrement( mPasswordMask[0] );
- else
- charLength += mFont->getCharXIncrement( c );
- if ( charLength > curX )
- break;
- }
- return count;
- }
- void GuiTextEditCtrl::onTouchDown( 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 = setCursorPos( 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();
- // Notify Script.
- if( isMethod("onTouchDown") )
- {
- char buf[3][32];
- dSprintf(buf[0], 32, "%d", event.modifier);
- dSprintf(buf[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
- dSprintf(buf[2], 32, "%d", event.mouseClickCount);
- Con::executef(this, 4, "onTouchDown", buf[0], buf[1], buf[2]);
- }
- }
- void GuiTextEditCtrl::onTouchDragged( const GuiEvent &event )
- {
- S32 pos = setCursorPos( 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::onTouchDragged(event);
- // Notify Script.
- if( isMethod("onTouchDragged") )
- {
- char buf[3][32];
- dSprintf(buf[0], 32, "%d", event.modifier);
- dSprintf(buf[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
- dSprintf(buf[2], 32, "%d", event.mouseClickCount);
- Con::executef(this, 4, "onTouchDragged", buf[0], buf[1], buf[2]);
- }
- }
- void GuiTextEditCtrl::onTouchUp(const GuiEvent &event)
- {
- mDragHit = false;
- mScrollDir = 0;
- mouseUnlock();
- // Notify Script.
- if( isMethod("onTouchUp") )
- {
- char buf[3][32];
- dSprintf(buf[0], 32, "%d", event.modifier);
- dSprintf(buf[1], 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
- dSprintf(buf[2], 32, "%d", event.mouseClickCount);
- Con::executef(this, 4, "onTouchUp", buf[0], buf[1], buf[2]);
- }
- }
- 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())
- 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)
- {
- switch (event.keyCode)
- {
- case KEY_TAB:
- if ( mTabComplete )
- {
- Con::executef( this, 2, "onTabComplete", "1" );
- return( true );
- }
- break; //*** DAW: 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;
- }
- }
- else if (event.modifier & SI_CTRL)
- {
- switch(event.keyCode)
- {
- // Added UNIX emacs key bindings - just a little hack here...
- // BJGTODO: Add vi bindings.
- // 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
- #if !(defined(TORQUE_OS_OSX) || defined(TORQUE_OS_IOS))
- // 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;
- // [neo, 5/24/2007 - #2986]
- // We don't want to embed control characters in the text, so just return false
- // so that any that any other consumer can have a bash at the input.
- default:
- return false;
- }
- }
- #if (defined(TORQUE_OS_OSX) || defined(TORQUE_OS_IOS))
- // mac style cut / copy / paste / undo keybinds
- else if (event.modifier & SI_ALT)
- {
- // Added Mac cut/copy/paste/undo keys
- // Mac command key maps to alt in torque.
- switch(event.keyCode)
- {
- 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;
- }
- }
- }
- #endif
- else
- {
- switch(event.keyCode)
- {
- case KEY_ESCAPE:
- if ( mEscapeCommand[0] )
- {
- Con::evaluate( mEscapeCommand );
- return( true );
- }
- return( Parent::onKeyDown( event ) );
- case KEY_RETURN:
- case KEY_NUMPADENTER:
- //first validate
- if (mProfile->mReturnTab)
- {
- //execute the validate command
- if ( mValidateCommand[0] )
- {
- char buf[16];
- dSprintf(buf, sizeof(buf), "%d", getId());
- Con::setVariable("$ThisControl", buf);
- Con::evaluate( mValidateCommand, false );
- }
-
- if( isMethod( "onValidate" ) )
- Con::executef( this, 2, "onValidate" );
- }
- updateHistory(&mTextBuffer, true);
- mHistoryDirty = false;
- //next exec the alt console command
- execAltConsoleCallback();
- // Notify of Return
- if ( isMethod("onReturn") )
- Con::executef( this, 2, "onReturn" );
- if (mProfile->mReturnTab)
- {
- GuiCanvas *root = getRoot();
- if (root)
- {
- root->tabNext();
- return true;
- }
- }
- return true;
- case KEY_UP:
- {
- 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(mHistoryDirty)
- {
- updateHistory(&mTextBuffer, false);
- mHistoryDirty = false;
- }
- mHistoryIndex++;
- if(mHistoryIndex > mHistoryLast)
- {
- mHistoryIndex = mHistoryLast + 1;
- setText("");
- }
- else
- setText(mHistoryBuf[mHistoryIndex]);
- return true;
- case KEY_LEFT:
- mBlockStart = 0;
- mBlockEnd = 0;
- if (mCursorPos > 0)
- {
- mCursorPos--;
- }
- return true;
- case KEY_RIGHT:
- mBlockStart = 0;
- mBlockEnd = 0;
- if (mCursorPos < stringLen)
- {
- mCursorPos++;
- }
- 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;
- }
- }
- switch ( event.keyCode )
- {
- case KEY_TAB:
- if ( mTabComplete )
- {
- Con::executef( this, 2, "onTabComplete", "0" );
- return( true );
- }
- case KEY_UP:
- case KEY_DOWN:
- case KEY_ESCAPE:
- return Parent::onKeyDown( event );
- }
- if(mFont.isNull())
- return false;
- if ( mFont->isValidChar( event.ascii ) )
- {
- // Get the character ready to add to a UTF8 string.
- UTF16 convertedChar[2] = { event.ascii, 0 };
- //see if it's a number field
- if ( mProfile->mNumbersOnly )
- {
- if (event.ascii == '-')
- {
- //a minus sign only exists at the beginning, and only a single minus sign
- if ( mCursorPos != 0 )
- {
- playDeniedSound();
- return true;
- }
- if ( mInsertOn && ( mTextBuffer.getChar(0) == '-' ) )
- {
- playDeniedSound();
- return true;
- }
- }
- // BJTODO: This is probably not unicode safe.
- else if ( event.ascii < '0' || event.ascii > '9' )
- {
- playDeniedSound();
- return true;
- }
- }
- //save the current state
- saveUndoState();
- //delete anything highlighted
- if ( mBlockEnd > 0 )
- {
- mTextBuffer.cut(mBlockStart, mBlockEnd-mBlockStart);
- mCursorPos = mBlockStart;
- mBlockStart = 0;
- mBlockEnd = 0;
- }
- if ( ( mInsertOn && ( stringLen < mMaxStrLen ) ) ||
- ( !mInsertOn && ( mCursorPos < mMaxStrLen ) ) )
- {
- if ( mCursorPos == stringLen )
- {
- mTextBuffer.append(convertedChar);
- mCursorPos++;
- }
- else
- {
- if ( mInsertOn )
- {
- 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();
- return true;
- }
- //not handled - pass the event to it's parent
- // Or eat it if that's appropriate.
- if (mSinkAllKeyEvents)
- return true;
- return Parent::onKeyDown( event );
- }
- void GuiTextEditCtrl::setFirstResponder()
- {
- Parent::setFirstResponder();
-
- #if !defined(TORQUE_OS_IOS) && !defined(TORQUE_OS_ANDROID)
- Platform::enableKeyboardTranslation();
- #endif
- }
- void GuiTextEditCtrl::onLoseFirstResponder()
- {
- Platform::disableKeyboardTranslation();
- //first, update the history
- updateHistory( &mTextBuffer, true );
- //execute the validate command
- if ( mValidateCommand[0] )
- {
- char buf[16];
- dSprintf(buf, sizeof(buf), "%d", getId());
- Con::setVariable("$ThisControl", buf);
- Con::evaluate( mValidateCommand, false );
- }
- if( isMethod( "onValidate" ) )
- Con::executef( this, 2, "onValidate" );
- if( isMethod( "onLoseFirstResponder" ) )
- Con::executef( this, 2, "onLoseFirstResponder" );
- // Redraw the control:
- setUpdate();
- }
- void GuiTextEditCtrl::parentResized(const Point2I &oldParentExtent, const Point2I &newParentExtent)
- {
- Parent::parentResized( oldParentExtent, newParentExtent );
- mTextOffsetReset = true;
- }
- void GuiTextEditCtrl::onRender(Point2I offset, const RectI & updateRect)
- {
- //Notice that there's no Highlight state. The HL colors are used for selected text - not hover.
- //The selected state is used when the box has the focus and can be typed in.
- GuiControlState currentState = NormalState;
- if (!mActive)
- {
- currentState = DisabledState;
- }
- else if (isFirstResponder())
- {
- currentState = SelectedState;
- }
- RectI ctrlRect = applyMargins(offset, mBounds.extent, currentState, mProfile);
- if (!ctrlRect.isValidRect())
- {
- return;
- }
- renderBorderedRect(ctrlRect, mProfile, currentState);
- //Render Text
- dglSetBitmapModulation(mProfile->mFontColor);
- RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
- RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
- if (contentRect.isValidRect())
- {
- drawText(contentRect, currentState);
- //Render the childen
- renderChildControls(offset, contentRect, updateRect);
- }
- }
- 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, GuiControlState currentState )
- {
- StringBuffer textBuffer;
- Point2I drawPoint = drawRect.point;
- // Just a little sanity.
- if(mCursorPos > (S32)mTextBuffer.length())
- mCursorPos = (S32)mTextBuffer.length();
- if(mCursorPos < 0)
- mCursorPos = 0;
- // Apply password masking (make the masking char optional perhaps?)
- if(mPasswordText)
- {
- for(U32 i = 0; i<mTextBuffer.length(); i++)
- textBuffer.append(mPasswordMask);
- }
- else
- {
- // Or else just copy it over.
- textBuffer.set(&mTextBuffer);
- }
- // Center vertically:
- drawPoint.y += ( ( drawRect.extent.y - mFont->getHeight() ) / 2 );
- // Align horizontally:
-
- S32 textWidth = mFont->getStrNWidth(textBuffer.getPtr(), textBuffer.length());
- if ( drawRect.extent.x > textWidth )
- {
- switch( mProfile->mAlignment )
- {
- case GuiControlProfile::RightAlign:
- drawPoint.x += ( drawRect.extent.x - textWidth );
- break;
- case GuiControlProfile::CenterAlign:
- drawPoint.x += ( ( drawRect.extent.x - textWidth ) / 2 );
- break;
- }
- }
- ColorI fontColor = mActive ? mProfile->mFontColor : mProfile->mFontColorNA;
- // now draw the text
- Point2I cursorStart, cursorEnd;
- mTextOffset = drawPoint;
- if ( mTextOffsetReset )
- {
- mTextOffset.x = drawPoint.x;
- mTextOffsetReset = false;
- }
- if ( drawRect.extent.x > textWidth )
- mTextOffset.x = drawPoint.x;
- else
- {
- // Alignment affects large text
- if ( mProfile->mAlignment == GuiControlProfile::RightAlign
- || mProfile->mAlignment == GuiControlProfile::CenterAlign )
- {
- if ( mTextOffset.x + textWidth < (drawRect.point.x + drawRect.extent.x))
- mTextOffset.x = (drawRect.point.x + drawRect.extent.x) - textWidth;
- }
- }
- // calculate the cursor
- if ( currentState == SelectedState )
- {
- // Where in the string are we?
- S32 cursorOffset=0, charWidth=0;
- UTF16 tempChar = mTextBuffer.getChar(mCursorPos);
- // Alright, we want to terminate things momentarily.
- if(mCursorPos > 0)
- {
- cursorOffset = mFont->getStrNWidth(textBuffer.getPtr(), mCursorPos);
- }
- else
- cursorOffset = 0;
- if ( tempChar )
- charWidth = mFont->getCharWidth( tempChar );
- else
- charWidth = 0;
- if( mTextOffset.x + cursorOffset + charWidth >= (drawRect.point.x + drawRect.extent.x))
- {
- // Cursor somewhere beyond the textcontrol,
- // skip forward roughly 25% of the total width (if possible)
- S32 skipForward = drawRect.extent.x / 4;
- if ( cursorOffset + skipForward > textWidth )
- mTextOffset.x = (drawRect.point.x + drawRect.extent.x) - textWidth;
- else
- mTextOffset.x -= skipForward;
- }
- else if( mTextOffset.x + cursorOffset < drawRect.point.x)
- {
- // Cursor somewhere before the textcontrol
- // skip backward roughly 25% of the total width (if possible)
- S32 skipBackward = drawRect.extent.x / 4;
- if ( cursorOffset - skipBackward < 0 )
- mTextOffset.x = drawRect.point.x;
- else
- mTextOffset.x += skipBackward;
- }
- cursorStart.x = mTextOffset.x + cursorOffset;
- cursorEnd.x = cursorStart.x;
- S32 cursorHeight = 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 ( currentState != SelectedState )
- mBlockStart = mBlockEnd = 0;
- //also verify the block start/end
- if ((mBlockStart > (S32)textBuffer.length() || (mBlockEnd > (S32)textBuffer.length()) || (mBlockStart > mBlockEnd)))
- mBlockStart = mBlockEnd = 0;
- Point2I tempOffset = mTextOffset;
- //draw the portion before the highlight
- if ( mBlockStart > 0 )
- {
- dglSetBitmapModulation( fontColor );
- const UTF16* preString2 = textBuffer.getPtr();
- dglDrawTextN( mFont, tempOffset, preString2, mBlockStart, mProfile->mFontColors);
- tempOffset.x += mFont->getStrNWidth(preString2, mBlockStart);
- }
- //draw the hilighted portion
- if ( mBlockEnd > 0 )
- {
- const UTF16* highlightBuff = textBuffer.getPtr() + mBlockStart;
- U32 highlightBuffLen = mBlockEnd-mBlockStart;
- S32 highlightWidth = mFont->getStrNWidth(highlightBuff, highlightBuffLen);
- dglDrawRectFill( Point2I( tempOffset.x, drawRect.point.y + 1 ),
- Point2I( tempOffset.x + highlightWidth, drawRect.point.y + drawRect.extent.y - 1),
- mProfile->mFillColorHL );
- dglSetBitmapModulation( mProfile->mFontColorHL );
- dglDrawTextN( mFont, tempOffset, highlightBuff, highlightBuffLen, mProfile->mFontColors );
- tempOffset.x += highlightWidth;
- }
- //draw the portion after the highlite
- if(mBlockEnd < (S32)mTextBuffer.length())
- {
- // Special handling if we are truncating when the ctrl is unfocused
- if (currentState != SelectedState && mTruncateWhenUnfocused)
- {
- StringBuffer terminationString = "...";
- S32 width = mBounds.extent.x;
- StringBuffer truncatedBuffer = truncate(textBuffer, terminationString, width);
- const UTF16* truncatedBufferPtr = truncatedBuffer.getPtr();
- U32 finalBuffLen = truncatedBuffer.length();
- dglSetBitmapModulation( fontColor );
- dglDrawTextN( mFont, tempOffset, truncatedBufferPtr, finalBuffLen, mProfile->mFontColors );
- }
- else
- {
- const UTF16* finalBuff = textBuffer.getPtr() + mBlockEnd;
- U32 finalBuffLen = mTextBuffer.length() - mBlockEnd;
- dglSetBitmapModulation( fontColor );
- dglDrawTextN( mFont, tempOffset, finalBuff, finalBuffLen, mProfile->mFontColors );
- }
- }
- //draw the cursor
- if (currentState == SelectedState && mCursorOn )
- dglDrawLine( cursorStart, cursorEnd, mProfile->mCursorColor );
- }
- bool GuiTextEditCtrl::hasText()
- {
- return (mTextBuffer.length());
- }
- void GuiTextEditCtrl::playDeniedSound()
- {
- if ( mDeniedSound.notNull() )
- {
- AUDIOHANDLE handle = alxCreateSource( mDeniedSound );
- alxPlay( handle );
- }
- }
- const char *GuiTextEditCtrl::getScriptValue()
- {
- return StringTable->insert(mTextBuffer.getPtr8());
- }
- void GuiTextEditCtrl::setScriptValue(const char *value)
- {
- mTextBuffer.set(value);
- mCursorPos = getMin((S32)(mTextBuffer.length() - 1), 0);
- }
- ConsoleMethod( GuiTextEditCtrl, getText, const char*, 2, 2, "() Get the contents of the textedit control\n"
- "@return Returns the current textedit buffer.")
- {
- if( !object->hasText() )
- return StringTable->EmptyString;
- char *retBuffer = Con::getReturnBuffer( GuiTextEditCtrl::MAX_STRING_LENGTH );
- object->getText( retBuffer );
- return retBuffer;
- }
- ConsoleMethod( GuiTextEditCtrl, getCursorPos, S32, 2, 2, "() Use the getCursorPos method to get the current position of the text cursor in the control.\n"
- "@return Returns the current position of the text cursor in the control, where 0 is at the beginning of the line, 1 is after the first letter, and so on.\n"
- "@sa setCursorPos")
- {
- return( object->getCursorPos() );
- }
- ConsoleMethod( GuiTextEditCtrl, setCursorPos, void, 3, 3, "( newPos ) Use the setCursorPos method to set the current position of text cursor to newPos.\n"
- "If the requested position is beyond the end of text, the cursor will be placed after the last letter. If the value is less than zero, the cursor will be placed at the front of the entry.\n"
- "@param newPos The new position to place the cursor at, where 0 is at the beginning of the line, 1 is after the first letter, and so on.\n"
- "@return No return value.\n"
- "@sa getCursorPos")
- {
- object->reallySetCursorPos( dAtoi( argv[2] ) );
- }
- ConsoleMethod( GuiTextEditCtrl, selectAllText, void, 2, 2, "textEditCtrl.selectAllText()" )
- {
- object->selectAllText();
- }
- ConsoleMethod( GuiTextEditCtrl, forceValidateText, void, 2, 2, "textEditCtrl.forceValidateText()" )
- {
- object->forceValidateText();
- }
|