WMTextView.Mod 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328
  1. MODULE WMTextView; (** AUTHOR "TF"; PURPOSE "Generic unicode text viewer"; *)
  2. IMPORT
  3. Kernel, Modules, Inputs, KernelLog, XML, Texts, TextUtilities, SyntaxHighlighter,
  4. WMGraphics, WMGraphicUtilities, WMMessages, WMComponents,
  5. WMStandardComponents, Strings, WMDropTarget, Raster,
  6. WMRectangles, WMWindowManager, WMProperties,
  7. Commands, FileHandlers, Streams, WMPopups, FP1616,
  8. WMPieMenu, WMEvents, UnicodeBidirectionality, PositionDebugging, ContextualDependency, D := Debugging;
  9. CONST
  10. TraceRenderOptimize = 0;
  11. TraceLayout = 1;
  12. TraceBaseLine = 2;
  13. TraceInvalidate = 3;
  14. TraceCopy = 4;
  15. TraceCommands = 5;
  16. Trace = {};
  17. (* When pressing the middle mouse button and holding down ALT, execute this command with
  18. the actual command and its parameters as parameter *)
  19. AltMMCommand = "WMUtilities.Call";
  20. CallURLPointer = 0; (* mousebutton which will invoke the URL *)
  21. (** Text wrapping modes (default = WrapWord) *)
  22. NoWrap* = 0; Wrap* = 1; WrapWord* = 2;
  23. AlignLeft = 0; AlignCenter = 1; AlignRight = 2;
  24. DragDist = 5;
  25. MaxCallParameterBuf = 1 * 1024 * 1024;
  26. MaxCommandLength = 256; (* without parameters *)
  27. (* If TRUE, a mouse right click will open a pie menu *)
  28. UsePieMenu = TRUE;
  29. InterclickNone = 0;
  30. Interclick01 = 1; (* mouse button 0 & 1 *)
  31. Interclick02 = 2; (* mouse button 0 & 2 *)
  32. InterclickCancelled = 99;
  33. SelectionColor = 0000FF60H;
  34. SelectionColorInterclick01 = LONGINT(0FFFF0060H);
  35. SelectionColorInterclick02 = LONGINT(0FF000060H);
  36. TYPE
  37. Char32 = Texts.Char32;
  38. ClickInfo = OBJECT
  39. VAR
  40. cmd, cmdPar : Strings.String;
  41. END ClickInfo;
  42. TabStops* = OBJECT
  43. VAR tabDist : LONGINT;
  44. (* return the next TabStop from the x position *)
  45. PROCEDURE GetNextTabStop*(x : LONGINT) : LONGINT;
  46. BEGIN
  47. RETURN ((x DIV tabDist) + 1) * tabDist
  48. END GetNextTabStop;
  49. END TabStops;
  50. TabPositions* = POINTER TO ARRAY OF LONGINT;
  51. CustomTabStops* = OBJECT (TabStops)
  52. VAR
  53. positions : TabPositions;
  54. PROCEDURE GetNextTabStop*(x : LONGINT) : LONGINT;
  55. VAR
  56. idx : LONGINT;
  57. BEGIN
  58. idx := 0;
  59. ASSERT(positions # NIL);
  60. IF x >= positions[LEN(positions) - 1] THEN RETURN GetNextTabStop^(x) END; (* return default tab stop *)
  61. WHILE x >= positions[idx] DO INC(idx) END;
  62. RETURN positions[idx]
  63. END GetNextTabStop;
  64. PROCEDURE &New *(tp : TabPositions);
  65. VAR
  66. idx : LONGINT;
  67. BEGIN
  68. idx := 0; tabDist := 150;
  69. WHILE idx < LEN(tp)-1 DO ASSERT(tp[idx] <= tp[idx+1]); INC(idx) END;
  70. positions := tp
  71. END New;
  72. END CustomTabStops;
  73. LineInfo = RECORD
  74. leftIndent, rightIndent, firstIndent, spaceBefore, spaceAfter : LONGINT;
  75. firstInParagraph, lastInParagraph : BOOLEAN;
  76. height, width, ascent : LONGINT;
  77. pos : LONGINT; (* the position in the text, where this line starts *)
  78. align : LONGINT;
  79. tabStops : TabStops;
  80. END;
  81. LineInfoArray = POINTER TO ARRAY OF LineInfo;
  82. TYPE
  83. Layout = OBJECT
  84. VAR
  85. nofLines : LONGINT;
  86. lines : LineInfoArray;
  87. text : Texts.Text;
  88. paperWidth : LONGINT;
  89. textWidth : LONGINT; (* maximal width of the text <= textWidth *)
  90. textHeight : LONGINT;
  91. layoutLineProc : PROCEDURE {DELEGATE} (VAR pos : LONGINT; VAR ch : Char32; VAR lineInfo : LineInfo; wrapWidth, stopPos, stopXPos : LONGINT);
  92. bidiFormatter : UnicodeBidirectionality.BidiFormatter;
  93. initialized : BOOLEAN;
  94. PROCEDURE &New*;
  95. BEGIN
  96. NEW(lines, 4);
  97. (* this helps saving some bidi computations *)
  98. initialized := FALSE;
  99. END New;
  100. (** Replace the text *)
  101. PROCEDURE SetText(text : Texts.Text);
  102. BEGIN
  103. ASSERT(text # NIL);
  104. SELF.text := text;
  105. END SetText;
  106. PROCEDURE GrowLines;
  107. VAR i : LONGINT; newLines : LineInfoArray;
  108. BEGIN
  109. NEW(newLines, LEN(lines) * 2);
  110. FOR i := 0 TO LEN(lines) - 1 DO newLines[i] := lines[i] END;
  111. lines := newLines
  112. END GrowLines;
  113. (** find the linenumber by the position *)
  114. PROCEDURE FindLineNrByPos(pos : LONGINT) : LONGINT;
  115. VAR a, b, m : LONGINT;
  116. BEGIN
  117. a := 0; b := nofLines - 1;
  118. WHILE (a < b) DO m := (a + b) DIV 2;
  119. IF lines[m].pos <= pos THEN a := m + 1
  120. ELSE b := m
  121. END
  122. END;
  123. (* last line hack *)
  124. IF lines[a].pos <= pos THEN INC(a) END;
  125. RETURN a - 1
  126. END FindLineNrByPos;
  127. PROCEDURE GetLineStartPos(lineNr : LONGINT) : LONGINT;
  128. BEGIN
  129. IF (lineNr >= 0) & (lineNr < nofLines) THEN RETURN lines[lineNr].pos ELSE RETURN 0 END
  130. END GetLineStartPos;
  131. (** return the length in characters of this line *)
  132. PROCEDURE GetLineLength(lineNr : LONGINT) : LONGINT;
  133. BEGIN
  134. IF (lineNr >= 0) & (lineNr < nofLines - 1) THEN RETURN lines[lineNr + 1].pos - lines[lineNr].pos
  135. ELSE
  136. IF (lineNr >= 0) & (lineNr < nofLines) THEN RETURN text.GetLength() - lines[lineNr].pos + 1
  137. ELSE RETURN 0
  138. END
  139. END
  140. END GetLineLength;
  141. PROCEDURE GetNofLines() : LONGINT;
  142. BEGIN
  143. RETURN nofLines
  144. END GetNofLines;
  145. PROCEDURE LayoutLine(VAR pos : LONGINT; VAR lineInfo : LineInfo);
  146. VAR
  147. dummyCh : Char32;
  148. BEGIN
  149. IF layoutLineProc # NIL THEN layoutLineProc(pos, dummyCh, lineInfo, paperWidth, -1, -1) END
  150. END LayoutLine;
  151. (* generate a new layout from scratch. if the text has not actually changed, no bidi-reformatting needs to be done *)
  152. PROCEDURE FullLayout(textChanged : BOOLEAN);
  153. VAR i, pos, oldpos : LONGINT;
  154. BEGIN
  155. ASSERT((text # NIL) & (lines#NIL));
  156. text.AcquireRead;
  157. textWidth := 0;
  158. IF TraceLayout IN Trace THEN KernelLog.String("FullLayout"); KernelLog.Ln END;
  159. (* create a new bidiformatter and reformat the whole text if necessary *)
  160. IF textChanged & initialized & text.isUTF THEN
  161. NEW(bidiFormatter,text);
  162. bidiFormatter.ReformatText;
  163. END;
  164. i := 0;
  165. pos := 0; nofLines := 0; textHeight := 0;
  166. WHILE pos < text.GetLength() DO
  167. oldpos := pos;
  168. LayoutLine(pos, lines[nofLines]); INC(textHeight, lines[nofLines].height);
  169. textWidth := Strings.Max(textWidth, lines[nofLines].width);
  170. ASSERT(pos > oldpos);
  171. IF TraceLayout IN Trace THEN KernelLog.String("Line from : "); KernelLog.Int(lines[nofLines].pos, 5); KernelLog.Ln END;
  172. INC(nofLines); IF nofLines >= LEN(lines) THEN GrowLines END
  173. END;
  174. IF TraceLayout IN Trace THEN KernelLog.String("FullLayout found "); KernelLog.Int(nofLines, 4); KernelLog.String(" lines"); KernelLog.Ln END;
  175. text.ReleaseRead
  176. END FullLayout;
  177. (** Fix the layouting of the text starting at pos where delta characters have been inserted (delta negativ if deleted) *)
  178. PROCEDURE FixLayoutFrom(pos, delta : LONGINT; VAR first, last : LONGINT; VAR linesChanged : BOOLEAN);
  179. VAR l, dl, oldh : LONGINT;
  180. BEGIN
  181. ASSERT(text # NIL);
  182. text.AcquireRead;
  183. linesChanged := FALSE;
  184. l := FindLineNrByPos(pos);
  185. IF (l < 0) THEN FullLayout(TRUE); first := 0; last := nofLines; text.ReleaseRead; RETURN END;
  186. pos := lines[l].pos;
  187. oldh := lines[l].height;
  188. LayoutLine(pos, lines[l]);
  189. IF oldh # lines[l].height THEN linesChanged := TRUE END;
  190. first := l;
  191. INC(l); dl := 0;
  192. IF (delta < 0) THEN
  193. IF (l >= nofLines) OR (lines[l].pos + delta = pos) THEN
  194. last := l;
  195. WHILE (l < nofLines) DO lines[l].pos := lines[l].pos + delta; INC(l) END
  196. ELSE
  197. linesChanged := TRUE;
  198. WHILE (pos < text.GetLength()) DO
  199. DEC(textHeight, lines[l].height);
  200. LayoutLine(pos, lines[l]);
  201. textWidth := Strings.Max(textWidth, lines[l].width);
  202. INC(textHeight, lines[nofLines].height);
  203. INC(dl);
  204. IF TraceLayout IN Trace THEN KernelLog.String("Line from : "); KernelLog.Int(lines[nofLines].pos, 5); KernelLog.Ln END;
  205. INC(l);
  206. IF l >= LEN(lines) THEN GrowLines END
  207. END;
  208. nofLines := l ;
  209. last := nofLines - 1
  210. END
  211. ELSE
  212. WHILE (pos < text.GetLength()) & (lines[l].pos + delta # pos) DO
  213. linesChanged := TRUE;
  214. DEC(textHeight, (lines[l].height));
  215. LayoutLine(pos, lines[l]);
  216. textWidth := Strings.Max(textWidth, lines[l].width);
  217. INC(textHeight, (lines[nofLines].height));
  218. INC(dl);
  219. IF TraceLayout IN Trace THEN KernelLog.String("Line from : "); KernelLog.Int(lines[nofLines].pos, 5); KernelLog.Ln END;
  220. INC(l); IF l >= LEN(lines) THEN GrowLines END
  221. END;
  222. last := l;
  223. IF TraceLayout IN Trace THEN
  224. KernelLog.String("Delta Lines : "); KernelLog.Int(dl, 4); KernelLog.Ln;
  225. KernelLog.String("Lines to redraw : "); KernelLog.Int(first, 5); KernelLog.String(" to "); KernelLog.Int(last, 5); KernelLog.Ln
  226. END;
  227. (* fix up the positions *)
  228. IF l < nofLines THEN WHILE (l < nofLines) DO lines[l].pos := lines[l].pos + delta; INC(l) END
  229. ELSE nofLines := l
  230. END
  231. END;
  232. text.ReleaseRead
  233. END FixLayoutFrom;
  234. END Layout;
  235. CONST HLOver* = 0; HLUnder* = 1; HLWave* = 2;
  236. TYPE
  237. Highlight* = OBJECT
  238. VAR
  239. kind : LONGINT;
  240. from*, to* : Texts.TextPosition;
  241. a*, b* : LONGINT; (* only valid after sort, while holding the lock *)
  242. active* : BOOLEAN; (* only valid after sort, while holding the lock *)
  243. oldFrom, oldTo : LONGINT;
  244. oldColor, color : WMGraphics.Color;
  245. text : Texts.UnicodeText;
  246. onChanged : WMMessages.CompCommand;
  247. PROCEDURE &New*;
  248. BEGIN
  249. color := SelectionColor;
  250. oldColor := color;
  251. END New;
  252. PROCEDURE SetKind*(kind : LONGINT);
  253. BEGIN
  254. IF SELF.kind # kind THEN
  255. SELF.kind := kind;
  256. onChanged(SELF, NIL)
  257. END
  258. END SetKind;
  259. PROCEDURE SetColor*(color : WMGraphics.Color);
  260. BEGIN
  261. oldColor := SELF.color;
  262. IF SELF.color # color THEN
  263. SELF.color := color;
  264. onChanged(SELF, NIL)
  265. END
  266. END SetColor;
  267. PROCEDURE SetFrom*(from : LONGINT);
  268. BEGIN
  269. IF text = NIL THEN RETURN END; (* if no text is set, the position within is undef *)
  270. text.AcquireRead;
  271. oldFrom := SELF.from.GetPosition();
  272. IF oldFrom # from THEN
  273. SELF.from.SetPosition(from);
  274. onChanged(SELF, NIL)
  275. END;
  276. text.ReleaseRead
  277. END SetFrom;
  278. PROCEDURE SetTo*(to : LONGINT);
  279. BEGIN
  280. IF text = NIL THEN RETURN END; (* if no text is set, the position within is undef *)
  281. text.AcquireRead;
  282. oldTo := SELF.to.GetPosition();
  283. IF oldTo # to THEN
  284. SELF.to.SetPosition(to);
  285. onChanged(SELF, NIL)
  286. END;
  287. text.ReleaseRead
  288. END SetTo;
  289. PROCEDURE SetFromTo*(from, to : LONGINT);
  290. BEGIN
  291. IF text = NIL THEN RETURN END; (* if no text is set, the position within is undef *)
  292. text.AcquireRead;
  293. oldTo := SELF.to.GetPosition();
  294. oldFrom := SELF.from.GetPosition();
  295. IF (oldTo # to) OR (oldFrom # from) THEN
  296. IF ((oldTo = oldFrom) & (to = from)) THEN
  297. SELF.to.SetPosition(to);
  298. SELF.from.SetPosition(from)
  299. ELSE
  300. SELF.to.SetPosition(to);
  301. SELF.from.SetPosition(from);
  302. onChanged(SELF, NIL)
  303. END
  304. END;
  305. text.ReleaseRead
  306. END SetFromTo;
  307. PROCEDURE Sort*;
  308. VAR t : LONGINT;
  309. BEGIN
  310. a := from.GetPosition();
  311. b := to.GetPosition();
  312. IF a > b THEN t := a; a := b; b := t END;
  313. active := a # b
  314. END Sort;
  315. PROCEDURE SetText(text : Texts.UnicodeText);
  316. BEGIN
  317. IF text # NIL THEN SELF.text := text; NEW(from, text); NEW(to, text) END
  318. END SetText;
  319. END Highlight;
  320. HighlightArray = POINTER TO ARRAY OF Highlight;
  321. TYPE
  322. PositionMarker* = OBJECT
  323. VAR
  324. pos : Texts.TextPosition;
  325. img : WMGraphics.Image;
  326. color : WMGraphics.Color;
  327. hotX, hotY : LONGINT;
  328. currentArea : WMRectangles.Rectangle;
  329. text : Texts.UnicodeText;
  330. onChanged : WMMessages.CompCommand;
  331. visible : BOOLEAN;
  332. PROCEDURE &Init*;
  333. BEGIN
  334. color := LONGINT(0FF0000CCH); visible := TRUE
  335. END Init;
  336. PROCEDURE Draw(canvas : WMGraphics.Canvas; x, y, ascent : LONGINT);
  337. BEGIN
  338. IF ~visible THEN RETURN END;
  339. IF img # NIL THEN canvas.DrawImage(x - hotX, y - hotY, img, WMGraphics.ModeSrcOverDst)
  340. ELSE
  341. currentArea := GetArea(x, y, ascent);
  342. canvas.Fill(currentArea, LONGINT(0FF0000CCH), WMGraphics.ModeSrcOverDst)
  343. END
  344. END Draw;
  345. PROCEDURE GetArea(x, y, ascent : LONGINT) : WMRectangles.Rectangle;
  346. BEGIN
  347. IF img # NIL THEN RETURN WMRectangles.MakeRect(x - hotX, y - hotY, x - hotX + img.width, y - hotY + img.height)
  348. ELSE RETURN WMRectangles.MakeRect(x , y - ascent, x + 2, y)
  349. END
  350. END GetArea;
  351. PROCEDURE Load*(CONST filename : ARRAY OF CHAR);
  352. BEGIN
  353. img := WMGraphics.LoadImage(filename, TRUE);
  354. IF img # NIL THEN hotX := img.width DIV 2; hotY := img.height DIV 2 END;
  355. onChanged(SELF, NIL)
  356. END Load;
  357. PROCEDURE SetVisible*(visible : BOOLEAN);
  358. BEGIN
  359. IF SELF.visible # visible THEN
  360. SELF.visible := visible;
  361. onChanged(SELF, NIL)
  362. END
  363. END SetVisible;
  364. PROCEDURE SetPosition*(pos : LONGINT);
  365. BEGIN
  366. IF text = NIL THEN RETURN END; (* if no text is set, the position within is undef *)
  367. text.AcquireRead;
  368. IF pos # SELF.pos.GetPosition() THEN
  369. SELF.pos.SetPosition(pos);
  370. onChanged(SELF, NIL)
  371. END;
  372. text.ReleaseRead
  373. END SetPosition;
  374. PROCEDURE GetPosition*() : LONGINT;
  375. BEGIN
  376. RETURN pos.GetPosition()
  377. END GetPosition;
  378. PROCEDURE SetColor*(color : WMGraphics.Color);
  379. BEGIN
  380. IF SELF.color # color THEN
  381. SELF.color := color;
  382. onChanged(SELF, NIL)
  383. END
  384. END SetColor;
  385. PROCEDURE SetText(text : Texts.UnicodeText);
  386. BEGIN
  387. IF text # NIL THEN
  388. SELF.text := text;
  389. NEW(pos, text);
  390. END
  391. END SetText;
  392. PROCEDURE SetNextInternalPosition*(next : LONGINT);
  393. BEGIN
  394. pos.nextInternalPos := next;
  395. END SetNextInternalPosition;
  396. PROCEDURE GetNextInternalPosition*() : LONGINT;
  397. BEGIN
  398. RETURN pos.nextInternalPos;
  399. END GetNextInternalPosition;
  400. END PositionMarker;
  401. PositionMarkerArray = POINTER TO ARRAY OF PositionMarker;
  402. TYPE
  403. Cursor = OBJECT(PositionMarker)
  404. VAR
  405. isVisible : BOOLEAN;
  406. PROCEDURE &Init*;
  407. BEGIN
  408. isVisible := TRUE;
  409. END Init;
  410. PROCEDURE SetCurrentVisibility(isVisible : BOOLEAN);
  411. BEGIN
  412. IF (SELF.isVisible # isVisible) THEN
  413. SELF.isVisible := isVisible;
  414. onChanged(SELF, NIL);
  415. END;
  416. END SetCurrentVisibility;
  417. PROCEDURE GetArea(x, y, ascent : LONGINT) : WMRectangles.Rectangle;
  418. BEGIN
  419. IF img # NIL THEN RETURN WMRectangles.MakeRect(x - hotX, y - hotY, x - hotX + img.width, y - hotY + img.height)
  420. ELSE RETURN WMRectangles.MakeRect(x , y - ascent - 2, x + 1, y + 2)
  421. END
  422. END GetArea;
  423. PROCEDURE Draw(canvas : WMGraphics.Canvas; x, y, ascent : LONGINT);
  424. BEGIN
  425. IF ~visible OR ~isVisible THEN RETURN END;
  426. IF img # NIL THEN canvas.DrawImage(x - hotX, y - hotY, img, WMGraphics.ModeSrcOverDst)
  427. ELSE
  428. currentArea := GetArea(x, y, ascent);
  429. canvas.Fill(currentArea, WMGraphics.Black, WMGraphics.ModeSrcOverDst)
  430. END
  431. END Draw;
  432. END Cursor;
  433. TYPE
  434. CursorBlinkerCallback = PROCEDURE {DELEGATE} (isVisible : BOOLEAN);
  435. (** Global thread that periodically toggles the visibility of the currently active cursor *)
  436. CursorBlinker* = OBJECT
  437. VAR
  438. cursor : ANY;
  439. callback : CursorBlinkerCallback;
  440. interval : LONGINT;
  441. isVisible : BOOLEAN;
  442. alive, dead : BOOLEAN;
  443. timer : Kernel.Timer;
  444. PROCEDURE &Init;
  445. BEGIN
  446. cursor := NIL; callback := NIL;
  447. interval := 500;
  448. isVisible := TRUE;
  449. alive := TRUE; dead := FALSE;
  450. NEW(timer);
  451. END Init;
  452. (** Set the currently active cursor and a callback that will be periodically called *)
  453. PROCEDURE Set*(cursor : ANY; callback : CursorBlinkerCallback);
  454. BEGIN {EXCLUSIVE}
  455. ASSERT((cursor # NIL) & (callback # NIL));
  456. IF (SELF.cursor # NIL) THEN callback(TRUE); END;
  457. SELF.cursor := cursor;
  458. SELF.callback := callback;
  459. isVisible := TRUE;
  460. timer.Wakeup;
  461. END Set;
  462. (** Set the cursor blinking interval in milliseconds. An interval of MAX(LONGINT) means don't blink *)
  463. PROCEDURE SetInterval*(ms : LONGINT);
  464. BEGIN {EXCLUSIVE}
  465. ASSERT(ms > 0);
  466. interval := ms;
  467. timer.Wakeup;
  468. IF (interval = MAX(LONGINT)) & (cursor # NIL) THEN
  469. isVisible := TRUE;
  470. callback(isVisible);
  471. END;
  472. END SetInterval;
  473. (** If 'cursor' is the currently active cursor, set the currently active cursor to NIL *)
  474. PROCEDURE Remove*(cursor : ANY);
  475. BEGIN {EXCLUSIVE}
  476. ASSERT(cursor # NIL);
  477. IF (SELF.cursor = cursor) THEN
  478. SELF.cursor := NIL;
  479. SELF.callback := NIL;
  480. END;
  481. END Remove;
  482. (** If 'cursor' is the currently active cursor, show it for one period *)
  483. PROCEDURE Show*(cursor : ANY);
  484. BEGIN {EXCLUSIVE}
  485. ASSERT(cursor # NIL);
  486. IF (SELF.cursor = cursor) THEN
  487. isVisible := TRUE;
  488. timer.Wakeup;
  489. END;
  490. END Show;
  491. PROCEDURE Finalize;
  492. BEGIN
  493. BEGIN {EXCLUSIVE} alive := FALSE; END;
  494. timer.Wakeup;
  495. BEGIN {EXCLUSIVE} AWAIT(dead); END;
  496. END Finalize;
  497. BEGIN {ACTIVE}
  498. WHILE alive DO
  499. BEGIN {EXCLUSIVE}
  500. AWAIT(~alive OR ((cursor # NIL) & (interval # MAX(LONGINT))));
  501. IF alive THEN
  502. callback(isVisible);
  503. isVisible := ~isVisible;
  504. END;
  505. END;
  506. timer.Sleep(interval);
  507. END;
  508. BEGIN {EXCLUSIVE} dead := TRUE; END;
  509. END CursorBlinker;
  510. TYPE
  511. TextDropTarget* = OBJECT(WMDropTarget.DropTarget);
  512. VAR text : Texts.Text;
  513. pos : Texts.TextPosition;
  514. PROCEDURE &New*(text : Texts.Text; pos : Texts.TextPosition);
  515. BEGIN
  516. SELF.text := text; SELF.pos := pos
  517. END New;
  518. PROCEDURE GetInterface*(type : LONGINT) : WMDropTarget.DropInterface;
  519. VAR di : WMDropTarget.DropText;
  520. BEGIN
  521. IF type = WMDropTarget.TypeText THEN
  522. NEW(di); di.text := text; di.pos := pos;
  523. RETURN di
  524. ELSE RETURN NIL
  525. END
  526. END GetInterface;
  527. END TextDropTarget;
  528. TYPE
  529. LinkWrapper* = POINTER TO RECORD
  530. link* : Texts.Link;
  531. END;
  532. TYPE
  533. TextView* = OBJECT(WMComponents.VisualComponent)
  534. VAR
  535. defaultTextColor-, defaultTextBgColor- : WMProperties.ColorProperty;
  536. defaultTextColorI, defaultTextBgColorI : LONGINT;
  537. isMultiLine- : WMProperties.BooleanProperty;
  538. isMultiLineI : BOOLEAN;
  539. (** Text wrapping mode: NoWrap, Wrap or WrapWord (default : WrapWord) *)
  540. wrapMode- : WMProperties.Int32Property;
  541. wrapModeI : LONGINT;
  542. firstLine- : WMProperties.Int32Property;
  543. firstLineI : LONGINT;
  544. leftShift- : WMProperties.Int32Property;
  545. leftShiftI : LONGINT; (* number of units, the view is shifted to left -> line scrolling *)
  546. showBorder- : WMProperties.BooleanProperty;
  547. showBorderI : BOOLEAN;
  548. borders- : WMProperties.RectangleProperty;
  549. bordersI, borderClip : WMRectangles.Rectangle;
  550. x0 : LONGINT; (* text starts at x = x0. Used to get column for line numbers in subclass CodeView *)
  551. alwaysShowCursor- : WMProperties.BooleanProperty;
  552. alwaysShowCursorI : BOOLEAN;
  553. showLabels- : WMProperties.BooleanProperty;
  554. (** Is set to TRUE, the characters will be replaced by passwordChar *)
  555. isPassword- : WMProperties.BooleanProperty;
  556. isPasswordI : BOOLEAN; (* cache of the property value to avoid per-character-locks *)
  557. passwordChar- : WMProperties.Int32Property; (* not cached *)
  558. (** Mouse wheel scrolling speed multiplier? (default: 3, 0: disable mouse wheel scrolling) *)
  559. mouseWheelScrollSpeed- : WMProperties.Int32Property;
  560. mouseWheelScrollSpeedI : LONGINT;
  561. (** Allow middle-click command execution? (default: TRUE) *)
  562. allowCommandExecution- : WMProperties.BooleanProperty;
  563. (** Allow text selection using the mouse? (default: TRUE) *)
  564. allowTextSelection- : WMProperties.BooleanProperty;
  565. (** Should a mouse right-click open the pie menu? (default : TRUE) *)
  566. allowPiemenu- : WMProperties.BooleanProperty;
  567. (** Syntax highlighting *)
  568. highlighting- : WMProperties.StringProperty;
  569. highlighter : SyntaxHighlighter.Highlighter;
  570. state : SyntaxHighlighter.State;
  571. fontCache : FontCache;
  572. (** vertical centering -- momentarily only working for a single line *)
  573. textAlignV-: WMProperties.Int32Property;
  574. showLineNumbers- : WMProperties.BooleanProperty;
  575. showLineNumbersI : BOOLEAN;
  576. lineNumberColor-, lineNumberBgColor- : WMProperties.ColorProperty;
  577. lineNumberColorI, lineNumberBgColorI : LONGINT;
  578. lineNumberFont, lineNumberFont10 : WMGraphics.Font;
  579. indicateTabs- : WMProperties.BooleanProperty;
  580. indicateTabsI : BOOLEAN;
  581. clBgCurrentLine- : WMProperties.ColorProperty;
  582. clBgCurrentLineI : LONGINT;
  583. selection- : Highlight;
  584. cursor- : Cursor;
  585. onLinkClicked- : WMEvents.EventSource;
  586. onCtrlClicked- : WMEvents.EventSource;
  587. (** Commands.Context.caller will be set to this object when executing a command *)
  588. commandCaller*: OBJECT;
  589. commandWriter*, errorWriter*: Streams.Writer;
  590. (** Called whenever the cursor position changes *)
  591. onCursorChanged* : PROCEDURE {DELEGATE};
  592. optimize* : BOOLEAN;
  593. piemenu : WMPieMenu.Menu;
  594. text : Texts.Text;
  595. layout : Layout;
  596. utilreader : Texts.TextReader; (* single process ! *)
  597. clipState : WMGraphics.CanvasState;
  598. defaultTabStops : TabStops;
  599. vScrollbar : WMStandardComponents.Scrollbar;
  600. hScrollbar : WMStandardComponents.Scrollbar;
  601. (* highlighting *)
  602. nofHighlights : LONGINT;
  603. highlights : HighlightArray;
  604. (* marked positions *)
  605. nofPositionMarkers : LONGINT;
  606. positionMarkers : PositionMarkerArray;
  607. lastCursorPos: LONGINT;
  608. selecting : BOOLEAN;
  609. doubleclickedWord : BOOLEAN;
  610. dragPossible : BOOLEAN;
  611. dragSelA, dragSelB : Texts.TextPosition;
  612. dragCopy : BOOLEAN;
  613. canStart, openFile : BOOLEAN; (* set for command selection mode *)
  614. commandMarker : Highlight;
  615. downX, downY : LONGINT;
  616. selectWords : BOOLEAN;
  617. wordSelOrdered : BOOLEAN;
  618. lineEnter : LONGINT;
  619. modifierFlags : SET;
  620. oldFlags : SET; (* old pointer flags *)
  621. interclick : LONGINT;
  622. lastTimeStamp : LONGINT;
  623. oldObject, focusObject : ANY;
  624. oldPos, focusPos : LONGINT;
  625. objHasFocus : BOOLEAN;
  626. PROCEDURE &Init*;
  627. BEGIN
  628. Init^;
  629. SetGenerator("WMTextView.GenTextView");
  630. SetNameAsString(StrTextView);
  631. (* properties *)
  632. NEW(defaultTextColor, PTVdefaultTextColor, NIL, NIL); properties.Add(defaultTextColor);
  633. NEW(defaultTextBgColor, PTVdefaultTextBgColor, NIL, NIL); properties.Add(defaultTextBgColor);
  634. NEW(isMultiLine, PTVIsMultiLine, NIL, NIL); properties.Add(isMultiLine);
  635. NEW(wrapMode, PTVWrapMode, NIL, NIL); properties.Add(wrapMode);
  636. NEW(firstLine, PTVfirstLine, NIL, NIL); properties.Add(firstLine);
  637. NEW(leftShift, PTVleftShift, NIL, NIL); properties.Add(leftShift);
  638. NEW(showBorder, PTVShowBorder, NIL, NIL); properties.Add(showBorder);
  639. NEW(borders, PTVborders, NIL, NIL); properties.Add(borders);
  640. NEW(alwaysShowCursor, PTValwaysShowCursor, NIL, NIL); properties.Add(alwaysShowCursor);
  641. NEW(showLabels, PTVShowLabels, NIL, NIL); properties.Add(showLabels);
  642. NEW(isPassword, PTVIsPassword, NIL, NIL); properties.Add(isPassword);
  643. NEW(passwordChar, PTVPasswordChar, NIL, NIL); properties.Add(passwordChar);
  644. NEW(mouseWheelScrollSpeed, PTVMouseWheelScrollSpeed, NIL, NIL); properties.Add(mouseWheelScrollSpeed);
  645. NEW(allowCommandExecution, PTVAllowCommandExecution, NIL, NIL); properties.Add(allowCommandExecution);
  646. NEW(allowTextSelection, PTVAllowTextSelection, NIL, NIL); properties.Add(allowTextSelection);
  647. NEW(allowPiemenu, PTVAllowPiemenu, NIL, NIL); properties.Add(allowPiemenu);
  648. NEW(highlighting, PTVHighlighting, NIL, NIL); properties.Add(highlighting);
  649. highlighter := NIL; state := NIL; fontCache := NIL;
  650. NEW(showLineNumbers, PTVShowLineNumbers, NIL, NIL); properties.Add(showLineNumbers);
  651. NEW(lineNumberColor, PTVLineNumberColor, NIL, NIL); properties.Add(lineNumberColor);
  652. NEW(lineNumberBgColor, PTVLineNumberBgColor, NIL, NIL); properties.Add(lineNumberBgColor);
  653. lineNumberFont := NIL; lineNumberFont10 := NIL;
  654. NEW(indicateTabs, PTVIndicateTabs, NIL, NIL); properties.Add(indicateTabs);
  655. NEW(clBgCurrentLine, PTVclBgCurrentLine, NIL, NIL); properties.Add(clBgCurrentLine);
  656. NEW(textAlignV, PVTtextAlignV, NIL, NIL); properties.Add(textAlignV);
  657. (* events *)
  658. NEW(onLinkClicked, SELF, PTVonLinkClick, PTVonLinkClickInfo, SELF.StringToCompCommand); events.Add(onLinkClicked);
  659. onLinkClicked.Add(LinkClicked);
  660. NEW(onCtrlClicked, SELF, PTVonCtrlLinkClick, PTVonCtrlLinkClickInfo, SELF.StringToCompCommand); events.Add(onCtrlClicked);
  661. (* selection and cursor *)
  662. NEW(layout);
  663. layout.layoutLineProc := LayoutLine;
  664. nofHighlights := 0;
  665. NEW(highlights, 4);
  666. nofPositionMarkers := 0;
  667. NEW(positionMarkers, 4); nofPositionMarkers := 0;
  668. selection := CreateHighlight();
  669. selection.kind := HLOver;
  670. selection.color := SelectionColor;
  671. cursor := CreateCursor();
  672. commandCaller := NIL;
  673. commandWriter := NIL;
  674. onCursorChanged := NIL;
  675. (* Initialization of internal fields *)
  676. optimize := FALSE;
  677. piemenu := NIL;
  678. text := NIL;
  679. utilreader := NIL;
  680. NEW(defaultTabStops); defaultTabStops.tabDist := 20;
  681. vScrollbar := NIL; hScrollbar := NIL;
  682. lastCursorPos := -1;
  683. selecting := FALSE;
  684. doubleclickedWord := FALSE;
  685. dragPossible := FALSE;
  686. dragSelA := NIL; dragSelB := NIL;
  687. canStart := FALSE; openFile := FALSE;
  688. downX := 0; downY := 0;
  689. selectWords := FALSE;
  690. wordSelOrdered := FALSE;
  691. lineEnter := 0;
  692. modifierFlags := {}; oldFlags := {};
  693. interclick := InterclickNone;
  694. lastTimeStamp := 0;
  695. oldObject := NIL; focusObject := NIL;
  696. oldPos := 0; focusPos := 0;
  697. objHasFocus := FALSE;
  698. takesFocus.Set(TRUE);
  699. needsTab.Set(TRUE);
  700. SetPointerInfo(manager.pointerText);
  701. END Init;
  702. PROCEDURE Initialize*;
  703. BEGIN
  704. ASSERT(IsCallFromSequencer());
  705. (*Initialize^; RecacheProperties;*)
  706. IF text#NIL THEN Resized END; (*implicit redundant invalidate in Resized *)(*! Resized is probably redundant*)
  707. (* from now on, bidi-formatting can be done *)
  708. layout.initialized := TRUE;
  709. Initialize^;
  710. cursor.SetVisible(FALSE);
  711. END Initialize;
  712. PROCEDURE Finalize;
  713. BEGIN
  714. Finalize^;
  715. IF text # NIL THEN text.onTextChanged.Remove(TextChanged); END;
  716. cursorBlinker.Remove(cursor);
  717. END Finalize;
  718. PROCEDURE FocusReceived*;
  719. BEGIN
  720. FocusReceived^;
  721. cursor.SetVisible(TRUE);
  722. cursorBlinker.Set(cursor, cursor.SetCurrentVisibility);
  723. (* let the module know that this is the currently visible TextView *)
  724. currentTextView := SELF;
  725. END FocusReceived;
  726. PROCEDURE FocusLost*;
  727. BEGIN
  728. FocusLost^;
  729. modifierFlags := {};
  730. cursorBlinker.Remove(cursor);
  731. SetInterclick(InterclickNone);
  732. IF ~alwaysShowCursorI THEN cursor.SetVisible(FALSE); END;
  733. END FocusLost;
  734. (* Inserts a character directly into the text. This should be used by external tools that insert character
  735. without the usage of the keyboard, e.g. WMUnicodeMarkerTool) *)
  736. PROCEDURE InsertChar(char : Char32) : INTEGER;
  737. VAR oneCharString : ARRAY 2 OF Texts.Char32;
  738. BEGIN
  739. (* Only insert a character into a valid text, that is either utf-formatted or gets a simple ASCII-character
  740. as input. *)
  741. IF text # NIL THEN
  742. IF text.isUTF OR (char < 256) THEN
  743. oneCharString[0] := char;
  744. oneCharString[1] := 0H;
  745. text.AcquireWrite;
  746. text.InsertUCS32(GetInternalPos(cursor.GetPosition()),oneCharString);
  747. text.ReleaseWrite;
  748. RETURN 0;
  749. ELSE
  750. RETURN -1;
  751. END;
  752. ELSE
  753. RETURN -2;
  754. END;
  755. END InsertChar;
  756. PROCEDURE RecacheProperties;
  757. VAR
  758. highlighter : SyntaxHighlighter.Highlighter;
  759. oldBorders : WMRectangles.Rectangle;
  760. string : Strings.String;
  761. BEGIN
  762. ASSERT(IsCallFromSequencer());
  763. RecacheProperties^;
  764. defaultTextColorI := defaultTextColor.Get();
  765. defaultTextBgColorI := defaultTextBgColor.Get();
  766. isMultiLineI := isMultiLine.Get();
  767. wrapModeI := wrapMode.Get();
  768. firstLineI := firstLine.Get();
  769. leftShiftI := leftShift.Get();
  770. showBorderI := showBorder.Get();
  771. oldBorders := bordersI;
  772. bordersI := borders.Get();
  773. alwaysShowCursorI := alwaysShowCursor.Get();
  774. mouseWheelScrollSpeedI := mouseWheelScrollSpeed.Get();
  775. isPasswordI := isPassword.Get();
  776. showLineNumbersI := showLineNumbers.Get();
  777. ShowLineNumbers(showLineNumbersI);
  778. lineNumberColorI := lineNumberColor.Get();
  779. lineNumberBgColorI := lineNumberBgColor.Get();
  780. indicateTabsI := indicateTabs.Get();
  781. clBgCurrentLineI := clBgCurrentLine.Get();
  782. string := highlighting.Get();
  783. IF (string # NIL) THEN
  784. highlighter := SyntaxHighlighter.GetHighlighter(string^);
  785. ELSE
  786. highlighter := NIL;
  787. END;
  788. IF text#NIL THEN
  789. SetSyntaxHighlighter(highlighter);
  790. UpdateScrollbars;
  791. IF ~WMRectangles.IsEqual(oldBorders, bordersI) THEN BordersChanged END;
  792. END;
  793. (*Invalidate;*)
  794. END RecacheProperties;
  795. PROCEDURE SetScrollbars*(hScrollbar, vScrollbar : WMStandardComponents.Scrollbar);
  796. BEGIN
  797. Acquire;
  798. IF hScrollbar # NIL THEN hScrollbar.onPositionChanged.Remove(ScrollbarsChanged) END;
  799. IF vScrollbar # NIL THEN vScrollbar.onPositionChanged.Remove(ScrollbarsChanged) END;
  800. SELF.hScrollbar := hScrollbar; SELF.vScrollbar := vScrollbar;
  801. IF hScrollbar # NIL THEN hScrollbar.onPositionChanged.Add(ScrollbarsChanged) END;
  802. IF vScrollbar # NIL THEN vScrollbar.onPositionChanged.Add(ScrollbarsChanged) END;
  803. UpdateScrollbars;
  804. Release
  805. END SetScrollbars;
  806. PROCEDURE ScrollbarsChanged(sender, data : ANY);
  807. BEGIN
  808. IF ~IsCallFromSequencer() THEN sequencer.ScheduleEvent(SELF.ScrollbarsChanged, sender, data)
  809. ELSE
  810. IF sender = vScrollbar THEN firstLine.Set(vScrollbar.pos.Get())
  811. ELSIF sender = hScrollbar THEN leftShift.Set(hScrollbar.pos.Get())
  812. END
  813. END
  814. END ScrollbarsChanged;
  815. PROCEDURE UpdateScrollbars;
  816. BEGIN
  817. IF vScrollbar # NIL THEN
  818. vScrollbar.max.Set(layout.GetNofLines());
  819. vScrollbar.pos.Set(firstLineI);
  820. END;
  821. IF hScrollbar # NIL THEN
  822. IF (wrapModeI # NoWrap) THEN
  823. hScrollbar.visible.Set(FALSE);
  824. ELSE
  825. hScrollbar.visible.Set(TRUE);
  826. (* hScrollbar.visible.Set(layout.textWidth > bounds.GetWidth()); *)
  827. hScrollbar.max.Set(layout.textWidth);
  828. hScrollbar.pageSize.Set(bounds.GetWidth());
  829. hScrollbar.pos.Set(leftShiftI);
  830. END;
  831. END;
  832. END UpdateScrollbars;
  833. PROCEDURE BordersChanged;
  834. VAR vScroll : LONGINT;
  835. BEGIN
  836. ASSERT(IsCallFromSequencer());
  837. IF (vScrollbar # NIL) & (vScrollbar.visible.Get()) THEN vScroll := vScrollbar.bounds.GetWidth() ELSE vScroll := 0 END;
  838. borderClip := WMRectangles.MakeRect(bordersI.l, bordersI.t, bounds.GetWidth() - bordersI.r, bounds.GetHeight() - bordersI.b);
  839. layout.paperWidth := bounds.GetWidth() - (bordersI.l + bordersI.r) - vScroll;
  840. layout.FullLayout(FALSE); CheckNumberOfLines;
  841. END BordersChanged;
  842. PROCEDURE WrapModeChanged;
  843. BEGIN
  844. ASSERT(IsCallFromSequencer());
  845. wrapModeI := wrapMode.Get();
  846. IF (wrapModeI # NoWrap) THEN
  847. leftShift.Set(0); leftShiftI := 0; (* no scrollbars -> don't shift *)
  848. END;
  849. optimize := TRUE;
  850. layout.FullLayout(optimize);
  851. optimize := FALSE;
  852. UpdateScrollbars;
  853. (*Invalidate;*)
  854. END WrapModeChanged;
  855. PROCEDURE PropertyChanged*(sender, property : ANY);
  856. VAR
  857. highlighter : SyntaxHighlighter.Highlighter;
  858. oldBorders : WMRectangles.Rectangle;
  859. string : Strings.String;
  860. BEGIN
  861. IF property = defaultTextColor THEN
  862. defaultTextColorI := defaultTextColor.Get(); Invalidate;
  863. ELSIF property = defaultTextBgColor THEN
  864. defaultTextBgColorI := defaultTextBgColor.Get(); Invalidate;
  865. ELSIF property = isMultiLine THEN
  866. isMultiLineI := isMultiLine.Get(); Invalidate;
  867. ELSIF property = wrapMode THEN
  868. wrapModeI := wrapMode.Get(); WrapModeChanged; Invalidate;
  869. ELSIF property = firstLine THEN
  870. firstLineI := firstLine.Get(); UpdateScrollbars; Invalidate;
  871. ELSIF property = leftShift THEN
  872. leftShiftI := leftShift.Get(); UpdateScrollbars; Invalidate;
  873. ELSIF property = showBorder THEN
  874. showBorderI := showBorder.Get(); Invalidate;
  875. ELSIF property = borders THEN
  876. oldBorders := bordersI; bordersI := borders.Get(); BordersChanged; Invalidate;
  877. ELSIF property = alwaysShowCursor THEN
  878. alwaysShowCursorI := alwaysShowCursor.Get();
  879. IF (alwaysShowCursorI = TRUE) THEN cursor.SetVisible(TRUE);
  880. ELSIF ~hasFocus THEN cursor.SetVisible(FALSE);
  881. END;
  882. Invalidate;
  883. ELSIF property = mouseWheelScrollSpeed THEN
  884. mouseWheelScrollSpeedI := mouseWheelScrollSpeed.Get();
  885. ELSIF property = isPassword THEN
  886. isPasswordI := isPassword.Get(); Invalidate;
  887. ELSIF (property = highlighting) THEN
  888. string := highlighting.Get();
  889. IF (string # NIL) THEN
  890. highlighter := SyntaxHighlighter.GetHighlighter(string^);
  891. ELSE
  892. highlighter := NIL;
  893. END;
  894. SetSyntaxHighlighter(highlighter);
  895. ELSIF (property = showLineNumbers) THEN
  896. showLineNumbersI := showLineNumbers.Get();
  897. ShowLineNumbers(showLineNumbersI);
  898. Invalidate;
  899. ELSIF (property = indicateTabs) THEN
  900. indicateTabsI := indicateTabs.Get(); Invalidate;
  901. ELSIF (property = clBgCurrentLine) THEN
  902. clBgCurrentLineI := clBgCurrentLine.Get(); Invalidate;
  903. ELSIF (property = textAlignV) THEN
  904. Invalidate;
  905. ELSIF (property = lineNumberColor) OR (property = lineNumberBgColor) THEN
  906. lineNumberColorI := lineNumberColor.Get();
  907. lineNumberBgColorI := lineNumberBgColor.Get();
  908. Invalidate;
  909. ELSE
  910. PropertyChanged^(sender, property)
  911. END
  912. END PropertyChanged;
  913. PROCEDURE Resized;
  914. VAR prevWidth: LONGINT;
  915. BEGIN
  916. ASSERT(IsCallFromSequencer());
  917. Resized^; (*? here, an implicit Invalidate() is triggered - this is probably redundant *)
  918. prevWidth := layout.paperWidth;
  919. layout.paperWidth := bounds.GetWidth() - (bordersI.l + bordersI.r);
  920. borderClip.r := bounds.GetWidth() - bordersI.r; borderClip.b := bounds.GetHeight() - bordersI.b;
  921. IF (prevWidth # layout.paperWidth) & (wrapMode.Get()#NoWrap) THEN
  922. layout.FullLayout(optimize);
  923. END;
  924. CheckNumberOfLines;
  925. END Resized;
  926. (** Replace the text *)
  927. PROCEDURE SetText*(text : Texts.Text);
  928. VAR i : LONGINT;
  929. BEGIN
  930. ASSERT(text # NIL);
  931. Acquire;
  932. IF SELF.text # NIL THEN SELF.text.onTextChanged.Remove(TextChanged) END; (* unregister the TextChanged listener from the old text *)
  933. SELF.text := text;
  934. text.onTextChanged.Add(TextChanged); (* register the TextChanged listener with the new text*)
  935. NEW(utilreader, text);
  936. (* update all highlights *)
  937. FOR i := 0 TO nofHighlights - 1 DO highlights[i].SetText(text) END;
  938. FOR i := 0 TO nofPositionMarkers - 1 DO
  939. positionMarkers[i].SetText(text);
  940. (* Let the cursor know about the local position-translation procedures *)
  941. IF text.isUTF THEN
  942. positionMarkers[i].pos.SetInternalPositionTranslator(GetInternalPos);
  943. positionMarkers[i].pos.SetDisplayPositionTranslator(GetDisplayPos);
  944. END;
  945. END;
  946. text.AcquireRead; (* also protect SELF.highlighter and SELF.state here *)
  947. IF (highlighter # NIL) THEN
  948. ASSERT(state # NIL);
  949. highlighter.RebuildRegions(utilreader, state);
  950. END;
  951. layout.SetText(text);
  952. layout.FullLayout(TRUE);
  953. CheckNumberOfLines;
  954. ASSERT(((highlighter = NIL) & (state = NIL)) OR ((highlighter # NIL) & (state # NIL)));
  955. text.ReleaseRead;
  956. (*Invalidate;(*! Redundant ?*)*)
  957. Release;
  958. END SetText;
  959. PROCEDURE SetSyntaxHighlighter*(highlighter : SyntaxHighlighter.Highlighter);
  960. BEGIN
  961. ASSERT(text # NIL);
  962. Acquire;
  963. IF (SELF.highlighter # highlighter) & ((SELF.highlighter # NIL) OR (highlighter # NIL)) THEN
  964. text.AcquireRead; (* also protect SELF.highlighter and SELF.state here *)
  965. SELF.highlighter := highlighter;
  966. IF (highlighter # NIL) THEN
  967. IF (state = NIL) THEN
  968. state := highlighter.GetState();
  969. ASSERT(state # NIL);
  970. END;
  971. highlighter.RebuildRegions(utilreader, state);
  972. ELSE
  973. state := NIL;
  974. END;
  975. layout.FullLayout(TRUE);
  976. CheckNumberOfLines;
  977. ASSERT(((highlighter = NIL) & (state = NIL)) OR ((highlighter # NIL) & (state # NIL)));
  978. text.ReleaseRead;
  979. Invalidate;
  980. END;
  981. Release;
  982. END SetSyntaxHighlighter;
  983. PROCEDURE ShowLineNumbers(enabled : BOOLEAN);
  984. VAR font : WMGraphics.Font;
  985. BEGIN
  986. font := GetFont( );
  987. IF enabled THEN
  988. x0 := 55;
  989. lineNumberFont := WMGraphics.GetFont(font.name, font.size, {});
  990. lineNumberFont10 := WMGraphics.GetFont(font.name, font.size, {WMGraphics.FontBold});
  991. ELSE
  992. x0 := 0;
  993. lineNumberFont := NIL;
  994. lineNumberFont10 := NIL;
  995. END;
  996. END ShowLineNumbers;
  997. PROCEDURE SetTabStops*(ts : TabStops);
  998. BEGIN
  999. Acquire;
  1000. defaultTabStops := ts;
  1001. layout.FullLayout(TRUE);
  1002. CheckNumberOfLines;
  1003. Release;
  1004. END SetTabStops;
  1005. (* BEGIN highlighting *)
  1006. PROCEDURE AddHighlight(highlight : Highlight);
  1007. VAR newHighlights : HighlightArray; i : LONGINT;
  1008. BEGIN
  1009. INC(nofHighlights);
  1010. IF nofHighlights > LEN(highlights) THEN
  1011. NEW(newHighlights, LEN(highlights) * 2);
  1012. FOR i := 0 TO LEN(highlights) - 1 DO newHighlights[i] := highlights[i] END;
  1013. highlights := newHighlights;
  1014. END;
  1015. highlights[nofHighlights - 1] := highlight;
  1016. HighlightChanged(highlight, NIL);
  1017. END AddHighlight;
  1018. PROCEDURE CreateHighlight*() : Highlight;
  1019. VAR h : Highlight;
  1020. BEGIN
  1021. Acquire;
  1022. NEW(h); h.SetText(text);
  1023. h.onChanged := HighlightChanged;
  1024. AddHighlight(h);
  1025. Release;
  1026. RETURN h
  1027. END CreateHighlight;
  1028. PROCEDURE RemoveHighlight*(x : Highlight);
  1029. VAR i : LONGINT;
  1030. BEGIN
  1031. Acquire;
  1032. i := 0; WHILE (i < nofHighlights) & (highlights[i] # x) DO INC(i) END;
  1033. IF i < nofHighlights THEN
  1034. WHILE (i < nofHighlights - 1) DO highlights[i] := highlights[i+1]; INC(i) END;
  1035. DEC(nofHighlights);
  1036. highlights[nofHighlights] := NIL
  1037. END;
  1038. HighlightChanged(NIL, NIL);
  1039. Release
  1040. END RemoveHighlight;
  1041. PROCEDURE InvalidateRange(a, b : LONGINT);
  1042. VAR
  1043. t, l0, l1 : LONGINT;
  1044. x0, y0, x1, y1, d : LONGINT;
  1045. ia, ib : LONGINT;
  1046. BEGIN
  1047. ia := GetDisplayPos(a);
  1048. ib := GetDisplayPos(b);
  1049. (* Sort the display positions, not the internal positions so as not to get weird results! *)
  1050. IF ia > ib THEN t := ia; ia := ib; ib := t END;
  1051. l0 := layout.FindLineNrByPos(ia);
  1052. l1 := layout.FindLineNrByPos(ib);
  1053. IF l0 = l1 THEN (* only one line... optimize *)
  1054. LineYPos(l0, y0, y1);
  1055. (* if text is UTF-formatted (and thus might content RTL-text) the whole line is invalidated.
  1056. this might - in some rare cases - be a bit slower than invalidating the minimum rectangle
  1057. but is guaranteed to always be correct. *)
  1058. IF text.isUTF OR (~(FindScreenPos(ia, x0, d) & FindScreenPos(ib, x1, d))) THEN
  1059. x0 := 0; x1 := bounds.GetWidth();
  1060. END;
  1061. InvalidateRect(WMRectangles.MakeRect(x0, y0, x1, y1));
  1062. ELSE
  1063. LineYPos(l0, y0, d); LineYPos(l1, d, y1);
  1064. InvalidateRect(WMRectangles.MakeRect(0, y0, bounds.GetWidth(), y1));
  1065. END;
  1066. IF TraceInvalidate IN Trace THEN KernelLog.String("ir ") END;
  1067. END InvalidateRange;
  1068. PROCEDURE HighlightChanged(sender, data : ANY);
  1069. VAR hl : Highlight; min, max : LONGINT;
  1070. BEGIN
  1071. IF ~initialized THEN RETURN END;
  1072. IF ~IsCallFromSequencer() THEN sequencer.ScheduleEvent(SELF.HighlightChanged, sender, data)
  1073. ELSE
  1074. text.AcquireRead;
  1075. IF (sender # NIL) & (sender IS Highlight) THEN
  1076. hl := sender(Highlight);
  1077. IF ((hl.oldFrom # hl.from.GetPosition()) & (hl.oldTo # hl.to.GetPosition())) OR (hl.oldColor # hl.color) THEN (* both changed *)
  1078. min := Strings.Min(
  1079. Strings.Min(hl.oldFrom, hl.from.GetPosition()),
  1080. Strings.Min(hl.oldTo, hl.to.GetPosition()));
  1081. max := Strings.Max(
  1082. Strings.Max(hl.oldFrom, hl.from.GetPosition()),
  1083. Strings.Max(hl.oldTo, hl.to.GetPosition()));
  1084. InvalidateRange(min, max)
  1085. ELSIF hl.oldTo # hl.to.GetPosition() THEN (* to changed *)
  1086. InvalidateRange(hl.oldTo, hl.to.GetPosition())
  1087. ELSIF hl.oldFrom # hl.from.GetPosition() THEN (* from changed *)
  1088. InvalidateRange(hl.oldFrom, hl.from.GetPosition())
  1089. ELSE (* position noch changed... probably color, style or visibility changed, invalidate range *)
  1090. InvalidateRange(hl.from.GetPosition(),hl.to.GetPosition())
  1091. END
  1092. ELSE
  1093. IF TraceInvalidate IN Trace THEN KernelLog.String("H") END;
  1094. Invalidate
  1095. END;
  1096. text.ReleaseRead
  1097. END
  1098. END HighlightChanged;
  1099. (* END highlighting *)
  1100. (* BEGIN PositionMarkers *)
  1101. PROCEDURE AddPositionMarker(pm : PositionMarker);
  1102. VAR newPositionMarkers : PositionMarkerArray; i : LONGINT;
  1103. BEGIN
  1104. INC(nofPositionMarkers);
  1105. IF nofPositionMarkers > LEN(positionMarkers) THEN
  1106. NEW(newPositionMarkers, LEN(positionMarkers) * 2);
  1107. FOR i := 0 TO LEN(positionMarkers) - 1 DO newPositionMarkers[i] := positionMarkers[i] END;
  1108. positionMarkers := newPositionMarkers
  1109. END;
  1110. positionMarkers[nofPositionMarkers - 1] := pm
  1111. END AddPositionMarker;
  1112. PROCEDURE CreatePositionMarker*() : PositionMarker;
  1113. VAR p : PositionMarker;
  1114. BEGIN
  1115. Acquire;
  1116. NEW(p); p.SetText(text);
  1117. p.onChanged := PositionMarkerChanged;
  1118. AddPositionMarker(p);
  1119. Release;
  1120. RETURN p
  1121. END CreatePositionMarker;
  1122. PROCEDURE CreateCursor*() : Cursor;
  1123. VAR p : Cursor;
  1124. BEGIN
  1125. Acquire;
  1126. NEW(p); p.SetText(text);
  1127. p.onChanged := PositionMarkerChanged;
  1128. AddPositionMarker(p);
  1129. Release;
  1130. RETURN p
  1131. END CreateCursor;
  1132. PROCEDURE RemovePositionMarker*(x : PositionMarker);
  1133. VAR i, xp, yp, l, ascent : LONGINT; newRect : WMRectangles.Rectangle;
  1134. BEGIN
  1135. Acquire;
  1136. i := 0; WHILE (i < nofPositionMarkers) & (positionMarkers[i] # x) DO INC(i) END;
  1137. IF i < nofPositionMarkers THEN
  1138. WHILE (i < nofPositionMarkers - 1) DO positionMarkers[i] := positionMarkers[i+1]; INC(i) END;
  1139. DEC(nofPositionMarkers);
  1140. positionMarkers[nofPositionMarkers] := NIL
  1141. END;
  1142. IF FindScreenPos(x.pos.GetPosition(), xp, yp) THEN
  1143. l := layout.FindLineNrByPos(x.pos.GetPosition());
  1144. IF (l < LEN(layout.lines^)) & (l >= 0) THEN
  1145. ascent := layout.lines[l].ascent;
  1146. (* IF ascent = 0 THEN ascent := layout.lines[l].height END;
  1147. IF ascent = 0 THEN ascent := 10 END; *)
  1148. newRect := x.GetArea(xp, yp, ascent);
  1149. InvalidateRect(newRect)
  1150. END
  1151. END;
  1152. Release
  1153. END RemovePositionMarker;
  1154. PROCEDURE PositionMarkerChanged(sender, data : ANY);
  1155. VAR newRect, combinedRect : WMRectangles.Rectangle; x, y, l, ascent : LONGINT;
  1156. BEGIN
  1157. IF ~IsCallFromSequencer() THEN sequencer.ScheduleEvent(SELF.PositionMarkerChanged, sender, data)
  1158. ELSE
  1159. data := sender;
  1160. IF (data # NIL) & (data IS PositionMarker) THEN
  1161. text.AcquireRead;
  1162. IF data = cursor THEN CheckCursor; END;
  1163. IF (data = cursor) & (clBgCurrentLineI # 0) THEN
  1164. Invalidate; (* HACK to handle clBgCurrentLine correcty. Should be replaced by a more efficient solution *)
  1165. ELSE
  1166. IF FindScreenPos(data(PositionMarker).pos.GetPosition(), x, y) THEN
  1167. l := layout.FindLineNrByPos(data(PositionMarker).pos.GetPosition());
  1168. IF (l < LEN(layout.lines^)) & (l >= 0) THEN
  1169. ascent := layout.lines[l].ascent;
  1170. (* IF ascent = 0 THEN ascent := layout.lines[l].height END;
  1171. IF ascent = 0 THEN ascent := 10 END;*)
  1172. newRect := data(PositionMarker).GetArea(x, y, ascent)
  1173. END
  1174. END;
  1175. combinedRect := data(PositionMarker).currentArea;
  1176. IF WMRectangles.RectEmpty(combinedRect) THEN combinedRect := newRect
  1177. ELSE WMRectangles.ExtendRect(combinedRect, newRect)
  1178. END;
  1179. IF ~WMRectangles.RectEmpty(combinedRect) THEN
  1180. IF (WMRectangles.Area(data(PositionMarker).currentArea) + WMRectangles.Area(newRect)) * 5 < WMRectangles.Area(combinedRect) THEN
  1181. InvalidateRect(data(PositionMarker).currentArea);
  1182. InvalidateRect(newRect)
  1183. ELSE
  1184. InvalidateRect(combinedRect)
  1185. END
  1186. END;
  1187. END;
  1188. text.ReleaseRead;
  1189. ELSE
  1190. Invalidate;
  1191. END;
  1192. END
  1193. END PositionMarkerChanged;
  1194. (* END PositionMarkers *)
  1195. PROCEDURE CheckNumberOfLines;
  1196. BEGIN
  1197. UpdateScrollbars;
  1198. firstLine.SetBounds(0, layout.GetNofLines() - 1)
  1199. END CheckNumberOfLines;
  1200. PROCEDURE CheckCursor;
  1201. VAR
  1202. cp, l, i : LONGINT;
  1203. ty : LONGINT;
  1204. lineStartPosition, lineLength: LONGINT;
  1205. li: LineInfo;
  1206. dummyCh : Char32;
  1207. x, dummyY, xend, paperWidth, newShift: LONGINT;
  1208. dummyBool : BOOLEAN;
  1209. BEGIN
  1210. ASSERT(IsCallFromSequencer() & text.HasReadLock());
  1211. (* Scroll up, down to make cursor visible *)
  1212. cp := cursor.GetPosition();
  1213. IF cp = lastCursorPos THEN
  1214. RETURN
  1215. ELSE
  1216. lastCursorPos := cp
  1217. END;
  1218. IF (cp < 0) THEN
  1219. cursor.SetPosition(GetDisplayPos(0));
  1220. ELSIF (cp > text.GetLength()) THEN
  1221. cursor.SetPosition(text.GetLength());
  1222. END;
  1223. l := layout.FindLineNrByPos(cursor.GetPosition());
  1224. IF (l < firstLineI) THEN
  1225. (* move the cursor down by 3 lines to get more context *)
  1226. l := Strings.Max(0, l - 3);
  1227. firstLine.Set(l);
  1228. ELSIF (l < layout.GetNofLines()) THEN
  1229. ty := bordersI.t; i := firstLineI;
  1230. WHILE i < l DO
  1231. ty := ty + layout.lines[i].height;
  1232. CheckParagraphBegin(i, ty);
  1233. CheckParagraphEnd(i, ty);
  1234. INC(i);
  1235. END;
  1236. ty := ty + layout.lines[i].height;
  1237. IF ty >= bounds.GetHeight() - bordersI.b THEN
  1238. l := Strings.Max(0, l - 3);
  1239. firstLine.Set(l)
  1240. END
  1241. END;
  1242. (* fof071127: Scroll left right to make cursor visible *)
  1243. lineStartPosition := layout.GetLineStartPos(l);
  1244. lineLength := layout.GetLineLength(l);
  1245. (* compute x position of the cursor on the line *)
  1246. IF optimize OR ~text.isUTF THEN
  1247. LayoutLine(lineStartPosition,dummyCh,li,layout.paperWidth,cp,-1);
  1248. x := li.width + GetLineLeftIndent(l);
  1249. ELSE
  1250. dummyBool := FindScreenPos(cp,x,dummyY);
  1251. IF x < 0 THEN
  1252. x := 0;
  1253. END;
  1254. INC(x,GetLineLeftIndent(l));
  1255. END;
  1256. (* compute x position of the end of the cursor's line *)
  1257. lineStartPosition := layout.GetLineStartPos(l);
  1258. lineLength := layout.GetLineLength(l);
  1259. LayoutLine(lineStartPosition, dummyCh, li, layout.paperWidth, lineStartPosition+lineLength-1, -1);
  1260. xend := li.width + GetLineLeftIndent(l);
  1261. newShift := leftShiftI;
  1262. (* align shift such that the cursor is visible *)
  1263. paperWidth := layout.paperWidth - bordersI.l - x0;
  1264. IF paperWidth > 0 THEN
  1265. IF x-leftShiftI > paperWidth THEN (* cursor right of displayed area *)
  1266. newShift := x-paperWidth; (* move content such that cursor is barely visible to the right *)
  1267. ELSIF x-leftShiftI < 0 THEN (* cursor is left of displayed area *)
  1268. newShift := x; (* move content such that cursor is barely visible to the left *)
  1269. END;
  1270. (* now check some possibly more optimal ways of displaying *)
  1271. IF xend-newShift < paperWidth THEN
  1272. (* line can be shown more fully to the left, we don't want to waste space to the right *)
  1273. newShift := xend-paperWidth;
  1274. IF newShift < 0 THEN newShift := 0 END;
  1275. END;
  1276. (* do the shift *)
  1277. IF newShift # leftShiftI THEN
  1278. leftShift.Set(newShift);
  1279. END;
  1280. END;
  1281. END CheckCursor;
  1282. PROCEDURE CheckParagraphBegin(lineNr : LONGINT; VAR height: LONGINT);
  1283. BEGIN
  1284. IF layout.lines[lineNr].firstInParagraph THEN height := height + layout.lines[lineNr].spaceBefore END
  1285. END CheckParagraphBegin;
  1286. PROCEDURE CheckParagraphEnd(lineNr : LONGINT; VAR height: LONGINT);
  1287. BEGIN
  1288. IF layout.lines[lineNr].lastInParagraph THEN height := height + layout.lines[lineNr].spaceAfter; END;
  1289. END CheckParagraphEnd;
  1290. PROCEDURE TextChanged(sender, data : ANY);
  1291. VAR
  1292. f, l, t, b, i, p, pa, pb, h: LONGINT; linesChanged, fullLayout : BOOLEAN;
  1293. info : Texts.TextChangeInfo;
  1294. BEGIN
  1295. IF ~initialized THEN RETURN END;
  1296. IF ~IsCallFromSequencer() THEN sequencer.ScheduleEvent(SELF.TextChanged, sender, data)
  1297. ELSE
  1298. IF (data # NIL) & (data IS Texts.TextChangeInfo) & (data(Texts.TextChangeInfo).op # Texts.OpMulti) THEN
  1299. text.AcquireRead;
  1300. info := data(Texts.TextChangeInfo);
  1301. IF text.GetTimestamp() = info.timestamp THEN
  1302. info := data(Texts.TextChangeInfo);
  1303. IF (highlighter # NIL) THEN
  1304. ASSERT(state # NIL);
  1305. fullLayout := FALSE;
  1306. IF ((info.op = Texts.OpInsert) OR (info.op = Texts.OpDelete)) THEN
  1307. highlighter.PatchRegions(info, utilreader, state, fullLayout);
  1308. ELSIF (info.op = Texts.OpAttributes) THEN
  1309. (* do nothing here *)
  1310. ELSE
  1311. highlighter.RebuildRegions(utilreader, state);
  1312. fullLayout := TRUE;
  1313. END;
  1314. IF fullLayout THEN
  1315. layout.FullLayout(TRUE);
  1316. lastTimeStamp := text.GetTimestamp();
  1317. CheckCursor;
  1318. CheckNumberOfLines;
  1319. text.ReleaseRead;
  1320. InvalidateRect(GetClientRect());
  1321. cursorBlinker.Show(cursor);
  1322. RETURN;
  1323. END;
  1324. END;
  1325. (* Upon an insertion or deletion in the text, parts of the text may need reformatting *)
  1326. IF info.op = Texts.OpInsert THEN
  1327. (* If necessary, reformat the affected text *)
  1328. IF layout.initialized & text.isUTF & (layout.bidiFormatter # NIL) THEN
  1329. layout.bidiFormatter.ReformatTextFrom(info.pos,info.len);
  1330. END;
  1331. layout.FixLayoutFrom(info.pos, info.len, f, l, linesChanged);
  1332. ELSE
  1333. (* If necessary, reformat the affected text *)
  1334. IF layout.initialized & text.isUTF & (layout.bidiFormatter # NIL) THEN
  1335. layout.bidiFormatter.ReformatTextFrom(info.pos,-info.len)
  1336. END;
  1337. layout.FixLayoutFrom(info.pos, -info.len, f, l, linesChanged);
  1338. END;
  1339. t := bordersI.t;
  1340. FOR i := firstLineI TO f - 1 DO
  1341. t := t + (layout.lines[i].height);
  1342. CheckParagraphBegin(i, t);
  1343. CheckParagraphEnd(i, t);
  1344. END;
  1345. h := bounds.GetHeight();
  1346. IF linesChanged THEN b := h ELSE
  1347. b := t; i := f;
  1348. WHILE (i <= l) & (b < h) DO
  1349. b := b + (layout.lines[i].height);
  1350. CheckParagraphBegin(i, b);
  1351. CheckParagraphEnd(i, b);
  1352. INC(i);
  1353. END
  1354. END;
  1355. pa := layout.lines[f].pos;
  1356. IF l + 1 < layout.nofLines THEN pb := layout.lines[l + 1].pos ELSE pb := text.GetLength() END;
  1357. FOR i := 0 TO nofPositionMarkers - 1 DO
  1358. p := positionMarkers[i].pos.GetPosition();
  1359. IF (p >= pa) & (p < pb) THEN
  1360. (* very conservative *)
  1361. h := positionMarkers[i].currentArea.b - positionMarkers[i].currentArea.t;
  1362. t := t - h;
  1363. b := b + h
  1364. END
  1365. END;
  1366. CheckCursor;
  1367. UpdateScrollbars;
  1368. InvalidateRect(WMRectangles.MakeRect(0, t, bounds.GetWidth(), b));
  1369. ELSIF (lastTimeStamp - info.timestamp) > 0 THEN
  1370. (* Don't update lastTimeStamp since we didn't update the layout *)
  1371. ELSE
  1372. IF (highlighter # NIL) THEN
  1373. ASSERT(state # NIL);
  1374. highlighter.RebuildRegions(utilreader, state);
  1375. END;
  1376. layout.FullLayout(TRUE);
  1377. lastTimeStamp := text.GetTimestamp();
  1378. CheckCursor;
  1379. InvalidateRect(GetClientRect())
  1380. END;
  1381. CheckNumberOfLines;
  1382. text.ReleaseRead
  1383. ELSE
  1384. text.AcquireRead;
  1385. IF (highlighter # NIL) THEN
  1386. ASSERT(state # NIL);
  1387. highlighter.RebuildRegions(utilreader, state);
  1388. END;
  1389. layout.FullLayout(TRUE);
  1390. lastTimeStamp := text.GetTimestamp();
  1391. CheckCursor;
  1392. CheckNumberOfLines;
  1393. text.ReleaseRead;
  1394. InvalidateRect(GetClientRect())
  1395. END;
  1396. cursorBlinker.Show(cursor);
  1397. END;
  1398. END TextChanged;
  1399. (* BEGIN view dependant layout functions *)
  1400. (** Return the left indent of a line - depending on alignment *)
  1401. (* returns left border, in case of errors *)
  1402. PROCEDURE GetLineLeftIndent(linenr : LONGINT): LONGINT;
  1403. VAR indent : LONGINT;
  1404. BEGIN
  1405. IF (linenr < 0) OR (linenr >= layout.nofLines) THEN RETURN 0 END;
  1406. IF layout.lines[linenr].firstInParagraph THEN indent := layout.lines[linenr].firstIndent ELSE indent := layout.lines[linenr].leftIndent END;
  1407. CASE layout.lines[linenr].align OF
  1408. AlignLeft : RETURN indent;
  1409. |AlignCenter : RETURN ((layout.paperWidth - (layout.lines[linenr].width)) DIV 2 - indent DIV 2);
  1410. |AlignRight : RETURN (layout.paperWidth - layout.lines[linenr].width - layout.lines[linenr].rightIndent);
  1411. ELSE
  1412. RETURN 0;
  1413. END;
  1414. END GetLineLeftIndent;
  1415. (** Find the line number that currently contains the y value (y relative to 0 in component)*)
  1416. PROCEDURE FindLineByY*(firstLine, y : LONGINT) : LONGINT;
  1417. VAR i : LONGINT; ypos : LONGINT;
  1418. BEGIN
  1419. ASSERT(text.HasReadLock());
  1420. ypos := bordersI.t; i := firstLine;
  1421. IF y < 0 THEN RETURN 0 END;
  1422. WHILE (i < layout.nofLines) & (ypos <= y) DO
  1423. ypos := ypos + layout.lines[i].height;
  1424. CheckParagraphBegin(i, ypos);
  1425. CheckParagraphEnd(i, ypos);
  1426. INC(i);
  1427. END;
  1428. RETURN Strings.Max(i -1, 0)
  1429. END FindLineByY;
  1430. PROCEDURE ViewToTextPos*(x, y: LONGINT; VAR pos : LONGINT);
  1431. VAR
  1432. l : LONGINT;
  1433. dummy : LineInfo;
  1434. dummyCh : Char32;
  1435. indent : LONGINT;
  1436. BEGIN
  1437. text.AcquireRead;
  1438. pos := -1;
  1439. x := Strings.Max(0, Strings.Min(x, bounds.GetWidth()));
  1440. y := Strings.Max(0, Strings.Min(y, bounds.GetHeight()));
  1441. l := FindLineByY(firstLineI, Strings.Min(Strings.Max(y, bordersI.t), bounds.GetHeight() - bordersI.b));
  1442. x := x - bordersI.l - x0 + leftShiftI;
  1443. IF x < 0 THEN x := 0 END;
  1444. IF l >= 0 THEN
  1445. dummy := layout.lines[l]; (* this line belongs in here! *)
  1446. pos := layout.GetLineStartPos(l);
  1447. IF dummy.firstInParagraph THEN indent := dummy.firstIndent
  1448. ELSE indent := dummy.leftIndent END;
  1449. IF dummy.align = 0 THEN (* Left *)
  1450. LayoutLine(pos, dummyCh, dummy, layout.paperWidth, -1, x-indent)
  1451. ELSIF dummy.align = 1 THEN (* Center *)
  1452. LayoutLine(pos, dummyCh, dummy, layout.paperWidth, -1, x-((layout.paperWidth - dummy.width - indent) DIV 2))
  1453. ELSIF dummy.align = 2 THEN (* Right *)
  1454. LayoutLine(pos, dummyCh, dummy, layout.paperWidth, -1, x-(layout.paperWidth - dummy.width - dummy.rightIndent))
  1455. END;
  1456. (* Adjust the position if necessary *)
  1457. IF IsRightToLeft(pos) THEN
  1458. DEC(pos);
  1459. END;
  1460. END;
  1461. text.ReleaseRead
  1462. END ViewToTextPos;
  1463. (* Returns the height for the given width *)
  1464. PROCEDURE GetHeight*(width: LONGINT): LONGINT;
  1465. VAR oldWidth, height : LONGINT;
  1466. BEGIN
  1467. oldWidth := layout.paperWidth;
  1468. layout.paperWidth := width;
  1469. layout.FullLayout(FALSE);
  1470. height := layout.textHeight;
  1471. (* reset old state *)
  1472. layout.paperWidth := oldWidth;
  1473. layout.FullLayout(FALSE);
  1474. RETURN height
  1475. END GetHeight;
  1476. (* Returns the size of the largest word and line in pixels *)
  1477. PROCEDURE GetMinMaxWidth*(VAR word, line : LONGINT);
  1478. VAR dx, pos : LONGINT;
  1479. cws, cls : LONGINT;
  1480. f,cf : WMGraphics.Font;
  1481. ch : Char32;
  1482. tabstring : ARRAY 256 OF CHAR; tabs : CustomTabStops; tp : TabPositions;
  1483. sr : Streams.StringReader; tabCounter, tabPos : LONGINT; token : ARRAY 16 OF CHAR;
  1484. pStyle : Texts.ParagraphStyle;
  1485. cStyle : Texts.CharacterStyle;
  1486. PROCEDURE GetWidth(ch : Char32; VAR dx : LONGINT);
  1487. VAR gs : WMGraphics.GlyphSpacings; vc : WMComponents.VisualComponent;
  1488. BEGIN
  1489. IF ch = Texts.ObjectChar THEN
  1490. IF (utilreader.object # NIL) & (utilreader.object IS WMGraphics.Image) THEN
  1491. dx := utilreader.object(WMGraphics.Image).width
  1492. ELSIF (utilreader.object # NIL) & (utilreader.object IS WMComponents.VisualComponent) THEN
  1493. vc := utilreader.object(WMComponents.VisualComponent);
  1494. dx := vc.bounds.GetWidth();
  1495. END
  1496. ELSIF ch = Texts.TabChar THEN
  1497. IF tabs # NIL THEN dx := tabs.GetNextTabStop(cls) - cls
  1498. ELSE dx := defaultTabStops.GetNextTabStop(cls) - cls
  1499. END;
  1500. ELSE
  1501. IF isPasswordI THEN ch := passwordChar.Get() END;
  1502. IF f.HasChar(ch) THEN
  1503. f.GetGlyphSpacings(ch, gs);
  1504. ELSE
  1505. WMGraphics.FBGetGlyphSpacings(ch, gs);
  1506. END;
  1507. dx := gs.bearing.l + gs.width + gs.bearing.r
  1508. END
  1509. END GetWidth;
  1510. BEGIN
  1511. cf := GetFont(); (* set the default component font *)
  1512. f := cf;
  1513. pos := 0; cws := 0; cls := 0; word := 0; line := 0;
  1514. text.AcquireRead;
  1515. utilreader.SetDirection(1); utilreader.SetPosition(pos);
  1516. REPEAT
  1517. utilreader.ReadCh(ch);
  1518. (* Get the Paragraph Style *)
  1519. IF utilreader.pstyle # NIL THEN
  1520. pStyle := utilreader.pstyle;
  1521. (* parse tabstops *)
  1522. COPY(pStyle.tabStops, tabstring);
  1523. IF (tabstring # "default") & (tabstring # "0") & (tabstring # "") THEN
  1524. NEW(sr, LEN(tabstring)); sr.Set(tabstring); tabCounter := 0;
  1525. WHILE (sr.res = Streams.Ok) DO
  1526. sr.SkipWhitespace; sr.String(token);
  1527. INC(tabCounter);
  1528. END;
  1529. NEW(tp, tabCounter);
  1530. sr.Reset; tabCounter := 0;
  1531. WHILE (sr.res = Streams.Ok) DO
  1532. sr.SkipWhitespace; sr.String(token);
  1533. Strings.StrToInt(token, tabPos);
  1534. tp[tabCounter] := tabPos;
  1535. INC(tabCounter);
  1536. END;
  1537. NEW(tabs, tp)
  1538. END
  1539. END;
  1540. (* Get the Character Styles / Attributes *)
  1541. IF utilreader.cstyle # NIL THEN
  1542. cStyle := utilreader.cstyle;
  1543. IF (cStyle.fontcache #NIL) & (cStyle.fontcache IS WMGraphics.Font) THEN
  1544. f := cStyle.fontcache(WMGraphics.Font);
  1545. ELSE
  1546. f := WMGraphics.GetFont(cStyle.family, ENTIER(FP1616.FixpToFloat(cStyle.size)), cStyle.style);
  1547. utilreader.cstyle.fontcache := f
  1548. END;
  1549. ELSIF utilreader.pstyle # NIL THEN
  1550. IF pStyle.charStyle # NIL THEN
  1551. cStyle := pStyle.charStyle;
  1552. IF (cStyle.fontcache #NIL) &
  1553. (cStyle.fontcache IS WMGraphics.Font) THEN
  1554. f := cStyle.fontcache(WMGraphics.Font);
  1555. ELSE
  1556. f := WMGraphics.GetFont(cStyle.family, ENTIER(FP1616.FixpToFloat(cStyle.size)), cStyle.style);
  1557. utilreader.pstyle.charStyle.fontcache := f
  1558. END
  1559. END;
  1560. ELSIF utilreader.attributes # NIL THEN
  1561. IF utilreader.attributes.fontInfo # NIL THEN
  1562. IF (utilreader.attributes.fontInfo.fontcache # NIL)
  1563. & (utilreader.attributes.fontInfo.fontcache IS WMGraphics.Font) THEN
  1564. f := utilreader.attributes.fontInfo.fontcache(WMGraphics.Font);
  1565. ELSE
  1566. f := GetFontFromAttr(utilreader.attributes.fontInfo);
  1567. utilreader.attributes.fontInfo.fontcache := f
  1568. END
  1569. ELSE f := cf
  1570. END
  1571. ELSE f := cf;
  1572. END;
  1573. INC(pos);
  1574. GetWidth(ch, dx);
  1575. IF (ch = Texts.ObjectChar) THEN
  1576. word := Strings.Max(word, dx);
  1577. cls := cls + dx;
  1578. cws := 0
  1579. ELSIF (ch = Texts.NewLineChar) THEN
  1580. line := Strings.Max(line, cls);
  1581. cls := 0
  1582. ELSIF (ch = 32) THEN
  1583. word := Strings.Max(word, cws);
  1584. cws := 0
  1585. ELSE
  1586. cws := cws + dx;
  1587. cls := cls + dx;
  1588. END;
  1589. UNTIL utilreader.eot;
  1590. line := Strings.Max(line, cls);
  1591. word := Strings.Max(word, cws);
  1592. text.ReleaseRead;
  1593. END GetMinMaxWidth;
  1594. (* END view dependant layout functions *)
  1595. PROCEDURE LineYPos(lineNr : LONGINT; VAR y0, y1 : LONGINT);
  1596. VAR i : LONGINT;
  1597. BEGIN
  1598. IF (lineNr >= firstLineI) & (lineNr < layout.GetNofLines()) THEN
  1599. y0 := bordersI.t; i := firstLineI;
  1600. WHILE i < lineNr DO
  1601. y0 := y0 + layout.lines[i].height;
  1602. CheckParagraphBegin(i, y0);
  1603. CheckParagraphEnd(i, y0);
  1604. INC(i);
  1605. END;
  1606. y1 := y0 + layout.lines[i].height;
  1607. CheckParagraphBegin(i, y1);
  1608. ELSE y0 := 0; y1 := 0
  1609. END
  1610. END LineYPos;
  1611. PROCEDURE FindScreenPos*(pos : LONGINT; VAR x, y : LONGINT) : BOOLEAN;
  1612. VAR
  1613. l, i, startPos, intPos: LONGINT;
  1614. ty : LONGINT;
  1615. li : LineInfo;
  1616. thisCh, lastCh : Char32;
  1617. lastLine : BOOLEAN;
  1618. f : WMGraphics.Font;
  1619. gs: WMGraphics.GlyphSpacings;
  1620. BEGIN
  1621. text.AcquireRead;
  1622. lastLine := FALSE;
  1623. IF (pos = text.GetLength()) THEN
  1624. utilreader.SetDirection(1); utilreader.SetPosition(text.GetLength() - 1);
  1625. utilreader.ReadCh(thisCh);
  1626. IF thisCh = Texts.NewLineChar THEN lastLine := TRUE END
  1627. END;
  1628. IF lastLine THEN
  1629. ty := bordersI.t; i := firstLineI;
  1630. WHILE i < layout.nofLines DO
  1631. ty := ty + layout.lines[i].height;
  1632. CheckParagraphBegin(i, ty);
  1633. CheckParagraphEnd(i, ty);
  1634. INC(i);
  1635. END;
  1636. IF i > 0 THEN
  1637. y := (ty + layout.lines[i - 1].ascent)
  1638. ELSE
  1639. f := GetFont();
  1640. y := (ty + f.GetAscent());
  1641. END;
  1642. x := bordersI.l + x0 - leftShiftI;
  1643. text.ReleaseRead;
  1644. RETURN TRUE
  1645. ELSIF (pos = 0) & (firstLineI = 0) THEN
  1646. ty := bordersI.t;
  1647. IF layout.GetNofLines() > 0 THEN
  1648. y := (ty + layout.lines[0].ascent);
  1649. ELSE
  1650. f := GetFont();
  1651. y := ty+f.GetAscent();
  1652. END;
  1653. CheckParagraphBegin(0, y);
  1654. x := bordersI.l + x0 - leftShiftI;
  1655. text.ReleaseRead;
  1656. RETURN TRUE
  1657. ELSE
  1658. l := layout.FindLineNrByPos(pos);
  1659. IF (l >= firstLineI) & (l < layout.GetNofLines()) THEN
  1660. ty := bordersI.t; i := firstLineI;
  1661. WHILE i < l DO
  1662. ty := ty + layout.lines[i].height;
  1663. CheckParagraphBegin(i, ty);
  1664. CheckParagraphEnd(i, ty);
  1665. INC(i);
  1666. END;
  1667. y := (ty + layout.lines[i].ascent);
  1668. CheckParagraphBegin(i, y);
  1669. startPos := layout.GetLineStartPos(i);
  1670. f := GetFont();
  1671. intPos := GetInternalPos(pos);
  1672. utilreader.SetPosition(intPos-1);
  1673. utilreader.ReadCh(lastCh);
  1674. utilreader.ReadCh(thisCh);
  1675. (* if this character is rtl and its predecessor is ltr, move the position to the right of the previous character *)
  1676. IF (intPos # 0) & (IsRightToLeft(intPos) & ~IsRightToLeft(intPos-1) & (intPos # startPos)) OR
  1677. ((~IsRightToLeft(intPos) OR (thisCh = 0AH)) & ~IsRightToLeft(intPos-1) & ODD(GetParagraphEmbeddingLevel(pos))) THEN
  1678. LayoutLine(startPos, lastCh, li, layout.paperWidth, GetDisplayPos(intPos-1), -1);
  1679. IF f.HasChar(lastCh) THEN
  1680. f.GetGlyphSpacings(lastCh, gs);
  1681. ELSE
  1682. WMGraphics.FBGetGlyphSpacings(lastCh, gs);
  1683. END;
  1684. x := li.width + GetLineLeftIndent(l) + bordersI.l + x0 - leftShiftI + gs.bearing.l + gs.width + gs.bearing.r;
  1685. ELSIF (intPos # 0) & ((thisCh = 0AH) OR (thisCh = 0H)) & IsRightToLeft(intPos-1) THEN
  1686. LayoutLine(startPos, thisCh, li, layout.paperWidth, GetDisplayPos(intPos-1), -1);
  1687. x := (li.width + GetLineLeftIndent(l) + bordersI.l + x0 - leftShiftI);
  1688. (* if this and its predecessor are rtl, move the position to the right of this character *)
  1689. ELSIF IsRightToLeft(intPos) THEN
  1690. LayoutLine(startPos, thisCh, li, layout.paperWidth, pos, -1);
  1691. IF f.HasChar(thisCh) THEN
  1692. f.GetGlyphSpacings(thisCh, gs);
  1693. ELSE
  1694. WMGraphics.FBGetGlyphSpacings(thisCh, gs);
  1695. END;
  1696. x := li.width + GetLineLeftIndent(l) + bordersI.l + x0 - leftShiftI + gs.bearing.l + gs.width + gs.bearing.r;
  1697. (* if this character is ltr and its predecessor is rtl move the position to the left of the predecessor *)
  1698. ELSIF (intPos # 0) & (~IsRightToLeft(intPos) OR (thisCh = 0AH)) & IsRightToLeft(intPos-1) THEN
  1699. LayoutLine(startPos, thisCh, li, layout.paperWidth, GetDisplayPos(intPos-1), -1);
  1700. x := (li.width + GetLineLeftIndent(l) + bordersI.l + x0 - leftShiftI);
  1701. (* if this and the previous character are ltr, leave the position at the left of this character *)
  1702. ELSE
  1703. LayoutLine(startPos, thisCh, li, layout.paperWidth, pos, -1);
  1704. x := (li.width + GetLineLeftIndent(l) + bordersI.l + x0 - leftShiftI);
  1705. END;
  1706. text.ReleaseRead;
  1707. RETURN TRUE
  1708. ELSE
  1709. text.ReleaseRead;
  1710. RETURN FALSE
  1711. END
  1712. END
  1713. END FindScreenPos;
  1714. (* Get the internal position for a given display position. *)
  1715. PROCEDURE GetInternalPos*(pos : LONGINT) : LONGINT;
  1716. VAR
  1717. lineNr, startPos, lineLength : LONGINT;
  1718. dummyTextReader : Texts.TextReader;
  1719. BEGIN
  1720. (* if the text is non-utf formatted, the internal position and the display position are the same *)
  1721. IF ~text.isUTF OR (layout.bidiFormatter = NIL) THEN
  1722. RETURN pos;
  1723. END;
  1724. text.AcquireRead;
  1725. lineNr := layout.FindLineNrByPos(pos);
  1726. startPos := layout.GetLineStartPos(lineNr);
  1727. lineLength := layout.GetLineLength(lineNr);
  1728. dummyTextReader := layout.bidiFormatter.ReorderLine(startPos,lineLength);
  1729. text.ReleaseRead;
  1730. RETURN layout.bidiFormatter.GetInternalPosition(pos,startPos);
  1731. END GetInternalPos;
  1732. (* Get the display position for a given display position. *)
  1733. PROCEDURE GetDisplayPos*(pos : LONGINT) : LONGINT;
  1734. VAR
  1735. lineNr, startPos, lineLength : LONGINT;
  1736. dummyTextReader : Texts.TextReader;
  1737. BEGIN
  1738. (* if the text is non-utf formatted, the internal position and the display position are the same *)
  1739. IF ~text.isUTF OR (layout.bidiFormatter = NIL) THEN
  1740. RETURN pos;
  1741. END;
  1742. lineNr := layout.FindLineNrByPos(pos);
  1743. startPos := layout.GetLineStartPos(lineNr);
  1744. lineLength := layout.GetLineLength(lineNr);
  1745. dummyTextReader := layout.bidiFormatter.ReorderLine(startPos,lineLength);
  1746. RETURN layout.bidiFormatter.GetDisplayPosition(pos,startPos);
  1747. END GetDisplayPos;
  1748. (* Checks if the current position is in an rtl context *)
  1749. PROCEDURE IsRightToLeft*(pos : LONGINT) : BOOLEAN;
  1750. VAR
  1751. lineNr, startPos, lineLength : LONGINT;
  1752. dummyTextReader : Texts.TextReader;
  1753. BEGIN
  1754. IF ~text.isUTF OR (layout.bidiFormatter = NIL) THEN
  1755. RETURN FALSE;
  1756. END;
  1757. lineNr := layout.FindLineNrByPos(pos);
  1758. startPos := layout.GetLineStartPos(lineNr);
  1759. lineLength := layout.GetLineLength(lineNr);
  1760. IF layout.initialized THEN
  1761. dummyTextReader := layout.bidiFormatter.ReorderLine(startPos,lineLength);
  1762. END;
  1763. RETURN ODD(layout.bidiFormatter.GetImplicitLevel(pos));
  1764. END IsRightToLeft;
  1765. (* Gets the paragraph embedding level of the current position's line *)
  1766. PROCEDURE GetParagraphEmbeddingLevel*(pos : LONGINT) : LONGINT;
  1767. BEGIN
  1768. IF ~text.isUTF OR (layout.bidiFormatter = NIL) THEN
  1769. RETURN 0;
  1770. END;
  1771. RETURN layout.bidiFormatter.GetParagraphEmbeddingLevel(pos);
  1772. END GetParagraphEmbeddingLevel;
  1773. PROCEDURE LayoutLine(VAR pos : LONGINT; VAR ch : Char32; VAR l : LineInfo; wrapwidth, stopPos, stopXPos : LONGINT);
  1774. VAR
  1775. i, wrapPos : LONGINT;
  1776. eol, first : BOOLEAN;
  1777. ascent, descent, leading, ld, a, d, dx, x : LONGINT;
  1778. align, firstIndent, leftIndent, rightIndent, spaceBefore, spaceAfter : LONGINT;
  1779. tabstring : ARRAY 256 OF CHAR; tabs : CustomTabStops; tp : TabPositions;
  1780. sr : Streams.StringReader; tabCounter, tabPos : LONGINT; token : ARRAY 16 OF CHAR;
  1781. pStyle : Texts.ParagraphStyle;
  1782. start, stop, isFirst : BOOLEAN;
  1783. bidiTextReader, localTextReader : Texts.TextReader;
  1784. regionStart, regionEnd,lastEnd : LONGINT;
  1785. readerPosition : LONGINT;
  1786. highlighterStyle, lastHighlighterStyle : SyntaxHighlighter.Style;
  1787. currentStyle, lastStyle : ANY;
  1788. cf: WMGraphics.Font;
  1789. style : RECORD
  1790. voff : LONGINT;
  1791. font : WMGraphics.Font;
  1792. END;
  1793. PROCEDURE GetExtents(ch : Char32; VAR dx, ascent, descent: LONGINT);
  1794. VAR gs : WMGraphics.GlyphSpacings; vc : WMComponents.VisualComponent; font : WMGraphics.Font;
  1795. BEGIN
  1796. IF ch = Texts.ObjectChar THEN
  1797. IF (localTextReader.object # NIL) & (localTextReader.object IS WMGraphics.Image) THEN
  1798. ascent := localTextReader.object(WMGraphics.Image).height - style.voff;
  1799. descent := style.voff;
  1800. dx := localTextReader.object(WMGraphics.Image).width
  1801. ELSIF (localTextReader.object # NIL) & (localTextReader.object IS WMComponents.VisualComponent) THEN
  1802. vc := localTextReader.object(WMComponents.VisualComponent);
  1803. dx := vc.bounds.GetWidth();
  1804. ascent := vc.bounds.GetHeight() - style.voff;
  1805. descent := style.voff;
  1806. (* Add a Sequencer to the object if none exists *)
  1807. IF (vc.sequencer = NIL) OR (vc.sequencer # sequencer) THEN
  1808. vc.SetSequencer(sequencer);
  1809. IF sequencer#NIL THEN vc.Reset(NIL, NIL); END;
  1810. END;
  1811. END
  1812. ELSIF ch = Texts.TabChar THEN
  1813. IF l.tabStops # NIL THEN dx := l.tabStops.GetNextTabStop(x) - x
  1814. ELSE dx := defaultTabStops.GetNextTabStop(x) - x
  1815. END;
  1816. ascent := style.font.GetAscent() - style.voff;
  1817. descent := style.font.GetDescent() + style.voff
  1818. ELSIF ch = Texts.LabelChar THEN
  1819. IF showLabels.Get() THEN
  1820. font := cf;
  1821. font.GetStringSize(localTextReader.object(Texts.LabelPiece).label^, dx, ascent);
  1822. INC(dx, 4);
  1823. ELSE
  1824. ascent := 0; descent := 0;
  1825. dx := 0;
  1826. END;
  1827. ELSE
  1828. IF isPasswordI THEN ch := passwordChar.Get() END;
  1829. IF style.font.HasChar(ch) THEN
  1830. style.font.GetGlyphSpacings(ch, gs);
  1831. ELSE
  1832. WMGraphics.FBGetGlyphSpacings(ch, gs);
  1833. END;
  1834. ascent := gs.ascent - style.voff;
  1835. descent := gs.descent + style.voff;
  1836. dx := gs.bearing.l + gs.width + gs.bearing.r
  1837. END
  1838. END GetExtents;
  1839. BEGIN
  1840. style.voff := 0;
  1841. cf := GetFont();
  1842. style.font := cf;
  1843. x := 0; l.pos := pos; l.height := style.font.GetHeight();
  1844. (* For layouting a reordered line, the reordered text is needed, to correctly measure
  1845. the extends of each character. *)
  1846. IF text.isUTF & (layout.bidiFormatter # NIL) THEN
  1847. isFirst := FALSE;
  1848. bidiTextReader := layout.bidiFormatter.ReadyTextReader(pos,isFirst);
  1849. END;
  1850. (* if a reformatted text is available initialize it correpsondingly *)
  1851. IF (bidiTextReader # NIL) THEN
  1852. (* if a reordered line is available, the contextual dependency rules are applied *)
  1853. bidiTextReader.CloneProperties(utilreader);
  1854. localTextReader := bidiTextReader;
  1855. localTextReader.text.AcquireRead;
  1856. localTextReader.SetPosition(0);
  1857. (* or initialize to default otherwise *)
  1858. ELSE
  1859. localTextReader := utilreader;
  1860. localTextReader.SetPosition(pos);
  1861. END;
  1862. localTextReader.SetDirection(1); first := TRUE;
  1863. (* the bidi formatter needs special treatment when finding out about the first line of the paragraph *)
  1864. start := FALSE; stop := FALSE;
  1865. IF (pos = 0) THEN start := TRUE;
  1866. ELSIF (bidiTextReader = NIL) THEN
  1867. localTextReader.SetPosition(pos-1);
  1868. localTextReader.ReadCh(ch);
  1869. IF (ch = Texts.NewLineChar) THEN start := TRUE;
  1870. ELSE start := FALSE;
  1871. END;
  1872. ELSE (* bidiTextReader # NIL *)
  1873. IF isFirst THEN
  1874. start := TRUE;
  1875. ELSE
  1876. start := FALSE;
  1877. END;
  1878. END;
  1879. i := 0; leading := 0; ascent := style.font.GetAscent(); descent := style.font.GetDescent();
  1880. align := AlignLeft; l.tabStops := NIL; COPY("", tabstring);
  1881. firstIndent := 0; leftIndent := 0; rightIndent := 0; spaceBefore := 0; spaceAfter := 0;
  1882. lastEnd := -1;
  1883. highlighterStyle := NIL; lastHighlighterStyle := NIL;
  1884. currentStyle := NIL; lastStyle := NIL;
  1885. eol := FALSE;
  1886. REPEAT
  1887. readerPosition := localTextReader.GetPosition();
  1888. localTextReader.ReadCh(ch);
  1889. IF (highlighter # NIL) THEN
  1890. ASSERT(state # NIL);
  1891. IF (lastEnd < readerPosition) THEN
  1892. highlighterStyle := highlighter.GetRegionStyle(readerPosition, state, regionStart, regionEnd);
  1893. IF (highlighterStyle # NIL) THEN
  1894. lastEnd := regionEnd;
  1895. ELSE
  1896. IF (ch > 32) THEN
  1897. highlighterStyle := highlighter.GetWordStyle(localTextReader, readerPosition, lastEnd);
  1898. END;
  1899. END;
  1900. localTextReader.SetPosition(readerPosition);
  1901. localTextReader.ReadCh(ch); (* restore text reader state *)
  1902. END;
  1903. IF (highlighterStyle = NIL) THEN
  1904. highlighterStyle := highlighter.GetDefaultStyle();
  1905. END;
  1906. END;
  1907. (* Get the Paragraph Style *)
  1908. IF localTextReader.pstyle # NIL THEN
  1909. pStyle := localTextReader.pstyle;
  1910. (* pStyle := Texts.GetParagraphStyleByName(pStyle.name); *)
  1911. spaceBefore := ENTIER(FP1616.FixpToFloat(pStyle.spaceBefore));
  1912. spaceAfter := ENTIER(FP1616.FixpToFloat(pStyle.spaceAfter));
  1913. firstIndent := ENTIER(FP1616.FixpToFloat(pStyle.firstIndent));
  1914. leftIndent := ENTIER(FP1616.FixpToFloat(pStyle.leftIndent));
  1915. rightIndent := ENTIER(FP1616.FixpToFloat(pStyle.rightIndent));
  1916. align := pStyle.alignment;
  1917. (* parse tabstops *)
  1918. COPY(pStyle.tabStops, tabstring);
  1919. IF (tabstring # "default") & (tabstring # "0") & (tabstring # "") THEN
  1920. NEW(sr, LEN(tabstring)); sr.Set(tabstring); tabCounter := 0;
  1921. WHILE (sr.res = Streams.Ok) DO
  1922. sr.SkipWhitespace; sr.String(token);
  1923. INC(tabCounter);
  1924. END;
  1925. NEW(tp, tabCounter);
  1926. sr.Reset; tabCounter := 0;
  1927. WHILE (sr.res = Streams.Ok) DO
  1928. sr.SkipWhitespace; sr.String(token);
  1929. Strings.StrToInt(token, tabPos);
  1930. tp[tabCounter] := tabPos;
  1931. INC(tabCounter);
  1932. END;
  1933. NEW(tabs, tp);
  1934. IF l.tabStops = NIL THEN l.tabStops := tabs END
  1935. END;
  1936. END;
  1937. IF (highlighterStyle = NIL) OR (highlighterStyle.defined * SyntaxHighlighter.DefineMask # SyntaxHighlighter.DefineMask) THEN
  1938. IF localTextReader.cstyle # NIL THEN
  1939. IF (currentStyle # localTextReader.cstyle) THEN
  1940. currentStyle := localTextReader.cstyle;
  1941. style.voff := ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.baselineShift));
  1942. ld := ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.leading));
  1943. IF (localTextReader.cstyle.fontcache #NIL) & (localTextReader.cstyle.fontcache IS WMGraphics.Font) THEN
  1944. style.font := localTextReader.cstyle.fontcache(WMGraphics.Font);
  1945. ELSE
  1946. style.font := WMGraphics.GetFont(localTextReader.cstyle.family, ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.size)), localTextReader.cstyle.style);
  1947. localTextReader.cstyle.fontcache := style.font;
  1948. END;
  1949. END;
  1950. ELSIF localTextReader.pstyle # NIL THEN
  1951. IF pStyle.charStyle # NIL THEN
  1952. style.voff := ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.baselineShift));
  1953. ld := ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.leading));
  1954. IF (localTextReader.cstyle.fontcache #NIL) & (localTextReader.cstyle.fontcache IS WMGraphics.Font) THEN
  1955. style.font := localTextReader.cstyle.fontcache(WMGraphics.Font);
  1956. ELSE
  1957. style.font := WMGraphics.GetFont(localTextReader.cstyle.family, ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.size)), localTextReader.cstyle.style);
  1958. localTextReader.pstyle.charStyle.fontcache := style.font
  1959. END
  1960. END;
  1961. ELSIF localTextReader.attributes # NIL THEN
  1962. IF (currentStyle # localTextReader.attributes) THEN
  1963. currentStyle := localTextReader.attributes;
  1964. style.voff := localTextReader.attributes.voff;
  1965. ld := 0;
  1966. IF localTextReader.attributes.fontInfo # NIL THEN
  1967. IF (localTextReader.attributes.fontInfo.fontcache # NIL) & (localTextReader.attributes.fontInfo.fontcache IS WMGraphics.Font) THEN
  1968. style.font := localTextReader.attributes.fontInfo.fontcache(WMGraphics.Font);
  1969. ELSE
  1970. style.font := GetFontFromAttr(localTextReader.attributes.fontInfo);
  1971. localTextReader.attributes.fontInfo.fontcache := style.font;
  1972. END
  1973. ELSE
  1974. style.font := cf
  1975. END
  1976. END;
  1977. ELSE
  1978. IF (currentStyle # DefaultStyle) THEN
  1979. currentStyle := DefaultStyle;
  1980. style.voff := 0;
  1981. style.font := cf;
  1982. ld := 0;
  1983. END;
  1984. END;
  1985. ASSERT(style.font # NIL);
  1986. END;
  1987. IF (highlighterStyle # NIL) THEN
  1988. IF (highlighterStyle # lastHighlighterStyle) OR (currentStyle # lastStyle) THEN
  1989. IF SyntaxHighlighter.Voff IN highlighterStyle.defined THEN style.voff := highlighterStyle.attributes.voff; END;
  1990. IF (SyntaxHighlighter.FontMask * highlighterStyle.defined # {}) THEN
  1991. CheckFont(highlighterStyle, style.font, fontCache);
  1992. style.font := highlighterStyle.attributes.fontInfo.fontcache (WMGraphics.Font);
  1993. END;
  1994. END;
  1995. currentStyle := NIL;
  1996. END;
  1997. lastStyle := currentStyle;
  1998. lastHighlighterStyle := highlighterStyle;
  1999. IF first THEN
  2000. IF (ch = Texts.NewLineChar) OR (ch = 0) THEN
  2001. ascent := style.font.GetAscent(); descent := style.font.GetDescent();
  2002. ELSE
  2003. descent := 0; ascent := 0;
  2004. END;
  2005. IF start THEN wrapwidth := wrapwidth - firstIndent - rightIndent;
  2006. ELSE wrapwidth := wrapwidth - leftIndent - rightIndent;
  2007. END;
  2008. first := FALSE;
  2009. END;
  2010. INC(pos);
  2011. IF (stopPos < 0) OR (pos <= stopPos) THEN
  2012. IF (ch # Texts.NewLineChar) & (ch # 0) THEN
  2013. GetExtents(ch, dx, a, d); ascent := Strings.Max(ascent, a); descent := Strings.Max(descent, d);
  2014. IF ld = 0 THEN ld := ascent + descent; ELSE ld := Strings.Max(ld, ascent + descent); END; leading := Strings.Max(leading, ld);
  2015. IF isMultiLineI & (wrapModeI # NoWrap) & (i > 0) & (x0 + x + dx > wrapwidth) THEN
  2016. eol := TRUE; DEC(pos); wrapPos := pos;
  2017. (* Go left for last space *)
  2018. IF wrapModeI = WrapWord THEN
  2019. pos := TextUtilities.FindPosWordLeft(localTextReader, pos);
  2020. IF pos <= l.pos THEN pos := wrapPos (* no word break found. wrap at latest possible pos *)
  2021. ELSE (* decrease width to actual size.. *)
  2022. (* localTextReader.SetPosition(pos);
  2023. WHILE pos < wrapPos DO
  2024. localTextReader.ReadCh(ch); GetExtents(ch, dx, a, d); x := x - dx; INC(pos)
  2025. END
  2026. *) END
  2027. END
  2028. ELSE
  2029. IF (stopXPos >= 0) & (x + dx DIV 2 > stopXPos) THEN
  2030. DEC(pos);
  2031. (* the bidi formatted text's lock needs to be released explicitly *)
  2032. IF (bidiTextReader # NIL) THEN
  2033. localTextReader.text.ReleaseRead;
  2034. END;
  2035. RETURN
  2036. END;
  2037. INC(x, dx)
  2038. END;
  2039. ELSE
  2040. eol := TRUE;
  2041. stop := TRUE;
  2042. IF (stopXPos >= 0) THEN DEC(pos) END;
  2043. END;
  2044. ELSE
  2045. eol := TRUE
  2046. END;
  2047. INC(i);
  2048. UNTIL eol OR localTextReader.eot;
  2049. l.width := x;
  2050. l.ascent := ascent; l.height := leading; (* ascent + descent; *)
  2051. l.align := align; l.leftIndent := leftIndent; l.rightIndent := rightIndent;
  2052. IF l.height = 0 THEN l.height := style.font.GetHeight() END;
  2053. IF start THEN l.firstInParagraph := TRUE; l.firstIndent := firstIndent; l.spaceBefore := spaceBefore;
  2054. ELSE l.firstInParagraph := FALSE; END;
  2055. IF stop THEN l.lastInParagraph := TRUE; l.spaceAfter := spaceAfter;
  2056. ELSE l.lastInParagraph := FALSE END;
  2057. (* the bidi formatted text's lock needs to be released explicitly *)
  2058. IF (bidiTextReader # NIL) THEN
  2059. localTextReader.text.ReleaseRead;
  2060. END;
  2061. END LayoutLine;
  2062. (* llen = -1 to render until the end of line > 0 to render llen elements in the line *)
  2063. PROCEDURE RenderLine*(canvas : WMGraphics.Canvas; VAR l : LineInfo; linenr, top, llen : LONGINT);
  2064. VAR sx, dx, dy, x, sp, i, j, k, t, tx, linelength, w, p : LONGINT; char : Char32; gs: WMGraphics.GlyphSpacings;
  2065. font : WMGraphics.Font;
  2066. vc : WMComponents.VisualComponent;
  2067. hc : BOOLEAN;
  2068. bidiTextReader, localTextReader : Texts.TextReader;
  2069. cursorPosition : LONGINT;
  2070. regionStart, regionEnd, lastEnd: LONGINT;
  2071. readerPosition : LONGINT;
  2072. lineNumberString : ARRAY 16 OF CHAR;
  2073. canvasState : WMGraphics.CanvasState;
  2074. cliprect, temp : WMRectangles.Rectangle;
  2075. highlighterStyle, lastHighlighterStyle : SyntaxHighlighter.Style;
  2076. currentStyle, lastStyle : ANY;
  2077. lastColor : LONGINT;
  2078. cf: WMGraphics.Font;
  2079. style : RECORD
  2080. color, bgColor, voff : LONGINT;
  2081. font : WMGraphics.Font;
  2082. END;
  2083. BEGIN
  2084. IF TraceRenderOptimize IN Trace THEN
  2085. KernelLog.String("RenderLine : "); KernelLog.Int(linenr, 5); KernelLog.String(" from position : ");
  2086. KernelLog.Int(layout.GetLineStartPos(linenr), 5); KernelLog.Ln;
  2087. END;
  2088. sp := l.pos;
  2089. IF sp >= text.GetLength() THEN RETURN END;
  2090. style.color := defaultTextColorI;
  2091. canvas.SetColor(style.color); lastColor := style.color;
  2092. style.bgColor := defaultTextBgColorI;
  2093. style.voff := 0;
  2094. cf := GetFont();
  2095. style.font := cf;
  2096. IF llen < 0 THEN
  2097. linelength := layout.GetLineLength(linenr);
  2098. (* hack for the bidi formatter *)
  2099. IF linenr = layout.GetNofLines() - 1 THEN
  2100. DEC(linelength);
  2101. END;
  2102. ELSE
  2103. linelength := llen
  2104. END;
  2105. (* if there is a bidi formatter, reorder the current line *)
  2106. IF text.isUTF & (layout.bidiFormatter # NIL) THEN
  2107. bidiTextReader := layout.bidiFormatter.ReorderLine(sp,linelength);
  2108. END;
  2109. (* the bidi text reader needs special treatment for the initialization *)
  2110. IF (bidiTextReader # NIL) THEN
  2111. (* after reordering the line, contextual dependency rules are applied *)
  2112. bidiTextReader := ContextualDependency.AnalyzeLine(bidiTextReader,-1,-1);
  2113. layout.bidiFormatter.SetReadyTextReader(sp,bidiTextReader);
  2114. bidiTextReader.CloneProperties(utilreader);
  2115. localTextReader := bidiTextReader;
  2116. localTextReader.text.AcquireRead;
  2117. localTextReader.SetPosition(0);
  2118. ELSE
  2119. (* revert the hack for the bidi formatter *)
  2120. IF (llen < 0) & (linenr = layout.GetNofLines() - 1) THEN
  2121. INC(linelength);
  2122. END;
  2123. localTextReader := utilreader;
  2124. localTextReader.text.AcquireRead;
  2125. localTextReader.SetPosition(sp);
  2126. END;
  2127. i := 0;
  2128. x := GetLineLeftIndent(linenr);
  2129. sx := - leftShiftI + bordersI.l + x0;
  2130. IF TraceBaseLine IN Trace THEN
  2131. canvas.Line(0, top + (l.ascent), bounds.GetWidth(), top + (l.ascent), 01F0000FFH, WMGraphics.ModeCopy)
  2132. END;
  2133. selection.Sort;
  2134. IF (cursor.visible) & (selection.b - selection.a <= 0) & (clBgCurrentLineI # 0) THEN
  2135. cursorPosition := cursor.GetPosition();
  2136. IF (l.pos <= cursorPosition) & (cursorPosition < l.pos + linelength) THEN
  2137. canvas.Fill(WMRectangles.MakeRect(0, top, bounds.GetWidth() - bordersI.r, top + l.height), clBgCurrentLineI, WMGraphics.ModeSrcOverDst);
  2138. END;
  2139. END;
  2140. IF showLineNumbersI THEN
  2141. canvas.SaveState(canvasState);
  2142. Strings.IntToStr(linenr + 1, lineNumberString);
  2143. temp := WMRectangles.MakeRect(bordersI.l, top, x0 - 1, top + l.height);
  2144. IF (lineNumberBgColorI # 0) THEN
  2145. canvas.Fill(temp, lineNumberBgColorI, WMGraphics.ModeSrcOverDst);
  2146. END;
  2147. temp.r := temp.r - 4;
  2148. IF ((linenr + 1) MOD 10 = 0) THEN
  2149. canvas.SetFont(lineNumberFont10);
  2150. ELSE
  2151. canvas.SetFont(lineNumberFont);
  2152. END;
  2153. canvas.SetColor(lineNumberColorI);
  2154. WMGraphics.DrawStringInRect(canvas, temp, FALSE, WMGraphics.AlignRight, WMGraphics.AlignCenter, lineNumberString);
  2155. canvas.RestoreState(canvasState); (* restore font and font color *)
  2156. canvas.SaveState(canvasState);
  2157. canvas.GetClipRect(cliprect);
  2158. cliprect.l := x0;
  2159. canvas.SetClipRect(cliprect);
  2160. END;
  2161. w := bounds.GetWidth() - bordersI.r;
  2162. localTextReader.SetDirection(1);
  2163. lastEnd := -1;
  2164. highlighterStyle := NIL; lastHighlighterStyle := NIL;
  2165. currentStyle := DefaultStyle; lastStyle := NIL;
  2166. REPEAT
  2167. readerPosition := localTextReader.GetPosition();
  2168. localTextReader.ReadCh(char);
  2169. IF (highlighter # NIL) THEN
  2170. ASSERT(state # NIL);
  2171. IF (lastEnd < readerPosition) THEN
  2172. highlighterStyle := highlighter.GetRegionStyle(readerPosition, state, regionStart, regionEnd);
  2173. IF (highlighterStyle # NIL) THEN
  2174. lastEnd := regionEnd;
  2175. ELSE
  2176. IF (char > 32) THEN
  2177. highlighterStyle := highlighter.GetWordStyle(localTextReader, readerPosition, lastEnd);
  2178. END;
  2179. END;
  2180. localTextReader.SetPosition(readerPosition);
  2181. localTextReader.ReadCh(char); (* restore text reader state *)
  2182. END;
  2183. IF (highlighterStyle = NIL) THEN
  2184. highlighterStyle := highlighter.GetDefaultStyle();
  2185. END;
  2186. END;
  2187. IF (highlighterStyle = NIL) OR (highlighterStyle.defined * SyntaxHighlighter.DefineMask # SyntaxHighlighter.DefineMask) THEN
  2188. IF (localTextReader.cstyle # NIL) THEN
  2189. IF (currentStyle # localTextReader.cstyle) THEN
  2190. currentStyle := localTextReader.cstyle;
  2191. style.color := localTextReader.cstyle.color;
  2192. style.bgColor := localTextReader.cstyle.bgColor;
  2193. style.voff := localTextReader.cstyle.baselineShift;
  2194. IF (localTextReader.cstyle.fontcache # NIL) & (localTextReader.cstyle.fontcache IS WMGraphics.Font) THEN
  2195. style.font := localTextReader.cstyle.fontcache(WMGraphics.Font);
  2196. ELSE
  2197. style.font := WMGraphics.GetFont(localTextReader.cstyle.family, ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.size)), localTextReader.cstyle.style);
  2198. localTextReader.cstyle.fontcache := style.font;
  2199. END;
  2200. END;
  2201. ELSIF (localTextReader.attributes # NIL) THEN
  2202. IF (currentStyle # localTextReader.attributes) THEN
  2203. currentStyle := localTextReader.attributes;
  2204. style.color := localTextReader.attributes.color;
  2205. style.bgColor := localTextReader.attributes.bgcolor;
  2206. style.voff := localTextReader.attributes.voff;
  2207. IF (localTextReader.attributes.fontInfo # NIL) THEN
  2208. IF (localTextReader.attributes.fontInfo.fontcache # NIL) & (localTextReader.attributes.fontInfo.fontcache IS WMGraphics.Font) THEN
  2209. style.font := localTextReader.attributes.fontInfo.fontcache (WMGraphics.Font);
  2210. ELSE
  2211. style.font := GetFontFromAttr(localTextReader.attributes.fontInfo);
  2212. localTextReader.attributes.fontInfo.fontcache := style.font;
  2213. END;
  2214. ELSE
  2215. style.font := cf;
  2216. END;
  2217. END;
  2218. ELSE
  2219. IF (currentStyle # DefaultStyle) THEN
  2220. currentStyle := DefaultStyle;
  2221. style.color := defaultTextColorI;
  2222. style.bgColor := defaultTextBgColorI;
  2223. style.voff := 0;
  2224. style.font := cf;
  2225. END;
  2226. END;
  2227. ASSERT(style.font # NIL);
  2228. END;
  2229. IF (highlighterStyle # NIL) THEN
  2230. IF (highlighterStyle # lastHighlighterStyle) OR (currentStyle # lastStyle) THEN
  2231. IF SyntaxHighlighter.Voff IN highlighterStyle.defined THEN style.voff := highlighterStyle.attributes.voff; END;
  2232. IF SyntaxHighlighter.Color IN highlighterStyle.defined THEN style.color := highlighterStyle.attributes.color; END;
  2233. IF SyntaxHighlighter.BgColor IN highlighterStyle.defined THEN style.bgColor := highlighterStyle.attributes.bgcolor; END;
  2234. IF (SyntaxHighlighter.FontMask * highlighterStyle.defined # {}) THEN
  2235. CheckFont(highlighterStyle, style.font, fontCache);
  2236. style.font := highlighterStyle.attributes.fontInfo.fontcache (WMGraphics.Font);
  2237. END;
  2238. END;
  2239. currentStyle := NIL; (* force reevaluation of localTextReader style *)
  2240. END;
  2241. lastStyle := currentStyle;
  2242. lastHighlighterStyle := highlighterStyle;
  2243. IF (style.color # lastColor) THEN canvas.SetColor(style.color); lastColor := style.color; END;
  2244. IF char = Texts.ObjectChar THEN
  2245. IF (localTextReader.object # NIL) & (localTextReader.object IS WMGraphics.Image) THEN
  2246. canvas.DrawImage(x, top + (l.ascent) + style.voff - localTextReader.object(WMGraphics.Image).height, localTextReader.object(WMGraphics.Image),
  2247. WMGraphics.ModeSrcOverDst);
  2248. dx := localTextReader.object(WMGraphics.Image).width
  2249. ELSIF (localTextReader.object # NIL) & (localTextReader.object IS WMComponents.VisualComponent) THEN
  2250. vc := localTextReader.object(WMComponents.VisualComponent);
  2251. dx := vc.bounds.GetWidth();
  2252. dy := vc.bounds.GetHeight();
  2253. canvas.SaveState(clipState); (* save the current clip-state *)
  2254. canvas.SetClipRect(WMRectangles.MakeRect(x + sx, top + (l.ascent - dy), x + dx + sx, top + (l.height)));
  2255. canvas.ClipRectAsNewLimits(x + sx, top + (l.ascent - dy));
  2256. (* assuming the component will not delay --> otherwise a buffer is needed *)
  2257. vc.Acquire; vc.Draw(canvas); vc.Release;
  2258. canvas.RestoreState(clipState)
  2259. END
  2260. ELSIF char = 0 THEN (* EOT *)
  2261. ELSIF char = Texts.TabChar THEN
  2262. tx := x;
  2263. IF l.firstInParagraph THEN tx := tx - l.firstIndent
  2264. ELSE tx := tx - l.leftIndent END;
  2265. IF l.tabStops # NIL THEN dx := l.tabStops.GetNextTabStop(tx) - tx
  2266. ELSE dx := defaultTabStops.GetNextTabStop(tx) - tx
  2267. END;
  2268. IF style.bgColor # 0 THEN
  2269. canvas.Fill(WMRectangles.MakeRect(x + sx, top, x + dx + sx, top + (l.height)), style.bgColor, WMGraphics.ModeSrcOverDst)
  2270. END;
  2271. IF indicateTabsI THEN canvas.SetPixel(x + sx + ((dx + 1) DIV 2), top + ((l.ascent + 1) DIV 2), WMGraphics.Blue, WMGraphics.ModeCopy); END;
  2272. ELSIF char = Texts.LabelChar THEN
  2273. IF showLabels.Get() THEN
  2274. font := cf;
  2275. font.GetStringSize(localTextReader.object(Texts.LabelPiece).label^, dx, dy);
  2276. font.RenderString(canvas, x + sx+2, top + (l.ascent), localTextReader.object(Texts.LabelPiece).label^);
  2277. INC(dx, 4);
  2278. canvas.Fill(WMRectangles.MakeRect(x + sx, top, x + dx + sx, top + (l.height)), LONGINT(0FF880050H), WMGraphics.ModeSrcOverDst);
  2279. WMGraphicUtilities.RectGlassShade(canvas, WMRectangles.MakeRect(x + sx, top, x + dx + sx, top + (l.height)), 1, FALSE)
  2280. ELSE dx := 0; END;
  2281. ELSE
  2282. IF char = Texts.NewLineChar THEN
  2283. localTextReader.text.ReleaseRead;
  2284. IF showLineNumbersI THEN canvas.RestoreState(canvasState); END;
  2285. RETURN
  2286. END;
  2287. IF isPasswordI THEN
  2288. char := passwordChar.Get()
  2289. END;
  2290. (* If the text is utf-formatted get the display version of the character.
  2291. Note, that only some special invisible characters differ from their actual representation. *)
  2292. IF text.isUTF THEN
  2293. UnicodeBidirectionality.GetDisplayCharacter(char);
  2294. END;
  2295. hc := style.font.HasChar(char);
  2296. IF hc THEN style.font.GetGlyphSpacings(char, gs)
  2297. ELSE WMGraphics.FBGetGlyphSpacings(char, gs)
  2298. END;
  2299. dx := gs.bearing.l + gs.width + gs.bearing.r;
  2300. IF style.bgColor MOD 256 # 0 THEN
  2301. canvas.Fill(WMRectangles.MakeRect(x + sx, top, x + dx + sx, top + (l.height)), style.bgColor, WMGraphics.ModeCopy)
  2302. END;
  2303. IF hc THEN style.font.RenderChar(canvas, x + sx, top + (l.ascent) + style.voff, char)
  2304. ELSE WMGraphics.FBRenderChar(canvas, x + sx, top + (l.ascent) + style.voff, char)
  2305. END
  2306. END;
  2307. (* link *)
  2308. IF localTextReader.link # NIL THEN
  2309. canvas.Line(x + sx, top + (l.ascent)+1, x + dx + sx, top + (l.ascent)+1, canvas.color, WMGraphics.ModeSrcOverDst);
  2310. END;
  2311. (* highlight - since highlights store the global text position, the line's starting position needs to be added,
  2312. when operating on the local, bidirectional text reader. *)
  2313. IF bidiTextReader # NIL THEN
  2314. p := GetInternalPos(localTextReader.GetPosition()+sp-1);
  2315. ELSE
  2316. p := localTextReader.GetPosition() - 1;
  2317. END;
  2318. FOR j := 0 TO nofHighlights - 1 DO
  2319. IF (p >= highlights[j].a) & (p < highlights[j].b) THEN
  2320. CASE highlights[j].kind OF
  2321. |HLOver: canvas.Fill(WMGraphics.MakeRectangle(x + sx, top, x + dx + sx, top + (l.height)), highlights[j].color, WMGraphics.ModeSrcOverDst)
  2322. |HLUnder: canvas.Line(x + sx, top + (l.ascent), x + dx + sx, top + (l.ascent), highlights[j].color, WMGraphics.ModeSrcOverDst);
  2323. |HLWave:
  2324. FOR k := 0 TO dx - 1 DO
  2325. t := 1 - ABS((x + k) MOD 4 - 2); (* because of compiler bug on intel *)
  2326. canvas.SetPixel(x + k + sx, top + l.ascent + t, highlights[j].color, WMGraphics.ModeSrcOverDst);
  2327. END;
  2328. ELSE
  2329. END
  2330. END
  2331. END;
  2332. x := x + dx;
  2333. INC(i)
  2334. UNTIL (i >= linelength) OR localTextReader.eot OR (x + sx > w);
  2335. localTextReader.text.ReleaseRead;
  2336. IF showLineNumbersI THEN canvas.RestoreState(canvasState); END;
  2337. END RenderLine;
  2338. PROCEDURE RenderAboveTextMarkers*(canvas : WMGraphics.Canvas);
  2339. VAR x, y, l, pos, i, ascent : LONGINT;
  2340. BEGIN
  2341. AssertLock;
  2342. IF text = NIL THEN RETURN END;
  2343. IF optimize THEN RETURN END;
  2344. text.AcquireRead;
  2345. FOR i := nofPositionMarkers - 1 TO 0 BY -1 DO
  2346. pos := positionMarkers[i].pos.GetPosition();
  2347. l := layout.FindLineNrByPos(pos);
  2348. IF FindScreenPos(pos, x, y) THEN
  2349. IF (l >= 0) & (l < layout.GetNofLines()) THEN
  2350. ascent := layout.lines[l].ascent;
  2351. (* IF ascent = 0 THEN ascent := layout.lines[l].height END;
  2352. IF ascent = 0 THEN ascent := 10 END; *)
  2353. ELSE ascent := 10 END;
  2354. positionMarkers[i].Draw(canvas, x, y, ascent)
  2355. END
  2356. END;
  2357. text.ReleaseRead;
  2358. END RenderAboveTextMarkers;
  2359. PROCEDURE DrawBackground*(canvas : WMGraphics.Canvas);
  2360. VAR la, lb, i, top, t, b : LONGINT; rect, clip : WMRectangles.Rectangle; cstate : WMGraphics.CanvasState;
  2361. BEGIN
  2362. ASSERT(layout # NIL);
  2363. canvas.GetClipRect(clip);
  2364. IF WMRectangles.RectEmpty(clip) THEN RETURN END;
  2365. rect := GetClientRect();
  2366. canvas.SaveState(cstate);
  2367. IF WMRectangles.Intersect(rect, clip) THEN
  2368. DrawBackground^(canvas);
  2369. IF showBorderI THEN
  2370. WMGraphicUtilities.DrawBevel(canvas, rect,
  2371. 1, TRUE, LONGINT(0808080FFH), WMGraphics.ModeCopy)
  2372. END;
  2373. END;
  2374. (* allow clean clipping in at inner border *)
  2375. WMRectangles.ClipRect(rect, borderClip);
  2376. WMRectangles.ClipRect(clip, borderClip);
  2377. canvas.SetClipRect(clip);
  2378. (* draw gutter *)
  2379. rect.r := x0 - 1;
  2380. IF showLineNumbersI & (lineNumberBgColorI # 0) & WMRectangles.Intersect(rect, clip) THEN
  2381. canvas.Fill(rect, lineNumberBgColorI, WMGraphics.ModeSrcOverDst);
  2382. END;
  2383. text.AcquireRead;
  2384. la := FindLineByY(firstLineI, clip.t);
  2385. lb := FindLineByY(firstLineI, clip.b);
  2386. (* prepare selections *)
  2387. FOR i := 0 TO nofHighlights - 1 DO
  2388. highlights[i].a := highlights[i].from.GetPosition();
  2389. highlights[i].b := highlights[i].to.GetPosition();
  2390. IF highlights[i].a > highlights[i].b THEN t := highlights[i].a; highlights[i].a := highlights[i].b; highlights[i].b := t END
  2391. END;
  2392. top := borderClip.t;
  2393. IF (la = lb) & (textAlignV.Get() = WMGraphics.AlignCenter) THEN
  2394. top := (borderClip.t+borderClip.b-layout.lines[la].height) DIV 2;
  2395. (* something is wrong with ascent and height here, does not comply with the notions in fonts *)
  2396. END;
  2397. FOR i := firstLineI TO la - 1 DO
  2398. top := top + (layout.lines[i].height);
  2399. CheckParagraphBegin(i, top);
  2400. CheckParagraphEnd(i, top);
  2401. END;
  2402. IF la >= 0 THEN
  2403. (* draw the lines that intersect the clipping rectangle *)
  2404. FOR i := la TO lb DO
  2405. CheckParagraphBegin(i, top);
  2406. RenderLine(canvas, layout.lines[i], i, top, -1);
  2407. top := top + (layout.lines[i].height);
  2408. CheckParagraphEnd(i, top);
  2409. END
  2410. END;
  2411. RenderAboveTextMarkers(canvas);
  2412. text.ReleaseRead;
  2413. canvas.RestoreState(cstate);
  2414. END DrawBackground;
  2415. PROCEDURE StoreLineEnter;
  2416. VAR pos, cl : LONGINT;
  2417. BEGIN
  2418. pos := cursor.GetPosition();
  2419. cl := layout.FindLineNrByPos(pos);
  2420. lineEnter := pos - layout.GetLineStartPos(cl)
  2421. END StoreLineEnter;
  2422. (* navigation *)
  2423. PROCEDURE WheelMove*(dz: LONGINT); (** PROTECTED *)
  2424. BEGIN
  2425. IF mouseWheelScrollSpeedI # 0 THEN
  2426. firstLine.Set(firstLine.Get() + mouseWheelScrollSpeedI * dz)
  2427. END;
  2428. END WheelMove;
  2429. (* abort a possible start of a command. Clear the command start indicator, if it was set *)
  2430. PROCEDURE AbortStart;
  2431. BEGIN
  2432. ASSERT(IsCallFromSequencer());
  2433. IF commandMarker # NIL THEN
  2434. RemoveHighlight(commandMarker);
  2435. commandMarker := NIL
  2436. END;
  2437. canStart := FALSE
  2438. END AbortStart;
  2439. (*
  2440. Handle double-click at text position <pos>.
  2441. Select the double-clicked word, whitespace or line.
  2442. Some explanations:
  2443. Why utilreader.GetPosition()+2 when searching to the left?
  2444. After we read the last character that should be included, the position of the reader is decremented.
  2445. When we now read the next character and see that it should not be included, the reader is decremented again.
  2446. -> The last character to be included was found at position utilreader.GetPosition()+2 (except when we reach EOT)
  2447. The same applies when search to the right. But to highlight the character at, for example, position 4, we need a highlight from 4-5.
  2448. That's why utilreader.GetPosition()-1 is used instead of utilreader.GetPosition()-2.
  2449. *)
  2450. PROCEDURE DoubleClickSelect(pos : LONGINT);
  2451. CONST
  2452. LineFeed = 0AH;
  2453. Underscore = 05FH;
  2454. VAR
  2455. char : Texts.Char32;
  2456. from, to : LONGINT;
  2457. BEGIN
  2458. ASSERT(text.HasReadLock());
  2459. utilreader.SetPosition(pos);
  2460. utilreader.SetDirection(1);
  2461. utilreader.ReadCh(char);
  2462. IF (char = LineFeed) OR utilreader.eot THEN (* select line *)
  2463. IF utilreader.eot THEN to := pos;
  2464. ELSE to := pos+1;
  2465. END;
  2466. from := TextUtilities.FindPosLineStart(utilreader, pos);
  2467. ELSIF TextUtilities.IsWhiteSpace(char,text.isUTF) THEN
  2468. WHILE ~utilreader.eot & TextUtilities.IsWhiteSpace(char,text.isUTF) & (char # LineFeed) DO utilreader.ReadCh(char); END;
  2469. IF utilreader.eot THEN to := utilreader.text.GetLength();
  2470. ELSE to := utilreader.GetPosition()-1;
  2471. END;
  2472. utilreader.SetPosition(pos);
  2473. utilreader.SetDirection(-1);
  2474. utilreader.ReadCh(char);
  2475. WHILE ~utilreader.eot & TextUtilities.IsWhiteSpace(char,text.isUTF) & (char # LineFeed) DO utilreader.ReadCh(char); END;
  2476. IF utilreader.eot THEN from := 0;
  2477. ELSE from := utilreader.GetPosition()+2;
  2478. END;
  2479. ELSIF TextUtilities.IsAlphaNum(char) OR (char = Underscore) THEN (* select word *)
  2480. WHILE ~utilreader.eot & (TextUtilities.IsAlphaNum(char) OR (char = Underscore)) DO utilreader.ReadCh(char); END;
  2481. IF utilreader.eot THEN to := utilreader.text.GetLength();
  2482. ELSE to := utilreader.GetPosition()-1;
  2483. END;
  2484. utilreader.SetPosition(pos);
  2485. utilreader.SetDirection(-1);
  2486. utilreader.ReadCh(char);
  2487. WHILE ~utilreader.eot & (TextUtilities.IsAlphaNum(char) OR (char = Underscore)) DO utilreader.ReadCh(char); END;
  2488. IF utilreader.eot THEN from := 0;
  2489. ELSE from := utilreader.GetPosition()+2;
  2490. END;
  2491. ELSE (* select the character at text position pos *)
  2492. from := pos; to := pos+1;
  2493. END;
  2494. selection.SetFromTo(from, to);
  2495. cursor.SetVisible(to - from > 0);
  2496. END DoubleClickSelect;
  2497. PROCEDURE SetInterclick(new : LONGINT);
  2498. VAR old : LONGINT;
  2499. BEGIN
  2500. old := interclick;
  2501. IF (old # new) THEN
  2502. interclick := new;
  2503. CASE new OF
  2504. | Interclick01: selection.SetColor(SelectionColorInterclick01);
  2505. | Interclick02: selection.SetColor(SelectionColorInterclick02);
  2506. ELSE
  2507. selection.SetColor(SelectionColor);
  2508. END;
  2509. END;
  2510. END SetInterclick;
  2511. PROCEDURE PointerDown*(x, y : LONGINT; keys : SET);
  2512. VAR pos, a, b, internalPos : LONGINT; oldInterclick : LONGINT;
  2513. BEGIN
  2514. ViewToTextPos(x,y,pos);
  2515. internalPos := GetInternalPos(pos);
  2516. oldInterclick := interclick;
  2517. IF (keys * {0, 1} = {0,1}) THEN SetInterclick(Interclick01);
  2518. ELSIF (keys * {0,2} = {0,2}) THEN SetInterclick(Interclick02);
  2519. ELSE SetInterclick(InterclickNone);
  2520. END;
  2521. (* Determine whether to cancel an interclick if any *)
  2522. IF (oldInterclick = InterclickCancelled) OR
  2523. ((oldInterclick # InterclickNone) & (interclick # InterclickNone)) THEN
  2524. SetInterclick(InterclickCancelled);
  2525. END;
  2526. IF allowCommandExecution.Get() & (keys * {0, 1, 2} = {1}) THEN
  2527. canStart := TRUE; openFile := FALSE;
  2528. IF commandMarker = NIL THEN
  2529. commandMarker := CreateHighlight();
  2530. commandMarker.SetKind(HLUnder);
  2531. commandMarker.SetColor(LONGINT(0FF0000FFH));
  2532. text.AcquireRead;
  2533. FindCommand(internalPos, a, b);
  2534. commandMarker.SetFromTo(a, b);
  2535. cursor.SetPosition(pos);
  2536. text.ReleaseRead
  2537. END;
  2538. END;
  2539. IF canStart & (2 IN keys) THEN openFile := TRUE; SetInterclick(InterclickCancelled); END;
  2540. IF keys * {0, 1, 2} = {0, 1, 2} THEN AbortStart END;
  2541. IF allowPiemenu.Get() & (keys * {0, 1, 2} = {2}) THEN
  2542. text.AcquireRead;
  2543. ViewToTextPos(x, y, pos);
  2544. cursor.SetPosition(pos);
  2545. text.ReleaseRead;
  2546. ShowContextMenu(x, y)
  2547. END;
  2548. IF allowTextSelection.Get() &
  2549. ( (keys * {0, 1, 2} = {0}) (* left mouse for select *)
  2550. OR (keys * {0, 1, 2} = {1}) & doubleclickedWord (* remove selection when double clicking *)
  2551. OR (keys * {0,1,2} = {2}) & (~allowPiemenu.Get())) (* right mouse for selection if pie menu is not enabled *)
  2552. THEN
  2553. AbortStart;
  2554. text.AcquireRead;
  2555. ViewToTextPos(x, y, pos);
  2556. dragPossible := FALSE; selectWords := FALSE;
  2557. IF internalPos >= 0 THEN
  2558. selection.Sort;
  2559. IF (internalPos >= selection.a) & (internalPos < selection.b) & (interclick = InterclickNone) THEN
  2560. dragPossible := TRUE; downX := x; downY := y
  2561. ELSIF (interclick = InterclickNone) THEN
  2562. (* clicking the same position twice --> Word Selection Mode *)
  2563. IF (internalPos = GetInternalPos(cursor.GetPosition())) OR ((internalPos - 1 = GetInternalPos(cursor.GetPosition())) & (internalPos - 1 = text.GetLength())) THEN
  2564. (* Workaround: The 2nd check is for the very last line of a text. LayoutLine gives pos = text.GetLength()+1 *)
  2565. selectWords := TRUE; wordSelOrdered := TRUE;
  2566. doubleclickedWord := TRUE;
  2567. DoubleClickSelect(internalPos);
  2568. ELSE
  2569. selection.SetFromTo(internalPos, internalPos); (* reset selection *)
  2570. cursor.SetVisible(TRUE);
  2571. END;
  2572. selecting := TRUE;
  2573. END
  2574. END;
  2575. cursor.SetPosition(pos);
  2576. text.ReleaseRead;
  2577. CursorChanged
  2578. END;
  2579. END PointerDown;
  2580. PROCEDURE PointerMove*(x, y : LONGINT; keys : SET);
  2581. VAR pos, a, b, internalPos : LONGINT;
  2582. BEGIN
  2583. IF ~canStart & dragPossible THEN
  2584. IF (ABS(x - downX) > DragDist) OR (ABS(y - downY) > DragDist) THEN dragPossible := FALSE; AutoStartDrag END
  2585. ELSE
  2586. IF (selecting OR canStart) & (interclick = InterclickNone) THEN
  2587. text.AcquireRead;
  2588. ViewToTextPos(x, y, pos);
  2589. internalPos := GetInternalPos(pos);
  2590. IF selecting & ~doubleclickedWord THEN
  2591. selection.Sort;
  2592. IF selectWords THEN
  2593. IF internalPos < selection.from.GetPosition() THEN
  2594. pos := TextUtilities.FindPosWordLeft(utilreader, internalPos - 1);
  2595. ELSE
  2596. pos := TextUtilities.FindPosWordRight(utilreader, internalPos + 1);
  2597. END;
  2598. selection.SetTo(internalPos)
  2599. ELSE
  2600. selection.SetTo(internalPos);
  2601. END;
  2602. selection.Sort;
  2603. cursor.SetVisible(selection.b - selection.a <= 0);
  2604. Texts.SetLastSelection(text, selection.from, selection.to);
  2605. cursor.SetPosition(pos);
  2606. StoreLineEnter;
  2607. ELSIF canStart THEN
  2608. IF commandMarker # NIL THEN
  2609. FindCommand(internalPos, a, b);
  2610. commandMarker.SetFromTo(a, b)
  2611. END
  2612. END;
  2613. IF doubleclickedWord THEN doubleclickedWord := FALSE; END; (* allow selecting again *)
  2614. text.ReleaseRead;
  2615. CursorChanged
  2616. END
  2617. END
  2618. END PointerMove;
  2619. PROCEDURE PointerUp*(x, y : LONGINT; keys : SET);
  2620. BEGIN
  2621. IF canStart & (commandMarker # NIL) THEN
  2622. commandMarker.Sort;
  2623. StartCommand((commandMarker.a + commandMarker.b) DIV 2, openFile);
  2624. AbortStart
  2625. END;
  2626. IF modifierFlags * Inputs.Ctrl # {} THEN
  2627. onCtrlClicked.Call(NIL)
  2628. END;
  2629. selecting := FALSE;
  2630. doubleclickedWord := FALSE;
  2631. IF (keys * {0,1,2} = {}) THEN
  2632. IF (interclick = Interclick02) THEN
  2633. DeleteSelection;
  2634. END;
  2635. SetInterclick(InterclickNone);
  2636. END;
  2637. IF dragPossible THEN selection.SetFromTo(0, 0); cursor.SetVisible(TRUE); Texts.ClearLastSelection (* reset selection *) END;
  2638. dragPossible := FALSE
  2639. END PointerUp;
  2640. (* Transforms the TextView Coordinates into TextObject obj Coordinates *)
  2641. PROCEDURE TransformCoordinates(VAR x, y : LONGINT; obj : WMComponents.VisualComponent);
  2642. VAR line, pos, x0, y0, y1 : LONGINT;
  2643. BEGIN
  2644. ViewToTextPos(x, y, pos);
  2645. IF FindScreenPos(pos, x0, y0) THEN
  2646. IF x0 > x THEN pos := pos - 1;
  2647. IF FindScreenPos(pos, x0, y0) THEN END;
  2648. END;
  2649. line := layout.FindLineNrByPos(GetInternalPos(pos));
  2650. LineYPos(line, y0, y1);
  2651. x := x - x0;
  2652. y := y - y0;
  2653. IF line >= 0 THEN y := y - (layout.lines[line].ascent - obj.bounds.GetHeight()); END
  2654. END
  2655. END TransformCoordinates;
  2656. (* Change the pointer according to the underlaying component *)
  2657. PROCEDURE ChangePointer(pointerInfo : WMWindowManager.PointerInfo);
  2658. BEGIN
  2659. IF GetPointerInfo() # pointerInfo THEN
  2660. SetPointerInfo(pointerInfo)
  2661. END
  2662. END ChangePointer;
  2663. (* Returns TRUE if an Object is Hit, FALSE otherwise *)
  2664. PROCEDURE HitObject(x, y : LONGINT; (* keys : SET;*) VAR pos : LONGINT; VAR obj : ANY): BOOLEAN;
  2665. VAR ch, tx, ty : LONGINT;
  2666. BEGIN
  2667. text.AcquireRead;
  2668. ViewToTextPos(x, y, pos);
  2669. IF FindScreenPos(pos, tx, ty) THEN
  2670. IF tx > x THEN pos := pos - 1 END
  2671. END;
  2672. utilreader.SetPosition(GetInternalPos(pos));
  2673. utilreader.ReadCh(ch);
  2674. text.ReleaseRead;
  2675. IF ch = Texts.ObjectChar THEN obj := utilreader.object;
  2676. RETURN TRUE
  2677. ELSE
  2678. RETURN FALSE
  2679. END
  2680. END HitObject;
  2681. (* Returns TRUE if a Link is Hit, FALSE otherwise *)
  2682. PROCEDURE HitLink(x, y : LONGINT; VAR pos : LONGINT; VAR link : Texts.Link): BOOLEAN;
  2683. VAR ch, tx, ty : LONGINT;
  2684. BEGIN
  2685. text.AcquireRead;
  2686. ViewToTextPos(x, y, pos);
  2687. IF FindScreenPos(pos, tx, ty) THEN
  2688. IF tx > x THEN pos := pos - 1 END
  2689. END;
  2690. utilreader.SetPosition(GetInternalPos(pos));
  2691. utilreader.ReadCh(ch);
  2692. text.ReleaseRead;
  2693. IF utilreader.link # NIL THEN
  2694. link := utilreader.link;
  2695. RETURN TRUE
  2696. ELSE
  2697. RETURN FALSE
  2698. END
  2699. END HitLink;
  2700. PROCEDURE LinkClick(link : Texts.Link);
  2701. VAR w : LinkWrapper;
  2702. BEGIN
  2703. NEW(w); w.link := link;
  2704. onLinkClicked.Call(w)
  2705. END LinkClick;
  2706. (* builtin behaviour *)
  2707. PROCEDURE LinkClicked*(sender, data : ANY);
  2708. VAR tempLink : ARRAY 2048 OF CHAR;
  2709. tempLabel : ARRAY 256 OF CHAR;
  2710. pos, i : LONGINT;
  2711. BEGIN
  2712. IF data IS LinkWrapper THEN
  2713. COPY(data(LinkWrapper).link^, tempLink);
  2714. IF tempLink[0] = "#" THEN (* internal link *)
  2715. i := 0;
  2716. WHILE tempLink[i] # 0X DO
  2717. tempLabel[i] := tempLink[i+1];
  2718. INC(i);
  2719. END;
  2720. tempLink[i] := 0X;
  2721. (* find label in tv *)
  2722. IF FindLabel(tempLabel, pos) THEN
  2723. i := layout.nofLines-1;
  2724. WHILE (i >= 0) DO
  2725. IF layout.GetLineStartPos(i) < pos THEN firstLine.Set(i); RETURN END;
  2726. DEC(i);
  2727. END;
  2728. END;
  2729. ELSE (* other links *)
  2730. END
  2731. END
  2732. END LinkClicked;
  2733. (* Returns the position of the label in text *)
  2734. PROCEDURE FindLabel*(CONST label : ARRAY OF CHAR; VAR pos : LONGINT): BOOLEAN;
  2735. VAR ch : LONGINT;
  2736. found : BOOLEAN;
  2737. BEGIN
  2738. found := FALSE; pos := 0;
  2739. text.AcquireRead;
  2740. utilreader.SetDirection(1); utilreader.SetPosition(pos);
  2741. REPEAT
  2742. utilreader.ReadCh(ch);
  2743. IF ch = Texts.LabelChar THEN
  2744. IF utilreader.object(Texts.LabelPiece).label^ = label THEN
  2745. found := TRUE;
  2746. END;
  2747. END;
  2748. INC(pos);
  2749. UNTIL utilreader.eot OR found;
  2750. text.ReleaseRead;
  2751. RETURN found;
  2752. END FindLabel;
  2753. (* Drag away operations *)
  2754. PROCEDURE AutoStartDrag*;
  2755. VAR img : WMGraphics.Image;
  2756. c : WMGraphics.BufferCanvas;
  2757. w, h, i, la, lb, top : LONGINT;
  2758. l : LineInfo;
  2759. BEGIN
  2760. text.AcquireRead;
  2761. selection.Sort;
  2762. NEW(dragSelA, text);NEW(dragSelB, text);
  2763. dragSelA.SetPosition(selection.a); dragSelB.SetPosition(selection.b);
  2764. la := Limit(layout.FindLineNrByPos(selection.a), 0, layout.GetNofLines() - 1);
  2765. lb := Limit(layout.FindLineNrByPos(selection.b), 0, layout.GetNofLines() - 1);
  2766. (* estimate the size of the selection *)
  2767. h := 0; w := 0;
  2768. FOR i := la TO lb DO
  2769. h := h + (layout.lines[i].height);
  2770. w := Strings.Max(w, layout.lines[i].width);
  2771. END;
  2772. h := Limit(h, 20, 200);
  2773. w := Limit(w, 20, 400);
  2774. (* render to bitmap *)
  2775. NEW(img); Raster.Create(img, w, h, Raster.BGRA8888);
  2776. NEW(c, img);
  2777. top := 0;
  2778. (* hack the startpos of the first line *)
  2779. l := layout.lines[la]; l.pos := selection.a;
  2780. IF la = lb THEN RenderLine(c, l, la, top, selection.b - selection.a)
  2781. ELSE
  2782. RenderLine(c, l, la, top, -1);
  2783. top := top + l.height
  2784. END;
  2785. FOR i := la + 1 TO lb DO
  2786. IF i = lb THEN
  2787. RenderLine(c, layout.lines[i], i, top, selection.b - layout.lines[i].pos)
  2788. ELSE
  2789. RenderLine(c, layout.lines[i], i, top, -1);
  2790. top := top + (l.height)
  2791. END
  2792. END;
  2793. text.ReleaseRead;
  2794. IF StartDrag(NIL, img, 0,0,DragWasAccepted, NIL) THEN
  2795. ELSE KernelLog.String("WMTextView : Drag could not be started")
  2796. END;
  2797. END AutoStartDrag;
  2798. PROCEDURE DragWasAccepted(sender, data : ANY);
  2799. VAR di : WMWindowManager.DragInfo;
  2800. dt : WMDropTarget.DropTarget;
  2801. itf : WMDropTarget.DropInterface;
  2802. targetText, temp : Texts.Text;
  2803. string : Strings.String;
  2804. pos, a, b, res : LONGINT;
  2805. BEGIN
  2806. IF (dragSelA = NIL) OR (dragSelB = NIL) THEN RETURN END;
  2807. IF (data # NIL) & (data IS WMWindowManager.DragInfo) THEN
  2808. di := data(WMWindowManager.DragInfo);
  2809. IF (di.data # NIL) & (di.data IS WMDropTarget.DropTarget) THEN
  2810. dt := di.data(WMDropTarget.DropTarget)
  2811. ELSE RETURN
  2812. END
  2813. ELSE RETURN
  2814. END;
  2815. itf := dt.GetInterface(WMDropTarget.TypeText);
  2816. IF itf # NIL THEN
  2817. targetText := itf(WMDropTarget.DropText).text;
  2818. IF targetText # NIL THEN
  2819. targetText.AcquireWrite;
  2820. IF ~dragCopy THEN
  2821. IF TraceCopy IN Trace THEN KernelLog.String("WMTextView: Not copy"); KernelLog.Ln; END;
  2822. text.AcquireWrite;
  2823. a := dragSelA.GetPosition(); b := dragSelB.GetPosition();
  2824. pos := itf(WMDropTarget.DropText).pos.GetPosition();
  2825. IF (targetText # text) OR (pos < a) OR (pos > b) THEN
  2826. NEW(temp); temp.AcquireWrite; temp.CopyFromText(text, a, b-a, 0); temp.ReleaseWrite;
  2827. text.Delete(a, b- a);
  2828. pos := itf(WMDropTarget.DropText).pos.GetPosition();
  2829. temp.AcquireRead;
  2830. targetText.CopyFromText(temp, 0, temp.GetLength(), pos);
  2831. temp.ReleaseRead;
  2832. END;
  2833. text.ReleaseWrite
  2834. ELSE
  2835. IF TraceCopy IN Trace THEN KernelLog.String("WMTextView: Copy"); KernelLog.Ln; END;
  2836. text.AcquireRead;
  2837. pos := itf(WMDropTarget.DropText).pos.GetPosition();
  2838. a := dragSelA.GetPosition(); b := dragSelB.GetPosition();
  2839. targetText.CopyFromText(text, a, b-a, pos);
  2840. text.ReleaseRead;
  2841. END;
  2842. targetText.ReleaseWrite
  2843. END;
  2844. RETURN
  2845. END;
  2846. itf := dt.GetInterface(WMDropTarget.TypeString);
  2847. IF (itf # NIL) THEN
  2848. IF ~dragCopy THEN
  2849. text.AcquireWrite;
  2850. a := dragSelA.GetPosition(); b := dragSelB.GetPosition();
  2851. NEW(temp);
  2852. temp.AcquireWrite;
  2853. temp.CopyFromText(text, a, b-a, 0);
  2854. IF (temp.GetLength() > 0) THEN NEW(string, temp.GetLength() * 5); ELSE NEW(string, 1); string[0] := 0X; END;
  2855. temp.ReleaseWrite;
  2856. text.ReleaseWrite;
  2857. TextUtilities.TextToStr(temp, string^);
  2858. itf(WMDropTarget.DropString).Set(string^, res);
  2859. IF res = 0 THEN
  2860. text.AcquireWrite;
  2861. text.Delete(a, b- a);
  2862. text.ReleaseWrite;
  2863. END;
  2864. ELSE
  2865. text.AcquireRead;
  2866. a := dragSelA.GetPosition(); b := dragSelB.GetPosition();
  2867. NEW(temp);
  2868. temp.AcquireWrite;
  2869. temp.CopyFromText(text, a, b-a, 0);
  2870. IF (temp.GetLength() > 0) THEN NEW(string, temp.GetLength() * 5); ELSE NEW(string, 1); string[0] := 0X; END;
  2871. temp.ReleaseWrite;
  2872. text.ReleaseRead;
  2873. TextUtilities.TextToStr(temp, string^);
  2874. itf(WMDropTarget.DropString).Set(string^, res);
  2875. END;
  2876. END;
  2877. END DragWasAccepted;
  2878. (* Drag onto operations *)
  2879. PROCEDURE DragOver(x, y : LONGINT; dragInfo : WMWindowManager.DragInfo);
  2880. VAR pos : LONGINT;
  2881. BEGIN
  2882. IF takesFocus.Get() THEN
  2883. text.AcquireRead;
  2884. ViewToTextPos(x, y, pos);
  2885. cursor.SetVisible(TRUE);
  2886. cursor.SetPosition(pos);
  2887. StoreLineEnter;
  2888. text.ReleaseRead
  2889. END;
  2890. END DragOver;
  2891. PROCEDURE DragDropped*(x, y : LONGINT; dragInfo : WMWindowManager.DragInfo);
  2892. VAR dropTarget : TextDropTarget;
  2893. pos, internalPos : LONGINT;
  2894. p : Texts.TextPosition;
  2895. BEGIN
  2896. IF takesFocus.Get() THEN
  2897. text.AcquireRead;
  2898. ViewToTextPos(x, y, pos) ;
  2899. (* prevent a selection from being dropped behind the paragraph separator *)
  2900. internalPos := GetInternalPos(pos);
  2901. IF text.isUTF & (layout.bidiFormatter # NIL) THEN
  2902. IF layout.bidiFormatter.IsLastCharacterInLine(internalPos) THEN
  2903. DEC(internalPos);
  2904. END;
  2905. END;
  2906. NEW(p, text); p.SetPosition(internalPos);
  2907. NEW(dropTarget, text, p);
  2908. text.ReleaseRead;
  2909. IF ~hasFocus & ~alwaysShowCursorI THEN cursor.SetVisible(FALSE) END;
  2910. dragInfo.data := dropTarget;
  2911. ConfirmDrag(TRUE, dragInfo);
  2912. IF dragInfo.onAccept # NIL THEN dragInfo.onAccept(SELF, dragInfo) END;
  2913. ELSE
  2914. ConfirmDrag(FALSE, dragInfo);
  2915. IF dragInfo.onReject # NIL THEN dragInfo.onReject(SELF, dragInfo) END;
  2916. END;
  2917. END DragDropped;
  2918. PROCEDURE CopySelection*;
  2919. BEGIN
  2920. IF isPassword.Get() THEN RETURN END;
  2921. text.AcquireRead;
  2922. Texts.clipboard.AcquireWrite;
  2923. selection.Sort;
  2924. IF selection.b - selection.a > 0 THEN
  2925. (* clear the clipboard *)
  2926. IF Texts.clipboard.GetLength() > 0 THEN Texts.clipboard.Delete(0, Texts.clipboard.GetLength()) END;
  2927. Texts.clipboard.CopyFromText(text, selection.a, selection.b - selection.a, 0);
  2928. END;
  2929. Texts.clipboard.ReleaseWrite;
  2930. text.ReleaseRead
  2931. END CopySelection;
  2932. PROCEDURE DeleteSelection*;
  2933. BEGIN
  2934. Acquire; (* protect cursor *)
  2935. text.AcquireWrite;
  2936. selection.Sort;
  2937. text.Delete(selection.a, selection.b - selection.a);
  2938. cursor.SetVisible(TRUE);
  2939. text.ReleaseWrite;
  2940. Release;
  2941. END DeleteSelection;
  2942. PROCEDURE Paste*;
  2943. BEGIN
  2944. text.AcquireWrite;
  2945. Texts.clipboard.AcquireRead;
  2946. IF Texts.clipboard.GetLength() > 0 THEN
  2947. IF selection.b - selection.a # 0 THEN DeleteSelection() END;
  2948. text.CopyFromText(Texts.clipboard, 0, Texts.clipboard.GetLength(), cursor.GetPosition())
  2949. END;
  2950. Texts.clipboard.ReleaseRead;
  2951. text.ReleaseWrite
  2952. END Paste;
  2953. PROCEDURE SelectAll*;
  2954. BEGIN
  2955. Acquire; (* protect cursor *)
  2956. text.AcquireRead;
  2957. selection.SetFromTo(0, text.GetLength());
  2958. cursor.SetVisible(text.GetLength() <= 0);
  2959. Texts.SetLastSelection(text, selection.from, selection.to);
  2960. text.ReleaseRead;
  2961. Release;
  2962. END SelectAll;
  2963. (* Prepare to start the selection by keyboard. Clear the selection, if it is not contigous *)
  2964. PROCEDURE KeyStartSelection(pos : LONGINT);
  2965. BEGIN
  2966. IF selection.to.GetPosition() # pos THEN selection.SetFromTo(pos, pos); cursor.SetVisible(TRUE); Texts.ClearLastSelection END;
  2967. END KeyStartSelection;
  2968. (* update the keyboard selection with the new position, redraw from the last StartSelection *)
  2969. PROCEDURE KeyUpdateSelection(pos : LONGINT);
  2970. BEGIN
  2971. selection.SetTo(pos);
  2972. selection.Sort;
  2973. cursor.SetVisible(selection.b - selection.a <= 0);
  2974. Texts.SetLastSelection(text, selection.from, selection.to)
  2975. END KeyUpdateSelection;
  2976. PROCEDURE CursorChanged;
  2977. BEGIN
  2978. cursorBlinker.Show(cursor);
  2979. IF (onCursorChanged # NIL) THEN onCursorChanged END
  2980. END CursorChanged;
  2981. PROCEDURE CursorUp*(select : BOOLEAN);
  2982. VAR
  2983. pos, cPos, cl, lineStart : LONGINT;
  2984. BEGIN
  2985. Acquire;
  2986. text.AcquireRead;
  2987. pos := GetInternalPos(cursor.GetPosition());
  2988. IF select THEN
  2989. KeyStartSelection(pos)
  2990. ELSE
  2991. selection.SetFromTo(pos, pos);
  2992. cursor.SetVisible(TRUE);
  2993. Texts.ClearLastSelection
  2994. END;
  2995. cl := layout.FindLineNrByPos(pos);
  2996. IF cl > 0 THEN
  2997. DEC(cl);
  2998. lineStart := layout.GetLineStartPos(cl);
  2999. cPos := lineStart + Strings.Min(layout.GetLineLength(cl) - 1, lineEnter);
  3000. cursor.SetPosition(cPos);
  3001. IF cl < firstLineI THEN firstLine.Set(cl) END
  3002. END;
  3003. IF select THEN
  3004. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()));
  3005. END;
  3006. text.ReleaseRead;
  3007. Release;
  3008. CursorChanged
  3009. END CursorUp;
  3010. PROCEDURE CursorDown*(select : BOOLEAN);
  3011. VAR pos, cPos, cl, lineStart : LONGINT;
  3012. BEGIN
  3013. Acquire;
  3014. text.AcquireRead;
  3015. pos := GetInternalPos(cursor.GetPosition());
  3016. IF select THEN
  3017. KeyStartSelection(pos)
  3018. ELSE
  3019. selection.SetFromTo(pos, pos);
  3020. cursor.SetVisible(TRUE);
  3021. Texts.ClearLastSelection
  3022. END;
  3023. cl := layout.FindLineNrByPos(pos);
  3024. IF cl < layout.GetNofLines() - 1 THEN
  3025. INC(cl);
  3026. lineStart := layout.GetLineStartPos(cl);
  3027. cPos := lineStart + Strings.Min(layout.GetLineLength(cl) - 1, lineEnter);
  3028. cursor.SetPosition(cPos);
  3029. IF cl > FindLineByY(firstLineI, bounds.GetHeight() - bordersI.b) THEN firstLine.Set(firstLineI + 1 ) END
  3030. END;
  3031. IF select THEN
  3032. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3033. END;
  3034. text.ReleaseRead;
  3035. Release;
  3036. CursorChanged
  3037. END CursorDown;
  3038. (* Move the cursor one character/word to the left *)
  3039. PROCEDURE CursorLeft*(word, select : BOOLEAN);
  3040. VAR
  3041. pos, cPos, wPos : LONGINT;
  3042. BEGIN
  3043. Acquire;
  3044. text.AcquireRead;
  3045. PositionDebugging.SetPos(GetInternalPos(cursor.GetPosition()),cursor.GetPosition());
  3046. pos := GetInternalPos(cursor.GetPosition());
  3047. IF select THEN
  3048. KeyStartSelection(pos)
  3049. ELSE
  3050. selection.SetFromTo(pos, pos);
  3051. cursor.SetVisible(TRUE);
  3052. Texts.ClearLastSelection
  3053. END;
  3054. cPos := GetInternalPos(cursor.GetPosition()) - 1;
  3055. IF ~word THEN
  3056. cursor.SetPosition(GetDisplayPos(cPos));
  3057. ELSE
  3058. wPos := TextUtilities.FindPosWordLeft(utilreader, cPos);
  3059. cursor.SetPosition(GetDisplayPos(wPos));
  3060. END;
  3061. IF select THEN
  3062. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3063. END;
  3064. StoreLineEnter;
  3065. text.ReleaseRead;
  3066. Release;
  3067. CursorChanged
  3068. END CursorLeft;
  3069. (* Move the cursor one character/word to the right *)
  3070. PROCEDURE CursorRight*(word, select : BOOLEAN);
  3071. VAR
  3072. pos, cPos, wPos : LONGINT;
  3073. BEGIN
  3074. Acquire;
  3075. text.AcquireRead;
  3076. PositionDebugging.SetPos(GetInternalPos(cursor.GetPosition()),cursor.GetPosition());
  3077. pos := GetInternalPos(cursor.GetPosition());
  3078. IF select THEN
  3079. KeyStartSelection(pos)
  3080. ELSE
  3081. selection.SetFromTo(pos, pos);
  3082. cursor.SetVisible(TRUE);
  3083. Texts.ClearLastSelection
  3084. END;
  3085. cPos := GetInternalPos(cursor.GetPosition()) + 1;
  3086. IF ~word THEN
  3087. cursor.SetPosition(GetDisplayPos(cPos));
  3088. ELSE
  3089. wPos := TextUtilities.FindPosWordRight(utilreader, cPos);
  3090. cursor.SetPosition(GetDisplayPos(wPos));
  3091. END;
  3092. IF select THEN
  3093. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3094. END;
  3095. StoreLineEnter;
  3096. text.ReleaseRead;
  3097. Release;
  3098. CursorChanged
  3099. END CursorRight;
  3100. PROCEDURE PageDown*(select : BOOLEAN);
  3101. VAR dy : LONGINT; i, pos, iPos : LONGINT;
  3102. cx, cy : LONGINT;
  3103. BEGIN
  3104. Acquire;
  3105. text.AcquireRead;
  3106. iPos := GetInternalPos(cursor.GetPosition());
  3107. IF select THEN
  3108. KeyStartSelection(iPos)
  3109. ELSE
  3110. selection.SetFromTo(iPos, iPos);
  3111. cursor.SetVisible(TRUE);
  3112. Texts.ClearLastSelection
  3113. END;
  3114. IF firstLineI = layout.GetNofLines() - 1 THEN
  3115. cursor.SetPosition(text.GetLength());
  3116. ELSE
  3117. (* save cursor screen pos for repositioning *)
  3118. IF ~FindScreenPos(cursor.GetPosition(), cx, cy) THEN cx := 0; cy := 0 END;
  3119. i := firstLineI; dy := 0;
  3120. WHILE (i < layout.GetNofLines() - 1) & (dy < bounds.GetHeight() - bordersI.t - bordersI.b) DO
  3121. INC(i); dy := dy + (layout.lines[i].height)
  3122. END;
  3123. firstLine.Set(i);
  3124. (* set cursor to nearest pos on new page *)
  3125. ViewToTextPos(cx, cy, pos);
  3126. IF pos >= 0 THEN
  3127. cursor.SetPosition(pos);
  3128. END;
  3129. END;
  3130. IF select THEN
  3131. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3132. END;
  3133. text.ReleaseRead;
  3134. Release;
  3135. CursorChanged
  3136. END PageDown;
  3137. PROCEDURE PageUp*(select : BOOLEAN);
  3138. VAR dy : LONGINT; i, pos, iPos : LONGINT;
  3139. cx, cy : LONGINT;
  3140. BEGIN
  3141. Acquire;
  3142. text.AcquireRead;
  3143. iPos := GetInternalPos(cursor.GetPosition());
  3144. IF select THEN
  3145. KeyStartSelection(iPos)
  3146. ELSE
  3147. selection.SetFromTo(iPos, iPos);
  3148. cursor.SetVisible(TRUE);
  3149. Texts.ClearLastSelection
  3150. END;
  3151. IF firstLineI = 0 THEN
  3152. cursor.SetPosition(0);
  3153. ELSE
  3154. (* save cursor screen pos for repositioning *)
  3155. IF ~FindScreenPos(cursor.GetPosition(), cx, cy) THEN cx := 0; cy := 0 END;
  3156. (* go up one page but at least one page *)
  3157. i := firstLineI; dy := 0;
  3158. WHILE (i > 0) & (dy < bounds.GetHeight() - bordersI.t - bordersI.b) DO
  3159. DEC(i); dy := dy + (layout.lines[i].height)
  3160. END;
  3161. IF (i > 0) & (i = firstLineI) THEN DEC(i) END;
  3162. firstLine.Set(i);
  3163. (* set cursor to nearest pos on new page *)
  3164. ViewToTextPos(cx, cy, pos);
  3165. IF pos >= 0 THEN
  3166. cursor.SetPosition(pos);
  3167. END
  3168. END;
  3169. IF select THEN
  3170. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3171. END;
  3172. text.ReleaseRead;
  3173. Release;
  3174. CursorChanged
  3175. END PageUp;
  3176. PROCEDURE Home*(ctrl, select : BOOLEAN);
  3177. VAR
  3178. lineStart, cl, pos : LONGINT;
  3179. BEGIN
  3180. Acquire;
  3181. text.AcquireRead;
  3182. pos := GetInternalPos(cursor.GetPosition());
  3183. IF select THEN
  3184. KeyStartSelection(pos)
  3185. ELSE
  3186. selection.SetFromTo(pos, pos);
  3187. cursor.SetVisible(TRUE);
  3188. Texts.ClearLastSelection
  3189. END;
  3190. IF ctrl THEN
  3191. cursor.SetPosition(GetDisplayPos(0));
  3192. firstLine.Set(0)
  3193. ELSE
  3194. cl := layout.FindLineNrByPos(cursor.GetPosition());
  3195. lineStart := layout.GetLineStartPos(cl);
  3196. cursor.SetPosition(GetDisplayPos(lineStart));
  3197. END;
  3198. StoreLineEnter;
  3199. IF select THEN
  3200. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3201. END;
  3202. text.ReleaseRead;
  3203. Release;
  3204. CursorChanged
  3205. END Home;
  3206. PROCEDURE End*(ctrl, select : BOOLEAN);
  3207. VAR lineEnd, textLength, cl, pos, dispPos: LONGINT;
  3208. BEGIN
  3209. Acquire;
  3210. text.AcquireRead;
  3211. pos := GetInternalPos(cursor.GetPosition());
  3212. IF select THEN
  3213. KeyStartSelection(pos)
  3214. ELSE
  3215. selection.SetFromTo(pos, pos);
  3216. cursor.SetVisible(TRUE);
  3217. Texts.ClearLastSelection
  3218. END;
  3219. IF ctrl THEN
  3220. textLength := text.GetLength();
  3221. cursor.SetPosition(GetDisplayPos(textLength));
  3222. firstLine.Set(layout.FindLineNrByPos(text.GetLength()))
  3223. ELSE
  3224. cl := layout.FindLineNrByPos(cursor.GetPosition());
  3225. lineEnd := layout.GetLineStartPos(cl) + layout.GetLineLength(cl) - 1;
  3226. dispPos := GetDisplayPos(lineEnd);
  3227. cursor.SetPosition(dispPos);
  3228. END;
  3229. StoreLineEnter;
  3230. IF select THEN
  3231. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3232. END;
  3233. text.ReleaseRead;
  3234. Release;
  3235. CursorChanged
  3236. END End;
  3237. PROCEDURE KeyEvent*(ucs :LONGINT; flags : SET; VAR keysym : LONGINT);
  3238. BEGIN
  3239. modifierFlags := flags;
  3240. IF Inputs.Release IN flags THEN RETURN END;
  3241. dragCopy := modifierFlags * Inputs.Ctrl # {};
  3242. IF keysym = 01H THEN (* Ctrl-A *)
  3243. SelectAll
  3244. ELSIF keysym = 03H THEN (* Ctrl-C *)
  3245. CopySelection
  3246. ELSIF (keysym = 0FF63H) & (flags * Inputs.Ctrl # {}) THEN (*Ctrl Insert *)
  3247. CopySelection
  3248. ELSIF keysym = 12H THEN (* Ctrl-R *)
  3249. layout.FullLayout(TRUE); Invalidate;CheckNumberOfLines;
  3250. KernelLog.String("Refreshed"); KernelLog.Ln;
  3251. ELSIF keysym = 0FF51H THEN (* Cursor Left *)
  3252. CursorLeft(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  3253. ELSIF keysym = 0FF53H THEN (* Cursor Right *)
  3254. CursorRight(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  3255. ELSIF keysym = 0FF54H THEN (* Cursor Down *)
  3256. CursorDown(flags * Inputs.Shift # {})
  3257. ELSIF keysym = 0FF52H THEN (* Cursor Up *)
  3258. CursorUp(flags * Inputs.Shift # {})
  3259. ELSIF keysym = 0FF56H THEN (* Page Down *)
  3260. PageDown(flags * Inputs.Shift # {})
  3261. ELSIF keysym = 0FF55H THEN (* Page Up *)
  3262. PageUp(flags * Inputs.Shift # {})
  3263. ELSIF keysym = 0FF50H THEN (* Cursor Home *)
  3264. Home(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  3265. ELSIF keysym = 0FF57H THEN (* Cursor End *)
  3266. End(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  3267. END
  3268. END KeyEvent;
  3269. (* called by users that override the KeyEvents to allow copy drag drop *)
  3270. PROCEDURE SetFlags*(flags : SET);
  3271. BEGIN
  3272. modifierFlags := flags;
  3273. dragCopy := modifierFlags * Inputs.Ctrl # {};
  3274. END SetFlags;
  3275. PROCEDURE FindCommandRange*(pos: LONGINT; VAR start, end, nofLastSelections : LONGINT);
  3276. VAR ch : LONGINT; string : ARRAY 23 OF CHAR; i : LONGINT; sDoCommands, lastWasTilde : BOOLEAN;
  3277. escapeString: ARRAY 32 OF LONGINT; escapePos: LONGINT; escape: BOOLEAN;
  3278. (* note: this simple algorithm can be emplyed if the substring to be implicitly searched for does not contain its first character *)
  3279. PROCEDURE String(escape: BOOLEAN; CONST escapeString: ARRAY OF LONGINT);
  3280. VAR done: BOOLEAN; escapePos: LONGINT;
  3281. BEGIN
  3282. done := FALSE; escapePos := -1;
  3283. REPEAT
  3284. utilreader.ReadCh(ch);
  3285. IF ch = ORD('"') THEN
  3286. IF escape THEN
  3287. escapePos := 0;
  3288. ELSE
  3289. done := TRUE
  3290. END;
  3291. ELSIF escapePos >= 0 THEN
  3292. IF escapeString[escapePos] = 0 THEN
  3293. IF ch =ORD("\") THEN done := TRUE
  3294. ELSE escapePos := -1
  3295. END;
  3296. ELSIF escapeString[escapePos] # ch THEN
  3297. escapePos := -1;
  3298. ELSE
  3299. INC(escapePos);
  3300. END;
  3301. END;
  3302. UNTIL done OR utilreader.eot;
  3303. END String;
  3304. BEGIN
  3305. nofLastSelections := 0;
  3306. text.AcquireRead;
  3307. utilreader.SetDirection(-1); utilreader.SetPosition(pos);
  3308. REPEAT utilreader.ReadCh(ch) UNTIL TextUtilities.IsWhiteSpace(ch,text.isUTF) OR utilreader.eot;
  3309. start := utilreader.GetPosition() + 2;
  3310. IF utilreader.eot THEN DEC(start, 2) END;
  3311. (* search ~ *)
  3312. i := 0; sDoCommands := FALSE; lastWasTilde := FALSE;
  3313. utilreader.SetDirection(1); utilreader.SetPosition(start);
  3314. REPEAT
  3315. utilreader.ReadCh(ch);
  3316. IF ch = ORD('"') THEN
  3317. escapeString[escapePos] := 0;
  3318. String(escape, escapeString);
  3319. ELSIF ch =ORD("\") THEN
  3320. escape := TRUE;
  3321. escapePos := 0;
  3322. ELSIF escape THEN
  3323. IF TextUtilities.IsWhiteSpace(ch,text.isUTF) THEN escape := FALSE
  3324. ELSE escapeString[escapePos] := ch; INC(escapePos);
  3325. END;
  3326. END;
  3327. (* check whether the command is SystemTools.DoCommands *)
  3328. IF (i < 22) THEN
  3329. string[i] := CHR(ch);
  3330. INC(i);
  3331. IF (i = 22) THEN
  3332. string[22] := 0X;
  3333. IF (string = "SystemTools.DoCommands") OR Strings.StartsWith2("PreliminaryCommands",string) THEN
  3334. sDoCommands := TRUE;
  3335. END;
  3336. END;
  3337. END;
  3338. IF (CHR(ch) = "^") THEN
  3339. INC(nofLastSelections);
  3340. END;
  3341. (* We do a special treatment of the command SystemTools.DoCommands since we don't want a single
  3342. tilde character to delimit the parameter string for the particular command - but two tilde characters *)
  3343. IF sDoCommands THEN
  3344. IF (ch = ORD("~")) THEN
  3345. IF ~lastWasTilde THEN
  3346. lastWasTilde := TRUE;
  3347. utilreader.ReadCh(ch);
  3348. ELSE
  3349. (* Two tilde characters only separated with whitespace means this is the
  3350. end of the SystemTools.DoCommands parameter string *)
  3351. END;
  3352. ELSIF lastWasTilde & ~TextUtilities.IsWhiteSpace(ch,text.isUTF) THEN
  3353. lastWasTilde := FALSE;
  3354. END;
  3355. END;
  3356. UNTIL (ch = ORD("~")) OR (utilreader.eot);
  3357. end := utilreader.GetPosition() - 1;
  3358. IF utilreader.eot THEN INC(end) END;
  3359. text.ReleaseRead
  3360. END FindCommandRange;
  3361. PROCEDURE FindCommand*(pos: LONGINT; VAR start, end : LONGINT);
  3362. VAR ch : LONGINT;
  3363. BEGIN
  3364. text.AcquireRead;
  3365. utilreader.SetDirection(-1); utilreader.SetPosition(pos);
  3366. REPEAT utilreader.ReadCh(ch) UNTIL TextUtilities.IsWhiteSpace(ch,text.isUTF) OR utilreader.eot;
  3367. start := utilreader.GetPosition() + 2;
  3368. IF utilreader.eot THEN DEC(start, 2) END;
  3369. utilreader.SetDirection(1); utilreader.SetPosition(pos);
  3370. REPEAT utilreader.ReadCh(ch) UNTIL TextUtilities.IsWhiteSpace(ch,text.isUTF) OR utilreader.eot;
  3371. end := utilreader.GetPosition() - 1;
  3372. IF utilreader.eot THEN INC(end) END;
  3373. text.ReleaseRead;
  3374. END FindCommand;
  3375. (** Start the command in the text, starting on pos (or wordboundary before),
  3376. caller should hold lock on text to make the pos stable *)
  3377. PROCEDURE StartCommand*(pos : LONGINT; openFile : BOOLEAN);
  3378. VAR
  3379. start, end, bufSize : LONGINT;
  3380. context : Commands.Context;
  3381. arg : Streams.StringReader;
  3382. command : ARRAY MaxCommandLength OF CHAR;
  3383. parameters : POINTER TO ARRAY OF CHAR;
  3384. s : Strings.String;
  3385. msg : ARRAY 128 OF CHAR;
  3386. ignore : Modules.Name;
  3387. paramSize, nofLastSelections, i, j, a, b, res : LONGINT;
  3388. selectionText : Texts.Text;
  3389. selectionOk : BOOLEAN;
  3390. from, to: Texts.TextPosition;
  3391. commandCaller:OBJECT;
  3392. commandWriter, errorWriter: Streams.Writer;
  3393. BEGIN
  3394. Acquire;
  3395. text.AcquireRead;
  3396. IF openFile THEN FindCommand(pos, start, end)
  3397. ELSE FindCommandRange(pos, start, end, nofLastSelections)
  3398. END;
  3399. bufSize := Strings.Max(Strings.Min((end - start) * 5 + 1 (* for UTF *), MaxCallParameterBuf), 1);
  3400. NEW(s, bufSize);
  3401. paramSize := 0;
  3402. TextUtilities.SubTextToStrAt(text, start, end - start, paramSize, s^);
  3403. INC(paramSize);
  3404. text.ReleaseRead;
  3405. Release;
  3406. IF Inputs.Shift * modifierFlags # {} THEN
  3407. (*
  3408. Command / open will not see the caller => called as if no calling context was specified.
  3409. => Opening a text while holding a shift key down will usually result in a new viewer being opened.
  3410. *)
  3411. commandCaller := NIL
  3412. ELSE
  3413. commandCaller := SELF.commandCaller;
  3414. END;
  3415. IF openFile THEN
  3416. FileHandlers.OpenFile(s^, NIL, commandCaller)
  3417. ELSE
  3418. command := "";
  3419. i := 0;
  3420. WHILE (i < MaxCommandLength) & (s[i] # 0X) & (s[i] # ";") & (s[i] # " ") & (s[i] # 09X) & (s[i] # 0DX) & (s[i] # 0AX) DO
  3421. command[i] := s[i]; INC(i);
  3422. END;
  3423. IF i < MaxCommandLength THEN
  3424. command[i] := 0X;
  3425. INC(i);
  3426. Commands.Split(command, ignore, ignore, res, msg);
  3427. IF res # Commands.Ok THEN
  3428. KernelLog.String("WMTextView: Command parsing error, res: "); KernelLog.Int(res, 0);
  3429. KernelLog.String(" ("); KernelLog.String(msg); KernelLog.String(")"); KernelLog.Ln;
  3430. RETURN;
  3431. END;
  3432. ELSE
  3433. KernelLog.String("WMTextView: Command execution error: Command too long"); KernelLog.Ln;
  3434. RETURN;
  3435. END;
  3436. IF (Inputs.Alt * modifierFlags # {}) THEN
  3437. (* execute AltMMCommand with actual command and its parameters as parameter *)
  3438. COPY(AltMMCommand, command);
  3439. commandWriter := NIL; errorWriter := NIL;
  3440. i := 0;
  3441. ELSE
  3442. commandWriter := SELF.commandWriter;
  3443. errorWriter := SELF.errorWriter;
  3444. END;
  3445. IF (i < LEN(s)) THEN (* copy parameter string *)
  3446. selectionOk := FALSE;
  3447. IF (nofLastSelections > 0) THEN
  3448. IF Texts.GetLastSelection(selectionText, from, to) THEN
  3449. selectionOk := TRUE;
  3450. selectionText.AcquireRead;
  3451. a := Strings.Min(from.GetPosition(), to.GetPosition());
  3452. b := Strings.Max(from.GetPosition(), to.GetPosition());
  3453. INC(paramSize, b - a + 1);
  3454. END;
  3455. END;
  3456. NEW(parameters, paramSize);
  3457. j := 0;
  3458. WHILE (i < LEN(s)) & (j < LEN(parameters)-1) DO
  3459. IF (s[i] = "^") & selectionOk THEN
  3460. TextUtilities.SubTextToStrAt(selectionText, a, b - a, j, parameters^);
  3461. ELSE
  3462. parameters[j] := s[i]; INC(j);
  3463. END;
  3464. INC(i);
  3465. END;
  3466. parameters[j] := 0X;
  3467. IF selectionOk THEN
  3468. selectionText.ReleaseRead;
  3469. END;
  3470. ELSE
  3471. NEW(parameters, 1); parameters[0] := 0X;
  3472. END;
  3473. NEW(arg, LEN(parameters)); arg.SetRaw(parameters^, 0, LEN(parameters));
  3474. NEW(context, NIL, arg, commandWriter, errorWriter, commandCaller);
  3475. IF TraceCommands IN Trace THEN
  3476. KernelLog.String("WMTextView: Executing command: '"); KernelLog.String(command); KernelLog.String("'");
  3477. KernelLog.String(", parameters: ");
  3478. IF (parameters[0] = 0X) THEN KernelLog.String("None"); ELSE KernelLog.String("'"); KernelLog.String(parameters^); KernelLog.String("'"); END;
  3479. KernelLog.Ln;
  3480. END;
  3481. Commands.Activate(command, context, {}, res, msg);
  3482. IF (res # Commands.Ok) THEN
  3483. IF commandWriter # NIL THEN
  3484. commandWriter.String("WMTextView: Command execution error, res: "); commandWriter.Int(res, 0);
  3485. commandWriter.String(" ("); commandWriter.String(msg); commandWriter.String(")"); commandWriter.Ln;
  3486. commandWriter.Update;
  3487. ELSE
  3488. KernelLog.String("WMTextView: Command execution error, res: "); KernelLog.Int(res, 0);
  3489. KernelLog.String(" ("); KernelLog.String(msg); KernelLog.String(")"); KernelLog.Ln;
  3490. END;
  3491. END;
  3492. END;
  3493. END StartCommand;
  3494. PROCEDURE Start(sender, data: ANY);
  3495. VAR msg: ARRAY 512 OF CHAR; res: LONGINT;
  3496. BEGIN
  3497. IF (data # NIL) & (data IS ClickInfo) THEN
  3498. IF data(ClickInfo).cmdPar # NIL THEN
  3499. Commands.Call(data(ClickInfo).cmdPar^, {}, res, msg);
  3500. IF res # 0 THEN KernelLog.String("WMTextView: "); KernelLog.String(msg); KernelLog.Ln END;
  3501. END
  3502. END
  3503. END Start;
  3504. PROCEDURE Open(sender, data: ANY);
  3505. BEGIN
  3506. IF (data # NIL) & (data IS ClickInfo) THEN
  3507. IF data(ClickInfo).cmd # NIL THEN
  3508. FileHandlers.OpenFile(data(ClickInfo).cmd^, NIL, commandCaller)
  3509. END
  3510. END
  3511. END Open;
  3512. PROCEDURE PieMenuStart(sender, data: ANY);
  3513. BEGIN
  3514. Start(piemenu, piemenu.userData)
  3515. END PieMenuStart;
  3516. PROCEDURE PieMenuOpen(sender, data: ANY);
  3517. BEGIN
  3518. Open(piemenu, piemenu.userData)
  3519. END PieMenuOpen;
  3520. PROCEDURE PieMenuCopy(sender, data: ANY);
  3521. BEGIN
  3522. CopySelection;
  3523. END PieMenuCopy;
  3524. PROCEDURE PieMenuPaste(sender, data: ANY);
  3525. BEGIN
  3526. Paste;
  3527. END PieMenuPaste;
  3528. PROCEDURE ShowContextMenu(x, y: LONGINT);
  3529. VAR
  3530. popup : WMPopups.Popup;
  3531. start, end, bufSize : LONGINT;
  3532. command, s : Strings.String;
  3533. clickInfo : ClickInfo;
  3534. str : ARRAY 256 OF CHAR;
  3535. window : WMWindowManager.Window;
  3536. nofLastSelections : LONGINT;
  3537. BEGIN
  3538. ASSERT(IsCallFromSequencer());
  3539. text.AcquireRead;
  3540. FindCommand(cursor.GetPosition(), start, end);
  3541. bufSize := Strings.Max(Strings.Min((end - start) * 5 + 1 (* for UTF *), 4096), 1);
  3542. NEW(command, bufSize);
  3543. TextUtilities.SubTextToStr(text, start, end - start, command^);
  3544. FindCommandRange(cursor.GetPosition(), start, end, nofLastSelections);
  3545. bufSize := Strings.Max(Strings.Min((end - start) * 5 + 1 (* for UTF *), MaxCallParameterBuf), 1);
  3546. NEW(s, bufSize);
  3547. TextUtilities.SubTextToStr(text, start, end - start, s^);
  3548. text.ReleaseRead;
  3549. NEW(clickInfo);
  3550. clickInfo.cmd := command;
  3551. clickInfo.cmdPar := s;
  3552. IF UsePieMenu THEN
  3553. NEW(piemenu); piemenu.SetEnabled({0, 1, 2, 3});
  3554. piemenu.SetText(1, Strings.NewString("Open"));
  3555. piemenu.SetText(3, Strings.NewString("Start"));
  3556. piemenu.SetText(2, Strings.NewString("Copy"));
  3557. piemenu.SetText(0, Strings.NewString("Paste"));
  3558. piemenu.userData := clickInfo;
  3559. piemenu.on1.Add(PieMenuOpen);
  3560. piemenu.on2.Add(PieMenuCopy);
  3561. piemenu.on3.Add(PieMenuStart);
  3562. piemenu.on0.Add(PieMenuPaste);
  3563. manager := WMWindowManager.GetDefaultManager();
  3564. window := manager.GetPositionOwner(x, y);
  3565. IF window = NIL THEN RETURN END;
  3566. Acquire; ToWMCoordinates(x, y, x, y); Release;
  3567. piemenu.Show(window, x, y, FALSE);
  3568. (* TODO: Can't set := NIL, since its used by the button handlers *)
  3569. ELSE
  3570. NEW(popup);
  3571. str := "Start "; Strings.Append(str, command^); popup.AddParButton(str, Start, clickInfo);
  3572. str := "Open "; Strings.Append(str, command^); popup.AddParButton(str, Open, clickInfo);
  3573. Acquire; ToWMCoordinates(x, y, x, y); Release;
  3574. popup.Popup(x, y);
  3575. END
  3576. END ShowContextMenu;
  3577. PROCEDURE HandleInternal(VAR x: WMMessages.Message);
  3578. VAR pos : LONGINT; obj : ANY; link : Texts.Link;
  3579. BEGIN
  3580. ASSERT(IsCallFromSequencer());
  3581. IF (x.msgType = WMMessages.MsgKey) & objHasFocus THEN (* forward KeyMsg *)
  3582. WITH focusObject : WMComponents.VisualComponent DO
  3583. focusObject.Handle(x);
  3584. InvalidateRange(focusPos, focusPos+1)
  3585. END
  3586. ELSIF (x.msgType # WMMessages.MsgKey) & HitObject(x.x, x.y, pos, obj) THEN (* forward Msg *)
  3587. SetFocus; cursor.SetVisible(FALSE);
  3588. IF obj IS WMComponents.VisualComponent THEN
  3589. WITH obj : WMComponents.VisualComponent DO
  3590. (* remove oldObject first *)
  3591. IF (oldObject # NIL) & (oldObject # obj) THEN
  3592. oldObject(WMComponents.VisualComponent).Handle(x);
  3593. InvalidateRange(oldPos, oldPos+1);
  3594. END;
  3595. TransformCoordinates(x.x, x.y, obj); (* transform to obj coords *)
  3596. obj.Handle(x); (* call obj Handle *)
  3597. ChangePointer(obj.GetPointerInfo()); (* change the pointer Image *)
  3598. InvalidateRange(pos, pos+1); (* redraw obj *)
  3599. oldObject := obj; oldPos := pos; (* store last object *)
  3600. (* transfer focus to Object *)
  3601. IF (x.msgType = WMMessages.MsgPointer) & (x.flags * {0, 1, 2} = {0}) THEN
  3602. (* remove old focus first *)
  3603. IF (focusObject # NIL) & (focusObject # obj) THEN
  3604. focusObject(WMComponents.VisualComponent).FocusLost;
  3605. InvalidateRange(focusPos, focusPos+1)
  3606. END;
  3607. objHasFocus := TRUE;
  3608. focusObject := obj; focusPos := pos;
  3609. (* FocusLost *)
  3610. END
  3611. END
  3612. END
  3613. ELSIF (x.msgType = WMMessages.MsgPointer) & HitLink(x.x, x.y, pos, link) THEN (* Link *)
  3614. ChangePointer(manager.pointerLink);
  3615. IF (x.msgSubType = 2) &(oldFlags / x.flags = {CallURLPointer}) THEN LinkClick(link); END;
  3616. oldFlags := x.flags;
  3617. ELSE
  3618. ChangePointer(manager.pointerText); (* change Pointer back *)
  3619. (* transfer focus back to TextView *)
  3620. IF (focusObject # NIL) & (x.msgType = WMMessages.MsgPointer) & (x.flags * {0, 1, 2} = {0}) THEN
  3621. focusObject(WMComponents.VisualComponent).FocusLost;
  3622. objHasFocus := FALSE;
  3623. InvalidateRange(focusPos, focusPos+1);
  3624. FocusReceived;
  3625. focusObject := NIL
  3626. END;
  3627. (* update last Object *)
  3628. IF (oldObject # NIL) & (x.msgType = WMMessages.MsgPointer) THEN
  3629. oldObject(WMComponents.VisualComponent).Handle(x);
  3630. InvalidateRange(oldPos, oldPos+1);
  3631. oldObject := NIL
  3632. END;
  3633. IF (x.msgType = WMMessages.MsgExt) & (x.ext # NIL) & (x.ext IS Texts.StyleChangedMsg) THEN
  3634. layout.FullLayout(TRUE); Invalidate; CheckNumberOfLines;
  3635. ELSE
  3636. HandleInternal^(x);
  3637. END;
  3638. END
  3639. END HandleInternal;
  3640. END TextView;
  3641. TYPE
  3642. FontEntry = OBJECT
  3643. VAR
  3644. name : ARRAY 256 OF CHAR;
  3645. attributes : FontAttributes;
  3646. next : FontEntry;
  3647. PROCEDURE &Init(CONST name : ARRAY OF CHAR);
  3648. BEGIN
  3649. COPY(name, SELF.name);
  3650. attributes := NIL;
  3651. next := NIL;
  3652. END Init;
  3653. END FontEntry;
  3654. FontAttributes = OBJECT
  3655. VAR
  3656. font : WMGraphics.Font; (* { font # {} *)
  3657. size : LONGINT;
  3658. style : SET;
  3659. next : FontAttributes;
  3660. PROCEDURE &Init(size : LONGINT; style : SET);
  3661. BEGIN
  3662. font := NIL;
  3663. SELF.size := size;
  3664. SELF.style := style;
  3665. next := NIL;
  3666. END Init;
  3667. END FontAttributes;
  3668. (* not thread-safe! not global to avoid locking and keep size smaller *)
  3669. FontCache = OBJECT
  3670. VAR
  3671. entries : FontEntry;
  3672. defaultFont : WMGraphics.Font;
  3673. PROCEDURE &Init;
  3674. BEGIN
  3675. NEW(entries, "head"); (* head of list *)
  3676. defaultFont := WMGraphics.GetDefaultFont();
  3677. END Init;
  3678. PROCEDURE Find(CONST name : ARRAY OF CHAR; size : LONGINT; style : SET) : WMGraphics.Font;
  3679. VAR font : WMGraphics.Font; e : FontEntry; a : FontAttributes;
  3680. BEGIN
  3681. font := NIL;
  3682. e := entries.next;
  3683. WHILE (e # NIL ) & (e.name < name) DO e := e.next; END;
  3684. IF (e # NIL) & (e.name = name) THEN
  3685. a := e.attributes;
  3686. WHILE (a # NIL) & (a.size < size) DO a := a.next; END;
  3687. WHILE (a # NIL) & (a.size = size) & (a.style # style) DO a := a.next; END;
  3688. IF (a # NIL) & (a.size = size) THEN
  3689. ASSERT(a.font # NIL);
  3690. font := a.font;
  3691. END;
  3692. END;
  3693. RETURN font;
  3694. END Find;
  3695. PROCEDURE Add(CONST name : ARRAY OF CHAR; size : LONGINT; style : SET) : WMGraphics.Font;
  3696. VAR entry, e : FontEntry; attribute, a : FontAttributes;
  3697. BEGIN
  3698. e := entries;
  3699. WHILE (e.next # NIL) & (e.next.name < name) DO e := e.next; END;
  3700. IF (e.next # NIL) & (e.next.name = name) THEN
  3701. entry := e.next;
  3702. ELSE
  3703. NEW(entry, name);
  3704. entry.next := e.next;
  3705. e.next := entry;
  3706. END;
  3707. ASSERT(entry # NIL);
  3708. NEW(attribute, size, style);
  3709. attribute.font := WMGraphics.GetFont(name, size, style);
  3710. IF (entry.attributes = NIL) THEN
  3711. entry.attributes := attribute;
  3712. ELSIF (entry.attributes.size >= attribute.size) THEN
  3713. attribute.next := entry.attributes;
  3714. entry.attributes := attribute;
  3715. ELSE
  3716. a := entry.attributes;
  3717. WHILE (a.next # NIL) & (a.next.size < attribute.size) DO a := a.next; END;
  3718. attribute.next := a.next;
  3719. a.next := attribute;
  3720. END;
  3721. ASSERT(attribute.font # NIL);
  3722. RETURN attribute.font;
  3723. END Add;
  3724. (* Get specified font. If not available, the system default font is returned *)
  3725. PROCEDURE GetFont(CONST name : ARRAY OF CHAR; size : LONGINT; style : SET) : WMGraphics.Font;
  3726. VAR font : WMGraphics.Font;
  3727. BEGIN
  3728. font := Find(name, size, style);
  3729. IF (font = NIL) THEN
  3730. font := Add(name, size, style);
  3731. END;
  3732. ASSERT(font # NIL);
  3733. RETURN font;
  3734. END GetFont;
  3735. END FontCache;
  3736. VAR
  3737. manager : WMWindowManager.WindowManager;
  3738. cursorBlinker- : CursorBlinker;
  3739. PTVIsMultiLine, PTVIsPassword, PTVShowBorder, PTValwaysShowCursor, PTVShowLabels : WMProperties.BooleanProperty;
  3740. PTVAllowCommandExecution, PTVAllowTextSelection, PTVAllowPiemenu : WMProperties.BooleanProperty;
  3741. PTVWrapMode, PTVMouseWheelScrollSpeed, PVTtextAlignV : WMProperties.Int32Property;
  3742. PTVfirstLine, PTVleftShift, PTVPasswordChar : WMProperties.Int32Property;
  3743. PTVdefaultTextColor, PTVdefaultTextBgColor : WMProperties.ColorProperty;
  3744. PTVborders : WMProperties.RectangleProperty;
  3745. PTVonLinkClick, PTVonLinkClickInfo : Strings.String;
  3746. PTVonCtrlLinkClick, PTVonCtrlLinkClickInfo : Strings.String;
  3747. PTVShowLineNumbers, PTVIndicateTabs : WMProperties.BooleanProperty;
  3748. PTVHighlighting : WMProperties.StringProperty;
  3749. PTVLineNumberColor, PTVLineNumberBgColor, PTVclBgCurrentLine : WMProperties.ColorProperty;
  3750. currentTextView : TextView;
  3751. StrTextView : Strings.String;
  3752. DefaultStyle : POINTER TO RECORD END;
  3753. PROCEDURE Limit(x, min, max : LONGINT) : LONGINT;
  3754. BEGIN
  3755. IF x < min THEN x := min END;
  3756. IF x > max THEN x := max END;
  3757. RETURN x
  3758. END Limit;
  3759. PROCEDURE GetFontFromAttr(info : Texts.FontInfo) : WMGraphics.Font;
  3760. BEGIN
  3761. RETURN WMGraphics.GetFont(info.name, info.size, info.style);
  3762. END GetFontFromAttr;
  3763. PROCEDURE IsSameFont(f1, f2 : WMGraphics.Font) : BOOLEAN;
  3764. BEGIN
  3765. RETURN (f1 = f2) OR ((f1 # NIL) & (f2 # NIL) & (f1.size = f2.size) & (f1.style = f2.style) & (f1.name = f2.name));
  3766. END IsSameFont;
  3767. PROCEDURE CheckFont(style : SyntaxHighlighter.Style; font : WMGraphics.Font; VAR fontCache : FontCache);
  3768. VAR fontname : ARRAY 256 OF CHAR; fontsize : LONGINT; fontstyle : SET;
  3769. BEGIN
  3770. ASSERT(style # NIL);
  3771. IF (fontCache = NIL) THEN NEW(fontCache); END;
  3772. IF (style.defined * SyntaxHighlighter.FontMask = SyntaxHighlighter.FontMask) OR (font = NIL) THEN
  3773. COPY(style.attributes.fontInfo.name, fontname);
  3774. fontsize := style.attributes.fontInfo.size;
  3775. fontstyle := style.attributes.fontInfo.style;
  3776. ELSIF (font # NIL) THEN
  3777. IF (SyntaxHighlighter.FontName IN style.defined) THEN COPY(style.attributes.fontInfo.name, fontname); ELSE COPY(font.name, fontname); END;
  3778. IF (SyntaxHighlighter.FontSize IN style.defined) THEN fontsize := style.attributes.fontInfo.size; ELSE fontsize := font.size; END;
  3779. IF (SyntaxHighlighter.FontStyle IN style.defined) THEN fontstyle := style.attributes.fontInfo.style; ELSE fontstyle := font.style; END;
  3780. END;
  3781. IF (style.attributes.fontInfo.fontcache = NIL) OR ~IsSameFont(style.attributes.fontInfo.fontcache (WMGraphics.Font), font) THEN
  3782. style.attributes.fontInfo.fontcache := fontCache.GetFont(fontname, fontsize, fontstyle);
  3783. END;
  3784. ASSERT(style.attributes.fontInfo.fontcache # NIL);
  3785. END CheckFont;
  3786. PROCEDURE InitStrings;
  3787. BEGIN
  3788. StrTextView := Strings.NewString("TextView");
  3789. PTVonLinkClick := Strings.NewString("Link Click Event");
  3790. PTVonLinkClickInfo := Strings.NewString("fired when a link is pressed");
  3791. PTVonCtrlLinkClick := Strings.NewString("Ctrl Click Event");
  3792. PTVonCtrlLinkClickInfo := Strings.NewString("fired when Ctrl pressend and clicked");
  3793. END InitStrings;
  3794. PROCEDURE InitPrototypes;
  3795. BEGIN
  3796. NEW(PTVIsMultiLine, NIL, Strings.NewString("multiLine"), Strings.NewString("defines if more than one line is visible"));
  3797. PTVIsMultiLine.Set(TRUE);
  3798. NEW(PTVShowBorder, NIL, Strings.NewString("ShowBorder"), Strings.NewString("show border"));
  3799. PTVShowBorder.Set(FALSE);
  3800. NEW(PTVIsPassword, NIL, Strings.NewString("password"),
  3801. Strings.NewString("defines if the view is a password text. Characters are replaced by passwordChar"));
  3802. NEW(PTVPasswordChar, NIL, Strings.NewString("passwordChar"),
  3803. Strings.NewString("character that is the placeholder for a character in a password"));
  3804. PTVPasswordChar.Set(43);
  3805. NEW(PTValwaysShowCursor, NIL, Strings.NewString("alwaysShowCursor"),
  3806. Strings.NewString("set to true, if the cursor should not be hidden when focus is lost"));
  3807. PTValwaysShowCursor.Set(FALSE);
  3808. NEW(PTVShowLabels, NIL, Strings.NewString("ShowLabels"),
  3809. Strings.NewString("set to true, if the labels should be shown in the text"));
  3810. PTVShowLabels.Set(FALSE);
  3811. NEW(PTVMouseWheelScrollSpeed, NIL, Strings.NewString("MouseWheelScrollSpeed"),
  3812. Strings.NewString("Multiplier for mouse wheel, 0 to disable mouse wheel scrolling"));
  3813. PTVMouseWheelScrollSpeed.Set(3);
  3814. NEW(PTVAllowCommandExecution, NIL, Strings.NewString("allowCommandExecution"),
  3815. Strings.NewString("if set to true, middle-clicked words are executed as command"));
  3816. PTVAllowCommandExecution.Set(TRUE);
  3817. NEW(PTVAllowTextSelection, NIL, Strings.NewString("allowTextSelection"),
  3818. Strings.NewString("is the user allowed to select text using the mouse?"));
  3819. PTVAllowTextSelection.Set(TRUE);
  3820. NEW(PTVAllowPiemenu, NIL, Strings.NewString("allowPiemenu"),
  3821. Strings.NewString("if set to true, a mouse right-click opens the pie menu"));
  3822. PTVAllowPiemenu.Set(TRUE);
  3823. NEW(PTVWrapMode, NIL, Strings.NewString("wrapMode"), Strings.NewString("Set text wrapping mode"));
  3824. PTVWrapMode.Set(WrapWord);
  3825. NEW(PTVfirstLine, NIL, Strings.NewString("firstLine"),
  3826. Strings.NewString("the first visible line of text in the view"));
  3827. PTVfirstLine.Set(0);
  3828. NEW(PTVleftShift, NIL, Strings.NewString("leftShift"),
  3829. Strings.NewString("how many pixels the text in the view is shifted to the left"));
  3830. PTVleftShift.Set(0);
  3831. NEW(PTVdefaultTextColor, NIL, Strings.NewString("defaultTextColor"),
  3832. Strings.NewString("the color of a text that does not explicitly specify a color"));
  3833. PTVdefaultTextColor.Set(0FFH);
  3834. NEW(PTVdefaultTextBgColor, NIL, Strings.NewString("defaultTextBgColor"),
  3835. Strings.NewString("The color of a text background if not specified otherwise in the text"));
  3836. PTVdefaultTextBgColor.Set(0);
  3837. NEW(PTVborders, NIL, Strings.NewString("borders"),
  3838. Strings.NewString("spaces from bounds of the component to the text"));
  3839. PTVborders.Set(WMRectangles.MakeRect(5, 5, 5, 5));
  3840. NEW(PTVIndicateTabs, NIL, Strings.NewString("IndicateTabs"), Strings.NewString("Indicate tabs?"));
  3841. PTVIndicateTabs.Set(FALSE);
  3842. NEW(PTVShowLineNumbers, NIL, Strings.NewString("ShowLineNumbers"), Strings.NewString("Show line numbers?"));
  3843. PTVShowLineNumbers.Set(FALSE);
  3844. NEW(PTVLineNumberColor, NIL, Strings.NewString("LineNumberColor"), Strings.NewString("Color of line numbers"));
  3845. PTVLineNumberColor.Set(WMGraphics.Black);
  3846. NEW(PTVLineNumberBgColor, NIL, Strings.NewString("LineNumberBgColor"), Strings.NewString("Background color of line numbers"));
  3847. PTVLineNumberBgColor.Set(0CCCCCCFFH);
  3848. NEW(PTVHighlighting, NIL, Strings.NewString("Highlighting"), Strings.NewString("Name of highlighting to be applied"));
  3849. PTVHighlighting.Set(NIL);
  3850. NEW(PTVclBgCurrentLine, NIL, Strings.NewString("ClBgCurrentLine"), Strings.NewString("Background color of currently edited line"));
  3851. PTVclBgCurrentLine.Set(0);
  3852. NEW(PVTtextAlignV, NIL, Strings.NewString("TextAlignV"), Strings.NewString("vertical Text Alignment"));
  3853. PVTtextAlignV.Set(WMGraphics.AlignTop);
  3854. END InitPrototypes;
  3855. PROCEDURE EnablePiemenu*;
  3856. BEGIN
  3857. PTVAllowPiemenu.Set( TRUE );
  3858. KernelLog.String( "Piemenu enabled" ); KernelLog.Ln
  3859. END EnablePiemenu;
  3860. PROCEDURE DisablePiemenu*;
  3861. BEGIN
  3862. PTVAllowPiemenu.Set( FALSE );
  3863. KernelLog.String( "Piemenu disabled" ); KernelLog.Ln
  3864. END DisablePiemenu;
  3865. PROCEDURE TextViewFactory*() : XML.Element;
  3866. VAR e : TextView;
  3867. BEGIN
  3868. NEW(e); RETURN e
  3869. END TextViewFactory;
  3870. (* Inserts a ocharacter from the outside into the current textView's text at its current position *)
  3871. PROCEDURE InsertChar*(newChar : Char32) : INTEGER;
  3872. BEGIN
  3873. IF currentTextView # NIL THEN
  3874. RETURN currentTextView.InsertChar(newChar);
  3875. ELSE
  3876. RETURN -3;
  3877. END;
  3878. END InsertChar;
  3879. PROCEDURE Refresh*;
  3880. BEGIN
  3881. IF currentTextView # NIL THEN
  3882. currentTextView.layout.FullLayout(TRUE);
  3883. currentTextView.Invalidate;
  3884. currentTextView.CheckNumberOfLines;
  3885. END;
  3886. END Refresh;
  3887. PROCEDURE Cleanup;
  3888. BEGIN
  3889. cursorBlinker.Finalize;
  3890. END Cleanup;
  3891. PROCEDURE GenTextView*(): XML.Element;
  3892. VAR e : TextView;
  3893. BEGIN
  3894. NEW(e); RETURN e
  3895. END GenTextView;
  3896. BEGIN
  3897. NEW(cursorBlinker);
  3898. NEW(DefaultStyle);
  3899. Modules.InstallTermHandler(Cleanup);
  3900. InitStrings;
  3901. InitPrototypes;
  3902. manager := WMWindowManager.GetDefaultManager();
  3903. END WMTextView.