From 0c95cf6607e1c6163ad410edafb0abb4ef019668 Mon Sep 17 00:00:00 2001 From: jesus Date: Fri, 7 Sep 2018 01:20:37 +0000 Subject: [PATCH] LCL, Implements DBCombobox and DBLookupCombobox cancelling dataset edit state on ESC if it is the source of editing. git-svn-id: trunk@58899 - --- lcl/dbctrls.pp | 34 ++++++++++++++++++++++++++++---- lcl/include/customdbcombobox.inc | 23 +++++++++++++++++++-- lcl/include/dbcombobox.inc | 11 ----------- lcl/include/dblookupcombobox.inc | 3 +++ 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/lcl/dbctrls.pp b/lcl/dbctrls.pp index 3b2104bebe..ec5a1f8ff0 100644 --- a/lcl/dbctrls.pp +++ b/lcl/dbctrls.pp @@ -52,6 +52,8 @@ Type FOnActiveChange: TNotifyEvent; // Curent State of Affairs FEditing: Boolean; + FEditingSourceSet: boolean; + FEditingSource: Boolean; IsModified: Boolean; function FieldCanModify: boolean; function IsKeyField(aField: TField): Boolean; @@ -61,6 +63,7 @@ Type procedure UpdateField; // make sure the field/fieldname is valid before we do stuff with it procedure ValidateField; + procedure ResetEditingSource; protected // Testing Events procedure ActiveChanged; override; @@ -88,6 +91,7 @@ Type // Current State of DB property CanModify: Boolean read GetCanModify; property Editing: Boolean read FEditing; + property EditingSource: boolean read FEditingSource; // Our Callbacks property OnDataChange: TNotifyEvent read FOnDataChange write FOnDataChange; @@ -735,7 +739,9 @@ Type procedure DataChange(Sender: TObject); virtual; abstract; procedure Notification(AComponent: TComponent; Operation: TOperation); override; procedure Change; override; + procedure KeyDown(var Key: Word; Shift: TShiftState); override; procedure UpdateData(Sender: TObject); virtual; abstract; + procedure UpdateRecord; procedure WndProc(var Message: TLMessage); override; public constructor Create(TheOwner: TComponent); override; @@ -757,7 +763,6 @@ Type TDBComboBox = class(TCustomDBComboBox) protected procedure DataChange(Sender: TObject); override; - procedure KeyDown(var Key: Word; Shift: TShiftState); override; procedure KeyPress(var Key: char); override; procedure UpdateData(Sender: TObject); override; published @@ -862,6 +867,7 @@ Type procedure Loaded; override; procedure UpdateData(Sender: TObject); override; procedure DataChange(Sender: TObject); override; + procedure DoOnSelect; override; public constructor Create(AOwner: TComponent); override; property KeyValue: variant read GetKeyValue write SetKeyValue; @@ -1628,6 +1634,11 @@ begin UpdateField; end; +procedure TFieldDataLink.ResetEditingSource; +begin + FEditingSource := false; + FEditingSourceSet := false; +end; {TFieldDataLink Protected Methods} @@ -1688,7 +1699,10 @@ begin begin FEditing := RealEditState; if not FEditing then + begin IsModified := False; + ResetEditingSource; + end; if Assigned(FOnEditingChange) then FOnEditingChange(Self); end; @@ -1808,11 +1822,22 @@ end; better please fix. } function TFieldDataLink.Edit: Boolean; +var + editingSrc: Boolean; begin + editingSrc := (not FEditing) and (Dataset<>nil) and not(Dataset.State in dsEditModes); + if (not FEditing) and CanModify then inherited Edit; Result := FEditing; + + if not FEditingSourceSet then + begin + // should be triggered one time only if editing succeeded + FEditingSource := FEditing and editingSrc; + FEditingSourceSet := true; + end; end; { Delphi Help -> @@ -1855,12 +1880,13 @@ begin FOnDataChange(Self); IsModified := False; + ResetEditingSource; end; CONST - DBCBEVENT_CHANGE = 1; // CustomDBBoxCombobox Detected change event - DBCBEVENT_SELECT = 2; // CustomDBBoxCombobox Detected select event - DBCBEVENT_CLOSEUP = 4; // CustomDBBoxCombobox Detected closeup event + DBCBEVENT_CHANGE = 1; // CustomDBCombobox Detected change event + DBCBEVENT_SELECT = 2; // CustomDBCombobox Detected select event + DBCBEVENT_CLOSEUP = 4; // CustomDBCombobox Detected closeup event {$Include dblookup.inc} {$Include dbedit.inc} diff --git a/lcl/include/customdbcombobox.inc b/lcl/include/customdbcombobox.inc index 790ab6db85..f707665767 100644 --- a/lcl/include/customdbcombobox.inc +++ b/lcl/include/customdbcombobox.inc @@ -38,6 +38,26 @@ begin PostMessage(Handle, LM_DEFERREDEDIT, 0, 0); end; +procedure TCustomDBComboBox.KeyDown(var Key: Word; Shift: TShiftState); +begin + inherited KeyDown(Key, Shift); + if Key=VK_ESCAPE then begin + //cancel out of editing by reset on esc + if FDatalink.EditingSource then + FDatalink.DataSet.Cancel + else + FDataLink.Reset; + SelectAll; + Key := VK_UNKNOWN; + end; +end; + +procedure TCustomDBComboBox.UpdateRecord; +begin + if FDatalink.Editing and FDatalink.IsModified then + FDataLink.UpdateRecord; +end; + function TCustomDBComboBox.GetReadOnly: Boolean; begin Result:=FDataLink.ReadOnly; @@ -162,8 +182,7 @@ end; procedure TCustomDBComboBox.EditingDone; begin - if FDatalink.Editing and FDatalink.IsModified then - FDataLink.UpdateRecord; + UpdateRecord; inherited EditingDone; end; diff --git a/lcl/include/dbcombobox.inc b/lcl/include/dbcombobox.inc index dbbbccdf49..7ea782eed0 100644 --- a/lcl/include/dbcombobox.inc +++ b/lcl/include/dbcombobox.inc @@ -28,17 +28,6 @@ begin Text := ''; end; -procedure TDBComboBox.KeyDown(var Key: Word; Shift: TShiftState); -begin - inherited KeyDown(Key, Shift); - if Key=VK_ESCAPE then begin - //cancel out of editing by reset on esc - FDataLink.Reset; - SelectAll; - Key := VK_UNKNOWN; - end; -end; - procedure TDBComboBox.KeyPress(var Key: char); begin inherited KeyPress(Key); diff --git a/lcl/include/dblookupcombobox.inc b/lcl/include/dblookupcombobox.inc index 5c5cea89ab..9b77c04413 100644 --- a/lcl/include/dblookupcombobox.inc +++ b/lcl/include/dblookupcombobox.inc @@ -34,6 +34,9 @@ begin i := ItemIndex; if i <> -1 then FLookup.UpdateData(i, FScrollListDataset) + else + if FDatalink.EditingSource then + FDatalink.Dataset.Cancel else UpdateLookup; end;