unit fuQExport4XlsxEditor;
{$I QExport4VerCtrl.inc}
interface

uses
  {$IFDEF VCL16}
    Winapi.Windows,
    Winapi.Messages,
    System.Variants,
    System.Classes,
    System.SysUtils,
    Vcl.Graphics,
    Vcl.Controls,
    Vcl.Forms,
    Vcl.Dialogs,
    Vcl.ExtCtrls,
    Vcl.StdCtrls,
    Vcl.Buttons,
    Vcl.ComCtrls,
    Vcl.ToolWin,
    Vcl.ImgList,
    Vcl.Menus,
  {$ELSE}
    Windows,
    Messages,
    SysUtils,
    {$IFDEF VCL6}
      Variants,
    {$ENDIF}
    Classes,
    Graphics,
    Controls,
    Forms,
    Dialogs,
    ExtCtrls,
    StdCtrls,
    Buttons,
    ComCtrls,
    ToolWin,
    ImgList,
    Menus,
  {$ENDIF}
  QExport4,
  QExport4Common,
  QExport4Xlsx;

type
  TfmQExport4XlsxEditor = class(TForm)
    paButtons: TPanel;
    buOK: TButton;
    buCancel: TButton;
    paFile: TPanel;
    laFileName: TLabel;
    btnSave: TSpeedButton;
    edFileName: TEdit;
    chkShow: TCheckBox;
    chkPrint: TCheckBox;
    paSelector: TPanel;
    pcStyleEditor: TPageControl;
    tsOptions: TTabSheet;
    lvOptions: TListView;
    tsStripStyles: TTabSheet;
    tlbProcessing: TToolBar;
    buAdd: TToolButton;
    buRemove: TToolButton;
    tlbSeparator1: TToolButton;
    buMoveUp: TToolButton;
    buMoveDown: TToolButton;
    rgStripStyle: TRadioGroup;
    lvSStyles: TListView;
    paTableName: TPanel;
    laTableName: TLabel;
    edSheetName: TEdit;
    paOptions: TPanel;
    pcOptions: TPageControl;
    tsCommon: TTabSheet;
    bvl1: TBevel;
    bvl2: TBevel;
    laSkipRecCount_01: TLabel;
    laSkipRecCount_02: TLabel;
    laCaptionRow: TLabel;
    laExportRecCount_02: TLabel;
    chkAllowCaptions: TCheckBox;
    chkAutoCalcStrType: TCheckBox;
    chkGoToFirsrRec: TCheckBox;
    chkVisibleFields: TCheckBox;
    edSkipRecCount: TEdit;
    edCaptionRow: TEdit;
    rbExportAll: TRadioButton;
    rbExportOnly: TRadioButton;
    edExportRecCount: TEdit;
    tsHeader: TTabSheet;
    meHeader: TMemo;
    tsFooter: TTabSheet;
    meFooter: TMemo;
    tsFormats: TTabSheet;
    laInteger: TLabel;
    laDate: TLabel;
    laDateTime: TLabel;
    laTrue: TLabel;
    laFalse: TLabel;
    laNull: TLabel;
    laFloat: TLabel;
    laTime: TLabel;
    laCurrency: TLabel;
    edInteger: TEdit;
    edDate: TEdit;
    edDateTime: TEdit;
    edTrue: TEdit;
    edFalse: TEdit;
    edNull: TEdit;
    edFloat: TEdit;
    edTime: TEdit;
    edCurrency: TEdit;
    btnRestoreInitial: TBitBtn;
    btnResetDefault: TBitBtn;
    paStylesEdit: TPanel;
    pcAllOptions: TPageControl;
    tsCommonCellStyle: TTabSheet;
    pcCommonCellStyle: TPageControl;
    tsFont: TTabSheet;
    bvl3: TBevel;
    bvl4: TBevel;
    bvl5: TBevel;
    laXlsxFont: TLabel;
    laXlsxFontSize: TLabel;
    btnFontColor: TSpeedButton;
    pbFontColor: TPaintBox;
    bvl6: TBevel;
    btnFontBold: TSpeedButton;
    btnFontItalic: TSpeedButton;
    btnUnderlineSingle: TSpeedButton;
    btnHorizontalLeft: TSpeedButton;
    btnHorizontalCenter: TSpeedButton;
    btnHorizontalRight: TSpeedButton;
    bvl8: TBevel;
    btnVerticalTop: TSpeedButton;
    btnVerticalCenter: TSpeedButton;
    btnVerticalBottom: TSpeedButton;
    btnBackgroundColor: TSpeedButton;
    pbBackColor: TPaintBox;
    cbXlsxFont: TComboBox;
    cbXlsxFontSize: TComboBox;
    chkUseBackground: TCheckBox;
    tsBorder: TTabSheet;
    bvl9: TBevel;
    btnBorderColor: TSpeedButton;
    pbBorder: TPaintBox;
    laBorderStyle: TLabel;
    laBorderColor: TLabel;
    chkUseBorder: TCheckBox;
    ilButtons: TImageList;
    ilListView: TImageList;
    ColorDialog: TColorDialog;
    sdFileName: TSaveDialog;
    pmHeaderFooter: TPopupMenu;
    miInsertTab: TMenuItem;
    miClear: TMenuItem;
    btnWrapText: TSpeedButton;
    cbBorderStyle: TComboBox;
    procedure edFileNameChange(Sender: TObject);
    procedure edSheetNameChange(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure lvOptionsSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure lvSStylesSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure pcStyleEditorChange(Sender: TObject);
    procedure btnFontColorMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnFontColorMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnFontColorClick(Sender: TObject);
    procedure btnBackgroundColorClick(Sender: TObject);
    procedure btnBackgroundColorMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure btnBackgroundColorMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure pbFontColorPaint(Sender: TObject);
    procedure pbBackColorPaint(Sender: TObject);
    procedure chkUseBackgroundClick(Sender: TObject);
    procedure chkUseBorderClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnFontBoldClick(Sender: TObject);
    procedure btnFontItalicClick(Sender: TObject);
    procedure btnUnderlineSingleClick(Sender: TObject);
    procedure rbExportAllClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure cbXlsxFontChange(Sender: TObject);
    procedure cbXlsxFontSizeChange(Sender: TObject);
    procedure lvSStylesChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure buAddClick(Sender: TObject);
    procedure buRemoveClick(Sender: TObject);
    procedure buMoveUpClick(Sender: TObject);
    procedure buMoveDownClick(Sender: TObject);
    procedure btnHorizontalLeftClick(Sender: TObject);
    procedure btnHorizontalCenterClick(Sender: TObject);
    procedure btnHorizontalRightClick(Sender: TObject);
    procedure btnVerticalTopClick(Sender: TObject);
    procedure btnVerticalCenterClick(Sender: TObject);
    procedure btnVerticalBottomClick(Sender: TObject);
    procedure btnWrapTextClick(Sender: TObject);
    procedure btnBorderColorClick(Sender: TObject);
    procedure btnBorderColorMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure btnBorderColorMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure pbBorderPaint(Sender: TObject);
    procedure cbBorderStyleChange(Sender: TObject);
  private
    FExportXlsx: TQExport4Xlsx;
    FHeaderStyle: TXlsxCellStyle;
    FCaptionRowStyle: TXlsxCellStyle;
    FDataStyle: TXlsxCellStyle;
    FFooterStyle: TXlsxCellStyle;
    FStripStylesList: TXlsxStyleList;

    FFileName: WideString;
    FSheetName: WideString;
    FSelectedOptions: TXlsxCellStyle;
    FEnableFontAndBorder: Boolean;
    procedure SetFileName(const Value: WideString);
    procedure SetSheetName(const Value: WideString);
    procedure SetExportXlsx(const Value: TQExport4Xlsx);
    procedure SetSelectedOptions(const Value: TXlsxCellStyle);
    procedure SetEnableFontAndBorder(const Value: Boolean);
    procedure SetFocusedToSelected(Item: TListItem);
    procedure SetStripStylesNumber(Item: TListItem);
  private
    procedure SetFontAndBorder(Source: TXlsxCellStyle);
  public
    procedure ApplyChanges;
    procedure FillStripStyles;
    procedure FillOptions;
    procedure FillOther;
    procedure Default;
    
    property FileName: WideString read FFileName write SetFileName;
    property SheetName: WideString read FSheetName write SetSheetName;

    property ExportXlsx: TQExport4Xlsx read FExportXlsx
      write SetExportXlsx;
    property SelectedOptions: TXlsxCellStyle read FSelectedOptions
      write SetSelectedOptions;
    property EnableFontAndBorder: Boolean read FEnableFontAndBorder
      write SetEnableFontAndBorder;
  end;

var
  fmQExport4XlsxEditor: TfmQExport4XlsxEditor;

function RunQExportXlsxEditor(AExport: TQExport4Xlsx): Boolean;

implementation

uses
  QExport4MSOfficeCommon;

{$R *.dfm}

function RunQExportXlsxEditor(AExport: TQExport4Xlsx): Boolean;
begin
  if not Assigned(AExport) then
    raise Exception.Create('Export is not assigned.');

  with TfmQExport4XlsxEditor.Create(nil) do
  try
    ExportXlsx := AExport;
    Default;
    FillStripStyles;
    FillOptions;
    FillOther;

    Result := ShowModal = mrOk;
    if Result then
      ApplyChanges;
  finally
    Free;
  end;
end;

{ TfmQExport4XlsxEditor }

procedure TfmQExport4XlsxEditor.edFileNameChange(Sender: TObject);
begin
  FileName := edFileName.Text;
end;

procedure TfmQExport4XlsxEditor.edSheetNameChange(Sender: TObject);
begin
  SheetName := edSheetName.Text;
end;

procedure TfmQExport4XlsxEditor.btnSaveClick(Sender: TObject);
begin
  sdFileName.FileName := edFileName.Text;
  if sdFileName.Execute then
    FileName := sdFileName.FileName;
end;

procedure TfmQExport4XlsxEditor.lvOptionsSelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
begin
  if Item.Selected then
    SelectedOptions := TXlsxCellStyle(Item.Data);
end;

procedure TfmQExport4XlsxEditor.lvSStylesSelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
begin
  if Item.Selected then
    SelectedOptions := TXlsxCellStyle(Item.Data);
end;

procedure TfmQExport4XlsxEditor.pcStyleEditorChange(Sender: TObject);
begin
  case pcStyleEditor.ActivePage.Tag of
    1:  //tsOptions
      if Assigned(lvOptions.Selected) then
        SelectedOptions := TXlsxCellStyle(lvOptions.Selected.Data);
    2:  //tsStripStyles
      if Assigned(lvSStyles.Selected) then
        SelectedOptions := TXlsxCellStyle(lvSStyles.Selected.Data)
      else
        SelectedOptions := nil;
  end;
end;

procedure TfmQExport4XlsxEditor.pbFontColorPaint(Sender: TObject);
begin
  if Assigned(FSelectedOptions) then
    PaintStandardColors(pbFontColor, FSelectedOptions.Font.Color);
end;

procedure TfmQExport4XlsxEditor.btnFontColorClick(Sender: TObject);
begin
  if ColorDialog.Execute then
  begin
    if ColorDialog.Color = -1 then
      ColorDialog.Color := clBlack;
    FSelectedOptions.Font.Color := ColorDialog.Color;
    pbFontColor.Repaint;
  end;
end;

procedure TfmQExport4XlsxEditor.btnFontColorMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  IncLeftAndTop(pbFontColor);
end;

procedure TfmQExport4XlsxEditor.btnFontColorMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecLeftAndTop(pbFontColor);
end;

procedure TfmQExport4XlsxEditor.pbBackColorPaint(Sender: TObject);
begin
  if Assigned(FSelectedOptions) then
    PaintStandardColors(pbBackColor, FSelectedOptions.BackgroundColor);
end;

procedure TfmQExport4XlsxEditor.btnBackgroundColorClick(Sender: TObject);
begin
  if ColorDialog.Execute then
  begin
    if ColorDialog.Color = -1 then
      ColorDialog.Color := clBlack;
    FSelectedOptions.BackgroundColor := ColorDialog.Color;
    pbBackColor.Repaint;
  end;
end;

procedure TfmQExport4XlsxEditor.btnBackgroundColorMouseDown(
  Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer);
begin
  IncLeftAndTop(pbBackColor);
end;

procedure TfmQExport4XlsxEditor.btnBackgroundColorMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecLeftAndTop(pbBackColor);
end;

procedure TfmQExport4XlsxEditor.chkUseBackgroundClick(Sender: TObject);
begin
  btnBackgroundColor.Enabled := chkUseBackground.Checked;
  if Assigned(FSelectedOptions) then
    FSelectedOptions.UseBackground := chkUseBackground.Checked;
end;

procedure TfmQExport4XlsxEditor.chkUseBorderClick(Sender: TObject);
begin
  laBorderColor.Enabled := chkUseBorder.Checked;
  btnBorderColor.Enabled := chkUseBorder.Checked;
  laBorderStyle.Enabled := chkUseBorder.Checked;
  cbBorderStyle.Enabled := chkUseBorder.Checked;
  if Assigned(FSelectedOptions) then
    FSelectedOptions.UseBorder := chkUseBorder.Checked;
end;

procedure TfmQExport4XlsxEditor.FormShow(Sender: TObject);
begin
  if Assigned(lvOptions.Items[0]) then
  begin
    lvOptions.Items[0].Focused := True;
    lvOptions.Items[0].Selected := True;
  end;
end;

procedure TfmQExport4XlsxEditor.btnFontBoldClick(Sender: TObject);
begin
  if btnFontBold.Down then
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style + [fsBold]
  else
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style - [fsBold];
end;

procedure TfmQExport4XlsxEditor.btnFontItalicClick(Sender: TObject);
begin
  if btnFontItalic.Down then
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style + [fsItalic]
  else
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style - [fsItalic];
end;

procedure TfmQExport4XlsxEditor.btnUnderlineSingleClick(Sender: TObject);
begin
  if btnUnderlineSingle.Down then
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style + [fsUnderline]
  else
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style - [fsUnderline];
end;

procedure TfmQExport4XlsxEditor.rbExportAllClick(Sender: TObject);
begin
  edExportRecCount.Enabled := rbExportOnly.Checked;
  laExportRecCount_02.Enabled := rbExportOnly.Checked;
end;

procedure TfmQExport4XlsxEditor.FormCreate(Sender: TObject);
begin
  FHeaderStyle := TXlsxCellStyle.Create;
  FCaptionRowStyle := TXlsxCellStyle.Create;
  FDataStyle := TXlsxCellStyle.Create;
  FFooterStyle := TXlsxCellStyle.Create;
  FStripStylesList := TXlsxStyleList.Create(Self);
end;

procedure TfmQExport4XlsxEditor.FormDestroy(Sender: TObject);

  procedure ClearListViewData(ListView: TListView);
  var
    i: Integer;
  begin
    for i := 0 to ListView.Items.Count - 1 do
      if Assigned(ListView.Items[i]) then
        ListView.Items[i].Data := nil;
  end;

begin
  ClearListViewData(lvOptions);
  FHeaderStyle.Free;
  FCaptionRowStyle.Free;
  FDataStyle.Free;
  FFooterStyle.Free;

  ClearListViewData(lvSStyles);
  FStripStylesList.Free;
  
  if Assigned(FExportXlsx) then
    FExportXlsx := nil;
end;

procedure TfmQExport4XlsxEditor.cbXlsxFontChange(Sender: TObject);
begin
  FSelectedOptions.Font.Name := cbXlsxFont.Text;
end;

procedure TfmQExport4XlsxEditor.cbXlsxFontSizeChange(Sender: TObject);
begin
  FSelectedOptions.Font.Size := StrToIntDef(cbXlsxFontSize.Text, 11);
end;

procedure TfmQExport4XlsxEditor.SetFileName(const Value: WideString);
begin
  if FFileName <> Value then
  begin
    FFileName := Value;
    edFileName.Text := Value;
  end;
end;

procedure TfmQExport4XlsxEditor.lvSStylesChange(Sender: TObject;
  Item: TListItem; Change: TItemChange);
begin
  if Assigned(lvSStyles.ItemFocused) then
  begin
    buRemove.Enabled := (lvSStyles.ItemFocused.Index >= 0);
    buMoveUp.Enabled := (lvSStyles.ItemFocused.Index > 0);
    buMoveDown.Enabled := (lvSStyles.ItemFocused.Index < lvSStyles.Items.Count - 1)
                         and (lvSStyles.ItemFocused.Index >= 0);
  end else
  begin
    buRemove.Enabled := (lvSStyles.Items.Count > 0);
    buMoveUp.Enabled := False;
    buMoveDown.Enabled := False;
  end;
end;

procedure TfmQExport4XlsxEditor.buAddClick(Sender: TObject);
var
  Num: Integer;
begin
  FStripStylesList.Add;
  lvSStyles.Items.BeginUpdate;
  try
    Num := lvSStyles.Items.Count;
    with lvSStyles.Items.Add do
    begin
      Caption := 'Strip Style ' + IntToStr(Num + 1);
      ImageIndex := 1;
      Data := Pointer(FStripStylesList[FStripStylesList.Count - 1].Options);
    end;
  finally
    lvSStyles.Items.EndUpdate;
  end;
end;

procedure TfmQExport4XlsxEditor.buRemoveClick(Sender: TObject);
begin
  if Assigned(lvSStyles.ItemFocused) then
  begin
    FStripStylesList.Items[lvSStyles.ItemFocused.Index].Free;
    FSelectedOptions := nil;
    lvSStyles.Items.BeginUpdate;
    try
      lvSStyles.Selected.Delete;
      ForAllListViewItems(lvSStyles, SetFocusedToSelected, False, True);
      ForAllListViewItems(lvSStyles, SetStripStylesNumber, False, True);
    finally
      lvSStyles.Items.EndUpdate;
    end;
  end;
end;

procedure TfmQExport4XlsxEditor.buMoveUpClick(Sender: TObject);
var
  TempStyle: TXlsxCellStyle;
begin
  TempStyle := TXlsxCellStyle.Create;
  try
    TempStyle.Assign(TXlsxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index].Data));
    TXlsxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index].Data).Assign(TXlsxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index - 1].Data));
    TXlsxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index - 1].Data).Assign(TempStyle);
  finally
    TempStyle.Free;
  end;
  lvSStyles.Items[lvSStyles.ItemFocused.Index - 1].Selected := True;
  lvSStyles.Items[lvSStyles.ItemFocused.Index - 1].Focused := True;
