1 |
+ |
|
2 |
|
{ |
3 |
|
/*************************************************************************** |
4 |
|
DBControlGrid.pas |
98 |
|
procedure ClearCache; |
99 |
|
function Add2Cache(RecNo: Longint; Control: TWinControl): TBitmap; |
100 |
|
function GetRowImage(RecNo, Offset: integer): TBitmap; |
101 |
+ |
procedure InvalidateRowImage(RecNo: integer); |
102 |
|
function IsEmpty(RecNo: integer): boolean; |
103 |
|
procedure MarkAsDeleted(RecNo: integer); |
104 |
|
property AlternateColor[RecNo: integer]: boolean read GetAlternateColor; |
153 |
|
FLastMouseButton: TMouseButton; |
154 |
|
FLastMouseShiftState: TShiftState; |
155 |
|
|
156 |
+ |
function ActiveControl: TControl; |
157 |
|
procedure EmptyGrid; |
158 |
|
function GetDataSource: TDataSource; |
159 |
|
function GetRecordCount: Integer; |
161 |
|
function GridCanModify: boolean; |
162 |
|
procedure DoDrawRow(aRow: integer; aRect: TRect; aState: TGridDrawState); |
163 |
|
procedure DoMoveRecord(Data: PtrInt); |
164 |
+ |
procedure DoSelectNext(Data: PtrInt); |
165 |
|
procedure DoScrollDataSet(Data: PtrInt); |
166 |
|
procedure DoSetupDrawPanel(Data: PtrInt); |
167 |
|
procedure DoSendMouseClicks(Data: PtrInt); |
239 |
|
property Color; |
240 |
|
property Constraints; |
241 |
|
property DataSource: TDataSource read GetDataSource write SetDataSource; |
238 |
– |
property DefaultRowHeight; |
242 |
|
property DefaultPositionAtEnd: boolean read FDefaultPositionAtEnd write FDefaultPositionAtEnd; |
243 |
|
property DragCursor; |
244 |
|
property DragMode; |
286 |
|
|
287 |
|
implementation |
288 |
|
|
289 |
< |
uses LCLType, Math, LCLIntf, Forms, LCLMessageGlue; |
289 |
> |
uses LCLType, Math, LCLIntf, Forms, LCLMessageGlue, EditBtn, MaskEdit; |
290 |
|
|
291 |
|
{ TDBControlGridDataLink } |
292 |
|
|
301 |
|
|
302 |
|
function TRowCache.Render(Control: TWinControl): TBitmap; |
303 |
|
var Container: TBitmap; |
301 |
– |
Msg: TLMPaint; |
304 |
|
begin |
305 |
|
Container := TBitmap.Create; |
306 |
|
try |
366 |
|
for i := StartIndex to Length(FList) - 1 do |
367 |
|
begin |
368 |
|
FList[i].FState := rcEmpty; |
369 |
+ |
FList[i].FBitmap := nil; |
370 |
|
FList[i].FAlternateColor := altColor; |
371 |
|
if UseAlternateColors then |
372 |
|
altColor := not altColor; |
414 |
|
procedure TRowCache.ClearCache; |
415 |
|
begin |
416 |
|
FreeImages(true); |
417 |
+ |
SetLength(FList,0); |
418 |
|
end; |
419 |
|
|
420 |
|
function TRowCache.Add2Cache(RecNo: Longint; Control: TWinControl): TBitmap; |
423 |
|
Dec(RecNo); {Adust to zero base} |
424 |
|
ExtendCache(RecNo + 1); |
425 |
|
FList[RecNo].FState := rcPresent; |
426 |
+ |
if FList[RecNo].FBitmap <> nil then |
427 |
+ |
FList[RecNo].FBitmap.Free; |
428 |
|
FList[RecNo].FBitmap := Render(Control); |
429 |
|
Result := FList[RecNo].FBitmap; |
430 |
|
end; |
472 |
|
until false; |
473 |
|
end; |
474 |
|
|
475 |
+ |
procedure TRowCache.InvalidateRowImage(RecNo: integer); |
476 |
+ |
begin |
477 |
+ |
Dec(RecNo); {adjust to zero base} |
478 |
+ |
if (RecNo < 0) or (RecNo >= Length(FList)) then |
479 |
+ |
Exit; |
480 |
+ |
|
481 |
+ |
if FList[RecNo].FState = rcPresent then |
482 |
+ |
begin |
483 |
+ |
FList[RecNo].FBitmap.Free; |
484 |
+ |
FList[RecNo].FBitmap := nil; |
485 |
+ |
FList[RecNo].FState := rcEmpty; |
486 |
+ |
end; |
487 |
+ |
end; |
488 |
+ |
|
489 |
|
function TRowCache.IsEmpty(RecNo: integer): boolean; |
490 |
|
begin |
491 |
|
Dec(RecNo); |
528 |
|
|
529 |
|
{ TDBControlGrid } |
530 |
|
|
531 |
+ |
function TDBControlGrid.ActiveControl: TControl; |
532 |
+ |
var AParent: TWinControl; |
533 |
+ |
begin |
534 |
+ |
Result := nil; |
535 |
+ |
AParent := Parent; |
536 |
+ |
while (AParent <> nil) and not (AParent is TCustomForm) do |
537 |
+ |
AParent := AParent.Parent; |
538 |
+ |
if (AParent <> nil) and (AParent is TCustomForm)then |
539 |
+ |
Result := TCustomForm(AParent).ActiveControl; |
540 |
+ |
end; |
541 |
+ |
|
542 |
|
procedure TDBControlGrid.EmptyGrid; |
543 |
|
var |
544 |
|
OldFixedRows: Integer; |
545 |
|
begin |
546 |
|
OldFixedRows := FixedRows; |
547 |
|
Clear; |
548 |
+ |
FRowCache.ClearCache; |
549 |
|
RowCount := OldFixedRows + 1; |
550 |
|
if dgpIndicator in FOptions then |
551 |
|
ColWidths[0]:=12; |
598 |
|
and ValidDataSet and FDatalink.DataSet.CanModify; |
599 |
|
end; |
600 |
|
|
569 |
– |
|
601 |
|
procedure TDBControlGrid.DoDrawRow(aRow: integer; aRect: TRect; |
602 |
|
aState: TGridDrawState); |
603 |
|
var CachedRow: TBitmap; |
610 |
|
begin |
611 |
|
FCacheRefreshQueued := true; |
612 |
|
Application.QueueAsyncCall(@DoMoveRecord,PtrInt(aRow)); |
613 |
< |
end |
613 |
> |
end; |
614 |
> |
Canvas.FillRect(aRect); |
615 |
|
end |
616 |
|
else |
617 |
|
Canvas.Draw(aRect.Left,aRect.Top,CachedRow) |
626 |
|
aRow := integer(Data); |
627 |
|
FInCacheRefresh := true; |
628 |
|
if assigned(FDataLink.DataSet) then |
629 |
< |
FDatalink.DataSet.MoveBy(aRow - FDrawRow) |
629 |
> |
FDatalink.DataSet.MoveBy(aRow - FDrawRow); |
630 |
|
end; |
631 |
|
|
632 |
|
procedure TDBControlGrid.DoSetupDrawPanel(Data: PtrInt); |
662 |
|
procedure TDBControlGrid.KeyDownHandler(Sender: TObject; var Key: Word; |
663 |
|
Shift: TShiftState); |
664 |
|
var Done: boolean; |
665 |
+ |
AControl: TControl; |
666 |
|
begin |
667 |
|
if Visible and assigned(FDrawPanel) and FDrawPanel.Visible and FWeHaveFocus then |
668 |
|
begin |
669 |
+ |
AControl := ActiveControl; |
670 |
+ |
if (AControl <> nil) and (AControl is TCustomComboBox) |
671 |
+ |
and ((Key in [VK_UP,VK_DOWN]) or |
672 |
+ |
(TCustomComboBox(AControl).DroppedDown and (Key = VK_RETURN)) or |
673 |
+ |
((TCustomComboBox(AControl).Text <> '') and (Key = VK_ESCAPE))) then |
674 |
+ |
Exit; {ignore these keys if we are in a combobox} |
675 |
+ |
|
676 |
+ |
if (AControl <> nil) and (AControl is TCustomMemo) |
677 |
+ |
and (Key in [VK_RETURN,VK_UP,VK_DOWN]) then Exit; {Ignore Return in a CustomMemo} |
678 |
+ |
|
679 |
+ |
if (AControl <> nil) and (AControl is TCustomGrid) |
680 |
+ |
and (Key in [VK_RETURN,VK_UP,VK_DOWN,VK_TAB]) then Exit; {Ignore Return in a CustomMemo} |
681 |
+ |
|
682 |
+ |
if (AControl <> nil) and ((AControl is TDateEdit) or (AControl is TCustomMaskedit)) |
683 |
+ |
and (Key in [VK_RETURN,VK_UP,VK_DOWN, |
684 |
+ |
VK_ESCAPE,VK_LEFT,VK_RIGHT]) then Exit; {Ignore Return in a CustomMemo} |
685 |
|
Done := false; |
686 |
|
if assigned(FOnKeyDownHander) then |
687 |
|
OnKeyDownHander(Sender,Key,Shift,Done); |
703 |
|
and (FModified or (FRowCache.IsEmpty(aDataSet.RecNo))) then |
704 |
|
begin |
705 |
|
RecNo := aDataSet.RecNo; |
657 |
– |
Application.ProcessMessages; {A couple of trips round the message loop seems to be necessary} |
706 |
|
Application.ProcessMessages; |
707 |
|
if RecNo = aDataSet.RecNo then {Guard against sudden changes} |
708 |
< |
FRowCache.Add2Cache(aDataSet.RecNo,FDrawPanel); |
708 |
> |
FRowCache.Add2Cache(RecNo,FDrawPanel); |
709 |
|
end; |
710 |
|
end; |
711 |
|
|
712 |
|
procedure TDBControlGrid.OnDataSetChanged(aDataSet: TDataSet); |
713 |
|
begin |
714 |
< |
if (aDataSet.State = dsBrowse) and (FLastRecordCount > GetRecordCount) then |
714 |
> |
if aDataSet.State = dsBrowse then |
715 |
|
begin |
716 |
< |
{must be delete} |
717 |
< |
FRowCache.MarkAsDeleted(FSelectedRecNo); |
718 |
< |
Dec(FSelectedRow); |
716 |
> |
if GetRecordCount = 0 then |
717 |
> |
begin |
718 |
> |
{Must be closed/reopened} |
719 |
> |
FRowCache.ClearCache; |
720 |
> |
FSelectedRow := 0; |
721 |
> |
end |
722 |
> |
else |
723 |
> |
if FLastRecordCount > GetRecordCount then |
724 |
> |
begin |
725 |
> |
{must be delete} |
726 |
> |
FRowCache.MarkAsDeleted(FSelectedRecNo); |
727 |
> |
Dec(FSelectedRow); |
728 |
> |
end; |
729 |
|
LayoutChanged; |
730 |
|
end; |
731 |
|
FLastRecordCount := GetRecordCount; |
732 |
|
if aDataSet.State = dsInsert then |
733 |
+ |
begin |
734 |
|
FRequiredRecNo := aDataSet.RecNo + 1; |
735 |
+ |
Application.QueueAsyncCall(@DoSelectNext,0); |
736 |
+ |
end; |
737 |
|
UpdateActive |
738 |
|
end; |
739 |
|
|
822 |
|
RemoveFreeNotification(FDrawPanel); |
823 |
|
FDrawPanel.RemoveAllHandlersOfObject(self); |
824 |
|
theForm := Parent; |
825 |
< |
while not (theForm is TCustomForm) and (theForm.Parent <> nil) do |
825 |
> |
while not ((theForm is TCustomForm) or (theForm is TCustomFrame)) |
826 |
> |
and (theForm.Parent <> nil) do |
827 |
|
theForm := theForm.Parent; |
828 |
|
FDrawPanel.Parent := theForm; |
829 |
|
end; |
840 |
|
FDrawPanel.Visible := false; |
841 |
|
FRowCache.Height := FDrawPanel.Height; |
842 |
|
FRowCache.Width := FDrawPanel.Width; |
843 |
< |
AddHandlerOnResize(@OnDrawPanelResize); |
843 |
> |
FDrawPanel.AddHandlerOnResize(@OnDrawPanelResize); |
844 |
|
FreeNotification(FDrawPanel); |
845 |
|
end; |
846 |
|
except |
864 |
|
|
865 |
|
procedure TDBControlGrid.SetupDrawPanel(aRow: integer); |
866 |
|
begin |
867 |
+ |
if FDrawPanel = nil then Exit; |
868 |
|
if ValidDataSet and FRowCache.AlternateColor[FDataLink.DataSet.RecNo] then |
869 |
|
FDrawPanel.Color := AlternateColor |
870 |
|
else |
1129 |
|
FDataLink.DataSet.MoveBy(integer(Data) - FDataLink.DataSet.RecNo); |
1130 |
|
end; |
1131 |
|
|
1132 |
+ |
procedure TDBControlGrid.DoSelectNext(Data: PtrInt); |
1133 |
+ |
begin |
1134 |
+ |
FDataLink.DataSet.MoveBy(1); |
1135 |
+ |
end; |
1136 |
+ |
|
1137 |
|
procedure TDBControlGrid.DrawAllRows; |
1138 |
|
begin |
1139 |
|
inherited DrawAllRows; |
1184 |
|
if aCol < FixedCols then |
1185 |
|
DrawIndicator(Canvas,aRow, aRect,GetDataSetState,false) |
1186 |
|
else |
1187 |
+ |
if (FDrawPanel = nil) or not FDataLink.Active then |
1188 |
+ |
DrawFillRect(Canvas,aRect) |
1189 |
+ |
else |
1190 |
|
if not FDrawingActiveRecord and FDataLink.Active then |
1191 |
|
DoDrawRow(aRow,aRect,aState); |
1192 |
|
{if we are drawing the active record then this is rendered by the Draw Panel |
1432 |
|
FInCacheRefresh := false; |
1433 |
|
FCacheRefreshQueued := false; |
1434 |
|
Row := FixedRows; |
1435 |
+ |
FDrawingActiveRecord := false; |
1436 |
+ |
FSelectedRecNo := 0; |
1437 |
+ |
FSelectedRow := 0; |
1438 |
+ |
FRequiredRecNo := 0; |
1439 |
|
end; |
1440 |
|
FRowCache.UseAlternateColors := AlternateColor <> Color; |
1441 |
|
FRowCache.AltColorStartNormal := AltColorStartNormal; |
1625 |
|
PrevRow := Row; |
1626 |
|
Row := FDrawRow; |
1627 |
|
if not FInCacheRefresh then |
1628 |
+ |
begin |
1629 |
|
FSelectedRow := FDrawRow; |
1630 |
+ |
if FDatalink.DataSet.State <> dsInsert then |
1631 |
+ |
FRowCache.InvalidateRowImage(FSelectedRecNo); |
1632 |
+ |
end; |
1633 |
|
InvalidateRow(PrevRow); |
1634 |
|
SetupDrawPanel(FDrawRow); |
1635 |
|
end; |
1691 |
|
FDataLink.Free; |
1692 |
|
end; |
1693 |
|
if assigned(FRowCache) then FRowCache.Free; |
1694 |
+ |
Application.RemoveAsyncCalls(self); |
1695 |
|
inherited Destroy; |
1696 |
|
end; |
1697 |
|
|
1730 |
|
end; |
1731 |
|
|
1732 |
|
end. |
1733 |
+ |
|