{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012 by Steema Software      }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.Stroke;
{$I TeeDefs.inc}

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms,
  FMX.Dialogs, FMX.Colors, FMXTee.Canvas, FMX.Edit, FMX.ListBox

  {$IFNDEF D18}
  {$DEFINE HASFMX20}
  {$ENDIF}

  {$IFNDEF HASFMX20}
  , FMX.StdCtrls
  {$ENDIF}
  ;

type
  TStrokeEditor = class(TForm)
    ComboColorBox1: TComboColorBox;
    CBVisible: TCheckBox;
    CBPenStyle: TComboBox;
    SolidItem: TListBoxItem;
    DashItem: TListBoxItem;
    DotItem: TListBoxItem;
    DashDotItem: TListBoxItem;
    DashDotDotItem: TListBoxItem;
    Panel1: TPanel;
    Button1: TButton;
    CBWidth: TComboTrackBar;
    Label1: TLabel;
    SmallDotsItem: TListBoxItem;
    procedure ComboColorBox1Change(Sender: TObject);
    procedure CBVisibleChange(Sender: TObject);
    procedure CBPenStyleChange(Sender: TObject);
    procedure CBWidthChange(Sender: TObject);
  private
    { Private declarations }
  protected
    Pen : TTeePen;
  public
    { Public declarations }
    class function Edit(AOwner:TComponent; const APen:TTeePen):Boolean;
    procedure RefreshPen(const APen:TTeePen);

    // Helper procs
    class procedure SetRepeatClick(ASpinBox:TSpinBox; const AValue:Boolean);
    class function InsertForm(const APen:TTeePen; const AParent:TControl):TStrokeEditor;
  end;

function EmbeddForm(AForm:TCommonCustomForm; AParent:TFmxObject):TCommonCustomForm;

const
  TeeCursorPrefix='cr';

Function DeleteCursorPrefix(Const S:String):String;
procedure TeeFillCursors(ACombo:TComboBox; ACursor:TCursor);

function TeeInputQuery(const ATitle,ACaption:String; var AValue:String):Boolean;
function YesNo(const AMessage:String):Boolean;
function YesNoCancel(const AMessage:String):Integer;

implementation

{$R *.fmx}

uses
  System.UIConsts, FMXTee.Procs;

function EmbeddForm(AForm:TCommonCustomForm; AParent:TFmxObject):TCommonCustomForm;
begin
  result:=AForm;

  while AForm.ChildrenCount>0 do
        AForm.Children[0].Parent:=AParent;
end;

type
  TTempCursors=class
  private
    FCombo : TComboBox;
    procedure ProcGetCursors(const S: string);
  end;

Function DeleteCursorPrefix(Const S:String):String;
begin
  result:=S;
  if Copy(result,1,2)=TeeCursorPrefix then Delete(result,1,2);
end;

procedure TTempCursors.ProcGetCursors(const S: string);
var tmp : TListBoxItem;
begin
  tmp:=TListBoxItem.Create(FCombo.Owner);
  tmp.Text:=DeleteCursorPrefix(S);
  FCombo.AddObject(tmp);
end;

procedure SetComboCursor(ACombo:TComboBox; ACursor:TCursor);
var tmpSt : String;
begin
  with ACombo do
  if CursorToIdent(ACursor,tmpSt) then
     ItemIndex:=ListBox.Items.IndexOf(DeleteCursorPrefix(tmpSt))
  else
     ItemIndex:=-1;
end;

procedure TeeFillCursors(ACombo:TComboBox; ACursor:TCursor);
var tmp : TTempCursors;
begin
  ACombo.BeginUpdate;
  ACombo.Clear;

  tmp:=TTempCursors.Create;
  try
    tmp.FCombo:=ACombo;
    GetCursorValues(tmp.ProcGetCursors);
    tmp.ProcGetCursors(TeeMsg_TeeHand);
  finally
    tmp.Free;
  end;

  ACombo.EndUpdate;

  SetComboCursor(ACombo,ACursor);
end;

