
    ,y_3                        d dl Z d dl mZ d dlmZmZmZmZmZmZ d Z	dZ
dZdZdZdZd Zd	Zd
Zd ZdZ G d de j                  Ze j        j        j        Z e j        e j        e j         e j        e j                            Ze j        j        j        Z e j        j        j!        Z"e j        j        j#        Z$ G d de j                  Z%d Z&d Z'd Z(d Z)d Z*d Z+d Z,d Z-d Z. G d de          Z/d Z0d Z1dS )    N)wintypes)PyGetWindowExceptionpointInRect
BaseWindowRectPointSize   i   i            	      c                   2    e Zd Zdej        fdej        fgZdS )POINTxyN)__name__
__module____qualname__ctypesc_long_fields_     _/var/www/api.educacionweb.es/myenv/lib/python3.11/site-packages/pygetwindow/_pygetwindow_win.pyr   r       s)        fm$fm$&HHHr   r   c                   V    e Zd ZdZdej        fdej        fdej        fdej        fgZdS )RECTzA nice wrapper of the RECT structure.

    Microsoft Documentation:
    https://msdn.microsoft.com/en-us/library/windows/desktop/dd162897(v=vs.85).aspx
    lefttoprightbottomN)r   r   r   __doc__r   r   r   r   r   r   r   r   +   sI         
 '&&-(6=)+HHHr   r   c                  P    g fd} t          t          |           d           S )Nc                     t          |           rVt          |           }t          j        |dz             }t	          | ||dz                                  | |j        f           dS N   T)isWindowVisiblegetWindowTextLengthr   create_unicode_buffergetWindowTextappendvalue)hWndlParamlengthbufftitless       r   foreach_windowz%_getAllTitles.<locals>.foreach_window;   sh    4   	.(..F/
;;D$fqj111MM4,---tr   r   enumWindowsenumWindowsProc)r3   r2   s    @r   _getAllTitlesr7   7   sB     F     //333Mr   c                    t          j                    }t          j        j                            t          t          z  t          z  t          | dt          j
        t          j        |          t           j                  dt                     |j                                        }t          j        j                            |           |S )as  A nice wrapper for FormatMessageW(). TODO

    Microsoft Documentation:
    https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-formatmessagew

    Additional information:
    https://stackoverflow.com/questions/18905702/python-ctypes-and-mutable-buffers
    https://stackoverflow.com/questions/455434/how-should-i-use-formatmessage-properly-in-c
    r   )r   LPWSTRr   windllkernel32FormatMessageWFORMAT_MESSAGE_FROM_SYSTEMFORMAT_MESSAGE_ALLOCATE_BUFFERFORMAT_MESSAGE_IGNORE_INSERTSNULLcastbyrefr-   rstrip	LocalFree)	errorCodelpBuffermsgs      r   _formatMessagerH   G   s       H
M))*DGe*e  iF  +F*.*3*+*0+fl86L6Lho*^*^*+*.0 0 0 .


!
!C
M$$X...Jr   c                      t           j        j                                        } t	          d| dt          |                     )z{A helper function that raises PyGetWindowException using the error
    information from GetLastError() and FormatMessage().zError code from Windows: z - )r   r:   r;   GetLastErrorr   rH   )rE   s    r   _raiseWithLastErrorrK   _   sA     &3355I

YYYP^_hPiPiPij
k
kkr   c                  v    t           j        j                                        } | dk    rdS t	          |           S )zAReturns a Window object of the currently active (focused) Window.r   N)r   r:   user32GetForegroundWindowWin32Window)r.   s    r   getActiveWindowrP   f   s4    =3355Dqyyt4   r   c                      t           j        j                                        dk    rdS fd} t	          t          |           d           t          S )zLReturns a string of the title text of the currently active (focused) Window.r   Nc                     | k    rAt          |           }t          j        |dz             }t          | ||dz              |j        adS r&   )r)   r   r*   r+   r-   activeWindowTitle)r.   r/   r0   r1   activeWindowHwnds       r   r3   z,getActiveWindowTitle.<locals>.foreach_windowz   sS    ###(..F/
;;D$fqj111!%tr   )r   r:   rM   rN   r5   r6   rS   )r3   rT   s    @r   getActiveWindowTitlerU   p   se    
 }+??AA1t     //333r   c           	          g }t                      D ]?}t          | ||j        |j        |j        |j                  r|                    |           @|S )zReturns a list of Window objects whose windows contain the point ``(x, y)``.

    * ``x`` (int, optional): The x position of the window(s).
    * ``y`` (int, optional): The y position of the window(s).)getAllWindowsr   r   r    widthheightr,   )r   r   windowsAtXYwindows       r   getWindowsAtr\      sY    
 K// ' 'q!V[&*flFMRR 	'v&&&r   c                     t                      }g }|D ]O\  }}|                                 |                                v r"|                    t          |                     P|S )zTReturns a list of Window objects that substring match ``title`` in their title text.)r7   upperr,   rO   )titlehWndsAndTitles
