150 |
|
FGrid: TCustomGrid; |
151 |
|
FCol,FRow: Integer; |
152 |
|
FEditText: string; |
153 |
+ |
function EditingKeyField: boolean; |
154 |
|
protected |
155 |
|
procedure WndProc(var TheMessage : TLMessage); override; |
156 |
|
procedure CloseUp; override; |
167 |
|
property OnEditingDone; |
168 |
|
end; |
169 |
|
|
170 |
+ |
TOnSelectPanelEditor = procedure(Sender: TObject; var aEditorPanel: TWinControl) of object; |
171 |
+ |
|
172 |
|
TDBDynamicGrid = class(TDBGrid) |
173 |
|
private |
174 |
|
{ Private declarations } |
179 |
|
FOnEditorPanelHide: TNotifyEvent; |
180 |
|
FOnEditorPanelShow: TNotifyEvent; |
181 |
|
FOnKeyDownHander: TKeyDownHandler; |
182 |
+ |
FOnSelectPanelEditor: TOnSelectPanelEditor; |
183 |
|
FResizing: boolean; |
184 |
|
FWeHaveFocus: boolean; |
181 |
– |
FHidingEditorPanel: boolean; |
182 |
– |
FAllowHide: boolean; |
185 |
|
FMouseDown: boolean; |
186 |
|
function ActiveControl: TControl; |
187 |
|
procedure DoShowEditorPanel(Data: PtrInt); |
225 |
|
property OnEditorPanelShow: TNotifyEvent read FOnEditorPanelShow write FOnEditorPanelShow; |
226 |
|
property OnEditorPanelHide: TNotifyEvent read FOnEditorPanelHide write FOnEditorPanelHide; |
227 |
|
property OnKeyDownHander: TKeyDownHandler read FOnKeyDownHander write FOnKeyDownHander; |
228 |
+ |
property OnSelectPanelEditor: TOnSelectPanelEditor read FOnSelectPanelEditor |
229 |
+ |
write FOnSelectPanelEditor; |
230 |
|
end; |
231 |
|
|
232 |
|
{TIBGridControlLink} |
263 |
|
FBookmark: TLocationArray; |
264 |
|
FDBLookupCellEditor: TDBLookupCellEditor; |
265 |
|
FActive: boolean; |
266 |
+ |
FFieldPosition: integer; |
267 |
|
procedure ColumnHeaderClick(Index: integer); |
268 |
|
function GetDataSource: TDataSource; |
269 |
|
function GetEditorBorderStyle: TBorderStyle; |
309 |
|
|
310 |
|
implementation |
311 |
|
|
312 |
< |
uses Math, IBQuery, LCLType; |
312 |
> |
uses LCLType, Variants, EditBtn; |
313 |
|
|
314 |
|
{ TIBGridControlLink } |
315 |
|
|
375 |
|
else |
376 |
|
begin |
377 |
|
for I := 0 to ColCount - 1 do |
378 |
+ |
if (I < FixedCols) or Columns[I - FixedCols].Visible then |
379 |
|
ColSum := ColSum + ColWidths[I]; |
380 |
|
|
381 |
|
if Colsum <> ClientWidth then |
382 |
|
begin |
383 |
|
ResizeColCount := 0; |
384 |
|
for I := 0 to Columns.Count -1 do |
385 |
< |
if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn then |
385 |
> |
if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn and Columns[I].Visible then |
386 |
|
begin |
387 |
|
Inc(ResizeColCount); |
388 |
|
Colsum := Colsum + TDBDynamicGridColumn(Columns[I]).DesignWidth - Columns[I].Width; |
395 |
|
n := (ClientWidth - ColSum) mod ResizeColCount; |
396 |
|
|
397 |
|
for I := 0 to Columns.Count -1 do |
398 |
< |
if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn then |
398 |
> |
if TDBDynamicGridColumn(Columns[I]).AutoSizeColumn and Columns[I].Visible then |
399 |
|
begin |
400 |
|
if I = 0 then |
401 |
|
Columns[I].Width := Columns[I].Width + adjustment + n |
566 |
|
Exit; {ignore these keys if we are in a combobox} |
567 |
|
|
568 |
|
if (AControl <> nil) and (AControl is TCustomMemo) |
569 |
< |
and (Key in [VK_RETURN,VK_UP,VK_DOWN]) then Exit; {Ignore Return in a CustomMemo} |
569 |
> |
and (Key in [VK_RETURN,VK_UP,VK_DOWN]) then Exit; {Ignore keys in a CustomMemo} |
570 |
|
|
571 |
|
if (AControl <> nil) and (AControl is TCustomGrid) |
572 |
< |
and (Key in [VK_RETURN,VK_UP,VK_DOWN,VK_TAB]) then Exit; {Ignore Return in a CustomMemo} |
572 |
> |
and (Key in [VK_RETURN,VK_UP,VK_DOWN,VK_TAB]) then Exit; {Ignore keys in a Custom Grid} |
573 |
> |
|
574 |
> |
if (AControl <> nil) and (AControl is TEBEdit) and (AControl.Owner is TDateEdit) then |
575 |
> |
begin |
576 |
> |
if (Key in [VK_LEFT,VK_RIGHT]) then Exit; {Ignore navigation keys} |
577 |
> |
if TDateEdit(AControl.Owner).DroppedDown and |
578 |
> |
(Key in [VK_RETURN,VK_UP,VK_DOWN,VK_ESCAPE]) then Exit; {Ignore TCalender navigation keys in a Data edit} |
579 |
> |
end; |
580 |
|
|
581 |
|
if assigned(FOnKeyDownHander) then |
582 |
|
OnKeyDownHander(Sender,Key,Shift,Done); |
621 |
|
if FEditorPanel <> nil then |
622 |
|
RemoveFreeNotification(FEditorPanel); |
623 |
|
FEditorPanel := AValue; |
624 |
< |
FreeNotification(FEditorPanel); |
624 |
> |
if FEditorPanel <> nil then |
625 |
> |
FreeNotification(FEditorPanel); |
626 |
|
end; |
627 |
|
|
628 |
|
procedure TDBDynamicGrid.ChangeBounds(ALeft, ATop, AWidth, AHeight: integer; |
689 |
|
|
690 |
|
procedure TDBDynamicGrid.Notification(AComponent: TComponent; |
691 |
|
Operation: TOperation); |
692 |
+ |
var i: integer; |
693 |
|
begin |
694 |
|
inherited Notification(AComponent, Operation); |
695 |
< |
if (Operation = opRemove) and |
696 |
< |
(AComponent = FEditorPanel) then FEditorPanel := nil; |
695 |
> |
if (Operation = opRemove) and not (csDestroying in ComponentState) then |
696 |
> |
begin |
697 |
> |
if AComponent = FEditorPanel then |
698 |
> |
FEditorPanel := nil |
699 |
> |
else |
700 |
> |
if AComponent is TControl then |
701 |
> |
begin |
702 |
> |
for i := 0 to Columns.Count - 1 do |
703 |
> |
if TDBDynamicGridColumn(Columns[I]).ColumnTotalsControl = AComponent then |
704 |
> |
TDBDynamicGridColumn(Columns[I]).ColumnTotalsControl := nil; |
705 |
> |
end; |
706 |
> |
end |
707 |
|
end; |
708 |
|
|
709 |
|
procedure TDBDynamicGrid.TopLeftChanged; |
716 |
|
begin |
717 |
|
inherited UpdateActive; |
718 |
|
|
719 |
+ |
if not (csLoading in ComponentState) and assigned(DataLink) |
720 |
+ |
and assigned(DataLink.DataSet) and DataLink.DataSet.Active then |
721 |
+ |
DoGridResize; |
722 |
+ |
|
723 |
|
if not (csLoading in ComponentState) and assigned(DataLink) and |
724 |
|
assigned(FEditorPanel) and not FEditorPanel.Visible and |
725 |
|
assigned(DataLink.DataSet) and (DataLink.DataSet.State = dsInsert) then |
758 |
|
end; |
759 |
|
|
760 |
|
procedure TDBDynamicGrid.ShowEditorPanel; |
761 |
+ |
var aEditor: TWinControl; |
762 |
|
begin |
763 |
|
if (csDesigning in ComponentState) or |
764 |
|
(DataSource = nil) or (DataSource.DataSet = nil) |
765 |
|
or ((DataSource.DataSet.RecordCount = 0) and (DataSource.DataSet.State <> dsInsert)) then |
766 |
|
Exit; |
767 |
< |
Editor := FEditorPanel; |
767 |
> |
aEditor := FEditorPanel; |
768 |
> |
if assigned(FOnSelectPanelEditor) then |
769 |
> |
OnSelectPanelEditor(self,aEditor); |
770 |
> |
if FEditorPanel <> aEditor then |
771 |
> |
SetEditorPanel(aEditor); |
772 |
> |
Editor := aEditor; |
773 |
|
EditorMode := true; |
774 |
|
end; |
775 |
|
|
808 |
|
Result := inherited Width |
809 |
|
end; |
810 |
|
|
811 |
+ |
type |
812 |
+ |
THackedGrid = class(TIBDynamicGrid) |
813 |
+ |
public |
814 |
+ |
property FixedCols; |
815 |
+ |
end; |
816 |
+ |
|
817 |
|
{ TDBLookupCellEditor } |
818 |
|
|
819 |
+ |
function TDBLookupCellEditor.EditingKeyField: boolean; |
820 |
+ |
begin |
821 |
+ |
with TIBDynamicGridColumn(TDBGrid(FGrid).Columns[FCol - THackedGrid(FGrid).FixedCols]) do |
822 |
+ |
Result := CompareText(FieldName, DBLookupProperties.DataFieldName) = 0; |
823 |
+ |
end; |
824 |
+ |
|
825 |
|
procedure TDBLookupCellEditor.WndProc(var TheMessage: TLMessage); |
826 |
|
begin |
827 |
|
if TheMessage.msg=LM_KILLFOCUS then begin |
840 |
|
UpdateData(nil); {Force Record Update} |
841 |
|
if FGrid<>nil then |
842 |
|
Begin |
843 |
< |
(FGrid as TIBDynamicGrid).EditorTextChanged(FCol, FRow, Trim(Text)); |
843 |
> |
if EditingKeyField then |
844 |
> |
begin |
845 |
> |
if not VarIsNull(KeyValue) then |
846 |
> |
(FGrid as TIBDynamicGrid).EditorTextChanged(FCol, FRow, KeyValue) |
847 |
> |
end |
848 |
> |
else |
849 |
> |
(FGrid as TIBDynamicGrid).EditorTextChanged(FCol, FRow, Trim(Text)); |
850 |
|
(FGrid as TIBDynamicGrid).UpdateData; |
851 |
|
end; |
852 |
|
inherited CloseUp; |
871 |
|
CheckAndInsert; |
872 |
|
Msg.Col := FCol; |
873 |
|
Msg.Row := FRow; |
874 |
< |
Msg.Value:= Trim(Text); |
874 |
> |
if EditingKeyField then |
875 |
> |
begin |
876 |
> |
if not VarIsNull(KeyValue) then |
877 |
> |
Msg.Value:= KeyValue |
878 |
> |
else |
879 |
> |
Msg.Value:= '' |
880 |
> |
end |
881 |
> |
else |
882 |
> |
Msg.Value:= Trim(Text); |
883 |
|
end; |
884 |
|
|
885 |
|
procedure TDBLookupCellEditor.msg_SetGrid(var Msg: TGridMessage); |
967 |
|
if DataFieldName <> '' then |
968 |
|
Editor.DataSource := TDBGrid(Grid).DataSource; |
969 |
|
end; |
970 |
< |
Editor.Text := Editor.FEditText; |
970 |
> |
if Editor.EditingKeyField then |
971 |
> |
begin |
972 |
> |
if not Field.IsNull then |
973 |
> |
Editor.KeyValue := Editor.FEditText |
974 |
> |
end |
975 |
> |
else |
976 |
> |
Editor.Text := Editor.FEditText; |
977 |
|
Editor.SelStart := Length(Editor.Text); |
978 |
|
end; |
979 |
|
|
998 |
|
destructor TIBDynamicGridColumn.Destroy; |
999 |
|
begin |
1000 |
|
if assigned(FDBLookupProperties) then FDBLookupProperties.Free; |
1001 |
+ |
Application.RemoveAsyncCalls(self); |
1002 |
|
inherited Destroy; |
1003 |
|
end; |
1004 |
|
|
1016 |
|
OnColumnHeaderClick(self,Index); |
1017 |
|
|
1018 |
|
FLastColIndex := Index; |
1019 |
< |
if assigned(DataSource) and assigned(DataSource.DataSet) and DataSource.DataSet.Active then |
1019 |
> |
FFieldPosition := 0; |
1020 |
> |
if assigned(DataSource) and assigned(DataSource.DataSet) and DataSource.DataSet.Active |
1021 |
> |
and (DataSource.DataSet is TIBParserDataSet) then |
1022 |
|
begin |
1023 |
+ |
if FLastColIndex < Columns.Count then |
1024 |
+ |
{try and cache field position while dataset still open} |
1025 |
+ |
FFieldPosition := TIBParserDataSet(DataSource.DataSet).Parser.GetFieldPosition(Columns[FLastColIndex].FieldName); |
1026 |
|
DataSource.DataSet.Active := false; |
1027 |
|
Application.QueueAsyncCall(@DoReopen,0) |
1028 |
|
end; |
1070 |
|
for i := 0 to Columns.Count - 1 do |
1071 |
|
begin |
1072 |
|
if TIBDynamicGridColumn(columns[i]).InitialSortColumn then |
1073 |
+ |
begin |
1074 |
+ |
FFieldPosition := 0; |
1075 |
|
FLastColIndex := i |
1076 |
+ |
end |
1077 |
|
end |
1078 |
|
end; |
1079 |
|
|
1090 |
|
|
1091 |
|
procedure TIBDynamicGrid.UpdateSQL(Sender: TObject; Parser: TSelectSQLParser); |
1092 |
|
var OrderBy: string; |
1017 |
– |
FieldPosition: integer; |
1093 |
|
begin |
1094 |
|
if assigned(DataSource) and assigned(DataSource.DataSet) |
1095 |
|
and (DataSource.DataSet is TIBCustomDataSet) then |
1096 |
|
begin |
1097 |
< |
if (FLastColIndex < 0) or (FLastColIndex >= Columns.Count) then Exit; |
1098 |
< |
FieldPosition := Parser.GetFieldPosition(Columns[FLastColIndex].FieldName); |
1099 |
< |
if FieldPosition > 0 then |
1097 |
> |
if (FFieldPosition = 0) and (FLastColIndex >= 0) and (FLastColIndex < Columns.Count) then |
1098 |
> |
{Not cached - let's hope we can find it before the dataset is opened. |
1099 |
> |
Won't work if dynamic columns} |
1100 |
> |
FFieldPosition := Parser.GetFieldPosition(Columns[FLastColIndex].FieldName); |
1101 |
> |
if FFieldPosition > 0 then |
1102 |
|
begin |
1103 |
|
if Descending then |
1104 |
< |
Parser.OrderByClause := IntToStr(FieldPosition) + ' desc' |
1104 |
> |
Parser.OrderByClause := IntToStr(FFieldPosition) + ' desc' |
1105 |
|
else |
1106 |
< |
Parser.OrderByClause := IntToStr(FieldPosition) + ' asc'; |
1106 |
> |
Parser.OrderByClause := IntToStr(FFieldPosition) + ' asc'; |
1107 |
|
end; |
1108 |
|
|
1109 |
|
if assigned(FOnUpdateSortOrder) then |
1247 |
|
|
1248 |
|
procedure TIBDynamicGrid.Notification(AComponent: TComponent; |
1249 |
|
Operation: TOperation); |
1250 |
+ |
var i: integer; |
1251 |
|
begin |
1252 |
|
inherited Notification(AComponent, Operation); |
1253 |
< |
if (Operation = opRemove) and |
1254 |
< |
(FIBControlLink <> nil) and (AComponent = DataSource) then FIBControlLink.IBDataSet := nil; |
1253 |
> |
if (Operation = opRemove) then |
1254 |
> |
begin |
1255 |
> |
if (FIBControlLink <> nil) and (AComponent = DataSource) then |
1256 |
> |
FIBControlLink.IBDataSet := nil |
1257 |
> |
else |
1258 |
> |
if AComponent is TDataSource then |
1259 |
> |
begin |
1260 |
> |
for i := 0 to Columns.Count - 1 do |
1261 |
> |
if TIBDynamicGridColumn(Columns[I]).DBLookupProperties.ListSource = AComponent then |
1262 |
> |
TIBDynamicGridColumn(Columns[I]).DBLookupProperties.ListSource := nil; |
1263 |
> |
end |
1264 |
> |
end |
1265 |
|
end; |
1266 |
|
|
1267 |
|
procedure TIBDynamicGrid.UpdateActive; |
1291 |
|
if assigned(FIBControlLink) then FIBControlLink.Free; |
1292 |
|
if assigned(FIndexFieldsList) then FIndexFieldsList.Free; |
1293 |
|
if assigned(FDBLookupCellEditor) then FDBLookupCellEditor.Free; |
1294 |
+ |
Application.RemoveAsyncCalls(self); |
1295 |
|
inherited Destroy; |
1296 |
|
end; |
1297 |
|
|