ExtListBox.icl 30 KB
Newer Older
Diederik van Arkel's avatar
Diederik van Arkel committed
1
2
3
4
5
6
7
implementation module ExtListBox

import StdBool, StdEnum, StdList, StdMisc, StdOrdList, StdTuple, StdFunc
import StdControl, StdControlReceiver, StdId, StdPicture, StdPSt, StdReceiver, StdWindow
import StdControlAttribute
import ioutil

Diederik van Arkel's avatar
Diederik van Arkel committed
8
9
10
trace_n` _ s :== s
//import dodebug

Diederik van Arkel's avatar
Diederik van Arkel committed
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
//--

::	ExtListBoxState ps =
	{ items			:: ![String]					// All items to be displayed
	, funs			:: ![IdFun ps]					// associated double click fun
	, shiftfuns		:: ![IdFun ps]					// associated shift double click fun
	, selection		:: ![Index]						// The current selection
	, listboxId		:: !ExtListBoxId	ps			// The ids related to this list box
	, lineHeight	:: !Int
	, initHeight	:: !Int
	, lMargin		:: !Int
	, tMargin		:: !Int
	, rMargin		:: !Int
	, bMargin		:: !Int
	, pen			:: ![PenAttribute]
	, newselfun		:: ![Index] ps -> ps
	}

//--

::	ExtListBoxId p
	=	{	controlId	:: !Id							// The Id of the outmost CompoundControl
		,	receiverId	:: !R2Id (MessageIn p) MessageOut	// The Id of the Receiver2Control that handles message passing
		}

openExtListBoxId :: !*env -> (!ExtListBoxId .a,!*env)	| Ids env
openExtListBoxId env
	# (cid, env)	= openId env
	# (rid,env)		= openR2Id env
	= ({controlId=cid,receiverId=rid},env)

//--

::	MessageIn ps
	=	InGetSelection										// Request to retrieve current selection
	|	InSetSelection		[Index]							// Request to set the selection to the given index
Diederik van Arkel's avatar
Diederik van Arkel committed
47
	|	InSetSelectAll										// Request to select all items
Diederik van Arkel's avatar
Diederik van Arkel committed
48
49
50
51
52
53
54
55
	|	InGetItems											// Request to retrieve all current items
	|	InOpenItems			Index [ExtListBoxItem ps]		// Request to add items behind the element with the given index
	|	InAppendItems		[ExtListBoxItem ps]				// Request to add items behind the last element
	|	InCloseItems		[Index]							// Request to remove items at the given index positions
	|	InCloseAllItems										// Request to remove all current items
	|	InExecItem			Index							// Request to execute function associated with given index
	|	InSetPen			[PenAttribute]					// Request to set control pen
	|	InGetPen											// Request to get control pen
Diederik van Arkel's avatar
Diederik van Arkel committed
56
	|	InTwiddleItems		Index Index						// Request to swap two items
Diederik van Arkel's avatar
Diederik van Arkel committed
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

::	MessageOut
	=	OutGetSelection		[(String,Index)]				// Reply to retrieve the current selection
	|	OutSetSelection										// Reply to set the selection
	|	OutGetItems			[String]						// Reply to get all items
	|	OutOpenItems										// Reply to add items
	|	OutAppendItems										// Reply to append items
	|	OutCloseItems										// Reply to remove items
	|	OutCloseAllItems									// Reply to remove all items
	|	OutExecItem											// Reply to execute item
	|	OutSetPen											// Reply to set the control pen
	|	OutGetPen			[PenAttribute]					// Reply to get the control pen
	| OutTwiddleItems

//--

:: ExtListBoxItem ps :== (String,IdFun ps,IdFun ps)

:: ExtListBoxControl ls ps
	= ExtListBoxControl [ExtListBoxItem ps] [Int] ([Int] ps -> ps) (ExtListBoxId ps) [ControlAttribute *(*(ExtListBoxState ps,ls),ps)]

instance Controls ExtListBoxControl
where
	getControlType _ = "ExtListBoxControl"
	controlToHandles (ExtListBoxControl contents selection newselfun listboxId attrs) ps
		# ((lineHeight,initHeight),ps)	= accScreenPicture (liheights penAtts) ps
		# listboxState =	{ items			= items
							, funs			= funs
							, shiftfuns		= shiftfuns
							, selection		= selection
							, listboxId		= listboxId
							, lineHeight	= lineHeight
							, initHeight	= initHeight
							, lMargin		= lMargin
							, tMargin		= tMargin
							, rMargin		= rMargin
							, bMargin		= bMargin
							, pen			= penAtts
							, newselfun		= newselfun
							}
97
98
		# (listboxState,domain,ps)		= calcControlDomain listboxState ps
		= controlToHandles (imp listboxId lineHeight listboxState domain listboxAtts) ps
Diederik van Arkel's avatar
Diederik van Arkel committed
99
100
101
102
103
104
105
106
	where
		(items,funs,shiftfuns)			= unzip3 contents
		liheights pen pic
			# pic				= setPenAttributes pen pic
			# (metrics,pic)		= getPenFontMetrics pic
			# lineHeight		= fontLineHeight metrics
			# initHeight		= metrics.fAscent + metrics.fLeading
			= ((lineHeight,initHeight),pic)
107
108
109
		imp listboxId lineHeight listboxState domain listboxAtts
			# (listboxLook,listboxState)	= customlook listboxState
			=
Diederik van Arkel's avatar
Diederik van Arkel committed
110
111
112
113
114
115
116
117
118
119
			{	addLS	= listboxState
			,	addDef	= CompoundControl
					(NilLS)
					(
					listboxAtts ++
					[	ControlId			listboxId.controlId
					,	ControlItemSpace	0 0							// No itemspace
					,	ControlHScroll		(altScrollFunction Horizontal 10)
					,	ControlVScroll		(altScrollFunction Vertical lineHeight)
					,	ControlViewDomain	domain
120
					,	ControlLook			True listboxLook
Diederik van Arkel's avatar
Diederik van Arkel committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
					,	ControlMouse		mouseFilter Able mouseFunction
					,	elbKeyboard
					]
					)
				  :+:	Receiver2 listboxId.receiverId receiver []
			}

		listboxAtts = map toLBCA (filter isListBoxControlAttribute attrs)
		penAtts = flatten (map getControlPenAtt (filter isControlPen attrs))
		(lMargin,rMargin) = case (map getControlHMarginAtt (filter isControlHMargin attrs)) of
				[]			-> (0,0)
				[(l,r):_]	-> (l,r)
		(tMargin,bMargin) = case (map getControlVMarginAtt (filter isControlVMargin attrs)) of
				[]			-> (0,0)
				[(l,r):_]	-> (l,r)

		isListBoxControlAttribute :: !(ControlAttribute .ps) -> Bool
		isListBoxControlAttribute (ControlFunction _)		= True
		isListBoxControlAttribute ControlHide				= True
		isListBoxControlAttribute (ControlPos _)			= True
		isListBoxControlAttribute (ControlPen _)			= True
		isListBoxControlAttribute (ControlSelectState _)	= True
		isListBoxControlAttribute (ControlViewSize _)		= True
		isListBoxControlAttribute (ControlOuterSize _)		= True
		isListBoxControlAttribute (ControlResize _)			= True
		isListBoxControlAttribute (ControlMouse _ _ _)		= True
		isListBoxControlAttribute (ControlKeyboard _ _ _)	= True
		isListBoxControlAttribute _							= False
		
		toLBCA (ControlFunction f)		= ControlFunction f
		toLBCA ControlHide				= ControlHide
		toLBCA (ControlPos p)			= ControlPos p
		toLBCA (ControlPen p)			= ControlPen p
		toLBCA (ControlSelectState p)	= ControlSelectState p
		toLBCA (ControlViewSize p)		= ControlViewSize p
		toLBCA (ControlOuterSize p)		= ControlOuterSize p
		toLBCA (ControlResize p)		= ControlResize p
		toLBCA (ControlMouse a b f)		= ControlMouse a b f
Diederik van Arkel's avatar
Diederik van Arkel committed
159
		toLBCA (ControlKeyboard a b f)	= joinKeyboards elbKeyboard (ControlKeyboard a b f)
Diederik van Arkel's avatar
Diederik van Arkel committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
		toLBCA _ = abort "ExtListBox.icl: unsupported control attribute handed to toLBCA"
			
				
 
		
		//	The receiver function:
		//receiver :: (MessageIn *(ls,PSt .l)) (*(ExtListBoxState *(ls,PSt .l),ls),PSt .l) -> (MessageOut,(*(ExtListBoxState *(ls,PSt .l),ls),PSt .l))
		
		//	Return the current selection:
		receiver InGetSelection ((listboxState=:{items,selection},ls),ps)
			= (OutGetSelection (map (\index->(items!!(index-1),index)) selection),((listboxState,ls),ps))
		
		//	Set a new selection:
		receiver (InSetSelection newSelection) ((listboxState=:{tMargin,bMargin,lineHeight,initHeight,listboxId},ls),ps)
174
175
176
177
178
179
			# listboxState		= {ExtListBoxState | listboxState & selection=newSelection}
			# (listboxState,ps)	= applySelfun listboxState ps
			# (newLook,listboxState)
								= customlook listboxState
			# ps				= scrolltoselection listboxId lineHeight tMargin bMargin newSelection ps
			# ps				= appPIO (setListBoxLook listboxId True (True,newLook)) ps
Diederik van Arkel's avatar
Diederik van Arkel committed
180
181
			= (OutSetSelection,((listboxState,ls),ps))
		
Diederik van Arkel's avatar
Diederik van Arkel committed
182
183
184
185
186
187
188
189
190
191
192
		//	Set a new selection:
		receiver InSetSelectAll ((listboxState=:{tMargin,bMargin,lineHeight,initHeight,listboxId,items},ls),ps)
			# newSelection		= [1..length items]
			# listboxState		= {ExtListBoxState | listboxState & selection=newSelection}
			# (listboxState,ps)	= applySelfun listboxState ps
			# (newLook,listboxState)
								= customlook listboxState
			# ps				= scrolltoselection listboxId lineHeight tMargin bMargin newSelection ps
			# ps				= appPIO (setListBoxLook listboxId True (True,newLook)) ps
			= (OutSetSelection,((listboxState,ls),ps))
		
Diederik van Arkel's avatar
Diederik van Arkel committed
193
194
195
196
197
198
		//	Return the current elements:
		receiver InGetItems ((listboxState=:{items},ls),ps)
			= (OutGetItems items,((listboxState,ls),ps))
		
		// Execute element:
		receiver (InExecItem index) ((listboxState=:{funs},ls),ps)
199
			| index > length funs
Diederik van Arkel's avatar
Diederik van Arkel committed
200
201
202
203
204
205
206
207
208
				= (OutExecItem,((listboxState,ls),ps))
			# ps = (funs!!(index -  1)) ps
			= (OutExecItem,((listboxState,ls),ps))
		
		//	Insert elements:
		receiver (InOpenItems behindIndex newItems) ((listboxState=:{pen,items,funs,shiftfuns,selection,lineHeight,initHeight},ls),ps)
			| nrNewItems==0
				= (OutOpenItems,((listboxState,ls),ps))
			# listboxState		= {listboxState & items=allItems, funs=allFuns, shiftfuns = allShiftFuns, selection=newSelection}
209
210
211
212
			# (listboxState,newDomain,ps)
								= calcControlDomain listboxState ps
			# (newLook,listboxState)
								= customlook listboxState
Diederik van Arkel's avatar
Diederik van Arkel committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
			# ps				= appPIO (seq
									[ setControlViewDomain customId newDomain
									, setControlLooks [(customId,True,(True,newLook))]
									]) ps
			= (OutOpenItems,((listboxState,ls),ps))
		where
			customId				= listboxState.listboxId.controlId
			nrNewItems				= (length newItems)		// Add only items upto maxNrItems
			okNewItems				= newItems%(0,nrNewItems-1)							// These are the proper new items
			(okNewItems`,okNewFuns`,okNewShiftFuns`)= unzip3 okNewItems
			okBehindIndex			= setBetween 0 (inc (length items)) behindIndex
			(itemsBefore,itemsAfter)= splitAt (okBehindIndex-1) items
			(funsBefore,funsAfter)	= splitAt (okBehindIndex-1) funs
			(shiftfunsBefore,shiftfunsAfter)= splitAt (okBehindIndex-1) shiftfuns
			allItems				= if (okBehindIndex==0)
										 (okNewItems`++items)
										 (itemsBefore++okNewItems`++itemsAfter)
			allFuns					= if (okBehindIndex==0)
										 (okNewFuns`++funs)
										 (funsBefore++okNewFuns`++funsAfter)
			allShiftFuns				= if (okBehindIndex==0)
										 (okNewShiftFuns`++shiftfuns)
										 (shiftfunsBefore++okNewShiftFuns`++shiftfunsAfter)
			(selecBefore,selecAfter)= span (\index->index<=okBehindIndex) (sort selection)
			newSelection			= selecBefore++map ((+) nrNewItems) selecAfter
		
		//	Append elements:
		receiver (InAppendItems newItems) ((listboxState=:{pen,items,funs,shiftfuns,selection,lineHeight,initHeight},ls),ps)
			| nrNewItems==0
				= (OutOpenItems,((listboxState,ls),ps))
			# listboxState	= {listboxState & items=allItems, funs=allFuns, shiftfuns = allShiftFuns, selection=newSelection}
