o
    &h3                     @   s2  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 de jZe jjjZe e je je e jZe jjjZ e jjj!Z"e jjj#Z$G dd de jZ%dd Z&dd Z'dd Z(dd Z)dd Z*dd Z+dd Z,dd Z-d d! Z.G d"d# d#eZ/d$d% Z0d&d' Z1dS )(    N)wintypes)PyGetWindowExceptionpointInRect
BaseWindowRectPointSize   i   i            	      c                   @   s    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_ r   r   s/Users/merlin/projects/employee-monitoring-system/venv/lib/python3.10/site-packages/pygetwindow/_pygetwindow_win.pyr       s    r   c                   @   s4   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   +   s    r   c                     "   g   fdd} t t| d  S )Nc                    sB   t | rt| }t|d }t| ||d   | |jf dS N   T)isWindowVisiblegetWindowTextLengthr   create_unicode_buffergetWindowTextappendvaluehWndlParamlengthZbufftitlesr   r   foreach_window;   s   z%_getAllTitles.<locals>.foreach_windowr   enumWindowsenumWindowsProcr/   r   r-   r   _getAllTitles7   s   r4   c                 C   sV   t  }tjjttB tB t	| dt
t|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   ZLPWSTRr   windllkernel32ZFormatMessageWFORMAT_MESSAGE_FROM_SYSTEMFORMAT_MESSAGE_ALLOCATE_BUFFERFORMAT_MESSAGE_IGNORE_INSERTSNULLcastbyrefr(   rstripZ	LocalFree)	errorCodeZlpBuffermsgr   r   r   _formatMessageG   s   

r@   c                  C   s    t jj } td| t| f )z{A helper function that raises PyGetWindowException using the error
    information from GetLastError() and FormatMessage().z Error code from Windows: %s - %s)r   r5   r6   ZGetLastErrorr   r@   )r>   r   r   r   _raiseWithLastError_   s   rA   c                  C   s    t jj } | dkrdS t| S )zAReturns a Window object of the currently active (focused) Window.r   N)r   r5   user32GetForegroundWindowWin32Window)r*   r   r   r   getActiveWindowf   s   rE   c                     s6   t jj   dkrdS  fdd} tt| d tS )zLReturns a string of the title text of the currently active (focused) Window.r   Nc                    s8   |  krt | }t|d }t| ||d  |jadS r!   )r$   r   r%   r&   r(   activeWindowTitler)   ZactiveWindowHwndr   r   r/   z   s   z,getActiveWindowTitle.<locals>.foreach_window)r   r5   rB   rC   r1   r2   rF   r3   r   rG   r   getActiveWindowTitlep   s   rH   c                 C   s8   g }t  D ]}t| ||j|j|j|jr|| q|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   ZwindowsAtXYwindowr   r   r   getWindowsAt   s   

rM   c                 C   s:   t  }g }|D ]\}}|  | v r|t| q|S )zTReturns a list of Window objects that substring match ``title`` in their title text.)r4   upperr'   rD   )titleZhWndsAndTitles
windowObjsr*   ZwinTitler   r   r   getWindowsWithTitle   s   rQ   c                   C   s   dd t  D S )zHReturns a list of strings of window titles for all visible windows.
    c                 S   s   g | ]}|j qS r   )rO   ).0rL   r   r   r   
<listcomp>   s    z getAllTitles.<locals>.<listcomp>)rI   r   r   r   r   getAllTitles   s   rT   c                     r    )z>Returns a list of Window objects for all visible windows.
    c                    s$   t jj| dkr t|  dS )Nr   T)r   r5   rB   IsWindowVisibler'   rD   )r*   r+   rP   r   r   r/      s   z%getAllWindows.<locals>.foreach_windowr   r0   r3   r   rV   r   rI      s   rI   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd ZeZdd Zdd ZeZdd Zedd  Zed!d" Zed#d$ Zed%d& Zed'd( Zd)S )*rD   c                 C   s   || _ |   d S N)_hWndZ_setupRectProperties)selfr*   r   r   r   __init__   s   zWin32Window.__init__c                 C   sF   t  }tjj| jt|}|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   r5   rB   ZGetWindowRectrX   r<   r   r   r   r   r   rA   )rY   rectresultr   r   r   _getWindowRect   s
   
