
    ^j<[                        d Z ddlmZ ddlZddlZddlmZmZmZ ddl	m
Z
mZ ddlmZ ddlmZ erddlmZ dd	lmZ dd
lmZ  G d de          Ze                    d           e                    d           dS )a<  Provides keyboard and mouse editing procedures for text layout.

Example usage::

    from pyglet import window
    from pyglet.text import layout, caret

    my_window = window.Window(...)
    my_layout = layout.IncrementalTextLayout(...)
    my_caret = caret.Caret(my_layout)
    my_window.push_handlers(my_caret)
    )annotationsN)TYPE_CHECKINGAnyPattern)clockevent)key)EventDispatcher)Batch)Window)IncrementalTextLayoutc                     e Zd ZU dZ ej        d          Zded<    ej        d          Zded<    ej        dej	                  Z
ded	<    ej        dej	                  Zded
<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   ded<   	 	 dgdhd*Zedid+            Zej        djd,            Zdkd-Zdld/Zdkd0Zedmd1            Zej        dnd3            Zedod4            Zej        dpd6            Zedqd7            Zej        drd9            Zedqd:            Zej        dsd<            Zedqd=            Z e j        dtd?            Z dudCZ!dvdEZ"dkdFZ#dwdIZ$dwdJZ%dkdKZ&dwdLZ'dwdMZ(dxdydOZ)dkdPZ*dkdQZ+dzdSZ,d{d|dWZ-d}dXZ.d~d[Z/dd^Z0ddbZ1dmdcZ2dmddZ3dzdeZ4dzdfZ5dS )Careta  Visible text insertion marker for `pyglet.text.layout.IncrementalTextLayout`.

    The caret is drawn as a single vertical bar at the document `position`
    on a text layout object.  If ``mark`` is not None, it gives the unmoving
    end of the current text selection.  The visible text selection on the
    layout is updated along with ``mark`` and ``position``.

    By default, the layout's graphics batch is used, so the caret does not need
    to be drawn explicitly.  Even if a different graphics batch is supplied,
    the caret will be correctly positioned and clipped within the layout.

    Updates to the document (and so the layout) are automatically propagated
    to the caret.

    If the window argument is supplied, the caret object dispatches the on_clipboard_copy event when copying text and copies the text.
    Pasting also works, which will dispatch the on_clipboard_paste event, and pastes the text to the current position of the caret, overriding selection.

    The caret object can be pushed onto a window event handler stack with
    ``Window.push_handlers``.  The caret will respond correctly to keyboard,
    text, mouse and activation events, including double- and triple-clicks.
    If the text layout is being used alongside other graphical widgets, a
    GUI toolkit will be needed to delegate keyboard and mouse events to the
    appropriate widget.  Pyglet does not provide such a toolkit at this stage.
    z	(?<=\W)\wzPattern[str]_next_word_rez(?<=\W)\w+\W*$_previous_word_rez\n)flags_next_para_re_previous_para_rer   int	_positionTbool_active_visible_blink_visible_click_countfloat_click_timeg      ?PERIOD   SCROLL_INCREMENTN
int | None_markdict[str, Any]_next_attributesr   r   r      layoutr   batchBatch | Nonecolortuple[int, int, int, int]windowWindow | NonereturnNonec                   ddl m} || _        |du| _        |p|j        | _        |j        | _        |^}}}}	|	r|	d         nd| _        |||| j        |||| j        f}
| j        j	        
                    d|j        | j        | j        d|
