cxtimeedit.pas

来自「胜天进销存源码,国产优秀的进销存」· PAS 代码 · 共 1,149 行 · 第 1/3 页

PAS
1,149
字号
    else
      Result := tzSec;
  end;
end;

procedure TcxCustomTimeEditProperties.UpdateEditMask;
begin
  EditMask := cxTimeEditFormats[FTimeFormat, FUse24HourFormat, 1];
end;

procedure TcxCustomTimeEditProperties.SetAutoCorrectHours(Value: Boolean);
begin
  if Value <> FAutoCorrectHours then
    FAutoCorrectHours := Value;
end;

procedure TcxCustomTimeEditProperties.SetUse24HourFormat(Value: Boolean);
begin
  if Value <> FUse24HourFormat then
  begin
    FUse24HourFormat := Value;
    UpdateEditMask;
    Changed;
  end;
end;

procedure TcxCustomTimeEditProperties.SetShowDate(Value: Boolean);
begin
  if Value <> FShowDate then
  begin
    FShowDate := Value;
    Changed;
  end;
end;

procedure TcxCustomTimeEditProperties.SetTimeFormat(Value: TcxTimeEditTimeFormat);
begin
  if FTimeFormat <> Value then
  begin
    FTimeFormat := Value;
    UpdateEditMask;
    Changed;
  end;
end;

procedure TcxCustomTimeEditProperties.SetUseTimeFormatWhenUnfocused(
  Value: Boolean);
begin
  if FUseTimeFormatWhenUnfocused <> Value then
  begin
    FUseTimeFormatWhenUnfocused := Value;
    Changed;
  end;
end;

{ TcxCustomTimeEdit }

procedure TcxCustomTimeEdit.Clear;
begin
  Time := 0;
end;

class function TcxCustomTimeEdit.GetPropertiesClass: TcxCustomEditPropertiesClass;
begin
  Result := TcxCustomTimeEditProperties;
end;

function TcxCustomTimeEdit.Increment(AButton: TcxSpinEditButton): Boolean;
var
  ACursorPosition: Integer;
  ASelStart, ASelLength: Integer;
begin
  ACursorPosition := 0;
  ASelLength := 0;
  if HandleAllocated then
  begin
    ASelStart := SelStart;
    if EditingPlace = tzTimeSuffix then
    begin
      ACursorPosition := (3 - Integer(ActiveProperties.TimeFormat)) * 3;
      while IsSpace(DisplayValue[ACursorPosition]) do
        Inc(ACursorPosition);
      if IsPM(TDateTime(Time)) then
        ASelLength := Length(TimeAMString)
      else
        ASelLength := Length(TimePMString);
      Dec(ACursorPosition);
    end else
    begin
      ASelLength := 1;
      case ASelStart of
        0..1:
          ACursorPosition := 1;
        2..4:
          ACursorPosition := 4;
        else
          ACursorPosition := 7;
      end;
    end;
  end;
  Result := inherited Increment(AButton);
  if HandleAllocated then
    SetSelection(ACursorPosition, ASelLength);
end;

procedure TcxCustomTimeEdit.PrepareEditValue(const ADisplayValue: TcxEditValue;
  out EditValue: TcxEditValue; AEditFocused: Boolean);

  function GetCorrectTimeSuffix(const ASuffix: string): string;
  var
    APos: Integer;
  begin
    APos := Pos(TimeAMString, ASuffix);
    if APos <> 0 then
      Result := TimeAMString
    else
      Result := TimePMString;
  end;

var
  ATimeStringLength: Integer;
  AText: string;
  AValue: Double;
begin
  AText := ADisplayValue;
  ATimeStringLength := (3 - Integer(ActiveProperties.TimeFormat)) * 3 - 1;
  if not ActiveProperties.Use24HourFormat then
    AText := Copy(AText, 1, ATimeStringLength);
  if Length(AText) = 2 then
    AText := AText + ':00'; // delphi bug
  if not ActiveProperties.Use24HourFormat then
    AText := AText + GetCorrectTimeSuffix(
      Copy(ADisplayValue, ATimeStringLength + 1,
        Length(ADisplayValue) - ATimeStringLength));
  AValue := 0;
  try
    try
      if AText <> '' then
        AValue := StrToTime(AText);
    except
      AValue := 0;
    end
  finally
    AValue := FSavedDate + AValue * cxSign(FSavedDate);
    EditValue := AValue;
  end;
  if EditValue <> 0 then
    EditValue := VarAsType(EditValue, varDate);
