/
CTDropDown.t
128 lines (105 loc) · 4.02 KB
/
CTDropDown.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module CTDropDown where
import COCOA
struct DropDown < Component, RespondsToSelectionEvents, HasSelectionResponder where
addOption :: String -> Request ()
setOptions :: [String] -> Action
getOptions :: Request [String]
getCurrentOption :: Request String
--------------------------------------------------------------------------------------------------
------ ** CTDropDown ** ----------------------------------------------------------
mkCocoaDropDown :: World -> Class DropDown
mkCocoaDropDown w = class
state := Inactive
size := {width=108; height=17}
title := "DropDown"
BaseComponent {setPosition=setPositionImpl;setSize=dummy1;getSize=dummy2;..} =
new basicComponent True Nothing "DropDown"
setPosition p = request
if isActive state then
Active ref = state
dropDownSetPosition ref p
setPositionImpl p
setSize s = request
if isActive state then
Active ref = state
size := (<- dropDownSetSize ref s)
else
size := s
getSize = request
result size
dsh := new defaultSelectionResponder
selectionChanged str = action
dsh.selectionChanged str
setSelectionResponder resp = request
dsh := resp
options := []
addOption o = request
insertOption o
setOptions opts = action
options := []
forall o <- (reverse opts) do
insertOption o
insertOption o = do
options := o : options
if isActive state then
Active ref = state
dropDownAddOption ref o
currentOption := (<- dropDownGetSelectedOption ref)
getOptions = request
result options
currentOption := ""
refreshMyOptionAndPerformCallback = action
if isActive state then
Active ref = state
currentOption := (<- dropDownGetSelectedOption ref)
send selectionChanged currentOption
getCurrentOption = request
result currentOption
destroyComp = request
state := destroyState state
getState = request
result state
initComp app = request
ref <- initDropDown w
state := Active ref
forall o <- options do
dropDownAddOption ref o
dropDownSetPosition ref (<- getPosition)
size := (<- dropDownSetSize ref size)
currentOption := (<- dropDownGetSelectedOption ref)
sh = new defaultHandler refreshMyOptionAndPerformCallback
addResponder sh
result ref
this = DropDown{id=self;..}
result this
defaultSelectionResponder = class
selectionChanged str = action
setSelectionResponder _ = request
result RespondsToSelectionEvents {..}
data SizeState = Small | Expanded
defaultHandler dropdownUpdateMethod = class
sizeState := Small
respondToInputEvent (KeyEvent t) modifiers = request
result NotConsumed
respondToInputEvent (MouseEvent t) modifiers = request
case t of
MouseClicked pos ->
case sizeState of
Small ->
sizeState := Expanded
_ ->
sizeState := Small
dropdownUpdateMethod
_ ->
result Consumed
respondToInputEvent _ modifiers = request
result NotConsumed
result RespondsToInputEvents {..}
--------------------------------------------------------------------------------------------------
------ ** EXTERN ** ----------------------------------------------------------
private
extern initDropDown :: World -> Request CocoaRef
extern dropDownAddOption :: CocoaRef -> String -> Request ()
extern dropDownSetPosition :: CocoaRef -> Position -> Request ()
extern dropDownSetSize :: CocoaRef -> Size -> Request Size
extern dropDownGetSelectedOption :: CocoaRef -> Request String