summaryrefslogtreecommitdiff
path: root/plugins/!NotAdopted/Chess4Net/ConnectionUnit.pas
blob: b5c12fef1e507748b4a96640b56e9f6a10359129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
////////////////////////////////////////////////////////////////////////////////
// All code below is exclusively owned by author of Chess4Net - Pavel Perminov
// (packpaul@mail.ru, packpaul1@gmail.com).
// Any changes, modifications, borrowing and adaptation are a subject for
// explicit permition from the owner.

unit ConnectionUnit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Mask;

type
  TConnectionForm = class(TForm)
    NickLabel: TLabel;
    NickEdit: TEdit;
    ConnectionRadioGroup: TRadioGroup;
    ServerRadioButton: TRadioButton;
    ClientRadioButton: TRadioButton;
    OKButton: TButton;
    CancelButton: TButton;
    IPEdit: TEdit;
    IPLabel: TLabel;
    PortLabel: TLabel;
    PortEdit: TMaskEdit;
    procedure FormShow(Sender: TObject);
    procedure ServerRadioButtonClick(Sender: TObject);
    procedure ClientRadioButtonClick(Sender: TObject);
    procedure NickEditExit(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure IPEditChange(Sender: TObject);
    procedure PortEditExit(Sender: TObject);
  public
    function GetPort: word;
    constructor Create(Owner: TComponent); reintroduce;
  end;

implementation

{$R *.dfm}

uses
  GlobalsLocalUnit;

procedure TConnectionForm.ServerRadioButtonClick(Sender: TObject);
begin
  IPEdit.Enabled := FALSE;
  OKButton.Enabled := TRUE;
end;

procedure TConnectionForm.FormShow(Sender: TObject);
var
  frmOwner: TForm;
begin
  frmOwner := (Owner as TForm);
  Left:= frmOwner.Left + (frmOwner.Width - Width) div 2;
  Top:= frmOwner.Top + (frmOwner.Height - Height) div 2;
end;

procedure TConnectionForm.ClientRadioButtonClick(Sender: TObject);
begin
  IPEdit.Enabled:= TRUE;
  if IPEdit.Text <> '' then OKButton.Enabled:= TRUE
    else OKButton.Enabled:= FALSE;
end;

procedure TConnectionForm.PortEditExit(Sender: TObject);
begin
  PortEdit.Text := IntToStr(GetPort);
end;

procedure TConnectionForm.NickEditExit(Sender: TObject);
begin
  if NickEdit.Text = '' then NickEdit.Text:= 'NN';
end;

procedure TConnectionForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then ModalResult:= mrOk;
end;

procedure TConnectionForm.IPEditChange(Sender: TObject);
begin
  if IPEdit.Text <> '' then OKButton.Enabled:= TRUE
    else OKButton.Enabled:= FALSE;
end;

constructor TConnectionForm.Create(Owner: TComponent);
begin
  FormStyle := (Owner as TForm).FormStyle;
  inherited;
  PortEdit.Text := IntToStr(DEFAULT_PORT);
end;

function TConnectionForm.GetPort: word;
var
  port: integer;
begin
  try
    port := StrToInt(Trim(PortEdit.Text));
    if (port > 0) and (port <= $FFFF) then
      Result := port
    else
      Result := DEFAULT_PORT
  except
    on EConvertError do
      Result := DEFAULT_PORT;
  end;
end;

end.