FilteredListBox.icl 22.5 KB
Newer Older
Diederik van Arkel's avatar
Diederik van Arkel committed
1 2
implementation module FilteredListBox

3
import StdBool, StdEnum, StdList, StdMisc, StdOrdList, StdTuple, StdFunc, StdArray
Diederik van Arkel's avatar
Diederik van Arkel committed
4 5 6 7 8 9 10 11 12 13 14 15 16
import StdControl, StdControlReceiver, StdId, StdPicture, StdPSt, StdReceiver, StdWindow
import StdControlAttribute
import ioutil

from commondef import strictSeq

::	FilteredListBoxState
	=	{ items			:: ![String]					// All items to be displayed
		, selection		:: ![Index]						// The current selection
		, listboxId		:: !FilteredListBoxId			// The ids related to this list box
		, lineHeight	:: !Int
		, initHeight	:: !Int
		, pen			:: ![PenAttribute]
17
		, ifilter		:: ![String] -> [String]		// the item filter
Diederik van Arkel's avatar
Diederik van Arkel committed
18
		, aitems		:: ![String]					// all items (unfiltered)
19
		, domain		:: !Rectangle
Diederik van Arkel's avatar
Diederik van Arkel committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
		}

::	FilteredListBoxId
	=	{	fcontrolId	:: !Id							// The Id of the outmost CompoundControl
		,	freceiverId	:: !R2Id FilteredMessageIn FilteredMessageOut	// The Id of the Receiver2Control that handles message passing
		}

openFilteredListBoxId :: !*env -> (!FilteredListBoxId,!*env)	| Ids env
openFilteredListBoxId env
	# (cid, env)	= openId env
	# (rid,env)	= openR2Id env
	= ({fcontrolId=cid,freceiverId=rid},env)

::	FilteredMessageIn
	=	FInGetSelection										// Request to retrieve current selection
	|	FInSetSelection		[Index]							// Request to set the selection to the given index
	|	FInGetItems											// Request to retrieve all current items
	|	FInAppendItems		[FilteredListBoxItem]			// Request to add items behind the last element
	|	FInCloseAllItems									// Request to remove all current items
	|	FInSetPen			[PenAttribute]					// Request to set control pen
	|	FInGetPen											// Request to get control pen
41
	| FInSetFilter ([String]->[String])
Diederik van Arkel's avatar
Diederik van Arkel committed
42 43 44 45 46 47 48 49 50 51 52
	| FInGetFilter

::	FilteredMessageOut
	=	FOutGetSelection		[(String,Index)]			// Reply to retrieve the current selection
	|	FOutSetSelection									// Reply to set the selection
	|	FOutGetItems			[String]					// Reply to get all items
	|	FOutAppendItems										// Reply to append items
	|	FOutCloseAllItems									// Reply to remove all items
	|	FOutSetPen											// Reply to set the control pen
	|	FOutGetPen			[PenAttribute]					// Reply to get the control pen
	| FOutSetFilter
53
	| FOutGetFilter ([String] -> [String])
Diederik van Arkel's avatar
Diederik van Arkel committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

:: FilteredListBoxItem :== String

:: FilteredListBoxControl ls ps
	= FilteredListBoxControl [FilteredListBoxItem] [Int] FilteredListBoxId [ControlAttribute *(*(FilteredListBoxState,ls),ps)]

instance Controls FilteredListBoxControl
where
	getControlType _ = "FilteredListBoxControl"
	controlToHandles (FilteredListBoxControl items selection listboxId attrs) ps
		#! ((lineHeight,initHeight),ps)	= accScreenPicture (liheights penAtts) ps
		#! (domain,ps)					= calcControlDomain penAtts items ps
		= controlToHandles (imp lineHeight initHeight domain) ps
	where
		liheights pen pic
			# pic				= setPenAttributes pen pic
			# (metrics,pic)		= getPenFontMetrics pic
			# lineHeight		= fontLineHeight metrics
			# initHeight		= metrics.fAscent + metrics.fLeading
			= ((lineHeight,initHeight),pic)
74 75 76
		imp lineHeight initHeight domain
			# (customLook,listboxState)	= customlook listboxState
			=
