
    ;OOf                     h   d dl mZmZ d dlmZmZ d dlmZ d dlm	Z	m
Z
 d dlmZmZmZmZ d dlmZmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZmZmZm Z m!Z!m"Z"  G d de#      Z$ G d d eejJ                  e&            Z' G d dee'      Z( G d de'e      Z) G d de#      Z* G d dee      Z+y)    )divisionprint_function)xrangewith_metaclass)is_mouse_press)SolidCanvasCanvasCombine)Widgetnocache_widget_render_instanceBOXGIVEN)calculate_top_bottom_fillernormalize_valign)signals)connect_signal)MonitoredListMonitoredFocusList)WidgetContainerMixin)	CURSOR_UPCURSOR_DOWNCURSOR_PAGE_UPCURSOR_PAGE_DOWNCURSOR_MAX_LEFTCURSOR_MAX_RIGHTc                       e Zd Zy)ListWalkerErrorN__name__
__module____qualname__     N/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/urwid/listbox.pyr   r   $       r"   r   c                   *    e Zd ZdgZd Zd Zd Zd Zy)
ListWalkermodifiedc                 0    t        j                  | d       y )Nr'   )r   emit_signalselfs    r#   	_modifiedzListWalker._modified*   s    D*-r"   c                 ^    	 | j                   }| |   |fS # t        t        t        f$ r Y yw xY w)z
        This default implementation relies on a focus attribute and a
        __getitem__() method defined in a subclass.

        Override and don't call this method if these are not defined.
        NN)focus
IndexErrorKeyError	TypeError)r+   r/   s     r#   	get_focuszListWalker.get_focus-   s8    	JJE;%%Hi0 		s    ,,c                 ^    	 | j                  |      }| |   |fS # t        t        f$ r Y yw xY w)z
        This default implementation relies on a next_position() method and a
        __getitem__() method defined in a subclass.

        Override and don't call this method if these are not defined.
        r.   )next_positionr0   r1   r+   positions     r#   get_nextzListWalker.get_next:   =    	))(3H>8++H% 		    ,,c                 ^    	 | j                  |      }| |   |fS # t        t        f$ r Y yw xY w)z
        This default implementation relies on a prev_position() method and a
        __getitem__() method defined in a subclass.

        Override and don't call this method if these are not defined.
        r.   )prev_positionr0   r1   r6   s     r#   get_prevzListWalker.get_prevG   r9   r:   N)r   r   r    r   r,   r3   r8   r=   r!   r"   r#   r&   r&   '   s    lG.r"   r&   c                   P    e Zd Zd
dZd Z ee      Zd Zd Zd Z	d Z
d Zd
dZy	)SimpleListWalkerc                     t        |dd      st        d|      t        j                  | |       d| _        || _        y)a  
        contents -- list to copy into this object

        wrap_around -- if true, jumps to beginning/end of list on move

        This class inherits :class:`MonitoredList` which means
        it can be treated as a list.

        Changes made to this object (when it is treated as a list) are
        detected automatically and will cause ListBox objects using
        this list walker to be updated.
        __getitem__Nz2SimpleListWalker expecting list like object, got: r   )getattrr   r   __init__r/   wrap_aroundr+   contentsrD   s      r#   rC   zSimpleListWalker.__init__U   s>     x5!Zb"deetX.
&r"   c                     | S )z_
        Return self.

        Provides compatibility with old SimpleListWalker class.
        r!   r*   s    r#   _get_contentszSimpleListWalker._get_contentsh   s	     r"   c                     | j                   t        |       k\  rt        dt        |       dz
        | _         t        j	                  |        y )Nr      )r/   lenmaxr&   r,   r*   s    r#   r,   zSimpleListWalker._modifiedq   s6    ::T"QD	!,DJT"r"   c                     t        d      )z
        This function inherited from MonitoredList is not
        implemented in SimpleListWalker.

        Use connect_signal(list_walker, "modified", ...) instead.
        9Use connect_signal(list_walker, "modified", ...) instead.NotImplementedErrorr+   callbacks     r#   set_modified_callbackz&SimpleListWalker.set_modified_callbackv        " #5 6 	6r"   c                     	 |dk  s|t        |       k\  rt        	 || _        | j                          y# t        t        f$ r t        d|      w xY w)Set focus position.r   zNo widget at position N)rK   
ValueErrorr2   r0   r/   r,   r6   s     r#   	set_focuszSimpleListWalker.set_focus   s^    	G!|x3t94    5 
 :& 	G8EFF	Gs	   4 Ac                 T    t        |       dz
  |k  r| j                  ryt        |dz   S z3
        Return position after start_from.
        rJ   r   rK   rD   r0   r6   s     r#   r5   zSimpleListWalker.next_position   0     t9q=H$!|r"   c                 V    |dk  r | j                   rt        |       dz
  S t        |dz
  S z4
        Return position before start_from.
        r   rJ   rD   rK   r0   r6   s     r#   r<   zSimpleListWalker.prev_position   2     q=4y1}$!|r"   c                 `    |rt        t        |       dz
  dd      S t        t        |             S zI
        Optional method for returning an iterable of positions.
        rJ   r   rK   r+   reverses     r#   	positionszSimpleListWalker.positions   -     #d)a-R00c$i  r"   NF)r   r   r    rC   rH   propertyrF   r,   rS   rX   r5   r<   rg   r!   r"   r#   r?   r?   T   s6    '& &H#