end;

procedure TfmQExport4XlsxEditor.buMoveDownClick(Sender: TObject);
var
  TempStyle: TXlsxCellStyle;
begin
  TempStyle := TXlsxCellStyle.Create;
  try
    TempStyle.Assign(TXlsxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index].Data));
    TXlsxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index].Data).Assign(TXlsxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index + 1].Data));
    TXlsxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index + 1].Data).Assign(TempStyle);
  finally
    TempStyle.Free;
  end;
  lvSStyles.Items[lvSStyles.ItemFocused.Index + 1].Selected := True;
  lvSStyles.Items[lvSStyles.ItemFocused.Index + 1].Focused := True;
end;

procedure TfmQExport4XlsxEditor.btnHorizontalLeftClick(Sender: TObject);
begin
  FSelectedOptions.Alignment := caLeft;
end;

procedure TfmQExport4XlsxEditor.btnHorizontalCenterClick(Sender: TObject);
begin
  FSelectedOptions.Alignment := caCenter;
end;

procedure TfmQExport4XlsxEditor.btnHorizontalRightClick(Sender: TObject);
begin
  FSelectedOptions.Alignment := caRight;
end;

procedure TfmQExport4XlsxEditor.btnVerticalTopClick(Sender: TObject);
begin
  FSelectedOptions.VerticalAligment := cvaTop;
