Hacking a TMemo component lazarus / 8-Other ---------------------------------------------------------------------- typo · 2010-10-03 12:31 Just for fun. Code: [Select]var   cc :TControlCanvas;   i, l :integer;   s, a :string;   MaxWidth :integer; begin   MyMemo.Lines.BeginUpdate;   MyMemo.Text := Trim(MyMemo.Text);   s := MyMemo.Text;   while pos('  ', s) > 0 do     s := AnsiReplaceStr(s, '  ', ' ');   MyMemo.Text := s;   Application.ProcessMessages;   cc := TControlCanvas.Create;   try     cc.Control := MyMemo;     cc.Font := MyMemo.Font;     {     // WrapText standard function seems to wrap only non-quoted strings, so     // you need to replace quotation characters on string in order to use it.     // You can use MyWrapText function instead. See it below in this thread.     if MyMemo.WordWrap = False then     begin       MaxWidth := cc.TextWidth(MyMemo.Text);       s := WrapText(MyMemo.Text, (15 * MyMemo.Width div 20 ) div (MaxWidth div length(MyMemo.Text))  );       MyMemo.Text := s;       Application.ProcessMessages;     end;     }     MaxWidth := 0;     for i := 0 to MyMemo.Lines.Count - 1 do     begin       if cc.TextWidth(MyMemo.lines[i]) > MaxWidth then         MaxWidth := cc.TextWidth(MyMemo.lines[i]);     end;     for i := 0 to MyMemo.Lines.Count - 2 do     begin          if (Trim(MyMemo.Lines[i]) = '') or (Trim(MyMemo.Lines[i + 1]) = '')         or (pos(' ', Trim(MyMemo.Lines[i])) = 0) then       Continue;       s := Trim(MyMemo.Lines[i]);       l := Length(s);       a := ' ';       while cc.TextWidth(Trim(MyMemo.Lines[i])) <= MaxWidth - cc.TextWidth(' ') do       begin                s := Trim(MyMemo.Lines[i]);         l := RPosEx(a, s, l - 1);         if l > 0 then System.Insert(' ', s, l)         else         begin           l := Length(s);           a := a + ' ';         end;         MyMemo.Lines[i] := Trim(s);       end;     end;   finally     cc.Free;   end;   MyMemo.Lines.EndUpdate;   MyMemo.SelStart := 0; end; ---------------------------------------------------------------------- fabienwang · 2010-10-03 12:58 where do we put this code? onFormCreate? ---------------------------------------------------------------------- typo · 2010-10-03 13:01 It can be on a button click event; ---------------------------------------------------------------------- fabienwang · 2010-10-03 13:19 AnsiReplaceStr is unknown, i'm using SVN version ---------------------------------------------------------------------- typo · 2010-10-03 13:20 Uses StrUtils. ---------------------------------------------------------------------- fabienwang · 2010-10-03 13:35 AHAHAH Justifying the text, nice ---------------------------------------------------------------------- Dibo · 2010-10-03 22:34 Hm, nothing happens on linux (GTK2) ---------------------------------------------------------------------- typo · 2010-10-04 03:42 I have tested it to Verdana, Courier, Arial and Times New Roman fonts on Windows. ---------------------------------------------------------------------- JuhaManninen · 2010-10-04 08:15 Quote from: Dibo on October 04, 2010, 12:34:24 amHm, nothing happens on linux (GTK2)  You must enter many lines. Hit enter between the lines. Cool! I don't even understand how the code does it. Must debug it later. Juha ---------------------------------------------------------------------- typo · 2010-10-07 10:48 It adds spaces to each line starting from the right. After measuring the longest line. Should work even for 2 lines, if the last one is the longest one. In fact, it needs the text to be wordwrapped, but it should work if you break the lines manually, although they will match the longest one and not necessarily the border of the control. This code could help: Code: [Select]function MyWrapText(Text :string; MaxCol :integer): string; const   NewLine = #13#10;   LastNewLineChar = #10; var   i, j :integer;   s, ss :string; begin   Result := Text;   if Length(Text) <= MaxCol then Exit;   ss := '';   repeat     // separates paragraphs     s := Copy2SymbDel(Text, LastNewLineChar);     s := Trim(s);     j := 0;     // here S contains 1 paragraph     while j < Length(s) do     begin       inc(j);       // finds the MaxCol-multiple character on the string       if j mod MaxCol = 0 then       begin         i := j;         // steps back until the space character         while s[i] <> ' ' do           Dec(i);         // inserts line end         if Pos(' ', s) > 0 then           System.Insert(NewLine, s, i + 1)         else System.Insert(NewLine, s, j)       end;     end;     ss := ss + s + NewLine;   until Text = '';   Result := Trim(ss); end;                    On Linux you should replace the LineEnd characters.