;  EXAMPLE.ASM written 09/15/86 by Martin Murray

********************************************************************************
*       THIS CODE IS IN NO WAY COPYRIGHT 1986 BY INOVATRONICS, INC.  IN FACT,  *
*       YOU CAN DO ANYTHING WITH IT THAT YOU WANT TO DO.  JUST REMEMBER,       *
*       INOVATRONICS, INC. WILL BEAR ABSOLUTELY NO RESPONSIBILITY FOR THE USE, *
*       MISUSE, INABILITY TO USE OR INABLITY TO UNDERSTAND ANY OR ALL PARTS OF *
*       THIS CODE.  ENJOY IT IN GOOD HEALTH.                                   *
********************************************************************************
********************************************************************************
*       THE PURPOSE OF THE CODE IS TO LET YOU SEE WHAT YOUR PowerWindows       *
*       GENERATED SOURCE CODE WILL LOOK LIKE IN A PROGRAM.  IN MOST CASES, ALL *
*       YOU SHOULD HAVE TO DO IS ASSEMBLE THIS FILE.  IT WILL  AUTOMATICALLY   *
*       INCLUDE YOUR SOURCE FILE, PROVIDED IT IS IN THE DEFAULT DIRECTORY, AND *
*       IS NAMED "example.i".  JUST ASSEMBLE IT, LINK IT AND RUN IT.  IT       *
*       DEFAULTS TO TERMINATING WHEN THE CLOSE GADGET IS HIT, BUT IF YOU LOOK  *
*       BELOW YOU'LL SEE HOW TO MAKE IT TERMINATE ON ANY EVENT AT ALL.         *
********************************************************************************

	include "exec/types.i"
	include "exec/io.i"
	include "exec/strings.i"
	include "libraries/dosextens.i"
	include "intuition/intuition.i"

	xref	_AbsExecBase

************	EQUATES
NULL	equ	0

************	MACROS
xlib	macro
	xref	_LVO\1
	endm

callsys	macro	(routine name to call, A6 must have base pointer of library)
	xlib	\1
	CALLLIB	_LVO\1
	endm

call	macro	(address to call)
	bsr	\1
	endm

push	macro	(long ea to push)
	move.l	\1,-(sp)
	endm

pull	macro	(long ea to fetch without popping)
	move.l	(sp),\1
	endm

pop	macro	(long ea to pop into)
	move.l	(sp)+,\1
	endm


*********    DATA AREA
	DATA
Intuitionname:	string <'intuition.library'>
InitialSP:	dc.l	0
MeMyselfandI:	dc.l	0
ReturnMsg:	dc.l	0
_IntuitionBase:	dc.l	0
CurrentWindow:	dc.l	0
	include "example.i"		;include the PowerWindows code


************	PROGRAM START
	CODE
;Intitialize by saving the stack pointer and finding our own task.
	move.l	sp,InitialSP		;save the stack pointer
	move.l	_AbsExecBase,a6		;get the system library address
	sub.l	a1,a1
	callsys	FindTask		;please, I must find myself
	move.l	d0,MeMyselfandI		;save the address of the TCB list node
	move.l	d0,a0			;  in case we need it for some reason
	tst.l	PR_CLI(a0)		;were we run by Workbench?
	bne	1$			;no-jump
	lea	PR_MSGPORT(a0),a0	;wait for the startup message
	push	a0
	callsys	WaitPort
	pop	a0			;get it
	callsys	GetMsg
	move.l	d0,ReturnMsg		;save the message
1$:
	moveq	#0,d0			;open intuition.library
	lea	Intuitionname,a1
	callsys	OpenLibrary
	move.l	d0,_IntuitionBase	;save her address
	beq	FatalExit		;quit NOW if no Intuition
	move.l	d0,a6			;A6 gets the pointer until we call Exec

main:
;do the opening of the window, et all.
	lea	NewWindowStructure,a0	;point to the NewWindow structure and
	callsys	OpenWindow		;  open the window (gadgets will come up
	move.l	d0,CurrentWindow	;  too.)
	beq	Exit			;leave if couldn't open window

;attach the menu if present
	ifd MenuList
	move.l	d0,a0			;window address to A0
	lea	MenuList,a1		;menustrip pointed to by A1
	callsys	SetMenuStrip
	endc

;draw the IntuiText only if there is some
	ifd IntuiTextList
	move.l	CurrentWindow,a0	;get the RastPort address
	move.l	WD_RPORT(a0),a0
	lea	IntuiTextList,a1	;print this list of IText structures
	moveq	#0,d0			;let the text position itself
	move.l	d0,d1
	callsys	PrintIText
	endc

;draw the Borders only if there are some of them
	ifd BorderList
	move.l	CurrentWindow,a0	;get the RastPort address
	move.l	WD_RPORT(a0),a0
	lea	BorderList,a1		;draw this list of Border structures
	moveq	#0,d0			;let the borders position themselves
	move.l	d0,d1
	callsys	DrawBorder
	endc

;now wait for the event we should terminate at.
	move.l	_AbsExecBase,a6		;point to the system again
loop:
	move.l	CurrentWindow,a0	;get the IDCMP port address and Wait on
	move.l	WD_USERPORT(a0),a0	;  it
	push	a0			;save the port address
	callsys	WaitPort
	pop	a0			;get the message
	callsys	GetMsg
	move.l	d0,a1			;message address to A1
	move.l	IM_CLASS(a1),d4		;save the event class in D4
	callsys	ReplyMsg		;reply the message
;termination test -- change it to any other IDCMP event, just be sure that your
;  IDCMP flags are set properly
	cmp.l	#CLOSEWINDOW,d4		;should we terminate now?
	bne	loop			;loop if not

	move.l	_IntuitionBase,a6	;be sure to do next through Intuition

;detach the menu if present
	ifd MenuList
	move.l	CurrentWindow,a0	;window address to A0
	callsys	ClearMenuStrip
	endc

	move.l	CurrentWindow,a0	;close the window
	callsys	CloseWindow

************	PROGRAM EXIT
Exit:
	move.l	_AbsExecBase,a6
	move.l	_IntuitionBase,a1	;close Intuition
	callsys	CloseLibrary
FatalExit:
	move.l	ReturnMsg,d0		;reply to any WorkBench message
	beq	1$			;jump if none
	move.l	a1,d0			;point with this here register
	callsys	ReplyMsg		;Reply the message
1$:
	moveq	#0,d0			;no error code returned
	move.l	InitialSP,sp		;return to the caller
	rts

	end