end;

procedure TfmQExport4XlsxEditor.btnVerticalCenterClick(Sender: TObject);
begin
  FSelectedOptions.VerticalAligment := cvaMiddle;
end;

procedure TfmQExport4XlsxEditor.btnVerticalBottomClick(Sender: TObject);
begin
  FSelectedOptions.VerticalAligment := cvaBottom;
end;

procedure TfmQExport4XlsxEditor.btnWrapTextClick(Sender: TObject);
begin
  FSelectedOptions.WrapText := btnWrapText.Down;
end;

procedure TfmQExport4XlsxEditor.btnBorderColorClick(Sender: TObject);
begin
  if ColorDialog.Execute then
  begin
    if ColorDialog.Color = -1 then
      ColorDialog.Color := clBlack;
    FSelectedOptions.Border.Color := ColorDialog.Color;
    pbBorder.Repaint;
  end;
end;

procedure TfmQExport4XlsxEditor.btnBorderColorMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  IncLeftAndTop(pbBorder);
end;

procedure TfmQExport4XlsxEditor.btnBorderColorMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecLeftAndTop(pbBorder);
end;

procedure TfmQExport4XlsxEditor.pbBorderPaint(Sender: TObject);
begin
  if Assigned(FSelectedOptions) then
    PaintStandardColors(pbBorder, FSelectedOptions.Border.Color);
