menudevice.icl 23.9 KB
Newer Older
Peter Achten's avatar
Peter Achten committed
1
2
3
4
5
6
7
8
implementation module menudevice


//	Clean object I/O library, version 1.2


import	StdBool, StdEnum, StdList, StdMisc
import	menuevent, osmenu
9
from	ostypes				import OSNoWindowPtr
10
import	commondef, devicefunctions, iostate, menuaccess, menucreate, menudefaccess, receiveraccess, StdId
Peter Achten's avatar
Peter Achten committed
11
12
13
14
15
16
17
18
19
from	StdProcessAttribute	import getProcessToolbarAtt, isProcessToolbar
from	StdPSt				import accPIO


menudeviceFatalError :: String String -> .x
menudeviceFatalError rule error
	= FatalError rule "menudevice" error


20
MenuFunctions :: DeviceFunctions (PSt .l)
Peter Achten's avatar
Peter Achten committed
21
MenuFunctions
22
23
	= {	dDevice	= MenuDevice
	  ,	dShow	= menuShow
Peter Achten's avatar
Peter Achten committed
24
25
26
27
28
29
30
	  ,	dHide	= menuHide
	  ,	dEvent	= menuEvent
	  ,	dDoIO	= menuIO
	  ,	dOpen	= menuOpen
	  ,	dClose	= menuClose
	  }

31
menuShow :: !(PSt .l) -> PSt .l
32
/* PA: mOSMenuBar information is stored in IOSt:OSDInfo.
Peter Achten's avatar
Peter Achten committed
33
34
35
36
37
38
39
40
41
42
43
44
45
menuShow pState=:{io=ioState}
	# (activeIO,ioState)	= IOStIsActive ioState
	| not activeIO
		= {pState & io=ioState}
	| otherwise
		# (tb,ioState)		= getIOToolbox ioState
		# (menus,ioState)	= IOStGetDevice MenuDevice ioState
		  mHs				= MenuSystemStateGetMenuHandles menus
		# (menuBar, tb)		= OSMenuBarSet mHs.mOSMenuBar tb
		  mHs				= {mHs & mOSMenuBar = menuBar}
		# ioState			= setIOToolbox tb ioState
		# ioState			= IOStSetDevice (MenuSystemState mHs) ioState
		= {pState & io=ioState}
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
*/
menuShow pState=:{io=ioState}
	# (activeIO,ioState)		= IOStIsActive ioState
	| not activeIO
		= {pState & io=ioState}
	# (osdinfo,ioState)			= IOStGetOSDInfo ioState
	  maybeMenuBar				= getOSDInfoOSMenuBar osdinfo
	| isNothing maybeMenuBar
		= menudeviceFatalError "MenuFunctions.dShow" "OSMenuBar could not be retrieved from OSDInfo"
	| otherwise
		# osMenuBar				= fromJust maybeMenuBar
		# (osMenuBar,ioState)	= accIOToolbox (OSMenuBarSet osMenuBar) ioState
		  osdinfo				= setOSDInfoOSMenuBar osMenuBar osdinfo
		# ioState				= IOStSetOSDInfo osdinfo ioState
		= {pState & io=ioState}
Peter Achten's avatar
Peter Achten committed
61

62
menuClose :: !(PSt .l) -> PSt .l
Peter Achten's avatar
Peter Achten committed
63
menuClose pState=:{io=ioState}
64
65
	# (osdinfo,ioState)				= IOStGetOSDInfo ioState
	  maybeOSMenuBar				= getOSDInfoOSMenuBar osdinfo
66
67
	| isNothing maybeOSMenuBar
		= menudeviceFatalError "MenuFunctions.dClose" "OSMenuBar could not be retrieved from OSDInfo"
68
	# (found,mDevice,ioState)		= IOStGetDevice MenuDevice ioState
69
70
71
	| not found
		= menudeviceFatalError "MenuFunctions.dClose" "could not retrieve MenuSystemState from IOSt"
	| otherwise
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
		# osMenuBar					= fromJust maybeOSMenuBar
		# (opt_guishare,ioState)	= IOStGetGUIShare ioState
		  menus						= MenuSystemStateGetMenuHandles mDevice
		# (tb,ioState)				= getIOToolbox ioState
		# (menus,(osMenuBar,tb))	= disposeMenuHandles (isJust opt_guishare) menus (osMenuBar,tb)
		# ioState					= setIOToolbox tb ioState
		  osdinfo					= setOSDInfoOSMenuBar osMenuBar osdinfo
		# ioState					= IOStSetOSDInfo osdinfo ioState
		# (ioid,ioState)			= IOStGetIOId ioState
		# (rt,ioState)				= IOStGetReceiverTable ioState
		# (it,ioState)				= IOStGetIdTable ioState
		# (mHs,_)					= menuHandlesGetMenus menus
		  (_,(rt,it))				= StateMap (disposeIds ioid) mHs (rt,it)
		# ioState					= IOStSetIdTable it ioState
		# ioState					= IOStSetReceiverTable rt ioState
