
    \
jP                     X    d Z ddlZddlZddlmZ ddlmZ ddlmZ  G d d          ZdS )aS  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)

.. versionadded:: 1.1
    N)clock)event)keyc                      e Zd ZdZ ej        d          Z ej        d          Z ej        dej                  Z	 ej        dej                  Z
dZdZdZdZdZdZdZd	Zd2dZd Zd Zd Zd Zd Z eeed          Zd Zd Z eeed          Zd Zd Z eeed          Z d
Z!d Z"d Z# ee#e"d          Z$d Z%d Z& ee&e%d          Z'd  Z(d! Z)d" Z*d# Z+d$ Z,d% Z-d& Z.d3d'Z/d( Z0d) Z1d4d+Z2d, Z3d- Z4d. Z5d/ Z6d0 Z7d1 Z8d
S )5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.  

    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(?<=\W)\w+\W*$z\n)flagsr   Tg      ?   Nr   r   r   c                    ddl m} || _        ||j        }|\  }}}|||d|||df}|                    d|j        |j        dd|f          | _        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.TextLayout`
                Layout to control.
            `batch` : `~pyglet.graphics.Batch`
                Graphics batch to add vertices to.
            `color` : (int, int, int)
                RGB tuple with components in range [0, 255].

        r   )glN      v2fc4BT)pygletr   _layoutbatchaddGL_LINESbackground_group_list_ideal_x_ideal_line_next_attributesvisiblepush_handlers)	selflayoutr   colorr   rgbcolorss	            K/DATA/AppData/hermes/venv/lib/python3.11/site-packages/pyglet/text/caret.py__init__zCaret.__init__g   s     	=LE1aQ31a-YYq"+v/FPUW]__
 "T"""""    c                 l    | j                                          | j                            |            dS )ziRemove the caret from its batch.

        Also disconnects the caret from further layout events.
        N)r   deleter   remove_handlersr   s    r$   r(   zCaret.delete   s4    
 	
$$T*****r&   c                     | j         r| j         | _        | j        r| j        r
| j        rd}nd}|| j        j        d<   || j        j        d<   d S )Nr   r         )PERIOD_blink_visible_visible_activer   r#   )r   dtalphas      r$   _blinkzCaret._blink   sg    ; 	:&*&9"9D= 	T\ 	d.A 	EEE$
!$
!r&   c                     d| _         d S NT)r   r*   s    r$   _nudgezCaret._nudge   s    r&   c                     || _         t          j        | j                   |r4| j        r-| j        r&t          j        | j        | j                   d| _        |                     d           d S )NFr   )r0   r   
unscheduler4   r1   r.   schedule_intervalr/   )r   r   s     r$   _set_visiblezCaret._set_visible   sh    %%% 	(t| 	( 	(#DK==="'DAr&   c                     | j         S N)r0   r*   s    r$   _get_visiblezCaret._get_visible   s
    }r&   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.

    :type: bool
    )docc                 J    || j         j        d d<   || j         j        dd<   d S )Nr,      r-   r   r#   )r   r   s     r$   
_set_colorzCaret._set_color   s.     %
"1"!&
!A#r&   c                 *    | j         j        d d         S )Nr,   rB   r*   s    r$   
_get_colorzCaret._get_color   s    z !$$r&   zCaret color.

    The default caret color is ``[0, 0, 0]`` (black).  Each RGB color
    component is in the range 0 to 255.

    :type: (int, int, int)
    c                 n    || _         | j                                         |                                  d S r=   )	_positionr   clear_update)r   indexs     r$   _set_positionzCaret._set_position   s/    ##%%%r&   c                     | j         S r=   )rG   r*   s    r$   _get_positionzCaret._get_position   s
    ~r&   z7Position of caret within document.

    :type: int
    c                     || _         |                     | j                   || j                            dd           d S d S )Nliner   )_markrI   r   r   set_selection)r   marks     r$   	_set_markzCaret._set_mark   sJ    
$*+++<L&&q!,,,,, <r&   c                     | j         S r=   )rQ   r*   s    r$   	_get_markzCaret._get_mark   s
    zr&   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.

    :type: int
    c                     | j         '| j                            | j                  \  | _         }| j                            || j                   | _        |                     |d           d S )NF)rP   update_ideal_x)r   r   get_point_from_positionrG   get_position_on_linerI   )r   rP   _s      r$   	_set_linezCaret._set_line   s_    = #|CCDNSSDM1::4OO$u55555r&   c                 \    | j         | j         S | j                            | j                  S r=   )r   r   get_line_from_positionrG   r*   s    r$   	_get_linezCaret._get_line   s,    '##<66t~FFFr&   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.
                    
    :type: int
    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.

        :Parameters:
            `attribute` : str
                Name of style attribute to retrieve.  See
                `pyglet.text.document` for a list of recognised attribute
                names.

        :rtype: object
        )
rQ   rG   r   KeyErrorr   document	get_styleminmaxget_style_range)r   	attributestartends       r$   rc   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c                    | 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.

        :Parameters:
            `attributes` : dict
                Dict mapping attribute names to style values.  See
                `pyglet.text.document` for a list of recognised attribute
                names.

        N)	rQ   rG   r   updaterd   re   r   rb   	set_style)r   
attributesrh   ri   s       r$   rl   zCaret.set_style  s     :t~!=!=!((444FDNDJ//$.$*--''sJ?????r&   c                     t          | j        | j                  }t          | j        | j                  }|| _        d | _        | j        j                            ||           | j                            dd           d S )Nr   )rd   rQ   rG   re   r   rb   delete_textrR   )r   rh   ri   s      r$   _delete_selectionzCaret._delete_selection%  so    DJ//$*dn--
))%555""1a(((((r&   c                    | j                             ||          }d| _        | j                             dd           | j                             ||          | _        |                     |           | j                                         dS )zMove the caret close to the given window coordinate.

        The `mark` will be reset to ``None``.

        :Parameters:
            `x` : int   
                X coordinate.
            `y` : int
                Y coordinate.

        Nr   rO   )	r   get_line_from_pointrQ   rR   rZ   rG   rI   r   rH   r   xyrP   s       r$   move_to_pointzCaret.move_to_point-  s     |//155
""1a(((::4CC$##%%%%%r&   c                     | j                             ||          }| j                             ||          | _        |                     |           | j                                         dS )zMove the caret close to the given window coordinate while
        maintaining the `mark`.

        :Parameters:
            `x` : int   
                X coordinate.
            `y` : int
                Y coordinate.

        rO   N)r   rr   rZ   rG   rI   r   rH   rs   s       r$   select_to_pointzCaret.select_to_point@  sc     |//155::4CC$##%%%%%r&   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 )zSelect the word at the given window coordinate.

        :Parameters:
            `x` : int   
                X coordinate.
            `y` : int
                Y coordinate.

        r      rO   N)r   rr   rZ   _previous_word_researchrb   textrh   rS   _next_word_relenrG   rI   r   rH   )r   rt   ru   rP   pm1m2s          r$   select_wordzCaret.select_wordP  s     |//155L--dA66#**4<+@+Eq!A#NN 	BBB	&&t|'<'A1EE 	T\*/00BBB$##%%%%%r&   c                 `   | j                             ||          }| j                             ||          }| j         j                            |          | _        | j         j                            |          | _        |                     |           | j	        
                                 dS )zSelect the paragraph at the given window coordinate.

        :Parameters:
            `x` : int   
                X coordinate.
            `y` : int
                Y coordinate.

        rO   N)r   rr   rZ   rb   get_paragraph_startrS   get_paragraph_endrG   rI   r   rH   )r   rt   ru   rP   r   s        r$   select_paragraphzCaret.select_paragraphl  s     |//155L--dA66L)==a@@	.@@CC$##%%%%%r&   c                    |'| j                             | j                  }d | _        n|| _        | j                             | j        |          \  }}|r|| _        || j         j        j        z  }|| j         j        j        z  }| j         j	        
                    t          d| j        dz
                      }|||j        z   |||j        z   g| j        j        d d <   | j        K| j                             t%          | j        | j                  t          | j        | j                             | j                             |           | j                             |           d S )Nr   rz   )r   r^   rG   r   rY   r   	top_groupview_xview_yrb   get_fontre   descentascentr   verticesrQ   rR   rd   ensure_line_visibleensure_x_visible)r   rP   rX   rt   ru   fonts         r$   rI   zCaret._update}  sM   <<66t~FFD#D#D|33DNDII1 	DM	T\#**	T\#**|$--c!T^a5G.H.HII"#Q%5q!dk/!J
