unit fuQExport4ODSEditor;

{$I QExport4VerCtrl.inc}

interface

uses
  {$IFDEF VCL16}
    Winapi.Windows,
    Winapi.Messages,
    System.SysUtils,
    System.Variants,
    System.Classes,
    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}
  QExport4BaseODFClass,
  QExport4ODS,
  QExport4,
  QExport4Common,
  QExport4StrIds;

type
  TfmQExport4ODSEditor = class(TForm)
    paButtons: TPanel;
    bOK: TButton;
    bCancel: TButton;
    paFile: TPanel;
    laFileName: TLabel;
    edFileName: TEdit;
    sbSave: TSpeedButton;
    cbShow: TCheckBox;
    cbPrint: TCheckBox;
    paSelector: TPanel;
    paOptions: TPanel;
    pcStyleEditor: TPageControl;
    paTableName: TPanel;
    laTableName: TLabel;
    edTableName: TEdit;
    tsOptions: TTabSheet;
    tsStripStyles: TTabSheet;
    ilButtons: TImageList;
    ilListView: TImageList;
    tbProcessing: TToolBar;
    bAdd: TToolButton;
    bRemove: TToolButton;
    bSeparator1: TToolButton;
    bMoveUp: TToolButton;
    bMoveDown: TToolButton;
    pcOptions: TPageControl;
    tsCommon: TTabSheet;
    tsHeader: TTabSheet;
    tsFooter: TTabSheet;
    tsFormats: TTabSheet;
    mmHeader: TMemo;
    mmFooter: TMemo;
    edInteger: TEdit;
    edDate: TEdit;
    edDateTime: TEdit;
    edTrue: TEdit;
    edFalse: TEdit;
    edNull: TEdit;
    edFloat: TEdit;
    edTime: TEdit;
    edCurrency: TEdit;
    laInteger: TLabel;
    laDate: TLabel;
    laDateTime: TLabel;
    laTrue: TLabel;
    laFalse: TLabel;
    laNull: TLabel;
    laFloat: TLabel;
    laTime: TLabel;
    laCurrency: TLabel;
    cbAllowCaptions: TCheckBox;
    cbAutoCalcStrType: TCheckBox;
    cbGoToFirsrRec: TCheckBox;
    cbVisibleFields: TCheckBox;
    bvl1: TBevel;
    bvl2: TBevel;
    laSkipRecCount_01: TLabel;
    edSkipRecCount: TEdit;
    laSkipRecCount_02: TLabel;
    edCaptionRow: TEdit;
    laCaptionRow: TLabel;
    rbExportAll: TRadioButton;
    rbExportOnly: TRadioButton;
    edExportRecCount: TEdit;
    laExportRecCount_02: TLabel;
    lvOptions: TListView;
    rgStripStyle: TRadioGroup;
    lvSStyles: TListView;
    paStylesEdit: TPanel;
    pcAllOptions: TPageControl;
    tsCommonCellStyle: TTabSheet;
    pcCommonCellStyle: TPageControl;
    tsFont: TTabSheet;
    tsBorder: TTabSheet;
    laODSFont: TLabel;
    cbODSFont: TComboBox;
    cbODSFontSize: TComboBox;
    laODSFontSize: TLabel;
    btnFontColor: TSpeedButton;
    pbFontColor: TPaintBox;
    Bevel5: TBevel;
    btnFontBold: TSpeedButton;
    btnFontItalic: TSpeedButton;
    btnFontStrikeOut: TSpeedButton;
    Bevel6: TBevel;
    btnUnderlineSingle: TSpeedButton;
    Bevel4: TBevel;
    btnHorizontalLeft: TSpeedButton;
    btnHorizontalCenter: TSpeedButton;
    btnHorizontalRight: TSpeedButton;
    btnHorizontalJustify: TSpeedButton;
    Bevel8: TBevel;
    Bevel3: TBevel;
    Bevel11: TBevel;
    btnVerticalTop: TSpeedButton;
    btnVerticalCenter: TSpeedButton;
    btnVerticalBottom: TSpeedButton;
    btnBorderTopColor: TSpeedButton;
    pbBorderTop: TPaintBox;
    cbUseBorder: TCheckBox;
    laBorderWidth: TLabel;
    edBorderWidth: TEdit;
    Bevel1: TBevel;
    ColorDialog: TColorDialog;
    sdFileName: TSaveDialog;
    pmHeaderFooter: TPopupMenu;
    miInsertTab: TMenuItem;
    miClear: TMenuItem;
    bbRestoreInitial: TBitBtn;
    bbResetDefault: TBitBtn;
    laBorderColor: TLabel;
    cbBackground: TCheckBox;
    sbBackgroundColor: TSpeedButton;
    pbBackColor: TPaintBox;
    paSampleText: TPanel;
    laSampleText: TLabel;
    pbSampleText: TPaintBox;
    bResetItem: TBitBtn;
    bResetAll: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure bbRestoreInitialClick(Sender: TObject);
    procedure bbResetDefaultClick(Sender: TObject);
    procedure miInsertTabClick(Sender: TObject);
    procedure miClearClick(Sender: TObject);
    procedure rbExportAllClick(Sender: TObject);
    procedure rbExportOnlyClick(Sender: TObject);
    procedure sbSaveClick(Sender: TObject);
    procedure bAddClick(Sender: TObject);
    procedure lvSStylesChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure bRemoveClick(Sender: TObject);
    procedure lvOptionsChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    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 pbFontColorPaint(Sender: TObject);
    procedure pcStyleEditorChange(Sender: TObject);
    procedure cbODSFontChange(Sender: TObject);
    procedure cbODSFontSizeChange(Sender: TObject);
    procedure cbODSFontSizeEnter(Sender: TObject);
    procedure cbODSFontSizeExit(Sender: TObject);
    procedure bMoveUpClick(Sender: TObject);
    procedure bMoveDownClick(Sender: TObject);
    procedure pbBorderTopPaint(Sender: TObject);
    procedure btnBorderTopColorMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure btnBorderTopColorMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure btnBorderTopColorClick(Sender: TObject);
    procedure cbUseBorderClick(Sender: TObject);
    procedure edBorderWidthChange(Sender: TObject);
    procedure edBorderWidthEnter(Sender: TObject);
    procedure edBorderWidthExit(Sender: TObject);
    procedure btnHorizontalLeftClick(Sender: TObject);
    procedure btnHorizontalCenterClick(Sender: TObject);
    procedure btnHorizontalRightClick(Sender: TObject);
    procedure btnHorizontalJustifyClick(Sender: TObject);
    procedure btnVerticalTopClick(Sender: TObject);
    procedure btnVerticalCenterClick(Sender: TObject);
    procedure btnVerticalBottomClick(Sender: TObject);
    procedure btnFontBoldClick(Sender: TObject);
    procedure btnFontItalicClick(Sender: TObject);
    procedure btnFontStrikeOutClick(Sender: TObject);
    procedure btnUnderlineSingleClick(Sender: TObject);
    procedure pbBackColorPaint(Sender: TObject);
    procedure sbBackgroundColorMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure sbBackgroundColorMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure sbBackgroundColorClick(Sender: TObject);
    procedure cbBackgroundClick(Sender: TObject);
    procedure NumberKeyPress(Sender: TObject; var Key: Char);
    procedure pbSampleTextPaint(Sender: TObject);
    procedure bResetItemClick(Sender: TObject);
    procedure bResetAllClick(Sender: TObject);
  private
    FExportODS: TQExport4ODS;
    FHrStyle: TODSCellParagraphStyle;
    FFrStyle: TODSCellParagraphStyle;
    FCRStyle: TODSCellParagraphStyle;
    FDStyle: TODSCellParagraphStyle;
    FCurrStyle: TODSCellParagraphStyle;
    FCurrFormats: TQExportFormats;
    FNeedResetFormats: Boolean;
    FSStylesList: TODSStylesList;

    function CurrentSheetMemo: TMemo;

    procedure AddMemos;
    procedure BlockUnBlock(Condition: Boolean);
    procedure AddListItem(Dest: TListView; ItemName: string; ImageInd: Integer);
    procedure ApplyChanges;
    procedure ClearMemos;
    procedure EnableSkipRecGroup(Value: Integer);
    procedure FillCommonFields;
    procedure FillStripType;
    procedure FillStripStyles;
    procedure FillFormats;
    procedure FormOptions;
    procedure GetCurrentStyle;
    procedure SetCurrentStatus;
    procedure ReplaceHeaderFooter;
    procedure SetSelectedToFocused(Item: TListItem);
    procedure StyleRenumber(Item: TListItem);
    procedure TuneExportGroup;
    procedure TuneSStyleButtons;
    procedure TuneOptionsList;
    procedure TuneBorderControls(Condition: Boolean);
    procedure TuneBackgroundControls(Condition: Boolean);
  public
    constructor Create(AOwner: TComponent); override;

    property ExportField: TQExport4ODS read FExportODS write FExportODS;
    property SStylesList: TODSStylesList read FSStylesList write FSStylesList;
    property NeedResetFormats: Boolean read FNeedResetFormats;
    property HrStyle: TODSCellParagraphStyle read FHrStyle write FHrStyle;
    property FrStyle: TODSCellParagraphStyle read FFrStyle write FFrStyle;
    property CRStyle: TODSCellParagraphStyle read FCRStyle write FCRStyle;
    property DStyle: TODSCellParagraphStyle read FDStyle write FDStyle;
  end;

