(*
/*

CLASS {NATIVE_Alien_windows|_Delphi},
NATIVE, DEPRECATED('Platform dependent'),
ABSTRACT :

NATIVE: "uses {$IFnDEF LINUX} Windows, {$ENDIF} NATIVE_Alien_windows;" .
*/
*)
unit NATIVE_Alien_windows;

interface

uses AL4 {$IFnDEF LINUX} , Windows, Messages {$ENDIF} ;

function Get_window_text(hWnd: Integer): String;
procedure List_desktop(List: DynAInt; Visible: Boolean);
function Get_window_class(hWnd: Integer): String;
procedure Get_loc(hWnd: Integer; Coord: DynAInt);
procedure List_child_windows(hWnd: Integer; List: DynAInt);
function Get_parent_window(hWnd: Integer): Integer;


implementation

function Get_window_text(hWnd: Integer): String;
{$IFnDEF LINUX}
var sz: Integer;
{$ENDIF}
begin
	{$IFnDEF LINUX}
    sz := SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0);
    SetLength(Result, sz);
    if sz > 0 then
       SendMessage(hWnd, WM_GETTEXT, sz, Integer( @Result[1] ));
    {$ENDIF}
end;

var handles: DynAInt;

{$IFDEF LINUX}
{$ELSE}
	{$IFDEF FPC}
		{$DEFINE FPCWIN}
	{$ENDIF}
{$ENDIF}

function Filter_desktop(Wnd: {$IFDEF FPCWIN} HWND {$ELSE} DWORD {$ENDIF} ;
	lPar: {$IFDEF FPCWIN} LPARAM {$ELSE} Integer {$ENDIF} 
	): {$IFDEF FPCWIN} BOOL {$ELSE} Boolean {$ENDIF}; stdcall;
begin
	{$IFnDEF LINUX}
    if (lPar = 0) or IsWindowVisible(Wnd) then handles.Add(MAXINT, Wnd);
    {$ENDIF}
    Result := TRUE;
end;

procedure List_desktop(List: DynAInt; Visible: Boolean);
begin
    handles := List;
    handles.Clear;
    {$IFnDEF LINUX}
    EnumWindows(@Filter_desktop, Integer(Visible));
    {$ENDIF}
end;

function Get_window_class(hWnd: Integer): String;
{$IFnDEF LINUX}
var buffer: array[0..255] of Char;
{$ENDIF}
begin
	{$IFDEF LINUX}
	Result := '';
	{$ELSE}
    GetClassName(hWnd, buffer, Sizeof(buffer));
    Result := buffer;
    {$ENDIF}
end;

procedure Get_loc(hWnd: Integer; Coord: DynAInt);
{$IFnDEF LINUX}
var R: TRect;
{$ENDIF}
begin
    Coord.Clear;
    {$IFnDEF LINUX}
    if GetWindowRect(hWnd, R) then
    begin
        Coord.Add(MAXINT, R.Left);
        Coord.Add(MAXINT, R.Top);
        Coord.Add(MAXINT, R.Right - R.Left);
        Coord.Add(MAXINT, R.Bottom - R.Top);
    end;
    {$ENDIF}
end;

{$IFnDEF LINUX}
function Filter_child(hWnd: {$IFDEF FPC} HWND {$ELSE} DWORD {$ENDIF} ; 
	lParam: {$IFDEF FPC} LPARAM {$ELSE} Integer {$ENDIF} ): BOOL; stdcall;
begin
    handles.Add(MAXINT, hWnd);
    Result := TRUE;
end;
{$ENDIF}

procedure List_child_windows(hWnd: Integer; List: DynAInt);
begin
    handles := List;
    handles.Clear;
    {$IFnDEF LINUX}
    EnumChildWindows(hWnd, @Filter_child, 0);
    {$ENDIF}
end;

function Get_parent_window(hWnd: Integer): Integer;
begin
    {$IFDEF LINUX}
	RESULT := 0
	{$ELSE}
    RESULT := GetParent(hWnd);
    {$ENDIF}
end;

