trichview.com Forum Index trichview.com
TRichView support forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Example] Converting table to text

 
Post new topic   Reply to topic    trichview.com Forum Index -> Examples, Demos
View previous topic :: View next topic  
Author Message
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6576

PostPosted: Sat Jan 28, 2006 10:44 pm    Post subject: [Example] Converting table to text Reply with quote

This example "removes" the table at the caret position without removing its content:

Code:
procedure ConvertTableToText(RichViewEdit: TCustomRichViewEdit);
var table: TRVTableItemInfo;
    rv: TRichView;
    rve: TCustomRichViewEdit;
    Stream: TMemoryStream;
    ItemNo,r,c: Integer;
begin
  if not RichViewEdit.GetCurrentItemEx(TRVTableItemInfo, rve,
TCustomRVItemInfo(table)) then
    exit;
  rv := TRichView.Create(nil);
  try
    rv.Visible := False;
    rv.Parent := RichViewEdit.Parent;
    rv.Style := RichViewEdit.Style;
    rv.RVFTextStylesReadMode := rvf_sIgnore;
    rv.RVFParaStylesReadMode := rvf_sIgnore;
    for r := 0 to table.Rows.Count-1 do
      for c := 0 to table.Rows[r].Count-1 do
        if table.Cells[r,c]<>nil then begin
          Stream := TMemoryStream.Create;
          try
            table.Cells[r,c].GetRVData.SaveRVFToStream(Stream, False, clNone, nil, nil);
            Stream.Position := 0;
            rv.InsertRVFFromStream(Stream, rv.ItemCount)
          finally
            Stream.Free;
          end;
        end;
    ItemNo := table.GetMyItemNo;
    Stream := TMemoryStream.Create;
    try
      rv.SaveRVFToStream(Stream, False);
      Stream.Position := 0;
      rve.SetSelectionBounds(ItemNo, 0, ItemNo, 1);
      rve.InsertRVFFromStreamEd(Stream);
    finally
      Stream.Free;
    end;
  finally
    rv.Free;
  end;
end;


The same for C++Builder:

Code:
void ConvertTableToText(TCustomRichViewEdit* RichViewEdit)
{
  TCustomRVItemInfo   *item;
  TCustomRichViewEdit *rve;
  if (! RichViewEdit->GetCurrentItemEx(__classid(TRVTableItemInfo), rve,
item))
    return;
  TRVTableItemInfo*table = (TRVTableItemInfo*)item;
  TRichView* rv = new TRichView((TComponent*)NULL);
  rv->Visible = false;
  rv->Parent  = RichViewEdit->Parent;
  rv->Style   = RichViewEdit->Style;
  rv->RVFTextStylesReadMode = rvf_sIgnore;
  rv->RVFParaStylesReadMode = rvf_sIgnore;
  for (int r=0; r<table->Rows->Count; r++)
    for (int c=0; c<table->Rows->Items[r]->Count; c++)
      if (table->Cells[r][c])
      {
        TMemoryStream*Stream = new TMemoryStream;
        table->Cells[r][c]->GetRVData()->SaveRVFToStream(Stream, false,
          clNone, NULL, NULL);
        Stream->Position = 0;
        rv->InsertRVFFromStream(Stream, rv->ItemCount);
        delete Stream;
      }
  int ItemNo = table->GetMyItemNo();
  TMemoryStream*Stream = new TMemoryStream;
  rv->SaveRVFToStream(Stream, false);
  Stream->Position = 0;
  rve->SetSelectionBounds(ItemNo, 0, ItemNo, 1);
  rve->InsertRVFFromStreamEd(Stream);
  delete Stream;
  delete rv;
}
Back to top
View user's profile Send private message Visit poster's website
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6576

PostPosted: Thu Oct 25, 2007 10:25 am    Post subject: Reply with quote

This version of ConvertTableToText inserts ';' instead of line breaks between table cells (line breaks are inserted between rows).
It has a new parameter: EqualDelimCount.
If it is True, the same number (table.ColCount-1) of semicolons will be inserted for each row.
If it is False, semicolons will not be used for horizontally merged cells.

For example, for this table:

EqualDelimCount=False:
Quote:
1;2;3
4;5
;6

EqualDelimCount=True:
Quote:
1;2;3
4;;5
;;6

Code:

uses RVNormalize; // from RichViewActions

procedure ConvertTableToText(RichViewEdit: TCustomRichViewEdit; EqualDelimCount: Boolean);
var table: TRVTableItemInfo;
    rv: TRichView;
    rve: TCustomRichViewEdit;
    Stream: TMemoryStream;
    ItemNo,r,c, mr, mc: Integer;
    NewLine: Boolean;