function RunQExportODSEditor(ExportODS: TQExport4ODS): boolean;

implementation

{$R *.dfm}

function RunQExportODSEditor(ExportODS: TQExport4ODS): boolean;
begin

  if not Assigned(ExportODS) then
    raise Exception.Create('The QExportODS parameter is not assigned!');

  with TfmQExport4ODSEditor.Create(nil) do
  try
    FExportODS := ExportODS;
    FHrStyle := TODSCellParagraphStyle.Create(nil);
    FFrStyle := TODSCellParagraphStyle.Create(nil);
    FCRStyle := TODSCellParagraphStyle.Create(nil);
    FDStyle := TODSCellParagraphStyle.Create(nil);
    FHrStyle.Assign(FExportODS.ODSOptions.HeaderStyle);
    FFrStyle.Assign(FExportODS.ODSOptions.FooterStyle);
    FCRStyle.Assign(FExportODS.ODSOptions.CaptionRowStyle);
    FDStyle.Assign(FExportODS.ODSOptions.DataStyle);
    FNeedResetFormats := false;
    FSStylesList := TODSStylesList.Create(nil);
    FSStylesList.Assign(ExportField.ODSOptions.StripStylesList);

    pcStyleEditor.ActivePage := tsOptions;

    ClearMemos;
    AddMemos;
    FillCommonFields;
    FillFormats;
    FillStripType;
    FillStripStyles;
    EnableSkipRecGroup(ExportODS.ExportRecCount);
    TuneSStyleButtons;
    GetCurrentStyle;    

    Result := ShowModal = mrOk;
    if Result then
      ApplyChanges;
  finally
    FHrStyle.Free;
    FFrStyle.Free;
    FCRStyle.Free;
    FDStyle.Free;
    FSStylesList.Free;
    Free;
  end;