87
		# ioState					= IOStRemoveDevice MenuDevice ioState		// PA: This is actually redundant, since IOStGetDevice already removed it
88
		# ioState					= IOStRemoveDeviceFunctions MenuDevice ioState
89
		= {pState & io=ioState}
Peter Achten's avatar
Peter Achten committed
90
where
91
92
93
94
95
	disposeIds :: !SystemId !(MenuStateHandle .pst) !*(!*ReceiverTable,!*IdTable)
						 -> (!MenuStateHandle .pst, !*(!*ReceiverTable,!*IdTable))
	disposeIds ioid (MenuLSHandle menuH=:{mlsHandle=mH=:{mItems}}) ts
		# (itemHs,ts)	= StateMap (disposeMenuIds ioid) mItems ts
		= (MenuLSHandle {menuH & mlsHandle={mH & mItems=itemHs}},ts)
Peter Achten's avatar
Peter Achten committed
96

97
menuHide :: !(PSt .l) -> PSt .l
Peter Achten's avatar
Peter Achten committed
98
99
100
101
menuHide pState=:{io=ioState}
	# (activeIO,ioState)	= IOStIsActive ioState
	| not activeIO
		= {pState & io=ioState}
102
103
104
	# (found,menus,ioState)	= IOStGetDevice MenuDevice ioState
	| not found
		= {pState & io=ioState}
Peter Achten's avatar
Peter Achten committed
105
	| otherwise
106
		# mHs				= MenuSystemStateGetMenuHandles menus
Peter Achten's avatar
Peter Achten committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
		# (tb,ioState)		= getIOToolbox ioState
		# tb				= OSMenuBarClear tb
		# ioState			= setIOToolbox tb ioState
		# ioState			= IOStSetDevice (MenuSystemState mHs) ioState
		= {pState & io=ioState}


/*	Opening menus:
	Note:	all interactive processes have atleast the AppleMenu to identify the 
			process, and is used for checking whether the process is active
			(see IOStIsActive further down this module).
			If the process is a subprocess, then the ioguishare of its IOSt
			contains the list to the Mac toolbox menu system.
*/
121
menuOpen :: !(PSt .l) -> PSt .l
Peter Achten's avatar
Peter Achten committed
122
123
124
125
126
127
128
129
130
131
132
133
134
menuOpen pState=:{io=ioState}
	# (hasMenu,ioState)		= IOStHasDevice MenuDevice ioState
	| hasMenu
		= {pState & io=ioState}
	| otherwise
		# (di,     ioState)	= IOStGetDocumentInterface ioState
		# (popUpId,ioState)	= getPopUpId di ioState
		  bound				= case di of
								NDI -> Finite 0
								SDI -> Infinite
								MDI -> Infinite
		  mHs				= {	mMenus		= []
							  ,	mKeys		= []
135
			//PA---			  ,	mOSMenuBar	= OSMenuBarNew OSNoWindowPtr OSNoWindowPtr (-1) // PA: menubar will be initialised by the (M/S)DI window creation
Peter Achten's avatar
Peter Achten committed
136
137
138
139
140
							  ,	mEnabled	= SystemAble
							  ,	mNrMenuBound= bound
							  ,	mPopUpId	= popUpId
							  }
		# ioState			= IOStSetDevice (MenuSystemState mHs) ioState
141
		# ioState			= IOStSetDeviceFunctions MenuFunctions ioState
142
		# ioState			= appIOToolbox OSinitialiseMenus ioState
Peter Achten's avatar
Peter Achten committed
143
144
		= {pState & io=ioState}
where
145
	getPopUpId :: !DocumentInterface !(IOSt .l) -> (!Maybe Id,!IOSt .l)
Peter Achten's avatar
Peter Achten committed
146
147
148
149
150
151
152
	getPopUpId NDI ioState
		= (Nothing,ioState)
	getPopUpId _ ioState
		# (id,ioState)	= openId ioState
		= (Just id,ioState)


153
menuIO :: !DeviceEvent !(PSt .l) -> (!DeviceEvent,!PSt .l)
154
155
156
157
158
159
menuIO deviceEvent pState
	# (ok,pState)	= accPIO (IOStHasDevice MenuDevice) pState
	| not ok		// This condition should never occur
		= menudeviceFatalError "MenuFunctions.dDoIO" "could not retrieve MenuSystemState from IOSt"
	| otherwise
		= menuIO deviceEvent pState
Peter Achten's avatar
Peter Achten committed
160
where
161
	menuIO :: !DeviceEvent !(PSt .l) -> (!DeviceEvent,!PSt .l)
Peter Achten's avatar
Peter Achten committed
162
	
163
164
	menuIO receiverEvent=:(ReceiverEvent msgEvent) pState
		= (ReceiverEvent msgEvent1,pState2)
Peter Achten's avatar
Peter Achten committed
165
	where
