unit _frmAdvanced;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxLookAndFeels,
  cxLookAndFeelPainters, Vcl.Menus, Vcl.StdCtrls, cxButtons, Vcl.ExtCtrls,
  cxControls, cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit,
  cxNavigator, Data.DB, cxDBData, cxGridLevel, cxClasses, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid, kbmMemTable,
  cxImageComboBox, siComp;

type
  TfrmAdvanced = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Image1: TImage;
    Panel2: TPanel;
    btnOk: TcxButton;
    btnCancel: TcxButton;
    cxGridDBTableView1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGrid: TcxGrid;
    tblAdvanced: TkbmMemTable;
    DataSource: TDataSource;
    tblAdvancedfldCaption: TStringField;
    tblAdvancedfldClassName: TStringField;
    tblAdvancedfldHashedClassName: TStringField;
    tblAdvancedfldAlwaysRestore: TBooleanField;
    cxGridDBTableView1fldCaption: TcxGridDBColumn;
    cxGridDBTableView1fldClassName: TcxGridDBColumn;
    cxGridDBTableView1fldHashedClassName: TcxGridDBColumn;
    cxGridDBTableView1fldAlwaysRestore: TcxGridDBColumn;
    Label2: TLabel;
    siLang: TsiLang;
    procedure btnCancelClick(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure cxGridDBTableView1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  frmAdvanced: TfrmAdvanced;

implementation

{$R *.dfm}

uses _frmSmartWindows, _WindowList;

procedure TfrmAdvanced.btnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmAdvanced.btnOkClick(Sender: TObject);
var
  wli: TWindowListItem;
begin
  tblAdvanced.DisableControls;
  try
    tblAdvanced.First;
    while not tblAdvanced.Eof do
    begin
      wli := frmSmartWindows.WindowList.GetItemByHashedClassName(tblAdvancedfldHashedClassName.Value);
      wli.AlwaysRestore := tblAdvancedfldAlwaysRestore.Value;

      if wli.AlwaysRestore then
      begin
        frmSmartWindows.AppUserConfig.WriteBool(frmSmartWindows.AddConfigPrefix(wli.HashedClassName), 'AlwaysRestore', True);
      end
      else
      begin
        frmSmartWindows.AppUserConfig.DeleteSection(frmSmartWindows.AddConfigPrefix(wli.HashedClassName));
      end;

      tblAdvanced.Next;
    end;
  finally
    tblAdvanced.EnableControls;
    frmSmartWindows.SaveAppUserConfig;
  end;

  Close;
end;

procedure TfrmAdvanced.cxGridDBTableView1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.Item.Index < 3 then
  begin
    ACanvas.Brush.Color := $00F0F0F0;
  end;
end;

procedure TfrmAdvanced.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  tblAdvanced.Active := False;
end;

procedure TfrmAdvanced.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #27 then
  begin
    Key := #0;
    Close;
  end;
end;

procedure TfrmAdvanced.FormShow(Sender: TObject);
var
  i: Integer;
begin
  tblAdvanced.Active := True;
  tblAdvanced.DisableControls;
  try
    for i := 0 to frmSmartWindows.WindowList.Count - 1 do
    begin
      //Show only application which are/was open in this session / Or have the AlwaysRestore Flag
      //Makes the list shorter and otherwise we dont have a window title because this is not saved in config file.
      if (frmSmartWindows.WindowList.Items[i].Title <> '') or (frmSmartWindows.WindowList.Items[i].AlwaysRestore) then
      begin
        tblAdvanced.Append;
        tblAdvancedfldCaption.Value := frmSmartWindows.WindowList.Items[i].Title;
        tblAdvancedfldClassName.Value := frmSmartWindows.WindowList.Items[i].ClassName;
        tblAdvancedfldHashedClassName.Value := frmSmartWindows.WindowList.Items[i].HashedClassName;
        tblAdvancedfldAlwaysRestore.Value := frmSmartWindows.WindowList.Items[i].AlwaysRestore;
        tblAdvanced.Post;
      end;
    end;
  finally
    tblAdvanced.First;
    tblAdvanced.EnableControls;
  end;
end;

end.
