cocoa: refactor CocoaSpinEdit implementation. Remove the use of Spin unit from CocoaPrivate. Change notifications during text change. #33047

git-svn-id: trunk@57187 -
This commit is contained in:
dmitry 2018-01-30 04:03:56 +00:00
parent b4f5e3399c
commit deb99c1a0c
2 changed files with 34 additions and 23 deletions

View File

@ -35,7 +35,7 @@ uses
MacOSAll, CocoaAll, CocoaUtils, CocoaGDIObjects,
// LCL
LMessages, LCLMessageGlue, ExtCtrls, Graphics, Forms,
LCLType, LCLProc, Controls, ComCtrls, Spin, StdCtrls;
LCLType, LCLProc, Controls, ComCtrls, StdCtrls;
const
SPINEDIT_DEFAULT_STEPPER_WIDTH = 15;
@ -1000,11 +1000,12 @@ end;
callback: ICommonCallback;
Stepper: NSStepper;
NumberFormatter: NSNumberFormatter;
Spin: TCustomFloatSpinEdit;
decimalPlaces: Integer;
//Spin: TCustomFloatSpinEdit;
procedure dealloc; override;
procedure UpdateValueFromCocoa(); message 'UpdateValueFromCocoa';
procedure UpdateControl(ASpinEdit: TCustomFloatSpinEdit); message 'UpdateControl:';
procedure CreateSubcontrols(ASpinEdit: TCustomFloatSpinEdit; const AParams: TCreateParams); message 'CreateSubControls:AParams:';
function updateStepper: boolean; message 'updateStepper';
procedure UpdateControl(min, max, inc, avalue: double; ADecimalPlaces: Integer); message 'UpdateControl:::::';
procedure CreateSubcontrols(const AParams: TCreateParams); message 'CreateSubControls:';
procedure PositionSubcontrols(const ALeft, ATop, AWidth, AHeight: Integer); message 'PositionSubcontrols:ATop:AWidth:AHeight:';
procedure StepperChanged(sender: NSObject); message 'StepperChanged:';
function GetFieldEditor: TCocoaFieldEditor; message 'GetFieldEditor';
@ -5091,30 +5092,36 @@ begin
inherited dealloc;
end;
procedure TCocoaSpinEdit.UpdateValueFromCocoa();
function TCocoaSpinEdit.updateStepper: boolean;
var
lValid: Boolean = False;
lValue: String;
lFloat: Double;
iv : Double;
begin
lValue := CocoaUtils.NSStringToString(stringValue());
lValid := SysUtils.TryStrToFloat(lValue, lFloat);
if lValid then
Spin.Value := lFloat;
begin
Stepper.setDoubleValue(lFloat);
Result := true;
end else
Result := false;
end;
procedure TCocoaSpinEdit.UpdateControl(ASpinEdit: TCustomFloatSpinEdit);
procedure TCocoaSpinEdit.UpdateControl(min, max, inc, avalue: double; ADecimalPlaces: Integer);
begin
Stepper.setMaxValue(ASpinEdit.MaxValue);
Stepper.setMinValue(ASpinEdit.MinValue);
Stepper.setIncrement(ASpinEdit.Increment);
Stepper.setDoubleValue(ASpinEdit.Value);
decimalPlaces := ADecimalPlaces;
Stepper.setMinValue(min);
Stepper.setMaxValue(max);
Stepper.setIncrement(inc);
Stepper.setDoubleValue(avalue);
// update the UI too
StepperChanged(Self);
end;
procedure TCocoaSpinEdit.CreateSubcontrols(ASpinEdit: TCustomFloatSpinEdit; const AParams: TCreateParams);
procedure TCocoaSpinEdit.CreateSubcontrols(const AParams: TCreateParams);
var
lParams: TCreateParams;
begin
@ -5122,8 +5129,6 @@ begin
WriteLn('[TCocoaSpinEdit.CreateSubcontrols]');
{$ENDIF}
Spin := ASpinEdit;
// Now creates the subcontrols
lParams := AParams;
//lParams.Style := AParams.Style or WS_VISIBLE;
@ -5173,7 +5178,7 @@ var
lNSStr: NSString;
lStr: string;
begin
lStr := Format('%.*f', [Spin.DecimalPlaces, Stepper.doubleValue()]);
lStr := Format('%.*f', [DecimalPlaces, Stepper.doubleValue()]);
lNSStr := CocoaUtils.NSStringUtf8(lStr);
setStringValue(lNSStr);
lNSStr.release;
@ -5197,14 +5202,16 @@ end;
procedure TCocoaSpinEdit.textDidEndEditing(notification: NSNotification);
begin
UpdateValueFromCocoa();
updateStepper;
StepperChanged(nil); // and refresh self
inherited textDidEndEditing(notification);
//if Assigned(callback) then callback.SendOnTextChanged;
end;
procedure TCocoaSpinEdit.controlTextDidChange(obj: NSNotification);
begin
// This was causing bug 31192
//UpdateValueFromCocoa();
updateStepper;
if Assigned(callback) then callback.SendOnTextChanged;
end;
function TCocoaSpinEdit.acceptsFirstResponder: Boolean;

View File

@ -43,6 +43,11 @@ type
implementation
procedure UpdateControlLCLToCocoa(src: TCustomFloatSpinEdit; dst: TCocoaSpinEdit);
begin
dst.UpdateControl(src.MinValue, src.MaxValue, src.Increment, src.Value, src.DecimalPlaces);
end;
{ TCocoaWSCustomFloatSpinEdit }
{------------------------------------------------------------------------------
@ -63,8 +68,8 @@ begin
if Result = 0 then Exit;
lSpin.callback := TLCLCommonCallback.Create(lSpin, AWinControl);
lSpin.CreateSubcontrols(TCustomFloatSpinEdit(AWinControl), AParams);
lSpin.UpdateControl(TCustomFloatSpinEdit(AWinControl));
lSpin.CreateSubcontrols(AParams);
UpdateControlLCLToCocoa(TCustomFloatSpinEdit(AWinControl), lSpin);
end;
{------------------------------------------------------------------------------
@ -98,8 +103,7 @@ begin
if ACustomFloatSpinEdit = nil then Exit;
if not ACustomFloatSpinEdit.HandleAllocated then Exit;
lSpin := TCocoaSpinEdit(ACustomFloatSpinEdit.Handle);
lSpin.UpdateControl(ACustomFloatSpinEdit);
UpdateControlLCLToCocoa(ACustomFloatSpinEdit, lSpin);
end;
class procedure TCocoaWSCustomFloatSpinEdit.SetBounds(