Search code examples
windowsdelphirichedit

How would you render soft returns in a Richedit control?


In an application that displays a richedit control I would like to be able to visually distinguish soft returns (produced with SHIFT ENTER) from hard returns (produced with ENTER).

I already use the JVCL richedit and don't want to switch at that point.

How would you proceed to do that?

Microsoft Word may be an inspiration source, they display a ↵ sign for soft returns and a ¶ sign for hard returns at the end of each line.

I am just looking for hints, good ideas how you would tackle this project. I am not asking anybody to do my work, of course. :-)


Solution

  • Although not a direct answer to your question, there is a possible solution to the problem you mentioned of needing to use both Richedit and Syntax highlighting in one control and that is the use of SynEdit.

    SynEdit include some non-visual components that allow exporting syntax formatted text, one of those components is TSynExporterRTF.

    Suppose you have a section of code which is in plain text inside your richedit and you want to syntax highlight that portion, you could select and copy that text to a TSynEdit and then export it to a TSynExporterRTF which will now contain syntax formatted text (assuming a highlighter has been defined correctly). Then you can simply write the data to a TMemoryStream and replace the selected richedit text with the now syntax formatted code.

    To do this you can try something like this:

    procedure SyntaxFormatRichEditText(RichEdit: TRichEdit; SynHighlighter: TSynCustomHighlighter);
    var
      SynEdit: TSynEdit;
      SynExporterRTF: TSynExporterRTF;
      MS: TMemoryStream;
    begin
      SynEdit := TSynEdit.Create(nil);
      try
        SynEdit.Highlighter := SynHighlighter;
        SynEdit.Lines.Text := RichEdit.SelText;
    
        SynExporterRTF := TSynExporterRTF.Create(nil);
        try
          SynExporterRTF.Highlighter := SynHighlighter;
    
          MS := TMemoryStream.Create;
          try
            SynExporterRTF.ExportAll(SynEdit.Lines);
            SynExporterRTF.SaveToStream(MS);
            MS.Seek(0, soBeginning);
            RichEdit.SetSelTextBuf(MS.Memory);
            RichEdit.SetFocus;
          finally
            MS.Free;
          end;
        finally
          SynExporterRTF.Free;
        end;
      finally
        SynEdit.Free;
      end;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      SyntaxFormatRichEditText(RichEdit1, SynPasSyn1);
    end;
    

    If anything though, as others have suggested the requirements you need are likely out of scope as to what the Richedit controls can offer.