1 ! Copyright (C) 2005, 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
6 IN: gadgets-presentations
7 USING: arrays definitions gadgets gadgets-borders
8 gadgets-buttons gadgets-grids gadgets-labels gadgets-outliner
9 gadgets-panes gadgets-paragraphs gadgets-theme
10 generic hashtables tools io kernel prettyprint sequences strings
11 styles words help math models namespaces ;
14 TUPLE: presentation object command ;
16 C: presentation ( button object command -- button )
17 [ set-presentation-command ] keep
18 [ set-presentation-object ] keep
19 [ set-gadget-delegate ] keep ;
21 : <object-presentation> ( gadget object -- button )
22 >r f <roll-button> r> f <presentation> ;
24 : <command-presentation> ( target command -- button )
25 dup command-name f <bevel-button> -rot <presentation> ;
27 : <commands-menu> ( target commands -- gadget )
28 [ hand-clicked get find-world hide-glass ] modify-operations
29 [ <command-presentation> ] map-with
30 make-pile 1 over set-pack-fill ;
32 : operations-menu ( presentation -- gadget )
33 dup presentation-object
34 dup object-operations <commands-menu>
37 : invoke-presentation ( presentation -- )
38 dup presentation-object swap presentation-command
39 [ dup default-operation ] unless*
42 : show-mouse-help ( presentation -- )
43 dup find-world [ world-status set-model* ] [ drop ] if* ;
45 : hide-mouse-help ( presentation -- )
46 find-world [ world-status f swap set-model* ] when* ;
48 M: presentation ungraft* ( presentation -- )
49 dup hide-mouse-help delegate ungraft* ;
52 { T{ button-up } [ [ invoke-presentation ] if-clicked ] }
53 { T{ button-down f f 3 } [ [ operations-menu ] if-clicked ] }
54 { T{ mouse-leave } [ dup hide-mouse-help button-update ] }
55 { T{ mouse-enter } [ dup show-mouse-help button-update ] }
58 ! Presentation help bar
59 : <presentation-help> ( model -- gadget )
60 [ [ presentation-object summary ] [ "" ] if* ] <filter>
61 <label-control> dup reverse-video-theme ;
63 : <listener-button> ( gadget quot -- button )
64 [ call-listener ] curry <roll-button> ;
68 : apply-style ( style gadget key quot -- style gadget )
69 >r pick hash r> when* ; inline
71 : apply-foreground-style ( style gadget -- style gadget )
72 foreground [ over set-label-color ] apply-style ;
74 : apply-background-style ( style gadget -- style gadget )
75 background [ <solid> over set-gadget-interior ] apply-style ;
77 : specified-font ( style -- font )
78 [ font swap hash [ "monospace" ] unless* ] keep
79 [ font-style swap hash [ plain ] unless* ] keep
80 font-size swap hash [ 12 ] unless* 3array ;
82 : apply-font-style ( style gadget -- style gadget )
83 over specified-font over set-label-font ;
85 : apply-presentation-style ( style gadget -- style gadget )
86 presented [ <object-presentation> ] apply-style ;
88 : apply-quotation-style ( style gadget -- style gadget )
89 quotation [ <listener-button> ] apply-style ;
91 : <styled-label> ( style text -- gadget )
93 apply-foreground-style
94 apply-background-style
96 apply-presentation-style
102 : apply-wrap-style ( style pane -- style pane )
104 2dup <paragraph> swap set-pane-prototype
105 <paragraph> over set-pane-current
108 : apply-border-width-style ( style gadget -- style gadget )
109 border-width [ <border> ] apply-style ;
111 : apply-border-color-style ( style gadget -- style gadget )
113 <solid> over set-gadget-boundary
116 : apply-page-color-style ( style gadget -- style gadget )
118 <solid> over set-gadget-interior
121 : apply-outliner-style ( style gadget -- style gadget )
122 outline [ <outliner> ] apply-style ;
124 : <styled-paragraph> ( style pane -- gadget )
126 apply-border-width-style
127 apply-border-color-style
128 apply-page-color-style
129 apply-presentation-style
130 apply-quotation-style
134 : styled-pane ( quot style -- gadget )
135 #! Create a pane, call the quotation to fill it out.
136 >r <pane> dup r> swap <styled-paragraph>
137 >r swap with-pane r> ; inline
139 : apply-table-gap-style ( style grid -- style grid )
140 table-gap [ over set-grid-gap ] apply-style ;
142 : apply-table-border-style ( style grid -- style grid )
143 table-border [ <grid-lines> over set-gadget-boundary ]
146 : styled-grid ( style grid -- grid )
148 apply-table-gap-style
149 apply-table-border-style
152 : <pane-grid> ( quot style grid -- gadget )
154 [ pick pick >r >r -rot styled-pane r> r> rot ] map
155 ] map styled-grid nip ;
157 M: pane-stream with-stream-table
158 >r rot <pane-grid> r> print-gadget ;
160 M: pane-stream with-nested-stream
161 >r styled-pane r> write-gadget ;
164 M: pack stream-close drop ;
166 M: paragraph stream-close drop ;
168 : gadget-write ( string gadget -- )
172 >r <label> dup text-theme r> add-gadget
175 M: pack stream-write gadget-write ;
177 : gadget-bl ( style stream -- )
178 >r " " <styled-label> <word-break-gadget> r> add-gadget ;
180 M: paragraph stream-write
182 [ over gadget-write ] [ H{ } over gadget-bl ] interleave
185 : gadget-write1 ( char gadget -- )
186 >r ch>string r> stream-write ;
188 M: pack stream-write1 gadget-write1 ;
190 M: paragraph stream-write1
192 [ H{ } swap gadget-bl drop ] [ gadget-write1 ] if ;
194 : gadget-format ( string style stream -- )
196 [ 3drop ] [ >r swap <styled-label> r> add-gadget ] if ;
198 M: pack stream-format
201 M: paragraph stream-format
202 presented pick hash [
206 [ pick pick gadget-format ]
207 [ 2dup gadget-bl ] interleave