12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293 |
- /**
- * @file lllineeditor.cpp
- * @brief LLLineEditor base class
- *
- * $LicenseInfo:firstyear=2001&license=viewergpl$
- *
- * Copyright (c) 2001-2009, Linden Research, Inc.
- *
- * Second Life Viewer Source Code
- * The source code in this file ("Source Code") is provided by Linden Lab
- * to you under the terms of the GNU General Public License, version 2.0
- * ("GPL"), unless you have obtained a separate licensing agreement
- * ("Other License"), formally executed by you and Linden Lab. Terms of
- * the GPL can be found in doc/GPL-license.txt in this distribution, or
- * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
- *
- * There are special exceptions to the terms and conditions of the GPL as
- * it is applied to this Source Code. View the full text of the exception
- * in the file doc/FLOSS-exception.txt in this software distribution, or
- * online at
- * http://secondlifegrid.net/programs/open_source/licensing/flossexception
- *
- * By copying, modifying or distributing this software, you acknowledge
- * that you have read and understood your obligations described above,
- * and agree to abide by those obligations.
- *
- * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
- * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
- * COMPLETENESS OR PERFORMANCE.
- * $/LicenseInfo$
- */
- // Text editor widget to let users enter a single line.
- #include "linden_common.h"
- #include "lllineeditor.h"
- #include "llbutton.h"
- #include "llclipboard.h"
- #include "llcontrol.h"
- #include "llfasttimer.h"
- #include "llgl.h"
- #include "llkeyboard.h"
- #include "lllocale.h"
- #include "llmenugl.h"
- #include "llrect.h"
- #include "llspellcheck.h"
- #include "llstring.h"
- #include "lltimer.h"
- #include "lltrans.h"
- #include "lluictrlfactory.h"
- #include "llwindow.h"
- // gcc 12.4+ sees array bound issues where there are none... HB
- #if defined(GCC_VERSION) && GCC_VERSION >= 120400
- # pragma GCC diagnostic ignored "-Warray-bounds"
- # pragma GCC diagnostic ignored "-Wstringop-overflow"
- #endif
- //
- // Constants
- //
- constexpr S32 UI_LINEEDITOR_CURSOR_THICKNESS = 2;
- constexpr S32 UI_LINEEDITOR_H_PAD = 2;
- constexpr S32 UI_LINEEDITOR_V_PAD = 1;
- constexpr F32 CURSOR_FLASH_DELAY = 1.f; // In seconds
- constexpr S32 SCROLL_INCREMENT_ADD = 0; // Make space for typing
- constexpr S32 SCROLL_INCREMENT_DEL = 4; // Make space for baskspacing
- constexpr F32 AUTO_SCROLL_TIME = 0.05f;
- constexpr F32 MARKER_BRIGHTNESS = 0.4f;
- constexpr F32 STANDOUT_BRIGHTNESS = 0.6f;
- constexpr S32 PREEDIT_BORDER = 1;
- ///////////////////////////////////////////////////////////////////////////////
- // LLLineEditor class
- ///////////////////////////////////////////////////////////////////////////////
- static const std::string LL_LINE_EDITOR_TAG = "line_editor";
- static LLRegisterWidget<LLLineEditor> r06(LL_LINE_EDITOR_TAG);
- //static
- LLUIImagePtr LLLineEditor::sImage;
- LLLineEditor::LLLineEditor(const std::string& name, const LLRect& rect,
- const std::string& default_text,
- const LLFontGL* font, S32 max_length_bytes,
- void (*commit_callback)(LLUICtrl* caller,
- void* user_data),
- void (*keystroke_callback)(LLLineEditor* caller,
- void* user_data),
- void (*focus_lost_callback)(LLFocusableElement* caller,
- void* user_data),
- void* userdata,
- LLLinePrevalidateFunc prevalidate_func,
- LLViewBorder::EBevel border_bevel,
- LLViewBorder::EStyle border_style,
- S32 border_thickness)
- : LLEditMenuHandler(HAS_CONTEXT_MENU | HAS_CUSTOM),
- LLUICtrl(name, rect, true, commit_callback, userdata,
- FOLLOWS_TOP | FOLLOWS_LEFT),
- mMaxLengthBytes(max_length_bytes),
- mCursorPos(0),
- mScrollHPos(0),
- mTextPadLeft(0),
- mTextPadRight(0),
- mCommitOnFocusLost(true),
- mRevertOnEsc(true),
- mKeystrokeCallback(keystroke_callback),
- mOnHandleKeyCallback(NULL),
- mOnHandleKeyData(NULL),
- mScrolledCallback(NULL),
- mScrolledCallbackData(NULL),
- mIsSelecting(false),
- mSelectionStart(0),
- mSelectionEnd(0),
- mLastSelectionX(-1),
- mLastSelectionY(-1),
- mLastSelectionStart(-1),
- mLastSelectionEnd(-1),
- mPrevalidateFunc(prevalidate_func),
- mCursorColor(LLUI::sTextCursorColor),
- mFgColor(LLUI::sTextFgColor),
- mReadOnlyFgColor(LLUI::sTextFgReadOnlyColor),
- mTentativeFgColor(LLUI::sTextFgTentativeColor),
- mWriteableBgColor(LLUI::sTextBgWriteableColor),
- mReadOnlyBgColor(LLUI::sTextBgReadOnlyColor),
- mFocusBgColor(LLUI::sTextBgFocusColor),
- mBorderThickness(border_thickness),
- mIgnoreArrowKeys(false),
- mIgnoreTab(true),
- mDrawAsterixes(false),
- mHandleEditKeysDirectly(false),
- mSelectAllonFocusReceived(false),
- mPassDelete(false),
- mReadOnly(false),
- mHaveHistory(false),
- mImage(sImage),
- mReplaceNewlinesWithSpaces(true),
- mSpellCheck(false)
- {
- llassert(max_length_bytes > 0);
- // Initialize current history line iterator
- mCurrentHistoryLine = mLineHistory.begin();
- if (font)
- {
- mGLFont = font;
- }
- else
- {
- mGLFont = LLFontGL::getFontSansSerifSmall();
- }
- setFocusLostCallback(focus_lost_callback);
- setTextPadding(0, 0);
- mScrollTimer.reset();
- setText(default_text);
- setCursor(mText.length());
- // Scalable UI somehow made these rectangles off-by-one.
- // I don't know why. JC
- LLRect border_rect(0, getRect().getHeight() - 1,
- getRect().getWidth() - 1, 0);
- mBorder = new LLViewBorder("line ed border", border_rect, border_bevel,
- border_style, mBorderThickness);
- addChild(mBorder);
- mBorder->setFollows(FOLLOWS_LEFT | FOLLOWS_RIGHT | FOLLOWS_TOP |
- FOLLOWS_BOTTOM);
- if (!sImage)
- {
- sImage = LLUI::getUIImage("sm_rounded_corners_simple.tga");
- }
- mImage = sImage;
- mShowMisspelled = LLSpellCheck::getInstance()->getShowMisspelled();
- }
- LLLineEditor::~LLLineEditor()
- {
- mCommitOnFocusLost = false;
- gFocusMgr.releaseFocusIfNeeded(this);
- }
- void LLLineEditor::onFocusReceived()
- {
- grabMenuHandler();
- LLUICtrl::onFocusReceived();
- updateAllowingLanguageInput();
- }
- void LLLineEditor::onFocusLost()
- {
- // The call to updateAllowLanguageInput() when loosing the keyboard focus
- // *may* indirectly invoke handleUnicodeCharHere(), so it must be called
- // before onCommit.
- updateAllowingLanguageInput();
- if (mCommitOnFocusLost && mText.getString() != mPrevText)
- {
- onCommit();
- }
- releaseMenuHandler();
- gWindowp->showCursorFromMouseMove();
- LLUICtrl::onFocusLost();
- }
- void LLLineEditor::onCommit()
- {
- // Put current line into the line history
- updateHistory();
- setControlValue(getValue());
- LLUICtrl::onCommit();
- resetDirty();
- selectAll();
- }
- // line history support
- void LLLineEditor::updateHistory()
- {
- // On history enabled line editors, remember committed line and reset
- // current history line number. Be sure only to remember lines that are not
- // empty and that are different from the last on the list.
- if (mHaveHistory && getLength())
- {
- if (!mLineHistory.empty())
- {
- // When not empty, last line of history should always be blank.
- if (mLineHistory.back().empty())
- {
- // discard the empty line
- mLineHistory.pop_back();
- }
- else
- {
- llwarns << "Last line of history was not blank." << llendl;
- }
- }
- // Add text to history, ignoring duplicates
- if (mLineHistory.empty() || getText() != mLineHistory.back())
- {
- mLineHistory.emplace_back(getText());
- }
- // Restore the blank line and set mCurrentHistoryLine to point at it
- mLineHistory.emplace_back("");
- mCurrentHistoryLine = mLineHistory.end() - 1;
- }
- }
- void LLLineEditor::reshape(S32 width, S32 height, bool called_from_parent)
- {
- LLUICtrl::reshape(width, height, called_from_parent);
- // For clamping side-effect:
- setTextPadding(mTextPadLeft, mTextPadRight);
- setCursor(mCursorPos);
- }
- void LLLineEditor::setEnabled(bool enabled)
- {
- mReadOnly = !enabled;
- setTabStop(!mReadOnly);
- updateAllowingLanguageInput();
- }
- void LLLineEditor::setMaxTextLength(S32 max_text_length)
- {
- mMaxLengthBytes = llmax(0, max_text_length);
- }
- void LLLineEditor::setTextPadding(S32 left, S32 right)
- {
- mTextPadLeft = llclamp(left, 0, getRect().getWidth());
- mTextPadRight = llclamp(right, 0, getRect().getWidth());
- mMinHPixels = UI_LINEEDITOR_H_PAD + mTextPadLeft;
- mMaxHPixels = getRect().getWidth() - mMinHPixels - mTextPadRight;
- }
- void LLLineEditor::setText(const std::string& new_text)
- {
- // If new text is identical, do not copy and do not move insertion point
- if (mText.getString() == new_text)
- {
- return;
- }
- // Check to see if entire field is selected.
- S32 len = mText.length();
- bool all_selected = len > 0 &&
- ((mSelectionStart == 0 && mSelectionEnd == len) ||
- (mSelectionStart == len && mSelectionEnd == 0));
- // Do safe truncation so we do not split multi-byte characters. Also
- // consider entire string selected when mSelectAllonFocusReceived is set on
- // an empty, focused line editor.
- all_selected = all_selected ||
- (len == 0 && hasFocus() && mSelectAllonFocusReceived);
- std::string truncated_utf8 = new_text;
- if (truncated_utf8.size() > (U32)mMaxLengthBytes)
- {
- truncated_utf8 = utf8str_truncate(new_text, mMaxLengthBytes);
- }
- mText.assign(truncated_utf8);
- if (all_selected)
- {
- // ...keep whole thing selected
- selectAll();
- }
- else
- {
- // Try to preserve insertion point, but deselect text
- deselect();
- }
- setCursor(llmin((S32)mText.length(), mCursorPos));
- // Set current history line to end of history.
- mCurrentHistoryLine = mLineHistory.end() - 1;
- mPrevText = mText;
- }
- // Picks a new cursor position based on the actual screen size of text being
- // drawn.
- S32 LLLineEditor::calculateCursorFromMouse(S32 local_mouse_x)
- {
- const llwchar* wtext = mText.getWString().c_str();
- LLWString asterix_text;
- if (mDrawAsterixes)
- {
- for (S32 i = 0, len = mText.length(); i < len; ++i)
- {
- asterix_text += (llwchar)0x2022L;
- }
- wtext = asterix_text.c_str();
- }
- return mScrollHPos +
- mGLFont->charFromPixelOffset(wtext, mScrollHPos,
- (F32)(local_mouse_x - mMinHPixels),
- // min-max range is inclusive
- (F32)(mMaxHPixels - mMinHPixels + 1));
- }
- void LLLineEditor::setCursorAtLocalPos(S32 local_mouse_x)
- {
- setCursor(calculateCursorFromMouse(local_mouse_x));
- }
- void LLLineEditor::setCursor(S32 pos)
- {
- S32 old_cursor_pos = mCursorPos;
- S32 old_scroll_pos = mScrollHPos;
- mCursorPos = llclamp(pos, 0, mText.length());
- S32 pixels_after_scroll = findPixelNearestPos();
- if (pixels_after_scroll > mMaxHPixels)
- {
- const llwchar* wtext = mText.getWString().c_str();
- LLWString asterix_text;
- if (mDrawAsterixes)
- {
- for (S32 i = 0, len = mText.length(); i < len; ++i)
- {
- asterix_text += (llwchar)0x2022L;
- }
- wtext = asterix_text.c_str();
- }
- std::string saved_text;
- if (mDrawAsterixes)
- {
- saved_text = mText.getString();
- std::string text;
- for (S32 i = 0, len = mText.length(); i < len; ++i)
- {
- text += '*';
- }
- mText = text;
- }
- S32 width_chars_to_left = mGLFont->getWidth(wtext, 0, mScrollHPos);
- S32 last_visible_char =
- mGLFont->maxDrawableChars(wtext,
- llmax(0.f, (F32)(mMaxHPixels -
- mMinHPixels +
- width_chars_to_left)));
- S32 min_scroll =
- mGLFont->firstDrawableChar(wtext,
- (F32)(mMaxHPixels - mMinHPixels -
- UI_LINEEDITOR_CURSOR_THICKNESS -
- UI_LINEEDITOR_H_PAD),
- mText.length(), mCursorPos);
- if (old_cursor_pos == last_visible_char)
- {
- mScrollHPos = llmin(mText.length(),
- llmax(min_scroll,
- mScrollHPos + SCROLL_INCREMENT_ADD));
- }
- else
- {
- mScrollHPos = min_scroll;
- }
- }
- else if (mCursorPos < mScrollHPos)
- {
- if (old_cursor_pos == mScrollHPos)
- {
- mScrollHPos = llmax(0,
- llmin(mCursorPos,
- mScrollHPos - SCROLL_INCREMENT_DEL));
- }
- else
- {
- mScrollHPos = mCursorPos;
- }
- }
- if (old_scroll_pos == 0 && mScrollHPos != 0 && mScrolledCallback)
- {
- mScrolledCallback(this, mScrolledCallbackData);
- }
- }
- void LLLineEditor::setCursorToEnd()
- {
- setCursor(mText.length());
- deselect();
- }
- void LLLineEditor::resetScrollPosition()
- {
- mScrollHPos = 0;
- // Make sure cursor says in visible range
- setCursor(getCursor());
- }
- void LLLineEditor::deselect()
- {
- mSelectionStart = 0;
- mSelectionEnd = 0;
- mIsSelecting = false;
- }
- void LLLineEditor::startSelection()
- {
- mIsSelecting = true;
- mSelectionStart = mCursorPos;
- mSelectionEnd = mCursorPos;
- }
- void LLLineEditor::endSelection()
- {
- if (mIsSelecting)
- {
- mIsSelecting = false;
- mSelectionEnd = mCursorPos;
- }
- }
- void LLLineEditor::selectAll()
- {
- mSelectionStart = mText.length();
- mSelectionEnd = 0;
- setCursor(mSelectionEnd);
- mIsSelecting = true;
- }
- void LLLineEditor::spellCorrect(void* data)
- {
- SpellMenuBind* menu_bind = (SpellMenuBind*)data;
- LLLineEditor* line = menu_bind->mOrigin;
- if (menu_bind && line)
- {
- LL_DEBUGS("SpellCheck") << menu_bind->mMenuItem->getName()
- << " : " << menu_bind->mOrigin->getName()
- << " : " << menu_bind->mWord << LL_ENDL;
- line->spellReplace(menu_bind);
- // Make it update:
- line->mKeystrokeTimer.reset();
- line->mPrevSpelledText.erase();
- }
- }
- void LLLineEditor::spellShow(void* data)
- {
- SpellMenuBind* menu_bind = (SpellMenuBind*)data;
- LLLineEditor* line = menu_bind->mOrigin;
- if (menu_bind && line)
- {
- line->mShowMisspelled = (menu_bind->mWord == "Show Misspellings");
- // Make it update:
- line->mKeystrokeTimer.reset();
- line->mPrevSpelledText.erase();
- }
- }
- void LLLineEditor::spellAdd(void* data)
- {
- SpellMenuBind* menu_bind = (SpellMenuBind*)data;
- LLLineEditor* line = menu_bind->mOrigin;
- if (menu_bind && line)
- {
- LLSpellCheck::getInstance()->addToCustomDictionary(menu_bind->mWord);
- // Make it update:
- line->mKeystrokeTimer.reset();
- line->mPrevSpelledText.erase();
- }
- }
- void LLLineEditor::spellIgnore(void* data)
- {
- SpellMenuBind* menu_bind = (SpellMenuBind*)data;
- LLLineEditor* line = menu_bind->mOrigin;
- if (menu_bind && line)
- {
- LLSpellCheck::getInstance()->addToIgnoreList(menu_bind->mWord);
- // Make it update:
- line->mKeystrokeTimer.reset();
- line->mPrevSpelledText.erase();
- }
- }
- std::vector<S32> LLLineEditor::getMisspelledWordsPositions()
- {
- std::vector<S32> bad_words_pos;
- const LLWString& text = mText.getWString();
- std::string selected_word;
- S32 word_start = 0;
- S32 word_end = mSpellCheckStart;
- S32 true_end;
- while (word_end < mSpellCheckEnd)
- {
- if (LLWStringUtil::isPartOfLexicalWord(text[word_end]))
- {
- // Select the word under the cursor
- while (word_end > 0 &&
- LLWStringUtil::isPartOfLexicalWord(text[word_end - 1]))
- {
- --word_end;
- }
- if (text[word_end] == L'\'')
- {
- // Do not count "'" at the start of a word
- ++word_end;
- }
- word_start = word_end;
- while (word_end < (S32)text.length() &&
- LLWStringUtil::isPartOfLexicalWord(text[word_end]))
- {
- ++word_end;
- }
- if (text[word_end - 1] == L'\'')
- {
- // Do not count "'" at the end of a word
- true_end = word_end - 1;
- }
- else
- {
- true_end = word_end;
- }
- // Do not bother for 2 or less characters words
- if (true_end > word_start + 2)
- {
- std::string part(text.begin(), text.end());
- selected_word = part.substr(word_start, true_end - word_start);
- if (!LLSpellCheck::getInstance()->checkSpelling(selected_word))
- {
- // Misspelled word here
- bad_words_pos.emplace_back(word_start);
- bad_words_pos.emplace_back(true_end);
- }
- }
- }
- ++word_end;
- }
- return bad_words_pos;
- }
- bool LLLineEditor::handleDoubleClick(S32 x, S32 y, MASK mask)
- {
- setFocus(true);
- if (mSelectionEnd == 0 && mSelectionStart == mText.length())
- {
- // If everything is selected, handle this as a normal click to change
- // insertion point
- handleMouseDown(x, y, mask);
- }
- else
- {
- const LLWString& wtext = mText.getWString();
- bool do_select_all = true;
- // Select the word we're on
- if (LLWStringUtil::isPartOfWord(wtext[mCursorPos]))
- {
- S32 old_selection_start = mLastSelectionStart;
- S32 old_selection_end = mLastSelectionEnd;
- // Select word the cursor is over
- while (mCursorPos > 0 &&
- LLWStringUtil::isPartOfWord(wtext[mCursorPos - 1]))
- { // Find the start of the word
- --mCursorPos;
- }
- startSelection();
- while (mCursorPos < (S32)wtext.length() &&
- LLWStringUtil::isPartOfWord(wtext[mCursorPos]))
- { // Find the end of the word
- ++mCursorPos;
- }
- mSelectionEnd = mCursorPos;
- // If nothing changed, then the word was already selected. Select
- // the whole line.
- do_select_all = old_selection_start == mSelectionStart &&
- old_selection_end == mSelectionEnd;
- }
- if (do_select_all)
- {
- selectAll();
- }
- }
- // We do not want handleMouseUp() to "finish" the selection (and thereby
- // set mSelectionEnd to where the mouse is), so we finish the selection
- // here.
- mIsSelecting = false;
- // Delay cursor flashing
- mKeystrokeTimer.reset();
- // Take selection to 'primary' clipboard
- updatePrimary();
- return true;
- }
- bool LLLineEditor::handleMouseDown(S32 x, S32 y, MASK mask)
- {
- // Check first whether the "clear search" button wants to deal with this.
- if (childrenHandleMouseDown(x, y, mask))
- {
- return true;
- }
- if (mSelectAllonFocusReceived && gFocusMgr.getKeyboardFocus() != this)
- {
- setFocus(true);
- }
- else
- {
- mLastSelectionStart = -1;
- setFocus(true);
- if (mask & MASK_SHIFT)
- {
- // Handle selection extension
- S32 old_cursor_pos = mCursorPos;
- setCursorAtLocalPos(x);
- if (hasSelection())
- {
- mSelectionEnd = mCursorPos;
- }
- else
- {
- mSelectionStart = old_cursor_pos;
- mSelectionEnd = mCursorPos;
- }
- // Assume we are starting a drag select
- mIsSelecting = true;
- }
- else
- {
- // Save selection for word/line selecting on double-click
- mLastSelectionStart = mSelectionStart;
- mLastSelectionEnd = mSelectionEnd;
- // Move cursor and deselect for regular click
- setCursorAtLocalPos(x);
- deselect();
- startSelection();
- }
- gFocusMgr.setMouseCapture(this);
- }
- // delay cursor flashing
- mKeystrokeTimer.reset();
- return true;
- }
- bool LLLineEditor::handleMiddleMouseDown(S32 x, S32 y, MASK mask)
- {
- setFocus(true);
- if (canPastePrimary())
- {
- setCursorAtLocalPos(x);
- pastePrimary();
- }
- return true;
- }
- bool LLLineEditor::handleRightMouseDown(S32 x, S32 y, MASK mask)
- {
- setFocus(true);
- S32 word_start = 0;
- S32 word_len = 0;
- S32 pos = calculateCursorFromMouse(x);
- // If the context menu has not yet been created for this editor, this call
- // will create it now. HB
- LLMenuGL* menu = createContextMenu();
- if (menu)
- {
- SpellMenuBind* menu_bind;
- LLMenuItemCallGL* menu_item;
- // Remove old suggestions
- for (S32 i = 0, count = mSuggestionMenuItems.size(); i < count; ++i)
- {
- menu_bind = mSuggestionMenuItems[i];
- if (menu_bind)
- {
- menu_item = menu_bind->mMenuItem;
- menu->remove(menu_item);
- menu_item->die();
- //delete menu_bind->mMenuItem;
- //menu_bind->mMenuItem = NULL;
- delete menu_bind;
- }
- }
- mSuggestionMenuItems.clear();
- LLSpellCheck* checker = NULL;
- // Not read-only, spell_check="true" in XUI and spell checking enabled
- bool spell_check = !mReadOnly && mSpellCheck;
- if (spell_check)
- {
- checker = LLSpellCheck::getInstance();
- spell_check = checker->getSpellCheck();
- }
- menu->setItemVisible("spell_sep", spell_check);
- if (spell_check)
- {
- // search for word matches
- bool is_word_part = getWordBoundriesAt(pos, &word_start,
- &word_len);
- if (is_word_part)
- {
- const LLWString& text = mText.getWString();
- std::string part(text.begin(), text.end());
- std::string selected_word = part.substr(word_start, word_len);
- if (!checker->checkSpelling(selected_word))
- {
- // misspelled word here
- std::vector<std::string> suggestions;
- S32 count = checker->getSuggestions(selected_word,
- suggestions);
- for (S32 i = 0; i < count; ++i)
- {
- menu_bind = new SpellMenuBind;
- menu_bind->mOrigin = this;
- menu_bind->mWord = suggestions[i];
- menu_bind->mWordPositionEnd = word_start + word_len;
- menu_bind->mWordPositionStart = word_start;
- menu_item = new LLMenuItemCallGL(menu_bind->mWord,
- spellCorrect,
- NULL, menu_bind);
- menu_bind->mMenuItem = menu_item;
- mSuggestionMenuItems.push_back(menu_bind);
- menu->append(menu_item);
- }
- menu_bind = new SpellMenuBind;
- menu_bind->mOrigin = this;
- menu_bind->mWord = selected_word;
- menu_bind->mWordPositionEnd = word_start + word_len;
- menu_bind->mWordPositionStart = word_start;
- menu_item = new LLMenuItemCallGL("Add word", spellAdd,
- NULL, menu_bind);
- menu_bind->mMenuItem = menu_item;
- mSuggestionMenuItems.push_back(menu_bind);
- menu->append(menu_item);
- menu_bind = new SpellMenuBind;
- menu_bind->mOrigin = this;
- menu_bind->mWord = selected_word;
- menu_bind->mWordPositionEnd = word_start + word_len;
- menu_bind->mWordPositionStart = word_start;
- menu_item = new LLMenuItemCallGL("Ignore word",
- spellIgnore,
- NULL, menu_bind);
- menu_bind->mMenuItem = menu_item;
- mSuggestionMenuItems.push_back(menu_bind);
- menu->append(menu_item);
- }
- }
- menu_bind = new SpellMenuBind;
- menu_bind->mOrigin = this;
- if (mShowMisspelled)
- {
- menu_bind->mWord = "Hide misspellings";
- }
- else
- {
- menu_bind->mWord = "Show misspellings";
- }
- menu_item = new LLMenuItemCallGL(menu_bind->mWord, spellShow,
- NULL, menu_bind);
- menu_bind->mMenuItem = menu_item;
- mSuggestionMenuItems.push_back(menu_bind);
- menu->append(menu_item);
- }
- menu->buildDrawLabels();
- menu->updateParent(LLMenuGL::sMenuContainer);
- LLMenuGL::showPopup(this, menu, x, y);
- }
- return true;
- }
- bool LLLineEditor::handleHover(S32 x, S32 y, MASK mask)
- {
- // Check first whether the "clear search" button wants to deal with this.
- if (!hasMouseCapture())
- {
- if (childrenHandleHover(x, y, mask) != NULL)
- {
- return true;
- }
- }
- bool handled = false;
- if (hasMouseCapture() && mIsSelecting)
- {
- if (x != mLastSelectionX || y != mLastSelectionY)
- {
- mLastSelectionX = x;
- mLastSelectionY = y;
- }
- // Scroll if mouse cursor outside of bounds
- if (mScrollTimer.hasExpired())
- {
- S32 increment = ll_roundp(mScrollTimer.getElapsedTimeF32() /
- AUTO_SCROLL_TIME);
- mScrollTimer.reset();
- mScrollTimer.setTimerExpirySec(AUTO_SCROLL_TIME);
- if (x < mMinHPixels && mScrollHPos > 0)
- {
- // Scroll to the left
- mScrollHPos = llclamp(mScrollHPos - increment, 0,
- mText.length());
- }
- else if (x > mMaxHPixels && mCursorPos < (S32)mText.length())
- {
- // If scrolling one pixel would make a difference...
- S32 pixels_after_scrolling_one_char = findPixelNearestPos(1);
- if (pixels_after_scrolling_one_char >= mMaxHPixels)
- {
- // ...scroll to the right
- mScrollHPos = llclamp(mScrollHPos + increment, 0,
- mText.length());
- }
- }
- }
- setCursorAtLocalPos(x);
- mSelectionEnd = mCursorPos;
- // Delay cursor flashing
- mKeystrokeTimer.reset();
- gWindowp->setCursor(UI_CURSOR_IBEAM);
- LL_DEBUGS("UserInput") << "hover handled by " << getName()
- << " (active)" << LL_ENDL;
- handled = true;
- }
- if (!handled)
- {
- gWindowp->setCursor(UI_CURSOR_IBEAM);
- LL_DEBUGS("UserInput") << "hover handled by " << getName()
- << " (inactive)" << LL_ENDL;
- handled = true;
- }
- return handled;
- }
- bool LLLineEditor::handleMouseUp(S32 x, S32 y, MASK mask)
- {
- bool handled = false;
- if (hasMouseCapture())
- {
- gFocusMgr.setMouseCapture(NULL);
- handled = true;
- }
- // Check first whether the "clear search" button wants to deal with this.
- if (!handled && childrenHandleMouseUp(x, y, mask) != NULL)
- {
- return true;
- }
- if (mIsSelecting)
- {
- setCursorAtLocalPos(x);
- mSelectionEnd = mCursorPos;
- handled = true;
- }
- if (handled)
- {
- // Delay cursor flashing
- mKeystrokeTimer.reset();
- // Take selection to 'primary' clipboard
- updatePrimary();
- }
- return handled;
- }
- // Remove a single character from the text
- void LLLineEditor::removeChar()
- {
- if (mCursorPos > 0)
- {
- mText.erase(mCursorPos - 1, 1);
- setCursor(mCursorPos - 1);
- }
- else
- {
- reportBadKeystroke();
- }
- }
- void LLLineEditor::addChar(llwchar uni_char)
- {
- llwchar new_c = uni_char;
- if (hasSelection())
- {
- deleteSelection();
- }
- else if (gKeyboardp && gKeyboardp->getInsertMode() == LL_KIM_OVERWRITE)
- {
- mText.erase(mCursorPos, 1);
- }
- S32 cur_bytes = mText.getString().size();
- S32 new_bytes = wchar_utf8_length(new_c);
- // Check byte length limit
- if (new_bytes + cur_bytes <= mMaxLengthBytes)
- {
- // Will we need to scroll ?
- LLWString w_buf;
- w_buf.assign(1, new_c);
- mText.insert(mCursorPos, w_buf);
- setCursor(mCursorPos + 1);
- }
- else
- {
- reportBadKeystroke();
- }
- gWindowp->hideCursorUntilMouseMove();
- }
- // Extends the selection box to the new cursor position
- void LLLineEditor::extendSelection(S32 new_cursor_pos)
- {
- if (!mIsSelecting)
- {
- startSelection();
- }
- setCursor(new_cursor_pos);
- mSelectionEnd = mCursorPos;
- }
- void LLLineEditor::setSelection(S32 start, S32 end)
- {
- // JC, yes, this seems odd, but I think you have to presume a selection
- // dragged from the end towards the start.
- S32 len = mText.length();
- mSelectionStart = llclamp(end, 0, len);
- mSelectionEnd = llclamp(start, 0, len);
- mIsSelecting = true;
- setCursor(start);
- }
- void LLLineEditor::setDrawAsterixes(bool b)
- {
- mDrawAsterixes = b;
- updateAllowingLanguageInput();
- }
- S32 LLLineEditor::prevWordPos(S32 cursor_pos) const
- {
- const LLWString& wtext = mText.getWString();
- while (cursor_pos > 0 && wtext[cursor_pos - 1] == ' ')
- {
- --cursor_pos;
- }
- while (cursor_pos > 0 &&
- LLWStringUtil::isPartOfWord(wtext[cursor_pos - 1]))
- {
- --cursor_pos;
- }
- return cursor_pos;
- }
- S32 LLLineEditor::nextWordPos(S32 cursor_pos) const
- {
- const LLWString& wtext = mText.getWString();
- while (cursor_pos < getLength() &&
- LLWStringUtil::isPartOfWord(wtext[cursor_pos]))
- {
- ++cursor_pos;
- }
- while (cursor_pos < getLength() && wtext[cursor_pos] == ' ')
- {
- ++cursor_pos;
- }
- return cursor_pos;
- }
- bool LLLineEditor::getWordBoundriesAt(S32 at, S32* word_begin,
- S32* word_length) const
- {
- const LLWString& wtext = mText.getWString();
- S32 pos = at;
- S32 start;
- if (LLWStringUtil::isPartOfLexicalWord(wtext[pos]))
- {
- while (pos > 0 && LLWStringUtil::isPartOfLexicalWord(wtext[pos - 1]))
- {
- --pos;
- }
- if (wtext[pos] == L'\'')
- {
- // Do not count "'" at the start of a word
- ++pos;
- }
- start = pos;
- while (pos < (S32)wtext.length() &&
- LLWStringUtil::isPartOfLexicalWord(wtext[pos]))
- {
- ++pos;
- }
- if (wtext[pos - 1] == L'\'')
- {
- // Do not count "'" at the end of a word
- --pos;
- }
- if (start >= pos)
- {
- return false;
- }
- *word_begin = start;
- *word_length = pos - start;
- return true;
- }
- return false;
- }
- void LLLineEditor::spellReplace(SpellMenuBind* data)
- {
- if (data)
- {
- S32 length = data->mWordPositionEnd - data->mWordPositionStart;
- mText.erase(data->mWordPositionStart, length);
- insert(data->mWord, data->mWordPositionStart);
- mCursorPos += data->mWord.length() - length;
- }
- }
- void LLLineEditor::insert(std::string what, S32 where)
- {
- LLLineEditorRollback rollback(this);
- LLWString clean_string(utf8str_to_wstring(what));
- LLWStringUtil::replaceTabsWithSpaces(clean_string, 4);
- mText.insert(where, clean_string);
- // See if we should move over the cursor acordingly. Validate new string
- // and rollback if needed.
- if (mPrevalidateFunc && !mPrevalidateFunc(mText.getWString()))
- {
- rollback.doRollback(this);
- reportBadKeystroke();
- }
- else if (mKeystrokeCallback)
- {
- mKeystrokeCallback(this, mCallbackUserData);
- }
- }
- bool LLLineEditor::handleSelectionKey(KEY key, MASK mask)
- {
- bool handled = false;
- if (mask & MASK_SHIFT)
- {
- handled = true;
- switch (key)
- {
- case KEY_LEFT:
- if (mCursorPos > 0)
- {
- S32 cursor_pos = mCursorPos - 1;
- if (mask & MASK_CONTROL)
- {
- cursor_pos = prevWordPos(cursor_pos);
- }
- extendSelection(cursor_pos);
- }
- else
- {
- reportBadKeystroke();
- }
- break;
- case KEY_RIGHT:
- if (mCursorPos < mText.length())
- {
- S32 cursor_pos = mCursorPos + 1;
- if (mask & MASK_CONTROL)
- {
- cursor_pos = nextWordPos(cursor_pos);
- }
- extendSelection(cursor_pos);
- }
- else
- {
- reportBadKeystroke();
- }
- break;
- case KEY_PAGE_UP:
- case KEY_HOME:
- extendSelection(0);
- break;
- case KEY_PAGE_DOWN:
- case KEY_END:
- {
- S32 len = mText.length();
- if (len)
- {
- extendSelection(len);
- }
- break;
- }
- default:
- handled = false;
- }
- }
- if (!handled && mHandleEditKeysDirectly && (MASK_CONTROL & mask) &&
- key == 'A')
- {
- if (canSelectAll())
- {
- selectAll();
- }
- else
- {
- reportBadKeystroke();
- }
- handled = true;
- }
- if (handled)
- {
- // take selection to 'primary' clipboard
- updatePrimary();
- }
- return handled;
- }
- void LLLineEditor::deleteSelection()
- {
- if (!mReadOnly && hasSelection())
- {
- S32 left_pos = llmin(mSelectionStart, mSelectionEnd);
- S32 selection_length = abs(mSelectionStart - mSelectionEnd);
- mText.erase(left_pos, selection_length);
- deselect();
- setCursor(left_pos);
- // Force spell-check update:
- mKeystrokeTimer.reset();
- mPrevSpelledText.erase();
- }
- }
- bool LLLineEditor::canCut() const
- {
- return !mReadOnly && !mDrawAsterixes && hasSelection();
- }
- // Cut selection to clipboard
- void LLLineEditor::cut()
- {
- if (canCut())
- {
- // Prepare for possible rollback
- LLLineEditorRollback rollback(this);
- S32 left_pos = llmin(mSelectionStart, mSelectionEnd);
- S32 length = abs(mSelectionStart - mSelectionEnd);
- gClipboard.copyFromSubstring(mText.getWString(), left_pos, length);
- deleteSelection();
- // Validate new string and rollback the if needed.
- bool need_to_rollback = mPrevalidateFunc &&
- !mPrevalidateFunc(mText.getWString());
- if (need_to_rollback)
- {
- rollback.doRollback(this);
- reportBadKeystroke();
- }
- else if (mKeystrokeCallback)
- {
- mKeystrokeCallback(this, mCallbackUserData);
- }
- // Force spell-check update:
- mKeystrokeTimer.reset();
- mPrevSpelledText.erase();
- }
- }
- bool LLLineEditor::canCopy() const
- {
- return !mDrawAsterixes && hasSelection();
- }
- // Copy selection to clipboard
- void LLLineEditor::copy()
- {
- if (canCopy())
- {
- S32 left_pos = llmin(mSelectionStart, mSelectionEnd);
- S32 length = abs(mSelectionStart - mSelectionEnd);
- gClipboard.copyFromSubstring(mText.getWString(), left_pos, length);
- // Force spell-check update:
- mKeystrokeTimer.reset();
- mPrevSpelledText.erase();
- }
- }
- bool LLLineEditor::canPaste() const
- {
- return !mReadOnly && gClipboard.canPasteString();
- }
- void LLLineEditor::paste()
- {
- bool is_primary = false;
- pasteHelper(is_primary);
- }
- void LLLineEditor::pastePrimary()
- {
- bool is_primary = true;
- pasteHelper(is_primary);
- }
- // Paste from primary (is_primary==true) or clipboard (is_primary==false)
- void LLLineEditor::pasteHelper(bool is_primary)
- {
- bool can_paste_it;
- if (is_primary)
- {
- can_paste_it = canPastePrimary();
- }
- else
- {
- can_paste_it = canPaste();
- }
- if (can_paste_it)
- {
- LLWString paste;
- if (is_primary)
- {
- paste = gClipboard.getPastePrimaryWString();
- }
- else
- {
- paste = gClipboard.getPasteWString();
- }
- if (!paste.empty())
- {
- // Prepare for possible rollback
- LLLineEditorRollback rollback(this);
- // Delete any selected characters
- if (!is_primary && hasSelection())
- {
- deleteSelection();
- }
- // Clean up string (replace tabs and returns and remove characters
- // that our fonts do not support)
- LLWString clean_string(paste);
- LLWStringUtil::replaceTabsWithSpaces(clean_string, 1);
- // Note: character 182 is the paragraph character
- LLWStringUtil::replaceChar(clean_string, '\n',
- mReplaceNewlinesWithSpaces ? ' '
- : 182);
- // Insert the string
- // Check to see that the size is not going to be larger than the
- // max number of bytes
- U32 available_bytes = mMaxLengthBytes - wstring_utf8_length(mText);
- if (available_bytes < (U32)wstring_utf8_length(clean_string))
- {
- // Does not all fit
- llwchar current_symbol = clean_string[0];
- U32 wchars_that_fit = 0;
- U32 total_bytes = wchar_utf8_length(current_symbol);
- // Loop over the "wide" characters (symbols) and check to see
- // how large (in bytes) each symbol is.
- while (total_bytes <= available_bytes)
- {
- // While we still have available bytes "accept" the current
- // symbol and check the size of the next one
- current_symbol = clean_string[++wchars_that_fit];
- total_bytes += wchar_utf8_length(current_symbol);
- }
- // Truncate the clean string at the limit of what will fit
- clean_string = clean_string.substr(0, wchars_that_fit);
- reportBadKeystroke();
- }
- mText.insert(mCursorPos, clean_string);
- setCursor(mCursorPos + (S32)clean_string.length());
- deselect();
- // Validate new string and rollback if needed.
- if (mPrevalidateFunc && !mPrevalidateFunc(mText.getWString()))
- {
- rollback.doRollback(this);
- reportBadKeystroke();
- }
- else if (mKeystrokeCallback)
- {
- mKeystrokeCallback(this, mCallbackUserData);
- }
- }
- // Force spell-check update:
- mKeystrokeTimer.reset();
- mPrevSpelledText.erase();
- }
- }
- // Copy selection to primary
- void LLLineEditor::copyPrimary()
- {
- if (canCopy())
- {
- S32 left_pos = llmin(mSelectionStart, mSelectionEnd);
- S32 length = abs(mSelectionStart - mSelectionEnd);
- gClipboard.copyFromPrimarySubstring(mText.getWString(), left_pos,
- length);
- // Force spell-check update:
- mKeystrokeTimer.reset();
- mPrevSpelledText.erase();
- }
- }
- bool LLLineEditor::canPastePrimary() const
- {
- return !mReadOnly && gClipboard.canPastePrimaryString();
- }
- void LLLineEditor::updatePrimary()
- {
- if (canCopy())
- {
- copyPrimary();
- }
- }
- bool LLLineEditor::handleSpecialKey(KEY key, MASK mask)
- {
- bool handled = false;
- switch (key)
- {
- case KEY_INSERT:
- if (mask == MASK_NONE && gKeyboardp)
- {
- gKeyboardp->toggleInsertMode();
- }
- handled = true;
- break;
- case KEY_BACKSPACE:
- if (!mReadOnly)
- {
- if (hasSelection())
- {
- deleteSelection();
- }
- else if (mCursorPos > 0)
- {
- removeChar();
- }
- else
- {
- reportBadKeystroke();
- }
- }
- handled = true;
- break;
- case KEY_PAGE_UP:
- case KEY_HOME:
- if (!mIgnoreArrowKeys)
- {
- setCursor(0);
- handled = true;
- }
- break;
- case KEY_PAGE_DOWN:
- case KEY_END:
- if (!mIgnoreArrowKeys)
- {
- S32 len = mText.length();
- if (len)
- {
- setCursor(len);
- }
- handled = true;
- }
- break;
- case KEY_LEFT:
- if (mIgnoreArrowKeys && mask == MASK_NONE)
- {
- break;
- }
- if ((mask & MASK_ALT) == 0)
- {
- if (hasSelection())
- {
- setCursor(llmin(mCursorPos - 1, mSelectionStart,
- mSelectionEnd));
- }
- else if (mCursorPos > 0)
- {
- S32 cursor_pos = mCursorPos - 1;
- if (mask & MASK_CONTROL)
- {
- cursor_pos = prevWordPos(cursor_pos);
- }
- setCursor(cursor_pos);
- }
- else
- {
- reportBadKeystroke();
- }
- handled = true;
- }
- break;
- case KEY_RIGHT:
- if (mIgnoreArrowKeys && mask == MASK_NONE)
- {
- break;
- }
- if ((mask & MASK_ALT) == 0)
- {
- if (hasSelection())
- {
- setCursor(llmax(mCursorPos + 1, mSelectionStart,
- mSelectionEnd));
- }
- else if (mCursorPos < mText.length())
- {
- S32 cursor_pos = mCursorPos + 1;
- if (mask & MASK_CONTROL)
- {
- cursor_pos = nextWordPos(cursor_pos);
- }
- setCursor(cursor_pos);
- }
- else
- {
- reportBadKeystroke();
- }
- handled = true;
- }
- break;
- // handle ctrl-uparrow if we have a history enabled line editor.
- case KEY_UP:
- if (mHaveHistory && mask == MASK_CONTROL)
- {
- if (mCurrentHistoryLine > mLineHistory.begin())
- {
- mText.assign(*(--mCurrentHistoryLine));
- setCursor(llmin((S32)mText.length(), mCursorPos));
- }
- else
- {
- reportBadKeystroke();
- }
- handled = true;
- }
- break;
- // handle ctrl-downarrow if we have a history enabled line editor
- case KEY_DOWN:
- if (mHaveHistory && mask == MASK_CONTROL)
- {
- if (!mLineHistory.empty() &&
- mCurrentHistoryLine < mLineHistory.end() - 1)
- {
- mText.assign(*(++mCurrentHistoryLine));
- setCursor(llmin((S32)mText.length(), mCursorPos));
- }
- else
- {
- reportBadKeystroke();
- }
- handled = true;
- }
- break;
- case KEY_RETURN:
- // store sent line in history
- updateHistory();
- break;
- case KEY_ESCAPE:
- if (mask == MASK_NONE && mRevertOnEsc &&
- mText.getString() != mPrevText)
- {
- setText(mPrevText);
- // Note, do not set handled, still want to loose focus (would
- // not commit because text is now unchanged)
- }
- break;
- default:
- break;
- }
- if (!handled && mHandleEditKeysDirectly)
- {
- // Standard edit keys (Ctrl-X, Delete, etc,) are handled here instead
- // of routed by the menu system.
- if (key == KEY_DELETE)
- {
- if (canDoDelete())
- {
- doDelete();
- }
- else
- {
- reportBadKeystroke();
- }
- handled = true;
- }
- else if (MASK_CONTROL & mask)
- {
- if (key == 'C')
- {
- if (canCopy())
- {
- copy();
- }
- else
- {
- reportBadKeystroke();
- }
- handled = true;
- }
- else if (key == 'V')
- {
- if (canPaste())
- {
- paste();
- }
- else
- {
- reportBadKeystroke();
- }
- handled = true;
- }
- else if (key == 'X')
- {
- if (canCut())
- {
- cut();
- }
- else
- {
- reportBadKeystroke();
- }
- handled = true;
- }
- }
- }
- return handled;
- }
- bool LLLineEditor::handleKeyHere(KEY key, MASK mask)
- {
- bool handled = false;
- // Key presses are not being passed to the Popup menu. A proper fix is
- // non-trivial so instead just close the menu.
- LLMenuGL* menu = getContextMenu();
- if (menu && menu->isOpen())
- {
- LLMenuGL::sMenuContainer->hideMenus();
- }
- if (gFocusMgr.getKeyboardFocus() == this)
- {
- LLLineEditorRollback rollback(this);
- bool selection_modified = false;
- if (!handled)
- {
- handled = handleSelectionKey(key, mask);
- selection_modified = handled;
- }
- // Handle most keys only if the text editor is writeable.
- if (!mReadOnly)
- {
- if (!handled && mOnHandleKeyCallback)
- {
- handled = mOnHandleKeyCallback(key, mask, this,
- mOnHandleKeyData);
- }
- if (!handled)
- {
- handled = handleSpecialKey(key, mask);
- }
- }
- if (handled)
- {
- mKeystrokeTimer.reset();
- // Most keystrokes will make the selection box go away, but not all
- // will.
- if (!selection_modified && KEY_SHIFT != key &&
- KEY_CONTROL != key && KEY_ALT != key && KEY_CAPSLOCK)
- {
- deselect();
- }
- // If read-only, don't allow changes
- bool need_to_rollback = mReadOnly &&
- mText.getString() == rollback.getText();
- if (!need_to_rollback)
- {
- // Validate new string and rollback the keystroke if needed.
- need_to_rollback = mPrevalidateFunc &&
- !mPrevalidateFunc(mText.getWString());
- }
- if (need_to_rollback)
- {
- rollback.doRollback(this);
- reportBadKeystroke();
- }
- // Notify owner if requested
- if (!need_to_rollback && handled)
- {
- if (mKeystrokeCallback)
- {
- mKeystrokeCallback(this, mCallbackUserData);
- }
- }
- }
- }
- return handled;
- }
- bool LLLineEditor::handleUnicodeCharHere(llwchar uni_char)
- {
- if (uni_char < 0x20 || uni_char == 0x7F) // Control character or DEL
- {
- return false;
- }
- bool handled = false;
- if (gFocusMgr.getKeyboardFocus() == this && getVisible() && !mReadOnly)
- {
- // Key presses are not being passed to the pop-up menu.
- // A proper fix is non-trivial so instead just close the menu.
- LLMenuGL* menu = getContextMenu();
- if (menu && menu->isOpen())
- {
- LLMenuGL::sMenuContainer->hideMenus();
- }
- handled = true;
- LLLineEditorRollback rollback(this);
- addChar(uni_char);
- mKeystrokeTimer.reset();
- deselect();
- // Validate new string and rollback the keystroke if needed.
- bool need_to_rollback = mPrevalidateFunc &&
- !mPrevalidateFunc(mText.getWString());
- if (need_to_rollback)
- {
- rollback.doRollback(this);
- reportBadKeystroke();
- }
- // Notify owner if requested
- if (!need_to_rollback && handled && mKeystrokeCallback)
- {
- // *HACK: the only usage of this callback does not do anything with
- // the character. We will have to do something about this if
- // something ever changes - Doug
- mKeystrokeCallback(this, mCallbackUserData);
- }
- }
- return handled;
- }
- bool LLLineEditor::canDoDelete() const
- {
- return !mReadOnly &&
- (!mPassDelete || hasSelection() || mCursorPos < mText.length());
- }
- void LLLineEditor::doDelete()
- {
- if (canDoDelete() && !mText.empty())
- {
- // Prepare for possible rollback
- LLLineEditorRollback rollback(this);
- if (hasSelection())
- {
- deleteSelection();
- }
- else if (mCursorPos < mText.length())
- {
- setCursor(mCursorPos + 1);
- removeChar();
- }
- // Validate new string and rollback the if needed.
- if (mPrevalidateFunc && !mPrevalidateFunc(mText.getWString()))
- {
- rollback.doRollback(this);
- reportBadKeystroke();
- }
- else
- {
- if (mKeystrokeCallback)
- {
- mKeystrokeCallback(this, mCallbackUserData);
- }
- }
- // Force spell-check update:
- mKeystrokeTimer.reset();
- mPrevSpelledText.erase();
- }
- }
- void LLLineEditor::drawMisspelled(LLRect background)
- {
- LL_FAST_TIMER(FTM_RENDER_SPELLCHECK);
- S32 elapsed = (S32)mSpellTimer.getElapsedTimeF32();
- S32 keystroke = (S32)mKeystrokeTimer.getElapsedTimeF32();
- // Do not bother checking if the text did not change in a while and fire a
- // spell checking only once a second while typing.
- if (keystroke < 2 && (elapsed & 1))
- {
- S32 new_start_spell = mScrollHPos;
- S32 cursorloc = calculateCursorFromMouse(mMaxHPixels);
- S32 length = (S32)mText.length();
- S32 new_end_spell = length > cursorloc ? cursorloc : length;
- if (new_start_spell != mSpellCheckStart ||
- new_end_spell != mSpellCheckEnd || isSpellDirty())
- {
- mSpellCheckStart = new_start_spell;
- mSpellCheckEnd = new_end_spell;
- resetSpellDirty();
- mMisspellLocations = getMisspelledWordsPositions();
- }
- }
- if (mShowMisspelled)
- {
- const S32 bottom = background.mBottom;
- const S32 maxw = getRect().getWidth();
- for (S32 i = 0, count = mMisspellLocations.size(); i < count; ++i)
- {
- S32 wstart =
- findPixelNearestPos(mMisspellLocations[i++] - mCursorPos);
- if (wstart > maxw)
- {
- wstart = maxw;
- }
- S32 wend = findPixelNearestPos(mMisspellLocations[i] - mCursorPos);
- if (wend > maxw)
- {
- wend = maxw;
- }
- // Draw the zig zag line
- gGL.color4ub(255, 0, 0, 200);
- while (wstart < wend)
- {
- gl_line_2d(wstart, bottom - 1, wstart + 3, bottom + 2);
- gl_line_2d(wstart + 3, bottom + 2, wstart + 6, bottom - 1);
- wstart += 6;
- }
- }
- }
- }
- void LLLineEditor::draw()
- {
- S32 text_len = mText.length();
- std::string saved_text;
- if (mDrawAsterixes)
- {
- saved_text = mText.getString();
- std::string text;
- for (S32 i = 0, len = mText.length(); i < len; ++i)
- {
- text += '*';
- }
- mText = text;
- }
- // Draw rectangle for the background
- LLRect background(0, getRect().getHeight(), getRect().getWidth(), 0);
- background.stretch(-mBorderThickness);
- LLColor4 bg_color = mReadOnlyBgColor;
- // Drawing solids requires texturing be disabled
- {
- gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
- // Draw background for text
- if (!mReadOnly)
- {
- if (gFocusMgr.getKeyboardFocus() == this)
- {
- bg_color = mFocusBgColor;
- }
- else
- {
- bg_color = mWriteableBgColor;
- }
- }
- gl_rect_2d(background, bg_color);
- }
- // Draw text
- S32 cursor_bottom = background.mBottom + 1;
- S32 cursor_top = background.mTop - 1;
- LLColor4 text_color;
- if (!mReadOnly)
- {
- if (!getTentative())
- {
- text_color = mFgColor;
- }
- else
- {
- text_color = mTentativeFgColor;
- }
- }
- else
- {
- text_color = mReadOnlyFgColor;
- }
- LLColor4 label_color = mTentativeFgColor;
- if (hasPreeditString())
- {
- // Draw preedit markers. This needs to be before drawing letters.
- for (U32 i = 0, size = mPreeditStandouts.size(); i < size; ++i)
- {
- const S32 preedit_left = mPreeditPositions[i];
- const S32 preedit_right = mPreeditPositions[i + 1];
- if (preedit_right > mScrollHPos)
- {
- S32 preedit_pixels_left =
- findPixelNearestPos(llmax(preedit_left,
- mScrollHPos) - mCursorPos);
- S32 preedit_pixels_right =
- llmin(findPixelNearestPos(preedit_right - mCursorPos),
- background.mRight);
- if (preedit_pixels_left >= background.mRight)
- {
- break;
- }
- LLColor4 color;
- if (mPreeditStandouts[i])
- {
- color = (text_color * STANDOUT_BRIGHTNESS +
- bg_color *
- (1.f - STANDOUT_BRIGHTNESS)).setAlpha(1.f);
- }
- else
- {
- color = (text_color * MARKER_BRIGHTNESS +
- bg_color *
- (1.f - MARKER_BRIGHTNESS)).setAlpha(1.f);
- }
- gl_rect_2d(preedit_pixels_left + PREEDIT_BORDER,
- background.mBottom + PREEDIT_BORDER,
- preedit_pixels_right - PREEDIT_BORDER,
- background.mBottom, color);
- }
- }
- }
- S32 rendered_text = 0;
- F32 rendered_pixels_right = (F32)mMinHPixels;
- F32 text_bottom = (F32)background.mBottom + (F32)UI_LINEEDITOR_V_PAD;
- if (gFocusMgr.getKeyboardFocus() == this && hasSelection())
- {
- S32 select_left;
- S32 select_right;
- if (mSelectionStart < mCursorPos)
- {
- select_left = mSelectionStart;
- select_right = mCursorPos;
- }
- else
- {
- select_left = mCursorPos;
- select_right = mSelectionStart;
- }
- if (select_left > mScrollHPos)
- {
- // Unselected, left side
- rendered_text = mGLFont->render(mText, mScrollHPos,
- rendered_pixels_right, text_bottom,
- text_color, LLFontGL::LEFT,
- LLFontGL::BOTTOM, LLFontGL::NORMAL,
- select_left - mScrollHPos,
- mMaxHPixels - ll_round(rendered_pixels_right),
- &rendered_pixels_right);
- }
- if (rendered_pixels_right < (F32)mMaxHPixels &&
- rendered_text < text_len)
- {
- LLColor4 color(1.f - bg_color.mV[0], 1.f - bg_color.mV[1],
- 1.f - bg_color.mV[2], 1.f);
- // Selected middle
- S32 width = mGLFont->getWidth(mText.getWString().c_str(),
- mScrollHPos + rendered_text,
- select_right - mScrollHPos - rendered_text);
- S32 right_delta = ll_round(rendered_pixels_right);
- width = llmin(width, mMaxHPixels - right_delta);
- gl_rect_2d(right_delta, cursor_top, right_delta + width,
- cursor_bottom, color);
- rendered_text += mGLFont->render(mText, mScrollHPos + rendered_text,
- rendered_pixels_right, text_bottom,
- LLColor4(1.f - text_color.mV[0],
- 1.f - text_color.mV[1],
- 1.f - text_color.mV[2],
- 1),
- LLFontGL::LEFT, LLFontGL::BOTTOM,
- LLFontGL::NORMAL,
- select_right - mScrollHPos - rendered_text,
- mMaxHPixels - right_delta,
- &rendered_pixels_right);
- }
- if (rendered_pixels_right < (F32)mMaxHPixels &&
- rendered_text < text_len)
- {
- // Unselected, right side
- mGLFont->render(mText, mScrollHPos + rendered_text,
- rendered_pixels_right, text_bottom, text_color,
- LLFontGL::LEFT, LLFontGL::BOTTOM, LLFontGL::NORMAL,
- S32_MAX,
- mMaxHPixels - ll_round(rendered_pixels_right),
- &rendered_pixels_right);
- }
- }
- else
- {
- mGLFont->render(mText, mScrollHPos, rendered_pixels_right, text_bottom,
- text_color, LLFontGL::LEFT, LLFontGL::BOTTOM,
- LLFontGL::NORMAL, S32_MAX,
- mMaxHPixels - ll_round(rendered_pixels_right),
- &rendered_pixels_right);
- }
- if (!mReadOnly && mSpellCheck && hasFocus() &&
- LLSpellCheck::getInstance()->getSpellCheck())
- {
- drawMisspelled(background);
- }
- // If we are editing...
- if (gFocusMgr.getKeyboardFocus() == this)
- {
- //mBorder->setVisible(true); // ok, programmer art just this once.
- // (Flash the cursor every half second)
- if (gShowTextEditCursor && !mReadOnly)
- {
- F32 elapsed = mKeystrokeTimer.getElapsedTimeF32();
- if (elapsed < CURSOR_FLASH_DELAY || (S32(elapsed * 2) & 1))
- {
- S32 cursor_left = findPixelNearestPos();
- cursor_left -= UI_LINEEDITOR_CURSOR_THICKNESS / 2;
- S32 cursor_right = cursor_left + UI_LINEEDITOR_CURSOR_THICKNESS;
- bool ins_mode = !hasSelection() && gKeyboardp &&
- gKeyboardp->getInsertMode() == LL_KIM_OVERWRITE;
- if (ins_mode)
- {
- static const LLWString space(utf8str_to_wstring(" "));
- S32 wswidth = mGLFont->getWidth(space.c_str());
- S32 width = mGLFont->getWidth(mText.getWString().c_str(),
- mCursorPos, 1) + 1;
- cursor_right = cursor_left + llmax(wswidth, width);
- }
- // Use same color as text for the Cursor
- gl_rect_2d(cursor_left, cursor_top, cursor_right,
- cursor_bottom, text_color);
- if (ins_mode)
- {
- mGLFont->render(mText, mCursorPos,
- (F32)(cursor_left +
- UI_LINEEDITOR_CURSOR_THICKNESS / 2),
- text_bottom,
- LLColor4(1.f - text_color.mV[0],
- 1.f - text_color.mV[1],
- 1.f - text_color.mV[2], 1),
- LLFontGL::LEFT, LLFontGL::BOTTOM,
- LLFontGL::NORMAL, 1);
- }
- // Make sure the IME is in the right place
- // RCalculcate for IME position
- S32 pixels_after_scroll = findPixelNearestPos();
- LLRect screen_pos = getScreenRect();
- LLCoordGL ime_pos(screen_pos.mLeft + pixels_after_scroll,
- screen_pos.mTop - UI_LINEEDITOR_V_PAD);
- ime_pos.mX = (S32)(ime_pos.mX * LLUI::sGLScaleFactor.mV[VX]);
- ime_pos.mY = (S32)(ime_pos.mY * LLUI::sGLScaleFactor.mV[VY]);
- gWindowp->setLanguageTextInput(ime_pos);
- }
- }
- // Draw label if no text is provided but we should draw it in a
- // different color to give indication that it is not text you typed in
- if (mText.empty() && mReadOnly)
- {
- mGLFont->render(mLabel.getWString(), 0, mMinHPixels,
- (F32)text_bottom, label_color, LLFontGL::LEFT,
- LLFontGL::BOTTOM, LLFontGL::NORMAL, S32_MAX,
- mMaxHPixels - ll_round(rendered_pixels_right),
- &rendered_pixels_right, false);
- }
- // Draw children (border)
- mBorder->setKeyboardFocusHighlight(true);
- LLView::draw();
- mBorder->setKeyboardFocusHighlight(false);
- }
- else // Does not have keyboard input
- {
- // Draw label if no text provided
- if (mText.empty())
- {
- mGLFont->render(mLabel.getWString(), 0, mMinHPixels,
- (F32)text_bottom, label_color,
- LLFontGL::LEFT, LLFontGL::BOTTOM, LLFontGL::NORMAL,
- S32_MAX,
- mMaxHPixels - ll_round(rendered_pixels_right),
- &rendered_pixels_right, false);
- }
- // Draw children (border)
- LLView::draw();
- }
- if (mDrawAsterixes)
- {
- mText = saved_text;
- }
- }
- // Returns the local screen space X coordinate associated with the text cursor
- // position.
- S32 LLLineEditor::findPixelNearestPos(S32 cursor_offset) const
- {
- S32 dpos = mCursorPos - mScrollHPos + cursor_offset;
- S32 width;
- if (mDrawAsterixes)
- {
- LLWString asterix;
- for (S32 i = 0, len = mText.length(); i < len; ++i)
- {
- asterix += llwchar('*');
- }
- width = mGLFont->getWidth(asterix.c_str(), mScrollHPos, dpos);
- }
- else
- {
- width = mGLFont->getWidth(mText.getWString().c_str(), mScrollHPos,
- dpos);
- }
- return mMinHPixels + width;
- }
- void LLLineEditor::reportBadKeystroke()
- {
- make_ui_sound("UISndBadKeystroke");
- }
- //virtual
- void LLLineEditor::clear()
- {
- mText.clear();
- setCursor(0);
- }
- //virtual
- void LLLineEditor::onTabInto()
- {
- selectAll();
- }
- // Start or stop the editor from accepting text-editing keystrokes
- void LLLineEditor::setFocus(bool new_state)
- {
- bool old_state = hasFocus();
- if (!new_state)
- {
- gWindowp->allowLanguageTextInput(this, false);
- }
- // Getting focus when we did not have it before, and we want to select all
- if (!old_state && new_state && mSelectAllonFocusReceived)
- {
- selectAll();
- // We do not want handleMouseUp() to "finish" the selection (and
- // thereby set mSelectionEnd to where the mouse is), so we finish the
- // selection here.
- mIsSelecting = false;
- }
- if (new_state)
- {
- grabMenuHandler();
- // Do not start the cursor flashing right away
- mKeystrokeTimer.reset();
- }
- else
- {
- // Not really needed, since loss of keyboard focus should take care of
- // this, but limited paranoia is ok.
- releaseMenuHandler();
- endSelection();
- }
- LLUICtrl::setFocus(new_state);
- if (new_state)
- {
- // Allow Language Text Input only when this LineEditor has no
- // prevalidate function attached. This criterion works fine for now,
- // since all prevalidate func reject any non-ASCII characters. I am not
- // sure for future versions, however.
- gWindowp->allowLanguageTextInput(this, mPrevalidateFunc == NULL);
- }
- }
- //virtual
- void LLLineEditor::setRect(const LLRect& rect)
- {
- LLUICtrl::setRect(rect);
- if (mBorder)
- {
- LLRect border_rect = mBorder->getRect();
- // Scalable UI somehow made these rectangles off-by-one.
- // I don't know why. JC
- border_rect.setOriginAndSize(border_rect.mLeft, border_rect.mBottom,
- rect.getWidth()-1, rect.getHeight() - 1);
- mBorder->setRect(border_rect);
- }
- }
- void LLLineEditor::setPrevalidate(bool (*func)(const LLWString&))
- {
- mPrevalidateFunc = func;
- updateAllowingLanguageInput();
- }
- // Limits what characters can be used to [1234567890.-] with [-] only valid in
- // the first position. Does NOT ensure that the string is a well-formed number
- // (that's the job of post-validation) for the simple reasons that intermediate
- // states may be invalid even if the final result is valid.
- //
- //static
- bool LLLineEditor::prevalidateFloat(const LLWString& str)
- {
- LLLocale locale(LLLocale::USER_LOCALE);
- bool success = true;
- LLWString trimmed = str;
- LLWStringUtil::trim(trimmed);
- S32 len = trimmed.length();
- if (len > 0)
- {
- // May be a comma or period, depending on the locale
- llwchar decimal_point = (llwchar)LLLocale::getDecimalPoint();
- S32 i = 0;
- // First character can be a negative sign
- if (trimmed[0] == '-')
- {
- ++i;
- }
- for ( ; i < len; ++i)
- {
- if (decimal_point != trimmed[i] &&
- !LLStringOps::isDigit(trimmed[i]))
- {
- success = false;
- break;
- }
- }
- }
- return success;
- }
- //static
- bool LLLineEditor::postvalidateFloat(const std::string& str)
- {
- LLLocale locale(LLLocale::USER_LOCALE);
- bool success = true;
- bool has_decimal = false;
- bool has_digit = false;
- LLWString trimmed = utf8str_to_wstring(str);
- LLWStringUtil::trim(trimmed);
- S32 len = trimmed.length();
- if (0 < len)
- {
- S32 i = 0;
- // First character can be a negative sign
- if ('-' == trimmed[0])
- {
- ++i;
- }
- // May be a comma or period, depending on the locale
- llwchar decimal_point = (llwchar)LLLocale::getDecimalPoint();
- for ( ; i < len; ++i)
- {
- if (decimal_point == trimmed[i])
- {
- if (has_decimal)
- {
- // Cannot have two
- success = false;
- break;
- }
- else
- {
- has_decimal = true;
- }
- }
- else
- if (LLStringOps::isDigit(trimmed[i]))
- {
- has_digit = true;
- }
- else
- {
- success = false;
- break;
- }
- }
- }
- // Gotta have at least one
- success = has_digit;
- return success;
- }
- // Limits what characters can be used to [1234567890-] with [-] only valid in
- // the first position. Does NOT ensure that the string is a well-formed number
- // (that's the job of post-validation) for the simple reasons that intermediate
- // states may be invalid even if the final result is valid.
- //static
- bool LLLineEditor::prevalidateInt(const LLWString& str)
- {
- LLLocale locale(LLLocale::USER_LOCALE);
- bool success = true;
- LLWString trimmed = str;
- LLWStringUtil::trim(trimmed);
- S32 len = trimmed.length();
- if (0 < len)
- {
- S32 i = 0;
- // First character can be a negative sign
- if ('-' == trimmed[0])
- {
- ++i;
- }
- for ( ; i < len; ++i)
- {
- if (!LLStringOps::isDigit(trimmed[i]))
- {
- success = false;
- break;
- }
- }
- }
- return success;
- }
- //static
- bool LLLineEditor::prevalidatePositiveS32(const LLWString& str)
- {
- LLLocale locale(LLLocale::USER_LOCALE);
- LLWString trimmed = str;
- LLWStringUtil::trim(trimmed);
- S32 len = trimmed.length();
- bool success = true;
- if (0 < len)
- {
- if ('-' == trimmed[0] || '0' == trimmed[0])
- {
- success = false;
- }
- S32 i = 0;
- while (success && (i < len))
- {
- if (!LLStringOps::isDigit(trimmed[i++]))
- {
- success = false;
- }
- }
- }
- if (success)
- {
- S32 val = strtol(wstring_to_utf8str(trimmed).c_str(), NULL, 10);
- if (val <= 0)
- {
- success = false;
- }
- }
- return success;
- }
- bool LLLineEditor::prevalidateNonNegativeS32(const LLWString& str)
- {
- LLLocale locale(LLLocale::USER_LOCALE);
- LLWString trimmed = str;
- LLWStringUtil::trim(trimmed);
- S32 len = trimmed.length();
- bool success = true;
- if (0 < len)
- {
- if ('-' == trimmed[0])
- {
- success = false;
- }
- S32 i = 0;
- while (success && (i < len))
- {
- if (!LLStringOps::isDigit(trimmed[i++]))
- {
- success = false;
- }
- }
- }
- if (success)
- {
- S32 val = strtol(wstring_to_utf8str(trimmed).c_str(), NULL, 10);
- if (val < 0)
- {
- success = false;
- }
- }
- return success;
- }
- bool LLLineEditor::prevalidateAlphaNum(const LLWString& str)
- {
- LLLocale locale(LLLocale::USER_LOCALE);
- bool rv = true;
- S32 len = str.length();
- if (len == 0) return rv;
- while (len--)
- {
- if (!LLStringOps::isAlnum((char)str[len]))
- {
- rv = false;
- break;
- }
- }
- return rv;
- }
- //static
- bool LLLineEditor::prevalidateAlphaNumSpace(const LLWString& str)
- {
- LLLocale locale(LLLocale::USER_LOCALE);
- bool rv = true;
- S32 len = str.length();
- if (len == 0) return rv;
- while (len--)
- {
- if (!(LLStringOps::isAlnum((char)str[len]) || ' ' == str[len]))
- {
- rv = false;
- break;
- }
- }
- return rv;
- }
- //static
- bool LLLineEditor::prevalidatePrintableNotPipe(const LLWString& str)
- {
- bool rv = true;
- S32 len = str.length();
- if (len == 0) return rv;
- while (len--)
- {
- if ('|' == str[len])
- {
- rv = false;
- break;
- }
- if (!(' ' == str[len] || LLStringOps::isAlnum((char)str[len]) ||
- LLStringOps::isPunct((char)str[len])))
- {
- rv = false;
- break;
- }
- }
- return rv;
- }
- //static
- bool LLLineEditor::prevalidatePrintableNoSpace(const LLWString& str)
- {
- bool rv = true;
- S32 len = str.length();
- if (len == 0) return rv;
- while (len--)
- {
- if (LLStringOps::isSpace(str[len]))
- {
- rv = false;
- break;
- }
- if (!(LLStringOps::isAlnum((char)str[len]) ||
- LLStringOps::isPunct((char)str[len])))
- {
- rv = false;
- break;
- }
- }
- return rv;
- }
- //static
- bool LLLineEditor::prevalidateASCII(const LLWString& str)
- {
- bool rv = true;
- S32 len = str.length();
- while (len--)
- {
- if (str[len] < 0x20 || str[len] > 0x7f)
- {
- rv = false;
- break;
- }
- }
- return rv;
- }
- void LLLineEditor::onMouseCaptureLost()
- {
- endSelection();
- }
- void LLLineEditor::setSelectAllonFocusReceived(bool b)
- {
- mSelectAllonFocusReceived = b;
- }
- void LLLineEditor::setKeystrokeCallback(void (*keystroke_callback)(LLLineEditor*,
- void*))
- {
- mKeystrokeCallback = keystroke_callback;
- }
- void LLLineEditor::setOnHandleKeyCallback(bool (*callback)(KEY, MASK,
- LLLineEditor*,
- void*),
- void* userdata)
- {
- mOnHandleKeyCallback = callback;
- mOnHandleKeyData = userdata;
- }
- void LLLineEditor::setScrolledCallback(void (*scrolled_callback)(LLLineEditor* caller,
- void* user_data),
- void* userdata)
- {
- mScrolledCallback = scrolled_callback;
- mScrolledCallbackData = userdata;
- }
- //virtual
- const std::string& LLLineEditor::getTag() const
- {
- return LL_LINE_EDITOR_TAG;
- }
- //virtual
- LLXMLNodePtr LLLineEditor::getXML(bool save_children) const
- {
- LLXMLNodePtr node = LLUICtrl::getXML();
- node->setName(LL_LINE_EDITOR_TAG);
- node->createChild("max_length", true)->setIntValue(mMaxLengthBytes);
- node->createChild("font",
- true)->setStringValue(LLFontGL::nameFromFont(mGLFont));
- if (mBorder)
- {
- std::string bevel;
- switch (mBorder->getBevel())
- {
- case LLViewBorder::BEVEL_IN:
- bevel = "in";
- break;
- case LLViewBorder::BEVEL_OUT:
- bevel = "out";
- break;
- case LLViewBorder::BEVEL_BRIGHT:
- bevel = "bright";
- break;
- case LLViewBorder::BEVEL_NONE:
- default:
- bevel = "none";
- }
- node->createChild("bevel_style", true)->setStringValue(bevel);
- std::string style;
- if (mBorder->getStyle() == LLViewBorder::STYLE_TEXTURE)
- {
- style = "texture";
- }
- else
- {
- style = "line";
- }
- node->createChild("border_style", true)->setStringValue(style);
- node->createChild("border_thickness",
- true)->setIntValue(mBorder->getBorderWidth());
- }
- if (!mLabel.empty())
- {
- node->createChild("label", true)->setStringValue(mLabel.getString());
- }
- node->createChild("select_all_on_focus_received",
- true)->setBoolValue(mSelectAllonFocusReceived);
- node->createChild("handle_edit_keys_directly",
- true)->setBoolValue(mHandleEditKeysDirectly);
- addColorXML(node, mCursorColor, "cursor_color", "TextCursorColor");
- addColorXML(node, mFgColor, "text_color", "TextFgColor");
- addColorXML(node, mReadOnlyFgColor, "text_readonly_color",
- "TextFgReadOnlyColor");
- addColorXML(node, mTentativeFgColor, "text_tentative_color",
- "TextFgTentativeColor");
- addColorXML(node, mReadOnlyBgColor, "bg_readonly_color",
- "TextBgReadOnlyColor");
- addColorXML(node, mWriteableBgColor, "bg_writeable_color",
- "TextBgWriteableColor");
- addColorXML(node, mFocusBgColor, "bg_focus_color", "TextBgFocusColor");
- node->createChild("select_on_focus",
- true)->setBoolValue(mSelectAllonFocusReceived);
- return node;
- }
- //static
- LLView* LLLineEditor::fromXML(LLXMLNodePtr node, LLView* parent,
- LLUICtrlFactory*)
- {
- std::string name = LL_LINE_EDITOR_TAG;
- node->getAttributeString("name", name);
- LLRect rect;
- createRect(node, rect, parent, LLRect());
- S32 max_text_length = 128;
- node->getAttributeS32("max_length", max_text_length);
- LLFontGL* font = LLView::selectFont(node);
- std::string text = node->getTextContents().substr(0, max_text_length - 1);
- LLViewBorder::EBevel bevel_style = LLViewBorder::BEVEL_IN;
- LLViewBorder::getBevelFromAttribute(node, bevel_style);
- LLViewBorder::EStyle border_style = LLViewBorder::STYLE_LINE;
- std::string border_string;
- node->getAttributeString("border_style", border_string);
- LLStringUtil::toLower(border_string);
- if (border_string == "texture")
- {
- border_style = LLViewBorder::STYLE_TEXTURE;
- }
- S32 border_thickness = 1;
- node->getAttributeS32("border_thickness", border_thickness);
- LLLineEditor* line_editor = new LLLineEditor(name, rect, text, font,
- max_text_length,
- NULL, NULL, NULL, NULL, NULL,
- bevel_style, border_style,
- border_thickness);
- std::string label;
- if (node->getAttributeString("label", label))
- {
- line_editor->setLabel(label);
- }
- bool select_all_on_focus_received = false;
- if (node->getAttributeBool("select_all_on_focus_received",
- select_all_on_focus_received))
- {
- line_editor->setSelectAllonFocusReceived(select_all_on_focus_received);
- }
- bool handle_edit_keys_directly = false;
- if (node->getAttributeBool("handle_edit_keys_directly",
- handle_edit_keys_directly))
- {
- line_editor->setHandleEditKeysDirectly(handle_edit_keys_directly);
- }
- bool commit_on_focus_lost = true;
- if (node->getAttributeBool("commit_on_focus_lost",
- commit_on_focus_lost))
- {
- line_editor->setCommitOnFocusLost(commit_on_focus_lost);
- }
- bool spell_check = false;
- if (node->getAttributeBool("spell_check", spell_check))
- {
- line_editor->setSpellCheck(spell_check);
- }
- line_editor->setColorParameters(node);
- if (node->hasAttribute("select_on_focus"))
- {
- bool selectall = false;
- node->getAttributeBool("select_on_focus", selectall);
- line_editor->setSelectAllonFocusReceived(selectall);
- }
- std::string prevalidate;
- if (node->getAttributeString("prevalidate", prevalidate))
- {
- LLStringUtil::toLower(prevalidate);
- if (prevalidate == "ascii")
- {
- line_editor->setPrevalidate(prevalidateASCII);
- }
- else if (prevalidate == "float")
- {
- line_editor->setPrevalidate(prevalidateFloat);
- }
- else if (prevalidate == "int")
- {
- line_editor->setPrevalidate(prevalidateInt);
- }
- else if (prevalidate == "positive_s32")
- {
- line_editor->setPrevalidate(prevalidatePositiveS32);
- }
- else if (prevalidate == "non_negative_s32")
- {
- line_editor->setPrevalidate(prevalidateNonNegativeS32);
- }
- else if (prevalidate == "alpha_num")
- {
- line_editor->setPrevalidate(prevalidateAlphaNum);
- }
- else if (prevalidate == "alpha_num_space")
- {
- line_editor->setPrevalidate(prevalidateAlphaNumSpace);
- }
- else if (prevalidate == "printable_not_pipe")
- {
- line_editor->setPrevalidate(prevalidatePrintableNotPipe);
- }
- else if (prevalidate == "printable_no_space")
- {
- line_editor->setPrevalidate(prevalidatePrintableNoSpace);
- }
- }
- line_editor->initFromXML(node, parent);
- return line_editor;
- }
- //static
- void LLLineEditor::cleanupLineEditor()
- {
- sImage = NULL;
- }
- //static
- LLUIImagePtr LLLineEditor::parseImage(std::string name, LLXMLNodePtr from,
- LLUIImagePtr def)
- {
- std::string xml_name;
- if (from->hasAttribute(name.c_str()))
- {
- from->getAttributeString(name.c_str(), xml_name);
- }
- if (xml_name.empty())
- {
- return def;
- }
- LLUIImagePtr image = LLUI::getUIImage(xml_name);
- return image.isNull() ? def : image;
- }
- void LLLineEditor::setColorParameters(LLXMLNodePtr node)
- {
- // overrides default image if supplied.
- mImage = parseImage("image", node, mImage);
- LLColor4 color;
- if (LLUICtrlFactory::getAttributeColor(node,"cursor_color", color))
- {
- setCursorColor(color);
- }
- if (node->hasAttribute("text_color"))
- {
- LLUICtrlFactory::getAttributeColor(node,"text_color", color);
- setFgColor(color);
- }
- if (node->hasAttribute("text_readonly_color"))
- {
- LLUICtrlFactory::getAttributeColor(node,"text_readonly_color", color);
- setReadOnlyFgColor(color);
- }
- if (LLUICtrlFactory::getAttributeColor(node,"text_tentative_color", color))
- {
- setTentativeFgColor(color);
- }
- if (node->hasAttribute("bg_readonly_color"))
- {
- LLUICtrlFactory::getAttributeColor(node,"bg_readonly_color", color);
- setReadOnlyBgColor(color);
- }
- if (node->hasAttribute("bg_writeable_color"))
- {
- LLUICtrlFactory::getAttributeColor(node,"bg_writeable_color", color);
- setWriteableBgColor(color);
- }
- }
- void LLLineEditor::updateAllowingLanguageInput()
- {
- // Allow Language Text Input only when this LineEditor has no prevalidate
- // function attached (as long as other criteria common to LLTextEditor).
- // This criterion works fine on 1.15.0.2, since all prevalidate func
- // reject any non-ASCII characters. I'm not sure on future versions,
- // however...
- if (hasFocus() && !mReadOnly && !mDrawAsterixes && !mPrevalidateFunc)
- {
- gWindowp->allowLanguageTextInput(this, true);
- }
- else
- {
- gWindowp->allowLanguageTextInput(this, false);
- }
- }
- bool LLLineEditor::hasPreeditString() const
- {
- return mPreeditPositions.size() > 1;
- }
- void LLLineEditor::resetPreedit()
- {
- if (hasPreeditString())
- {
- if (hasSelection())
- {
- llwarns << "Preedit and selection ! Deselecting." << llendl;
- deselect();
- }
- const S32 preedit_pos = mPreeditPositions.front();
- mText.erase(preedit_pos, mPreeditPositions.back() - preedit_pos);
- mText.insert(preedit_pos, mPreeditOverwrittenWString);
- setCursor(preedit_pos);
- mPreeditWString.clear();
- mPreeditOverwrittenWString.clear();
- mPreeditPositions.clear();
- // Do not reset keystroke timer nor invoke keystroke callback, because
- // a call to updatePreedit should be follow soon in normal course of
- // operation, and timer and callback will be maintained there. Doing so
- // here made an odd sound (VWR-3410).
- }
- }
- void LLLineEditor::updatePreedit(const LLWString& preedit_string,
- const segment_lengths_t& preedit_segment_lengths,
- const standouts_t& preedit_standouts,
- S32 caret_position)
- {
- // Just in case.
- if (mReadOnly)
- {
- return;
- }
- // Note that call to updatePreedit is always preceeded by resetPreedit,
- // so we have no existing selection/preedit.
- S32 insert_preedit_at = mCursorPos;
- mPreeditWString = preedit_string;
- mPreeditPositions.resize(preedit_segment_lengths.size() + 1);
- S32 position = insert_preedit_at;
- for (segment_lengths_t::size_type i = 0,
- size = preedit_segment_lengths.size();
- i < size; ++i)
- {
- mPreeditPositions[i] = position;
- position += preedit_segment_lengths[i];
- }
- mPreeditPositions.back() = position;
- if (gKeyboardp && gKeyboardp->getInsertMode() == LL_KIM_OVERWRITE)
- {
- mPreeditOverwrittenWString.assign(LLWString(mText, insert_preedit_at,
- mPreeditWString.length()));
- mText.erase(insert_preedit_at, mPreeditWString.length());
- }
- else
- {
- mPreeditOverwrittenWString.clear();
- }
- mText.insert(insert_preedit_at, mPreeditWString);
- mPreeditStandouts = preedit_standouts;
- setCursor(position);
- setCursor(mPreeditPositions.front() + caret_position);
- // Update of the preedit should be caused by some key strokes.
- mKeystrokeTimer.reset();
- if (mKeystrokeCallback)
- {
- mKeystrokeCallback(this, mCallbackUserData);
- }
- }
- bool LLLineEditor::getPreeditLocation(S32 query_offset, LLCoordGL* coord,
- LLRect* bounds, LLRect* control) const
- {
- if (control)
- {
- LLRect control_rect_screen;
- localRectToScreen(getRect(), &control_rect_screen);
- LLUI::screenRectToGL(control_rect_screen, control);
- }
- S32 preedit_left_column, preedit_right_column;
- if (hasPreeditString())
- {
- preedit_left_column = mPreeditPositions.front();
- preedit_right_column = mPreeditPositions.back();
- }
- else
- {
- preedit_left_column = preedit_right_column = mCursorPos;
- }
- if (preedit_right_column < mScrollHPos)
- {
- // This should not occur...
- return false;
- }
- const S32 query = query_offset >= 0 ? preedit_left_column + query_offset
- : mCursorPos;
- if (query < mScrollHPos || query < preedit_left_column ||
- query > preedit_right_column)
- {
- return false;
- }
- if (coord)
- {
- S32 query_local = findPixelNearestPos(query - mCursorPos);
- S32 query_screen_x, query_screen_y;
- localPointToScreen(query_local, getRect().getHeight() / 2,
- &query_screen_x, &query_screen_y);
- LLUI::screenPointToGL(query_screen_x, query_screen_y, &coord->mX,
- &coord->mY);
- }
- if (bounds)
- {
- S32 preedit_left_local = findPixelNearestPos(llmax(preedit_left_column,
- mScrollHPos) -
- mCursorPos);
- S32 preedit_right_local = llmin(findPixelNearestPos(preedit_right_column -
- mCursorPos),
- getRect().getWidth() - mBorderThickness);
- if (preedit_left_local > preedit_right_local)
- {
- // Is this condition possible ?
- preedit_right_local = preedit_left_local;
- }
- LLRect preedit_rect_local(preedit_left_local, getRect().getHeight(),
- preedit_right_local, 0);
- LLRect preedit_rect_screen;
- localRectToScreen(preedit_rect_local, &preedit_rect_screen);
- LLUI::screenRectToGL(preedit_rect_screen, bounds);
- }
- return true;
- }
- void LLLineEditor::getPreeditRange(S32* position, S32* length) const
- {
- if (hasPreeditString())
- {
- *position = mPreeditPositions.front();
- *length = mPreeditPositions.back() - mPreeditPositions.front();
- }
- else
- {
- *position = mCursorPos;
- *length = 0;
- }
- }
- void LLLineEditor::getSelectionRange(S32* position, S32* length) const
- {
- if (hasSelection())
- {
- *position = llmin(mSelectionStart, mSelectionEnd);
- *length = abs(mSelectionStart - mSelectionEnd);
- }
- else
- {
- *position = mCursorPos;
- *length = 0;
- }
- }
- void LLLineEditor::markAsPreedit(S32 position, S32 length)
- {
- deselect();
- setCursor(position);
- if (hasPreeditString())
- {
- llwarns << "markAsPreedit invoked when hasPreeditString is true."
- << llendl;
- }
- mPreeditWString.assign(LLWString(mText.getWString(), position, length));
- if (length > 0)
- {
- mPreeditPositions.resize(2);
- mPreeditPositions[0] = position;
- mPreeditPositions[1] = position + length;
- mPreeditStandouts.resize(1);
- mPreeditStandouts[0] = false;
- }
- else
- {
- mPreeditPositions.clear();
- mPreeditStandouts.clear();
- }
- if (gKeyboardp && gKeyboardp->getInsertMode() == LL_KIM_OVERWRITE)
- {
- mPreeditOverwrittenWString = mPreeditWString;
- }
- else
- {
- mPreeditOverwrittenWString.clear();
- }
- }
- S32 LLLineEditor::getPreeditFontSize() const
- {
- return ll_roundp(mGLFont->getLineHeight() * LLUI::sGLScaleFactor.mV[VY]);
- }
- LLWString LLLineEditor::getConvertedText() const
- {
- LLWString text = getWText();
- LLWStringUtil::trim(text);
- if (!mReplaceNewlinesWithSpaces)
- {
- // Convert paragraph symbols back into newlines.
- LLWStringUtil::replaceChar(text, 182, '\n');
- }
- return text;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // LLSearchEditor class
- ///////////////////////////////////////////////////////////////////////////////
- static const std::string LL_SEARCH_EDITOR_TAG = "search_editor";
- static LLRegisterWidget<LLSearchEditor> r07(LL_SEARCH_EDITOR_TAG);
- LLSearchEditor::LLSearchEditor(const std::string& name, const LLRect& rect,
- S32 max_length)
- : LLUICtrl(name, rect, true, NULL, NULL),
- mSearchCallback(NULL),
- mLineCommitCallback(NULL),
- mCommitCallbackUserData(NULL)
- {
- LLRect line_edit_rect(0, getRect().getHeight(), getRect().getWidth(), 0);
- mSearchLineEditor = new LLLineEditor(name + "_line_editor", line_edit_rect,
- LLStringUtil::null, NULL, max_length,
- NULL, NULL, NULL, this);
- mSearchLineEditor->setFollowsAll();
- mSearchLineEditor->setSelectAllonFocusReceived(true);
- addChild(mSearchLineEditor);
- // Button is square, and as tall as search editor
- S32 btn_width = rect.getHeight();
- LLRect clear_btn_rect(rect.getWidth() - btn_width, rect.getHeight(),
- rect.getWidth(), 0);
- mClearSearchButton = new LLButton(name + "_clear_button", clear_btn_rect,
- "icn_clear_lineeditor.tga",
- "UIImgBtnCloseInactiveUUID",
- NULL, onClearSearch, this,
- NULL, LLStringUtil::null);
- mClearSearchButton->setFollowsRight();
- mClearSearchButton->setFollowsTop();
- mClearSearchButton->setImageColor(LLUI::sTextFgTentativeColor);
- mClearSearchButton->setTabStop(false);
- mClearSearchButton->setToolTip(LLTrans::getString("TooltipClearSearch"));
- mSearchLineEditor->addChild(mClearSearchButton);
- mSearchLineEditor->setTextPadding(0, btn_width);
- }
- //virtual
- void LLSearchEditor::clear()
- {
- if (mSearchLineEditor)
- {
- mSearchLineEditor->clear();
- }
- }
- //virtual
- void LLSearchEditor::draw()
- {
- mClearSearchButton->setVisible(!mSearchLineEditor->getWText().empty());
- LLUICtrl::draw();
- }
- void LLSearchEditor::setCommitCallback(void (*cb)(LLUICtrl*, void*))
- {
- mLineCommitCallback = cb;
- if (mLineCommitCallback)
- {
- mSearchLineEditor->setCommitCallback(onSearchEditCommit);
- }
- else
- {
- mSearchLineEditor->setCommitCallback(NULL);
- }
- }
- void LLSearchEditor::setSearchCallback(void (*cb)(const std::string&, void*),
- void* userdata)
- {
- mSearchCallback = cb;
- if (mSearchCallback)
- {
- mSearchLineEditor->setKeystrokeCallback(onSearchEditKeystroke);
- }
- else
- {
- mSearchLineEditor->setKeystrokeCallback(NULL);
- }
- mCallbackUserData = userdata;
- }
- //static
- void LLSearchEditor::onSearchEditCommit(LLUICtrl* ctrl, void* data)
- {
- LLSearchEditor* self = (LLSearchEditor*)data;
- if (self && self->mLineCommitCallback)
- {
- self->mLineCommitCallback(ctrl, self->mCommitCallbackUserData);
- }
- }
- //static
- void LLSearchEditor::onSearchEditKeystroke(LLLineEditor* caller, void* data)
- {
- LLSearchEditor* self = (LLSearchEditor*)data;
- if (caller && self && self->mSearchCallback)
- {
- self->mSearchCallback(caller->getText(), self->mCallbackUserData);
- }
- }
- //static
- void LLSearchEditor::onClearSearch(void* data)
- {
- LLSearchEditor* self = (LLSearchEditor*)data;
- if (!self) return;
- self->setText(LLStringUtil::null);
- if (self->mSearchCallback)
- {
- self->mSearchCallback(LLStringUtil::null, self->mCallbackUserData);
- }
- }
- //virtual
- const std::string& LLSearchEditor::getTag() const
- {
- return LL_SEARCH_EDITOR_TAG;
- }
- //virtual
- LLXMLNodePtr LLSearchEditor::getXML(bool save_children) const
- {
- LLXMLNodePtr nodep = LLUICtrl::getXML();
- nodep->setName(LL_SEARCH_EDITOR_TAG);
- return nodep;
- }
- //static
- LLView* LLSearchEditor::fromXML(LLXMLNodePtr nodep, LLView* parentp,
- LLUICtrlFactory*)
- {
- std::string name = LL_SEARCH_EDITOR_TAG;
- nodep->getAttributeString("name", name);
- LLRect rect;
- createRect(nodep, rect, parentp, LLRect());
- S32 max_text_length = 128;
- nodep->getAttributeS32("max_length", max_text_length);
- std::string text = nodep->getValue().substr(0, max_text_length - 1);
- LLSearchEditor* self = new LLSearchEditor(name, rect, max_text_length);
- std::string label;
- if (nodep->getAttributeString("label", label))
- {
- self->mSearchLineEditor->setLabel(label);
- }
- self->setText(text);
- self->initFromXML(nodep, parentp);
- return self;
- }
|