
;
;
; HTML viewer v0.5
;
; (C) 1996 John Kennedy for Amiga Format
;
;

; Define Constants

#screen_height=256    ; Assume a 512 by 256 screen
#limitx=560           ; How far across the screen text can go...
#limit_height=1200    ; Limit to size of screen to attempt
; Define list
NEWTYPE .linktype
  x1.q
  y1.q
  x2.q
  y2.q
  address$
End NEWTYPE
Dim List hyperlinks.linktype(100)

; Rest of program...

Statement alert{t$,a$,b$,c$,d$}
  ; This procedure puts up a box
  WTitle t$,t$
  Window 1,100,100,300,100,$1002,t$,1,2
  Use Window 1    ; Use the new window for i/o
  NPrint ""
  NPrint a$
  NPrint b$
  NPrint c$
  NPrint d$
  NPrint " * Click to continue *"
  MouseWait       ; Wait for a mouse click
  Free Window 1   ; Shut the about window
  Use Window 0    ; Return to main window!
End Statement
Statement load{}
  SHARED html$
  SHARED count
  html$=""
  WTitle "Loading..","Loading.."
  MaxLen path$=192
  MaxLen name$=192

  path$="ram:"
  pattern$="(#?html||#?htm)"
  p$=ASLFileRequest$("HTML File to load",path$,name$,pattern$)

  count=0
  flag=ReadFile(0,p$)
  If flag=True
    FileInput 0
    While NOT Eof(0)
      html$=html$+Inkey$(1)
      count=count+1
    Wend
  Else
    alert{"File Error"," Sorry, the File you requested"," could not be loaded.", " Please try again",""}

  EndIf
  End Statement

; Start of Program!
; -----------------

; Open a screen
Screen 0,10,"Blitz Browser"
height=screen_height
Gosub open_display
WLocate 10,10
Print "Welcome to Blitz Browser"
Repeat

  WTitle "Waiting","Waiting"

  Select WaitEvent
    Case $100   ; Menu operation
      Select ItemHit
        Case 0; The Load option
        load{}
        Gosub process

        Case 1; The Save option
        ; Some day
        Case 2; The About option
        alert{"About"," ","HTML Viewer","Early alpha"," "}
        Case 3; The Quit option
        Goto goodbye
      End Select

    Case $200   ; Close gadget selected
      Goto goodbye
    Case $40    ; Scroll bar clicked
        y.w=VPropPot(0,1)*height
        PositionSuperBitMap 0,y.w
        Redraw 0,1
    Case $8 ; Mouse button
        Gosub click

  End Select
Forever

.process

WTitle "Processing HTML","Processing HTML"
WCls
Redraw 0,1
preview=On    ; Nothing is printed, at first

ResetList hyperlinks()

For scan=1 To 2
  bold=Off
  underline=Off
  italic=Off
  size=3
  oldsize=3
  original=3
  offset=0
  Gosub use_font
  chunk$=""
  startx=8
  starty=8
  xpos=startx
  ypos=starty
  sizey=13
  WLocate xpos,ypos
  letter=1
  tag=False
  value=False
  t$=""
  v$=""
  lnk$=""
  While letter<count
    l$=Mid$(html$,letter,1)
      If Asc(l$)=10 l$=" "
      If tag=True
        If l$=">" Gosub process_tag:l$="":tag=False
       t$=t$+l$
      Else
        If l$="<" tag=True:t$="":l$=""
        If (l$<>"" AND value=True) v$=v$+l$
        If (l$<>"" AND value=False) Then Gosub process_text
     EndIf
    letter=letter+1
  Wend
  If preview=On
    ; Close existing screen and re-open one the right size
    CloseWindow 0
    height=ypos
    If height<#screen_height Then height=#screen_height
    If height>#limit_height Then height=#screen_height
    Gosub open_display
  End If

  preview=Off
  Next scan
Return

.flush_text
  If preview=Off Print chunk$:offset=WCursX
  chunk$=""
Return

.process_text
chunk$=chunk$+l$

If (offset+(Len(chunk$)*sizex))>#limitx OR ((offset+(Len(chunk$)*sizex))>(#limitx-120) AND l$=" ")
    If preview=Off Print chunk$
    If preview=On
      t$="Height:"+Str$(ypos)
     WTitle t$,t$
    End If
    Gosub line_break
    chunk$=""
