From 9678243a400587a1ef810ec19148842b744db43b Mon Sep 17 00:00:00 2001 From: jesusr Date: Tue, 4 Apr 2017 06:14:30 +0000 Subject: [PATCH] jujiboutils: TJLabeledDateEdit: added support for abbreviated month names, Trigger OnEditingDone on selecting a date from calendar. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5830 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/jujiboutils/src/jcontrolutils.pas | 37 +++++++++++++++++-- components/jujiboutils/src/jdbgridutils.pas | 9 +++-- .../jujiboutils/src/jlabeleddateedit.pas | 5 +-- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/components/jujiboutils/src/jcontrolutils.pas b/components/jujiboutils/src/jcontrolutils.pas index f31d44a22..265204cb8 100644 --- a/components/jujiboutils/src/jcontrolutils.pas +++ b/components/jujiboutils/src/jcontrolutils.pas @@ -33,7 +33,8 @@ function NormalizeTime(const Value: string; theValue: TTime; function NormalizeDateTime(const Value: string; theValue: TDateTime; const theFormat: string = ''): string; function NormalizeDateSeparator(const s: string): string; -function IsValidDateString(const Value: string): boolean; +function IsValidDateString(Value: string): boolean; +function ValidateDateString(Value:string; out aDate:TDateTime): boolean; function IsValidTimeString(const Value: string): boolean; function IsValidDateTimeString(const Value: string): boolean; @@ -131,9 +132,8 @@ begin 'y', 'Y': BigEndianForm; end; - if IsValidDateString(texto) then + if ValidateDateString(texto, aDate) then begin - aDate := StrToDate(texto); Result := FormatDateTime(aDateFormat, aDate); end; end; @@ -242,13 +242,42 @@ begin Result[i] := DefaultFormatSettings.DateSeparator; end; -function IsValidDateString(const Value: string): boolean; +procedure HackAbbreviatedDates(var Value: string); +var + i: Integer; +begin + with DefaultFormatSettings do + if pos('mmm', Lowercase(ShortDateFormat))>0 then begin + // Value is probably formatted using month names (mmmm) or + // abbreviated month names (mmm), and that is not supported + // by free pascal. Lets hack it ... + for i:=1 to 12 do begin + if pos(LongMonthNames[i], value)>0 then begin + Value := StringReplace(Value, LongMonthNames[i], format('%.2d',[i]), [rfIgnoreCase]); + break; + end else + if pos(ShortMonthNames[i], value)>0 then begin + Value := StringReplace(Value, ShortMonthNames[i], format('%.2d',[i]), [rfIgnoreCase]); + break; + end; + end; + end; +end; + +function IsValidDateString(Value: string): boolean; var bTime: TDateTime; begin + HackAbbreviatedDates(Value); Result := TryStrToDate(Value, bTime); end; +function ValidateDateString(Value: string; out aDate: TDateTime): boolean; +begin + HackAbbreviatedDates(Value); + Result := TryStrToDate(Value, aDate); +end; + function IsValidTimeString(const Value: string): boolean; var bTime: TDateTime; diff --git a/components/jujiboutils/src/jdbgridutils.pas b/components/jujiboutils/src/jdbgridutils.pas index 4746fb07d..2c90c88fb 100644 --- a/components/jujiboutils/src/jdbgridutils.pas +++ b/components/jujiboutils/src/jdbgridutils.pas @@ -762,6 +762,8 @@ begin end; procedure TJDbGridDateCtrl.myEditOnEditingDone(Sender: TObject); +var + aDate: TDateTime; begin if not Assigned(Field) or not Assigned(Field.Dataset) or not Field.DataSet.Active then exit; @@ -782,11 +784,11 @@ begin else begin CellEditor.Caption := NormalizeDate(CellEditor.Caption, theValue); - if IsValidDateString(CellEditor.Caption) then + if ValidateDateString(CellEditor.Caption, aDate) then begin if (not updated) then begin - theValue := StrToDate(CellEditor.Caption); + theValue := aDate; if FormatDateTime(DisplayFormat, theValue) <> FormatDateTime(DisplayFormat, Field.AsDateTime) then begin @@ -880,9 +882,8 @@ begin if Length(CellEditor.Caption) = 0 then theValue := 0 else - if IsValidDateString(CellEditor.Caption) then + if ValidateDateString(CellEditor.Caption, theValue) then begin - theValue := StrToDate(CellEditor.Caption); Field.DataSet.Edit; Field.AsDateTime := theValue; CellEditor.SelectAll; diff --git a/components/jujiboutils/src/jlabeleddateedit.pas b/components/jujiboutils/src/jlabeleddateedit.pas index 62f94affa..8ea2f73b4 100644 --- a/components/jujiboutils/src/jlabeleddateedit.pas +++ b/components/jujiboutils/src/jlabeleddateedit.pas @@ -209,9 +209,7 @@ begin if Length(Text) = 0 then theValue := 0 else - if IsValidDateString(Text) then - theValue := StrToDate(Text) - else + if not ValidateDateString(Text, theValue) then begin ShowMessage(Format(SInvalidDate, [Text])); SetFocus; @@ -312,6 +310,7 @@ procedure TJLabeledDateEdit.CalendarPopupReturnDate(Sender: TObject; const ADate: TDateTime); begin Value := ADate; + EditingDone; end; constructor TJLabeledDateEdit.Create(TheOwner: TComponent);