AAA:!L&&s4>4:'F'F'*4>4:'F'FH H H 	((...%%a(((((r&   c                     | j         t          | j        j        j                  k    r#t          | j        j        j                  | _         |                                  d S r=   )positionr   r   rb   r}   rI   r*   s    r$   on_layout_updatezCaret.on_layout_update  sG    =3t|49:::: 5 :;;DM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
)rQ   rp   replacerG   r   r   rb   insert_textr   r7   r   EVENT_HANDLED)r   r}   poss      r$   on_textzCaret.on_text  s     :!""$$$||D$''n#d))#))#tT5JKKK""r&   Fc                    |t           j        k    re| j        |                                  n| j        dk    r=| xj        dz  c_        | j        j                            | j        | j        dz              n|t           j        k    rq| j        |                                  n| j        t          | j        j        j
                  k     r-| j        j                            | j        | j        dz              n+| j        $|s"d| _        | j                            dd           |t           j        k    rt          d| j        dz
            | _        n|t           j        k    r;t#          t          | j        j        j
                  | j        dz             | _        nv|t           j        k    rt          d| j        dz
            | _        nG|t           j        k    r3| j        }|| j                                        dz
  k     r
|dz   | _        n|t           j        k    r&| j                            | j                  | _        n|t           j        k    r| j        }|| j                                        dz
  k     r<| j                            |dz             dz
  | _        |                     |           n[t          | j        j        j
                  | _        n6|t           j        k    r	d| _        n|t           j        k    r$t          | j        j        j
                  | _        n|t           j        k    rt| j        dz   }| j                            | j        j        j
        |          }|s$t          | j        j        j
                  | _        n|                                | _        ne|t           j         k    rU| j        }| j!                            | j        j        j
        d|          }|sd| _        n|                                | _        | j"        #                                 | $                                 tJ          j&        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   rz   )'r   MOTION_BACKSPACErS   rp   rG   r   rb   ro   MOTION_DELETEr   r}   rQ   rR   MOTION_LEFTre   r   MOTION_RIGHTrd   	MOTION_UPrP   MOTION_DOWNget_line_countMOTION_BEGINNING_OF_LINEget_position_from_lineMOTION_END_OF_LINErI   MOTION_BEGINNING_OF_FILEMOTION_END_OF_FILEMOTION_NEXT_WORDr~   r|   rh   MOTION_PREVIOUS_WORDr{   r   rH   r7   r   r   )r   motionselectrP   r   ms         r$   on_text_motionzCaret.on_text_motion  s    S)))y$&&((((!##!#%11NDNQ$68 8 8s(((y$&&((((#dl&;&@"A"AAA%11NDNQ$68 8 8Z#F#DJL&&q!,,,S_$$4=1#455DMMs'''DL$9$> ? ? $ 13 3DMM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 * ! !		##%%%""r&   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)rS   r   r   r   r   )r   r   s     r$   on_text_motion_selectzCaret.on_text_motion_select  s4     9DIFD)))""r&   c                     | 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".
        )r   r   SCROLL_INCREMENTr   r   r   )r   rt   ru   scroll_xscroll_ys        r$   on_mouse_scrollzCaret.on_mouse_scroll  sI     	x$*???x$*???""r&   c                    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      ?rz   r   r,   r   )	time_click_time_click_countrv   r   r   r7   r   r   )r   rt   ru   button	modifiersts         r$   on_mouse_presszCaret.on_mouse_press  s     IKKt$&&" !D9;;!!q!$$$$!##Q""""!##!!!Q''' !D""r&   c                     | 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.
        )rS   r   rx   r7   r   r   )r   rt   ru   dxdybuttonsr   s          r$   on_mouse_dragzCaret.on_mouse_drag&  sA     9DIQ"""""r&   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r1   r   r   r   r*   s    r$   on_activatezCaret.on_activate3  s    
 |""r&   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   r*   s    r$   on_deactivatezCaret.on_deactivate<  s    
 |""r&   )Nr
   r6   )F)9__name__
__module____qualname____doc__recompiler~   r{   DOTALL_next_para_re_previous_para_rerG   r1   r0   r/   r   r   r.   r   r%   r(   r4   r7   r;   r>   propertyr   rC   rE   r   rK   rM   r   rQ   rT   rV   rS   r\   r_   rP   rc   rl   rp   rv   rx   r   r   rI   r   r   r   r   r   r   r   r   r    r&   r$   r   r   ;   s=        . BJ|,,M"
#455BJuBI666M"
5	:::IGHNLK F %# # # #>+ + +% % %       h|\ 8 	 	 	G' ' '% % % HZ 2 	 	 	E  
   x} ; 	 	 	H
 E- - -   8Iy	
	 
	 
	D6 6 6G G G 8Iy	 	 	DL L L0@ @ @.) ) )& & &&& & & & & &8& & &") ) ) ),  
# # #"A# A# A# A#F
# 
# 
## # ## # #@# # ## # ## # # # #r&   r   )	r   r   r   r   r   r   pyglet.windowr   r   r   r&   r$   <module>r      s   H  
			                   H# H# H# H# H# H# H# H# H# H#r&   