GFX Scroll Bar full screen, need example

BlitzPlus Forums/BlitzPlus Programming/GFX Scroll Bar full screen, need example

The Forum Archive is locked. These are the original blitzbasic.com forums, kept so their posts can still be read and searched. No new topics, replies or edits can be made here. To join the conversation, head over to the Blitz3D+ forums.

OK, heres some really old code that I did. Its untested, but it allows you to view a document on the screen. Hope it helps....

[Code]
; Simple Document Displayer By Prof (Un-Tested)
; Last tested/compiled 21/10/02
;
; This set of functions can be used to display a document in a window.
; The document must be a raw-asci file. Scroll bars are automatically
; calculated so the user can scroll through the document.
;
AppTitle("Document Displayer")
Graphics 640,480,32,2
SetBuffer BackBuffer()
ClsColor(190,190,190):Cls:Flip

Dim Tex$(1000) ; Reserve 1000 lines in memory
fntArial=LoadFont("Arial",14,True,False,False) ; Try different fonts
SetFont fntArial


f$=RequestFile$("Open a text file","*.*")
ViewDocument(f$,10,10,400,300)


End

; ****************************************************************

Function ViewDocument(filename$,WinX,WinY,WinW,WinH)
; If a filename is given then the file is opened otherwise DATA
; statements are Read in (at the end of this code)
; WinX,WinY,WinW,WinH is the windows position and size
;
; It works out the number of lines to display based on the current
; font size and the height of the window. Also clips the length of
; the text to the window width.
;
; Open the file if the filename exists otherwise read DATA statements

debug=False

If filename$<>"" And FileType(filename$)=True
fileIn=ReadFile(filename$):LinesInDoc#=0
Repeat
LinesInDoc=LinesInDoc+1:tex$(LinesInDoc)=ReadLine$(FileIn)
Until Eof(FileIn)
CloseFile(FileIn):tex$(LinesInDoc+1)="*" ; put stopper at the end
Else
Restore TextDATA:LinesInDoc=1
Repeat
Read d$:Tex$(LinesInDoc)=d$:LinesInDoc=LinesInDoc+1
Until d$="*"
EndIf

Index#=1.0 ; First line of text
LinesInWindow#=(WinH/FontHeight()) ; No of lines of text in the window
DocSize#=(LinesInDoc)*FontHeight() ; Length of whole doc in pixels
ButtonSize#=16 ; Size of the scroll buttons
Button1X#=WinX+WinW-ButtonSize-1 ; X Position of top scroll button
Button1Y#=WinY+1 ; Y Position of top scroll button
Button2X#=WinX+WinW-ButtonSize-1 ; X Position of bottom scroll button
Button2Y#=WinY+WinH-ButtonSize-1 ; Y Position of bottom scroll button
ButtonDistance#=WinH-(ButtonSize*2) ; Distance between scroll buttons
t#=DocSize/(LinesInWindow*FontHeight()) ; Proportion variable
ThumbH#=(ButtonDistance/t#)-2 ; Height of scroll thumb
ThumbRange#=ButtonDistance-ThumbH ; Thumb range
Divider#=(LinesInDoc)/(ThumbRange+ThumbH) ; Proportion variable
ThumbX=WinX+WinW-ButtonSize-1 ; X Position of scroll thumb
ThumbYMin=WinY+ButtonSize+1 ; Minimum Y coordinate
ThumbYMax=((WinY+WinH)-Buttonsize-ThumbH-1) ; Maximum Y coorditate
ThumbY=ThumbYMin ; Y Position of scroll thumb
ThumbW=ButtonSize ; Width of scroll thumb


HighLightedBox(WinX,WinY,WinW,WinH,True) ; The Main Window
DrawScrollButton(1,Button1X,Button1Y,ButtonSize,ButtonSize,0)
DrawScrollButton(2,Button2X,Button2Y,ButtonSize,ButtonSize,0)
DrawScrollThumb(ThumbX,ThumbY,ButtonSize,ThumbH,WinX,WinY,WinW,WinH)
ShowPage(Index,LinesInWindow,WInX,WinY,WinW,WinH)