6!r"   r?   c                   4    e Zd ZddZd Zd Zd Zd ZddZy)	SimpleFocusListWalkerc                 t    t        |dd      st        d|      t        j                  | |       || _        y)a8  
        contents -- list to copy into this object

        wrap_around -- if true, jumps to beginning/end of list on move

        This class inherits :class:`MonitoredList` which means
        it can be treated as a list.

        Changes made to this object (when it is treated as a list) are
        detected automatically and will cause ListBox objects using
        this list walker to be updated.

        Also, items added or removed before the widget in focus with
        normal list methods will cause the focus to be updated
        intelligently.
        rA   Nz7SimpleFocusListWalker expecting list like object, got: )rB   r   r   rC   rD   rE   s      r#   rC   zSimpleFocusListWalker.__init__   s=    " x5!#+#. / /##D(3&r"   c                     t        d      )z
        This function inherited from MonitoredList is not
        implemented in SimpleFocusListWalker.

        Use connect_signal(list_walker, "modified", ...) instead.
        rN   rO   rQ   s     r#   rS   z+SimpleFocusListWalker.set_modified_callback   rT   r"   c                 2    || _         | j                          y)rV   N)r/   r,   r6   s     r#   rX   zSimpleFocusListWalker.set_focus   s    
r"   c                 T    t        |       dz
  |k  r| j                  ryt        |dz   S rZ   r[   r6   s     r#   r5   z#SimpleFocusListWalker.next_position   r\   r"   c                 V    |dk  r | j                   rt        |       dz
  S t        |dz
  S r^   r_   r6   s     r#   r<   z#SimpleFocusListWalker.prev_position   r`   r"   c                 `    |rt        t        |       dz
  dd      S t        t        |             S rb   rd   re   s     r#   rg   zSimpleFocusListWalker.positions   rh   r"   Nri   )	r   r   r    rC   rS   rX   r5   r<   rg   r!   r"   r#   rl   rl      s     '.6
!r"   rl   c                       e Zd Zy)ListBoxErrorNr   r!   r"   r#   rt   rt      r$   r"   rt   c                   P   e Zd ZdZdZ eeg      Zd Zd Z	d Z
 ee	e
d      Zd+dZd+d	Zd
 Zd Zd,dZd Zd Z eed      Zd Z eeed      Zd Zd Z ed d      Zd Zd Zd Zd Zd Zd Z	 	 d-dZ d Z!d Z"d  Z#d! Z$d" Z%d# Z&d$ Z'd% Z(d& Z)d' Z*d+d(Z+d) Z,d* Z-y).ListBoxz0
    a horizontally stacked list of widgets
    Tc                     | j                  |       	 t        | j                  d| j                         d| _        d| _        d| _	        d| _
        d| _        y# t        $ r t        |       | _        Y ?w xY w)z
        :param body: a ListWalker subclass such as
            :class:`SimpleFocusListWalker` that contains
            widgets to be displayed inside the list box
        :type body: ListWalker
        r'   r   r   rJ   leftfirst selectableN)	_set_bodyr   _body_invalidate	NameErrorr   renderoffset_rowsinset_fractionpref_colset_focus_pendingset_focus_valign_pendingr+   bodys     r#   rC   zListBox.__init__   s{     	t	?4::z43C3CD  $  "4 )-%-  	? 9>DK		?s   !A A43A4c                     | j                   S N)r|   r*   s    r#   	_get_bodyzListBox._get_body  s    zzr"   c                 n    t        |dd       r|| _        nt        |      | _        | j                          y )Nr3   )rB   r|   r?   r}   r   s     r#   r{   zListBox._set_body  s-    4d+DJ)$/DJr"   z
    a ListWalker subclass such as :class:`SimpleFocusListWalker` that contains
    widgets to be displayed inside the list box
    )docc                 :   |\  }}| j                   s| j                  r| j                  ||f|       | j                  j	                         \  }}|y|}| j                  ||f      \  }}	|r
||k\  r|dz
  }d}
|r0|j                         r |rt        |d      r|j                  |f      }
|
,|
\  }}||z   |	z
  }|dk  r|}	n||k\  r||z
  dz
  }|dk  r| d}}	|	}|j                  |fd      }|}|}g }|}|dkD  rf| j                  j                  |      \  }}|||z  }n@|}|j                  |f      }|r|j                  |||f       ||kD  r||z
  }n||z  }|dkD  rf||z   |	z
  |z
  }|dk  rd}|}||z
  |z
  |	z   }g }|dkD  rd| j                  j                  |      \  }}|nC|j                  |f      }|r|j                  |||f       ||kD  r||z
  }||z  }n||z  }|dkD  rdt        d|      }|dkD  r#|dkD  r||k  r||z  }||z  }d}n||z  }||z  }d}|}|dkD  rg| j                  j                  |      \  }}|nF|j                  |f      }|j                  |||f       ||kD  r||z
  }||z  }n||z  }||z  }|dkD  rg||	z
  ||||
f||f||ffS )a  
        Returns the widgets that would be displayed in
        the ListBox given the current *size* and *focus*.

        see :meth:`Widget.render` for parameter details

        :returns: (*middle*, *top*, *bottom*) or (``None``, ``None``, ``None``)

        *middle*
            (*row offset*(when +ve) or *inset*(when -ve),
            *focus widget*, *focus position*, *focus rows*,
            *cursor coords* or ``None``)
        *top*
            (*# lines to trim off top*,
            list of (*widget*, *position*, *rows*) tuples above focus
            in order from bottom to top)
        *bottom*
            (*# lines to trim off bottom*,
            list of (*widget*, *position*, *rows*) tuples below focus
            in order from top to bottom)
        N)NNNrJ   get_cursor_coordsr   T)r   r   _set_focus_completer|   r3   get_focus_offset_inset