166
167
168
169
170
171
		(_,mDevice,ioState)			= IOStGetDevice MenuDevice pState.io
		menus						= MenuSystemStateGetMenuHandles mDevice
		ioState1					= IOStSetDevice (MenuSystemState menus1) ioState
		pState1						= {pState & io=ioState1}
		(msgEvent1,menus1,pState2)	= menuMsgIO msgEvent menus pState1
		
172
		menuMsgIO :: !MsgEvent !(MenuHandles (PSt .l)) (PSt .l) -> (!MsgEvent,!MenuHandles (PSt .l),PSt .l)
173
174
175
176
		menuMsgIO msgEvent menus=:{mMenus=mHs} pState
			# (msgEvent,mHs,pState)	= menusMsgIO (getMsgEventRecLoc msgEvent).rlParentId msgEvent mHs pState
			= (msgEvent,{menus & mMenus=mHs},pState)
		where
177
178
			menusMsgIO :: !Id !MsgEvent ![MenuStateHandle (PSt .l)] (PSt .l)
						  -> (!MsgEvent,![MenuStateHandle (PSt .l)], PSt .l)
179
			menusMsgIO menuId msgEvent msHs pState
180
181
				# (empty,msHs)				= u_isEmpty msHs
				| empty
182
183
184
185
186
187
188
189
190
					= menudeviceFatalError "menuIO (ReceiverEvent _) _" "menu could not be found"
				# (msH,msHs)				= HdTl msHs
				  (id,msH)					= menuStateHandleGetMenuId msH
				| id==menuId
					# (msgEvent,msH,pState)	= menuStateMsgIO msgEvent msH pState
					= (msgEvent,[msH:msHs],pState)
				| otherwise
					# (msgEvent,msHs,pState)= menusMsgIO menuId msgEvent msHs pState
					= (msgEvent,[msH:msHs],pState)
Peter Achten's avatar
Peter Achten committed
191
	
192
193
	menuIO deviceEvent=:(MenuTraceEvent info) pState
		= (deviceEvent,pState2)
Peter Achten's avatar
Peter Achten committed
194
	where
195
196
197
198
199
200
		(_,mDevice,ioState)	= IOStGetDevice MenuDevice pState.io
		menus				= MenuSystemStateGetMenuHandles mDevice
		ioState1			= IOStSetDevice (MenuSystemState menus1) ioState
		pState1				= {pState & io=ioState1}
		(menus1,pState2)	= menuTraceIO info menus pState1
		
201
		menuTraceIO :: !MenuTraceInfo !(MenuHandles (PSt .l)) (PSt .l) -> (!MenuHandles (PSt .l),PSt .l)
202
203
204
205
		menuTraceIO info=:{mtId} menus=:{mMenus=mHs} pState
			# (mHs,pState)	= menusTraceIO mtId info mHs pState
			= ({menus & mMenus=mHs},pState)
		where
206
			menusTraceIO :: !Id !MenuTraceInfo ![MenuStateHandle (PSt .l)] (PSt .l) -> (![MenuStateHandle (PSt .l)],PSt .l)
207
			menusTraceIO menuId info msHs pState
208
209
				# (empty,msHs)		= u_isEmpty msHs
				| empty
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
					= menudeviceFatalError "menuIO (MenuTraceEvent _) _" "menu could not be found"
				# (msH,msHs)		= HdTl msHs
				  (id, msH)			= menuStateHandleGetMenuId msH
				| id==menuId
					# (msH,pState)	= menuStateTraceIO info msH pState
					= ([msH:msHs],pState)
				| otherwise
					# (msHs,pState)	= menusTraceIO menuId info msHs pState
					= ([msH:msHs],pState)
	
	menuIO deviceEvent=:(ToolbarSelection {tbsItemNr}) pState
		# (atts,pState)			= accPIO IOStGetProcessAttributes pState
		  (hasToolbarAtt,att)	= Select isProcessToolbar undef atts
		| not hasToolbarAtt
			= (deviceEvent,pState)
		| otherwise
			# toolbarItems		= getProcessToolbarAtt att
			  f					= gettoolbarfunction tbsItemNr toolbarItems
			= (deviceEvent,f pState)
Peter Achten's avatar
Peter Achten committed
229
	where
230
231
232
233
234
235
236
237
238
239
240
241
242
		gettoolbarfunction :: !Int ![ToolbarItem .pst] -> IdFun .pst
		gettoolbarfunction i [item:items]
			| i==1 && isItem	= f
			| otherwise			= gettoolbarfunction i` items
		where
			(isItem,i`,f)		= case item of
					  				ToolbarItem _ _ f	-> (True,i-1,f)
					  				ToolbarSeparator	-> (False,i,undef)
		gettoolbarfunction _ []
			= menudeviceFatalError "menuIO (ToolbarSelection)" "toolbar index out of range"
	
	menuIO _ _
		= menudeviceFatalError "menuIO" "unexpected DeviceEvent"