244
245
246
247
248
249
250
251
			# (listboxState,newDomain,ps)
							= calcControlDomain listboxState ps
			# (newLook,listboxState)
							= customlook listboxState
			# ps			= appPIO (seq
								[ setControlLooks [(customId,False,(True,newLook))]
								, setControlViewDomain customId newDomain
								]) ps
Diederik van Arkel's avatar
Diederik van Arkel committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
			= (OutAppendItems,((listboxState,ls),ps))
		where
			customId				= listboxState.listboxId.controlId
			nrNewItems				= (length newItems)		// Add only items upto maxNrItems
			okNewItems				= newItems%(0,nrNewItems-1)	// These are the proper new items
			(okNewItems`,okNewFuns`,okNewShiftFuns`)
									= unzip3 okNewItems
			allItems				= items ++ okNewItems`
			allFuns					= funs ++ okNewFuns`
			allShiftFuns			= shiftfuns ++ okNewShiftFuns`
			newSelection			= selection
		
		//	Remove elements:
		receiver (InCloseItems closeItems) ((listboxState=:{pen,items,funs,shiftfuns,selection,lineHeight,initHeight},ls),ps)
			| nrCloseItems==0
				= (OutCloseItems,((listboxState,ls),ps))
			# listboxState		= {listboxState & items=allItems, funs = allFuns, shiftfuns = allShiftFuns, selection=newSelection}
269
270
271
272
			# (listboxState,newDomain,ps)
								= calcControlDomain listboxState ps
			# (newLook,listboxState)
								= customlook listboxState
Diederik van Arkel's avatar
Diederik van Arkel committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
			# ps				= appPIO (seq
									[ setControlViewDomain customId newDomain
									, setControlLooks [(customId,True,(True,newLook))]
									]) ps
			= (OutCloseItems,((listboxState,ls),ps))
		where
			customId				= listboxState.listboxId.controlId
			nrCloseItems			= length closeItems
			allItems				= [ item \\ item <- items & i <- [1..] | not (isMember i closeItems) ]
			allFuns					= [ item \\ item <- funs & i <- [1..] | not (isMember i closeItems) ]
			allShiftFuns			= [ item \\ item <- shiftfuns & i <- [1..] | not (isMember i closeItems) ]
			newSelection			= removeMembers selection closeItems
		
		//	Remove all:
287
		receiver (InCloseAllItems) ((listboxState=:{pen,items,funs,selection,lineHeight,initHeight,listboxId},ls),ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
288
			# listboxState	= {listboxState & items=[], funs = [], shiftfuns = [], selection=[]}
289
290
291
292
			# (listboxState,newDomain,ps)
							= calcControlDomain listboxState ps
			# (newLook,listboxState)
							= customlook listboxState
Diederik van Arkel's avatar
Diederik van Arkel committed
293
			# ps			= appPIO (seq
294
295
								[ setControlViewDomain listboxId.controlId newDomain
								, setControlLooks [(listboxId.controlId,True,(True,newLook))]
Diederik van Arkel's avatar
Diederik van Arkel committed
296
297
298
299
								]) ps
			= (OutCloseAllItems,((listboxState,ls),ps))
		
		// Set control pen:
300
		receiver (InSetPen newpen) ((listboxState=:{items,pen,listboxId},ls),ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
301
302
303
304
305
306
307
308
			# pen							= removeDupAtt (newpen++pen)
			# ((lineHeight,initHeight),ps)	= accScreenPicture (liheights pen) ps
			# listboxState					=
				{ listboxState
				& pen			= pen
				, lineHeight	= lineHeight
				, initHeight	= initHeight
				}
309
310
			# (listboxState,newDomain,ps)	= calcControlDomain listboxState ps
			# (newLook,listboxState)		= customlook listboxState
Diederik van Arkel's avatar
Diederik van Arkel committed
311
			# ps			= appPIO (seq
312
313
314
								[ setControlViewDomain listboxId.controlId newDomain
								, setControlLooks [(listboxId.controlId,True,(True,newLook))]
								, setControlScrollFunction listboxId.controlId Vertical (altScrollFunction Vertical lineHeight)
Diederik van Arkel's avatar
Diederik van Arkel committed
315
316
317
318
319
320
321
322
323
								]) ps
			= (OutSetPen,((listboxState,ls),ps))
		
		// Get control pen:
		receiver (InGetPen) ((listboxState=:{pen},ls),ps)
			= (OutGetPen pen,((listboxState,ls),ps))

		receiver (InTwiddleItems idx1 idx2) ((listboxState=:{items,selection},ls),ps)
			# listboxState	= {listboxState & items = twiddled_items}
