;Set your editor's TAB width to 3 chars.

;*************************************************************************
;PrintPic.asm
;	Reads an ILBM file and displays it as a screen/window until the right
;	mouse button is clicked. It also (spool) prints the picture if the LEFT
;	mouse button is pressed. This program is similiar to Scheppner's Display
;	program in that it handles normal and HAM ILBM's.
;
;	This program is different than Display in the following ways:
;	1). It is written in assembly code.
;	2). It utilizes the dissidents IFF ILBM library which can be opened and
;			used by many different tasks.
;	3). It utilizes the dissidents requester library for filename selection.
;	4). It utilizes the dissidents prtspool.library for printing.
;
;This source code was assembled using INNOVATRONICS CAPE assembler. It was
;linked using Blink as follows:
;
;	Blink StartUp.o PrintPic.o small.lib NODEBUG to PrintPic
;
;	small.lib is from CATS, or Fish Disk #92.
;	StartUp.o is my own version of a standard, startup code.
;
; If using an asm other than CAPE, delete the next 4 lines and also uncomment
; the section directive toward the end.

	SMALLOBJ	;CAPE directive for PC-relative addressing replaces absolute
	ADDSYM
	OPTIMON
	OBJFILE	"PrintPic.o"

   SECTION ShowPicCode,CODE

	INCLUDE	"rad:IFF.i"		;the asm include file for ilbm.library
	INCLUDE	"rad:FileIO.i"	;the asm include file for requester.library

	;for prtspool.library
_LVOPrintText			equ	-30
_LVOPrintNullText		equ	-36
_LVOPrintWindow		equ	-42
_LVOPrintFullWindow	equ	-48
_LVOAbortPrint			equ	-54
_LVOPrintRawText		equ	-60
_LVOPrintRawNullText equ	-66

	;from StartUp.o
	XREF	_SysBase,_DOSBase,_stdout,_BUFFER

	;from small.lib
	XREF	_LVOCloseScreen,_LVOCloseWindow
	XREF	_LVODelay
	XREF	_LVOWait,_LVOWaitPort,_LVOGetMsg,_LVOPutMsg,_LVOReplyMsg,_LVOSignal
	XREF	_LVOOpenLibrary,_LVOCloseLibrary
	XREF	_LVODisplayAlert,_LVOWrite,_LVOInput,_LVOOutput,_LVOCurrentDir
	XREF	_LVOAllocMem,_LVOFreeMem
	XREF	_LVOFindTask

LIB_VERSION	equ	33

out	rts

   XDEF  _main
_main:
		movea.l	_SysBase,a6
		moveq		#LIB_VERSION,d0
		lea		IntuiName,a1
		jsr		_LVOOpenLibrary(a6)
		move.l	d0,_IntuitionBase
		beq.s		out
;======Open dissidents Requester lib========
		moveq		#0,d0					;any version (for now)
		lea		RequesterName,a1
		jsr		_LVOOpenLibrary(a6)
		move.l	d0,d1
		beq.s		REQe
		move.l	d0,_RequesterBase
;======Open dissidents ILBM library===========
		moveq		#0,d0					;any version (for now)
		lea		ILBMName,a1
		jsr		_LVOOpenLibrary(a6)
		move.l	d0,d1
		beq.s		IFFe
		move.l	d0,_ILBMBase      
;================Do our Display Pic routine=================
		bsr		MAIN2
   ;---Free the FileIO if allocated (FileIO lib checks for 0)
		movea.l	a5,a1
		movea.l	_RequesterBase,a6
		jsr		_LVOReleaseFileIO(a6)
	;---Close requester, ilbm libs
		movea.l	_SysBase,a6
		movea.l	_RequesterBase,a1
		jsr		_LVOCloseLibrary(a6)
		movea.l	_ILBMBase,a1
		jsr		_LVOCloseLibrary(a6)
cIntu movea.l	_IntuitionBase,a1
		jmp		_LVOCloseLibrary(a6)		;exit program, return to AmigaDOS
;==========Display An Alert Error Msg=========
	;---No ILBM lib
IFFe	movea.l	_RequesterBase,a1
		jsr		_LVOCloseLibrary(a6)
		lea		IFFErrMsg,a1
		bra.s		post
	;---No Requester lib