Peter Achten's avatar
Peter Achten committed
243
244
245
246


/*	Apply the Menu(Mods)Function of a selected menu item.
*/
247
menuStateTraceIO :: !MenuTraceInfo !(MenuStateHandle (PSt .l)) (PSt .l) -> (!MenuStateHandle (PSt .l),PSt .l)
Peter Achten's avatar
Peter Achten committed
248
249
250
251
252
menuStateTraceIO info=:{mtParents} (MenuLSHandle {mlsState=ls,mlsHandle=mH=:{mItems}}) pState
	# (mItems,(ls,pState)) = subMenusTraceIO info mtParents mItems (ls,pState)
	= (MenuLSHandle {mlsState=ls,mlsHandle={mH & mItems=mItems}},pState)
where
//	subMenusTraceIO finds the final submenu that contains the selected menu item and then applies its Menu(Mods)Function.
253
254
255
256
257
258
259
260
	subMenusTraceIO :: !MenuTraceInfo ![Int] ![MenuElementHandle .ls .pst] !*(.ls,.pst)
										 -> (![MenuElementHandle .ls .pst], *(.ls,.pst))
	subMenusTraceIO info [] itemHs (ls,pst)
		# (_,itemHs,(ls,pst))	= menuElementsTraceIO info.mtItemNr info 0 itemHs (ls,pst)
		= (itemHs,(ls,pst))
	subMenusTraceIO info [subIndex:subIndices] itemHs (ls,pst)
		# (_,itemHs,(ls,pst))	= subMenuTraceIO subIndex info subIndices 0 itemHs (ls,pst)
		= (itemHs,(ls,pst))
Peter Achten's avatar
Peter Achten committed
261
	where
262
263
264
265
		subMenuTraceIO :: !Int !MenuTraceInfo ![Int] !Int ![MenuElementHandle .ls .pst] !*(.ls,.pst)
												 -> (!Int,![MenuElementHandle .ls .pst], *(.ls,.pst))
		subMenuTraceIO parentIndex info parentsIndex zIndex [itemH:itemHs] (ls,pst)
			# (zIndex,itemH,(ls,pst))	= subMenuTraceIO` parentIndex info parentsIndex zIndex itemH (ls,pst)
Peter Achten's avatar
Peter Achten committed
266
			| parentIndex<zIndex
267
				= (zIndex,[itemH:itemHs],(ls,pst))
Peter Achten's avatar
Peter Achten committed
268
			| otherwise
269
270
				# (zIndex,itemHs,(ls,pst))	= subMenuTraceIO parentIndex info parentsIndex zIndex itemHs (ls,pst)
				= (zIndex,[itemH:itemHs],(ls,pst))
Peter Achten's avatar
Peter Achten committed
271
		where
272
273
274
			subMenuTraceIO` :: !Int !MenuTraceInfo ![Int] !Int !(MenuElementHandle .ls .pst) !*(.ls,.pst)
													  -> (!Int, !MenuElementHandle .ls .pst,  *(.ls,.pst))
			subMenuTraceIO` parentIndex info parentsIndex zIndex itemH=:(SubMenuHandle subH=:{mSubItems}) (ls,pst)
Peter Achten's avatar
Peter Achten committed
275
				| parentIndex<>zIndex
276
					= (zIndex+1,itemH,(ls,pst))
Peter Achten's avatar
Peter Achten committed
277
				| otherwise
278
279
280
					# (itemHs,(ls,pst))= subMenusTraceIO info parentsIndex mSubItems (ls,pst)
					= (zIndex+1,SubMenuHandle {subH & mSubItems=itemHs},(ls,pst))
			subMenuTraceIO` parentIndex info parentsIndex zIndex (RadioMenuHandle itemH=:{mRadioItems}) (ls,pst)
Peter Achten's avatar
Peter Achten committed
281
				# (nrRadios,itemHs)	= Ulength mRadioItems
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
				= (zIndex+nrRadios,RadioMenuHandle {itemH & mRadioItems=itemHs},(ls,pst))
			subMenuTraceIO` parentIndex info parentsIndex zIndex itemH=:(MenuItemHandle _) (ls,pst)
				= (zIndex+1,itemH,(ls,pst))
			subMenuTraceIO` parentIndex info parentsIndex zIndex (MenuListLSHandle itemHs) (ls,pst)
				# (zIndex,itemHs,(ls,pst))		= subMenuTraceIO parentIndex info parentsIndex zIndex itemHs (ls,pst)
				= (zIndex,MenuListLSHandle itemHs,(ls,pst))
			subMenuTraceIO` parentIndex info parentsIndex zIndex (MenuExtendLSHandle {mExtendLS=ls1,mExtendItems=itemHs}) (ls,pst)
				# (zIndex,itemHs,((ls1,ls),pst))= subMenuTraceIO parentIndex info parentsIndex zIndex itemHs ((ls1,ls),pst)
				= (zIndex,MenuExtendLSHandle {mExtendLS=ls1,mExtendItems=itemHs},(ls,pst))
			subMenuTraceIO` parentIndex info parentsIndex zIndex (MenuChangeLSHandle {mChangeLS=ls1,mChangeItems=itemHs}) (ls,pst)
				# (zIndex,itemHs,(ls1,pst))		= subMenuTraceIO parentIndex info parentsIndex zIndex itemHs (ls1,pst)
				= (zIndex,MenuChangeLSHandle {mChangeLS=ls1,mChangeItems=itemHs},(ls,pst))
			subMenuTraceIO` _ _ _ zIndex itemH (ls,pst)
				= (zIndex,itemH,(ls,pst))
		subMenuTraceIO _ _ _ zIndex itemHs (ls,pst)
			= (zIndex,itemHs,(ls,pst))
