How to make a plain text editor

<< Click to display table of contents >>

How to make a plain text editor

How to make a plain text editor based on TRichViewEdit

Exclude all but text from AcceptDragDropFormat property.

Process OnPaste event (see below).

Disable all UI commands changing text and paragraph attributes.

procedure TMyForm.MyRichViewEditPaste(Sender: TCustomRichViewEdit; 

  var DoDefault: Boolean); 

var s: String

begin 

  if Clipboard.HasFormat(CF_TEXT) the

  begin 

    s := Clipboard.AsText; 

    Sender.InsertText(s, False);

  end

  DoDefault := False; 

end

How to make ONE LINE plain text editor

Exclude all but text from AcceptDragDropFormat property.

Process OnPaste event (see below).

Process OnOleDrop event (see below).

Disable all UI commands changing text and paragraph attributes or inserting multiline text.

Set WordWrap property = False.

Include rvoDoNotWantReturns in EditorOptions.

Set VSmallStep property = 1, before calling Format.

If you do not want tabulators as well, set RVStyle.SpacesInTab = 4.

uses Clipbrd, ActiveX; 

// Returning one line string made from s. 

// This function replaces all line breaks with ' | '. 

// You can change this function, for example to return the first line 

function MakeOneLineString(const s: String): String

var p: Integer; 

begin 

  Result := AdjustLineBreaks(s); 

  while True do begin 

    p := Pos(#13#10, Result); 

    if p=0 then 

      break; 

    Delete(Result, p, 2); 

    Insert(' | ', Result, p); 

 end

end

 

procedure TMyForm.MyRichViewEditOleDrop(Sender: TCustomRichView

  const DataObject: IDataObject; Shift: TShiftState; X, Y: Integer; 

  PossibleDropEffects: TRVOleDropEffects;

  var DropEffect: TRVOleDropEffectvar DoDefault: Boolean); 

var FmtEtc: TFormatEtc; 

  StgMedium: TStgMedium; 

  ptr: Pointer; 

  s: String

  p, ByteCount, CharCount: Integer; 

begin 

  DoDefault := False; 

 

  FillChar(StgMedium, sizeof(StgMedium), 0); 

  FillChar(FmtEtc, sizeof(FmtEtc), 0); 

  FmtEtc.cfFormat := CF_TEXT; // CF_UNICODETEXT for Delphi 2009+ 

  FmtEtc.dwAspect := DVASPECT_CONTENT; 

  FmtEtc.lindex := -1

  FmtEtc.tymed := TYMED_HGLOBAL; 

  if DataObject.GetData(FmtEtc, StgMedium)<>S_OK then 

    exit; 

  if StgMedium.tymed=TYMED_HGLOBAL then

  begin 

    ptr := GlobalLock(StgMedium.HGlobal); 

    try 

      ByteCount := GlobalSize(StgMedium.HGlobal);

      CharCount := ByteCount;

      // CharCount := CharCount div 2// uncomment for Delphi 2009+

      SetLength(s, CharCount); 

      Move(ptr^, PChar(s)^, ByteCount); 

    finally 

      GlobalUnlock(StgMedium.HGlobal); 

    end

    p := Pos(#0, s); 

    if p>0 then 

      s := Copy(s, 1, p-1); 

    MyRichViewEdit.InsertText(MakeOneLineString(s), False);

  end

  ReleaseStgMedium(StgMedium); 

end

 

procedure TMyForm.MyRichViewEditPaste(Sender: TCustomRichViewEdit

  var DoDefault: Boolean); 

var s: String

begin 

  if Clipboard.HasFormat(CF_TEXT) then

  begin 

    s := Clipboard.AsText; 

    Sender.InsertText(MakeOneLineString(s), False);

  end

  DoDefault := False; 

end