selectablehasattrr   rowsr=   appendr8   rL   )r+   sizer/   maxcolmaxrowfocus_widget	focus_postop_posr   
inset_rowscursorcxcyeffective_cytrim_top
focus_rowspos
fill_lines
fill_aboveprevp_rowstrim_bottom
fill_belownextn_rowss                            r#   calculate_visiblezListBox.calculate_visible(  s   ,   !!T%B%B$$vv&6? #'**"6"6"8i!"&"="=FO#Z kV+ !)K l--/E|$78%77	BFB+j8La
'$rk1n?/:lAJ !&&y6
  

1n

++S2ID#|z)GYY	+F!!D#v#68
"!*,& J 1n !;.;fD?!K j(;6C

1n

++S2ID#|YY	+F!!D#v#68
"$Z/f$
& J 1n J'
q=Xq[X%J&z)
h&
x'1n

++S2ID#|YY	+Fc624
"!*,z)& J6!K 1n z)<:v0z"[*$=? 	?r"   c                    |\  }}| j                  ||f|      \  }}}|t        d||      S |\  }}	}
}}|\  }}|\  }}g }d}|j                          |D ]d  \  }}}|j                  |f      }||j	                         k7  r t        d||||j	                         fz        ||z  }|j                  ||df       f |	j                  |f|      }|j	                         |k7  r t        d|	|
||j	                         fz        |j                  }|||k7  rt        d|	d|
d	|d
|d	      ||z  }|j                  ||
df       |D ]d  \  }}}|j                  |f      }||j	                         k7  r t        d||||j	                         fz        ||z  }|j                  ||df       f t        |      }|r|j                  |       ||z  }|r|j                  |       ||z  }||kD  rt        d|||f      ||k  rU|
}|r|d   d   }|dk7  s| j                  j                  |      dk7  rt        d|||f      |j                  d||z
         |S )zb
        Render ListBox and return canvas.

        see :meth:`Widget.render` for details
        r/    r   zKWidget %r at position %r within listbox calculated %d rows but rendered %d!FzQFocus Widget %r at position %r within listbox calculated %d rows but rendered %d!zFocus Widget z at position z) within listbox calculated cursor coords z but rendered cursor coords !TzDListbox contents too long!  Probably urwid's fault (please report): rc   rJ   r.   zEListbox contents too short!  Probably urwid's fault (please report): )r   r   rf   r   r   rt   r   r   r	   trimtrim_endr|   r8   pad_trim_top_bottom)r+   r   r/   r   r   middletopbottom_ignorer   r   r   r   r   r   r   r   combinelistr   widgetw_posw_rowscanvasfocus_canvasc_cursorfinal_canvas
bottom_poss                              r#   r   zListBox.render  s     "44VE 5 +V>sFF33?E<y*f"*"(Z#- 	7F5]]F9-F&"#psyz  AG  IO  IT  IT  IV  sW  $W  X  XFNDu56	7 $**F9E*B*,r  vB  CL  MW  Ye  Yj  Yj  Yl  um   m  n  n&&&H"4  MY  Zc  dj  ks   t  u  u
L)T:;#- 	7F5]]F9-F&"#psyz  AG  IO  IT  IT  IV  sW  $W  X  XFNDu56	7 %[1h'HD!!+.KD&=lopvw}k~   A  B  B&="J
2q(9:a4::#6#6z#Bk#Q"qtu{  }C  qD  $F  G  G,,Q>r"   c                     |\  }}| j                  ||fd      \  }}}|y|\  }}}	}
}|sy|\  }}||z  }|dk  s||k\  ry||fS )zB
        See :meth:`Widget.get_cursor_coords` for details
        TNr   )r   )r+   r   r   r   r   r   r   offset_inset_ignore1_ignore2_ignore3r   xys                 r#   r   zListBox.get_cursor_coords  sy      "44Vd$V>=C:h(F1	\q5AK1vr"   c                 <    t        |t              \  }}||f| _        y)zSet the focus widget's display offset and inset.

        :param valign: one of:
            'top', 'middle', 'bottom'
            ('fixed top', rows)
            ('fixed bottom', rows)
            ('relative', percentage 0=top 100=bottom)
        N)r   rt   r   )r+   valignvtvas       r#   set_focus_valignzListBox.set_focus_valign  s      "&6B(*B%r"   Nc                     |dvrt        d|      | j                  j                         \  }}|t        d      |||f| _        | j                  j                  |       y)aT  
        Set the focus position and try to keep the old focus in view.

        :param position: a position compatible with :meth:`self._body.set_focus`
        :param coming_from: set to 'above' or 'below' if you know that
                            old position is above or below the new position.
        :type coming_from: str
        )abovebelowNzcoming_from value invalid: Nz!Can't set focus, ListBox is empty)rt   r|   r3   r0   r   rX   )r+   r7   coming_fromr   r   s        r#   rX   zListBox.set_focus  sl     66     "&**"6"6"8i@AA!,lI!E