end;

procedure TfmQExport4XlsxEditor.cbBorderStyleChange(Sender: TObject);
begin
  FSelectedOptions.Border.Style := TXlsxBorderStyle(cbBorderStyle.ItemIndex);
end;

//******************************************************************************

procedure TfmQExport4XlsxEditor.SetSheetName(const Value: WideString);
begin
  if Value <> '' then
  begin
    FSheetName := Value;
    edSheetName.Text := Value;
  end;
end;

procedure TfmQExport4XlsxEditor.SetExportXlsx(const Value: TQExport4Xlsx);
begin
  FExportXlsx := Value;
  FHeaderStyle.Assign(FExportXlsx.XlsxOptions.HeaderStyle);
  FCaptionRowStyle.Assign(FExportXlsx.XlsxOptions.CaptionRowStyle);
  FDataStyle.Assign(FExportXlsx.XlsxOptions.DataStyle);
  FFooterStyle.Assign(FExportXlsx.XlsxOptions.FooterStyle);
  FStripStylesList.Assign(FExportXlsx.XlsxOptions.StripStylesList);
end;

procedure TfmQExport4XlsxEditor.SetSelectedOptions(
  const Value: TXlsxCellStyle);
begin
  FSelectedOptions := Value;
  SetFontAndBorder(FSelectedOptions);
  EnableFontAndBorder := Assigned(Value);
