made a copy
[strongtalk-kjk.git] / StrongtalkSource / ToggleButton.dlt
blob39e78e7fbc8fb41169a0e1c9606f4a0d8c40e54a
1 Delta define: #ToggleButton as: (\r
2 (Class subclassOf: 'PressHandler' instanceVariables: 'model <Boolean>\r
3 modelSink <[Boolean]>\r
4 trueLook <Visual>\r
5 falseLook <Visual>\r
6 look <Boolean>\r
7 pressedBorder <Border>\r
8 unpressedBorder <Border>\r
9 frame <Frame>\r
10                 ') classVariables: 'FocusPainter <Painter>\r
11                         '; protocols: 'View[Boolean]') !\r
13 (Delta mirrorFor: #ToggleButton) revision: '$Revision: 1.3 $'!\r
15 (Delta mirrorFor: #ToggleButton) group: 'ui'!\r
17 (Delta mirrorFor: #ToggleButton)\r
18 comment: \r
19 'General non-native button.\r
21 Tags: %User %UI\r
23 (c) 1995-1997 Sun Microsystems, Inc. ALL RIGHTS RESERVED. \r
24 Use and distribution of this software is subject to the terms of the attached source license\r
25 '!\r
27 ! (Delta mirrorFor: #ToggleButton) classSide methodsFor: 'instance creation' !\r
30 withTrueLook: t <Visual>\r
31 falseLook: f <Visual>\r
32 ^<Instance>\r
34         ^self new trueLook: t falseLook: f initialLook: false!\r
36 withTrueLook: t <Visual>\r
37 falseLook: f <Visual>\r
38 initialLook: b <Boolean>\r
39 ^<Instance>\r
41         ^self new trueLook: t falseLook: f initialLook: b! !\r
43 ! (Delta mirrorFor: #ToggleButton) methodsFor: 'accessing' !\r
46 falseLook ^<Visual>\r
48         ^falseLook!\r
50 look ^<Boolean>\r
52         ^look!\r
54 look: flag <Boolean>\r
56         look := flag.\r
57         self updatePart.!\r
59 model ^<Boolean>\r
61         ^model!\r
63 model: f <Boolean>\r
65         model := f.\r
66         self look: f.!\r
68 modelSink ^<[Boolean]>\r
70         ^modelSink!\r
72 modelSink: b <[Boolean]>\r
74         modelSink := b!\r
76 pressedBorder ^<Border>\r
78         ^pressedBorder!\r
80 pressedBorder: b <Border>\r
82         pressedBorder notNil\r
83                 ifTrue: [ self changing; updatePart ].\r
84         pressedBorder := b!\r
86 trueLook ^<Visual>\r
88         ^trueLook!\r
90 trueLook: t <Visual> falseLook: f <Visual>\r
92         trueLook := t.\r
93         falseLook := f.\r
95         self    onPress: [ self look: self model not ];\r
96                         onAction: [ self toggle ];\r
97                         onCancel: [ self look: self model ].\r
99         self look: self model.!\r
101 trueLook: t <Visual> falseLook: f <Visual> initialLook: b <Boolean>\r
103         model := b.\r
104         look := b.\r
105         self trueLook: t falseLook: f!\r
107 unpressedBorder ^<Border>\r
109         ^unpressedBorder!\r
111 unpressedBorder: b <Border>\r
113         unpressedBorder notNil\r
114                 ifTrue: [ self changing; updatePart ].\r
115         unpressedBorder := b! !\r
117 ! (Delta mirrorFor: #ToggleButton) methodsFor: 'initialize' !\r
120 initialize\r
121         super initialize.\r
122         model := false.\r
123         self modelSink: [ :f <Boolean> | ].\r
124         look := false.\r
125         self    pressedBorder: (Border standard3DRaised: false);\r
126                         unpressedBorder: (Border standard3DRaised: true).! !\r
128 ! (Delta mirrorFor: #ToggleButton) methodsFor: 'control' !\r
131 toggle\r
133         self model: self model not.\r
134         self modelSink value: self model.\r
135         self updatePart.! !\r
137 ! (Delta mirrorFor: #ToggleButton) methodsFor: 'private' !\r
140 action\r
142         super action.\r
144         "We regain the focus if needed because some parents might move or reparent us\r
145                 during the action (like outliners)"\r
146         (self isFocus not and: [ self isAttached and: [ self session activeWindow == self topWindow ] ])\r
147                 ifTrue: [ self becomeFocus ].!\r
149 focusPainter ^<Painter>\r
151         FocusPainter isNil\r
152                 ifTrue: [       "We don't need a critical region because replacement won't hurt"\r
153                                                 FocusPainter := Painter new lineStyle: #dot     ].\r
154         ^FocusPainter!\r
156 frame ^<Frame>\r
158         ^frame!\r
160 frame: f <Frame>\r
162         frame := f.\r
163         self part: f.!\r
165 isFocusCandidate ^<Boolean>\r
167         ^true!\r
169 press\r
171         self scrollIfNeededToMakeVisible: self allocation enclosingRect\r
172                          relativeTo: self minimizeMotion: true.\r
173         super press.\r
174         self becomeFocus!\r
176 skipCheckLayout ^<Boolean>\r
178         ^false!\r
180 updatePart\r
182         | b <Border> f <Frame> look <Visual> |\r
183         self hasPart\r
184                 ifTrue: [ self frame removePart ]\r
185                 ifFalse: [ self frame: (Frame new) ].\r
187         self pressing\r
188                 ifTrue: [ b := self pressedBorder ]\r
189                 ifFalse: [ b := self unpressedBorder ].\r
191         look := self look\r
192                 ifTrue: [ self trueLook ]\r
193                 ifFalse: [ self falseLook ].\r
195         self frame\r
196                 part: look;\r
197                 border: b.! !\r
199 ! (Delta mirrorFor: #ToggleButton) methodsFor: 'restricted-to session' !\r
202 isFocus: f <Boolean>\r
204         super isFocus: f.\r
205         (f not and: [ self isGrabbing ])\r
206                 ifTrue: [ self releaseGrab ].\r
207         self invalidate.! !\r
209 ! (Delta mirrorFor: #ToggleButton) methodsFor: 'displaying' !\r
212 localDisplayOn: can <Canvas>\r
214         super localDisplayOn: can.\r
216         (self isFocus and: [ self blinkState and: [ self pressing not ] ])\r
217                 ifTrue: [ can withPainter: self focusPainter\r
218                                                         do: [ can displayRectangle:\r
219                                                                                  ((self frame part allocationRelativeTo: self) enclosingRect\r
220                                                                                                 expandBy: 1) ]  ].! !\r
222 ! (Delta mirrorFor: #ToggleButton) methodsFor: 'event processing' !\r
225 keyDown: key <Symbol> focus: focus <Visual> event: ev <UIEvent> ^<Boolean>\r
227         focus == self\r
228                 ifFalse: [ ^false ].\r
230         key = #RETURN\r
231                 ifTrue: [       self press.\r
232                                                 ^true ].\r
233         key = #SPACE\r
234                 ifTrue: [       self action.\r
235                                                 ^true ].\r
236         (key = #TAB and: [ self pressing ])\r
237                 ifTrue: [       self cancel.\r
238                                                 ^false ].\r
240         ^false!\r
242 keyUp: key <Symbol> focus: focus <Visual> event: ev <UIEvent> ^<Boolean>\r
244         focus == self\r
245                 ifFalse: [ ^false ].\r
247         ^(key = #RETURN and: [ self pressing ])\r
248                 onTrue: [ self action ]! !\r