EndIf
Return
.process_tag
  t$=LCase$(t$)
  t$=StripTrail$(t$,32)
  t$=StripLead$(t$,32)
  Select t$
        Case "br"
          Gosub flush_text
          Gosub line_break
        Case "p"
          Gosub flush_text
          Gosub new_paragraph
        Case "hr"
          Gosub flush_text
          Gosub horizontal_line
        Case "/b"
          bold=Off:Gosub flush_text:Gosub use_font
        Case "/u"
          underlined=Off:Gosub flush_text:Gosub use_font
        Case "/i"
          italic=Off:Gosub flush_text:Gosub use_font
        Case "b"
          bold=On:Gosub flush_text:Gosub use_font
        Case "u"
          underlined=On:Gosub flush_text:Gosub use_font
        Case "i"
          italic=On:Gosub flush_text:Gosub use_font
        Case "h1"
          size=1:Gosub flush_text:Gosub use_font
        Case "h2"
          size=2:Gosub flush_text:Gosub use_font
        Case "h3"
          size=3:Gosub flush_text:Gosub use_font
        Case "h4"
          size=4:Gosub flush_text:Gosub use_font
        Case "h5"
          size=5:Gosub flush_text:Gosub use_font
        Case "h6"
          size=6:Gosub flush_text:Gosub use_font
        Case "/h1"
          size=0:Gosub flush_text:Gosub use_font:Gosub new_paragraph
        Case "/h2"
          size=0:Gosub flush_text:Gosub use_font:Gosub new_paragraph
        Case "/h3"
          size=0:Gosub flush_text:Gosub use_font:Gosub new_paragraph
        Case "/h4"
          size=0:Gosub flush_text:Gosub use_font:Gosub new_paragraph
        Case "/h5"
          size=0:Gosub flush_text:Gosub use_font:Gosub new_paragraph
        Case "/h6"
          size=0:Gosub flush_text:Gosub use_font:Gosub new_paragraph
        Case "title"  ; start capturing title
          value=True:v$=""
        Case "/title" ; title captured
          If value=True
            Gosub print_title:value=False
          EndIf
        Case "/a" ; link captured
          If value=True
            Gosub make_link:value=False
          EndIf
  End Select
        ; Now tests for more fiddly commands
        If Left$(t$,2)="a "
          value=True:v$=""
          If preview=Off
            AddItem hyperlinks()
            hyperlinks()\address=t$
          EndIf
        EndIf

        If Left$(t$,3)="img"
          Gosub picture
        EndIf

Return

.click
; test to see if there is a link under the pointer
; when the user clicks the left mouse button

x=WMouseX
y=WMouseY+VPropPot(0,1)*height
ResetList hyperlinks()
l$=""
While (NextItem(hyperlinks()) AND l$="")
USEPATH hyperlinks()
  If (x>=\x1 AND x<=\x2) AND (y>=\y1 AND y<=\y2)
    l$=\address
  EndIf
Wend
If l$<>"" Then NPrint "The link is ",l$
Return

;
; HTML COMMANDS
;
.horizontal_line

If preview=Off
  y=WCursY
  w=InnerWidth
  WLine 8,y,(w-32),y,1
  WLine 8,y+1,(w-32),y+1,2
EndIf
Gosub new_paragraph
Return
.picture
Gosub flush_text
Gosub new_paragraph
If preview=Off
  y=WCursY
  x=WCursX
  WBox x,y,x+16,y+16,2
  WLine x,y,x+16,y+16,1
  WLine x,y+16,x+16,y,1
End If

Gosub new_paragraph
Return

.print_title
v$=StripTrail$(v$,32)
v$=StripLead$(v$,32)
WTitle v$,v$
Return
.make_link
  Gosub flush_text
  v$=StripTrail$(v$,32)
  v$=StripLead$(v$,32)
  If preview=Off
    USEPATH hyperlinks()
    \x1=WCursX
    \y1=WCursY
    WColour 2
    Print v$," "
    WColour 1
    \x2=WCursX
    \y2=WCursY+sizey
    ;WBox \x1,\y1,\x2,\y2,2  ; Used for testing...
  End If
Return
;
; Text positions
;
.line_break
  xpos=startx
  ypos=ypos+sizey
  WLocate xpos,ypos
  offset=0
Return
.new_paragraph
  xpos=startx
  ypos=ypos+sizey+sizey
  WLocate xpos,ypos
  offset=0
Return

; FONT Routines
.use_font
; Control the font used, depending on size and attributes
    style.b=0
    If underlined=On style=style+1
    If bold=On style=style+2
    If italic=On style=style+4
    WindowFont size,style

; Alter how much space a newline takes up, depending
; on the size of the font used.
    Select size
    Case 0
      sizey=13
      sizex=5
    Case 1
      sizey=28
      sizex=12
    Case 2
      sizey=24
      sizex=10
    Case 3
      sizey=18
      sizex=7
    Case 4
      sizey=15
      sizex=6
    Case 5
      sizey=13
      sizex=5
    Case 6
      sizey=11
      sizex=4

    End Select
    If italic=On sizex=sizex+1
    If bold=On sizex=sizex+2

;

Return
.prepare_fonts
f$="times.font"
LoadFont 6,f$,11
LoadFont 5,f$,13
LoadFont 4,f$,15
LoadFont 3,f$,18
LoadFont 2,f$,24
LoadFont 1,f$,28
LoadFont 0,"helvetica.font",13
Return

.open_display
;
; Define and open a Screen and Window
;
; Define a BitMap
BitMap 0,640,height+#screen_height,2
; Define menus
MenuTitle 0,0,"Project"
MenuItem 0,0,0,0,"Load       ","L"
MenuItem 0,0,0,1,"Save       ","S"
MenuItem 0,0,0,2,"About      "
MenuItem 0,0,0,3,"Quit       ","Q"
; Define Gadgets
PropGadget 0,-16,0,128+2+1024,1,16,244
; Open a Special Window
Window 0,0,0,640,256,$0+$0+$20000+$20+$8+$400+$80+$1000,"SuperWindow",1,2,0,0
SetMenu 0
SetVProp 0,1,0,(10/height)
Redraw 0,1

Gosub prepare_fonts
WindowFont 0
Return

.goodbye
  CloseWindow 0
  CloseScreen 0
End

