program tstpict2, 1.0(100)  ! LibXL example of adding a picture 
!------------------------------------------------------------------------
!EDIT HISTORY
!Version 1.0:-
! [100] 26-Jun-25 / jdm / created
!------------------------------------------------------------------------
!NOTES 
!------------------------------------------------------------------------

++include ashinc:ashell.def
++include libxl:libxl64.bsi             ! test 64 bit version
++include'once sosfunc:fnshellex.bsi    ! Fn'Shell'Exec()

map1 handles
	map2 book,BookHandle
	map2 sheet,SheetHandle
	map2 textFormat,FormatHandle
    map2 pictFormat,FormatHandle
    
map1 misc
	map2 dlflags,b,4
	map2 file$,s,100,"tstpict"
	map2 launch,f
	map2 row,i,4
	map2 col,i,4
	map2 bookformat,b,1
    map2 status,i,4
    map2 pictspec$,s,260

dimx pictid(0), i, 4, auto_extend
    
	? "TSTNUMFOR: Test LibXL SetPicture"
	? "Loading/initializing libxl library..." 
	dlflags = DLF_STATUS_BASERR or DLF_SYSERR_BASERR	! trigger Basic error 65 on errors
	dlflags = dlflags or DLF_PRINT_ERRORS				! print status/syserr errors to screen
	dlflags = dlflags or DLF_ASCII  					! [101] we only need ascii here
	if Fn'LibXL'Load(dlflags=dlflags) then              ! [101] was flags=
		? "Unable to load LibXL library" 	! (probably shouldn't even get here - error trap instead)
		end
	endif

!>!    Book* book = xlCreateBook();
!>!

	input "Output format: 0=XLS, 1=XLSX [0]: ",bookformat
	if bookformat = 1 then
		file$ += ".xlsx"
	else
		file$ += ".xls"
	endif
    ? "Creating book..."
	book = Fn'LibXL'CreateBook(bookformat,rcbase=0)		! note, we don't really need the book for this prog (only 1 book)
														! use base 0 to match the original C example
    ? "book = ";Fn'Dec2Hex$(book)

	sheet = Fn'LibXL'AddSheet("Sheet1")
	textFormat = Fn'LibXL'AddFormat'SetAttributes(alignh=ALIGNH_LEFT)

	call Fn'LibXL'SheetSetCol(hsheet=sheet, colfirst=0, collast=0, width=-1)
	call Fn'LibXL'SheetSetCol(hsheet=sheet, colfirst=1, collast=1, width=50)
    
    call Fn'LibXL'SheetSetRow(hsheet=sheet, rowfirst=3, rowlast=20, height=90)
    
    ! [103] create a title centered across columns
    call Fn'LibXL'SheetCenterAcrossColumns(row=0,colfirst=0,collast=3, &
                        text$="Variations of SetPicture", &
                        bold=1,fillpattern=FILLPATTERN_SOLID, patternfgc=COLOR_GRAY25)
                        
    pictspec$ = "tstpict2.jpg"
    ? "Input sample image [";pictspec$;"]: ";
    input "", pictspec$
    pictid(1) = Fn'LibXL'AddPicture(fpath$=pictspec$)
    if pictid(1) >= 0 then
        ? "Picture loaded; id = ";pictid(1)
    else
        ? "Unable to load picture!"
        end
    endif
    

    for row = 1 to 13
        if row > 1 then     ! reload image for each variation (needed to allow different SetPicture options?)
            pictid(row) = Fn'LibXL'AddPicture(fpath$=pictspec$)
        endif
        
        switch row
            case 1
                call LibXL'SheetSetPicture(row=row+3,col=0,pictureid=pictid(row),dscale=0, pos=POSITION_MOVE_AND_SIZE)
                call Fn'LibXL'Write(row=row+3, col=1, hformat=textFormat, value="POSITION_MOVE_AND_SIZE, dscale=0")
                exit
            case 2
                call LibXL'SheetSetPicture(row=row+3,col=0,pictureid=pictid(row),dscale=0, pos=POSITION_ONLY_MOVE)
                call Fn'LibXL'Write(row=row+3, col=1, hformat=textFormat, value="POSITION_ONLY_MOVE, dscale=0")
                exit
            case 3
                call LibXL'SheetSetPicture(row=row+3,col=0,pictureid=pictid(row),dscale=0, pos=POSITION_ABSOLUTE)
                call Fn'LibXL'Write(row=row+3, col=1, hformat=textFormat, value="POSITION_ABSOLUTE, dscale=0")
                exit
            case 4
                exit
            case 5
                call LibXL'SheetSetPicture(row=row+3,col=0,pictureid=pictid(row),dscale=-0.75,pos=POSITION_MOVE_AND_SIZE)
                call Fn'LibXL'Write(row=row+3, col=1, hformat=textFormat, value="POSITION_MOVE_AND_SIZE, dscale=-0.75")
                exit
            case 6
                call LibXL'SheetSetPicture(row=row+3,col=0,pictureid=pictid(row),dscale=-0.75, pos=POSITION_ONLY_MOVE)
                call Fn'LibXL'Write(row=row+3, col=1, hformat=textFormat, value="POSITION_ONLY_MOVE, dscale=-0.75")
                exit
            case 7
                call LibXL'SheetSetPicture(row=row+3,col=0,pictureid=pictid(row),dscale=-0.75, pos=POSITION_ABSOLUTE)
                call Fn'LibXL'Write(row=row+3, col=1, hformat=textFormat, value="POSITION_ABSOLUTE, dscale=-0.75")
                exit

            case 8
                exit
            case 9
                call LibXL'SheetSetPicture(row=row+3,col=0,pictureid=pictid(row),height=75,width=50,pos=POSITION_MOVE_AND_SIZE)
                call Fn'LibXL'Write(row=row+3, col=1, hformat=textFormat, value="POSITION_MOVE_AND_SIZE, height=75,width=50")
                exit
            case 10
                call LibXL'SheetSetPicture(row=row+3,col=0,pictureid=pictid(row),height=75,width=50, pos=POSITION_ONLY_MOVE)
                call Fn'LibXL'Write(row=row+3, col=1, hformat=textFormat, value="POSITION_ONLY_MOVE, height=75,width=50")
                exit
            case 11
                call LibXL'SheetSetPicture(row=row+3,col=0,pictureid=pictid(row),height=75,width=50, pos=POSITION_ABSOLUTE)
                call Fn'LibXL'Write(row=row+3, col=1, hformat=textFormat, value="POSITION_ABSOLUTE, height=75,width=50")
                exit

            case 12
                exit
            case 13
                call LibXL'SheetSetPicture(row=row+3,col=0,pictureid=pictid(row))
                call Fn'LibXL'Write(row=row+3, col=1, hformat=textFormat, value="all defaults")
                exit
        endswitch
    next row
    
	if lookup(file$) then kill file$		! else SaveBook will error out
	call Fn'LibXL'SaveBook(file$)

	call LibXL'ReleaseBook()
	call Fn'LibXL'Unload()

	? file$;" written"
	input "enter 1 to launch: ",launch
	if launch then
		![102] xcall MIAMEX, MX_SHELLEX, launch, file$
        status = Fn'Shell'Exec(file$)
        ? Fn'Shell'Exec'Status'Description$(status)
	endif
	end
