cxspinedit.pas

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

PAS
1,961
字号
  begin
    FValueType := Value;
    Changed;
  end;
end;

{ TcxCustomSpinEditProperties }

constructor TcxCustomSpinEditProperties.Create(AOwner: TPersistent);
begin
  inherited Create(AOwner);
  Buttons.Add;
  Buttons.Add;
  FSpinButtons := TcxSpinEditButtons.Create(Self);
  FCanEdit := True;
  FIncrement := 1.0;
  FLargeIncrement := 10.0;
end;

destructor TcxCustomSpinEditProperties.Destroy;
begin
  FreeAndNil(FSpinButtons);
  inherited Destroy;
end;

procedure TcxCustomSpinEditProperties.Assign(Source: TPersistent);
begin
  if Source is TcxCustomSpinEditProperties then
  begin
    BeginUpdate;
    try
      with Source as TcxCustomSpinEditProperties do
      begin
        Self.FIDefaultValuesProvider := FIDefaultValuesProvider;
        Self.AssignedValues.ValueType := False;
        if AssignedValues.ValueType then
          Self.ValueType := ValueType;
      end;
      inherited Assign(Source);
      with Source as TcxCustomSpinEditProperties do
      begin
        Self.CanEdit := CanEdit;
        Self.Circular := Circular;
        Self.ExceptionOnInvalidInput := ExceptionOnInvalidInput;
        Self.Increment := Increment;
        Self.LargeIncrement := LargeIncrement;
        Self.SpinButtons := SpinButtons;
        Self.UseCtrlIncrement := UseCtrlIncrement;
        Self.OnGetValue := OnGetValue;
      end;
    finally
      EndUpdate;
    end
  end
  else
    inherited Assign(Source);
end;

procedure TcxCustomSpinEditProperties.Changed;

  function GetButtonCount: Integer;
  begin
    Result := 0;
    if SpinButtons.Visible then
    begin
      Inc(Result, 2);
      if SpinButtons.ShowFastButtons then
        Inc(Result, 2);
    end;
  end;

var
  AButtonCount: Integer;
begin
  if FSpinButtons <> nil then
  begin
    BeginUpdate;
    try
      AButtonCount := GetButtonCount;
      while Buttons.Count < AButtonCount do
        Buttons.Add;
      while Buttons.Count > AButtonCount do
        Buttons[0].Free;
    finally
      EndUpdate(False);
    end;
  end;
  inherited Changed;
end;

function TcxCustomSpinEditProperties.CreateViewData(AStyle: TcxCustomEditStyle;
  AIsInplace: Boolean; APreviewMode: Boolean = False): TcxCustomEditViewData;
begin
  Result := inherited CreateViewData(AStyle, AIsInplace, APreviewMode);
  with TcxSpinEditViewData(Result) do
    if Edit <> nil then
      PressedState := TcxCustomSpinEdit(Edit).FPressedState
    else
      PressedState := epsNone;
end;

class function TcxCustomSpinEditProperties.GetContainerClass: TcxContainerClass;
begin
  Result := TcxSpinEdit;
end;

class function TcxCustomSpinEditProperties.GetViewInfoClass: TcxContainerViewInfoClass;
begin
  Result := TcxSpinEditViewInfo;
end;

function TcxCustomSpinEditProperties.IsDisplayValueValid(var DisplayValue: TcxEditValue; AEditFocused: Boolean):
  Boolean;
var
  AValue: TcxEditValue;
begin
  Result := inherited IsDisplayValueValid(DisplayValue, AEditFocused);
  if not Result or not IsDisplayValueNumeric then
    Exit;
  try
    if DisplayValue <> '' then
    begin
      Result := TryTextToValue(Trim(VarToStr(DisplayValue)), AValue);
      if not Result then
        Exit;
      if (AValue < InternalMinValue) or (AValue > InternalMaxValue) then
        DisplayValue := VarToStr(AValue);
    end;
    Result := True;
  except
    on EConvertError do
      Result := False;
  end;
end;

function TcxCustomSpinEditProperties.IsEditValueValid(var EditValue: TcxEditValue;
  AEditFocused: Boolean): Boolean;
var
  AValue: TcxEditValue;
