Page 1 of 1

How to set table.SetCellVAlign for selected cells and at cur

Posted: Wed Jan 26, 2011 1:55 am
by w2m
1. How to set table.SetCellVAlign for selected cells and at the caret position?

Code: Select all

procedure TFormMain.TableCellVAlignMiddle1Click( Sender: TObject );
var
  Item: TCustomRVItemInfo;
  Table: TRVTableItemInfo;
  RichViewEdit: TCustomRichViewEdit;
  ItemNo: integer;
  r, c: integer;
  tr, lc: integer;
  sc, sr: integer;
  RVData: TCustomRVFormattedData;
begin
  if Assigned( AdvOfficePager1.ActivePage ) then
  begin
    cxTRichViewEdit := TcxTRichViewEdit( AdvOfficePager1.ActivePage.Controls[ 0 ] );
    if not cxTRichViewEdit.InnerEditor.CanChange or not cxTRichViewEdit.InnerEditor.GetCurrentItemEx( TRVTableItemInfo,
      RichViewEdit, Item ) then
      exit;
    Table := TRVTableItemInfo( Item );
    ItemNo := RichViewEdit.GetItemNo( Table );
    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
          if table.IsCellSelected( r, c ) then
          begin
            with table.Cells[ r, c ].VisibleBorders do
              table.SetCellVAlign( rvcMiddle, r, c );
          end
          else
          // not selected so SetCellVAlign at the caret position?
          begin

          end;
  end;
end;
2. How to select only the single cell at the caret position?
3. Is there a way to select one cell? I can select 2 cells but not one?

Posted: Wed Jan 26, 2011 5:05 pm
by Sergey Tkachenko

Code: Select all

var Data: Integer;

...
RichViewEdit.BeginItemModify(ItemNo, Data);

if table.GetEditedCell(r,c)<>nil then
  // the caret is in a table cell
  table.SetCellVAlign( rvcMiddle, r, c )
else begin
  // applying to selected cells
  RichViewEdit.BeginUndoGroup(rvutModifyItem);
  RichViewEdit.SetUndoGroupMode(True);
  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) and table.IsCellSelected( r, c ) then 
        table.SetCellVAlign( rvcMiddle, r, c ); 
  RichViewEdit.SetUndoGroupMode(False);
end;

RichViewEdit.EndItemModify(ItemNo, Data);
RichViewEdit.Change;

Posted: Wed Jan 26, 2011 5:07 pm
by Sergey Tkachenko
User cannot select one cell with the mouse of the keyboard.
However, you can select it using table.Select method
(in RichViewActions, TrvActionTableSelectCell calls this method to select one cell)

Posted: Wed Jan 26, 2011 6:42 pm
by w2m
Your code almost worked but I had to modify it to work without an exception in the line TRichViewEdit.InnerEditor.BeginItemModify( ItemNo, Data );

Code: Select all

procedure TFormMain.TableCellVAlignMiddle1Click( Sender: TObject );
var
  Item: TCustomRVItemInfo;
  Table: TRVTableItemInfo;
  RichViewEdit: TCustomRichViewEdit;
  ItemNo: integer;
  r, c: integer;
  Data: Integer;
begin
  if Assigned( AdvOfficePager1.ActivePage ) then
  begin
    cxTRichViewEdit := TcxTRichViewEdit( AdvOfficePager1.ActivePage.Controls[ 0 ] );
    if not cxTRichViewEdit.InnerEditor.CanChange or not cxTRichViewEdit.InnerEditor.GetCurrentItemEx( TRVTableItemInfo,
      RichViewEdit, Item ) then
      exit;
    Table := TRVTableItemInfo( Item );
    ItemNo := RichViewEdit.GetItemNo( Table );
    cxTRichViewEdit.InnerEditor.BeginItemModify( ItemNo, Data );
    if table.GetEditedCell( r, c ) <> nil then
      // the caret is in a table cell
      table.SetCellVAlign( rvcMiddle, r, c )
    else
    begin
      // applying to selected cells
      cxTRichViewEdit.InnerEditor.BeginUndoGroup( rvutModifyItem );
      cxTRichViewEdit.InnerEditor.SetUndoGroupMode( True );
      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 ) and table.IsCellSelected( r, c ) then
            table.SetCellVAlign( rvcMiddle, r, c );
      cxTRichViewEdit.InnerEditor.SetUndoGroupMode( False );
    end;
    cxTRichViewEdit.InnerEditor.EndItemModify( ItemNo, Data );
    cxTRichViewEdit.InnerEditor.Change;
  end;
end;
This is what I did to make Select cell work:

Code: Select all

procedure TFormMain.SelectCell1Click( Sender: TObject );
var
  Item: TCustomRVItemInfo;
  Table: TRVTableItemInfo;
  RichViewEdit: TCustomRichViewEdit;
  ItemNo: integer;
  r, c: integer;
  Data: Integer;
begin
  if Assigned( AdvOfficePager1.ActivePage ) then
  begin
    cxTRichViewEdit := TcxTRichViewEdit( AdvOfficePager1.ActivePage.Controls[ 0 ] );
    if not cxTRichViewEdit.InnerEditor.CanChange or not cxTRichViewEdit.InnerEditor.GetCurrentItemEx( TRVTableItemInfo,
      RichViewEdit, Item ) then
      exit;
    Table := TRVTableItemInfo( Item );
    ItemNo := RichViewEdit.GetItemNo( Table );
    cxTRichViewEdit.InnerEditor.BeginItemModify( ItemNo, Data );
    if table.GetEditedCell( r, c ) <> nil then
      // the caret is in a table cell
      table.Select( r, c, 0, 0 );
    cxTRichViewEdit.InnerEditor.EndItemModify( ItemNo, Data );
  end;
end;
Do you think this is correct?

Thanks for your help.

Bill

Posted: Thu Jan 27, 2011 12:35 pm
by Sergey Tkachenko
BeginItemModify, EndItemModify and Change must be called for TRichViewEdit containing the table in its items.
It's not necessary the root editor (i.e. cxTRichViewEdit.InnerEditor), it is the editor returned by GetCurrentItemEx.
So my original code was correct.

As for your second code sample, it is correct (with the exception of using cxTRichViewEdit.InnerEditor instead of RichViewEdit for BeginItemModify/EndItemModify).
Actually BeginItemModify and EndItemModify are not necessary for selection, because selection is not a modification. It's enough to call RichViewEdit.Invalidate after table.Select.