]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/worlds/worlds.factor
Change a throw to rethrow so that we don't lose the original stack trace
[factor.git] / basis / ui / gadgets / worlds / worlds.factor
1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs continuations kernel math models
4 namespaces opengl sequences io combinators fry math.vectors
5 ui.gadgets ui.gestures ui.render ui.backend ui.gadgets.tracks
6 math.geometry.rect ;
7 IN: ui.gadgets.worlds
8
9 TUPLE: world < track
10 active? focused?
11 glass
12 title status
13 fonts handle
14 window-loc ;
15
16 : find-world ( gadget -- world/f ) [ world? ] find-parent ;
17
18 : show-status ( string/f gadget -- )
19     find-world dup [
20         status>> dup [ set-model ] [ 2drop ] if
21     ] [ 2drop ] if ;
22
23 : hide-status ( gadget -- ) f swap show-status ;
24
25 ERROR: no-world-found ;
26
27 : find-gl-context ( gadget -- )
28     find-world dup
29     [ handle>> select-gl-context ] [ no-world-found ] if ;
30
31 : (request-focus) ( child world ? -- )
32     pick parent>> pick eq? [
33         [ dup parent>> dup ] 2dip
34         [ (request-focus) ] keep
35     ] unless focus-child ;
36
37 M: world request-focus-on ( child gadget -- )
38     2dup eq?
39     [ 2drop ] [ dup focused?>> (request-focus) ] if ;
40
41 : new-world ( gadget title status class -- world )
42     { 0 1 } swap new-track
43         t >>root?
44         t >>active?
45         H{ } clone >>fonts
46         { 0 0 } >>window-loc
47         swap >>status
48         swap >>title
49         swap 1 track-add
50     dup request-focus ;
51
52 : <world> ( gadget title status -- world )
53     world new-world ;
54
55 M: world layout*
56     dup call-next-method
57     dup glass>> [
58         [ dup rect-dim ] dip (>>dim)
59     ] when* drop ;
60
61 M: world focusable-child* gadget-child ;
62
63 M: world children-on nip children>> ;
64
65 : (draw-world) ( world -- )
66     dup handle>> [
67         [ dup init-gl ] keep draw-gadget
68     ] with-gl-context ;
69
70 : draw-world? ( world -- ? )
71     #! We don't draw deactivated worlds, or those with 0 size.
72     #! On Windows, the latter case results in GL errors.
73     [ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] tri and and ;
74
75 TUPLE: world-error error world ;
76
77 C: <world-error> world-error
78
79 SYMBOL: ui-error-hook
80
81 : ui-error ( error -- )
82     ui-error-hook get [ call ] [ die ] if* ;
83
84 ui-error-hook [ [ rethrow ] ] initialize
85
86 : draw-world ( world -- )
87     dup draw-world? [
88         dup world [
89             [
90                 (draw-world)
91             ] [
92                 over <world-error> ui-error
93                 f >>active? drop
94             ] recover
95         ] with-variable
96     ] [
97         drop
98     ] if ;
99
100 world H{
101     { T{ key-down f { C+ } "x" } [ T{ cut-action } send-action ] }
102     { T{ key-down f { C+ } "c" } [ T{ copy-action } send-action ] }
103     { T{ key-down f { C+ } "v" } [ T{ paste-action } send-action ] }
104     { T{ key-down f { C+ } "a" } [ T{ select-all-action } send-action ] }
105     { T{ button-down f { C+ } 1 } [ drop T{ button-down f f 3 } button-gesture ] }
106     { T{ button-down f { A+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
107     { T{ button-down f { M+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
108     { T{ button-up f { C+ } 1 } [ drop T{ button-up f f 3 } button-gesture ] }
109     { T{ button-up f { A+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
110     { T{ button-up f { M+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
111 } set-gestures
112
113 PREDICATE: specific-button-up < button-up #>> ;
114 PREDICATE: specific-button-down < button-down #>> ;
115 PREDICATE: specific-drag < drag #>> ;
116
117 : generalize-gesture ( gesture -- )
118     clone f >># button-gesture ;
119
120 M: world handle-gesture ( gesture gadget -- ? )
121     2dup call-next-method [
122         {
123             { [ over specific-button-up? ] [ drop generalize-gesture f ] }
124             { [ over specific-button-down? ] [ drop generalize-gesture f ] }
125             { [ over specific-drag? ] [ drop generalize-gesture f ] }
126             [ 2drop t ]
127         } cond
128     ] [ 2drop f ] if ;
129
130 : close-global ( world global -- )
131     [ get-global find-world eq? ] keep '[ f _ set-global ] when ;