//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <inifiles.hpp>
#pragma hdrstop

#include "NastForm.h"
#include "MAIN.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TNastav *Nastav;
//---------------------------------------------------------------------------
__fastcall TNastav::TNastav(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TNastav::PortyDropDown(TObject *Sender)
{
  Porty->Items->Clear();
  HANDLE h;
  char jmeno[8];

  for(int i=1;i<257;i++){
    sprintf(jmeno,"COM%i",i);
    h=CreateFile(jmeno,0,0,NULL,OPEN_EXISTING,0,NULL);
    if(h!=INVALID_HANDLE_VALUE||CisloPortu==i){
      Porty->Items->AddObject(jmeno,(TObject*)i);
      CloseHandle(h);
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TNastav::FormShow(TObject *Sender)
{
  Porty->Items->Clear();
  HANDLE h;
  char jmeno[8];

  for(int i=1;i<257;i++){
    sprintf(jmeno,"COM%i",i);
    h=CreateFile(jmeno,0,0,NULL,OPEN_EXISTING,0,NULL);
    if(h!=INVALID_HANDLE_VALUE||CisloPortu==i){
      Porty->Items->AddObject(jmeno,(TObject*)i);
      CloseHandle(h);
      if(CisloPortu==i){
        Porty->ItemIndex=Porty->Items->Count-1;
        Porty->Text=Porty->Items->Strings[Porty->Items->Count-1];
      }
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TNastav::OKClick(TObject *Sender)
{
  try{
    TIniFile *ini=new TIniFile(MainForm->Adresar);
    ini->WriteInteger("PORT","Port",
      int(Porty->Items->Objects[Porty->ItemIndex]));
    MessageBox(Handle,
      "Nové nastavení se akceptuje až při novém spuštění programu!",
      Application->Title.c_str(),
      MB_ICONINFORMATION);
    Application->Terminate();
  }
  catch(...){
    MessageBox(Handle,
      "Zvolte port!",
      Application->Title.c_str(),
      MB_ICONHAND);
  }
}
//---------------------------------------------------------------------------
void __fastcall TNastav::StornoClick(TObject *Sender)
{
  if(Application->MainForm==this)
    Application->Terminate();
}
//---------------------------------------------------------------------------