windowObjsr.   winTitles        r   getWindowsWithTitlerc      sc    "__NJ( 1 1h;;==HNN,,,,k$//000r   c                  2    d t                      D             S )zHReturns a list of strings of window titles for all visible windows.
    c                     g | ]	}|j         
S r   )r_   ).0r[   s     r   
<listcomp>z getAllTitles.<locals>.<listcomp>   s    777VFL777r   )rW   r   r   r   getAllTitlesrh      s     87}7777r   c                  P    g fd} t          t          |           d           S )z>Returns a list of Window objects for all visible windows.
    c                     t           j        j                            |           dk    r"                    t          |                      dS )Nr   T)r   r:   rM   IsWindowVisibler,   rO   )r.   r/   ra   s     r   r3   z%getAllWindows.<locals>.foreach_window   sB    =//55::k$//000tr   r   r4   )r3   ra   s    @r   rW   rW      sC     J     //333r   c                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd ZeZd Zd ZeZd Zed             Zed             Zed             Zed             Zed             ZdS )rO   c                 <    || _         |                                  d S N)_hWnd_setupRectProperties)selfr.   s     r   __init__zWin32Window.__init__   s!    
!!#####r   c                    t                      }t          j        j                            | j        t          j        |                    }|dk    r&t          |j        |j	        |j
        |j                  S t                       dS )a  A nice wrapper for GetWindowRect(). TODO

        Syntax:
        BOOL GetWindowRect(
          HWND   hWnd,
          LPRECT lpRect
        );

        Microsoft Documentation:
        https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowrect
        r   N)r   r   r:   rM   GetWindowRectro   rB   r   r   r    r!   r"   rK   )rq   rectresults      r   _getWindowRectzWin32Window._getWindowRect   sj     vv%33DJT@R@RSSQ;;	48TZEEE!!!!!r   c                 0    | j         j        d| j        dS )Nz(hWnd=))	__class__r   ro   rq   s    r   __repr__zWin32Window.__repr__   s     $ 7 7 7DDr   c                 L    t          |t                    o| j        |j        k    S rn   )
isinstancerO   ro   )rq   others     r   __eq__zWin32Window.__eq__   s     %--K$*2KKr   c                     t           j        j                            | j        t
          dd          }|dk    rt                       dS dS )zCloses this window. This may trigger "Are you sure you want to
        quit?" dialogs or other actions that prevent the window from
        actually closing. This is identical to clicking the X button on the
        window.r   N)r   r:   rM   PostMessageAro   WM_CLOSErK   rq   rv   s     r   closezWin32Window.close   sF    
 %224:xANNQ;;!!!!! ;r   c                 d    t           j        j                            | j        t
                     dS )zMinimizes this window.N)r   r:   rM   
ShowWindowro   SW_MINIMIZEr{   s    r   minimizezWin32Window.minimize   %    ''
K@@@@@r   c                 d    t           j        j                            | j        t
                     dS )zMaximizes this window.N)r   r:   rM   r   ro   SW_MAXIMIZEr{   s    r   maximizezWin32Window.maximize   r   r   c                 d    t           j        j                            | j        t
                     dS )zCIf maximized or minimized, restores the window to it's normal size.N)r   r:   rM   r   ro   
