1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays hashtables io kernel math math.functions
4 namespaces make opengl sequences strings splitting ui.gadgets
5 ui.gadgets.tracks ui.gadgets.packs fonts ui.render ui.pens.solid
6 ui.baseline-alignment ui.text colors colors.constants models
7 combinators opengl.gl ;
10 ! A label gadget draws a string.
11 TUPLE: label < gadget text font ;
15 M: label string>> ( label -- string )
16 text>> dup string? [ "\n" join ] unless ; inline
20 PREDICATE: string-array < array [ string? ] all? ;
24 : ?string-lines ( string -- string/array )
25 CHAR: \n over memq? [ string-lines ] when ;
27 ERROR: not-a-string object ;
29 M: label (>>string) ( string label -- )
32 { [ dup string-array? ] [ ] }
33 { [ dup string? ] [ ?string-lines ] }
36 ] dip (>>text) ; inline
38 : label-theme ( gadget -- gadget )
39 sans-serif-font >>font ; inline
41 : new-label ( string class -- label )
46 : <label> ( string -- label )
49 : >label< ( label -- font text )
50 [ font>> ] [ text>> ] bi ;
57 : label-metrics ( label -- metrics )
58 >label< dup string? [ first ] unless line-metrics ;
63 label-metrics ascent>> round ;
66 label-metrics cap-height>> round ;
68 : draw-text* ( font text fg bg -- )
70 [ font-with-background ] when* swap
71 [ font-with-foreground ] when* swap
74 : draw-shadowed-text ( font text -- )
76 { 0 1 } [ over shadow>> background get draw-text* ]
78 ] [ foreground get transparent draw-text* ] 2bi ;
80 : draw-normal-text ( font text -- )
81 foreground get background get draw-text* ;
85 [ draw-shadowed-text ] [ draw-normal-text ] if ;
87 M: label gadget-text* string>> % ;
89 TUPLE: label-control < label ;
91 M: label-control model-changed
92 swap value>> >>string relayout ;
94 : <label-control> ( model -- gadget )
95 "" label-control new-label
98 : text-theme ( gadget -- gadget )
99 monospace-font >>font ;
101 : reverse-video-theme ( label -- label )
102 sans-serif-font reverse-video-font >>font
103 COLOR: black <solid> >>interior ;
105 GENERIC: >label ( obj -- gadget )
106 M: string >label <label> ;
107 M: array >label <label> ;
109 M: f >label drop <gadget> ;
113 : label-on-left/right ( -- track )
117 { 5 5 } >>gap ; inline
120 : label-on-left ( gadget label -- button )
122 swap >label f track-add
125 : label-on-right ( label gadget -- button )
128 swap >label 1 track-add ;