From 12b16151cc9f4f110e1039431adcef09e1a5e236 Mon Sep 17 00:00:00 2001 From: jujibo Date: Sun, 9 Oct 2011 09:48:14 +0000 Subject: [PATCH] Manage properly mouse clicks inside and outside the grid for input values git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2067 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/jujiboutils/src/jdbgridcontrol.pas | 21 +++++- components/jujiboutils/src/jdbgridutils.pas | 73 ++++++++++++++++++- 2 files changed, 89 insertions(+), 5 deletions(-) diff --git a/components/jujiboutils/src/jdbgridcontrol.pas b/components/jujiboutils/src/jdbgridcontrol.pas index 410a75564..c331373a2 100644 --- a/components/jujiboutils/src/jdbgridcontrol.pas +++ b/components/jujiboutils/src/jdbgridcontrol.pas @@ -23,7 +23,7 @@ interface uses Classes, SysUtils, LResources, Forms, Controls, Graphics, DB, Grids, DBGrids, - Dialogs, jdbgridutils; + Dialogs, LCLType, jdbgridutils; type @@ -74,6 +74,9 @@ type function GetDefaultEditor(Column: integer): TWinControl; override; procedure UpdateData; override; property Columns: TJDBGridColumns read GetColumns write SetColumns; + procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: integer); + override; + procedure KeyDown(var Key: word; Shift: TShiftState); override; public { Public declarations } constructor Create(TheOwner: TComponent); override; @@ -193,6 +196,22 @@ begin inherited UpdateData; end; +procedure TJDBGridControl.MouseDown(Button: TMouseButton; Shift: TShiftState; + X, Y: integer); +begin + if integerDbGridControl.CanDefocus and doubleDbGridControl.CanDefocus and + dateTimeDbGridControl.CanDefocus and stringDbGridControl.CanDefocus and + dateDbGridControl.CanDefocus and timeDbGridControl.CanDefocus then + inherited MouseDown(Button, Shift, X, Y) + else + abort; +end; + +procedure TJDBGridControl.KeyDown(var Key: word; Shift: TShiftState); +begin + inherited KeyDown(Key, Shift); +end; + constructor TJDBGridControl.Create(TheOwner: TComponent); begin inherited Create(TheOwner); diff --git a/components/jujiboutils/src/jdbgridutils.pas b/components/jujiboutils/src/jdbgridutils.pas index 84de365ec..40be6a76d 100644 --- a/components/jujiboutils/src/jdbgridutils.pas +++ b/components/jujiboutils/src/jdbgridutils.pas @@ -46,6 +46,7 @@ type constructor Create; destructor Destroy; override; function Editor(aGrid: TDBGrid; aMaxLength: integer = 0): TStringCellEditor; + function CanDefocus: boolean; end; { TJDbGridDateTimeCtrl } @@ -71,6 +72,7 @@ type constructor Create; destructor Destroy; override; function Editor(aGrid: TDBGrid): TStringCellEditor; + function CanDefocus: boolean; end; { TJDbGridTimeCtrl } @@ -96,6 +98,7 @@ type constructor Create; destructor Destroy; override; function Editor(aGrid: TDBGrid): TStringCellEditor; + function CanDefocus: boolean; end; { TJDbGridDateCtrl } @@ -121,6 +124,7 @@ type constructor Create; destructor Destroy; override; function Editor(aGrid: TDBGrid): TStringCellEditor; + function CanDefocus: boolean; end; { TJDbGridIntegerCtrl } @@ -141,6 +145,7 @@ type constructor Create; destructor Destroy; override; function Editor(aGrid: TDBGrid): TStringCellEditor; + function CanDefocus: boolean; end; { TJDbGridDoubleCtrl } @@ -166,6 +171,7 @@ type constructor Create; destructor Destroy; override; function Editor(aGrid: TDBGrid; aDecimals: integer = 2): TStringCellEditor; + function CanDefocus: boolean; end; @@ -255,6 +261,11 @@ begin Result := CellEditor; end; +function TJDbGridStringCtrl.CanDefocus: boolean; +begin + Result := True; +end; + { TJDbGridDateTimeCtrl } function TJDbGridDateTimeCtrl.getFormat: string; @@ -296,7 +307,8 @@ begin if (not updated) then begin theValue := StrToDateTime(CellEditor.Caption); - if FormatDateTime(DisplayFormat, theValue) <> FormatDateTime(DisplayFormat, Field.AsDateTime) then + if FormatDateTime(DisplayFormat, theValue) <> + FormatDateTime(DisplayFormat, Field.AsDateTime) then begin Field.DataSet.DisableControls; Field.DataSet.Edit; @@ -416,6 +428,18 @@ begin Result := CellEditor; end; +function TJDbGridDateTimeCtrl.CanDefocus: boolean; +begin + if CellEditor.Focused and (Length(CellEditor.Text) = 0) then + Result := True + else + if CellEditor.Focused and not + (IsValidDateTimeString(NormalizeDateTime(CellEditor.Caption, theValue))) then + Result := False + else + Result := True; +end; + { TJDbGridTimeCtrl } function TJDbGridTimeCtrl.getFormat: string; @@ -457,7 +481,8 @@ begin if (not updated) then begin theValue := StrToTime(CellEditor.Caption); - if FormatDateTime(DisplayFormat, theValue) <> FormatDateTime(DisplayFormat, Field.AsDateTime) then + if FormatDateTime(DisplayFormat, theValue) <> + FormatDateTime(DisplayFormat, Field.AsDateTime) then begin Field.DataSet.DisableControls; Field.DataSet.Edit; @@ -575,6 +600,18 @@ begin Result := CellEditor; end; +function TJDbGridTimeCtrl.CanDefocus: boolean; +begin + if CellEditor.Focused and (Length(CellEditor.Text) = 0) then + Result := True + else + if CellEditor.Focused and not + (IsValidTimeString(NormalizeTime(CellEditor.Caption, theValue))) then + Result := False + else + Result := True; +end; + { TJDbGridDateCtrl } function TJDbGridDateCtrl.getFormat: string; @@ -616,7 +653,8 @@ begin if (not updated) then begin theValue := StrToDate(CellEditor.Caption); - if FormatDateTime(DisplayFormat, theValue) <> FormatDateTime(DisplayFormat, Field.AsDateTime) then + if FormatDateTime(DisplayFormat, theValue) <> + FormatDateTime(DisplayFormat, Field.AsDateTime) then begin Field.DataSet.DisableControls; Field.DataSet.Edit; @@ -736,6 +774,18 @@ begin Result := CellEditor; end; +function TJDbGridDateCtrl.CanDefocus: boolean; +begin + if CellEditor.Focused and (Length(CellEditor.Text) = 0) then + Result := True + else + if CellEditor.Focused and not + (IsValidDateTimeString(NormalizeDateTime(CellEditor.Caption, theValue))) then + Result := False + else + Result := True; +end; + { TJDbGridDoubleCtrl } function TJDbGridDoubleCtrl.getDecimals: integer; @@ -816,7 +866,6 @@ begin CellEditor.Text := '' else CellEditor.Text := FloatToStr(Field.AsFloat); - //CellEditor.Text := CurrToStr(redondear(Field.AsCurrency, fDecimals)); updated := True; theGrid.SetFocus; // No perder el foco end @@ -880,6 +929,14 @@ begin Result := CellEditor; end; +function TJDbGridDoubleCtrl.CanDefocus: boolean; +begin + if CellEditor.Focused then + Result := IsValidFloat(CellEditor.Text) + else + Result := True; +end; + { TJDbGridIntegerCtrl } procedure TJDbGridIntegerCtrl.myEditOnEnter(Sender: TObject); @@ -993,5 +1050,13 @@ begin Result := CellEditor; end; +function TJDbGridIntegerCtrl.CanDefocus: boolean; +begin + if CellEditor.Focused then + Result := IsValidInteger(CellEditor.Text) + else + Result := True; +end; + end.