fd          | _        d| _        d| _        i | _        d	| _        || _        |                    |            dS )
a  Create a caret for a layout.

        By default the layout's batch is used, so the caret does not need to
        be drawn explicitly.

        :Parameters:
            `layout` : `~pyglet.text.layout.IncrementalTextLayout`
                Layout to control.
            `batch` : `~pyglet.graphics.Batch`
                Graphics batch to add vertices to.
            `color` : (int, int, int, int)
                An RGBA or RGB tuple with components in the range [0, 255].
                RGB colors will be treated as having an opacity of 255.
            `window` : `~pyglet.window.Window`
                For the clipboard feature to work, a window object is needed to be passed in to access and set clipboard content.
        r   )glNr&      Bn)f)   r5   )colorsvisibleT)pygletr1   _layout_custom_batchr(   _batchforeground_decoration_group_group_visible_alphaprogramvertex_listGL_LINES_list_ideal_x_ideal_liner$   r7   _windowpush_handlers)selfr'   r(   r*   r,   r1   rgbar6   s              L/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/text/caret.py__init__zCaret.__init__L   s   $ 	"$.+v|8 1a! '(0addSAq$-q!Q8KK[(44QT[RVR]=A6N=J 5 L L
  "T"""""    c                    | j         S N)r9   rG   s    rL   r'   zCaret.layouty   s
    |rN   c                    | j         |k    r| j        |j        k    rd S ddlm} || _         | j        r| j        n|j        }|j        | _        | j        	                    | j
        || j        |           d S )Nr   )rA   )r9   r=   group	pyglet.glrA   r:   r;   r(   r<   migraterB   )rG   r'   rA   r(   s       rL   r'   zCaret.layout}   s    <6!!dkV\&A&AF&&&&&&#1Cv|8DJ$+uEEEEErN   c                    t          j        | j                   | j                                         | j                            |            dS )ziRemove the caret from its batch.

        Also disconnects the caret from further layout events.
        N)r   
unschedule_blinkrB   deleter9   remove_handlersrQ   s    rL   rY   zCaret.delete   sG    
 	%%%
$$T*****rN   dtc                    | j         r| j         | _        | j        r| j        r| j        r| j        }nd}|| j        j        d<   || j        j        d<   d S )Nr         )r   r   r   r   r>   rB   r6   )rG   r[   alphas      rL   rX   zCaret._blink   sl    ; 	:&*&9"9D= 	T\ 	d.A 	'EEE  %
!$
!rN   c                    d| _         d S NT)r7   rQ   s    rL   _nudgezCaret._nudge   s    rN   c                    | j         S )zCaret visibility.

        The caret may be hidden despite this property due to the periodic blinking
        or by `on_deactivate` if the event handler is attached to a window.
        )r   rQ   s    rL   r7   zCaret.visible   s     }rN   r7   c                    || _         t          j        | j                   |r4| j        r-| j        r&t          j        | j        | j                   d| _        |                     d           d S )NFr   )r   r   rW   rX   r   r   schedule_intervalr   )rG   r7   s     rL   r7   zCaret.visible   sh    %%% 	(t| 	( 	(#DK==="'DArN   c                *    | j         j        dd         S )az  An RGBA tuple of the current caret color.

        When blinking off, the alpha channel will be set to ``0``.  The
        default caret color when visible is ``(0, 0, 0, 255)`` (opaque black).

        You may set the color to an RGBA or RGB color tuple.

        .. warning:: This setter can fail for a short time after layout / window init!

                     Use ``__init__``'s ``color`` keyword argument instead if you
                     run into this problem.

        Each color channel must be between 0 and 255, inclusive. If the color
        set to an RGB color, the previous alpha channel value will be used.
        N   rB   r6   rQ   s    rL   r*   zCaret.color   s    " z !$$rN   0tuple[int, int, int, int] | tuple[int, int, int]c                |    |^}}}}|r|d         n| j         j        d         }||||||||f| j         j        d d <   d S )Nr   r]   rh   )rG   r*   rH   rI   rJ   _arK   s          rL   r*   zCaret.color   sW    1a" 1BqEETZ.q1 !Q1aA5
!!!rN   c                    | j         S )z"Position of caret within document.)r   rQ   s    rL   positionzCaret.position   s     ~rN   rm   c                n    || _         | j                                         |                                  d S rP   )r   r$   clear_update)rG   rm   s     rL   rm   zCaret.position   s/    !##%%%rN   c                    | j         S )a_  Position of immovable end of text selection within document.

        An interactive text selection is determined by its immovable end (the
        caret's position when a mouse drag begins) and the caret's position, which
        moves interactively by mouse and keyboard input.

        This property is ``None`` when there is no selection.
        )r"   rQ   s    rL   markz
Caret.mark   s     zrN   rr   c                    || _         |                     | j                   || j                            dd           d S d S )Nliner   )r"   rp   rD   r9   set_selection)rG   rr   s     rL   rr   z
Caret.mark   sJ    
$*+++<L&&q!,,,,, <rN   c                \    | j         | j         S | j                            | j                  S )zIndex of line containing the caret's position.

        When set, ``position`` is modified to place the caret on requested line
        while maintaining the closest possible X offset.
        )rD   r9   get_line_from_positionr   rQ   s    rL   ru   z
Caret.line   s.     '##|224>BBBrN   ru   c                    | j         '| j                            | j                  \  | _         }| j                            || j                   | _        |                     |d           d S )NF)ru   update_ideal_x)rC   r9   get_point_from_positionr   get_position_on_linerp   )rG   ru   _s      rL   ru   z
Caret.line   s_    = #|CCDNSSDM1::4OO$u55555rN   	attributestrr   c                `   | j         | j         | j        k    rC	 | j        |         S # t          $ r( | j        j                            || j                  cY S w xY wt          | j        | j                   }t          | j        | j                   }| j        j        	                    |||          S )a  Get the document's named style at the caret's current position.

        If there is a text selection and the style varies over the selection,
        `pyglet.text.document.STYLE_INDETERMINATE` is returned.

        Args:
            attribute:
                Name of style attribute to retrieve.  See
                :py:mod:`~pyglet.text.document` for a list of recognised attribute
                names.
        )
r"   r   r$   KeyErrorr9   document	get_styleminmaxget_style_range)rG   r~   startends       rL   r   zCaret.get_style   s     :t~!=!=R,Y77 R R R|,66y$.QQQQQR DNDJ//$.$*--|$44YsKKKs   & /AA
attributesc                   | j         | j         | j        k    r| j                            |           dS t	          | j        | j                   }t          | j        | j                   }| j        j                            |||           dS )a  Set the document style at the caret's current position.

        If there is a text selection the style is modified immediately.
        Otherwise, the next text that is entered before the position is
        modified will take on the given style.

        Args:
            attributes:
                Dict mapping attribute names to style values.  See
                :py:mod:`~pyglet.text.document` for a list of recognised attribute
                names.

        N)	r"   r   r$   updater   r   r9   r   	set_style)rG   r   r   r   s       rL   r   zCaret.set_style  s     :t~!=!=!((444FDNDJ//$.$*--''sJ?????rN   c                    t          | j        | j                  }t          | j        | j                  }|| _        d | _        | j        j                            ||           | j                            dd           d S )Nr   )r   r"   r   r   r9   r   delete_textrv   )rG   r   r   s      rL   _delete_selectionzCaret._delete_selection,  so    DJ//$*dn--
))%555""1a(((((rN   xyc                   | j                             ||          }d| _        | j                             dd           | j                             ||          | _        |                     |           | j                                         dS )zlMove the caret close to the given window coordinate.

        The `mark` will be reset to ``None``.
        Nr   rt   )	r9   get_line_from_pointr"   rv   r|   r   rp   r$   ro   rG   r   r   ru   s       rL   move_to_pointzCaret.move_to_point4  s    
 |//155
""1a(((::4CC$##%%%%%rN   c                    | j                             ||          }| j                             ||          | _        |                     |           | j                                         dS )zQMove the caret close to the given window coordinate while maintaining the `mark`.rt   N)r9   r   r|   r   rp   r$   ro   r   s       rL   select_to_pointzCaret.select_to_point@  sa    |//155::4CC$##%%%%%rN   c                    d| _         t          | j        j        j                  | _        |                                  | j                                         dS )z Select all text in the document.r   N)	r"   lenr9   r   textr   rp   r$   ro   rQ   s    rL   
select_allzCaret.select_allG  sH    
T\2788##%%%%%rN   c                8   | j                             ||          }| j                             ||          }| j                            | j         j        j        d|dz             }|sd}n|                                }|| _        | j	                            | j         j        j        |          }|st          | j         j        j                  }n|                                }|| _        |                     |           | j                                         dS )z/Select the word at the given window coordinate.r   r5   rt   N)r9   r   r|   r   searchr   r   r   rr   r   r   r   rp   r$   ro   )	rG   r   r   ru   pmatch1mark1match2mark2s	            rL   select_wordzCaret.select_wordN  s    |//155L--dA66'..t|/D/I1aRSeTT 	#EELLNNE	#**4<+@+EqII 	#-233EELLNNE$##%%%%%rN   c                `   | j                             ||          }| j                             ||          }| j         j                            |          | _        | j         j                            |          | _        |                     |           | j	        
                                 dS )z4Select the paragraph at the given window coordinate.rt   N)r9   r   r|   r   get_paragraph_startrr   get_paragraph_endr   rp   r$   ro   )rG   r   r   ru   r   s        rL   select_paragraphzCaret.select_paragraphc  s    |//155L--dA66L)==a@@	.@@CC$##%%%%%rN   rz   c                
   |'| j                             | j                  }d | _        n|| _        | j                             | j        |          \  }}| j         j        }|r|| _        || j         j        z  }|| j         j        | j         	                                z   z  }| j
        K| j                             t          | j        | j
                  t          | j        | j
                             | j                             |           | j                             |           | j         j                            t          d| j        dz
                      }|||j        z   ||||j        z   |g| j        j        d d <   d S )Nr   r5   )r9   rx   r   rD   r{   zrC   leftbottom_get_content_heightr"   rv   r   r   ensure_line_visibleensure_x_visibler   get_fontdescentascentrB   rm   )rG   ru   rz   r   r   r   fonts          rL   rp   zCaret._updatel  s]   <<66t~FFD#D#D|33DNDII1LN 	DM	T\	T\ 4<#C#C#E#EEE:!L&&s4>4:'F'FDN\`\fHgHghhh((...%%a(((|$--c!T^a5G.H.HII"#Q%5q!Q_a!P
AAArN   c                `    | j         j         | j         j         dfdz  | j        j        d d <   d S )Nr   r2   )r9   view_xview_yrB   translationrQ   s    rL   on_translation_updatezCaret.on_translation_update  s9    &*l&9%9DL<O;OQR$SVW$W
qqq!!!rN   c                    | j         t          | j        j        j                  k    r#t          | j        j        j                  | _         |                                  dS )z?Handler for the `IncrementalTextLayout.on_layout_update` event.N)rm   r   r9   r   r   rp   rQ   s    rL   on_layout_updatezCaret.on_layout_update  sG    =3t|49:::: 5 :;;DMrN   r   c                8   | j         |                                  |                    dd          }| j        }| xj        t	          |          z  c_        | j        j                            ||| j                   | 	                                 t          j        S )zHandler for the `pyglet.window.Window.on_text` event.

        Caret keyboard handlers assume the layout always has keyboard focus.
        GUI toolkits should filter keyboard and text events by widget focus
        before invoking this handler.
        N
)r"   r   replacer   r   r9   r   insert_textr$   rb   r   EVENT_HANDLED)rG   r   poss      rL   on_textzCaret.on_text  s     :!""$$$||D$''n#d))#))#tT5JKKK""rN   Fmotionselectc                   |t           j        k    ry| j        |                                  n| j        dk    rQ| xj        dz  c_        | j        j                            | j        | j        dz              |                                  n|t           j	        k    rp| j        |                                  nT| j        t          | j        j        j                  k     r-| j        j                            | j        | j        dz              |t           j        k    r;| j        |s| j        j        | _        ndt          d| j        dz
            | _        nE|t           j        k    rW| j        |s| j        j        | _        nt%          t          | j        j        j                  | j        dz             | _        n|t           j        k    rt          d| j        dz
            | _        n|t           j        k    r3| j        }|| j                                        dz
  k     r
|dz   | _        nl|t           j        k    r&| j                            | j                  | _        n6|t           j        k    r| j        }|| j                                        dz
  k     r<| j                            |dz             dz
  | _        |                     |           nt          | j        j        j                  | _        n|t           j        k    r	d| _        n|t           j        k    r%t          | j        j        j                  | _        nP|t           j        k    rv| j        dz   }| j                            | j        j        j        |          }|s%t          | j        j        j                  | _        n|                                | _        n|t           j         k    rX| j        }| j!                            | j        j        j        d|          }|s	d| _        n}|                                | _        nb|t           j"        k    r| j#        rx| j        }| j$        }||k    r| j        j        j        ||         }n| j        j        j        ||         }| j#        %                    |           | &                    d|           n|t           j'        k    r| j#        r| j$        |                                  | j#        (                                )                    dd          }| j        }| xj        t          |          z  c_        | j        j        *                    ||| j+                   | ,                                 | &                    d|           | j$        $|s"d| _$        | j        -                    dd           | j+        .                                 | ,                                 t^          j0        S )a  Handler for the `pyglet.window.Window.on_text_motion` event.

        Caret keyboard handlers assume the layout always has keyboard focus.
        GUI toolkits should filter keyboard and text events by widget focus
        before invoking this handler.
        Nr   r5   on_clipboard_copyr   r   on_clipboard_paste)1r	   MOTION_BACKSPACErr   r   r   r9   r   r   rp   MOTION_DELETEr   r   MOTION_LEFT_selection_startrm   r   MOTION_RIGHT_selection_endr   	MOTION_UPru   MOTION_DOWNget_line_countMOTION_BEGINNING_OF_LINEget_position_from_lineMOTION_END_OF_LINEMOTION_BEGINNING_OF_FILEMOTION_END_OF_FILEMOTION_NEXT_WORDr   r   r   MOTION_PREVIOUS_WORDr   MOTION_COPYrE   r"   set_clipboard_textdispatch_eventMOTION_PASTEget_clipboard_textr   r   r$   rb   rv   ro   r   r   )rG   r   r   ru   r   mrr   r   s           rL   on_text_motionzCaret.on_text_motion  s'    S)))y$&&((((!##!#%11$.$.STBTUUUs(((y$&&((((#dl&;&@"A"AAA%11$.$.STBTUUUS_$$y$V$ $ = #At}q'8 9 9s'''y$V$ $ ; #C(=(B$C$CT]UVEV W Ws}$$Aty1}--DIIs&&9Ddl1133a777 1H	s333 L??	JJDMMs---9Ddl1133a777!%!D!DTAX!N!NQR!RT"""" #DL$9$> ? ?s333DMMs--- 5 :;;DMMs+++.1$C"))$,*?*DcJJA * #DL$9$> ? ? !		s///.C&--dl.C.H!SQQA * ! !		s&&4<&.C:DTzz|,1$s(;|,1#d(;L++D111 3T::::s'''DL'z%&&(((<2244<<T4HHD.CNNc$ii'NNL!--c49NOOOKKMMM 4d;;;:!&!DJL&&q!,,,##%%%""rN   c                l    | j         | j        | _         |                     |d           t          j        S )a  Handler for the `pyglet.window.Window.on_text_motion_select` event.

        Caret keyboard handlers assume the layout always has keyboard focus.
        GUI toolkits should filter keyboard and text events by widget focus
        before invoking this handler.
        NT)rr   rm   r   r   r   )rG   r   s     rL   on_text_motion_selectzCaret.on_text_motion_select  s4     9DIFD)))""rN   scroll_xscroll_yc                    | j         xj        || j        z  z  c_        | j         xj        || j        z  z  c_        t          j        S )aV  Handler for the `pyglet.window.Window.on_mouse_scroll` event.

        Mouse handlers do not check the bounds of the coordinates: GUI
        toolkits should filter events that do not intersect the layout
        before invoking this handler.

        The layout viewport is scrolled by `SCROLL_INCREMENT` pixels per
        "click".
        )r9   r   r    r   r   r   )rG   r   r   r   r   s        rL   on_mouse_scrollzCaret.on_mouse_scroll  sI     	x$*???x$*???""rN   button	modifiersc                   t          j                     }|| j        z
  dk     r| xj        dz  c_        nd| _        t          j                     | _        | j        dk    r|                     ||           nJ| j        dk    r|                     ||           n(| j        dk    r|                     ||           d| _        |                                  t          j        S )a  Handler for the `pyglet.window.Window.on_mouse_press` event.

        Mouse handlers do not check the bounds of the coordinates: GUI
        toolkits should filter events that do not intersect the layout
        before invoking this handler.

        This handler keeps track of the number of mouse presses within
        a short span of time and uses this to reconstruct double- and
        triple-click events for selecting words and paragraphs.  This
        technique is not suitable when a GUI toolkit is in use, as the active
        widget must also be tracked.  Do not use this mouse handler if
        a GUI toolkit is being used.
        g      ?r5   r2   r]   r   )	timer   r   r   r   r   rb   r   r   )rG   r   r   r   r   ts         rL   on_mouse_presszCaret.on_mouse_press  s     IKKt$&&" !D9;;!!q!$$$$!##Q""""!##!!!Q''' !D""rN   dxdybuttonsc                    | j         | j        | _         |                     ||           |                                  t          j        S )zHandler for the `pyglet.window.Window.on_mouse_drag` event.

        Mouse handlers do not check the bounds of the coordinates: GUI
        toolkits should filter events that do not intersect the layout
        before invoking this handler.
        )rr   rm   r   rb   r   r   )rG   r   r   r   r   r   r   s          rL   on_mouse_dragzCaret.on_mouse_drag2  sA     9DIQ"""""rN   c                @    d| _         | j         | _        t          j        S )z~Handler for the `pyglet.window.Window.on_activate` event.

        The caret is hidden when the window is not active.
        Tr   r7   r   r   rQ   s    rL   on_activatezCaret.on_activate?  s    
 |""rN   c                @    d| _         | j         | _        t          j        S )zHandler for the `pyglet.window.Window.on_deactivate` event.

        The caret is hidden when the window is not active.
        Fr   rQ   s    rL   on_deactivatezCaret.on_deactivateH  s    
 |""rN   c                    t           j        S )zSDispatched when text is copied.
        This default handler does nothing.
        r   r   rG   r   s     rL   r   zCaret.on_clipboard_copyQ       ""rN   c                    t           j        S )zRDispatched when text is pasted.
        The default handler does nothing.
        r   r   s     rL   r   zCaret.on_clipboard_pasteV  r   rN   )Nr%   N)
