lllineeditor.cpp 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293
  1. /**
  2. * @file lllineeditor.cpp
  3. * @brief LLLineEditor base class
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. // Text editor widget to let users enter a single line.
  33. #include "linden_common.h"
  34. #include "lllineeditor.h"
  35. #include "llbutton.h"
  36. #include "llclipboard.h"
  37. #include "llcontrol.h"
  38. #include "llfasttimer.h"
  39. #include "llgl.h"
  40. #include "llkeyboard.h"
  41. #include "lllocale.h"
  42. #include "llmenugl.h"
  43. #include "llrect.h"
  44. #include "llspellcheck.h"
  45. #include "llstring.h"
  46. #include "lltimer.h"
  47. #include "lltrans.h"
  48. #include "lluictrlfactory.h"
  49. #include "llwindow.h"
  50. // gcc 12.4+ sees array bound issues where there are none... HB
  51. #if defined(GCC_VERSION) && GCC_VERSION >= 120400
  52. # pragma GCC diagnostic ignored "-Warray-bounds"
  53. # pragma GCC diagnostic ignored "-Wstringop-overflow"
  54. #endif
  55. //
  56. // Constants
  57. //
  58. constexpr S32 UI_LINEEDITOR_CURSOR_THICKNESS = 2;
  59. constexpr S32 UI_LINEEDITOR_H_PAD = 2;
  60. constexpr S32 UI_LINEEDITOR_V_PAD = 1;
  61. constexpr F32 CURSOR_FLASH_DELAY = 1.f; // In seconds
  62. constexpr S32 SCROLL_INCREMENT_ADD = 0; // Make space for typing
  63. constexpr S32 SCROLL_INCREMENT_DEL = 4; // Make space for baskspacing
  64. constexpr F32 AUTO_SCROLL_TIME = 0.05f;
  65. constexpr F32 MARKER_BRIGHTNESS = 0.4f;
  66. constexpr F32 STANDOUT_BRIGHTNESS = 0.6f;
  67. constexpr S32 PREEDIT_BORDER = 1;
  68. ///////////////////////////////////////////////////////////////////////////////
  69. // LLLineEditor class
  70. ///////////////////////////////////////////////////////////////////////////////
  71. static const std::string LL_LINE_EDITOR_TAG = "line_editor";
  72. static LLRegisterWidget<LLLineEditor> r06(LL_LINE_EDITOR_TAG);
  73. //static
  74. LLUIImagePtr LLLineEditor::sImage;
  75. LLLineEditor::LLLineEditor(const std::string& name, const LLRect& rect,
  76. const std::string& default_text,
  77. const LLFontGL* font, S32 max_length_bytes,
  78. void (*commit_callback)(LLUICtrl* caller,
  79. void* user_data),
  80. void (*keystroke_callback)(LLLineEditor* caller,
  81. void* user_data),
  82. void (*focus_lost_callback)(LLFocusableElement* caller,
  83. void* user_data),
  84. void* userdata,
  85. LLLinePrevalidateFunc prevalidate_func,
  86. LLViewBorder::EBevel border_bevel,
  87. LLViewBorder::EStyle border_style,
  88. S32 border_thickness)
  89. : LLEditMenuHandler(HAS_CONTEXT_MENU | HAS_CUSTOM),
  90. LLUICtrl(name, rect, true, commit_callback, userdata,
  91. FOLLOWS_TOP | FOLLOWS_LEFT),
  92. mMaxLengthBytes(max_length_bytes),
  93. mCursorPos(0),
  94. mScrollHPos(0),
  95. mTextPadLeft(0),
  96. mTextPadRight(0),
  97. mCommitOnFocusLost(true),
  98. mRevertOnEsc(true),
  99. mKeystrokeCallback(keystroke_callback),
  100. mOnHandleKeyCallback(NULL),
  101. mOnHandleKeyData(NULL),
  102. mScrolledCallback(NULL),
  103. mScrolledCallbackData(NULL),
  104. mIsSelecting(false),
  105. mSelectionStart(0),
  106. mSelectionEnd(0),
  107. mLastSelectionX(-1),
  108. mLastSelectionY(-1),
  109. mLastSelectionStart(-1),
  110. mLastSelectionEnd(-1),
  111. mPrevalidateFunc(prevalidate_func),
  112. mCursorColor(LLUI::sTextCursorColor),
  113. mFgColor(LLUI::sTextFgColor),
  114. mReadOnlyFgColor(LLUI::sTextFgReadOnlyColor),
  115. mTentativeFgColor(LLUI::sTextFgTentativeColor),
  116. mWriteableBgColor(LLUI::sTextBgWriteableColor),
  117. mReadOnlyBgColor(LLUI::sTextBgReadOnlyColor),
  118. mFocusBgColor(LLUI::sTextBgFocusColor),
  119. mBorderThickness(border_thickness),
  120. mIgnoreArrowKeys(false),
  121. mIgnoreTab(true),
  122. mDrawAsterixes(false),
  123. mHandleEditKeysDirectly(false),
  124. mSelectAllonFocusReceived(false),
  125. mPassDelete(false),
  126. mReadOnly(false),
  127. mHaveHistory(false),
  128. mImage(sImage),
  129. mReplaceNewlinesWithSpaces(true),
  130. mSpellCheck(false)
  131. {
  132. llassert(max_length_bytes > 0);
  133. // Initialize current history line iterator
  134. mCurrentHistoryLine = mLineHistory.begin();
  135. if (font)
  136. {
  137. mGLFont = font;
  138. }
  139. else
  140. {
  141. mGLFont = LLFontGL::getFontSansSerifSmall();
  142. }
  143. setFocusLostCallback(focus_lost_callback);
  144. setTextPadding(0, 0);
  145. mScrollTimer.reset();
  146. setText(default_text);
  147. setCursor(mText.length());
  148. // Scalable UI somehow made these rectangles off-by-one.
  149. // I don't know why. JC
  150. LLRect border_rect(0, getRect().getHeight() - 1,
  151. getRect().getWidth() - 1, 0);
  152. mBorder = new LLViewBorder("line ed border", border_rect, border_bevel,
  153. border_style, mBorderThickness);
  154. addChild(mBorder);
  155. mBorder->setFollows(FOLLOWS_LEFT | FOLLOWS_RIGHT | FOLLOWS_TOP |
  156. FOLLOWS_BOTTOM);
  157. if (!sImage)
  158. {
  159. sImage = LLUI::getUIImage("sm_rounded_corners_simple.tga");
  160. }
  161. mImage = sImage;
  162. mShowMisspelled = LLSpellCheck::getInstance()->getShowMisspelled();
  163. }
  164. LLLineEditor::~LLLineEditor()
  165. {
  166. mCommitOnFocusLost = false;
  167. gFocusMgr.releaseFocusIfNeeded(this);
  168. }
  169. void LLLineEditor::onFocusReceived()
  170. {
  171. grabMenuHandler();
  172. LLUICtrl::onFocusReceived();
  173. updateAllowingLanguageInput();
  174. }
  175. void LLLineEditor::onFocusLost()
  176. {
  177. // The call to updateAllowLanguageInput() when loosing the keyboard focus
  178. // *may* indirectly invoke handleUnicodeCharHere(), so it must be called
  179. // before onCommit.
  180. updateAllowingLanguageInput();
  181. if (mCommitOnFocusLost && mText.getString() != mPrevText)
  182. {
  183. onCommit();
  184. }
  185. releaseMenuHandler();
  186. gWindowp->showCursorFromMouseMove();
  187. LLUICtrl::onFocusLost();
  188. }
  189. void LLLineEditor::onCommit()
  190. {
  191. // Put current line into the line history
  192. updateHistory();
  193. setControlValue(getValue());
  194. LLUICtrl::onCommit();
  195. resetDirty();
  196. selectAll();
  197. }
  198. // line history support
  199. void LLLineEditor::updateHistory()
  200. {
  201. // On history enabled line editors, remember committed line and reset
  202. // current history line number. Be sure only to remember lines that are not
  203. // empty and that are different from the last on the list.
  204. if (mHaveHistory && getLength())
  205. {
  206. if (!mLineHistory.empty())
  207. {
  208. // When not empty, last line of history should always be blank.
  209. if (mLineHistory.back().empty())
  210. {
  211. // discard the empty line
  212. mLineHistory.pop_back();
  213. }
  214. else
  215. {
  216. llwarns << "Last line of history was not blank." << llendl;
  217. }
  218. }
  219. // Add text to history, ignoring duplicates
  220. if (mLineHistory.empty() || getText() != mLineHistory.back())
  221. {
  222. mLineHistory.emplace_back(getText());
  223. }
  224. // Restore the blank line and set mCurrentHistoryLine to point at it
  225. mLineHistory.emplace_back("");
  226. mCurrentHistoryLine = mLineHistory.end() - 1;
  227. }
  228. }
  229. void LLLineEditor::reshape(S32 width, S32 height, bool called_from_parent)
  230. {
  231. LLUICtrl::reshape(width, height, called_from_parent);
  232. // For clamping side-effect:
  233. setTextPadding(mTextPadLeft, mTextPadRight);
  234. setCursor(mCursorPos);
  235. }
  236. void LLLineEditor::setEnabled(bool enabled)
  237. {
  238. mReadOnly = !enabled;
  239. setTabStop(!mReadOnly);
  240. updateAllowingLanguageInput();
  241. }
  242. void LLLineEditor::setMaxTextLength(S32 max_text_length)
  243. {
  244. mMaxLengthBytes = llmax(0, max_text_length);
  245. }
  246. void LLLineEditor::setTextPadding(S32 left, S32 right)
  247. {
  248. mTextPadLeft = llclamp(left, 0, getRect().getWidth());
  249. mTextPadRight = llclamp(right, 0, getRect().getWidth());
  250. mMinHPixels = UI_LINEEDITOR_H_PAD + mTextPadLeft;
  251. mMaxHPixels = getRect().getWidth() - mMinHPixels - mTextPadRight;
  252. }
  253. void LLLineEditor::setText(const std::string& new_text)
  254. {
  255. // If new text is identical, do not copy and do not move insertion point
  256. if (mText.getString() == new_text)
  257. {
  258. return;
  259. }
  260. // Check to see if entire field is selected.
  261. S32 len = mText.length();
  262. bool all_selected = len > 0 &&
  263. ((mSelectionStart == 0 && mSelectionEnd == len) ||
  264. (mSelectionStart == len && mSelectionEnd == 0));
  265. // Do safe truncation so we do not split multi-byte characters. Also
  266. // consider entire string selected when mSelectAllonFocusReceived is set on
  267. // an empty, focused line editor.
  268. all_selected = all_selected ||
  269. (len == 0 && hasFocus() && mSelectAllonFocusReceived);
  270. std::string truncated_utf8 = new_text;
  271. if (truncated_utf8.size() > (U32)mMaxLengthBytes)
  272. {
  273. truncated_utf8 = utf8str_truncate(new_text, mMaxLengthBytes);
  274. }
  275. mText.assign(truncated_utf8);
  276. if (all_selected)
  277. {
  278. // ...keep whole thing selected
  279. selectAll();
  280. }
  281. else
  282. {
  283. // Try to preserve insertion point, but deselect text
  284. deselect();
  285. }
  286. setCursor(llmin((S32)mText.length(), mCursorPos));
  287. // Set current history line to end of history.
  288. mCurrentHistoryLine = mLineHistory.end() - 1;
  289. mPrevText = mText;
  290. }
  291. // Picks a new cursor position based on the actual screen size of text being
  292. // drawn.
  293. S32 LLLineEditor::calculateCursorFromMouse(S32 local_mouse_x)
  294. {
  295. const llwchar* wtext = mText.getWString().c_str();
  296. LLWString asterix_text;
  297. if (mDrawAsterixes)
  298. {
  299. for (S32 i = 0, len = mText.length(); i < len; ++i)
  300. {
  301. asterix_text += (llwchar)0x2022L;
  302. }
  303. wtext = asterix_text.c_str();
  304. }
  305. return mScrollHPos +
  306. mGLFont->charFromPixelOffset(wtext, mScrollHPos,
  307. (F32)(local_mouse_x - mMinHPixels),
  308. // min-max range is inclusive
  309. (F32)(mMaxHPixels - mMinHPixels + 1));
  310. }
  311. void LLLineEditor::setCursorAtLocalPos(S32 local_mouse_x)
  312. {
  313. setCursor(calculateCursorFromMouse(local_mouse_x));
  314. }
  315. void LLLineEditor::setCursor(S32 pos)
  316. {
  317. S32 old_cursor_pos = mCursorPos;
  318. S32 old_scroll_pos = mScrollHPos;
  319. mCursorPos = llclamp(pos, 0, mText.length());
  320. S32 pixels_after_scroll = findPixelNearestPos();
  321. if (pixels_after_scroll > mMaxHPixels)
  322. {
  323. const llwchar* wtext = mText.getWString().c_str();
  324. LLWString asterix_text;
  325. if (mDrawAsterixes)
  326. {
  327. for (S32 i = 0, len = mText.length(); i < len; ++i)
  328. {
  329. asterix_text += (llwchar)0x2022L;
  330. }
  331. wtext = asterix_text.c_str();
  332. }
  333. std::string saved_text;
  334. if (mDrawAsterixes)
  335. {
  336. saved_text = mText.getString();
  337. std::string text;
  338. for (S32 i = 0, len = mText.length(); i < len; ++i)
  339. {
  340. text += '*';
  341. }
  342. mText = text;
  343. }
  344. S32 width_chars_to_left = mGLFont->getWidth(wtext, 0, mScrollHPos);
  345. S32 last_visible_char =
  346. mGLFont->maxDrawableChars(wtext,
  347. llmax(0.f, (F32)(mMaxHPixels -
  348. mMinHPixels +
  349. width_chars_to_left)));
  350. S32 min_scroll =
  351. mGLFont->firstDrawableChar(wtext,
  352. (F32)(mMaxHPixels - mMinHPixels -
  353. UI_LINEEDITOR_CURSOR_THICKNESS -
  354. UI_LINEEDITOR_H_PAD),
  355. mText.length(), mCursorPos);
  356. if (old_cursor_pos == last_visible_char)
  357. {
  358. mScrollHPos = llmin(mText.length(),
  359. llmax(min_scroll,
  360. mScrollHPos + SCROLL_INCREMENT_ADD));
  361. }
  362. else
  363. {
  364. mScrollHPos = min_scroll;
  365. }
  366. }
  367. else if (mCursorPos < mScrollHPos)
  368. {
  369. if (old_cursor_pos == mScrollHPos)
  370. {
  371. mScrollHPos = llmax(0,
  372. llmin(mCursorPos,
  373. mScrollHPos - SCROLL_INCREMENT_DEL));
  374. }
  375. else
  376. {
  377. mScrollHPos = mCursorPos;
  378. }
  379. }
  380. if (old_scroll_pos == 0 && mScrollHPos != 0 && mScrolledCallback)
  381. {
  382. mScrolledCallback(this, mScrolledCallbackData);
  383. }
  384. }
  385. void LLLineEditor::setCursorToEnd()
  386. {
  387. setCursor(mText.length());
  388. deselect();
  389. }
  390. void LLLineEditor::resetScrollPosition()
  391. {
  392. mScrollHPos = 0;
  393. // Make sure cursor says in visible range
  394. setCursor(getCursor());
  395. }
  396. void LLLineEditor::deselect()
  397. {
  398. mSelectionStart = 0;
  399. mSelectionEnd = 0;
  400. mIsSelecting = false;
  401. }
  402. void LLLineEditor::startSelection()
  403. {
  404. mIsSelecting = true;
  405. mSelectionStart = mCursorPos;
  406. mSelectionEnd = mCursorPos;
  407. }
  408. void LLLineEditor::endSelection()
  409. {
  410. if (mIsSelecting)
  411. {
  412. mIsSelecting = false;
  413. mSelectionEnd = mCursorPos;
  414. }
  415. }
  416. void LLLineEditor::selectAll()
  417. {
  418. mSelectionStart = mText.length();
  419. mSelectionEnd = 0;
  420. setCursor(mSelectionEnd);
  421. mIsSelecting = true;
  422. }
  423. void LLLineEditor::spellCorrect(void* data)
  424. {
  425. SpellMenuBind* menu_bind = (SpellMenuBind*)data;
  426. LLLineEditor* line = menu_bind->mOrigin;
  427. if (menu_bind && line)
  428. {
  429. LL_DEBUGS("SpellCheck") << menu_bind->mMenuItem->getName()
  430. << " : " << menu_bind->mOrigin->getName()
  431. << " : " << menu_bind->mWord << LL_ENDL;
  432. line->spellReplace(menu_bind);
  433. // Make it update:
  434. line->mKeystrokeTimer.reset();
  435. line->mPrevSpelledText.erase();
  436. }
  437. }
  438. void LLLineEditor::spellShow(void* data)
  439. {
  440. SpellMenuBind* menu_bind = (SpellMenuBind*)data;
  441. LLLineEditor* line = menu_bind->mOrigin;
  442. if (menu_bind && line)
  443. {
  444. line->mShowMisspelled = (menu_bind->mWord == "Show Misspellings");
  445. // Make it update:
  446. line->mKeystrokeTimer.reset();
  447. line->mPrevSpelledText.erase();
  448. }
  449. }
  450. void LLLineEditor::spellAdd(void* data)
  451. {
  452. SpellMenuBind* menu_bind = (SpellMenuBind*)data;
  453. LLLineEditor* line = menu_bind->mOrigin;
  454. if (menu_bind && line)
  455. {
  456. LLSpellCheck::getInstance()->addToCustomDictionary(menu_bind->mWord);
  457. // Make it update:
  458. line->mKeystrokeTimer.reset();
  459. line->mPrevSpelledText.erase();
  460. }
  461. }
  462. void LLLineEditor::spellIgnore(void* data)
  463. {
  464. SpellMenuBind* menu_bind = (SpellMenuBind*)data;
  465. LLLineEditor* line = menu_bind->mOrigin;
  466. if (menu_bind && line)
  467. {
  468. LLSpellCheck::getInstance()->addToIgnoreList(menu_bind->mWord);
  469. // Make it update:
  470. line->mKeystrokeTimer.reset();
  471. line->mPrevSpelledText.erase();
  472. }
  473. }
  474. std::vector<S32> LLLineEditor::getMisspelledWordsPositions()
  475. {
  476. std::vector<S32> bad_words_pos;
  477. const LLWString& text = mText.getWString();
  478. std::string selected_word;
  479. S32 word_start = 0;
  480. S32 word_end = mSpellCheckStart;
  481. S32 true_end;
  482. while (word_end < mSpellCheckEnd)
  483. {
  484. if (LLWStringUtil::isPartOfLexicalWord(text[word_end]))
  485. {
  486. // Select the word under the cursor
  487. while (word_end > 0 &&
  488. LLWStringUtil::isPartOfLexicalWord(text[word_end - 1]))
  489. {
  490. --word_end;
  491. }
  492. if (text[word_end] == L'\'')
  493. {
  494. // Do not count "'" at the start of a word
  495. ++word_end;
  496. }
  497. word_start = word_end;
  498. while (word_end < (S32)text.length() &&
  499. LLWStringUtil::isPartOfLexicalWord(text[word_end]))
  500. {
  501. ++word_end;
  502. }
  503. if (text[word_end - 1] == L'\'')
  504. {
  505. // Do not count "'" at the end of a word
  506. true_end = word_end - 1;
  507. }
  508. else
  509. {
  510. true_end = word_end;
  511. }
  512. // Do not bother for 2 or less characters words
  513. if (true_end > word_start + 2)
  514. {
  515. std::string part(text.begin(), text.end());
  516. selected_word = part.substr(word_start, true_end - word_start);
  517. if (!LLSpellCheck::getInstance()->checkSpelling(selected_word))
  518. {
  519. // Misspelled word here
  520. bad_words_pos.emplace_back(word_start);
  521. bad_words_pos.emplace_back(true_end);
  522. }
  523. }
  524. }
  525. ++word_end;
  526. }
  527. return bad_words_pos;
  528. }
  529. bool LLLineEditor::handleDoubleClick(S32 x, S32 y, MASK mask)
  530. {
  531. setFocus(true);
  532. if (mSelectionEnd == 0 && mSelectionStart == mText.length())
  533. {
  534. // If everything is selected, handle this as a normal click to change
  535. // insertion point
  536. handleMouseDown(x, y, mask);
  537. }
  538. else
  539. {
  540. const LLWString& wtext = mText.getWString();
  541. bool do_select_all = true;
  542. // Select the word we're on
  543. if (LLWStringUtil::isPartOfWord(wtext[mCursorPos]))
  544. {
  545. S32 old_selection_start = mLastSelectionStart;
  546. S32 old_selection_end = mLastSelectionEnd;
  547. // Select word the cursor is over
  548. while (mCursorPos > 0 &&
  549. LLWStringUtil::isPartOfWord(wtext[mCursorPos - 1]))
  550. { // Find the start of the word
  551. --mCursorPos;
  552. }
  553. startSelection();
  554. while (mCursorPos < (S32)wtext.length() &&
  555. LLWStringUtil::isPartOfWord(wtext[mCursorPos]))
  556. { // Find the end of the word
  557. ++mCursorPos;
  558. }
  559. mSelectionEnd = mCursorPos;
  560. // If nothing changed, then the word was already selected. Select
  561. // the whole line.
  562. do_select_all = old_selection_start == mSelectionStart &&
  563. old_selection_end == mSelectionEnd;
  564. }
  565. if (do_select_all)
  566. {
  567. selectAll();
  568. }
  569. }
  570. // We do not want handleMouseUp() to "finish" the selection (and thereby
  571. // set mSelectionEnd to where the mouse is), so we finish the selection
  572. // here.
  573. mIsSelecting = false;
  574. // Delay cursor flashing
  575. mKeystrokeTimer.reset();
  576. // Take selection to 'primary' clipboard
  577. updatePrimary();
  578. return true;
  579. }
  580. bool LLLineEditor::handleMouseDown(S32 x, S32 y, MASK mask)
  581. {
  582. // Check first whether the "clear search" button wants to deal with this.
  583. if (childrenHandleMouseDown(x, y, mask))
  584. {
  585. return true;
  586. }
  587. if (mSelectAllonFocusReceived && gFocusMgr.getKeyboardFocus() != this)
  588. {
  589. setFocus(true);
  590. }
  591. else
  592. {
  593. mLastSelectionStart = -1;
  594. setFocus(true);
  595. if (mask & MASK_SHIFT)
  596. {
  597. // Handle selection extension
  598. S32 old_cursor_pos = mCursorPos;
  599. setCursorAtLocalPos(x);
  600. if (hasSelection())
  601. {
  602. mSelectionEnd = mCursorPos;
  603. }
  604. else
  605. {
  606. mSelectionStart = old_cursor_pos;
  607. mSelectionEnd = mCursorPos;
  608. }
  609. // Assume we are starting a drag select
  610. mIsSelecting = true;
  611. }
  612. else
  613. {
  614. // Save selection for word/line selecting on double-click
  615. mLastSelectionStart = mSelectionStart;
  616. mLastSelectionEnd = mSelectionEnd;
  617. // Move cursor and deselect for regular click
  618. setCursorAtLocalPos(x);
  619. deselect();
  620. startSelection();
  621. }
  622. gFocusMgr.setMouseCapture(this);
  623. }
  624. // delay cursor flashing
  625. mKeystrokeTimer.reset();
  626. return true;
  627. }
  628. bool LLLineEditor::handleMiddleMouseDown(S32 x, S32 y, MASK mask)
  629. {
  630. setFocus(true);
  631. if (canPastePrimary())
  632. {
  633. setCursorAtLocalPos(x);
  634. pastePrimary();
  635. }
  636. return true;
  637. }
  638. bool LLLineEditor::handleRightMouseDown(S32 x, S32 y, MASK mask)
  639. {
  640. setFocus(true);
  641. S32 word_start = 0;
  642. S32 word_len = 0;
  643. S32 pos = calculateCursorFromMouse(x);
  644. // If the context menu has not yet been created for this editor, this call
  645. // will create it now. HB
  646. LLMenuGL* menu = createContextMenu();
  647. if (menu)
  648. {
  649. SpellMenuBind* menu_bind;
  650. LLMenuItemCallGL* menu_item;
  651. // Remove old suggestions
  652. for (S32 i = 0, count = mSuggestionMenuItems.size(); i < count; ++i)
  653. {
  654. menu_bind = mSuggestionMenuItems[i];
  655. if (menu_bind)
  656. {
  657. menu_item = menu_bind->mMenuItem;
  658. menu->remove(menu_item);
  659. menu_item->die();
  660. //delete menu_bind->mMenuItem;
  661. //menu_bind->mMenuItem = NULL;
  662. delete menu_bind;
  663. }
  664. }
  665. mSuggestionMenuItems.clear();
  666. LLSpellCheck* checker = NULL;
  667. // Not read-only, spell_check="true" in XUI and spell checking enabled
  668. bool spell_check = !mReadOnly && mSpellCheck;
  669. if (spell_check)
  670. {
  671. checker = LLSpellCheck::getInstance();
  672. spell_check = checker->getSpellCheck();
  673. }
  674. menu->setItemVisible("spell_sep", spell_check);
  675. if (spell_check)
  676. {
  677. // search for word matches
  678. bool is_word_part = getWordBoundriesAt(pos, &word_start,
  679. &word_len);
  680. if (is_word_part)
  681. {
  682. const LLWString& text = mText.getWString();
  683. std::string part(text.begin(), text.end());
  684. std::string selected_word = part.substr(word_start, word_len);
  685. if (!checker->checkSpelling(selected_word))
  686. {
  687. // misspelled word here
  688. std::vector<std::string> suggestions;
  689. S32 count = checker->getSuggestions(selected_word,
  690. suggestions);
  691. for (S32 i = 0; i < count; ++i)
  692. {
  693. menu_bind = new SpellMenuBind;
  694. menu_bind->mOrigin = this;
  695. menu_bind->mWord = suggestions[i];
  696. menu_bind->mWordPositionEnd = word_start + word_len;
  697. menu_bind->mWordPositionStart = word_start;
  698. menu_item = new LLMenuItemCallGL(menu_bind->mWord,
  699. spellCorrect,
  700. NULL, menu_bind);
  701. menu_bind->mMenuItem = menu_item;
  702. mSuggestionMenuItems.push_back(menu_bind);
  703. menu->append(menu_item);
  704. }
  705. menu_bind = new SpellMenuBind;
  706. menu_bind->mOrigin = this;
  707. menu_bind->mWord = selected_word;
  708. menu_bind->mWordPositionEnd = word_start + word_len;
  709. menu_bind->mWordPositionStart = word_start;
  710. menu_item = new LLMenuItemCallGL("Add word", spellAdd,
  711. NULL, menu_bind);
  712. menu_bind->mMenuItem = menu_item;
  713. mSuggestionMenuItems.push_back(menu_bind);
  714. menu->append(menu_item);
  715. menu_bind = new SpellMenuBind;
  716. menu_bind->mOrigin = this;
  717. menu_bind->mWord = selected_word;
  718. menu_bind->mWordPositionEnd = word_start + word_len;
  719. menu_bind->mWordPositionStart = word_start;
  720. menu_item = new LLMenuItemCallGL("Ignore word",
  721. spellIgnore,
  722. NULL, menu_bind);
  723. menu_bind->mMenuItem = menu_item;
  724. mSuggestionMenuItems.push_back(menu_bind);
  725. menu->append(menu_item);
  726. }
  727. }
  728. menu_bind = new SpellMenuBind;
  729. menu_bind->mOrigin = this;
  730. if (mShowMisspelled)
  731. {
  732. menu_bind->mWord = "Hide misspellings";
  733. }
  734. else
  735. {
  736. menu_bind->mWord = "Show misspellings";
  737. }
  738. menu_item = new LLMenuItemCallGL(menu_bind->mWord, spellShow,
  739. NULL, menu_bind);
  740. menu_bind->mMenuItem = menu_item;
  741. mSuggestionMenuItems.push_back(menu_bind);
  742. menu->append(menu_item);
  743. }
  744. menu->buildDrawLabels();
  745. menu->updateParent(LLMenuGL::sMenuContainer);
  746. LLMenuGL::showPopup(this, menu, x, y);
  747. }
  748. return true;
  749. }
  750. bool LLLineEditor::handleHover(S32 x, S32 y, MASK mask)
  751. {
  752. // Check first whether the "clear search" button wants to deal with this.
  753. if (!hasMouseCapture())
  754. {
  755. if (childrenHandleHover(x, y, mask) != NULL)
  756. {
  757. return true;
  758. }
  759. }
  760. bool handled = false;
  761. if (hasMouseCapture() && mIsSelecting)
  762. {
  763. if (x != mLastSelectionX || y != mLastSelectionY)
  764. {
  765. mLastSelectionX = x;
  766. mLastSelectionY = y;
  767. }
  768. // Scroll if mouse cursor outside of bounds
  769. if (mScrollTimer.hasExpired())
  770. {
  771. S32 increment = ll_roundp(mScrollTimer.getElapsedTimeF32() /
  772. AUTO_SCROLL_TIME);
  773. mScrollTimer.reset();
  774. mScrollTimer.setTimerExpirySec(AUTO_SCROLL_TIME);
  775. if (x < mMinHPixels && mScrollHPos > 0)
  776. {
  777. // Scroll to the left
  778. mScrollHPos = llclamp(mScrollHPos - increment, 0,
  779. mText.length());
  780. }
  781. else if (x > mMaxHPixels && mCursorPos < (S32)mText.length())
  782. {
  783. // If scrolling one pixel would make a difference...
  784. S32 pixels_after_scrolling_one_char = findPixelNearestPos(1);
  785. if (pixels_after_scrolling_one_char >= mMaxHPixels)
  786. {
  787. // ...scroll to the right
  788. mScrollHPos = llclamp(mScrollHPos + increment, 0,
  789. mText.length());
  790. }
  791. }
  792. }
  793. setCursorAtLocalPos(x);
  794. mSelectionEnd = mCursorPos;
  795. // Delay cursor flashing
  796. mKeystrokeTimer.reset();
  797. gWindowp->setCursor(UI_CURSOR_IBEAM);
  798. LL_DEBUGS("UserInput") << "hover handled by " << getName()
  799. << " (active)" << LL_ENDL;
  800. handled = true;
  801. }
  802. if (!handled)
  803. {
  804. gWindowp->setCursor(UI_CURSOR_IBEAM);
  805. LL_DEBUGS("UserInput") << "hover handled by " << getName()
  806. << " (inactive)" << LL_ENDL;
  807. handled = true;
  808. }
  809. return handled;
  810. }
  811. bool LLLineEditor::handleMouseUp(S32 x, S32 y, MASK mask)
  812. {
  813. bool handled = false;
  814. if (hasMouseCapture())
  815. {
  816. gFocusMgr.setMouseCapture(NULL);
  817. handled = true;
  818. }
  819. // Check first whether the "clear search" button wants to deal with this.
  820. if (!handled && childrenHandleMouseUp(x, y, mask) != NULL)
  821. {
  822. return true;
  823. }
  824. if (mIsSelecting)
  825. {
  826. setCursorAtLocalPos(x);
  827. mSelectionEnd = mCursorPos;
  828. handled = true;
  829. }
  830. if (handled)
  831. {
  832. // Delay cursor flashing
  833. mKeystrokeTimer.reset();
  834. // Take selection to 'primary' clipboard
  835. updatePrimary();
  836. }
  837. return handled;
  838. }
  839. // Remove a single character from the text
  840. void LLLineEditor::removeChar()
  841. {
  842. if (mCursorPos > 0)
  843. {
  844. mText.erase(mCursorPos - 1, 1);
  845. setCursor(mCursorPos - 1);
  846. }
  847. else
  848. {
  849. reportBadKeystroke();
  850. }
  851. }
  852. void LLLineEditor::addChar(llwchar uni_char)
  853. {
  854. llwchar new_c = uni_char;
  855. if (hasSelection())
  856. {
  857. deleteSelection();
  858. }
  859. else if (gKeyboardp && gKeyboardp->getInsertMode() == LL_KIM_OVERWRITE)
  860. {
  861. mText.erase(mCursorPos, 1);
  862. }
  863. S32 cur_bytes = mText.getString().size();
  864. S32 new_bytes = wchar_utf8_length(new_c);
  865. // Check byte length limit
  866. if (new_bytes + cur_bytes <= mMaxLengthBytes)
  867. {
  868. // Will we need to scroll ?
  869. LLWString w_buf;
  870. w_buf.assign(1, new_c);
  871. mText.insert(mCursorPos, w_buf);
  872. setCursor(mCursorPos + 1);
  873. }
  874. else
  875. {
  876. reportBadKeystroke();
  877. }
  878. gWindowp->hideCursorUntilMouseMove();
  879. }
  880. // Extends the selection box to the new cursor position
  881. void LLLineEditor::extendSelection(S32 new_cursor_pos)
  882. {
  883. if (!mIsSelecting)
  884. {
  885. startSelection();
  886. }
  887. setCursor(new_cursor_pos);
  888. mSelectionEnd = mCursorPos;
  889. }
  890. void LLLineEditor::setSelection(S32 start, S32 end)
  891. {
  892. // JC, yes, this seems odd, but I think you have to presume a selection
  893. // dragged from the end towards the start.
  894. S32 len = mText.length();
  895. mSelectionStart = llclamp(end, 0, len);
  896. mSelectionEnd = llclamp(start, 0, len);
  897. mIsSelecting = true;
  898. setCursor(start);
  899. }
  900. void LLLineEditor::setDrawAsterixes(bool b)
  901. {
  902. mDrawAsterixes = b;
  903. updateAllowingLanguageInput();
  904. }
  905. S32 LLLineEditor::prevWordPos(S32 cursor_pos) const
  906. {
  907. const LLWString& wtext = mText.getWString();
  908. while (cursor_pos > 0 && wtext[cursor_pos - 1] == ' ')
  909. {
  910. --cursor_pos;
  911. }
  912. while (cursor_pos > 0 &&
  913. LLWStringUtil::isPartOfWord(wtext[cursor_pos - 1]))
  914. {
  915. --cursor_pos;
  916. }
  917. return cursor_pos;
  918. }
  919. S32 LLLineEditor::nextWordPos(S32 cursor_pos) const
  920. {
  921. const LLWString& wtext = mText.getWString();
  922. while (cursor_pos < getLength() &&
  923. LLWStringUtil::isPartOfWord(wtext[cursor_pos]))
  924. {
  925. ++cursor_pos;
  926. }
  927. while (cursor_pos < getLength() && wtext[cursor_pos] == ' ')
  928. {
  929. ++cursor_pos;
  930. }
  931. return cursor_pos;
  932. }
  933. bool LLLineEditor::getWordBoundriesAt(S32 at, S32* word_begin,
  934. S32* word_length) const
  935. {
  936. const LLWString& wtext = mText.getWString();
  937. S32 pos = at;
  938. S32 start;
  939. if (LLWStringUtil::isPartOfLexicalWord(wtext[pos]))
  940. {
  941. while (pos > 0 && LLWStringUtil::isPartOfLexicalWord(wtext[pos - 1]))
  942. {
  943. --pos;
  944. }
  945. if (wtext[pos] == L'\'')
  946. {
  947. // Do not count "'" at the start of a word
  948. ++pos;
  949. }
  950. start = pos;
  951. while (pos < (S32)wtext.length() &&
  952. LLWStringUtil::isPartOfLexicalWord(wtext[pos]))
  953. {
  954. ++pos;
  955. }
  956. if (wtext[pos - 1] == L'\'')
  957. {
  958. // Do not count "'" at the end of a word
  959. --pos;
  960. }
  961. if (start >= pos)
  962. {
  963. return false;
  964. }
  965. *word_begin = start;
  966. *word_length = pos - start;
  967. return true;
  968. }
  969. return false;
  970. }
  971. void LLLineEditor::spellReplace(SpellMenuBind* data)
  972. {
  973. if (data)
  974. {
  975. S32 length = data->mWordPositionEnd - data->mWordPositionStart;
  976. mText.erase(data->mWordPositionStart, length);
  977. insert(data->mWord, data->mWordPositionStart);
  978. mCursorPos += data->mWord.length() - length;
  979. }
  980. }
  981. void LLLineEditor::insert(std::string what, S32 where)
  982. {
  983. LLLineEditorRollback rollback(this);
  984. LLWString clean_string(utf8str_to_wstring(what));
  985. LLWStringUtil::replaceTabsWithSpaces(clean_string, 4);
  986. mText.insert(where, clean_string);
  987. // See if we should move over the cursor acordingly. Validate new string
  988. // and rollback if needed.
  989. if (mPrevalidateFunc && !mPrevalidateFunc(mText.getWString()))
  990. {
  991. rollback.doRollback(this);
  992. reportBadKeystroke();
  993. }
  994. else if (mKeystrokeCallback)
  995. {
  996. mKeystrokeCallback(this, mCallbackUserData);
  997. }
  998. }
  999. bool LLLineEditor::handleSelectionKey(KEY key, MASK mask)
  1000. {
  1001. bool handled = false;
  1002. if (mask & MASK_SHIFT)
  1003. {
  1004. handled = true;
  1005. switch (key)
  1006. {
  1007. case KEY_LEFT:
  1008. if (mCursorPos > 0)
  1009. {
  1010. S32 cursor_pos = mCursorPos - 1;
  1011. if (mask & MASK_CONTROL)
  1012. {
  1013. cursor_pos = prevWordPos(cursor_pos);
  1014. }
  1015. extendSelection(cursor_pos);
  1016. }
  1017. else
  1018. {
  1019. reportBadKeystroke();
  1020. }
  1021. break;
  1022. case KEY_RIGHT:
  1023. if (mCursorPos < mText.length())
  1024. {
  1025. S32 cursor_pos = mCursorPos + 1;
  1026. if (mask & MASK_CONTROL)
  1027. {
  1028. cursor_pos = nextWordPos(cursor_pos);
  1029. }
  1030. extendSelection(cursor_pos);
  1031. }
  1032. else
  1033. {
  1034. reportBadKeystroke();
  1035. }
  1036. break;
  1037. case KEY_PAGE_UP:
  1038. case KEY_HOME:
  1039. extendSelection(0);
  1040. break;
  1041. case KEY_PAGE_DOWN:
  1042. case KEY_END:
  1043. {
  1044. S32 len = mText.length();
  1045. if (len)
  1046. {
  1047. extendSelection(len);
  1048. }
  1049. break;
  1050. }
  1051. default:
  1052. handled = false;
  1053. }
  1054. }
  1055. if (!handled && mHandleEditKeysDirectly && (MASK_CONTROL & mask) &&
  1056. key == 'A')
  1057. {
  1058. if (canSelectAll())
  1059. {
  1060. selectAll();
  1061. }
  1062. else
  1063. {
  1064. reportBadKeystroke();
  1065. }
  1066. handled = true;
  1067. }
  1068. if (handled)
  1069. {
  1070. // take selection to 'primary' clipboard
  1071. updatePrimary();
  1072. }
  1073. return handled;
  1074. }
  1075. void LLLineEditor::deleteSelection()
  1076. {
  1077. if (!mReadOnly && hasSelection())
  1078. {
  1079. S32 left_pos = llmin(mSelectionStart, mSelectionEnd);
  1080. S32 selection_length = abs(mSelectionStart - mSelectionEnd);
  1081. mText.erase(left_pos, selection_length);
  1082. deselect();
  1083. setCursor(left_pos);
  1084. // Force spell-check update:
  1085. mKeystrokeTimer.reset();
  1086. mPrevSpelledText.erase();
  1087. }
  1088. }
  1089. bool LLLineEditor::canCut() const
  1090. {
  1091. return !mReadOnly && !mDrawAsterixes && hasSelection();
  1092. }
  1093. // Cut selection to clipboard
  1094. void LLLineEditor::cut()
  1095. {
  1096. if (canCut())
  1097. {
  1098. // Prepare for possible rollback
  1099. LLLineEditorRollback rollback(this);
  1100. S32 left_pos = llmin(mSelectionStart, mSelectionEnd);
  1101. S32 length = abs(mSelectionStart - mSelectionEnd);
  1102. gClipboard.copyFromSubstring(mText.getWString(), left_pos, length);
  1103. deleteSelection();
  1104. // Validate new string and rollback the if needed.
  1105. bool need_to_rollback = mPrevalidateFunc &&
  1106. !mPrevalidateFunc(mText.getWString());
  1107. if (need_to_rollback)
  1108. {
  1109. rollback.doRollback(this);
  1110. reportBadKeystroke();
  1111. }
  1112. else if (mKeystrokeCallback)
  1113. {
  1114. mKeystrokeCallback(this, mCallbackUserData);
  1115. }
  1116. // Force spell-check update:
  1117. mKeystrokeTimer.reset();
  1118. mPrevSpelledText.erase();
  1119. }
  1120. }
  1121. bool LLLineEditor::canCopy() const
  1122. {
  1123. return !mDrawAsterixes && hasSelection();
  1124. }
  1125. // Copy selection to clipboard
  1126. void LLLineEditor::copy()
  1127. {
  1128. if (canCopy())
  1129. {
  1130. S32 left_pos = llmin(mSelectionStart, mSelectionEnd);
  1131. S32 length = abs(mSelectionStart - mSelectionEnd);
  1132. gClipboard.copyFromSubstring(mText.getWString(), left_pos, length);
  1133. // Force spell-check update:
  1134. mKeystrokeTimer.reset();
  1135. mPrevSpelledText.erase();
  1136. }
  1137. }
  1138. bool LLLineEditor::canPaste() const
  1139. {
  1140. return !mReadOnly && gClipboard.canPasteString();
  1141. }
  1142. void LLLineEditor::paste()
  1143. {
  1144. bool is_primary = false;
  1145. pasteHelper(is_primary);
  1146. }
  1147. void LLLineEditor::pastePrimary()
  1148. {
  1149. bool is_primary = true;
  1150. pasteHelper(is_primary);
  1151. }
  1152. // Paste from primary (is_primary==true) or clipboard (is_primary==false)
  1153. void LLLineEditor::pasteHelper(bool is_primary)
  1154. {
  1155. bool can_paste_it;
  1156. if (is_primary)
  1157. {
  1158. can_paste_it = canPastePrimary();
  1159. }
  1160. else
  1161. {
  1162. can_paste_it = canPaste();
  1163. }
  1164. if (can_paste_it)
  1165. {
  1166. LLWString paste;
  1167. if (is_primary)
  1168. {
  1169. paste = gClipboard.getPastePrimaryWString();
  1170. }
  1171. else
  1172. {
  1173. paste = gClipboard.getPasteWString();
  1174. }
  1175. if (!paste.empty())
  1176. {
  1177. // Prepare for possible rollback
  1178. LLLineEditorRollback rollback(this);
  1179. // Delete any selected characters
  1180. if (!is_primary && hasSelection())
  1181. {
  1182. deleteSelection();
  1183. }
  1184. // Clean up string (replace tabs and returns and remove characters
  1185. // that our fonts do not support)
  1186. LLWString clean_string(paste);
  1187. LLWStringUtil::replaceTabsWithSpaces(clean_string, 1);
  1188. // Note: character 182 is the paragraph character
  1189. LLWStringUtil::replaceChar(clean_string, '\n',
  1190. mReplaceNewlinesWithSpaces ? ' '
  1191. : 182);
  1192. // Insert the string
  1193. // Check to see that the size is not going to be larger than the
  1194. // max number of bytes
  1195. U32 available_bytes = mMaxLengthBytes - wstring_utf8_length(mText);
  1196. if (available_bytes < (U32)wstring_utf8_length(clean_string))
  1197. {
  1198. // Does not all fit
  1199. llwchar current_symbol = clean_string[0];
  1200. U32 wchars_that_fit = 0;
  1201. U32 total_bytes = wchar_utf8_length(current_symbol);
  1202. // Loop over the "wide" characters (symbols) and check to see
  1203. // how large (in bytes) each symbol is.
  1204. while (total_bytes <= available_bytes)
  1205. {
  1206. // While we still have available bytes "accept" the current
  1207. // symbol and check the size of the next one
  1208. current_symbol = clean_string[++wchars_that_fit];
  1209. total_bytes += wchar_utf8_length(current_symbol);
  1210. }
  1211. // Truncate the clean string at the limit of what will fit
  1212. clean_string = clean_string.substr(0, wchars_that_fit);
  1213. reportBadKeystroke();
  1214. }
  1215. mText.insert(mCursorPos, clean_string);
  1216. setCursor(mCursorPos + (S32)clean_string.length());
  1217. deselect();
  1218. // Validate new string and rollback if needed.
  1219. if (mPrevalidateFunc && !mPrevalidateFunc(mText.getWString()))
  1220. {
  1221. rollback.doRollback(this);
  1222. reportBadKeystroke();
  1223. }
  1224. else if (mKeystrokeCallback)
  1225. {
  1226. mKeystrokeCallback(this, mCallbackUserData);
  1227. }
  1228. }
  1229. // Force spell-check update:
  1230. mKeystrokeTimer.reset();
  1231. mPrevSpelledText.erase();
  1232. }
  1233. }
  1234. // Copy selection to primary
  1235. void LLLineEditor::copyPrimary()
  1236. {
  1237. if (canCopy())
  1238. {
  1239. S32 left_pos = llmin(mSelectionStart, mSelectionEnd);
  1240. S32 length = abs(mSelectionStart - mSelectionEnd);
  1241. gClipboard.copyFromPrimarySubstring(mText.getWString(), left_pos,
  1242. length);
  1243. // Force spell-check update:
  1244. mKeystrokeTimer.reset();
  1245. mPrevSpelledText.erase();
  1246. }
  1247. }
  1248. bool LLLineEditor::canPastePrimary() const
  1249. {
  1250. return !mReadOnly && gClipboard.canPastePrimaryString();
  1251. }
  1252. void LLLineEditor::updatePrimary()
  1253. {
  1254. if (canCopy())
  1255. {
  1256. copyPrimary();
  1257. }
  1258. }
  1259. bool LLLineEditor::handleSpecialKey(KEY key, MASK mask)
  1260. {
  1261. bool handled = false;
  1262. switch (key)
  1263. {
  1264. case KEY_INSERT:
  1265. if (mask == MASK_NONE && gKeyboardp)
  1266. {
  1267. gKeyboardp->toggleInsertMode();
  1268. }
  1269. handled = true;
  1270. break;
  1271. case KEY_BACKSPACE:
  1272. if (!mReadOnly)
  1273. {
  1274. if (hasSelection())
  1275. {
  1276. deleteSelection();
  1277. }
  1278. else if (mCursorPos > 0)
  1279. {
  1280. removeChar();
  1281. }
  1282. else
  1283. {
  1284. reportBadKeystroke();
  1285. }
  1286. }
  1287. handled = true;
  1288. break;
  1289. case KEY_PAGE_UP:
  1290. case KEY_HOME:
  1291. if (!mIgnoreArrowKeys)
  1292. {
  1293. setCursor(0);
  1294. handled = true;
  1295. }
  1296. break;
  1297. case KEY_PAGE_DOWN:
  1298. case KEY_END:
  1299. if (!mIgnoreArrowKeys)
  1300. {
  1301. S32 len = mText.length();
  1302. if (len)
  1303. {
  1304. setCursor(len);
  1305. }
  1306. handled = true;
  1307. }
  1308. break;
  1309. case KEY_LEFT:
  1310. if (mIgnoreArrowKeys && mask == MASK_NONE)
  1311. {
  1312. break;
  1313. }
  1314. if ((mask & MASK_ALT) == 0)
  1315. {
  1316. if (hasSelection())
  1317. {
  1318. setCursor(llmin(mCursorPos - 1, mSelectionStart,
  1319. mSelectionEnd));
  1320. }
  1321. else if (mCursorPos > 0)
  1322. {
  1323. S32 cursor_pos = mCursorPos - 1;
  1324. if (mask & MASK_CONTROL)
  1325. {
  1326. cursor_pos = prevWordPos(cursor_pos);
  1327. }
  1328. setCursor(cursor_pos);
  1329. }
  1330. else
  1331. {
  1332. reportBadKeystroke();
  1333. }
  1334. handled = true;
  1335. }
  1336. break;
  1337. case KEY_RIGHT:
  1338. if (mIgnoreArrowKeys && mask == MASK_NONE)
  1339. {
  1340. break;
  1341. }
  1342. if ((mask & MASK_ALT) == 0)
  1343. {
  1344. if (hasSelection())
  1345. {
  1346. setCursor(llmax(mCursorPos + 1, mSelectionStart,
  1347. mSelectionEnd));
  1348. }
  1349. else if (mCursorPos < mText.length())
  1350. {
  1351. S32 cursor_pos = mCursorPos + 1;
  1352. if (mask & MASK_CONTROL)
  1353. {
  1354. cursor_pos = nextWordPos(cursor_pos);
  1355. }
  1356. setCursor(cursor_pos);
  1357. }
  1358. else
  1359. {
  1360. reportBadKeystroke();
  1361. }
  1362. handled = true;
  1363. }
  1364. break;
  1365. // handle ctrl-uparrow if we have a history enabled line editor.
  1366. case KEY_UP:
  1367. if (mHaveHistory && mask == MASK_CONTROL)
  1368. {
  1369. if (mCurrentHistoryLine > mLineHistory.begin())
  1370. {
  1371. mText.assign(*(--mCurrentHistoryLine));
  1372. setCursor(llmin((S32)mText.length(), mCursorPos));
  1373. }
  1374. else
  1375. {
  1376. reportBadKeystroke();
  1377. }
  1378. handled = true;
  1379. }
  1380. break;
  1381. // handle ctrl-downarrow if we have a history enabled line editor
  1382. case KEY_DOWN:
  1383. if (mHaveHistory && mask == MASK_CONTROL)
  1384. {
  1385. if (!mLineHistory.empty() &&
  1386. mCurrentHistoryLine < mLineHistory.end() - 1)
  1387. {
  1388. mText.assign(*(++mCurrentHistoryLine));
  1389. setCursor(llmin((S32)mText.length(), mCursorPos));
  1390. }
  1391. else
  1392. {
  1393. reportBadKeystroke();
  1394. }
  1395. handled = true;
  1396. }
  1397. break;
  1398. case KEY_RETURN:
  1399. // store sent line in history
  1400. updateHistory();
  1401. break;
  1402. case KEY_ESCAPE:
  1403. if (mask == MASK_NONE && mRevertOnEsc &&
  1404. mText.getString() != mPrevText)
  1405. {
  1406. setText(mPrevText);
  1407. // Note, do not set handled, still want to loose focus (would
  1408. // not commit because text is now unchanged)
  1409. }
  1410. break;
  1411. default:
  1412. break;
  1413. }
  1414. if (!handled && mHandleEditKeysDirectly)
  1415. {
  1416. // Standard edit keys (Ctrl-X, Delete, etc,) are handled here instead
  1417. // of routed by the menu system.
  1418. if (key == KEY_DELETE)
  1419. {
  1420. if (canDoDelete())
  1421. {
  1422. doDelete();
  1423. }
  1424. else
  1425. {
  1426. reportBadKeystroke();
  1427. }
  1428. handled = true;
  1429. }
  1430. else if (MASK_CONTROL & mask)
  1431. {
  1432. if (key == 'C')
  1433. {
  1434. if (canCopy())
  1435. {
  1436. copy();
  1437. }
  1438. else
  1439. {
  1440. reportBadKeystroke();
  1441. }
  1442. handled = true;
  1443. }
  1444. else if (key == 'V')
  1445. {
  1446. if (canPaste())
  1447. {
  1448. paste();
  1449. }
  1450. else
  1451. {
  1452. reportBadKeystroke();
  1453. }
  1454. handled = true;
  1455. }
  1456. else if (key == 'X')
  1457. {
  1458. if (canCut())
  1459. {
  1460. cut();
  1461. }
  1462. else
  1463. {
  1464. reportBadKeystroke();
  1465. }
  1466. handled = true;
  1467. }
  1468. }
  1469. }
  1470. return handled;
  1471. }
  1472. bool LLLineEditor::handleKeyHere(KEY key, MASK mask)
  1473. {
  1474. bool handled = false;
  1475. // Key presses are not being passed to the Popup menu. A proper fix is
  1476. // non-trivial so instead just close the menu.
  1477. LLMenuGL* menu = getContextMenu();
  1478. if (menu && menu->isOpen())
  1479. {
  1480. LLMenuGL::sMenuContainer->hideMenus();
  1481. }
  1482. if (gFocusMgr.getKeyboardFocus() == this)
  1483. {
  1484. LLLineEditorRollback rollback(this);
  1485. bool selection_modified = false;
  1486. if (!handled)
  1487. {
  1488. handled = handleSelectionKey(key, mask);
  1489. selection_modified = handled;
  1490. }
  1491. // Handle most keys only if the text editor is writeable.
  1492. if (!mReadOnly)
  1493. {
  1494. if (!handled && mOnHandleKeyCallback)
  1495. {
  1496. handled = mOnHandleKeyCallback(key, mask, this,
  1497. mOnHandleKeyData);
  1498. }
  1499. if (!handled)
  1500. {
  1501. handled = handleSpecialKey(key, mask);
  1502. }
  1503. }
  1504. if (handled)
  1505. {
  1506. mKeystrokeTimer.reset();
  1507. // Most keystrokes will make the selection box go away, but not all
  1508. // will.
  1509. if (!selection_modified && KEY_SHIFT != key &&
  1510. KEY_CONTROL != key && KEY_ALT != key && KEY_CAPSLOCK)
  1511. {
  1512. deselect();
  1513. }
  1514. // If read-only, don't allow changes
  1515. bool need_to_rollback = mReadOnly &&
  1516. mText.getString() == rollback.getText();
  1517. if (!need_to_rollback)
  1518. {
  1519. // Validate new string and rollback the keystroke if needed.
  1520. need_to_rollback = mPrevalidateFunc &&
  1521. !mPrevalidateFunc(mText.getWString());
  1522. }
  1523. if (need_to_rollback)
  1524. {
  1525. rollback.doRollback(this);
  1526. reportBadKeystroke();
  1527. }
  1528. // Notify owner if requested
  1529. if (!need_to_rollback && handled)
  1530. {
  1531. if (mKeystrokeCallback)
  1532. {
  1533. mKeystrokeCallback(this, mCallbackUserData);
  1534. }
  1535. }
  1536. }
  1537. }
  1538. return handled;
  1539. }
  1540. bool LLLineEditor::handleUnicodeCharHere(llwchar uni_char)
  1541. {
  1542. if (uni_char < 0x20 || uni_char == 0x7F) // Control character or DEL
  1543. {
  1544. return false;
  1545. }
  1546. bool handled = false;
  1547. if (gFocusMgr.getKeyboardFocus() == this && getVisible() && !mReadOnly)
  1548. {
  1549. // Key presses are not being passed to the pop-up menu.
  1550. // A proper fix is non-trivial so instead just close the menu.
  1551. LLMenuGL* menu = getContextMenu();
  1552. if (menu && menu->isOpen())
  1553. {
  1554. LLMenuGL::sMenuContainer->hideMenus();
  1555. }
  1556. handled = true;
  1557. LLLineEditorRollback rollback(this);
  1558. addChar(uni_char);
  1559. mKeystrokeTimer.reset();
  1560. deselect();
  1561. // Validate new string and rollback the keystroke if needed.
  1562. bool need_to_rollback = mPrevalidateFunc &&
  1563. !mPrevalidateFunc(mText.getWString());
  1564. if (need_to_rollback)
  1565. {
  1566. rollback.doRollback(this);
  1567. reportBadKeystroke();
  1568. }
  1569. // Notify owner if requested
  1570. if (!need_to_rollback && handled && mKeystrokeCallback)
  1571. {
  1572. // *HACK: the only usage of this callback does not do anything with
  1573. // the character. We will have to do something about this if
  1574. // something ever changes - Doug
  1575. mKeystrokeCallback(this, mCallbackUserData);
  1576. }
  1577. }
  1578. return handled;
  1579. }
  1580. bool LLLineEditor::canDoDelete() const
  1581. {
  1582. return !mReadOnly &&
  1583. (!mPassDelete || hasSelection() || mCursorPos < mText.length());
  1584. }
  1585. void LLLineEditor::doDelete()
  1586. {
  1587. if (canDoDelete() && !mText.empty())
  1588. {
  1589. // Prepare for possible rollback
  1590. LLLineEditorRollback rollback(this);
  1591. if (hasSelection())
  1592. {
  1593. deleteSelection();
  1594. }
  1595. else if (mCursorPos < mText.length())
  1596. {
  1597. setCursor(mCursorPos + 1);
  1598. removeChar();
  1599. }
  1600. // Validate new string and rollback the if needed.
  1601. if (mPrevalidateFunc && !mPrevalidateFunc(mText.getWString()))
  1602. {
  1603. rollback.doRollback(this);
  1604. reportBadKeystroke();
  1605. }
  1606. else
  1607. {
  1608. if (mKeystrokeCallback)
  1609. {
  1610. mKeystrokeCallback(this, mCallbackUserData);
  1611. }
  1612. }
  1613. // Force spell-check update:
  1614. mKeystrokeTimer.reset();
  1615. mPrevSpelledText.erase();
  1616. }
  1617. }
  1618. void LLLineEditor::drawMisspelled(LLRect background)
  1619. {
  1620. LL_FAST_TIMER(FTM_RENDER_SPELLCHECK);
  1621. S32 elapsed = (S32)mSpellTimer.getElapsedTimeF32();
  1622. S32 keystroke = (S32)mKeystrokeTimer.getElapsedTimeF32();
  1623. // Do not bother checking if the text did not change in a while and fire a
  1624. // spell checking only once a second while typing.
  1625. if (keystroke < 2 && (elapsed & 1))
  1626. {
  1627. S32 new_start_spell = mScrollHPos;
  1628. S32 cursorloc = calculateCursorFromMouse(mMaxHPixels);
  1629. S32 length = (S32)mText.length();
  1630. S32 new_end_spell = length > cursorloc ? cursorloc : length;
  1631. if (new_start_spell != mSpellCheckStart ||
  1632. new_end_spell != mSpellCheckEnd || isSpellDirty())
  1633. {
  1634. mSpellCheckStart = new_start_spell;
  1635. mSpellCheckEnd = new_end_spell;
  1636. resetSpellDirty();
  1637. mMisspellLocations = getMisspelledWordsPositions();
  1638. }
  1639. }
  1640. if (mShowMisspelled)
  1641. {
  1642. const S32 bottom = background.mBottom;
  1643. const S32 maxw = getRect().getWidth();
  1644. for (S32 i = 0, count = mMisspellLocations.size(); i < count; ++i)
  1645. {
  1646. S32 wstart =
  1647. findPixelNearestPos(mMisspellLocations[i++] - mCursorPos);
  1648. if (wstart > maxw)
  1649. {
  1650. wstart = maxw;
  1651. }
  1652. S32 wend = findPixelNearestPos(mMisspellLocations[i] - mCursorPos);
  1653. if (wend > maxw)
  1654. {
  1655. wend = maxw;
  1656. }
  1657. // Draw the zig zag line
  1658. gGL.color4ub(255, 0, 0, 200);
  1659. while (wstart < wend)
  1660. {
  1661. gl_line_2d(wstart, bottom - 1, wstart + 3, bottom + 2);
  1662. gl_line_2d(wstart + 3, bottom + 2, wstart + 6, bottom - 1);
  1663. wstart += 6;
  1664. }
  1665. }
  1666. }
  1667. }
  1668. void LLLineEditor::draw()
  1669. {
  1670. S32 text_len = mText.length();
  1671. std::string saved_text;
  1672. if (mDrawAsterixes)
  1673. {
  1674. saved_text = mText.getString();
  1675. std::string text;
  1676. for (S32 i = 0, len = mText.length(); i < len; ++i)
  1677. {
  1678. text += '*';
  1679. }
  1680. mText = text;
  1681. }
  1682. // Draw rectangle for the background
  1683. LLRect background(0, getRect().getHeight(), getRect().getWidth(), 0);
  1684. background.stretch(-mBorderThickness);
  1685. LLColor4 bg_color = mReadOnlyBgColor;
  1686. // Drawing solids requires texturing be disabled
  1687. {
  1688. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1689. // Draw background for text
  1690. if (!mReadOnly)
  1691. {
  1692. if (gFocusMgr.getKeyboardFocus() == this)
  1693. {
  1694. bg_color = mFocusBgColor;
  1695. }
  1696. else
  1697. {
  1698. bg_color = mWriteableBgColor;
  1699. }
  1700. }
  1701. gl_rect_2d(background, bg_color);
  1702. }
  1703. // Draw text
  1704. S32 cursor_bottom = background.mBottom + 1;
  1705. S32 cursor_top = background.mTop - 1;
  1706. LLColor4 text_color;
  1707. if (!mReadOnly)
  1708. {
  1709. if (!getTentative())
  1710. {
  1711. text_color = mFgColor;
  1712. }
  1713. else
  1714. {
  1715. text_color = mTentativeFgColor;
  1716. }
  1717. }
  1718. else
  1719. {
  1720. text_color = mReadOnlyFgColor;
  1721. }
  1722. LLColor4 label_color = mTentativeFgColor;
  1723. if (hasPreeditString())
  1724. {
  1725. // Draw preedit markers. This needs to be before drawing letters.
  1726. for (U32 i = 0, size = mPreeditStandouts.size(); i < size; ++i)
  1727. {
  1728. const S32 preedit_left = mPreeditPositions[i];
  1729. const S32 preedit_right = mPreeditPositions[i + 1];
  1730. if (preedit_right > mScrollHPos)
  1731. {
  1732. S32 preedit_pixels_left =
  1733. findPixelNearestPos(llmax(preedit_left,
  1734. mScrollHPos) - mCursorPos);
  1735. S32 preedit_pixels_right =
  1736. llmin(findPixelNearestPos(preedit_right - mCursorPos),
  1737. background.mRight);
  1738. if (preedit_pixels_left >= background.mRight)
  1739. {
  1740. break;
  1741. }
  1742. LLColor4 color;
  1743. if (mPreeditStandouts[i])
  1744. {
  1745. color = (text_color * STANDOUT_BRIGHTNESS +
  1746. bg_color *
  1747. (1.f - STANDOUT_BRIGHTNESS)).setAlpha(1.f);
  1748. }
  1749. else
  1750. {
  1751. color = (text_color * MARKER_BRIGHTNESS +
  1752. bg_color *
  1753. (1.f - MARKER_BRIGHTNESS)).setAlpha(1.f);
  1754. }
  1755. gl_rect_2d(preedit_pixels_left + PREEDIT_BORDER,
  1756. background.mBottom + PREEDIT_BORDER,
  1757. preedit_pixels_right - PREEDIT_BORDER,
  1758. background.mBottom, color);
  1759. }
  1760. }
  1761. }
  1762. S32 rendered_text = 0;
  1763. F32 rendered_pixels_right = (F32)mMinHPixels;
  1764. F32 text_bottom = (F32)background.mBottom + (F32)UI_LINEEDITOR_V_PAD;
  1765. if (gFocusMgr.getKeyboardFocus() == this && hasSelection())
  1766. {
  1767. S32 select_left;
  1768. S32 select_right;
  1769. if (mSelectionStart < mCursorPos)
  1770. {
  1771. select_left = mSelectionStart;
  1772. select_right = mCursorPos;
  1773. }
  1774. else
  1775. {
  1776. select_left = mCursorPos;
  1777. select_right = mSelectionStart;
  1778. }
  1779. if (select_left > mScrollHPos)
  1780. {
  1781. // Unselected, left side
  1782. rendered_text = mGLFont->render(mText, mScrollHPos,
  1783. rendered_pixels_right, text_bottom,
  1784. text_color, LLFontGL::LEFT,
  1785. LLFontGL::BOTTOM, LLFontGL::NORMAL,
  1786. select_left - mScrollHPos,
  1787. mMaxHPixels - ll_round(rendered_pixels_right),
  1788. &rendered_pixels_right);
  1789. }
  1790. if (rendered_pixels_right < (F32)mMaxHPixels &&
  1791. rendered_text < text_len)
  1792. {
  1793. LLColor4 color(1.f - bg_color.mV[0], 1.f - bg_color.mV[1],
  1794. 1.f - bg_color.mV[2], 1.f);
  1795. // Selected middle
  1796. S32 width = mGLFont->getWidth(mText.getWString().c_str(),
  1797. mScrollHPos + rendered_text,
  1798. select_right - mScrollHPos - rendered_text);
  1799. S32 right_delta = ll_round(rendered_pixels_right);
  1800. width = llmin(width, mMaxHPixels - right_delta);
  1801. gl_rect_2d(right_delta, cursor_top, right_delta + width,
  1802. cursor_bottom, color);
  1803. rendered_text += mGLFont->render(mText, mScrollHPos + rendered_text,
  1804. rendered_pixels_right, text_bottom,
  1805. LLColor4(1.f - text_color.mV[0],
  1806. 1.f - text_color.mV[1],
  1807. 1.f - text_color.mV[2],
  1808. 1),
  1809. LLFontGL::LEFT, LLFontGL::BOTTOM,
  1810. LLFontGL::NORMAL,
  1811. select_right - mScrollHPos - rendered_text,
  1812. mMaxHPixels - right_delta,
  1813. &rendered_pixels_right);
  1814. }
  1815. if (rendered_pixels_right < (F32)mMaxHPixels &&
  1816. rendered_text < text_len)
  1817. {
  1818. // Unselected, right side
  1819. mGLFont->render(mText, mScrollHPos + rendered_text,
  1820. rendered_pixels_right, text_bottom, text_color,
  1821. LLFontGL::LEFT, LLFontGL::BOTTOM, LLFontGL::NORMAL,
  1822. S32_MAX,
  1823. mMaxHPixels - ll_round(rendered_pixels_right),
  1824. &rendered_pixels_right);
  1825. }
  1826. }
  1827. else
  1828. {
  1829. mGLFont->render(mText, mScrollHPos, rendered_pixels_right, text_bottom,
  1830. text_color, LLFontGL::LEFT, LLFontGL::BOTTOM,
  1831. LLFontGL::NORMAL, S32_MAX,
  1832. mMaxHPixels - ll_round(rendered_pixels_right),
  1833. &rendered_pixels_right);
  1834. }
  1835. if (!mReadOnly && mSpellCheck && hasFocus() &&
  1836. LLSpellCheck::getInstance()->getSpellCheck())
  1837. {
  1838. drawMisspelled(background);
  1839. }
  1840. // If we are editing...
  1841. if (gFocusMgr.getKeyboardFocus() == this)
  1842. {
  1843. //mBorder->setVisible(true); // ok, programmer art just this once.
  1844. // (Flash the cursor every half second)
  1845. if (gShowTextEditCursor && !mReadOnly)
  1846. {
  1847. F32 elapsed = mKeystrokeTimer.getElapsedTimeF32();
  1848. if (elapsed < CURSOR_FLASH_DELAY || (S32(elapsed * 2) & 1))
  1849. {
  1850. S32 cursor_left = findPixelNearestPos();
  1851. cursor_left -= UI_LINEEDITOR_CURSOR_THICKNESS / 2;
  1852. S32 cursor_right = cursor_left + UI_LINEEDITOR_CURSOR_THICKNESS;
  1853. bool ins_mode = !hasSelection() && gKeyboardp &&
  1854. gKeyboardp->getInsertMode() == LL_KIM_OVERWRITE;
  1855. if (ins_mode)
  1856. {
  1857. static const LLWString space(utf8str_to_wstring(" "));
  1858. S32 wswidth = mGLFont->getWidth(space.c_str());
  1859. S32 width = mGLFont->getWidth(mText.getWString().c_str(),
  1860. mCursorPos, 1) + 1;
  1861. cursor_right = cursor_left + llmax(wswidth, width);
  1862. }
  1863. // Use same color as text for the Cursor
  1864. gl_rect_2d(cursor_left, cursor_top, cursor_right,
  1865. cursor_bottom, text_color);
  1866. if (ins_mode)
  1867. {
  1868. mGLFont->render(mText, mCursorPos,
  1869. (F32)(cursor_left +
  1870. UI_LINEEDITOR_CURSOR_THICKNESS / 2),
  1871. text_bottom,
  1872. LLColor4(1.f - text_color.mV[0],
  1873. 1.f - text_color.mV[1],
  1874. 1.f - text_color.mV[2], 1),
  1875. LLFontGL::LEFT, LLFontGL::BOTTOM,
  1876. LLFontGL::NORMAL, 1);
  1877. }
  1878. // Make sure the IME is in the right place
  1879. // RCalculcate for IME position
  1880. S32 pixels_after_scroll = findPixelNearestPos();
  1881. LLRect screen_pos = getScreenRect();
  1882. LLCoordGL ime_pos(screen_pos.mLeft + pixels_after_scroll,
  1883. screen_pos.mTop - UI_LINEEDITOR_V_PAD);
  1884. ime_pos.mX = (S32)(ime_pos.mX * LLUI::sGLScaleFactor.mV[VX]);
  1885. ime_pos.mY = (S32)(ime_pos.mY * LLUI::sGLScaleFactor.mV[VY]);
  1886. gWindowp->setLanguageTextInput(ime_pos);
  1887. }
  1888. }
  1889. // Draw label if no text is provided but we should draw it in a
  1890. // different color to give indication that it is not text you typed in
  1891. if (mText.empty() && mReadOnly)
  1892. {
  1893. mGLFont->render(mLabel.getWString(), 0, mMinHPixels,
  1894. (F32)text_bottom, label_color, LLFontGL::LEFT,
  1895. LLFontGL::BOTTOM, LLFontGL::NORMAL, S32_MAX,
  1896. mMaxHPixels - ll_round(rendered_pixels_right),
  1897. &rendered_pixels_right, false);
  1898. }
  1899. // Draw children (border)
  1900. mBorder->setKeyboardFocusHighlight(true);
  1901. LLView::draw();
  1902. mBorder->setKeyboardFocusHighlight(false);
  1903. }
  1904. else // Does not have keyboard input
  1905. {
  1906. // Draw label if no text provided
  1907. if (mText.empty())
  1908. {
  1909. mGLFont->render(mLabel.getWString(), 0, mMinHPixels,
  1910. (F32)text_bottom, label_color,
  1911. LLFontGL::LEFT, LLFontGL::BOTTOM, LLFontGL::NORMAL,
  1912. S32_MAX,
  1913. mMaxHPixels - ll_round(rendered_pixels_right),
  1914. &rendered_pixels_right, false);
  1915. }
  1916. // Draw children (border)
  1917. LLView::draw();
  1918. }
  1919. if (mDrawAsterixes)
  1920. {
  1921. mText = saved_text;
  1922. }
  1923. }
  1924. // Returns the local screen space X coordinate associated with the text cursor
  1925. // position.
  1926. S32 LLLineEditor::findPixelNearestPos(S32 cursor_offset) const
  1927. {
  1928. S32 dpos = mCursorPos - mScrollHPos + cursor_offset;
  1929. S32 width;
  1930. if (mDrawAsterixes)
  1931. {
  1932. LLWString asterix;
  1933. for (S32 i = 0, len = mText.length(); i < len; ++i)
  1934. {
  1935. asterix += llwchar('*');
  1936. }
  1937. width = mGLFont->getWidth(asterix.c_str(), mScrollHPos, dpos);
  1938. }
  1939. else
  1940. {
  1941. width = mGLFont->getWidth(mText.getWString().c_str(), mScrollHPos,
  1942. dpos);
  1943. }
  1944. return mMinHPixels + width;
  1945. }
  1946. void LLLineEditor::reportBadKeystroke()
  1947. {
  1948. make_ui_sound("UISndBadKeystroke");
  1949. }
  1950. //virtual
  1951. void LLLineEditor::clear()
  1952. {
  1953. mText.clear();
  1954. setCursor(0);
  1955. }
  1956. //virtual
  1957. void LLLineEditor::onTabInto()
  1958. {
  1959. selectAll();
  1960. }
  1961. // Start or stop the editor from accepting text-editing keystrokes
  1962. void LLLineEditor::setFocus(bool new_state)
  1963. {
  1964. bool old_state = hasFocus();
  1965. if (!new_state)
  1966. {
  1967. gWindowp->allowLanguageTextInput(this, false);
  1968. }
  1969. // Getting focus when we did not have it before, and we want to select all
  1970. if (!old_state && new_state && mSelectAllonFocusReceived)
  1971. {
  1972. selectAll();
  1973. // We do not want handleMouseUp() to "finish" the selection (and
  1974. // thereby set mSelectionEnd to where the mouse is), so we finish the
  1975. // selection here.
  1976. mIsSelecting = false;
  1977. }
  1978. if (new_state)
  1979. {
  1980. grabMenuHandler();
  1981. // Do not start the cursor flashing right away
  1982. mKeystrokeTimer.reset();
  1983. }
  1984. else
  1985. {
  1986. // Not really needed, since loss of keyboard focus should take care of
  1987. // this, but limited paranoia is ok.
  1988. releaseMenuHandler();
  1989. endSelection();
  1990. }
  1991. LLUICtrl::setFocus(new_state);
  1992. if (new_state)
  1993. {
  1994. // Allow Language Text Input only when this LineEditor has no
  1995. // prevalidate function attached. This criterion works fine for now,
  1996. // since all prevalidate func reject any non-ASCII characters. I am not
  1997. // sure for future versions, however.
  1998. gWindowp->allowLanguageTextInput(this, mPrevalidateFunc == NULL);
  1999. }
  2000. }
  2001. //virtual
  2002. void LLLineEditor::setRect(const LLRect& rect)
  2003. {
  2004. LLUICtrl::setRect(rect);
  2005. if (mBorder)
  2006. {
  2007. LLRect border_rect = mBorder->getRect();
  2008. // Scalable UI somehow made these rectangles off-by-one.
  2009. // I don't know why. JC
  2010. border_rect.setOriginAndSize(border_rect.mLeft, border_rect.mBottom,
  2011. rect.getWidth()-1, rect.getHeight() - 1);
  2012. mBorder->setRect(border_rect);
  2013. }
  2014. }
  2015. void LLLineEditor::setPrevalidate(bool (*func)(const LLWString&))
  2016. {
  2017. mPrevalidateFunc = func;
  2018. updateAllowingLanguageInput();
  2019. }
  2020. // Limits what characters can be used to [1234567890.-] with [-] only valid in
  2021. // the first position. Does NOT ensure that the string is a well-formed number
  2022. // (that's the job of post-validation) for the simple reasons that intermediate
  2023. // states may be invalid even if the final result is valid.
  2024. //
  2025. //static
  2026. bool LLLineEditor::prevalidateFloat(const LLWString& str)
  2027. {
  2028. LLLocale locale(LLLocale::USER_LOCALE);
  2029. bool success = true;
  2030. LLWString trimmed = str;
  2031. LLWStringUtil::trim(trimmed);
  2032. S32 len = trimmed.length();
  2033. if (len > 0)
  2034. {
  2035. // May be a comma or period, depending on the locale
  2036. llwchar decimal_point = (llwchar)LLLocale::getDecimalPoint();
  2037. S32 i = 0;
  2038. // First character can be a negative sign
  2039. if (trimmed[0] == '-')
  2040. {
  2041. ++i;
  2042. }
  2043. for ( ; i < len; ++i)
  2044. {
  2045. if (decimal_point != trimmed[i] &&
  2046. !LLStringOps::isDigit(trimmed[i]))
  2047. {
  2048. success = false;
  2049. break;
  2050. }
  2051. }
  2052. }
  2053. return success;
  2054. }
  2055. //static
  2056. bool LLLineEditor::postvalidateFloat(const std::string& str)
  2057. {
  2058. LLLocale locale(LLLocale::USER_LOCALE);
  2059. bool success = true;
  2060. bool has_decimal = false;
  2061. bool has_digit = false;
  2062. LLWString trimmed = utf8str_to_wstring(str);
  2063. LLWStringUtil::trim(trimmed);
  2064. S32 len = trimmed.length();
  2065. if (0 < len)
  2066. {
  2067. S32 i = 0;
  2068. // First character can be a negative sign
  2069. if ('-' == trimmed[0])
  2070. {
  2071. ++i;
  2072. }
  2073. // May be a comma or period, depending on the locale
  2074. llwchar decimal_point = (llwchar)LLLocale::getDecimalPoint();
  2075. for ( ; i < len; ++i)
  2076. {
  2077. if (decimal_point == trimmed[i])
  2078. {
  2079. if (has_decimal)
  2080. {
  2081. // Cannot have two
  2082. success = false;
  2083. break;
  2084. }
  2085. else
  2086. {
  2087. has_decimal = true;
  2088. }
  2089. }
  2090. else
  2091. if (LLStringOps::isDigit(trimmed[i]))
  2092. {
  2093. has_digit = true;
  2094. }
  2095. else
  2096. {
  2097. success = false;
  2098. break;
  2099. }
  2100. }
  2101. }
  2102. // Gotta have at least one
  2103. success = has_digit;
  2104. return success;
  2105. }
  2106. // Limits what characters can be used to [1234567890-] with [-] only valid in
  2107. // the first position. Does NOT ensure that the string is a well-formed number
  2108. // (that's the job of post-validation) for the simple reasons that intermediate
  2109. // states may be invalid even if the final result is valid.
  2110. //static
  2111. bool LLLineEditor::prevalidateInt(const LLWString& str)
  2112. {
  2113. LLLocale locale(LLLocale::USER_LOCALE);
  2114. bool success = true;
  2115. LLWString trimmed = str;
  2116. LLWStringUtil::trim(trimmed);
  2117. S32 len = trimmed.length();
  2118. if (0 < len)
  2119. {
  2120. S32 i = 0;
  2121. // First character can be a negative sign
  2122. if ('-' == trimmed[0])
  2123. {
  2124. ++i;
  2125. }
  2126. for ( ; i < len; ++i)
  2127. {
  2128. if (!LLStringOps::isDigit(trimmed[i]))
  2129. {
  2130. success = false;
  2131. break;
  2132. }
  2133. }
  2134. }
  2135. return success;
  2136. }
  2137. //static
  2138. bool LLLineEditor::prevalidatePositiveS32(const LLWString& str)
  2139. {
  2140. LLLocale locale(LLLocale::USER_LOCALE);
  2141. LLWString trimmed = str;
  2142. LLWStringUtil::trim(trimmed);
  2143. S32 len = trimmed.length();
  2144. bool success = true;
  2145. if (0 < len)
  2146. {
  2147. if ('-' == trimmed[0] || '0' == trimmed[0])
  2148. {
  2149. success = false;
  2150. }
  2151. S32 i = 0;
  2152. while (success && (i < len))
  2153. {
  2154. if (!LLStringOps::isDigit(trimmed[i++]))
  2155. {
  2156. success = false;
  2157. }
  2158. }
  2159. }
  2160. if (success)
  2161. {
  2162. S32 val = strtol(wstring_to_utf8str(trimmed).c_str(), NULL, 10);
  2163. if (val <= 0)
  2164. {
  2165. success = false;
  2166. }
  2167. }
  2168. return success;
  2169. }
  2170. bool LLLineEditor::prevalidateNonNegativeS32(const LLWString& str)
  2171. {
  2172. LLLocale locale(LLLocale::USER_LOCALE);
  2173. LLWString trimmed = str;
  2174. LLWStringUtil::trim(trimmed);
  2175. S32 len = trimmed.length();
  2176. bool success = true;
  2177. if (0 < len)
  2178. {
  2179. if ('-' == trimmed[0])
  2180. {
  2181. success = false;
  2182. }
  2183. S32 i = 0;
  2184. while (success && (i < len))
  2185. {
  2186. if (!LLStringOps::isDigit(trimmed[i++]))
  2187. {
  2188. success = false;
  2189. }
  2190. }
  2191. }
  2192. if (success)
  2193. {
  2194. S32 val = strtol(wstring_to_utf8str(trimmed).c_str(), NULL, 10);
  2195. if (val < 0)
  2196. {
  2197. success = false;
  2198. }
  2199. }
  2200. return success;
  2201. }
  2202. bool LLLineEditor::prevalidateAlphaNum(const LLWString& str)
  2203. {
  2204. LLLocale locale(LLLocale::USER_LOCALE);
  2205. bool rv = true;
  2206. S32 len = str.length();
  2207. if (len == 0) return rv;
  2208. while (len--)
  2209. {
  2210. if (!LLStringOps::isAlnum((char)str[len]))
  2211. {
  2212. rv = false;
  2213. break;
  2214. }
  2215. }
  2216. return rv;
  2217. }
  2218. //static
  2219. bool LLLineEditor::prevalidateAlphaNumSpace(const LLWString& str)
  2220. {
  2221. LLLocale locale(LLLocale::USER_LOCALE);
  2222. bool rv = true;
  2223. S32 len = str.length();
  2224. if (len == 0) return rv;
  2225. while (len--)
  2226. {
  2227. if (!(LLStringOps::isAlnum((char)str[len]) || ' ' == str[len]))
  2228. {
  2229. rv = false;
  2230. break;
  2231. }
  2232. }
  2233. return rv;
  2234. }
  2235. //static
  2236. bool LLLineEditor::prevalidatePrintableNotPipe(const LLWString& str)
  2237. {
  2238. bool rv = true;
  2239. S32 len = str.length();
  2240. if (len == 0) return rv;
  2241. while (len--)
  2242. {
  2243. if ('|' == str[len])
  2244. {
  2245. rv = false;
  2246. break;
  2247. }
  2248. if (!(' ' == str[len] || LLStringOps::isAlnum((char)str[len]) ||
  2249. LLStringOps::isPunct((char)str[len])))
  2250. {
  2251. rv = false;
  2252. break;
  2253. }
  2254. }
  2255. return rv;
  2256. }
  2257. //static
  2258. bool LLLineEditor::prevalidatePrintableNoSpace(const LLWString& str)
  2259. {
  2260. bool rv = true;
  2261. S32 len = str.length();
  2262. if (len == 0) return rv;
  2263. while (len--)
  2264. {
  2265. if (LLStringOps::isSpace(str[len]))
  2266. {
  2267. rv = false;
  2268. break;
  2269. }
  2270. if (!(LLStringOps::isAlnum((char)str[len]) ||
  2271. LLStringOps::isPunct((char)str[len])))
  2272. {
  2273. rv = false;
  2274. break;
  2275. }
  2276. }
  2277. return rv;
  2278. }
  2279. //static
  2280. bool LLLineEditor::prevalidateASCII(const LLWString& str)
  2281. {
  2282. bool rv = true;
  2283. S32 len = str.length();
  2284. while (len--)
  2285. {
  2286. if (str[len] < 0x20 || str[len] > 0x7f)
  2287. {
  2288. rv = false;
  2289. break;
  2290. }
  2291. }
  2292. return rv;
  2293. }
  2294. void LLLineEditor::onMouseCaptureLost()
  2295. {
  2296. endSelection();
  2297. }
  2298. void LLLineEditor::setSelectAllonFocusReceived(bool b)
  2299. {
  2300. mSelectAllonFocusReceived = b;
  2301. }
  2302. void LLLineEditor::setKeystrokeCallback(void (*keystroke_callback)(LLLineEditor*,
  2303. void*))
  2304. {
  2305. mKeystrokeCallback = keystroke_callback;
  2306. }
  2307. void LLLineEditor::setOnHandleKeyCallback(bool (*callback)(KEY, MASK,
  2308. LLLineEditor*,
  2309. void*),
  2310. void* userdata)
  2311. {
  2312. mOnHandleKeyCallback = callback;
  2313. mOnHandleKeyData = userdata;
  2314. }
  2315. void LLLineEditor::setScrolledCallback(void (*scrolled_callback)(LLLineEditor* caller,
  2316. void* user_data),
  2317. void* userdata)
  2318. {
  2319. mScrolledCallback = scrolled_callback;
  2320. mScrolledCallbackData = userdata;
  2321. }
  2322. //virtual
  2323. const std::string& LLLineEditor::getTag() const
  2324. {
  2325. return LL_LINE_EDITOR_TAG;
  2326. }
  2327. //virtual
  2328. LLXMLNodePtr LLLineEditor::getXML(bool save_children) const
  2329. {
  2330. LLXMLNodePtr node = LLUICtrl::getXML();
  2331. node->setName(LL_LINE_EDITOR_TAG);
  2332. node->createChild("max_length", true)->setIntValue(mMaxLengthBytes);
  2333. node->createChild("font",
  2334. true)->setStringValue(LLFontGL::nameFromFont(mGLFont));
  2335. if (mBorder)
  2336. {
  2337. std::string bevel;
  2338. switch (mBorder->getBevel())
  2339. {
  2340. case LLViewBorder::BEVEL_IN:
  2341. bevel = "in";
  2342. break;
  2343. case LLViewBorder::BEVEL_OUT:
  2344. bevel = "out";
  2345. break;
  2346. case LLViewBorder::BEVEL_BRIGHT:
  2347. bevel = "bright";
  2348. break;
  2349. case LLViewBorder::BEVEL_NONE:
  2350. default:
  2351. bevel = "none";
  2352. }
  2353. node->createChild("bevel_style", true)->setStringValue(bevel);
  2354. std::string style;
  2355. if (mBorder->getStyle() == LLViewBorder::STYLE_TEXTURE)
  2356. {
  2357. style = "texture";
  2358. }
  2359. else
  2360. {
  2361. style = "line";
  2362. }
  2363. node->createChild("border_style", true)->setStringValue(style);
  2364. node->createChild("border_thickness",
  2365. true)->setIntValue(mBorder->getBorderWidth());
  2366. }
  2367. if (!mLabel.empty())
  2368. {
  2369. node->createChild("label", true)->setStringValue(mLabel.getString());
  2370. }
  2371. node->createChild("select_all_on_focus_received",
  2372. true)->setBoolValue(mSelectAllonFocusReceived);
  2373. node->createChild("handle_edit_keys_directly",
  2374. true)->setBoolValue(mHandleEditKeysDirectly);
  2375. addColorXML(node, mCursorColor, "cursor_color", "TextCursorColor");
  2376. addColorXML(node, mFgColor, "text_color", "TextFgColor");
  2377. addColorXML(node, mReadOnlyFgColor, "text_readonly_color",
  2378. "TextFgReadOnlyColor");
  2379. addColorXML(node, mTentativeFgColor, "text_tentative_color",
  2380. "TextFgTentativeColor");
  2381. addColorXML(node, mReadOnlyBgColor, "bg_readonly_color",
  2382. "TextBgReadOnlyColor");
  2383. addColorXML(node, mWriteableBgColor, "bg_writeable_color",
  2384. "TextBgWriteableColor");
  2385. addColorXML(node, mFocusBgColor, "bg_focus_color", "TextBgFocusColor");
  2386. node->createChild("select_on_focus",
  2387. true)->setBoolValue(mSelectAllonFocusReceived);
  2388. return node;
  2389. }
  2390. //static
  2391. LLView* LLLineEditor::fromXML(LLXMLNodePtr node, LLView* parent,
  2392. LLUICtrlFactory*)
  2393. {
  2394. std::string name = LL_LINE_EDITOR_TAG;
  2395. node->getAttributeString("name", name);
  2396. LLRect rect;
  2397. createRect(node, rect, parent, LLRect());
  2398. S32 max_text_length = 128;
  2399. node->getAttributeS32("max_length", max_text_length);
  2400. LLFontGL* font = LLView::selectFont(node);
  2401. std::string text = node->getTextContents().substr(0, max_text_length - 1);
  2402. LLViewBorder::EBevel bevel_style = LLViewBorder::BEVEL_IN;
  2403. LLViewBorder::getBevelFromAttribute(node, bevel_style);
  2404. LLViewBorder::EStyle border_style = LLViewBorder::STYLE_LINE;
  2405. std::string border_string;
  2406. node->getAttributeString("border_style", border_string);
  2407. LLStringUtil::toLower(border_string);
  2408. if (border_string == "texture")
  2409. {
  2410. border_style = LLViewBorder::STYLE_TEXTURE;
  2411. }
  2412. S32 border_thickness = 1;
  2413. node->getAttributeS32("border_thickness", border_thickness);
  2414. LLLineEditor* line_editor = new LLLineEditor(name, rect, text, font,
  2415. max_text_length,
  2416. NULL, NULL, NULL, NULL, NULL,
  2417. bevel_style, border_style,
  2418. border_thickness);
  2419. std::string label;
  2420. if (node->getAttributeString("label", label))
  2421. {
  2422. line_editor->setLabel(label);
  2423. }
  2424. bool select_all_on_focus_received = false;
  2425. if (node->getAttributeBool("select_all_on_focus_received",
  2426. select_all_on_focus_received))
  2427. {
  2428. line_editor->setSelectAllonFocusReceived(select_all_on_focus_received);
  2429. }
  2430. bool handle_edit_keys_directly = false;
  2431. if (node->getAttributeBool("handle_edit_keys_directly",
  2432. handle_edit_keys_directly))
  2433. {
  2434. line_editor->setHandleEditKeysDirectly(handle_edit_keys_directly);
  2435. }
  2436. bool commit_on_focus_lost = true;
  2437. if (node->getAttributeBool("commit_on_focus_lost",
  2438. commit_on_focus_lost))
  2439. {
  2440. line_editor->setCommitOnFocusLost(commit_on_focus_lost);
  2441. }
  2442. bool spell_check = false;
  2443. if (node->getAttributeBool("spell_check", spell_check))
  2444. {
  2445. line_editor->setSpellCheck(spell_check);
  2446. }
  2447. line_editor->setColorParameters(node);
  2448. if (node->hasAttribute("select_on_focus"))
  2449. {
  2450. bool selectall = false;
  2451. node->getAttributeBool("select_on_focus", selectall);
  2452. line_editor->setSelectAllonFocusReceived(selectall);
  2453. }
  2454. std::string prevalidate;
  2455. if (node->getAttributeString("prevalidate", prevalidate))
  2456. {
  2457. LLStringUtil::toLower(prevalidate);
  2458. if (prevalidate == "ascii")
  2459. {
  2460. line_editor->setPrevalidate(prevalidateASCII);
  2461. }
  2462. else if (prevalidate == "float")
  2463. {
  2464. line_editor->setPrevalidate(prevalidateFloat);
  2465. }
  2466. else if (prevalidate == "int")
  2467. {
  2468. line_editor->setPrevalidate(prevalidateInt);
  2469. }
  2470. else if (prevalidate == "positive_s32")
  2471. {
  2472. line_editor->setPrevalidate(prevalidatePositiveS32);
  2473. }
  2474. else if (prevalidate == "non_negative_s32")
  2475. {
  2476. line_editor->setPrevalidate(prevalidateNonNegativeS32);
  2477. }
  2478. else if (prevalidate == "alpha_num")
  2479. {
  2480. line_editor->setPrevalidate(prevalidateAlphaNum);
  2481. }
  2482. else if (prevalidate == "alpha_num_space")
  2483. {
  2484. line_editor->setPrevalidate(prevalidateAlphaNumSpace);
  2485. }
  2486. else if (prevalidate == "printable_not_pipe")
  2487. {
  2488. line_editor->setPrevalidate(prevalidatePrintableNotPipe);
  2489. }
  2490. else if (prevalidate == "printable_no_space")
  2491. {
  2492. line_editor->setPrevalidate(prevalidatePrintableNoSpace);
  2493. }
  2494. }
  2495. line_editor->initFromXML(node, parent);
  2496. return line_editor;
  2497. }
  2498. //static
  2499. void LLLineEditor::cleanupLineEditor()
  2500. {
  2501. sImage = NULL;
  2502. }
  2503. //static
  2504. LLUIImagePtr LLLineEditor::parseImage(std::string name, LLXMLNodePtr from,
  2505. LLUIImagePtr def)
  2506. {
  2507. std::string xml_name;
  2508. if (from->hasAttribute(name.c_str()))
  2509. {
  2510. from->getAttributeString(name.c_str(), xml_name);
  2511. }
  2512. if (xml_name.empty())
  2513. {
  2514. return def;
  2515. }
  2516. LLUIImagePtr image = LLUI::getUIImage(xml_name);
  2517. return image.isNull() ? def : image;
  2518. }
  2519. void LLLineEditor::setColorParameters(LLXMLNodePtr node)
  2520. {
  2521. // overrides default image if supplied.
  2522. mImage = parseImage("image", node, mImage);
  2523. LLColor4 color;
  2524. if (LLUICtrlFactory::getAttributeColor(node,"cursor_color", color))
  2525. {
  2526. setCursorColor(color);
  2527. }
  2528. if (node->hasAttribute("text_color"))
  2529. {
  2530. LLUICtrlFactory::getAttributeColor(node,"text_color", color);
  2531. setFgColor(color);
  2532. }
  2533. if (node->hasAttribute("text_readonly_color"))
  2534. {
  2535. LLUICtrlFactory::getAttributeColor(node,"text_readonly_color", color);
  2536. setReadOnlyFgColor(color);
  2537. }
  2538. if (LLUICtrlFactory::getAttributeColor(node,"text_tentative_color", color))
  2539. {
  2540. setTentativeFgColor(color);
  2541. }
  2542. if (node->hasAttribute("bg_readonly_color"))
  2543. {
  2544. LLUICtrlFactory::getAttributeColor(node,"bg_readonly_color", color);
  2545. setReadOnlyBgColor(color);
  2546. }
  2547. if (node->hasAttribute("bg_writeable_color"))
  2548. {
  2549. LLUICtrlFactory::getAttributeColor(node,"bg_writeable_color", color);
  2550. setWriteableBgColor(color);
  2551. }
  2552. }
  2553. void LLLineEditor::updateAllowingLanguageInput()
  2554. {
  2555. // Allow Language Text Input only when this LineEditor has no prevalidate
  2556. // function attached (as long as other criteria common to LLTextEditor).
  2557. // This criterion works fine on 1.15.0.2, since all prevalidate func
  2558. // reject any non-ASCII characters. I'm not sure on future versions,
  2559. // however...
  2560. if (hasFocus() && !mReadOnly && !mDrawAsterixes && !mPrevalidateFunc)
  2561. {
  2562. gWindowp->allowLanguageTextInput(this, true);
  2563. }
  2564. else
  2565. {
  2566. gWindowp->allowLanguageTextInput(this, false);
  2567. }
  2568. }
  2569. bool LLLineEditor::hasPreeditString() const
  2570. {
  2571. return mPreeditPositions.size() > 1;
  2572. }
  2573. void LLLineEditor::resetPreedit()
  2574. {
  2575. if (hasPreeditString())
  2576. {
  2577. if (hasSelection())
  2578. {
  2579. llwarns << "Preedit and selection ! Deselecting." << llendl;
  2580. deselect();
  2581. }
  2582. const S32 preedit_pos = mPreeditPositions.front();
  2583. mText.erase(preedit_pos, mPreeditPositions.back() - preedit_pos);
  2584. mText.insert(preedit_pos, mPreeditOverwrittenWString);
  2585. setCursor(preedit_pos);
  2586. mPreeditWString.clear();
  2587. mPreeditOverwrittenWString.clear();
  2588. mPreeditPositions.clear();
  2589. // Do not reset keystroke timer nor invoke keystroke callback, because
  2590. // a call to updatePreedit should be follow soon in normal course of
  2591. // operation, and timer and callback will be maintained there. Doing so
  2592. // here made an odd sound (VWR-3410).
  2593. }
  2594. }
  2595. void LLLineEditor::updatePreedit(const LLWString& preedit_string,
  2596. const segment_lengths_t& preedit_segment_lengths,
  2597. const standouts_t& preedit_standouts,
  2598. S32 caret_position)
  2599. {
  2600. // Just in case.
  2601. if (mReadOnly)
  2602. {
  2603. return;
  2604. }
  2605. // Note that call to updatePreedit is always preceeded by resetPreedit,
  2606. // so we have no existing selection/preedit.
  2607. S32 insert_preedit_at = mCursorPos;
  2608. mPreeditWString = preedit_string;
  2609. mPreeditPositions.resize(preedit_segment_lengths.size() + 1);
  2610. S32 position = insert_preedit_at;
  2611. for (segment_lengths_t::size_type i = 0,
  2612. size = preedit_segment_lengths.size();
  2613. i < size; ++i)
  2614. {
  2615. mPreeditPositions[i] = position;
  2616. position += preedit_segment_lengths[i];
  2617. }
  2618. mPreeditPositions.back() = position;
  2619. if (gKeyboardp && gKeyboardp->getInsertMode() == LL_KIM_OVERWRITE)
  2620. {
  2621. mPreeditOverwrittenWString.assign(LLWString(mText, insert_preedit_at,
  2622. mPreeditWString.length()));
  2623. mText.erase(insert_preedit_at, mPreeditWString.length());
  2624. }
  2625. else
  2626. {
  2627. mPreeditOverwrittenWString.clear();
  2628. }
  2629. mText.insert(insert_preedit_at, mPreeditWString);
  2630. mPreeditStandouts = preedit_standouts;
  2631. setCursor(position);
  2632. setCursor(mPreeditPositions.front() + caret_position);
  2633. // Update of the preedit should be caused by some key strokes.
  2634. mKeystrokeTimer.reset();
  2635. if (mKeystrokeCallback)
  2636. {
  2637. mKeystrokeCallback(this, mCallbackUserData);
  2638. }
  2639. }
  2640. bool LLLineEditor::getPreeditLocation(S32 query_offset, LLCoordGL* coord,
  2641. LLRect* bounds, LLRect* control) const
  2642. {
  2643. if (control)
  2644. {
  2645. LLRect control_rect_screen;
  2646. localRectToScreen(getRect(), &control_rect_screen);
  2647. LLUI::screenRectToGL(control_rect_screen, control);
  2648. }
  2649. S32 preedit_left_column, preedit_right_column;
  2650. if (hasPreeditString())
  2651. {
  2652. preedit_left_column = mPreeditPositions.front();
  2653. preedit_right_column = mPreeditPositions.back();
  2654. }
  2655. else
  2656. {
  2657. preedit_left_column = preedit_right_column = mCursorPos;
  2658. }
  2659. if (preedit_right_column < mScrollHPos)
  2660. {
  2661. // This should not occur...
  2662. return false;
  2663. }
  2664. const S32 query = query_offset >= 0 ? preedit_left_column + query_offset
  2665. : mCursorPos;
  2666. if (query < mScrollHPos || query < preedit_left_column ||
  2667. query > preedit_right_column)
  2668. {
  2669. return false;
  2670. }
  2671. if (coord)
  2672. {
  2673. S32 query_local = findPixelNearestPos(query - mCursorPos);
  2674. S32 query_screen_x, query_screen_y;
  2675. localPointToScreen(query_local, getRect().getHeight() / 2,
  2676. &query_screen_x, &query_screen_y);
  2677. LLUI::screenPointToGL(query_screen_x, query_screen_y, &coord->mX,
  2678. &coord->mY);
  2679. }
  2680. if (bounds)
  2681. {
  2682. S32 preedit_left_local = findPixelNearestPos(llmax(preedit_left_column,
  2683. mScrollHPos) -
  2684. mCursorPos);
  2685. S32 preedit_right_local = llmin(findPixelNearestPos(preedit_right_column -
  2686. mCursorPos),
  2687. getRect().getWidth() - mBorderThickness);
  2688. if (preedit_left_local > preedit_right_local)
  2689. {
  2690. // Is this condition possible ?
  2691. preedit_right_local = preedit_left_local;
  2692. }
  2693. LLRect preedit_rect_local(preedit_left_local, getRect().getHeight(),
  2694. preedit_right_local, 0);
  2695. LLRect preedit_rect_screen;
  2696. localRectToScreen(preedit_rect_local, &preedit_rect_screen);
  2697. LLUI::screenRectToGL(preedit_rect_screen, bounds);
  2698. }
  2699. return true;
  2700. }
  2701. void LLLineEditor::getPreeditRange(S32* position, S32* length) const
  2702. {
  2703. if (hasPreeditString())
  2704. {
  2705. *position = mPreeditPositions.front();
  2706. *length = mPreeditPositions.back() - mPreeditPositions.front();
  2707. }
  2708. else
  2709. {
  2710. *position = mCursorPos;
  2711. *length = 0;
  2712. }
  2713. }
  2714. void LLLineEditor::getSelectionRange(S32* position, S32* length) const
  2715. {
  2716. if (hasSelection())
  2717. {
  2718. *position = llmin(mSelectionStart, mSelectionEnd);
  2719. *length = abs(mSelectionStart - mSelectionEnd);
  2720. }
  2721. else
  2722. {
  2723. *position = mCursorPos;
  2724. *length = 0;
  2725. }
  2726. }
  2727. void LLLineEditor::markAsPreedit(S32 position, S32 length)
  2728. {
  2729. deselect();
  2730. setCursor(position);
  2731. if (hasPreeditString())
  2732. {
  2733. llwarns << "markAsPreedit invoked when hasPreeditString is true."
  2734. << llendl;
  2735. }
  2736. mPreeditWString.assign(LLWString(mText.getWString(), position, length));
  2737. if (length > 0)
  2738. {
  2739. mPreeditPositions.resize(2);
  2740. mPreeditPositions[0] = position;
  2741. mPreeditPositions[1] = position + length;
  2742. mPreeditStandouts.resize(1);
  2743. mPreeditStandouts[0] = false;
  2744. }
  2745. else
  2746. {
  2747. mPreeditPositions.clear();
  2748. mPreeditStandouts.clear();
  2749. }
  2750. if (gKeyboardp && gKeyboardp->getInsertMode() == LL_KIM_OVERWRITE)
  2751. {
  2752. mPreeditOverwrittenWString = mPreeditWString;
  2753. }
  2754. else
  2755. {
  2756. mPreeditOverwrittenWString.clear();
  2757. }
  2758. }
  2759. S32 LLLineEditor::getPreeditFontSize() const
  2760. {
  2761. return ll_roundp(mGLFont->getLineHeight() * LLUI::sGLScaleFactor.mV[VY]);
  2762. }
  2763. LLWString LLLineEditor::getConvertedText() const
  2764. {
  2765. LLWString text = getWText();
  2766. LLWStringUtil::trim(text);
  2767. if (!mReplaceNewlinesWithSpaces)
  2768. {
  2769. // Convert paragraph symbols back into newlines.
  2770. LLWStringUtil::replaceChar(text, 182, '\n');
  2771. }
  2772. return text;
  2773. }
  2774. ///////////////////////////////////////////////////////////////////////////////
  2775. // LLSearchEditor class
  2776. ///////////////////////////////////////////////////////////////////////////////
  2777. static const std::string LL_SEARCH_EDITOR_TAG = "search_editor";
  2778. static LLRegisterWidget<LLSearchEditor> r07(LL_SEARCH_EDITOR_TAG);
  2779. LLSearchEditor::LLSearchEditor(const std::string& name, const LLRect& rect,
  2780. S32 max_length)
  2781. : LLUICtrl(name, rect, true, NULL, NULL),
  2782. mSearchCallback(NULL),
  2783. mLineCommitCallback(NULL),
  2784. mCommitCallbackUserData(NULL)
  2785. {
  2786. LLRect line_edit_rect(0, getRect().getHeight(), getRect().getWidth(), 0);
  2787. mSearchLineEditor = new LLLineEditor(name + "_line_editor", line_edit_rect,
  2788. LLStringUtil::null, NULL, max_length,
  2789. NULL, NULL, NULL, this);
  2790. mSearchLineEditor->setFollowsAll();
  2791. mSearchLineEditor->setSelectAllonFocusReceived(true);
  2792. addChild(mSearchLineEditor);
  2793. // Button is square, and as tall as search editor
  2794. S32 btn_width = rect.getHeight();
  2795. LLRect clear_btn_rect(rect.getWidth() - btn_width, rect.getHeight(),
  2796. rect.getWidth(), 0);
  2797. mClearSearchButton = new LLButton(name + "_clear_button", clear_btn_rect,
  2798. "icn_clear_lineeditor.tga",
  2799. "UIImgBtnCloseInactiveUUID",
  2800. NULL, onClearSearch, this,
  2801. NULL, LLStringUtil::null);
  2802. mClearSearchButton->setFollowsRight();
  2803. mClearSearchButton->setFollowsTop();
  2804. mClearSearchButton->setImageColor(LLUI::sTextFgTentativeColor);
  2805. mClearSearchButton->setTabStop(false);
  2806. mClearSearchButton->setToolTip(LLTrans::getString("TooltipClearSearch"));
  2807. mSearchLineEditor->addChild(mClearSearchButton);
  2808. mSearchLineEditor->setTextPadding(0, btn_width);
  2809. }
  2810. //virtual
  2811. void LLSearchEditor::clear()
  2812. {
  2813. if (mSearchLineEditor)
  2814. {
  2815. mSearchLineEditor->clear();
  2816. }
  2817. }
  2818. //virtual
  2819. void LLSearchEditor::draw()
  2820. {
  2821. mClearSearchButton->setVisible(!mSearchLineEditor->getWText().empty());
  2822. LLUICtrl::draw();
  2823. }
  2824. void LLSearchEditor::setCommitCallback(void (*cb)(LLUICtrl*, void*))
  2825. {
  2826. mLineCommitCallback = cb;
  2827. if (mLineCommitCallback)
  2828. {
  2829. mSearchLineEditor->setCommitCallback(onSearchEditCommit);
  2830. }
  2831. else
  2832. {
  2833. mSearchLineEditor->setCommitCallback(NULL);
  2834. }
  2835. }
  2836. void LLSearchEditor::setSearchCallback(void (*cb)(const std::string&, void*),
  2837. void* userdata)
  2838. {
  2839. mSearchCallback = cb;
  2840. if (mSearchCallback)
  2841. {
  2842. mSearchLineEditor->setKeystrokeCallback(onSearchEditKeystroke);
  2843. }
  2844. else
  2845. {
  2846. mSearchLineEditor->setKeystrokeCallback(NULL);
  2847. }
  2848. mCallbackUserData = userdata;
  2849. }
  2850. //static
  2851. void LLSearchEditor::onSearchEditCommit(LLUICtrl* ctrl, void* data)
  2852. {
  2853. LLSearchEditor* self = (LLSearchEditor*)data;
  2854. if (self && self->mLineCommitCallback)
  2855. {
  2856. self->mLineCommitCallback(ctrl, self->mCommitCallbackUserData);
  2857. }
  2858. }
  2859. //static
  2860. void LLSearchEditor::onSearchEditKeystroke(LLLineEditor* caller, void* data)
  2861. {
  2862. LLSearchEditor* self = (LLSearchEditor*)data;
  2863. if (caller && self && self->mSearchCallback)
  2864. {
  2865. self->mSearchCallback(caller->getText(), self->mCallbackUserData);
  2866. }
  2867. }
  2868. //static
  2869. void LLSearchEditor::onClearSearch(void* data)
  2870. {
  2871. LLSearchEditor* self = (LLSearchEditor*)data;
  2872. if (!self) return;
  2873. self->setText(LLStringUtil::null);
  2874. if (self->mSearchCallback)
  2875. {
  2876. self->mSearchCallback(LLStringUtil::null, self->mCallbackUserData);
  2877. }
  2878. }
  2879. //virtual
  2880. const std::string& LLSearchEditor::getTag() const
  2881. {
  2882. return LL_SEARCH_EDITOR_TAG;
  2883. }
  2884. //virtual
  2885. LLXMLNodePtr LLSearchEditor::getXML(bool save_children) const
  2886. {
  2887. LLXMLNodePtr nodep = LLUICtrl::getXML();
  2888. nodep->setName(LL_SEARCH_EDITOR_TAG);
  2889. return nodep;
  2890. }
  2891. //static
  2892. LLView* LLSearchEditor::fromXML(LLXMLNodePtr nodep, LLView* parentp,
  2893. LLUICtrlFactory*)
  2894. {
  2895. std::string name = LL_SEARCH_EDITOR_TAG;
  2896. nodep->getAttributeString("name", name);
  2897. LLRect rect;
  2898. createRect(nodep, rect, parentp, LLRect());
  2899. S32 max_text_length = 128;
  2900. nodep->getAttributeS32("max_length", max_text_length);
  2901. std::string text = nodep->getValue().substr(0, max_text_length - 1);
  2902. LLSearchEditor* self = new LLSearchEditor(name, rect, max_text_length);
  2903. std::string label;
  2904. if (nodep->getAttributeString("label", label))
  2905. {
  2906. self->mSearchLineEditor->setLabel(label);
  2907. }
  2908. self->setText(text);
  2909. self->initFromXML(nodep, parentp);
  2910. return self;
  2911. }