REQe	lea		ReqErrMsg,a1
post	bsr		dsp_alert
		bra.s		cIntu

	XDEF	dsp_alert
;This makes an alert message out of the passed string in a1
;and displays it as an alert or CLI msg. 79 chars MAX in passed string.
dsp_alert:
		lea		_BUFFER+2,a0	;for WB, skip first WORD of _BUFFER for X pos
		moveq		#20,d0
		move.b	d0,(a0)+			;y co-ordinate = 20
		move.l	a0,d2
	;---copy passed string to BUFFER and get numOfChars
		movea.l	a1,a6
lenS	move.b	(a6)+,(a0)+
		bne.s		lenS
		subq.l	#1,a6
		suba.l	a1,a6			;numOfChars
	;---If the CLI, write the msg there
		move.l	_stdout,d1
		beq.s		WBalert
		move.l	a6,d3
	;---add new line char
		addq.l	#1,d3
		clr.b		(a0)
		moveq		#10,d0
		move.b	d0,-(a0)
		movea.l	_DOSBase,a6
		jmp		_LVOWrite(a6)
	;---If WB, post an alert
WBalert:
		clr.b		(a0)			;continuation byte = 0
		moveq		#80-1,d1		;80 chars - 1 Dbra
		sub.l		a6,d1
		bcc.s		aler
		moveq		#1,d1
aler	lsr.l		#1,d1
		lsl.w		#3,d1			;assume Topaz 8 system font
		movea.l	d2,a0
		subq.l	#3,a0
		move.w	d1,(a0)		;x co-ordinate = (numOfChars/2) * 8 (WORD)
	;---display the alert
		moveq		#50,d1		;Height
		moveq		#0,d0			;RECOVERY_ALERT
		movea.l	_IntuitionBase,a6
		jmp		_LVODisplayAlert(a6)

MAIN2:
;===========Get a FileIO structure for the FileIO lib==========
		movea.l	_RequesterBase,a6
		jsr		_LVOGetFileIO(a6)
		movea.l	d0,a5
		move.l	d0,d1
		bne.s		gFIO
		rts
	;---Setup the FileIO fields
		;---X pos = 6
gFIO	moveq		#6,d1
		move.w	d1,FILEIO_X(a5)
		;---Y pos = 11
		moveq		#11,d1
		move.w	d1,FILEIO_Y(a5)
		;---DrawMode = JAM2
		moveq		#1,d1
		move.b	d1,FILEIO_DRAWMODE(a5)
		;---PenA = 0
		clr.b		FILEIO_PENA(a5)
		;---PenB = 1
		move.b	d1,FILEIO_PENB(a5)
		;---The pathname buffer
		lea		_BUFFER,a0
		move.l	a0,FILEIO_BUFFER(a5)
;========Get the address of our ILBMFrame==============
		lea		ILBMFrame,a2
;========Set pointers initially to 0====================
next	suba.l	a3,a3		;screen = 0
		suba.l	a4,a4		;window = 0
;=======Get the filename to display===========
		movea.l	a5,a0					;the FileIO
		suba.l	a1,a1					;open requester on WB screen
		movea.l	_RequesterBase,a6
		jsr		_LVODoFileIOWindow(a6)
		move.l   d0,d1			;pathname buffer address
		beq		close_up		;an error in opening the window. Exit
		addq.l	#1,d0
		beq		close_up		;If -1, user must have selected CANCEL. Exit
;======Load the IFF pic into a window that the lib opens========
		movea.l	a2,a1
		;pathname in d1
		clr.l		iWindow(a1)				;indicate no open window
		clr.l		iScreen(a1)				;no open screen
		moveq		#3,d0						;blank mouse pointer, no title bar	
		move.b	d0,iUserFlags(a1)
		movea.l	_ILBMBase,a6
		jsr		_LVOLoadIFFToWindow(a6)
	;---get opened screen and window addresses
		movea.l	iWindow(a2),a4
		movea.l	iScreen(a2),a3
	;---check for error (not IFF_OKAY)
		move.b	d0,d1
		beq.s		dspp
	;---ERROR: "Load error"
		lea		BadLoad,a1
		move.l	a4,d0
		beq.s		post_msg
		movea.l	d0,a0
		move.l	a1,d0
		movea.l	_RequesterBase,a6
		jsr		_LVOAutoMessage(a6)
		bra.s		nextp
