unit uAuditColumnsEditor;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, VirtualTrees, Buttons, ToolWin, GR32_RangeBars;

type
  TFormAuditColumnsEditor = class(TForm)
    LabelColumnsList: TGroupBox;
    ColumnsList: TListView;
    ColumnStateBox: TGroupBox;
    ButtonShowHide: TButton;
    ButtonUp: TButton;
    ButtonDown: TButton;
    LabelColumnSize: TLabel;
    ButtonReset: TButton;
    ButtonOk: TButton;
    ButtonCancel: TButton;
    ColumnSize: TGaugeBar;
    LabelColumnSizeValue: TEdit;
    procedure ButtonUpClick(Sender: TObject);
    procedure ButtonDownClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ColumnsListClick(Sender: TObject);
    procedure ColumnsListKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ColumnsListSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure ButtonOkClick(Sender: TObject);
    procedure ButtonCancelClick(Sender: TObject);
    procedure ButtonResetClick(Sender: TObject);
    procedure ButtonShowHideClick(Sender: TObject);
    procedure ColumnsListCustomDrawItem(Sender: TCustomListView;
      Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
    procedure ColumnSizeChange(Sender: TObject);
    procedure LabelColumnSizeValueChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormAuditColumnsEditor: TFormAuditColumnsEditor;

implementation

uses uAuditGames;

{$R *.dfm}

procedure TFormAuditColumnsEditor.ButtonUpClick(Sender: TObject);
var
  ItemCaption: String;
  ItemPosition, ItemID: ShortInt;
  ItemChecked: Boolean;
  ItemSize: Integer;
  InsertItem: TListItem;
begin
  if (ColumnsList.Selected <> nil) and (ColumnsList.Selected.Index > 2) then
     begin
       ItemCaption:= ColumnsList.Selected.Caption;
       ItemPosition:= ColumnsList.Selected.Index;
       ItemChecked:= ColumnsList.Selected.Checked;
       ItemSize:= StrToInt(ColumnsList.Selected.SubItems[0]);
       ItemID:= ColumnsList.Selected.ImageIndex;
       ColumnsList.DeleteSelected;
       with ColumnsList do
         InsertItem:= Items.Insert(ItemPosition-1);
       InsertItem.Caption:= ItemCaption;
       InsertItem.Checked:= ItemChecked;
       InsertItem.SubItems.Add(IntToStr(ItemSize));
       InsertItem.ImageIndex:= ItemID;
       ColumnsList.ItemFocused:= ColumnsList.Items[ItemPosition-1];
       ColumnsList.ItemIndex:= ItemPosition-1;
       ColumnsList.Selected.MakeVisible(False);
     end;
  ColumnsList.SetFocus;
end;

procedure TFormAuditColumnsEditor.ButtonDownClick(Sender: TObject);
var
  ItemCaption: String;
  ItemPosition, ItemID: ShortInt;
  ItemChecked: Boolean;
  ItemSize: Integer;
  InsertItem: TListItem;
begin
  if (ColumnsList.Selected <> nil) and (ColumnsList.Selected.Index < (FormAudit.AuditTree.Header.Columns.Count-1)) and
     (ColumnsList.Selected.Index > 1) then
     begin
       ItemCaption:= ColumnsList.Selected.Caption;
       ItemPosition:= ColumnsList.Selected.Index;
       ItemChecked:= ColumnsList.Selected.Checked;
       ItemSize:= StrToInt(ColumnsList.Selected.SubItems[0]);
       ItemID:= ColumnsList.Selected.ImageIndex;
       ColumnsList.DeleteSelected;
       with ColumnsList do
         InsertItem:= Items.Insert(ItemPosition+1);
       InsertItem.Caption:= ItemCaption;
       InsertItem.Checked:= ItemChecked;
       InsertItem.SubItems.Add(IntToStr(ItemSize));
       InsertItem.ImageIndex:= ItemID;
       ColumnsList.ItemFocused:= ColumnsList.Items[ItemPosition+1];
       ColumnsList.ItemIndex:= ItemPosition+1;
       ColumnsList.Selected.MakeVisible(False);
     end;
  ColumnsList.SetFocus;
end;

procedure TFormAuditColumnsEditor.FormShow(Sender: TObject);
var
  Loop: ShortInt;
begin
  ColumnsList.Items.BeginUpdate;
  for Loop:=0 to FormAudit.AuditTree.Header.Columns.Count-1 do
  begin
    with ColumnsList.Items.Add do
    begin
      Caption:= FormAudit.AuditTree.Header.Columns.Items[FormAudit.AuditTree.Header.Columns.ColumnFromPosition(Loop)].Text;
      ImageIndex:= FormAudit.AuditTree.Header.Columns.Items[FormAudit.AuditTree.Header.Columns.ColumnFromPosition(Loop)].ID; // column ID number (fixed, never changes)
      Checked:= coVisible in FormAudit.AuditTree.Header.Columns.Items[FormAudit.AuditTree.Header.Columns.ColumnFromPosition(Loop)].Options;
      SubItems.Add(IntToStr(FormAudit.AuditTree.Header.Columns.Items[FormAudit.AuditTree.Header.Columns.ColumnFromPosition(Loop)].Width));
    end;
  end;
  ColumnsList.Items.EndUpdate;
end;

procedure TFormAuditColumnsEditor.ColumnsListClick(Sender: TObject);
begin
  if ColumnsList.Selected <> nil then
     begin
       if ColumnsList.Selected.Index <> 0 then
          begin
            ButtonShowHide.Tag:= Ord(ColumnsList.Selected.Checked);
            case ColumnsList.Selected.Checked of
              True : ButtonShowHide.Caption:= '&Hide';
              False: ButtonShowHide.Caption:= '&Show';
            end;
          end;
       ColumnSize.Position:= StrToInt(ColumnsList.Selected.SubItems[0]);
       if ColumnsList.Selected.Index = 0 then
          ColumnsList.Selected.Checked:= True;
     end;
end;

procedure TFormAuditColumnsEditor.ColumnsListKeyUp(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if Key = VK_SPACE then
     ColumnsList.OnClick(Self);
end;

procedure TFormAuditColumnsEditor.ColumnsListSelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
begin
  if Selected then
     ColumnsList.OnClick(Self);
end;

procedure TFormAuditColumnsEditor.ButtonOkClick(Sender: TObject);
begin
  FormAudit.UpdateColumns;
  Close;
end;

procedure TFormAuditColumnsEditor.ButtonCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TFormAuditColumnsEditor.ButtonResetClick(Sender: TObject);
var
  Loop: ShortInt;
begin
  ColumnsList.Items.BeginUpdate;
  ColumnsList.Clear;
  for Loop:=0 to FormAudit.AuditTree.Header.Columns.Count-1 do
  begin
    with ColumnsList.Items.Add do
    begin
      Caption:= FormAudit.AuditTree.Header.Columns.Items[Loop].Text;
      ImageIndex:= FormAudit.AuditTree.Header.Columns.Items[Loop].ID; // column ID number (fixed, never changes)
      Checked:= True;
      SubItems.Add(IntToStr(FormAudit.AuditTree.Header.Columns.Items[Loop].Tag));
    end;
  end;
  ColumnsList.Items.EndUpdate;  
end;

procedure TFormAuditColumnsEditor.ButtonShowHideClick(Sender: TObject);
begin
  if (ColumnsList.Selected <> nil) and (ColumnsList.Selected.Index > 0) then
     begin
       case Boolean(ButtonShowHide.Tag) of
         True : ButtonShowHide.Caption:= '&Show';
         False: ButtonShowHide.Caption:= '&Hide';
       end;
       ButtonShowHide.Tag:= Ord(not Boolean(ButtonShowHide.Tag));
       ColumnsList.Selected.Checked:= Boolean(ButtonShowHide.Tag);
     end;
  ColumnsList.SetFocus;
end;

procedure TFormAuditColumnsEditor.ColumnsListCustomDrawItem(
  Sender: TCustomListView; Item: TListItem; State: TCustomDrawState;
  var DefaultDraw: Boolean);
begin
  if not Item.Checked then
     Sender.Canvas.Font.Color:= clGray;
end;

procedure TFormAuditColumnsEditor.ColumnSizeChange(Sender: TObject);
begin
  if ColumnsList.Selected = nil then
     Exit;
  LabelColumnSizeValue.Text:= IntToStr(ColumnSize.Position);
  ColumnsList.Selected.SubItems[0]:= IntToStr(ColumnSize.Position);
end;

procedure TFormAuditColumnsEditor.LabelColumnSizeValueChange(
  Sender: TObject);
begin
  if ColumnSize.Position <> StrToInt(LabelColumnSizeValue.Text) then
     ColumnSize.Position:= StrToInt(LabelColumnSizeValue.Text);
end;

end.