Diederik van Arkel's avatar
Diederik van Arkel committed
77 78 79 80 81 82 83 84 85
			{	addLS	= listboxState
			,	addDef	= CompoundControl
					(NilLS)
					(
					[	ControlId			listboxId.fcontrolId
					,	ControlItemSpace	0 0							// No itemspace
					,	ControlHScroll		(altScrollFunction Horizontal 10)
					,	ControlVScroll		(altScrollFunction Vertical lineHeight)
					,	ControlViewDomain	domain
86
					,	ControlLook			True customLook
Diederik van Arkel's avatar
Diederik van Arkel committed
87 88 89 90 91 92 93 94 95 96 97 98 99
					:	listboxAtts
					]
					)
				  :+:	Receiver2 listboxId.freceiverId receiver []
			}
		where
			listboxState =
				{ items			= items
				, selection		= selection
				, listboxId		= listboxId
				, lineHeight	= lineHeight
				, initHeight	= initHeight
				, pen			= penAtts
100
				, ifilter		= id
Diederik van Arkel's avatar
Diederik van Arkel committed
101
				, aitems		= items
102
				, domain		= domain
Diederik van Arkel's avatar
Diederik van Arkel committed
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 129 130 131 132 133 134
				}

		listboxAtts = map toLBCA (filter isListBoxControlAttribute attrs)
		penAtts = flatten (map getControlPenAtt (filter isControlPen attrs))

		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 c)		= ControlMouse a b c
		toLBCA (ControlKeyboard a b c)	= ControlKeyboard a b c
		toLBCA _ = abort "FilteredListBox:toLBCA: unsupported ControlAttribute"
			
				
		receiver (FInSetFilter filt) ((listboxState=:{pen,aitems},ls),ps)
135
			# items = filt aitems
Diederik van Arkel's avatar
Diederik van Arkel committed
136 137 138
			# listboxState = {listboxState & ifilter = filt, items = items}
			// refresh...
			# (newDomain,ps)		= calcControlDomain pen items ps
139
			# (newLook,listboxState)= customlook {listboxState & domain=newDomain}
Diederik van Arkel's avatar
Diederik van Arkel committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
			# ps					= appPIO (seq
				[ setControlViewDomain customId newDomain
				, setControlLooks [(customId,True,(True,newLook))]
				]) ps
			= (FOutSetFilter,((listboxState,ls),ps))
		where
			customId		= listboxState.listboxId.fcontrolId
		receiver (FInGetFilter) ((listboxState=:{ifilter},ls),ps)
			= (FOutGetFilter ifilter,((listboxState,ls),ps))
		//	Return the current selection:
		receiver FInGetSelection ((listboxState=:{items,selection},ls),ps)
			= (FOutGetSelection (map (\index->(items!!(index-1),index)) selection),((listboxState,ls),ps))
		
		//	Set a new selection:
		receiver (FInSetSelection newSelection) ((listboxState=:{lineHeight,initHeight},ls),ps)
			# listboxState	= {FilteredListBoxState | listboxState & selection=newSelection}
156
			# (newLook,listboxState)= customlook listboxState
157
			#! ps			= scroll_to_selection newSelection lineHeight customId ps
Diederik van Arkel's avatar
Diederik van Arkel committed
158 159 160 161 162 163 164 165 166 167 168
			#! ps			= appPIO (setControlLooks [(customId,True,(True,newLook))]) ps
			= (FOutSetSelection,((listboxState,ls),ps))
		where
			customId		= listboxState.listboxId.fcontrolId
		
		//	Return the current elements:

		receiver FInGetItems ((listboxState=:{items},ls),ps)
			= (FOutGetItems items,((listboxState,ls),ps))
		
		//	Append elements:
169 170 171
		receiver (FInAppendItems newItems) ((listboxState=:{pen,items,aitems,ifilter,lineHeight,initHeight,domain},ls),ps)
			# listboxState			= {listboxState & items=allFilteredItems, aitems = aitems++newItems}
			| isEmpty filteredNewItems
Diederik van Arkel's avatar
Diederik van Arkel committed
172
				= (FOutAppendItems,((listboxState,ls),ps))
173 174 175 176 177
			# (newDomain,ps)		= adjustControlDomain pen filteredNewItems domain ps
			# (newLook,listboxState)= customlook {listboxState & domain=newDomain}
			# (wdef,ps) = accPIO (getParentWindow customId) ps
			# delta = scrolltoend newDomain wdef
			# ps = appPIO (seq
