factor/library/ui/menus.factor

51 lines
1.4 KiB
Factor
Raw Normal View History

2005-03-01 22:11:08 -05:00
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets
USING: generic kernel lists math namespaces sequences ;
2005-03-01 22:11:08 -05:00
2005-03-02 21:26:11 -05:00
: hide-menu ( -- )
world get
2005-03-01 22:11:08 -05:00
dup world-menu [ unparent ] when* f swap set-world-menu ;
: show-menu ( menu -- )
2005-03-02 21:26:11 -05:00
hide-menu
world get
2005-03-01 22:11:08 -05:00
2dup set-world-menu
2dup world-hand screen-pos >rect rot move-gadget
add-gadget ;
: menu-item-border ( child -- border )
2005-06-23 03:15:44 -04:00
<plain-gadget> 1 <border> ;
2005-03-01 22:11:08 -05:00
: <menu-item> ( label quot -- gadget )
2005-05-03 19:00:52 -04:00
>r <label> menu-item-border dup r> button-gestures ;
2005-03-01 22:11:08 -05:00
TUPLE: menu ;
2005-03-01 22:11:08 -05:00
: menu-actions ( menu -- )
[ drop hide-menu ] [ button-down 1 ] set-action ;
2005-03-02 21:26:11 -05:00
: assoc>menu ( assoc menu -- )
#! Given an association list mapping labels to quotations.
#! Prepend a call to hide-menu to each quotation.
[
uncons \ hide-menu swons <menu-item> swap add-gadget
] each-with ;
2005-03-01 22:11:08 -05:00
C: menu ( assoc -- gadget )
#! Given an association list mapping labels to quotations.
[ f line-border swap set-delegate ] keep
2005-03-01 22:11:08 -05:00
<line-pile> [ swap add-gadget ] 2keep
2005-03-02 21:26:11 -05:00
rot assoc>menu dup menu-actions ;
2005-03-01 22:11:08 -05:00
! While a menu is open, clicking anywhere sends the click to
! the menu.
M: menu inside? ( point menu -- ? ) 2drop t ;
2005-03-03 20:43:55 -05:00
: actionize ( obj assoc -- assoc )
#! Prepends an object to each cdr of the assoc list. Utility
#! word for constructing menu action association lists.
[
unswons >r >r unit [ car ] cons r> append r> swons
] map-with ;