WMTextView.Mod 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319
  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();
  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; leading := 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. eol := FALSE;
  1883. REPEAT
  1884. readerPosition := localTextReader.GetPosition();
  1885. localTextReader.ReadCh(ch);
  1886. IF (highlighter # NIL) THEN
  1887. ASSERT(state # NIL);
  1888. IF (lastEnd < readerPosition) THEN
  1889. highlighterStyle := highlighter.GetRegionStyle(readerPosition, state, regionStart, regionEnd);
  1890. IF (highlighterStyle # NIL) THEN
  1891. lastEnd := regionEnd;
  1892. ELSE
  1893. IF (ch > 32) THEN
  1894. highlighterStyle := highlighter.GetWordStyle(localTextReader, readerPosition, lastEnd);
  1895. END;
  1896. END;
  1897. localTextReader.SetPosition(readerPosition);
  1898. localTextReader.ReadCh(ch); (* restore text reader state *)
  1899. END;
  1900. IF (highlighterStyle = NIL) THEN
  1901. highlighterStyle := highlighter.GetDefaultStyle();
  1902. END;
  1903. END;
  1904. (* Get the Paragraph Style *)
  1905. IF localTextReader.pstyle # NIL THEN
  1906. pStyle := localTextReader.pstyle;
  1907. (* pStyle := Texts.GetParagraphStyleByName(pStyle.name); *)
  1908. spaceBefore := ENTIER(FP1616.FixpToFloat(pStyle.spaceBefore));
  1909. spaceAfter := ENTIER(FP1616.FixpToFloat(pStyle.spaceAfter));
  1910. firstIndent := ENTIER(FP1616.FixpToFloat(pStyle.firstIndent));
  1911. leftIndent := ENTIER(FP1616.FixpToFloat(pStyle.leftIndent));
  1912. rightIndent := ENTIER(FP1616.FixpToFloat(pStyle.rightIndent));
  1913. align := pStyle.alignment;
  1914. (* parse tabstops *)
  1915. COPY(pStyle.tabStops, tabstring);
  1916. IF (tabstring # "default") & (tabstring # "0") & (tabstring # "") THEN
  1917. NEW(sr, LEN(tabstring)); sr.Set(tabstring); tabCounter := 0;
  1918. WHILE (sr.res = Streams.Ok) DO
  1919. sr.SkipWhitespace; sr.String(token);
  1920. INC(tabCounter);
  1921. END;
  1922. NEW(tp, tabCounter);
  1923. sr.Reset; tabCounter := 0;
  1924. WHILE (sr.res = Streams.Ok) DO
  1925. sr.SkipWhitespace; sr.String(token);
  1926. Strings.StrToInt(token, tabPos);
  1927. tp[tabCounter] := tabPos;
  1928. INC(tabCounter);
  1929. END;
  1930. NEW(tabs, tp);
  1931. IF l.tabStops = NIL THEN l.tabStops := tabs END
  1932. END;
  1933. END;
  1934. IF (highlighterStyle = NIL) OR (highlighterStyle.defined * SyntaxHighlighter.DefineMask # SyntaxHighlighter.DefineMask) THEN
  1935. IF localTextReader.cstyle # NIL THEN
  1936. IF (currentStyle # localTextReader.cstyle) THEN
  1937. currentStyle := localTextReader.cstyle;
  1938. style.voff := ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.baselineShift));
  1939. ld := ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.leading));
  1940. IF (localTextReader.cstyle.fontcache #NIL) & (localTextReader.cstyle.fontcache IS WMGraphics.Font) THEN
  1941. style.font := localTextReader.cstyle.fontcache(WMGraphics.Font);
  1942. ELSE
  1943. style.font := WMGraphics.GetFont(localTextReader.cstyle.family, ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.size)), localTextReader.cstyle.style);
  1944. localTextReader.cstyle.fontcache := style.font;
  1945. END;
  1946. END;
  1947. ELSIF localTextReader.pstyle # NIL THEN
  1948. IF pStyle.charStyle # NIL THEN
  1949. style.voff := ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.baselineShift));
  1950. ld := ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.leading));
  1951. IF (localTextReader.cstyle.fontcache #NIL) & (localTextReader.cstyle.fontcache IS WMGraphics.Font) THEN
  1952. style.font := localTextReader.cstyle.fontcache(WMGraphics.Font);
  1953. ELSE
  1954. style.font := WMGraphics.GetFont(localTextReader.cstyle.family, ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.size)), localTextReader.cstyle.style);
  1955. localTextReader.pstyle.charStyle.fontcache := style.font
  1956. END
  1957. END;
  1958. ELSIF localTextReader.attributes # NIL THEN
  1959. IF (currentStyle # localTextReader.attributes) THEN
  1960. currentStyle := localTextReader.attributes;
  1961. style.voff := localTextReader.attributes.voff;
  1962. ld := 0;
  1963. IF localTextReader.attributes.fontInfo # NIL THEN
  1964. IF (localTextReader.attributes.fontInfo.fontcache # NIL) & (localTextReader.attributes.fontInfo.fontcache IS WMGraphics.Font) THEN
  1965. style.font := localTextReader.attributes.fontInfo.fontcache(WMGraphics.Font);
  1966. ELSE
  1967. style.font := GetFontFromAttr(localTextReader.attributes.fontInfo);
  1968. localTextReader.attributes.fontInfo.fontcache := style.font;
  1969. END
  1970. ELSE
  1971. style.font := cf
  1972. END
  1973. END;
  1974. ELSE
  1975. IF (currentStyle # DefaultStyle) THEN
  1976. currentStyle := DefaultStyle;
  1977. style.voff := 0;
  1978. style.font := cf;
  1979. ld := 0;
  1980. END;
  1981. END;
  1982. ASSERT(style.font # NIL);
  1983. END;
  1984. IF (highlighterStyle # NIL) THEN
  1985. IF (highlighterStyle # lastHighlighterStyle) OR (currentStyle # lastStyle) THEN
  1986. IF SyntaxHighlighter.Voff IN highlighterStyle.defined THEN style.voff := highlighterStyle.attributes.voff; END;
  1987. IF (SyntaxHighlighter.FontMask * highlighterStyle.defined # {}) THEN
  1988. CheckFont(highlighterStyle, style.font, fontCache);
  1989. style.font := highlighterStyle.attributes.fontInfo.fontcache (WMGraphics.Font);
  1990. END;
  1991. END;
  1992. currentStyle := NIL;
  1993. END;
  1994. lastStyle := currentStyle;
  1995. lastHighlighterStyle := highlighterStyle;
  1996. IF first THEN
  1997. IF (ch = Texts.NewLineChar) OR (ch = 0) THEN
  1998. ascent := style.font.GetAscent(); descent := style.font.GetDescent();
  1999. ELSE
  2000. descent := 0; ascent := 0;
  2001. END;
  2002. IF start THEN wrapwidth := wrapwidth - firstIndent - rightIndent;
  2003. ELSE wrapwidth := wrapwidth - leftIndent - rightIndent;
  2004. END;
  2005. first := FALSE;
  2006. END;
  2007. INC(pos);
  2008. IF (stopPos < 0) OR (pos <= stopPos) THEN
  2009. IF (ch # Texts.NewLineChar) & (ch # 0) THEN
  2010. GetExtents(ch, dx, a, d); ascent := Strings.Max(ascent, a); descent := Strings.Max(descent, d);
  2011. IF ld = 0 THEN ld := ascent + descent; ELSE ld := Strings.Max(ld, ascent + descent); END; leading := Strings.Max(leading, ld);
  2012. IF isMultiLineI & (wrapModeI # NoWrap) & (i > 0) & (x0 + x + dx > wrapwidth) THEN
  2013. eol := TRUE; DEC(pos); wrapPos := pos;
  2014. (* Go left for last space *)
  2015. IF wrapModeI = WrapWord THEN
  2016. pos := TextUtilities.FindPosWordLeft(localTextReader, pos);
  2017. IF pos <= l.pos THEN pos := wrapPos (* no word break found. wrap at latest possible pos *)
  2018. ELSE (* decrease width to actual size.. *)
  2019. (* localTextReader.SetPosition(pos);
  2020. WHILE pos < wrapPos DO
  2021. localTextReader.ReadCh(ch); GetExtents(ch, dx, a, d); x := x - dx; INC(pos)
  2022. END
  2023. *) END
  2024. END
  2025. ELSE
  2026. IF (stopXPos >= 0) & (x + dx DIV 2 > stopXPos) THEN
  2027. DEC(pos);
  2028. (* the bidi formatted text's lock needs to be released explicitly *)
  2029. IF (bidiTextReader # NIL) THEN
  2030. localTextReader.text.ReleaseRead;
  2031. END;
  2032. RETURN
  2033. END;
  2034. INC(x, dx)
  2035. END;
  2036. ELSE
  2037. eol := TRUE;
  2038. stop := TRUE;
  2039. IF (stopXPos >= 0) THEN DEC(pos) END;
  2040. END;
  2041. ELSE
  2042. eol := TRUE
  2043. END;
  2044. INC(i);
  2045. UNTIL eol OR localTextReader.eot;
  2046. l.width := x;
  2047. l.ascent := ascent; l.height := leading; (* ascent + descent; *)
  2048. l.align := align; l.leftIndent := leftIndent; l.rightIndent := rightIndent;
  2049. IF l.height = 0 THEN l.height := style.font.GetHeight() END;
  2050. IF start THEN l.firstInParagraph := TRUE; l.firstIndent := firstIndent; l.spaceBefore := spaceBefore;
  2051. ELSE l.firstInParagraph := FALSE; END;
  2052. IF stop THEN l.lastInParagraph := TRUE; l.spaceAfter := spaceAfter;
  2053. ELSE l.lastInParagraph := FALSE END;
  2054. (* the bidi formatted text's lock needs to be released explicitly *)
  2055. IF (bidiTextReader # NIL) THEN
  2056. localTextReader.text.ReleaseRead;
  2057. END;
  2058. END LayoutLine;
  2059. (* llen = -1 to render until the end of line > 0 to render llen elements in the line *)
  2060. PROCEDURE RenderLine*(canvas : WMGraphics.Canvas; VAR l : LineInfo; linenr, top, llen : LONGINT);
  2061. VAR sx, dx, dy, x, sp, i, j, k, t, tx, linelength, w, p : LONGINT; char : Char32; gs: WMGraphics.GlyphSpacings;
  2062. font : WMGraphics.Font;
  2063. vc : WMComponents.VisualComponent;
  2064. hc : BOOLEAN;
  2065. bidiTextReader, localTextReader : Texts.TextReader;
  2066. cursorPosition : LONGINT;
  2067. regionStart, regionEnd, lastEnd: LONGINT;
  2068. readerPosition : LONGINT;
  2069. lineNumberString : ARRAY 16 OF CHAR;
  2070. canvasState : WMGraphics.CanvasState;
  2071. cliprect, temp : WMRectangles.Rectangle;
  2072. highlighterStyle, lastHighlighterStyle : SyntaxHighlighter.Style;
  2073. currentStyle, lastStyle : ANY;
  2074. lastColor : LONGINT;
  2075. cf: WMGraphics.Font;
  2076. style : RECORD
  2077. color, bgColor, voff : LONGINT;
  2078. font : WMGraphics.Font;
  2079. END;
  2080. BEGIN
  2081. IF TraceRenderOptimize IN Trace THEN
  2082. KernelLog.String("RenderLine : "); KernelLog.Int(linenr, 5); KernelLog.String(" from position : ");
  2083. KernelLog.Int(layout.GetLineStartPos(linenr), 5); KernelLog.Ln;
  2084. END;
  2085. sp := l.pos;
  2086. IF sp >= text.GetLength() THEN RETURN END;
  2087. style.color := defaultTextColorI;
  2088. canvas.SetColor(style.color); lastColor := style.color;
  2089. style.bgColor := defaultTextBgColorI;
  2090. style.voff := 0;
  2091. cf := GetFont();
  2092. style.font := cf;
  2093. IF llen < 0 THEN
  2094. linelength := layout.GetLineLength(linenr);
  2095. (* hack for the bidi formatter *)
  2096. IF linenr = layout.GetNofLines() - 1 THEN
  2097. DEC(linelength);
  2098. END;
  2099. ELSE
  2100. linelength := llen
  2101. END;
  2102. (* if there is a bidi formatter, reorder the current line *)
  2103. IF text.isUTF & (layout.bidiFormatter # NIL) THEN
  2104. bidiTextReader := layout.bidiFormatter.ReorderLine(sp,linelength);
  2105. END;
  2106. (* the bidi text reader needs special treatment for the initialization *)
  2107. IF (bidiTextReader # NIL) THEN
  2108. (* after reordering the line, contextual dependency rules are applied *)
  2109. bidiTextReader := ContextualDependency.AnalyzeLine(bidiTextReader,-1,-1);
  2110. layout.bidiFormatter.SetReadyTextReader(sp,bidiTextReader);
  2111. bidiTextReader.CloneProperties(utilreader);
  2112. localTextReader := bidiTextReader;
  2113. localTextReader.text.AcquireRead;
  2114. localTextReader.SetPosition(0);
  2115. ELSE
  2116. (* revert the hack for the bidi formatter *)
  2117. IF (llen < 0) & (linenr = layout.GetNofLines() - 1) THEN
  2118. INC(linelength);
  2119. END;
  2120. localTextReader := utilreader;
  2121. localTextReader.text.AcquireRead;
  2122. localTextReader.SetPosition(sp);
  2123. END;
  2124. i := 0;
  2125. x := GetLineLeftIndent(linenr);
  2126. sx := - leftShiftI + bordersI.l + x0;
  2127. IF TraceBaseLine IN Trace THEN
  2128. canvas.Line(0, top + (l.ascent), bounds.GetWidth(), top + (l.ascent), 01F0000FFH, WMGraphics.ModeCopy)
  2129. END;
  2130. selection.Sort;
  2131. IF (cursor.visible) & (selection.b - selection.a <= 0) & (clBgCurrentLineI # 0) THEN
  2132. cursorPosition := cursor.GetPosition();
  2133. IF (l.pos <= cursorPosition) & (cursorPosition < l.pos + linelength) THEN
  2134. canvas.Fill(WMRectangles.MakeRect(0, top, bounds.GetWidth() - bordersI.r, top + l.height), clBgCurrentLineI, WMGraphics.ModeSrcOverDst);
  2135. END;
  2136. END;
  2137. IF showLineNumbersI THEN
  2138. canvas.SaveState(canvasState);
  2139. Strings.IntToStr(linenr + 1, lineNumberString);
  2140. temp := WMRectangles.MakeRect(bordersI.l, top, x0 - 1, top + l.height);
  2141. IF (lineNumberBgColorI # 0) THEN
  2142. canvas.Fill(temp, lineNumberBgColorI, WMGraphics.ModeSrcOverDst);
  2143. END;
  2144. temp.r := temp.r - 4;
  2145. IF ((linenr + 1) MOD 10 = 0) THEN
  2146. canvas.SetFont(lineNumberFont10);
  2147. ELSE
  2148. canvas.SetFont(lineNumberFont);
  2149. END;
  2150. canvas.SetColor(lineNumberColorI);
  2151. WMGraphics.DrawStringInRect(canvas, temp, FALSE, WMGraphics.AlignRight, WMGraphics.AlignCenter, lineNumberString);
  2152. canvas.RestoreState(canvasState); (* restore font and font color *)
  2153. canvas.SaveState(canvasState);
  2154. canvas.GetClipRect(cliprect);
  2155. cliprect.l := x0;
  2156. canvas.SetClipRect(cliprect);
  2157. END;
  2158. w := bounds.GetWidth() - bordersI.r;
  2159. localTextReader.SetDirection(1);
  2160. lastEnd := -1;
  2161. highlighterStyle := NIL; lastHighlighterStyle := NIL;
  2162. currentStyle := DefaultStyle; lastStyle := NIL;
  2163. REPEAT
  2164. readerPosition := localTextReader.GetPosition();
  2165. localTextReader.ReadCh(char);
  2166. IF (highlighter # NIL) THEN
  2167. ASSERT(state # NIL);
  2168. IF (lastEnd < readerPosition) THEN
  2169. highlighterStyle := highlighter.GetRegionStyle(readerPosition, state, regionStart, regionEnd);
  2170. IF (highlighterStyle # NIL) THEN
  2171. lastEnd := regionEnd;
  2172. ELSE
  2173. IF (char > 32) THEN
  2174. highlighterStyle := highlighter.GetWordStyle(localTextReader, readerPosition, lastEnd);
  2175. END;
  2176. END;
  2177. localTextReader.SetPosition(readerPosition);
  2178. localTextReader.ReadCh(char); (* restore text reader state *)
  2179. END;
  2180. IF (highlighterStyle = NIL) THEN
  2181. highlighterStyle := highlighter.GetDefaultStyle();
  2182. END;
  2183. END;
  2184. IF (highlighterStyle = NIL) OR (highlighterStyle.defined * SyntaxHighlighter.DefineMask # SyntaxHighlighter.DefineMask) THEN
  2185. IF (localTextReader.cstyle # NIL) THEN
  2186. IF (currentStyle # localTextReader.cstyle) THEN
  2187. currentStyle := localTextReader.cstyle;
  2188. style.color := localTextReader.cstyle.color;
  2189. style.bgColor := localTextReader.cstyle.bgColor;
  2190. style.voff := localTextReader.cstyle.baselineShift;
  2191. IF (localTextReader.cstyle.fontcache # NIL) & (localTextReader.cstyle.fontcache IS WMGraphics.Font) THEN
  2192. style.font := localTextReader.cstyle.fontcache(WMGraphics.Font);
  2193. ELSE
  2194. style.font := WMGraphics.GetFont(localTextReader.cstyle.family, ENTIER(FP1616.FixpToFloat(localTextReader.cstyle.size)), localTextReader.cstyle.style);
  2195. localTextReader.cstyle.fontcache := style.font;
  2196. END;
  2197. END;
  2198. ELSIF (localTextReader.attributes # NIL) THEN
  2199. IF (currentStyle # localTextReader.attributes) THEN
  2200. currentStyle := localTextReader.attributes;
  2201. style.color := localTextReader.attributes.color;
  2202. style.bgColor := localTextReader.attributes.bgcolor;
  2203. style.voff := localTextReader.attributes.voff;
  2204. IF (localTextReader.attributes.fontInfo # NIL) THEN
  2205. IF (localTextReader.attributes.fontInfo.fontcache # NIL) & (localTextReader.attributes.fontInfo.fontcache IS WMGraphics.Font) THEN
  2206. style.font := localTextReader.attributes.fontInfo.fontcache (WMGraphics.Font);
  2207. ELSE
  2208. style.font := GetFontFromAttr(localTextReader.attributes.fontInfo);
  2209. localTextReader.attributes.fontInfo.fontcache := style.font;
  2210. END;
  2211. ELSE
  2212. style.font := cf;
  2213. END;
  2214. END;
  2215. ELSE
  2216. IF (currentStyle # DefaultStyle) THEN
  2217. currentStyle := DefaultStyle;
  2218. style.color := defaultTextColorI;
  2219. style.bgColor := defaultTextBgColorI;
  2220. style.voff := 0;
  2221. style.font := cf;
  2222. END;
  2223. END;
  2224. ASSERT(style.font # NIL);
  2225. END;
  2226. IF (highlighterStyle # NIL) THEN
  2227. IF (highlighterStyle # lastHighlighterStyle) OR (currentStyle # lastStyle) THEN
  2228. IF SyntaxHighlighter.Voff IN highlighterStyle.defined THEN style.voff := highlighterStyle.attributes.voff; END;
  2229. IF SyntaxHighlighter.Color IN highlighterStyle.defined THEN style.color := highlighterStyle.attributes.color; END;
  2230. IF SyntaxHighlighter.BgColor IN highlighterStyle.defined THEN style.bgColor := highlighterStyle.attributes.bgcolor; END;
  2231. IF (SyntaxHighlighter.FontMask * highlighterStyle.defined # {}) THEN
  2232. CheckFont(highlighterStyle, style.font, fontCache);
  2233. style.font := highlighterStyle.attributes.fontInfo.fontcache (WMGraphics.Font);
  2234. END;
  2235. END;
  2236. currentStyle := NIL; (* force reevaluation of localTextReader style *)
  2237. END;
  2238. lastStyle := currentStyle;
  2239. lastHighlighterStyle := highlighterStyle;
  2240. IF (style.color # lastColor) THEN canvas.SetColor(style.color); lastColor := style.color; END;
  2241. IF char = Texts.ObjectChar THEN
  2242. IF (localTextReader.object # NIL) & (localTextReader.object IS WMGraphics.Image) THEN
  2243. canvas.DrawImage(x, top + (l.ascent) + style.voff - localTextReader.object(WMGraphics.Image).height, localTextReader.object(WMGraphics.Image),
  2244. WMGraphics.ModeSrcOverDst);
  2245. dx := localTextReader.object(WMGraphics.Image).width
  2246. ELSIF (localTextReader.object # NIL) & (localTextReader.object IS WMComponents.VisualComponent) THEN
  2247. vc := localTextReader.object(WMComponents.VisualComponent);
  2248. dx := vc.bounds.GetWidth();
  2249. dy := vc.bounds.GetHeight();
  2250. canvas.SaveState(clipState); (* save the current clip-state *)
  2251. canvas.SetClipRect(WMRectangles.MakeRect(x + sx, top + (l.ascent - dy), x + dx + sx, top + (l.height)));
  2252. canvas.ClipRectAsNewLimits(x + sx, top + (l.ascent - dy));
  2253. (* assuming the component will not delay --> otherwise a buffer is needed *)
  2254. vc.Acquire; vc.Draw(canvas); vc.Release;
  2255. canvas.RestoreState(clipState)
  2256. END
  2257. ELSIF char = 0 THEN (* EOT *)
  2258. ELSIF char = Texts.TabChar THEN
  2259. tx := x;
  2260. IF l.firstInParagraph THEN tx := tx - l.firstIndent
  2261. ELSE tx := tx - l.leftIndent END;
  2262. IF l.tabStops # NIL THEN dx := l.tabStops.GetNextTabStop(tx) - tx
  2263. ELSE dx := defaultTabStops.GetNextTabStop(tx) - tx
  2264. END;
  2265. IF style.bgColor # 0 THEN
  2266. canvas.Fill(WMRectangles.MakeRect(x + sx, top, x + dx + sx, top + (l.height)), style.bgColor, WMGraphics.ModeSrcOverDst)
  2267. END;
  2268. IF indicateTabsI THEN canvas.SetPixel(x + sx + ((dx + 1) DIV 2), top + ((l.ascent + 1) DIV 2), WMGraphics.Blue, WMGraphics.ModeCopy); END;
  2269. ELSIF char = Texts.LabelChar THEN
  2270. IF showLabels.Get() THEN
  2271. font := cf;
  2272. font.GetStringSize(localTextReader.object(Texts.LabelPiece).label^, dx, dy);
  2273. font.RenderString(canvas, x + sx+2, top + (l.ascent), localTextReader.object(Texts.LabelPiece).label^);
  2274. INC(dx, 4);
  2275. canvas.Fill(WMRectangles.MakeRect(x + sx, top, x + dx + sx, top + (l.height)), LONGINT(0FF880050H), WMGraphics.ModeSrcOverDst);
  2276. WMGraphicUtilities.RectGlassShade(canvas, WMRectangles.MakeRect(x + sx, top, x + dx + sx, top + (l.height)), 1, FALSE)
  2277. ELSE dx := 0; END;
  2278. ELSE
  2279. IF char = Texts.NewLineChar THEN
  2280. localTextReader.text.ReleaseRead;
  2281. IF showLineNumbersI THEN canvas.RestoreState(canvasState); END;
  2282. RETURN
  2283. END;
  2284. IF isPasswordI THEN
  2285. char := passwordChar.Get()
  2286. END;
  2287. (* If the text is utf-formatted get the display version of the character.
  2288. Note, that only some special invisible characters differ from their actual representation. *)
  2289. IF text.isUTF THEN
  2290. UnicodeBidirectionality.GetDisplayCharacter(char);
  2291. END;
  2292. hc := style.font.HasChar(char);
  2293. IF hc THEN style.font.GetGlyphSpacings(char, gs)
  2294. ELSE WMGraphics.FBGetGlyphSpacings(char, gs)
  2295. END;
  2296. dx := gs.bearing.l + gs.width + gs.bearing.r;
  2297. IF style.bgColor MOD 256 # 0 THEN
  2298. canvas.Fill(WMRectangles.MakeRect(x + sx, top, x + dx + sx, top + (l.height)), style.bgColor, WMGraphics.ModeCopy)
  2299. END;
  2300. IF hc THEN style.font.RenderChar(canvas, x + sx, top + (l.ascent) + style.voff, char)
  2301. ELSE WMGraphics.FBRenderChar(canvas, x + sx, top + (l.ascent) + style.voff, char)
  2302. END
  2303. END;
  2304. (* link *)
  2305. IF localTextReader.link # NIL THEN
  2306. canvas.Line(x + sx, top + (l.ascent)+1, x + dx + sx, top + (l.ascent)+1, canvas.color, WMGraphics.ModeSrcOverDst);
  2307. END;
  2308. (* highlight - since highlights store the global text position, the line's starting position needs to be added,
  2309. when operating on the local, bidirectional text reader. *)
  2310. IF bidiTextReader # NIL THEN
  2311. p := GetInternalPos(localTextReader.GetPosition()+sp-1);
  2312. ELSE
  2313. p := localTextReader.GetPosition() - 1;
  2314. END;
  2315. FOR j := 0 TO nofHighlights - 1 DO
  2316. IF (p >= highlights[j].a) & (p < highlights[j].b) THEN
  2317. CASE highlights[j].kind OF
  2318. |HLOver: canvas.Fill(WMGraphics.MakeRectangle(x + sx, top, x + dx + sx, top + (l.height)), highlights[j].color, WMGraphics.ModeSrcOverDst)
  2319. |HLUnder: canvas.Line(x + sx, top + (l.ascent), x + dx + sx, top + (l.ascent), highlights[j].color, WMGraphics.ModeSrcOverDst);
  2320. |HLWave:
  2321. FOR k := 0 TO dx - 1 DO
  2322. t := 1 - ABS((x + k) MOD 4 - 2); (* because of compiler bug on intel *)
  2323. canvas.SetPixel(x + k + sx, top + l.ascent + t, highlights[j].color, WMGraphics.ModeSrcOverDst);
  2324. END;
  2325. ELSE
  2326. END
  2327. END
  2328. END;
  2329. x := x + dx;
  2330. INC(i)
  2331. UNTIL (i >= linelength) OR localTextReader.eot OR (x + sx > w);
  2332. localTextReader.text.ReleaseRead;
  2333. IF showLineNumbersI THEN canvas.RestoreState(canvasState); END;
  2334. END RenderLine;
  2335. PROCEDURE RenderAboveTextMarkers*(canvas : WMGraphics.Canvas);
  2336. VAR x, y, l, pos, i, ascent : LONGINT;
  2337. BEGIN
  2338. AssertLock;
  2339. IF text = NIL THEN RETURN END;
  2340. IF optimize THEN RETURN END;
  2341. text.AcquireRead;
  2342. FOR i := nofPositionMarkers - 1 TO 0 BY -1 DO
  2343. pos := positionMarkers[i].pos.GetPosition();
  2344. l := layout.FindLineNrByPos(pos);
  2345. IF FindScreenPos(pos, x, y) THEN
  2346. IF (l >= 0) & (l < layout.GetNofLines()) THEN
  2347. ascent := layout.lines[l].ascent;
  2348. (* IF ascent = 0 THEN ascent := layout.lines[l].height END;
  2349. IF ascent = 0 THEN ascent := 10 END; *)
  2350. ELSE ascent := 10 END;
  2351. positionMarkers[i].Draw(canvas, x, y, ascent)
  2352. END
  2353. END;
  2354. text.ReleaseRead;
  2355. END RenderAboveTextMarkers;
  2356. PROCEDURE DrawBackground*(canvas : WMGraphics.Canvas);
  2357. VAR la, lb, i, top, t, b : LONGINT; rect, clip : WMRectangles.Rectangle; cstate : WMGraphics.CanvasState;
  2358. BEGIN
  2359. ASSERT(layout # NIL);
  2360. DrawBackground^(canvas);
  2361. canvas.GetClipRect(clip);
  2362. IF WMRectangles.RectEmpty(clip) THEN RETURN END;
  2363. rect := GetClientRect();
  2364. canvas.SaveState(cstate);
  2365. IF showBorderI THEN
  2366. WMGraphicUtilities.DrawBevel(canvas, rect,
  2367. 1, TRUE, LONGINT(0808080FFH), WMGraphics.ModeCopy)
  2368. END;
  2369. (* allow clean clipping in at inner border *)
  2370. WMRectangles.ClipRect(rect, borderClip);
  2371. WMRectangles.ClipRect(clip, borderClip);
  2372. canvas.SetClipRect(clip);
  2373. (* draw gutter *)
  2374. rect.r := x0 - 1;
  2375. IF showLineNumbersI & (lineNumberBgColorI # 0) & WMRectangles.Intersect(rect, clip) THEN
  2376. canvas.Fill(rect, lineNumberBgColorI, WMGraphics.ModeSrcOverDst);
  2377. END;
  2378. text.AcquireRead;
  2379. la := FindLineByY(firstLineI, clip.t);
  2380. lb := FindLineByY(firstLineI, clip.b);
  2381. (* prepare selections *)
  2382. FOR i := 0 TO nofHighlights - 1 DO
  2383. highlights[i].a := highlights[i].from.GetPosition();
  2384. highlights[i].b := highlights[i].to.GetPosition();
  2385. IF highlights[i].a > highlights[i].b THEN t := highlights[i].a; highlights[i].a := highlights[i].b; highlights[i].b := t END
  2386. END;
  2387. top := borderClip.t;
  2388. IF (la = lb) & (textAlignV.Get() = WMGraphics.AlignCenter) THEN
  2389. top := (borderClip.t+borderClip.b-layout.lines[la].height) DIV 2;
  2390. (* something is wrong with ascent and height here, does not comply with the notions in fonts *)
  2391. END;
  2392. FOR i := firstLineI TO la - 1 DO
  2393. top := top + (layout.lines[i].height);
  2394. CheckParagraphBegin(i, top);
  2395. CheckParagraphEnd(i, top);
  2396. END;
  2397. IF la >= 0 THEN
  2398. (* draw the lines that intersect the clipping rectangle *)
  2399. FOR i := la TO lb DO
  2400. CheckParagraphBegin(i, top);
  2401. RenderLine(canvas, layout.lines[i], i, top, -1);
  2402. top := top + (layout.lines[i].height);
  2403. CheckParagraphEnd(i, top);
  2404. END
  2405. END;
  2406. RenderAboveTextMarkers(canvas);
  2407. text.ReleaseRead;
  2408. canvas.RestoreState(cstate);
  2409. END DrawBackground;
  2410. PROCEDURE StoreLineEnter;
  2411. VAR pos, cl : LONGINT;
  2412. BEGIN
  2413. pos := cursor.GetPosition();
  2414. cl := layout.FindLineNrByPos(pos);
  2415. lineEnter := pos - layout.GetLineStartPos(cl)
  2416. END StoreLineEnter;
  2417. (* navigation *)
  2418. PROCEDURE WheelMove*(dz: LONGINT); (** PROTECTED *)
  2419. BEGIN
  2420. IF mouseWheelScrollSpeedI # 0 THEN
  2421. firstLine.Set(firstLine.Get() + mouseWheelScrollSpeedI * dz)
  2422. END;
  2423. END WheelMove;
  2424. (* abort a possible start of a command. Clear the command start indicator, if it was set *)
  2425. PROCEDURE AbortStart;
  2426. BEGIN
  2427. ASSERT(IsCallFromSequencer());
  2428. IF commandMarker # NIL THEN
  2429. RemoveHighlight(commandMarker);
  2430. commandMarker := NIL
  2431. END;
  2432. canStart := FALSE
  2433. END AbortStart;
  2434. (*
  2435. Handle double-click at text position <pos>.
  2436. Select the double-clicked word, whitespace or line.
  2437. Some explanations:
  2438. Why utilreader.GetPosition()+2 when searching to the left?
  2439. After we read the last character that should be included, the position of the reader is decremented.
  2440. When we now read the next character and see that it should not be included, the reader is decremented again.
  2441. -> The last character to be included was found at position utilreader.GetPosition()+2 (except when we reach EOT)
  2442. 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.
  2443. That's why utilreader.GetPosition()-1 is used instead of utilreader.GetPosition()-2.
  2444. *)
  2445. PROCEDURE DoubleClickSelect(pos : LONGINT);
  2446. CONST
  2447. LineFeed = 0AH;
  2448. Underscore = 05FH;
  2449. VAR
  2450. char : Texts.Char32;
  2451. from, to : LONGINT;
  2452. BEGIN
  2453. ASSERT(text.HasReadLock());
  2454. utilreader.SetPosition(pos);
  2455. utilreader.SetDirection(1);
  2456. utilreader.ReadCh(char);
  2457. IF (char = LineFeed) OR utilreader.eot THEN (* select line *)
  2458. IF utilreader.eot THEN to := pos;
  2459. ELSE to := pos+1;
  2460. END;
  2461. from := TextUtilities.FindPosLineStart(utilreader, pos);
  2462. ELSIF TextUtilities.IsWhiteSpace(char,text.isUTF) THEN
  2463. WHILE ~utilreader.eot & TextUtilities.IsWhiteSpace(char,text.isUTF) & (char # LineFeed) DO utilreader.ReadCh(char); END;
  2464. IF utilreader.eot THEN to := utilreader.text.GetLength();
  2465. ELSE to := utilreader.GetPosition()-1;
  2466. END;
  2467. utilreader.SetPosition(pos);
  2468. utilreader.SetDirection(-1);
  2469. utilreader.ReadCh(char);
  2470. WHILE ~utilreader.eot & TextUtilities.IsWhiteSpace(char,text.isUTF) & (char # LineFeed) DO utilreader.ReadCh(char); END;
  2471. IF utilreader.eot THEN from := 0;
  2472. ELSE from := utilreader.GetPosition()+2;
  2473. END;
  2474. ELSIF TextUtilities.IsAlphaNum(char) OR (char = Underscore) THEN (* select word *)
  2475. WHILE ~utilreader.eot & (TextUtilities.IsAlphaNum(char) OR (char = Underscore)) DO utilreader.ReadCh(char); END;
  2476. IF utilreader.eot THEN to := utilreader.text.GetLength();
  2477. ELSE to := utilreader.GetPosition()-1;
  2478. END;
  2479. utilreader.SetPosition(pos);
  2480. utilreader.SetDirection(-1);
  2481. utilreader.ReadCh(char);
  2482. WHILE ~utilreader.eot & (TextUtilities.IsAlphaNum(char) OR (char = Underscore)) DO utilreader.ReadCh(char); END;
  2483. IF utilreader.eot THEN from := 0;
  2484. ELSE from := utilreader.GetPosition()+2;
  2485. END;
  2486. ELSE (* select the character at text position pos *)
  2487. from := pos; to := pos+1;
  2488. END;
  2489. selection.SetFromTo(from, to);
  2490. cursor.SetVisible(to - from > 0);
  2491. END DoubleClickSelect;
  2492. PROCEDURE SetInterclick(new : LONGINT);
  2493. VAR old : LONGINT;
  2494. BEGIN
  2495. old := interclick;
  2496. IF (old # new) THEN
  2497. interclick := new;
  2498. CASE new OF
  2499. | Interclick01: selection.SetColor(SelectionColorInterclick01);
  2500. | Interclick02: selection.SetColor(SelectionColorInterclick02);
  2501. ELSE
  2502. selection.SetColor(SelectionColor);
  2503. END;
  2504. END;
  2505. END SetInterclick;
  2506. PROCEDURE PointerDown*(x, y : LONGINT; keys : SET);
  2507. VAR pos, a, b, internalPos : LONGINT; oldInterclick : LONGINT;
  2508. BEGIN
  2509. ViewToTextPos(x,y,pos);
  2510. internalPos := GetInternalPos(pos);
  2511. oldInterclick := interclick;
  2512. IF (keys * {0, 1} = {0,1}) THEN SetInterclick(Interclick01);
  2513. ELSIF (keys * {0,2} = {0,2}) THEN SetInterclick(Interclick02);
  2514. ELSE SetInterclick(InterclickNone);
  2515. END;
  2516. (* Determine whether to cancel an interclick if any *)
  2517. IF (oldInterclick = InterclickCancelled) OR
  2518. ((oldInterclick # InterclickNone) & (interclick # InterclickNone)) THEN
  2519. SetInterclick(InterclickCancelled);
  2520. END;
  2521. IF allowCommandExecution.Get() & (keys * {0, 1, 2} = {1}) THEN
  2522. canStart := TRUE; openFile := FALSE;
  2523. IF commandMarker = NIL THEN
  2524. commandMarker := CreateHighlight();
  2525. commandMarker.SetKind(HLUnder);
  2526. commandMarker.SetColor(LONGINT(0FF0000FFH));
  2527. text.AcquireRead;
  2528. FindCommand(internalPos, a, b);
  2529. commandMarker.SetFromTo(a, b);
  2530. cursor.SetPosition(pos);
  2531. text.ReleaseRead
  2532. END;
  2533. END;
  2534. IF canStart & (2 IN keys) THEN openFile := TRUE; SetInterclick(InterclickCancelled); END;
  2535. IF keys * {0, 1, 2} = {0, 1, 2} THEN AbortStart END;
  2536. IF allowPiemenu.Get() & (keys * {0, 1, 2} = {2}) THEN
  2537. text.AcquireRead;
  2538. ViewToTextPos(x, y, pos);
  2539. cursor.SetPosition(pos);
  2540. text.ReleaseRead;
  2541. ShowContextMenu(x, y)
  2542. END;
  2543. IF allowTextSelection.Get() &
  2544. ( (keys * {0, 1, 2} = {0}) (* left mouse for select *)
  2545. OR (keys * {0, 1, 2} = {1}) & doubleclickedWord (* remove selection when double clicking *)
  2546. OR (keys * {0,1,2} = {2}) & (~allowPiemenu.Get())) (* right mouse for selection if pie menu is not enabled *)
  2547. THEN
  2548. AbortStart;
  2549. text.AcquireRead;
  2550. ViewToTextPos(x, y, pos);
  2551. dragPossible := FALSE; selectWords := FALSE;
  2552. IF internalPos >= 0 THEN
  2553. selection.Sort;
  2554. IF (internalPos >= selection.a) & (internalPos < selection.b) & (interclick = InterclickNone) THEN
  2555. dragPossible := TRUE; downX := x; downY := y
  2556. ELSIF (interclick = InterclickNone) THEN
  2557. (* clicking the same position twice --> Word Selection Mode *)
  2558. IF (internalPos = GetInternalPos(cursor.GetPosition())) OR ((internalPos - 1 = GetInternalPos(cursor.GetPosition())) & (internalPos - 1 = text.GetLength())) THEN
  2559. (* Workaround: The 2nd check is for the very last line of a text. LayoutLine gives pos = text.GetLength()+1 *)
  2560. selectWords := TRUE; wordSelOrdered := TRUE;
  2561. doubleclickedWord := TRUE;
  2562. DoubleClickSelect(internalPos);
  2563. ELSE
  2564. selection.SetFromTo(internalPos, internalPos); (* reset selection *)
  2565. cursor.SetVisible(TRUE);
  2566. END;
  2567. selecting := TRUE;
  2568. END
  2569. END;
  2570. cursor.SetPosition(pos);
  2571. text.ReleaseRead;
  2572. CursorChanged
  2573. END;
  2574. END PointerDown;
  2575. PROCEDURE PointerMove*(x, y : LONGINT; keys : SET);
  2576. VAR pos, a, b, internalPos : LONGINT;
  2577. BEGIN
  2578. IF ~canStart & dragPossible THEN
  2579. IF (ABS(x - downX) > DragDist) OR (ABS(y - downY) > DragDist) THEN dragPossible := FALSE; AutoStartDrag END
  2580. ELSE
  2581. IF (selecting OR canStart) & (interclick = InterclickNone) THEN
  2582. text.AcquireRead;
  2583. ViewToTextPos(x, y, pos);
  2584. internalPos := GetInternalPos(pos);
  2585. IF selecting & ~doubleclickedWord THEN
  2586. selection.Sort;
  2587. IF selectWords THEN
  2588. IF internalPos < selection.from.GetPosition() THEN
  2589. pos := TextUtilities.FindPosWordLeft(utilreader, internalPos - 1);
  2590. ELSE
  2591. pos := TextUtilities.FindPosWordRight(utilreader, internalPos + 1);
  2592. END;
  2593. selection.SetTo(internalPos)
  2594. ELSE
  2595. selection.SetTo(internalPos);
  2596. END;
  2597. selection.Sort;
  2598. cursor.SetVisible(selection.b - selection.a <= 0);
  2599. Texts.SetLastSelection(text, selection.from, selection.to);
  2600. cursor.SetPosition(pos);
  2601. StoreLineEnter;
  2602. ELSIF canStart THEN
  2603. IF commandMarker # NIL THEN
  2604. FindCommand(internalPos, a, b);
  2605. commandMarker.SetFromTo(a, b)
  2606. END
  2607. END;
  2608. IF doubleclickedWord THEN doubleclickedWord := FALSE; END; (* allow selecting again *)
  2609. text.ReleaseRead;
  2610. CursorChanged
  2611. END
  2612. END
  2613. END PointerMove;
  2614. PROCEDURE PointerUp*(x, y : LONGINT; keys : SET);
  2615. BEGIN
  2616. IF canStart & (commandMarker # NIL) THEN
  2617. commandMarker.Sort;
  2618. StartCommand((commandMarker.a + commandMarker.b) DIV 2, openFile);
  2619. AbortStart
  2620. END;
  2621. IF modifierFlags * Inputs.Ctrl # {} THEN
  2622. onCtrlClicked.Call(NIL)
  2623. END;
  2624. selecting := FALSE;
  2625. doubleclickedWord := FALSE;
  2626. IF (keys * {0,1,2} = {}) THEN
  2627. IF (interclick = Interclick02) THEN
  2628. DeleteSelection;
  2629. END;
  2630. SetInterclick(InterclickNone);
  2631. END;
  2632. IF dragPossible THEN selection.SetFromTo(0, 0); cursor.SetVisible(TRUE); Texts.ClearLastSelection (* reset selection *) END;
  2633. dragPossible := FALSE
  2634. END PointerUp;
  2635. (* Transforms the TextView Coordinates into TextObject obj Coordinates *)
  2636. PROCEDURE TransformCoordinates(VAR x, y : LONGINT; obj : WMComponents.VisualComponent);
  2637. VAR line, pos, x0, y0, y1 : LONGINT;
  2638. BEGIN
  2639. ViewToTextPos(x, y, pos);
  2640. IF FindScreenPos(pos, x0, y0) THEN
  2641. IF x0 > x THEN pos := pos - 1;
  2642. IF FindScreenPos(pos, x0, y0) THEN END;
  2643. END;
  2644. line := layout.FindLineNrByPos(GetInternalPos(pos));
  2645. LineYPos(line, y0, y1);
  2646. x := x - x0;
  2647. y := y - y0;
  2648. IF line >= 0 THEN y := y - (layout.lines[line].ascent - obj.bounds.GetHeight()); END
  2649. END
  2650. END TransformCoordinates;
  2651. (* Change the pointer according to the underlaying component *)
  2652. PROCEDURE ChangePointer(pointerInfo : WMWindowManager.PointerInfo);
  2653. BEGIN
  2654. IF GetPointerInfo() # pointerInfo THEN
  2655. SetPointerInfo(pointerInfo)
  2656. END
  2657. END ChangePointer;
  2658. (* Returns TRUE if an Object is Hit, FALSE otherwise *)
  2659. PROCEDURE HitObject(x, y : LONGINT; (* keys : SET;*) VAR pos : LONGINT; VAR obj : ANY): BOOLEAN;
  2660. VAR ch, tx, ty : LONGINT;
  2661. BEGIN
  2662. text.AcquireRead;
  2663. ViewToTextPos(x, y, pos);
  2664. IF FindScreenPos(pos, tx, ty) THEN
  2665. IF tx > x THEN pos := pos - 1 END
  2666. END;
  2667. utilreader.SetPosition(GetInternalPos(pos));
  2668. utilreader.ReadCh(ch);
  2669. text.ReleaseRead;
  2670. IF ch = Texts.ObjectChar THEN obj := utilreader.object;
  2671. RETURN TRUE
  2672. ELSE
  2673. RETURN FALSE
  2674. END
  2675. END HitObject;
  2676. (* Returns TRUE if a Link is Hit, FALSE otherwise *)
  2677. PROCEDURE HitLink(x, y : LONGINT; VAR pos : LONGINT; VAR link : Texts.Link): BOOLEAN;
  2678. VAR ch, tx, ty : LONGINT;
  2679. BEGIN
  2680. text.AcquireRead;
  2681. ViewToTextPos(x, y, pos);
  2682. IF FindScreenPos(pos, tx, ty) THEN
  2683. IF tx > x THEN pos := pos - 1 END
  2684. END;
  2685. utilreader.SetPosition(GetInternalPos(pos));
  2686. utilreader.ReadCh(ch);
  2687. text.ReleaseRead;
  2688. IF utilreader.link # NIL THEN
  2689. link := utilreader.link;
  2690. RETURN TRUE
  2691. ELSE
  2692. RETURN FALSE
  2693. END
  2694. END HitLink;
  2695. PROCEDURE LinkClick(link : Texts.Link);
  2696. VAR w : LinkWrapper;
  2697. BEGIN
  2698. NEW(w); w.link := link;
  2699. onLinkClicked.Call(w)
  2700. END LinkClick;
  2701. (* builtin behaviour *)
  2702. PROCEDURE LinkClicked*(sender, data : ANY);
  2703. VAR tempLink : ARRAY 2048 OF CHAR;
  2704. tempLabel : ARRAY 256 OF CHAR;
  2705. pos, i : LONGINT;
  2706. BEGIN
  2707. IF data IS LinkWrapper THEN
  2708. COPY(data(LinkWrapper).link^, tempLink);
  2709. IF tempLink[0] = "#" THEN (* internal link *)
  2710. i := 0;
  2711. WHILE tempLink[i] # 0X DO
  2712. tempLabel[i] := tempLink[i+1];
  2713. INC(i);
  2714. END;
  2715. tempLink[i] := 0X;
  2716. (* find label in tv *)
  2717. IF FindLabel(tempLabel, pos) THEN
  2718. i := layout.nofLines-1;
  2719. WHILE (i >= 0) DO
  2720. IF layout.GetLineStartPos(i) < pos THEN firstLine.Set(i); RETURN END;
  2721. DEC(i);
  2722. END;
  2723. END;
  2724. ELSE (* other links *)
  2725. END
  2726. END
  2727. END LinkClicked;
  2728. (* Returns the position of the label in text *)
  2729. PROCEDURE FindLabel*(CONST label : ARRAY OF CHAR; VAR pos : LONGINT): BOOLEAN;
  2730. VAR ch : LONGINT;
  2731. found : BOOLEAN;
  2732. BEGIN
  2733. found := FALSE; pos := 0;
  2734. text.AcquireRead;
  2735. utilreader.SetDirection(1); utilreader.SetPosition(pos);
  2736. REPEAT
  2737. utilreader.ReadCh(ch);
  2738. IF ch = Texts.LabelChar THEN
  2739. IF utilreader.object(Texts.LabelPiece).label^ = label THEN
  2740. found := TRUE;
  2741. END;
  2742. END;
  2743. INC(pos);
  2744. UNTIL utilreader.eot OR found;
  2745. text.ReleaseRead;
  2746. RETURN found;
  2747. END FindLabel;
  2748. (* Drag away operations *)
  2749. PROCEDURE AutoStartDrag*;
  2750. VAR img : WMGraphics.Image;
  2751. c : WMGraphics.BufferCanvas;
  2752. w, h, i, la, lb, top : LONGINT;
  2753. l : LineInfo;
  2754. BEGIN
  2755. text.AcquireRead;
  2756. selection.Sort;
  2757. NEW(dragSelA, text);NEW(dragSelB, text);
  2758. dragSelA.SetPosition(selection.a); dragSelB.SetPosition(selection.b);
  2759. la := Limit(layout.FindLineNrByPos(selection.a), 0, layout.GetNofLines() - 1);
  2760. lb := Limit(layout.FindLineNrByPos(selection.b), 0, layout.GetNofLines() - 1);
  2761. (* estimate the size of the selection *)
  2762. h := 0; w := 0;
  2763. FOR i := la TO lb DO
  2764. h := h + (layout.lines[i].height);
  2765. w := Strings.Max(w, layout.lines[i].width);
  2766. END;
  2767. h := Limit(h, 20, 200);
  2768. w := Limit(w, 20, 400);
  2769. (* render to bitmap *)
  2770. NEW(img); Raster.Create(img, w, h, Raster.BGRA8888);
  2771. NEW(c, img);
  2772. top := 0;
  2773. (* hack the startpos of the first line *)
  2774. l := layout.lines[la]; l.pos := selection.a;
  2775. IF la = lb THEN RenderLine(c, l, la, top, selection.b - selection.a)
  2776. ELSE
  2777. RenderLine(c, l, la, top, -1);
  2778. top := top + l.height
  2779. END;
  2780. FOR i := la + 1 TO lb DO
  2781. IF i = lb THEN
  2782. RenderLine(c, layout.lines[i], i, top, selection.b - layout.lines[i].pos)
  2783. ELSE
  2784. RenderLine(c, layout.lines[i], i, top, -1);
  2785. top := top + (l.height)
  2786. END
  2787. END;
  2788. text.ReleaseRead;
  2789. IF StartDrag(NIL, img, 0,0,DragWasAccepted, NIL) THEN
  2790. ELSE KernelLog.String("WMTextView : Drag could not be started")
  2791. END;
  2792. END AutoStartDrag;
  2793. PROCEDURE DragWasAccepted(sender, data : ANY);
  2794. VAR di : WMWindowManager.DragInfo;
  2795. dt : WMDropTarget.DropTarget;
  2796. itf : WMDropTarget.DropInterface;
  2797. targetText, temp : Texts.Text;
  2798. string : Strings.String;
  2799. pos, a, b, res : LONGINT;
  2800. BEGIN
  2801. IF (dragSelA = NIL) OR (dragSelB = NIL) THEN RETURN END;
  2802. IF (data # NIL) & (data IS WMWindowManager.DragInfo) THEN
  2803. di := data(WMWindowManager.DragInfo);
  2804. IF (di.data # NIL) & (di.data IS WMDropTarget.DropTarget) THEN
  2805. dt := di.data(WMDropTarget.DropTarget)
  2806. ELSE RETURN
  2807. END
  2808. ELSE RETURN
  2809. END;
  2810. itf := dt.GetInterface(WMDropTarget.TypeText);
  2811. IF itf # NIL THEN
  2812. targetText := itf(WMDropTarget.DropText).text;
  2813. IF targetText # NIL THEN
  2814. targetText.AcquireWrite;
  2815. IF ~dragCopy THEN
  2816. IF TraceCopy IN Trace THEN KernelLog.String("WMTextView: Not copy"); KernelLog.Ln; END;
  2817. text.AcquireWrite;
  2818. a := dragSelA.GetPosition(); b := dragSelB.GetPosition();
  2819. pos := itf(WMDropTarget.DropText).pos.GetPosition();
  2820. IF (targetText # text) OR (pos < a) OR (pos > b) THEN
  2821. NEW(temp); temp.AcquireWrite; temp.CopyFromText(text, a, b-a, 0); temp.ReleaseWrite;
  2822. text.Delete(a, b- a);
  2823. pos := itf(WMDropTarget.DropText).pos.GetPosition();
  2824. temp.AcquireRead;
  2825. targetText.CopyFromText(temp, 0, temp.GetLength(), pos);
  2826. temp.ReleaseRead;
  2827. END;
  2828. text.ReleaseWrite
  2829. ELSE
  2830. IF TraceCopy IN Trace THEN KernelLog.String("WMTextView: Copy"); KernelLog.Ln; END;
  2831. text.AcquireRead;
  2832. pos := itf(WMDropTarget.DropText).pos.GetPosition();
  2833. a := dragSelA.GetPosition(); b := dragSelB.GetPosition();
  2834. targetText.CopyFromText(text, a, b-a, pos);
  2835. text.ReleaseRead;
  2836. END;
  2837. targetText.ReleaseWrite
  2838. END;
  2839. RETURN
  2840. END;
  2841. itf := dt.GetInterface(WMDropTarget.TypeString);
  2842. IF (itf # NIL) THEN
  2843. IF ~dragCopy THEN
  2844. text.AcquireWrite;
  2845. a := dragSelA.GetPosition(); b := dragSelB.GetPosition();
  2846. NEW(temp);
  2847. temp.AcquireWrite;
  2848. temp.CopyFromText(text, a, b-a, 0);
  2849. IF (temp.GetLength() > 0) THEN NEW(string, temp.GetLength() * 5); ELSE NEW(string, 1); string[0] := 0X; END;
  2850. temp.ReleaseWrite;
  2851. text.ReleaseWrite;
  2852. TextUtilities.TextToStr(temp, string^);
  2853. itf(WMDropTarget.DropString).Set(string^, res);
  2854. IF res = 0 THEN
  2855. text.AcquireWrite;
  2856. text.Delete(a, b- a);
  2857. text.ReleaseWrite;
  2858. END;
  2859. ELSE
  2860. text.AcquireRead;
  2861. a := dragSelA.GetPosition(); b := dragSelB.GetPosition();
  2862. NEW(temp);
  2863. temp.AcquireWrite;
  2864. temp.CopyFromText(text, a, b-a, 0);
  2865. IF (temp.GetLength() > 0) THEN NEW(string, temp.GetLength() * 5); ELSE NEW(string, 1); string[0] := 0X; END;
  2866. temp.ReleaseWrite;
  2867. text.ReleaseRead;
  2868. TextUtilities.TextToStr(temp, string^);
  2869. itf(WMDropTarget.DropString).Set(string^, res);
  2870. END;
  2871. END;
  2872. END DragWasAccepted;
  2873. (* Drag onto operations *)
  2874. PROCEDURE DragOver(x, y : LONGINT; dragInfo : WMWindowManager.DragInfo);
  2875. VAR pos : LONGINT;
  2876. BEGIN
  2877. IF takesFocus.Get() THEN
  2878. text.AcquireRead;
  2879. ViewToTextPos(x, y, pos);
  2880. cursor.SetVisible(TRUE);
  2881. cursor.SetPosition(pos);
  2882. StoreLineEnter;
  2883. text.ReleaseRead
  2884. END;
  2885. END DragOver;
  2886. PROCEDURE DragDropped*(x, y : LONGINT; dragInfo : WMWindowManager.DragInfo);
  2887. VAR dropTarget : TextDropTarget;
  2888. pos, internalPos : LONGINT;
  2889. p : Texts.TextPosition;
  2890. BEGIN
  2891. IF takesFocus.Get() THEN
  2892. text.AcquireRead;
  2893. ViewToTextPos(x, y, pos) ;
  2894. (* prevent a selection from being dropped behind the paragraph separator *)
  2895. internalPos := GetInternalPos(pos);
  2896. IF text.isUTF & (layout.bidiFormatter # NIL) THEN
  2897. IF layout.bidiFormatter.IsLastCharacterInLine(internalPos) THEN
  2898. DEC(internalPos);
  2899. END;
  2900. END;
  2901. NEW(p, text); p.SetPosition(internalPos);
  2902. NEW(dropTarget, text, p);
  2903. text.ReleaseRead;
  2904. IF ~hasFocus & ~alwaysShowCursorI THEN cursor.SetVisible(FALSE) END;
  2905. dragInfo.data := dropTarget;
  2906. ConfirmDrag(TRUE, dragInfo);
  2907. IF dragInfo.onAccept # NIL THEN dragInfo.onAccept(SELF, dragInfo) END;
  2908. ELSE
  2909. ConfirmDrag(FALSE, dragInfo);
  2910. IF dragInfo.onReject # NIL THEN dragInfo.onReject(SELF, dragInfo) END;
  2911. END;
  2912. END DragDropped;
  2913. PROCEDURE CopySelection*;
  2914. BEGIN
  2915. IF isPassword.Get() THEN RETURN END;
  2916. text.AcquireRead;
  2917. Texts.clipboard.AcquireWrite;
  2918. selection.Sort;
  2919. IF selection.b - selection.a > 0 THEN
  2920. (* clear the clipboard *)
  2921. IF Texts.clipboard.GetLength() > 0 THEN Texts.clipboard.Delete(0, Texts.clipboard.GetLength()) END;
  2922. Texts.clipboard.CopyFromText(text, selection.a, selection.b - selection.a, 0);
  2923. END;
  2924. Texts.clipboard.ReleaseWrite;
  2925. text.ReleaseRead
  2926. END CopySelection;
  2927. PROCEDURE DeleteSelection*;
  2928. BEGIN
  2929. Acquire; (* protect cursor *)
  2930. text.AcquireWrite;
  2931. selection.Sort;
  2932. text.Delete(selection.a, selection.b - selection.a);
  2933. cursor.SetVisible(TRUE);
  2934. text.ReleaseWrite;
  2935. Release;
  2936. END DeleteSelection;
  2937. PROCEDURE Paste*;
  2938. BEGIN
  2939. text.AcquireWrite;
  2940. Texts.clipboard.AcquireRead;
  2941. IF Texts.clipboard.GetLength() > 0 THEN
  2942. IF selection.b - selection.a # 0 THEN DeleteSelection() END;
  2943. text.CopyFromText(Texts.clipboard, 0, Texts.clipboard.GetLength(), cursor.GetPosition())
  2944. END;
  2945. Texts.clipboard.ReleaseRead;
  2946. text.ReleaseWrite
  2947. END Paste;
  2948. PROCEDURE SelectAll*;
  2949. BEGIN
  2950. Acquire; (* protect cursor *)
  2951. text.AcquireRead;
  2952. selection.SetFromTo(0, text.GetLength());
  2953. cursor.SetVisible(text.GetLength() <= 0);
  2954. Texts.SetLastSelection(text, selection.from, selection.to);
  2955. text.ReleaseRead;
  2956. Release;
  2957. END SelectAll;
  2958. (* Prepare to start the selection by keyboard. Clear the selection, if it is not contigous *)
  2959. PROCEDURE KeyStartSelection(pos : LONGINT);
  2960. BEGIN
  2961. IF selection.to.GetPosition() # pos THEN selection.SetFromTo(pos, pos); cursor.SetVisible(TRUE); Texts.ClearLastSelection END;
  2962. END KeyStartSelection;
  2963. (* update the keyboard selection with the new position, redraw from the last StartSelection *)
  2964. PROCEDURE KeyUpdateSelection(pos : LONGINT);
  2965. BEGIN
  2966. selection.SetTo(pos);
  2967. selection.Sort;
  2968. cursor.SetVisible(selection.b - selection.a <= 0);
  2969. Texts.SetLastSelection(text, selection.from, selection.to)
  2970. END KeyUpdateSelection;
  2971. PROCEDURE CursorChanged;
  2972. BEGIN
  2973. cursorBlinker.Show(cursor);
  2974. IF (onCursorChanged # NIL) THEN onCursorChanged END
  2975. END CursorChanged;
  2976. PROCEDURE CursorUp*(select : BOOLEAN);
  2977. VAR
  2978. pos, cPos, cl, lineStart : LONGINT;
  2979. BEGIN
  2980. Acquire;
  2981. text.AcquireRead;
  2982. pos := GetInternalPos(cursor.GetPosition());
  2983. IF select THEN
  2984. KeyStartSelection(pos)
  2985. ELSE
  2986. selection.SetFromTo(pos, pos);
  2987. cursor.SetVisible(TRUE);
  2988. Texts.ClearLastSelection
  2989. END;
  2990. cl := layout.FindLineNrByPos(pos);
  2991. IF cl > 0 THEN
  2992. DEC(cl);
  2993. lineStart := layout.GetLineStartPos(cl);
  2994. cPos := lineStart + Strings.Min(layout.GetLineLength(cl) - 1, lineEnter);
  2995. cursor.SetPosition(cPos);
  2996. IF cl < firstLineI THEN firstLine.Set(cl) END
  2997. END;
  2998. IF select THEN
  2999. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()));
  3000. END;
  3001. text.ReleaseRead;
  3002. Release;
  3003. CursorChanged
  3004. END CursorUp;
  3005. PROCEDURE CursorDown*(select : BOOLEAN);
  3006. VAR pos, cPos, cl, lineStart : LONGINT;
  3007. BEGIN
  3008. Acquire;
  3009. text.AcquireRead;
  3010. pos := GetInternalPos(cursor.GetPosition());
  3011. IF select THEN
  3012. KeyStartSelection(pos)
  3013. ELSE
  3014. selection.SetFromTo(pos, pos);
  3015. cursor.SetVisible(TRUE);
  3016. Texts.ClearLastSelection
  3017. END;
  3018. cl := layout.FindLineNrByPos(pos);
  3019. IF cl < layout.GetNofLines() - 1 THEN
  3020. INC(cl);
  3021. lineStart := layout.GetLineStartPos(cl);
  3022. cPos := lineStart + Strings.Min(layout.GetLineLength(cl) - 1, lineEnter);
  3023. cursor.SetPosition(cPos);
  3024. IF cl > FindLineByY(firstLineI, bounds.GetHeight() - bordersI.b) THEN firstLine.Set(firstLineI + 1 ) END
  3025. END;
  3026. IF select THEN
  3027. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3028. END;
  3029. text.ReleaseRead;
  3030. Release;
  3031. CursorChanged
  3032. END CursorDown;
  3033. (* Move the cursor one character/word to the left *)
  3034. PROCEDURE CursorLeft*(word, select : BOOLEAN);
  3035. VAR
  3036. pos, cPos, wPos : LONGINT;
  3037. BEGIN
  3038. Acquire;
  3039. text.AcquireRead;
  3040. PositionDebugging.SetPos(GetInternalPos(cursor.GetPosition()),cursor.GetPosition());
  3041. pos := GetInternalPos(cursor.GetPosition());
  3042. IF select THEN
  3043. KeyStartSelection(pos)
  3044. ELSE
  3045. selection.SetFromTo(pos, pos);
  3046. cursor.SetVisible(TRUE);
  3047. Texts.ClearLastSelection
  3048. END;
  3049. cPos := GetInternalPos(cursor.GetPosition()) - 1;
  3050. IF ~word THEN
  3051. cursor.SetPosition(GetDisplayPos(cPos));
  3052. ELSE
  3053. wPos := TextUtilities.FindPosWordLeft(utilreader, cPos);
  3054. cursor.SetPosition(GetDisplayPos(wPos));
  3055. END;
  3056. IF select THEN
  3057. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3058. END;
  3059. StoreLineEnter;
  3060. text.ReleaseRead;
  3061. Release;
  3062. CursorChanged
  3063. END CursorLeft;
  3064. (* Move the cursor one character/word to the right *)
  3065. PROCEDURE CursorRight*(word, select : BOOLEAN);
  3066. VAR
  3067. pos, cPos, wPos : LONGINT;
  3068. BEGIN
  3069. Acquire;
  3070. text.AcquireRead;
  3071. PositionDebugging.SetPos(GetInternalPos(cursor.GetPosition()),cursor.GetPosition());
  3072. pos := GetInternalPos(cursor.GetPosition());
  3073. IF select THEN
  3074. KeyStartSelection(pos)
  3075. ELSE
  3076. selection.SetFromTo(pos, pos);
  3077. cursor.SetVisible(TRUE);
  3078. Texts.ClearLastSelection
  3079. END;
  3080. cPos := GetInternalPos(cursor.GetPosition()) + 1;
  3081. IF ~word THEN
  3082. cursor.SetPosition(GetDisplayPos(cPos));
  3083. ELSE
  3084. wPos := TextUtilities.FindPosWordRight(utilreader, cPos);
  3085. cursor.SetPosition(GetDisplayPos(wPos));
  3086. END;
  3087. IF select THEN
  3088. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3089. END;
  3090. StoreLineEnter;
  3091. text.ReleaseRead;
  3092. Release;
  3093. CursorChanged
  3094. END CursorRight;
  3095. PROCEDURE PageDown*(select : BOOLEAN);
  3096. VAR dy : LONGINT; i, pos, iPos : LONGINT;
  3097. cx, cy : LONGINT;
  3098. BEGIN
  3099. Acquire;
  3100. text.AcquireRead;
  3101. iPos := GetInternalPos(cursor.GetPosition());
  3102. IF select THEN
  3103. KeyStartSelection(iPos)
  3104. ELSE
  3105. selection.SetFromTo(iPos, iPos);
  3106. cursor.SetVisible(TRUE);
  3107. Texts.ClearLastSelection
  3108. END;
  3109. IF firstLineI = layout.GetNofLines() - 1 THEN
  3110. cursor.SetPosition(text.GetLength());
  3111. ELSE
  3112. (* save cursor screen pos for repositioning *)
  3113. IF ~FindScreenPos(cursor.GetPosition(), cx, cy) THEN cx := 0; cy := 0 END;
  3114. i := firstLineI; dy := 0;
  3115. WHILE (i < layout.GetNofLines() - 1) & (dy < bounds.GetHeight() - bordersI.t - bordersI.b) DO
  3116. INC(i); dy := dy + (layout.lines[i].height)
  3117. END;
  3118. firstLine.Set(i);
  3119. (* set cursor to nearest pos on new page *)
  3120. ViewToTextPos(cx, cy, pos);
  3121. IF pos >= 0 THEN
  3122. cursor.SetPosition(pos);
  3123. END;
  3124. END;
  3125. IF select THEN
  3126. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3127. END;
  3128. text.ReleaseRead;
  3129. Release;
  3130. CursorChanged
  3131. END PageDown;
  3132. PROCEDURE PageUp*(select : BOOLEAN);
  3133. VAR dy : LONGINT; i, pos, iPos : LONGINT;
  3134. cx, cy : LONGINT;
  3135. BEGIN
  3136. Acquire;
  3137. text.AcquireRead;
  3138. iPos := GetInternalPos(cursor.GetPosition());
  3139. IF select THEN
  3140. KeyStartSelection(iPos)
  3141. ELSE
  3142. selection.SetFromTo(iPos, iPos);
  3143. cursor.SetVisible(TRUE);
  3144. Texts.ClearLastSelection
  3145. END;
  3146. IF firstLineI = 0 THEN
  3147. cursor.SetPosition(0);
  3148. ELSE
  3149. (* save cursor screen pos for repositioning *)
  3150. IF ~FindScreenPos(cursor.GetPosition(), cx, cy) THEN cx := 0; cy := 0 END;
  3151. (* go up one page but at least one page *)
  3152. i := firstLineI; dy := 0;
  3153. WHILE (i > 0) & (dy < bounds.GetHeight() - bordersI.t - bordersI.b) DO
  3154. DEC(i); dy := dy + (layout.lines[i].height)
  3155. END;
  3156. IF (i > 0) & (i = firstLineI) THEN DEC(i) END;
  3157. firstLine.Set(i);
  3158. (* set cursor to nearest pos on new page *)
  3159. ViewToTextPos(cx, cy, pos);
  3160. IF pos >= 0 THEN
  3161. cursor.SetPosition(pos);
  3162. END
  3163. END;
  3164. IF select THEN
  3165. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3166. END;
  3167. text.ReleaseRead;
  3168. Release;
  3169. CursorChanged
  3170. END PageUp;
  3171. PROCEDURE Home*(ctrl, select : BOOLEAN);
  3172. VAR
  3173. lineStart, cl, pos : LONGINT;
  3174. BEGIN
  3175. Acquire;
  3176. text.AcquireRead;
  3177. pos := GetInternalPos(cursor.GetPosition());
  3178. IF select THEN
  3179. KeyStartSelection(pos)
  3180. ELSE
  3181. selection.SetFromTo(pos, pos);
  3182. cursor.SetVisible(TRUE);
  3183. Texts.ClearLastSelection
  3184. END;
  3185. IF ctrl THEN
  3186. cursor.SetPosition(GetDisplayPos(0));
  3187. firstLine.Set(0)
  3188. ELSE
  3189. cl := layout.FindLineNrByPos(cursor.GetPosition());
  3190. lineStart := layout.GetLineStartPos(cl);
  3191. cursor.SetPosition(GetDisplayPos(lineStart));
  3192. END;
  3193. StoreLineEnter;
  3194. IF select THEN
  3195. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3196. END;
  3197. text.ReleaseRead;
  3198. Release;
  3199. CursorChanged
  3200. END Home;
  3201. PROCEDURE End*(ctrl, select : BOOLEAN);
  3202. VAR lineEnd, textLength, cl, pos, dispPos: LONGINT;
  3203. BEGIN
  3204. Acquire;
  3205. text.AcquireRead;
  3206. pos := GetInternalPos(cursor.GetPosition());
  3207. IF select THEN
  3208. KeyStartSelection(pos)
  3209. ELSE
  3210. selection.SetFromTo(pos, pos);
  3211. cursor.SetVisible(TRUE);
  3212. Texts.ClearLastSelection
  3213. END;
  3214. IF ctrl THEN
  3215. textLength := text.GetLength();
  3216. cursor.SetPosition(GetDisplayPos(textLength));
  3217. firstLine.Set(layout.FindLineNrByPos(text.GetLength()))
  3218. ELSE
  3219. cl := layout.FindLineNrByPos(cursor.GetPosition());
  3220. lineEnd := layout.GetLineStartPos(cl) + layout.GetLineLength(cl) - 1;
  3221. dispPos := GetDisplayPos(lineEnd);
  3222. cursor.SetPosition(dispPos);
  3223. END;
  3224. StoreLineEnter;
  3225. IF select THEN
  3226. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3227. END;
  3228. text.ReleaseRead;
  3229. Release;
  3230. CursorChanged
  3231. END End;
  3232. PROCEDURE KeyEvent*(ucs :LONGINT; flags : SET; VAR keysym : LONGINT);
  3233. BEGIN
  3234. modifierFlags := flags;
  3235. IF Inputs.Release IN flags THEN RETURN END;
  3236. dragCopy := modifierFlags * Inputs.Ctrl # {};
  3237. IF keysym = 01H THEN (* Ctrl-A *)
  3238. SelectAll
  3239. ELSIF keysym = 03H THEN (* Ctrl-C *)
  3240. CopySelection
  3241. ELSIF (keysym = 0FF63H) & (flags * Inputs.Ctrl # {}) THEN (*Ctrl Insert *)
  3242. CopySelection
  3243. ELSIF keysym = 12H THEN (* Ctrl-R *)
  3244. layout.FullLayout(TRUE); Invalidate;CheckNumberOfLines;
  3245. KernelLog.String("Refreshed"); KernelLog.Ln;
  3246. ELSIF keysym = 0FF51H THEN (* Cursor Left *)
  3247. CursorLeft(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  3248. ELSIF keysym = 0FF53H THEN (* Cursor Right *)
  3249. CursorRight(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  3250. ELSIF keysym = 0FF54H THEN (* Cursor Down *)
  3251. CursorDown(flags * Inputs.Shift # {})
  3252. ELSIF keysym = 0FF52H THEN (* Cursor Up *)
  3253. CursorUp(flags * Inputs.Shift # {})
  3254. ELSIF keysym = 0FF56H THEN (* Page Down *)
  3255. PageDown(flags * Inputs.Shift # {})
  3256. ELSIF keysym = 0FF55H THEN (* Page Up *)
  3257. PageUp(flags * Inputs.Shift # {})
  3258. ELSIF keysym = 0FF50H THEN (* Cursor Home *)
  3259. Home(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  3260. ELSIF keysym = 0FF57H THEN (* Cursor End *)
  3261. End(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  3262. END
  3263. END KeyEvent;
  3264. (* called by users that override the KeyEvents to allow copy drag drop *)
  3265. PROCEDURE SetFlags*(flags : SET);
  3266. BEGIN
  3267. modifierFlags := flags;
  3268. dragCopy := modifierFlags * Inputs.Ctrl # {};
  3269. END SetFlags;
  3270. PROCEDURE FindCommandRange*(pos: LONGINT; VAR start, end, nofLastSelections : LONGINT);
  3271. VAR ch : LONGINT; string : ARRAY 23 OF CHAR; i : LONGINT; sDoCommands, lastWasTilde : BOOLEAN;
  3272. escapeString: ARRAY 32 OF LONGINT; escapePos: LONGINT; escape: BOOLEAN;
  3273. (* note: this simple algorithm can be emplyed if the substring to be implicitly searched for does not contain its first character *)
  3274. PROCEDURE String(escape: BOOLEAN; CONST escapeString: ARRAY OF LONGINT);
  3275. VAR done: BOOLEAN; escapePos: LONGINT;
  3276. BEGIN
  3277. done := FALSE; escapePos := -1;
  3278. REPEAT
  3279. utilreader.ReadCh(ch);
  3280. IF ch = ORD('"') THEN
  3281. IF escape THEN
  3282. escapePos := 0;
  3283. ELSE
  3284. done := TRUE
  3285. END;
  3286. ELSIF escapePos >= 0 THEN
  3287. IF escapeString[escapePos] = 0 THEN
  3288. IF ch =ORD("\") THEN done := TRUE
  3289. ELSE escapePos := -1
  3290. END;
  3291. ELSIF escapeString[escapePos] # ch THEN
  3292. escapePos := -1;
  3293. ELSE
  3294. INC(escapePos);
  3295. END;
  3296. END;
  3297. UNTIL done OR utilreader.eot;
  3298. END String;
  3299. BEGIN
  3300. nofLastSelections := 0;
  3301. text.AcquireRead;
  3302. utilreader.SetDirection(-1); utilreader.SetPosition(pos);
  3303. REPEAT utilreader.ReadCh(ch) UNTIL TextUtilities.IsWhiteSpace(ch,text.isUTF) OR utilreader.eot;
  3304. start := utilreader.GetPosition() + 2;
  3305. IF utilreader.eot THEN DEC(start, 2) END;
  3306. (* search ~ *)
  3307. i := 0; sDoCommands := FALSE; lastWasTilde := FALSE;
  3308. utilreader.SetDirection(1); utilreader.SetPosition(start);
  3309. REPEAT
  3310. utilreader.ReadCh(ch);
  3311. IF ch = ORD('"') THEN
  3312. escapeString[escapePos] := 0;
  3313. String(escape, escapeString);
  3314. ELSIF ch =ORD("\") THEN
  3315. escape := TRUE;
  3316. escapePos := 0;
  3317. ELSIF escape THEN
  3318. IF TextUtilities.IsWhiteSpace(ch,text.isUTF) THEN escape := FALSE
  3319. ELSE escapeString[escapePos] := ch; INC(escapePos);
  3320. END;
  3321. END;
  3322. (* check whether the command is SystemTools.DoCommands *)
  3323. IF (i < 22) THEN
  3324. string[i] := CHR(ch);
  3325. INC(i);
  3326. IF (i = 22) THEN
  3327. string[22] := 0X;
  3328. IF (string = "SystemTools.DoCommands") OR Strings.StartsWith2("PreliminaryCommands",string) THEN
  3329. sDoCommands := TRUE;
  3330. END;
  3331. END;
  3332. END;
  3333. IF (CHR(ch) = "^") THEN
  3334. INC(nofLastSelections);
  3335. END;
  3336. (* We do a special treatment of the command SystemTools.DoCommands since we don't want a single
  3337. tilde character to delimit the parameter string for the particular command - but two tilde characters *)
  3338. IF sDoCommands THEN
  3339. IF (ch = ORD("~")) THEN
  3340. IF ~lastWasTilde THEN
  3341. lastWasTilde := TRUE;
  3342. utilreader.ReadCh(ch);
  3343. ELSE
  3344. (* Two tilde characters only separated with whitespace means this is the
  3345. end of the SystemTools.DoCommands parameter string *)
  3346. END;
  3347. ELSIF lastWasTilde & ~TextUtilities.IsWhiteSpace(ch,text.isUTF) THEN
  3348. lastWasTilde := FALSE;
  3349. END;
  3350. END;
  3351. UNTIL (ch = ORD("~")) OR (utilreader.eot);
  3352. end := utilreader.GetPosition() - 1;
  3353. IF utilreader.eot THEN INC(end) END;
  3354. text.ReleaseRead
  3355. END FindCommandRange;
  3356. PROCEDURE FindCommand*(pos: LONGINT; VAR start, end : LONGINT);
  3357. VAR ch : LONGINT;
  3358. BEGIN
  3359. text.AcquireRead;
  3360. utilreader.SetDirection(-1); utilreader.SetPosition(pos);
  3361. REPEAT utilreader.ReadCh(ch) UNTIL TextUtilities.IsWhiteSpace(ch,text.isUTF) OR utilreader.eot;
  3362. start := utilreader.GetPosition() + 2;
  3363. IF utilreader.eot THEN DEC(start, 2) END;
  3364. utilreader.SetDirection(1); utilreader.SetPosition(pos);
  3365. REPEAT utilreader.ReadCh(ch) UNTIL TextUtilities.IsWhiteSpace(ch,text.isUTF) OR utilreader.eot;
  3366. end := utilreader.GetPosition() - 1;
  3367. IF utilreader.eot THEN INC(end) END;
  3368. text.ReleaseRead;
  3369. END FindCommand;
  3370. (** Start the command in the text, starting on pos (or wordboundary before),
  3371. caller should hold lock on text to make the pos stable *)
  3372. PROCEDURE StartCommand*(pos : LONGINT; openFile : BOOLEAN);
  3373. VAR
  3374. start, end, bufSize : LONGINT;
  3375. context : Commands.Context;
  3376. arg : Streams.StringReader;
  3377. command : ARRAY MaxCommandLength OF CHAR;
  3378. parameters : POINTER TO ARRAY OF CHAR;
  3379. s : Strings.String;
  3380. msg : ARRAY 128 OF CHAR;
  3381. ignore : Modules.Name;
  3382. paramSize, nofLastSelections, i, j, a, b, res : LONGINT;
  3383. selectionText : Texts.Text;
  3384. selectionOk : BOOLEAN;
  3385. from, to: Texts.TextPosition;
  3386. commandCaller:OBJECT;
  3387. commandWriter, errorWriter: Streams.Writer;
  3388. BEGIN
  3389. Acquire;
  3390. text.AcquireRead;
  3391. IF openFile THEN FindCommand(pos, start, end)
  3392. ELSE FindCommandRange(pos, start, end, nofLastSelections)
  3393. END;
  3394. bufSize := Strings.Max(Strings.Min((end - start) * 5 + 1 (* for UTF *), MaxCallParameterBuf), 1);
  3395. NEW(s, bufSize);
  3396. paramSize := 0;
  3397. TextUtilities.SubTextToStrAt(text, start, end - start, paramSize, s^);
  3398. INC(paramSize);
  3399. text.ReleaseRead;
  3400. Release;
  3401. IF Inputs.Shift * modifierFlags # {} THEN
  3402. (*
  3403. Command / open will not see the caller => called as if no calling context was specified.
  3404. => Opening a text while holding a shift key down will usually result in a new viewer being opened.
  3405. *)
  3406. commandCaller := NIL
  3407. ELSE
  3408. commandCaller := SELF.commandCaller;
  3409. END;
  3410. IF openFile THEN
  3411. FileHandlers.OpenFile(s^, NIL, commandCaller)
  3412. ELSE
  3413. command := "";
  3414. i := 0;
  3415. WHILE (i < MaxCommandLength) & (s[i] # 0X) & (s[i] # ";") & (s[i] # " ") & (s[i] # 09X) & (s[i] # 0DX) & (s[i] # 0AX) DO
  3416. command[i] := s[i]; INC(i);
  3417. END;
  3418. IF i < MaxCommandLength THEN
  3419. command[i] := 0X;
  3420. INC(i);
  3421. Commands.Split(command, ignore, ignore, res, msg);
  3422. IF res # Commands.Ok THEN
  3423. KernelLog.String("WMTextView: Command parsing error, res: "); KernelLog.Int(res, 0);
  3424. KernelLog.String(" ("); KernelLog.String(msg); KernelLog.String(")"); KernelLog.Ln;
  3425. RETURN;
  3426. END;
  3427. ELSE
  3428. KernelLog.String("WMTextView: Command execution error: Command too long"); KernelLog.Ln;
  3429. RETURN;
  3430. END;
  3431. IF (Inputs.Alt * modifierFlags # {}) THEN
  3432. (* execute AltMMCommand with actual command and its parameters as parameter *)
  3433. COPY(AltMMCommand, command);
  3434. commandWriter := NIL; errorWriter := NIL;
  3435. i := 0;
  3436. ELSE
  3437. commandWriter := SELF.commandWriter;
  3438. errorWriter := SELF.errorWriter;
  3439. END;
  3440. IF (i < LEN(s)) THEN (* copy parameter string *)
  3441. selectionOk := FALSE;
  3442. IF (nofLastSelections > 0) THEN
  3443. IF Texts.GetLastSelection(selectionText, from, to) THEN
  3444. selectionOk := TRUE;
  3445. selectionText.AcquireRead;
  3446. a := Strings.Min(from.GetPosition(), to.GetPosition());
  3447. b := Strings.Max(from.GetPosition(), to.GetPosition());
  3448. INC(paramSize, b - a + 1);
  3449. END;
  3450. END;
  3451. NEW(parameters, paramSize);
  3452. j := 0;
  3453. WHILE (i < LEN(s)) & (j < LEN(parameters)-1) DO
  3454. IF (s[i] = "^") & selectionOk THEN
  3455. TextUtilities.SubTextToStrAt(selectionText, a, b - a, j, parameters^);
  3456. ELSE
  3457. parameters[j] := s[i]; INC(j);
  3458. END;
  3459. INC(i);
  3460. END;
  3461. parameters[j] := 0X;
  3462. IF selectionOk THEN
  3463. selectionText.ReleaseRead;
  3464. END;
  3465. ELSE
  3466. NEW(parameters, 1); parameters[0] := 0X;
  3467. END;
  3468. NEW(arg, LEN(parameters)); arg.SetRaw(parameters^, 0, LEN(parameters));
  3469. NEW(context, NIL, arg, commandWriter, errorWriter, commandCaller);
  3470. IF TraceCommands IN Trace THEN
  3471. KernelLog.String("WMTextView: Executing command: '"); KernelLog.String(command); KernelLog.String("'");
  3472. KernelLog.String(", parameters: ");
  3473. IF (parameters[0] = 0X) THEN KernelLog.String("None"); ELSE KernelLog.String("'"); KernelLog.String(parameters^); KernelLog.String("'"); END;
  3474. KernelLog.Ln;
  3475. END;
  3476. Commands.Activate(command, context, {}, res, msg);
  3477. IF (res # Commands.Ok) THEN
  3478. IF commandWriter # NIL THEN
  3479. commandWriter.String("WMTextView: Command execution error, res: "); commandWriter.Int(res, 0);
  3480. commandWriter.String(" ("); commandWriter.String(msg); commandWriter.String(")"); commandWriter.Ln;
  3481. commandWriter.Update;
  3482. ELSE
  3483. KernelLog.String("WMTextView: Command execution error, res: "); KernelLog.Int(res, 0);
  3484. KernelLog.String(" ("); KernelLog.String(msg); KernelLog.String(")"); KernelLog.Ln;
  3485. END;
  3486. END;
  3487. END;
  3488. END StartCommand;
  3489. PROCEDURE Start(sender, data: ANY);
  3490. VAR msg: ARRAY 512 OF CHAR; res: LONGINT;
  3491. BEGIN
  3492. IF (data # NIL) & (data IS ClickInfo) THEN
  3493. IF data(ClickInfo).cmdPar # NIL THEN
  3494. Commands.Call(data(ClickInfo).cmdPar^, {}, res, msg);
  3495. IF res # 0 THEN KernelLog.String("WMTextView: "); KernelLog.String(msg); KernelLog.Ln END;
  3496. END
  3497. END
  3498. END Start;
  3499. PROCEDURE Open(sender, data: ANY);
  3500. BEGIN
  3501. IF (data # NIL) & (data IS ClickInfo) THEN
  3502. IF data(ClickInfo).cmd # NIL THEN
  3503. FileHandlers.OpenFile(data(ClickInfo).cmd^, NIL, commandCaller)
  3504. END
  3505. END
  3506. END Open;
  3507. PROCEDURE PieMenuStart(sender, data: ANY);
  3508. BEGIN
  3509. Start(piemenu, piemenu.userData)
  3510. END PieMenuStart;
  3511. PROCEDURE PieMenuOpen(sender, data: ANY);
  3512. BEGIN
  3513. Open(piemenu, piemenu.userData)
  3514. END PieMenuOpen;
  3515. PROCEDURE PieMenuCopy(sender, data: ANY);
  3516. BEGIN
  3517. CopySelection;
  3518. END PieMenuCopy;
  3519. PROCEDURE PieMenuPaste(sender, data: ANY);
  3520. BEGIN
  3521. Paste;
  3522. END PieMenuPaste;
  3523. PROCEDURE ShowContextMenu(x, y: LONGINT);
  3524. VAR
  3525. popup : WMPopups.Popup;
  3526. start, end, bufSize : LONGINT;
  3527. command, s : Strings.String;
  3528. clickInfo : ClickInfo;
  3529. str : ARRAY 256 OF CHAR;
  3530. window : WMWindowManager.Window;
  3531. nofLastSelections : LONGINT;
  3532. BEGIN
  3533. ASSERT(IsCallFromSequencer());
  3534. text.AcquireRead;
  3535. FindCommand(cursor.GetPosition(), start, end);
  3536. bufSize := Strings.Max(Strings.Min((end - start) * 5 + 1 (* for UTF *), 4096), 1);
  3537. NEW(command, bufSize);
  3538. TextUtilities.SubTextToStr(text, start, end - start, command^);
  3539. FindCommandRange(cursor.GetPosition(), start, end, nofLastSelections);
  3540. bufSize := Strings.Max(Strings.Min((end - start) * 5 + 1 (* for UTF *), MaxCallParameterBuf), 1);
  3541. NEW(s, bufSize);
  3542. TextUtilities.SubTextToStr(text, start, end - start, s^);
  3543. text.ReleaseRead;
  3544. NEW(clickInfo);
  3545. clickInfo.cmd := command;
  3546. clickInfo.cmdPar := s;
  3547. IF UsePieMenu THEN
  3548. NEW(piemenu); piemenu.SetEnabled({0, 1, 2, 3});
  3549. piemenu.SetText(1, Strings.NewString("Open"));
  3550. piemenu.SetText(3, Strings.NewString("Start"));
  3551. piemenu.SetText(2, Strings.NewString("Copy"));
  3552. piemenu.SetText(0, Strings.NewString("Paste"));
  3553. piemenu.userData := clickInfo;
  3554. piemenu.on1.Add(PieMenuOpen);
  3555. piemenu.on2.Add(PieMenuCopy);
  3556. piemenu.on3.Add(PieMenuStart);
  3557. piemenu.on0.Add(PieMenuPaste);
  3558. manager := WMWindowManager.GetDefaultManager();
  3559. window := manager.GetPositionOwner(x, y);
  3560. IF window = NIL THEN RETURN END;
  3561. Acquire; ToWMCoordinates(x, y, x, y); Release;
  3562. piemenu.Show(window, x, y, FALSE);
  3563. (* TODO: Can't set := NIL, since its used by the button handlers *)
  3564. ELSE
  3565. NEW(popup);
  3566. str := "Start "; Strings.Append(str, command^); popup.AddParButton(str, Start, clickInfo);
  3567. str := "Open "; Strings.Append(str, command^); popup.AddParButton(str, Open, clickInfo);
  3568. Acquire; ToWMCoordinates(x, y, x, y); Release;
  3569. popup.Popup(x, y);
  3570. END
  3571. END ShowContextMenu;
  3572. PROCEDURE HandleInternal(VAR x: WMMessages.Message);
  3573. VAR pos : LONGINT; obj : ANY; link : Texts.Link;
  3574. BEGIN
  3575. ASSERT(IsCallFromSequencer());
  3576. IF (x.msgType = WMMessages.MsgKey) & objHasFocus THEN (* forward KeyMsg *)
  3577. WITH focusObject : WMComponents.VisualComponent DO
  3578. focusObject.Handle(x);
  3579. InvalidateRange(focusPos, focusPos+1)
  3580. END
  3581. ELSIF (x.msgType # WMMessages.MsgKey) & HitObject(x.x, x.y, pos, obj) THEN (* forward Msg *)
  3582. SetFocus; cursor.SetVisible(FALSE);
  3583. IF obj IS WMComponents.VisualComponent THEN
  3584. WITH obj : WMComponents.VisualComponent DO
  3585. (* remove oldObject first *)
  3586. IF (oldObject # NIL) & (oldObject # obj) THEN
  3587. oldObject(WMComponents.VisualComponent).Handle(x);
  3588. InvalidateRange(oldPos, oldPos+1);
  3589. END;
  3590. TransformCoordinates(x.x, x.y, obj); (* transform to obj coords *)
  3591. obj.Handle(x); (* call obj Handle *)
  3592. ChangePointer(obj.GetPointerInfo()); (* change the pointer Image *)
  3593. InvalidateRange(pos, pos+1); (* redraw obj *)
  3594. oldObject := obj; oldPos := pos; (* store last object *)
  3595. (* transfer focus to Object *)
  3596. IF (x.msgType = WMMessages.MsgPointer) & (x.flags * {0, 1, 2} = {0}) THEN
  3597. (* remove old focus first *)
  3598. IF (focusObject # NIL) & (focusObject # obj) THEN
  3599. focusObject(WMComponents.VisualComponent).FocusLost;
  3600. InvalidateRange(focusPos, focusPos+1)
  3601. END;
  3602. objHasFocus := TRUE;
  3603. focusObject := obj; focusPos := pos;
  3604. (* FocusLost *)
  3605. END
  3606. END
  3607. END
  3608. ELSIF (x.msgType = WMMessages.MsgPointer) & HitLink(x.x, x.y, pos, link) THEN (* Link *)
  3609. ChangePointer(manager.pointerLink);
  3610. IF (x.msgSubType = 2) &(oldFlags / x.flags = {CallURLPointer}) THEN LinkClick(link); END;
  3611. oldFlags := x.flags;
  3612. ELSE
  3613. ChangePointer(manager.pointerText); (* change Pointer back *)
  3614. (* transfer focus back to TextView *)
  3615. IF (focusObject # NIL) & (x.msgType = WMMessages.MsgPointer) & (x.flags * {0, 1, 2} = {0}) THEN
  3616. focusObject(WMComponents.VisualComponent).FocusLost;
  3617. objHasFocus := FALSE;
  3618. InvalidateRange(focusPos, focusPos+1);
  3619. FocusReceived;
  3620. focusObject := NIL
  3621. END;
  3622. (* update last Object *)
  3623. IF (oldObject # NIL) & (x.msgType = WMMessages.MsgPointer) THEN
  3624. oldObject(WMComponents.VisualComponent).Handle(x);
  3625. InvalidateRange(oldPos, oldPos+1);
  3626. oldObject := NIL
  3627. END;
  3628. IF (x.msgType = WMMessages.MsgExt) & (x.ext # NIL) & (x.ext IS Texts.StyleChangedMsg) THEN
  3629. layout.FullLayout(TRUE); Invalidate; CheckNumberOfLines;
  3630. ELSE
  3631. HandleInternal^(x);
  3632. END;
  3633. END
  3634. END HandleInternal;
  3635. END TextView;
  3636. TYPE
  3637. FontEntry = OBJECT
  3638. VAR
  3639. name : ARRAY 256 OF CHAR;
  3640. attributes : FontAttributes;
  3641. next : FontEntry;
  3642. PROCEDURE &Init(CONST name : ARRAY OF CHAR);
  3643. BEGIN
  3644. COPY(name, SELF.name);
  3645. attributes := NIL;
  3646. next := NIL;
  3647. END Init;
  3648. END FontEntry;
  3649. FontAttributes = OBJECT
  3650. VAR
  3651. font : WMGraphics.Font; (* { font # {} *)
  3652. size : LONGINT;
  3653. style : SET;
  3654. next : FontAttributes;
  3655. PROCEDURE &Init(size : LONGINT; style : SET);
  3656. BEGIN
  3657. font := NIL;
  3658. SELF.size := size;
  3659. SELF.style := style;
  3660. next := NIL;
  3661. END Init;
  3662. END FontAttributes;
  3663. (* not thread-safe! not global to avoid locking and keep size smaller *)
  3664. FontCache = OBJECT
  3665. VAR
  3666. entries : FontEntry;
  3667. defaultFont : WMGraphics.Font;
  3668. PROCEDURE &Init;
  3669. BEGIN
  3670. NEW(entries, "head"); (* head of list *)
  3671. defaultFont := WMGraphics.GetDefaultFont();
  3672. END Init;
  3673. PROCEDURE Find(CONST name : ARRAY OF CHAR; size : LONGINT; style : SET) : WMGraphics.Font;
  3674. VAR font : WMGraphics.Font; e : FontEntry; a : FontAttributes;
  3675. BEGIN
  3676. font := NIL;
  3677. e := entries.next;
  3678. WHILE (e # NIL ) & (e.name < name) DO e := e.next; END;
  3679. IF (e # NIL) & (e.name = name) THEN
  3680. a := e.attributes;
  3681. WHILE (a # NIL) & (a.size < size) DO a := a.next; END;
  3682. WHILE (a # NIL) & (a.size = size) & (a.style # style) DO a := a.next; END;
  3683. IF (a # NIL) & (a.size = size) THEN
  3684. ASSERT(a.font # NIL);
  3685. font := a.font;
  3686. END;
  3687. END;
  3688. RETURN font;
  3689. END Find;
  3690. PROCEDURE Add(CONST name : ARRAY OF CHAR; size : LONGINT; style : SET) : WMGraphics.Font;
  3691. VAR entry, e : FontEntry; attribute, a : FontAttributes;
  3692. BEGIN
  3693. e := entries;
  3694. WHILE (e.next # NIL) & (e.next.name < name) DO e := e.next; END;
  3695. IF (e.next # NIL) & (e.next.name = name) THEN
  3696. entry := e.next;
  3697. ELSE
  3698. NEW(entry, name);
  3699. entry.next := e.next;
  3700. e.next := entry;
  3701. END;
  3702. ASSERT(entry # NIL);
  3703. NEW(attribute, size, style);
  3704. attribute.font := WMGraphics.GetFont(name, size, style);
  3705. IF (entry.attributes = NIL) THEN
  3706. entry.attributes := attribute;
  3707. ELSIF (entry.attributes.size >= attribute.size) THEN
  3708. attribute.next := entry.attributes;
  3709. entry.attributes := attribute;
  3710. ELSE
  3711. a := entry.attributes;
  3712. WHILE (a.next # NIL) & (a.next.size < attribute.size) DO a := a.next; END;
  3713. attribute.next := a.next;
  3714. a.next := attribute;
  3715. END;
  3716. ASSERT(attribute.font # NIL);
  3717. RETURN attribute.font;
  3718. END Add;
  3719. (* Get specified font. If not available, the system default font is returned *)
  3720. PROCEDURE GetFont(CONST name : ARRAY OF CHAR; size : LONGINT; style : SET) : WMGraphics.Font;
  3721. VAR font : WMGraphics.Font;
  3722. BEGIN
  3723. font := Find(name, size, style);
  3724. IF (font = NIL) THEN
  3725. font := Add(name, size, style);
  3726. END;
  3727. ASSERT(font # NIL);
  3728. RETURN font;
  3729. END GetFont;
  3730. END FontCache;
  3731. VAR
  3732. manager : WMWindowManager.WindowManager;
  3733. cursorBlinker- : CursorBlinker;
  3734. PTVIsMultiLine, PTVIsPassword, PTVShowBorder, PTValwaysShowCursor, PTVShowLabels : WMProperties.BooleanProperty;
  3735. PTVAllowCommandExecution, PTVAllowTextSelection, PTVAllowPiemenu : WMProperties.BooleanProperty;
  3736. PTVWrapMode, PTVMouseWheelScrollSpeed, PVTtextAlignV : WMProperties.Int32Property;
  3737. PTVfirstLine, PTVleftShift, PTVPasswordChar : WMProperties.Int32Property;
  3738. PTVdefaultTextColor, PTVdefaultTextBgColor : WMProperties.ColorProperty;
  3739. PTVborders : WMProperties.RectangleProperty;
  3740. PTVonLinkClick, PTVonLinkClickInfo : Strings.String;
  3741. PTVonCtrlLinkClick, PTVonCtrlLinkClickInfo : Strings.String;
  3742. PTVShowLineNumbers, PTVIndicateTabs : WMProperties.BooleanProperty;
  3743. PTVHighlighting : WMProperties.StringProperty;
  3744. PTVLineNumberColor, PTVLineNumberBgColor, PTVclBgCurrentLine : WMProperties.ColorProperty;
  3745. currentTextView : TextView;
  3746. StrTextView : Strings.String;
  3747. DefaultStyle : POINTER TO RECORD END;
  3748. PROCEDURE Limit(x, min, max : LONGINT) : LONGINT;
  3749. BEGIN
  3750. IF x < min THEN x := min END;
  3751. IF x > max THEN x := max END;
  3752. RETURN x
  3753. END Limit;
  3754. PROCEDURE GetFontFromAttr(info : Texts.FontInfo) : WMGraphics.Font;
  3755. BEGIN
  3756. RETURN WMGraphics.GetFont(info.name, info.size, info.style);
  3757. END GetFontFromAttr;
  3758. PROCEDURE IsSameFont(f1, f2 : WMGraphics.Font) : BOOLEAN;
  3759. BEGIN
  3760. RETURN (f1 = f2) OR ((f1 # NIL) & (f2 # NIL) & (f1.size = f2.size) & (f1.style = f2.style) & (f1.name = f2.name));
  3761. END IsSameFont;
  3762. PROCEDURE CheckFont(style : SyntaxHighlighter.Style; font : WMGraphics.Font; VAR fontCache : FontCache);
  3763. VAR fontname : ARRAY 256 OF CHAR; fontsize : LONGINT; fontstyle : SET;
  3764. BEGIN
  3765. ASSERT(style # NIL);
  3766. IF (fontCache = NIL) THEN NEW(fontCache); END;
  3767. IF (style.defined * SyntaxHighlighter.FontMask = SyntaxHighlighter.FontMask) OR (font = NIL) THEN
  3768. COPY(style.attributes.fontInfo.name, fontname);
  3769. fontsize := style.attributes.fontInfo.size;
  3770. fontstyle := style.attributes.fontInfo.style;
  3771. ELSIF (font # NIL) THEN
  3772. IF (SyntaxHighlighter.FontName IN style.defined) THEN COPY(style.attributes.fontInfo.name, fontname); ELSE COPY(font.name, fontname); END;
  3773. IF (SyntaxHighlighter.FontSize IN style.defined) THEN fontsize := style.attributes.fontInfo.size; ELSE fontsize := font.size; END;
  3774. IF (SyntaxHighlighter.FontStyle IN style.defined) THEN fontstyle := style.attributes.fontInfo.style; ELSE fontstyle := font.style; END;
  3775. END;
  3776. IF (style.attributes.fontInfo.fontcache = NIL) OR ~IsSameFont(style.attributes.fontInfo.fontcache (WMGraphics.Font), font) THEN
  3777. style.attributes.fontInfo.fontcache := fontCache.GetFont(fontname, fontsize, fontstyle);
  3778. END;
  3779. ASSERT(style.attributes.fontInfo.fontcache # NIL);
  3780. END CheckFont;
  3781. PROCEDURE InitStrings;
  3782. BEGIN
  3783. StrTextView := Strings.NewString("TextView");
  3784. PTVonLinkClick := Strings.NewString("Link Click Event");
  3785. PTVonLinkClickInfo := Strings.NewString("fired when a link is pressed");
  3786. PTVonCtrlLinkClick := Strings.NewString("Ctrl Click Event");
  3787. PTVonCtrlLinkClickInfo := Strings.NewString("fired when Ctrl pressend and clicked");
  3788. END InitStrings;
  3789. PROCEDURE InitPrototypes;
  3790. BEGIN
  3791. NEW(PTVIsMultiLine, NIL, Strings.NewString("multiLine"), Strings.NewString("defines if more than one line is visible"));
  3792. PTVIsMultiLine.Set(TRUE);
  3793. NEW(PTVShowBorder, NIL, Strings.NewString("ShowBorder"), Strings.NewString("show border"));
  3794. PTVShowBorder.Set(FALSE);
  3795. NEW(PTVIsPassword, NIL, Strings.NewString("password"),
  3796. Strings.NewString("defines if the view is a password text. Characters are replaced by passwordChar"));
  3797. NEW(PTVPasswordChar, NIL, Strings.NewString("passwordChar"),
  3798. Strings.NewString("character that is the placeholder for a character in a password"));
  3799. PTVPasswordChar.Set(43);
  3800. NEW(PTValwaysShowCursor, NIL, Strings.NewString("alwaysShowCursor"),
  3801. Strings.NewString("set to true, if the cursor should not be hidden when focus is lost"));
  3802. PTValwaysShowCursor.Set(FALSE);
  3803. NEW(PTVShowLabels, NIL, Strings.NewString("ShowLabels"),
  3804. Strings.NewString("set to true, if the labels should be shown in the text"));
  3805. PTVShowLabels.Set(FALSE);
  3806. NEW(PTVMouseWheelScrollSpeed, NIL, Strings.NewString("MouseWheelScrollSpeed"),
  3807. Strings.NewString("Multiplier for mouse wheel, 0 to disable mouse wheel scrolling"));
  3808. PTVMouseWheelScrollSpeed.Set(3);
  3809. NEW(PTVAllowCommandExecution, NIL, Strings.NewString("allowCommandExecution"),
  3810. Strings.NewString("if set to true, middle-clicked words are executed as command"));
  3811. PTVAllowCommandExecution.Set(TRUE);
  3812. NEW(PTVAllowTextSelection, NIL, Strings.NewString("allowTextSelection"),
  3813. Strings.NewString("is the user allowed to select text using the mouse?"));
  3814. PTVAllowTextSelection.Set(TRUE);
  3815. NEW(PTVAllowPiemenu, NIL, Strings.NewString("allowPiemenu"),
  3816. Strings.NewString("if set to true, a mouse right-click opens the pie menu"));
  3817. PTVAllowPiemenu.Set(TRUE);
  3818. NEW(PTVWrapMode, NIL, Strings.NewString("wrapMode"), Strings.NewString("Set text wrapping mode"));
  3819. PTVWrapMode.Set(WrapWord);
  3820. NEW(PTVfirstLine, NIL, Strings.NewString("firstLine"),
  3821. Strings.NewString("the first visible line of text in the view"));
  3822. PTVfirstLine.Set(0);
  3823. NEW(PTVleftShift, NIL, Strings.NewString("leftShift"),
  3824. Strings.NewString("how many pixels the text in the view is shifted to the left"));
  3825. PTVleftShift.Set(0);
  3826. NEW(PTVdefaultTextColor, NIL, Strings.NewString("defaultTextColor"),
  3827. Strings.NewString("the color of a text that does not explicitly specify a color"));
  3828. PTVdefaultTextColor.Set(0FFH);
  3829. NEW(PTVdefaultTextBgColor, NIL, Strings.NewString("defaultTextBgColor"),
  3830. Strings.NewString("The color of a text background if not specified otherwise in the text"));
  3831. PTVdefaultTextBgColor.Set(0);
  3832. NEW(PTVborders, NIL, Strings.NewString("borders"),
  3833. Strings.NewString("spaces from bounds of the component to the text"));
  3834. PTVborders.Set(WMRectangles.MakeRect(5, 5, 5, 5));
  3835. NEW(PTVIndicateTabs, NIL, Strings.NewString("IndicateTabs"), Strings.NewString("Indicate tabs?"));
  3836. PTVIndicateTabs.Set(FALSE);
  3837. NEW(PTVShowLineNumbers, NIL, Strings.NewString("ShowLineNumbers"), Strings.NewString("Show line numbers?"));
  3838. PTVShowLineNumbers.Set(FALSE);
  3839. NEW(PTVLineNumberColor, NIL, Strings.NewString("LineNumberColor"), Strings.NewString("Color of line numbers"));
  3840. PTVLineNumberColor.Set(WMGraphics.Black);
  3841. NEW(PTVLineNumberBgColor, NIL, Strings.NewString("LineNumberBgColor"), Strings.NewString("Background color of line numbers"));
  3842. PTVLineNumberBgColor.Set(0CCCCCCFFH);
  3843. NEW(PTVHighlighting, NIL, Strings.NewString("Highlighting"), Strings.NewString("Name of highlighting to be applied"));
  3844. PTVHighlighting.Set(NIL);
  3845. NEW(PTVclBgCurrentLine, NIL, Strings.NewString("ClBgCurrentLine"), Strings.NewString("Background color of currently edited line"));
  3846. PTVclBgCurrentLine.Set(0);
  3847. NEW(PVTtextAlignV, NIL, Strings.NewString("TextAlignV"), Strings.NewString("vertical Text Alignment"));
  3848. PVTtextAlignV.Set(WMGraphics.AlignTop);
  3849. END InitPrototypes;
  3850. PROCEDURE EnablePiemenu*;
  3851. BEGIN
  3852. PTVAllowPiemenu.Set( TRUE );
  3853. KernelLog.String( "Piemenu enabled" ); KernelLog.Ln
  3854. END EnablePiemenu;
  3855. PROCEDURE DisablePiemenu*;
  3856. BEGIN
  3857. PTVAllowPiemenu.Set( FALSE );
  3858. KernelLog.String( "Piemenu disabled" ); KernelLog.Ln
  3859. END DisablePiemenu;
  3860. PROCEDURE TextViewFactory*() : XML.Element;
  3861. VAR e : TextView;
  3862. BEGIN
  3863. NEW(e); RETURN e
  3864. END TextViewFactory;
  3865. (* Inserts a ocharacter from the outside into the current textView's text at its current position *)
  3866. PROCEDURE InsertChar*(newChar : Char32) : INTEGER;
  3867. BEGIN
  3868. IF currentTextView # NIL THEN
  3869. RETURN currentTextView.InsertChar(newChar);
  3870. ELSE
  3871. RETURN -3;
  3872. END;
  3873. END InsertChar;
  3874. PROCEDURE Refresh*;
  3875. BEGIN
  3876. IF currentTextView # NIL THEN
  3877. currentTextView.layout.FullLayout(TRUE);
  3878. currentTextView.Invalidate;
  3879. currentTextView.CheckNumberOfLines;
  3880. END;
  3881. END Refresh;
  3882. PROCEDURE Cleanup;
  3883. BEGIN
  3884. cursorBlinker.Finalize;
  3885. END Cleanup;
  3886. PROCEDURE GenTextView*(): XML.Element;
  3887. VAR e : TextView;
  3888. BEGIN
  3889. NEW(e); RETURN e
  3890. END GenTextView;
  3891. BEGIN
  3892. NEW(cursorBlinker);
  3893. NEW(DefaultStyle);
  3894. Modules.InstallTermHandler(Cleanup);
  3895. InitStrings;
  3896. InitPrototypes;
  3897. manager := WMWindowManager.GetDefaultManager();
  3898. END WMTextView.