begin
  if not RichViewEdit.GetCurrentItemEx(TRVTableItemInfo, rve,
    TCustomRVItemInfo(table)) then
    exit;
  rv := TRichView.Create(nil);
  try
    rv.Visible := False;
    rv.Parent := RichViewEdit.Parent;
    rv.Style := RichViewEdit.Style;
    rv.RVFTextStylesReadMode := rvf_sIgnore;
    rv.RVFParaStylesReadMode := rvf_sIgnore;
    for r := 0 to table.RowCount-1 do begin
      NewLine := True;
      for c := 0 to table.ColCount-1 do begin
        if (c<>0) then begin
          if EqualDelimCount then
            if NewLine then
              rv.AddNLATag(';',0, 0,0)
            else
              rv.AddNLATag(';',0,-1,0)
          else begin
            table.Rows.GetMainCell(r, c, mr, mc);
            if mc=c then
              if NewLine then
                rv.AddNLATag(';',0, 0,0)
              else
                rv.AddNLATag(';',0,-1,0)
            else
              if NewLine then
                rv.AddNLATag('',0, 0,0);
          end;
          NewLine := False;
        end;
        if table.Cells[r,c]<>nil then begin
          Stream := TMemoryStream.Create;
          try
            table.Cells[r,c].GetRVData.SaveRVFToStream(Stream, False, clNone, nil, nil);
            Stream.Position := 0;
            if ((c=0) and (r<>0)) or NewLine or
               (table.Cells[r,c].GetRVData.GetItemStyle(0)=rvsListMarker) or
               table.Cells[r,c].GetRVData.GetItem(0).GetBoolValue(rvbpFullWidth) then
              rv.InsertRVFFromStream(Stream, rv.ItemCount)
            else
              rv.AppendRVFFromStream(Stream, -1);
            NewLine := False;
          finally
            Stream.Free;
          end;
        end;
      end;
    end;
    NormalizeRichView(rv.RVData);
    ItemNo := table.GetMyItemNo;
    Stream := TMemoryStream.Create;
    try
      rv.SaveRVFToStream(Stream, False);
      Stream.Position := 0;
      rve.SetSelectionBounds(ItemNo, 0, ItemNo, 1);
      rve.InsertRVFFromStreamEd(Stream);
    finally
      Stream.Free;
    end;
  finally
    rv.Free;
  end;
end;

Ways to improve:
1) this code uses TextStyles[0] and ParaStyles[0] for AddNLATag. You can change it to text style from table cells.
2) to do something with semicolons and line breaks inside table cells (this code keeps them as they are)
Back to top
View user's profile Send private message Visit poster's website
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6576

PostPosted: Thu Jul 29, 2010 8:52 am    Post subject: Reply with quote

The examples above convert the current table (at the caret position) as an editing operation. The user can undo it.

The procedure ConvertAllTablesToText below converts all tables, without a possibility to undo. If Recursive=True, nested tables are converted as well.

Code:
procedure ConvertAllTablesToText(RVData: TCustomRVData;
  Recursive: Boolean); forward;

procedure ConvertOneTableToText(RVData: TCustomRVData; ItemNo: Integer;
  Recursive: Boolean);
var table: TRVTableItemInfo;
    rv: TRichView;
    Stream: TMemoryStream;
    r,c: Integer;
    Color: TColor;
begin
  table := RVData.GetItem(ItemNo) as TRVTableItemInfo;
  if Recursive then
    for r := 0 to table.Rows.Count-1 do
      for c := 0 to table.Rows[r].Count-1 do
        if table.Cells[r,c]<>nil then
          ConvertAllTablesToText(table.Cells[r,c].GetRVData, Recursive);
  rv := TRichView.Create(nil);
  try
    rv.Visible := False;
    rv.Style := RVData.GetRVStyle;
    rv.RVFTextStylesReadMode := rvf_sIgnore;
    rv.RVFParaStylesReadMode := rvf_sIgnore;
    for r := 0 to table.Rows.Count-1 do
      for c := 0 to table.Rows[r].Count-1 do
        if table.Cells[r,c]<>nil then begin
          Stream := TMemoryStream.Create;
          try
            table.Cells[r,c].GetRVData.SaveRVFToStream(Stream, False, clNone, nil, nil);
            Stream.Position := 0;
            rv.InsertRVFFromStream(Stream, rv.ItemCount)
          finally
            Stream.Free;
          end;
        end;
    RVData.DeleteItems(ItemNo, 1);
    Stream := TMemoryStream.Create;
    try
      rv.SaveRVFToStream(Stream, False);
      Stream.Position := 0;
      RVData.InsertRVFFromStream(Stream, ItemNo, Color, nil, nil, False);
    finally
      Stream.Free;
    end;
  finally
    rv.Free;
  end;
end;

procedure ConvertAllTablesToText(RVData: TCustomRVData;
  Recursive: Boolean);
var i: Integer;
begin
  for i := RVData.ItemCount-1 downto 0 do
    if RVData.GetItemStyle(i)=rvsTable then
      ConvertOneTableToText(RVData, i, Recursive);
end;


How to use:
Code:
  ConvertAllTablesToText(RichViewEdit1.RVData, True);
  RichViewEdit1.Format;
  RichViewEdit1.ClearUndo;
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    trichview.com Forum Index -> Examples, Demos All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group