unit TableQuickDesignWin;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ImgList, ASTable, ASTableEditor, ASColors,
  Menus, System.ImageList, HSColorMap, UITypes;

type
  TStyleRec = record
    Heading1: TColor;
    Heading2: TColor;
    Highlight1: TColor;
    Highlight2: TColor;
  end;

type
  TTableQuickDesignFrm = class(TForm)
    gbColors: TGroupBox;
    gbOptions: TGroupBox;
    cbFirstRow: TCheckBox;
    cbFirstCol: TCheckBox;
    cbEvenRows: TCheckBox;
    cbEvenCols: TCheckBox;
    cbLastRow: TCheckBox;
    cbLastCol: TCheckBox;
    gbPreview: TGroupBox;
    btnOk: TButton;
    btnCancel: TButton;
    HSColorMap: THSColorMap;
    tbValue: TTrackBar;
    cbDarkBackground: TCheckBox;
    cbGridlinesX: TCheckBox;
    cbGridlinesY: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure UpdatePreview(Sender: TObject);
    procedure lvColorsSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure tbValueChange(Sender: TObject);
  private
    FPreviewEditor: TASTableEditor;
    FTable: TASTable;
    FPopupMenu: TPopupMenu;
    function MakeStyleRec(const ABaseColor: THSV): TStyleRec;
    function GetPreviewTable: TASTable;
  public
    property Table: TASTable read FTable write FTable;
    property PreviewTable: TASTable read GetPreviewTable;
  end;

var
  TableQuickDesignFrm: TTableQuickDesignFrm;

implementation

uses Math, StrUtils, ASAttributes;

{$R *.dfm}

procedure TTableQuickDesignFrm.FormCreate(Sender: TObject);
begin

  FPreviewEditor := TASTableEditor.Create(Self);
  FPreviewEditor.Parent := gbPreview;
  FPreviewEditor.SetBounds(16, 24, gbPreview.ClientWidth - 32, gbPreview.ClientHeight - 40);
  FPreviewEditor.Anchors := [akLeft, akTop, akRight, akBottom];
  FPreviewEditor.Color := clWhite;
  FPreviewEditor.DoubleBuffered := true;
  FPreviewEditor.NoSelection := true;

  FPopupMenu := TPopupMenu.Create(Self);

end;

procedure TTableQuickDesignFrm.FormShow(Sender: TObject);
begin
  if Assigned(FTable) then
    FPreviewEditor.Table := FTable;

  FPreviewEditor.ReadOnly := true;
  FPreviewEditor.PopupMenu := FPopupMenu;

  HSColorMap.Hue := 226;
  HSColorMap.Saturation := 1;

  UpdatePreview(Sender);
end;

function TTableQuickDesignFrm.GetPreviewTable: TASTable;
begin
  result := FPreviewEditor.Table;
end;

procedure TTableQuickDesignFrm.lvColorsSelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
begin
  UpdatePreview(Sender);
end;

function TTableQuickDesignFrm.MakeStyleRec(const ABaseColor: THSV): TStyleRec;
begin
  if cbDarkBackground.Checked then
    with Result, THSV(ABaseColor) do
    begin
      Heading1 := TRGB(THSV.Create(Hue, Saturation * 0.5, Value * 0.6));
      Heading2 := TRGB(THSV.Create(Hue, Saturation * 0.5, Value * 0.7));
      Highlight1 := TRGB(THSV.Create(Hue, Saturation * 0.3, 0.3));
      Highlight2 := TRGB(THSV.Create(Hue, Saturation * 0.3, 0.3));
    end
  else
    with Result, THSV(ABaseColor) do
    begin
      Heading1 := TRGB(THSV.Create(Hue, Saturation * 0.572, Value * 0.941));
      Heading2 := TRGB(THSV.Create(Hue, Saturation * 0.572, Value * 0.741));
      Highlight1 := TRGB(THSV.Create(Hue, Saturation * 0.08, 0.976));
      Highlight2 := TRGB(THSV.Create(Hue, Saturation * 0.08, 0.976));
    end;
end;

procedure TTableQuickDesignFrm.tbValueChange(Sender: TObject);
begin
  HSColorMap.Value := 1 - tbValue.Position / tbValue.Max;
  UpdatePreview(Sender);
end;

procedure TTableQuickDesignFrm.UpdatePreview(Sender: TObject);
var
  style: TStyleRec;
  HadGridX, HasGridX: boolean;
  HadGridY, HasGridY: boolean;