SW_RESTOREr{   s    r   restorezWin32Window.restore   s%    ''
J?????r   c                 d    t           j        j                            | j        t
                     dS )zBIf hidden or showing, shows the window on screen and in title bar.N)r   r:   rM   r   ro   SW_SHOWr{   s    r   showzWin32Window.show   %    ''
7;;;;;r   c                 d    t           j        j                            | j        t
                     dS )zAIf hidden or showing, hides the window from screen and title bar.N)r   r:   rM   r   ro   SW_HIDEr{   s    r   hidezWin32Window.hide   r   r   c                     t           j        j                            | j                  }|dk    rt                       dS dS )zAActivate this window and make it the foreground (focused) window.r   N)r   r:   rM   SetForegroundWindowro   rK   r   s     r   activatezWin32Window.activate   s>    %99$*EEQ;;!!!!! ;r   c           	          t           j        j                            | j        t
          | j        | j        | j        |z   | j	        |z   d          }|dk    rt                       dS dS )z0Resizes the window relative to its current size.r   Nr   r:   rM   SetWindowPosro   HWND_TOPr   r    rX   rY   rK   )rq   widthOffsetheightOffsetrv   s       r   resizezWin32Window.resize   s{    %224:xTXT\^b^hkv^vx|  yD  GS  yS  UV  W  WQ;;!!!!! ;r   c           	          t           j        j                            | j        t
          | j        | j        ||d          }|dk    rt                       dS dS )z-Resizes the window to a new width and height.r   N)	r   r:   rM   r   ro   r   r   r    rK   )rq   newWidth	newHeightrv   s       r   resizeTozWin32Window.resizeTo   sS    %224:xTXT\^fhqstuuQ;;!!!!! ;r   c           	          t           j        j                            | j        t
          | j        |z   | j        |z   | j        | j	        d          }|dk    rt                       dS dS )z2Moves the window relative to its current position.r   Nr   )rq   xOffsetyOffsetrv   s       r   movezWin32Window.move  sw    %224:xU\I\^b^fip^prvr|  C  J  LM  N  NQ;;!!!!! ;r   c           	          t           j        j                            | j        t
          ||| j        | j        d          }|dk    rt                       dS dS )z2Moves the window to new coordinates on the screen.r   N)	r   r:   rM   r   ro   r   rX   rY   rK   )rq   newLeftnewToprv   s       r   moveTozWin32Window.moveTo  sT    %224:xRXZ^ZdfjfqstuuQ;;!!!!! ;r   c                 \    t           j        j                            | j                  dk    S )z6Returns ``True`` if the window is currently minimized.r   )r   r:   rM   IsIconicro   r{   s    r   isMinimizedzWin32Window.isMinimized  $     }#,,TZ88A==r   c                 \    t           j        j                            | j                  dk    S )z6Returns ``True`` if the window is currently maximized.r   )r   r:   rM   IsZoomedro   r{   s    r   isMaximizedzWin32Window.isMaximized  r   r   c                 &    t                      | k    S )zJReturns ``True`` if the window is currently the active, foreground window.)rP   r{   s    r   isActivezWin32Window.isActive  s       D((r   c                     t           j        j                            | j                  }t          j        |dz             }t           j        j                            | j        ||dz              |j        S )z%Returns the window title as a string.r'   )r   r:   rM   GetWindowTextLengthWro   r*   GetWindowTextWr-   )rq   textLenInCharactersstringBuffers      r   r_   zWin32Window.title$  sf     %m2GG
SS34G!4KLL++DJFY\]F]^^^ !!r   c                 *    t          | j                  S )z3Return ``True`` if the window is currently visible.)r(   ro   r{   s    r   visiblezWin32Window.visible.  s     tz***r   N)r   r   r   rr   rw   r|   r   r   r   r   r   r   r   r   r   	resizeRelr   r   moveRelr   propertyr   r   r   r_   r   r   r   r   rO   rO      s       $ $ $
" " "(E E EL L L" " "A A A
A A A
@ @ @< < << < <" " "" " "
 I" " "" " "
 G" " " > > X> > > X> ) ) X) " " X" + + X+ + +r   rO   c                      t                      } t          j        j                            t          j        |                      t          | j        | j                  S )zReturns the current xy coordinates of the mouse cursor as a two-integer
    tuple by calling the GetCursorPos() win32 function.

    Returns:
      (x, y) tuple of the current xy coordinates of the mouse cursor.
    )r   r   )	r   r   r:   rM   GetCursorPosrB   r   r   r   )cursors    r   r   r   4  sG     WWF
M%%fl6&:&:;;;68vx((((r   c                      t          t          j        j                            d          t          j        j                            d                    S )zReturns the width and height of the screen as a two-integer tuple.

    Returns:
      (width, height) tuple of the screen size, in pixels.
    r   r'   )rX   rY   )r	   r   r:   rM   GetSystemMetricsr   r   r   
resolutionr   A  s>     fm*;;A>>v}G[GlGlmnGoGoppppr   )2r   r   pygetwindowr   r   r   r   r   r	   r@   r>   r=   r?   r   r   r   r   r   r   r   	Structurer   r:   rM   EnumWindowsr5   WINFUNCTYPEc_boolc_intPOINTERr6   r   r+   r   r)   rk   r(   r   r7   rH   rK   rP   rU   r\   rc   rh   rW   rO   r   r   r   r   r   <module>r      s7          X X X X X X X X X X X X X X X X 	 ", '  *  


  
& & & & &F & & & m".$&$V]FL.&.QWQ]B^B^__$3m*? -&6	+ 	+ 	+ 	+ 	+6 	+ 	+ 	+     0l l l! ! !  .	 	 	  8 8 8
 
 
A+ A+ A+ A+ A+* A+ A+ A+H
) 
) 
)q q q r   