unit _frmDebug;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, cxGraphics,
  cxLookAndFeels, cxLookAndFeelPainters, Vcl.Menus, cxControls, cxContainer,
  cxEdit, cxTextEdit, cxMemo, cxButtons, cxCheckBox, cxImage, _frmSmartWindows,
  siComp;

type
  TfrmDebug = class(TForm)
    Panel1: TPanel;
    btnClose: TcxButton;
    memDebug: TcxMemo;
    btnDebugOn: TcxButton;
    btnDebugOff: TcxButton;
    btnClear: TcxButton;
    chkDebugToFile: TcxCheckBox;
    Panel2: TPanel;
    Label1: TLabel;
    edtCaption: TcxTextEdit;
    imgIcon: TcxImage;
    edtClassName: TcxTextEdit;
    Label3: TLabel;
    edtHashedClassName: TcxTextEdit;
    lblHashedClassName: TLabel;
    btnCopy: TcxButton;
    siLang: TsiLang;
    edtCurrentResolution: TcxTextEdit;
    Label2: TLabel;
    Label4: TLabel;
    edtPCName: TcxTextEdit;
    Label5: TLabel;
    memListSizes: TcxMemo;
    chkWindowIDinIgnoreList: TcxCheckBox;
    chkWindowIDWithCaption: TcxCheckBox;
    chkWindowModal: TcxCheckBox;
    procedure btnCloseClick(Sender: TObject);
    procedure btnDebugOnClick(Sender: TObject);
    procedure btnDebugOffClick(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure chkDebugToFileClick(Sender: TObject);
    procedure btnCopyClick(Sender: TObject);
  private
    FDebugActive: Boolean;
    FDebugToFile: Boolean;
    FDebugFileName: string;
    procedure SetDebugActive(const Value: Boolean);
    procedure SetDebugToFile(const Value: Boolean);
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  public
    procedure AddToDebugLog(const AString: string);
    procedure AddToListSizesLog(const AString: string);
    procedure UpdateForegroundWindow(const AHashedStringInfo: THashedStringInfo; const AResolution: string; const APCName: string);
    property DebugActive: Boolean read FDebugActive write SetDebugActive;
    property DebugToFile: Boolean read FDebugToFile write SetDebugToFile;
  end;

var
  frmDebug: TfrmDebug;

implementation

{$R *.dfm}

uses Clipbrd;

procedure TfrmDebug.AddToDebugLog(const AString: string);
var
  LogStr: string;
begin
  if FDebugActive then
  begin
    if AString = '' then
      LogStr := ''
    else
      LogStr := FormatDateTime('dd.mm.yyyy hh:nn:ss:zzz', now) + '  ::  ' + AString;

    memDebug.Lines.Add(LogStr);
    SendMessage(memDebug.Handle, EM_SCROLLCARET, 0, 0);
    if FDebugToFile then
    begin
      memDebug.Lines.SaveToFile(FDebugFileName);
    end;
  end;
end;

procedure TfrmDebug.AddToListSizesLog(const AString: string);
begin
  if AString = '' then
    memListSizes.Lines.Add('')
  else
    memListSizes.Lines.Add(FormatDateTime('dd.mm.yyyy hh:nn:ss:zzz', now) + '  ::  ' + AString);
end;

procedure TfrmDebug.btnClearClick(Sender: TObject);
begin
  memDebug.Clear;
end;

procedure TfrmDebug.btnCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmDebug.btnCopyClick(Sender: TObject);
var
  clip: string;
begin
  clip := 'Active Window:' + #13#10 + 'Caption: ' + edtCaption.Text + #13#10 + 'ClassName: ' + edtClassName.Text + #13#10 + 'Hashed ClassName: ' + edtHashedClassName.Text;
  Clipboard.SetTextBuf(PChar(clip));
end;

procedure TfrmDebug.btnDebugOffClick(Sender: TObject);
begin
  DebugActive := False;
end;

procedure TfrmDebug.btnDebugOnClick(Sender: TObject);
begin
  DebugActive := True;
end;

procedure TfrmDebug.chkDebugToFileClick(Sender: TObject);
begin
  FDebugToFile := chkDebugToFile.Checked;
  if FDebugActive then
    SetDebugActive(True);
end;

procedure TfrmDebug.CreateParams(var Params: TCreateParams);
begin
  inherited;
  Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
  Params.Style := Params.Style or SW_SHOWNOACTIVATE;
end;

procedure TfrmDebug.FormShow(Sender: TObject);
begin
  chkDebugToFile.Checked := FDebugToFile;
end;

procedure TfrmDebug.SetDebugActive(const Value: Boolean);
begin
  FDebugActive := Value;
  btnDebugOn.Enabled := not DebugActive;
  btnDebugOff.Enabled := DebugActive;

  if FDebugToFile and FDebugActive then
  begin
    FDebugFileName := frmSmartWindows.ConfigPath + 'DebugLog-' + FormatDateTime('yyyy-mm-dd_hh-nn-ss', now) + '.txt';
    AddToDebugLog('');
    AddToDebugLog('Saving debug log to file "' + FDebugFileName + '"');
  end;
end;

procedure TfrmDebug.SetDebugToFile(const Value: Boolean);
begin
  FDebugToFile := Value;
end;

procedure TfrmDebug.UpdateForegroundWindow(const AHashedStringInfo: THashedStringInfo; const AResolution: string; const APCName: string);
begin
  edtCaption.Text := AHashedStringInfo.WindowCaption;
  edtClassName.Text := AHashedStringInfo.ClassName;
  edtHashedClassName.Text := AHashedStringInfo.HashedString;
  edtPCName.Text := APCName;
  edtCurrentResolution.Text := AResolution;
  chkWindowIDinIgnoreList.Checked := AHashedStringInfo.IsClassNameIgnored;
  chkWindowIDWithCaption.Checked := AHashedStringInfo.GenClassNameWithCaption;
  if AHashedStringInfo.Icon > 0 then
  begin
    imgIcon.Picture.Icon.Handle := AHashedStringInfo.Icon;
  end
  else
  begin
    imgIcon.Picture := nil;
  end;
end;

end.