begin

  HadGridX := FPreviewEditor.Table.Style['horizontal-grid-width'] = '1';
  HasGridX := cbGridlinesX.Checked;

  HadGridY := FPreviewEditor.Table.Style['vertical-grid-width'] = '1';
  HasGridY := cbGridlinesY.Checked;

  style := MakeStyleRec(THSV.Create(HSColorMap.Color.hsvHue, HSColorMap.Color.hsvSaturation, HSColorMap.Color.hsvValue));

  FPreviewEditor.Table.Style.Clear;
  FPreviewEditor.Table.FirstRow.Clear;
  FPreviewEditor.Table.FirstCol.Clear;
  FPreviewEditor.Table.LastRow.Clear;
  FPreviewEditor.Table.LastCol.Clear;
  FPreviewEditor.Table.EvenRows.Clear;
  FPreviewEditor.Table.EvenCols.Clear;
  FPreviewEditor.Table.OddRows.Clear;
  FPreviewEditor.Table.OddCols.Clear;

  FPreviewEditor.Table.Style.SetProp('horizontal-grid-width', IfThen(cbGridlinesX.Checked, '1', '0'));
  FPreviewEditor.Table.Style.SetProp('horizontal-grid-color', ColorToHex(style.Heading1));

  FPreviewEditor.Table.Style.SetProp('vertical-grid-width', IfThen(cbGridlinesY.Checked, '1', '0'));
  FPreviewEditor.Table.Style.SetProp('vertical-grid-color', ColorToHex(style.Heading1));

  if cbDarkBackground.Checked then
  begin
    FPreviewEditor.Table.Style.SetProp('background-color', '#000000');
    FPreviewEditor.Table.Style.SetProp('text-color', '#FFFFFF');
  end;

  if cbFirstRow.Checked then
  begin
    FPreviewEditor.Table.FirstRow.SetProp('background-color', ColorToHex(style.Heading1));
    FPreviewEditor.Table.FirstRow.SetProp('text-color',
      ColorToHex(IfThen(ColorIsDark(style.Heading1), clWhite, clBlack)));
    FPreviewEditor.Table.FirstRow.SetProp('bold', 'true');
  end;

  if cbFirstCol.Checked then
  begin
    FPreviewEditor.Table.FirstCol.SetProp('background-color', ColorToHex(style.Heading1));
    FPreviewEditor.Table.FirstCol.SetProp('text-color',
      ColorToHex(IfThen(ColorIsDark(style.Heading1), clWhite, clBlack)));
    FPreviewEditor.Table.FirstCol.SetProp('bold', 'true');
  end;

  if cbLastRow.Checked then
  begin
    FPreviewEditor.Table.LastRow.SetProp('background-color', ColorToHex(style.Heading2));
    FPreviewEditor.Table.LastRow.SetProp('text-color',
      ColorToHex(IfThen(ColorIsDark(style.Heading2), clWhite, clBlack)));
    FPreviewEditor.Table.LastRow.SetProp('bold', 'true');
  end;

  if cbLastCol.Checked then
  begin
    FPreviewEditor.Table.LastCol.SetProp('background-color', ColorToHex(style.Heading2));
    FPreviewEditor.Table.LastCol.SetProp('text-color',
      ColorToHex(IfThen(ColorIsDark(style.Heading2), clWhite, clBlack)));
    FPreviewEditor.Table.LastCol.SetProp('bold', 'true');
  end;

  if cbEvenRows.Checked then
  begin
    FPreviewEditor.Table.EvenRows.SetProp('background-color', ColorToHex(style.Highlight1));
    FPreviewEditor.Table.EvenRows.SetProp('text-color',
      ColorToHex(IfThen(ColorIsDark(style.Highlight1), clWhite, clBlack)));
  end;

  if cbEvenCols.Checked then
  begin
    FPreviewEditor.Table.EvenCols.SetProp('background-color', ColorToHex(style.Highlight2));
    FPreviewEditor.Table.EvenCols.SetProp('text-color',
      ColorToHex(IfThen(ColorIsDark(style.Highlight2), clWhite, clBlack)));
  end;

  if (HasGridX xor HadGridX) or (HasGridY xor HadGridY) then
    FPreviewEditor.UpdateMetricsFromStyle;

  FPreviewEditor.Invalidate;

end;

end.