r'   r   r(   r)   r*   r+   r,   r-   r.   r/   )r.   r   )r'   r   r.   r/   )r.   r/   )r[   r   r.   r/   )r.   r   )r7   r   r.   r/   )r.   r+   )r*   ri   r.   r/   )r.   r   )rm   r   r.   r/   )rr   r   r.   r/   )ru   r   r.   r/   )r~   r   r.   r   )r   r#   r.   r/   )r   r   r   r   r.   r/   ra   )ru   r!   rz   r   r.   r/   )r   r   r.   r   )F)r   r   r   r   r.   r   )r   r   r.   r   )
r   r   r   r   r   r   r   r   r.   r   )
r   r   r   r   r   r   r   r   r.   r   )r   r   r   r   r   r   r   r   r   r   r   r   r.   r   )6__name__
__module____qualname____doc__recompiler   __annotations__r   DOTALLr   r   r   r   r   r   r   r   r   r    r"   rM   propertyr'   setterrY   rX   rb   r7   r*   rm   rr   ru   r   r   r   r   r   r   r   r   rp   r   r   r   r   r   r   r   r   r   r   r   r    rN   rL   r   r      s         0 #-"*\":":M::::&0bj1B&C&CCCCC","*U")"D"D"DMDDDD&0bjbi&H&H&HHHHHIGHNLK F *))))E$$$$LP\`+# +# +# +# +#Z    X ]F F F ]F+ + + +% % % %       X ^   ^ % % % X%$ \6 6 6 \6    X _   _
 	 	 	 X	 
[- - - [- 	C 	C 	C X	C 
[6 6 6 [6L L L L,@ @ @ @,) ) ) )
& 
& 
& 
&& & & && & & && & & &*& & & &Q Q Q Q Q.X X X X   # # # #"Y# Y# Y# Y# Y#v
# 
# 
# 
## # # ## # # #@# # # ## # # ## # # ## # # #
# # # # # #rN   r   r   r   )r   
__future__r   r   r   typingr   r   r   r8   r   r   pyglet.windowr	   pyglet.eventr
   pyglet.graphicsr   r   pyglet.text.layoutr   r   register_event_typer  rN   rL   <module>r     s2    # " " " " " 				  . . . . . . . . . .               ( ( ( ( ( ( 9%%%%%%$$$$$$888888~# ~# ~# ~# ~#O ~# ~# ~#@   - . . .   . / / / / /rN   