
{******************************************}
{                                          }
{             FastReport v5.0              }
{            Expression Editor             }
{                                          }
{         Copyright (c) 1998-2014          }
{         by Alexander Tzyganenko,         }
{            Fast Reports Inc.             }
{                                          }
{******************************************}

unit fcxEditExpr;

interface

{$I fcx.inc}

uses
  SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, ExtCtrls, Buttons, fcxSlice, fcxDataTree
  {$IFDEF FPC}
  , LCLType
  {$ELSE}
  , ImgList
  {$ENDIF}
{$IFDEF Delphi6}
, Variants
{$ENDIF};
  

type

  { TfcxExprEditorForm }

  TfcxExprEditorForm = class(TForm)
    ExprMemo: TMemo;
    Panel1: TPanel;
    OkB: TButton;
    CancelB: TButton;
    Splitter1: TSplitter;
    Panel2: TPanel;
    ExprL: TLabel;
    Panel: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure ExprMemoDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure ExprMemoDragDrop(Sender, Source: TObject; X, Y: Integer);
  private
    FDataTree: TfcxDataTreeForm;
    FScriptComponent: TfcxSlice;
    procedure FillSliceItems;
    procedure OnDataTreeDblClick(Sender: TObject);
    procedure SetScriptComponent(AValue: TfcxSlice);
  public
    property ScriptComponent: TfcxSlice read FScriptComponent write SetScriptComponent;
  end;


implementation
{$IFDEF FPC}
{$R *.lfm}
{$ELSE}
{$R *.DFM}
{$ENDIF}

uses
  //frxDock,
  fcxRes;

{ TfcxExprEditorForm }

procedure TfcxExprEditorForm.FormCreate(Sender: TObject);
begin
  Caption := fcxResources.Get('sExpressionEditor');
  ExprL.Caption := fcxResources.Get('sExpression') + ':';
  OkB.Caption := fcxResources.Get('sOkBtn');
  CancelB.Caption := fcxResources.Get('sCancelBtn');
  ExprMemo.BorderStyle := bsSingle;

  FDataTree := TfcxDataTreeForm.Create(Self);
  FDataTree.OnDblClick := OnDataTreeDblClick;
  FDataTree.Parent := Panel;
  FDataTree.Align := alClient;
  FDataTree.HintPanel.Height := 60;
  FDataTree.Visible := True;
  FDataTree.AddBrackets := True;
  FDataTree.AddCaptions := False;

  if UseRightToLeftAlignment then
    FlipChildren(True);
  {$IFDEF DELPHI24}
    ScaleForPPI(Screen.PixelsPerInch);
  {$ENDIF}
end;

procedure TfcxExprEditorForm.OnDataTreeDblClick(Sender: TObject);
begin
  ExprMemo.SelText := FDataTree.GetFieldName;
  ExprMemo.SetFocus;
end;

procedure TfcxExprEditorForm.FillSliceItems;
var
  AItems: TStringList;
  i: integer;
begin
  if not Assigned(ScriptComponent) then
    Exit;
  AItems := TStringList.Create;
  for i := 0 to ScriptComponent.MeasuresContainer.Count - 1 do
    AItems.Add(ScriptComponent.MeasuresContainer.Measures[i].Name + '=' + ScriptComponent.MeasuresContainer.Measures[i].Caption);
  FDataTree.Measures.Assign(AItems);
  AItems.Clear;
  with ScriptComponent.XAxisContainer do
    for i := 0 to LevelCount - 1 do
      if LevelInfoWOMeasures[i].LevelType = fcATLT_SubGroup then
        AItems.Add(LevelInfoWOMeasures[i].RegionField.Name + ' SubGroup=' + LevelInfoWOMeasures[i].RegionField.Caption)
      else
        AItems.Add(LevelInfoWOMeasures[i].RegionField.Name + '=' + LevelInfoWOMeasures[i].RegionField.Caption);
  with ScriptComponent.YAxisContainer do
    for i := 0 to LevelCount - 1 do
      if LevelInfoWOMeasures[i].LevelType = fcATLT_SubGroup then
        AItems.Add(LevelInfoWOMeasures[i].RegionField.Name + ' SubGroup=' + LevelInfoWOMeasures[i].RegionField.Caption)
      else
        AItems.Add(LevelInfoWOMeasures[i].RegionField.Name + '=' + LevelInfoWOMeasures[i].RegionField.Caption);
  FDataTree.Dimensions.Assign(AItems);
  AItems.Clear;
  for i := 0 to ScriptComponent.SliceFields.Count - 1 do
    AItems.Add(ScriptComponent.SliceFields[i].FieldName + '=' + ScriptComponent.SliceFields[i].Caption);
  FDataTree.SliceFields.Assign(AItems);
  AItems.Free;
end;

procedure TfcxExprEditorForm.SetScriptComponent(AValue: TfcxSlice);
begin
  if FScriptComponent <> Avalue then
  begin
    FScriptComponent := AValue;
    FillSliceItems;
    FDataTree.UpdateItems;
  end;
end;

procedure TfcxExprEditorForm.ExprMemoDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
  Accept := (Source is TTreeView) and (TControl(Source).Owner = FDataTree) and
    (FDataTree.GetFieldName <> '');
end;

procedure TfcxExprEditorForm.ExprMemoDragDrop(Sender, Source: TObject; X,
  Y: Integer);
begin
  ExprMemo.SelText := FDataTree.GetFieldName;
  ExprMemo.SetFocus;
end;

end.
