[Solved] Memo on wordwarp event lazarus / 31-LCL ---------------------------------------------------------------------- lazdeveloper · 2020-04-16 17:59 Hi guys Is it possible to know how many times a text line has warped in the memo component? Is it possible to catch this? Or alternatively can a notify event onwordwarp be implemented and how? Many thanks ---------------------------------------------------------------------- jamie · 2020-04-16 22:30 Do you really need to use a Tmemo for this ? There are other options... Like using a TsynEdit maybe or just dump it to a TScrollBox and handle the painting yourself. ---------------------------------------------------------------------- winni · 2020-04-16 22:54 Hi! Tell Scotty to set the Word Warp to eight and a half. But besides that Enterprise stuff: Don't use TMemo with WordWrap. You got nothing but trouble. This is not a text editor with all abilities it should have. This is a missdesigned component since Windows 3.1 If you realy  need TMemo then turn the WordWrap off. Now you got an editor similar to a listbox. If you realy need something for editing text then look at Jamies message. Winni ---------------------------------------------------------------------- jamie · 2020-04-16 23:10 There Is a way of doing it but realty do we want to ?  You need to basically scan the buffer and determine for each line if it will wrap... and that of course will be done considering TAB chars etc.. need to know where it will line wrap. ---------------------------------------------------------------------- winni · 2020-04-16 23:30 Yes - a long way. Or the other way round: Set the caret with selstart. Get the caretpos. With the help of the font.height you get the line number. Look in the Memo.lines where the text at the caret position appears. If nothing helps: Count the chars. The difference of the lines of the caret position and the Memo.lines is the number of the wordwraps between zero and the actual position. I did that when Delphi was new. Never touched Memo since then - only for reasons the name includes: Memo Winni ---------------------------------------------------------------------- lazdeveloper · 2020-04-17 19:01 My view or say what I am looking for is to find a way inside the wordwarp procedure itself. Let's put it this way: When we set the property wordwarp to true then there is some code that works and get the text warped perfectly. Where is this code? Once we locate this code, then it may be possible to add a new property Say CountOfLinesWraped of integer type that is set to how many times the text wraped and it can be simply read at any time. ---------------------------------------------------------------------- winni · 2020-04-17 19:28 Hi! This code is inside the Memos of the different Widget Sets. It is not the job of the LCL to do the WordWrap. Winni ---------------------------------------------------------------------- lazdeveloper · 2020-04-17 19:44 Does it mean the the wordwarpping code is dispersed  in many places? If that so then it seems a hard job to be done. ---------------------------------------------------------------------- winni · 2020-04-17 19:51 Hi! Even harder: In case of Windows you have to ask MicroSoft if they give you their code of the Memo. I don't think they will do that. Winni ---------------------------------------------------------------------- lucamar · 2020-04-17 20:23 Quote from: lazdeveloper on April 17, 2020, 09:44:01 pmDoes it mean the the wordwarpping code is dispersed  in many places? It means that it's not implemented by FPC/Lazarus but by whoever is responsible for the underlying widget set: Apple in the case of Cocoa, Microsoft for Win32/64, the "gnomes" for GTK, etc. FPC/Lazarus just interface with the system libs/APIs ---------------------------------------------------------------------- lazdeveloper · 2020-04-17 20:36 Oh yes now got it. I thought that Memo is  pure LCL. Now I know why there are similar questions in different forums a round that. Well, then it has to be done manually by stopping the wordwarp property and implementing it one more time. Thank you all. ---------------------------------------------------------------------- jamie · 2020-04-18 00:29 The memo is paint by the OS directly,.. it Is responsible for that mess... Why not turn off WORD WRAP ? or just use a ListBox size by side ? ---------------------------------------------------------------------- winni · 2020-04-18 00:53 Hi! Yes - I said that in message #3. Winni ---------------------------------------------------------------------- lazdeveloper · 2020-04-18 09:25 Well, I ought to use Memo since it is a component of Fortes Report available via OPM. Unfortunately,  Fortes does not have Listbox. ---------------------------------------------------------------------- jamie · 2020-04-18 11:35 I think for what ever reason it must be my age or maybe early stages of dementia setting in because I am getting lost here !  The Tmemo has a Lines Property which is basically a TStringList in nature.. If you use dual TlistBoxes instead you can still use Tmemo if the report refuses to use any other controls but hide the memos and simply assign the lines of the TListBox from the Memo...   For example  Use an event of the Tmemo to show activity to the memo and inside that event do this..    ListBox.Items.Assign(Memo.Lines); Now all content will be inside the TlistBox as well for the Tmemo, just don't show the memo. ---------------------------------------------------------------------- lazdeveloper · 2020-04-18 11:44 I guess there a point lost here. Fortes report offeres it's own visual components that you can see and use on the main report component. Given this, simply you cannot use listbox on the report since it will take you to another unexpected behaviour since listbox is not one of the report set. Please have a look at Fortes report to see the point further. Cheers ---------------------------------------------------------------------- jamie · 2020-04-18 11:59 No, you can still use a Tmemo but you don't  understand what I laid out so I'll just withdraw from this and hope one day  you'll see the light from where you sit. Maybe someone else can help you. ---------------------------------------------------------------------- winni · 2020-04-18 12:31 Hi! Again and again and again: If you want to have control about the line numbers then disable WordWrap Or use another component. Winni ---------------------------------------------------------------------- lucamar · 2020-04-18 14:41 I think the OP's isn't talking about TMemo but about TFRMemo, the similar control from Fortes Report. To the OP: I don't how similar they are to each other but if FR's derives from the standard one then everything said here applies: unless the folks at Fortes have done something to correct it, in some platforms Lines.Count return the number of visible lines rather than the number of "real" lines (paragraphs, if you will); and worse: that behaviour is platform dependent, so programs compiled for, say, Windows vs. Linux behave differently. And because (again, unless a workaround is applied in the derived class) it's implemented by the OS there is no easy, cross-platform way to add a callback for each line that gets wrapped. ---------------------------------------------------------------------- lazdeveloper · 2020-04-18 15:09 Thank you all. Lucamar you are right.. the TRLMemo of fortes is implemented in RLRepot.pas at https://github.com/fortesinformatica/fortesreport- ce/blob/master/Source/RLReport.pas Ofcourse it inherits from an ancesstor class. However if you look at the implementation of the function RunMemo taken from the RLReport.pas given up Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure RunMemo(const Buffer: string; Canvas: TObject;  Alignment: TRLTextAlignment; const ARect: TRect; MaxWidth, MaxHeight: Integer;  var Size: TPoint);var  LineHeight: Integer;  LineWidth: Integer;  LineOffset: Integer;  Pos: Integer;  Wrapped: Boolean;  TextLn: string;  Aux: TRect;  LineAlign: TRLTextAlignment;begin  Size.X := 0;  LineHeight := CanvasTextHeight(Canvas, 'A');  LineOffset := 0;  Pos := 1;  while Pos <= Length(Buffer) do  begin    TextLn := NextLine(Buffer, Pos, Wrapped, LineWidth, Canvas, MaxWidth);    if LineWidth > Size.X then      Size.X := LineWidth;    Aux := Rect(ARect.Left + 0, ARect.Top + LineOffset, ARect.Left +      MaxWidth, ARect.Top + LineOffset + LineHeight);    if Aux.Bottom > ARect.Bottom then      Aux.Bottom := ARect.Bottom;    LineAlign := Alignment;    if (LineAlign = taJustify) and not Wrapped then      LineAlign := taLeftJustify;    CanvasTextRect(Canvas, Aux, TextLn, LineAlign);    Inc(LineOffset, LineHeight);    if LineOffset > MaxHeight then      Break;  end;  Size.Y := LineOffset;end;  Here I am trying to find a footstep to get the lines that are wrapped ---------------------------------------------------------------------- lucamar · 2020-04-18 16:30 It's difficult to say without studying more carefully the whole implementation but I making the marked additions should probably do (almost) what you want: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TWrapLineEvent = procedure(const Position: Integer); procedure RunMemo(const Buffer: string; Canvas: TObject;  Alignment: TRLTextAlignment; const ARect: TRect; MaxWidth, MaxHeight: Integer;  var Size: TPoint;  OnWrapLine: TWrapLineEvent = nil);var  LineHeight: Integer;  LineWidth: Integer;  LineOffset: Integer;  Pos: Integer;  Wrapped: Boolean;  TextLn: string;  Aux: TRect;  LineAlign: TRLTextAlignment;begin  Size.X := 0;  LineHeight := CanvasTextHeight(Canvas, 'A');  LineOffset := 0;  Pos := 1;  while Pos <= Length(Buffer) do  begin    TextLn := NextLine(Buffer, Pos, Wrapped, LineWidth, Canvas, MaxWidth);    if Wrapped and Assigned(OnWrapLine) then       OnWrapLine(Pos);    if LineWidth > Size.X then      Size.X := LineWidth;    Aux := Rect(ARect.Left + 0, ARect.Top + LineOffset, ARect.Left +      MaxWidth, ARect.Top + LineOffset + LineHeight);    if Aux.Bottom > ARect.Bottom then      Aux.Bottom := ARect.Bottom;    LineAlign := Alignment;    if (LineAlign = taJustify) and not Wrapped then      LineAlign := taLeftJustify;    CanvasTextRect(Canvas, Aux, TextLn, LineAlign);    Inc(LineOffset, LineHeight);    if LineOffset > MaxHeight then      Break;  end;  Size.Y := LineOffset;end; ---------------------------------------------------------------------- lazdeveloper · 2020-04-18 18:31 Cheers lucamar. Will try it out. Many thanks ---------------------------------------------------------------------- lazdeveloper · 2020-04-19 23:33 Finally the solution was to call the lines.count method Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i I:= Memo1.lines.count;// you must have the form that the memo on it visible for the method to work // or else it will give you wrong value  It seems that there are some issues with this method see for example https://forum.lazarus.freepascal.org/index.php?topic=26914.0 And this as well https://forum.lazarus.freepascal.org/index.php/topic,25763.0.html However, under win10 it works perfect. Thank you all for your contributions. ---------------------------------------------------------------------- jamie · 2020-04-20 00:01 There is a way to determine if the Text has wrapped on the screen.. What you need to do is scan the screen vertically using two windows control messages.. EM_CHARFROMPOS;     That returns the Index of a character within the buffer using the X,Y plot on the memo's canvas. From that, you do this. EM_LINEFROMCHAR; That will return the line number. So lets say the font size you are using requires 16 pixels per line, now you know you need to increment 16 times per line number that gives you the Y axes.. X would be 0 I guess.  So if you have lets say line 1 and 2 displaying the same line internally because it got wrapped around the end results will report line 2 as line 1 . this is how you can determine if the lines are wrapped.  Its a two step process using two messages via the SendMessage(Memo.Handle, WM_CHARFROMPOS,0 @POintStructureHoldingTheXandY); The return is the location in the buffer and from that value you can get the line number.. etc..  I could make up a working example... ---------------------------------------------------------------------- jamie · 2020-04-20 01:19 This little example will report to you the actual internal line number in the buffer when you move your mouse over the memo. so if you have lines that are wrapping this will report the actual line number they belong to in the buffer. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Memo1MouseMove(Sender: TObject; Shift: TShiftState; X,  Y: Integer);var  CSP,L,R:Integer;  S:String;begin  WIth Tmemo(Sender)do   Begin     R := SendMessage(Handle, EM_CHARFROMPOS,0,MAKELPARAM(X,Y));     S := Lines.Text;     L := 1;   CSP:= 0;     {Character index in buffer sits in the lower WORD}     IF SmallInt(R) <> -1 Then     While (S[L]<>#0)and(L< WOrd(R)) do     Begin       If S[L] in [#13,#10] Then {Skip Line endings}        Begin          Inc(CSP); // Line number count;          If (S[L]<> S[L+1])And(S[L+1] in [#13,#10]) Then inc(L);        end;         Inc(L);     End;     Form1.Caption := 'Line Numner :'+CSP.Tostring;   end;   end;   You can all this directly via a method supplying a X, Y to determine which lines on the memo are showing which lines in the buffer.  The Y axes can be multiplied times the height of  your text// Use the Canvas for that. Y := LineNUmberOFInterest*Canvas.TextHeight('H'); X could be 0; But first you can see how the values resolve using the mouse.. Make sure you have some long lines in there so you can get wraps