unit fcxCalculationFrame;

interface

{$I fcx.inc}

uses
  SysUtils, Variants, Classes, TypInfo,
  Graphics, Controls, Forms, Dialogs, StdCtrls,
  fcxTypes, fcxSlice;

type

  { TfcxCalculationEditorFrame }

  TfcxCalculationEditorFrame = class(TFrame)
    cbScriptFunction: TComboBox;
    rbExpression: TRadioButton;
    rbScriptFunction: TRadioButton;
    procedure cbScriptFunctionDblClick(Sender: TObject);
    procedure cbScriptFunctionDropDown(Sender: TObject);
    procedure rbExpressionChange(Sender: TObject);
  private
    FSlice: TfcxSlice;
    FEventInfo: PTypeInfo;
    FItemName: String;
    FSuffix: String;
    FAllowTypeChange: Boolean;
    FCalculationType: TfcxCalculationType;
    function GetValue: String;
    procedure SetAllowTypeChange(AValue: Boolean);
    procedure SetCalculationType(AValue: TfcxCalculationType);
    procedure SetValue(const Value: String);
    procedure UpdateControl;
  public
    constructor Create(AOwner: TComponent); override;
    procedure Localize;
    property AllowTypeChange: Boolean read FAllowTypeChange write SetAllowTypeChange;
    property CalculationType: TfcxCalculationType read FCalculationType write SetCalculationType;
    property EventInfo: PTypeInfo read FEventInfo write FEventInfo;
    property ItemName: String read FItemName write FItemName;
    property Suffix: String read FSuffix write FSuffix;
    property Slice: TfcxSlice read FSlice write FSlice;
    property Value: String read GetValue write SetValue;
  end;

implementation

uses
  fcxRes,
  fcxCodeUtils
{$ifdef INCLUDE_FAST_SCRIPT}
  , fcxEditExpr
  , fcxfsFormulaEditor
{$ENDIF};

{$IFDEF FPC}
  {$R *.lfm}
{$ELSE}
  {$R *.dfm}
{$ENDIF}

procedure TfcxCalculationEditorFrame.cbScriptFunctionDblClick(Sender: TObject);
{$IFDEF INCLUDE_FAST_SCRIPT}
var
  FuncName: String;
  LinePos: Integer;
  Combo: TCombobox absolute Sender;
{$ENDIF}
begin
{$IFDEF INCLUDE_FAST_SCRIPT}
  if CalculationType = fcctScriptFunction then
  begin
    FuncName := Combo.Text;
    if FuncName = '' then
    begin
      FuncName := StringReplace(ItemName, ' ', '_', []) + Suffix;
      Combo.Text := FuncName;
      LinePos := fcxLocateEventHandler(FSlice.Script, FSlice.ScriptLanguage, FuncName, EventInfo);
      if LinePos = -1 then
        LinePos := fcxAddEvent(FSlice.Script, FSlice.ScriptLanguage, EventInfo, FuncName)
      else
        Inc(LinePos, 3);
    end
    else
    begin
      LinePos := fcxLocateEventHandler(FSlice.Script, FSlice.ScriptLanguage, FuncName, EventInfo);
      inc(LinePos, 3);
    end;
    with TfcxfsFormulaEditor.Create(Self) do
    begin
      ScriptComponent := FSlice;
      Edit(LinePos);
    end;
  end
  else
  begin
    with TfcxExprEditorForm.Create(Self) do
    begin
      ScriptComponent := FSlice;
      ExprMemo.Text := Combo.Text;
      if ShowModal = mrOk then
        Combo.Text := ExprMemo.Text;
      Free;
    end
  end;
{$ENDIF}
end;

procedure TfcxCalculationEditorFrame.cbScriptFunctionDropDown(Sender: TObject);
begin
  fcxGetEventHandlersList(FSlice.Script, FSlice.ScriptLanguage, EventInfo, (Sender as TCombobox).Items);
end;

procedure TfcxCalculationEditorFrame.rbExpressionChange(Sender: TObject);
begin
  if rbExpression.Checked then
    FCalculationType := fcctExpression
  else
    FCalculationType := fcctScriptFunction;
end;

constructor TfcxCalculationEditorFrame.Create(AOwner: TComponent);
begin
  inherited;
  FAllowTypeChange := True;
  FCalculationType := fcctExpression;
  Localize;
  UpdateControl;
end;

function TfcxCalculationEditorFrame.GetValue: String;
begin
  Result := cbScriptFunction.Text;
end;

procedure TfcxCalculationEditorFrame.SetAllowTypeChange(AValue: Boolean);
begin
  if FAllowTypeChange = AValue then Exit;
  FAllowTypeChange := AValue;
  UpdateControl;
end;

procedure TfcxCalculationEditorFrame.SetCalculationType(AValue: TfcxCalculationType);
begin
  if FCalculationType = AValue then Exit;
  FCalculationType := AValue;
  UpdateControl;
end;

procedure TfcxCalculationEditorFrame.Localize;
begin
  rbExpression.Caption := fcxResources.Get('sExpression');
  rbScriptFunction.Caption := fcxResources.Get('sScriptFunction');
end;

procedure TfcxCalculationEditorFrame.SetValue(const Value: String);
begin
  cbScriptFunction.Text := Value;
end;

procedure TfcxCalculationEditorFrame.UpdateControl;
begin
  rbExpression.Enabled := FAllowTypeChange;
  rbScriptFunction.Enabled := FAllowTypeChange;
  rbExpression.Checked := FCalculationType = fcctExpression;
  rbScriptFunction.Checked := FCalculationType = fcctScriptFunction;
  if FCalculationType = fcctExpression then
    cbScriptFunction.Style := csSimple
  else
    cbScriptFunction.Style := csDropDown;
end;

end.
