unit Unit7;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  TFlatButtonUnit, ExtCtrls, TFlatPanelUnit, TFlatEditUnit, StdCtrls,
  TFlatMemoUnit, WSocket, TFlatHintUnit, BUFormAnimation;

type
  TfrmChat = class(TForm)
    FlatPanel1: TFlatPanel;
    FlatButton1: TFlatButton;
    FlatButton2: TFlatButton;
    FlatPanel2: TFlatPanel;
    memChat: TFlatMemo;
    Msg: TFlatEdit;
    btnChatsend: TFlatButton;
    btnClose: TFlatButton;
    FlatHint1: TFlatHint;
    Cchat: TWSocket;
    BUFormAnimation1: TBUFormAnimation;
    procedure FlatButton1Click(Sender: TObject);
    procedure FlatButton2Click(Sender: TObject);
    procedure btnChatsendClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure CchatDataAvailable(Sender: TObject; Error: Word);
    procedure CchatSessionClosed(Sender: TObject; Error: Word);
    procedure CchatSessionConnected(Sender: TObject; Error: Word);
    procedure FlatPanel1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ServerResponse(Status: string);
    procedure FormActivate(Sender: TObject);
  private
  RcvBuf : array [0..1023] of char;
  RcvLen : integer;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmChat: TfrmChat;
  Connected: boolean;

implementation

uses Unit1;

{$R *.DFM}

procedure TfrmChat.ServerResponse(Status: String);
begin
   memChat.Lines.Add(Status);
end;

procedure TfrmChat.FlatButton1Click(Sender: TObject);
begin
frmChat.Close;
end;

procedure TfrmChat.FlatButton2Click(Sender: TObject);
begin
frmChat.WindowState:=wsMinimized;
end;

procedure TfrmChat.btnChatsendClick(Sender: TObject);
var
  You, Say: string;
begin
   if Connected  then
      begin
         You := frmClient.Nick.Text;
         Say := Msg.Text;
         Cchat.SendStr(You + ': ' + Say + #13#10);
         ServerResponse(You + ': ' + Say);
         Msg.Clear;
      end
   else
      ServerResponse('Not connected');
end;

procedure TfrmChat.FormShow(Sender: TObject);
begin
   Cchat.Addr  := frmClient.Host.Text;
   Cchat.Port  := '6668';
   Cchat.Proto := 'tcp';
   Cchat.Connect;
end;

procedure TfrmChat.btnCloseClick(Sender: TObject);
begin
   frmClient.info.caption:='Stopping chat session...';
   frmClient.Client.SendStr('DCW' + #13#10);
   frmChat.Hide;
   memChat.Clear;
   Cchat.Close;
end;

procedure TfrmChat.CchatDataAvailable(Sender: TObject; Error: Word);
var
  Len: Integer;
  I: Integer;
begin
   Len := Cchat.Receive(@RcvBuf[RcvLen], SizeOf(RcvBuf) - RcvLen - 1);
   if Len <= 0 then
      Exit;

      RcvLen := RcvLen + Len;
      RcvBuf[RcvLen] := #0;
         while TRUE do
            begin
               I := StrScan(@RcvBuf, #10) - RcvBuf;
               if I < 0 then
                  Break;
                  RcvBuf[I] := #0;
                  ServerResponse(StrPas(RcvBuf));
                  RcvBuf[I] := #10;
               if I >= (RcvLen - 1) then
                  begin
                     RcvLen := 0;
                     Break;
                  end;
               Move(RcvBuf[I + 1], RcvBuf, RcvLen - I);
               RcvLen := RcvLen - I - 1;
            end;
end;

procedure TfrmChat.CchatSessionClosed(Sender: TObject; Error: Word);
begin
Connected := False;
end;

procedure TfrmChat.CchatSessionConnected(Sender: TObject; Error: Word);
begin
Connected := True;
end;

procedure TfrmChat.FlatPanel1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
ReleaseCapture;
Perform(WM_NCLBUTTONDOWN,HTCAPTION,0);
end;

procedure TfrmChat.FormActivate(Sender: TObject);
begin
BUFormAnimation1.AppearRectangle;
end;

end.
