 ;====Uncomment these directives for MANX asm only!! Advice: Buy a good asm.
 ; far   code ;so that we can use register a4, and no limit on program size.
 ; far   data

	SMALLOBJ ;CAPE PC directive. Comment out if not using CAPE
	OBJFILE	"rad:CustomList.o"

; This program opens and utilizes the requester.library in order to use
; the new SPECIAL_REQ flag. This flag allows us to display a list of
; strings in a requester with all the features of the FileIO requester.
; Because version 0 of the library did not support this feature, but version
; 1.4 now does, we must open the library with version #1. Old applications
; that do not know about SPECIAL_REQ will still work with this version of
; the library. It is backward compatible as far as the disk IO features are
; concerned.

; If running this program from the CLI and you specify any argument on the
; command (i.e.  1> TestFileIO blort ), then the window will open on a hires
; screen. You can then see what the requester looks like in hires.

;  For Manx,   ln -o TestFileIO  ManxStartUp.o  main.o  cl32.lib
;
;  For Others, Blink StartUp.o main.o amiga.lib NODEBUG to TestFileIO

 INCLUDE  "rad:FileIO.i"

 ;======Amiga Library routines======
   XREF    _LVOCloseLibrary,_LVOCloseScreen,_LVOCloseWindow,_LVOSetMenuStrip
   XREF    _LVOOpenWindow,_LVOOpenScreen,_LVOOpenLibrary
   XREF    _LVOGetMsg,_LVOReplyMsg,_LVOWait,_LVOMove,_LVOText
   XREF    _LVOSetAPen,_LVOSetBPen,_LVOSetDrMd

 ;======From the startup code======
   XREF    _SysBase,_DOSBase

LIB_VERSION equ 33

   SECTION FileIOTestCode,CODE

   XDEF   _main
_main:
    movem.l   d2-d7/a2-a6,-(sp)
    movea.l   _SysBase,a6
;======Open The Intuition Library=======
    moveq     #LIB_VERSION,d0
    lea       IntuitionName,a1
    jsr       _LVOOpenLibrary(a6)
    lea       _IntuitionBase,a4
    move.l    d0,(a4)+
    beq       C7
;======Open The Graphics Library========
    moveq     #LIB_VERSION,d0
    lea       GfxName,a1
    jsr       _LVOOpenLibrary(a6)
    move.l    d0,(a4)+
    beq       C6
;*******Open the "Brand New, Improved, Exciting" Requester library*********
    moveq     #1,d0            ;version #1 please
    lea       RequesterName,a1
    jsr       _LVOOpenLibrary(a6)
    move.l    d0,(a4)+
    beq       C5
;=====If started from WBench, then don't open a CUSTOM screen
    movea.l   _IntuitionBase,a6
    move.l    52(sp),d0
    beq.s     .9
;=====If opened from CLI with any argument, then open CUSTOM screen
    subq.l    #2,48(sp)
    bcs.s     .9
    lea       newScreen,a0
    jsr       _LVOOpenScreen(a6)
    lea       newWindow,a0
   ;-----Window's Screen = ScreenPtr
    move.l    d0,30(a0)
    beq.s     .9A             ;If an error, forget the screen!
   ;-----Window's Type = CUSTOMSCREEN
    move.w    #15,46(a0)
;=========Open the FileIO window==========
.9  lea       newWindow,a0
.9A jsr       _LVOOpenWindow(a6)
    move.l    d0,(a4)+
    beq       C2
    movea.l   d0,a3
   ;---Get Window's RastPort
    move.l    50(a3),(a4)
    lea       ProjectMenu,a1
    movea.l   a3,a0
    jsr       _LVOSetMenuStrip(a6)
;---Get a FileIO structure
E1  movea.l   _RequesterBase,a6
    jsr       _LVOGetFileIO(a6)
    movea.l   d0,a4
    move.l    d0,d1
    beq       IOe          ;If NULL, then error, so exit this test program.
;---Set Colors and DrawMode
    moveq     #1,d0
    move.b    d0,FILEIO_DRAWMODE(a4)
    movea.l   RastPort,a2
    movea.l   a2,a1
    movea.l   _GfxBase,a6
    jsr       _LVOSetDrMd(a6)
    moveq     #2,d0
    move.b    d0,FILEIO_PENA(a4)
    movea.l   a2,a1
    jsr       _LVOSetAPen(a6)
    moveq     #0,d0
    move.b    d0,FILEIO_PENB(a4)
    movea.l   a2,a1
    jsr       _LVOSetBPen(a6)
;====VERY IMPORTANT: Indicate that we want a SPECIAL_REQ==========
    bset.b    #7,(a4)  ;set the SPECIAL_REQ bit of Flags
