//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "trayicon"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{

if(Button1->Caption == "Start"){


if(StrToInt(Edit3->Text) >= 5000){

        ClientSocket1->Address = Edit1->Text;
        ClientSocket1->Port = StrToInt(Edit2->Text);
        ClientSocket1->Active = true;

        Timer1->Interval = (StrToInt(Edit3->Text));
        Button1->Caption = "Stop";

} else {

        ShowMessage("Zbyt mały interwał!");
}

} else {

        Timer1->Enabled = false;
        ClientSocket1->Active = false;
        Button1->Caption = "Start";
}

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
        this->Close();        
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
      TCustomWinSocket *Socket)
{

 Button1->Enabled = true;
 ShowMessage("Połączyłes się z serwerem!");
 Timer1->Enabled = true;

}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Disconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
        ShowMessage("Rozłączyłes się!");

}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Error(TObject *Sender,
      TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
        ShowMessage("Nastąpił błąd komunikacja nie jest możliwa!");
        this->Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ClientSocket1Connecting(TObject *Sender,
      TCustomWinSocket *Socket)
{
        Button1->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
   HINSTANCE shlexec_error;

        //troche kombinacja, bo nie moglem dojsc do ladu z importem dllki kompilowanej gcc z biblioteka
        //libgdi32.a, byla to koniecznosc ze wzgledu na wysoki opór materii
        //(borland - funkcja robiaca screeny po prostu nie dziala pod tym kompilatorem, pewnie jakies autorskie pomysly borlanda z gdi)
        shlexec_error = ShellExecute(0, "open", "ScreenMakerApp.exe", "skrinn.bmp", NULL ,0);

        if( (int)shlexec_error == ERROR_FILE_NOT_FOUND)
                MessageBox(0,"Nie mogę zrobic zrzutu! Brak programu.",0,MB_OK);

        ClientSocket1->Socket->SendText('s');

}
//---------------------------------------------------------------------------


void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
      TCustomWinSocket *Socket)
{

int messageSize = Socket->ReceiveLength();

if(messageSize == sizeof(char)){

AnsiString messageText = Socket->ReceiveText();

if (messageText == "g"){

        TFileStream *fs;
        fs = new TFileStream("skrinn.bmp", fmOpenRead | fmShareDenyNone);

        ClientSocket1->Socket->SendText( IntToStr(fs->Size) );
        fs->Free();


} else if( messageText == "k"){

 ClientSocket1->Socket->SendText('f');


} else if (messageText == "r"){

        TFileStream *fs;
        char *buffer;
        fs = new TFileStream("skrinn.bmp", fmOpenRead | fmShareDenyNone);

        buffer = new char[fs->Size];
        fs->Read(buffer,fs->Size);

        if( ClientSocket1->Socket->Connected){

                ClientSocket1->Socket->SendBuf(buffer, fs->Size);
                fs->Free();
                delete buffer;


        } else {

                ShowMessage("Połączenie utracone!");
        }

}

}

}
//---------------------------------------------------------------------------

