{ *********************************************************************** }
{                                                                         }
{ Copular Chat Client v3.0 Source Code                                    }
{ Main Form Unit                                                          }
{                                                                         }
{ Copyright (c) 1998-2002 SAF Studio                                      }
{                                                                         }
{ Author  : Niu Yu Ping                                                   }
{ Nickname: DecimalOX                                                     }
{ Address : Jilin City China                                              }
{                                                                         }
{ QICQ    : 103106262                                                     }
{ Homepage: www.safree.com                                                }
{ EMail   : decimalox@sohu.com                                            }
{                                                                         }
{ *********************************************************************** }

unit main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, KsSkinEngine, KsHooks, KsForms, KsSkinForms, KsControls,
  KsButtons, KsSkinButtons, ExtCtrls, ComCtrls, IniFiles;

type
  TMainForm = class(TForm)
    FormSKE: TSeSkinEngine;
    SeSkinForm1: TSeSkinForm;
    GoRoomBtn: TSeSkinButton;
    MoveTimer: TTimer;
    RoomList: TListView;
    procedure FormCreate(Sender: TObject);
    procedure MoveTimerTimer(Sender: TObject);
    procedure GoRoomBtnClick(Sender: TObject);
    procedure RoomListDblClick(Sender: TObject);
  private
    { Private declarations }
    FUserName: string;
    FPassword: string;
    procedure SetPassword(const Value: string);
    procedure SetUserName(const Value: string);

    //Load room list from file
    procedure LoadRoom;
  public
    { Public declarations }

    //Temp values is used to store name and password which in Chat Form
    property UserName: string read FUserName write SetUserName;
    property Password: string read FPassword write SetPassword;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}

uses
  Chat;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  UserName:='';
  Password:='';

  //Set form position
  Top:=Screen.Height;
  Left:=Screen.Width div 2 - Width div 2;

  MoveTimer.Enabled:=True;
  LoadRoom;
end;

procedure TMainForm.SetPassword(const Value: string);
begin
  FPassword := Value;
end;

procedure TMainForm.SetUserName(const Value: string);
begin
  FUserName := Value;
end;

//Begin moving from bottom to center
procedure TMainForm.MoveTimerTimer(Sender: TObject);
begin
  Top:=Top-50;
  if Top<=Screen.Height div 2 - Height div 2 then
  begin
    Top:=Screen.Height div 2 - Height div 2;
    MoveTimer.Enabled:=False;
  end;
end;

procedure TMainForm.LoadRoom;
var
  SearchRec: TSearchRec;
begin
  RoomList.Items.Clear;
  if FindFirst(GetCurrentDir+'\maps\*.map', faAnyFile, SearchRec)=0 then
    with TIniFile.Create(GetCurrentDir+'\maps\'+SearchRec.Name) do
      try
        with RoomList.Items.Add do
        begin
          Caption:=Copy(SearchRec.Name, 1, Length(SearchRec.Name)-4);
          SubItems.Add(ReadString('Map', 'Memo', ''));
        end;
      finally
        Free;
      end;
  while FindNext(SearchRec)=0 do
    with TIniFile.Create(GetCurrentDir+'\maps\'+SearchRec.Name) do
      try
        with RoomList.Items.Add do
        begin
          Caption:=Copy(SearchRec.Name, 1, Length(SearchRec.Name)-4);
          SubItems.Add(ReadString('Map', 'Memo', ''));
        end;
      finally
        Free;
      end;
  FindClose(SearchRec);
end;

procedure TMainForm.GoRoomBtnClick(Sender: TObject);
begin
  if RoomList.SelCount=0 then
    Exit;

  with TChatForm.Create(Self, RoomList.Selected.Caption) do
    try
      ShowModal;
    finally
      Free;
    end;
end;

procedure TMainForm.RoomListDblClick(Sender: TObject);
begin
  GoRoomBtnClick(Self);
end;

end.