;====Set up the XY co-ordinates of where the requester will open====
    moveq     #6,d0
    move.w    d0,FILEIO_X(a4)  ;x position
    moveq     #11,d0
    move.w    d0,FILEIO_Y(a4)  ;y position
;===========SET UP OUR LIST OF STRINGS==========
;NOTE: Since our list is initially empty, we don't have to call NewEntryList
;first, but we will anyway to develop good habits.
    movea.l   _RequesterBase,a6
    movea.l   a4,a1
    jsr       _LVONewEntryList(a6)
    lea       StringAddr,a2
    moveq     #10-1,d7         ;make a list of 10 strings
nxtEN:
    move.l    d7,d1
    addq.w    #1,d1            ;ID = loopcount+1 (i.e. 1 to 10)
    movea.l   a4,a1
    movea.l   (a2)+,a0
    jsr       _LVOAddEntry(a6)
    dbra      d7,nxtEN(pc)
;---Set up our routine, Information(), as the custom FileIO gadget routine
;   We don't have to set up these 2 fields, we could leave them 0 in which
;   case the custom gadget is ignored.
    lea       About,a0
    move.l    a0,FILEIO_TEXT(a4)
    lea       Information,a0
    move.l    a0,FILEIO_ROUTINE(a4)
;====Create a mask of the Window's UserPort's mp_Sigbit=====
E3  movea.l   86(a3),a0
    move.b    15(a0),d0
    moveq     #0,d7
    Bset.l    d0,d7
;=====Get the message that arrived at our UserPort====
E4  movea.l   86(a3),a0
    movea.l   _SysBase,a6
    jsr       _LVOGetMsg(a6)
    move.l    d0,d1
    bne.s     E7
;===Check if we are ready to exit the program=====
E5  Btst.b    #0,Quit
    beq       E15
;----Print out "Click Mouse to start demo....
    movea.l   RastPort,a2
    moveq     #5,d0
    moveq     #75,d1
    movea.l   a2,a1           ;our window's RastPort
    move.l    a6,-(sp)        ;save _SysBase
    movea.l   _GfxBase,a6
    jsr       _LVOMove(a6)
    moveq     #36,d0          ;# of bytes to output.
    lea       Click,a0
    movea.l   a2,a1
    jsr       _LVOText(a6)
    movea.l   (sp)+,a6        ;restore _SysBase
;===Wait for a message sent to our Window (from Intuition)===
E6  move.l    d7,d0
    jsr       _LVOWait(a6)
    bra.s     E4
;====Copy all the info we want from the IntuiMessage====
E7  movea.l   d0,a1
    lea       20(a1),a0  ;get the address of the first field to copy.
    move.l    (a0)+,d6   ;Copy the Class field to d6
    move.w    (a0)+,d5   ;Copy the Code field to d5
    move.w    (a0)+,d4   ;Copy the qualifier field to d4
    movea.l   (a0)+,a2   ;Copy the IAddress field to a2
    move.w    (a0)+,d3   ;Copy MouseX position to d3
    move.w    (a0)+,d2   ;Copy MouseY position to d2
;====Now reply to the message so Intuition can dispose of it
E8  ;Address of the message is in a1.
    jsr       _LVOReplyMsg(a6)
;========switch (class)=========
    Bclr.l    #9,d6  ;CLOSEWINDOW
    bne.s     CW
    Bclr.l    #8,d6  ;MENUPICK
    bne.s     MU
    Bclr.l    #3,d6  ;MOUSEBUTTONS
    beq       E4
;---Make sure that it's an UP select if MOUSEBUTTONS
    subi.b    #$68,d5
    beq       E4     ;ignore down
;===The FOLLOWING ROUTINE IS OUR TEST CALL. WHEN THE USER IS DONE (in FileIO lib)
;===HE WILL SELECT EITHER THE CANCEL OR OK! GADGET. IF CANCEL, TestFileIO
;===DOES NOTHING. IF OK!, TestFileIO JUSTS PRINTS THE SELECTED STRING.
E2  bsr     TestFileIO
    bra     E4
;=========case CLOSEWINDOW:============
CW  Bclr.b    #0,Quit
    bra       E4
;=========case MENUPICK:==============
  ;------Determine which item
MU lsr.w   #5,d5         ;Shift the item # bits into lowest bits of reg.
   andi.w  #$3F,d5       ;Isolate the Item # from the Menu and subitem #.
   bne     E4
;---Set MULTIPLE_FILES
   bchg.b  #MULTIPLE_FILES,1(a4)
   bra     E4
;======if an error, indicate NO_MEMORY. This is a FileIO routine
;======and is callable even if GetFileIO() fails
IOe movea.l   a3,a0
    moveq     #0,d0
    movea.l   _RequesterBase,a6
    jsr       _LVOAutoFileMessage(a6)
