unit TableStyleDialogWin;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ColorContainer, StdCtrls, ComCtrls, FontCombobox, ASAttributes,
  ASColors;

type
  TTableStyleDialogFrm = class(TForm)
    PageControl1: TPageControl;
    tsBackground: TTabSheet;
    tsBorder: TTabSheet;
    tsText: TTabSheet;
    Label2: TLabel;
    ccBorderColor: TColorContainer;
    Label3: TLabel;
    eBorderWidth: TEdit;
    Label1: TLabel;
    ccBackgroundColor: TColorContainer;
    Label4: TLabel;
    cbBackgroundColor: TCheckBox;
    cbBorderColor: TCheckBox;
    cbBorderWidth: TCheckBox;
    cbTextFont: TFontCombobox;
    Label5: TLabel;
    eTextSize: TEdit;
    cbUseTextFont: TCheckBox;
    cbTextSize: TCheckBox;
    Label6: TLabel;
    ccTextColor: TColorContainer;
    cbUseTextColor: TCheckBox;
    Label7: TLabel;
    cbTextWeight: TComboBox;
    cbUseTextWeight: TCheckBox;
    Label8: TLabel;
    cbTextStyle: TComboBox;
    cbUseTextStyle: TCheckBox;
    Label9: TLabel;
    cbTextUnderline: TComboBox;
    cbUseTextUnderline: TCheckBox;
    Label10: TLabel;
    cbTextStrikeThrough: TComboBox;
    cbUseTextStrikeThrough: TCheckBox;
    tsAlignment: TTabSheet;
    Label11: TLabel;
    cbAlignment: TComboBox;
    cbUseAlignment: TCheckBox;
    Label12: TLabel;
    cbEllipsis: TComboBox;
    cbUseEllipsis: TCheckBox;
    cbPadding: TCheckBox;
    ePadding: TEdit;
    Label13: TLabel;
    btnOk: TButton;
    btnCancel: TButton;
    procedure ccBackgroundColorChange(Sender: TObject);
    procedure ccBorderColorChange(Sender: TObject);
    procedure eBorderWidthChange(Sender: TObject);
    procedure cbTextFontChange(Sender: TObject);
    procedure eTextSizeChange(Sender: TObject);
    procedure ccTextColorChange(Sender: TObject);
    procedure cbTextWeightChange(Sender: TObject);
    procedure cbTextStyleChange(Sender: TObject);
    procedure cbTextUnderlineChange(Sender: TObject);
    procedure cbTextStrikeThroughChange(Sender: TObject);
    procedure cbAlignmentChange(Sender: TObject);
    procedure cbEllipsisChange(Sender: TObject);
    procedure ePaddingChange(Sender: TObject);
  strict private
    FAttributes: TASAttributes;
  public
    property Attributes: TASAttributes read FAttributes write FAttributes;
    function Execute: boolean;
  end;

function StyleDialog(AOwner: TComponent; const AStyleName: string;
  var AAttributes: TASAttributes): boolean;

implementation

uses
  Math, StrUtils;

{$R *.dfm}

{ TTableStyleDialogFrm }

function TryStrToBool(const S: string; out B: boolean): boolean;
begin
  result :=  true;
  if SameText(S, 'true') then
    B := true
  else if SameText(S, 'false') then
    B := false
  else
    result := false;
end;

function TryStrToAlignment(const S: string; out T: TAlignment): boolean;
begin
  result :=  true;
  if SameText(S, 'left') then
    T := taLeftJustify
  else if SameText(S, 'center') then
    T := taCenter
  else if SameText(S, 'right') then
    T := taRightJustify
  else
    result := false;
end;

procedure TTableStyleDialogFrm.cbAlignmentChange(Sender: TObject);
begin
  cbUseAlignment.Checked := true;
end;

procedure TTableStyleDialogFrm.cbEllipsisChange(Sender: TObject);
begin
  cbUseEllipsis.Checked := true;
end;

procedure TTableStyleDialogFrm.cbTextFontChange(Sender: TObject);
begin
  cbUseTextFont.Checked := true;
end;

procedure TTableStyleDialogFrm.cbTextStrikeThroughChange(Sender: TObject);
begin
  cbUseTextStrikeThrough.Checked := true;
end;

procedure TTableStyleDialogFrm.cbTextStyleChange(Sender: TObject);
begin
  cbUseTextStyle.Checked := true;
end;

procedure TTableStyleDialogFrm.cbTextUnderlineChange(Sender: TObject);
begin
  cbUseTextUnderline.Checked := true;
end;

procedure TTableStyleDialogFrm.cbTextWeightChange(Sender: TObject);
begin
  cbUseTextWeight.Checked := true;
end;

procedure TTableStyleDialogFrm.ccBackgroundColorChange(Sender: TObject);
begin
  cbBackgroundColor.Checked := true;
end;

procedure TTableStyleDialogFrm.ccBorderColorChange(Sender: TObject);
begin
  cbBorderColor.Checked := true;
end;

procedure TTableStyleDialogFrm.ccTextColorChange(Sender: TObject);
begin
  cbUseTextColor.Checked := true;
end;

procedure TTableStyleDialogFrm.eBorderWidthChange(Sender: TObject);
begin
  cbBorderWidth.Checked := true;
end;

procedure TTableStyleDialogFrm.ePaddingChange(Sender: TObject);
begin
  cbPadding.Checked := true;
end;

procedure TTableStyleDialogFrm.eTextSizeChange(Sender: TObject);
begin
  cbTextSize.Checked := true;
end;