end;

{ TfmQExport4ODSEditor }

procedure TfmQExport4ODSEditor.ApplyChanges;
begin
  //Re-assign modified styles
  with ExportField.ODSOptions do
  begin
    HeaderStyle.Assign(FHrStyle);
    FooterStyle.Assign(FFrStyle);
    CaptionRowStyle.Assign(FCRStyle);
    DataStyle.Assign(FDStyle);
    StripStylesList.Assign(SStylesList);
  end;
  //Replace old headers and footers
  ReplaceHeaderFooter;
  //Commit changes for Allow Caption property
  ExportField.AllowCaptions := cbAllowCaptions.Checked;
  //Commit changes for Auto Calculate String Type property
  ExportField.AutoCalcStrType := cbAutoCalcStrType.Checked;
  //Commit changes for Only Visible Fields Type property
  ExportField.OnlyVisibleFields := cbVisibleFields.Checked;
  //Commit changes for Go to first record property
  ExportField.GoToFirstRecord := cbGoToFirsrRec.Checked;
  //Commit changes for Table Name property
  if ExportField.SheetName <> edTableName.Text then
    ExportField.SheetName := edTableName.Text;
  //Commit changes for File Name property
  if (edFileName.Text <> ExportField.FileName) then
    ExportField.FileName := edFileName.Text;
  //Commit changes for Skip Record Count property
  if (StrToIntDef(edSkipRecCount.Text, 0) <> ExportField.SkipRecCount) then
    ExportField.SkipRecCount := StrToIntDef(edSkipRecCount.Text, 0);
  //Commit changes for Caption Row property
  if (StrToIntDef(edCaptionRow.Text, -1) <> ExportField.CaptionRow) then
    ExportField.CaptionRow := StrToIntDef(edCaptionRow.Text, -1);
  //Commit changes for Show File property
  ExportField.ShowFile := cbShow.Checked;
  //Commit changes for Print File property
  ExportField.PrintFile := cbPrint.Checked;
  //Fill Skip Rec Count
  if ExportField.ExportRecCount <> StrToIntDef(edExportRecCount.Text, 0) then
    ExportField.ExportRecCount := StrToIntDef(edExportRecCount.Text, 0);
  //Fill Current Style
  case rgStripStyle.ItemIndex of
    0: ExportField.ODSOptions.StripStyle := sstNone;
    1: ExportField.ODSOptions.StripStyle := sstColumn;
    2: ExportField.ODSOptions.StripStyle := sstRow;
  end;
  //Commit current formats
  with ExportField.Formats do
  begin
    if IntegerFormat <> edInteger.Text then
      IntegerFormat := edInteger.Text;
    if FloatFormat <> edFloat.Text then
      FloatFormat := edFloat.Text;
    if DateFormat <> edDate.Text then
      DateFormat := edDate.Text;
    if TimeFormat <> edTime.Text then
      TimeFormat := edTime.Text;
    if DateTimeFormat <> edDateTime.Text then
      DateTimeFormat := edDateTime.Text;
    if CurrencyFormat <> edCurrency.Text then
      CurrencyFormat := edCurrency.Text;
    if BooleanTrue <> edTrue.Text then
      BooleanTrue := edTrue.Text;
    if BooleanFalse <> edFalse.Text then
      BooleanFalse := edFalse.Text;
    if NullString <> edNull.Text then
      NullString := edNull.Text;
  end;
end;

procedure TfmQExport4ODSEditor.FillCommonFields;
begin
  edCaptionRow.Text := IntToStr(ExportField.CaptionRow);
  edSkipRecCount.Text := IntToStr(ExportField.SkipRecCount);
  edFileName.Text := ExportField.FileName;
  edTableName.Text := ExportField.SheetName;
  cbAllowCaptions.Checked := ExportField.AllowCaptions;
  cbAutoCalcStrType.Checked := ExportField.AutoCalcStrType;
  cbVisibleFields.Checked := ExportField.OnlyVisibleFields;
  cbGoToFirsrRec.Checked := ExportField.GoToFirstRecord;
  cbShow.Checked := ExportField.ShowFile;
  cbPrint.Checked := ExportField.PrintFile;
