WMTextView.Mod 142 KB

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