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
This commit is contained in:
jesusr
2017-04-04 06:14:30 +00:00
parent b13c6aff73
commit 9678243a40
3 changed files with 40 additions and 11 deletions

View File

@@ -33,7 +33,8 @@ function NormalizeTime(const Value: string; theValue: TTime;
function NormalizeDateTime(const Value: string; theValue: TDateTime; function NormalizeDateTime(const Value: string; theValue: TDateTime;
const theFormat: string = ''): string; const theFormat: string = ''): string;
function NormalizeDateSeparator(const s: 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 IsValidTimeString(const Value: string): boolean;
function IsValidDateTimeString(const Value: string): boolean; function IsValidDateTimeString(const Value: string): boolean;
@@ -131,9 +132,8 @@ begin
'y', 'Y': BigEndianForm; 'y', 'Y': BigEndianForm;
end; end;
if IsValidDateString(texto) then if ValidateDateString(texto, aDate) then
begin begin
aDate := StrToDate(texto);
Result := FormatDateTime(aDateFormat, aDate); Result := FormatDateTime(aDateFormat, aDate);
end; end;
end; end;
@@ -242,13 +242,42 @@ begin
Result[i] := DefaultFormatSettings.DateSeparator; Result[i] := DefaultFormatSettings.DateSeparator;
end; 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 var
bTime: TDateTime; bTime: TDateTime;
begin begin
HackAbbreviatedDates(Value);
Result := TryStrToDate(Value, bTime); Result := TryStrToDate(Value, bTime);
end; end;
function ValidateDateString(Value: string; out aDate: TDateTime): boolean;
begin
HackAbbreviatedDates(Value);
Result := TryStrToDate(Value, aDate);
end;
function IsValidTimeString(const Value: string): boolean; function IsValidTimeString(const Value: string): boolean;
var var
bTime: TDateTime; bTime: TDateTime;

View File

@@ -762,6 +762,8 @@ begin
end; end;
procedure TJDbGridDateCtrl.myEditOnEditingDone(Sender: TObject); procedure TJDbGridDateCtrl.myEditOnEditingDone(Sender: TObject);
var
aDate: TDateTime;
begin begin
if not Assigned(Field) or not Assigned(Field.Dataset) or not Field.DataSet.Active then if not Assigned(Field) or not Assigned(Field.Dataset) or not Field.DataSet.Active then
exit; exit;
@@ -782,11 +784,11 @@ begin
else else
begin begin
CellEditor.Caption := NormalizeDate(CellEditor.Caption, theValue); CellEditor.Caption := NormalizeDate(CellEditor.Caption, theValue);
if IsValidDateString(CellEditor.Caption) then if ValidateDateString(CellEditor.Caption, aDate) then
begin begin
if (not updated) then if (not updated) then
begin begin
theValue := StrToDate(CellEditor.Caption); theValue := aDate;
if FormatDateTime(DisplayFormat, theValue) <> if FormatDateTime(DisplayFormat, theValue) <>
FormatDateTime(DisplayFormat, Field.AsDateTime) then FormatDateTime(DisplayFormat, Field.AsDateTime) then
begin begin
@@ -880,9 +882,8 @@ begin
if Length(CellEditor.Caption) = 0 then if Length(CellEditor.Caption) = 0 then
theValue := 0 theValue := 0
else else
if IsValidDateString(CellEditor.Caption) then if ValidateDateString(CellEditor.Caption, theValue) then
begin begin
theValue := StrToDate(CellEditor.Caption);
Field.DataSet.Edit; Field.DataSet.Edit;
Field.AsDateTime := theValue; Field.AsDateTime := theValue;
CellEditor.SelectAll; CellEditor.SelectAll;

View File

@@ -209,9 +209,7 @@ begin
if Length(Text) = 0 then if Length(Text) = 0 then
theValue := 0 theValue := 0
else else
if IsValidDateString(Text) then if not ValidateDateString(Text, theValue) then
theValue := StrToDate(Text)
else
begin begin
ShowMessage(Format(SInvalidDate, [Text])); ShowMessage(Format(SInvalidDate, [Text]));
SetFocus; SetFocus;
@@ -312,6 +310,7 @@ procedure TJLabeledDateEdit.CalendarPopupReturnDate(Sender: TObject;
const ADate: TDateTime); const ADate: TDateTime);
begin begin
Value := ADate; Value := ADate;
EditingDone;
end; end;
constructor TJLabeledDateEdit.Create(TheOwner: TComponent); constructor TJLabeledDateEdit.Create(TheOwner: TComponent);