unit UTools;
{ ********************************************************************************* }
{ (c) GNU                                                                           }
{ author: Heiko Tietze                                                              }
{ version: 1.0                                                                      }
{ date: 11.12.2004                                                                  }
{ description: some helper functions and global definitons                          }
{ ********************************************************************************* }

{$I Def.inc}

interface

uses {$IFDEF VCL}
     Graphics, Windows,
     {$ELSE}
     QGraphics,
     {$ENDIF}
     SysUtils;

//CLX fires 4117 in case of cursor down, but WIN32 still holds 40 for VK_Down
const ke_CursorDown={$IFDEF VCL}40 {$ELSE}4117{$ENDIF};
const ke_CursorUp=  {$IFDEF VCL}38 {$ELSE}4115{$ENDIF};
const ke_Enter=     {$IFDEF VCL}13 {$ELSE}4100{$ENDIF};
const ke_Escape=    {$IFDEF VCL}27 {$ELSE}4096{$ENDIF};

type TNetMsgType=(nmChat=100,
                  nmConnect=101,
                  nmAsk=102,
                  nmAnswer=103,
                  nmClientDisconnect=104,
                  nmServerDisconnect=105,
                  nmNewGame=106,
                  nmNextPlayer=107,
                  nmInterChangeLetters=108);
     TNetworkMessage=procedure(Command:TNetMsgType; Sender, Recipient: Byte; Value:string) of Object;

function FileSize2String(fs:extended):string;
function LightOrDark(Value : TColor):TColor;
{$IFDEF VCL}
function ComputerName :string;
function UserName:string;
{$ENDIF}

implementation

function FileSize2String(fs:extended):string;
const SizeNames : array[0..4] of string = (' Byte',' kB',' MB',' GB',' TB');
var i:byte;
begin
   i:=0; while fs>1000.0 do begin fs:=fs/1024.0; inc(i);end;
   Result:=FloatToStrF(fs,ffGeneral,4,2)+SizeNames[i];
end;

function LightOrDark(Value : TColor):TColor;
var l:longint;
begin
   l:=ColorToRGB(Value);
   if (l and $00FF0000) shr 16+
      (l and $0000FF00) shr 8+
      (l and $000000FF) > 500 then Result:=clBlack
                              else Result:=clWhite;
end;

{$IFDEF VCL}
function ComputerName :string;
var Len : Cardinal;
begin
  Len:=MAX_COMPUTERNAME_LENGTH;
  SetLength(Result,Len+1 );
  if GetComputerName(@Result[1],Len) then SetLength(Result,Len)
                                     else Result:='';
end;

function UserName:string;
var lLength:LongWord;
begin
  lLength:=1024;
  SetLength(Result,lLength );
  if GetUserName(PChar(Result),lLength) and (lLength>0) then
     SetLength(Result,lLength-1); // Hack off trailing #0
end;
{$ENDIF}

end.