end;

function TcxCustomTimeEdit.GetIncrement(AButton: TcxSpinEditButton): Double;
var
  AEditingPlace: TcxTimeEditZoneKind;
  AIsForwardDirection: Boolean;
begin
  Result := 0;
  AIsForwardDirection := AButton in [sebForward, sebFastForward];
  AEditingPlace := EditingPlace;
  case AEditingPlace of
    tzTimeSuffix:
      begin
        Result := cxTimeEditHalfDayIncrement;
        if AIsForwardDirection and (Value >= cxTimeEditHalfDayIncrement) or
            not AIsForwardDirection and (Value < cxTimeEditHalfDayIncrement) then
          Result := -Result;
      end;
    tzHour: Result := cxTimeEditOneHourIncrement;
    tzMin: Result := cxTimeEditOneMinIncrement;
    tzSec: Result := cxTimeEditOneSecIncrement;
  end;
  if (AEditingPlace in [tzMin, tzSec]) and (AButton in [sebFastBackward, sebFastForward]) then
    Result := Result * 10;
  if not AIsForwardDirection then
    Result := -Result;
end;

function TcxCustomTimeEdit.GetValue: Variant;
var
  AHours, ATimeSuffixLength, AValue: Integer;
  S: string;
begin
  S := Text;
  AHours := StrToInt(Copy(S, 1, 2));
  if not ActiveProperties.Use24HourFormat then
    AHours := AHours mod 12;
  AValue := AHours * 60 * 60;
  if ActiveProperties.TimeFormat <> tfHour then
    AValue := AValue + StrToInt(Copy(S, 4, 2)) * 60;
  if ActiveProperties.TimeFormat = tfHourMinSec then
    AValue := AValue + StrToInt(Copy(S, 7, 2));
  ATimeSuffixLength := Length(TimePMString);
  if not ActiveProperties.Use24HourFormat and InternalCompareString(Copy(S,
      Length(S) - ATimeSuffixLength + 1, ATimeSuffixLength), TimePMString, True) then
    Inc(AValue, 12 * 60 * 60);
  Result := AValue;
end;

function TcxCustomTimeEdit.IncrementValueToStr(const AValue: TcxEditValue): string;
var
  ATime: TDateTime;
begin
  ATime := AValue;
  Result := inherited IncrementValueToStr(ATime * {$IFDEF DELPHI7}MSecsPerSec{$ELSE}1000{$ENDIF} / MSecsPerDay);
end;

procedure TcxCustomTimeEdit.Initialize;
begin
  inherited Initialize;
  ControlStyle := ControlStyle - [csSetCaption];
end;

function TcxCustomTimeEdit.InternalGetEditingValue: TcxEditValue;
begin
  PrepareEditValue(Text, Result, True);
end;

function TcxCustomTimeEdit.InternalGetText: string;
begin
  Result := DisplayValue;
end;

procedure TcxCustomTimeEdit.InternalSetDisplayValue(const Value: TcxEditValue);
begin
  if ActiveProperties.Use24HourFormat or (VarToStr(Value) <> '') then
    inherited InternalSetDisplayValue(Value)
  else
    InnerTextEdit.EditValue := Value;
end;

procedure TcxCustomTimeEdit.InternalSetEditValue(const Value: TcxEditValue;
  AValidateEditValue: Boolean);
var
  AValue: TcxEditValue;
  AIsEditValueValid: Boolean;
begin
  AValue := Value;
  AIsEditValueValid := not AValidateEditValue or ActiveProperties.IsEditValueValid(AValue, InternalFocused);
  if VarIsNull(Value) or not AIsEditValueValid then
    FSavedDate := 0
  else
    if VarIsStr(AValue) then
      FSavedDate := TDate(DateOf(StrToDateTime(Value)))
    else
      FSavedDate := TDate(DateOf(Value));
  inherited InternalSetEditValue(Value, AValidateEditValue);
end;

function TcxCustomTimeEdit.InternalSetText(const Value: string): Boolean;
begin
  Result := SetDisplayText(Value);
end;

function TcxCustomTimeEdit.IsValidChar(AChar: Char): Boolean;
begin
  Result := True;