X&r"   c                 6    | j                   j                         S )z
        Return a `(focus widget, focus position)` tuple, for backwards
        compatibility. You may also use the new standard container
        properties :attr:`focus` and :attr:`focus_position` to read these values.
        r|   r3   r*   s    r#   r3   zListBox.get_focus(  s     zz##%%r"   c                 <    | j                   j                         d   S )z^
        Return the widget in focus according to our :obj:`list walker <ListWalker>`.
        r   r   r*   s    r#   
_get_focuszListBox._get_focus0  s     zz##%a((r"   z7the child widget in focus or None when ListBox is emptyc                 Z    | j                   j                         \  }}|t        d      |S )z
        Return the list walker position of the widget in focus. The type
        of value returned depends on the :obj:`list walker <ListWalker>`.

        z#No focus_position, ListBox is empty)r|   r3   r0   )r+   wr   s      r#   _get_focus_positionzListBox._get_focus_position8  s0     %%'39BCC
r"   a  
        the position of child widget in focus. The valid values for this
        position depend on the list walker in use.
        :exc:`IndexError` will be raised by reading this property when the
        ListBox is empty or setting this property to an invalid position.
        c                 4      G  fddt               } |       S )Nc                   (    e Zd ZW  j                  Zy)*ListBox._contents.<locals>.ListBoxContentsN)r   r   r    _contents__getitem__rA   r*   s   r#   ListBoxContentsr   J  s    33Kr"   r   )object)r+   r   s   ` r#   	_contentszListBox._contentsI  s    	4f 	4  r"   c                    t        | j                  dd       }|r	  ||      d fS | j                  j	                         \  }}	 	 | j                  j                  |       | j                  j	                         d   | j                  j                  |       S # t        t        f$ r t        d|      w xY w# t        t        f$ r t        d|      w xY w# | j                  j                  |       w xY w)NrA   z ListBox.contents key not found: r   )rB   r|   r0   r1   r3   rX   )r+   keygetitemr   	old_focuss        r#   r   zListBox._contents__getitem__M  s    $**mT:Nd++ zz++-9	,N

$$S)zz++-a0 JJ  + ) NsLMMN ) NsLMMN JJ  +s#   	B 7B8 B58CC C6c                     | j                   S r   )r   r*   s    r#   <lambda>zListBox.<lambda>_  s
    T^^ r"   a  
        An object that allows reading widgets from the ListBox's list
        walker as a `(widget, options)` tuple. `None` is currently the only
        value for options.

        .. warning::

            This object may not be used to set or iterate over contents.

            You must use the list walker stored as
            :attr:`.body` to perform manipulation and iteration, if supported.
        c                      y)z
        There are currently no options for ListBox contents.

        Return None as a placeholder for future options.
        Nr!   r*   s    r#   optionszListBox.optionsl  s     r"   c           
         |\  }}| j                   \  }}d| _         d| _        | j                  j                         \  }}|y|j	                  |f|      }	t        |||t        |	ddd      \  }
}| j                  ||f|
       y)zf
        Finish setting the offset and inset now that we have have a
        maxcol & maxrow.
        Nr   )r   r   r|   r3   r   r   r   shift_focus)r+   r   r/   r   r   r   r   r   r   r   rtoprbots               r#   _set_focus_valign_completez"ListBox._set_focus_valign_completet  s    
  --2(,%!%"&**"6"6"8i  &E20E4q!-
d 	&&)40r"   c                 b   |\  }}d| _         d| _        | j                  ||f|      \  }}}|y|\  }}	}
}}|\  }}|\  }}|	j                         ry|r|dd }||z   }|D ]L  \  }}}|j                         r1| j                  j                  |       | j                  ||f|        y||z  }N y)zr
        Choose the first visible, selectable widget below the
        current focus as the focus widget.
        Nr   rc   )r   r   r   r   r|   rX   r   )r+   r   r/   r   r   r   r   r   
row_offsetr   r   r   r   r   r   r   r   new_row_offsetr   r   r   s                        r#   _set_focus_first_selectablez#ListBox._set_focus_first_selectable  s    
  (,%!%"44VE 5 +V>BH?
L)Z"*"(Z""$#CRJ#j0!+ 	#FC  "

$$S)  &&!1"$d"N	#r"   c                 h   |\  }}| j                          | j                  dk(  r| j                  ||f|      S | j                  | j	                  ||f|      S | j                  \  }}}d| _        | j
                  j                         \  }}	||	k(  ry| j
                  j                  |       | j                  ||f|      \  }
}}|
\  }}}}}|\  }}|\  }}|}|D ](  \  }}}||z  }||	k(  s| j                  ||f||d        y ||z   }|D ](  \  }}}||	k(  r| j                  ||f||d        y||z  }* | j
                  j                  |	       | j
                  j                         \  }}	|j                  |f|      }|dk(  rd}n|dk(  r||z
  }n||z
  dz  }| j                  ||f|       y)zO
        Finish setting the position now that we have maxcol & maxrow.
        rz   Nr   r   r      )r}   r   r   r   r   r|   r3   rX   r   change_focusr   r   )r+   r   r/   r   r   r   r   r   new_focus_widgetr7   r   r   r   focus_offsetr   r   r   r   r   r   offsetr   r   r   s                           r#   r   zListBox._set_focus_complete  s     !!%7733( (((422( (/3/E/E,\9!% &*ZZ%9%9%;"(  	

Y'00&Gs6BHAlIz6"*"(Z!+ 	FCdNFh!!66"2CG&	 
*!+ 	FCh!!66"2CG&dNF	 	

