a
    If%                     @   s   d Z ddlmZ ddlZddlmZ ddlZda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dd Zedkrdd lmZ ed!d"d# dS )$z3
A number of functions that enhance IDLE on macOS.
    )
expanduserN)platformc                  C   sj   t dkrbt } | jdd}d|v r,dan,d|vr:dand| jd	d
dv rTdanda|   ndadS )zl
    Initializes OS X Tk variant values for
    isAquaTk(), isCarbonTk(), isCocoaTk(), and isXQuartz().
    darwintkZwindowingsystemZx11xquartzZaquaotherZAppKitZwinfoZserver.cocoacarbonN)r   tkinterZTkr   call_tk_typeZdestroy)rootZws r   lib/python3.9/idlelib/macosx.py_init_tk_type   s    
r   c                   C   s   t s
t  t dkpt dkS )zK
    Returns True if IDLE is using a native OS X Tk (Cocoa or Carbon).
    r	   r
   r   r   r   r   r   r   isAquaTk%   s    r   c                   C   s   t s
t  t dkS )zb
    Returns True if IDLE is using a Carbon Aqua Tk (instead of the
    newer Cocoa Aqua Tk).
    r
   r   r   r   r   r   
isCarbonTk-   s    r   c                   C   s   t s
t  t dkS )z8
    Returns True if IDLE is using a Cocoa Aqua Tk.
    r	   r   r   r   r   r   	isCocoaTk6   s    r   c                   C   s   t s
t  t dkS )z7
    Returns True if IDLE is using an OS X X11 Tk.
    r   r   r   r   r   r   	isXQuartz>   s    r   c                 C   s2   t  r*| jdd}|dvr dS d|S dS dS )a2  
    Returns a string warning message if the Tk version in use appears to
    be one known to cause problems with IDLE.
    1. Apple Cocoa-based Tk 8.5.7 shipped with Mac OS X 10.6 is unusable.
    2. Apple Cocoa-based Tk 8.5.9 in OS X 10.7 and 10.8 is better but
        can still crash unexpectedly.
    info
patchlevel)z8.5.7z8.5.9FzWARNING: The version of Tcl/Tk ({0}) in use may be unstable.
Visit https://www.python.org/download/mac/tcltk/ for current information.N)r   r   r   format)r   r   r   r   r   tkVersionWarningG   s    	r   c                  C   sh   t dkrdS td} z:t| d}t|W  d   W S 1 sB0    Y  W n tyb   Y dS 0 dS )z-
    Fetch the macOS system preferences.
    r   Nz.~/Library/Preferences/.GlobalPreferences.plistrb)r   r   openplistlibloadOSError)Z
plist_pathZ
plist_filer   r   r   readSystemPreferences\   s    .r    c                  C   s,   t dkrdS t } | r(| ddkr(dS dS )zJ
    Warn if "Prefer tabs when opening documents" is set to "Always".
    r   NZAppleWindowTabbingModealwayszWARNING: The system preference "Prefer tabs when opening documents" is set to "Always". This will cause various problems with IDLE. For the best experience, change this setting when running IDLE (via System Preferences -> Dock).)r   r    get)Zprefsr   r   r   preferTabsPreferenceWarningk   s    r#   c                    s    fdd}|  d| dS )z
    This ensures that the application will respond to open AppleEvents, which
    makes is feasible to use IDLE as the default application for python files.
    c                     s   | D ]}  | qd S )N)r   )argsfnflistr   r   
doOpenFile   s    z'addOpenEventSupport.<locals>.doOpenFilez::tk::mac::OpenDocumentN)createcommand)r   r'   r(   r   r&   r   addOpenEventSupport   s    r*   c                 C   s,   z| j dd W n tjy&   Y n0 d S )NZconsoleZhide)r   r   r   ZTclErrorr   r   r   r   hideTkConsole   s    r,   c                    s  ddl m} ddlm} ddlm |jd d d }|jd d dd= |jd d d	| |jd
 d dd= |jd d dd= |}j|d i }||ddd |d< }|jd|dd |ffdd	}	| d%fdd	}	d& fdd	}
d'fdd	}
d|	 
d|
 d|
  rR
d j d j t r||ddd |d< }|jd |d! |jddd"dgf t rĈd#|	 d$| |jd
 d d= dS )(zf
    Replace the Tk root menu by something that is more appropriate for
    IDLE with an Aqua Tk.
    r   )Menu)mainmenuwindow   N      )menur0   )nameZtearoffZWindow)labelr7   Z	underlinec                    s8   |  d}|d u rd}|dkr*| d|  |  d S )Nendr5   r   )indexdeleteZadd_windows_to_menu)r7   r:   r/   r   r   postwindowsmenu   s    
z)overrideRootMenu.<locals>.postwindowsmenuc                    s   ddl m} |  dS )zHandle Help 'About IDLE' event.r   )
help_aboutN)idlelibr>   ZAboutDialog)eventr>   r+   r   r   about_dialog   s    z&overrideRootMenu.<locals>.about_dialogc                    s$   ddl m}  j_|d dS )z&Handle Options 'Configure IDLE' event.r   )configdialogZSettingsN)r?   rB   ZinversedictZinstance_dictZConfigDialog)r@   rB   )r'   r   r   r   config_dialog   s    z'overrideRootMenu.<locals>.config_dialogc                    s   ddl m} |  dS )zHandle Help 'IDLE Help' event.r   )helpN)r?   rD   Zshow_idlehelp)r@   rD   r+   r   r   help_dialog   s    z%overrideRootMenu.<locals>.help_dialog<<about-idle>>z<<open-config-dialog>>z::tk::mac::ShowPreferencesz<<close-all-windows>>exitZappleZapplicationZIDLE)r9   r7   )z
About IDLErF   ZtkAboutDialogz::tk::mac::ShowHelp)N)N)N)r   r-   r?   r.   r0   ZmenudefsinsertZ	configureZadd_cascadeZregister_callbackZbindr)   Zclose_all_callbackr   r   )r   r'   r-   r.   Z	closeItemZmenubarZmenudictr7   r=   rA   rC   rE   r   )r'   r   r0   r   overrideRootMenu   sN    
rI   c                 C   s(   |  dd |  dd |  dd dS )zRemoved bad AquaTk Button-2 (right) and Paste bindings.

    They prevent context menu access and seem to be gone in AquaTk8.6.
    See issue #24801.
    Textz<B2>z<B2-Motion>z<<PasteSelection>>N)Zunbind_classr+   r   r   r   fixb2context   s    rK   c                 C   s.   t  r*t|  t| | t| | t|  dS )a  
    Perform initial OS X customizations if needed.
    Called from pyshell.main() after initial calls to Tk()

    There are currently three major versions of Tk in use on OS X:
        1. Aqua Cocoa Tk (native default since OS X 10.6)
        2. Aqua Carbon Tk (original native, 32-bit only, deprecated)
        3. X11 (supported by some third-party distributors, deprecated)
    There are various differences among the three that affect IDLE
    behavior, primarily with menus, mouse key events, and accelerators.
    Some one-time customizations are performed here.
    Others are dynamically tested throughout idlelib by calls to the
    isAquaTk(), isCarbonTk(), isCocoaTk(), isXQuartz() functions which
    are initialized here as well.
    N)r   r,   rI   r*   rK   )r   r'   r   r   r   setupApp  s
    

rL   __main__)mainzidlelib.idle_test.test_macosxr6   )	verbosity)__doc__os.pathr   r   sysr   r   r   r   r   r   r   r   r   r    r#   r*   r,   rI   rK   rL   __name__ZunittestrN   r   r   r   r   <module>   s*   		h
