***********************************************************************
*
* busybox uevent.aii -- Version 3.0
*
* Copyright (c)
* Apple Computer, Inc.  1986-1990
* All Rights Reserved.
*
* This file contains the code which implements the main event loop
* used by the busybox program.
*
***********************************************************************

	case on
	longi on
	longa on

	INCLUDE 'e16.event'
	INCLUDE 'm16.menu'
	INCLUDE 'e16.window'
	INCLUDE 'm16.util2'
	INCLUDE 'm16.window'
	INCLUDE 'busybox.equ'

***********************************************************************
* This procedure is	called when an inControl message is returned
* by TaskMaster.
*
* When this routine	gets control, the ID of the control that was
* hit is in TaskDATA4.  The control handle is in TaskData2 and
* the part code is in taskData 3.
*
	EXPORT doControls
doControls	PROC
	import event, openThisWindow

	lda event+owmTaskData4 ;The ID
	cmp #ButButtonsID	;If theID is in range...
	bcc no
	cmp #Prog6ID+1
	bcs no
	pha
	jsr openThisWindow	;...open the window.

no	rts

	ENDP

*********************************************************************
*
* checkFrontW
*
* This routine checks the front window to see if any changes need
* to be made to the	menu items.
*
* We do this so that the edit items are	only active when a desk
* accessory is active.
*
	EXPORT checkFrontW
checkFrontW	PROC

	pha	;What is the front window?
	pha
	_FrontWindow
	PullLong theWindow

	lda theWindow	;If front window is	lastWindow, then
	cmp lastWindow	;everything is already ok.
	bne doit
	ldx theWindow+2
	cpx lastWindow+2
	beq okAlready

doit	ora theWindow+2	;If there are no windows, then
	bne atLeastOneWindow ;everything should	be disabled.
	pea $80
	pea EditMenuID
	_SetMenuFlag
	_DrawMenuBar
	jmp nowOk

atLeastOneWindow	pha
	pushlong theWindow
	_GetSysWFlag
	ldx #$FF7F	;Assume system window (desk-accessory).
	pla
	bne aa	;It is a system window.
	ldx #$80	;It is one of ours.
aa	phx
	pea EditMenuID
	_SetMenuFlag
	_DrawMenuBar

nowOk	lda theWindow
	sta lastWindow
	lda theWindow+2
	sta lastWindow+2

okAlready	rts

theWindow	DC.L 0

	EXPORT lastWindow
lastWindow		;       
	DC.L 0

	ENDP

*************************************************************************
*
* mainEvent
*
* This is the main part of the program.	The program cycles in this
* loop until the user choose select.
*
	EXPORT mainEvent
mainEvent	PROC
	import quitFlag, event, doCloseTop, doMenu

	stz quitFlag	;quitFlag flag will	be set by QuitItem.
	stz lastWindow	;Init this for checkFrontW
	stz lastWindow+2

loop	jsr checkFrontW

	pha	;Call TaskMaster
	pea $FFFF
	pushlong #event
	_TaskMaster
	pla

	cmp #wInGoAway
	bne aa
	jsr doCloseTop
	jmp zz

aa	cmp #wInSpecial
	beq a0
	cmp #wInMenuBar
	bne b

a0	jsr doMenu
	jmp zz

b	cmp #wInControl
	bne zz
	jsr doControls

zz	lda quitFlag
	beq loop

	rts

	ENDP

	END
