********************************************************************
*          Program to demonstrate use of Screens Windows           *
*          Gadgets Menus etc to form a complete program            *
*          shell.Compiles with Devpac V2                           *       
*                          Part One                                *
*                                                                  *
*                 By S.Marshall for Newsflash UK                   *
*                                                                  *
********************************************************************

	INCDIR	 	"SYS:INCLUDE/"
	INCLUDE 	Intuition/Intuition.i
	INCLUDE 	Intuition/Intuition_lib.i
	INCLUDE		Graphics/Graphics_lib.i
	INCLUDE		Graphics/View.I
 	INCLUDE 	Exec/Exec_lib.i
	INCLUDE		Exec/memory.i
	INCLUDE		Exec/Libraries.i
	INCLUDE		Libraries/Dos.i
	INCLUDE		Libraries/Dos_lib.i
	INCLUDE		Libraries/Dosextens.i
	
NULL	EQU	0

CALLSYS    MACRO
	IFGT	NARG-1         
	FAIL	!!!           
	ENDC                 
	JSR	_LVO\1(A6)
	ENDM
		
;*****************************************

	sub.l		a1,a1			;clear a1
	CALLEXEC	FindTask		;find task - us
	move.l		d0,a4			;process in a4
	move.l		d0,OurProc		;save it for later

	tst.l		pr_CLI(a4)		;test if from CLI
	beq.s		Workbench		;branch if from workbench
	
	bra.s		end_startup		;and run the user prog

Workbench
	lea		pr_MsgPort(a4),a0	;tasks message port in a0
	CALLSYS		WaitPort		;wait for workbench message
	lea		pr_MsgPort(a4),a0	;tasks message port in a0
	CALLSYS		GetMsg			;get workbench message
	move.l		d0,returnMsg		;save it for later reply

end_startup
	bsr.s		_main			;call our program

	tst.l		returnMsg		;test if from workbench
	beq.s		exitToDOS		;if I was a CLI

	CALLEXEC	Forbid			;forbid multitasking
	move.l		returnMsg(pc),a1	;get workbench message
	CALLSYS		ReplyMsg		;reply workbench message

exitToDOS
	moveq		#0,d0			;flag no error
	rts					;Quit our program

_main	
	moveq		#0,d0			;clear d0 (any lib version)
  	lea		DOSname(pc),a1		;lib name ind a1
  	CALLEXEC	OpenLibrary		;try to open library
  	move.l		d0,_DOSBase		;store lib base
  	beq		Error			;cleanup and quit if fail
	  
  	moveq		#0,d0			;clear d0 (any lib version)
  	lea		Grafname(pc),a1		;lib name ind a1
  	CALLSYS		OpenLibrary		;try to open library
  	move.l		d0,_GfxBase		;store lib base
  	beq		GfxError		;cleanup and quit if fail
  	
  	moveq		#0,d0			;clear d0 (any lib version)
  	lea		Intname(pc),a1		;lib name ind a1
  	CALLSYS		OpenLibrary		;try to open library
  	move.l		d0,_IntuitionBase	;store lib base
  	beq		IntError		;cleanup and quit if fail
  	
  	lea 		NewScrn(pc),a0		;NewScreen struct in a0
  	CALLINT 	OpenScreen		;try to open screen
  	move.l 		d0,ScreenPtr		;store screen pointer 
  	beq 		CloseInt		;cleanup and quit if fail
	move.l		d0,a0			;screen pointer in a0
	lea		sc_ViewPort(a0),a0	;screens viewport in a0
	lea		Palette(pc),a1		;Palette address in a1	
	moveq		#8,d0			;number of colours in d0
	CALLGRAF	LoadRGB4		;set screen colours
  	
  	lea 		NewWdw(pc),a0		;NewWindow stuct in a0
  	CALLINT 	OpenWindow		;try to open window
  	move.l 		d0,WindowPtr		;save pointer to window	
  	beq 		ScreenClose		;cleanup and quit if fail

	move.l		OurProc(pc),a0		;process in a0
	move.l		pr_WindowPtr(a0),OldWdw	;store old pointer
	move.l		WindowPtr(pc),pr_WindowPtr(a0) ;change to our pointer
	
  	lea		ScreenImage(pc),a1	;image struct in a1
  	bsr		Imagedraw		;draw it

	move.l		#255,d7			;index for loop
Scroll
	CALLGRAF	WaitTOF			;use WaitTOF to slow down
	moveq		#0,d0			;not used - set to 0
	move.l		#-1,d1			;amount to move - 1 pixel up
	move.l		ScreenPtr(pc),a0	;pointer to screen to move
	CALLINT		MoveScreen		;move it
	dbra		d7,Scroll		;loop til finished

	move.l		WindowPtr(pc),d0	;window in d0
	bsr.s		Waitmouse		;wait for let mouse down
	
  	lea		AnalyzerImage(pc),a1	;image struct in a1
  	bsr		Imagedraw		;draw it

;------ Temp code to test redirection of requesters and wait for mouse
	
	move.l		WindowPtr(pc),d0	;window in d0
	bsr.s		Waitmouse		;wait for let mouse down
	
	move.l		#flname,d1
	move.l		#MODE_OLDFILE,d2
	CALLDOS		Open

;------ end of temp code

WindowClose  
  	move.l		OurProc(pc),a0		;process in a0
  	move.l		OldWdw(pc),pr_WindowPtr(a0) ;restore old pointer
  	
  	move.l 		WindowPtr(pc),a0	;window in a0
  	CALLINT 	CloseWindow		;close it
  
ScreenClose
  	move.l 		ScreenPtr(pc),a0	;screen in a0
  	CALLSYS 	CloseScreen		;close it

