/*

CLASS {NATIVE_Alien_windows|_Cpp}, NATIVE:

*/

#ifdef SEPARATE_UNITS
    #include "NATIVE_Alien_windows.h"
#endif

std::string AL4_alien_windows::Get_window_text(HWND hWnd)
{
    int sz = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0);
    TCHAR* txt = new TCHAR[sz+1];
    txt[0] = '\0';
    if (sz > 0)
        SendMessage(hWnd, WM_GETTEXT, sz, (int)&txt[0]);
    std::string RESULT = txt;
    delete[] txt;
    return RESULT;
}

ArrayInt* AL4_alien_windows::handles;

BOOL AL4_alien_windows::Filter_desktop(HWND hWnd, LPARAM lParam)
{
    if ((lParam == 0) || IsWindowVisible(hWnd)) handles->Add((int)hWnd);
    return TRUE;
}

void AL4_alien_windows::List_desktop(ArrayInt* List, bool Visible)
{
    handles = List;
    handles->Clear();
    EnumWindows(Filter_desktop, (int)Visible);
}

std::string AL4_alien_windows::Get_window_class(HWND hWnd)
{
    TCHAR buffer[256]; 
    GetClassName(hWnd, &buffer[0], sizeof(buffer));
    return buffer;
}

void AL4_alien_windows::Get_loc(HWND hWnd, ArrayInt* Coord)
{
    RECT R;
    Coord->Clear();
    if (GetWindowRect(hWnd, &R))
    {
        Coord->Add(R.left);
        Coord->Add(R.top);
        Coord->Add(R.right - R.left);
        Coord->Add(R.bottom - R.top);
    }
}

BOOL AL4_alien_windows::Filter_child(HWND hWnd, LPARAM lParam)
{
    handles->Add((int)hWnd);
    return TRUE;
}

void AL4_alien_windows::List_child_windows(HWND hWnd, ArrayInt* List)
{
    handles = List;
    handles->Clear();
    EnumChildWindows(hWnd, Filter_child, 0);
}

/*
    
CONST STR:
    NATIVE_Import_alien_windows = @
    "#include <windows.h>"

    NATIVE_Caption_fore = @
    "   HWND handle = GetForegroundWindow();"
    "   RESULT = AL4_alien_windows::Get_window_text(handle);"
    
    NATIVE_Window_fore = @
    "   RESULT = (int)GetForegroundWindow();"
    
    NATIVE_Find_window = @
    "   RESULT = (int)FindWindow(X_Class, X_Caption);"
    
    NATIVE_List_windows = @
    "   AL4_alien_windows::List_desktop(X_Array, X_Visible);"
    
    NATIVE_Is_window = @
    "   RESULT = IsWindow((HWND)X_Handle);"
    
    NATIVE_Is_visible_win = @
    "   RESULT = IsWindowVisible((HWND)X_Handle);"
    
    NATIVE_Is_maximized = @
    "   RESULT = IsZoomed((HWND)X_Handle);"
    
    NATIVE_Is_minimized = @
    "   RESULT = IsIconic((HWND)X_Handle);"
    
    NATIVE_Window_caption = @
    "   RESULT = AL4_alien_windows::Get_window_text((HWND)X_Handle);"
    
    NATIVE_Window_class = @
    "   RESULT = AL4_alien_windows::Get_window_class((HWND)X_Handle);"
    
    NATIVE_Window_loc = @
    "   AL4_alien_windows::Get_loc((HWND)X_Handle, X_Coord);"
    
    NATIVE_Show_window = @
    "   int flag;"
    "   if (X_Show)"
    "        if (X_Activate) flag = SW_SHOW;"
    "        else flag = SW_SHOWNOACTIVATE;"
    "   else flag = SW_HIDE;"
    "   ShowWindow((HWND)X_Handle, flag);"
    
    NATIVE_Bring_to_front = @
    "   BringWindowToTop((HWND)X_Handle);"
    
    NATIVE_Set_window_loc = @
    "   int flags;"
    "   flags = SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW;"
    "   if (!X_XY) flags |= SWP_NOMOVE;"
    "   if ((X_W <= 0) && (X_H <= 0)) flags |= SWP_NOSIZE;"
    "   SetWindowPos((HWND)X_Handle, 0,  X_X, X_Y, X_W, X_H, flags);"
    
    NATIVE_Minimize = @
    "   ShowWindow((HWND)X_Handle, SW_MINIMIZE);"
    
    NATIVE_Restore = @
    "   ShowWindow((HWND)X_Handle, SW_RESTORE);"
    
    NATIVE_Close_window = @
    "   PostMessage((HWND)X_Handle, WM_CLOSE, 0, 0);"
    
    NATIVE_Close_app = @
    "    DWORD pid;"
    "    GetWindowThreadProcessId((HWND)X_Handle, &pid);"
    "    HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid);"
    "    if (hProcess == 0) return;"
    "    TerminateProcess(hProcess, 0);"
    "    CloseHandle(hProcess);"
    
    NATIVE_List_child = @
    "   AL4_alien_windows::List_child_windows((HWND)X_Handle, X_Child);"
    
    .
        

END
*/