end;

procedure TfmQExport4XlsxEditor.SetFontAndBorder(Source: TXlsxCellStyle);
begin
  if Assigned(Source) then
    with Source do
    begin
      cbXlsxFont.ItemIndex := cbXlsxFont.Items.IndexOf(Font.Name);
      cbXlsxFontSize.Text := IntToStr(Font.Size);
      btnFontBold.Down := fsBold in Font.Style;
      btnFontItalic.Down := fsItalic in Font.Style;
      btnUnderlineSingle.Down := fsUnderline in Font.Style;
      pbFontColor.Repaint;
      case Alignment of
        caLeft:
          btnHorizontalLeft.Down := True;
        caRight:
          btnHorizontalRight.Down := True;
        caCenter:
          btnHorizontalCenter.Down := True;
      end;
      case VerticalAligment of
        cvaTop:
          btnVerticalTop.Down := True;
        cvaMiddle:
          btnVerticalCenter.Down := True;
        cvaBottom:
          btnVerticalBottom.Down := True;
      end;
      btnWrapText.Down := WrapText;
      chkUseBackground.Checked := UseBackground;
      chkUseBackground.OnClick(chkUseBackground);
      pbBackColor.Repaint;
      chkUseBorder.Checked := UseBorder;
      chkUseBorder.OnClick(chkUseBorder);
      cbBorderStyle.ItemIndex := Integer(Border.Style);
      pbBorder.Repaint;
    end;