function TeeInputQuery(const ATitle,ACaption:String; var AValue:String):Boolean;
{$IFDEF D24}
var DialogSvc: IFMXDialogServiceSync;
    tmp : Array[0..0] of String;
{$ENDIF}
{$IFDEF FMI}
var tmp : WideString;
{$ENDIF}
begin
  {$IFDEF D24}
  if TPlatformServices.Current.SupportsPlatformService(IFMXDialogServiceSync, DialogSvc) then
  begin
    tmp[0]:=AValue;

    result:=DialogSvc.InputQuerySync(ATitle,ACaption,tmp);

    if result then
       AValue:=tmp[0];
  end
  else
     result:=False;

  {$ELSE}
  {$IFDEF FMI}
  tmp:=AValue;
  result:=InputQuery(ATitle,ACaption,tmp);
  AValue:=tmp;
  {$ELSE}
  result:=InputQuery(ATitle,ACaption,AValue);
  {$ENDIF}
  {$ENDIF}
end;

function YesNoCancel(const AMessage:String):Integer;
{$IFDEF D24}
var DialogSvc: IFMXDialogServiceSync;
{$ENDIF}
begin
  {$IFDEF D24}
  if TPlatformServices.Current.SupportsPlatformService(IFMXDialogServiceSync, DialogSvc) then
     result:=DialogSvc.MessageDialogSync(AMessage,TMsgDlgType.mtConfirmation,mbYesNoCancel,TMsgDlgBtn.mbYes,-1)
  else
     result:=mrCancel;

  {$ELSE}
  result:=MessageDlg(AMessage, TMsgDlgType.mtConfirmation, mbYesNo, 0);
  {$ENDIF}
end;

function YesNo(const AMessage:String):Boolean;
begin
  result:= MessageDlg(AMessage, TMsgDlgType.mtConfirmation, mbYesNo, 0) = mrYes;
end;

class function TStrokeEditor.InsertForm(const APen:TTeePen; const AParent:TControl):TStrokeEditor;
begin
  result:=TStrokeEditor.Create(AParent.Owner);
  result.RefreshPen(APen);
  result.Panel1.Visible:=False;
  EmbeddForm(result,AParent);
end;

class procedure TStrokeEditor.SetRepeatClick(ASpinBox:TSpinBox; const AValue:Boolean);

  procedure SetButton(const AName:String);
  var B : TFmxObject;
  begin
    B := ASpinBox.FindStyleResource(AName);
    if Assigned(B) and (B is TCustomButton) then
       TCustomButton(B).RepeatClick:=AValue;
  end;

begin
  SetButton('minusbutton');
  SetButton('plusbutton');
end;

procedure TStrokeEditor.CBVisibleChange(Sender: TObject);
begin
  Pen.Visible:=CBVisible.IsChecked;
end;

procedure TStrokeEditor.CBPenStyleChange(Sender: TObject);
begin
  if CBPenStyle.Selected=SmallDotsItem then
     Pen.SmallDots:=True
  else
  begin
    Pen.Style:=TPenStyle(CBPenStyle.ItemIndex);
    Pen.SmallDots:=False;
  end;
end;

procedure TStrokeEditor.ComboColorBox1Change(Sender: TObject);
begin
  Pen.Color:=ComboColorBox1.Color;
end;

procedure TStrokeEditor.CBWidthChange(Sender: TObject);
begin
  if Assigned(Pen) then
     Pen.Width:=CBWidth.Value;
end;

procedure TStrokeEditor.RefreshPen(const APen:TTeePen);
begin
  Pen:=APen;

  ComboColorBox1.Color:=Pen.Color;
  CBWidth.Value:=Pen.Width;
  CBVisible.IsChecked:=Pen.Visible;

  if Pen.SmallDots then
     CBPenStyle.ItemIndex:=SmallDotsItem.Index
  else
     CBPenStyle.ItemIndex:=Ord(Pen.Style);
end;

class function TStrokeEditor.Edit(AOwner: TComponent; const APen: TTeePen): Boolean;
begin
  with TStrokeEditor.Create(AOwner) do
  try
    RefreshPen(APen);
    result:=ShowModal=mrOk;
  finally
    Free;
  end;
end;

end.