Peter Achten's avatar
Peter Achten committed
298
299
	
//	menuElementsTraceIO applies the Menu(Mods)Function of the menu item at index itemIndex to the context state.
300
301
	menuElementsTraceIO :: !Int !MenuTraceInfo !Int ![MenuElementHandle .ls .pst] !*(.ls,.pst)
										   -> (!Int,![MenuElementHandle .ls .pst], *(.ls,.pst))
Peter Achten's avatar
Peter Achten committed
302
303
304
305
306
307
308
309
	menuElementsTraceIO itemIndex info zIndex [itemH:itemHs] ls_ps
		# (zIndex,itemH,ls_ps)		= menuElementTraceIO itemIndex info zIndex itemH ls_ps
		| itemIndex<zIndex
			= (zIndex,[itemH:itemHs],ls_ps)
		| otherwise
			# (zIndex,itemHs,ls_ps)	= menuElementsTraceIO itemIndex info zIndex itemHs ls_ps
			= (zIndex,[itemH:itemHs],ls_ps)
	where
310
311
		menuElementTraceIO :: !Int !MenuTraceInfo !Int !(MenuElementHandle .ls .pst) !*(.ls,.pst)
											  -> (!Int, !MenuElementHandle .ls .pst,  *(.ls,.pst))
312
313
314
		menuElementTraceIO itemIndex info zIndex (MenuItemHandle itemH=:{mItemAtts}) (ls,ps)
			| itemIndex<>zIndex || not hasFun	= (zIndex+1,MenuItemHandle itemH,  (ls,ps))
			| otherwise							= (zIndex+1,MenuItemHandle itemH,f (ls,ps))
Peter Achten's avatar
Peter Achten committed
315
316
317
318
319
		where
			(hasFun,fAtt)	= Select isEitherFun undef mItemAtts
			f				= if (isMenuFunction fAtt)	(getMenuFun fAtt)
														(getMenuModsFun fAtt info.mtModifiers)
			isEitherFun f	= isMenuFunction f || isMenuModsFunction f
320
		menuElementTraceIO itemIndex info zIndex (RadioMenuHandle radioH=:{mRadioIndex,mRadioItems}) (ls,pst)
Peter Achten's avatar
Peter Achten committed
321
322
323
			# (nrRadios,itemHs)		= Ulength mRadioItems
			| itemIndex>zIndex+nrRadios
				// Selected item is not one of these radio items
324
				= (zIndex+nrRadios,RadioMenuHandle {radioH & mRadioItems=itemHs},(ls,pst))
Peter Achten's avatar
Peter Achten committed
325
			| otherwise
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
				# (_,itemHs,(ls,pst))	= menuElementsTraceIO itemIndex info zIndex itemHs (ls,pst)
				= (zIndex+nrRadios,RadioMenuHandle {radioH & mRadioItems=itemHs},(ls,pst))		// It is assumed that the mRadioIndex is correctly set by the menu EventFunction
		menuElementTraceIO itemIndex info zIndex itemH=:(SubMenuHandle _) (ls,pst)
			= (zIndex+1,itemH,(ls,pst))
		menuElementTraceIO itemIndex info zIndex (MenuListLSHandle itemHs) (ls,pst)
			# (zIndex,itemHs,(ls,pst)) = menuElementsTraceIO itemIndex info zIndex itemHs (ls,pst)
			= (zIndex,MenuListLSHandle itemHs,(ls,pst))
		menuElementTraceIO itemIndex info zIndex (MenuExtendLSHandle {mExtendLS=ls1,mExtendItems=itemHs}) (ls,pst)
			# (zIndex,itemHs,((ls1,ls),pst)) = menuElementsTraceIO itemIndex info zIndex itemHs ((ls1,ls),pst)
			= (zIndex,MenuExtendLSHandle {mExtendLS=ls1,mExtendItems=itemHs},(ls,pst))
		menuElementTraceIO itemIndex info zIndex (MenuChangeLSHandle {mChangeLS=ls1,mChangeItems=itemHs}) (ls,pst)
			# (zIndex,itemHs,(ls1,pst)) = menuElementsTraceIO itemIndex info zIndex itemHs (ls1,pst)
			= (zIndex,MenuChangeLSHandle {mChangeLS=ls1,mChangeItems=itemHs},(ls,pst))
		menuElementTraceIO _ _ zIndex itemH (ls,pst)
			= (zIndex,itemH,(ls,pst))
	menuElementsTraceIO _ _ zIndex itemHs (ls,pst)
		= (zIndex,itemHs,(ls,pst))