begin
  if VarIsStr(EditValue) or VarIsNumericEx(EditValue) then
  begin
    Result := TryTextToValue(VarToStr(EditValue), AValue);
    if Result then
      EditValue := AValue;
  end
  else
    Result :=  {VarIsNumericEx(EditValue) or} VarIsSoftNull(EditValue) or
      VarIsDate(EditValue);
  if not Result then
    Exit;
  try
    EditValue := PrepareValue(EditValue);
//    EditValue := CheckValueBounds(EditValue);
    EditValue := SetVariantType(EditValue);
  except
    Result := False;
  end;
end;

procedure TcxCustomSpinEditProperties.ValidateDisplayValue(
  var ADisplayValue: TcxEditValue; var AErrorText: TCaption;
  var AError: Boolean; AEdit: TcxCustomEdit);
var
  AEditValue: TcxEditValue;
  AIsUserErrorDisplayValue: Boolean;
begin
  if TcxCustomSpinEdit(AEdit).FIsCustomText then
    AError := not TcxCustomSpinEdit(AEdit).InternalPrepareEditValue(ADisplayValue,
      False, AEditValue, AErrorText)
  else
    AEditValue := TcxCustomSpinEdit(AEdit).FInternalValue;
  if not AError then
  begin
    if VarIsNull(AEditValue) then
    begin
      AEditValue := CheckValueBounds(0);
      AEditValue := SetVariantType(AEditValue);
    end;
    if not ExceptionOnInvalidInput then
      if AEditValue < InternalMinValue then
        AEditValue := InternalMinValue
      else
        if AEditValue > InternalMaxValue then
          AEditValue := InternalMaxValue;
    ADisplayValue := VarToStr(AEditValue);
    if ExceptionOnInvalidInput then
    begin
      AError := (AEditValue < InternalMinValue) or (AEditValue > InternalMaxValue);
      if AError then
        AErrorText := cxGetResourceString(@cxSEditValueOutOfBounds);
    end;
  end;

  DoValidate(ADisplayValue, AErrorText, AError, AEdit, AIsUserErrorDisplayValue);
end;

function TcxCustomSpinEditProperties.DefaultFocusedDisplayValue: TcxEditValue;
begin
  PrepareDisplayValue(CheckValueBounds(0), Result, True)
end;

procedure TcxCustomSpinEditProperties.DefineProperties(Filer: TFiler);

  function HasZeroIncrement: Boolean;
  begin
    Result := (Increment = 0) and ((Filer.Ancestor = nil) or
      (TcxCustomSpinEditProperties(Filer.Ancestor).Increment <> Increment));
  end;

  function HasZeroLargeIncrement: Boolean;
  begin
    Result := (LargeIncrement = 0) and ((Filer.Ancestor = nil) or
      (TcxCustomSpinEditProperties(Filer.Ancestor).LargeIncrement <> LargeIncrement));
  end;

begin
  inherited DefineProperties(Filer);
  Filer.DefineProperty('ZeroIncrement', ReadZeroIncrement, WriteZeroIncrement,
    HasZeroIncrement);
  Filer.DefineProperty('ZeroLargeIncrement', ReadZeroLargeIncrement,
    WriteZeroLargeIncrement, HasZeroLargeIncrement);
end;

class function TcxCustomSpinEditProperties.GetAssignedValuesClass: TcxCustomEditPropertiesValuesClass;
begin
  Result := TcxSpinEditPropertiesValues;
end;

function TcxCustomSpinEditProperties.GetEditValueSource(AEditFocused: Boolean): TcxDataEditValueSource;
begin
  if not AEditFocused and not AssignedValues.DisplayFormat then
    Result := evsText
  else
    Result := evsValue;
end;

class function TcxCustomSpinEditProperties.GetViewDataClass: TcxCustomEditViewDataClass;
begin
  Result := TcxSpinEditViewData;
end;

function TcxCustomSpinEditProperties.IsEditValueNumeric: Boolean;
begin
  Result := True;
end;

function TcxCustomSpinEditProperties.CheckValueBounds(const Value: Variant): Variant;
begin
  Result := Value;
  if VarIsNumericEx(Result) then
    if IsValueBoundDefined(evbMin) and (Result < MinValue) then
      Result := MinValue
    else
      if IsValueBoundDefined(evbMax) and (Result > MaxValue) then
        Result := MaxValue;
end;