;========NOW BEGINS OUR EXIT ROUTINE=========
 ;ReleaseFileIO will free our list of strings
E15 movea.l   a4,a1                ;If the pointer to FileIO was NULL, then
    movea.l   _RequesterBase,a6    ;ReleaseFileIO just returns, so it's safe to
    jsr       _LVOReleaseFileIO(a6) ;always release any return value of GetFileIO.
;=====Close the Window and Screen======
C1  movea.l   _IntuitionBase,a6
    movea.l   a3,a0
    jsr       _LVOCloseWindow(a6)
C2  move.l    ScreenPtr,d0
    beq.s     C3            ;check if we specified a screen
    movea.l   d0,a0
    ; _IntuitionBase in a6
    jsr       _LVOCloseScreen(a6)
;=====Close Whichever Libs are Open (_SysBase in a6 for ALL calls)=====
C3  movea.l  _SysBase,a6
    movea.l  _RequesterBase,a1
    jsr      _LVOCloseLibrary(a6)
C5  movea.l  _GfxBase,a1
    jsr      _LVOCloseLibrary(a6)
C6  movea.l  _IntuitionBase,a1
    jsr      _LVOCloseLibrary(a6)
C7  movem.l  (sp)+,d2-d7/a2-a6
    rts

;*******************************************
; This just calls the DoFileIO library routine and displays a msg on return.
; (GetFileIO must have been called with success first).
; The DoFileIO routine returns -1 if the user selected CANCEL, -2 if the
; library is in use by another task, or returns non-zero if OK! was selected.
; At this point, if the user selected OK, our FILEIO_FILENAME buffer will
; contain the selected string and FILEIO_FILESIZE will be the ID.
;
;   TestFileIO(FileIO, window)
;                a4      a3

typename:
;--This shows what would happen (automatically) if the lib was in use on a
;  call to DoFileIO(), or the requester couldn't open.

.error:
		lea		errmsg,a0
prt	move.l	a0,d0
		movea.l	a3,a0
		jsr		_LVOAutoMessage(a6)
		moveq		#0,d0
		rts
.can	lea		cancel,a0
		bra.s		prt
.inuse:
		lea		inuse,a0
		bra.s		prt

   XDEF TestFileIO
TestFileIO:
		movea.l	_RequesterBase,a6
	;---DoFileIO(FileIO, window)
		movea.l	a3,a1
		movea.l	a4,a0
		jsr		_LVODoFileIO(a6)
		move.l	d0,d1
		beq.s		.error
;Must have been an error if returned 0. If we were using DoFileIOWindow(),
;the window might not have opened. Or the requester might not have opened.
;We could check the FileIO's ERRNO field to see what the specific error was.
		addq.l	#1,d1
		beq.s		.can		;If -1, user must have selected CANCEL
		addq.l	#1,d1
		beq.s    .inuse	;If -2, the library was in use
 ;---AutoMessage(string, window)  Display our chosen string.
		;Filename buffer address in d0
		movea.l	a3,a0
		jsr		_LVOAutoMessage(a6)
 ;---If ID = -1 then user must have typed a string not in the list
		move.l	FILEIO_FILESIZE(a4),d0  ;this is the ID of the chosen field
		bpl.s		out1
		movea.l	a3,a0
		lea		NotIn,a1
		move.l	a1,d0
		jsr		_LVOAutoMessage(a6)
	;---Did our custom routine end the requester?
out1	moveq		#ERR_APPGADG,d0
		sub.b		FILEIO_ERRNO(a4),d0
		bne.s		out2
		movea.l	a3,a0
		lea		AppCan,a1
		move.l	a1,d0
		jsr		_LVOAutoMessage(a6)
	;---If MULTIPLE_FILES, display all selected entries
out2	btst.b   #MULTIPLE_FILES,1(a4)
		beq.s    out3
		clr.l    -(sp)      ;zero our PTR to a FileEntry
morFL	movea.l  sp,a0
		movea.l  a4,a1
		jsr      _LVORetrieveEntry(a6)
		move.l   d0,d1
		beq.s    last
		movea.l  d0,a0
		move.l   8(a0),d0   ;get the Entry structure
		addq.l   #5,d0      ;get the EntryString address
		movea.l  a3,a0
		jsr      _LVOAutoMessage(a6)
		bra.s    morFL
last	addq.l   #4,sp
out3	moveq		#1,d0
		rts

;=====================================================================
; Our routine for the custom FileIO gadget. Lib passes our FileIO in a2
; and Window in a3. This returns the BOOL value from AutoPrompt3 so that
; if the user selects YES (TRUE) the requester ends.

   XDEF Information