function TTableStyleDialogFrm.Execute: boolean;
var
  val: string;
  int: integer;
  b: boolean;
  clr: TColor;
  alignment: TAlignment;
begin
  cbBackgroundColor.Checked := FAttributes.TryGetValue('background-color', val) and TryStrToColor(val, clr);
  if cbBackgroundColor.Checked then
    ccBackgroundColor.Color := clr;

  cbBorderColor.Checked := FAttributes.TryGetValue('border-color', val) and TryStrToColor(val, clr);
  if cbBorderColor.Checked then
    ccBorderColor.Color := clr;

  cbBorderWidth.Checked := FAttributes.TryGetValue('border-width', val) and TryStrToInt(val, int) and (int >= 0);
  if cbBorderWidth.Checked then
    eBorderWidth.Text := val {sic!};

  cbUseTextFont.Checked := FAttributes.TryGetValue('font-family', val);
  if cbUseTextFont.Checked then
    cbTextFont.Text := val;

  cbTextSize.Checked := FAttributes.TryGetValue('font-size', val) and TryStrToInt(val, int) and (int > 0);
  if cbTextSize.Checked then
    eTextSize.Text := val;

  cbUseTextColor.Checked := FAttributes.TryGetValue('text-color', val) and TryStrToColor(val, clr);
  if cbUseTextColor.Checked then
    ccTextColor.Color := clr;

  cbUseTextWeight.Checked := FAttributes.TryGetValue('bold', val) and TryStrToBool(val, b);
  if cbUseTextWeight.Checked then
    cbTextWeight.ItemIndex := IfThen(b, 1, 0);

  cbUseTextStyle.Checked := FAttributes.TryGetValue('italic', val) and TryStrToBool(val, b);
  if cbUseTextStyle.Checked then
    cbTextStyle.ItemIndex := IfThen(b, 1, 0);

  cbUseTextUnderline.Checked := FAttributes.TryGetValue('underline', val) and TryStrToBool(val, b);
  if cbUseTextUnderline.Checked then
    cbTextUnderline.ItemIndex := IfThen(b, 1, 0);

  cbUseTextStrikeThrough.Checked := FAttributes.TryGetValue('strike-through', val) and TryStrToBool(val, b);
  if cbUseTextStrikeThrough.Checked then
    cbTextStrikeThrough.ItemIndex := IfThen(b, 1, 0);

  cbUseAlignment.Checked := FAttributes.TryGetValue('text-align', val) and TryStrToAlignment(val, alignment);
  case alignment of
    taLeftJustify:
      cbAlignment.ItemIndex := 0;
    taRightJustify:
      cbAlignment.ItemIndex := 2;
    taCenter:
      cbAlignment.ItemIndex := 1;
  end;

  cbUseEllipsis.Checked := FAttributes.TryGetValue('no-ellipsis', val) and TryStrToBool(val, b);
  if cbUseEllipsis.Checked then
    cbEllipsis.ItemIndex := IfThen(b, 0, 1);

  cbPadding.Checked := FAttributes.TryGetValue('padding', val) and TryStrToInt(val, int) and (int >= 0);
  if cbPadding.Checked then
    ePadding.Text := val;

  result := ShowModal = mrOk;

  if result then
  begin

    FAttributes.Clear;

    if cbBackgroundColor.Checked then
      FAttributes.SetProp('background-color', ColorToHex(ccBackgroundColor.Color));

    if cbBorderColor.Checked then
      FAttributes.SetProp('border-color', ColorToHex(ccBorderColor.Color));

    if cbBorderWidth.Checked then
      FAttributes.SetProp('border-width', eBorderWidth.Text);

    if cbUseTextFont.Checked then
      FAttributes.SetProp('font-family', cbTextFont.Text);

    if cbTextSize.Checked then
      FAttributes.SetProp('font-size', eTextSize.Text);

    if cbUseTextColor.Checked then
      FAttributes.SetProp('text-color', ColorToHex(ccTextColor.Color));

    if cbUseTextWeight.Checked then
      FAttributes.SetProp('bold', IfThen(cbTextWeight.ItemIndex = 1, 'true', 'false'));

    if cbUseTextStyle.Checked then
      FAttributes.SetProp('italic', IfThen(cbTextStyle.ItemIndex = 1, 'true', 'false'));

    if cbUseTextUnderline.Checked then
      FAttributes.SetProp('underline', IfThen(cbTextUnderline.ItemIndex = 1, 'true', 'false'));

    if cbUseTextStrikeThrough.Checked then
      FAttributes.SetProp('strike-through', IfThen(cbTextStrikeThrough.ItemIndex = 1, 'true', 'false'));

    if cbUseAlignment.Checked and (cbAlignment.ItemIndex <> -1) then
      case cbAlignment.ItemIndex of
        0:
          FAttributes.SetProp('text-align', 'left');
        1:
          FAttributes.SetProp('text-align', 'center');
        2:
          FAttributes.SetProp('text-align', 'right');
      end;

    if cbUseEllipsis.Checked then
      FAttributes.SetProp('no-ellipsis', IfThen(cbEllipsis.ItemIndex = 0, 'true', 'false'));

    if cbPadding.Checked then
      FAttributes.SetProp('padding', ePadding.Text);

  end;
end;

function StyleDialog(AOwner: TComponent; const AStyleName: string;
  var AAttributes: TASAttributes): boolean;
begin
  with TTableStyleDialogFrm.Create(AOwner) do
    try
      if AStyleName <> '' then
        Caption := AStyleName;
      Attributes := AAttributes;
      result := Execute;
      if result then
        AAttributes := Attributes;
    finally
      Free;
    end;
end;


end.