Diederik van Arkel's avatar
Diederik van Arkel committed
178 179
				[ setControlLooks [(customId,False,(True,newLook))]
				, setControlViewDomain customId newDomain
180
				, if (delta==0) id (moveControlViewFrame customId {vx=0, vy=delta})
Diederik van Arkel's avatar
Diederik van Arkel committed
181 182 183 184
				]) ps
			= (FOutAppendItems,((listboxState,ls),ps))
		where
			customId				= listboxState.listboxId.fcontrolId
185
			filteredNewItems		= ifilter newItems
186
			allFilteredItems		= items++filteredNewItems
Diederik van Arkel's avatar
Diederik van Arkel committed
187

188
			scrolltoend dom=:{corner2={y=bot}} wdef
Diederik van Arkel's avatar
Diederik van Arkel committed
189
				| isNothing wdef
190
					= zero
Diederik van Arkel's avatar
Diederik van Arkel committed
191 192 193
				# wdef = fromJust wdef
				# (exists,frame) = getControlViewFrame customId wdef
				| not exists || isNothing frame
194
					= zero
Diederik van Arkel's avatar
Diederik van Arkel committed
195
				# frame = fromJust frame
196
				= bot - frame.corner2.y
Diederik van Arkel's avatar
Diederik van Arkel committed
197 198 199
		
		//	Remove elements:
		//	Remove all:
200
		receiver (FInCloseAllItems) ((listboxState=:{listboxId,pen,items,selection,lineHeight,initHeight},ls),ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
201 202
			# listboxState	= {listboxState & items=[], aitems = [], selection=[]}
			# (newDomain,ps)= calcControlDomain pen [] ps
203
			# (newLook,listboxState)= customlook {listboxState & domain=newDomain}
Diederik van Arkel's avatar
Diederik van Arkel committed
204
			# ps			= appPIO (seq
205 206
								[ setControlViewDomain listboxId.fcontrolId newDomain
								, setControlLooks [(listboxId.fcontrolId,True,(True,newLook))]
Diederik van Arkel's avatar
Diederik van Arkel committed
207 208 209 210
								]) ps
			= (FOutCloseAllItems,((listboxState,ls),ps))
		
		// Set control pen:
211
		receiver (FInSetPen newpen) ((listboxState=:{listboxId,items,pen},ls),ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
212 213 214
			# pen							= removeDupAtt (newpen++pen)
			# (newDomain,ps)				= calcControlDomain pen items ps
			# ((lineHeight,initHeight),ps)	= accScreenPicture (liheights pen) ps
215 216
			# listboxState
				= {listboxState & domain = newDomain, pen = pen, lineHeight = lineHeight, initHeight = initHeight }
217
			# (newLook,listboxState)= customlook listboxState
Diederik van Arkel's avatar
Diederik van Arkel committed
218
			# ps			= appPIO (seq
219 220 221
								[ setControlViewDomain listboxId.fcontrolId newDomain
								, setControlLooks [(listboxId.fcontrolId,True,(True,newLook))]
								, setControlScrollFunction listboxId.fcontrolId Vertical (altScrollFunction Vertical lineHeight)
Diederik van Arkel's avatar
Diederik van Arkel committed
222 223 224 225 226 227 228 229 230
								]) ps
			= (FOutSetPen,((listboxState,ls),ps))
		
		// Get control pen:
		receiver (FInGetPen) ((listboxState=:{pen},ls),ps)
			= (FOutGetPen pen,((listboxState,ls),ps))

		calcControlDomain :: ![.PenAttribute] ![.{#Char}] !*(PSt .a) -> *(!.Rectangle,!*PSt .a);
		calcControlDomain pen items ps
231
			= accPIO (accScreenPicture calc) ps
Diederik van Arkel's avatar
Diederik van Arkel committed
232 233 234 235 236 237 238 239 240 241 242 243
		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)
				# newDomain			= {corner1=zero,corner2={x=maxWidth,y=height}} // calculate new domain...
				= (newDomain,pic)

244 245 246 247 248 249 250 251 252 253 254 255 256
		adjustControlDomain :: ![.PenAttribute] ![.{#Char}] !.Rectangle !*(PSt .a) -> *(!.Rectangle,!*PSt .a);
		adjustControlDomain pen items {corner2={x=oldMaxWidth,y=oldHeight}} ps
			= accPIO (accScreenPicture calc) ps
		where
			calc pic
				# pic				= setPenAttributes pen pic
				# (metrics,pic)		= getPenFontMetrics pic
				# (itemWidths,pic)	= getPenFontStringWidths items pic
				# maxWidth			= maxList [oldMaxWidth:itemWidths]
				# nrItems			= length items
				# height			= oldHeight + nrItems*(fontLineHeight metrics)
				# newDomain			= {corner1=zero,corner2={x=maxWidth,y=height}} // calculate new domain...
				= (newDomain,pic)
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

scroll_to_selection newSelection lineHeight customId ps
	# first_item=hd newSelection
	# last_item=last newSelection
	# top = (first_item-1) * lineHeight
	# bot = last_item * lineHeight
	# (wdef,ps) = accPIO (getParentWindow customId) ps
	| isNothing wdef = ps
	# wdef = fromJust wdef
	# (exists,frame) = getControlViewFrame customId wdef
	| not exists = ps
	| isNothing frame = ps
	# frame = fromJust frame
	# delta = top - frame.corner1.y
	| delta < 0 || (bot-top) > (frame.corner2.y-frame.corner1.y)
		= appPIO (moveControlViewFrame customId {vx=0, vy=delta}) ps
	# delta = bot - frame.corner2.y
	| delta > 0
		= appPIO (moveControlViewFrame customId {vx=0, vy=delta}) ps
	= ps
Diederik van Arkel's avatar
Diederik van Arkel committed
277 278 279 280 281 282 283 284 285 286 287 288
 
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 _      = []

//	The look of the custom control lists all items and the current selection
289 290 291 292
customlook ls=:{items,selection,pen,lineHeight,initHeight}
	= (customlook,ls)
where
  customlook _ {newFrame} pict
293 294 295 296 297 298 299 300 301 302
	# min_y			= newFrame.corner1.y
 	# max_y			= newFrame.corner2.y
	# pict			= setPenAttributes pen pict
	# pict			= unfill newFrame pict
	# (_,pict)		= foldl drawline (initHeight,pict) items 
					with
					  drawline (y,p) line
						| y<min_y || y-lineHeight>max_y
							= (y+lineHeight,p)
							= (y+lineHeight,drawAt {x=0,y=y} line p)
303
	# pict			= hilite_selections selection pict
Diederik van Arkel's avatar
Diederik van Arkel committed
304
	= pict
305
  where
306 307
	x1				= newFrame.corner1.x
	x2				= newFrame.corner2.x
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

	hilite_selections [selection:selections] pict
		# (last_line_n,selections) = determine_last_line_of_rectangle selection selections
		# pict = drawsel selection last_line_n pict
		= hilite_selections selections pict
		with
			determine_last_line_of_rectangle line_n [next_line_n:next_lines]
				| next_line_n==line_n+1
					= determine_last_line_of_rectangle next_line_n next_lines
			determine_last_line_of_rectangle line_n lines
				= (line_n,lines)
	hilite_selections [] pict
		= pict;
	
	drawsel i j		= hilite {corner1={x=x1,y=(i-1)*lineHeight}, corner2={x=x2,y=j*lineHeight-1}}
Diederik van Arkel's avatar
Diederik van Arkel committed
323 324 325 326 327 328 329 330 331 332

//--

flbMouse :: ({#Char} -> .(*(PSt .a) -> *PSt .a)) -> .ControlAttribute *((FilteredListBoxState,.b),*PSt .a);
flbMouse efun = ControlMouse mouseFilter Able (mouse efun)

flbKeyboard :: ({#Char} -> .(*(PSt .a) -> *PSt .a)) -> .ControlAttribute *((FilteredListBoxState,.b),*PSt .a);
flbKeyboard efun = ControlKeyboard keyFilter Able (keyboard efun)

keyFilter :: KeyboardState -> Bool
333
keyFilter (SpecialKey _ (KeyDown _) _) = True
334
keyFilter _ = False
Diederik van Arkel's avatar
Diederik van Arkel committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

keyboard efun (SpecialKey key (KeyDown repeat) {shiftDown,controlDown}) ((lbState=:{selection,items,lineHeight},ls),ps)
	| key == enterKey
		| not hasSelection
			= ((lbState,ls),ps)
		// execute selection
		// shift-execute
		// ? what if mul-selection
//		| shiftDown
//			# ps	= (shiftfuns!!(lastSelection-1)) ps
//			= ((lbState,ls),ps)
		# ps	= efun (items!!(lastSelection-1)) ps
		= ((lbState,ls),ps)
	| key == upKey
		// set selection one earlier
		// if shift - extend selection one up
		// if control ...
		// if control-shift ...
353 354
		# newSelection	= if hasSelection [max 1 (lastSelection - 1)] [1]
		# lbState		= {lbState & selection = newSelection}
355 356
		# (newLook,lbState)
						= customlook lbState
357
		# ps			= scroll_to_selection newSelection lineHeight customId ps
Diederik van Arkel's avatar
Diederik van Arkel committed
358 359 360 361 362 363 364
		# ps			= appPIO (setControlLooks [(customId,True,(True,newLook))]) ps
		= ((lbState,ls),ps)
	| key == downKey
		// set selection one later
		// if shift - extend selection one up
		// if control ...
		// if control-shift ...
365 366
		# newSelection	= [if hasSelection (min nrItems (lastSelection + 1)) nrItems]
		# lbState		= {lbState & selection = newSelection}
367 368
		# (newLook,lbState)
						= customlook lbState
369
		# ps			= scroll_to_selection newSelection lineHeight customId ps
Diederik van Arkel's avatar
Diederik van Arkel committed
370 371 372 373 374 375 376
		# ps			= appPIO (setControlLooks [(customId,True,(True,newLook))]) ps
		= ((lbState,ls),ps)
	| key == beginKey
		// set selection first
		// if shift - extend selection one up
		// if control ...
		// if control-shift ...
377 378
		# newSelection	= [1]
		# lbState		= {lbState & selection = newSelection}
379 380
		# (newLook,lbState)
						= customlook lbState
381
		# ps			= scroll_to_selection newSelection lineHeight customId ps
Diederik van Arkel's avatar
Diederik van Arkel committed
382 383 384 385 386 387 388
		# ps			= appPIO (setControlLooks [(customId,True,(True,newLook))]) ps
		= ((lbState,ls),ps)
	| key == endKey
		// set selection last
		// if shift - extend selection one up
		// if control ...
		// if control-shift ...
389 390
		# newSelection	= [nrItems]
		# lbState		= {lbState & selection = newSelection}
391 392
		# (newLook,lbState)
						= customlook lbState
393
		# ps			= scroll_to_selection newSelection lineHeight customId ps
Diederik van Arkel's avatar
Diederik van Arkel committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
		# ps			= appPIO (setControlLooks [(customId,True,(True,newLook))]) ps
		= ((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
		# newSelection	= if hasSelection
							(if (top <= frame.corner1.y)		//topLine
415 416
								[max 1 (lastSelection - linesOnPage)]
								[2 + (frame.corner1.y / lineHeight)]	//topOfPage
Diederik van Arkel's avatar
Diederik van Arkel committed
417
							)
418 419
							[1]
		# lbState		= {lbState & selection = newSelection}
420 421
		# (newLook,lbState)
						= customlook lbState
422
		# ps			= scroll_to_selection newSelection lineHeight customId ps
Diederik van Arkel's avatar
Diederik van Arkel committed
423 424 425 426 427 428 429 430 431 432
		# ps			= appPIO (setControlLooks [(customId,True,(True,newLook))]) ps
		= ((lbState,ls),ps)
	| key == pgDownKey
		// set selection page-down
		// if shift - extend selection one up
		// if control ...
		// if control-shift ...
		# bot = (inc lastSelection) * lineHeight
		# newSelection	= if hasSelection
							(if (bot >= frame.corner2.y)		//bottomLine
433 434
								[min nrItems (lastSelection + linesOnPage)]
								[frame.corner2.y / lineHeight]	//bottomOfPage
Diederik van Arkel's avatar
Diederik van Arkel committed
435
							)
436 437
							[nrItems]
		# lbState		= {lbState & selection = newSelection}
438 439
		# (newLook,lbState)
						= customlook lbState
440
		# ps			= scroll_to_selection newSelection lineHeight customId ps
Diederik van Arkel's avatar
Diederik van Arkel committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
		# ps			= appPIO (setControlLooks [(customId,True,(True,newLook))]) ps
		= ((lbState,ls),ps)
	= ((lbState,ls),ps)
where
	nrItems		= length items
	customId	= lbState.listboxId.fcontrolId
	hasSelection
		| isEmpty selection = False
		= True
	lastSelection = hd selection
keyboard _ _ _ = abort "FilteredListBox: unsupported keyboard action"

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

//	The mouse either sets, adds, or removes items to the selection:
mouse efun (MouseDown pos {shiftDown,controlDown} 1) ((listboxState=:{items,selection,lineHeight,initHeight},ls),ps)
	# listboxState	= {FilteredListBoxState | listboxState & selection=okSelection}
461 462
	# (newLook,listboxState)
					= customlook listboxState
Diederik van Arkel's avatar
Diederik van Arkel committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476
	# ps			= appPIO (setControlLooks [(customId,True,(True,newLook))]) ps
	= ((listboxState,ls),ps)
where
	nrItems		= length items
	hasSelection= not (isEmpty selection)
	[prevIndex:_]= selection
	listSelection
		| prevIndex < newIndex
			= [prevIndex..newIndex]
			= [newIndex..prevIndex]
	newIndex	= pos.y/lineHeight+1
	newSelection
		| shiftDown
			| hasSelection
477
				= removeMembers selection listSelection++listSelection
Diederik van Arkel's avatar
Diederik van Arkel committed
478 479 480
			= [newIndex]
		| controlDown
			| isMember newIndex selection
481 482
				= removeMember newIndex selection
			= selection++[newIndex]
Diederik van Arkel's avatar
Diederik van Arkel committed
483 484 485 486 487 488 489
		= [newIndex]
	okSelection	= filter (isBetween 1 nrItems) newSelection
	customId	= listboxState.listboxId.fcontrolId

mouse efun (MouseDown pos {shiftDown} _) ((listboxState=:{items,lineHeight,initHeight},ls),ps)
// double click
	# listboxState	= {FilteredListBoxState | listboxState & selection=okSelection}
490 491
	# (newLook,listboxState)
					= customlook listboxState
Diederik van Arkel's avatar
Diederik van Arkel committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505
	# ps			= appPIO (setControlLooks [(customId,True,(True,newLook))]) ps
	| newIndex > length listboxState.items
		= ((listboxState,ls),ps)
//	| shiftDown
//		# ps	= (shiftfuns!!(newIndex-1)) ps
//		= ((listboxState,ls),ps)
	# ps	= efun (items!!(newIndex-1)) ps
	= ((listboxState,ls),ps)
where
	customId	= listboxState.listboxId.fcontrolId
	newIndex	= pos.y/lineHeight+1
	nrItems		= length items
	newSelection= [newIndex]
	okSelection	= filter (isBetween 1 nrItems) newSelection
Diederik van Arkel's avatar
Diederik van Arkel committed
506
mouse _ _ s = s	//abort "FilteredListBox: unsupported mouse action"
Diederik van Arkel's avatar
Diederik van Arkel committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
		

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

getFilteredListBoxSelection :: !FilteredListBoxId !(PSt .l) -> (!(!Bool,![(String,!Index)]),!PSt .l)
getFilteredListBoxSelection {freceiverId} pState
	# ((_,maybe_out),pState)	= syncSend2 freceiverId FInGetSelection pState
	| isNothing maybe_out
		= ((False,[]),pState)
	# result					= case (fromJust maybe_out) of
									(FOutGetSelection selection)	-> (True,selection)
									_							-> (False,[])
	| otherwise
		= (result,pState)

setFilteredListBoxSelection :: !FilteredListBoxId ![Index] !(PSt .l) -> PSt .l
setFilteredListBoxSelection {freceiverId} selection pState
	= snd (syncSend2 freceiverId (FInSetSelection selection) pState)

getFilteredListBoxItems :: !FilteredListBoxId !(PSt .l) -> (!(!Bool,![String]),!PSt .l)
getFilteredListBoxItems {freceiverId} pState
	# ((_,maybe_out),pState)	= syncSend2 freceiverId FInGetItems pState
	| isNothing maybe_out
		= ((False,[]),pState)
	# result					= case (fromJust maybe_out) of
									(FOutGetItems items)	-> (True,items)
									_					-> (False,[])
	| otherwise
		= (result,pState)

appendFilteredListBoxItems :: !FilteredListBoxId ![FilteredListBoxItem] !(PSt .l) -> PSt .l
appendFilteredListBoxItems {freceiverId} items pState
	= snd (syncSend2 freceiverId (FInAppendItems items) pState)

setFilteredListBoxPen :: !FilteredListBoxId ![PenAttribute] !(PSt .l) -> PSt .l
setFilteredListBoxPen {freceiverId} pen ps
	= snd (syncSend2 freceiverId (FInSetPen pen) ps)

546
setFilter :: !FilteredListBoxId ([String]->[String]) !(PSt .l) -> PSt .l
Diederik van Arkel's avatar
Diederik van Arkel committed
547 548 549
setFilter {freceiverId} flt ps
	= snd (syncSend2 freceiverId (FInSetFilter flt) ps)

550
getFilter :: !FilteredListBoxId !(PSt .l) -> (![String]->[String],PSt .l)
Diederik van Arkel's avatar
Diederik van Arkel committed
551 552
getFilter {freceiverId} ps
	# ((_,out),ps) = (syncSend2 freceiverId (FInGetFilter) ps)
553
	| isNothing out = (id,ps)
Diederik van Arkel's avatar
Diederik van Arkel committed
554 555
	# out = case (fromJust out) of
				(FOutGetFilter filt)	-> filt
556
				_						-> id
Diederik van Arkel's avatar
Diederik van Arkel committed
557 558
	= (out,ps)

559
exec_next_filtered :: !Bool !FilteredListBoxId (String (PSt .l) -> (PSt .l)) !(PSt .l) -> (PSt .l)
Diederik van Arkel's avatar
Diederik van Arkel committed
560 561 562 563 564
exec_next_filtered next lbId efun ps
	#	((ok,sel),ps)	= getFilteredListBoxSelection lbId ps
	| not ok = ps
	#	((ok,lst),ps)	= getFilteredListBoxItems lbId ps
	| not ok =  ps
565 566 567 568 569 570
	# l = length lst
	| l == 0 = ps
	#	selected_line_numbers	= if (isEmpty sel)
									(lines_from 1 lst l)
									(next_selected_line_numbers (snd(hd sel)) lst l)
	#	ps				= setFilteredListBoxSelection lbId selected_line_numbers ps
Diederik van Arkel's avatar
Diederik van Arkel committed
571 572 573 574 575 576
	#	((ok,sel),ps)	= getFilteredListBoxSelection lbId ps
	| not ok = ps
	| isEmpty sel  = ps
	# path =  fst(hd sel)
	= efun path ps
where
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
	next_selected_line_numbers line_n lst l
		| next
			# line_n = inc line_n
			| line_n > l
				= lines_from 1 lst l
				= next_if_string_begins_with_space line_n lst l
			# line_n = dec line_n
			| line_n < 1
				= move_up_while_string_begins_with_space l lst l
				= next_if_string_begins_with_space line_n lst l

	next_if_string_begins_with_space line_n lst l
		# s=lst!!(line_n-1)
		| size s>0 && s.[0]==' '
			= next_selected_line_numbers line_n lst l
			= lines_from line_n lst l

	move_up_while_string_begins_with_space line_n lst l
		| line_n == 1
			= lines_from 1 lst l
		# s=lst!!(line_n-1)
		| size s>0 && s.[0]==' '
			= move_up_while_string_begins_with_space (dec line_n) lst l
			= lines_from line_n lst l

	lines_from line_n lst l
		# s=lst!!(line_n-1)
		| size s>0 && s.[0]==' '
			= [line_n]
			= [line_n:lines_beginning_with_space (line_n+1) lst l]

	lines_beginning_with_space line_n lst l
		| line_n>l
			= []
		# s=lst!!(line_n-1)
		| size s>0 && s.[0]==' '
			= [line_n:lines_beginning_with_space (line_n+1) lst l]
			= []
Diederik van Arkel's avatar
Diederik van Arkel committed
615 616 617 618 619 620

//	Auxiliary functions:

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