[Example] How to paste RVF without images

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
Sergey Tkachenko
Site Admin
Posts: 17521
Joined: Sat Aug 27, 2005 10:28 am
Contact:

[Example] How to paste RVF without images

Post by Sergey Tkachenko »

Code: Select all

uses CRVData, RVMarker, RVItem;

// Removing all images from RVData
procedure RemoveImages(RVData: TCustomRVData);
var i,r,c: Integer;
    table: TRVTableItemInfo;

    {..............................................}
    procedure DeleteItem(i: Integer);
    var SameAsPrev, BR, PageBreak: Boolean;
    begin
      SameAsPrev := RVData.GetItem(i).SameAsPrev;
      BR         := RVData.GetItem(i).BR;
      PageBreak  := RVData.GetItem(i).PageBreakBefore;
      RVData.DeleteItems(i, 1);
      if (i<RVData.ItemCount) and RVData.GetItem(i).SameAsPrev then begin
        RVData.GetItem(i).SameAsPrev := SameAsPrev;
        RVData.GetItem(i).BR := BR;
        RVData.GetItem(i).PageBreakBefore := PageBreak;
      end;
    end;
    {..............................................}
    function ListHasImages(ListInfo: TRVListInfo): Boolean;
    var i: Integer;
    begin
      Result := True;
      for i := 0 to ListInfo.Levels.Count-1 do
        if not ListInfo.Levels[i].UsesFont then
          exit;
      Result := False;
    end;

begin
  for i := RVData.ItemCount-1 downto 0 do
    case RVData.GetItemStyle(i) of
      rvsPicture, rvsHotPicture, rvsBullet, rvsHotspot:
        DeleteItem(i);
      rvsListMarker:
        begin
          r := TRVMarkerItemInfo(RVData.GetItem(i)).ListNo;
          if (r>=0) and (r<=RVData.GetRVStyle.ListStyles.Count) and
            ListHasImages(RVData.GetRVStyle.ListStyles[r]) then
            DeleteItem(i);
        end;
      rvsTable:
        begin
          Table := TRVTableItemInfo(RVData.GetItem(i));
          Table.State := Table.State - [rvtsInserted];
          Table.BackgroundImageFileName :=  '';
          Table.BackgroundImage := nil;
          Table.BackgroundStyle := rvbsColor;
          Table.State := Table.State + [rvtsInserted];
          for r := 0 to Table.RowCount-1 do
            for c := 0 to Table.ColCount-1 do
              if Table.Cells[r,c]<>nil then begin
                Table.Cells[r,c].BackgroundImageFileName :=  '';
                Table.Cells[r,c].BackgroundImage := nil;
                Table.Cells[r,c].BackgroundStyle := rvbsColor;
                RemoveImages(Table.Cells[r,c].GetRVData);
              end;
        end;
    end;
end;

// pasting RVF without images in rve
procedure PasteRVFWithoutImages(rve: TCustomRichViewEdit);
var tmprve: TRichViewEdit;
    Stream: TMemoryStream;
begin
  if rve.ReadOnly or not rve.CanPasteRVF then
    exit;
  tmprve := TRichViewEdit.Create(nil);
  try
    tmprve.Visible := False;
    tmprve.Parent := rve.Parent;
    tmprve.AssignEvents(rve);
    tmprve.RVFOptions := rve.RVFOptions-[rvfoSaveTextStyles, rvfoSaveParaStyles];
    tmprve.RVFParaStylesReadMode := rve.RVFParaStylesReadMode;
    tmprve.RVFTextStylesReadMode := rve.RVFTextStylesReadMode;
    tmprve.Style := rve.Style;
    tmprve.PasteRVF;
    RemoveImages(tmprve.RVData);
    Stream := TMemoryStream.Create;
    try
      tmprve.SaveRVFToStream(Stream, False);
      Stream.Position := 0;
      rve.InsertRVFFromStreamEd(Stream);
    finally
      Stream.Free;
    end;
  finally
    tmprve.Free;
  end;
end;

// Assign this code to OnPaste event:
procedure TForm3.RichViewEdit1Paste(Sender: TCustomRichViewEdit;
  var DoDefault: Boolean);
begin
  if RichViewEdit1.CanPasteRVF then begin
    PasteRVFWithoutImages(RichViewEdit1);
    DoDefault := False;
  end;
end;
Sergey Tkachenko
Site Admin
Posts: 17521
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Images can appear when pasting or as a result of drag&drop in the following formats: RVF, RTF, bitmap, metafile, graphic files.

How to prevent images from appearing

Solution for RVF:
Pasting: use the code above.
D&D: exclude rddRVF from RichViewEdit1.AcceptDragDropFormats property.

Solution for RTF:
Assign RichViewEdit1.RTFReadProperties.IgnorePictures := True

Other formats:
Pasting: in the next update of TRichView, we will add a new property AcceptPasteFormats where you can exclude rvddBitmap, rvddMetafile, rvddFiles. In the current version of TRichView you can use OnPaste:

Code: Select all

procedure TForm3.RichViewEdit1Paste(Sender: TCustomRichViewEdit;
  var DoDefault: Boolean);
begin
  if RichViewEdit1.CanPasteRVF then
    PasteRVFWithoutImages(RichViewEdit1)
  else if RichViewEdit1.CanPasteRTF then
    RichViewEdit1.PasteRTF
  else if Clipboard.HasFormat(CF_UNICODETEXT) or Clipboard.HasFormat(CF_TEXT) then
    RichViewEdit1.PasteText;
  DoDefault := False;
end;
D&D: exclude rvddBitmap, rvddMetafile, rvddFiles from RichViewEdit1.AcceptDragDropFormats property.

See also: How to make a plain text editor
Post Reply