WMTextView.Mod 142 KB

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