]> gitweb.factorcode.org Git - factor.git/blob - library/ui/gadgets/presentations.factor
Menus
[factor.git] / library / ui / gadgets / presentations.factor
1 ! Copyright (C) 2005, 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 IN: gadgets-listener
4 DEFER: call-listener
5
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 ;
12
13 ! Clickable objects
14 TUPLE: presentation object command ;
15
16 C: presentation ( button object command -- button )
17     [ set-presentation-command ] keep
18     [ set-presentation-object ] keep
19     [ set-gadget-delegate ] keep ;
20
21 : <object-presentation> ( gadget object -- button )
22     >r f <roll-button> r> f <presentation> ;
23
24 : <command-presentation> ( target command -- button )
25     dup command-name f <bevel-button> -rot <presentation> ;
26
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 ;
31
32 : operations-menu ( presentation -- gadget )
33     dup presentation-object
34     dup object-operations <commands-menu>
35     swap show-menu ;
36
37 : invoke-presentation ( presentation -- )
38     dup presentation-object swap presentation-command
39     [ dup default-operation ] unless*
40     invoke-command ;
41
42 : show-mouse-help ( presentation -- )
43     dup find-world [ world-status set-model* ] [ drop ] if* ;
44
45 : hide-mouse-help ( presentation -- )
46     find-world [ world-status f swap set-model* ] when* ;
47
48 M: presentation ungraft* ( presentation -- )
49     dup hide-mouse-help delegate ungraft* ;
50
51 presentation H{
52     { T{ button-up } [ [ invoke-presentation ] if-clicked ] }
53     { T{ button-down f f 3 } [ [ operations-menu ] if-clicked ] }
54     { T{ mouse-leave } [ global [ dup short. flush ] bind  dup hide-mouse-help button-update ] }
55     { T{ mouse-enter } [ global [ dup short. flush ] bind  dup show-mouse-help button-update ] }
56 } set-gestures
57
58 ! Presentation help bar
59 : <presentation-help> ( model -- gadget )
60     [ [ presentation-object summary ] [ "" ] if* ] <filter>
61     <label-control> dup reverse-video-theme ;
62
63 : <listener-button> ( gadget quot -- button )
64     [ call-listener ] curry <roll-button> ;
65
66 ! Character styles
67
68 : apply-style ( style gadget key quot -- style gadget )
69     >r pick hash r> when* ; inline
70
71 : apply-foreground-style ( style gadget -- style gadget )
72     foreground [ over set-label-color ] apply-style ;
73
74 : apply-background-style ( style gadget -- style gadget )
75     background [ <solid> over set-gadget-interior ] apply-style ;
76
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 ;
81
82 : apply-font-style ( style gadget -- style gadget )
83     over specified-font over set-label-font ;
84
85 : apply-presentation-style ( style gadget -- style gadget )
86     presented [ <object-presentation> ] apply-style ;
87
88 : apply-quotation-style ( style gadget -- style gadget )
89     quotation [ <listener-button> ] apply-style ;
90
91 : <styled-label> ( style text -- gadget )
92     <label>
93     apply-foreground-style
94     apply-background-style
95     apply-font-style
96     apply-presentation-style
97     apply-quotation-style
98     nip ;
99
100 ! Paragraph styles
101
102 : apply-wrap-style ( style pane -- style pane )
103     wrap-margin [
104         2dup <paragraph> swap set-pane-prototype
105         <paragraph> over set-pane-current
106     ] apply-style ;
107
108 : apply-border-width-style ( style gadget -- style gadget )
109     border-width [ <border> ] apply-style ;
110
111 : apply-border-color-style ( style gadget -- style gadget )
112     border-color [
113         <solid> over set-gadget-boundary
114     ] apply-style ;
115
116 : apply-page-color-style ( style gadget -- style gadget )
117     page-color [
118         <solid> over set-gadget-interior
119     ] apply-style ;
120
121 : apply-outliner-style ( style gadget -- style gadget )
122     outline [ <outliner> ] apply-style ;
123
124 : <styled-paragraph> ( style pane -- gadget )
125     apply-wrap-style
126     apply-border-width-style
127     apply-border-color-style
128     apply-page-color-style
129     apply-presentation-style
130     apply-quotation-style
131     apply-outliner-style
132     nip ;
133
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
138
139 : apply-table-gap-style ( style grid -- style grid )
140     table-gap [ over set-grid-gap ] apply-style ;
141
142 : apply-table-border-style ( style grid -- style grid )
143     table-border [ <grid-lines> over set-gadget-boundary ]
144     apply-style ;
145
146 : styled-grid ( style grid -- grid )
147     <grid>
148     apply-table-gap-style
149     apply-table-border-style
150     nip ;
151
152 : <pane-grid> ( quot style grid -- gadget )
153     [
154         [ pick pick >r >r -rot styled-pane r> r> rot ] map
155     ] map styled-grid nip ;
156
157 M: pane-stream with-stream-table
158     >r rot <pane-grid> r> print-gadget ;
159
160 M: pane-stream with-nested-stream
161     >r styled-pane r> write-gadget ;
162
163 ! Stream utilities
164 M: pack stream-close drop ;
165
166 M: paragraph stream-close drop ;
167
168 : gadget-write ( string gadget -- )
169     over empty? [
170         2drop
171     ] [
172         >r <label> dup text-theme r> add-gadget
173     ] if ;
174
175 M: pack stream-write gadget-write ;
176
177 : gadget-bl ( style stream -- )
178     >r " " <styled-label> <word-break-gadget> r> add-gadget ;
179
180 M: paragraph stream-write
181     swap " " split
182     [ over gadget-write ] [ H{ } over gadget-bl ] interleave
183     drop ;
184
185 : gadget-write1 ( char gadget -- )
186     >r ch>string r> stream-write ;
187
188 M: pack stream-write1 gadget-write1 ;
189
190 M: paragraph stream-write1
191     over CHAR: \s =
192     [ H{ } swap gadget-bl drop ] [ gadget-write1 ] if ;
193
194 : gadget-format ( string style stream -- )
195     pick empty?
196     [ 3drop ] [ >r swap <styled-label> r> add-gadget ] if ;
197
198 M: pack stream-format
199     gadget-format ;
200
201 M: paragraph stream-format
202     presented pick hash [
203         gadget-format
204     ] [
205         rot " " split
206         [ pick pick gadget-format ]
207         [ 2dup gadget-bl ] interleave
208         2drop
209     ] if ;