function TcxCustomSpinEditProperties.ExtendValueUpToBound: Boolean;
begin
  Result := True;
end;

function TcxCustomSpinEditProperties.GetBoundsCheckingKind: TcxSpinBoundsCheckingKind;
begin
  if FCircular then
    Result := bckCircular
  else
    if ExtendValueUpToBound then
      Result := bckExtendToBound
    else
      Result := bckDoNotExceed;
end;

function TcxCustomSpinEditProperties.GetMaxMinValueForCurrentValueType(
  AMinValue: Boolean = True): TcxEditValue;
begin
  if ValueType = vtInt then
    if AMinValue then
      Result := MinIntValue
    else
      Result := MaxIntValue
  else
    if AMinValue then
      Result := -MaxDouble
    else
      Result := MaxDouble;
end;

function TcxCustomSpinEditProperties.InternalMaxValue: TcxEditValue;
begin
  if IsValueBoundDefined(evbMax) then
    Result := DoubleAsValueType(MaxValue, ValueType)
  else
    Result := GetMaxMinValueForCurrentValueType(False);
  Result := VarToCurrentValueType(Result);
end;

function TcxCustomSpinEditProperties.InternalMinValue: TcxEditValue;
begin
  if IsValueBoundDefined(evbMin) then
    Result := DoubleAsValueType(MinValue, ValueType)
  else
    Result := GetMaxMinValueForCurrentValueType;
  Result := VarToCurrentValueType(Result);
end;

function TcxCustomSpinEditProperties.IsDisplayValueNumeric: Boolean;
begin
  Result := True;
end;

function TcxCustomSpinEditProperties.IsValueBoundsValid(AValue: Extended): Boolean;
begin
  if ValueType = vtInt then
    Result := (AValue >= MinIntValue) and (AValue <= MaxIntValue)
  else
    Result := (AValue >= -MaxDouble) and (AValue <= MaxDouble);
end;

function TcxCustomSpinEditProperties.PrepareValue(const AValue: TcxEditValue): Variant;
begin
  if VarIsSoftNull(AValue) then
    Result := VarToCurrentValueType(0)
  else
    Result := VarToCurrentValueType(AValue);
end;

function TcxCustomSpinEditProperties.PreserveSelection: Boolean;
begin
  Result := False;
end;

function TcxCustomSpinEditProperties.SetVariantType(const Value: TcxEditValue): TcxEditValue;
begin
  if VarIsNumericEx(Value) then
    Result := VarToCurrentValueType(Value)
  else
    Result := Value;
end;

function TcxCustomSpinEditProperties.DoubleAsValueType(AValue: Double;
  AValueType: TcxSpinEditValueType): Double;
begin
  if AValueType = vtInt then
    Result := Round(AValue)
  else
    Result := AValue;
end;

function TcxCustomSpinEditProperties.GetAssignedValues: TcxSpinEditPropertiesValues;
begin
  Result := TcxSpinEditPropertiesValues(FAssignedValues);
end;

function TcxCustomSpinEditProperties.GetValueType: TcxSpinEditValueType;
const
  ASpinEditValueTypeMap: array [Boolean] of TcxSpinEditValueType = (vtInt, vtFloat);
begin
  if AssignedValues.ValueType then
    Result := FValueType
  else
    if IDefaultValuesProvider <> nil then
      Result := ASpinEditValueTypeMap[IDefaultValuesProvider.DefaultIsFloatValue]
    else
      Result := vtFloat;
end;

function TcxCustomSpinEditProperties.IsIncrementStored: Boolean;
begin
  Result := FIncrement <> 1.0;
end;

function TcxCustomSpinEditProperties.IsLargeIncrementStored: Boolean;
begin
  Result := FLargeIncrement <> 10.0;
end;

function TcxCustomSpinEditProperties.IsValueTypeStored: Boolean;
begin
  Result := AssignedValues.ValueType;
end;

procedure TcxCustomSpinEditProperties.ReadZeroIncrement(Reader: TReader);
begin
  Reader.ReadBoolean;
  Increment := 0;
end;

procedure TcxCustomSpinEditProperties.ReadZeroLargeIncrement(Reader: TReader);
begin
  Reader.ReadBoolean;
  LargeIncrement := 0;

⌨️ 快捷键说明

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