h(:://1{{F9e,F'!D[FTka'F&&)62r"   c                 :   |\  }}|dk\  r&||k\  rt        d|d|d      || _        d| _        n[| j                  j	                         \  }}|j                  |fd      }||z   dk  rt        d|d|d      d| _        | |f| _        | j                          y)	a  
        Move the location of the current focus relative to the top.
        This is used internally by methods that know the widget's *size*.

        See also :meth:`.set_focus_valign`.

        :param size: see :meth:`Widget.render` for details
        :param offset_inset: either the number of rows between the
            top of the listbox and the start of the focus widget (+ve
            value) or the number of lines of the focus widget hidden off
            the top edge of the listbox (-ve value) or ``0`` if the top edge
            of the focus widget is aligned with the top edge of the
            listbox.
        :type offset_inset: int
        r   Invalid offset_inset: , only z rows in list boxrx   T rows in target!N)rt   r   r   r|   r3   r   r}   )r+   r   r   r   r   targetr   tgt_rowss           r#   r   zListBox.shift_focus  s       1v%"Zfhn#opp+D"'D"jj224OFG{{VIt5Hh&!+"Yego#pqq D$0=":Dr"   c                    |\  }}| j                   j                         \  }}|yd}t        |d      r|j                  |f      }|5t        |d      r)|j	                  |f      }t        |      t        k(  r|\  }}||| _        yy)z+Update self.pref_col from the focus widget.Nget_pref_colr   )r|   r3   r   r   r   typetupler   )	r+   r   r   r   r   old_posr   coordsr   s	            r#   update_pref_col_from_focusz"ListBox.update_pref_col_from_focus  s      **..0>66.)**F95H/B C--vi8FF|u$#
$DM  r"   c                    |\  }}|r|d   | _         n| j                  ||f       | j                          | j                  j	                  |       | j                  j                         \  }	}
|	j                  |fd      }||dz
  }d}||z
  }|dk(  r0|	j                         r ||kD  r|||z
  k\  r|}n|||z
  k\  r|}n||z  }|dk(  r0|	j                         r ||k  r|||z
  k\  r|}n|||z
  k\  r|}n||z  }|dk\  r|| _        d| _	        n+||z   dk  rt        d|d	|d
      d| _        | |f| _	        ||y| j                   f}t        |	d      syg }t        |      dk(  r0|\  }|dk(  rt        d|      }nb|dk(  sJ d       t        |dd      }nH|\  }}|dk  s||k\  rt        d|d|      |dk(  rt        |dd      }n|dk(  rt        ||      }n|g}|D ]  }|	j                  |f||      s y y)a  
        Change the current focus widget.
        This is used internally by methods that know the widget's *size*.

        See also :meth:`.set_focus`.

        :param size: see :meth:`Widget.render` for details
        :param position: a position compatible with :meth:`self._body.set_focus`
        :param offset_inset: either the number of rows between the
            top of the listbox and the start of the focus widget (+ve
            value) or the number of lines of the focus widget hidden off
            the top edge of the listbox (-ve value) or 0 if the top edge
            of the focus widget is aligned with the top edge of the
            listbox (default if unspecified)
        :type offset_inset: int
        :param coming_from: either 'above', 'below' or unspecified `None`
        :type coming_from: str
        :param cursor_coords: (x, y) tuple indicating the desired
            column and row for the cursor, a (x,) tuple indicating only
            the column for the cursor, or unspecified
        :type cursor_coords: (int, int)
        :param snap_rows: the maximum number of extra rows to scroll
            when trying to "snap" a selectable focus into the view
        :type snap_rows: int
        r   TNrJ   r   r   rx   r   r   r   move_cursor_to_coordszLmust specify coming_from ('above' or 'below') if cursor row is not specifiedrc   z;cursor_coords row outside valid range for target. pref_row:z target_rows:)r   r   r}   r|   rX   r3   r   r   r   r   rt   r   rK   ranger  )r+   r   r7   r   r   cursor_coords	snap_rowsr   r   r   r   r   	align_topalign_bottomattempt_rowsr   pref_rowrows                     r#   r   zListBox.change_focus  s   8   )!,DM++VFO<

X&**..0;;	40
I 	(G#%%' </L<77+lY66(	)G#%%' 9,I44(l\99+	) 1+D"'Dh&!+"Yego#pqq D$0=":D "!]],Mv56}" (KXG#$a3"g-}/}}-$hB7 $1 Xx!|x83"s{  }E  $F  G  GG#$hB8g%$h: (z 	C++VIhsC	r"   c                 :   |\  }}| j                   j                         \  }}|j                  |fd      }| j                  }d}|dk(  rP| j                  \  }	}
|	dk  s
|
dk  s|	|
k\  rt        d| j                        ||	z  |
z  }|r||k\  rt        d      ||fS )z2Return (offset rows, inset rows) for focus widget.Tr   zInvalid inset_fraction: z*urwid inset_fraction error (please report))r|   r3   r   r   r   rt   )r+   r   r   r   r   r   r   r   r   inumidens              r#   r   zListBox.get_focus_offset_inset  s     JJ002c!&&y$7
&&
!,,JD$ax4!8tt|"ATAT#VWW#d*d2JjJ6"#OPPJ&&r"   c                 l   |\  }}| j                   j                         \  }}|y|j                         syt        |d      sy|j	                  |f      }|y|\  }}| j                  ||f      \  }	}
||
k  r| j                  ||f|        y|	|
z
  |z   |k\  r| j                  ||f||z
  dz
         yy)z5Shift the focus widget so that its cursor is visible.Nr   rJ   )r|   r3   r   r   r   r   r   )r+   r   r   r   r   r   r   r   r   r   r   s              r#   make_cursor_visiblezListBox.make_cursor_visible  s     JJ002c&&(|$78//	:>B"&"="=V#Z 