zWin32Window._getWindowRectc                 C   s   d| j j| jf S )Nz%s(hWnd=%s))	__class__r   rX   rY   r   r   r   __repr__   s   zWin32Window.__repr__c                 C   s   t |to
| j|jkS rW   )
isinstancerD   rX   )rY   otherr   r   r   __eq__   s   zWin32Window.__eq__c                 C   s,   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   r5   rB   ZPostMessageArX   WM_CLOSErA   rY   r\   r   r   r   close   s   
zWin32Window.closec                 C      t jj| jt dS )zMinimizes this window.N)r   r5   rB   
ShowWindowrX   SW_MINIMIZEr_   r   r   r   minimize      zWin32Window.minimizec                 C   rg   )zMaximizes this window.N)r   r5   rB   rh   rX   SW_MAXIMIZEr_   r   r   r   maximize   rk   zWin32Window.maximizec                 C   rg   )zCIf maximized or minimized, restores the window to it's normal size.N)r   r5   rB   rh   rX   
SW_RESTOREr_   r   r   r   restore   rk   zWin32Window.restorec                 C   rg   )zBIf hidden or showing, shows the window on screen and in title bar.N)r   r5   rB   rh   rX   SW_SHOWr_   r   r   r   show   rk   zWin32Window.showc                 C   rg   )zAIf hidden or showing, hides the window from screen and title bar.N)r   r5   rB   rh   rX   SW_HIDEr_   r   r   r   hide   rk   zWin32Window.hidec                 C   s&   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   r5   rB   ZSetForegroundWindowrX   rA   re   r   r   r   activate   s   
zWin32Window.activatec              	   C   sB   t jj| jt| j| j| j| | j	| d}|dkrt
  dS dS )z0Resizes the window relative to its current size.r   Nr   r5   rB   SetWindowPosrX   HWND_TOPr   r   rJ   rK   rA   )rY   ZwidthOffsetZheightOffsetr\   r   r   r   resize      ,
zWin32Window.resizec              	   C   s6   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   r5   rB   rv   rX   rw   r   r   rA   )rY   ZnewWidthZ	newHeightr\   r   r   r   resizeTo       
zWin32Window.resizeToc              	   C   sB   t jj| jt| j| | j| | j| j	d}|dkrt
  dS dS )z2Moves the window relative to its current position.r   Nru   )rY   ZxOffsetZyOffsetr\   r   r   r   move  ry   zWin32Window.movec              	   C   s6   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   r5   rB   rv   rX   rw   rJ   rK   rA   )rY   ZnewLeftZnewTopr\   r   r   r   moveTo  r{   zWin32Window.moveToc                 C      t jj| jdkS )z6Returns ``True`` if the window is currently minimized.r   )r   r5   rB   ZIsIconicrX   r_   r   r   r   isMinimized     zWin32Window.isMinimizedc                 C   r~   )z6Returns ``True`` if the window is currently maximized.r   )r   r5   rB   ZIsZoomedrX   r_   r   r   r   isMaximized  r   zWin32Window.isMaximizedc                 C   s
   t  | kS )zJReturns ``True`` if the window is currently the active, foreground window.)rE   r_   r   r   r   isActive     
zWin32Window.isActivec                 C   s<   t jj| j}t |d }t jj| j||d  |jS )z%Returns the window title as a string.r"   )r   r5   rB   GetWindowTextLengthWrX   r%   GetWindowTextWr(   )rY   ZtextLenInCharactersZstringBufferr   r   r   rO   $  s   zWin32Window.titlec                 C   s
   t | jS )z3Return ``True`` if the window is currently visible.)r#   rX   r_   r   r   r   visible.  r   zWin32Window.visibleN)r   r   r   rZ   r]   r`   rc   rf   rj   rm   ro   rq   rs   rt   rx   Z	resizeRelrz   r|   ZmoveRelr}   propertyr   r   r   rO   r   r   r   r   r   rD      s8    




	rD   c                  C   s*   t  } tjjt|  t| j| jd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   r5   rB   ZGetCursorPosr<   r   r   r   )cursorr   r   r   r   4  s   r   c                   C   s    t tjjdtjjd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"   )rJ   rK   )r   r   r5   rB   ZGetSystemMetricsr   r   r   r   
resolutionA  s    r   )2r   r   Zpygetwindowr   r   r   r   r   r   r:   r8   r7   r9   ri   rl   rr   rp   rn   rw   rd   Z	Structurer   r5   rB   ZEnumWindowsr1   ZWINFUNCTYPEZc_boolc_intZPOINTERr2   r   r&   r   r$   rU   r#   r   r4   r@   rA   rE   rH   rM   rQ   rT   rI   rD   r   r   r   r   r   r   <module>   sF     





 