324
325
			# (newLook,listboxState)
							= customlook listboxState
Diederik van Arkel's avatar
Diederik van Arkel committed
326
327
328
329
330
331
332
333
334
335
			# ps			= appPIO (setControlLooks [(controlId,True,(True,newLook))]) ps
			= (OutTwiddleItems,((listboxState,ls),ps))
		where
			controlId				= listboxState.listboxId.controlId
			twiddled_items = twiddle idx1 idx2 items


//--
		
calcControlDomain listboxState=:{pen,items,lMargin,rMargin,tMargin,bMargin} ps
336
337
	# (dom,ps)	= accPIO (accScreenPicture calc) ps
	= (listboxState,dom,ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
where
	calc pic
		# pic				= setPenAttributes pen pic
		# (metrics,pic)		= getPenFontMetrics pic
		# (itemWidths,pic)	= getPenFontStringWidths items pic
		# minWidth			= 0
		# maxWidth			= maxList [minWidth:itemWidths]
		# nrItems			= length items
		# height			= nrItems*(fontLineHeight metrics)
		# width				= maxWidth + lMargin + rMargin
		# height			= height + tMargin + bMargin
		# newDomain			= {corner1=zero,corner2={x=width,y=height}}
		= (newDomain,pic)

 
//removeDupAtt :: !.[a] -> .[a] | Eq a
removeDupAtt [x:xs] = [x:removeDupAtt (filter (diff x) xs)]
where
	diff (PenSize _)	(PenSize _)		= False
	diff (PenPos _)		(PenPos _)		= False
	diff (PenColour _)	(PenColour _)	= False
	diff (PenBack _)	(PenBack _)		= False
	diff (PenFont _)	(PenFont _)		= False
	diff _ _ = True
removeDupAtt _      = []

//-- Look funs

//	The look of the custom control lists all items and the current selection
367
368
369
370
371
//customlook :: !(ExtListBoxState .a) .b .UpdateState *Picture -> *Picture
customlook ls=:{items,selection,pen,lineHeight,initHeight,lMargin,tMargin}
	= (customlook,ls)
where
  customlook _ {newFrame} pict
Diederik van Arkel's avatar
Diederik van Arkel committed
372
373
374
375
376
377
378
	# pict				= setPenAttributes pen pict
	# pict				= unfill newFrame pict
	# drawlines			= fst (smap drawline items (initHeight+tMargin))
	# pict				= seq drawlines pict
	# drawselection		= map drawsel selection
	# pict				= seq drawselection pict
	= pict
379
  where
Diederik van Arkel's avatar
Diederik van Arkel committed
380
381
382
383
	(x1,x2)			= (newFrame.corner1.x,newFrame.corner2.x)
	drawsel i		= hilite {corner1={x=x1,y=tMargin + (i-1)*lineHeight}, corner2={x=x2,y= tMargin + i*lineHeight-1}}
	drawline line y	= (drawAt {x=lMargin,y=y} line,y+lineHeight)

384
385
updatelook ls=:{selection,lineHeight,tMargin} oldsel (x1,x2)
	= (update,ls)
Diederik van Arkel's avatar
Diederik van Arkel committed
386
where
387
388
389
390
391
392
393
	update pict
		# drawselection		= map drawsel oldsel
		# pict				= seq drawselection pict
		# drawselection		= map drawsel selection
		# pict				= seq drawselection pict
		= pict

Diederik van Arkel's avatar
Diederik van Arkel committed
394
395
	drawsel i		= hilite {corner1={x=x1,y=tMargin + (i-1)*lineHeight}, corner2={x=x2,y= tMargin + i*lineHeight-1}}

Diederik van Arkel's avatar
Diederik van Arkel committed
396
updateListBoxLook :: !.(ExtListBoxId .a) [.Int] !*(!(!u:(ExtListBoxState *(PSt .b)),.c),*(PSt .b)) -> *((v:(ExtListBoxState *(PSt .b)),.c),*(PSt .b)), [u <= v]
Diederik van Arkel's avatar
Diederik van Arkel committed
397
398
399
updateListBoxLook customId=:{controlId} newSelection ((lbState=:{tMargin,bMargin,selection,lineHeight},ls),ps)
	| sameSelection newSelection selection
		= ((lbState,ls),ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
400
	# lbState		= {lbState & selection = newSelection}
401
	# (lbState,ps)	= applySelfun lbState ps
Diederik van Arkel's avatar
Diederik van Arkel committed
402
403
404
405
406
407
408
409
	# (wdef,ps)		= accPIO (getParentWindow controlId) ps
	| isNothing wdef
		= ((lbState,ls),ps)
	# (_,frame)		= getControlViewFrame controlId (fromJust wdef)
	| isNothing frame
		= ((lbState,ls),ps)
	# frame			= fromJust frame
	# (x1,x2)		= (frame.corner1.x,frame.corner2.x)
410
411
412
413
414
	# (update,lbState)
					= updatelook lbState selection (x1,x2)
	#! ps			= appPIO (appControlPicture controlId update) ps
	# (newLook,lbState)
					= customlook lbState
Diederik van Arkel's avatar
Diederik van Arkel committed
415
	#! ps			= appPIO (setListBoxLook customId False (True,newLook)) ps
Diederik van Arkel's avatar
Diederik van Arkel committed
416
	#! ps			= scrolltoselection customId lineHeight tMargin bMargin newSelection ps
Diederik van Arkel's avatar
Diederik van Arkel committed
417
418
	= ((lbState,ls),ps)
where
Diederik van Arkel's avatar
Diederik van Arkel committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//	sameSelection n [] = False
//	sameSelection n [o] = n == o
//	sameSelection _ _ = False
	sameSelection n o = sort n == sort o

//--

joinKeyboards (ControlKeyboard oFilter oState oKeyboard) (ControlKeyboard nFilter nState nKeyboard)
	// ignore SelectState attributs => always Able
	= ControlKeyboard filter Able keyboard
where
	filter keyState = nFilter keyState || oFilter keyState
	
	keyboard keyState ks
		| nFilter keyState
			= nKeyboard keyState ks
			= oKeyboard keyState ks
Diederik van Arkel's avatar
Diederik van Arkel committed
436
437
438
439
440
441
442

//--

elbKeyboard :: .ControlAttribute *((ExtListBoxState *(PSt .a),.b),*PSt .a);
elbKeyboard = ControlKeyboard keyFilter Able keyboard

keyFilter :: KeyboardState -> Bool
443
444
keyFilter (SpecialKey _ (KeyDown _) _) = True
keyFilter k  = False
Diederik van Arkel's avatar
Diederik van Arkel committed
445

Diederik van Arkel's avatar
Diederik van Arkel committed
446
//keyboard :: !.KeyboardState *((.ExtListBoxState *(PSt .a),.b),*PSt .a) -> *((ExtListBoxState *(PSt .a),.b),*PSt .a);
Diederik van Arkel's avatar
Diederik van Arkel committed
447
448
449
450
451
452
453
454
455
456
457
458
keyboard (SpecialKey key (KeyDown repeat) {shiftDown,controlDown}) ((lbState=:{tMargin,listboxId,selection,items,lineHeight,funs,shiftfuns},ls),ps)
	| key == enterKey || key == returnKey
		| not hasSelection
			= ((lbState,ls),ps)
		// execute selection
		// shift-execute
		// ? what if mul-selection
		| shiftDown
			# ps	= (shiftfuns!!(lastSelection-1)) ps
			= ((lbState,ls),ps)
		# ps	= (funs!!(lastSelection-1)) ps
		= ((lbState,ls),ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
459
460
461
	| key == escapeKey
		# newSelection	= []
		= updateListBoxLook listboxId newSelection ((lbState,ls),ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
462
463
464
465
466
	| key == upKey
		// set selection one earlier
		// if shift - extend selection one up
		// if control ...
		// if control-shift ...
Diederik van Arkel's avatar
Diederik van Arkel committed
467
468
469
470
471
472
473
474
475
476
477
		# newIndex		= if hasSelection (max 1 (lastSelection - 1)) 1
		| shiftDown
			| hasSelection
				# listSelection = if (lastSelection < newIndex)
									[lastSelection..newIndex]
									[newIndex..lastSelection]
				# newSelection	= removeDup [newIndex:listSelection++selection]
			= updateListBoxLook listboxId newSelection ((lbState,ls),ps)
			# newSelection	= [newIndex]
			= updateListBoxLook listboxId newSelection ((lbState,ls),ps)
		# newSelection	= [newIndex]
Diederik van Arkel's avatar
Diederik van Arkel committed
478
479
480
481
482
483
		= updateListBoxLook listboxId newSelection ((lbState,ls),ps)
	| key == downKey
		// set selection one later
		// if shift - extend selection one up
		// if control ...
		// if control-shift ...
Diederik van Arkel's avatar
Diederik van Arkel committed
484
485
486
487
488
489
490
491
492
493
494
		# newIndex		= if hasSelection (min nrItems (lastSelection + 1)) nrItems
		| shiftDown
			| hasSelection
				# listSelection = if (lastSelection < newIndex)
									[lastSelection..newIndex]
									[newIndex..lastSelection]
				# newSelection	= removeDup [newIndex:listSelection++selection]
			= updateListBoxLook listboxId newSelection ((lbState,ls),ps)
			# newSelection	= [newIndex]
			= updateListBoxLook listboxId newSelection ((lbState,ls),ps)
		# newSelection	= [newIndex]
Diederik van Arkel's avatar
Diederik van Arkel committed
495
496
497
498
499
500
		= updateListBoxLook listboxId newSelection ((lbState,ls),ps)
	| key == beginKey
		// set selection first
		// if shift - extend selection one up
		// if control ...
		// if control-shift ...
Diederik van Arkel's avatar
Diederik van Arkel committed
501
		# newSelection	= [1]
Diederik van Arkel's avatar
Diederik van Arkel committed
502
503
504
505
506
507
		= updateListBoxLook listboxId newSelection ((lbState,ls),ps)
	| key == endKey
		// set selection last
		// if shift - extend selection one up
		// if control ...
		// if control-shift ...
Diederik van Arkel's avatar
Diederik van Arkel committed
508
		# newSelection	= [nrItems]
Diederik van Arkel's avatar
Diederik van Arkel committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
		= updateListBoxLook listboxId newSelection ((lbState,ls),ps)
	# (wstate,ps)	= accPIO (getParentWindow customId) ps
	| isNothing wstate
		= ((lbState,ls),ps)
	# wstate	= fromJust wstate
	# (ok,frame) = getControlViewFrame customId wstate
	| not ok
		= ((lbState,ls),ps)
	| isNothing frame
		= ((lbState,ls),ps)
	# frame = fromJust frame
	# linesOnPage = max 1 (dec ((frame.corner2.y - frame.corner1.y) / lineHeight))
	| key == pgUpKey
		// set selection page-up
		// if shift - extend selection one up
		// if control ...
		// if control-shift ...
		# top = (lastSelection-2) * lineHeight + tMargin
		# newSelection	= if hasSelection
							(if (top <= frame.corner1.y)		//topLine
								(max 1 (lastSelection - linesOnPage))
Diederik van Arkel's avatar
Diederik van Arkel committed
530
								(1 + ((frame.corner1.y - tMargin) / lineHeight))	//topOfPage
Diederik van Arkel's avatar
Diederik van Arkel committed
531
532
							)
							1
Diederik van Arkel's avatar
Diederik van Arkel committed
533
		= updateListBoxLook listboxId [newSelection] ((lbState,ls),ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
534
535
536
537
538
539
540
541
542
	| key == pgDownKey
		// set selection page-down
		// if shift - extend selection one up
		// if control ...
		// if control-shift ...
		# bot = (inc lastSelection) * lineHeight + tMargin
		# newSelection	= if hasSelection
							(if (bot >= frame.corner2.y)		//bottomLine
								(min nrItems (lastSelection + linesOnPage))
Diederik van Arkel's avatar
Diederik van Arkel committed
543
								(min nrItems (frame.corner2.y / lineHeight))	//bottomOfPage
Diederik van Arkel's avatar
Diederik van Arkel committed
544
545
							)
							nrItems
Diederik van Arkel's avatar
Diederik van Arkel committed
546
		= updateListBoxLook listboxId [newSelection] ((lbState,ls),ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
547
548
549
	= ((lbState,ls),ps)
where
	nrItems		= length items
550
	customId	= listboxId.controlId
Diederik van Arkel's avatar
Diederik van Arkel committed
551
552
553
554
555
	hasSelection
		| isEmpty selection = False
		= True
	lastSelection = hd selection
keyboard _ state
Diederik van Arkel's avatar
Diederik van Arkel committed
556
	= state	//abort "ExtListBox.icl: keyboard function only supports special keys"
Diederik van Arkel's avatar
Diederik van Arkel committed
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
	
//scrolltoselection :: .Id .Int .Bool .Int !*(PSt .a) -> *PSt .a;
scrolltoselection {controlId} lineHeight tMargin bMargin selection ps
	| not singlesel = ps
	# top = (selitem-1) * lineHeight
	# bot = selitem * lineHeight + tMargin + bMargin
	# (wdef,ps) = accPIO (getParentWindow controlId) ps
	| isNothing wdef = ps
	# wdef = fromJust wdef
	# (exists,frame) = getControlViewFrame controlId wdef
	| not exists = ps
	| isNothing frame = ps
	# frame = fromJust frame
	# delta = top - frame.corner1.y
	| delta < 0
		= appPIO (moveControlViewFrame controlId {vx=0, vy=delta}) ps
	# delta = bot - frame.corner2.y
	| delta > 0
		= appPIO (moveControlViewFrame controlId {vx=0, vy=delta}) ps
	= ps
where
	singlesel = length selection == 1
	selitem = hd selection

//	The mouse responds only to MouseDowns:
mouseFilter :: !MouseState -> Bool
mouseFilter (MouseDown _ _ _)		= True
mouseFilter _						= False

586
587
588
589
applySelfun ls=:{newselfun,selection} ps
	#! ps	= newselfun selection ps
	= (ls,ps)
	
Diederik van Arkel's avatar
Diederik van Arkel committed
590
//	The mouse either sets, adds, or removes items to the selection:
Diederik van Arkel's avatar
Diederik van Arkel committed
591
//mouseFunction :: !.MouseState *((.ExtListBoxState *(PSt .a),.b),*PSt .a) -> *((ExtListBoxState *(PSt .a),.b),*PSt .a);
Diederik van Arkel's avatar
Diederik van Arkel committed
592
mouseFunction (MouseDown pos {shiftDown,controlDown} 1) ((listboxState=:{tMargin,items,selection,lineHeight,initHeight},ls),ps)
593
594
595
596
	# listboxState				= {ExtListBoxState | listboxState & selection=okSelection}
	# (listboxState,ps)			= applySelfun listboxState ps
	# (newLook,listboxState)	= customlook listboxState
	# ps						= appPIO (setControlLooks [(customId,True,(True,newLook))]) ps
Diederik van Arkel's avatar
Diederik van Arkel committed
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
	= ((listboxState,ls),ps)
where
	nrItems		= length items
	hasSelection= not (isEmpty selection)
	[prevIndex:_]= selection
	listSelection
		| prevIndex < newIndex
			= [prevIndex..newIndex]
			= [newIndex..prevIndex]
	newIndex	= (pos.y-tMargin)/lineHeight+1
	newSelection
		| shiftDown
			| hasSelection
				= removeDup [newIndex:listSelection++selection]
			= [newIndex]
		| controlDown
			| isMember newIndex selection
				= removeMembers selection [newIndex]
			= [newIndex:selection]
		= [newIndex]
	okSelection	= filter (isBetween 1 nrItems) newSelection
	customId	= listboxState.listboxId.controlId

mouseFunction (MouseDown pos {shiftDown} _) ((listboxState=:{tMargin,items,funs,shiftfuns,lineHeight,initHeight},ls),ps)
// double click
622
623
624
625
	# listboxState				= {ExtListBoxState | listboxState & selection=okSelection}
	# (listboxState,ps)			= applySelfun listboxState ps
	# (newLook,listboxState)	= customlook listboxState
	# ps						= appPIO (setControlLooks [(customId,True,(True,newLook))]) ps
Diederik van Arkel's avatar
Diederik van Arkel committed
626
627
628
629
630
631
632
633
634
635
636
637
638
639
	| newIndex < 1 || newIndex > length funs
		= ((listboxState,ls),ps)
	| shiftDown
		# ps	= (shiftfuns!!(newIndex-1)) ps
		= ((listboxState,ls),ps)
	# ps	= (funs!!(newIndex-1)) ps
	= ((listboxState,ls),ps)
where
	customId	= listboxState.listboxId.controlId
	newIndex	= (pos.y-tMargin)/lineHeight+1
	nrItems		= length items
	newSelection= [newIndex]
	okSelection	= filter (isBetween 1 nrItems) newSelection

Diederik van Arkel's avatar
Diederik van Arkel committed
640
641
642
mouseFunction arg state
//	= abort "ExtListBox.icl: mouse only supports MouseDowns"
	= trace_n` ("ExtListBox.icl: unexpected argument to mouseFunction",arg) state
Diederik van Arkel's avatar
Diederik van Arkel committed
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661

//	The functions below take care of the proper communication with the receiver that
//	belongs to the listbox control.

getExtListBoxSelection :: !(ExtListBoxId .a) !(PSt .l) -> (!(!Bool,![(String,!Index)]),!PSt .l)
getExtListBoxSelection {receiverId} pState
	# ((_,maybe_out),pState)	= syncSend2 receiverId InGetSelection pState
	| isNothing maybe_out
		= ((False,[]),pState)
	# result					= case (fromJust maybe_out) of
									(OutGetSelection selection)	-> (True,selection)
									_							-> (False,[])
	| otherwise
		= (result,pState)

setExtListBoxSelection :: !(ExtListBoxId .a) ![Index] !(PSt .l) -> PSt .l
setExtListBoxSelection {receiverId} selection pState
	= snd (syncSend2 receiverId (InSetSelection selection) pState)

Diederik van Arkel's avatar
Diederik van Arkel committed
662
663
664
665
setExtListBoxSelectionAll :: !(ExtListBoxId .a) !(PSt .l) -> PSt .l
setExtListBoxSelectionAll {receiverId} pState
	= snd (syncSend2 receiverId InSetSelectAll pState)

Diederik van Arkel's avatar
Diederik van Arkel committed
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
getExtListBoxItems :: !(ExtListBoxId .a) !(PSt .l) -> (!(!Bool,![String]),!PSt .l)
getExtListBoxItems {receiverId} pState
	# ((_,maybe_out),pState)	= syncSend2 receiverId InGetItems pState
	| isNothing maybe_out
		= ((False,[]),pState)
	# result					= case (fromJust maybe_out) of
									(OutGetItems items)	-> (True,items)
									_					-> (False,[])
	| otherwise
		= (result,pState)

openExtListBoxItems :: !(ExtListBoxId .a) !Index ![ExtListBoxItem .a] !(PSt .l) -> PSt .l
openExtListBoxItems {receiverId} index items pState
	= snd (syncSend2 receiverId (InOpenItems index items) pState)

execExtListBoxItem  :: !(ExtListBoxId .a) !Index !(PSt .l) -> PSt .l
execExtListBoxItem {receiverId} index  pState
	= snd (syncSend2 receiverId (InExecItem index) pState)

appendExtListBoxItems :: !(ExtListBoxId .a) ![ExtListBoxItem .a] !(PSt .l) -> PSt .l
appendExtListBoxItems {receiverId} items pState
	= snd (syncSend2 receiverId (InAppendItems items) pState)

closeExtListBoxItems :: !(ExtListBoxId .a) ![Index] !(PSt .l) -> PSt .l
closeExtListBoxItems {receiverId} items pState
	= snd (syncSend2 receiverId (InCloseItems items) pState)

closeAllExtListBoxItems :: !(ExtListBoxId .a) !(PSt .l) -> PSt .l
closeAllExtListBoxItems {receiverId} pState
	= snd (syncSend2 receiverId (InCloseAllItems) pState)

showExtListBoxControl :: !(ExtListBoxId .a) !*(IOSt .l) -> *IOSt .l
showExtListBoxControl {controlId} ioState = showControls [controlId] ioState

hideExtListBoxControl :: !(ExtListBoxId .a) !*(IOSt .l) -> *IOSt .l
hideExtListBoxControl {controlId} ioState = hideControls [controlId] ioState

enableExtListBoxControl :: !(ExtListBoxId .a) !*(IOSt .l) -> *IOSt .l
enableExtListBoxControl {controlId} ioState = enableControls [controlId] ioState

disableExtListBoxControl :: !(ExtListBoxId .a) !*(IOSt .l) -> *IOSt .l
disableExtListBoxControl {controlId} ioState = disableControls [controlId] ioState

setExtListBoxPen :: !(ExtListBoxId .a) ![PenAttribute] !(PSt .l) -> PSt .l
setExtListBoxPen {receiverId} pen ps
	= snd (syncSend2 receiverId (InSetPen pen) ps)

getExtListBoxPen :: !(ExtListBoxId .a) !(PSt .l) -> (Maybe [PenAttribute],PSt .l)
getExtListBoxPen {receiverId} ps
	# ((_,maybe_out),ps)	= syncSend2 receiverId InGetPen ps
	| isNothing maybe_out
		= (Nothing,ps)
	# result					= case (fromJust maybe_out) of
									(OutGetPen pen)		-> (Just pen)
									_					-> Nothing
	| otherwise
		= (result,ps)

setListBoxLook listboxId=:{controlId} redraw newlook io = setControlLook controlId redraw newlook io

exec_next :: !Bool !(ExtListBoxId .ps) !(PSt .l) -> (PSt .l)
exec_next next lbId ps
	#	((ok,sel),ps)	= getExtListBoxSelection lbId ps
	| not ok = ps
	#	fun				= if next inc dec
		idx				= (if (isEmpty sel) 1 (fun (snd(hd sel))))
		((ok,lst),ps)	= getExtListBoxItems lbId ps
	| not ok =  ps
	#	num				= length lst
	| num == 0 = ps
	#	idx				= normalise idx 1 num num
		ps				= execExtListBoxItem lbId idx ps
		ps				= setExtListBoxSelection lbId [idx] ps
	= ps
where
	normalise num min max incr
		| num < min = normalise (num+incr) min max incr
		| num > max = normalise (num-incr) min max incr
		= num

upSelItem :: !(ExtListBoxId .ps) !*([a],!*PSt .l) -> *(Bool,*([a],*PSt .l))
upSelItem lbId=:{receiverId} (ls,ps)
	// get sel
	# ((ok,sel),ps)	= getExtListBoxSelection lbId ps
	// if not single sel fail...
	| not ok || length sel <> 1
		= (False,(ls,ps))
	// if first fail...
	# (_,sel) = hd sel
	| sel == 1
		= (False,(ls,ps))
	// twiddle with prev
	# ((_,r),ps) = syncSend2 receiverId (InTwiddleItems sel (dec sel)) ps
	| isNothing r
		= (False,(ls,ps))
	# ls = twiddle sel (dec sel) ls
	# ps = setExtListBoxSelection lbId [dec sel] ps
	= (True,(ls,ps))
dnSelItem :: !(ExtListBoxId .ps) !*([a],!*PSt .l) -> *(Bool,*([a],*PSt .l))
dnSelItem lbId=:{receiverId} (ls,ps)
	// get sel
	# ((ok,sel),ps)	= getExtListBoxSelection lbId ps
	// if not single sel fail...
	| not ok || length sel <> 1
		= (False,(ls,ps))
	// if last fail...
	# ((ok,its),ps)	= getExtListBoxItems lbId ps
	| not ok
		= (False,(ls,ps))
	# (_,sel) = hd sel
	| sel == length its
		= (False,(ls,ps))
	// twiddle with prev
	# ((_,r),ps) = syncSend2 receiverId (InTwiddleItems sel (inc sel)) ps
	| isNothing r
		= (False,(ls,ps))
	# ls = twiddle sel (inc sel) ls
	# ps = setExtListBoxSelection lbId [inc sel] ps
	= (True,(ls,ps))

//	Auxiliary functions:

smap f [x:xs] s
	# (y,s)	= f x s
	# (ys,s)= smap f xs s
	= ([y:ys],s)
smap _ _ s
	= ([],s)

isBetween :: x x x -> Bool	| Ord x
isBetween low up x
	= low<=x && x<=up

setBetween :: x x x -> x | Ord x
setBetween low up x
	| x<low		= low
	| x<up		= x
	| otherwise	= up

twiddle idx1 idx2 items = updateAt idx1` itm2 (updateAt idx2` itm1 items)
where
	itm1 = items!!idx1`	// do error checks...
	itm2 = items!!idx2`	// do error checks...
	idx1` = dec idx1
	idx2` = dec idx2