?vfo"v7#b(F2vfovby{< 3r"   c                 ^   |\  }}| j                   s| j                  r| j                  ||fd       | j                  j	                         \  }}|S |j                         r)|j                  |f      | j                  ||f       yfd}| j                     t        k(  r || j                  ||f            S | j                     t        k(  r || j                  ||f            S | j                     t        k(  r || j                  ||f            S | j                     t        k(  r || j!                  ||f            S | j                     t"        k(  r || j%                  ||f            S | j                     t&        k(  r || j)                  ||f            S S )a  Move selection through the list elements scrolling when
        necessary. Keystrokes are first passed to widget in focus
        in case that widget can handle them.

        Keystrokes handled by this widget are:
         'up'        up one line (or widget)
         'down'      down one line (or widget)
         'page up'   move cursor up one listbox length (or widget)
         'page down' move cursor down one listbox length (or widget)
        Tr   Nc                     | rS y r   r!   )	unhandledr   s    r#   
actual_keyz$ListBox.keypress.<locals>.actual_key  s    
 r"   )r   r   r   r|   r3   r   keypressr  _command_mapr   _keypress_upr   _keypress_downr   _keypress_page_upr   _keypress_page_downr   _keypress_max_leftr   _keypress_max_right)r+   r   r   r   r   r   r   r  s     `     r#   r  zListBox.keypress  s     !!T%B%B$$vfoT$C JJ002cJ""$''	#6C{((&9	
 S!Y.d//0@ABBS![0d11662BCDDS!^3d44ff5EFGGS!%55d667GHIIS!_4d55vv6FGHHS!%55d667GHII
r"   c                     t        t        | j                  j                                     | _        | j                  d       y)Nr   Tr   iterr   rg   focus_positionr   r+   r   s     r#   r  zListBox._keypress_max_left  s2    "4		(;(;(=#>?e$r"   c                     t        t        | j                  j                  d                  | _        | j                  d       y)NTrf   r   r  r   s     r#   r  zListBox._keypress_max_right  s7    "4		(;(;D(;(I#JKh'r"   c                 ~   |\  }}| j                  ||fd      \  }}}|y|\  }}}	}
}|\  }}|}|	}d }|D ]6  \  }}}||z  }|s|j                         s | j                  ||f||d        y  |dz  }| j                          |dkD  rh| j                  j                  |      \  }}|y|j                  |fd      }||z  }|r'|j                         r| j                  ||f||d       y |dkD  rh|j                         r|dz   |k\  r.|| j                  ||f|       y | j                  ||f||d       y |n|\  }}||z   dz   |k\  r^|9| j                  j                  |      \  }}|y |j                  |fd      }||z  }| k\  r|dz
   }| j                  ||f||d       y | j                  ||f|dz          y )NTr   rJ   r   )r   r   r   r}   r|   r=   r   r   )r+   r   r   r   r   r   r   focus_row_offsetr   r   r   r   r   r   r   r   r   r   r   r   s                       r#   r  zListBox._keypress_up  sG   "44FOT#V>$AG>i"*%
 !+ 	FC$J))+!!6&/3)	 	a
1n**--c2KFC~;;y$/D$J))+!!6&/3) 1n &&(,<Q,>,F ~  &*=vfosG% CAa!!!#v- >"&**"5"5c":KFC~!;;y$7D$&J;$&$(FJ!!6&/#) 	&*:1*<=r"   c                 l   |\  }}| j                  ||fd      \  }}}|y|\  }}}	}
}|\  }}||
z   }|
}|	}d }|D ]5  \  }}}|r(|j                         r| j                  ||f||d        y ||z  }7 |dz  }| j                          ||k  rg| j                  j                  |      \  }}|y|j                  |f      }|r'|j                         r| j                  ||f||d       y ||z  }||k  rg|j                         r||
z   dz
  dk  r4|| j                  ||f||z
         y | j                  ||f|||z
  d       y |Y|\  }}||z   dz
  dk  rI|!| j                  j                  |      \  }}|y ||z  }||k\  r|dz
  }| j                  ||f||d       y | j                  ||f|dz
         y )NTr   rJ   r   )r   r   r   r}   r|   r8   r   r   )r+   r   r   r   r   r   r   r$  r   r   r   r   r   r   r   r   r   r   r   r   s                       r#   r  zListBox._keypress_down=  sM   "44FOT#V>$BHAi
6"(Z%
2
 !+ 	FC))+!!6&/3)$J	 	a
6!**--c2KFC~;;y)D))+!!6&/3)$J 6! &&(,<Z,G,IQ,N ~  &tO% vfos4* CAa!!!#a' >"&**"5"5c":KFC~$&J'!'J!!6&/#+ 	&*:1*<=r"   c                 h   |\  }}| j                  ||fd      \  }}}|y|\  }}}	}
}|\  }}|}|j                         s|}n|	|\  }}| }n
|dk\  rd}n|}||z
  }||z   }d x}}g }|j                  |||	|
