/*
  ICS Mail Queue demo - C++Builder translation of the Delphi unit OverbyteIcsMailQuTstdiag.pas
  (demos-delphi-vcl), ICS V9.8. The diagnostic log window; DemoForm fills it while DiagWinFlag is set.

  How this C++ version differs from the Delphi original, apart from syntax:
  - DiagWinFlag is also set when the window is shown again (OnShow, assigned in FormCreate).
  - FormDestroy is not bound; the destructor clears DiagWinFlag.
*/

#include <vcl.h>
#pragma hdrstop

#include <memory>

#include "OverbyteIcsMailQuTstdiag.h"
#include "OverbyteIcsMailQuTst1.h"
#include "OverbyteIcsIniFiles.hpp"

#pragma package(smart_init)
#pragma resource "*.dfm"

TDiagForm *DiagForm;

namespace {
const wchar_t *const SectionWindow = L"DiagWindow";
}

__fastcall TDiagForm::TDiagForm(TComponent *Owner)
    : TForm(Owner)
{
}

__fastcall TDiagForm::~TDiagForm()
{
    DiagWinFlag = false;
}

void __fastcall TDiagForm::mCopyClick(TObject *Sender)
{
    TraceBox->CopyToClipboard();
}

void __fastcall TDiagForm::mPasteClick(TObject *Sender)
{
    TraceBox->PasteFromClipboard();
}

void __fastcall TDiagForm::mSelAllClick(TObject *Sender)
{
    TraceBox->SelectAll();
}

void __fastcall TDiagForm::mCopyAllClick(TObject *Sender)
{
    TraceBox->SelectAll();
    TraceBox->CopyToClipboard();
}

void __fastcall TDiagForm::FormClose(TObject *Sender, TCloseAction &Action)
{
    DiagWinFlag = false;
    std::unique_ptr<TIcsIniFile> ini(new TIcsIniFile(IniFileName, true));
    ini->WriteInteger(SectionWindow, "Top", Top);
    ini->WriteInteger(SectionWindow, "Left", Left);
    ini->WriteInteger(SectionWindow, "Width", Width);
    ini->WriteInteger(SectionWindow, "Height", Height);
    ini->UpdateFile();
}

void __fastcall TDiagForm::FormCreate(TObject *Sender)
{
    std::unique_ptr<TIcsIniFile> ini(new TIcsIniFile(IniFileName, true));
    Top = ini->ReadInteger(SectionWindow, "Top", (Screen->Height - Height) / 2);
    Left = ini->ReadInteger(SectionWindow, "Left", (Screen->Width - Width) / 2);
    Width = ini->ReadInteger(SectionWindow, "Width", Width);
    Height = ini->ReadInteger(SectionWindow, "Height", Height);
    DiagWinFlag = true;
    // Delphi sets the flag only here, so after the window is closed once, showing it again
    // (DemoForm sets Visible) leaves it empty for good.
    OnShow = FormShow;
}

void __fastcall TDiagForm::FormShow(TObject *Sender)
{
    DiagWinFlag = true;
}