end;

procedure TfmQExport4XlsxEditor.ApplyChanges;
begin
  FExportXlsx.XlsxOptions.HeaderStyle.Assign(FHeaderStyle);
  FExportXlsx.XlsxOptions.CaptionRowStyle.Assign(FCaptionRowStyle);
  FExportXlsx.XlsxOptions.DataStyle.Assign(FDataStyle);
  FExportXlsx.XlsxOptions.FooterStyle.Assign(FFooterStyle);
  FExportXlsx.XlsxOptions.StripStylesList.Assign(FStripStylesList);
  FExportXlsx.FileName := FileName;
  FExportXlsx.ShowFile := chkShow.Checked;
  FExportXlsx.PrintFile := chkPrint.Checked;
  FExportXlsx.SheetName := SheetName;
  FExportXlsx.AllowCaptions := chkAllowCaptions.Checked;
  FExportXlsx.AutoCalcStrType := chkAutoCalcStrType.Checked;
  FExportXlsx.OnlyVisibleFields := chkVisibleFields.Checked;
  FExportXlsx.GoToFirstRecord := chkGoToFirsrRec.Checked;
  FExportXlsx.SkipRecCount := StrToIntDef(edSkipRecCount.Text, 0);
  FExportXlsx.CaptionRow := StrToIntDef(edCaptionRow.Text, -1);
  if rbExportOnly.Checked then
    FExportXlsx.ExportRecCount := StrToIntDef(edExportRecCount.Text, 0);
  FExportXlsx.Header.Text := meHeader.Text;
  FExportXlsx.Footer.Text := meFooter.Text;
  FExportXlsx.XlsxOptions.StripStyleType := TMSStripStyleType(rgStripStyle.ItemIndex);
  with FExportXlsx.Formats do
  begin
    IntegerFormat := edInteger.Text;
    FloatFormat := edFloat.Text;
    DateFormat := edDate.Text;
    TimeFormat := edTime.Text;
    DateTimeFormat := edDateTime.Text;
    CurrencyFormat := edCurrency.Text;
    BooleanTrue := edTrue.Text;
    BooleanFalse := edFalse.Text;
    NullString := edNull.Text;
  end;
