factor/library/ui/gadgets/panes.factor

147 lines
4.3 KiB
Factor
Raw Normal View History

2006-06-07 23:51:28 -04:00
! Copyright (C) 2005, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: gadgets-panes
2005-10-07 20:26:21 -04:00
USING: arrays gadgets gadgets-buttons gadgets-editors
2006-06-07 23:51:28 -04:00
gadgets-frames gadgets-grids gadgets-labels gadgets-scrolling
gadgets-theme generic hashtables io kernel line-editor math
namespaces prettyprint sequences strings styles threads ;
2005-06-27 00:54:49 -04:00
2006-05-26 17:44:00 -04:00
TUPLE: pane output active current input prototype continuation ;
2005-06-27 00:54:49 -04:00
: add-output 2dup set-pane-output add-gadget ;
2005-09-25 00:18:12 -04:00
: <active-line> ( current input -- line )
{ { [ ] f @center } { [ ] f @left } } make-frame ;
2005-06-27 00:54:49 -04:00
2005-12-16 21:12:35 -05:00
: init-line ( pane -- )
dup pane-prototype clone swap set-pane-current ;
: prepare-line ( pane -- )
dup init-line dup pane-active unparent
2005-08-26 21:42:43 -04:00
[ dup pane-current swap pane-input <active-line> ] keep
2005-06-27 00:54:49 -04:00
2dup set-pane-active add-gadget ;
: pop-continuation ( pane -- quot )
dup pane-continuation f rot set-pane-continuation ;
2005-07-06 03:29:42 -04:00
: pane-eval ( string pane -- )
2005-12-17 00:12:32 -05:00
pop-continuation dup [
[ continue-with ] in-thread
] when 2drop ;
2005-08-26 00:55:56 -04:00
SYMBOL: structured-input
2005-07-06 03:29:42 -04:00
: pane-call ( quot pane -- )
dup [ "Command: " write over short. ] with-stream*
>r structured-input set-global
"\"structured-input\" \"gadgets-panes\" lookup get-global call"
2005-09-03 18:44:45 -04:00
r> pane-eval ;
2005-08-26 00:55:56 -04:00
: replace-input ( string pane -- ) pane-input set-editor-text ;
2005-10-01 01:44:49 -04:00
2005-09-28 23:29:00 -04:00
: print-input ( string pane -- )
2005-12-17 00:12:32 -05:00
[
dup [
<input> presented set
bold font-style set
] make-hash format terpri
] with-stream* ;
2005-09-28 23:29:00 -04:00
2005-12-17 00:12:32 -05:00
: pane-commit ( pane -- )
2006-05-18 22:01:38 -04:00
dup pane-input commit-editor-text
swap 2dup print-input pane-eval ;
: pane-clear ( pane -- )
dup pane-output clear-incremental pane-current clear-gadget ;
2005-06-27 00:54:49 -04:00
2005-12-17 00:12:32 -05:00
C: pane ( -- pane )
<pile> over set-delegate
1 over set-pack-fill
2005-12-17 00:12:32 -05:00
<shelf> over set-pane-prototype
<pile> <incremental> over add-output
2005-12-28 20:25:17 -05:00
dup prepare-line ;
2005-06-27 00:54:49 -04:00
2006-05-26 17:40:41 -04:00
M: pane gadget-gestures
pane-input [
H{
{ T{ key-down f f "RETURN" } [ pane-commit ] }
{ T{ key-down f f "UP" } [ pane-input [ history-prev ] with-editor ] }
{ T{ key-down f f "DOWN" } [ pane-input [ history-next ] with-editor ] }
{ T{ key-down f { C+ } "l" } [ pane-clear ] }
}
] [
H{ }
] if ;
2005-12-17 00:12:32 -05:00
: <input-pane> ( -- pane )
2006-05-26 17:40:41 -04:00
<pane> "" <editor> over set-pane-input ;
2005-12-17 00:12:32 -05:00
M: pane focusable-child* ( pane -- editor )
2005-09-25 00:18:12 -04:00
pane-input [ t ] unless* ;
2005-08-24 21:52:10 -04:00
: prepare-print ( current -- gadget )
#! Optimization: if line has 1 child, add the child.
dup gadget-children {
{ [ dup empty? ] [ 2drop "" <label> ] }
{ [ dup length 1 = ] [ nip first ] }
{ [ t ] [ drop ] }
} cond ;
2005-08-24 21:52:10 -04:00
2005-12-16 21:12:35 -05:00
M: pane stream-terpri ( pane -- )
dup pane-current prepare-print
over pane-output add-incremental
prepare-line ;
2005-08-24 21:52:10 -04:00
2005-12-28 20:25:17 -05:00
: pane-write ( pane seq -- )
[ over pane-current stream-write ]
[ dup stream-terpri ] interleave drop ;
2005-06-27 00:54:49 -04:00
2005-12-28 20:25:17 -05:00
: pane-format ( style pane seq -- )
[ pick pick pane-current stream-format ]
[ dup stream-terpri ] interleave 2drop ;
2005-06-27 00:54:49 -04:00
2005-12-17 20:03:41 -05:00
: write-gadget ( gadget pane -- )
#! Print a gadget to the given pane.
pane-current add-gadget ;
: gadget. ( gadget -- )
#! Print a gadget to the current pane.
stdio get write-gadget terpri ;
2005-06-27 00:54:49 -04:00
! Panes are streams.
M: pane stream-flush ( pane -- ) drop ;
M: pane stream-readln ( pane -- line )
2005-09-18 01:37:28 -04:00
[ over set-pane-continuation stop ] callcc1 nip ;
2005-06-27 00:54:49 -04:00
2006-05-26 17:40:41 -04:00
: scroll-pane ( pane -- ) pane-input [ scroll>caret ] when* ;
2005-09-25 00:18:12 -04:00
M: pane stream-write1 ( char pane -- )
2005-12-16 21:12:35 -05:00
[ pane-current stream-write1 ] keep scroll-pane ;
2005-12-16 22:24:39 -05:00
M: pane stream-write ( string pane -- )
[ swap "\n" split pane-write ] keep scroll-pane ;
2005-06-27 00:54:49 -04:00
2005-12-16 21:12:35 -05:00
M: pane stream-format ( string style pane -- )
[ rot "\n" split pane-format ] keep scroll-pane ;
M: pane stream-close ( pane -- ) drop ;
2005-09-25 00:18:12 -04:00
: ?terpri
dup pane-current gadget-children empty?
[ dup stream-terpri ] unless drop ;
: with-pane ( pane quot -- )
#! Clear the pane and run the quotation in a scope with
#! stdio set to the pane.
over pane-clear over >r with-stream* r> ?terpri ; inline
2005-12-17 20:03:41 -05:00
: make-pane ( quot -- pane )
#! Execute the quotation with output to an output-only pane.
<pane> [ swap with-pane ] keep ; inline
2006-06-07 23:04:37 -04:00
M: pane with-stream-table ( quot grid pane -- )
>r [ [ swap make-pane ] map-with ] map-with
<grid> 5 over set-grid-gap
2006-06-07 23:51:28 -04:00
r> [ write-gadget ] keep stream-terpri ;