unit FMX.CommonGuiTools;
interface
uses
FMX.Grid, FMX.Forms;
type
TGridTools =
class abstract
class procedure UpdateCells(
const AGrid : TGrid;
const Visible : Boolean = True );
overload;
class procedure UpdateCells(
const AGrid : TGrid;
const AMethod : TOnGetValue;
const VisibleRows : Boolean = True );
overload;
class procedure AutosizeColumn(
const AGrid : TGrid;
const ColumnIndex : Integer;
const MinWidth : Single = 0 );
end;
TFormTools =
class abstract
class procedure CenterForm(
const AForm, BForm : TCommonCustomForm );
end;
implementation
uses
System.Math;
{$IF CompilerVersion = 23}
type
THackedColumn =
class( TColumn )
end;
{$IFEND}
{ TGridTools }
class procedure TGridTools.AutosizeColumn(
const AGrid : TGrid;
const ColumnIndex : Integer;
const MinWidth : Single );
var
LCol : Integer;
LWidth : Single;
LClientWidth : Single;
begin
LWidth := 0;
for LCol := 0
to Pred( AGrid.ColumnCount )
do
begin
if ( LCol <> ColumnIndex )
and AGrid.ColumnByIndex( LCol ).Visible
then
LWidth := LWidth + AGrid.ColumnByIndex( LCol ).Width;
end;
repeat
LClientWidth := AGrid.ClientWidth;
AGrid.ColumnByIndex( ColumnIndex ).Width := Max( MinWidth, AGrid.ClientWidth - LWidth );
until SameValue( LClientWidth, AGrid.ClientWidth, 0.0001 );
end;
class procedure TGridTools.UpdateCells(
const AGrid : TGrid;
const Visible : Boolean );
var
LCol, LRow, LRowMin, LRowMax : Integer;
LColumn : TColumn;
begin
if Visible
then
begin
LRowMin := AGrid.TopRow;
LRowMax := Pred( Min( AGrid.TopRow + AGrid.VisibleRows, AGrid.RowCount ) );
end
else
begin
LRowMin := 0;
LRowMax := Pred( AGrid.RowCount );
end;
for LCol := 0
to Pred( AGrid.ColumnCount )
do
begin
LColumn := AGrid.ColumnByIndex( LCol );
{$IF CompilerVersion = 23}
THackedColumn( LColumn ).UpdateColumn;
{$ELSE}
for LRow := LRowMin
to LRowMax
do
if ( Visible
and AGrid.ColumnByIndex( LCol ).Visible )
or not Visible
then
LColumn.UpdateCell( LRow );
{$IFEND}
end;
end;
class procedure TGridTools.UpdateCells(
const AGrid : TGrid;
const AMethod : TOnGetValue;
const VisibleRows : Boolean );
var
LMethod : TOnGetValue;
begin
LMethod := AGrid.OnGetValue;
try
AGrid.OnGetValue := AMethod;
UpdateCells( AGrid, VisibleRows );
finally
AGrid.OnGetValue := LMethod;
end;
end;
{ TFormTools }
class procedure TFormTools.CenterForm(
const AForm, BForm : TCommonCustomForm );
begin
AForm.Left := BForm.Width
div 2 + BForm.Left - AForm.Width
div 2;
AForm.Top := BForm.Height
div 2 + BForm.Top - AForm.Height
div 2;
end;
end.