Peter Achten's avatar
Peter Achten committed
343
344
345
346


/*	menuStateMsgIO handles all message events.
*/
347
menuStateMsgIO :: !MsgEvent !(MenuStateHandle (PSt .l)) (PSt .l) -> (!MsgEvent,!MenuStateHandle (PSt .l),PSt .l)
Peter Achten's avatar
Peter Achten committed
348
349
350
351
352
353
354
355
356
357
358
359
menuStateMsgIO msgEvent msH=:(MenuLSHandle mlsH=:{mlsState=ls,mlsHandle=mH}) pState
	= (msgEvent1,MenuLSHandle {mlsH & mlsState=ls1,mlsHandle=mH1},pState1)
where
	recLoc							= getMsgEventRecLoc msgEvent
	rId								= recLoc.rlReceiverId
	action							= case msgEvent of
										(QASyncMessage msg)	-> menuQASyncIO rId msg
										( ASyncMessage msg) -> menuASyncIO  rId msg
										(  SyncMessage msg) -> menuSyncIO   rId msg
	(msgEvent1,mH1,(ls1,pState1))	= action mH (ls,pState)

	//	menuQASyncIO queues an asynchronous message in the message queue of the indicated receiver menu element.
360
361
362
	menuQASyncIO :: !Id !QASyncMessage !(MenuHandle .ls .pst) *(.ls,.pst) -> (!MsgEvent,!MenuHandle .ls .pst,*(.ls,.pst))
	menuQASyncIO rId msg mH=:{mItems} (ls,pst)
		= (QASyncMessage msg,{mH & mItems=itemHs},(ls,pst))
Peter Achten's avatar
Peter Achten committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
	where
		(_,itemHs)	= elementsQASyncIO rId msg.qasmMsg mItems
		
		elementsQASyncIO :: !Id !SemiDynamic ![MenuElementHandle .ls .pst] -> (!Bool,![MenuElementHandle .ls .pst])
		elementsQASyncIO rId msg [itemH:itemHs]
			# (done,itemH)		= elementQASyncIO rId msg itemH
			| done
				= (done,[itemH:itemHs])
			| otherwise
				# (done,itemHs)	= elementsQASyncIO rId msg itemHs
				= (done,[itemH:itemHs])
		where
			elementQASyncIO :: !Id !SemiDynamic !(MenuElementHandle .ls .pst) -> (!Bool,!MenuElementHandle .ls .pst)
			elementQASyncIO rId msg mrH=:(MenuReceiverHandle itemH=:{mReceiverHandle=rH})
				| rId<>rH.rId	= (False,mrH)
				| otherwise		= (True,MenuReceiverHandle {itemH & mReceiverHandle=receiverAddASyncMessage rId msg rH})
			elementQASyncIO rId msg (SubMenuHandle itemH=:{mSubItems=itemHs})
				# (done,itemHs)	= elementsQASyncIO rId msg itemHs
				= (done,SubMenuHandle {itemH & mSubItems=itemHs})
			elementQASyncIO rId msg (MenuListLSHandle itemHs)
				# (done,itemHs)	= elementsQASyncIO rId msg itemHs
				= (done,MenuListLSHandle itemHs)
			elementQASyncIO rId msg (MenuExtendLSHandle	mExH=:{mExtendItems=itemHs})
				# (done,itemHs)	= elementsQASyncIO rId msg itemHs
				= (done,MenuExtendLSHandle {mExH & mExtendItems=itemHs})
			elementQASyncIO rId msg (MenuChangeLSHandle	mChH=:{mChangeItems=itemHs})
				# (done,itemHs)	= elementsQASyncIO rId msg itemHs
				= (done,MenuChangeLSHandle {mChH & mChangeItems=itemHs})
			elementQASyncIO rId msg itemH
				= (False,itemH)
		elementsQASyncIO _ _ _
			= (False,[])

	//	menuASyncIO handles the first asynchronous message in the message queue of the indicated receiver menu element.
397
	menuASyncIO :: !Id !ASyncMessage !(MenuHandle .ls .pst) *(.ls,.pst) -> (!MsgEvent,!MenuHandle .ls .pst,*(.ls,.pst))
Peter Achten's avatar
Peter Achten committed
398
399
400
401
402
	menuASyncIO rId msg mH=:{mItems} ls_ps
		= (ASyncMessage msg,{mH & mItems=itemHs},ls_ps1)
	where
		(_,itemHs,ls_ps1)	= elementsASyncIO rId mItems ls_ps
		
