WMTextView.Mod 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317
  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; rect : WMRectangles.Rectangle; cstate : WMGraphics.CanvasState;
  2357. BEGIN
  2358. ASSERT(layout # NIL);
  2359. DrawBackground^(canvas);
  2360. rect := GetClientRect();
  2361. IF WMRectangles.RectEmpty(rect) THEN RETURN END;
  2362. IF showBorderI THEN
  2363. WMGraphicUtilities.DrawBevel(canvas, rect,
  2364. 1, TRUE, LONGINT(0808080FFH), WMGraphics.ModeCopy)
  2365. END;
  2366. canvas.SaveState(cstate);
  2367. (* allow clean clipping in at inner border *)
  2368. WMRectangles.ClipRect(rect, borderClip);
  2369. canvas.SetClipRect(rect);
  2370. (* draw gutter *)
  2371. IF showLineNumbersI & (lineNumberBgColorI # 0) THEN
  2372. rect.r := x0-1;
  2373. canvas.Fill(rect, lineNumberBgColorI, WMGraphics.ModeSrcOverDst);
  2374. END;
  2375. text.AcquireRead;
  2376. la := FindLineByY(firstLineI, Strings.Max(rect.t, bordersI.t));
  2377. lb := FindLineByY(firstLineI, Strings.Min(rect.b, bounds.GetHeight() - bordersI.b));
  2378. (* prepare selections *)
  2379. FOR i := 0 TO nofHighlights - 1 DO
  2380. highlights[i].a := highlights[i].from.GetPosition();
  2381. highlights[i].b := highlights[i].to.GetPosition();
  2382. IF highlights[i].a > highlights[i].b THEN t := highlights[i].a; highlights[i].a := highlights[i].b; highlights[i].b := t END
  2383. END;
  2384. top := bordersI.t;
  2385. IF (la = lb) & (textAlignV.Get() = WMGraphics.AlignCenter) THEN
  2386. t := bordersI.t;
  2387. b := bounds.GetHeight()-bordersI.b;
  2388. top := (t+b-layout.lines[la].height) DIV 2 (*- layout.lines[la].ascent*);
  2389. (* something is wrong with ascent and height here, does not comply with the notions in fonts *)
  2390. END;
  2391. FOR i := firstLineI TO la - 1 DO
  2392. top := top + (layout.lines[i].height);
  2393. CheckParagraphBegin(i, top);
  2394. CheckParagraphEnd(i, top);
  2395. END;
  2396. IF la >= 0 THEN
  2397. (* draw the lines that intersect the clipping rectangle *)
  2398. FOR i := la TO lb DO
  2399. CheckParagraphBegin(i, top);
  2400. RenderLine(canvas, layout.lines[i], i, top, -1);
  2401. top := top + (layout.lines[i].height);
  2402. CheckParagraphEnd(i, top);
  2403. END
  2404. END;
  2405. RenderAboveTextMarkers(canvas);
  2406. text.ReleaseRead;
  2407. canvas.RestoreState(cstate);
  2408. END DrawBackground;
  2409. PROCEDURE StoreLineEnter;
  2410. VAR pos, cl : LONGINT;
  2411. BEGIN
  2412. pos := cursor.GetPosition();
  2413. cl := layout.FindLineNrByPos(pos);
  2414. lineEnter := pos - layout.GetLineStartPos(cl)
  2415. END StoreLineEnter;
  2416. (* navigation *)
  2417. PROCEDURE WheelMove*(dz: LONGINT); (** PROTECTED *)
  2418. BEGIN
  2419. IF mouseWheelScrollSpeedI # 0 THEN
  2420. firstLine.Set(firstLine.Get() + mouseWheelScrollSpeedI * dz)
  2421. END;
  2422. END WheelMove;
  2423. (* abort a possible start of a command. Clear the command start indicator, if it was set *)
  2424. PROCEDURE AbortStart;
  2425. BEGIN
  2426. ASSERT(IsCallFromSequencer());
  2427. IF commandMarker # NIL THEN
  2428. RemoveHighlight(commandMarker);
  2429. commandMarker := NIL
  2430. END;
  2431. canStart := FALSE
  2432. END AbortStart;
  2433. (*
  2434. Handle double-click at text position <pos>.
  2435. Select the double-clicked word, whitespace or line.
  2436. Some explanations:
  2437. Why utilreader.GetPosition()+2 when searching to the left?
  2438. After we read the last character that should be included, the position of the reader is decremented.
  2439. When we now read the next character and see that it should not be included, the reader is decremented again.
  2440. -> The last character to be included was found at position utilreader.GetPosition()+2 (except when we reach EOT)
  2441. 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.
  2442. That's why utilreader.GetPosition()-1 is used instead of utilreader.GetPosition()-2.
  2443. *)
  2444. PROCEDURE DoubleClickSelect(pos : LONGINT);
  2445. CONST
  2446. LineFeed = 0AH;
  2447. Underscore = 05FH;
  2448. VAR
  2449. char : Texts.Char32;
  2450. from, to : LONGINT;
  2451. BEGIN
  2452. ASSERT(text.HasReadLock());
  2453. utilreader.SetPosition(pos);
  2454. utilreader.SetDirection(1);
  2455. utilreader.ReadCh(char);
  2456. IF (char = LineFeed) OR utilreader.eot THEN (* select line *)
  2457. IF utilreader.eot THEN to := pos;
  2458. ELSE to := pos+1;
  2459. END;
  2460. from := TextUtilities.FindPosLineStart(utilreader, pos);
  2461. ELSIF TextUtilities.IsWhiteSpace(char,text.isUTF) THEN
  2462. WHILE ~utilreader.eot & TextUtilities.IsWhiteSpace(char,text.isUTF) & (char # LineFeed) DO utilreader.ReadCh(char); END;
  2463. IF utilreader.eot THEN to := utilreader.text.GetLength();
  2464. ELSE to := utilreader.GetPosition()-1;
  2465. END;
  2466. utilreader.SetPosition(pos);
  2467. utilreader.SetDirection(-1);
  2468. utilreader.ReadCh(char);
  2469. WHILE ~utilreader.eot & TextUtilities.IsWhiteSpace(char,text.isUTF) & (char # LineFeed) DO utilreader.ReadCh(char); END;
  2470. IF utilreader.eot THEN from := 0;
  2471. ELSE from := utilreader.GetPosition()+2;
  2472. END;
  2473. ELSIF TextUtilities.IsAlphaNum(char) OR (char = Underscore) THEN (* select word *)
  2474. WHILE ~utilreader.eot & (TextUtilities.IsAlphaNum(char) OR (char = Underscore)) DO utilreader.ReadCh(char); END;
  2475. IF utilreader.eot THEN to := utilreader.text.GetLength();
  2476. ELSE to := utilreader.GetPosition()-1;
  2477. END;
  2478. utilreader.SetPosition(pos);
  2479. utilreader.SetDirection(-1);
  2480. utilreader.ReadCh(char);
  2481. WHILE ~utilreader.eot & (TextUtilities.IsAlphaNum(char) OR (char = Underscore)) DO utilreader.ReadCh(char); END;
  2482. IF utilreader.eot THEN from := 0;
  2483. ELSE from := utilreader.GetPosition()+2;
  2484. END;
  2485. ELSE (* select the character at text position pos *)
  2486. from := pos; to := pos+1;
  2487. END;
  2488. selection.SetFromTo(from, to);
  2489. cursor.SetVisible(to - from > 0);
  2490. END DoubleClickSelect;
  2491. PROCEDURE SetInterclick(new : LONGINT);
  2492. VAR old : LONGINT;
  2493. BEGIN
  2494. old := interclick;
  2495. IF (old # new) THEN
  2496. interclick := new;
  2497. CASE new OF
  2498. | Interclick01: selection.SetColor(SelectionColorInterclick01);
  2499. | Interclick02: selection.SetColor(SelectionColorInterclick02);
  2500. ELSE
  2501. selection.SetColor(SelectionColor);
  2502. END;
  2503. END;
  2504. END SetInterclick;
  2505. PROCEDURE PointerDown*(x, y : LONGINT; keys : SET);
  2506. VAR pos, a, b, internalPos : LONGINT; oldInterclick : LONGINT;
  2507. BEGIN
  2508. ViewToTextPos(x,y,pos);
  2509. internalPos := GetInternalPos(pos);
  2510. oldInterclick := interclick;
  2511. IF (keys * {0, 1} = {0,1}) THEN SetInterclick(Interclick01);
  2512. ELSIF (keys * {0,2} = {0,2}) THEN SetInterclick(Interclick02);
  2513. ELSE SetInterclick(InterclickNone);
  2514. END;
  2515. (* Determine whether to cancel an interclick if any *)
  2516. IF (oldInterclick = InterclickCancelled) OR
  2517. ((oldInterclick # InterclickNone) & (interclick # InterclickNone)) THEN
  2518. SetInterclick(InterclickCancelled);
  2519. END;
  2520. IF allowCommandExecution.Get() & (keys * {0, 1, 2} = {1}) THEN
  2521. canStart := TRUE; openFile := FALSE;
  2522. IF commandMarker = NIL THEN
  2523. commandMarker := CreateHighlight();
  2524. commandMarker.SetKind(HLUnder);
  2525. commandMarker.SetColor(LONGINT(0FF0000FFH));
  2526. text.AcquireRead;
  2527. FindCommand(internalPos, a, b);
  2528. commandMarker.SetFromTo(a, b);
  2529. cursor.SetPosition(pos);
  2530. text.ReleaseRead
  2531. END;
  2532. END;
  2533. IF canStart & (2 IN keys) THEN openFile := TRUE; SetInterclick(InterclickCancelled); END;
  2534. IF keys * {0, 1, 2} = {0, 1, 2} THEN AbortStart END;
  2535. IF allowPiemenu.Get() & (keys * {0, 1, 2} = {2}) THEN
  2536. text.AcquireRead;
  2537. ViewToTextPos(x, y, pos);
  2538. cursor.SetPosition(pos);
  2539. text.ReleaseRead;
  2540. ShowContextMenu(x, y)
  2541. END;
  2542. IF allowTextSelection.Get() &
  2543. ( (keys * {0, 1, 2} = {0}) (* left mouse for select *)
  2544. OR (keys * {0, 1, 2} = {1}) & doubleclickedWord (* remove selection when double clicking *)
  2545. OR (keys * {0,1,2} = {2}) & (~allowPiemenu.Get())) (* right mouse for selection if pie menu is not enabled *)
  2546. THEN
  2547. AbortStart;
  2548. text.AcquireRead;
  2549. ViewToTextPos(x, y, pos);
  2550. dragPossible := FALSE; selectWords := FALSE;
  2551. IF internalPos >= 0 THEN
  2552. selection.Sort;
  2553. IF (internalPos >= selection.a) & (internalPos < selection.b) & (interclick = InterclickNone) THEN
  2554. dragPossible := TRUE; downX := x; downY := y
  2555. ELSIF (interclick = InterclickNone) THEN
  2556. (* clicking the same position twice --> Word Selection Mode *)
  2557. IF (internalPos = GetInternalPos(cursor.GetPosition())) OR ((internalPos - 1 = GetInternalPos(cursor.GetPosition())) & (internalPos - 1 = text.GetLength())) THEN
  2558. (* Workaround: The 2nd check is for the very last line of a text. LayoutLine gives pos = text.GetLength()+1 *)
  2559. selectWords := TRUE; wordSelOrdered := TRUE;
  2560. doubleclickedWord := TRUE;
  2561. DoubleClickSelect(internalPos);
  2562. ELSE
  2563. selection.SetFromTo(internalPos, internalPos); (* reset selection *)
  2564. cursor.SetVisible(TRUE);
  2565. END;
  2566. selecting := TRUE;
  2567. END
  2568. END;
  2569. cursor.SetPosition(pos);
  2570. text.ReleaseRead;
  2571. CursorChanged
  2572. END;
  2573. END PointerDown;
  2574. PROCEDURE PointerMove*(x, y : LONGINT; keys : SET);
  2575. VAR pos, a, b, internalPos : LONGINT;
  2576. BEGIN
  2577. IF ~canStart & dragPossible THEN
  2578. IF (ABS(x - downX) > DragDist) OR (ABS(y - downY) > DragDist) THEN dragPossible := FALSE; AutoStartDrag END
  2579. ELSE
  2580. IF (selecting OR canStart) & (interclick = InterclickNone) THEN
  2581. text.AcquireRead;
  2582. ViewToTextPos(x, y, pos);
  2583. internalPos := GetInternalPos(pos);
  2584. IF selecting & ~doubleclickedWord THEN
  2585. selection.Sort;
  2586. IF selectWords THEN
  2587. IF internalPos < selection.from.GetPosition() THEN
  2588. pos := TextUtilities.FindPosWordLeft(utilreader, internalPos - 1);
  2589. ELSE
  2590. pos := TextUtilities.FindPosWordRight(utilreader, internalPos + 1);
  2591. END;
  2592. selection.SetTo(internalPos)
  2593. ELSE
  2594. selection.SetTo(internalPos);
  2595. END;
  2596. selection.Sort;
  2597. cursor.SetVisible(selection.b - selection.a <= 0);
  2598. Texts.SetLastSelection(text, selection.from, selection.to);
  2599. cursor.SetPosition(pos);
  2600. StoreLineEnter;
  2601. ELSIF canStart THEN
  2602. IF commandMarker # NIL THEN
  2603. FindCommand(internalPos, a, b);
  2604. commandMarker.SetFromTo(a, b)
  2605. END
  2606. END;
  2607. IF doubleclickedWord THEN doubleclickedWord := FALSE; END; (* allow selecting again *)
  2608. text.ReleaseRead;
  2609. CursorChanged
  2610. END
  2611. END
  2612. END PointerMove;
  2613. PROCEDURE PointerUp*(x, y : LONGINT; keys : SET);
  2614. BEGIN
  2615. IF canStart & (commandMarker # NIL) THEN
  2616. commandMarker.Sort;
  2617. StartCommand((commandMarker.a + commandMarker.b) DIV 2, openFile);
  2618. AbortStart
  2619. END;
  2620. IF modifierFlags * Inputs.Ctrl # {} THEN
  2621. onCtrlClicked.Call(NIL)
  2622. END;
  2623. selecting := FALSE;
  2624. doubleclickedWord := FALSE;
  2625. IF (keys * {0,1,2} = {}) THEN
  2626. IF (interclick = Interclick02) THEN
  2627. DeleteSelection;
  2628. END;
  2629. SetInterclick(InterclickNone);
  2630. END;
  2631. IF dragPossible THEN selection.SetFromTo(0, 0); cursor.SetVisible(TRUE); Texts.ClearLastSelection (* reset selection *) END;
  2632. dragPossible := FALSE
  2633. END PointerUp;
  2634. (* Transforms the TextView Coordinates into TextObject obj Coordinates *)
  2635. PROCEDURE TransformCoordinates(VAR x, y : LONGINT; obj : WMComponents.VisualComponent);
  2636. VAR line, pos, x0, y0, y1 : LONGINT;
  2637. BEGIN
  2638. ViewToTextPos(x, y, pos);
  2639. IF FindScreenPos(pos, x0, y0) THEN
  2640. IF x0 > x THEN pos := pos - 1;
  2641. IF FindScreenPos(pos, x0, y0) THEN END;
  2642. END;
  2643. line := layout.FindLineNrByPos(GetInternalPos(pos));
  2644. LineYPos(line, y0, y1);
  2645. x := x - x0;
  2646. y := y - y0;
  2647. IF line >= 0 THEN y := y - (layout.lines[line].ascent - obj.bounds.GetHeight()); END
  2648. END
  2649. END TransformCoordinates;
  2650. (* Change the pointer according to the underlaying component *)
  2651. PROCEDURE ChangePointer(pointerInfo : WMWindowManager.PointerInfo);
  2652. BEGIN
  2653. IF GetPointerInfo() # pointerInfo THEN
  2654. SetPointerInfo(pointerInfo)
  2655. END
  2656. END ChangePointer;
  2657. (* Returns TRUE if an Object is Hit, FALSE otherwise *)
  2658. PROCEDURE HitObject(x, y : LONGINT; (* keys : SET;*) VAR pos : LONGINT; VAR obj : ANY): BOOLEAN;
  2659. VAR ch, tx, ty : LONGINT;
  2660. BEGIN
  2661. text.AcquireRead;
  2662. ViewToTextPos(x, y, pos);
  2663. IF FindScreenPos(pos, tx, ty) THEN
  2664. IF tx > x THEN pos := pos - 1 END
  2665. END;
  2666. utilreader.SetPosition(GetInternalPos(pos));
  2667. utilreader.ReadCh(ch);
  2668. text.ReleaseRead;
  2669. IF ch = Texts.ObjectChar THEN obj := utilreader.object;
  2670. RETURN TRUE
  2671. ELSE
  2672. RETURN FALSE
  2673. END
  2674. END HitObject;
  2675. (* Returns TRUE if a Link is Hit, FALSE otherwise *)
  2676. PROCEDURE HitLink(x, y : LONGINT; VAR pos : LONGINT; VAR link : Texts.Link): BOOLEAN;
  2677. VAR ch, tx, ty : LONGINT;
  2678. BEGIN
  2679. text.AcquireRead;
  2680. ViewToTextPos(x, y, pos);
  2681. IF FindScreenPos(pos, tx, ty) THEN
  2682. IF tx > x THEN pos := pos - 1 END
  2683. END;
  2684. utilreader.SetPosition(GetInternalPos(pos));
  2685. utilreader.ReadCh(ch);
  2686. text.ReleaseRead;
  2687. IF utilreader.link # NIL THEN
  2688. link := utilreader.link;
  2689. RETURN TRUE
  2690. ELSE
  2691. RETURN FALSE
  2692. END
  2693. END HitLink;
  2694. PROCEDURE LinkClick(link : Texts.Link);
  2695. VAR w : LinkWrapper;
  2696. BEGIN
  2697. NEW(w); w.link := link;
  2698. onLinkClicked.Call(w)
  2699. END LinkClick;
  2700. (* builtin behaviour *)
  2701. PROCEDURE LinkClicked*(sender, data : ANY);
  2702. VAR tempLink : ARRAY 2048 OF CHAR;
  2703. tempLabel : ARRAY 256 OF CHAR;
  2704. pos, i : LONGINT;
  2705. BEGIN
  2706. IF data IS LinkWrapper THEN
  2707. COPY(data(LinkWrapper).link^, tempLink);
  2708. IF tempLink[0] = "#" THEN (* internal link *)
  2709. i := 0;
  2710. WHILE tempLink[i] # 0X DO
  2711. tempLabel[i] := tempLink[i+1];
  2712. INC(i);
  2713. END;
  2714. tempLink[i] := 0X;
  2715. (* find label in tv *)
  2716. IF FindLabel(tempLabel, pos) THEN
  2717. i := layout.nofLines-1;
  2718. WHILE (i >= 0) DO
  2719. IF layout.GetLineStartPos(i) < pos THEN firstLine.Set(i); RETURN END;
  2720. DEC(i);
  2721. END;
  2722. END;
  2723. ELSE (* other links *)
  2724. END
  2725. END
  2726. END LinkClicked;
  2727. (* Returns the position of the label in text *)
  2728. PROCEDURE FindLabel*(CONST label : ARRAY OF CHAR; VAR pos : LONGINT): BOOLEAN;
  2729. VAR ch : LONGINT;
  2730. found : BOOLEAN;
  2731. BEGIN
  2732. found := FALSE; pos := 0;
  2733. text.AcquireRead;
  2734. utilreader.SetDirection(1); utilreader.SetPosition(pos);
  2735. REPEAT
  2736. utilreader.ReadCh(ch);
  2737. IF ch = Texts.LabelChar THEN
  2738. IF utilreader.object(Texts.LabelPiece).label^ = label THEN
  2739. found := TRUE;
  2740. END;
  2741. END;
  2742. INC(pos);
  2743. UNTIL utilreader.eot OR found;
  2744. text.ReleaseRead;
  2745. RETURN found;
  2746. END FindLabel;
  2747. (* Drag away operations *)
  2748. PROCEDURE AutoStartDrag*;
  2749. VAR img : WMGraphics.Image;
  2750. c : WMGraphics.BufferCanvas;
  2751. w, h, i, la, lb, top : LONGINT;
  2752. l : LineInfo;
  2753. BEGIN
  2754. text.AcquireRead;
  2755. selection.Sort;
  2756. NEW(dragSelA, text);NEW(dragSelB, text);
  2757. dragSelA.SetPosition(selection.a); dragSelB.SetPosition(selection.b);
  2758. la := Limit(layout.FindLineNrByPos(selection.a), 0, layout.GetNofLines() - 1);
  2759. lb := Limit(layout.FindLineNrByPos(selection.b), 0, layout.GetNofLines() - 1);
  2760. (* estimate the size of the selection *)
  2761. h := 0; w := 0;
  2762. FOR i := la TO lb DO
  2763. h := h + (layout.lines[i].height);
  2764. w := Strings.Max(w, layout.lines[i].width);
  2765. END;
  2766. h := Limit(h, 20, 200);
  2767. w := Limit(w, 20, 400);
  2768. (* render to bitmap *)
  2769. NEW(img); Raster.Create(img, w, h, Raster.BGRA8888);
  2770. NEW(c, img);
  2771. top := 0;
  2772. (* hack the startpos of the first line *)
  2773. l := layout.lines[la]; l.pos := selection.a;
  2774. IF la = lb THEN RenderLine(c, l, la, top, selection.b - selection.a)
  2775. ELSE
  2776. RenderLine(c, l, la, top, -1);
  2777. top := top + l.height
  2778. END;
  2779. FOR i := la + 1 TO lb DO
  2780. IF i = lb THEN
  2781. RenderLine(c, layout.lines[i], i, top, selection.b - layout.lines[i].pos)
  2782. ELSE
  2783. RenderLine(c, layout.lines[i], i, top, -1);
  2784. top := top + (l.height)
  2785. END
  2786. END;
  2787. text.ReleaseRead;
  2788. IF StartDrag(NIL, img, 0,0,DragWasAccepted, NIL) THEN
  2789. ELSE KernelLog.String("WMTextView : Drag could not be started")
  2790. END;
  2791. END AutoStartDrag;
  2792. PROCEDURE DragWasAccepted(sender, data : ANY);
  2793. VAR di : WMWindowManager.DragInfo;
  2794. dt : WMDropTarget.DropTarget;
  2795. itf : WMDropTarget.DropInterface;
  2796. targetText, temp : Texts.Text;
  2797. string : Strings.String;
  2798. pos, a, b, res : LONGINT;
  2799. BEGIN
  2800. IF (dragSelA = NIL) OR (dragSelB = NIL) THEN RETURN END;
  2801. IF (data # NIL) & (data IS WMWindowManager.DragInfo) THEN
  2802. di := data(WMWindowManager.DragInfo);
  2803. IF (di.data # NIL) & (di.data IS WMDropTarget.DropTarget) THEN
  2804. dt := di.data(WMDropTarget.DropTarget)
  2805. ELSE RETURN
  2806. END
  2807. ELSE RETURN
  2808. END;
  2809. itf := dt.GetInterface(WMDropTarget.TypeText);
  2810. IF itf # NIL THEN
  2811. targetText := itf(WMDropTarget.DropText).text;
  2812. IF targetText # NIL THEN
  2813. targetText.AcquireWrite;
  2814. IF ~dragCopy THEN
  2815. IF TraceCopy IN Trace THEN KernelLog.String("WMTextView: Not copy"); KernelLog.Ln; END;
  2816. text.AcquireWrite;
  2817. a := dragSelA.GetPosition(); b := dragSelB.GetPosition();
  2818. pos := itf(WMDropTarget.DropText).pos.GetPosition();
  2819. IF (targetText # text) OR (pos < a) OR (pos > b) THEN
  2820. NEW(temp); temp.AcquireWrite; temp.CopyFromText(text, a, b-a, 0); temp.ReleaseWrite;
  2821. text.Delete(a, b- a);
  2822. pos := itf(WMDropTarget.DropText).pos.GetPosition();
  2823. temp.AcquireRead;
  2824. targetText.CopyFromText(temp, 0, temp.GetLength(), pos);
  2825. temp.ReleaseRead;
  2826. END;
  2827. text.ReleaseWrite
  2828. ELSE
  2829. IF TraceCopy IN Trace THEN KernelLog.String("WMTextView: Copy"); KernelLog.Ln; END;
  2830. text.AcquireRead;
  2831. pos := itf(WMDropTarget.DropText).pos.GetPosition();
  2832. a := dragSelA.GetPosition(); b := dragSelB.GetPosition();
  2833. targetText.CopyFromText(text, a, b-a, pos);
  2834. text.ReleaseRead;
  2835. END;
  2836. targetText.ReleaseWrite
  2837. END;
  2838. RETURN
  2839. END;
  2840. itf := dt.GetInterface(WMDropTarget.TypeString);
  2841. IF (itf # NIL) THEN
  2842. IF ~dragCopy THEN
  2843. text.AcquireWrite;
  2844. a := dragSelA.GetPosition(); b := dragSelB.GetPosition();
  2845. NEW(temp);
  2846. temp.AcquireWrite;
  2847. temp.CopyFromText(text, a, b-a, 0);
  2848. IF (temp.GetLength() > 0) THEN NEW(string, temp.GetLength() * 5); ELSE NEW(string, 1); string[0] := 0X; END;
  2849. temp.ReleaseWrite;
  2850. text.ReleaseWrite;
  2851. TextUtilities.TextToStr(temp, string^);
  2852. itf(WMDropTarget.DropString).Set(string^, res);
  2853. IF res = 0 THEN
  2854. text.AcquireWrite;
  2855. text.Delete(a, b- a);
  2856. text.ReleaseWrite;
  2857. END;
  2858. ELSE
  2859. text.AcquireRead;
  2860. a := dragSelA.GetPosition(); b := dragSelB.GetPosition();
  2861. NEW(temp);
  2862. temp.AcquireWrite;
  2863. temp.CopyFromText(text, a, b-a, 0);
  2864. IF (temp.GetLength() > 0) THEN NEW(string, temp.GetLength() * 5); ELSE NEW(string, 1); string[0] := 0X; END;
  2865. temp.ReleaseWrite;
  2866. text.ReleaseRead;
  2867. TextUtilities.TextToStr(temp, string^);
  2868. itf(WMDropTarget.DropString).Set(string^, res);
  2869. END;
  2870. END;
  2871. END DragWasAccepted;
  2872. (* Drag onto operations *)
  2873. PROCEDURE DragOver(x, y : LONGINT; dragInfo : WMWindowManager.DragInfo);
  2874. VAR pos : LONGINT;
  2875. BEGIN
  2876. IF takesFocus.Get() THEN
  2877. text.AcquireRead;
  2878. ViewToTextPos(x, y, pos);
  2879. cursor.SetVisible(TRUE);
  2880. cursor.SetPosition(pos);
  2881. StoreLineEnter;
  2882. text.ReleaseRead
  2883. END;
  2884. END DragOver;
  2885. PROCEDURE DragDropped*(x, y : LONGINT; dragInfo : WMWindowManager.DragInfo);
  2886. VAR dropTarget : TextDropTarget;
  2887. pos, internalPos : LONGINT;
  2888. p : Texts.TextPosition;
  2889. BEGIN
  2890. IF takesFocus.Get() THEN
  2891. text.AcquireRead;
  2892. ViewToTextPos(x, y, pos) ;
  2893. (* prevent a selection from being dropped behind the paragraph separator *)
  2894. internalPos := GetInternalPos(pos);
  2895. IF text.isUTF & (layout.bidiFormatter # NIL) THEN
  2896. IF layout.bidiFormatter.IsLastCharacterInLine(internalPos) THEN
  2897. DEC(internalPos);
  2898. END;
  2899. END;
  2900. NEW(p, text); p.SetPosition(internalPos);
  2901. NEW(dropTarget, text, p);
  2902. text.ReleaseRead;
  2903. IF ~hasFocus & ~alwaysShowCursorI THEN cursor.SetVisible(FALSE) END;
  2904. dragInfo.data := dropTarget;
  2905. ConfirmDrag(TRUE, dragInfo);
  2906. IF dragInfo.onAccept # NIL THEN dragInfo.onAccept(SELF, dragInfo) END;
  2907. ELSE
  2908. ConfirmDrag(FALSE, dragInfo);
  2909. IF dragInfo.onReject # NIL THEN dragInfo.onReject(SELF, dragInfo) END;
  2910. END;
  2911. END DragDropped;
  2912. PROCEDURE CopySelection*;
  2913. BEGIN
  2914. IF isPassword.Get() THEN RETURN END;
  2915. text.AcquireRead;
  2916. Texts.clipboard.AcquireWrite;
  2917. selection.Sort;
  2918. IF selection.b - selection.a > 0 THEN
  2919. (* clear the clipboard *)
  2920. IF Texts.clipboard.GetLength() > 0 THEN Texts.clipboard.Delete(0, Texts.clipboard.GetLength()) END;
  2921. Texts.clipboard.CopyFromText(text, selection.a, selection.b - selection.a, 0);
  2922. END;
  2923. Texts.clipboard.ReleaseWrite;
  2924. text.ReleaseRead
  2925. END CopySelection;
  2926. PROCEDURE DeleteSelection*;
  2927. BEGIN
  2928. Acquire; (* protect cursor *)
  2929. text.AcquireWrite;
  2930. selection.Sort;
  2931. text.Delete(selection.a, selection.b - selection.a);
  2932. cursor.SetVisible(TRUE);
  2933. text.ReleaseWrite;
  2934. Release;
  2935. END DeleteSelection;
  2936. PROCEDURE Paste*;
  2937. BEGIN
  2938. text.AcquireWrite;
  2939. Texts.clipboard.AcquireRead;
  2940. IF Texts.clipboard.GetLength() > 0 THEN
  2941. IF selection.b - selection.a # 0 THEN DeleteSelection() END;
  2942. text.CopyFromText(Texts.clipboard, 0, Texts.clipboard.GetLength(), cursor.GetPosition())
  2943. END;
  2944. Texts.clipboard.ReleaseRead;
  2945. text.ReleaseWrite
  2946. END Paste;
  2947. PROCEDURE SelectAll*;
  2948. BEGIN
  2949. Acquire; (* protect cursor *)
  2950. text.AcquireRead;
  2951. selection.SetFromTo(0, text.GetLength());
  2952. cursor.SetVisible(text.GetLength() <= 0);
  2953. Texts.SetLastSelection(text, selection.from, selection.to);
  2954. text.ReleaseRead;
  2955. Release;
  2956. END SelectAll;
  2957. (* Prepare to start the selection by keyboard. Clear the selection, if it is not contigous *)
  2958. PROCEDURE KeyStartSelection(pos : LONGINT);
  2959. BEGIN
  2960. IF selection.to.GetPosition() # pos THEN selection.SetFromTo(pos, pos); cursor.SetVisible(TRUE); Texts.ClearLastSelection END;
  2961. END KeyStartSelection;
  2962. (* update the keyboard selection with the new position, redraw from the last StartSelection *)
  2963. PROCEDURE KeyUpdateSelection(pos : LONGINT);
  2964. BEGIN
  2965. selection.SetTo(pos);
  2966. selection.Sort;
  2967. cursor.SetVisible(selection.b - selection.a <= 0);
  2968. Texts.SetLastSelection(text, selection.from, selection.to)
  2969. END KeyUpdateSelection;
  2970. PROCEDURE CursorChanged;
  2971. BEGIN
  2972. cursorBlinker.Show(cursor);
  2973. IF (onCursorChanged # NIL) THEN onCursorChanged END
  2974. END CursorChanged;
  2975. PROCEDURE CursorUp*(select : BOOLEAN);
  2976. VAR
  2977. pos, cPos, cl, lineStart : LONGINT;
  2978. BEGIN
  2979. Acquire;
  2980. text.AcquireRead;
  2981. pos := GetInternalPos(cursor.GetPosition());
  2982. IF select THEN
  2983. KeyStartSelection(pos)
  2984. ELSE
  2985. selection.SetFromTo(pos, pos);
  2986. cursor.SetVisible(TRUE);
  2987. Texts.ClearLastSelection
  2988. END;
  2989. cl := layout.FindLineNrByPos(pos);
  2990. IF cl > 0 THEN
  2991. DEC(cl);
  2992. lineStart := layout.GetLineStartPos(cl);
  2993. cPos := lineStart + Strings.Min(layout.GetLineLength(cl) - 1, lineEnter);
  2994. cursor.SetPosition(cPos);
  2995. IF cl < firstLineI THEN firstLine.Set(cl) END
  2996. END;
  2997. IF select THEN
  2998. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()));
  2999. END;
  3000. text.ReleaseRead;
  3001. Release;
  3002. CursorChanged
  3003. END CursorUp;
  3004. PROCEDURE CursorDown*(select : BOOLEAN);
  3005. VAR pos, cPos, cl, lineStart : LONGINT;
  3006. BEGIN
  3007. Acquire;
  3008. text.AcquireRead;
  3009. pos := GetInternalPos(cursor.GetPosition());
  3010. IF select THEN
  3011. KeyStartSelection(pos)
  3012. ELSE
  3013. selection.SetFromTo(pos, pos);
  3014. cursor.SetVisible(TRUE);
  3015. Texts.ClearLastSelection
  3016. END;
  3017. cl := layout.FindLineNrByPos(pos);
  3018. IF cl < layout.GetNofLines() - 1 THEN
  3019. INC(cl);
  3020. lineStart := layout.GetLineStartPos(cl);
  3021. cPos := lineStart + Strings.Min(layout.GetLineLength(cl) - 1, lineEnter);
  3022. cursor.SetPosition(cPos);
  3023. IF cl > FindLineByY(firstLineI, bounds.GetHeight() - bordersI.b) THEN firstLine.Set(firstLineI + 1 ) END
  3024. END;
  3025. IF select THEN
  3026. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3027. END;
  3028. text.ReleaseRead;
  3029. Release;
  3030. CursorChanged
  3031. END CursorDown;
  3032. (* Move the cursor one character/word to the left *)
  3033. PROCEDURE CursorLeft*(word, select : BOOLEAN);
  3034. VAR
  3035. pos, cPos, wPos : LONGINT;
  3036. BEGIN
  3037. Acquire;
  3038. text.AcquireRead;
  3039. PositionDebugging.SetPos(GetInternalPos(cursor.GetPosition()),cursor.GetPosition());
  3040. pos := GetInternalPos(cursor.GetPosition());
  3041. IF select THEN
  3042. KeyStartSelection(pos)
  3043. ELSE
  3044. selection.SetFromTo(pos, pos);
  3045. cursor.SetVisible(TRUE);
  3046. Texts.ClearLastSelection
  3047. END;
  3048. cPos := GetInternalPos(cursor.GetPosition()) - 1;
  3049. IF ~word THEN
  3050. cursor.SetPosition(GetDisplayPos(cPos));
  3051. ELSE
  3052. wPos := TextUtilities.FindPosWordLeft(utilreader, cPos);
  3053. cursor.SetPosition(GetDisplayPos(wPos));
  3054. END;
  3055. IF select THEN
  3056. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3057. END;
  3058. StoreLineEnter;
  3059. text.ReleaseRead;
  3060. Release;
  3061. CursorChanged
  3062. END CursorLeft;
  3063. (* Move the cursor one character/word to the right *)
  3064. PROCEDURE CursorRight*(word, select : BOOLEAN);
  3065. VAR
  3066. pos, cPos, wPos : LONGINT;
  3067. BEGIN
  3068. Acquire;
  3069. text.AcquireRead;
  3070. PositionDebugging.SetPos(GetInternalPos(cursor.GetPosition()),cursor.GetPosition());
  3071. pos := GetInternalPos(cursor.GetPosition());
  3072. IF select THEN
  3073. KeyStartSelection(pos)
  3074. ELSE
  3075. selection.SetFromTo(pos, pos);
  3076. cursor.SetVisible(TRUE);
  3077. Texts.ClearLastSelection
  3078. END;
  3079. cPos := GetInternalPos(cursor.GetPosition()) + 1;
  3080. IF ~word THEN
  3081. cursor.SetPosition(GetDisplayPos(cPos));
  3082. ELSE
  3083. wPos := TextUtilities.FindPosWordRight(utilreader, cPos);
  3084. cursor.SetPosition(GetDisplayPos(wPos));
  3085. END;
  3086. IF select THEN
  3087. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3088. END;
  3089. StoreLineEnter;
  3090. text.ReleaseRead;
  3091. Release;
  3092. CursorChanged
  3093. END CursorRight;
  3094. PROCEDURE PageDown*(select : BOOLEAN);
  3095. VAR dy : LONGINT; i, pos, iPos : LONGINT;
  3096. cx, cy : LONGINT;
  3097. BEGIN
  3098. Acquire;
  3099. text.AcquireRead;
  3100. iPos := GetInternalPos(cursor.GetPosition());
  3101. IF select THEN
  3102. KeyStartSelection(iPos)
  3103. ELSE
  3104. selection.SetFromTo(iPos, iPos);
  3105. cursor.SetVisible(TRUE);
  3106. Texts.ClearLastSelection
  3107. END;
  3108. IF firstLineI = layout.GetNofLines() - 1 THEN
  3109. cursor.SetPosition(text.GetLength());
  3110. ELSE
  3111. (* save cursor screen pos for repositioning *)
  3112. IF ~FindScreenPos(cursor.GetPosition(), cx, cy) THEN cx := 0; cy := 0 END;
  3113. i := firstLineI; dy := 0;
  3114. WHILE (i < layout.GetNofLines() - 1) & (dy < bounds.GetHeight() - bordersI.t - bordersI.b) DO
  3115. INC(i); dy := dy + (layout.lines[i].height)
  3116. END;
  3117. firstLine.Set(i);
  3118. (* set cursor to nearest pos on new page *)
  3119. ViewToTextPos(cx, cy, pos);
  3120. IF pos >= 0 THEN
  3121. cursor.SetPosition(pos);
  3122. END;
  3123. END;
  3124. IF select THEN
  3125. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3126. END;
  3127. text.ReleaseRead;
  3128. Release;
  3129. CursorChanged
  3130. END PageDown;
  3131. PROCEDURE PageUp*(select : BOOLEAN);
  3132. VAR dy : LONGINT; i, pos, iPos : LONGINT;
  3133. cx, cy : LONGINT;
  3134. BEGIN
  3135. Acquire;
  3136. text.AcquireRead;
  3137. iPos := GetInternalPos(cursor.GetPosition());
  3138. IF select THEN
  3139. KeyStartSelection(iPos)
  3140. ELSE
  3141. selection.SetFromTo(iPos, iPos);
  3142. cursor.SetVisible(TRUE);
  3143. Texts.ClearLastSelection
  3144. END;
  3145. IF firstLineI = 0 THEN
  3146. cursor.SetPosition(0);
  3147. ELSE
  3148. (* save cursor screen pos for repositioning *)
  3149. IF ~FindScreenPos(cursor.GetPosition(), cx, cy) THEN cx := 0; cy := 0 END;
  3150. (* go up one page but at least one page *)
  3151. i := firstLineI; dy := 0;
  3152. WHILE (i > 0) & (dy < bounds.GetHeight() - bordersI.t - bordersI.b) DO
  3153. DEC(i); dy := dy + (layout.lines[i].height)
  3154. END;
  3155. IF (i > 0) & (i = firstLineI) THEN DEC(i) END;
  3156. firstLine.Set(i);
  3157. (* set cursor to nearest pos on new page *)
  3158. ViewToTextPos(cx, cy, pos);
  3159. IF pos >= 0 THEN
  3160. cursor.SetPosition(pos);
  3161. END
  3162. END;
  3163. IF select THEN
  3164. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3165. END;
  3166. text.ReleaseRead;
  3167. Release;
  3168. CursorChanged
  3169. END PageUp;
  3170. PROCEDURE Home*(ctrl, select : BOOLEAN);
  3171. VAR
  3172. lineStart, cl, pos : LONGINT;
  3173. BEGIN
  3174. Acquire;
  3175. text.AcquireRead;
  3176. pos := GetInternalPos(cursor.GetPosition());
  3177. IF select THEN
  3178. KeyStartSelection(pos)
  3179. ELSE
  3180. selection.SetFromTo(pos, pos);
  3181. cursor.SetVisible(TRUE);
  3182. Texts.ClearLastSelection
  3183. END;
  3184. IF ctrl THEN
  3185. cursor.SetPosition(GetDisplayPos(0));
  3186. firstLine.Set(0)
  3187. ELSE
  3188. cl := layout.FindLineNrByPos(cursor.GetPosition());
  3189. lineStart := layout.GetLineStartPos(cl);
  3190. cursor.SetPosition(GetDisplayPos(lineStart));
  3191. END;
  3192. StoreLineEnter;
  3193. IF select THEN
  3194. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3195. END;
  3196. text.ReleaseRead;
  3197. Release;
  3198. CursorChanged
  3199. END Home;
  3200. PROCEDURE End*(ctrl, select : BOOLEAN);
  3201. VAR lineEnd, textLength, cl, pos, dispPos: LONGINT;
  3202. BEGIN
  3203. Acquire;
  3204. text.AcquireRead;
  3205. pos := GetInternalPos(cursor.GetPosition());
  3206. IF select THEN
  3207. KeyStartSelection(pos)
  3208. ELSE
  3209. selection.SetFromTo(pos, pos);
  3210. cursor.SetVisible(TRUE);
  3211. Texts.ClearLastSelection
  3212. END;
  3213. IF ctrl THEN
  3214. textLength := text.GetLength();
  3215. cursor.SetPosition(GetDisplayPos(textLength));
  3216. firstLine.Set(layout.FindLineNrByPos(text.GetLength()))
  3217. ELSE
  3218. cl := layout.FindLineNrByPos(cursor.GetPosition());
  3219. lineEnd := layout.GetLineStartPos(cl) + layout.GetLineLength(cl) - 1;
  3220. dispPos := GetDisplayPos(lineEnd);
  3221. cursor.SetPosition(dispPos);
  3222. END;
  3223. StoreLineEnter;
  3224. IF select THEN
  3225. KeyUpdateSelection(GetInternalPos(cursor.GetPosition()))
  3226. END;
  3227. text.ReleaseRead;
  3228. Release;
  3229. CursorChanged
  3230. END End;
  3231. PROCEDURE KeyEvent*(ucs :LONGINT; flags : SET; VAR keysym : LONGINT);
  3232. BEGIN
  3233. modifierFlags := flags;
  3234. IF Inputs.Release IN flags THEN RETURN END;
  3235. dragCopy := modifierFlags * Inputs.Ctrl # {};
  3236. IF keysym = 01H THEN (* Ctrl-A *)
  3237. SelectAll
  3238. ELSIF keysym = 03H THEN (* Ctrl-C *)
  3239. CopySelection
  3240. ELSIF (keysym = 0FF63H) & (flags * Inputs.Ctrl # {}) THEN (*Ctrl Insert *)
  3241. CopySelection
  3242. ELSIF keysym = 12H THEN (* Ctrl-R *)
  3243. layout.FullLayout(TRUE); Invalidate;CheckNumberOfLines;
  3244. KernelLog.String("Refreshed"); KernelLog.Ln;
  3245. ELSIF keysym = 0FF51H THEN (* Cursor Left *)
  3246. CursorLeft(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  3247. ELSIF keysym = 0FF53H THEN (* Cursor Right *)
  3248. CursorRight(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  3249. ELSIF keysym = 0FF54H THEN (* Cursor Down *)
  3250. CursorDown(flags * Inputs.Shift # {})
  3251. ELSIF keysym = 0FF52H THEN (* Cursor Up *)
  3252. CursorUp(flags * Inputs.Shift # {})
  3253. ELSIF keysym = 0FF56H THEN (* Page Down *)
  3254. PageDown(flags * Inputs.Shift # {})
  3255. ELSIF keysym = 0FF55H THEN (* Page Up *)
  3256. PageUp(flags * Inputs.Shift # {})
  3257. ELSIF keysym = 0FF50H THEN (* Cursor Home *)
  3258. Home(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  3259. ELSIF keysym = 0FF57H THEN (* Cursor End *)
  3260. End(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  3261. END
  3262. END KeyEvent;
  3263. (* called by users that override the KeyEvents to allow copy drag drop *)
  3264. PROCEDURE SetFlags*(flags : SET);
  3265. BEGIN
  3266. modifierFlags := flags;
  3267. dragCopy := modifierFlags * Inputs.Ctrl # {};
  3268. END SetFlags;
  3269. PROCEDURE FindCommandRange*(pos: LONGINT; VAR start, end, nofLastSelections : LONGINT);
  3270. VAR ch : LONGINT; string : ARRAY 23 OF CHAR; i : LONGINT; sDoCommands, lastWasTilde : BOOLEAN;
  3271. escapeString: ARRAY 32 OF LONGINT; escapePos: LONGINT; escape: BOOLEAN;
  3272. (* note: this simple algorithm can be emplyed if the substring to be implicitly searched for does not contain its first character *)
  3273. PROCEDURE String(escape: BOOLEAN; CONST escapeString: ARRAY OF LONGINT);
  3274. VAR done: BOOLEAN; escapePos: LONGINT;
  3275. BEGIN
  3276. done := FALSE; escapePos := -1;
  3277. REPEAT
  3278. utilreader.ReadCh(ch);
  3279. IF ch = ORD('"') THEN
  3280. IF escape THEN
  3281. escapePos := 0;
  3282. ELSE
  3283. done := TRUE
  3284. END;
  3285. ELSIF escapePos >= 0 THEN
  3286. IF escapeString[escapePos] = 0 THEN
  3287. IF ch =ORD("\") THEN done := TRUE
  3288. ELSE escapePos := -1
  3289. END;
  3290. ELSIF escapeString[escapePos] # ch THEN
  3291. escapePos := -1;
  3292. ELSE
  3293. INC(escapePos);
  3294. END;
  3295. END;
  3296. UNTIL done OR utilreader.eot;
  3297. END String;
  3298. BEGIN
  3299. nofLastSelections := 0;
  3300. text.AcquireRead;
  3301. utilreader.SetDirection(-1); utilreader.SetPosition(pos);
  3302. REPEAT utilreader.ReadCh(ch) UNTIL TextUtilities.IsWhiteSpace(ch,text.isUTF) OR utilreader.eot;
  3303. start := utilreader.GetPosition() + 2;
  3304. IF utilreader.eot THEN DEC(start, 2) END;
  3305. (* search ~ *)
  3306. i := 0; sDoCommands := FALSE; lastWasTilde := FALSE;
  3307. utilreader.SetDirection(1); utilreader.SetPosition(start);
  3308. REPEAT
  3309. utilreader.ReadCh(ch);
  3310. IF ch = ORD('"') THEN
  3311. escapeString[escapePos] := 0;
  3312. String(escape, escapeString);
  3313. ELSIF ch =ORD("\") THEN
  3314. escape := TRUE;
  3315. escapePos := 0;
  3316. ELSIF escape THEN
  3317. IF TextUtilities.IsWhiteSpace(ch,text.isUTF) THEN escape := FALSE
  3318. ELSE escapeString[escapePos] := ch; INC(escapePos);
  3319. END;
  3320. END;
  3321. (* check whether the command is SystemTools.DoCommands *)
  3322. IF (i < 22) THEN
  3323. string[i] := CHR(ch);
  3324. INC(i);
  3325. IF (i = 22) THEN
  3326. string[22] := 0X;
  3327. IF (string = "SystemTools.DoCommands") OR Strings.StartsWith2("PreliminaryCommands",string) THEN
  3328. sDoCommands := TRUE;
  3329. END;
  3330. END;
  3331. END;
  3332. IF (CHR(ch) = "^") THEN
  3333. INC(nofLastSelections);
  3334. END;
  3335. (* We do a special treatment of the command SystemTools.DoCommands since we don't want a single
  3336. tilde character to delimit the parameter string for the particular command - but two tilde characters *)
  3337. IF sDoCommands THEN
  3338. IF (ch = ORD("~")) THEN
  3339. IF ~lastWasTilde THEN
  3340. lastWasTilde := TRUE;
  3341. utilreader.ReadCh(ch);
  3342. ELSE
  3343. (* Two tilde characters only separated with whitespace means this is the
  3344. end of the SystemTools.DoCommands parameter string *)
  3345. END;
  3346. ELSIF lastWasTilde & ~TextUtilities.IsWhiteSpace(ch,text.isUTF) THEN
  3347. lastWasTilde := FALSE;
  3348. END;
  3349. END;
  3350. UNTIL (ch = ORD("~")) OR (utilreader.eot);
  3351. end := utilreader.GetPosition() - 1;
  3352. IF utilreader.eot THEN INC(end) END;
  3353. text.ReleaseRead
  3354. END FindCommandRange;
  3355. PROCEDURE FindCommand*(pos: LONGINT; VAR start, end : LONGINT);
  3356. VAR ch : LONGINT;
  3357. BEGIN
  3358. text.AcquireRead;
  3359. utilreader.SetDirection(-1); utilreader.SetPosition(pos);
  3360. REPEAT utilreader.ReadCh(ch) UNTIL TextUtilities.IsWhiteSpace(ch,text.isUTF) OR utilreader.eot;
  3361. start := utilreader.GetPosition() + 2;
  3362. IF utilreader.eot THEN DEC(start, 2) END;
  3363. utilreader.SetDirection(1); utilreader.SetPosition(pos);
  3364. REPEAT utilreader.ReadCh(ch) UNTIL TextUtilities.IsWhiteSpace(ch,text.isUTF) OR utilreader.eot;
  3365. end := utilreader.GetPosition() - 1;
  3366. IF utilreader.eot THEN INC(end) END;
  3367. text.ReleaseRead;
  3368. END FindCommand;
  3369. (** Start the command in the text, starting on pos (or wordboundary before),
  3370. caller should hold lock on text to make the pos stable *)
  3371. PROCEDURE StartCommand*(pos : LONGINT; openFile : BOOLEAN);
  3372. VAR
  3373. start, end, bufSize : LONGINT;
  3374. context : Commands.Context;
  3375. arg : Streams.StringReader;
  3376. command : ARRAY MaxCommandLength OF CHAR;
  3377. parameters : POINTER TO ARRAY OF CHAR;
  3378. s : Strings.String;
  3379. msg : ARRAY 128 OF CHAR;
  3380. ignore : Modules.Name;
  3381. paramSize, nofLastSelections, i, j, a, b, res : LONGINT;
  3382. selectionText : Texts.Text;
  3383. selectionOk : BOOLEAN;
  3384. from, to: Texts.TextPosition;
  3385. commandCaller:OBJECT;
  3386. commandWriter, errorWriter: Streams.Writer;
  3387. BEGIN
  3388. Acquire;
  3389. text.AcquireRead;
  3390. IF openFile THEN FindCommand(pos, start, end)
  3391. ELSE FindCommandRange(pos, start, end, nofLastSelections)
  3392. END;
  3393. bufSize := Strings.Max(Strings.Min((end - start) * 5 + 1 (* for UTF *), MaxCallParameterBuf), 1);
  3394. NEW(s, bufSize);
  3395. paramSize := 0;
  3396. TextUtilities.SubTextToStrAt(text, start, end - start, paramSize, s^);
  3397. INC(paramSize);
  3398. text.ReleaseRead;
  3399. Release;
  3400. IF Inputs.Shift * modifierFlags # {} THEN
  3401. (*
  3402. Command / open will not see the caller => called as if no calling context was specified.
  3403. => Opening a text while holding a shift key down will usually result in a new viewer being opened.
  3404. *)
  3405. commandCaller := NIL
  3406. ELSE
  3407. commandCaller := SELF.commandCaller;
  3408. END;
  3409. IF openFile THEN
  3410. FileHandlers.OpenFile(s^, NIL, commandCaller)
  3411. ELSE
  3412. command := "";
  3413. i := 0;
  3414. WHILE (i < MaxCommandLength) & (s[i] # 0X) & (s[i] # ";") & (s[i] # " ") & (s[i] # 09X) & (s[i] # 0DX) & (s[i] # 0AX) DO
  3415. command[i] := s[i]; INC(i);
  3416. END;
  3417. IF i < MaxCommandLength THEN
  3418. command[i] := 0X;
  3419. INC(i);
  3420. Commands.Split(command, ignore, ignore, res, msg);
  3421. IF res # Commands.Ok THEN
  3422. KernelLog.String("WMTextView: Command parsing error, res: "); KernelLog.Int(res, 0);
  3423. KernelLog.String(" ("); KernelLog.String(msg); KernelLog.String(")"); KernelLog.Ln;
  3424. RETURN;
  3425. END;
  3426. ELSE
  3427. KernelLog.String("WMTextView: Command execution error: Command too long"); KernelLog.Ln;
  3428. RETURN;
  3429. END;
  3430. IF (Inputs.Alt * modifierFlags # {}) THEN
  3431. (* execute AltMMCommand with actual command and its parameters as parameter *)
  3432. COPY(AltMMCommand, command);
  3433. commandWriter := NIL; errorWriter := NIL;
  3434. i := 0;
  3435. ELSE
  3436. commandWriter := SELF.commandWriter;
  3437. errorWriter := SELF.errorWriter;
  3438. END;
  3439. IF (i < LEN(s)) THEN (* copy parameter string *)
  3440. selectionOk := FALSE;
  3441. IF (nofLastSelections > 0) THEN
  3442. IF Texts.GetLastSelection(selectionText, from, to) THEN
  3443. selectionOk := TRUE;
  3444. selectionText.AcquireRead;
  3445. a := Strings.Min(from.GetPosition(), to.GetPosition());
  3446. b := Strings.Max(from.GetPosition(), to.GetPosition());
  3447. INC(paramSize, b - a + 1);
  3448. END;
  3449. END;
  3450. NEW(parameters, paramSize);
  3451. j := 0;
  3452. WHILE (i < LEN(s)) & (j < LEN(parameters)-1) DO
  3453. IF (s[i] = "^") & selectionOk THEN
  3454. TextUtilities.SubTextToStrAt(selectionText, a, b - a, j, parameters^);
  3455. ELSE
  3456. parameters[j] := s[i]; INC(j);
  3457. END;
  3458. INC(i);
  3459. END;
  3460. parameters[j] := 0X;
  3461. IF selectionOk THEN
  3462. selectionText.ReleaseRead;
  3463. END;
  3464. ELSE
  3465. NEW(parameters, 1); parameters[0] := 0X;
  3466. END;
  3467. NEW(arg, LEN(parameters)); arg.SetRaw(parameters^, 0, LEN(parameters));
  3468. NEW(context, NIL, arg, commandWriter, errorWriter, commandCaller);
  3469. IF TraceCommands IN Trace THEN
  3470. KernelLog.String("WMTextView: Executing command: '"); KernelLog.String(command); KernelLog.String("'");
  3471. KernelLog.String(", parameters: ");
  3472. IF (parameters[0] = 0X) THEN KernelLog.String("None"); ELSE KernelLog.String("'"); KernelLog.String(parameters^); KernelLog.String("'"); END;
  3473. KernelLog.Ln;
  3474. END;
  3475. Commands.Activate(command, context, {}, res, msg);
  3476. IF (res # Commands.Ok) THEN
  3477. IF commandWriter # NIL THEN
  3478. commandWriter.String("WMTextView: Command execution error, res: "); commandWriter.Int(res, 0);
  3479. commandWriter.String(" ("); commandWriter.String(msg); commandWriter.String(")"); commandWriter.Ln;
  3480. commandWriter.Update;
  3481. ELSE
  3482. KernelLog.String("WMTextView: Command execution error, res: "); KernelLog.Int(res, 0);
  3483. KernelLog.String(" ("); KernelLog.String(msg); KernelLog.String(")"); KernelLog.Ln;
  3484. END;
  3485. END;
  3486. END;
  3487. END StartCommand;
  3488. PROCEDURE Start(sender, data: ANY);
  3489. VAR msg: ARRAY 512 OF CHAR; res: LONGINT;
  3490. BEGIN
  3491. IF (data # NIL) & (data IS ClickInfo) THEN
  3492. IF data(ClickInfo).cmdPar # NIL THEN
  3493. Commands.Call(data(ClickInfo).cmdPar^, {}, res, msg);
  3494. IF res # 0 THEN KernelLog.String("WMTextView: "); KernelLog.String(msg); KernelLog.Ln END;
  3495. END
  3496. END
  3497. END Start;
  3498. PROCEDURE Open(sender, data: ANY);
  3499. BEGIN
  3500. IF (data # NIL) & (data IS ClickInfo) THEN
  3501. IF data(ClickInfo).cmd # NIL THEN
  3502. FileHandlers.OpenFile(data(ClickInfo).cmd^, NIL, commandCaller)
  3503. END
  3504. END
  3505. END Open;
  3506. PROCEDURE PieMenuStart(sender, data: ANY);
  3507. BEGIN
  3508. Start(piemenu, piemenu.userData)
  3509. END PieMenuStart;
  3510. PROCEDURE PieMenuOpen(sender, data: ANY);
  3511. BEGIN
  3512. Open(piemenu, piemenu.userData)
  3513. END PieMenuOpen;
  3514. PROCEDURE PieMenuCopy(sender, data: ANY);
  3515. BEGIN
  3516. CopySelection;
  3517. END PieMenuCopy;
  3518. PROCEDURE PieMenuPaste(sender, data: ANY);
  3519. BEGIN
  3520. Paste;
  3521. END PieMenuPaste;
  3522. PROCEDURE ShowContextMenu(x, y: LONGINT);
  3523. VAR
  3524. popup : WMPopups.Popup;
  3525. start, end, bufSize : LONGINT;
  3526. command, s : Strings.String;
  3527. clickInfo : ClickInfo;
  3528. str : ARRAY 256 OF CHAR;
  3529. window : WMWindowManager.Window;
  3530. nofLastSelections : LONGINT;
  3531. BEGIN
  3532. ASSERT(IsCallFromSequencer());
  3533. text.AcquireRead;
  3534. FindCommand(cursor.GetPosition(), start, end);
  3535. bufSize := Strings.Max(Strings.Min((end - start) * 5 + 1 (* for UTF *), 4096), 1);
  3536. NEW(command, bufSize);
  3537. TextUtilities.SubTextToStr(text, start, end - start, command^);
  3538. FindCommandRange(cursor.GetPosition(), start, end, nofLastSelections);
  3539. bufSize := Strings.Max(Strings.Min((end - start) * 5 + 1 (* for UTF *), MaxCallParameterBuf), 1);
  3540. NEW(s, bufSize);
  3541. TextUtilities.SubTextToStr(text, start, end - start, s^);
  3542. text.ReleaseRead;
  3543. NEW(clickInfo);
  3544. clickInfo.cmd := command;
  3545. clickInfo.cmdPar := s;
  3546. IF UsePieMenu THEN
  3547. NEW(piemenu); piemenu.SetEnabled({0, 1, 2, 3});
  3548. piemenu.SetText(1, Strings.NewString("Open"));
  3549. piemenu.SetText(3, Strings.NewString("Start"));
  3550. piemenu.SetText(2, Strings.NewString("Copy"));
  3551. piemenu.SetText(0, Strings.NewString("Paste"));
  3552. piemenu.userData := clickInfo;
  3553. piemenu.on1.Add(PieMenuOpen);
  3554. piemenu.on2.Add(PieMenuCopy);
  3555. piemenu.on3.Add(PieMenuStart);
  3556. piemenu.on0.Add(PieMenuPaste);
  3557. manager := WMWindowManager.GetDefaultManager();
  3558. window := manager.GetPositionOwner(x, y);
  3559. IF window = NIL THEN RETURN END;
  3560. Acquire; ToWMCoordinates(x, y, x, y); Release;
  3561. piemenu.Show(window, x, y, FALSE);
  3562. (* TODO: Can't set := NIL, since its used by the button handlers *)
  3563. ELSE
  3564. NEW(popup);
  3565. str := "Start "; Strings.Append(str, command^); popup.AddParButton(str, Start, clickInfo);
  3566. str := "Open "; Strings.Append(str, command^); popup.AddParButton(str, Open, clickInfo);
  3567. Acquire; ToWMCoordinates(x, y, x, y); Release;
  3568. popup.Popup(x, y);
  3569. END
  3570. END ShowContextMenu;
  3571. PROCEDURE HandleInternal(VAR x: WMMessages.Message);
  3572. VAR pos : LONGINT; obj : ANY; link : Texts.Link;
  3573. BEGIN
  3574. ASSERT(IsCallFromSequencer());
  3575. IF (x.msgType = WMMessages.MsgKey) & objHasFocus THEN (* forward KeyMsg *)
  3576. WITH focusObject : WMComponents.VisualComponent DO
  3577. focusObject.Handle(x);
  3578. InvalidateRange(focusPos, focusPos+1)
  3579. END
  3580. ELSIF (x.msgType # WMMessages.MsgKey) & HitObject(x.x, x.y, pos, obj) THEN (* forward Msg *)
  3581. SetFocus; cursor.SetVisible(FALSE);
  3582. IF obj IS WMComponents.VisualComponent THEN
  3583. WITH obj : WMComponents.VisualComponent DO
  3584. (* remove oldObject first *)
  3585. IF (oldObject # NIL) & (oldObject # obj) THEN
  3586. oldObject(WMComponents.VisualComponent).Handle(x);
  3587. InvalidateRange(oldPos, oldPos+1);
  3588. END;
  3589. TransformCoordinates(x.x, x.y, obj); (* transform to obj coords *)
  3590. obj.Handle(x); (* call obj Handle *)
  3591. ChangePointer(obj.GetPointerInfo()); (* change the pointer Image *)
  3592. InvalidateRange(pos, pos+1); (* redraw obj *)
  3593. oldObject := obj; oldPos := pos; (* store last object *)
  3594. (* transfer focus to Object *)
  3595. IF (x.msgType = WMMessages.MsgPointer) & (x.flags * {0, 1, 2} = {0}) THEN
  3596. (* remove old focus first *)
  3597. IF (focusObject # NIL) & (focusObject # obj) THEN
  3598. focusObject(WMComponents.VisualComponent).FocusLost;
  3599. InvalidateRange(focusPos, focusPos+1)
  3600. END;
  3601. objHasFocus := TRUE;
  3602. focusObject := obj; focusPos := pos;
  3603. (* FocusLost *)
  3604. END
  3605. END
  3606. END
  3607. ELSIF (x.msgType = WMMessages.MsgPointer) & HitLink(x.x, x.y, pos, link) THEN (* Link *)
  3608. ChangePointer(manager.pointerLink);
  3609. IF (x.msgSubType = 2) &(oldFlags / x.flags = {CallURLPointer}) THEN LinkClick(link); END;
  3610. oldFlags := x.flags;
  3611. ELSE
  3612. ChangePointer(manager.pointerText); (* change Pointer back *)
  3613. (* transfer focus back to TextView *)
  3614. IF (focusObject # NIL) & (x.msgType = WMMessages.MsgPointer) & (x.flags * {0, 1, 2} = {0}) THEN
  3615. focusObject(WMComponents.VisualComponent).FocusLost;
  3616. objHasFocus := FALSE;
  3617. InvalidateRange(focusPos, focusPos+1);
  3618. FocusReceived;
  3619. focusObject := NIL
  3620. END;
  3621. (* update last Object *)
  3622. IF (oldObject # NIL) & (x.msgType = WMMessages.MsgPointer) THEN
  3623. oldObject(WMComponents.VisualComponent).Handle(x);
  3624. InvalidateRange(oldPos, oldPos+1);
  3625. oldObject := NIL
  3626. END;
  3627. IF (x.msgType = WMMessages.MsgExt) & (x.ext # NIL) & (x.ext IS Texts.StyleChangedMsg) THEN
  3628. layout.FullLayout(TRUE); Invalidate; CheckNumberOfLines;
  3629. ELSE
  3630. HandleInternal^(x);
  3631. END;
  3632. END
  3633. END HandleInternal;
  3634. END TextView;
  3635. TYPE
  3636. FontEntry = OBJECT
  3637. VAR
  3638. name : ARRAY 256 OF CHAR;
  3639. attributes : FontAttributes;
  3640. next : FontEntry;
  3641. PROCEDURE &Init(CONST name : ARRAY OF CHAR);
  3642. BEGIN
  3643. COPY(name, SELF.name);
  3644. attributes := NIL;
  3645. next := NIL;
  3646. END Init;
  3647. END FontEntry;
  3648. FontAttributes = OBJECT
  3649. VAR
  3650. font : WMGraphics.Font; (* { font # {} *)
  3651. size : LONGINT;
  3652. style : SET;
  3653. next : FontAttributes;
  3654. PROCEDURE &Init(size : LONGINT; style : SET);
  3655. BEGIN
  3656. font := NIL;
  3657. SELF.size := size;
  3658. SELF.style := style;
  3659. next := NIL;
  3660. END Init;
  3661. END FontAttributes;
  3662. (* not thread-safe! not global to avoid locking and keep size smaller *)
  3663. FontCache = OBJECT
  3664. VAR
  3665. entries : FontEntry;
  3666. defaultFont : WMGraphics.Font;
  3667. PROCEDURE &Init;
  3668. BEGIN
  3669. NEW(entries, "head"); (* head of list *)
  3670. defaultFont := WMGraphics.GetDefaultFont();
  3671. END Init;
  3672. PROCEDURE Find(CONST name : ARRAY OF CHAR; size : LONGINT; style : SET) : WMGraphics.Font;
  3673. VAR font : WMGraphics.Font; e : FontEntry; a : FontAttributes;
  3674. BEGIN
  3675. font := NIL;
  3676. e := entries.next;
  3677. WHILE (e # NIL ) & (e.name < name) DO e := e.next; END;
  3678. IF (e # NIL) & (e.name = name) THEN
  3679. a := e.attributes;
  3680. WHILE (a # NIL) & (a.size < size) DO a := a.next; END;
  3681. WHILE (a # NIL) & (a.size = size) & (a.style # style) DO a := a.next; END;
  3682. IF (a # NIL) & (a.size = size) THEN
  3683. ASSERT(a.font # NIL);
  3684. font := a.font;
  3685. END;
  3686. END;
  3687. RETURN font;
  3688. END Find;
  3689. PROCEDURE Add(CONST name : ARRAY OF CHAR; size : LONGINT; style : SET) : WMGraphics.Font;
  3690. VAR entry, e : FontEntry; attribute, a : FontAttributes;
  3691. BEGIN
  3692. e := entries;
  3693. WHILE (e.next # NIL) & (e.next.name < name) DO e := e.next; END;
  3694. IF (e.next # NIL) & (e.next.name = name) THEN
  3695. entry := e.next;
  3696. ELSE
  3697. NEW(entry, name);
  3698. entry.next := e.next;
  3699. e.next := entry;
  3700. END;
  3701. ASSERT(entry # NIL);
  3702. NEW(attribute, size, style);
  3703. attribute.font := WMGraphics.GetFont(name, size, style);
  3704. IF (entry.attributes = NIL) THEN
  3705. entry.attributes := attribute;
  3706. ELSIF (entry.attributes.size >= attribute.size) THEN
  3707. attribute.next := entry.attributes;
  3708. entry.attributes := attribute;
  3709. ELSE
  3710. a := entry.attributes;
  3711. WHILE (a.next # NIL) & (a.next.size < attribute.size) DO a := a.next; END;
  3712. attribute.next := a.next;
  3713. a.next := attribute;
  3714. END;
  3715. ASSERT(attribute.font # NIL);
  3716. RETURN attribute.font;
  3717. END Add;
  3718. (* Get specified font. If not available, the system default font is returned *)
  3719. PROCEDURE GetFont(CONST name : ARRAY OF CHAR; size : LONGINT; style : SET) : WMGraphics.Font;
  3720. VAR font : WMGraphics.Font;
  3721. BEGIN
  3722. font := Find(name, size, style);
  3723. IF (font = NIL) THEN
  3724. font := Add(name, size, style);
  3725. END;
  3726. ASSERT(font # NIL);
  3727. RETURN font;
  3728. END GetFont;
  3729. END FontCache;
  3730. VAR
  3731. manager : WMWindowManager.WindowManager;
  3732. cursorBlinker- : CursorBlinker;
  3733. PTVIsMultiLine, PTVIsPassword, PTVShowBorder, PTValwaysShowCursor, PTVShowLabels : WMProperties.BooleanProperty;
  3734. PTVAllowCommandExecution, PTVAllowTextSelection, PTVAllowPiemenu : WMProperties.BooleanProperty;
  3735. PTVWrapMode, PTVMouseWheelScrollSpeed, PVTtextAlignV : WMProperties.Int32Property;
  3736. PTVfirstLine, PTVleftShift, PTVPasswordChar : WMProperties.Int32Property;
  3737. PTVdefaultTextColor, PTVdefaultTextBgColor : WMProperties.ColorProperty;
  3738. PTVborders : WMProperties.RectangleProperty;
  3739. PTVonLinkClick, PTVonLinkClickInfo : Strings.String;
  3740. PTVonCtrlLinkClick, PTVonCtrlLinkClickInfo : Strings.String;
  3741. PTVShowLineNumbers, PTVIndicateTabs : WMProperties.BooleanProperty;
  3742. PTVHighlighting : WMProperties.StringProperty;
  3743. PTVLineNumberColor, PTVLineNumberBgColor, PTVclBgCurrentLine : WMProperties.ColorProperty;
  3744. currentTextView : TextView;
  3745. StrTextView : Strings.String;
  3746. DefaultStyle : POINTER TO RECORD END;
  3747. PROCEDURE Limit(x, min, max : LONGINT) : LONGINT;
  3748. BEGIN
  3749. IF x < min THEN x := min END;
  3750. IF x > max THEN x := max END;
  3751. RETURN x
  3752. END Limit;
  3753. PROCEDURE GetFontFromAttr(info : Texts.FontInfo) : WMGraphics.Font;
  3754. BEGIN
  3755. RETURN WMGraphics.GetFont(info.name, info.size, info.style);
  3756. END GetFontFromAttr;
  3757. PROCEDURE IsSameFont(f1, f2 : WMGraphics.Font) : BOOLEAN;
  3758. BEGIN
  3759. RETURN (f1 = f2) OR ((f1 # NIL) & (f2 # NIL) & (f1.size = f2.size) & (f1.style = f2.style) & (f1.name = f2.name));
  3760. END IsSameFont;
  3761. PROCEDURE CheckFont(style : SyntaxHighlighter.Style; font : WMGraphics.Font; VAR fontCache : FontCache);
  3762. VAR fontname : ARRAY 256 OF CHAR; fontsize : LONGINT; fontstyle : SET;
  3763. BEGIN
  3764. ASSERT(style # NIL);
  3765. IF (fontCache = NIL) THEN NEW(fontCache); END;
  3766. IF (style.defined * SyntaxHighlighter.FontMask = SyntaxHighlighter.FontMask) OR (font = NIL) THEN
  3767. COPY(style.attributes.fontInfo.name, fontname);
  3768. fontsize := style.attributes.fontInfo.size;
  3769. fontstyle := style.attributes.fontInfo.style;
  3770. ELSIF (font # NIL) THEN
  3771. IF (SyntaxHighlighter.FontName IN style.defined) THEN COPY(style.attributes.fontInfo.name, fontname); ELSE COPY(font.name, fontname); END;
  3772. IF (SyntaxHighlighter.FontSize IN style.defined) THEN fontsize := style.attributes.fontInfo.size; ELSE fontsize := font.size; END;
  3773. IF (SyntaxHighlighter.FontStyle IN style.defined) THEN fontstyle := style.attributes.fontInfo.style; ELSE fontstyle := font.style; END;
  3774. END;
  3775. IF (style.attributes.fontInfo.fontcache = NIL) OR ~IsSameFont(style.attributes.fontInfo.fontcache (WMGraphics.Font), font) THEN
  3776. style.attributes.fontInfo.fontcache := fontCache.GetFont(fontname, fontsize, fontstyle);
  3777. END;
  3778. ASSERT(style.attributes.fontInfo.fontcache # NIL);
  3779. END CheckFont;
  3780. PROCEDURE InitStrings;
  3781. BEGIN
  3782. StrTextView := Strings.NewString("TextView");
  3783. PTVonLinkClick := Strings.NewString("Link Click Event");
  3784. PTVonLinkClickInfo := Strings.NewString("fired when a link is pressed");
  3785. PTVonCtrlLinkClick := Strings.NewString("Ctrl Click Event");
  3786. PTVonCtrlLinkClickInfo := Strings.NewString("fired when Ctrl pressend and clicked");
  3787. END InitStrings;
  3788. PROCEDURE InitPrototypes;
  3789. BEGIN
  3790. NEW(PTVIsMultiLine, NIL, Strings.NewString("multiLine"), Strings.NewString("defines if more than one line is visible"));
  3791. PTVIsMultiLine.Set(TRUE);
  3792. NEW(PTVShowBorder, NIL, Strings.NewString("ShowBorder"), Strings.NewString("show border"));
  3793. PTVShowBorder.Set(FALSE);
  3794. NEW(PTVIsPassword, NIL, Strings.NewString("password"),
  3795. Strings.NewString("defines if the view is a password text. Characters are replaced by passwordChar"));
  3796. NEW(PTVPasswordChar, NIL, Strings.NewString("passwordChar"),
  3797. Strings.NewString("character that is the placeholder for a character in a password"));
  3798. PTVPasswordChar.Set(43);
  3799. NEW(PTValwaysShowCursor, NIL, Strings.NewString("alwaysShowCursor"),
  3800. Strings.NewString("set to true, if the cursor should not be hidden when focus is lost"));
  3801. PTValwaysShowCursor.Set(FALSE);
  3802. NEW(PTVShowLabels, NIL, Strings.NewString("ShowLabels"),
  3803. Strings.NewString("set to true, if the labels should be shown in the text"));
  3804. PTVShowLabels.Set(FALSE);
  3805. NEW(PTVMouseWheelScrollSpeed, NIL, Strings.NewString("MouseWheelScrollSpeed"),
  3806. Strings.NewString("Multiplier for mouse wheel, 0 to disable mouse wheel scrolling"));
  3807. PTVMouseWheelScrollSpeed.Set(3);
  3808. NEW(PTVAllowCommandExecution, NIL, Strings.NewString("allowCommandExecution"),
  3809. Strings.NewString("if set to true, middle-clicked words are executed as command"));
  3810. PTVAllowCommandExecution.Set(TRUE);
  3811. NEW(PTVAllowTextSelection, NIL, Strings.NewString("allowTextSelection"),
  3812. Strings.NewString("is the user allowed to select text using the mouse?"));
  3813. PTVAllowTextSelection.Set(TRUE);
  3814. NEW(PTVAllowPiemenu, NIL, Strings.NewString("allowPiemenu"),
  3815. Strings.NewString("if set to true, a mouse right-click opens the pie menu"));
  3816. PTVAllowPiemenu.Set(TRUE);
  3817. NEW(PTVWrapMode, NIL, Strings.NewString("wrapMode"), Strings.NewString("Set text wrapping mode"));
  3818. PTVWrapMode.Set(WrapWord);
  3819. NEW(PTVfirstLine, NIL, Strings.NewString("firstLine"),
  3820. Strings.NewString("the first visible line of text in the view"));
  3821. PTVfirstLine.Set(0);
  3822. NEW(PTVleftShift, NIL, Strings.NewString("leftShift"),
  3823. Strings.NewString("how many pixels the text in the view is shifted to the left"));
  3824. PTVleftShift.Set(0);
  3825. NEW(PTVdefaultTextColor, NIL, Strings.NewString("defaultTextColor"),
  3826. Strings.NewString("the color of a text that does not explicitly specify a color"));
  3827. PTVdefaultTextColor.Set(0FFH);
  3828. NEW(PTVdefaultTextBgColor, NIL, Strings.NewString("defaultTextBgColor"),
  3829. Strings.NewString("The color of a text background if not specified otherwise in the text"));
  3830. PTVdefaultTextBgColor.Set(0);
  3831. NEW(PTVborders, NIL, Strings.NewString("borders"),
  3832. Strings.NewString("spaces from bounds of the component to the text"));
  3833. PTVborders.Set(WMRectangles.MakeRect(5, 5, 5, 5));
  3834. NEW(PTVIndicateTabs, NIL, Strings.NewString("IndicateTabs"), Strings.NewString("Indicate tabs?"));
  3835. PTVIndicateTabs.Set(FALSE);
  3836. NEW(PTVShowLineNumbers, NIL, Strings.NewString("ShowLineNumbers"), Strings.NewString("Show line numbers?"));
  3837. PTVShowLineNumbers.Set(FALSE);
  3838. NEW(PTVLineNumberColor, NIL, Strings.NewString("LineNumberColor"), Strings.NewString("Color of line numbers"));
  3839. PTVLineNumberColor.Set(WMGraphics.Black);
  3840. NEW(PTVLineNumberBgColor, NIL, Strings.NewString("LineNumberBgColor"), Strings.NewString("Background color of line numbers"));
  3841. PTVLineNumberBgColor.Set(0CCCCCCFFH);
  3842. NEW(PTVHighlighting, NIL, Strings.NewString("Highlighting"), Strings.NewString("Name of highlighting to be applied"));
  3843. PTVHighlighting.Set(NIL);
  3844. NEW(PTVclBgCurrentLine, NIL, Strings.NewString("ClBgCurrentLine"), Strings.NewString("Background color of currently edited line"));
  3845. PTVclBgCurrentLine.Set(0);
  3846. NEW(PVTtextAlignV, NIL, Strings.NewString("TextAlignV"), Strings.NewString("vertical Text Alignment"));
  3847. PVTtextAlignV.Set(WMGraphics.AlignTop);
  3848. END InitPrototypes;
  3849. PROCEDURE EnablePiemenu*;
  3850. BEGIN
  3851. PTVAllowPiemenu.Set( TRUE );
  3852. KernelLog.String( "Piemenu enabled" ); KernelLog.Ln
  3853. END EnablePiemenu;
  3854. PROCEDURE DisablePiemenu*;
  3855. BEGIN
  3856. PTVAllowPiemenu.Set( FALSE );
  3857. KernelLog.String( "Piemenu disabled" ); KernelLog.Ln
  3858. END DisablePiemenu;
  3859. PROCEDURE TextViewFactory*() : XML.Element;
  3860. VAR e : TextView;
  3861. BEGIN
  3862. NEW(e); RETURN e
  3863. END TextViewFactory;
  3864. (* Inserts a ocharacter from the outside into the current textView's text at its current position *)
  3865. PROCEDURE InsertChar*(newChar : Char32) : INTEGER;
  3866. BEGIN
  3867. IF currentTextView # NIL THEN
  3868. RETURN currentTextView.InsertChar(newChar);
  3869. ELSE
  3870. RETURN -3;
  3871. END;
  3872. END InsertChar;
  3873. PROCEDURE Refresh*;
  3874. BEGIN
  3875. IF currentTextView # NIL THEN
  3876. currentTextView.layout.FullLayout(TRUE);
  3877. currentTextView.Invalidate;
  3878. currentTextView.CheckNumberOfLines;
  3879. END;
  3880. END Refresh;
  3881. PROCEDURE Cleanup;
  3882. BEGIN
  3883. cursorBlinker.Finalize;
  3884. END Cleanup;
  3885. PROCEDURE GenTextView*(): XML.Element;
  3886. VAR e : TextView;
  3887. BEGIN
  3888. NEW(e); RETURN e
  3889. END GenTextView;
  3890. BEGIN
  3891. NEW(cursorBlinker);
  3892. NEW(DefaultStyle);
  3893. Modules.InstallTermHandler(Cleanup);
  3894. InitStrings;
  3895. InitPrototypes;
  3896. manager := WMWindowManager.GetDefaultManager();
  3897. END WMTextView.