end;

procedure TfmQExport4XlsxEditor.FillStripStyles;
var
  i: Integer;
begin
  lvSStyles.Items.BeginUpdate;
  try
    lvSStyles.OnSelectItem := nil;
    lvSStyles.Items.Clear;
    for i := 0 to FStripStylesList.Count - 1 do
      with lvSStyles.Items.Add do
      begin
        Caption := 'Strip Style ' + IntToStr(i + 1);
        ImageIndex := 1;
        Data := Pointer(FStripStylesList[i].Options);
      end;
    if Assigned(lvSStyles.Items[0]) then
    begin
      lvSStyles.Items[0].Focused := True;
      lvSStyles.Items[0].Selected := True;
    end;
  finally
    lvSStyles.OnSelectItem := lvSStylesSelectItem;
    lvSStyles.Items.EndUpdate;
  end;
end;

procedure TfmQExport4XlsxEditor.FillOptions;
const
  cOptions: array [0..3] of WideString = ('Header', 'Caption Row', 'Data', 'Footer');
var
  i: Integer;
begin
  lvOptions.Items.BeginUpdate;
  try
    lvOptions.OnSelectItem := nil;
    lvOptions.Items.Clear;
    for i := 0 to 3 do
      with lvOptions.Items.Add do
      begin
        Caption := cOptions[i];
        ImageIndex := 2;
        case i of
          0: Data := Pointer(FHeaderStyle);
          1: Data := Pointer(FCaptionRowStyle);
          2: Data := Pointer(FDataStyle);
          3: Data := Pointer(FFooterStyle);
        end;
      end;
  finally
    lvOptions.OnSelectItem := lvOptionsSelectItem;
    lvOptions.Items.EndUpdate;
  end;
