need to auto-fit table to contents without wordwrapping

General TRichView support forum. Please post your questions here
Post Reply
MikeP
Posts: 5
Joined: Mon Sep 13, 2010 7:21 pm

need to auto-fit table to contents without wordwrapping

Post by MikeP »

I need to construct a table in TRichView 19.2 (Delphi 12). I need it to auto-size without word-wrapping the cell contents. what's wrong with my code?

what this code does is a full-width table divided into two very wide columns--completely ignoring the width of the table's contents.

Code: Select all

procedure TfrmTRichView_TRichView_AddTable.FormCreate(Sender: TObject);
var
  Table: TRVTableItemInfo;
  RVData: TCustomRVData;
  iParaStyleIndex: Integer;
begin
  // Assuming you have a TRichView component named RichView1
  RVData := RichView1.RVData;

  // Create a table with 1 row and 2 columns
  Table := TRVTableItemInfo.CreateEx(1, 2, RVData);
  table.BestWidth:=0;
  table.Options:=table.Options - [rvtoIgnoreContentWidth] + [rvtoRTFAllowAutofit];

  // Add a new paragraph style with no word wrap
  iParaStyleIndex := RichView1.Style.ParaStyles.Count;
  with RichView1.Style.ParaStyles.Add do
  begin
    Options := Options + [rvpaoNoWrap]; // Disable word wrap
    Alignment:=rvaRight;
  end;

  // Apply the style to text inside the cells
  Table.Cells[0,0].Clear;
  Table.Cells[0,0].BestWidth:=0;
  Table.Cells[0,0].AddNL('No Wrap', 0, iParaStyleIndex);

  Table.Cells[1,0].Clear;
  Table.Cells[1,0].BestWidth:=0;
  Table.Cells[1,0].AddNL('No Wrap', 0, iParaStyleIndex);

  // Insert table into document
  RVData.AddItem('', Table);

  // Update document display
  RichView1.Format;
end;
Thank you!
Sergey Tkachenko
Site Admin
Posts: 17805
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: need to auto-fit table to contents without wordwrapping

Post by Sergey Tkachenko »

Your code is correct.
But if you do not specify widths of columns or a width of a table, TRichView will display it filling the whole editor width.
To auto-fit without word-wrap, assign some small value either to table.BestWith, or to BestWidth of all columns.
Like this:

Code: Select all

var
  Table: TRVTableItemInfo;
  RVData: TCustomRVData;
  iParaStyleIndex: Integer;
begin
  // Assuming you have a TRichView component named RichView1
  RVData := RichViewEdit1.RVData;

  // Create a table with 1 row and 2 columns
  Table := TRVTableItemInfo.CreateEx(1, 2, RVData);
  table.BestWidth := 1; // this is it, a small width
  table.CellBorderWidth := 1; // to make cell border visible

  // Add a new paragraph style with no word wrap
  iParaStyleIndex := RichViewEdit1.Style.ParaStyles.Count;
  with RichViewEdit1.Style.ParaStyles.Add do
  begin
    Options := Options + [rvpaoNoWrap]; // Disable word wrap
    Alignment:=rvaRight;
  end;

  // Apply the style to text inside the cells
  Table.Cells[0,0].Clear;
  Table.Cells[0,0].BestWidth:=0;
  Table.Cells[0,0].AddNL('No Wrap', 0, iParaStyleIndex);

  // in TRichView, Cells have syntax Cells[Row, Column]
  Table.Cells[0,1].Clear;
  Table.Cells[0,1].BestWidth:=0;
  Table.Cells[0,1].AddNL('No Wrap', 0, iParaStyleIndex);

  // Insert table into document
  RVData.AddItem('', Table);

  // Update document display
  RichViewEdit1.Format;
end;
Sergey Tkachenko
Site Admin
Posts: 17805
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: need to auto-fit table to contents without wordwrapping

Post by Sergey Tkachenko »

A thing to improve: this code will add a new paragraph style each time when it is executed.

Change

Code: Select all

 iParaStyleIndex := RichViewEdit1.Style.ParaStyles.Count;
  with RichViewEdit1.Style.ParaStyles.Add do
  begin
    Options := Options + [rvpaoNoWrap]; // Disable word wrap
    Alignment:=rvaRight;
  end;
to

Code: Select all

  var ParaStyle: TParaInfo;

  ParaStyle := TParaInfo.Create(nil);
  ParaStyle.Assign(RichViewedit1.Style.ParaStyles[0]);
  ParaStyle.Options := ParaStyle.Options + [rvpaoNoWrap]; // Disable word wrap
  ParaStyle.Alignment:=rvaRight;
  iParaStyleIndex := RichViewEdit1.Style.FindParaStyle(ParaStyle);
  ParaStyle.Free;
MikeP
Posts: 5
Joined: Mon Sep 13, 2010 7:21 pm

Re: need to auto-fit table to contents without wordwrapping

Post by MikeP »

perfect, thank you so much!
best regards,
mp
Post Reply