f       |	}|D ]   \  }}}||z  }|j                  ||||f       " t        |      }|| kD  r^| j                  j                  |      \  }}|n=|j                  |f      }||z  }|dkD  r|dz  }|j                  ||||f       || kD  r^|d   \  }}}}|dkD  r$| }|D cg c]  \  }}}}||z   |||f }}}}}|d   \  }}}}||k\  r|d= |dz  }| j                  ||f       t        t        |t        |                  t        t        |dz
  dd            z   }g }d}|D ]#  } ||    \  }}}}|j                         s|s"t        d|       }!||z   dk  r6| j                  ||f||dz
   d| j                  |dz
  f|| |dz
  z
  z
         n$| j                  ||f||d| j                  |!f|       |r%| j                  j                  |d   d         dk(  r	 | j                  ||fd      \  }}}|\  }"}#}$}%}&|"||z   kD  r|j                  |        |"|k  r|j                  |        
|"dk  r|j                  |        d}$ y  |ry |r5|j                         r%| j                  j                  |d   d         dk(  r	 |D 'cg c]	  }'|'|vs|' }(}'|(|z   D ]H  } ||    \  }}}}||	k(  r|s||z   dk  r|| |dz
  z
  z  }|dz
   }| j                  ||f||dd |        y  | j                  ||ft        |dz
  |             | j                  ||fd      \  }}}|\  }"}#}}$}%|"|k\  ry |sy |d   \  }#}$}}%| j                  j                  |      \  }}|y |j                  |fd      }| j                  ||f||dz
   d| j                  |dz
  fd       y c c}}}}w c c}'w )NTr   rJ   rc   r   r.   )r   r   r   rK   r|   r=   r   r   listr   rL   r   r   r   min))r+   r   r   r   r   r   r   r   r   r   r   r   r   r   topmost_visiblescroll_from_rowr   r   r  tr   r   r   snap_region_startr   pradjustrosearch_orderbad_choicescut_off_selectable_chosenir	  act_row_offset_ign1_ign2_ign3_ign4jgood_choicess)                                            r#   r  zListBox._keypress_page_up  s   "44FOT#V>$BH?
L)Z"* % &&(-OCAa bO1_O-O $o5	 %v-
 -10/ 	*\)J?@!+ 	8FC$JHHz6357	8  FI:%**--c2KFC~u;;y)D$JA~!Q&!HHz6357 I:%  e
Aq!>!\F89::*2a!"V)Q1%:A:  d
Aq!!" 	''8 V$5s1v>?6+A-r2678 $%! /	A,-aD)JT$$& 1zk*H j A%!!F6?C1fIw]]DF+d1f568
 !!F6?C]]H-y;
 tzz22:b>!3DE  #'"8"8#'FC9?6NE5% 
9 44""1%
*""1% !""1%,-)_/	d %,113zz"":b>!#45D $0Ha1K3GHH, 	A,-aD)JTi j A%zkd1f55	#AvY
vGT 	$ 	&&)3vax
+CD #44FOT#V390sE5Z' #$R5 uc5jj))#.>{{F9d+6&/3$q&	dmmT!V,a	2i ;Z Is   ,P'
	P/P/c           
         |\  }}| j                  ||fd      \  }}}|y|\  }}}	}
}|\  }}||z
  }|j                         s|}n||\  }}|dz   }n
||
k\  r|
}n|}||z
  }| }d x}}g }|j                  |||	|
f       |	}||
z  }|D ]   \  }}}|j                  ||||f       ||z  }" t        |      }|||z   k  r`| j                  j                  |      \  }}|n?|j                  |f      }|j                  ||||f       ||z  }||k  r|dz  }|||z   k  r`|d   \  }}}}||z   |k  r)|||z   z
  }|D cg c]  \  }}}}||z   |||f }}}}}|d   \  }}}}||z   dk  r|d= |dz  }| j                  ||f       t        t        |t        |                  t        t        |dz
  dd            z   }g }d}|D ]  } ||    \  }}}}|j                         s|s!t        ||z
  dz
  |dz
        }!||k\  r1| j                  ||f||dz
  d| j                  df||z   |z
  dz
         n$| j                  ||f||d| j                  |!f|       | j                  ||fd      \  }}}|\  }"}#}$}%}&|"||z
  k  r|j                  |        |"|kD  r|j                  |        |"|z   |kD  r|j                  |        d} y  |ry |D 'cg c]	  }'|'|vs|' }(}'|(|z   D ]F  } ||    \  }}}}||	k(  r|s||k\  r|||z   |z
  dz
  z  }|dz
  }| j                  ||f||dd |        y  | j                  ||ft        d|
z
  |             | j                  ||fd      \  }}}|\  }"}#}}$}%|"|k  ry |sy |d   \  }#}$}}%| j                  j                  |      \  }}|y |j                  |fd      }| j                  ||f||dz
  d| j                  dfd       y c c}}}}w c c}'w )NTrJ   rc   r   r   )r   r   r   rK   r|   r8   r   r   r'  r   r(  r   r   r   rL   ))r+   r   r   r   r   r   r   r   r   r   r   r   r   r   bottom_edger*  r   r   r  r+  r   r   r   r,  r   r-  r/  r0  r.  r1  r2  r3  r4  r	  r5  r6  r7  r8  r9  r:  r;  s)                                            r#   r  zListBox._keypress_page_downH  sl   "44FOT#V>$BH?
L)Z"(Z z) &&()OCAa!eOJ&(O)O  /1	 &%
 )-,+ 	*\)J?@j 
