lltexteditor.cpp 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580
  1. /**
  2. * @file lltexteditor.cpp
  3. * @brief LLTextEditor 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 a multi-line ASCII document.
  33. #include "linden_common.h"
  34. #include <queue>
  35. #include "lltexteditor.h"
  36. #include "llclipboard.h"
  37. #include "llcontrol.h"
  38. #include "lldir.h"
  39. #include "llfasttimer.h"
  40. #include "llfontfreetype.h"
  41. #include "llimagegl.h"
  42. #include "llkeyboard.h"
  43. #include "llmenugl.h"
  44. #include "llrender.h"
  45. #include "llscrollbar.h"
  46. #include "llstl.h"
  47. #include "llspellcheck.h"
  48. #include "lltimer.h"
  49. #include "lluictrlfactory.h"
  50. #include "llundo.h"
  51. #include "llviewborder.h"
  52. #include "llwindow.h"
  53. // gcc 12.4+ sees array bound issues where there are none... HB
  54. #if defined(GCC_VERSION) && GCC_VERSION >= 120400
  55. # pragma GCC diagnostic ignored "-Warray-bounds"
  56. # pragma GCC diagnostic ignored "-Wstringop-overflow"
  57. #endif
  58. static const std::string LL_SIMPLE_TEXT_EDITOR_TAG = "simple_text_editor";
  59. static LLRegisterWidget<LLTextEditor> r27(LL_SIMPLE_TEXT_EDITOR_TAG);
  60. //
  61. // Constants
  62. //
  63. constexpr S32 UI_TEXTEDITOR_BORDER = 1;
  64. constexpr S32 UI_TEXTEDITOR_H_PAD = 4;
  65. constexpr S32 UI_TEXTEDITOR_V_PAD_TOP = 4;
  66. constexpr S32 UI_TEXTEDITOR_LINE_NUMBER_MARGIN = 32;
  67. constexpr S32 UI_TEXTEDITOR_LINE_NUMBER_DIGITS = 4;
  68. constexpr F32 CURSOR_FLASH_DELAY = 1.f; // In seconds
  69. constexpr S32 CURSOR_THICKNESS = 2;
  70. constexpr S32 SPACES_PER_TAB = 4;
  71. constexpr F32 PREEDIT_MARKER_BRIGHTNESS = 0.4f;
  72. constexpr S32 PREEDIT_MARKER_GAP = 1;
  73. constexpr S32 PREEDIT_MARKER_POSITION = 2;
  74. constexpr S32 PREEDIT_MARKER_THICKNESS = 1;
  75. constexpr F32 PREEDIT_STANDOUT_BRIGHTNESS = 0.6f;
  76. constexpr S32 PREEDIT_STANDOUT_GAP = 1;
  77. constexpr S32 PREEDIT_STANDOUT_POSITION = 2;
  78. constexpr S32 PREEDIT_STANDOUT_THICKNESS = 2;
  79. LLColor4 LLTextEditor::sLinkColor = LLColor4::blue;
  80. void (*LLTextEditor::sURLcallback)(const std::string&) = NULL;
  81. bool (*LLTextEditor::sSecondlifeURLcallback)(const std::string&) = NULL;
  82. bool (*LLTextEditor::sSecondlifeURLcallbackRightClick)(const std::string&) = NULL;
  83. ///////////////////////////////////////////////////////////////////
  84. class LLTextEditor::LLTextCmdInsert : public LLTextEditor::LLTextCmd
  85. {
  86. public:
  87. LLTextCmdInsert(S32 pos, bool group_with_next, const LLWString& ws)
  88. : LLTextCmd(pos, group_with_next),
  89. mWString(ws)
  90. {
  91. }
  92. virtual ~LLTextCmdInsert()
  93. {
  94. }
  95. virtual bool execute(LLTextEditor* editor, S32* delta)
  96. {
  97. *delta = insert(editor, getPosition(), mWString);
  98. LLWStringUtil::truncate(mWString, *delta);
  99. //mWString = wstring_truncate(mWString, *delta);
  100. return (*delta != 0);
  101. }
  102. virtual S32 undo(LLTextEditor* editor)
  103. {
  104. remove(editor, getPosition(), mWString.length());
  105. return getPosition();
  106. }
  107. virtual S32 redo(LLTextEditor* editor)
  108. {
  109. insert(editor, getPosition(), mWString);
  110. return getPosition() + mWString.length();
  111. }
  112. private:
  113. LLWString mWString;
  114. };
  115. ///////////////////////////////////////////////////////////////////
  116. class LLTextEditor::LLTextCmdAddChar : public LLTextEditor::LLTextCmd
  117. {
  118. public:
  119. LLTextCmdAddChar(S32 pos, bool group_with_next, llwchar wc)
  120. : LLTextCmd(pos, group_with_next),
  121. mWString(1, wc),
  122. mBlockExtensions(false)
  123. {
  124. }
  125. virtual void blockExtensions()
  126. {
  127. mBlockExtensions = true;
  128. }
  129. virtual bool canExtend(S32 pos) const
  130. {
  131. return !mBlockExtensions &&
  132. pos == getPosition() + (S32)mWString.length();
  133. }
  134. virtual bool execute(LLTextEditor* editor, S32* delta)
  135. {
  136. *delta = insert(editor, getPosition(), mWString);
  137. LLWStringUtil::truncate(mWString, *delta);
  138. //mWString = wstring_truncate(mWString, *delta);
  139. return *delta != 0;
  140. }
  141. virtual bool extendAndExecute(LLTextEditor* editor, S32 pos, llwchar wc,
  142. S32* delta)
  143. {
  144. LLWString ws;
  145. ws += wc;
  146. *delta = insert(editor, pos, ws);
  147. if (*delta > 0)
  148. {
  149. mWString += wc;
  150. }
  151. return *delta != 0;
  152. }
  153. virtual S32 undo(LLTextEditor* editor)
  154. {
  155. remove(editor, getPosition(), mWString.length());
  156. return getPosition();
  157. }
  158. virtual S32 redo(LLTextEditor* editor)
  159. {
  160. insert(editor, getPosition(), mWString);
  161. return getPosition() + mWString.length();
  162. }
  163. private:
  164. LLWString mWString;
  165. bool mBlockExtensions;
  166. };
  167. ///////////////////////////////////////////////////////////////////
  168. class LLTextEditor::LLTextCmdOverwriteChar : public LLTextEditor::LLTextCmd
  169. {
  170. public:
  171. LLTextCmdOverwriteChar(S32 pos, bool group_with_next, llwchar wc)
  172. : LLTextCmd(pos, group_with_next),
  173. mChar(wc),
  174. mOldChar(0)
  175. {
  176. }
  177. virtual bool execute(LLTextEditor* editor, S32* delta)
  178. {
  179. mOldChar = editor->getWChar(getPosition());
  180. overwrite(editor, getPosition(), mChar);
  181. *delta = 0;
  182. return true;
  183. }
  184. virtual S32 undo(LLTextEditor* editor)
  185. {
  186. overwrite(editor, getPosition(), mOldChar);
  187. return getPosition();
  188. }
  189. virtual S32 redo(LLTextEditor* editor)
  190. {
  191. overwrite(editor, getPosition(), mChar);
  192. return getPosition() + 1;
  193. }
  194. private:
  195. llwchar mChar;
  196. llwchar mOldChar;
  197. };
  198. ///////////////////////////////////////////////////////////////////
  199. class LLTextEditor::LLTextCmdRemove : public LLTextEditor::LLTextCmd
  200. {
  201. public:
  202. LLTextCmdRemove(S32 pos, bool group_with_next, S32 len)
  203. : LLTextCmd(pos, group_with_next),
  204. mLen(len)
  205. {
  206. }
  207. virtual bool execute(LLTextEditor* editor, S32* delta)
  208. {
  209. mWString = editor->getWSubString(getPosition(), mLen);
  210. *delta = remove(editor, getPosition(), mLen);
  211. return *delta != 0;
  212. }
  213. virtual S32 undo(LLTextEditor* editor)
  214. {
  215. insert(editor, getPosition(), mWString);
  216. return getPosition() + mWString.length();
  217. }
  218. virtual S32 redo(LLTextEditor* editor)
  219. {
  220. remove(editor, getPosition(), mLen);
  221. return getPosition();
  222. }
  223. private:
  224. LLWString mWString;
  225. S32 mLen;
  226. };
  227. ///////////////////////////////////////////////////////////////////
  228. LLTextEditor::LLTextEditor(const std::string& name, const LLRect& rect,
  229. S32 max_length, // In bytes
  230. const std::string& default_text, LLFontGL* font,
  231. bool allow_embedded_items)
  232. : LLEditMenuHandler(HAS_CONTEXT_MENU | HAS_UNDO_REDO | HAS_CUSTOM),
  233. LLUICtrl(name, rect, true, NULL, NULL, FOLLOWS_TOP | FOLLOWS_LEFT),
  234. mTextIsUpToDate(true),
  235. mMaxTextByteLength(max_length),
  236. mBaseDocIsPristine(true),
  237. mPristineCmd(NULL),
  238. mLastCmd(NULL),
  239. mCursorPos(0),
  240. mIsSelecting(false),
  241. mSelectionStart(0),
  242. mSelectionEnd(0),
  243. mScrolledToBottom(true),
  244. mOnScrollEndCallback(NULL),
  245. mOnScrollEndData(NULL),
  246. mKeystrokeCallback(NULL),
  247. mKeystrokeData(NULL),
  248. mOnHandleKeyCallback(NULL),
  249. mOnHandleKeyData(NULL),
  250. mCursorColor(LLUI::sTextCursorColor),
  251. mFgColor(LLUI::sTextFgColor),
  252. mDefaultColor(LLUI::sTextDefaultColor),
  253. mReadOnlyFgColor(LLUI::sTextFgReadOnlyColor),
  254. mWriteableBgColor(LLUI::sTextBgWriteableColor),
  255. mReadOnlyBgColor(LLUI::sTextBgReadOnlyColor),
  256. mFocusBgColor(LLUI::sTextBgFocusColor),
  257. mLinkColor(sLinkColor),
  258. mReadOnly(false),
  259. mWordWrap(false),
  260. mShowLineNumbers(false),
  261. mTabsToNextField(true),
  262. mCommitOnFocusLost(false),
  263. mHideScrollbarForShortDocs(false),
  264. mTrackBottom(false),
  265. mAllowEmbeddedItems(allow_embedded_items),
  266. mPreserveSegments(false),
  267. mHandleEditKeysDirectly(false),
  268. mMouseDownX(0),
  269. mMouseDownY(0),
  270. mLastSelectionX(-1),
  271. mLastSelectionY(-1),
  272. mReflowNeeded(false),
  273. mScrollNeeded(false),
  274. mParseHTML(false),
  275. mSpellCheck(true)
  276. {
  277. // Reset desired x cursor position
  278. mDesiredXPixel = -1;
  279. if (font)
  280. {
  281. mGLFont = font;
  282. }
  283. else
  284. {
  285. mGLFont = LLFontGL::getFontSansSerif();
  286. }
  287. updateTextRect();
  288. S32 line_height = ll_roundp(mGLFont->getLineHeight());
  289. S32 page_size = mTextRect.getHeight() / line_height;
  290. // Init the scrollbar
  291. LLRect scroll_rect;
  292. scroll_rect.setOriginAndSize(getRect().getWidth() - SCROLLBAR_SIZE, 1,
  293. SCROLLBAR_SIZE, getRect().getHeight() - 1);
  294. S32 lines_in_doc = getLineCount();
  295. mScrollbar = new LLScrollbar("Scrollbar", scroll_rect,
  296. LLScrollbar::VERTICAL, lines_in_doc, 0,
  297. page_size, NULL, this);
  298. mScrollbar->setFollowsRight();
  299. mScrollbar->setFollowsTop();
  300. mScrollbar->setFollowsBottom();
  301. mScrollbar->setEnabled(true);
  302. mScrollbar->setVisible(true);
  303. mScrollbar->setOnScrollEndCallback(mOnScrollEndCallback, mOnScrollEndData);
  304. addChild(mScrollbar);
  305. mBorder = new LLViewBorder("text ed border",
  306. LLRect(0, getRect().getHeight(),
  307. getRect().getWidth(), 0),
  308. LLViewBorder::BEVEL_IN,
  309. LLViewBorder::STYLE_LINE, UI_TEXTEDITOR_BORDER);
  310. addChild(mBorder);
  311. appendText(default_text, false, false);
  312. resetDirty(); // Update saved text state
  313. mHTML.clear();
  314. mShowMisspelled = LLSpellCheck::getInstance()->getShowMisspelled();
  315. }
  316. LLTextEditor::~LLTextEditor()
  317. {
  318. gFocusMgr.releaseFocusIfNeeded(this); // calls onCommit()
  319. // Scrollbar is deleted by LLView
  320. mHoverSegment = NULL;
  321. std::for_each(mSegments.begin(), mSegments.end(), DeletePointer());
  322. mSegments.clear();
  323. std::for_each(mUndoStack.begin(), mUndoStack.end(), DeletePointer());
  324. mUndoStack.clear();
  325. }
  326. void LLTextEditor::spellReplace(SpellMenuBind* data)
  327. {
  328. if (data)
  329. {
  330. S32 length = data->mWordPositionEnd - data->mWordPositionStart;
  331. remove(data->mWordPositionStart, length, true);
  332. LLWString clean_string = utf8str_to_wstring(data->mWord);
  333. insert(data->mWordPositionStart, clean_string, false);
  334. mCursorPos += clean_string.length() - length;
  335. needsReflow();
  336. }
  337. }
  338. void LLTextEditor::spellCorrect(void* data)
  339. {
  340. SpellMenuBind* menu_bind = (SpellMenuBind*)data;
  341. LLTextEditor* text = menu_bind->mOrigin;
  342. if (menu_bind && text)
  343. {
  344. LL_DEBUGS("SpellCheck") << menu_bind->mMenuItem->getName()
  345. << " : " << text->getName()
  346. << " : " << menu_bind->mWord << LL_ENDL;
  347. text->spellReplace(menu_bind);
  348. // Make it update:
  349. text->mKeystrokeTimer.reset();
  350. text->mPrevSpelledText.erase();
  351. }
  352. }
  353. void LLTextEditor::spellShow(void* data)
  354. {
  355. SpellMenuBind* menu_bind = (SpellMenuBind*)data;
  356. LLTextEditor* text = menu_bind->mOrigin;
  357. if (menu_bind && text)
  358. {
  359. text->mShowMisspelled = (menu_bind->mWord == "Show Misspellings");
  360. // Make it update:
  361. text->mKeystrokeTimer.reset();
  362. text->mPrevSpelledText.erase();
  363. }
  364. }
  365. void LLTextEditor::spellAdd(void* data)
  366. {
  367. SpellMenuBind* menu_bind = (SpellMenuBind*)data;
  368. LLTextEditor* text = menu_bind->mOrigin;
  369. if (menu_bind && text)
  370. {
  371. LLSpellCheck::getInstance()->addToCustomDictionary(menu_bind->mWord);
  372. // Make it update:
  373. text->mKeystrokeTimer.reset();
  374. text->mPrevSpelledText.erase();
  375. }
  376. }
  377. void LLTextEditor::spellIgnore(void* data)
  378. {
  379. SpellMenuBind* menu_bind = (SpellMenuBind*)data;
  380. LLTextEditor* text = menu_bind->mOrigin;
  381. if (menu_bind && text)
  382. {
  383. LLSpellCheck::getInstance()->addToIgnoreList(menu_bind->mWord);
  384. // Make it update:
  385. text->mKeystrokeTimer.reset();
  386. text->mPrevSpelledText.erase();
  387. }
  388. }
  389. std::vector<S32> LLTextEditor::getMisspelledWordsPositions()
  390. {
  391. std::vector<S32> bad_words_pos;
  392. const LLWString& text = mWText;
  393. std::string selected_word;
  394. S32 word_start = 0;
  395. S32 word_end = mSpellCheckStart;
  396. S32 true_end;
  397. while (word_end < mSpellCheckEnd)
  398. {
  399. if (LLWStringUtil::isPartOfLexicalWord(text[word_end]))
  400. {
  401. // Select the word under the cursor
  402. while (word_end > 0 &&
  403. LLWStringUtil::isPartOfLexicalWord(text[word_end - 1]))
  404. {
  405. --word_end;
  406. }
  407. if (text[word_end] == L'\'')
  408. {
  409. // Do not count "'" at the start of a word
  410. ++word_end;
  411. }
  412. word_start = word_end;
  413. while (word_end < (S32)text.length() &&
  414. LLWStringUtil::isPartOfLexicalWord(text[word_end]))
  415. {
  416. ++word_end;
  417. }
  418. if (text[word_end - 1] == L'\'')
  419. {
  420. // Do not count "'" at the end of a word
  421. true_end = word_end - 1;
  422. }
  423. else
  424. {
  425. true_end = word_end;
  426. }
  427. if (true_end > word_start + 2) // Do not bother for 2 or less characters words
  428. {
  429. std::string part(text.begin(), text.end());
  430. selected_word = part.substr(word_start, true_end - word_start);
  431. if (!LLSpellCheck::getInstance()->checkSpelling(selected_word))
  432. {
  433. // Misspelled word here
  434. bad_words_pos.emplace_back(word_start);
  435. bad_words_pos.push_back(true_end);
  436. }
  437. }
  438. }
  439. ++word_end;
  440. }
  441. return bad_words_pos;
  442. }
  443. void LLTextEditor::setTrackColor(const LLColor4& color)
  444. {
  445. mScrollbar->setTrackColor(color);
  446. }
  447. void LLTextEditor::setThumbColor(const LLColor4& color)
  448. {
  449. mScrollbar->setThumbColor(color);
  450. }
  451. void LLTextEditor::setHighlightColor(const LLColor4& color)
  452. {
  453. mScrollbar->setHighlightColor(color);
  454. }
  455. void LLTextEditor::setShadowColor(const LLColor4& color)
  456. {
  457. mScrollbar->setShadowColor(color);
  458. }
  459. void LLTextEditor::updateLineStartList(S32 startpos)
  460. {
  461. updateSegments();
  462. bindEmbeddedChars(mGLFont);
  463. S32 seg_num = mSegments.size();
  464. S32 seg_idx = 0;
  465. S32 seg_offset = 0;
  466. if (!mLineStartList.empty())
  467. {
  468. getSegmentAndOffset(startpos, &seg_idx, &seg_offset);
  469. line_info t(seg_idx, seg_offset);
  470. line_list_t::iterator iter = std::upper_bound(mLineStartList.begin(),
  471. mLineStartList.end(), t,
  472. line_info_compare());
  473. if (iter != mLineStartList.begin()) --iter;
  474. seg_idx = iter->mSegment;
  475. seg_offset = iter->mOffset;
  476. mLineStartList.erase(iter, mLineStartList.end());
  477. }
  478. while (seg_idx < seg_num)
  479. {
  480. mLineStartList.emplace_back(seg_idx, seg_offset);
  481. bool line_ended = false;
  482. S32 start_x = mShowLineNumbers ? UI_TEXTEDITOR_LINE_NUMBER_MARGIN : 0;
  483. S32 line_width = start_x;
  484. while (!line_ended && seg_idx < seg_num)
  485. {
  486. LLTextSegment* segment = mSegments[seg_idx];
  487. S32 start_idx = segment->getStart() + seg_offset;
  488. S32 end_idx = start_idx;
  489. while (end_idx < segment->getEnd() && mWText[end_idx] != '\n')
  490. {
  491. ++end_idx;
  492. }
  493. if (start_idx == end_idx)
  494. {
  495. if (end_idx >= segment->getEnd())
  496. {
  497. // Empty segment
  498. ++seg_idx;
  499. seg_offset = 0;
  500. }
  501. else
  502. {
  503. // Empty line
  504. line_ended = true;
  505. ++seg_offset;
  506. }
  507. }
  508. else
  509. {
  510. const llwchar* str = mWText.c_str() + start_idx;
  511. S32 drawn = mGLFont->maxDrawableChars(str,
  512. (F32)abs(mTextRect.getWidth()) - line_width,
  513. end_idx - start_idx,
  514. mWordWrap,
  515. mAllowEmbeddedItems);
  516. if (drawn == 0 && line_width == start_x)
  517. {
  518. // If at the beginning of a line, draw at least one
  519. // character, even if it does not all fit.
  520. drawn = 1;
  521. }
  522. seg_offset += drawn;
  523. line_width += mGLFont->getWidth(str, 0, drawn,
  524. mAllowEmbeddedItems);
  525. end_idx = segment->getStart() + seg_offset;
  526. if (end_idx < segment->getEnd())
  527. {
  528. line_ended = true;
  529. if (mWText[end_idx] == '\n')
  530. {
  531. ++seg_offset; // skip newline
  532. }
  533. }
  534. else
  535. {
  536. // Finished with segment
  537. ++seg_idx;
  538. seg_offset = 0;
  539. }
  540. }
  541. }
  542. }
  543. unbindEmbeddedChars(mGLFont);
  544. mScrollbar->setDocSize(getLineCount());
  545. if (mHideScrollbarForShortDocs)
  546. {
  547. bool short_doc = mScrollbar->getDocSize() <= mScrollbar->getPageSize();
  548. mScrollbar->setVisible(!short_doc);
  549. }
  550. // If scrolled to bottom, stay at bottom unless user is selecting text.
  551. // Do this after updating page size.
  552. if (mScrolledToBottom && mTrackBottom && !hasMouseCapture())
  553. {
  554. endOfDoc();
  555. }
  556. }
  557. ////////////////////////////////////////////////////////////
  558. // LLTextEditor
  559. // Public methods
  560. bool LLTextEditor::truncate()
  561. {
  562. bool did_truncate = false;
  563. // First rough check - if we are less than 1/4th the size, we are OK
  564. if (mWText.size() >= (size_t)(mMaxTextByteLength / 4))
  565. {
  566. // Have to check actual byte size
  567. S32 utf8_byte_size = wstring_utf8_length(mWText);
  568. if (utf8_byte_size > mMaxTextByteLength)
  569. {
  570. // Truncate safely in UTF-8
  571. std::string temp_utf8_text = wstring_to_utf8str(mWText);
  572. temp_utf8_text = utf8str_truncate(temp_utf8_text,
  573. mMaxTextByteLength);
  574. mWText = utf8str_to_wstring(temp_utf8_text);
  575. mTextIsUpToDate = false;
  576. did_truncate = true;
  577. }
  578. }
  579. return did_truncate;
  580. }
  581. void LLTextEditor::setText(const std::string& utf8str)
  582. {
  583. // LLStringUtil::removeCRLF(utf8str);
  584. mUTF8Text = utf8str_removeCRLF(utf8str);
  585. // mUTF8Text = utf8str;
  586. mWText = utf8str_to_wstring(mUTF8Text);
  587. mTextIsUpToDate = true;
  588. truncate();
  589. blockUndo();
  590. setCursorPos(0);
  591. deselect();
  592. needsReflow();
  593. resetDirty();
  594. }
  595. void LLTextEditor::setWText(const LLWString& wtext)
  596. {
  597. mWText = wtext;
  598. mUTF8Text.clear();
  599. mTextIsUpToDate = false;
  600. truncate();
  601. blockUndo();
  602. setCursorPos(0);
  603. deselect();
  604. needsReflow();
  605. resetDirty();
  606. }
  607. //virtual
  608. void LLTextEditor::setValue(const LLSD& value)
  609. {
  610. setText(value.asString());
  611. }
  612. const std::string& LLTextEditor::getText() const
  613. {
  614. if (!mTextIsUpToDate)
  615. {
  616. if (mAllowEmbeddedItems)
  617. {
  618. llwarns << "getText() called on text with embedded items (not supported)"
  619. << llendl;
  620. }
  621. mUTF8Text = wstring_to_utf8str(mWText);
  622. mTextIsUpToDate = true;
  623. }
  624. return mUTF8Text;
  625. }
  626. //virtual
  627. LLSD LLTextEditor::getValue() const
  628. {
  629. return LLSD(getText());
  630. }
  631. void LLTextEditor::setWordWrap(bool b)
  632. {
  633. mWordWrap = b;
  634. setCursorPos(0);
  635. deselect();
  636. needsReflow();
  637. }
  638. void LLTextEditor::setBorderVisible(bool b)
  639. {
  640. mBorder->setVisible(b);
  641. }
  642. bool LLTextEditor::isBorderVisible() const
  643. {
  644. return mBorder->getVisible();
  645. }
  646. void LLTextEditor::setHideScrollbarForShortDocs(bool b)
  647. {
  648. mHideScrollbarForShortDocs = b;
  649. if (mHideScrollbarForShortDocs)
  650. {
  651. bool short_doc = mScrollbar->getDocSize() <= mScrollbar->getPageSize();
  652. mScrollbar->setVisible(!short_doc);
  653. }
  654. }
  655. void LLTextEditor::selectNext(const std::string& search_text_in,
  656. bool case_insensitive, bool wrap)
  657. {
  658. if (search_text_in.empty())
  659. {
  660. return;
  661. }
  662. LLWString text = getWText();
  663. LLWString search_text = utf8str_to_wstring(search_text_in);
  664. if (case_insensitive)
  665. {
  666. LLWStringUtil::toLower(text);
  667. LLWStringUtil::toLower(search_text);
  668. }
  669. if (mIsSelecting)
  670. {
  671. LLWString selected_text = text.substr(mSelectionEnd,
  672. mSelectionStart - mSelectionEnd);
  673. if (selected_text == search_text)
  674. {
  675. // We already have this word selected, we are searching for the next.
  676. mCursorPos += search_text.size();
  677. }
  678. }
  679. size_t loc = text.find(search_text, mCursorPos);
  680. // If Maybe we wrapped, search again
  681. if (wrap && loc == std::string::npos)
  682. {
  683. loc = text.find(search_text);
  684. }
  685. // If still not found, then search_text just is not found.
  686. if (loc == std::string::npos)
  687. {
  688. mIsSelecting = false;
  689. mSelectionEnd = 0;
  690. mSelectionStart = 0;
  691. return;
  692. }
  693. setCursorPos(loc);
  694. scrollToPos(mCursorPos);
  695. mIsSelecting = true;
  696. mSelectionEnd = mCursorPos;
  697. mSelectionStart = llmin((S32)getLength(),
  698. (S32)(mCursorPos + search_text.size()));
  699. }
  700. bool LLTextEditor::replaceText(const std::string& search_text_in,
  701. const std::string& replace_text,
  702. bool case_insensitive, bool wrap)
  703. {
  704. bool replaced = false;
  705. if (search_text_in.empty())
  706. {
  707. return replaced;
  708. }
  709. LLWString search_text = utf8str_to_wstring(search_text_in);
  710. if (mIsSelecting)
  711. {
  712. LLWString text = getWText();
  713. LLWString selected_text = text.substr(mSelectionEnd,
  714. mSelectionStart - mSelectionEnd);
  715. if (case_insensitive)
  716. {
  717. LLWStringUtil::toLower(selected_text);
  718. LLWStringUtil::toLower(search_text);
  719. }
  720. if (selected_text == search_text)
  721. {
  722. // *HACK: this is used when replacing SLURLs with names in chat.
  723. // We invalidate any existing segment at this position then, when
  724. // the replacement text length does not match the replaced text
  725. // length, we shift the segments that follow...
  726. // *TODO: make it a proper text editor feature, and extend segments
  727. // preservation over text deletion and insertion.
  728. if (mPreserveSegments)
  729. {
  730. S32 offset = utf8str_to_wstring(replace_text).size() -
  731. search_text.size();
  732. if (offset != 0 && !mSegments.empty())
  733. {
  734. for (segment_list_t::iterator it = mSegments.begin(),
  735. end = mSegments.end();
  736. it != end; )
  737. {
  738. LLTextSegment* segment = *it;
  739. S32 seg_start = segment->getStart();
  740. S32 seg_end = segment->getEnd();
  741. if (seg_end > mCursorPos)
  742. {
  743. if (seg_start > mCursorPos)
  744. {
  745. segment->shift(offset);
  746. }
  747. else
  748. {
  749. // This is the current segment: only change
  750. // its end position.
  751. S32 new_end = seg_end + offset;
  752. if (seg_start >= new_end)
  753. {
  754. // If we replaced it with empty text, we
  755. // need to delete it entirely.
  756. delete *it;
  757. it = mSegments.erase(it);
  758. continue;
  759. }
  760. segment->setEnd(new_end);
  761. }
  762. }
  763. ++it;
  764. }
  765. }
  766. }
  767. insertText(replace_text);
  768. replaced = true;
  769. }
  770. }
  771. selectNext(search_text_in, case_insensitive, wrap);
  772. return replaced;
  773. }
  774. void LLTextEditor::replaceTextAll(const std::string& search_text,
  775. const std::string& replace_text,
  776. bool case_insensitive)
  777. {
  778. S32 cur_pos = mScrollbar->getDocPos();
  779. setCursorPos(0);
  780. selectNext(search_text, case_insensitive, false);
  781. bool replaced = true;
  782. while (replaced)
  783. {
  784. replaced = replaceText(search_text,replace_text, case_insensitive,
  785. false);
  786. }
  787. mScrollbar->setDocPos(cur_pos);
  788. }
  789. // Picks a new cursor position based on the screen size of text being drawn.
  790. void LLTextEditor::setCursorAtLocalPos(S32 local_x, S32 local_y, bool round)
  791. {
  792. setCursorPos(getCursorPosFromLocalCoord(local_x, local_y, round));
  793. }
  794. S32 LLTextEditor::prevWordPos(S32 cursorPos) const
  795. {
  796. const LLWString& wtext = mWText;
  797. while (cursorPos > 0 && wtext[cursorPos - 1] == ' ')
  798. {
  799. --cursorPos;
  800. }
  801. while (cursorPos > 0 && LLWStringUtil::isPartOfWord(wtext[cursorPos - 1]))
  802. {
  803. --cursorPos;
  804. }
  805. return cursorPos;
  806. }
  807. S32 LLTextEditor::nextWordPos(S32 cursorPos) const
  808. {
  809. const LLWString& wtext = mWText;
  810. while (cursorPos < getLength() &&
  811. LLWStringUtil::isPartOfWord(wtext[cursorPos]))
  812. {
  813. ++cursorPos;
  814. }
  815. while (cursorPos < getLength() && wtext[cursorPos] == ' ')
  816. {
  817. ++cursorPos;
  818. }
  819. return cursorPos;
  820. }
  821. bool LLTextEditor::getWordBoundriesAt(const S32 at, S32* word_begin,
  822. S32* word_length) const
  823. {
  824. S32 pos = at;
  825. S32 start;
  826. if (LLWStringUtil::isPartOfLexicalWord(mWText[pos]))
  827. {
  828. while (pos > 0 && LLWStringUtil::isPartOfLexicalWord(mWText[pos - 1]))
  829. {
  830. --pos;
  831. }
  832. if (mWText[pos] == L'\'')
  833. {
  834. // Do not count "'" at the start of a word
  835. ++pos;
  836. }
  837. start = pos;
  838. while (pos < getLength() &&
  839. LLWStringUtil::isPartOfLexicalWord(mWText[pos]))
  840. {
  841. ++pos;
  842. }
  843. if (mWText[pos - 1] == L'\'')
  844. {
  845. // Do not count "'" at the end of a word
  846. --pos;
  847. }
  848. if (start >= pos)
  849. {
  850. return false;
  851. }
  852. *word_begin = start;
  853. *word_length = pos - start;
  854. return true;
  855. }
  856. return false;
  857. }
  858. S32 LLTextEditor::getLineStart(S32 line) const
  859. {
  860. S32 num_lines = getLineCount();
  861. if (num_lines == 0)
  862. {
  863. return 0;
  864. }
  865. line = llclamp(line, 0, num_lines - 1);
  866. S32 segidx = mLineStartList[line].mSegment;
  867. S32 segoffset = mLineStartList[line].mOffset;
  868. LLTextSegment* seg = mSegments[segidx];
  869. S32 res = seg->getStart() + segoffset;
  870. if (res > seg->getEnd())
  871. {
  872. llwarns << "Text length (" << res << ") greater than text end ("
  873. << seg->getEnd() << ")." << llendl;
  874. res = seg->getEnd();
  875. }
  876. return res;
  877. }
  878. // Given an offset into text (pos), find the corresponding line (from the start
  879. // of the doc) and an offset into the line.
  880. void LLTextEditor::getLineAndOffset(S32 startpos, S32* linep, S32* offsetp) const
  881. {
  882. if (mLineStartList.empty())
  883. {
  884. *linep = 0;
  885. *offsetp = startpos;
  886. }
  887. else
  888. {
  889. S32 seg_idx, seg_offset;
  890. getSegmentAndOffset(startpos, &seg_idx, &seg_offset);
  891. line_info tline(seg_idx, seg_offset);
  892. line_list_t::const_iterator iter = std::upper_bound(mLineStartList.begin(),
  893. mLineStartList.end(),
  894. tline,
  895. line_info_compare());
  896. if (iter != mLineStartList.begin()) --iter;
  897. *linep = iter - mLineStartList.begin();
  898. S32 line_start = mSegments[iter->mSegment]->getStart() + iter->mOffset;
  899. *offsetp = startpos - line_start;
  900. }
  901. }
  902. void LLTextEditor::getSegmentAndOffset(S32 startpos, S32* segidxp,
  903. S32* offsetp) const
  904. {
  905. if (mSegments.empty())
  906. {
  907. *segidxp = -1;
  908. *offsetp = startpos;
  909. }
  910. LLTextSegment tseg(startpos);
  911. segment_list_t::const_iterator seg_iter;
  912. seg_iter = std::upper_bound(mSegments.begin(), mSegments.end(), &tseg,
  913. LLTextSegment::compare());
  914. if (seg_iter != mSegments.begin()) --seg_iter;
  915. *segidxp = seg_iter - mSegments.begin();
  916. *offsetp = startpos - (*seg_iter)->getStart();
  917. }
  918. const LLTextSegment* LLTextEditor::getPreviousSegment() const
  919. {
  920. // Find segment index at character to left of cursor (or rightmost edge of
  921. // selection)
  922. S32 idx = llmax(0, getSegmentIdxAtOffset(mCursorPos) - 1);
  923. return idx >= 0 ? mSegments[idx] : NULL;
  924. }
  925. void LLTextEditor::getSelectedSegments(std::vector<const LLTextSegment*>& segments) const
  926. {
  927. S32 left = hasSelection() ? llmin(mSelectionStart, mSelectionEnd)
  928. : mCursorPos;
  929. S32 right = hasSelection() ? llmax(mSelectionStart, mSelectionEnd)
  930. : mCursorPos;
  931. S32 first_idx = llmax(0, getSegmentIdxAtOffset(left));
  932. S32 last_idx = llmax(0, first_idx, getSegmentIdxAtOffset(right));
  933. for (S32 idx = first_idx; idx <= last_idx; ++idx)
  934. {
  935. segments.push_back(mSegments[idx]);
  936. }
  937. }
  938. S32 LLTextEditor::getCursorPosFromLocalCoord(S32 local_x, S32 local_y,
  939. bool round) const
  940. {
  941. if (mShowLineNumbers)
  942. {
  943. local_x -= UI_TEXTEDITOR_LINE_NUMBER_MARGIN;
  944. }
  945. // If round is true, if the position is on the right half of a character,
  946. // the cursor will be put to its right. If round is false, the cursor will
  947. // always be put to the character's left.
  948. // Figure out which line we are nearest to.
  949. S32 total_lines = getLineCount();
  950. S32 line_height = ll_roundp(mGLFont->getLineHeight());
  951. S32 max_visible_lines = mTextRect.getHeight() / line_height;
  952. S32 scroll_lines = mScrollbar->getDocPos();
  953. // Lines currently visible
  954. S32 visible_lines = llmin(total_lines - scroll_lines, max_visible_lines);
  955. //S32 line = S32(0.5f + ((mTextRect.mTop - local_y) / mGLFont->getLineHeight()));
  956. S32 line = (mTextRect.mTop - 1 - local_y) / line_height;
  957. if (line >= total_lines)
  958. {
  959. return getLength(); // past the end
  960. }
  961. line = llclamp(line, 0, visible_lines) + scroll_lines;
  962. S32 line_start = getLineStart(line);
  963. S32 next_start = getLineStart(line + 1);
  964. S32 line_end = (next_start != line_start) ? next_start - 1 : getLength();
  965. if (line_start == -1)
  966. {
  967. return 0;
  968. }
  969. S32 line_len = line_end - line_start;
  970. S32 pos;
  971. if (mAllowEmbeddedItems)
  972. {
  973. // Figure out which character we are nearest to.
  974. bindEmbeddedChars(mGLFont);
  975. pos = mGLFont->charFromPixelOffset(mWText.c_str(), line_start,
  976. (F32)(local_x - mTextRect.mLeft),
  977. (F32)(mTextRect.getWidth()),
  978. line_len, round, true);
  979. unbindEmbeddedChars(mGLFont);
  980. }
  981. else
  982. {
  983. pos = mGLFont->charFromPixelOffset(mWText.c_str(), line_start,
  984. (F32)(local_x - mTextRect.mLeft),
  985. (F32)mTextRect.getWidth(),
  986. line_len, round);
  987. }
  988. return line_start + pos;
  989. }
  990. void LLTextEditor::setCursor(S32 row, S32 column)
  991. {
  992. // Make sure we are not trying to set the cursor out of boundaries
  993. if (row < 0)
  994. {
  995. row = 0;
  996. }
  997. if (column < 0)
  998. {
  999. column = 0;
  1000. }
  1001. const llwchar* doc = mWText.c_str();
  1002. while (row--)
  1003. {
  1004. while (*doc && *doc++ != '\n');
  1005. }
  1006. while (column-- && *doc && *doc++ != '\n');
  1007. setCursorPos(doc - mWText.c_str());
  1008. }
  1009. void LLTextEditor::setCursorPos(S32 offset)
  1010. {
  1011. mCursorPos = llclamp(offset, 0, (S32)getLength());
  1012. needsScroll();
  1013. // Reset desired x cursor position
  1014. mDesiredXPixel = -1;
  1015. }
  1016. void LLTextEditor::setFont(LLFontGL* fontp)
  1017. {
  1018. mGLFont = fontp ? fontp : LLFontGL::getFontSansSerif();
  1019. // Take into account the new font size. HB
  1020. updateLineStartList();
  1021. // Propagate the new font size information to the scrollbar. HB
  1022. mScrollbar->setDocSize(getLineCount());
  1023. S32 line_height = ll_roundp(mGLFont->getLineHeight());
  1024. S32 page_lines = mTextRect.getHeight() / line_height;
  1025. mScrollbar->setPageSize(page_lines);
  1026. }
  1027. void LLTextEditor::deselect()
  1028. {
  1029. mSelectionStart = 0;
  1030. mSelectionEnd = 0;
  1031. mIsSelecting = false;
  1032. }
  1033. void LLTextEditor::startSelection()
  1034. {
  1035. if (!mIsSelecting)
  1036. {
  1037. mIsSelecting = true;
  1038. mSelectionStart = mCursorPos;
  1039. mSelectionEnd = mCursorPos;
  1040. }
  1041. }
  1042. void LLTextEditor::endSelection()
  1043. {
  1044. if (mIsSelecting)
  1045. {
  1046. mIsSelecting = false;
  1047. mSelectionEnd = mCursorPos;
  1048. }
  1049. }
  1050. void LLTextEditor::setSelection(S32 start, S32 end)
  1051. {
  1052. setCursorPos(end);
  1053. startSelection();
  1054. setCursorPos(start);
  1055. endSelection();
  1056. }
  1057. bool LLTextEditor::selectionContainsLineBreaks()
  1058. {
  1059. if (hasSelection())
  1060. {
  1061. S32 left = llmin(mSelectionStart, mSelectionEnd);
  1062. S32 right = left + abs(mSelectionStart - mSelectionEnd);
  1063. const LLWString& wtext = mWText;
  1064. for (S32 i = left; i < right; ++i)
  1065. {
  1066. if (wtext[i] == '\n')
  1067. {
  1068. return true;
  1069. }
  1070. }
  1071. }
  1072. return false;
  1073. }
  1074. // Assumes that pos is at the start of the line spaces may be positive (indent)
  1075. // or negative (unindent). Returns the actual number of characters added or
  1076. // removed.
  1077. S32 LLTextEditor::indentLine(S32 pos, S32 spaces)
  1078. {
  1079. llassert(pos >= 0);
  1080. llassert(pos <= getLength());
  1081. S32 delta_spaces = 0;
  1082. if (spaces >= 0)
  1083. {
  1084. // Indent
  1085. for (S32 i = 0; i < spaces; ++i)
  1086. {
  1087. delta_spaces += addChar(pos, ' ');
  1088. }
  1089. }
  1090. else
  1091. {
  1092. // Unindent
  1093. for (S32 i = 0; i < -spaces; ++i)
  1094. {
  1095. const LLWString& wtext = mWText;
  1096. if (wtext[pos] == ' ')
  1097. {
  1098. delta_spaces += remove(pos, 1, false);
  1099. }
  1100. }
  1101. }
  1102. return delta_spaces;
  1103. }
  1104. void LLTextEditor::indentSelectedLines(S32 spaces)
  1105. {
  1106. if (hasSelection())
  1107. {
  1108. const LLWString& text = mWText;
  1109. S32 left = llmin(mSelectionStart, mSelectionEnd);
  1110. S32 right = left + abs(mSelectionStart - mSelectionEnd);
  1111. bool cursor_on_right = mSelectionEnd > mSelectionStart;
  1112. S32 cur = left;
  1113. // Expand left to start of line
  1114. while (cur > 0 && text[cur] != '\n')
  1115. {
  1116. --cur;
  1117. }
  1118. left = cur;
  1119. if (cur > 0)
  1120. {
  1121. ++left;
  1122. }
  1123. // Expand right to end of line
  1124. if (text[right - 1] == '\n')
  1125. {
  1126. --right;
  1127. }
  1128. else
  1129. {
  1130. while (text[right] != '\n' && right <= getLength())
  1131. {
  1132. ++right;
  1133. }
  1134. }
  1135. // Find each start-of-line and indent it
  1136. do
  1137. {
  1138. if (text[cur] == '\n')
  1139. {
  1140. ++cur;
  1141. }
  1142. S32 delta_spaces = indentLine(cur, spaces);
  1143. if (delta_spaces > 0)
  1144. {
  1145. cur += delta_spaces;
  1146. }
  1147. right += delta_spaces;
  1148. //text = mWText;
  1149. // Find the next new line
  1150. while (cur < right && text[cur] != '\n')
  1151. {
  1152. ++cur;
  1153. }
  1154. }
  1155. while (cur < right);
  1156. if (right < getLength() && text[right] == '\n')
  1157. {
  1158. ++right;
  1159. }
  1160. // Set the selection and cursor
  1161. if (cursor_on_right)
  1162. {
  1163. mSelectionStart = left;
  1164. mSelectionEnd = right;
  1165. }
  1166. else
  1167. {
  1168. mSelectionStart = right;
  1169. mSelectionEnd = left;
  1170. }
  1171. mCursorPos = mSelectionEnd;
  1172. }
  1173. }
  1174. //virtual
  1175. void LLTextEditor::selectAll()
  1176. {
  1177. mSelectionStart = getLength();
  1178. mSelectionEnd = 0;
  1179. mCursorPos = mSelectionEnd;
  1180. }
  1181. bool LLTextEditor::handleToolTip(S32 x, S32 y, std::string& msg,
  1182. LLRect* sticky_rect_screen)
  1183. {
  1184. for (child_list_const_iter_t child_it = getChildList()->begin(),
  1185. end = getChildList()->end();
  1186. child_it != end; ++child_it)
  1187. {
  1188. LLView* viewp = *child_it;
  1189. S32 local_x = x - viewp->getRect().mLeft;
  1190. S32 local_y = y - viewp->getRect().mBottom;
  1191. if (viewp->handleToolTip(local_x, local_y, msg, sticky_rect_screen))
  1192. {
  1193. return true;
  1194. }
  1195. }
  1196. if (mSegments.empty())
  1197. {
  1198. return true;
  1199. }
  1200. const LLTextSegment* cur_segment = getSegmentAtLocalPos(x, y);
  1201. if (cur_segment)
  1202. {
  1203. bool has_tool_tip = cur_segment->getToolTip(msg);
  1204. if (has_tool_tip)
  1205. {
  1206. // Just use a slop area around the cursor
  1207. // Convert rect local to screen coordinates
  1208. S32 SLOP = 8;
  1209. localPointToScreen(x - SLOP, y - SLOP,
  1210. &(sticky_rect_screen->mLeft),
  1211. &(sticky_rect_screen->mBottom));
  1212. sticky_rect_screen->mRight = sticky_rect_screen->mLeft + 2 * SLOP;
  1213. sticky_rect_screen->mTop = sticky_rect_screen->mBottom + 2 * SLOP;
  1214. }
  1215. }
  1216. return true;
  1217. }
  1218. bool LLTextEditor::handleScrollWheel(S32 x, S32 y, S32 clicks)
  1219. {
  1220. // Pretend the mouse is over the scrollbar
  1221. return mScrollbar->handleScrollWheel(0, 0, clicks);
  1222. }
  1223. bool LLTextEditor::handleMouseDown(S32 x, S32 y, MASK mask)
  1224. {
  1225. bool handled = false;
  1226. // Key presses are not being passed to the Popup menu.
  1227. // A proper fix is non-trivial so instead just close the menu.
  1228. LLMenuGL* menu = getContextMenu();
  1229. if (menu && menu->isOpen())
  1230. {
  1231. LLMenuGL::sMenuContainer->hideMenus();
  1232. }
  1233. // Let scrollbar have first dibs
  1234. handled = LLView::childrenHandleMouseDown(x, y, mask) != NULL;
  1235. if (!handled)
  1236. {
  1237. if (!(mask & MASK_SHIFT))
  1238. {
  1239. deselect();
  1240. }
  1241. // If we are not scrolling (handled by child), then we are selecting
  1242. if (mask & MASK_SHIFT)
  1243. {
  1244. S32 old_cursor_pos = mCursorPos;
  1245. setCursorAtLocalPos(x, y, true);
  1246. if (hasSelection())
  1247. {
  1248. #if 0 // Mac-like behavior - extend selection towards the cursor
  1249. if (mCursorPos < mSelectionStart &&
  1250. mCursorPos < mSelectionEnd)
  1251. {
  1252. // ...left of selection
  1253. mSelectionStart = llmax(mSelectionStart,
  1254. mSelectionEnd);
  1255. mSelectionEnd = mCursorPos;
  1256. }
  1257. else if (mCursorPos > mSelectionStart &&
  1258. mCursorPos > mSelectionEnd)
  1259. {
  1260. // ...right of selection
  1261. mSelectionStart = llmin(mSelectionStart,
  1262. mSelectionEnd);
  1263. mSelectionEnd = mCursorPos;
  1264. }
  1265. else
  1266. {
  1267. mSelectionEnd = mCursorPos;
  1268. }
  1269. #else
  1270. // Windows behavior
  1271. mSelectionEnd = mCursorPos;
  1272. #endif
  1273. }
  1274. else
  1275. {
  1276. mSelectionStart = old_cursor_pos;
  1277. mSelectionEnd = mCursorPos;
  1278. }
  1279. // Assume we are starting a drag select
  1280. mIsSelecting = true;
  1281. }
  1282. else
  1283. {
  1284. setCursorAtLocalPos(x, y, true);
  1285. startSelection();
  1286. }
  1287. gFocusMgr.setMouseCapture(this);
  1288. handled = true;
  1289. }
  1290. if (hasTabStop())
  1291. {
  1292. setFocus(true);
  1293. handled = true;
  1294. }
  1295. // Delay cursor flashing
  1296. resetKeystrokeTimer();
  1297. return handled;
  1298. }
  1299. bool LLTextEditor::handleMiddleMouseDown(S32 x, S32 y, MASK mask)
  1300. {
  1301. setFocus(true);
  1302. if (canPastePrimary())
  1303. {
  1304. setCursorAtLocalPos(x, y, true);
  1305. pastePrimary();
  1306. }
  1307. return true;
  1308. }
  1309. bool LLTextEditor::handleRightMouseDown(S32 x, S32 y, MASK mask)
  1310. {
  1311. setFocus(true);
  1312. S32 word_start = 0;
  1313. S32 word_len = 0;
  1314. S32 pos = getCursorPosFromLocalCoord(x, y, true);
  1315. // If the context menu has not yet been created for this editor, this call
  1316. // will create it now. HB
  1317. LLMenuGL* menu = createContextMenu();
  1318. if (menu)
  1319. {
  1320. SpellMenuBind* menu_bind;
  1321. LLMenuItemCallGL* menu_item;
  1322. // Remove old suggestions
  1323. for (S32 i = 0, count = mSuggestionMenuItems.size();
  1324. i < count; ++i)
  1325. {
  1326. menu_bind = mSuggestionMenuItems[i];
  1327. if (menu_bind)
  1328. {
  1329. menu_item = menu_bind->mMenuItem;
  1330. menu->remove(menu_item);
  1331. menu_item->die();
  1332. #if 0
  1333. delete menu_bind->mMenuItem;
  1334. menu_bind->mMenuItem = NULL;
  1335. #endif
  1336. delete menu_bind;
  1337. }
  1338. }
  1339. mSuggestionMenuItems.clear();
  1340. // Not read-only, spell_check="true" in xui and spell checking enabled
  1341. bool spell_check = !mReadOnly && mSpellCheck &&
  1342. LLSpellCheck::getInstance()->getSpellCheck();
  1343. menu->setItemVisible("spell_sep", spell_check);
  1344. if (spell_check)
  1345. {
  1346. // Search for word matches
  1347. bool is_word_part = getWordBoundriesAt(pos, &word_start,
  1348. &word_len);
  1349. if (is_word_part)
  1350. {
  1351. const LLWString& text = mWText;
  1352. std::string part(text.begin(), text.end());
  1353. std::string selected_word = part.substr(word_start, word_len);
  1354. if (!LLSpellCheck::getInstance()->checkSpelling(selected_word))
  1355. {
  1356. // Misspelled word here
  1357. std::vector<std::string> suggestions;
  1358. S32 count =
  1359. LLSpellCheck::getInstance()->getSuggestions(selected_word,
  1360. suggestions);
  1361. for (S32 i = 0; i < count; ++i)
  1362. {
  1363. menu_bind = new SpellMenuBind;
  1364. menu_bind->mOrigin = this;
  1365. menu_bind->mWord = suggestions[i];
  1366. menu_bind->mWordPositionEnd = word_start + word_len;
  1367. menu_bind->mWordPositionStart = word_start;
  1368. menu_item = new LLMenuItemCallGL(menu_bind->mWord,
  1369. spellCorrect,
  1370. NULL, menu_bind);
  1371. menu_bind->mMenuItem = menu_item;
  1372. mSuggestionMenuItems.push_back(menu_bind);
  1373. menu->append(menu_item);
  1374. }
  1375. menu_bind = new SpellMenuBind;
  1376. menu_bind->mOrigin = this;
  1377. menu_bind->mWord = selected_word;
  1378. menu_bind->mWordPositionEnd = word_start + word_len;
  1379. menu_bind->mWordPositionStart = word_start;
  1380. menu_item = new LLMenuItemCallGL("Add word", spellAdd,
  1381. NULL, menu_bind);
  1382. menu_bind->mMenuItem = menu_item;
  1383. mSuggestionMenuItems.push_back(menu_bind);
  1384. menu->append(menu_item);
  1385. menu_bind = new SpellMenuBind;
  1386. menu_bind->mOrigin = this;
  1387. menu_bind->mWord = selected_word;
  1388. menu_bind->mWordPositionEnd = word_start + word_len;
  1389. menu_bind->mWordPositionStart = word_start;
  1390. menu_item = new LLMenuItemCallGL("Ignore word",
  1391. spellIgnore, NULL,
  1392. menu_bind);
  1393. menu_bind->mMenuItem = menu_item;
  1394. mSuggestionMenuItems.push_back(menu_bind);
  1395. menu->append(menu_item);
  1396. }
  1397. }
  1398. menu_bind = new SpellMenuBind;
  1399. menu_bind->mOrigin = this;
  1400. if (mShowMisspelled)
  1401. {
  1402. menu_bind->mWord = "Hide misspellings";
  1403. }
  1404. else
  1405. {
  1406. menu_bind->mWord = "Show misspellings";
  1407. }
  1408. menu_item = new LLMenuItemCallGL(menu_bind->mWord, spellShow,
  1409. NULL, menu_bind);
  1410. menu_bind->mMenuItem = menu_item;
  1411. mSuggestionMenuItems.push_back(menu_bind);
  1412. menu->append(menu_item);
  1413. }
  1414. menu->buildDrawLabels();
  1415. menu->updateParent(LLMenuGL::sMenuContainer);
  1416. LLMenuGL::showPopup(this, menu, x, y);
  1417. }
  1418. return true;
  1419. }
  1420. bool LLTextEditor::handleHover(S32 x, S32 y, MASK mask)
  1421. {
  1422. bool handled = false;
  1423. mHoverSegment = NULL;
  1424. if (hasMouseCapture())
  1425. {
  1426. if (mIsSelecting)
  1427. {
  1428. if (x != mLastSelectionX || y != mLastSelectionY)
  1429. {
  1430. mLastSelectionX = x;
  1431. mLastSelectionY = y;
  1432. }
  1433. if (y > mTextRect.mTop)
  1434. {
  1435. mScrollbar->setDocPos(mScrollbar->getDocPos() - 1);
  1436. }
  1437. else if (y < mTextRect.mBottom)
  1438. {
  1439. mScrollbar->setDocPos(mScrollbar->getDocPos() + 1);
  1440. }
  1441. setCursorAtLocalPos(x, y, true);
  1442. mSelectionEnd = mCursorPos;
  1443. }
  1444. LL_DEBUGS("UserInput") << "hover handled by " << getName()
  1445. << " (active)" << LL_ENDL;
  1446. gWindowp->setCursor(UI_CURSOR_IBEAM);
  1447. handled = true;
  1448. }
  1449. if (!handled)
  1450. {
  1451. // Pass to children
  1452. handled = LLView::childrenHandleHover(x, y, mask) != NULL;
  1453. }
  1454. if (handled)
  1455. {
  1456. // Delay cursor flashing
  1457. resetKeystrokeTimer();
  1458. }
  1459. // Opaque
  1460. if (!handled)
  1461. {
  1462. // Check to see if we are over an HTML-style link
  1463. if (!mSegments.empty())
  1464. {
  1465. const LLTextSegment* cur_segment = getSegmentAtLocalPos(x, y);
  1466. if (cur_segment)
  1467. {
  1468. if (cur_segment->getStyle()->isLink())
  1469. {
  1470. LL_DEBUGS("UserInput") << "hover handled by " << getName()
  1471. << " (over link, inactive)"
  1472. << LL_ENDL;
  1473. gWindowp->setCursor(UI_CURSOR_HAND);
  1474. handled = true;
  1475. }
  1476. else if (cur_segment->getStyle()->getIsEmbeddedItem())
  1477. {
  1478. LL_DEBUGS("UserInput") << "hover handled by " << getName()
  1479. << " (over embedded item, inactive)"
  1480. << LL_ENDL;
  1481. gWindowp->setCursor(UI_CURSOR_HAND);
  1482. handled = true;
  1483. }
  1484. mHoverSegment = cur_segment;
  1485. }
  1486. }
  1487. if (!handled)
  1488. {
  1489. LL_DEBUGS("UserInput") << "hover handled by " << getName()
  1490. << " (inactive)" << LL_ENDL;
  1491. if (!mScrollbar->getVisible() ||
  1492. x < getRect().getWidth() - SCROLLBAR_SIZE)
  1493. {
  1494. gWindowp->setCursor(UI_CURSOR_IBEAM);
  1495. }
  1496. else
  1497. {
  1498. gWindowp->setCursor(UI_CURSOR_ARROW);
  1499. }
  1500. handled = true;
  1501. }
  1502. }
  1503. if (mOnScrollEndCallback && mOnScrollEndData &&
  1504. mScrollbar->getDocPos() == mScrollbar->getDocPosMax())
  1505. {
  1506. mOnScrollEndCallback(mOnScrollEndData);
  1507. }
  1508. return handled;
  1509. }
  1510. bool LLTextEditor::handleMouseUp(S32 x, S32 y, MASK mask)
  1511. {
  1512. bool handled = false;
  1513. // let scrollbar have first dibs
  1514. handled = LLView::childrenHandleMouseUp(x, y, mask) != NULL;
  1515. if (!handled)
  1516. {
  1517. if (mIsSelecting)
  1518. {
  1519. // Finish selection
  1520. if (y > mTextRect.mTop)
  1521. {
  1522. mScrollbar->setDocPos(mScrollbar->getDocPos() - 1);
  1523. }
  1524. else if (y < mTextRect.mBottom)
  1525. {
  1526. mScrollbar->setDocPos(mScrollbar->getDocPos() + 1);
  1527. }
  1528. setCursorAtLocalPos(x, y, true);
  1529. endSelection();
  1530. }
  1531. if (!hasSelection())
  1532. {
  1533. handleMouseUpOverSegment(x, y, mask);
  1534. }
  1535. // Take selection to 'primary' clipboard
  1536. updatePrimary();
  1537. handled = true;
  1538. }
  1539. // Delay cursor flashing
  1540. resetKeystrokeTimer();
  1541. if (hasMouseCapture())
  1542. {
  1543. gFocusMgr.setMouseCapture(NULL);
  1544. handled = true;
  1545. }
  1546. return handled;
  1547. }
  1548. bool LLTextEditor::handleDoubleClick(S32 x, S32 y, MASK mask)
  1549. {
  1550. bool handled = false;
  1551. // Let scrollbar have first dibs
  1552. handled = LLView::childrenHandleDoubleClick(x, y, mask) != NULL;
  1553. if (!handled)
  1554. {
  1555. setCursorAtLocalPos(x, y, false);
  1556. deselect();
  1557. const LLWString& text = mWText;
  1558. if (LLWStringUtil::isPartOfWord(text[mCursorPos]))
  1559. {
  1560. // Select word the cursor is over
  1561. while (mCursorPos > 0 &&
  1562. LLWStringUtil::isPartOfWord(text[mCursorPos - 1]))
  1563. {
  1564. --mCursorPos;
  1565. }
  1566. startSelection();
  1567. while (mCursorPos < (S32)text.length() &&
  1568. LLWStringUtil::isPartOfWord(text[mCursorPos]))
  1569. {
  1570. ++mCursorPos;
  1571. }
  1572. mSelectionEnd = mCursorPos;
  1573. }
  1574. else if (mCursorPos < (S32)text.length() &&
  1575. !iswspace(text[mCursorPos]))
  1576. {
  1577. // Select the character the cursor is over
  1578. startSelection();
  1579. mSelectionEnd = ++mCursorPos;
  1580. }
  1581. // We do not want handleMouseUp() to "finish" the selection (and
  1582. // thereby set mSelectionEnd to where the mouse is), so we finish the
  1583. // selectionhere.
  1584. mIsSelecting = false;
  1585. // Delay cursor flashing
  1586. resetKeystrokeTimer();
  1587. // Take selection to 'primary' clipboard
  1588. updatePrimary();
  1589. handled = true;
  1590. }
  1591. return handled;
  1592. }
  1593. // Allow calling cards to be dropped onto text fields. Append the name and
  1594. // a carriage return.
  1595. //virtual
  1596. bool LLTextEditor::handleDragAndDrop(S32 x, S32 y, MASK mask, bool drop,
  1597. EDragAndDropType cargo_type,
  1598. void* cargo_data, EAcceptance* accept,
  1599. std::string& tooltip_msg)
  1600. {
  1601. *accept = ACCEPT_NO;
  1602. return true;
  1603. }
  1604. // Returns change in number of characters in mText
  1605. S32 LLTextEditor::execute(LLTextCmd* cmd)
  1606. {
  1607. S32 delta = 0;
  1608. if (cmd->execute(this, &delta))
  1609. {
  1610. // Delete top of undo stack
  1611. undo_stack_t::iterator enditer = std::find(mUndoStack.begin(),
  1612. mUndoStack.end(), mLastCmd);
  1613. if (enditer != mUndoStack.begin())
  1614. {
  1615. --enditer;
  1616. std::for_each(mUndoStack.begin(), enditer, DeletePointer());
  1617. mUndoStack.erase(mUndoStack.begin(), enditer);
  1618. }
  1619. // Push the new command is now on the top (front) of the undo stack.
  1620. mUndoStack.push_front(cmd);
  1621. mLastCmd = cmd;
  1622. if (mKeystrokeCallback)
  1623. {
  1624. mKeystrokeCallback(this, mKeystrokeData);
  1625. }
  1626. }
  1627. else
  1628. {
  1629. // Operation failed, so do not put it on the undo stack.
  1630. delete cmd;
  1631. }
  1632. return delta;
  1633. }
  1634. S32 LLTextEditor::insert(S32 pos, const LLWString& wstr,
  1635. bool group_with_next_op)
  1636. {
  1637. return execute(new LLTextCmdInsert(pos, group_with_next_op, wstr));
  1638. }
  1639. S32 LLTextEditor::remove(S32 pos, S32 length, bool group_with_next_op)
  1640. {
  1641. return execute(new LLTextCmdRemove(pos, group_with_next_op, length));
  1642. }
  1643. S32 LLTextEditor::append(const LLWString& wstr, bool group_with_next_op)
  1644. {
  1645. return insert(mWText.length(), wstr, group_with_next_op);
  1646. }
  1647. S32 LLTextEditor::overwriteChar(S32 pos, llwchar wc)
  1648. {
  1649. if ((S32)mWText.length() == pos)
  1650. {
  1651. return addChar(pos, wc);
  1652. }
  1653. else
  1654. {
  1655. return execute(new LLTextCmdOverwriteChar(pos, false, wc));
  1656. }
  1657. }
  1658. // Removes a single character from the text. Tries to remove a pseudo-tab (up
  1659. // to four spaces in a row)
  1660. void LLTextEditor::removeCharOrTab()
  1661. {
  1662. if (!getEnabled())
  1663. {
  1664. return;
  1665. }
  1666. if (mCursorPos > 0)
  1667. {
  1668. S32 chars_to_remove = 1;
  1669. const LLWString& text = mWText;
  1670. if (text[mCursorPos - 1] == ' ')
  1671. {
  1672. // Try to remove a "tab"
  1673. S32 line, offset;
  1674. getLineAndOffset(mCursorPos, &line, &offset);
  1675. if (offset > 0)
  1676. {
  1677. chars_to_remove = offset % SPACES_PER_TAB;
  1678. if (chars_to_remove == 0)
  1679. {
  1680. chars_to_remove = SPACES_PER_TAB;
  1681. }
  1682. for (S32 i = 0; i < chars_to_remove; ++i)
  1683. {
  1684. if (text[ mCursorPos - i - 1] != ' ')
  1685. {
  1686. // Fewer than a full tab's worth of spaces, so just
  1687. // delete a single character.
  1688. chars_to_remove = 1;
  1689. break;
  1690. }
  1691. }
  1692. }
  1693. }
  1694. for (S32 i = 0; i < chars_to_remove; ++i)
  1695. {
  1696. setCursorPos(mCursorPos - 1);
  1697. remove(mCursorPos, 1, false);
  1698. }
  1699. }
  1700. else
  1701. {
  1702. reportBadKeystroke();
  1703. }
  1704. }
  1705. // Remove a single character from the text
  1706. S32 LLTextEditor::removeChar(S32 pos)
  1707. {
  1708. if (mKeystrokeCallback)
  1709. {
  1710. mKeystrokeCallback(this, mKeystrokeData);
  1711. }
  1712. return remove(pos, 1, false);
  1713. }
  1714. void LLTextEditor::removeChar()
  1715. {
  1716. if (!getEnabled())
  1717. {
  1718. return;
  1719. }
  1720. if (mCursorPos > 0)
  1721. {
  1722. setCursorPos(mCursorPos - 1);
  1723. removeChar(mCursorPos);
  1724. }
  1725. else
  1726. {
  1727. reportBadKeystroke();
  1728. }
  1729. }
  1730. // Add a single character to the text
  1731. S32 LLTextEditor::addChar(S32 pos, llwchar wc)
  1732. {
  1733. if (wstring_utf8_length(mWText) +
  1734. wchar_utf8_length(wc) >= mMaxTextByteLength)
  1735. {
  1736. make_ui_sound("UISndBadKeystroke");
  1737. return 0;
  1738. }
  1739. if (mKeystrokeCallback)
  1740. {
  1741. mKeystrokeCallback(this, mKeystrokeData);
  1742. }
  1743. if (mLastCmd && mLastCmd->canExtend(pos))
  1744. {
  1745. S32 delta = 0;
  1746. mLastCmd->extendAndExecute(this, pos, wc, &delta);
  1747. return delta;
  1748. }
  1749. else
  1750. {
  1751. return execute(new LLTextCmdAddChar(pos, false, wc));
  1752. }
  1753. }
  1754. void LLTextEditor::addChar(llwchar wc)
  1755. {
  1756. if (!getEnabled())
  1757. {
  1758. return;
  1759. }
  1760. if (hasSelection())
  1761. {
  1762. deleteSelection(true);
  1763. }
  1764. else if (gKeyboardp && gKeyboardp->getInsertMode() == LL_KIM_OVERWRITE)
  1765. {
  1766. removeChar(mCursorPos);
  1767. }
  1768. setCursorPos(mCursorPos + addChar(mCursorPos, wc));
  1769. }
  1770. bool LLTextEditor::handleSelectionKey(KEY key, MASK mask)
  1771. {
  1772. bool handled = false;
  1773. if (mask & MASK_SHIFT)
  1774. {
  1775. handled = true;
  1776. switch (key)
  1777. {
  1778. case KEY_LEFT:
  1779. if (0 < mCursorPos)
  1780. {
  1781. startSelection();
  1782. --mCursorPos;
  1783. if (mask & MASK_CONTROL)
  1784. {
  1785. mCursorPos = prevWordPos(mCursorPos);
  1786. }
  1787. mSelectionEnd = mCursorPos;
  1788. }
  1789. break;
  1790. case KEY_RIGHT:
  1791. if (mCursorPos < getLength())
  1792. {
  1793. startSelection();
  1794. ++mCursorPos;
  1795. if (mask & MASK_CONTROL)
  1796. {
  1797. mCursorPos = nextWordPos(mCursorPos);
  1798. }
  1799. mSelectionEnd = mCursorPos;
  1800. }
  1801. break;
  1802. case KEY_UP:
  1803. startSelection();
  1804. changeLine(-1);
  1805. mSelectionEnd = mCursorPos;
  1806. break;
  1807. case KEY_PAGE_UP:
  1808. startSelection();
  1809. changePage(-1);
  1810. mSelectionEnd = mCursorPos;
  1811. break;
  1812. case KEY_HOME:
  1813. startSelection();
  1814. if (mask & MASK_CONTROL)
  1815. {
  1816. mCursorPos = 0;
  1817. }
  1818. else
  1819. {
  1820. startOfLine();
  1821. }
  1822. mSelectionEnd = mCursorPos;
  1823. break;
  1824. case KEY_DOWN:
  1825. startSelection();
  1826. changeLine(1);
  1827. mSelectionEnd = mCursorPos;
  1828. break;
  1829. case KEY_PAGE_DOWN:
  1830. startSelection();
  1831. changePage(1);
  1832. mSelectionEnd = mCursorPos;
  1833. break;
  1834. case KEY_END:
  1835. startSelection();
  1836. if (mask & MASK_CONTROL)
  1837. {
  1838. mCursorPos = getLength();
  1839. }
  1840. else
  1841. {
  1842. endOfLine();
  1843. }
  1844. mSelectionEnd = mCursorPos;
  1845. break;
  1846. default:
  1847. handled = false;
  1848. }
  1849. }
  1850. if (!handled && mHandleEditKeysDirectly)
  1851. {
  1852. if ((MASK_CONTROL & mask) && key == 'A')
  1853. {
  1854. if (canSelectAll())
  1855. {
  1856. selectAll();
  1857. }
  1858. else
  1859. {
  1860. reportBadKeystroke();
  1861. }
  1862. handled = true;
  1863. }
  1864. }
  1865. if (handled)
  1866. {
  1867. // Take selection to 'primary' clipboard
  1868. updatePrimary();
  1869. }
  1870. return handled;
  1871. }
  1872. bool LLTextEditor::handleNavigationKey(KEY key, MASK mask)
  1873. {
  1874. bool handled = false;
  1875. // Ignore capslock key
  1876. if (MASK_NONE == mask)
  1877. {
  1878. handled = true;
  1879. switch (key)
  1880. {
  1881. case KEY_UP:
  1882. if (mReadOnly)
  1883. {
  1884. mScrollbar->setDocPos(mScrollbar->getDocPos() - 1);
  1885. }
  1886. else
  1887. {
  1888. changeLine(-1);
  1889. }
  1890. break;
  1891. case KEY_PAGE_UP:
  1892. changePage(-1);
  1893. break;
  1894. case KEY_HOME:
  1895. if (mReadOnly)
  1896. {
  1897. mScrollbar->setDocPos(0);
  1898. }
  1899. else
  1900. {
  1901. startOfLine();
  1902. }
  1903. break;
  1904. case KEY_DOWN:
  1905. if (mReadOnly)
  1906. {
  1907. mScrollbar->setDocPos(mScrollbar->getDocPos() + 1);
  1908. }
  1909. else
  1910. {
  1911. changeLine(1);
  1912. }
  1913. break;
  1914. case KEY_PAGE_DOWN:
  1915. changePage(1);
  1916. break;
  1917. case KEY_END:
  1918. if (mReadOnly)
  1919. {
  1920. mScrollbar->setDocPos(mScrollbar->getDocPosMax());
  1921. }
  1922. else
  1923. {
  1924. endOfLine();
  1925. }
  1926. break;
  1927. case KEY_LEFT:
  1928. if (mReadOnly)
  1929. {
  1930. break;
  1931. }
  1932. if (hasSelection())
  1933. {
  1934. setCursorPos(llmin(mCursorPos - 1, mSelectionStart,
  1935. mSelectionEnd));
  1936. }
  1937. else
  1938. {
  1939. if (0 < mCursorPos)
  1940. {
  1941. setCursorPos(mCursorPos - 1);
  1942. }
  1943. else
  1944. {
  1945. reportBadKeystroke();
  1946. }
  1947. }
  1948. break;
  1949. case KEY_RIGHT:
  1950. if (mReadOnly)
  1951. {
  1952. break;
  1953. }
  1954. if (hasSelection())
  1955. {
  1956. setCursorPos(llmax(mCursorPos + 1, mSelectionStart,
  1957. mSelectionEnd));
  1958. }
  1959. else
  1960. {
  1961. if (mCursorPos < getLength())
  1962. {
  1963. setCursorPos(mCursorPos + 1);
  1964. }
  1965. else
  1966. {
  1967. reportBadKeystroke();
  1968. }
  1969. }
  1970. break;
  1971. default:
  1972. handled = false;
  1973. }
  1974. }
  1975. if (mOnScrollEndCallback && mOnScrollEndData &&
  1976. mScrollbar->getDocPos() == mScrollbar->getDocPosMax())
  1977. {
  1978. mOnScrollEndCallback(mOnScrollEndData);
  1979. }
  1980. return handled;
  1981. }
  1982. void LLTextEditor::deleteSelection(bool group_with_next_op)
  1983. {
  1984. if (getEnabled() && hasSelection())
  1985. {
  1986. S32 pos = llmin(mSelectionStart, mSelectionEnd);
  1987. S32 length = abs(mSelectionStart - mSelectionEnd);
  1988. remove(pos, length, group_with_next_op);
  1989. deselect();
  1990. setCursorPos(pos);
  1991. }
  1992. }
  1993. //virtual
  1994. bool LLTextEditor::canCut() const
  1995. {
  1996. return !mReadOnly && hasSelection();
  1997. }
  1998. // Cuts selection to clipboard
  1999. void LLTextEditor::cut()
  2000. {
  2001. if (!canCut())
  2002. {
  2003. return;
  2004. }
  2005. S32 left_pos = llmin(mSelectionStart, mSelectionEnd);
  2006. S32 length = abs(mSelectionStart - mSelectionEnd);
  2007. gClipboard.copyFromSubstring(mWText, left_pos, length);
  2008. deleteSelection(false);
  2009. needsReflow();
  2010. if (mKeystrokeCallback)
  2011. {
  2012. mKeystrokeCallback(this, mKeystrokeData);
  2013. }
  2014. // Force spell-check update:
  2015. mKeystrokeTimer.reset();
  2016. mPrevSpelledText.erase();
  2017. }
  2018. bool LLTextEditor::canCopy() const
  2019. {
  2020. return hasSelection();
  2021. }
  2022. // Copies selection to clipboard
  2023. void LLTextEditor::copy()
  2024. {
  2025. if (!canCopy())
  2026. {
  2027. return;
  2028. }
  2029. S32 left_pos = llmin(mSelectionStart, mSelectionEnd);
  2030. S32 length = abs(mSelectionStart - mSelectionEnd);
  2031. gClipboard.copyFromSubstring(mWText, left_pos, length);
  2032. // Force spell-check update:
  2033. mKeystrokeTimer.reset();
  2034. mPrevSpelledText.erase();
  2035. }
  2036. bool LLTextEditor::canPaste() const
  2037. {
  2038. return !mReadOnly && gClipboard.canPasteString();
  2039. }
  2040. // Pastes from clipboard
  2041. void LLTextEditor::paste()
  2042. {
  2043. pasteHelper(false);
  2044. }
  2045. // Pastes from primary
  2046. void LLTextEditor::pastePrimary()
  2047. {
  2048. pasteHelper(true);
  2049. }
  2050. // Pastes from primary (is_primary == true) or clipboard (is_primary == false)
  2051. void LLTextEditor::pasteHelper(bool is_primary)
  2052. {
  2053. bool can_paste_it;
  2054. if (is_primary)
  2055. {
  2056. can_paste_it = canPastePrimary();
  2057. }
  2058. else
  2059. {
  2060. can_paste_it = canPaste();
  2061. }
  2062. if (!can_paste_it)
  2063. {
  2064. return;
  2065. }
  2066. LLWString paste;
  2067. if (is_primary)
  2068. {
  2069. paste = gClipboard.getPastePrimaryWString();
  2070. }
  2071. else
  2072. {
  2073. paste = gClipboard.getPasteWString();
  2074. }
  2075. if (paste.empty())
  2076. {
  2077. return;
  2078. }
  2079. // Delete any selected characters (the paste replaces them)
  2080. if (!is_primary && hasSelection())
  2081. {
  2082. deleteSelection(true);
  2083. }
  2084. // Clean up string (replace tabs and remove characters that our fonts do
  2085. // not support).
  2086. LLWString clean_string(paste);
  2087. LLWStringUtil::replaceTabsWithSpaces(clean_string, SPACES_PER_TAB);
  2088. if (mAllowEmbeddedItems)
  2089. {
  2090. constexpr llwchar LF = 10;
  2091. S32 len = clean_string.length();
  2092. for (S32 i = 0; i < len; ++i)
  2093. {
  2094. llwchar wc = clean_string[i];
  2095. if (wc < LLFontFreetype::FIRST_CHAR && wc != LF)
  2096. {
  2097. clean_string[i] = LL_UNKNOWN_CHAR;
  2098. }
  2099. else if (wc >= FIRST_EMBEDDED_CHAR && wc <= LAST_EMBEDDED_CHAR)
  2100. {
  2101. clean_string[i] = pasteEmbeddedItem(wc);
  2102. }
  2103. }
  2104. }
  2105. // Insert the new text into the existing text.
  2106. setCursorPos(mCursorPos + insert(mCursorPos, clean_string, false));
  2107. deselect();
  2108. needsReflow();
  2109. if (mKeystrokeCallback)
  2110. {
  2111. mKeystrokeCallback(this, mKeystrokeData);
  2112. }
  2113. // Force spell-check update:
  2114. mKeystrokeTimer.reset();
  2115. mPrevSpelledText.erase();
  2116. }
  2117. // Copies selection to primary
  2118. void LLTextEditor::copyPrimary()
  2119. {
  2120. if (!canCopy())
  2121. {
  2122. return;
  2123. }
  2124. S32 left_pos = llmin(mSelectionStart, mSelectionEnd);
  2125. S32 length = abs(mSelectionStart - mSelectionEnd);
  2126. gClipboard.copyFromPrimarySubstring(mWText, left_pos, length);
  2127. // Force spell-check update:
  2128. mKeystrokeTimer.reset();
  2129. mPrevSpelledText.erase();
  2130. }
  2131. bool LLTextEditor::canPastePrimary() const
  2132. {
  2133. return !mReadOnly && gClipboard.canPastePrimaryString();
  2134. }
  2135. void LLTextEditor::updatePrimary()
  2136. {
  2137. if (canCopy())
  2138. {
  2139. copyPrimary();
  2140. }
  2141. }
  2142. bool LLTextEditor::handleControlKey(KEY key, MASK mask)
  2143. {
  2144. bool handled = false;
  2145. if (mask & MASK_CONTROL)
  2146. {
  2147. handled = true;
  2148. switch (key)
  2149. {
  2150. case KEY_HOME:
  2151. if (mask & MASK_SHIFT)
  2152. {
  2153. startSelection();
  2154. mCursorPos = 0;
  2155. mSelectionEnd = mCursorPos;
  2156. }
  2157. else
  2158. {
  2159. // Ctrl-Home, Ctrl-Left, Ctrl-Right, Ctrl-Down
  2160. // all move the cursor as if clicking, so should deselect.
  2161. deselect();
  2162. setCursorPos(0);
  2163. }
  2164. break;
  2165. case KEY_END:
  2166. {
  2167. if (mask & MASK_SHIFT)
  2168. {
  2169. startSelection();
  2170. }
  2171. else
  2172. {
  2173. // Ctrl-Home, Ctrl-Left, Ctrl-Right, Ctrl-Down
  2174. // all move the cursor as if clicking, so should deselect.
  2175. deselect();
  2176. }
  2177. endOfDoc();
  2178. if (mask & MASK_SHIFT)
  2179. {
  2180. mSelectionEnd = mCursorPos;
  2181. }
  2182. break;
  2183. }
  2184. case KEY_RIGHT:
  2185. if (mCursorPos < getLength())
  2186. {
  2187. // Ctrl-Home, Ctrl-Left, Ctrl-Right, Ctrl-Down
  2188. // all move the cursor as if clicking, so should deselect.
  2189. deselect();
  2190. setCursorPos(nextWordPos(mCursorPos + 1));
  2191. }
  2192. break;
  2193. case KEY_LEFT:
  2194. if (mCursorPos > 0)
  2195. {
  2196. // Ctrl-Home, Ctrl-Left, Ctrl-Right, Ctrl-Down
  2197. // all move the cursor as if clicking, so should deselect.
  2198. deselect();
  2199. setCursorPos(prevWordPos(mCursorPos - 1));
  2200. }
  2201. break;
  2202. default:
  2203. handled = false;
  2204. }
  2205. }
  2206. if (handled)
  2207. {
  2208. updatePrimary();
  2209. }
  2210. return handled;
  2211. }
  2212. bool LLTextEditor::handleEditKey(KEY key, MASK mask)
  2213. {
  2214. bool handled = false;
  2215. // Standard edit keys (Ctrl-X, Delete, etc,) are handled here instead of
  2216. // routed by the menu system.
  2217. if (KEY_DELETE == key)
  2218. {
  2219. if (canDoDelete())
  2220. {
  2221. doDelete();
  2222. }
  2223. else
  2224. {
  2225. reportBadKeystroke();
  2226. }
  2227. handled = true;
  2228. }
  2229. else if (MASK_CONTROL & mask)
  2230. {
  2231. if ('C' == key)
  2232. {
  2233. if (canCopy())
  2234. {
  2235. copy();
  2236. }
  2237. else
  2238. {
  2239. reportBadKeystroke();
  2240. }
  2241. handled = true;
  2242. }
  2243. else if ('V' == key)
  2244. {
  2245. if (canPaste())
  2246. {
  2247. paste();
  2248. }
  2249. else
  2250. {
  2251. reportBadKeystroke();
  2252. }
  2253. handled = true;
  2254. }
  2255. else if ('X' == key)
  2256. {
  2257. if (canCut())
  2258. {
  2259. cut();
  2260. }
  2261. else
  2262. {
  2263. reportBadKeystroke();
  2264. }
  2265. handled = true;
  2266. }
  2267. }
  2268. if (handled)
  2269. {
  2270. // Take selection to 'primary' clipboard
  2271. updatePrimary();
  2272. }
  2273. return handled;
  2274. }
  2275. bool LLTextEditor::handleSpecialKey(KEY key, MASK mask, bool* return_key_hit)
  2276. {
  2277. *return_key_hit = false;
  2278. bool handled = true;
  2279. switch (key)
  2280. {
  2281. case KEY_INSERT:
  2282. if (mask == MASK_NONE && gKeyboardp)
  2283. {
  2284. gKeyboardp->toggleInsertMode();
  2285. }
  2286. break;
  2287. case KEY_BACKSPACE:
  2288. if (hasSelection())
  2289. {
  2290. deleteSelection(false);
  2291. }
  2292. else if (0 < mCursorPos)
  2293. {
  2294. removeCharOrTab();
  2295. }
  2296. else
  2297. {
  2298. reportBadKeystroke();
  2299. }
  2300. break;
  2301. case KEY_RETURN:
  2302. if (mask == MASK_NONE)
  2303. {
  2304. if (hasSelection())
  2305. {
  2306. deleteSelection(false);
  2307. }
  2308. autoIndent(); // *TODO: make this optional
  2309. }
  2310. else
  2311. {
  2312. handled = false;
  2313. break;
  2314. }
  2315. break;
  2316. case KEY_TAB:
  2317. if (mask & (MASK_CONTROL | MASK_ALT))
  2318. {
  2319. handled = false;
  2320. break;
  2321. }
  2322. if (hasSelection() && selectionContainsLineBreaks())
  2323. {
  2324. indentSelectedLines((mask & MASK_SHIFT) ? -SPACES_PER_TAB
  2325. : SPACES_PER_TAB);
  2326. }
  2327. else
  2328. {
  2329. if (hasSelection())
  2330. {
  2331. deleteSelection(false);
  2332. }
  2333. S32 line, offset;
  2334. getLineAndOffset(mCursorPos, &line, &offset);
  2335. S32 spaces_needed = SPACES_PER_TAB - (offset % SPACES_PER_TAB);
  2336. for (S32 i = 0; i < spaces_needed; ++i)
  2337. {
  2338. addChar(' ');
  2339. }
  2340. }
  2341. break;
  2342. default:
  2343. handled = false;
  2344. }
  2345. return handled;
  2346. }
  2347. void LLTextEditor::unindentLineBeforeCloseBrace()
  2348. {
  2349. if (mCursorPos >= 1)
  2350. {
  2351. const LLWString& text = mWText;
  2352. if (' ' == text[mCursorPos - 1])
  2353. {
  2354. removeCharOrTab();
  2355. }
  2356. }
  2357. }
  2358. bool LLTextEditor::handleKeyHere(KEY key, MASK mask)
  2359. {
  2360. bool handled = false;
  2361. bool selection_modified = false;
  2362. bool return_key_hit = false;
  2363. bool text_may_have_changed = true;
  2364. // Key presses are not being passed to the Popup menu.
  2365. // A proper fix is non-trivial so instead just close the menu.
  2366. LLMenuGL* menu = getContextMenu();
  2367. if (menu && menu->isOpen())
  2368. {
  2369. LLMenuGL::sMenuContainer->hideMenus();
  2370. }
  2371. if (gFocusMgr.getKeyboardFocus() == this)
  2372. {
  2373. // Special case for TAB. If want to move to next field, report not
  2374. // handled and let the parent take care of field movement.
  2375. if (KEY_TAB == key && mTabsToNextField)
  2376. {
  2377. return false;
  2378. }
  2379. handled = handleNavigationKey(key, mask);
  2380. if (handled)
  2381. {
  2382. text_may_have_changed = false;
  2383. }
  2384. if (!handled)
  2385. {
  2386. handled = handleSelectionKey(key, mask);
  2387. if (handled)
  2388. {
  2389. selection_modified = true;
  2390. }
  2391. }
  2392. if (!handled)
  2393. {
  2394. handled = handleControlKey(key, mask);
  2395. if (handled)
  2396. {
  2397. selection_modified = true;
  2398. }
  2399. }
  2400. if (!handled && mHandleEditKeysDirectly)
  2401. {
  2402. handled = handleEditKey(key, mask);
  2403. if (handled)
  2404. {
  2405. selection_modified = true;
  2406. text_may_have_changed = true;
  2407. }
  2408. }
  2409. // Key presses are not being passed to the Popup menu.
  2410. // A proper fix is non-trivial so instead just close the menu.
  2411. LLMenuGL* menu = getContextMenu();
  2412. if (menu && menu->isOpen())
  2413. {
  2414. LLMenuGL::sMenuContainer->hideMenus();
  2415. }
  2416. // Handle most keys only if the text editor is writeable.
  2417. if (!mReadOnly)
  2418. {
  2419. if (!handled && mOnHandleKeyCallback)
  2420. {
  2421. handled = mOnHandleKeyCallback(key, mask, this,
  2422. mOnHandleKeyData);
  2423. }
  2424. if (!handled)
  2425. {
  2426. handled = handleSpecialKey(key, mask, &return_key_hit);
  2427. if (handled)
  2428. {
  2429. selection_modified = true;
  2430. text_may_have_changed = true;
  2431. }
  2432. }
  2433. }
  2434. if (handled)
  2435. {
  2436. resetKeystrokeTimer();
  2437. // Most keystrokes will make the selection box go away, but not all will.
  2438. if (!selection_modified && KEY_SHIFT != key && KEY_TAB != key &&
  2439. KEY_CONTROL != key && KEY_ALT != key && KEY_CAPSLOCK)
  2440. {
  2441. deselect();
  2442. }
  2443. if (text_may_have_changed)
  2444. {
  2445. needsReflow();
  2446. }
  2447. needsScroll();
  2448. }
  2449. }
  2450. return handled;
  2451. }
  2452. bool LLTextEditor::handleUnicodeCharHere(llwchar uni_char)
  2453. {
  2454. if (uni_char < 0x20 || uni_char == 0x7F) // Control character or DEL
  2455. {
  2456. return false;
  2457. }
  2458. bool handled = false;
  2459. if (gFocusMgr.getKeyboardFocus() == this)
  2460. {
  2461. // Handle most keys only if the text editor is writeable.
  2462. if (!mReadOnly)
  2463. {
  2464. if ('}' == uni_char)
  2465. {
  2466. unindentLineBeforeCloseBrace();
  2467. }
  2468. // TODO: KLW Add auto show of tool tip on (
  2469. addChar(uni_char);
  2470. // Keys that add characters temporarily hide the cursor
  2471. gWindowp->hideCursorUntilMouseMove();
  2472. handled = true;
  2473. }
  2474. if (handled)
  2475. {
  2476. resetKeystrokeTimer();
  2477. // Most keystrokes will make the selection box go away, but not all
  2478. // will.
  2479. deselect();
  2480. needsReflow();
  2481. }
  2482. }
  2483. return handled;
  2484. }
  2485. //virtual
  2486. bool LLTextEditor::canDoDelete() const
  2487. {
  2488. return !mReadOnly && (hasSelection() || (mCursorPos < getLength()));
  2489. }
  2490. void LLTextEditor::doDelete()
  2491. {
  2492. if (!canDoDelete())
  2493. {
  2494. return;
  2495. }
  2496. if (hasSelection())
  2497. {
  2498. deleteSelection(false);
  2499. }
  2500. else if (mCursorPos < getLength())
  2501. {
  2502. S32 i;
  2503. S32 chars_to_remove = 1;
  2504. const LLWString& text = mWText;
  2505. if (text[ mCursorPos ] == ' ' &&
  2506. mCursorPos + SPACES_PER_TAB < getLength())
  2507. {
  2508. // Try to remove a full tab's worth of spaces
  2509. S32 line, offset;
  2510. getLineAndOffset(mCursorPos, &line, &offset);
  2511. chars_to_remove = SPACES_PER_TAB - (offset % SPACES_PER_TAB);
  2512. if (chars_to_remove == 0)
  2513. {
  2514. chars_to_remove = SPACES_PER_TAB;
  2515. }
  2516. for (i = 0; i < chars_to_remove; ++i)
  2517. {
  2518. if (text[mCursorPos + i] != ' ')
  2519. {
  2520. chars_to_remove = 1;
  2521. break;
  2522. }
  2523. }
  2524. }
  2525. for (i = 0; i < chars_to_remove; ++i)
  2526. {
  2527. setCursorPos(mCursorPos + 1);
  2528. removeChar();
  2529. }
  2530. }
  2531. needsReflow();
  2532. if (mKeystrokeCallback)
  2533. {
  2534. mKeystrokeCallback(this, mKeystrokeData);
  2535. }
  2536. // Force spell-check update:
  2537. mKeystrokeTimer.reset();
  2538. mPrevSpelledText.erase();
  2539. }
  2540. void LLTextEditor::blockUndo()
  2541. {
  2542. mBaseDocIsPristine = false;
  2543. mLastCmd = NULL;
  2544. std::for_each(mUndoStack.begin(), mUndoStack.end(), DeletePointer());
  2545. mUndoStack.clear();
  2546. // Force spell-check update:
  2547. mKeystrokeTimer.reset();
  2548. mPrevSpelledText.erase();
  2549. }
  2550. //virtual
  2551. bool LLTextEditor::canUndo() const
  2552. {
  2553. return !mReadOnly && mLastCmd != NULL;
  2554. }
  2555. //virtual
  2556. void LLTextEditor::undo()
  2557. {
  2558. if (!canUndo())
  2559. {
  2560. return;
  2561. }
  2562. S32 pos = 0;
  2563. deselect();
  2564. do
  2565. {
  2566. pos = mLastCmd->undo(this);
  2567. undo_stack_t::iterator iter = std::find(mUndoStack.begin(),
  2568. mUndoStack.end(), mLastCmd);
  2569. if (iter != mUndoStack.end())
  2570. {
  2571. ++iter;
  2572. }
  2573. if (iter != mUndoStack.end())
  2574. {
  2575. mLastCmd = *iter;
  2576. }
  2577. else
  2578. {
  2579. mLastCmd = NULL;
  2580. }
  2581. }
  2582. while (mLastCmd && mLastCmd->groupWithNext());
  2583. setCursorPos(pos);
  2584. needsReflow();
  2585. // Force spell-check update:
  2586. mKeystrokeTimer.reset();
  2587. mPrevSpelledText.erase();
  2588. }
  2589. //virtual
  2590. bool LLTextEditor::canRedo() const
  2591. {
  2592. return !mReadOnly && mUndoStack.size() > 0 &&
  2593. mLastCmd != mUndoStack.front();
  2594. }
  2595. //virtual
  2596. void LLTextEditor::redo()
  2597. {
  2598. if (!canRedo())
  2599. {
  2600. return;
  2601. }
  2602. S32 pos = 0;
  2603. deselect();
  2604. do
  2605. {
  2606. if (!mLastCmd)
  2607. {
  2608. mLastCmd = mUndoStack.back();
  2609. }
  2610. else
  2611. {
  2612. undo_stack_t::iterator iter = std::find(mUndoStack.begin(),
  2613. mUndoStack.end(),
  2614. mLastCmd);
  2615. if (iter != mUndoStack.begin())
  2616. {
  2617. mLastCmd = *(--iter);
  2618. }
  2619. else
  2620. {
  2621. mLastCmd = NULL;
  2622. }
  2623. }
  2624. if (mLastCmd)
  2625. {
  2626. pos = mLastCmd->redo(this);
  2627. }
  2628. }
  2629. while (mLastCmd && mLastCmd->groupWithNext() &&
  2630. mLastCmd != mUndoStack.front());
  2631. setCursorPos(pos);
  2632. needsReflow();
  2633. // Force spell-check update:
  2634. mKeystrokeTimer.reset();
  2635. mPrevSpelledText.erase();
  2636. }
  2637. void LLTextEditor::onFocusReceived()
  2638. {
  2639. grabMenuHandler();
  2640. LLUICtrl::onFocusReceived();
  2641. updateAllowingLanguageInput();
  2642. }
  2643. // virtual, from LLView
  2644. void LLTextEditor::onFocusLost()
  2645. {
  2646. updateAllowingLanguageInput();
  2647. // Route menu back to the default
  2648. releaseMenuHandler();
  2649. if (mCommitOnFocusLost)
  2650. {
  2651. onCommit();
  2652. }
  2653. // Make sure cursor is shown again
  2654. gWindowp->showCursorFromMouseMove();
  2655. LLUICtrl::onFocusLost();
  2656. }
  2657. void LLTextEditor::setEnabled(bool enabled)
  2658. {
  2659. // just treat enabled as read-only flag
  2660. bool read_only = !enabled;
  2661. if (read_only != mReadOnly)
  2662. {
  2663. mReadOnly = read_only;
  2664. updateSegments();
  2665. updateAllowingLanguageInput();
  2666. }
  2667. }
  2668. void LLTextEditor::drawBackground()
  2669. {
  2670. S32 left = 0;
  2671. S32 top = getRect().getHeight();
  2672. S32 right = getRect().getWidth();
  2673. S32 bottom = 0;
  2674. LLColor4 bg_color =
  2675. mReadOnly ? mReadOnlyBgColor
  2676. : gFocusMgr.getKeyboardFocus() == this ? mFocusBgColor
  2677. : mWriteableBgColor;
  2678. if (mShowLineNumbers)
  2679. {
  2680. gl_rect_2d(left, top, UI_TEXTEDITOR_LINE_NUMBER_MARGIN, bottom,
  2681. mReadOnlyBgColor); // line number area always read-only
  2682. gl_rect_2d(UI_TEXTEDITOR_LINE_NUMBER_MARGIN, top, right, bottom,
  2683. bg_color); // body text area to the right of line numbers
  2684. gl_rect_2d(UI_TEXTEDITOR_LINE_NUMBER_MARGIN, top,
  2685. UI_TEXTEDITOR_LINE_NUMBER_MARGIN - 1, bottom,
  2686. LLColor4::grey3); // separator
  2687. }
  2688. else
  2689. {
  2690. gl_rect_2d(left, top, right, bottom, bg_color); // body text area
  2691. }
  2692. LLView::draw();
  2693. }
  2694. // Draws the black box behind the selected text
  2695. void LLTextEditor::drawSelectionBackground()
  2696. {
  2697. // Draw selection even if we do not have keyboard focus for search/replace
  2698. if (hasSelection())
  2699. {
  2700. const LLWString& text = mWText;
  2701. const S32 text_len = getLength();
  2702. std::queue<S32> line_endings;
  2703. S32 line_height = ll_roundp(mGLFont->getLineHeight());
  2704. S32 selection_left = llmin(mSelectionStart, mSelectionEnd);
  2705. S32 selection_right = llmax(mSelectionStart, mSelectionEnd);
  2706. S32 selection_left_x = mTextRect.mLeft;
  2707. S32 selection_left_y = mTextRect.mTop - line_height;
  2708. S32 selection_right_x = mTextRect.mRight;
  2709. S32 selection_right_y = mTextRect.mBottom;
  2710. bool selection_right_visible = false;
  2711. // Skip through the lines we are not drawing.
  2712. S32 cur_line = mScrollbar->getDocPos();
  2713. S32 left_line_num = cur_line;
  2714. S32 num_lines = getLineCount();
  2715. S32 line_start = -1;
  2716. if (cur_line >= num_lines)
  2717. {
  2718. return;
  2719. }
  2720. line_start = getLineStart(cur_line);
  2721. S32 left_visible_pos = line_start;
  2722. S32 right_visible_pos = line_start;
  2723. S32 text_y = mTextRect.mTop - line_height;
  2724. // Find the coordinates of the selected area
  2725. while (cur_line < num_lines)
  2726. {
  2727. S32 next_line = -1;
  2728. S32 line_end = text_len;
  2729. if (cur_line + 1 < num_lines)
  2730. {
  2731. next_line = getLineStart(cur_line + 1);
  2732. line_end = next_line;
  2733. line_end = line_end - line_start == 0 ||
  2734. text[next_line - 1] == '\n' ||
  2735. text[next_line - 1] == '\0' ||
  2736. text[next_line - 1] == ' ' ||
  2737. text[next_line - 1] == '\t' ? next_line - 1
  2738. : next_line;
  2739. }
  2740. const llwchar* line = text.c_str() + line_start;
  2741. if (line_start <= selection_left && selection_left <= line_end)
  2742. {
  2743. left_line_num = cur_line;
  2744. selection_left_x =
  2745. mTextRect.mLeft +
  2746. mGLFont->getWidth(line, 0, selection_left - line_start,
  2747. mAllowEmbeddedItems);
  2748. selection_left_y = text_y;
  2749. }
  2750. if (line_start <= selection_right && selection_right <= line_end)
  2751. {
  2752. selection_right_visible = true;
  2753. selection_right_x =
  2754. mTextRect.mLeft +
  2755. mGLFont->getWidth(line, 0, selection_right - line_start,
  2756. mAllowEmbeddedItems);
  2757. #if 0
  2758. if (selection_right == line_end)
  2759. {
  2760. // Add empty space for "newline"
  2761. selection_right_x += mGLFont->getWidth("n");
  2762. }
  2763. #endif
  2764. selection_right_y = text_y;
  2765. }
  2766. // If selection spans end of current line...
  2767. if (selection_left <= line_end && line_end < selection_right &&
  2768. selection_left != selection_right)
  2769. {
  2770. // Extend selection slightly beyond end of line to indicate
  2771. // selection of newline character (use "n" character to
  2772. // determine width)
  2773. const LLWString nstr(utf8str_to_wstring("n"));
  2774. line_endings.push(mTextRect.mLeft +
  2775. mGLFont->getWidth(line, 0,
  2776. line_end - line_start,
  2777. mAllowEmbeddedItems) +
  2778. mGLFont->getWidth(nstr.c_str()));
  2779. }
  2780. // Move down one line
  2781. text_y -= line_height;
  2782. right_visible_pos = line_end;
  2783. line_start = next_line;
  2784. ++cur_line;
  2785. if (selection_right_visible)
  2786. {
  2787. break;
  2788. }
  2789. }
  2790. // Draw the selection box (we are using a box instead of reversing the
  2791. // colors on the selected text).
  2792. bool selection_visible = left_visible_pos <= selection_right &&
  2793. selection_left <= right_visible_pos;
  2794. if (selection_visible)
  2795. {
  2796. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  2797. const LLColor4& color = mReadOnly ? mReadOnlyBgColor
  2798. : mWriteableBgColor;
  2799. F32 alpha = hasFocus() ? 1.f : 0.5f;
  2800. gGL.color4f(1.f - color.mV[0], 1.f - color.mV[1],
  2801. 1.f - color.mV[2], alpha);
  2802. S32 margin_offset =
  2803. mShowLineNumbers ? UI_TEXTEDITOR_LINE_NUMBER_MARGIN : 0;
  2804. if (selection_left_y == selection_right_y)
  2805. {
  2806. // Draw from selection start to selection end
  2807. gl_rect_2d(selection_left_x + margin_offset,
  2808. selection_left_y + line_height + 1,
  2809. selection_right_x + margin_offset,
  2810. selection_right_y);
  2811. }
  2812. else
  2813. {
  2814. // Draw from selection start to the end of the first line
  2815. if (mTextRect.mRight == selection_left_x)
  2816. {
  2817. selection_left_x -= CURSOR_THICKNESS;
  2818. }
  2819. S32 line_end = line_endings.front();
  2820. line_endings.pop();
  2821. gl_rect_2d(selection_left_x + margin_offset,
  2822. selection_left_y + line_height + 1,
  2823. line_end + margin_offset, selection_left_y);
  2824. S32 line_num = left_line_num + 1;
  2825. while (line_endings.size())
  2826. {
  2827. S32 vert_offset = -(line_num - left_line_num) * line_height;
  2828. // Draw the block between the two lines
  2829. gl_rect_2d(mTextRect.mLeft + margin_offset,
  2830. selection_left_y + vert_offset + line_height + 1,
  2831. line_endings.front() + margin_offset,
  2832. selection_left_y + vert_offset);
  2833. line_endings.pop();
  2834. ++line_num;
  2835. }
  2836. // Draw from the start of the last line to selection end
  2837. if (mTextRect.mLeft == selection_right_x)
  2838. {
  2839. selection_right_x += CURSOR_THICKNESS;
  2840. }
  2841. gl_rect_2d(mTextRect.mLeft + margin_offset,
  2842. selection_right_y + line_height + 1,
  2843. selection_right_x + margin_offset,
  2844. selection_right_y);
  2845. }
  2846. }
  2847. }
  2848. }
  2849. void LLTextEditor::drawMisspelled()
  2850. {
  2851. LL_FAST_TIMER(FTM_RENDER_SPELLCHECK);
  2852. // Do not bother checking if the text did not change in a while, and
  2853. // fire a spell checking every second while typing only when the text
  2854. // is under 1024 characters large.
  2855. S32 elapsed = (S32)mSpellTimer.getElapsedTimeF32();
  2856. S32 keystroke = (S32)mKeystrokeTimer.getElapsedTimeF32();
  2857. if (keystroke < 2 &&
  2858. ((getLength() < 1024 && (elapsed & 1)) || keystroke > 0))
  2859. {
  2860. S32 new_start_spell = getLineStart(mScrollbar->getDocPos());
  2861. S32 new_end_spell =
  2862. getLineStart(mScrollbar->getDocPos() + 1 +
  2863. mScrollbar->getDocSize()-mScrollbar->getDocPosMax());
  2864. if (mScrollbar->getDocPos() == mScrollbar->getDocPosMax())
  2865. {
  2866. new_end_spell = (S32)mWText.length();
  2867. }
  2868. if (new_start_spell != mSpellCheckStart ||
  2869. new_end_spell != mSpellCheckEnd || isSpellDirty())
  2870. {
  2871. mSpellCheckStart = new_start_spell;
  2872. mSpellCheckEnd = new_end_spell;
  2873. resetSpellDirty();
  2874. mMisspellLocations = getMisspelledWordsPositions();
  2875. }
  2876. }
  2877. if (mShowMisspelled)
  2878. {
  2879. const LLWString& text = mWText;
  2880. const S32 text_len = getLength();
  2881. const S32 num_lines = getLineCount();
  2882. const F32 line_height = mGLFont->getLineHeight();
  2883. const S32 start_search_pos = mScrollbar->getDocPos();
  2884. // Skip through the lines we are not drawing.
  2885. if (start_search_pos >= num_lines)
  2886. {
  2887. return;
  2888. }
  2889. const S32 start_line_start = getLineStart(start_search_pos);
  2890. const F32 start_text_y = (F32)mTextRect.mTop - line_height;
  2891. const S32 misspells = (S32)mMisspellLocations.size();
  2892. bool found_first_visible = false;
  2893. bool visible;
  2894. for (S32 i = 0; i < misspells; ++i)
  2895. {
  2896. S32 wstart = mMisspellLocations[i++];
  2897. S32 wend = mMisspellLocations[i];
  2898. S32 search_pos = start_search_pos;
  2899. S32 line_start = start_line_start;
  2900. F32 text_y = start_text_y;
  2901. F32 word_left = 0.f;
  2902. F32 word_right = 0.f;
  2903. S32 line_end = 0;
  2904. // Determine if the word is visible and if so at what coordinates
  2905. while (mTextRect.mBottom <= ll_round(text_y) &&
  2906. search_pos < num_lines)
  2907. {
  2908. line_end = text_len + 1;
  2909. S32 next_line = -1;
  2910. visible = false;
  2911. if (search_pos + 1 < num_lines)
  2912. {
  2913. next_line = getLineStart(search_pos + 1);
  2914. line_end = next_line - 1;
  2915. }
  2916. const llwchar* line = text.c_str() + line_start;
  2917. // Find the cursor and selection bounds
  2918. if (line_start <= wstart && wend <= line_end)
  2919. {
  2920. visible = true;
  2921. word_left = (F32)mTextRect.mLeft - 1.f +
  2922. mGLFont->getWidthF32(line, 0,
  2923. wstart - line_start,
  2924. mAllowEmbeddedItems);
  2925. word_right = (F32)mTextRect.mLeft + 1.f +
  2926. mGLFont->getWidthF32(line, 0,
  2927. wend - line_start,
  2928. mAllowEmbeddedItems);
  2929. // Draw the zig zag line
  2930. gGL.color4ub(255, 0, 0, 200);
  2931. while (word_left < word_right)
  2932. {
  2933. gl_line_2d((S32)word_left, (S32)text_y - 2,
  2934. (S32)word_left + 3, (S32)text_y + 1);
  2935. gl_line_2d((S32)word_left + 3, (S32)text_y + 1,
  2936. (S32)word_left + 6, (S32)text_y - 2);
  2937. word_left += 6;
  2938. }
  2939. break;
  2940. }
  2941. if (visible && !found_first_visible)
  2942. {
  2943. found_first_visible = true;
  2944. }
  2945. else if (!visible && found_first_visible)
  2946. {
  2947. // We found the last visible misspelled word. Stop now.
  2948. return;
  2949. }
  2950. // move down one line
  2951. text_y -= line_height;
  2952. line_start = next_line;
  2953. ++search_pos;
  2954. }
  2955. if (mShowLineNumbers)
  2956. {
  2957. word_left += UI_TEXTEDITOR_LINE_NUMBER_MARGIN;
  2958. word_right += UI_TEXTEDITOR_LINE_NUMBER_MARGIN;
  2959. }
  2960. }
  2961. }
  2962. }
  2963. void LLTextEditor::drawCursor()
  2964. {
  2965. if (gFocusMgr.getKeyboardFocus() == this && gShowTextEditCursor &&
  2966. !mReadOnly)
  2967. {
  2968. const LLWString& text = mWText;
  2969. const S32 text_len = getLength();
  2970. // Skip through the lines we are not drawing.
  2971. S32 cur_pos = mScrollbar->getDocPos();
  2972. S32 num_lines = getLineCount();
  2973. if (cur_pos >= num_lines)
  2974. {
  2975. return;
  2976. }
  2977. S32 line_start = getLineStart(cur_pos);
  2978. F32 line_height = mGLFont->getLineHeight();
  2979. F32 text_y = (F32)(mTextRect.mTop) - line_height;
  2980. F32 cursor_left = 0.f;
  2981. F32 next_char_left = 0.f;
  2982. F32 cursor_bottom = 0.f;
  2983. bool cursor_visible = false;
  2984. S32 line_end = 0;
  2985. // Determine if the cursor is visible and if so at what coordinates
  2986. while (mTextRect.mBottom <= ll_round(text_y) && cur_pos < num_lines)
  2987. {
  2988. line_end = text_len + 1;
  2989. S32 next_line = -1;
  2990. if (cur_pos + 1 < num_lines)
  2991. {
  2992. next_line = getLineStart(cur_pos + 1);
  2993. line_end = next_line - 1;
  2994. }
  2995. const llwchar* line = text.c_str() + line_start;
  2996. // Find the cursor and selection bounds
  2997. if (line_start <= mCursorPos && mCursorPos <= line_end)
  2998. {
  2999. cursor_visible = true;
  3000. next_char_left = (F32)mTextRect.mLeft +
  3001. mGLFont->getWidthF32(line, 0,
  3002. mCursorPos - line_start,
  3003. mAllowEmbeddedItems);
  3004. cursor_left = next_char_left - 1.f;
  3005. cursor_bottom = text_y;
  3006. break;
  3007. }
  3008. // Move down one line
  3009. text_y -= line_height;
  3010. line_start = next_line;
  3011. ++cur_pos;
  3012. }
  3013. if (mShowLineNumbers)
  3014. {
  3015. cursor_left += UI_TEXTEDITOR_LINE_NUMBER_MARGIN;
  3016. }
  3017. // Draw the cursor
  3018. if (cursor_visible)
  3019. {
  3020. // Flash the cursor every half second starting a fixed time after
  3021. // the last keystroke
  3022. F32 elapsed = mKeystrokeTimer.getElapsedTimeF32();
  3023. if (elapsed < CURSOR_FLASH_DELAY || (S32(elapsed * 2) & 1))
  3024. {
  3025. F32 cursor_top = cursor_bottom + line_height + 1.f;
  3026. F32 cursor_right = cursor_left + (F32)CURSOR_THICKNESS;
  3027. if (gKeyboardp &&
  3028. gKeyboardp->getInsertMode() == LL_KIM_OVERWRITE &&
  3029. !hasSelection())
  3030. {
  3031. cursor_left += CURSOR_THICKNESS;
  3032. const LLWString space(utf8str_to_wstring(" "));
  3033. F32 spacew = mGLFont->getWidthF32(space.c_str());
  3034. if (mCursorPos == line_end)
  3035. {
  3036. cursor_right = cursor_left + spacew;
  3037. }
  3038. else
  3039. {
  3040. F32 width = mGLFont->getWidthF32(text.c_str(),
  3041. mCursorPos, 1,
  3042. mAllowEmbeddedItems);
  3043. cursor_right = cursor_left + llmax(spacew, width);
  3044. }
  3045. }
  3046. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  3047. gGL.color4fv(mCursorColor.mV);
  3048. gl_rect_2d(llfloor(cursor_left), llfloor(cursor_top),
  3049. llfloor(cursor_right), llfloor(cursor_bottom));
  3050. if (gKeyboardp &&
  3051. gKeyboardp->getInsertMode() == LL_KIM_OVERWRITE &&
  3052. !hasSelection() && text[mCursorPos] != '\n')
  3053. {
  3054. const LLTextSegment* segmentp =
  3055. getSegmentAtOffset(mCursorPos);
  3056. LLColor4 text_color;
  3057. if (segmentp)
  3058. {
  3059. text_color = segmentp->getColor();
  3060. }
  3061. else if (mReadOnly)
  3062. {
  3063. text_color = mReadOnlyFgColor;
  3064. }
  3065. else
  3066. {
  3067. text_color = mFgColor;
  3068. }
  3069. mGLFont->render(text, mCursorPos, next_char_left,
  3070. cursor_bottom + line_height,
  3071. LLColor4(1.f - text_color.mV[VRED],
  3072. 1.f - text_color.mV[VGREEN],
  3073. 1.f - text_color.mV[VBLUE], 1.f),
  3074. LLFontGL::LEFT, LLFontGL::TOP,
  3075. LLFontGL::NORMAL, 1);
  3076. }
  3077. // Make sure the IME is in the right place
  3078. LLRect screen_pos = getScreenRect();
  3079. LLCoordGL ime_pos(screen_pos.mLeft + llfloor(cursor_left),
  3080. screen_pos.mBottom + llfloor(cursor_top));
  3081. ime_pos.mX = (S32) (ime_pos.mX * LLUI::sGLScaleFactor.mV[VX]);
  3082. ime_pos.mY = (S32) (ime_pos.mY * LLUI::sGLScaleFactor.mV[VY]);
  3083. gWindowp->setLanguageTextInput(ime_pos);
  3084. }
  3085. }
  3086. }
  3087. }
  3088. void LLTextEditor::drawPreeditMarker()
  3089. {
  3090. if (!hasPreeditString())
  3091. {
  3092. return;
  3093. }
  3094. const llwchar* text = mWText.c_str();
  3095. const S32 text_len = getLength();
  3096. const S32 num_lines = getLineCount();
  3097. S32 cur_line = mScrollbar->getDocPos();
  3098. if (cur_line >= num_lines)
  3099. {
  3100. return;
  3101. }
  3102. const S32 line_height = ll_roundp(mGLFont->getLineHeight());
  3103. S32 line_start = getLineStart(cur_line);
  3104. S32 line_y = mTextRect.mTop - line_height;
  3105. while (mTextRect.mBottom <= line_y && num_lines > cur_line)
  3106. {
  3107. S32 next_start = -1;
  3108. S32 line_end = text_len;
  3109. if (cur_line + 1 < num_lines)
  3110. {
  3111. next_start = getLineStart(cur_line + 1);
  3112. line_end = next_start;
  3113. }
  3114. if (text[line_end-1] == '\n')
  3115. {
  3116. --line_end;
  3117. }
  3118. // Does this line contain preedits?
  3119. if (line_start >= mPreeditPositions.back())
  3120. {
  3121. // We have passed the preedits.
  3122. break;
  3123. }
  3124. if (line_end > mPreeditPositions.front())
  3125. {
  3126. for (U32 i = 0, count = mPreeditStandouts.size(); i < count; ++i)
  3127. {
  3128. S32 left = mPreeditPositions[i];
  3129. S32 right = mPreeditPositions[i + 1];
  3130. if (right <= line_start || left >= line_end)
  3131. {
  3132. continue;
  3133. }
  3134. S32 preedit_left = mTextRect.mLeft;
  3135. if (left > line_start)
  3136. {
  3137. preedit_left += mGLFont->getWidth(text, line_start,
  3138. left - line_start,
  3139. mAllowEmbeddedItems);
  3140. }
  3141. S32 preedit_right = mTextRect.mLeft;
  3142. if (right < line_end)
  3143. {
  3144. preedit_right += mGLFont->getWidth(text, line_start,
  3145. right - line_start,
  3146. mAllowEmbeddedItems);
  3147. }
  3148. else
  3149. {
  3150. preedit_right += mGLFont->getWidth(text, line_start,
  3151. line_end - line_start,
  3152. mAllowEmbeddedItems);
  3153. }
  3154. if (mPreeditStandouts[i])
  3155. {
  3156. gl_rect_2d(preedit_left + PREEDIT_STANDOUT_GAP,
  3157. line_y + PREEDIT_STANDOUT_POSITION,
  3158. preedit_right - PREEDIT_STANDOUT_GAP - 1,
  3159. line_y + PREEDIT_STANDOUT_POSITION -
  3160. PREEDIT_STANDOUT_THICKNESS,
  3161. (mCursorColor * PREEDIT_STANDOUT_BRIGHTNESS +
  3162. mWriteableBgColor *
  3163. (1 - PREEDIT_STANDOUT_BRIGHTNESS)).setAlpha(1.f));
  3164. }
  3165. else
  3166. {
  3167. gl_rect_2d(preedit_left + PREEDIT_MARKER_GAP,
  3168. line_y + PREEDIT_MARKER_POSITION,
  3169. preedit_right - PREEDIT_MARKER_GAP - 1,
  3170. line_y + PREEDIT_MARKER_POSITION -
  3171. PREEDIT_MARKER_THICKNESS,
  3172. (mCursorColor * PREEDIT_MARKER_BRIGHTNESS +
  3173. mWriteableBgColor *
  3174. (1 - PREEDIT_MARKER_BRIGHTNESS)).setAlpha(1.f));
  3175. }
  3176. }
  3177. }
  3178. // Move down one line
  3179. line_y -= line_height;
  3180. line_start = next_start;
  3181. ++cur_line;
  3182. }
  3183. }
  3184. void LLTextEditor::drawText()
  3185. {
  3186. const LLWString& text = mWText;
  3187. const S32 text_len = getLength();
  3188. if (text_len <= 0) return;
  3189. S32 selection_left = -1;
  3190. S32 selection_right = -1;
  3191. // Draw selection even if we do not have keyboard focus for search/replace
  3192. if (hasSelection())
  3193. {
  3194. selection_left = llmin(mSelectionStart, mSelectionEnd);
  3195. selection_right = llmax(mSelectionStart, mSelectionEnd);
  3196. }
  3197. LLGLSUIDefault gls_ui;
  3198. // There are several concepts that are important for understanding the
  3199. // following drawing code.
  3200. // The document is logically a sequence of characters (stored in a
  3201. // LLWString).
  3202. // Variables below with "start" or "end" in their names refer to positions
  3203. // or offsets into it.
  3204. // Next there are two kinds of "line" variables to understand. Newline
  3205. // characters in the character sequence represent logical lines. These are
  3206. // what get numbered and so variables representing this kind of line have
  3207. // "num" in their names.
  3208. // The others represent line fragments or displayed lines which the
  3209. // scrollbar deals with.
  3210. // When the "show line numbers" property is turned on, we draw line numbers
  3211. // to the left of the beginning of each logical line and not in front of
  3212. // wrapped "continuation" display lines. -MG
  3213. // Scrollbar counts each wrap as a new line.
  3214. S32 cur_line = mScrollbar->getDocPos();
  3215. S32 num_lines = getLineCount();
  3216. if (cur_line >= num_lines) return;
  3217. S32 line_start = getLineStart(cur_line);
  3218. S32 prev_start = getLineStart(cur_line - 1);
  3219. // Does not count wraps. i.e. only counts newlines.
  3220. S32 cur_line_num = getLineForPosition(line_start);
  3221. S32 prev_line_num = getLineForPosition(prev_start);
  3222. bool cur_line_is_continuation = cur_line_num > 0 &&
  3223. cur_line_num == prev_line_num;
  3224. bool line_wraps = false;
  3225. LLTextSegment t(line_start);
  3226. segment_list_t::iterator seg_iter;
  3227. seg_iter = std::upper_bound(mSegments.begin(), mSegments.end(), &t,
  3228. LLTextSegment::compare());
  3229. if (seg_iter == mSegments.end() || (*seg_iter)->getStart() > line_start)
  3230. {
  3231. --seg_iter;
  3232. }
  3233. LLTextSegment* cur_segment = *seg_iter;
  3234. S32 line_height = ll_roundp(mGLFont->getLineHeight());
  3235. F32 text_y = (F32)(mTextRect.mTop - line_height);
  3236. while (mTextRect.mBottom <= text_y && cur_line < num_lines)
  3237. {
  3238. S32 next_start = -1;
  3239. S32 line_end = text_len;
  3240. if (cur_line + 1 < num_lines)
  3241. {
  3242. next_start = getLineStart(cur_line + 1);
  3243. line_end = next_start;
  3244. }
  3245. line_wraps = text[line_end - 1] != '\n';
  3246. if (!line_wraps)
  3247. {
  3248. --line_end; // do not attempt to draw the newline char.
  3249. }
  3250. F32 text_start = (F32)mTextRect.mLeft;
  3251. F32 text_x = text_start +
  3252. (mShowLineNumbers ? UI_TEXTEDITOR_LINE_NUMBER_MARGIN : 0);
  3253. // Draw the line numbers
  3254. if (mShowLineNumbers && !cur_line_is_continuation)
  3255. {
  3256. const LLFontGL* num_font = LLFontGL::getFontMonospace();
  3257. F32 y_top = text_y +
  3258. (F32)ll_roundp(num_font->getLineHeight()) * 0.5f;
  3259. const LLWString ltext =
  3260. utf8str_to_wstring(llformat("%*d",
  3261. UI_TEXTEDITOR_LINE_NUMBER_DIGITS,
  3262. cur_line_num));
  3263. bool is_cur_line = getCurrentLine() == cur_line_num;
  3264. const U8 style = is_cur_line ? LLFontGL::BOLD : LLFontGL::NORMAL;
  3265. const LLColor4 fg_color = is_cur_line ? mCursorColor
  3266. : mReadOnlyFgColor;
  3267. num_font->render(ltext, // string to draw
  3268. 0, // begin offset
  3269. 3., // x
  3270. y_top, // y
  3271. fg_color,
  3272. LLFontGL::LEFT, // horizontal alignment
  3273. LLFontGL::VCENTER, // vertical alignment
  3274. style,
  3275. S32_MAX, // max chars
  3276. UI_TEXTEDITOR_LINE_NUMBER_MARGIN); // max pixels
  3277. }
  3278. S32 seg_start = line_start;
  3279. while (seg_start < line_end)
  3280. {
  3281. while (cur_segment->getEnd() <= seg_start)
  3282. {
  3283. ++seg_iter;
  3284. if (seg_iter == mSegments.end())
  3285. {
  3286. llwarns << "Ran off the segmentation end !" << llendl;
  3287. return;
  3288. }
  3289. cur_segment = *seg_iter;
  3290. }
  3291. // Draw a segment within the line
  3292. S32 clipped_end = llmin(line_end, cur_segment->getEnd());
  3293. S32 clipped_len = clipped_end - seg_start;
  3294. if (clipped_len > 0)
  3295. {
  3296. LLStyleSP style = cur_segment->getStyle();
  3297. if (style->isImage() && cur_segment->getStart() >= seg_start &&
  3298. cur_segment->getStart() <= clipped_end)
  3299. {
  3300. S32 style_image_height = style->mImageHeight;
  3301. S32 style_image_width = style->mImageWidth;
  3302. LLUIImagePtr image = style->getImage();
  3303. image->draw(ll_round(text_x),
  3304. ll_round(text_y) +
  3305. line_height-style_image_height,
  3306. style_image_width, style_image_height);
  3307. }
  3308. bool is_embedded = cur_segment == mHoverSegment &&
  3309. style->getIsEmbeddedItem();
  3310. if (is_embedded)
  3311. {
  3312. style->mUnderline = true;
  3313. is_embedded = true;
  3314. }
  3315. S32 left_pos = llmin(mSelectionStart, mSelectionEnd);
  3316. if (!is_embedded && mParseHTML && left_pos > seg_start &&
  3317. left_pos < clipped_end && mIsSelecting &&
  3318. mSelectionStart == mSelectionEnd)
  3319. {
  3320. mHTML = style->getLinkHREF();
  3321. }
  3322. drawClippedSegment(text, seg_start, clipped_end, text_x,
  3323. text_y, selection_left, selection_right,
  3324. style, &text_x);
  3325. if (text_x == text_start && mShowLineNumbers)
  3326. {
  3327. text_x += UI_TEXTEDITOR_LINE_NUMBER_MARGIN;
  3328. }
  3329. // Note: text_x is incremented by drawClippedSegment()
  3330. seg_start += clipped_len;
  3331. }
  3332. }
  3333. // Move down one line
  3334. text_y -= (F32)line_height;
  3335. if (line_wraps)
  3336. {
  3337. --cur_line_num;
  3338. }
  3339. // So as to not not number the continuation lines
  3340. cur_line_is_continuation = line_wraps;
  3341. line_start = next_start;
  3342. ++cur_line;
  3343. ++cur_line_num;
  3344. }
  3345. }
  3346. // Draws a single text segment, reversing the color for selection if needed.
  3347. void LLTextEditor::drawClippedSegment(const LLWString& text, S32 seg_start,
  3348. S32 seg_end, F32 x, F32 y,
  3349. S32 selection_left, S32 selection_right,
  3350. const LLStyleSP& style, F32* right_x)
  3351. {
  3352. if (!style->isVisible())
  3353. {
  3354. return;
  3355. }
  3356. const LLFontGL* font = mGLFont;
  3357. LLColor4 color = style->getColor();
  3358. if (style->getFontString()[0])
  3359. {
  3360. font = LLFontGL::getFont(style->getFontID());
  3361. }
  3362. U8 font_flags = LLFontGL::NORMAL;
  3363. if (style->mBold)
  3364. {
  3365. font_flags |= LLFontGL::BOLD;
  3366. }
  3367. if (style->mItalic)
  3368. {
  3369. font_flags |= LLFontGL::ITALIC;
  3370. }
  3371. if (style->mUnderline)
  3372. {
  3373. font_flags |= LLFontGL::UNDERLINE;
  3374. }
  3375. if (style->getIsEmbeddedItem())
  3376. {
  3377. color = mReadOnly ? LLUI::sTextEmbeddedItemReadOnlyColor
  3378. : LLUI::sTextEmbeddedItemColor;
  3379. }
  3380. F32 y_top = y + (F32)ll_roundp(font->getLineHeight());
  3381. bool use_embedded = mAllowEmbeddedItems && style->getIsEmbeddedItem();
  3382. if (selection_left > seg_start)
  3383. {
  3384. // Draw normally
  3385. S32 start = seg_start;
  3386. S32 end = llmin(selection_left, seg_end);
  3387. S32 length = end - start;
  3388. font->render(text, start, x, y_top, color, LLFontGL::LEFT,
  3389. LLFontGL::TOP, font_flags, length, S32_MAX,
  3390. right_x, use_embedded);
  3391. }
  3392. x = *right_x;
  3393. if (selection_left < seg_end && selection_right > seg_start)
  3394. {
  3395. // Draw reversed
  3396. S32 start = llmax(selection_left, seg_start);
  3397. S32 end = llmin(selection_right, seg_end);
  3398. S32 length = end - start;
  3399. font->render(text, start, x, y_top,
  3400. LLColor4(1.f - color.mV[0],
  3401. 1.f - color.mV[1],
  3402. 1.f - color.mV[2], 1.f),
  3403. LLFontGL::LEFT, LLFontGL::TOP, font_flags, length,
  3404. S32_MAX, right_x, use_embedded);
  3405. }
  3406. x = *right_x;
  3407. if (selection_right < seg_end)
  3408. {
  3409. // Draw normally
  3410. S32 start = llmax(selection_right, seg_start);
  3411. S32 end = seg_end;
  3412. S32 length = end - start;
  3413. font->render(text, start, x, y_top, color, LLFontGL::LEFT,
  3414. LLFontGL::TOP, font_flags, length, S32_MAX, right_x,
  3415. use_embedded);
  3416. }
  3417. }
  3418. void LLTextEditor::draw()
  3419. {
  3420. // Do on-demand reflow
  3421. if (mReflowNeeded)
  3422. {
  3423. updateLineStartList();
  3424. mReflowNeeded = false;
  3425. }
  3426. // Then update scroll position, as cursor may have moved
  3427. if (mScrollNeeded)
  3428. {
  3429. updateScrollFromCursor();
  3430. mScrollNeeded = false;
  3431. }
  3432. {
  3433. LLLocalClipRect clip(LLRect(0, getRect().getHeight(),
  3434. getRect().getWidth() -
  3435. (mScrollbar->getVisible() ? SCROLLBAR_SIZE : 0),
  3436. 0));
  3437. bindEmbeddedChars(mGLFont);
  3438. drawBackground();
  3439. drawSelectionBackground();
  3440. drawPreeditMarker();
  3441. drawText();
  3442. drawCursor();
  3443. if (!mReadOnly && mSpellCheck && hasFocus() &&
  3444. LLSpellCheck::getInstance()->getSpellCheck())
  3445. {
  3446. drawMisspelled();
  3447. }
  3448. unbindEmbeddedChars(mGLFont);
  3449. // RN: the decision was made to always show the orange border for
  3450. // keyboard focus but do not put an insertion caret when in readonly
  3451. // mode
  3452. mBorder->setKeyboardFocusHighlight(gFocusMgr.getKeyboardFocus() == this
  3453. /*&& !mReadOnly*/);
  3454. }
  3455. LLView::draw(); // Draw children (scrollbar and border)
  3456. // Remember if we are supposed to be at the bottom of the buffer
  3457. mScrolledToBottom = isScrolledToBottom();
  3458. }
  3459. void LLTextEditor::onTabInto()
  3460. {
  3461. #if 0
  3462. // Selecting all on tabInto causes users to hit tab twice and replace their
  3463. // text with a tab character theoretically, one could selectAll if
  3464. // mTabsToNextField is true, but we could not think of a use case where
  3465. // you would want to select all anyway preserve insertion point when
  3466. // returning to the editor
  3467. selectAll();
  3468. #endif
  3469. }
  3470. //virtual
  3471. void LLTextEditor::clear()
  3472. {
  3473. setText(LLStringUtil::null);
  3474. std::for_each(mSegments.begin(), mSegments.end(), DeletePointer());
  3475. mSegments.clear();
  3476. }
  3477. // Start or stop the editor from accepting text-editing keystrokes
  3478. // see also LLLineEditor
  3479. void LLTextEditor::setFocus(bool new_state)
  3480. {
  3481. bool old_state = hasFocus();
  3482. // Do not change anything if the focus state did not change
  3483. if (new_state == old_state) return;
  3484. // Notify early if we are losing focus.
  3485. if (!new_state)
  3486. {
  3487. gWindowp->allowLanguageTextInput(this, false);
  3488. }
  3489. LLUICtrl::setFocus(new_state);
  3490. if (new_state)
  3491. {
  3492. // Route menu to this class
  3493. grabMenuHandler();
  3494. // Do not start the cursor flashing right away
  3495. resetKeystrokeTimer();
  3496. }
  3497. else
  3498. {
  3499. // Route menu back to the default
  3500. releaseMenuHandler();
  3501. endSelection();
  3502. }
  3503. }
  3504. // Given a line (from the start of the doc) and an offset into the line, find
  3505. // the offset (pos) into text.
  3506. S32 LLTextEditor::getPos(S32 line, S32 offset)
  3507. {
  3508. S32 line_start = getLineStart(line);
  3509. S32 next_start = getLineStart(line+1);
  3510. if (next_start == line_start)
  3511. {
  3512. next_start = getLength() + 1;
  3513. }
  3514. S32 line_length = next_start - line_start - 1;
  3515. line_length = llmax(line_length, 0);
  3516. return line_start + llmin(offset, line_length);
  3517. }
  3518. void LLTextEditor::changePage(S32 delta)
  3519. {
  3520. S32 line, offset;
  3521. getLineAndOffset(mCursorPos, &line, &offset);
  3522. // Get desired x position to remember previous position
  3523. S32 desired_x_pixel = mDesiredXPixel;
  3524. // Allow one line overlap
  3525. S32 page_size = mScrollbar->getPageSize() - 1;
  3526. if (delta == -1)
  3527. {
  3528. line = llmax(line - page_size, 0);
  3529. setCursorPos(getPos(line, offset));
  3530. mScrollbar->setDocPos(mScrollbar->getDocPos() - page_size);
  3531. }
  3532. else
  3533. if (delta == 1)
  3534. {
  3535. setCursorPos(getPos(line + page_size, offset));
  3536. mScrollbar->setDocPos(mScrollbar->getDocPos() + page_size);
  3537. }
  3538. // Put desired position into remember-buffer after setCursorPos()
  3539. mDesiredXPixel = desired_x_pixel;
  3540. if (mOnScrollEndCallback && mOnScrollEndData &&
  3541. mScrollbar->getDocPos() == mScrollbar->getDocPosMax())
  3542. {
  3543. mOnScrollEndCallback(mOnScrollEndData);
  3544. }
  3545. }
  3546. void LLTextEditor::changeLine(S32 delta)
  3547. {
  3548. bindEmbeddedChars(mGLFont);
  3549. S32 line, offset;
  3550. getLineAndOffset(mCursorPos, &line, &offset);
  3551. S32 line_start = getLineStart(line);
  3552. // Set desired x position to remembered previous position
  3553. S32 desired_x_pixel = mDesiredXPixel;
  3554. // If remembered position was reset (thus -1), calculate new one here
  3555. if (desired_x_pixel == -1)
  3556. {
  3557. desired_x_pixel = mGLFont->getWidth(mWText.c_str(), line_start, offset,
  3558. mAllowEmbeddedItems);
  3559. }
  3560. S32 new_line = 0;
  3561. if (delta < 0 && line > 0)
  3562. {
  3563. new_line = line - 1;
  3564. }
  3565. else
  3566. if (delta > 0 && line < getLineCount() - 1)
  3567. {
  3568. new_line = line + 1;
  3569. }
  3570. else
  3571. {
  3572. unbindEmbeddedChars(mGLFont);
  3573. return;
  3574. }
  3575. S32 num_lines = getLineCount();
  3576. S32 new_line_start = getLineStart(new_line);
  3577. S32 new_line_end = getLength();
  3578. if (new_line + 1 < num_lines)
  3579. {
  3580. new_line_end = getLineStart(new_line + 1) - 1;
  3581. }
  3582. S32 new_line_len = new_line_end - new_line_start;
  3583. S32 new_offset =
  3584. mGLFont->charFromPixelOffset(mWText.c_str(), new_line_start,
  3585. (F32)desired_x_pixel,
  3586. (F32)mTextRect.getWidth(),
  3587. new_line_len, mAllowEmbeddedItems);
  3588. setCursorPos (getPos(new_line, new_offset));
  3589. // Put desired position into remember-buffer after setCursorPos()
  3590. mDesiredXPixel = desired_x_pixel;
  3591. unbindEmbeddedChars(mGLFont);
  3592. }
  3593. bool LLTextEditor::isScrolledToTop()
  3594. {
  3595. return mScrollbar->isAtBeginning();
  3596. }
  3597. bool LLTextEditor::isScrolledToBottom()
  3598. {
  3599. return mScrollbar->isAtEnd();
  3600. }
  3601. void LLTextEditor::startOfLine()
  3602. {
  3603. S32 line, offset;
  3604. getLineAndOffset(mCursorPos, &line, &offset);
  3605. setCursorPos(mCursorPos - offset);
  3606. }
  3607. void LLTextEditor::setCursorAndScrollToEnd()
  3608. {
  3609. deselect();
  3610. endOfDoc();
  3611. needsReflow();
  3612. }
  3613. void LLTextEditor::scrollToPos(S32 pos)
  3614. {
  3615. mScrollbar->setDocSize(getLineCount());
  3616. S32 line, offset;
  3617. getLineAndOffset(pos, &line, &offset);
  3618. S32 page_size = mScrollbar->getPageSize();
  3619. if (line < mScrollbar->getDocPos())
  3620. {
  3621. // Scroll so that the cursor is at the top of the page
  3622. mScrollbar->setDocPos(line);
  3623. }
  3624. else if (line >= mScrollbar->getDocPos() + page_size - 1)
  3625. {
  3626. S32 new_pos = 0;
  3627. if (line < mScrollbar->getDocSize() - 1)
  3628. {
  3629. // Scroll so that the cursor is one line above the bottom of the
  3630. // page
  3631. new_pos = line - page_size + 1;
  3632. }
  3633. else
  3634. {
  3635. // If there is less than a page of text remaining, scroll so that
  3636. // the cursor is at the bottom
  3637. new_pos = mScrollbar->getDocPosMax();
  3638. }
  3639. mScrollbar->setDocPos(new_pos);
  3640. }
  3641. // Check if we have scrolled to bottom for callback if asked for callback
  3642. if (mOnScrollEndCallback && mOnScrollEndData &&
  3643. mScrollbar->getDocPos() == mScrollbar->getDocPosMax())
  3644. {
  3645. mOnScrollEndCallback(mOnScrollEndData);
  3646. }
  3647. }
  3648. void LLTextEditor::getLineAndColumnForPosition(S32 position, S32* line,
  3649. S32* col, bool include_wordwrap)
  3650. {
  3651. if (include_wordwrap)
  3652. {
  3653. getLineAndOffset(mCursorPos, line, col);
  3654. }
  3655. else
  3656. {
  3657. const LLWString& text = mWText;
  3658. S32 line_count = 0;
  3659. S32 line_start = 0;
  3660. S32 i;
  3661. for (i = 0; text[i] && i < position; ++i)
  3662. {
  3663. if ('\n' == text[i])
  3664. {
  3665. line_start = i + 1;
  3666. ++line_count;
  3667. }
  3668. }
  3669. *line = line_count;
  3670. *col = i - line_start;
  3671. }
  3672. }
  3673. void LLTextEditor::getCurrentLineAndColumn(S32* line, S32* col,
  3674. bool include_wordwrap)
  3675. {
  3676. getLineAndColumnForPosition(mCursorPos, line, col, include_wordwrap);
  3677. }
  3678. S32 LLTextEditor::getCurrentLine()
  3679. {
  3680. return getLineForPosition(mCursorPos);
  3681. }
  3682. S32 LLTextEditor::getLineForPosition(S32 position)
  3683. {
  3684. S32 line, col;
  3685. getLineAndColumnForPosition(position, &line, &col, false);
  3686. return line;
  3687. }
  3688. void LLTextEditor::endOfLine()
  3689. {
  3690. S32 line, offset;
  3691. getLineAndOffset(mCursorPos, &line, &offset);
  3692. S32 num_lines = getLineCount();
  3693. if (line + 1 >= num_lines)
  3694. {
  3695. setCursorPos(getLength());
  3696. }
  3697. else
  3698. {
  3699. setCursorPos(getLineStart(line + 1) - 1);
  3700. }
  3701. }
  3702. void LLTextEditor::endOfDoc()
  3703. {
  3704. mScrollbar->setDocPos(mScrollbar->getDocPosMax());
  3705. mScrolledToBottom = true;
  3706. S32 len = getLength();
  3707. if (len)
  3708. {
  3709. setCursorPos(len);
  3710. }
  3711. if (mOnScrollEndCallback && mOnScrollEndData &&
  3712. mScrollbar->getDocPos() == mScrollbar->getDocPosMax())
  3713. {
  3714. mOnScrollEndCallback(mOnScrollEndData);
  3715. }
  3716. }
  3717. // Sets the scrollbar from the cursor position
  3718. void LLTextEditor::updateScrollFromCursor()
  3719. {
  3720. if (mReadOnly)
  3721. {
  3722. // No cursor in read only mode
  3723. return;
  3724. }
  3725. scrollToPos(mCursorPos);
  3726. }
  3727. void LLTextEditor::reshape(S32 width, S32 height, bool called_from_parent)
  3728. {
  3729. LLView::reshape(width, height, called_from_parent);
  3730. // Do this first after reshape, because other things depend on up-to-date
  3731. // mTextRect
  3732. updateTextRect();
  3733. needsReflow();
  3734. // Propagate shape information to scrollbar
  3735. mScrollbar->setDocSize(getLineCount());
  3736. S32 line_height = ll_roundp(mGLFont->getLineHeight());
  3737. S32 page_lines = mTextRect.getHeight() / line_height;
  3738. mScrollbar->setPageSize(page_lines);
  3739. }
  3740. void LLTextEditor::autoIndent()
  3741. {
  3742. // Count the number of spaces in the current line
  3743. S32 line, offset;
  3744. getLineAndOffset(mCursorPos, &line, &offset);
  3745. S32 line_start = getLineStart(line);
  3746. S32 space_count = 0;
  3747. S32 i;
  3748. const LLWString& text = mWText;
  3749. while (' ' == text[line_start])
  3750. {
  3751. ++space_count;
  3752. ++line_start;
  3753. }
  3754. // If we are starting a braced section, indent one level.
  3755. if (mCursorPos > 0 && text[mCursorPos - 1] == '{')
  3756. {
  3757. space_count += SPACES_PER_TAB;
  3758. }
  3759. // Insert that number of spaces on the new line
  3760. addChar('\n');
  3761. for (i = 0; i < space_count; ++i)
  3762. {
  3763. addChar(' ');
  3764. }
  3765. }
  3766. // Inserts new text at the cursor position
  3767. void LLTextEditor::insertText(const std::string& new_text)
  3768. {
  3769. bool enabled = getEnabled();
  3770. setEnabled(true);
  3771. // Delete any selected characters (the insertion replaces them)
  3772. if (hasSelection())
  3773. {
  3774. deleteSelection(true);
  3775. }
  3776. setCursorPos(mCursorPos + insert(mCursorPos, utf8str_to_wstring(new_text),
  3777. false));
  3778. needsReflow();
  3779. setEnabled(enabled);
  3780. }
  3781. void LLTextEditor::appendColoredText(const std::string& new_text,
  3782. bool allow_undo, bool prepend_newline,
  3783. const LLColor4& color,
  3784. const std::string& font_name)
  3785. {
  3786. LLStyleSP style(new LLStyle);
  3787. style->setVisible(true);
  3788. style->setColor(color);
  3789. style->setFontName(font_name);
  3790. appendStyledText(new_text, allow_undo, prepend_newline, style);
  3791. }
  3792. void LLTextEditor::appendStyledText(const std::string& new_text,
  3793. bool allow_undo,
  3794. bool prepend_newline,
  3795. LLStyleSP stylep)
  3796. {
  3797. S32 part = (S32)WHOLE;
  3798. if (mParseHTML)
  3799. {
  3800. S32 start = 0, end = 0;
  3801. std::string text = new_text;
  3802. while (findHTML(text, &start, &end))
  3803. {
  3804. LLStyleSP html(new LLStyle);
  3805. html->setVisible(true);
  3806. html->setColor(mLinkColor);
  3807. if (stylep)
  3808. {
  3809. html->setFontName(stylep->getFontString());
  3810. }
  3811. html->mUnderline = true;
  3812. if (start > 0)
  3813. {
  3814. if (part == (S32)WHOLE || part == (S32)START)
  3815. {
  3816. part = (S32)START;
  3817. }
  3818. else
  3819. {
  3820. part = (S32)MIDDLE;
  3821. }
  3822. std::string subtext = text.substr(0, start);
  3823. appendText(subtext, allow_undo, prepend_newline, stylep);
  3824. }
  3825. html->setLinkHREF(text.substr(start, end - start));
  3826. appendText(text.substr(start, end - start), allow_undo,
  3827. prepend_newline, html);
  3828. if (end < (S32)text.length())
  3829. {
  3830. text = text.substr(end, text.length() - end);
  3831. end = 0;
  3832. part = (S32)END;
  3833. }
  3834. else
  3835. {
  3836. break;
  3837. }
  3838. }
  3839. if (part != (S32)WHOLE)
  3840. {
  3841. part = (S32)END;
  3842. }
  3843. if (end < (S32)text.length())
  3844. {
  3845. appendText(text, allow_undo, prepend_newline, stylep);
  3846. }
  3847. }
  3848. else
  3849. {
  3850. appendText(new_text, allow_undo, prepend_newline, stylep);
  3851. }
  3852. }
  3853. // Appends new text to end of document
  3854. void LLTextEditor::appendText(const std::string& new_text, bool allow_undo,
  3855. bool prepend_newline, const LLStyleSP stylep)
  3856. {
  3857. // Save old state
  3858. bool was_scrolled_to_bottom =
  3859. mScrollbar->getDocPos() == mScrollbar->getDocPosMax();
  3860. S32 selection_start = mSelectionStart;
  3861. S32 selection_end = mSelectionEnd;
  3862. bool was_selecting = mIsSelecting;
  3863. S32 cursor_pos = mCursorPos;
  3864. S32 old_length = getLength();
  3865. bool cursor_was_at_end = mCursorPos == old_length;
  3866. deselect();
  3867. setCursorPos(old_length);
  3868. // Add carriage return if not first line
  3869. if (getLength() != 0 && prepend_newline)
  3870. {
  3871. std::string final_text = "\n";
  3872. final_text += new_text;
  3873. append(utf8str_to_wstring(final_text), true);
  3874. }
  3875. else
  3876. {
  3877. append(utf8str_to_wstring(new_text), true);
  3878. }
  3879. if (stylep)
  3880. {
  3881. S32 segment_start = old_length;
  3882. S32 segment_end = getLength();
  3883. LLTextSegment* segment = new LLTextSegment(stylep, segment_start,
  3884. segment_end);
  3885. mSegments.push_back(segment);
  3886. }
  3887. needsReflow();
  3888. // Set the cursor and scroll position
  3889. // Maintain the scroll position unless the scroll was at the end of the doc
  3890. // (in which case, move it to the new end of the doc) or unless the user
  3891. // was doing actively selecting
  3892. if (was_scrolled_to_bottom && !was_selecting)
  3893. {
  3894. if (selection_start != selection_end)
  3895. {
  3896. // maintain an existing non-active selection
  3897. mSelectionStart = selection_start;
  3898. mSelectionEnd = selection_end;
  3899. }
  3900. endOfDoc();
  3901. }
  3902. else if (selection_start != selection_end)
  3903. {
  3904. mSelectionStart = selection_start;
  3905. mSelectionEnd = selection_end;
  3906. mIsSelecting = was_selecting;
  3907. setCursorPos(cursor_pos);
  3908. }
  3909. else if (cursor_was_at_end)
  3910. {
  3911. setCursorPos(getLength());
  3912. }
  3913. else
  3914. {
  3915. setCursorPos(cursor_pos);
  3916. }
  3917. if (!allow_undo)
  3918. {
  3919. blockUndo();
  3920. }
  3921. }
  3922. S32 LLTextEditor::removeFirstLine()
  3923. {
  3924. S32 num_lines = getLineCount();
  3925. if (!num_lines)
  3926. {
  3927. return 0;
  3928. }
  3929. S32 length = getLineStart(1) - 1;
  3930. if (length <= 0)
  3931. {
  3932. length = getLength();
  3933. }
  3934. deselect();
  3935. removeStringNoUndo(0, length);
  3936. pruneSegments();
  3937. updateLineStartList();
  3938. needsScroll();
  3939. return length;
  3940. }
  3941. void LLTextEditor::removeTextFromEnd(S32 num_chars)
  3942. {
  3943. if (num_chars <= 0) return;
  3944. num_chars = llclamp(num_chars, 0, getLength());
  3945. remove(getLength() - num_chars, num_chars, false);
  3946. S32 len = getLength();
  3947. mCursorPos = llclamp(mCursorPos, 0, len);
  3948. mSelectionStart = llclamp(mSelectionStart, 0, len);
  3949. mSelectionEnd = llclamp(mSelectionEnd, 0, len);
  3950. pruneSegments();
  3951. // pruneSegments will invalidate mLineStartList.
  3952. updateLineStartList();
  3953. needsScroll();
  3954. }
  3955. ///////////////////////////////////////////////////////////////////
  3956. // Returns change in number of characters in mWText
  3957. S32 LLTextEditor::insertStringNoUndo(S32 pos, const LLWString& wstr)
  3958. {
  3959. S32 old_len = mWText.length(); // length() returns character length
  3960. S32 insert_len = wstr.length();
  3961. mWText.insert(pos, wstr);
  3962. mTextIsUpToDate = false;
  3963. if (truncate())
  3964. {
  3965. // The user's not getting everything he's hoping for
  3966. make_ui_sound("UISndBadKeystroke");
  3967. insert_len = mWText.length() - old_len;
  3968. }
  3969. return insert_len;
  3970. }
  3971. S32 LLTextEditor::removeStringNoUndo(S32 pos, S32 length)
  3972. {
  3973. mWText.erase(pos, length);
  3974. mTextIsUpToDate = false;
  3975. // This will be wrong if someone calls removeStringNoUndo with an excessive
  3976. // length
  3977. return -length;
  3978. }
  3979. S32 LLTextEditor::overwriteCharNoUndo(S32 pos, llwchar wc)
  3980. {
  3981. if (pos > (S32)mWText.length())
  3982. {
  3983. return 0;
  3984. }
  3985. mWText[pos] = wc;
  3986. mTextIsUpToDate = false;
  3987. return 1;
  3988. }
  3989. void LLTextEditor::makePristine()
  3990. {
  3991. mPristineCmd = mLastCmd;
  3992. mBaseDocIsPristine = !mLastCmd;
  3993. // Create a clean partition in the undo stack. We do not want a single
  3994. // command to extend from the "pre-pristine" state to the "post-pristine"
  3995. // state.
  3996. if (mLastCmd)
  3997. {
  3998. mLastCmd->blockExtensions();
  3999. }
  4000. }
  4001. bool LLTextEditor::isPristine() const
  4002. {
  4003. if (mPristineCmd)
  4004. {
  4005. return mPristineCmd == mLastCmd;
  4006. }
  4007. // No undo stack, so check if the version before and commands were done was
  4008. // the original version
  4009. return !mLastCmd && mBaseDocIsPristine;
  4010. }
  4011. bool LLTextEditor::tryToRevertToPristineState()
  4012. {
  4013. if (!isPristine())
  4014. {
  4015. deselect();
  4016. S32 i = 0;
  4017. while (!isPristine() && canUndo())
  4018. {
  4019. undo();
  4020. --i;
  4021. }
  4022. while (!isPristine() && canRedo())
  4023. {
  4024. redo();
  4025. ++i;
  4026. }
  4027. if (!isPristine())
  4028. {
  4029. // failed, so go back to where we started
  4030. while (i > 0)
  4031. {
  4032. undo();
  4033. --i;
  4034. }
  4035. }
  4036. needsReflow();
  4037. }
  4038. return isPristine(); // true => success
  4039. }
  4040. void LLTextEditor::updateTextRect()
  4041. {
  4042. mTextRect.setOriginAndSize(UI_TEXTEDITOR_BORDER + UI_TEXTEDITOR_H_PAD,
  4043. UI_TEXTEDITOR_BORDER,
  4044. getRect().getWidth() - SCROLLBAR_SIZE -
  4045. 2 * (UI_TEXTEDITOR_BORDER +
  4046. UI_TEXTEDITOR_H_PAD),
  4047. getRect().getHeight() -
  4048. 2 * UI_TEXTEDITOR_BORDER -
  4049. UI_TEXTEDITOR_V_PAD_TOP);
  4050. }
  4051. void LLTextEditor::loadKeywords(const std::string& filename,
  4052. const std::vector<std::string>& funcs,
  4053. const std::vector<std::string>& tooltips,
  4054. const LLColor3& color)
  4055. {
  4056. if (mKeywords.loadFromFile(filename))
  4057. {
  4058. S32 count = llmin(funcs.size(), tooltips.size());
  4059. for (S32 i = 0; i < count; ++i)
  4060. {
  4061. std::string name = utf8str_trim(funcs[i]);
  4062. mKeywords.addToken(LLKeywordToken::WORD, name, color, tooltips[i]);
  4063. }
  4064. mKeywords.findSegments(&mSegments, mWText, mDefaultColor);
  4065. llassert(mSegments.front()->getStart() == 0 &&
  4066. mSegments.back()->getEnd() == getLength());
  4067. }
  4068. }
  4069. void LLTextEditor::updateSegments()
  4070. {
  4071. // For now, we allow keywords-based syntax highlighting (e.g. for the
  4072. // script editor), or embedded items (e.g. for notecards and group notices
  4073. // with inventory offers), or styled text (with colors, links, etc), the
  4074. // latter staying untouched by updateSegments(). It is however not possible
  4075. // to mix up the three types of text editors...
  4076. if (mKeywords.isLoaded())
  4077. {
  4078. // *HACK: no non-ASCII keywords for now
  4079. mKeywords.findSegments(&mSegments, mWText, mDefaultColor);
  4080. }
  4081. else if (mAllowEmbeddedItems)
  4082. {
  4083. findEmbeddedItemSegments();
  4084. }
  4085. // Make sure we have at least one segment
  4086. if (mSegments.size() == 1 && mSegments[0]->getIsDefault())
  4087. {
  4088. delete mSegments[0];
  4089. mSegments.clear(); // create default segment
  4090. }
  4091. if (mSegments.empty())
  4092. {
  4093. LLColor4& text_color = mReadOnly ? mReadOnlyFgColor : mFgColor;
  4094. LLTextSegment* default_segment = new LLTextSegment(text_color, 0,
  4095. mWText.length());
  4096. default_segment->setIsDefault(true);
  4097. mSegments.push_back(default_segment);
  4098. }
  4099. }
  4100. // Only effective if text was removed from the end of the editor
  4101. // *NOTE: Using this will invalidate references to mSegments from
  4102. // mLineStartList.
  4103. void LLTextEditor::pruneSegments()
  4104. {
  4105. S32 len = mWText.length();
  4106. // Find and update the first valid segment
  4107. segment_list_t::iterator segments_begin = mSegments.begin();
  4108. segment_list_t::iterator iter = mSegments.end();
  4109. while (iter != segments_begin)
  4110. {
  4111. LLTextSegment* seg = *(--iter);
  4112. if (seg->getStart() < len)
  4113. {
  4114. // valid segment
  4115. if (seg->getEnd() > len)
  4116. {
  4117. seg->setEnd(len);
  4118. }
  4119. break; // done
  4120. }
  4121. }
  4122. if (iter != mSegments.end())
  4123. {
  4124. // erase invalid segments
  4125. ++iter;
  4126. std::for_each(iter, mSegments.end(), DeletePointer());
  4127. mSegments.erase(iter, mSegments.end());
  4128. }
  4129. #if 0 // We do not care, and it can happen legally for
  4130. // removeTextFromEnd(text->getMaxLength())
  4131. else
  4132. {
  4133. llwarns << "Tried to erase end of empty LLTextEditor" << llendl;
  4134. }
  4135. #endif
  4136. }
  4137. void LLTextEditor::findEmbeddedItemSegments()
  4138. {
  4139. mHoverSegment = NULL;
  4140. std::for_each(mSegments.begin(), mSegments.end(), DeletePointer());
  4141. mSegments.clear();
  4142. bool found_embedded_items = false;
  4143. const LLWString& text = mWText;
  4144. S32 idx = 0;
  4145. while (text[idx])
  4146. {
  4147. if (text[idx] >= FIRST_EMBEDDED_CHAR &&
  4148. text[idx] <= LAST_EMBEDDED_CHAR)
  4149. {
  4150. found_embedded_items = true;
  4151. break;
  4152. }
  4153. ++idx;
  4154. }
  4155. if (!found_embedded_items)
  4156. {
  4157. return;
  4158. }
  4159. S32 text_len = text.length();
  4160. LLColor4& text_color = mReadOnly ? mReadOnlyFgColor : mFgColor;
  4161. bool in_text = false;
  4162. if (idx > 0)
  4163. {
  4164. // text
  4165. mSegments.push_back(new LLTextSegment(text_color, 0, text_len));
  4166. in_text = true;
  4167. }
  4168. LLStyleSP embedded_style(new LLStyle);
  4169. embedded_style->setIsEmbeddedItem(true);
  4170. // Start with i just after the first embedded item
  4171. while (text[idx])
  4172. {
  4173. if (text[idx] >= FIRST_EMBEDDED_CHAR &&
  4174. text[idx] <= LAST_EMBEDDED_CHAR)
  4175. {
  4176. if (in_text)
  4177. {
  4178. mSegments.back()->setEnd(idx);
  4179. }
  4180. // item
  4181. mSegments.push_back(new LLTextSegment(embedded_style, idx,
  4182. idx + 1));
  4183. in_text = false;
  4184. }
  4185. else if (!in_text)
  4186. {
  4187. // text
  4188. mSegments.push_back(new LLTextSegment(text_color, idx, text_len));
  4189. in_text = true;
  4190. }
  4191. ++idx;
  4192. }
  4193. }
  4194. bool LLTextEditor::handleMouseUpOverSegment(S32 x, S32 y, MASK mask)
  4195. {
  4196. if (hasMouseCapture())
  4197. {
  4198. // This mouse up was part of a click. Regardless of where the cursor
  4199. // is, see if we recently touched a link and launch it if we did.
  4200. if (mParseHTML && mHTML.length() > 0)
  4201. {
  4202. // Special handling for slurls
  4203. if (sSecondlifeURLcallback && !(*sSecondlifeURLcallback)(mHTML))
  4204. {
  4205. if (sURLcallback)
  4206. {
  4207. (*sURLcallback)(mHTML);
  4208. }
  4209. }
  4210. mHTML.clear();
  4211. }
  4212. }
  4213. return false;
  4214. }
  4215. // Finds the text segment (if any) at the give local screen position
  4216. const LLTextSegment* LLTextEditor::getSegmentAtLocalPos(S32 x, S32 y) const
  4217. {
  4218. // Find the cursor position at the requested local screen position
  4219. S32 offset = getCursorPosFromLocalCoord(x, y, false);
  4220. S32 idx = getSegmentIdxAtOffset(offset);
  4221. return idx >= 0 ? mSegments[idx] : NULL;
  4222. }
  4223. const LLTextSegment* LLTextEditor::getSegmentAtOffset(S32 offset) const
  4224. {
  4225. S32 idx = getSegmentIdxAtOffset(offset);
  4226. return idx >= 0 ? mSegments[idx] : NULL;
  4227. }
  4228. S32 LLTextEditor::getSegmentIdxAtOffset(S32 offset) const
  4229. {
  4230. if (mSegments.empty() || offset < 0 || offset >= getLength())
  4231. {
  4232. return -1;
  4233. }
  4234. else
  4235. {
  4236. S32 segidx, segoff;
  4237. getSegmentAndOffset(offset, &segidx, &segoff);
  4238. return segidx;
  4239. }
  4240. }
  4241. void LLTextEditor::onMouseCaptureLost()
  4242. {
  4243. endSelection();
  4244. }
  4245. void LLTextEditor::setOnScrollEndCallback(void (*callback)(void*),
  4246. void* userdata)
  4247. {
  4248. mOnScrollEndCallback = callback;
  4249. mOnScrollEndData = userdata;
  4250. mScrollbar->setOnScrollEndCallback(callback, userdata);
  4251. }
  4252. void LLTextEditor::setKeystrokeCallback(void (*callback)(LLTextEditor*, void*),
  4253. void* userdata)
  4254. {
  4255. mKeystrokeCallback = callback;
  4256. mKeystrokeData = userdata;
  4257. }
  4258. void LLTextEditor::setOnHandleKeyCallback(bool (*callback)(KEY, MASK,
  4259. LLTextEditor*,
  4260. void*),
  4261. void* userdata)
  4262. {
  4263. mOnHandleKeyCallback = callback;
  4264. mOnHandleKeyData = userdata;
  4265. }
  4266. ///////////////////////////////////////////////////////////////////
  4267. // Hack for Notecards
  4268. bool LLTextEditor::importBuffer(const char* buffer, S32 length)
  4269. {
  4270. std::istringstream instream(buffer);
  4271. // Version 1 format:
  4272. // Linden text version 1\n
  4273. // {\n
  4274. // <EmbeddedItemList chunk>
  4275. // Text length <bytes without \0>\n
  4276. // <text without \0> (text may contain ext_char_values)
  4277. // }\n
  4278. char tbuf[MAX_STRING];
  4279. S32 version = 0;
  4280. instream.getline(tbuf, MAX_STRING);
  4281. if (1 != sscanf(tbuf, "Linden text version %d", &version))
  4282. {
  4283. llwarns << "Invalid Linden text file header " << llendl;
  4284. return false;
  4285. }
  4286. if (version != 1)
  4287. {
  4288. llwarns << "Invalid Linden text file version: " << version << llendl;
  4289. return false;
  4290. }
  4291. instream.getline(tbuf, MAX_STRING);
  4292. if (sscanf(tbuf, "{"))
  4293. {
  4294. llwarns << "Invalid Linden text file format" << llendl;
  4295. return false;
  4296. }
  4297. S32 text_len = 0;
  4298. instream.getline(tbuf, MAX_STRING);
  4299. if (sscanf(tbuf, "Text length %d", &text_len) != 1)
  4300. {
  4301. llwarns << "Invalid Linden text length field" << llendl;
  4302. return false;
  4303. }
  4304. if (text_len > mMaxTextByteLength)
  4305. {
  4306. llwarns << "Invalid Linden text length: " << text_len << llendl;
  4307. return false;
  4308. }
  4309. char* text = new char[text_len + 1];
  4310. if (!text)
  4311. {
  4312. llerrs << "Memory allocation failure." << llendl;
  4313. return false;
  4314. }
  4315. bool success = true;
  4316. instream.get(text, text_len + 1, '\0');
  4317. text[text_len] = '\0';
  4318. if (text_len != (S32)strlen(text))
  4319. {
  4320. llwarns << llformat("Invalid text length: %d != %d ",
  4321. strlen(text), text_len) << llendl;
  4322. success = false;
  4323. }
  4324. instream.getline(tbuf, MAX_STRING);
  4325. if (success && sscanf(tbuf, "}"))
  4326. {
  4327. llwarns << "Invalid Linden text file format: missing terminal }"
  4328. << llendl;
  4329. success = false;
  4330. }
  4331. if (success)
  4332. {
  4333. // Actually set the text
  4334. setText(text);
  4335. }
  4336. delete[] text;
  4337. setCursorPos(mCursorPos);
  4338. deselect();
  4339. needsReflow();
  4340. return success;
  4341. }
  4342. bool LLTextEditor::exportBuffer(std::string& buffer)
  4343. {
  4344. std::ostringstream outstream(buffer);
  4345. outstream << "Linden text version 1\n";
  4346. outstream << "{\n";
  4347. outstream << llformat("Text length %d\n", mWText.length());
  4348. outstream << getText();
  4349. outstream << "}\n";
  4350. return true;
  4351. }
  4352. //////////////////////////////////////////////////////////////////////////
  4353. // LLTextSegment
  4354. LLTextSegment::LLTextSegment(S32 start)
  4355. : mStart(start),
  4356. mEnd(0),
  4357. mToken(NULL),
  4358. mIsDefault(false)
  4359. {
  4360. }
  4361. LLTextSegment::LLTextSegment(const LLStyleSP& style, S32 start, S32 end)
  4362. : mStyle(style),
  4363. mStart(start),
  4364. mEnd(end),
  4365. mToken(NULL),
  4366. mIsDefault(false)
  4367. {
  4368. }
  4369. LLTextSegment::LLTextSegment(const LLColor4& color, S32 start, S32 end,
  4370. bool is_visible)
  4371. : mStyle(new LLStyle(is_visible, color, LLStringUtil::null)),
  4372. mStart(start),
  4373. mEnd(end),
  4374. mToken(NULL),
  4375. mIsDefault(false)
  4376. {
  4377. }
  4378. LLTextSegment::LLTextSegment(const LLColor4& color, S32 start, S32 end)
  4379. : mStyle(new LLStyle(true, color, LLStringUtil::null)),
  4380. mStart(start),
  4381. mEnd(end),
  4382. mToken(NULL),
  4383. mIsDefault(false)
  4384. {
  4385. }
  4386. LLTextSegment::LLTextSegment(const LLColor3& color, S32 start, S32 end)
  4387. : mStyle(new LLStyle(true, color, LLStringUtil::null)),
  4388. mStart(start),
  4389. mEnd(end),
  4390. mToken(NULL),
  4391. mIsDefault(false)
  4392. {
  4393. }
  4394. bool LLTextSegment::getToolTip(std::string& msg) const
  4395. {
  4396. if (mToken && !mToken->getToolTip().empty())
  4397. {
  4398. const LLWString& wmsg = mToken->getToolTip();
  4399. msg = wstring_to_utf8str(wmsg);
  4400. return true;
  4401. }
  4402. return false;
  4403. }
  4404. void LLTextSegment::dump() const
  4405. {
  4406. llinfos << "Segment "
  4407. #if 0
  4408. << "(color: " << mColor.mV[VX] << ", " << mColor.mV[VY] << ", "
  4409. << mColor.mV[VZ] << ")"
  4410. #endif
  4411. << "[" << mStart << ", " << getEnd() << "]" << llendl;
  4412. }
  4413. //virtual
  4414. const std::string& LLTextEditor::getTag() const
  4415. {
  4416. return LL_SIMPLE_TEXT_EDITOR_TAG;
  4417. }
  4418. //virtual
  4419. LLXMLNodePtr LLTextEditor::getXML(bool save_children) const
  4420. {
  4421. LLXMLNodePtr node = LLUICtrl::getXML();
  4422. node->setName(LL_SIMPLE_TEXT_EDITOR_TAG);
  4423. // Attributes
  4424. node->createChild("max_length", true)->setIntValue(getMaxLength());
  4425. node->createChild("embedded_items",
  4426. true)->setBoolValue(mAllowEmbeddedItems);
  4427. node->createChild("font",
  4428. true)->setStringValue(LLFontGL::nameFromFont(mGLFont));
  4429. node->createChild("word_wrap", true)->setBoolValue(mWordWrap);
  4430. node->createChild("hide_scrollbar",
  4431. true)->setBoolValue(mHideScrollbarForShortDocs);
  4432. addColorXML(node, mCursorColor, "cursor_color", "TextCursorColor");
  4433. addColorXML(node, mFgColor, "text_color", "TextFgColor");
  4434. addColorXML(node, mDefaultColor, "text_default_color", "TextDefaultColor");
  4435. addColorXML(node, mReadOnlyFgColor, "text_readonly_color",
  4436. "TextFgReadOnlyColor");
  4437. addColorXML(node, mReadOnlyBgColor, "bg_readonly_color",
  4438. "TextBgReadOnlyColor");
  4439. addColorXML(node, mWriteableBgColor, "bg_writeable_color",
  4440. "TextBgWriteableColor");
  4441. addColorXML(node, mFocusBgColor, "bg_focus_color", "TextBgFocusColor");
  4442. // Contents
  4443. node->setStringValue(getText());
  4444. return node;
  4445. }
  4446. //static
  4447. LLView* LLTextEditor::fromXML(LLXMLNodePtr nodep, LLView* parentp,
  4448. LLUICtrlFactory*)
  4449. {
  4450. std::string name = "text_editor";
  4451. nodep->getAttributeString("name", name);
  4452. LLRect rect;
  4453. createRect(nodep, rect, parentp, LLRect());
  4454. U32 max_text_length = 255;
  4455. nodep->getAttributeU32("max_length", max_text_length);
  4456. bool allow_embedded_items = false;
  4457. nodep->getAttributeBool("embedded_items", allow_embedded_items);
  4458. LLFontGL* fontp = LLView::selectFont(nodep);
  4459. std::string text = nodep->getTextContents().substr(0, max_text_length - 1);
  4460. LLTextEditor* self = new LLTextEditor(name, rect, max_text_length, text,
  4461. fontp, allow_embedded_items);
  4462. self->setTextEditorParameters(nodep);
  4463. bool hide_scrollbar = false;
  4464. nodep->getAttributeBool("hide_scrollbar", hide_scrollbar);
  4465. self->setHideScrollbarForShortDocs(hide_scrollbar);
  4466. self->initFromXML(nodep, parentp);
  4467. return self;
  4468. }
  4469. void LLTextEditor::setTextEditorParameters(LLXMLNodePtr node)
  4470. {
  4471. bool word_wrap = false;
  4472. node->getAttributeBool("word_wrap", word_wrap);
  4473. setWordWrap(word_wrap);
  4474. node->getAttributeBool("show_line_numbers", mShowLineNumbers);
  4475. node->getAttributeBool("track_bottom", mTrackBottom);
  4476. // By default, spell check is enabled for text editors
  4477. if (node->hasAttribute("spell_check"))
  4478. {
  4479. node->getAttributeBool("spell_check", mSpellCheck);
  4480. }
  4481. LLColor4 color;
  4482. if (LLUICtrlFactory::getAttributeColor(node, "cursor_color", color))
  4483. {
  4484. setCursorColor(color);
  4485. }
  4486. if (LLUICtrlFactory::getAttributeColor(node, "text_color", color))
  4487. {
  4488. setFgColor(color);
  4489. }
  4490. if (LLUICtrlFactory::getAttributeColor(node, "text_readonly_color", color))
  4491. {
  4492. setReadOnlyFgColor(color);
  4493. }
  4494. if (LLUICtrlFactory::getAttributeColor(node, "bg_readonly_color", color))
  4495. {
  4496. setReadOnlyBgColor(color);
  4497. }
  4498. if (LLUICtrlFactory::getAttributeColor(node, "bg_writeable_color", color))
  4499. {
  4500. setWriteableBgColor(color);
  4501. }
  4502. }
  4503. // Refactoring note: we may eventually want to replace this with std::regex
  4504. // or std::tokenizer capabilities since we have already fixed at least two
  4505. // JIRAs concerning logic issues associated with this function.
  4506. S32 LLTextEditor::findHTMLToken(const std::string& line, S32 pos,
  4507. bool reverse) const
  4508. {
  4509. static const std::string openers = " \t\n('\"[{<>";
  4510. static const std::string closers = " \t\n)'\"]}><;";
  4511. if (reverse)
  4512. {
  4513. for (S32 index = pos; index >= 0; --index)
  4514. {
  4515. char c = line[index];
  4516. S32 m2 = openers.find(c);
  4517. if (m2 >= 0)
  4518. {
  4519. return index + 1;
  4520. }
  4521. }
  4522. return 0; // index is -1, we do not want to return that.
  4523. }
  4524. // Adjust the search slightly, to allow matching parenthesis inside the URL
  4525. S32 len = line.length();
  4526. S32 paren_count = 0;
  4527. for (S32 index = pos; index < len; ++index)
  4528. {
  4529. char c = line[index];
  4530. if (c == '(')
  4531. {
  4532. ++paren_count;
  4533. }
  4534. else if (c == ')')
  4535. {
  4536. if (paren_count <= 0)
  4537. {
  4538. return index;
  4539. }
  4540. else
  4541. {
  4542. --paren_count;
  4543. }
  4544. }
  4545. else
  4546. {
  4547. S32 m2 = closers.find(c);
  4548. if (m2 >= 0)
  4549. {
  4550. return index;
  4551. }
  4552. }
  4553. }
  4554. return len;
  4555. }
  4556. bool LLTextEditor::findHTML(const std::string& line, S32* begin,
  4557. S32* end) const
  4558. {
  4559. static const std::string badneighbors =
  4560. ".,<>?';\"][}{=-+_)(*&^%$#@!~`\t\r\n\\";
  4561. bool matched = false;
  4562. size_t m1 = line.find("://", *end);
  4563. if (m1 != std::string::npos) // Easy match.
  4564. {
  4565. *begin = findHTMLToken(line, m1, true);
  4566. *end = findHTMLToken(line, m1, false);
  4567. // Load_url only handles http and https so do not hilite ftp, smb, etc
  4568. size_t m2 = line.substr(*begin, (m1 - *begin)).find("http");
  4569. size_t m3 = line.substr(*begin, (m1 - *begin)).find("secondlife");
  4570. if ((m2 != std::string::npos || m3 != std::string::npos) &&
  4571. badneighbors.find(line.substr(m1 + 3, 1)) == std::string::npos)
  4572. {
  4573. matched = true;
  4574. }
  4575. }
  4576. #if 0
  4577. // Matches things like secondlife.com (no http://) needs a whitelist to
  4578. // really be effective.
  4579. else // Harder match.
  4580. {
  4581. m1 = line.find(".", *end);
  4582. if (m1 != std::string::npos)
  4583. {
  4584. *end = findHTMLToken(line, m1, false);
  4585. *begin = findHTMLToken(line, m1, true);
  4586. m1 = line.rfind(".", *end);
  4587. if ((*end - m1) > 2 && m1 > *begin)
  4588. {
  4589. size_t m2 = badneighbors.find(line.substr(m1 + 1, 1));
  4590. size_t m3 = badneighbors.find(line.substr(m1 - 1, 1));
  4591. if (m3 == std::string::npos && m2 == std::string::npos)
  4592. {
  4593. matched = true;
  4594. }
  4595. }
  4596. }
  4597. }
  4598. #endif
  4599. if (matched)
  4600. {
  4601. std::string url = line.substr(*begin, *end - *begin);
  4602. std::string slurl_id = "slurl.com/secondlife/";
  4603. size_t strpos = url.find(slurl_id);
  4604. if (strpos == std::string::npos)
  4605. {
  4606. slurl_id = "maps.secondlife.com/secondlife/";
  4607. strpos = url.find(slurl_id);
  4608. if (strpos == std::string::npos)
  4609. {
  4610. slurl_id = "secondlife://";
  4611. strpos = url.find(slurl_id);
  4612. if (strpos == std::string::npos)
  4613. {
  4614. slurl_id = "sl://";
  4615. strpos = url.find(slurl_id);
  4616. }
  4617. }
  4618. }
  4619. if (strpos != std::string::npos)
  4620. {
  4621. strpos += slurl_id.length();
  4622. while (url.find("/", strpos) == std::string::npos)
  4623. {
  4624. if ((size_t)(*end + 2) >= line.length() ||
  4625. line.substr(*end, 1) != " ")
  4626. {
  4627. matched = false;
  4628. break;
  4629. }
  4630. strpos = (*end + 1) - *begin;
  4631. *end = findHTMLToken(line,(*begin + strpos), false);
  4632. url = line.substr(*begin, *end - *begin);
  4633. }
  4634. }
  4635. }
  4636. if (!matched)
  4637. {
  4638. *begin = *end = 0;
  4639. }
  4640. return matched;
  4641. }
  4642. void LLTextEditor::updateAllowingLanguageInput()
  4643. {
  4644. if (hasFocus() && !mReadOnly)
  4645. {
  4646. gWindowp->allowLanguageTextInput(this, true);
  4647. }
  4648. else
  4649. {
  4650. gWindowp->allowLanguageTextInput(this, false);
  4651. }
  4652. }
  4653. // Preedit is managed off the undo/redo command stack.
  4654. bool LLTextEditor::hasPreeditString() const
  4655. {
  4656. return mPreeditPositions.size() > 1;
  4657. }
  4658. void LLTextEditor::resetPreedit()
  4659. {
  4660. if (hasPreeditString())
  4661. {
  4662. if (hasSelection())
  4663. {
  4664. llwarns << "Preedit and selection !" << llendl;
  4665. deselect();
  4666. }
  4667. mCursorPos = mPreeditPositions.front();
  4668. removeStringNoUndo(mCursorPos, mPreeditPositions.back() - mCursorPos);
  4669. insertStringNoUndo(mCursorPos, mPreeditOverwrittenWString);
  4670. mPreeditWString.clear();
  4671. mPreeditOverwrittenWString.clear();
  4672. mPreeditPositions.clear();
  4673. // A call to updatePreedit should soon follow under a normal course of
  4674. // operation, so we do not need to maintain internal variables such as
  4675. // line start positions now.
  4676. }
  4677. }
  4678. void LLTextEditor::updatePreedit(const LLWString& preedit_string,
  4679. const segment_lengths_t& preedit_segment_lengths,
  4680. const standouts_t& preedit_standouts,
  4681. S32 caret_position)
  4682. {
  4683. // Just in case.
  4684. if (mReadOnly)
  4685. {
  4686. return;
  4687. }
  4688. gWindowp->hideCursorUntilMouseMove();
  4689. S32 insert_preedit_at = mCursorPos;
  4690. mPreeditWString = preedit_string;
  4691. mPreeditPositions.resize(preedit_segment_lengths.size() + 1);
  4692. S32 position = insert_preedit_at;
  4693. for (segment_lengths_t::size_type i = 0,
  4694. size = preedit_segment_lengths.size();
  4695. i < size; ++i)
  4696. {
  4697. mPreeditPositions[i] = position;
  4698. position += preedit_segment_lengths[i];
  4699. }
  4700. mPreeditPositions.back() = position;
  4701. if (gKeyboardp && gKeyboardp->getInsertMode() == LL_KIM_OVERWRITE)
  4702. {
  4703. mPreeditOverwrittenWString = getWSubString(insert_preedit_at,
  4704. mPreeditWString.length());
  4705. removeStringNoUndo(insert_preedit_at, mPreeditWString.length());
  4706. }
  4707. else
  4708. {
  4709. mPreeditOverwrittenWString.clear();
  4710. }
  4711. insertStringNoUndo(insert_preedit_at, mPreeditWString);
  4712. mPreeditStandouts = preedit_standouts;
  4713. needsReflow();
  4714. setCursorPos(insert_preedit_at + caret_position);
  4715. // Update of the preedit should be caused by some key strokes.
  4716. mKeystrokeTimer.reset();
  4717. }
  4718. bool LLTextEditor::getPreeditLocation(S32 query_offset, LLCoordGL* coord,
  4719. LLRect* bounds, LLRect* control) const
  4720. {
  4721. if (control)
  4722. {
  4723. LLRect control_rect_screen;
  4724. localRectToScreen(mTextRect, &control_rect_screen);
  4725. LLUI::screenRectToGL(control_rect_screen, control);
  4726. }
  4727. S32 preedit_left_position, preedit_right_position;
  4728. if (hasPreeditString())
  4729. {
  4730. preedit_left_position = mPreeditPositions.front();
  4731. preedit_right_position = mPreeditPositions.back();
  4732. }
  4733. else
  4734. {
  4735. preedit_left_position = preedit_right_position = mCursorPos;
  4736. }
  4737. const S32 query = (query_offset >= 0 ? preedit_left_position + query_offset
  4738. : mCursorPos);
  4739. if (query < preedit_left_position || query > preedit_right_position)
  4740. {
  4741. return false;
  4742. }
  4743. const S32 first_visible_line = mScrollbar->getDocPos();
  4744. if (query < getLineStart(first_visible_line))
  4745. {
  4746. return false;
  4747. }
  4748. S32 current_line = first_visible_line;
  4749. S32 current_line_start, current_line_end;
  4750. while (true)
  4751. {
  4752. current_line_start = getLineStart(current_line);
  4753. current_line_end = getLineStart(current_line + 1);
  4754. if (query >= current_line_start && query < current_line_end)
  4755. {
  4756. break;
  4757. }
  4758. if (current_line_start == current_line_end)
  4759. {
  4760. // We have reached on the last line. The query position must be
  4761. // here.
  4762. break;
  4763. }
  4764. ++current_line;
  4765. }
  4766. const llwchar* const text = mWText.c_str();
  4767. const S32 line_height = ll_roundp(mGLFont->getLineHeight());
  4768. if (coord)
  4769. {
  4770. const S32 query_x = mTextRect.mLeft +
  4771. mGLFont->getWidth(text, current_line_start,
  4772. query - current_line_start,
  4773. mAllowEmbeddedItems);
  4774. const S32 query_y = mTextRect.mTop -
  4775. (current_line - first_visible_line) * line_height -
  4776. line_height / 2;
  4777. S32 query_screen_x, query_screen_y;
  4778. localPointToScreen(query_x, query_y, &query_screen_x, &query_screen_y);
  4779. LLUI::screenPointToGL(query_screen_x, query_screen_y, &coord->mX,
  4780. &coord->mY);
  4781. }
  4782. if (bounds)
  4783. {
  4784. S32 preedit_left = mTextRect.mLeft;
  4785. if (preedit_left_position > current_line_start)
  4786. {
  4787. preedit_left += mGLFont->getWidth(text, current_line_start,
  4788. preedit_left_position -
  4789. current_line_start,
  4790. mAllowEmbeddedItems);
  4791. }
  4792. S32 preedit_right = mTextRect.mLeft;
  4793. if (preedit_right_position < current_line_end)
  4794. {
  4795. preedit_right += mGLFont->getWidth(text, current_line_start,
  4796. preedit_right_position -
  4797. current_line_start,
  4798. mAllowEmbeddedItems);
  4799. }
  4800. else
  4801. {
  4802. preedit_right += mGLFont->getWidth(text, current_line_start,
  4803. current_line_end -
  4804. current_line_start,
  4805. mAllowEmbeddedItems);
  4806. }
  4807. const S32 preedit_top = mTextRect.mTop -
  4808. (current_line - first_visible_line) *
  4809. line_height;
  4810. const S32 preedit_bottom = preedit_top - line_height;
  4811. const LLRect preedit_rect_local(preedit_left, preedit_top,
  4812. preedit_right, preedit_bottom);
  4813. LLRect preedit_rect_screen;
  4814. localRectToScreen(preedit_rect_local, &preedit_rect_screen);
  4815. LLUI::screenRectToGL(preedit_rect_screen, bounds);
  4816. }
  4817. return true;
  4818. }
  4819. void LLTextEditor::getSelectionRange(S32* position, S32* length) const
  4820. {
  4821. if (hasSelection())
  4822. {
  4823. *position = llmin(mSelectionStart, mSelectionEnd);
  4824. *length = abs(mSelectionStart - mSelectionEnd);
  4825. }
  4826. else
  4827. {
  4828. *position = mCursorPos;
  4829. *length = 0;
  4830. }
  4831. }
  4832. void LLTextEditor::getPreeditRange(S32* position, S32* length) const
  4833. {
  4834. if (hasPreeditString())
  4835. {
  4836. *position = mPreeditPositions.front();
  4837. *length = mPreeditPositions.back() - mPreeditPositions.front();
  4838. }
  4839. else
  4840. {
  4841. *position = mCursorPos;
  4842. *length = 0;
  4843. }
  4844. }
  4845. void LLTextEditor::markAsPreedit(S32 position, S32 length)
  4846. {
  4847. deselect();
  4848. setCursorPos(position);
  4849. if (hasPreeditString())
  4850. {
  4851. llwarns << "markAsPreedit invoked when hasPreeditString is true."
  4852. << llendl;
  4853. }
  4854. mPreeditWString = LLWString(mWText, position, length);
  4855. if (length > 0)
  4856. {
  4857. mPreeditPositions.resize(2);
  4858. mPreeditPositions[0] = position;
  4859. mPreeditPositions[1] = position + length;
  4860. mPreeditStandouts.resize(1);
  4861. mPreeditStandouts[0] = false;
  4862. }
  4863. else
  4864. {
  4865. mPreeditPositions.clear();
  4866. mPreeditStandouts.clear();
  4867. }
  4868. if (gKeyboardp && gKeyboardp->getInsertMode() == LL_KIM_OVERWRITE)
  4869. {
  4870. mPreeditOverwrittenWString = mPreeditWString;
  4871. }
  4872. else
  4873. {
  4874. mPreeditOverwrittenWString.clear();
  4875. }
  4876. }
  4877. S32 LLTextEditor::getPreeditFontSize() const
  4878. {
  4879. return ll_roundp(mGLFont->getLineHeight() * LLUI::sGLScaleFactor.mV[VY]);
  4880. }