end;

procedure TfmQExport4ODSEditor.FillFormats;
begin
  if FNeedResetFormats then
  begin
    try
      FCurrFormats := TQExportFormats.Create;
      with FCurrFormats 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;
    finally
      FCurrFormats.Free;
    end;
  end
  else
    with ExportField.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;
  FNeedResetFormats := false;
end;

procedure TfmQExport4ODSEditor.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to pcAllOptions.PageCount - 1 do
    pcAllOptions.Pages[i].Parent := paStylesEdit;
end;

procedure TfmQExport4ODSEditor.FormDestroy(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to pcAllOptions.PageCount - 1 do
    pcAllOptions.Pages[i].Parent := pcAllOptions;
end;

procedure TfmQExport4ODSEditor.bbRestoreInitialClick(Sender: TObject);
begin
  FNeedResetFormats := false;
  FillFormats;
end;

procedure TfmQExport4ODSEditor.bbResetDefaultClick(Sender: TObject);
begin
  FNeedResetFormats := true;
  FillFormats;
end;

function TfmQExport4ODSEditor.CurrentSheetMemo: TMemo;
begin
  if pcOptions.ActivePage = tsHeader then Result := mmHeader
  else if pcOptions.ActivePage = tsFooter then Result := mmFooter
  else Result := nil;
end;

procedure TfmQExport4ODSEditor.ClearMemos;
begin
  mmHeader.Lines.Clear;
  mmFooter.Lines.Clear;
end;

procedure TfmQExport4ODSEditor.AddMemos;
var
  i: Integer;
begin
  for i := 0 to ExportField.Header.Count - 1 do
    mmHeader.Lines.Add(ExportField.Header[i]);
  for i := 0 to ExportField.Footer.Count - 1 do
    mmFooter.Lines.Add(ExportField.Footer[i]);
end;

procedure TfmQExport4ODSEditor.miInsertTabClick(Sender: TObject);
var
  MM: TMemo;
  Point: TPoint;
  Str: string;
begin
  MM := CurrentSheetMemo;
  if Assigned(MM) then begin
    Point := GetMemoCaretPos(MM);
    Str := MM.Lines[Point.Y];
    Insert(Chr(9), Str, Point.X + 1);
    MM.Lines[Point.Y] := Str;
    MM.Perform(EM_SETSEL, Point.X + 1, Point.X + 1);
  end;
end;

procedure TfmQExport4ODSEditor.miClearClick(Sender: TObject);
var
  MM: TMemo;
begin
  MM := CurrentSheetMemo;
  if Assigned(MM) then MM.Lines.Clear;
end;

procedure TfmQExport4ODSEditor.rbExportAllClick(Sender: TObject);
begin
  TuneExportGroup;
  edExportRecCount.Text := '0';
end;

procedure TfmQExport4ODSEditor.TuneExportGroup;
begin
  edExportRecCount.Enabled := rbExportOnly.Checked;
  laExportRecCount_02.Enabled := rbExportOnly.Checked;
end;

procedure TfmQExport4ODSEditor.rbExportOnlyClick(Sender: TObject);
begin
  TuneExportGroup;
end;

procedure TfmQExport4ODSEditor.EnableSkipRecGroup(Value: Integer);
begin
  if (Value <> 0) then
  begin
    rbExportOnly.Checked := true;
    TuneExportGroup;
    edExportRecCount.Text := IntToStr(Value);
  end;
end;

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

constructor TfmQExport4ODSEditor.Create(AOwner: TComponent);
begin
  inherited;
  cbODSFont.Items.Assign(Screen.Fonts);
  FormOptions;
end;

procedure TfmQExport4ODSEditor.FillStripType;
begin
  rgStripStyle.ItemIndex := Integer(ExportField.ODSOptions.StripStyle);
end;

procedure TfmQExport4ODSEditor.ReplaceHeaderFooter;
var
  I: Integer;
begin
  ExportField.Header.Clear;
  for I := 0 to mmHeader.Lines.Count - 1 do
    ExportField.Header.Add(mmHeader.Lines[I]);
  ExportField.Footer.Clear;
  for I := 0 to mmFooter.Lines.Count - 1 do
    ExportField.Footer.Add(mmFooter.Lines[I]);
end;

procedure TfmQExport4ODSEditor.TuneSStyleButtons;
begin
  if Assigned(lvSStyles.ItemFocused) then
  begin
    bRemove.Enabled := (lvSStyles.ItemFocused.Index >= 0);
    bMoveUp.Enabled := (lvSStyles.ItemFocused.Index > 0);
    bMoveDown.Enabled := (lvSStyles.ItemFocused.Index < lvSStyles.Items.Count - 1)
                         and (lvSStyles.ItemFocused.Index >= 0);
  end else
  begin
    bRemove.Enabled := (lvSStyles.Items.Count > 0);
    bMoveUp.Enabled := False;
    bMoveDown.Enabled := False;
  end;
end;

procedure TfmQExport4ODSEditor.bAddClick(Sender: TObject);
var
  Num: Integer;
begin
  SStylesList.Add;
  lvSStyles.Items.BeginUpdate;
  try
    Num := lvSStyles.Items.Count;
    with lvSStyles.Items.Add do
    begin
      Caption := QExportLoadStr(QED_ODS_StripStyle) + IntToStr(Num + 1);
      ImageIndex := 1;
    end;
    lvSStyles.Items[lvSStyles.Items.Count - 1].Focused := true;
    lvSStyles.Items[lvSStyles.Items.Count - 1].Selected := true;
  finally
    lvSStyles.Items.EndUpdate;
  end;
end;

procedure TfmQExport4ODSEditor.lvSStylesChange(Sender: TObject;
  Item: TListItem; Change: TItemChange);
begin
  TuneSStyleButtons;
  GetCurrentStyle;
end;

procedure TfmQExport4ODSEditor.StyleRenumber(Item: TListItem);
begin
  Item.Caption := QExportLoadStr(QED_ODS_StripStyle) + IntToStr(Item.Index + 1);
end;

procedure TfmQExport4ODSEditor.bRemoveClick(Sender: TObject);
begin
  if Assigned(lvSStyles.ItemFocused) then
  begin
    SStylesList.Items[lvSStyles.ItemFocused.Index].Free;
    lvSStyles.Items.BeginUpdate;
    try
      lvSStyles.Selected.Delete;
      ForAllListViewItems(lvSStyles, SetSelectedToFocused, false, true);
      ForAllListViewItems(lvSStyles, StyleRenumber, false, true);
    finally
      lvSStyles.Items.EndUpdate;
    end;
    TuneSStyleButtons;
    GetCurrentStyle;
  end;
end;

procedure TfmQExport4ODSEditor.SetSelectedToFocused(Item: TListItem);
begin
  if Item.Focused then
    Item.Selected := true;
end;

procedure TfmQExport4ODSEditor.FormOptions;
begin
  lvOptions.Items.BeginUpdate;
  try
    lvOptions.Items.Clear;
    AddListItem(lvOptions, QExportLoadStr(QED_ODS_Header), 2);
    AddListItem(lvOptions, QExportLoadStr(QED_ODS_CaptionRow), 2);
    AddListItem(lvOptions, QExportLoadStr(QED_ODS_Data), 2);
    AddListItem(lvOptions, QExportLoadStr(QED_ODS_Footer), 2);
    lvOptions.Items[0].Focused := true;
    lvOptions.Items[0].Selected := true;
  finally
    lvOptions.Items.EndUpdate;
  end;
end;

procedure TfmQExport4ODSEditor.AddListItem(Dest: TListView;
  ItemName: string; ImageInd: Integer);
begin
  with Dest.Items.Add do
  begin
    Caption := ItemName;
    ImageIndex := ImageInd;
  end;
end;

procedure TfmQExport4ODSEditor.FillStripStyles;
var
  I: Integer;
begin
  lvSStyles.Items.Clear;
  lvSStyles.Items.BeginUpdate;
  try
    for I := 0 to SStylesList.Count - 1 do
      AddListItem(lvSStyles, QExportLoadStr(QED_ODS_StripStyle) + IntToStr(I + 1), 1);
    if assigned(lvSStyles.Items[0]) then
    begin
      lvSStyles.Items[0].Focused := true;
      lvSStyles.Items[0].Selected := true;
    end;
  finally
    lvSStyles.Items.EndUpdate;
  end;
end;

procedure TfmQExport4ODSEditor.GetCurrentStyle;
begin
  FCurrStyle := nil;
  if pcStyleEditor.ActivePage = tsOptions then
  begin
    tsBorder.TabVisible := true;
    if (not Assigned(lvOptions.ItemFocused)) or
        (not Assigned(lvOptions.Items[lvOptions.ItemFocused.Index])) then
    begin
      SetCurrentStatus;
      Exit;
    end;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      QExportLoadStr(QED_ODS_Header) then
      FCurrStyle := HrStyle;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      QExportLoadStr(QED_ODS_Footer) then
      FCurrStyle := FrStyle;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      QExportLoadStr(QED_ODS_CaptionRow) then
      FCurrStyle := CRStyle;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      QExportLoadStr(QED_ODS_Data) then
      FCurrStyle := DStyle;
  end;
  if pcStyleEditor.ActivePage = tsStripStyles then
  begin
    tsBorder.TabVisible := false;
    if (not Assigned(lvSStyles.ItemFocused)) or
        (not Assigned(lvSStyles.Items[lvSStyles.ItemFocused.Index])) then
    begin
      SetCurrentStatus;
      Exit;
    end;
    if lvSStyles.ItemFocused.Index < SStylesList.Count then
      FCurrStyle := SStylesList[lvSStyles.ItemFocused.Index];
  end;
  SetCurrentStatus;
end;

procedure TfmQExport4ODSEditor.SetCurrentStatus;
begin
  if assigned(FCurrStyle) then
  begin
    BlockUnBlock(true);
    TuneOptionsList;
  end
  else
  begin
    BlockUnBlock(false);
    pbSampleText.Repaint;
  end;
end;

procedure TfmQExport4ODSEditor.lvOptionsChange(Sender: TObject;
  Item: TListItem; Change: TItemChange);
begin
  GetCurrentStyle;
end;

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

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

procedure TfmQExport4ODSEditor.btnFontColorClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if ColorDialog.Execute then
    if ColorDialog.Color <> -1 then
        FCurrStyle.Font.Color := ColorDialog.Color
      else
        FCurrStyle.Font.Color := clBlack;
  pbFontColor.Repaint;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODSEditor.pbFontColorPaint(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  PaintStandardColors(pbFontColor, FCurrStyle.Font.Color);
end;

procedure TfmQExport4ODSEditor.pcStyleEditorChange(Sender: TObject);
begin
  GetCurrentStyle;
end;

procedure TfmQExport4ODSEditor.BlockUnBlock(Condition: Boolean);
begin
  tsCommonCellStyle.Enabled := Condition;
  laODSFont.Enabled := Condition;
  cbODSFont.Enabled := Condition;
  laODSFontSize.Enabled := Condition;
  cbODSFontSize.Enabled := Condition;
  btnFontColor.Enabled := Condition;
  pbFontColor.Enabled := Condition;
  btnFontBold.Enabled := Condition;
  btnFontItalic.Enabled := Condition;
  btnFontStrikeOut.Enabled := Condition;
  btnUnderlineSingle.Enabled := Condition;
  btnHorizontalLeft.Enabled := Condition;
  btnHorizontalCenter.Enabled := Condition;
  btnHorizontalRight.Enabled := Condition;
  btnHorizontalJustify.Enabled := Condition;
  btnVerticalTop.Enabled := Condition;
  btnVerticalCenter.Enabled := Condition;
  btnVerticalBottom.Enabled := Condition;
  cbUseBorder.Enabled := Condition;
  laBorderColor.Enabled := Condition;
  btnBorderTopColor.Enabled := Condition;
  pbBorderTop.Enabled := Condition;
  laBorderWidth.Enabled := Condition;
  edBorderWidth.Enabled := Condition;
  cbBackground.Enabled := Condition;
  pbBackColor.Enabled := Condition;
  sbBackgroundColor.Enabled := Condition;
end;

procedure TfmQExport4ODSEditor.cbODSFontChange(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  FCurrStyle.Font.Name := cbODSFont.Text;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODSEditor.cbODSFontSizeChange(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  FCurrStyle.Font.Size := StrToIntDef(cbODSFontSize.Text, 10);
  pbSampleText.Repaint;  
end;

procedure TfmQExport4ODSEditor.cbODSFontSizeEnter(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  cbODSFontSize.Text := IntToStr(FCurrStyle.Font.Size);
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODSEditor.cbODSFontSizeExit(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  cbODSFontSize.Text := IntToStr(FCurrStyle.Font.Size);
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODSEditor.bMoveUpClick(Sender: TObject);
var
  TempStyle: TODSCellParagraphStyle;
begin
  TempStyle := TODSCellParagraphStyle.Create(nil);
  try
    TempStyle.Assign(SStylesList.Items[lvSStyles.ItemFocused.Index]);
    SStylesList.Items[lvSStyles.ItemFocused.Index].Assign(SStylesList.Items[lvSStyles.ItemFocused.Index - 1]);
    SStylesList.Items[lvSStyles.ItemFocused.Index - 1].Assign(TempStyle);
  finally
    TempStyle.Free;
  end;
  lvSStyles.Items[lvSStyles.ItemFocused.Index - 1].Selected := true;
  lvSStyles.Items[lvSStyles.ItemFocused.Index - 1].Focused := true;
end;

procedure TfmQExport4ODSEditor.bMoveDownClick(Sender: TObject);
var
  TempStyle: TODSCellParagraphStyle;
begin
  TempStyle := TODSCellParagraphStyle.Create(nil);
  try
    TempStyle.Assign(SStylesList.Items[lvSStyles.ItemFocused.Index]);
    SStylesList.Items[lvSStyles.ItemFocused.Index].Assign(SStylesList.Items[lvSStyles.ItemFocused.Index + 1]);
    SStylesList.Items[lvSStyles.ItemFocused.Index + 1].Assign(TempStyle);
  finally
    TempStyle.Free;
  end;
  lvSStyles.Items[lvSStyles.ItemFocused.Index + 1].Selected := true;
  lvSStyles.Items[lvSStyles.ItemFocused.Index + 1].Focused := true;
end;

procedure TfmQExport4ODSEditor.pbBorderTopPaint(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if FCurrStyle.Border.BorderStyle = bsODFNone then Exit;
  PaintStandardColors(pbBorderTop, FCurrStyle.Border.BorderColor);
end;

procedure TfmQExport4ODSEditor.TuneOptionsList;
begin
  if not (csDestroying in ComponentState) then
  begin
    if not Assigned(FCurrStyle) then Exit;
    cbODSFont.ItemIndex := cbODSFont.Items.IndexOf(FCurrStyle.Font.Name);
    cbODSFontSize.Text := IntToStr(FCurrStyle.Font.Size);
    TuneBorderControls(FCurrStyle.Border.BorderStyle = bsODFSolid);
    TuneBackgroundControls(FCurrStyle.AllowBackground);
    btnVerticalTop.Down := FCurrStyle.VerticalAligment = taODFTop;
    btnVerticalCenter.Down := FCurrStyle.VerticalAligment = taODFMiddle;
    btnVerticalBottom.Down := FCurrStyle.VerticalAligment = taODFBottom;
    btnHorizontalLeft.Down := FCurrStyle.Alignment = taODFLeft;
    btnHorizontalRight.Down := FCurrStyle.Alignment = taODFRight;
    btnHorizontalCenter.Down := FCurrStyle.Alignment = taODFCenter;
    btnHorizontalJustify.Down := FCurrStyle.Alignment = taODFJustify;
    btnFontBold.Down := fsBold in FCurrStyle.Font.Style;
    btnFontItalic.Down := fsItalic in FCurrStyle.Font.Style;
    btnUnderlineSingle.Down := fsUnderline in FCurrStyle.Font.Style;
    btnFontStrikeOut.Down := fsStrikeOut in FCurrStyle.Font.Style;
    pbFontColor.Repaint;
    pbSampleText.Repaint;
  end;
end;

procedure TfmQExport4ODSEditor.btnBorderTopColorMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  IncLeftAndTop(pbBorderTop);
end;

procedure TfmQExport4ODSEditor.btnBorderTopColorMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecLeftAndTop(pbBorderTop);
end;

procedure TfmQExport4ODSEditor.btnBorderTopColorClick(Sender: TObject);
begin
  if ColorDialog.Execute then
    if ColorDialog.Color <> -1 then
        FCurrStyle.Border.BorderColor := ColorDialog.Color
      else
        FCurrStyle.Border.BorderColor := clBlack;
  pbBorderTop.Repaint;
end;

procedure TfmQExport4ODSEditor.cbUseBorderClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if cbUseBorder.Checked then
    FCurrStyle.Border.BorderStyle := bsODFSolid
  else
    FCurrStyle.Border.BorderStyle := bsODFNone;
  TuneBorderControls(cbUseBorder.Checked);
end;

procedure TfmQExport4ODSEditor.TuneBorderControls(Condition: Boolean);
begin
  laBorderColor.Enabled := Condition;
  pbBorderTop.Enabled := Condition;
  btnBorderTopColor.Enabled := Condition;
  laBorderWidth.Enabled := Condition;
  edBorderWidth.Enabled := Condition;
  cbUseBorder.Checked := Condition;
  if Condition then
  begin
    pbBorderTop.Repaint;
    edBorderWidth.Text := IntToStr(FCurrStyle.Border.BorderWidth);
  end;
end;

procedure TfmQExport4ODSEditor.edBorderWidthChange(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if StrToIntDef(edBorderWidth.Text, 1) <> FCurrStyle.Border.BorderWidth then
    FCurrStyle.Border.BorderWidth := StrToIntDef(edBorderWidth.Text, 1);
end;

procedure TfmQExport4ODSEditor.edBorderWidthEnter(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if StrToIntDef(edBorderWidth.Text, 1) <> FCurrStyle.Border.BorderWidth then
    edBorderWidth.Text := IntToStr(FCurrStyle.Border.BorderWidth);
end;

procedure TfmQExport4ODSEditor.edBorderWidthExit(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if StrToIntDef(edBorderWidth.Text, 1) <> FCurrStyle.Border.BorderWidth then
    edBorderWidth.Text := IntToStr(FCurrStyle.Border.BorderWidth);
end;

procedure TfmQExport4ODSEditor.btnHorizontalLeftClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if btnHorizontalLeft.Down then
    FCurrStyle.Alignment := taODFLeft;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODSEditor.btnHorizontalCenterClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if btnHorizontalCenter.Down then
    FCurrStyle.Alignment := taODFCenter;
  pbSampleText.Repaint;    
end;

procedure TfmQExport4ODSEditor.btnHorizontalRightClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if btnHorizontalRight.Down then
    FCurrStyle.Alignment := taODFRight;
  pbSampleText.Repaint;    
end;

procedure TfmQExport4ODSEditor.btnHorizontalJustifyClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if btnHorizontalJustify.Down then
    FCurrStyle.Alignment := taODFJustify;
  pbSampleText.Repaint;    
end;

procedure TfmQExport4ODSEditor.btnVerticalTopClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if btnVerticalTop.Down then
    FCurrStyle.VerticalAligment := taODFTop;
  pbSampleText.Repaint;    
end;

procedure TfmQExport4ODSEditor.btnVerticalCenterClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if btnVerticalCenter.Down then
    FCurrStyle.VerticalAligment := taODFMiddle;
  pbSampleText.Repaint;    
end;

procedure TfmQExport4ODSEditor.btnVerticalBottomClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if btnVerticalBottom.Down then
    FCurrStyle.VerticalAligment := taODFBottom;
  pbSampleText.Repaint;    
end;

procedure TfmQExport4ODSEditor.btnFontBoldClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if btnFontBold.Down then
    FCurrStyle.Font.Style := FCurrStyle.Font.Style + [fsBold]
  else
    FCurrStyle.Font.Style := FCurrStyle.Font.Style - [fsBold];
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODSEditor.btnFontItalicClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if btnFontItalic.Down then
    FCurrStyle.Font.Style := FCurrStyle.Font.Style + [fsItalic]
  else
    FCurrStyle.Font.Style := FCurrStyle.Font.Style - [fsItalic];
  pbSampleText.Repaint;    
end;

procedure TfmQExport4ODSEditor.btnFontStrikeOutClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if btnFontStrikeOut.Down then
    FCurrStyle.Font.Style := FCurrStyle.Font.Style + [fsStrikeOut]
  else
    FCurrStyle.Font.Style := FCurrStyle.Font.Style - [fsStrikeOut];
  pbSampleText.Repaint;  
end;

procedure TfmQExport4ODSEditor.btnUnderlineSingleClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if btnUnderlineSingle.Down then
    FCurrStyle.Font.Style := FCurrStyle.Font.Style + [fsUnderline]
  else
    FCurrStyle.Font.Style := FCurrStyle.Font.Style - [fsUnderline];
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODSEditor.pbBackColorPaint(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if not FCurrStyle.AllowBackground then Exit;
  PaintStandardColors(pbBackColor, FCurrStyle.BackgroundColor);
end;

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

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

procedure TfmQExport4ODSEditor.sbBackgroundColorClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  if ColorDialog.Execute then
    if FCurrStyle.AllowBackground then
      if ColorDialog.Color <> -1 then
        FCurrStyle.BackgroundColor := ColorDialog.Color
      else
        FCurrStyle.BackgroundColor := clWhite;
  pbBackColor.Repaint;
  pbSampleText.Repaint;  
end;

procedure TfmQExport4ODSEditor.TuneBackgroundControls(Condition: Boolean);
begin
  cbBackground.Checked := Condition;
  sbBackgroundColor.Enabled := Condition;
  pbBackColor.Enabled := Condition;
  if Condition then
    pbBackColor.Repaint;
end;

procedure TfmQExport4ODSEditor.cbBackgroundClick(Sender: TObject);
begin
  if not Assigned(FCurrStyle) then Exit;
  FCurrStyle.AllowBackground := cbBackground.Checked;
  TuneBackgroundControls(cbBackground.Checked);
  pbSampleText.Repaint;  
end;

procedure TfmQExport4ODSEditor.NumberKeyPress(Sender: TObject;
  var Key: Char);
begin
  if not QExport4Common.CharInSet(Key, ['1', '2', '3', '4', '5', '6', '7',
                        '8', '9', '0', #8])
    then Key := #0;
end;

procedure TfmQExport4ODSEditor.pbSampleTextPaint(Sender: TObject);
begin
  if FCurrStyle = nil then Exit;
  DrawODSSample(pbSampleText, FCurrStyle);
end;

procedure TfmQExport4ODSEditor.bResetItemClick(Sender: TObject);
begin
  FCurrStyle := nil;
  if pcStyleEditor.ActivePage = tsOptions then
  begin
    tsBorder.TabVisible := true;
    if not assigned(lvOptions.Items[lvOptions.ItemFocused.Index]) then
      Exit;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      QExportLoadStr(QED_ODS_Header) then
      begin
        HrStyle.SetDefault;
        FCurrStyle := HrStyle;
      end;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      QExportLoadStr(QED_ODS_Footer) then
      begin
        FrStyle.SetDefault;
        FCurrStyle := FrStyle;
      end;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      QExportLoadStr(QED_ODS_CaptionRow) then
      begin
        CRStyle.SetDefault;
        FCurrStyle := CRStyle;
      end;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      QExportLoadStr(QED_ODS_Data) then
      begin
        DStyle.SetDefault;
        FCurrStyle := DStyle;
      end;
  end;
  if pcStyleEditor.ActivePage = tsStripStyles then
  begin
    tsBorder.TabVisible := false;
    if not assigned(lvSStyles.Items[lvSStyles.ItemFocused.Index]) then
      Exit;
    SStylesList[lvSStyles.ItemFocused.Index].SetDefault;
    FCurrStyle := SStylesList[lvSStyles.ItemFocused.Index];
  end;
  SetCurrentStatus;
end;

procedure TfmQExport4ODSEditor.bResetAllClick(Sender: TObject);
var
  i: Integer;
begin
  if pcStyleEditor.ActivePage = tsOptions then
  begin
    tsBorder.TabVisible := true;
    HrStyle.SetDefault;
    FrStyle.SetDefault;
    CRStyle.SetDefault;
    DStyle.SetDefault;
  end;
  if pcStyleEditor.ActivePage = tsStripStyles then
  begin
    tsBorder.TabVisible := false;
    for i := 0 to SStylesList.Count - 1 do
      SStylesList[i].SetDefault;
  end;
  GetCurrentStyle;
end;

end.