!+ 	FCHHz6357$J	  F6)++**--c2KFC~u;;y)DHHz6357$JF"!Q&! 6)++ "#2
Aq$v%zD01F89::*2a!"V)Q1%:A: "#1
Aq$d?a!" 	''8 V$5s1v>?6+A-r2678 $%! *	A,-aD)JT$$& 6*,Q.Q7H V#!!F6?C1Hg]]A&f$Z/14
 !!F6?C]]H-y;
 #'"8"8#'FC9?6NE5% 
9 44""1%
*""1% d"V+""1%,-)U*	Z % $0Ha1K3GHH, 	A,-aD)JTi V#Yv-j8::	#AX
vGT 	& 	&&)3q|J+GH #44FOT#V390sE5Z' #$R5 uc5jj))#.>{{F9d+6&/3qdmmQ'	-W ;F Is    O
	O"O"c                    |\  }}| j                  ||fd      \  }	}
}|	y|	\  }}}}}|
\  }}|\  }}|j                          ||||fgz   |z   }| }|D ]  \  }}}||z   |kD  r n||z  } y|xr ||k(  }t        |      r*|dk(  r%|j                         r| j	                  ||f||       t        |d      sy|j                  |f|||||z
  |      S )zf
        Pass the event to the contained widgets.
        May change focus on button 1 press.
        Tr   FrJ   mouse_event)r   rf   r   r   r   r   r?  )r+   r   eventbuttoncolr
  r/   r   r   r   r   r   r   r   r   r   r   r   r   r   w_listwrowr   r   r   s                            r#   r?  zListBox.mouse_event  s4   
  "44ff5E 5 V>?E<y*f"*$Y
356 y & 	Auff}s"FND	
 +!|+% VQY||~!!6&/5$?q'}}fYvsCH 	r"   c                    |\  }}g }| j                  ||f|      \  }}}|ddgS |\  }	}
|\  }}|dk(  rc|\  }}}}}||z  }|D ]  \  }}}||z  } ||k  r|j                  d       n/| j                  j                  |      dk(  r|j                  d       |	dk(  rH|\  }}}}}|
D ]  \  }}}||z  } | j                  j	                  |      dk(  r|j                  dd       |S )a+  
        Return a list that may contain ``'top'`` and/or ``'bottom'``.

        i.e. this function will return one of: [], [``'top'``],
        [``'bottom'``] or [``'top'``, ``'bottom'``].

        convenience function for checking whether the top and bottom
        of the list are visible
        r   r   r   r   r.   )r   r   r|   r8   r=   insert)r+   r   r/   r   r   lr   r   r   r   r   r   r   r   r   r   r   cs                     r#   ends_visiblezListBox.ends_visible  s3      22VFO 3 s6>(##%#U!*0'J3a$J % #3d"
#F""$$S)[8"q=*0'J3a % #3d"
#zz""3';6E"r"   c              #   B  K   t        | j                  dd      }|r |       D ]  }|  y| j                  j                         \  }}|y|}	 | | j                  j                  |      \  }}|sn&|}	 | j                  j	                  |      \  }}|sy| &w)a=  
        Return an iterator over the positions in this ListBox.

        If self._body does not implement positions() then iterate
        from the focus widget down to the bottom, then from above
        the focus up to the top.  This is the best we can do with
        a minimal list walker implementation.
        rg   N)rB   r|   r3   r8   r=   r+   positions_fnr   r   r   r   s         r#   __iter__zListBox.__iter__E  s      tzz;=#~ 	"&**"6"6"8iIZZ((-FAse  ZZ((-FAseI s   BBc              #   F  K   t        | j                  dd      }|r |d      D ]  }|  y| j                  j                         \  }}|y|}	 | j                  j                  |      \  }}|sn| &|}	 | | j                  j	                  |      \  }}|sy&w)a  
        Return a reversed iterator over the positions in this ListBox.

        If :attr:`body` does not implement :meth:`positions` then iterate
        from above the focus widget up to the top, then from the focus
        widget down to the bottom.  Note that this is not actually the
        reverse of what `__iter__()` produces, but this is the best we can
        do with a minimal list walker implementation.
        rg   NTr"  )rB   r|   r3   r=   r8   rK  s         r#   __reversed__zListBox.__reversed__b  s      tzz;=#D1 	"&**"6"6"8iZZ((-FAseI  IZZ((-FAse s   BB!ri   r   )r   NNN).r   r   r    __doc___selectable	frozensetr   _sizingrC   r   r{   rj   r   r   r   r   r   rX   r3   r   r/   r   r  r   r   rF   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r?  rI  rM  rO  r!   r"   r#   rv   rv      s-    KG -F Iy / 	DD?N@F,
/'&&)
 ZRTE	 19 C N!,$ 3 : H1(#>83v@%( -1.2pd'"61f

J>ZO>dv2rm-^%P$L:r"   rv   N),
__future__r   r   urwid.compatr   r   
urwid.utilr   urwid.canvasr   r	   urwid.widgetr
   r   r   r   urwid.decorationr   r   urwidr   urwid.signalsr   urwid.monitored_listr   r   urwid.containerr   urwid.command_mapr   r   r   r   r   r   	Exceptionr   MetaSignalsr   r&   r?   rl   rt   rv   r!   r"   r#   <module>ra     s   , 0 / % 3 K K J  ( B 0I I	i 	+ 3 3V< +ZP!}j P!fA!J(: A!H	9 	Pf* Pr"   