factor/library/ui/tools/workspace.factor

319 lines
8.1 KiB
Factor
Raw Normal View History

! Copyright (C) 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2006-08-26 14:30:58 -04:00
USING: arrays gadgets gadgets-listener gadgets-buttons
gadgets-walker gadgets-help gadgets-walker sequences
2006-08-26 17:13:24 -04:00
gadgets-browser gadgets-books gadgets-frames gadgets-controls
2006-08-27 19:02:16 -04:00
gadgets-grids gadgets-presentations kernel models namespaces
styles words help parser inspector memory generic threads
gadgets-text definitions inference test prettyprint math strings
hashtables ;
IN: gadgets-workspace
2006-08-26 17:13:24 -04:00
2006-08-27 19:02:16 -04:00
GENERIC: call-tool* ( arg tool -- )
TUPLE: tool gadget ;
2006-08-26 17:13:24 -04:00
C: tool ( gadget -- tool )
{
{ [ dup <toolbar> ] f f @top }
2006-08-27 19:02:16 -04:00
{ [ ] set-tool-gadget f @center }
2006-08-26 17:13:24 -04:00
} make-frame* ;
2006-08-27 19:02:16 -04:00
M: tool focusable-child* tool-gadget ;
2006-08-26 17:13:24 -04:00
2006-08-27 19:02:16 -04:00
M: tool call-tool* tool-gadget call-tool* ;
2006-08-27 19:02:16 -04:00
TUPLE: workspace ;
: workspace-tabs
{
2006-08-31 21:58:15 -04:00
{ "Listener" <listener-gadget> }
{ "Definitions" <browser> }
{ "Documentation" <help-gadget> }
{ "Walker" <walker-gadget> }
} ;
2006-08-27 19:02:16 -04:00
C: workspace ( -- workspace )
2006-08-31 21:58:15 -04:00
workspace-tabs [ second execute <tool> ] map <book>
over set-gadget-delegate dup dup set-control-self ;
2006-08-27 19:02:16 -04:00
2006-08-31 21:58:15 -04:00
M: workspace pref-dim* delegate pref-dim* { 550 650 } vmax ;
2006-08-26 17:13:24 -04:00
: <workspace-tabs> ( book -- tabs )
control-model
workspace-tabs dup length [ swap first 2array ] 2map
<radio-box> ;
2006-08-26 17:13:24 -04:00
: init-status ( world -- )
dup world-status <presentation-help> swap @bottom grid-add ;
2006-08-27 19:02:16 -04:00
: init-tabs ( world -- )
[ world-gadget <workspace-tabs> ] keep @top grid-add ;
2006-08-26 17:13:24 -04:00
2006-08-27 19:02:16 -04:00
: workspace-window ( -- workspace )
2006-08-26 17:13:24 -04:00
<workspace> dup <world>
[ init-status ] keep
[ init-tabs ] keep
open-window ;
2006-08-27 19:02:16 -04:00
: show-tool ( class workspace -- tool )
2006-08-31 22:36:31 -04:00
[ book-pages [ tool-gadget class eq? ] find-with swap ] keep
2006-08-31 21:58:15 -04:00
control-model set-model* ;
2006-08-27 19:02:16 -04:00
: find-workspace ( -- workspace )
[ workspace? ] find-window
[ world-gadget ] [ workspace-window find-workspace ] if* ;
: call-tool ( arg class -- )
find-workspace show-tool call-tool* ;
: commands-window ( workspace -- )
dup find-world world-focus [ ] [ gadget-child ] ?if
[ commands. ] "Commands" pane-window ;
2006-08-31 21:58:15 -04:00
: select-tool ( workspace class -- ) swap show-tool drop ;
: tool-window ( class -- ) workspace-window show-tool drop ;
workspace {
{ f "Keyboard help" T{ key-down f f "F1" } [ commands-window ] }
{ f "Listener" T{ key-down f f "F2" } [ listener-gadget select-tool ] }
2006-08-31 21:58:15 -04:00
{ f "Definitions" T{ key-down f f "F3" } [ browser select-tool ] }
{ f "Documentation" T{ key-down f f "F4" } [ help-gadget select-tool ] }
{ f "Walker" T{ key-down f f "F5" } [ walker-gadget select-tool ] }
{ f "New listener" T{ key-down f { S+ } "F2" } [ listener-gadget tool-window drop ] }
{ f "New definitions" T{ key-down f { S+ } "F3" } [ browser tool-window drop ] }
{ f "New documentation" T{ key-down f { S+ } "F4" } [ help-gadget tool-window drop ] }
} define-commands
2006-08-28 18:14:54 -04:00
! Walker tool
M: walker-gadget call-tool* ( arg tool -- )
>r first2 r> (walk) ;
: walk ( quot -- )
continuation dup continuation-data pop* 2array
walker-gadget call-tool stop ;
2006-08-27 19:02:16 -04:00
! Listener tool
G: call-listener ( quot/string listener -- )
1 standard-combination ;
M: quotation call-listener
listener-gadget-input interactor-call ;
M: string call-listener
listener-gadget-input set-editor-text ;
M: input call-listener
>r input-string r> call-listener ;
2006-08-27 19:02:16 -04:00
M: listener-gadget call-tool* ( quot/string listener -- )
call-listener ;
2006-08-27 19:02:16 -04:00
: listener-run-files ( seq -- )
dup empty? [
drop
] [
[ [ run-file ] each ] curry listener-gadget call-tool
] if ;
! Browser tool
M: browser call-tool*
over vocab-link? [
>r vocab-link-name r> show-vocab
] [
show-word
] if ;
! Help tool
M: help-gadget call-tool* show-help ;
! Operations
V{ } clone operations set-global
: define-operation ( class props -- )
<operation> operations get push-new ;
2006-08-31 21:59:57 -04:00
M: operation invoke-command ( target operation -- )
dup command-quot swap operation-listener?
[ curry listener-gadget call-tool ] [ call ] if ;
: modify-operation ( quot operation -- operation )
clone
[ command-quot append ] keep
[ set-command-quot ] keep ;
: modify-operations ( quot operations -- operations )
[ modify-operation ] map-with ;
: modify-listener-operation ( quot operation -- operation )
clone t over set-operation-listener?
modify-operation ;
: modify-listener-operations ( quot operations -- operations )
[ modify-listener-operation ] map-with ;
2006-08-31 21:59:57 -04:00
! Objects
2006-09-01 01:20:38 -04:00
[ drop t ] H{
{ +button+ 1 }
{ +name+ "Inspect" }
{ +quot+ [ inspect ] }
{ +listener+ t }
} define-operation
! Input
2006-09-01 01:20:38 -04:00
[ input? ] H{
{ +button+ 1 }
{ +name+ "Input" }
{ +quot+ [ listener-gadget call-tool ] }
} define-operation
! Words
2006-09-01 01:20:38 -04:00
[ word? ] H{
{ +button+ 1 }
{ +name+ "Browse" }
{ +gesture+ T{ key-down f { A+ } "b" } }
{ +quot+ [ browser call-tool ] }
} define-operation
2006-09-01 01:20:38 -04:00
[ word? ] H{
{ +button+ 2 }
{ +name+ "Edit" }
{ +gesture+ T{ key-down f { A+ } "e" } }
{ +quot+ [ edit ] }
} define-operation
2006-09-01 01:20:38 -04:00
[ word? ] H{
{ +button+ 3 }
{ +name+ "Documentation" }
{ +gesture+ T{ key-down f { A+ } "h" } }
{ +quot+ [ help-gadget call-tool ] }
} define-operation
2006-09-01 01:20:38 -04:00
[ word? ] H{
{ +name+ "Usage" }
{ +gesture+ T{ key-down f { A+ } "u" } }
{ +quot+ [ usage. ] }
{ +listener+ t }
} define-operation
2006-09-01 01:20:38 -04:00
[ word? ] H{
{ +name+ "Reload" }
{ +gesture+ T{ key-down f { A+ } "r" } }
{ +quot+ [ reload ] }
{ +listener+ t }
} define-operation
2006-09-01 01:20:38 -04:00
[ word? ] H{
{ +name+ "Watch" }
{ +quot+ [ watch ] }
{ +listener+ t }
} define-operation
! Vocabularies
2006-09-01 01:20:38 -04:00
[ vocab-link? ] H{
{ +button+ 1 }
{ +name+ "Browse" }
{ +quot+ [ browser call-tool ] }
} define-operation
! Link
2006-09-01 01:20:38 -04:00
[ link? ] H{
{ +button+ 1 }
{ +name+ "Follow" }
{ +quot+ [ help-gadget call-tool ] }
} define-operation
2006-09-01 01:20:38 -04:00
[ link? ] H{
{ +button+ 2 }
{ +name+ "Edit" }
{ +quot+ [ edit ] }
} define-operation
2006-09-01 01:20:38 -04:00
[ word-link? ] H{
2006-09-01 01:27:51 -04:00
{ +button+ 3 }
2006-09-01 01:20:38 -04:00
{ +name+ "Definition" }
{ +quot+ [ link-name browser call-tool ] }
} define-operation
! Strings
2006-09-01 01:20:38 -04:00
[ string? ] H{
{ +name+ "Apropos (all)" }
{ +gesture+ T{ key-down f { A+ } "a" } }
{ +quot+ [ apropos ] }
{ +listener+ t }
} define-operation
: usable-words ( -- seq )
[
use get [ hash-values [ dup set ] each ] each
] make-hash hash-values natural-sort ;
2006-09-01 01:20:38 -04:00
[ string? ] H{
{ +name+ "Apropos (used)" }
{ +gesture+ T{ key-down f f "TAB" } }
{ +quot+ [ usable-words (apropos) ] }
{ +listener+ t }
} define-operation
! Quotations
2006-09-01 01:20:38 -04:00
[ quotation? ] H{
{ +name+ "Infer" }
{ +gesture+ T{ key-down f { C+ A+ } "i" } }
2006-08-31 21:59:57 -04:00
{ +quot+ [ infer . ] }
{ +listener+ t }
} define-operation
2006-09-01 01:20:38 -04:00
[ quotation? ] H{
{ +name+ "Walk" }
{ +gesture+ T{ key-down f { C+ A+ } "w" } }
{ +quot+ [ walk ] }
{ +listener+ t }
} define-operation
2006-09-01 01:20:38 -04:00
[ quotation? ] H{
{ +name+ "Time" }
{ +gesture+ T{ key-down f { C+ A+ } "t" } }
{ +quot+ [ time ] }
{ +listener+ t }
} define-operation
! Define commands in terms of operations
! Tile commands
tile
[ tile-definition ] \ word class-operations modify-operations
[ command-name "Browse" = not ] subset
T{ command f f "Close" f [ close-tile ] } add*
define-commands*
! Interactor commands
: selected-word ( editor -- string )
dup gadget-selection?
2006-08-31 21:58:15 -04:00
[ dup T{ word-elt } select-elt ] unless
gadget-selection ;
: word-action ( target -- quot )
selected-word search ;
2006-08-31 21:58:15 -04:00
: quot-action ( quot -- quot )
field-commit parse ;
interactor [
{
{ f "Evaluate" T{ key-down f f "RETURN" } [ interactor-commit ] }
{ f "Send EOF" T{ key-down f { C+ } "d" } [ f swap interactor-eval ] }
} <commands> %
[ word-action ] \ word class-operations modify-listener-operations %
[ selected-word ] string class-operations modify-listener-operations %
[ quot-action ] quotation class-operations modify-listener-operations %
{
{ f "History" T{ key-down f { C+ } "h" } [ [ interactor-history. ] swap interactor-call ] }
{ f "Clear output" T{ key-down f f "CLEAR" } [ [ clear-output ] swap interactor-call ] }
2006-08-31 21:58:15 -04:00
{ f "Clear stack" T{ key-down f { C+ } "CLEAR" } [ [ clear ] swap interactor-call ] }
} <commands> %
] { } make define-commands*