! Copyright (C) 2005 Slava Pestov. ! See http://factor.sf.net/license.txt for BSD license. IN: gadgets-panes USING: gadgets gadgets-editors gadgets-labels gadgets-layouts gadgets-scrolling generic hashtables io kernel line-editor lists math namespaces prettyprint sequences strings styles threads vectors ; DEFER: ! A pane is an area that can display text. ! output: pile ! current: shelf ! input: editor TUPLE: pane output active current input continuation ; : add-output 2dup set-pane-output add-gadget ; : add-input 2dup set-pane-input add-gadget ; : ( input current -- line ) 2vector [ add-gadgets ] keep ; : init-active-line ( pane -- ) dup pane-active unparent [ dup pane-current swap pane-input ] keep 2dup set-pane-active add-gadget ; : pop-continuation ( pane -- quot ) dup pane-continuation f rot set-pane-continuation ; : pane-eval ( string pane -- ) pop-continuation in-thread drop ; SYMBOL: structured-input : elements. ( quot -- ) [ 2 nesting-limit set 5 length-limit set newline ] with-pprint ; : pane-call ( quot pane -- ) 2dup [ elements. ] with-stream* >r structured-input global set-hash "structured-input global hash call" r> pane-eval ; : editor-commit ( editor -- line ) #! Add current line to the history, and clear the editor. [ commit-history line-text get line-clear ] with-editor ; : pane-return ( pane -- ) [ pane-input editor-commit ] keep 2dup stream-print pane-eval ; : pane-clear ( pane -- ) dup pane-output clear-incremental pane-current clear-gadget ; : pane-actions ( line -- ) [ [[ [ button-down 1 ] [ pane-input click-editor ] ]] [[ [ "RETURN" ] [ pane-return ] ]] [[ [ "UP" ] [ pane-input [ history-prev ] with-editor ] ]] [[ [ "DOWN" ] [ pane-input [ history-next ] with-editor ] ]] [[ [ "CTRL" "l" ] [ pane get pane-clear ] ]] ] swap add-actions ; C: pane ( -- pane ) over set-delegate over add-output over set-pane-current "" over set-pane-input dup init-active-line dup pane-actions ; M: pane focusable-child* ( pane -- editor ) pane-input ; : pane-write-1 ( style text pane -- ) pick not pick empty? and [ 3drop ] [ >r r> pane-current add-gadget ] ifte ; : prepare-print ( current -- gadget ) #! Optimization: if line has 1 child, add the child. dup gadget-children { { [ dup empty? ] [ 2drop ""