program TestScroll
!This program shows how to scroll to the last line in
!a window with scroll bars.
!It also shows how to make a window with no scroll bars
!that loses lines off the top as lines are typed in at
!the bottom.

use dflib
use dfwin

TYPE (windowconfig) wc
TYPE(qwinfo) wci
TYPE(rccoord) rc

integer*4 i, j, status, irow, rowmoves, pagesize
character*1 ch
i = 0

! Use unit 5 for input and output
! It will always scroll down but lines will
! be lost at the top.
open(unit=5,file="user")
!set the scrollable screen fairly small
wc%numtextrows = 16 ! number of scrollable rows+2
wc%numtextcols = 45 ! number of scrollable cols
wc.numxpixels  = -1
wc.numypixels  = -1
wc.numcolors   = -1
wc.title       = "Unit 5"C
wc.fontsize    = -1 
! try to set with user values
status = setwindowconfig(wc)
! if that fails, try again with system computed values
if(.not. status) tmp = setwindowconfig(wc)

! Set the size of window to be actually seen
! be the same or slightly bigger
WCI.TYPE = QWIN$SET
WCI.X  = 10
WCI.Y  = 2
WCI.H  = 16 ! lines hi
WCI.W  = 47 ! chars across
RES = SETBKCOLOR(15)
RES = SETTEXTCOLOR(0)
CALL CLEARSCREEN($GCLEARSCREEN)
STATUS = SETWSIZEQQ(5,WCI)


!Use unit 6 for ouput you wish to keep around
!and which you might want to scroll through
open(unit=6,file="user") 
!set a large number of scrollable rows,
!much bigger than the visible screen size
wc%numtextrows =  300 ! number of scollable rows
wc%numtextcols = 80
wc.numxpixels  = -1
wc.numypixels  = -1
wc.numcolors   = -1
wc.title       = "Unit 6"C
wc.fontsize    = #00080010
! try to set with user values
status = setwindowconfig(wc)
! if that fails, try again with system computed values
if(.not. status) tmp = setwindowconfig(wc)

! Set the size of window to be actually seen
WCI.TYPE = QWIN$SET
WCI.X  = 0
WCI.Y  = 0
WCI.H  = 18        ! pagesize lines hi
WCI.W  = 45        ! 45 chars across
RES = SETBKCOLOR(15)
RES = SETTEXTCOLOR(0)
CALL CLEARSCREEN($GCLEARSCREEN)
STATUS = SETWSIZEQQ(6,WCI)




!*****************************************
!Start with a clean slate, the outputtext
!position being 1,1 in the scrollable window
call settextposition(1,1,rc)
irow = 1

do i = 1,145
   write(6,*) "working i = ",i
end do

!*****************************************
!where are we when finishing text output
call gettextposition(rc)
rowmoves = rc.row - irow
!adjust for visible window size
pagesize = 18 ! for unit 6 in SetWsizeQQ
rowmoves = rowmoves - pagesize+1
!call tobottom(6) ! for simple situations
call scrolldown(6, rowmoves) ! for complicated situations

!Get user input on unit 5 (can also write to unit 5)
do i = 1,30
write(5,*) 'Enter an integer'
read(5,*) j
enddo
end program

subroutine tobottom(unit)
use dflib
use dfwin
integer unit, hwnd, res
hwnd = GetHwndQQ(unit)
res = SendMessage(hwnd,WM_VSCROLL,SB_BOTTOM,0)
end subroutine tobottom

subroutine scrolldown(unit,rows)
use dflib
use dfwin
integer unit, hwnd, res, rows, i
hwnd = GetHwndQQ(unit)

do i = 1, rows
res = SendMessage(hwnd,WM_VSCROLL,SB_LINEDOWN,0)
enddo

end subroutine scrolldown