(*
/*

CONST STR :
    NATIVE_Import_alien_windows = @
    "uses {$IFnDEF LINUX} Windows, Messages, {$ENDIF} NATIVE_Alien_windows;"

    NATIVE_Caption_fore = @
    "var handle: Integer;"
    "	{$IFnDEF LINUX}"     
    "   handle := GetForegroundWindow;"
    "   RESULT := NATIVE_Alien_windows.Get_window_text(handle);"
    "	{$ELSE}"
    "	RESULT := '';"
    "	{$ENDIF}"     
    
    NATIVE_Window_fore = @
    "	{$IFnDEF LINUX}"
    "   RESULT := GetForegroundWindow;"
    "	{$ENDIF}"     
    
    NATIVE_Find_window = @
    "	{$IFnDEF LINUX}"
    "   RESULT := FindWindow(PChar(X_Class), PChar(X_Caption));"
    "	{$ENDIF}"     
    
    NATIVE_List_windows = @
    "   NATIVE_Alien_windows.List_desktop(X_Array, X_Visible);"
    
    NATIVE_Is_window = @
    "	{$IFnDEF LINUX}"
    "   RESULT := IsWindow(X_Handle);"
    "	{$ENDIF}"     
    
    NATIVE_Is_visible_win = @
    "	{$IFnDEF LINUX}"
    "   RESULT := IsWindowVisible(X_Handle);"
    "	{$ENDIF}"     
    
    NATIVE_Is_maximized = @
    "	{$IFnDEF LINUX}"
    "   RESULT := IsZoomed(X_Handle);"
    "	{$ENDIF}"     
    
    NATIVE_Is_minimized = @
    "	{$IFnDEF LINUX}"
    "   RESULT := IsIconic(X_Handle);"
    "	{$ENDIF}"     
    
    NATIVE_Window_caption = @
    "	{$IFnDEF LINUX}"
    "   RESULT := NATIVE_Alien_windows.Get_window_text(X_Handle);"
    "	{$ENDIF}"     
    
    NATIVE_Window_class = @
    "	{$IFnDEF LINUX}"
    "   RESULT := NATIVE_Alien_windows.Get_window_class(X_Handle);"
    "	{$ENDIF}"     
    
    NATIVE_Window_loc = @
    "	{$IFnDEF LINUX}"
    "   NATIVE_Alien_windows.Get_loc(X_Handle, X_Coord);"
    "	{$ENDIF}"     
    
    NATIVE_Show_window = @
    "var flag: DWORD;"
    "	{$IFnDEF LINUX}"
    "   if X_Show then"
    "        if X_Activate then flag := SW_SHOW"
    "        else flag := SW_SHOWNOACTIVATE"
    "   else flag := SW_HIDE;"
    "   ShowWindow(X_Handle, flag);"
    "	{$ENDIF}"     
    
    NATIVE_Bring_to_front = @
    "	{$IFnDEF LINUX}"
    "   SetWindowPos(X_Handle, 0, 0, 0, 0, 0, SWP_NOACTIVATE or "
    "	SWP_NOMOVE or SWP_NOSIZE or SWP_SHOWWINDOW);"
    "	{$ENDIF}"     
    
    NATIVE_Set_window_loc = @
    "var flags: Integer;"
    "	{$IFnDEF LINUX}"
    "   flags := SWP_NOZORDER or SWP_NOACTIVATE or SWP_SHOWWINDOW;"
    "   if not X_XY then flags := flags or SWP_NOMOVE;"
    "   if (X_W <= 0) and (X_H <= 0) then flags := flags or SWP_NOSIZE;"
    "   SetWindowPos(X_Handle, 0,  X_X, X_Y, X_W, X_H, flags);"
    "	{$ENDIF}"     
    
    NATIVE_Minimize = @
    "	{$IFnDEF LINUX}"
    "   ShowWindow(X_Handle, SW_MINIMIZE);"
    "	{$ENDIF}"     
    
    NATIVE_Restore = @
    "	{$IFnDEF LINUX}"
    "   ShowWindow(X_Handle, SW_RESTORE);"
    "	{$ENDIF}"   
    
    NATIVE_Cancel_key = @
    "	{$IFnDEF LINUX}"
    "   PostMessage(X_Handle, WM_KEYDOWN, 27, 0);"
    "   PostMessage(X_Handle, WM_CHAR, $1B0001, 0);"
    "   PostMessage(X_Handle, WM_KEYUP, 27, 0);"
    "	{$ENDIF}"     

    NATIVE_Close_window = @
    "	{$IFnDEF LINUX}"
    "   PostMessage(X_Handle, WM_CLOSE, 0, 0);"
    "	{$ENDIF}"     
    
    NATIVE_Close_app = @
    "var pid: Pointer;"
    "var hProcess: DWORD;"
    "	{$IFnDEF LINUX}"
    "    GetWindowThreadProcessId(HWND(X_Handle), pid);"
    "    hProcess := OpenProcess(PROCESS_TERMINATE, FALSE, DWORD(pid));"
    "    if hProcess = 0 then Exit;"
    "    TerminateProcess(hProcess, 0);"
    "    CloseHandle(hProcess);"
    "	{$ENDIF}"     
    
	NATIVE_Set_foreground = @
    "	{$IFnDEF LINUX}"
	"	SetForegroundWindow(X_Handle);"
    "	{$ENDIF}"     
    
    NATIVE_List_child = @
    "   NATIVE_Alien_windows.List_child_windows(X_Handle, X_Child);"
    
    NATIVE_Parent_window = @
    "	RESULT := NATIVE_Alien_windows.Get_parent_window(X_Handle);"     
    
    .
        
    
END
*/
*)

end.