end;

procedure TfmQExport4XlsxEditor.FillOther;
begin
  FileName := FExportXlsx.FileName;
  chkShow.Checked := FExportXlsx.ShowFile;
  chkPrint.Checked := FExportXlsx.PrintFile;
  SheetName := FExportXlsx.SheetName;
  chkAllowCaptions.Checked := FExportXlsx.AllowCaptions;
  chkAutoCalcStrType.Checked := FExportXlsx.AutoCalcStrType;
  chkVisibleFields.Checked := FExportXlsx.OnlyVisibleFields;
  chkGoToFirsrRec.Checked := FExportXlsx.GoToFirstRecord;
  edSkipRecCount.Text := IntToStr(FExportXlsx.SkipRecCount);
  edCaptionRow.Text := IntToStr(FExportXlsx.CaptionRow);
  if FExportXlsx.ExportRecCount > 0 then
  begin
    rbExportOnly.Checked := True;
    edExportRecCount.Text := IntToStr(FExportXlsx.ExportRecCount);
  end;
  meHeader.Text := FExportXlsx.Header.Text;
  meFooter.Text := FExportXlsx.Footer.Text;

  with FExportXlsx.Formats do
  begin
    edInteger.Text := IntegerFormat;
    edFloat.Text := FloatFormat;
    edDate.Text := DateFormat;
    edTime.Text := TimeFormat;
    edDateTime.Text := DateTimeFormat;
    edCurrency.Text := CurrencyFormat;
    edTrue.Text := BooleanTrue;
    edFalse.Text := BooleanFalse;
    edNull.Text := NullString;
  end;

  cbXlsxFont.Items.Assign(Screen.Fonts);
  rgStripStyle.ItemIndex := Integer(FExportXlsx.XlsxOptions.StripStyleType);
end;

procedure TfmQExport4XlsxEditor.Default;
begin
  pcStyleEditor.ActivePage := tsOptions;
  pcCommonCellStyle.ActivePage := tsFont;
  pcOptions.ActivePage := tsCommon;
  EnableFontAndBorder := False;
end;

procedure TfmQExport4XlsxEditor.SetEnableFontAndBorder(
  const Value: Boolean);
begin
  if FEnableFontAndBorder <> Value then
  begin
    FEnableFontAndBorder := Value;
    laXlsxFont.Enabled := Value;
    cbXlsxFont.Enabled := Value;
    laXlsxFontSize.Enabled := Value;
    cbXlsxFontSize.Enabled := Value;
    btnFontColor.Enabled := Value;
    btnFontBold.Enabled := Value;
    btnFontItalic.Enabled := Value;
    btnUnderlineSingle.Enabled := Value;
    btnHorizontalLeft.Enabled := Value;
    btnHorizontalCenter.Enabled := Value;
    btnHorizontalRight.Enabled := Value;
    btnVerticalTop.Enabled := Value;
    btnVerticalCenter.Enabled := Value;
    btnVerticalBottom.Enabled := Value;
    btnWrapText.Enabled := Value;

    chkUseBorder.Enabled := Value;
    chkUseBorder.OnClick(chkUseBorder);
    chkUseBackground.Enabled := Value;
    chkUseBackground.OnClick(chkUseBackground);
  end;
end;

procedure TfmQExport4XlsxEditor.SetFocusedToSelected(Item: TListItem);
begin
  if Item.Focused then Item.Selected := True;
end;

procedure TfmQExport4XlsxEditor.SetStripStylesNumber(Item: TListItem);
begin
  Item.Caption := 'Strip Style ' + IntToStr(Item.Index + 1);
end;

end.