CloseInt:  
  	move.l 		_IntuitionBase(pc),a1	;intuition lib base in a1
  	CALLEXEC 	CloseLibrary		;close intuition

IntError
	move.l 		_GfxBase(pc),a1		;graphics lib base in a1
  	CALLEXEC 	CloseLibrary		;close graphics
  	
GfxError
	move.l 		_DOSBase(pc),a1		;DOS lib base in a1
  	CALLEXEC 	CloseLibrary		;close DOS
  
Error
	rts					;quit
	
********************************************************************
; Subroutine to wait (legally) for the left mouse button
; Waitmouse  (Window)   (no return value)
;		d0
  	
Waitmouse:
	movem.l		a5-a6,-(sp)		;store a5 and a6
	move.l 		d0,a5			;window address in d0
  	move.l 		wd_UserPort(a5),a5	;userport address in a5
.Loop
  	move.l 		a5,a0			;userport in a0
 	CALLEXEC 	WaitPort		;wait for message
  	move.l 		a5,a0			;userport in a0
  	CALLSYS		GetMsg			;get the message
  	tst 		d0			;test result
  	beq.s 		.Loop 			;try again if fail
  	move.l 		d0,a1			;message in a1
  	cmp.l 		#MOUSEBUTTONS,im_Class(a1) ;is it mousebuttons
	bne.s		.Reply			;if not reply and try again
	cmp.w		#SELECTDOWN,im_Code(a1)	;if yes is it Selectdown
	beq.s		.MBPressed		;if yes reply message and rts
.Reply	
  	CALLSYS		ReplyMsg		;reply to message
  	bra.s 		.Loop			;loop back always
.MBPressed
  	CALLSYS		ReplyMsg		;reply message
	movem.l		(sp)+,a5-a6		;restore a5 and a6
	rts					;return

; Subroutine to render an image into window

Imagedraw:
	move.l		a6,-(sp)	;save a6
	move.l 		WindowPtr(pc),a0 ;address of Window in a0
  	move.l 		wd_RPort(a0),a0	;Windows rastport in a0
  	moveq 		#0,D0		;no left offset
  	moveq 		#0,D1		;no top offset
  	CALLINT 	DrawImage	;draw the image
  	move.l		(sp)+,a6	;restore a6
  	rts

;===============================================================
; NewScreen and NewWindow structures
 
NewScrn 
  	dc.w 		0,256		;X and Y co-ords of top left
  	dc.w 		320,256 	;Size of screen
  	dc.w		4		;Depth (number of bitplanes) 
  	dc.b 		0,1		;colours to use for border etc
  	dc.w 		NULL		;Viewmodes
  	dc.w 		CUSTOMSCREEN	;we are using a custom screen (=15)
  	dc.l 		Font80		;make sure we get the correct font
  	dc.l 		ScreenTitle	;Pointer to screen title
  	dc.l 		NULL		;Gadgets (none yet)
  	dc.l 		NULL		;Custom bitmap (none)
 	EVEN
  
ScreenTitle
	dc.b 		"AmiTracker",0
  	EVEN
  	
Font80:	dc.l		FontName
	dc.w		8
	dc.b		0
	dc.b		0
	
FontName:
	dc.b		"topaz.font",0
	EVEN  	
    
NewWdw 
  	dc.w 		0,10
  	dc.w 		320,246
  	dc.b 		-1,-1
  	dc.l 		GADGETDOWN|GADGETUP|MOUSEBUTTONS|RAWKEY|MENUPICK
  	dc.l 		SMART_REFRESH|BACKDROP|BORDERLESS|ACTIVATE
  	dc.l 		NULL
  	dc.l 		NULL
  	dc.l 		NULL
ScreenPtr
  	dc.l 		NULL
  	dc.l 		NULL
  	dc.w 		NULL,NULL
  	dc.w 		NULL,NULL
  	dc.w 		CUSTOMSCREEN
  	
;------ Data for palette
Palette:
	dc.w	$000
	dc.w	$ccc
	dc.w	$444
	dc.w	$fd0
	dc.w	$777
	dc.w	$f00
	dc.w	$aaa
	dc.w	$666
	
;------ Image Structures for display
ScreenImage:

	dc.w 		0,0		;left, top
	dc.w		320,246		;width, height
	dc.w		2		;depth
	dc.l		Image1Data	;image data
	dc.b		6,0		;plane pick,plane on/off
	dc.l		NULL		;next image
	
AnalyzerImage:

	dc.w 		126,47		;left, top
	dc.w		194,52		;width, height
	dc.w		2		;depth
	dc.l		Image2Data	;image data
	dc.b		6,0		;plane pick,plane on/off
	dc.l		NULL		;next image

_DOSBase
	dc.l		0	;storage for DOS lib pointer
_GfxBase
	dc.l		0	;storage for Graphics lib pointer
_IntuitionBase
	dc.l		0	;storage for Intuition lib pointer
OurProc:
	dc.l		0	;storage for ptr to process
OldWdw:
	dc.l		0	;storage for old pr_WindowPtr
WindowPtr
	dc.l		0	;storage for window structure pointer 
returnMsg:
	dc.l		0	;storage for workbench message

DOSname
	DOSNAME			;macro for DOS lib name
Grafname
	GRAFNAME		;macro for Graphics lib name
Intname
	INTNAME			;macro for Intuition lib name

;------ temp filename for testing purposes	
flname:	dc.b	'Test:1',0
	
;===============================================================
		SECTION	GraphicData,DATA_C
Image1Data:
	INCBIN	Df0:source/Sreendata	;load data for screen image
	
Image2Data:
	INCBIN	Df0:source/sa.data	;load data for Analyzer image


