unit DBGrids.Customize;
interface
uses
Classes, Windows, Controls, Graphics, Grids, DBGrids;
type
TDBGridCustomizeOptionBase =
class( TControl )
private
fCustomized : Boolean;
fCustomize : Boolean;
procedure SetCustomize(
const Value : Boolean );
protected
procedure AssignTo( Dest : TPersistent );
override;
public
procedure Invalidate;
override;
constructor Create( AOwner : TComponent );
override;
published
property Customize : Boolean
read fCustomize
write SetCustomize;
end;
TDBGridCustomizeOption =
class( TDBGridCustomizeOptionBase )
private
fRowColor : TColor;
fFontColorAutomatic : Boolean;
fFontColor : TColor;
fFontColorAuto : TColor;
procedure SetFontColor(
const Value : TColor );
procedure SetFontColorAutomatic(
const Value : Boolean );
procedure SetRowColor(
const Value : TColor );
function GetFontColor : TColor;
protected
procedure AssignTo( Dest : TPersistent );
override;
public
constructor Create( AOwner : TComponent );
override;
published
property RowColor : TColor
read fRowColor
write SetRowColor;
property FontColor : TColor
read GetFontColor
write SetFontColor;
property FontColorAutomatic : Boolean
read fFontColorAutomatic
write SetFontColorAutomatic;
end;
TDBGridCustomizeOptions =
class( TDBGridCustomizeOptionBase )
private
fEvenRow : TDBGridCustomizeOption;
fOddRow : TDBGridCustomizeOption;
fOddHilightRow : TDBGridCustomizeOption;
fEvenHilightRow : TDBGridCustomizeOption;
fOddCount : integer;
function GetOddCount : integer;
procedure SetOddCount(
const Value : integer );
protected
procedure AssignTo( Dest : TPersistent );
override;
public
constructor Create( AOwner : TComponent );
override;
function GetCustomizedColors( RowNr : integer; RowHighLight : Boolean;
var ARowColor, AFontColor : TColor ) : Boolean;
published
property OddRow : TDBGridCustomizeOption
read fOddRow;
property EvenRow : TDBGridCustomizeOption
read fEvenRow;
property OddHilightRow : TDBGridCustomizeOption
read fOddHilightRow;
property EvenHilightRow : TDBGridCustomizeOption
read fEvenHilightRow;
property OddCount : integer
read GetOddCount
write SetOddCount;
end;
TDBGrid =
class( DBGrids.TDBGrid )
private
fIsHighlight : Boolean;
fGridCustomizer : TDBGridCustomizeOptions;
protected
procedure DrawColumnCell(
const Rect : TRect; DataCol : integer; Column : TColumn; State : TGridDrawState );
override;
procedure DrawCellHighlight(
const ARect : TRect; AState : TGridDrawState; ACol : integer; ARow : integer );
override;
public
constructor Create( AOwner : TComponent );
override;
published
property GridCustomizer : TDBGridCustomizeOptions
read fGridCustomizer;
end;
function GetAutomaticFontColor( AColor : TColor ) : TColor;
implementation
function GetAutomaticFontColor( AColor : TColor ) : TColor;
var
lColor : TColor;
r, g, b : Byte;
begin
lColor := ColorToRGB( AColor );
r := ( lColor
shr 0 )
and $FF;
g := ( lColor
shr 8 )
and $FF;
b := ( lColor
shr 16 )
and $FF;
if ( r * 2 + g * 4 + b >= ( $FF ) * 4 )
then
Result := clBlack
else
Result := clWhite;
end;
{ TDBGrid }
constructor TDBGrid.Create( AOwner : TComponent );
begin
inherited;
fGridCustomizer := TDBGridCustomizeOptions.Create( Self );
end;
procedure TDBGrid.DrawCellHighlight(
const ARect : TRect; AState : TGridDrawState; ACol, ARow : integer );
begin
inherited;
fIsHighlight := True;
end;
procedure TDBGrid.DrawColumnCell(
const Rect : TRect; DataCol : integer; Column : TColumn; State : TGridDrawState );
var
lRect : TRect;
lRowNr : integer;
lRowColor : TColor;
lFontColor : TColor;
lSaveColor : TColor;
lSaveFontColor : TColor;
begin
if Assigned( DataLink.DataSet )
then
begin
lRowColor := Canvas.Brush.Color;
lFontColor := Canvas.Font.Color;
if GridCustomizer.GetCustomizedColors( DataLink.DataSet.RecNo, fIsHighlight, lRowColor, lFontColor )
then
begin
lSaveColor := Canvas.Brush.Color;
lSaveFontColor := Canvas.Font.Color;
lRect := Rect;
lRect.Left := lRect.Left + 1;
Canvas.Brush.Color := lRowColor;
Canvas.FillRect( Rect );
Canvas.Font.Color := lFontColor;
DefaultDrawColumnCell( lRect, DataCol, Column, State );
Canvas.Brush.Color := lSaveColor;
Canvas.Font.Color := lSaveFontColor;
end;
end;
fIsHighlight := False;
inherited;
end;
{ TDBGridCustomizeOptions }
procedure TDBGridCustomizeOptions.AssignTo( Dest : TPersistent );
begin
if Dest
is TDBGridCustomizeOptions
then
with TDBGridCustomizeOptions( Dest )
do
begin
EvenRow.Assign( Self.EvenRow );
EvenHilightRow.Assign( Self.EvenHilightRow );
OddRow.Assign( Self.OddRow );
OddHilightRow.Assign( Self.OddRow );
OddCount := Self.OddCount;
end;
inherited;
end;
constructor TDBGridCustomizeOptions.Create( AOwner : TComponent );
begin
inherited;
fEvenRow := TDBGridCustomizeOption.Create( Self );
fOddRow := TDBGridCustomizeOption.Create( Self );
fEvenHilightRow := TDBGridCustomizeOption.Create( Self );
fOddHilightRow := TDBGridCustomizeOption.Create( Self );
end;
function TDBGridCustomizeOptions.GetCustomizedColors( RowNr : integer; RowHighLight : Boolean;
var ARowColor, AFontColor : TColor ) : Boolean;
var
lCustomizer : TDBGridCustomizeOption;
begin
Result := False;
if not Customize
then
Exit;
if ( OddCount = 0 )
or ( ( RowNr + OddCount - 1 )
mod ( OddCount * 2 ) >= OddCount )
then
begin
if RowHighLight
then
lCustomizer := EvenHilightRow
else
lCustomizer := EvenRow;
end
else
begin
if RowHighLight
then
lCustomizer := OddHilightRow
else
lCustomizer := OddRow;
end;
if not lCustomizer.Customize
then
Exit;
ARowColor := lCustomizer.RowColor;
AFontColor := lCustomizer.FontColor;
Result := True;
end;
function TDBGridCustomizeOptions.GetOddCount : integer;
begin
if fOddCount < 0
then
fOddCount := 0;
Result := fOddCount;
end;
procedure TDBGridCustomizeOptions.SetOddCount(
const Value : integer );
begin
if fOddCount <> Value
then
begin
fOddCount := Value;
Invalidate;
end;
end;
{ TDBGridCustomizeOption }
procedure TDBGridCustomizeOption.AssignTo( Dest : TPersistent );
begin
if Dest
is TDBGridCustomizeOption
then
with TDBGridCustomizeOption( Dest )
do
begin
FontColor := Self.fFontColor;
FontColorAutomatic := Self.FontColorAutomatic;
RowColor := Self.fRowColor;
end;
inherited;
end;
constructor TDBGridCustomizeOption.Create( AOwner : TComponent );
begin
inherited;
fRowColor := clWindow;
fFontColor := clWindowText;
fFontColorAuto := GetAutomaticFontColor( fRowColor );
fFontColorAutomatic := True;
end;
function TDBGridCustomizeOption.GetFontColor : TColor;
begin
if fFontColorAutomatic
then
Result := fFontColorAuto
else
Result := fFontColor;
end;
procedure TDBGridCustomizeOption.SetFontColor(
const Value : TColor );
begin
if ( fFontColor <> Value )
and not fFontColorAutomatic
then
begin
fFontColor := Value;
Invalidate;
end;
end;
procedure TDBGridCustomizeOption.SetFontColorAutomatic(
const Value : Boolean );
begin
if fFontColorAutomatic <> Value
then
begin
fFontColorAutomatic := Value;
Invalidate;
end;
end;
procedure TDBGridCustomizeOption.SetRowColor(
const Value : TColor );
begin
if fRowColor <> Value
then
begin
fRowColor := Value;
fFontColorAuto := GetAutomaticFontColor( fRowColor );
Invalidate;
end;
end;
{ TDBGridCustomizeOptionBase }
procedure TDBGridCustomizeOptionBase.AssignTo( Dest : TPersistent );
begin
if Dest
is TDBGridCustomizeOptionBase
then
begin
with TDBGridCustomizeOptionBase( Dest )
do
begin
Customize := Self.Customize;
end;
end
else
inherited;
end;
constructor TDBGridCustomizeOptionBase.Create( AOwner : TComponent );
begin
inherited;
fCustomized := False;
fCustomize := False;
end;
procedure TDBGridCustomizeOptionBase.Invalidate;
begin
if ( fCustomize
or fCustomized )
then
begin
if Assigned( Owner )
and ( Owner
is TControl )
then
TControl( Owner ).Invalidate;
fCustomized := fCustomize;
end;
end;
procedure TDBGridCustomizeOptionBase.SetCustomize(
const Value : Boolean );
begin
if fCustomize <> Value
then
begin
fCustomize := Value;
Invalidate;
end;
end;
end.