/*
  ICS Network Tools log window - C++Builder translation of the Delphi unit OverbyteIcsNetTools2.pas
  (demos-delphi-vcl), ICS V9.8.

  Shows the log lines the main form (OverbyteIcsNetTools1) buffers; its position is kept in the
  [LogWindow] section of the ICS ini file. Both forms read GetIcsIniFileName(), which is the
  file name the Delphi keeps in the FIniFileName global of the main unit.
*/

#include <vcl.h>
#pragma hdrstop

#include "OverbyteIcsNetTools2.h"
#include "OverbyteIcsNetTools1.h"

#include <memory>

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

TFormLog *FormLog;

namespace {

// Same section and key names as the Delphi, so both builds share the ini file.
const String SectionLogWindow = "LogWindow";
const String KeyTop           = "Top";
const String KeyLeft          = "Left";
const String KeyWidth         = "Width";
const String KeyHeight        = "Height";

}  // namespace

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

// Also called by the main form when it closes while the log window is visible.
void __fastcall TFormLog::FormClose(TObject *Sender, TCloseAction &Action)
{
    // The second constructor argument (PreserveAnsi) has a Delphi default that the header drops.
    std::unique_ptr<TIcsIniFile> IniFile(new TIcsIniFile(GetIcsIniFileName(), true));
    IniFile->WriteInteger(SectionLogWindow, KeyWidth, Width);
    IniFile->WriteInteger(SectionLogWindow, KeyHeight, Height);
    IniFile->WriteInteger(SectionLogWindow, KeyTop, Top);
    IniFile->WriteInteger(SectionLogWindow, KeyLeft, Left);
    IniFile->UpdateFile();
}

void __fastcall TFormLog::FormShow(TObject *Sender)
{
    std::unique_ptr<TIcsIniFile> IniFile(new TIcsIniFile(GetIcsIniFileName(), true));
    Width = IniFile->ReadInteger(SectionLogWindow, KeyWidth, Width);
    Height = IniFile->ReadInteger(SectionLogWindow, KeyHeight, Height);
    // default to the right of the main form
    Top = IniFile->ReadInteger(SectionLogWindow, KeyTop, ToolsForm->Top);
    Left = IniFile->ReadInteger(SectionLogWindow, KeyLeft, ToolsForm->Left + ToolsForm->Width + 20);
}