;MainLoop
Flip
Repeat
If ScrollDownActivated(Button2X,Button2Y,ButtonSize,ButtonSize)
DrawScrollButton(2,Button2X,Button2Y,ButtonSize,ButtonSize,True)
While ThumbY<ThumbYMax And ScrollDownActivated(Button2X,Button2Y,ButtonSize,ButtonSize)
Index#=Index#+Divider#
ShowPage(Index#,LinesInWindow,WinX,WinY,WinW,WinH)
ThumbY=ThumbY+1
DrawScrollThumb(ThumbX,ThumbY,ButtonSize,ThumbH,WinX,WinY,WinW,WinH)
Flip
Wend
DrawScrollButton(2,Button2X,Button2Y,ButtonSize,ButtonSize,False)
Flip
EndIf

If ScrollUpActivated(Button1X,Button1Y,ButtonSize,ButtonSize)
DrawScrollButton(1,Button1X,Button1Y,ButtonSize,ButtonSize,True)
While ThumbY>ThumbYMin And ScrollUpActivated(Button1X,Button1Y,ButtonSize,ButtonSize)
Index#=Index#-Divider#
ShowPage(Index#,LinesInWindow,WinX,WinY,WinW,WinH)
ThumbY=ThumbY-1: DrawScrollThumb(ThumbX,ThumbY,ButtonSize,ThumbH,WinX,WinY,WinW,WinH)
Flip
Wend
DrawScrollButton(1,Button1X,Button1Y,ButtonSize,ButtonSize,False)
Flip
EndIf

If PointInArea(MouseX(),MouseY(),ThumbX,ThumbY,ThumbW,ThumbH) And MouseDown(1) ; moving scroll bar
While MouseDown(1)
ThumbY=MouseY()-(ThumbH/2)
ThumbY=KeepInRange(ThumbY,ThumbYMin,ThumbYMax)
DrawScrollThumb(ThumbX,ThumbY,ButtonSize,ThumbH,WinX,WinY,WinW,WinH)
Index#=((ThumbY-ThumbYMin)*Divider#)+1
ShowPage(Index#,LinesInWindow,WinX,WinY,WinW,WinH)
Flip
Wend
EndIf

If debug
; Remove this code if you wish
Color 190,190,190:Rect 0,420,GraphicsWidth(),40
Color 0,0,0
Text 0,400,"WinX="+WinX+" WinY="+WinY+" WinW="+WinW+" WinH="+winH+" DocSize="+DocSize+"px"+" Lines In Doc="+LinesInDoc+" Lines In Window="+LinesInWindow
Text 0,425,"ThumbX="+ThumbX+" ThumbY="+ThumbY+" ThumbW="+ThumbW+" ThumbH="+ThumbH+ " Thumb Range="+ThumbRange
Text 0,450,"Index#="+Index#+" Divider="+Divider+" Button Distance="+ButtonDistance
Flip
EndIf
Until KeyHit(1)
End Function

; **********************************************************************

Function ScrollDownActivated(X,Y,W,H)
;tests to see if scrolling down has been activated
; returns TRUE if it has. x,y,w,h is the area of a scroll button
If (KeyDown(208)) Or (PointInArea(MouseX(),MouseY(),X,Y,W,H) And MouseDown(1))
Return True
EndIf
End Function

; **********************************************************************

Function ScrollUpActivated(X,Y,W,H)
; tests to see if scrolling down has been activated
If (KeyDown(200)) Or (PointInArea(MouseX(),MouseY(),X,Y,W,H) And MouseDown(1))
Return True
EndIf
End Function

; **********************************************************************

Function ShowPage(Index,LinesInWindow,WinX,WinY,WinW,WinH)
; Puts a page of text in the window

FH=FontHeight():yPos=WinY
Color 230,230,230:Rect WinX+1,WinY+1,WinW-2-16,WinH-2 ;clear window
Color 0,0,0
Viewport WinX,WinY,WinW-16-1,WinH-1 ;clip to the window area
For d = Index To (Index+LinesInWindow)-1 Step 1
Text WinX+4,YPos,tex$(d):YPos=YPos+FH
Next
Viewport 0,0,GraphicsWidth(),GraphicsHeight()
End Function

; **********************************************************************

Function DrawScrollThumb(x,y,w,h,WinX,WinY,WinW,WinH)
; Draws the scroll thumb (bit you can drap up & down)
; Draw area behind the thumb first
Color 215,215,215 : Rect WinX+WinW-w-1,WinY+w+1,w,WinH-2-(w*2)
; Now draw the actual thumb
HighLightedBox(x,y,w,h,False)
End Function

; **********************************************************************

Function DrawScrollButton(n,x,y,w,h,Inverse)
; n= the button number
; 1=top, 2=bottom, 3=left, 4=right
HighLightedBox(x,y,w,h,Inverse)
DrawScrollButtonArrow(n,x,y,w,h,Inverse)
End Function

; **********************************************************************

Function HighlightedBox(x,y,w,h,Inverse)
; draws a highlighted box with the light coming
; from the top left.
; If Inverse is true, the box is inversed
If inverse=True
c1=255:c2=90
Else
c1=90:c2=255
EndIf
Color 190,190,190:Rect x,y,w,h ; Draw solid area
Color c2,c2,c2:Line X,Y,X+W-1,Y :Line X,Y,X,Y+H-1 ; Top & Left
Color c1,c1,c1:Line X+W-1,Y,X+W-1,Y+H-1:Line X,Y+H-1,X+W-1,Y+H-1; Bottom & Right
End Function

; **********************************************************************

Function DrawScrollButtonArrow(n,x,y,w,h,Inverse)
; Draws a black arrow
Color 0,0,0
If n=1
Rect x+7,y+6+Inverse,2,1
Rect x+6,y+7+Inverse,4,1
Rect x+5,y+8+Inverse,6,1
Rect x+4,y+9+Inverse,8,1
ElseIf n=2
Rect x+4,y+6+Inverse,8,1
Rect x+5,y+7+Inverse,6,1
Rect x+6,y+8+Inverse,4,1
Rect x+7,y+9+Inverse,2,1
EndIf
End Function

; **********************************************************************

Function PointInArea(px,py,x,y,w,h)
; Function to check if px,py is inside the area x,y,w,h
; Returns TRUE if inside
If px>x And px<x+w
If py>y And py<y+h
Return True
EndIf
End If
End Function

; **********************************************************************

Function KeepInRange(n,Min,Max)
;Keeps n within a range defined by Min & Max.
If n<Min
n=Min
EndIf
If n>Max
n=Max
EndIf
Return n
End Function

; **********************************************************************

Function ClipText $ (s$,width)
; clips a string to a width in pixels
If StringWidth(s$)>=(width-4)
P=1:temp$=""
Repeat
c$=Mid$(s$,p,1)
temp$=temp$+c$
w=StringWidth(Temp$)
p=p+1
Until w>=(width-4)
s$=Left$(Temp$,p-2)
End If
Return s$
End Function


; The following DATA statements are not used if opening an
; external file. Use these DATA statements if you wish to
; include the document within the final executable.

.TextDATA
Data "REPORT: No document name given or the file does not exist."
Data "----------------------------------------------------------"
Data " "
Data " "
Data " "
Data " "
Data " "
Data "This text is from the data statements from within the"
Data "program itself. Handy if you want to keep all of the"
Data "documentation within the final executable."
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data " "
Data "*"

[/Code]