1 |
+ |
|
2 |
|
{ |
3 |
|
/*************************************************************************** |
4 |
|
DBControlGrid.pas |
68 |
|
} |
69 |
|
|
70 |
|
type |
70 |
– |
TRowCacheState = (rcEmpty,rcPresent,rcDeleted); |
71 |
– |
TRowDetails = record |
72 |
– |
FState: TRowCacheState; |
73 |
– |
FAlternateColor: boolean; |
74 |
– |
FBitmap: TBitmap; |
75 |
– |
end; |
76 |
– |
|
71 |
|
{ TRowCache } |
72 |
|
|
73 |
|
TRowCache = class |
74 |
|
private |
75 |
+ |
type |
76 |
+ |
TRowCacheState = (rcEmpty,rcPresent,rcDeleted); |
77 |
+ |
TRowDetails = record |
78 |
+ |
FState: TRowCacheState; |
79 |
+ |
FAlternateColor: boolean; |
80 |
+ |
FBitmap: TBitmap; |
81 |
+ |
end; |
82 |
+ |
|
83 |
+ |
private |
84 |
|
FAltColorStartNormal: boolean; |
85 |
|
FHeight: integer; |
86 |
|
FList: array of TRowDetails; |
155 |
|
FLastMouseButton: TMouseButton; |
156 |
|
FLastMouseShiftState: TShiftState; |
157 |
|
|
158 |
+ |
function ActiveControl: TControl; |
159 |
|
procedure EmptyGrid; |
160 |
|
function GetDataSource: TDataSource; |
161 |
|
function GetRecordCount: Integer; |
288 |
|
|
289 |
|
implementation |
290 |
|
|
291 |
< |
uses LCLType, Math, LCLIntf, Forms, LCLMessageGlue; |
291 |
> |
uses LCLType, Math, LCLIntf, Forms, LCLMessageGlue, EditBtn, MaskEdit; |
292 |
|
|
293 |
|
{ TDBControlGridDataLink } |
294 |
|
|
303 |
|
|
304 |
|
function TRowCache.Render(Control: TWinControl): TBitmap; |
305 |
|
var Container: TBitmap; |
302 |
– |
Msg: TLMPaint; |
306 |
|
begin |
307 |
|
Container := TBitmap.Create; |
308 |
|
try |
309 |
|
Container.SetSize(Control.Width,Control.Height); |
310 |
+ |
Container.Canvas.Brush.Color := control.Color; |
311 |
|
Control.PaintTo(Container.Canvas,0,0); |
312 |
|
except |
313 |
|
Container.Free; |
369 |
|
for i := StartIndex to Length(FList) - 1 do |
370 |
|
begin |
371 |
|
FList[i].FState := rcEmpty; |
372 |
+ |
FList[i].FBitmap := nil; |
373 |
|
FList[i].FAlternateColor := altColor; |
374 |
|
if UseAlternateColors then |
375 |
|
altColor := not altColor; |
417 |
|
procedure TRowCache.ClearCache; |
418 |
|
begin |
419 |
|
FreeImages(true); |
420 |
+ |
SetLength(FList,0); |
421 |
|
end; |
422 |
|
|
423 |
|
function TRowCache.Add2Cache(RecNo: Longint; Control: TWinControl): TBitmap; |
418 |
– |
var i: integer; |
424 |
|
begin |
425 |
|
Dec(RecNo); {Adust to zero base} |
426 |
|
ExtendCache(RecNo + 1); |
427 |
|
FList[RecNo].FState := rcPresent; |
428 |
+ |
if FList[RecNo].FBitmap <> nil then |
429 |
+ |
FList[RecNo].FBitmap.Free; |
430 |
|
FList[RecNo].FBitmap := Render(Control); |
431 |
|
Result := FList[RecNo].FBitmap; |
432 |
|
end; |
483 |
|
if FList[RecNo].FState = rcPresent then |
484 |
|
begin |
485 |
|
FList[RecNo].FBitmap.Free; |
486 |
+ |
FList[RecNo].FBitmap := nil; |
487 |
|
FList[RecNo].FState := rcEmpty; |
488 |
|
end; |
489 |
|
end; |
530 |
|
|
531 |
|
{ TDBControlGrid } |
532 |
|
|
533 |
+ |
function TDBControlGrid.ActiveControl: TControl; |
534 |
+ |
var AParent: TWinControl; |
535 |
+ |
begin |
536 |
+ |
Result := nil; |
537 |
+ |
AParent := Parent; |
538 |
+ |
while (AParent <> nil) and not (AParent is TCustomForm) do |
539 |
+ |
AParent := AParent.Parent; |
540 |
+ |
if (AParent <> nil) and (AParent is TCustomForm)then |
541 |
+ |
Result := TCustomForm(AParent).ActiveControl; |
542 |
+ |
end; |
543 |
+ |
|
544 |
|
procedure TDBControlGrid.EmptyGrid; |
545 |
|
var |
546 |
|
OldFixedRows: Integer; |
547 |
|
begin |
548 |
|
OldFixedRows := FixedRows; |
549 |
|
Clear; |
550 |
+ |
FRowCache.ClearCache; |
551 |
|
RowCount := OldFixedRows + 1; |
552 |
|
if dgpIndicator in FOptions then |
553 |
|
ColWidths[0]:=12; |
600 |
|
and ValidDataSet and FDatalink.DataSet.CanModify; |
601 |
|
end; |
602 |
|
|
583 |
– |
|
603 |
|
procedure TDBControlGrid.DoDrawRow(aRow: integer; aRect: TRect; |
604 |
|
aState: TGridDrawState); |
605 |
|
var CachedRow: TBitmap; |
612 |
|
begin |
613 |
|
FCacheRefreshQueued := true; |
614 |
|
Application.QueueAsyncCall(@DoMoveRecord,PtrInt(aRow)); |
615 |
< |
end |
615 |
> |
end; |
616 |
> |
Canvas.FillRect(aRect); |
617 |
|
end |
618 |
|
else |
619 |
|
Canvas.Draw(aRect.Left,aRect.Top,CachedRow) |
628 |
|
aRow := integer(Data); |
629 |
|
FInCacheRefresh := true; |
630 |
|
if assigned(FDataLink.DataSet) then |
631 |
< |
FDatalink.DataSet.MoveBy(aRow - FDrawRow) |
631 |
> |
FDatalink.DataSet.MoveBy(aRow - FDrawRow); |
632 |
|
end; |
633 |
|
|
634 |
|
procedure TDBControlGrid.DoSetupDrawPanel(Data: PtrInt); |
664 |
|
procedure TDBControlGrid.KeyDownHandler(Sender: TObject; var Key: Word; |
665 |
|
Shift: TShiftState); |
666 |
|
var Done: boolean; |
667 |
+ |
AControl: TControl; |
668 |
|
begin |
669 |
< |
if Visible and assigned(FDrawPanel) and FDrawPanel.Visible and FWeHaveFocus then |
669 |
> |
if Visible and assigned(FDrawPanel) and FDrawPanel.Visible and FWeHaveFocus |
670 |
> |
and (Self.Owner=Screen.ActiveForm) then |
671 |
|
begin |
672 |
+ |
AControl := ActiveControl; |
673 |
+ |
if (AControl <> nil) and (AControl is TCustomComboBox) |
674 |
+ |
and ((Key in [VK_UP,VK_DOWN]) or |
675 |
+ |
(TCustomComboBox(AControl).DroppedDown and (Key = VK_RETURN)) or |
676 |
+ |
((TCustomComboBox(AControl).Text <> '') and (Key = VK_ESCAPE))) then |
677 |
+ |
Exit; {ignore these keys if we are in a combobox} |
678 |
+ |
|
679 |
+ |
if (AControl <> nil) and (AControl is TCustomMemo) |
680 |
+ |
and (Key in [VK_RETURN,VK_UP,VK_DOWN]) then Exit; {Ignore Return in a CustomMemo} |
681 |
+ |
|
682 |
+ |
if (AControl <> nil) and (AControl is TCustomGrid) |
683 |
+ |
and (Key in [VK_RETURN,VK_UP,VK_DOWN,VK_TAB]) then Exit; {Ignore Return in a CustomMemo} |
684 |
+ |
|
685 |
+ |
if (AControl <> nil) and ((AControl is TDateEdit) or (AControl is TCustomMaskedit)) |
686 |
+ |
and (Key in [VK_RETURN,VK_UP,VK_DOWN, |
687 |
+ |
VK_ESCAPE,VK_LEFT,VK_RIGHT]) then Exit; {Ignore Return in a CustomMemo} |
688 |
|
Done := false; |
689 |
|
if assigned(FOnKeyDownHander) then |
690 |
|
OnKeyDownHander(Sender,Key,Shift,Done); |
706 |
|
and (FModified or (FRowCache.IsEmpty(aDataSet.RecNo))) then |
707 |
|
begin |
708 |
|
RecNo := aDataSet.RecNo; |
671 |
– |
Application.ProcessMessages; {A couple of trips round the message loop seems to be necessary} |
709 |
|
Application.ProcessMessages; |
710 |
|
if RecNo = aDataSet.RecNo then {Guard against sudden changes} |
711 |
< |
FRowCache.Add2Cache(aDataSet.RecNo,FDrawPanel); |
711 |
> |
FRowCache.Add2Cache(RecNo,FDrawPanel); |
712 |
|
end; |
713 |
|
end; |
714 |
|
|
715 |
|
procedure TDBControlGrid.OnDataSetChanged(aDataSet: TDataSet); |
716 |
|
begin |
717 |
< |
if (aDataSet.State = dsBrowse) and (FLastRecordCount > GetRecordCount) then |
717 |
> |
if aDataSet.State = dsBrowse then |
718 |
|
begin |
719 |
< |
{must be delete} |
720 |
< |
FRowCache.MarkAsDeleted(FSelectedRecNo); |
721 |
< |
Dec(FSelectedRow); |
719 |
> |
if GetRecordCount = 0 then |
720 |
> |
begin |
721 |
> |
{Must be closed/reopened} |
722 |
> |
FRowCache.ClearCache; |
723 |
> |
FSelectedRow := 0; |
724 |
> |
end |
725 |
> |
else |
726 |
> |
if FLastRecordCount > GetRecordCount then |
727 |
> |
begin |
728 |
> |
{must be delete} |
729 |
> |
FRowCache.MarkAsDeleted(FSelectedRecNo); |
730 |
> |
Dec(FSelectedRow); |
731 |
> |
end; |
732 |
|
LayoutChanged; |
733 |
|
end; |
734 |
|
FLastRecordCount := GetRecordCount; |
867 |
|
|
868 |
|
procedure TDBControlGrid.SetupDrawPanel(aRow: integer); |
869 |
|
begin |
870 |
+ |
if FDrawPanel = nil then Exit; |
871 |
|
if ValidDataSet and FRowCache.AlternateColor[FDataLink.DataSet.RecNo] then |
872 |
|
FDrawPanel.Color := AlternateColor |
873 |
|
else |
1179 |
|
result := dsInactive; |
1180 |
|
end; |
1181 |
|
|
1134 |
– |
var |
1135 |
– |
DataCol: Integer; |
1182 |
|
begin |
1183 |
|
PrepareCanvas(aCol, aRow, aState); |
1184 |
|
|
1185 |
|
if aCol < FixedCols then |
1186 |
|
DrawIndicator(Canvas,aRow, aRect,GetDataSetState,false) |
1187 |
|
else |
1188 |
< |
if FDrawPanel = nil then |
1189 |
< |
DrawFillRect(Canvas,aRect) else |
1188 |
> |
if (FDrawPanel = nil) or not FDataLink.Active then |
1189 |
> |
DrawFillRect(Canvas,aRect) |
1190 |
> |
else |
1191 |
|
if not FDrawingActiveRecord and FDataLink.Active then |
1192 |
|
DoDrawRow(aRow,aRect,aState); |
1193 |
|
{if we are drawing the active record then this is rendered by the Draw Panel |
1289 |
|
procedure TDBControlGrid.KeyDown(var Key: Word; Shift: TShiftState); |
1290 |
|
type |
1291 |
|
TOperation=(opMoveBy,opCancel,opAppend,opInsert,opDelete); |
1245 |
– |
var |
1246 |
– |
DeltaCol,DeltaRow: Integer; |
1292 |
|
|
1293 |
|
procedure DoOnKeyDown; |
1294 |
|
begin |
1431 |
|
FInCacheRefresh := false; |
1432 |
|
FCacheRefreshQueued := false; |
1433 |
|
Row := FixedRows; |
1434 |
+ |
FDrawingActiveRecord := false; |
1435 |
+ |
FSelectedRecNo := 0; |
1436 |
+ |
FSelectedRow := 0; |
1437 |
+ |
FRequiredRecNo := 0; |
1438 |
|
end; |
1439 |
|
FRowCache.UseAlternateColors := AlternateColor <> Color; |
1440 |
|
FRowCache.AltColorStartNormal := AltColorStartNormal; |
1690 |
|
FDataLink.Free; |
1691 |
|
end; |
1692 |
|
if assigned(FRowCache) then FRowCache.Free; |
1693 |
+ |
if not (csDesigning in ComponentState) then |
1694 |
+ |
Application.RemoveOnKeyDownBeforeHandler( @KeyDownHandler ); |
1695 |
+ |
Application.RemoveAsyncCalls(self); |
1696 |
|
inherited Destroy; |
1697 |
|
end; |
1698 |
|
|
1731 |
|
end; |
1732 |
|
|
1733 |
|
end. |
1734 |
+ |
|