end;

function TcxCustomTimeEdit.IsCharValidForPos(var AChar: Char;
  APos: Integer): Boolean;
var
  ATimeZoneInfo: TcxTimeEditZoneInfo;
begin
  ActiveProperties.GetTimeZoneInfo(APos, ATimeZoneInfo);
  Result := IsCharValidForTimeEdit(Self, AChar, APos, ATimeZoneInfo);
end;

procedure TcxCustomTimeEdit.PropertiesChanged(Sender: TObject);
begin
  if not Focused then
    DataBinding.UpdateDisplayValue;
  SelStart := Length(DisplayValue);
  inherited PropertiesChanged(Sender);
end;

procedure TcxCustomTimeEdit.SetValue(const Value: Variant);
var
  ATime: TDateTime;
begin
  ATime := Value;
  Time := ATime * {$IFDEF DELPHI7}MSecsPerSec{$ELSE}1000{$ENDIF} / MSecsPerDay;
end;

function TcxCustomTimeEdit.EditingPlace: TcxTimeEditZoneKind;
begin
  Result := ActiveProperties.GetEditingPlace(SelStart + 1);
end;

function TcxCustomTimeEdit.GetProperties: TcxCustomTimeEditProperties;
begin
  Result := TcxCustomTimeEditProperties(FProperties);
end;

function TcxCustomTimeEdit.GetActiveProperties: TcxCustomTimeEditProperties;
begin
  Result := TcxCustomTimeEditProperties(InternalGetActiveProperties);
end;

function TcxCustomTimeEdit.GetTime: TTime;
var
  AValue: TcxEditValue;
begin
  Result := TTime(SysUtils.Now);
  try
    if Focused then
    begin
      PrepareEditValue(DisplayValue, AValue, True);
      Result := TTime(VarAsType(AValue, varDate));
    end
    else
      if VarIsStr(EditValue) then
        Result := TTime(StrToDateTime(EditValue))
      else
      begin
        Result := TTime(VarAsType(EditValue, varDate));
      end;
  finally
    Result := Abs(Frac(Result));
  end;
end;

procedure TcxCustomTimeEdit.SetProperties(Value: TcxCustomTimeEditProperties);
begin
  FProperties.Assign(Value);
end;

procedure TcxCustomTimeEdit.SetTime(Value: TTime);
begin
  InternalEditValue := VarToDateTime(
    Abs(Frac(Value)) * cxSign(FSavedDate) + FSavedDate);
end;

{ TcxTimeEdit }

class function TcxTimeEdit.GetPropertiesClass: TcxCustomEditPropertiesClass;
begin
  Result := TcxTimeEditProperties;
end;

function TcxTimeEdit.GetActiveProperties: TcxTimeEditProperties;
begin
  Result := TcxTimeEditProperties(InternalGetActiveProperties);
end;

function TcxTimeEdit.GetProperties: TcxTimeEditProperties;
begin
  Result := TcxTimeEditProperties(FProperties);
end;

procedure TcxTimeEdit.SetProperties(Value: TcxTimeEditProperties);
begin
  FProperties.Assign(Value);
end;

{ TcxFilterTimeEditHelper }

class procedure TcxFilterTimeEditHelper.InitializeEdit(AEdit: TcxCustomEdit;
  AEditProperties: TcxCustomEditProperties);
begin
  inherited InitializeEdit(AEdit, AEditProperties);
  TcxTimeEdit(AEdit).Time := 0;
end;

class function TcxFilterTimeEditHelper.GetFilterEditClass: TcxCustomEditClass;
begin
  Result := TcxTimeEdit;
end;

initialization
  cxTimeEditHalfDayIncrement := 12 * 60 * 60;
  cxTimeEditOneHourIncrement := 60 * 60;
  cxTimeEditOneMinIncrement := 60;
  cxTimeEditOneSecIncrement := 1;
  FTimeEditFormatListener := TcxTimeEditFormatListener.Create(nil);

  GetRegisteredEditProperties.Register(TcxTimeEditProperties, scxSEditRepositoryTimeItem);
  FilterEditsController.Register(TcxTimeEditProperties, TcxFilterTimeEditHelper);

finalization
  FilterEditsController.Unregister(TcxTimeEditProperties, TcxFilterTimeEditHelper);
  FreeAndNil(FTimeEditFormatListener);

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?