403
		elementsASyncIO :: !Id ![MenuElementHandle .ls .pst] *(.ls,.pst) -> (!Bool,![MenuElementHandle .ls .pst],*(.ls,.pst))
Peter Achten's avatar
Peter Achten committed
404
405
406
407
408
409
410
411
		elementsASyncIO rId [itemH:itemHs] ls_ps
			# (done,itemH,ls_ps)		= elementASyncIO rId itemH ls_ps
			| done
				= (done,[itemH:itemHs],ls_ps)
			| otherwise
				# (done,itemHs,ls_ps)	= elementsASyncIO rId itemHs ls_ps
				= (done,[itemH:itemHs],ls_ps)
		where
412
413
			elementASyncIO :: !Id !(MenuElementHandle .ls .pst) *(.ls,.pst) -> (!Bool,!MenuElementHandle .ls .pst,*(.ls,.pst))
			elementASyncIO rId mrH=:(MenuReceiverHandle itemH=:{mReceiverHandle=rH}) (ls,pst)
Peter Achten's avatar
Peter Achten committed
414
				| rId<>rH.rId
415
					= (False,mrH,(ls,pst))
Peter Achten's avatar
Peter Achten committed
416
				| otherwise
417
418
					# (rH,(ls,pst))	= receiverASyncIO rH (ls,pst)
					= (True,MenuReceiverHandle {itemH & mReceiverHandle=rH},(ls,pst))
Peter Achten's avatar
Peter Achten committed
419
			where
420
421
422
423
				receiverASyncIO :: !(ReceiverHandle .ls .pst) *(.ls,.pst) -> (!ReceiverHandle .ls .pst,*(.ls,.pst))
				receiverASyncIO rH=:{rASMQ=[msg:msgs],rFun} (ls,pst)
					# (ls,_,pst)	= rFun msg (ls,pst)
					= ({rH & rASMQ=msgs},(ls,pst))
Peter Achten's avatar
Peter Achten committed
424
425
				receiverASyncIO _ _
					= menudeviceFatalError "receiverASyncIO" "unexpected empty asynchronous message queue"
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
			elementASyncIO rId (SubMenuHandle itemH=:{mSubItems=itemHs}) (ls,pst)
				# (done,itemHs,(ls,pst))	= elementsASyncIO rId itemHs (ls,pst)
				= (done,SubMenuHandle {itemH & mSubItems=itemHs},(ls,pst))
			elementASyncIO rId (MenuListLSHandle itemHs) (ls,pst)
				# (done,itemHs,(ls,pst))	= elementsASyncIO rId itemHs (ls,pst)
				= (done,MenuListLSHandle itemHs,(ls,pst))
			elementASyncIO rId (MenuExtendLSHandle {mExtendLS=ls1,mExtendItems=itemHs}) (ls,pst)
				# (done,itemHs,((ls1,ls),pst))	= elementsASyncIO rId itemHs ((ls1,ls),pst)
				= (done,MenuExtendLSHandle {mExtendLS=ls1,mExtendItems=itemHs},(ls,pst))
			elementASyncIO rId (MenuChangeLSHandle {mChangeLS=ls1,mChangeItems=itemHs}) (ls,pst)
				# (done,itemHs,(ls1,pst))	= elementsASyncIO rId itemHs (ls1,pst)
				= (done,MenuChangeLSHandle {mChangeLS=ls1,mChangeItems=itemHs},(ls,pst))
			elementASyncIO _ itemH (ls,pst)
				= (False,itemH,(ls,pst))
		elementsASyncIO _ _ (ls,pst)
			= (False,[],(ls,pst))
Peter Achten's avatar
Peter Achten committed
442
443

	//	menuSyncIO lets the indicated receiver control handle the synchronous message.
444
445
	menuSyncIO :: !Id !SyncMessage !(MenuHandle .ls .pst) *(.ls,.pst) -> (!MsgEvent,MenuHandle .ls .pst,*(.ls,.pst))
	menuSyncIO r2Id msg mH=:{mItems} (ls,pst)
Peter Achten's avatar
Peter Achten committed
446
447
		= (SyncMessage msg1,{mH & mItems=itemHs},ls_ps1)
	where
448
		(_,msg1,itemHs,ls_ps1)	= elementsSyncIO r2Id msg mItems (ls,pst)
Peter Achten's avatar
Peter Achten committed
449
		
450
451
		elementsSyncIO :: !Id !SyncMessage ![MenuElementHandle .ls .pst] *(.ls,.pst)
					-> (!Bool,!SyncMessage, [MenuElementHandle .ls .pst],*(.ls,.pst))
Peter Achten's avatar
Peter Achten committed
452
453
454
455
456
457
458
459
		elementsSyncIO r2Id msg [itemH:itemHs] ls_ps
			# (done,msg,itemH,ls_ps)		= elementSyncIO r2Id msg itemH ls_ps
			| done
				= (done,msg,[itemH:itemHs],ls_ps)
			| otherwise
				# (done,msg,itemHs,ls_ps)	= elementsSyncIO r2Id msg itemHs ls_ps
				= (done,msg,[itemH:itemHs],ls_ps)
		where