Information:
    movem.l  a2/a3,-(sp)
    movea.l  a3,a0
    lea      Msg1,a1
    lea      Msg2,a2
    lea      Msg3,a3
    movea.l  _RequesterBase,a6
    jsr      _LVOAutoPrompt3(a6)
    movem.l  (sp)+,a2/a3
    rts

  ; SECTION MainData,DATA  ;Not needed for CAPE PC relative addressing!!

   XDEF _IntuitionBase,_GfxBase,ScreenPtr
   XDEF _RequesterBase
 ;must be in this order
_IntuitionBase dc.l 0
_GfxBase       dc.l 0
_RequesterBase dc.l 0
window         dc.l 0
RastPort       dc.l 0

   XDEF   newScreen
newScreen:
   dc.w   0,0         ;LeftEdge, TopEdge
   dc.w   640,400     ;Width, Height
   dc.w   2           ;Depth
   dc.b   0,1         ;Detail, Block pens
   dc.w   -32764      ;ViewPort Modes HIRES|LACE (must set/clr HIRES as needed)
   dc.w   15          ;CUSTOMSCREEN
   dc.l   TextAttr    ;Font
   dc.l   ScrTitle
   dc.l   0           ;Gadgets
   dc.l   0           ;CustomBitmap

   XDEF   newWindow
newWindow:
          dc.w   30,30
          dc.w   306,145
          dc.b   0,1
 ;IDCMP = MOUSEBUTTONS|CLOSEWINDOW|MENUPICK
          dc.l   $308
 ;WindowFlags = WINDOWDRAG|WINDOWDEPTH|SMART_REFRESH|ACTIVATE|WINDOWSIZE
 ;(no FOLLOWMOUSE allowed as that messes up the requester when using
 ;DoFileIO(). If you need FOLLOWMOUSE, then use DoFileIOWindow() to open
 ;the req in its own window.)
          dc.l   $100F
          dc.l   0
          dc.l   0
          dc.l   WINTITLE
ScreenPtr dc.l   0
          dc.l   0
          dc.w   306,145
          dc.w   600,240
          dc.w   1         ;WBENCHSCREEN

;==========THE PROJECT MENU===========

ProjectMenu:
   dc.l 0
   dc.w 0,0
   dc.w 90,0
   dc.w 1
   dc.l ProjectTitle
   dc.l ExtItem
   dc.w 0,0,0,0

 ;These are MenuItem structures for the preceding Menu Structure.
ExtItem   dc.l 0
          dc.w 0,0
          dc.w 200,10
          dc.w $5F
          dc.l 0
          dc.l MultiText
          dc.l 0
          dc.b 'M'
          dc.b 0
          dc.l 0
          dc.w 0
MultiText dc.b 0,1,1,0
          dc.w 19,0
          dc.l TextAttr,MultiString,0

TextAttr:        ;Topaz 8 is a ROM font so doesn't need to be opened
   dc.l   FONTNAME
   dc.w   8      ;TOPAZ_EIGHTY
   dc.b   0,0

StringAddr:
   dc.l  Ten
   dc.l  Nine
   dc.l  Eight
   dc.l  Seven
   dc.l  Six
   dc.l  Five
   dc.l  Four
   dc.l  Three
   dc.l  Two
   dc.l  One

Ten   dc.b 'Ten',0
Nine  dc.b 'Nine',0
Eight dc.b 'Eight',0
Seven dc.b 'Seven',0
Six   dc.b 'Six',0
Five  dc.b 'Five',0
Four  dc.b 'Four',0
Three dc.b 'Three',0
Two   dc.b 'Two',0
One   dc.b 'And this is One',0

Quit dc.b 1  ;When this is a 0, the user wants to exit.

ScrTitle			dc.b 'Example FileIO Program Screen',0
WINTITLE			dc.b 'Example FileIO Program Window',0
Click				dc.b 'Click mouse for demo or CLOSEWINDOW.',0
IntuitionName	dc.b 'intuition.library',0
DOSName			dc.b 'dos.library',0
GfxName			dc.b 'graphics.library',0
RequesterName	dc.b 'requester.library',0
FONTNAME			dc.b 'topaz.font',0
errmsg			dc.b 'Error in opening the requester',0
cancel			dc.b 'The CANCEL gadget was selected',0
inuse				dc.b 'Another task using the requester',0
About				dc.b '   About',0
Msg1				dc.b 'An example of SPECIAL_REQ',0
Msg2				dc.b 'by Jeff Glatt',0
Msg3				dc.b 'dissidents',0
NotIn				dc.b 'This string is not in the list.',0
AppCan			dc.b 'Cancelled by the custom gadget.',0
MultiString		dc.b 'Multi File',0
ProjectTitle	dc.b 'Project',0

   END
