diff --git a/components/jujiboutils/changes.txt b/components/jujiboutils/changes.txt index acc89f316..b57c75531 100644 --- a/components/jujiboutils/changes.txt +++ b/components/jujiboutils/changes.txt @@ -5,6 +5,7 @@ Note: Lazarus Trunk required Version pre-1.0 -------------------------------------------------- +2011-09-30 Added: ftString support to TJDBGridControl. MaxLength property. 2011-09-27 Added: ftDateTime support to TJDBGridControl TJFloatEdit, TJLabeledFloatEdit, TJDBFloatEdit, TJDBLabeledFloatEdit diff --git a/components/jujiboutils/jujibo-utils/src/jdbgridcontrol.pas b/components/jujiboutils/jujibo-utils/src/jdbgridcontrol.pas index 973251d29..96ac52ca3 100644 --- a/components/jujiboutils/jujibo-utils/src/jdbgridcontrol.pas +++ b/components/jujiboutils/jujibo-utils/src/jdbgridcontrol.pas @@ -32,10 +32,14 @@ type TJDBColumn = class(TColumn) private fDecimals: integer; + fMaxLength: integer; function getDecimals: integer; + function getMaxLength: integer; procedure setDecimals(AValue: integer); + procedure setMaxLength(AValue: integer); published property Decimals: integer read getDecimals write setDecimals; + property MaxLength: integer read getMaxLength write setMaxLength; end; { TJDBGridColumns } @@ -57,6 +61,7 @@ type private { Private declarations } usingControl: boolean; + stringDbGridControl: TJDbGridStringCtrl; dateDbGridControl: TJDbGridDateCtrl; timeDbGridControl: TJDbGridTimeCtrl; integerDbGridControl: TJDbGridIntegerCtrl; @@ -94,12 +99,23 @@ begin Result := fDecimals; end; +function TJDBColumn.getMaxLength: integer; +begin + Result := fMaxLength; +end; + procedure TJDBColumn.setDecimals(AValue: integer); begin if (AValue >= 0) and (AValue <= 10) then fDecimals := AValue; end; +procedure TJDBColumn.setMaxLength(AValue: integer); +begin + if AValue >= 0 then + fMaxLength := AValue; +end; + { TJDBGridColumns } function TJDBGridColumns.GetColumn(Index: integer): TJDBColumn; @@ -181,6 +197,11 @@ begin usingControl := True; Result := doubleDbGridControl.Editor(Self, aField.Size); end; + ftString: + begin + usingControl:= True; + Result := stringDbGridControl.Editor(Self, Columns.Items[Column - 1].MaxLength); + end; end; end; end; @@ -195,6 +216,7 @@ end; constructor TJDBGridControl.Create(TheOwner: TComponent); begin inherited Create(TheOwner); + stringDbGridControl := TJDbGridStringCtrl.Create; dateDbGridControl := TJDbGridDateCtrl.Create; timeDbGridControl := TJDbGridTimeCtrl.Create; integerDbGridControl := TJDbGridIntegerCtrl.Create; @@ -204,6 +226,7 @@ end; destructor TJDBGridControl.Destroy; begin + stringDbGridControl.Free; dateDbGridControl.Free; timeDbGridControl.Free; integerDbGridControl.Free; diff --git a/components/jujiboutils/jujibo-utils/src/jdbgridutils.pas b/components/jujiboutils/jujibo-utils/src/jdbgridutils.pas index a974fd365..7b5f769ac 100644 --- a/components/jujiboutils/jujibo-utils/src/jdbgridutils.pas +++ b/components/jujiboutils/jujibo-utils/src/jdbgridutils.pas @@ -22,11 +22,30 @@ unit jdbgridutils; interface uses - Classes, SysUtils, Grids, Dialogs, LCLType, DBGrids, Controls, DB, + Classes, SysUtils, Grids, Dialogs, LCLType, LCLProc, DBGrids, Controls, DB, jcontrolutils, jinputconsts; type + { TJDbGridStringCtrl } + + TJDbGridStringCtrl = class(TObject) + private + Field: TField; + updated: boolean; + fMaxLength: integer; + procedure myEditEnter(Sender: TObject); + procedure myEditOnEditingDone(Sender: TObject); + procedure OnKeyPress(Sender: TObject; var key: char); + procedure OnKeyDown(Sender: TObject; var Key: word; Shift: TShiftState); + public + CellEditor: TStringCellEditor; + theGrid: TDBGrid; + constructor Create; + destructor Destroy; override; + function Editor(aGrid: TDBGrid; aMaxLength: integer = 0): TStringCellEditor; + end; + { TJDbGridDateTimeCtrl } TJDbGridDateTimeCtrl = class(TObject) @@ -153,6 +172,88 @@ implementation uses Math; +{ TJDbGridStringCtrl } + +procedure TJDbGridStringCtrl.myEditEnter(Sender: TObject); +begin + Field := theGrid.SelectedField; + CellEditor.BoundsRect := theGrid.SelectedFieldRect; + CellEditor.Text := Field.AsString; + CellEditor.OnKeyPress := @OnKeyPress; // Recuperamos el control :-p + CellEditor.OnKeyDown := @OnKeyDown; + updated := False; + CellEditor.SelectAll; +end; + +procedure TJDbGridStringCtrl.myEditOnEditingDone(Sender: TObject); +begin + if (not updated) then + begin + Field.DataSet.DisableControls; + if (fMaxLength > 0) and (UTF8Length(CellEditor.Text) > fMaxLength) then + CellEditor.Text := UTF8Copy(CellEditor.Text, 0, fMaxLength); + Field.DataSet.Edit; + Field.AsString := CellEditor.Text; + field.DataSet.EnableControls; + end; +end; + +procedure TJDbGridStringCtrl.OnKeyPress(Sender: TObject; var key: char); +begin + if (fMaxLength > 0) and (UTF8Length(CellEditor.Text) >= fMaxLength) then + key := #0; +end; + +procedure TJDbGridStringCtrl.OnKeyDown(Sender: TObject; var Key: word; + Shift: TShiftState); +begin + if key = VK_ESCAPE then + begin + CellEditor.Text := Field.AsString; + updated := True; + theGrid.SetFocus; // No perder el foco + end + else + if Key in [VK_UP, VK_DOWN] then + begin + Key := VK_UNKNOWN; + end + else + if Key in [VK_RETURN, VK_TAB, VK_RIGHT, VK_LEFT] then + begin + Field.DataSet.Edit; + if (fMaxLength > 0) and (UTF8Length(CellEditor.Text) > fMaxLength) then + CellEditor.Text := UTF8Copy(CellEditor.Text, 0, fMaxLength); + Field.AsString := CellEditor.Text; + CellEditor.SelectAll; + updated := True; + end; +end; + +constructor TJDbGridStringCtrl.Create; +begin + inherited Create; + CellEditor := TStringCellEditor.Create(nil); + CellEditor.OnEnter := @myEditEnter; + CellEditor.OnKeyDown := @OnKeyDown; + CellEditor.OnEditingDone := @myEditOnEditingDone; + CellEditor.OnKeyPress := @OnKeyPress; +end; + +destructor TJDbGridStringCtrl.Destroy; +begin + CellEditor.Free; + inherited Destroy; +end; + +function TJDbGridStringCtrl.Editor(aGrid: TDBGrid; + aMaxLength: integer): TStringCellEditor; +begin + theGrid := aGrid; + fMaxLength := aMaxLength; + Result := CellEditor; +end; + { TJDbGridDateTimeCtrl } function TJDbGridDateTimeCtrl.getFormat: string;