460
461
			elementSyncIO :: !Id !SyncMessage !(MenuElementHandle .ls .pst) *(.ls,.pst)
					   -> (!Bool,!SyncMessage,  MenuElementHandle .ls .pst, *(.ls,.pst))
Peter Achten's avatar
Peter Achten committed
462
463
464
465
466
467
468
			elementSyncIO rId msg mrH=:(MenuReceiverHandle itemH=:{mReceiverHandle=rH}) ls_ps
				| rId<>rH.rId
					= (False,msg,mrH,ls_ps)
				| otherwise
					# (msg,rH,ls_ps)= receiverSyncIO msg rH ls_ps
					= (True,msg,MenuReceiverHandle {itemH & mReceiverHandle=rH},ls_ps)
			where
469
470
				receiverSyncIO :: !SyncMessage !(ReceiverHandle .ls .pst) *(.ls,.pst)
							  -> (!SyncMessage,  ReceiverHandle .ls .pst, *(.ls,.pst))
Peter Achten's avatar
Peter Achten committed
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
				receiverSyncIO msg rH ls_ps
					# (response,rH,ls_ps)	= receiverHandleSyncMessage msg rH ls_ps
					= ({msg & smResp=response},rH,ls_ps)
			elementSyncIO rId msg (SubMenuHandle itemH=:{mSubItems=itemHs}) ls_ps
				# (done,msg,itemHs,ls_ps)	= elementsSyncIO rId msg itemHs ls_ps
				= (done,msg,SubMenuHandle {itemH & mSubItems=itemHs},ls_ps)
			elementSyncIO rId msg (MenuListLSHandle itemHs) ls_ps
				# (done,msg,itemHs,ls_ps)	= elementsSyncIO rId msg itemHs ls_ps
				= (done,msg,MenuListLSHandle itemHs,ls_ps)
			elementSyncIO rId msg (MenuExtendLSHandle {mExtendLS=ls1,mExtendItems=itemHs}) (ls,ps)
				# (done,msg,itemHs,((ls1,ls),ps))	= elementsSyncIO rId msg itemHs ((ls1,ls),ps)
				= (done,msg,MenuExtendLSHandle {mExtendLS=ls1,mExtendItems=itemHs},(ls,ps))
			elementSyncIO rId msg (MenuChangeLSHandle {mChangeLS=ls1,mChangeItems=itemHs}) (ls,ps)
				# (done,msg,itemHs,(ls1,ps))	= elementsSyncIO rId msg itemHs (ls1,ps)
				= (done,msg,MenuChangeLSHandle {mChangeLS=ls1,mChangeItems=itemHs},(ls,ps))
			elementSyncIO rId msg itemH ls_ps
				= (False,msg,itemH,ls_ps)
		elementsSyncIO _ msg _ ls_ps
			= (False,msg,[],ls_ps)


/*	Check if the interactive process is active:
	Note:	this test depends on the fact that every interactive process has an AppleMenu,
			with mac menu ID AppleMenuId. This is taken care of by the creation
			of the interactive process (see menuOpen above).
*/
497
IOStIsActive :: !(IOSt .l) -> (!Bool, !IOSt .l)
Peter Achten's avatar
Peter Achten committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
IOStIsActive ioState
/* RWS +++
	# (globalHandle,ioState)= accIOToolbox (GetMHandle AppleMenuId) ioState
	  (menus,ioState)		= IOStGetDevice MenuDevice ioState
	  (mHs,menuHs)			= MenuHandlesGetMenuStateHandles (MenuSystemStateGetMenuHandles menus)
	  (appleH,others)		= HdTl mHs
	  (apple,appleH1)		= menuStateHandleGetHandle appleH
	  ioState				= IOStSetDevice (MenuSystemState {menuHs & mMenus=[appleH1:others]}) ioState
	= (apple==globalHandle,ioState)
*/	= (True,ioState)


/*	Activate the interactive process:
*/
512
ActivateMenuSystem :: !(IOSt .l) -> IOSt .l
Peter Achten's avatar
Peter Achten committed
513
ActivateMenuSystem ioState
514
515
516
517
518
519
520
521
522
523
524
	# ioState					= SelectIOSt ioState
	# (osdinfo,ioState)			= IOStGetOSDInfo ioState
	  maybeOSMenuBar			= getOSDInfoOSMenuBar osdinfo
	| isNothing maybeOSMenuBar
		= ioState
	| otherwise
		# osMenuBar				= fromJust maybeOSMenuBar
		# (osMenuBar,ioState)	= accIOToolbox (OSMenuBarSet osMenuBar) ioState
		  osdinfo				= setOSDInfoOSMenuBar osMenuBar osdinfo
		# ioState				= IOStSetOSDInfo osdinfo ioState
		= ioState