dbgrid.ThumbTracking and fixes from Jesus

git-svn-id: trunk@6759 -
This commit is contained in:
mattias 2005-02-06 22:43:38 +00:00
parent cb7baa3546
commit 303e21e7b1
2 changed files with 72 additions and 64 deletions

View File

@ -155,6 +155,8 @@ type
property Items[Index: Integer]: TColumn read GetColumn write SetColumn; default; property Items[Index: Integer]: TColumn read GetColumn write SetColumn; default;
end; end;
{ TCustomDbGrid }
TCustomDbGrid=class(TCustomGrid) TCustomDbGrid=class(TCustomGrid)
private private
FDataLink: TComponentDataLink; FDataLink: TComponentDataLink;
@ -174,9 +176,11 @@ type
FDrawingActiveRecord: Boolean; FDrawingActiveRecord: Boolean;
FEditingColumn: Integer; FEditingColumn: Integer;
FOldPosition: Integer; FOldPosition: Integer;
FDefaultColWidths: boolean;
function GetCurrentField: TField; function GetCurrentField: TField;
function GetDataSource: TDataSource; function GetDataSource: TDataSource;
function GetRecordCount: Integer; function GetRecordCount: Integer;
function GetThumbTracking: boolean;
procedure OnRecordChanged(Field:TField); procedure OnRecordChanged(Field:TField);
procedure OnDataSetChanged(aDataSet: TDataSet); procedure OnDataSetChanged(aDataSet: TDataSet);
procedure OnDataSetOpen(aDataSet: TDataSet); procedure OnDataSetOpen(aDataSet: TDataSet);
@ -193,6 +197,7 @@ type
procedure SetCurrentField(const AValue: TField); procedure SetCurrentField(const AValue: TField);
procedure SetDataSource(const AValue: TDataSource); procedure SetDataSource(const AValue: TDataSource);
procedure SetOptions(const AValue: TDbGridOptions); procedure SetOptions(const AValue: TDbGridOptions);
procedure SetThumbTracking(const AValue: boolean);
procedure UpdateBufferCount; procedure UpdateBufferCount;
procedure UpdateData; procedure UpdateData;
@ -279,8 +284,10 @@ type
public public
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
procedure DefaultDrawColumnCell(const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); procedure DefaultDrawColumnCell(const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure ResetColWidths;
destructor Destroy; override; destructor Destroy; override;
property SelectedField: TField read GetCurrentField write SetCurrentField; property SelectedField: TField read GetCurrentField write SetCurrentField;
property ThumbTracking: boolean read GetThumbTracking write SetThumbTracking;
end; end;
@ -431,6 +438,11 @@ begin
result := FDataLink.DataSet.RecordCount; result := FDataLink.DataSet.RecordCount;
end; end;
function TCustomDbGrid.GetThumbTracking: boolean;
begin
Result := goThumbTracking in inherited Options;
end;
function TCustomDbGrid.GetCurrentField: TField; function TCustomDbGrid.GetCurrentField: TField;
begin begin
result := GetFieldFromGridColumn( Col ); result := GetFieldFromGridColumn( Col );
@ -453,6 +465,7 @@ begin
{$Ifdef dbgdbgrid} {$Ifdef dbgdbgrid}
DebugLn('(',name,') ','TCustomDBGrid.OnDataSetOpen'); DebugLn('(',name,') ','TCustomDBGrid.OnDataSetOpen');
{$endif} {$endif}
FDefaultColWidths := True;
LinkActive(True); LinkActive(True);
UpdateActive; UpdateActive;
end; end;
@ -504,6 +517,7 @@ begin
{$ifdef dbgdbgrid} {$ifdef dbgdbgrid}
DebugLn('(',name,') ','TCustomDBGrid.OnNewDataSet'); DebugLn('(',name,') ','TCustomDBGrid.OnNewDataSet');
{$endif} {$endif}
FDefaultColWidths := True;
LinkActive(True); LinkActive(True);
UpdateActive; UpdateActive;
end; end;
@ -549,6 +563,7 @@ end;
procedure TCustomDbGrid.SetDataSource(const AValue: TDataSource); procedure TCustomDbGrid.SetDataSource(const AValue: TDataSource);
begin begin
if AValue = FDatalink.Datasource then Exit; if AValue = FDatalink.Datasource then Exit;
FDefaultColWidths := True;
FDataLink.DataSource := AValue; FDataLink.DataSource := AValue;
UpdateActive; UpdateActive;
end; end;
@ -608,6 +623,14 @@ begin
end; end;
end; end;
procedure TCustomDbGrid.SetThumbTracking(const AValue: boolean);
begin
if Avalue then
inherited Options := Inherited Options + [goThumbTracking]
else
inherited Options := Inherited Options - [goThumbTracking];
end;
procedure TCustomDbGrid.UpdateBufferCount; procedure TCustomDbGrid.UpdateBufferCount;
var var
BuffCount: Integer; BuffCount: Integer;
@ -827,7 +850,7 @@ begin
i := 0; i := 0;
result := -1; result := -1;
if FDataLink.Active then if FDataLink.Active then
while (i<FDataLink.DataSet.FieldCount) do begin while (i<FDataLink.DataSet.FieldCount)and(Column>=0) do begin
if FDataLink.Fields[i].Visible then begin if FDataLink.Fields[i].Visible then begin
Dec(Column); Dec(Column);
if Column<0 then begin if Column<0 then begin
@ -843,10 +866,12 @@ procedure TCustomDbGrid.UpdateGridColumnSizes;
var var
i: Integer; i: Integer;
begin begin
if dgIndicator in Options then if FDefaultColWidths then begin
ColWidths[0]:=12; if dgIndicator in Options then
for i:=FixedCols to ColCount-1 do ColWidths[0]:=12;
ColWidths[i] := GetColumnWidth(i); for i:=FixedCols to ColCount-1 do
ColWidths[i] := GetColumnWidth(i);
end;
end; end;
procedure TCustomDbGrid.UpdateScrollbarRange; procedure TCustomDbGrid.UpdateScrollbarRange;
@ -863,7 +888,9 @@ begin
aRange := GetRecordCount + VisibleRowCount - 1; aRange := GetRecordCount + VisibleRowCount - 1;
aPage := VisibleRowCount; aPage := VisibleRowCount;
if aPage<1 then aPage := 1; if aPage<1 then aPage := 1;
aPos := FDataLink.DataSet.RecNo; if FDatalink.BOF then aPos := 0 else
if FDatalink.EOF then aPos := aRange
else aPos := FDataLink.DataSet.RecNo - 1; // RecNo is 1 based
end else begin end else begin
aRange := 6; aRange := 6;
aPage := 2; aPage := 2;
@ -1505,12 +1532,14 @@ procedure TCustomDbGrid.HeaderSized(IsColumn: Boolean; Index: Integer);
var var
i: Integer; i: Integer;
begin begin
if IsColumn then if IsColumn then begin
if Columns.Enabled then begin if Columns.Enabled then begin
i := ColumnIndexFromGridColumn(Index); i := ColumnIndexFromGridColumn(Index);
if i>=0 then if i>=0 then
Columns[i].Width := ColWidths[Index]; Columns[i].Width := ColWidths[Index];
end; end;
FDefaultColWidths := True;
end;
end; end;
procedure TCustomDbGrid.UpdateActive; procedure TCustomDbGrid.UpdateActive;
@ -1521,8 +1550,10 @@ begin
DebugLn(Name,'.UpdateActive: ActiveRecord=', dbgs(ActiveRecord), DebugLn(Name,'.UpdateActive: ActiveRecord=', dbgs(ActiveRecord),
' FixedRows=',dbgs(FixedRows), ' Row=', dbgs(Row)); ' FixedRows=',dbgs(FixedRows), ' Row=', dbgs(Row));
{$endif} {$endif}
if FixedRows + ActiveRecord <> Row then if FixedRows + ActiveRecord <> Row then begin
InvalidateRow(Row); InvalidateRow(Row);
EditingColumn(Col, false);
end;
Row:= FixedRows + ActiveRecord; Row:= FixedRows + ActiveRecord;
end; end;
//Invalidate; //Invalidate;
@ -1598,6 +1629,8 @@ begin
FDataLink.OnUpdateData:=@OnUpdateData; FDataLink.OnUpdateData:=@OnUpdateData;
FDataLink.VisualControl:= True; FDataLink.VisualControl:= True;
FDefaultColWidths := True;
FOptions := [dgColumnResize, dgTitles, dgIndicator, dgRowLines, dgColLines, FOptions := [dgColumnResize, dgTitles, dgIndicator, dgRowLines, dgColLines,
dgConfirmDelete, dgCancelOnExit, dgTabs, dgEditing, dgAlwaysShowSelection]; dgConfirmDelete, dgCancelOnExit, dgTabs, dgEditing, dgAlwaysShowSelection];
@ -1657,6 +1690,14 @@ begin
end; end;
end; end;
procedure TCustomDbGrid.ResetColWidths;
begin
if not FDefaultColWidths then begin
FDefaultColWidths := True;
LayoutChanged;
end;
end;
destructor TCustomDbGrid.Destroy; destructor TCustomDbGrid.Destroy;
begin begin
FDataLink.OnDataSetChanged:=nil; FDataLink.OnDataSetChanged:=nil;
@ -1961,6 +2002,9 @@ end.
{ {
$Log$ $Log$
Revision 1.31 2005/02/06 22:43:38 mattias
dbgrid.ThumbTracking and fixes from Jesus
Revision 1.30 2005/01/16 13:16:31 mattias Revision 1.30 2005/01/16 13:16:31 mattias
added DoCompareCells, changed OnCompareCell from Jesus added DoCompareCells, changed OnCompareCell from Jesus

View File

@ -436,7 +436,6 @@ type
ValidGrid: Boolean; // true if there is something to show ValidGrid: Boolean; // true if there is something to show
AccumWidth: TList; // Accumulated width per column AccumWidth: TList; // Accumulated width per column
AccumHeight: TList; // Accumulated Height per row AccumHeight: TList; // Accumulated Height per row
HScrDiv,VScrDiv: Double; // Transform const for ThumbTracking
TLColOff,TLRowOff: Integer; // TopLeft Offset in pixels TLColOff,TLRowOff: Integer; // TopLeft Offset in pixels
MaxTopLeft: TPoint; // Max Top left ( cell coorditates) MaxTopLeft: TPoint; // Max Top left ( cell coorditates)
end; end;
@ -770,6 +769,7 @@ type
procedure EndUpdate(UO: TUpdateOption); overload; procedure EndUpdate(UO: TUpdateOption); overload;
procedure EndUpdate(FullUpdate: Boolean); overload; procedure EndUpdate(FullUpdate: Boolean); overload;
procedure EndUpdate; overload; procedure EndUpdate; overload;
procedure EraseBackground(DC: HDC); override;
procedure ExchangeColRow(IsColumn: Boolean; index, WithIndex: Integer); procedure ExchangeColRow(IsColumn: Boolean; index, WithIndex: Integer);
function IscellSelected(aCol,aRow: Integer): Boolean; function IscellSelected(aCol,aRow: Integer): Boolean;
function IscellVisible(aCol, aRow: Integer): Boolean; function IscellVisible(aCol, aRow: Integer): Boolean;
@ -1882,8 +1882,6 @@ var
TW:= Integer(AccumWidth[MaxTopLeft.X])-(HsbRange-ClientWidth); TW:= Integer(AccumWidth[MaxTopLeft.X])-(HsbRange-ClientWidth);
HsbRange:=HsbRange + TW - FixedWidth + 1; HsbRange:=HsbRange + TW - FixedWidth + 1;
end; end;
if HsbRange>ClientWidth then
HscrDiv := Double(ColCount-FixedCols-1)/(HsbRange-ClientWidth);
end; end;
end else end else
if FScrollBars in [ssHorizontal, ssBoth] then HsbRange:=0; if FScrollBars in [ssHorizontal, ssBoth] then HsbRange:=0;
@ -1891,14 +1889,10 @@ var
if ScrollBarAutomatic(ssVertical) then begin if ScrollBarAutomatic(ssVertical) then begin
if VSbVisible then begin if VSbVisible then begin
VSbRange:= GridHeight + 2 - Integer(BorderStyle){ + dh}; VSbRange:= GridHeight + 2 - Integer(BorderStyle){ + dh};
if not (goSmoothScroll in Options) then begin if not (goSmoothScroll in Options) then begin
TH:= Integer(accumHeight[MaxTopLeft.Y])-(VsbRange-ClientHeight); TH:= Integer(accumHeight[MaxTopLeft.Y])-(VsbRange-ClientHeight);
VsbRange:=VsbRange + TH -FixedHeight + 1; VsbRange:=VsbRange + TH -FixedHeight + 1;
end; end;
if VSbRange>ClientHeight then
VScrDiv:= Double(RowCount-FixedRows-1)/(VsbRange-ClientHeight);
end; end;
end else end else
if FScrollBars in [ssVertical, ssBoth] then VsbRange:= 0; if FScrollBars in [ssVertical, ssBoth] then VsbRange:= 0;
@ -1914,8 +1908,6 @@ begin
FGCache.ScrollWidth:=FGCache.ClientWidth-FGCache.FixedWidth; FGCache.ScrollWidth:=FGCache.ClientWidth-FGCache.FixedWidth;
FGCache.ScrollHeight:=FGCache.ClientHeight-FGCache.FixedHeight; FGCache.ScrollHeight:=FGCache.ClientHeight-FGCache.FixedHeight;
FGCache.MaxTopLeft:=CalcMaxTopLeft; FGCache.MaxTopLeft:=CalcMaxTopLeft;
FGCache.HScrDiv:=0;
FGCache.VScrDiv:=0;
if not(goSmoothScroll in Options) then begin if not(goSmoothScroll in Options) then begin
FGCache.TLColOff:=0; FGCache.TLColOff:=0;
FGCache.TLRowOff:=0; FGCache.TLRowOff:=0;
@ -2644,42 +2636,18 @@ begin
if goTabs in Options then Msg.Result:= Msg.Result or DLGC_WANTTAB; if goTabs in Options then Msg.Result:= Msg.Result or DLGC_WANTTAB;
end; end;
//
// NOTE: WMHScroll and VMHScroll
// This methods are used to pre-calculate the scroll position
//
procedure TCustomGrid.WMHScroll(var message: TLMHScroll); procedure TCustomGrid.WMHScroll(var message: TLMHScroll);
var var
C,TL,CTL: Integer; C,TL,CTL: Integer;
begin begin
// Avoid invalidating right know, just let the scrollbar
// calculate its position
{
BeginUpdate;
Inherited;
message.Result:=1;
EndUpdate(uoNone);
}
{$IfDef dbgScroll} {$IfDef dbgScroll}
DebugLn('HSCROLL: Code=',IntToStr(message.ScrollCode),' Position=', IntToStr(message.Pos)); DebugLn('HSCROLL: Code=',IntToStr(message.ScrollCode),' Position=', IntToStr(message.Pos));
{$Endif} {$Endif}
if FGCache.HScrDiv<=0 then Exit;
if FEditor<>nil then if FEditor<>nil then
EditorGetValue; EditorGetValue;
if goThumbTracking in Options then begin
C:=FFixedCols + Round( message.Pos * FGCache.HScrDiv );
if (FCol<>C) then begin
Inc(FUpdateScrollBarsCount);
MoveExtend(False, C, FRow);
Dec(FUpdateScrollBarsCount);
end;
end else begin
TL:= Integer(FGCache.AccumWidth[ FGCache.MaxTopLeft.X ]) - FGCAche.FixedWidth; TL:= Integer(FGCache.AccumWidth[ FGCache.MaxTopLeft.X ]) - FGCAche.FixedWidth;
CTL:= Integer(FGCache.AccumWidth[ FtopLeft.X ]) - FGCache.FixedWidth; CTL:= Integer(FGCache.AccumWidth[ FtopLeft.X ]) - FGCache.FixedWidth;
@ -2694,8 +2662,13 @@ begin
SB_PAGEDOWN: C := CTL + FGCache.ClientWidth; SB_PAGEDOWN: C := CTL + FGCache.ClientWidth;
SB_PAGEUP: C := CTL - FGCache.ClientWidth; SB_PAGEUP: C := CTL - FGCache.ClientWidth;
// Scrolls to the current scroll bar position // Scrolls to the current scroll bar position
SB_THUMBPOSITION, SB_THUMBPOSITION:
SB_THUMBTRACK: C := message.Pos; C := Message.Pos;
SB_THUMBTRACK:
if goThumbTracking in Options then
C := message.Pos
else
Exit;
// Ends scrolling // Ends scrolling
SB_ENDSCROLL: Exit; SB_ENDSCROLL: Exit;
end; end;
@ -2733,34 +2706,16 @@ begin
Invalidate; Invalidate;
end; end;
end; end;
end;
procedure TCustomGrid.WMVScroll(var message: TLMVScroll); procedure TCustomGrid.WMVScroll(var message: TLMVScroll);
var var
C, TL, CTL: Integer; C, TL, CTL: Integer;
begin begin
// Avoid invalidating right know, just let the scrollbar
// calculate its position
{
BeginUpdate;
Inherited;
message.Result:=1;
EndUpdate(uoNone);
}
{$IfDef dbgScroll} {$IfDef dbgScroll}
DebugLn('VSCROLL: Code=',IntToStr(message.ScrollCode),' Position=', IntToStr(message.Pos)); DebugLn('VSCROLL: Code=',IntToStr(message.ScrollCode),' Position=', IntToStr(message.Pos));
{$Endif} {$Endif}
if FGCache.VScrDiv<=0 then Exit;
if FEditor<>nil then EditorGetValue; if FEditor<>nil then EditorGetValue;
if goThumbTracking in Options then begin
C:=FFixedRows + Round( message.Pos * FGCache.VScrDiv );
if (C<>FRow) then begin
Inc(FUpdateScrollBarsCount);
MoveExtend(False, FCol, C);
Dec(FUpdateScrollBarsCount);
end;
end else begin
TL:= Integer(FGCache.AccumHeight[ FGCache.MaxTopLeft.Y ]) - FGCache.FixedHeight; TL:= Integer(FGCache.AccumHeight[ FGCache.MaxTopLeft.Y ]) - FGCache.FixedHeight;
CTL:= Integer(FGCache.AccumHeight[ FtopLeft.Y ]) - FGCache.FixedHeight; CTL:= Integer(FGCache.AccumHeight[ FtopLeft.Y ]) - FGCache.FixedHeight;
@ -2776,8 +2731,13 @@ begin
SB_PAGEDOWN: C := CTL + FGCache.ClientHeight; SB_PAGEDOWN: C := CTL + FGCache.ClientHeight;
SB_PAGEUP: C := CTL - FGCache.ClientHeight; SB_PAGEUP: C := CTL - FGCache.ClientHeight;
// Scrolls to the current scroll bar position // Scrolls to the current scroll bar position
SB_THUMBPOSITION, SB_THUMBPOSITION:
SB_THUMBTRACK: C := message.Pos; C := message.Pos;
SB_THUMBTRACK:
if goThumbTracking in Options then
C := message.Pos
else
Exit;
// Ends scrolling // Ends scrolling
SB_ENDSCROLL: Exit; SB_ENDSCROLL: Exit;
end; end;
@ -2814,7 +2774,6 @@ begin
Invalidate; Invalidate;
end; end;
end; end;
end;
procedure TCustomGrid.WMChar(var message: TLMChar); procedure TCustomGrid.WMChar(var message: TLMChar);
var var
@ -4233,6 +4192,11 @@ begin
EndUpdate(true); EndUpdate(true);
end; end;
procedure TCustomGrid.EraseBackground(DC: HDC);
begin
//
end;
function TCustomGrid.IsCellSelected(aCol, aRow: Integer): Boolean; function TCustomGrid.IsCellSelected(aCol, aRow: Integer): Boolean;
begin begin
Result:= (FRange.Left<=aCol) and Result:= (FRange.Left<=aCol) and