post_msg:
		bsr		dsp_alert
		bra.s		nextp
;========Display picture until the user clicks the mouse=======
; NOTE: The window that the lib opened for us has an IDCMP of MOUSEBUTTONS.
; If we needed something else, we would ModifyIDCMP() here.
dspp	bset.b	#0,25(a4)		;RMBTRAP
mmsg2	movea.l	_SysBase,a6
mmsg	movea.l	86(a4),a0
		jsr		_LVOGetMsg(a6)
		move.l	d0,d1
		bne.s		wmsg
		movea.l	86(a4),a0
		jsr		_LVOWaitPort(a6)
		bra.s		mmsg
wmsg	movea.l	d0,a1
		move.l	20(a1),-(sp)	;save CLASS
		move.w	24(a1),-(sp)	;save CODE
		jsr		_LVOReplyMsg(a6)
		move.w	(sp)+,d1
		move.l	(sp)+,d0
		Btst.l	#3,d0
		beq.s		mmsg
	;---If MENU BUTTON, dump the picture
		subi.b	#$68,d1  ;Right mouse down?
		beq.s		nextp
		subq.b	#1,d1    ;Left mouse down?
		bne.s		mmsg
		bsr		PrintPic
		bra.s		mmsg2
	;---Close window, screen, and then get next picture
nextp	bsr.s		close_up
		bra		next

	XDEF close_up
close_up:
;================drain the IDCMP of the window and close it===============
		move.l	a4,d0
		beq.s		nowind
		movea.l	_SysBase,a6
MMSG	move.l	86(a4),a0
		jsr		_LVOGetMsg(a6)
		move.l	d0,d1
		beq.s		nMSG
		movea.l	d1,a1
		jsr		_LVOReplyMsg(a6)
		bra.s		MMSG
nMSG	movea.l	_IntuitionBase,a6
		movea.l	a4,a0
		jsr		_LVOCloseWindow(a6)
;=============Close the Screen=====================
nowind:
		move.l	a3,d0
		beq.s		noscr
		movea.l	d0,a0
		movea.l	_IntuitionBase,a6
		jsr		_LVOCloseScreen(a6)
noscr	rts

;***************************************************
; This routine is all that is needed to print out any window's rastport.
;
; PrintPic(window)
;				a4

	XDEF	PrintPic
PrintPic:
;======== Open prtspool.library =======
		lea		PrtSpoolName,a1
		moveq		#0,d0
		movea.l	_SysBase,a6
		jsr		_LVOOpenLibrary(a6)
		lea		PrtErrMsg,a1
		move.l	d0,d1
		beq		dsp_alert
;======= Print out the entire rastport ========
; NOTE: User should have set up Preferences via WorkBench. If the printing
; was spooled, we should return immediately. We could even exit this program
; by pressing the right mouse button while the printing was is progress. If
; no spool, this call doesn't return until the printing is complete, or
; aborted.
		movea.l	a4,a0
		movea.l	d0,a6
		moveq		#1,d0
		swap		d0		;change colors to BW when posting "Remap to White" req (bit #16 set)
							;low byte of d1 = 0 (i.e. 1 copy only)
		jsr		_LVOPrintFullWindow(a6)
;======= Close prtspool.library =========
		movea.l	a6,a1
		movea.l	_SysBase,a6
		jmp		_LVOCloseLibrary(a6)


;	SECTION view,DATA		;If not using CAPE, uncomment this line

 ;******************* DATA ***********************
	XDEF  _IntuitionBase
_IntuitionBase	dc.l 0	; intuition lib pointer

	XDEF	_RequesterBase,_ILBMBase
_RequesterBase	dc.l 0
_ILBMBase		dc.l 0

	XDEF	ILBMFrame
	CNOP 0,2	;word-align
ILBMFrame		ds.b	sizeofILBMFrame

BadLoad			dc.b	'Load Error',0

IFFErrMsg		dc.b 'Need the dissidents '
ILBMName			dc.b 'ilbm.library',0
ReqErrMsg		dc.b 'Need the dissidents '
RequesterName	dc.b 'requester.library',0
INUSE				dc.b 'requester.library already in use',0
IntuiName		dc.b 'intuition.library',0
PrtErrMsg		dc.b 'Need the dissidents '
PrtSpoolName	dc.b 'prtspool.library',0

	END
