.
.RogoboInfo

; Rogobo - "RollOver - GOod BOy!"
;           ^   ^      ^^   ^^
; -------------------------------------------------------------------------

; Javascript rollover generator
; (Public Domain program and source code - "DoWhatYouLikeWare" 7 Feb 2000)
; --
; Written by James L Boyd - thesurfaces@rockers.co.uk

; -------------------------------------------------------------------------

; * * * * I M P O R T A N T ! * * * *

; Make sure you have Blitzlibs:amigalibs.res set in
; your Compiler Options! This code uses the Blitz
; reqtools includes, as well as some OS structures.

; If it fails to compile because of the reqtools stuff,
; uncomment the next line and try again:

; INCLUDE "Blitz2:Developers/amigaincludes/libraries/reqtools.bb2"

; -------------------------------------------------------------------------
.
.Top

; version string:
v$="$VER: Rogobo 1.0 (07/02/2000) by James L Boyd "

WBStartup                           ; run from an icon
FindScreen 0,"Workbench Screen"     ; get Workbench screen

*scr.Screen=Peek.l(Addr Screen(0))  ; get screen structure

sw.w=*scr\Width                     ; screen width
sh.w=*scr\Height                    ; screen height

bh.b=*scr\BarHeight+1               ; title bar height

*scfont.TextAttr=*scr.Screen\Font   ; get screen's font
fheight.b=(*scfont.TextAttr\ta_YSize)
fname$=Peek$(*scfont.TextAttr\ta_Name)

LoadFont f,fname$,fheight           ; use screen's font

gw.w=300                            ; width of gadgets
pad.b=3                             ; gadget text padding (above & below)
gh.b=bh+pad                         ; gadget height (= title bar + padding)

lb.b=*scr\WBorLeft                  ; window left border
rb.b=*scr\WBorRight                 ; window right border

GTButton 0,0,-lb,-bh,gw,gh,"Create new rollover",0  ; first button
GTButton 0,1,-lb,pad,gw,gh,"Save rollover HTML",0   ; second button
GTButton 0,2,-lb,bh+pad*2,gw/2,gh,"About",0           ; third button
GTButton 0,3,(gw/2-lb),bh+pad*2,gw/2,gh,"Quit",0   ; fourth button
;     ^^^ "Trial and error does it every time" ;)

numgadshi.b=3                       ; number of rows of gadgets

ww.w=gw+lb+rb                       ; calculate window width

wh.w=(gh)*(numgadshi+1)-(pad-2)     ; calculate window height

wx.w=(sw/2)-(ww/2)                  ; centre window x position
wy.w=(sh/2)-(wh/2)                  ; centre window y position

; -------------------------------------------------------------------------

; gadtools menus space themselves out properly!

; you need the Ultimate Blitz Basic CD for this library,
; or alter it to suit "normal" Blitz menus...

GTMenuTitle 0,0,"Project"
GTMenuItem  0,0,0,0,"New Project","N"
GTMenuItem  0,0,0,1,"About Rogobo...","A"
GTMenuItem  0,0,0,2,"Quit Rogobo...","Q"

GTMenuTitle 0,1,"Rollovers"
GTMenuItem  0,0,1,0,"Create new...","C"
GTMenuItem  0,0,1,1,"Save HTML...","S"

; -------------------------------------------------------------------------

If Window (0,wx,wy,ww,wh,$20140e,"Rogobo",1,2)=0
  Request "Rogobo","Rogobo couldn't open its window!","Abort"
EndIf

AttachGTList 0,0                    ; attach gadgets
GTSetMenu 0                         ; attach gadtools menus

; -------------------------------------------------------------------------
.
.Funcs

; Functions & Statements

; (...and no, I can't be bothered commenting them!)

; -------------------------------------------------------------------------

; busy window:

Function.l LockWindow {win.l}
  lock.l=AllocMem_(SizeOf .Requester,1)
  If lock
    win=Peek.l(Addr Window(win))
    InitRequester_(lock)

    If Request_(lock,win)
      *Exec.Library=Peek.l(4)
      If *Exec\lib_Version=>39
        Dim tag.TagItem(1)
        tag(0)\ti_Tag=#WA_BusyPointer,-1
        tag(1)\ti_Tag=#TAG_END
        SetWindowPointerA_ win,&tag(0)
      EndIf
    Else
      FreeMem_ lock,SizeOf .Requester
      lock=0
    EndIf
          ; note - we'd normally free the "lock"
          ; memory here, but it's needed by the
          ; UnLockWindow {} function, which does
          ; free it!
  EndIf
  Function Return lock
End Function

; -------------------------------------------------------------------------

; un-busy window:

Statement UnlockWindow{win.l,lock.l}
  win=Peek.l(Addr Window(win))
  *Exec.Library=Peek.l(4)
  If *Exec\lib_Version=>39
    Delay_ 5
    Dim tag.TagItem(0)
    tag(0)\ti_Tag=#TAG_END
    SetWindowPointerA_ win,&tag(0)
  EndIf
  EndRequest_ lock,win
  FreeMem_ lock,SizeOf .Requester
End Statement

; -------------------------------------------------------------------------

; Exists () replacement (doesn't keep file locked!):

Function.l Exist {f$}
  lock.l=Lock_(&f$,#ACCESS_READ)

  If lock
    DEFTYPE .FileInfoBlock fib
    If Examine_(lock, fib) <> 0
      If fib\fib_DirEntryType < 0
        size=Peek.l(&fib\fib_Size)  ; file
      Else size=-1                  ; drawer
      EndIf
    Else size=-2 ; failed to examine file! rare occurence!
    EndIf
    UnLock_ lock
  Else size=0 ; failed to lock file (doesn't exist basically)...
  EndIf

Function Return size
End Function

; -------------------------------------------------------------------------

; Reqtools string requester, replaces Blitz's Enforcer-hitter:

Function$ RTGetString {title$,body$,deftext$,maxchars.l}

  body$=Replace$(body$,"|",Chr$(10))
  gadget$="OK|Cancel"
  #mx=9

  Dim ReqTags.TagItem(#mx)
  ReqTags(0)\ti_Tag   = #RT_Window,      Peek.l(Addr Window(Used Window))
  ReqTags(1)\ti_Tag   = #RTGS_GadFmt,    &gadget$
  ReqTags(2)\ti_Tag   = #RTGS_TextFmt,   &body$
  ReqTags(3)\ti_Tag   = #RT_ReqPos,      #REQPOS_POINTER
  ReqTags(4)\ti_Tag   = #RT_LockWindow,  -1
  ReqTags(5)\ti_Tag   = #RTGS_BackFill,  -1
  ReqTags(6)\ti_Tag   = #RTGS_Invisible, 0
  ReqTags(7)\ti_Tag   = #RTGS_Flags,     #GSREQF_CENTERTEXT
  ReqTags(#mx)\ti_Tag = #TAG_DONE

  DEFTYPE.rtReqInfo *reqinfo

  If Peek.l(&deftext$-4) > maxchars Then maxchars=Peek.l(&deftext$-4)

  res.l=maxchars/4
  While Frac(res)<>0
    maxchars+1
    res=maxchars/4
  Wend

  *mem=AllocMem_(maxchars,#MEMF_PUBLIC|#MEMF_CLEAR)
  If *mem
    Poke$ *mem,deftext$
    result.l=rtGetStringA_ (*mem,maxchars,&title$,*reqinfo,&ReqTags(0)\ti_Tag)
    ret$=Peek$ (*mem)
    FreeMem_ *mem,maxchars
    If result
      Function Return ret$
    Else Function Return ""
    EndIf
  EndIf

End Function

; say "hey, no more rollovers!":

Statement MaxOut {}
  lock.l=LockWindow {0}
  Request "Rogobo","Only 128 rollovers allowed!","Better save now"
  UnlockWindow {0,lock}
End Statement

; -------------------------------------------------------------------------

.
.Variables

; Main variables:

#maxrolls=127                   ; max number of rollovers (keep in a byte ;)

; newbies beware! if you increase this number,
; you need to know what you're doing with bytes,
; words, etc in the rest of the program!

NEWTYPE.Rollover                ; custom newtype to hold rollover data
  name$                         ;  - rollover name
  firstpic$                     ;  - main image
  secndpic$                     ;  - alternative image
End NEWTYPE

Dim rolls.Rollover (#maxrolls)  ; array of rollover buttons

norolls.b=0                     ; current rollover number

MaxLen p$=192                   ; setup ASL file requester path string
MaxLen f$=192                   ; setup ASL file requester filename string

p$="RAM:":f$=""                 ; start image path

; -------------------------------------------------------------------------
.
.MainLoop

; Hmm, quite a bit of duplicated code here... :/
; But who cares? :)

; Main loop:

Repeat

  Select WaitEvent              ; let's multitask!

    Case $200                   ; close gadget hit...quit

      If norolls>0
        lock.l=LockWindow {0}
        If Request ("Rogobo","Are you sure you want to quit?","Yes|No") Then UnlockWindow {0,lock}:End
        UnlockWindow {0,lock}
      Else End
      EndIf

    Case $100                   ; menu hit

      Select MenuHit

        Case $0                 ; Project menu

          Select ItemHit

            Case $0             ; New Project

              lock.l=LockWindow {0}
              If Request ("Rogobo","New Project||You will lose all existing rollovers!|Are you sure?","Yes|No")
                norolls=0 ; easy :)
                WTitle "Rogobo"
              EndIf
              UnlockWindow {0,lock}

            Case $1             ; About

              lock.l=LockWindow {0}
                a$="Rogobo:|"+Chr$(34)+"RollOver! GOod BOy!"+Chr$(34)+"||Javascript rollover generator"
                a$+"|by James L Boyd||Public Domain 2000."
                Request "Rogobo",a$,"OK"
              UnlockWindow {0,lock}

            Case $2             ; Quit

              If norolls>0
                lock.l=LockWindow {0}
                If Request ("Rogobo","Are you sure you want to quit?","Yes|No") Then UnlockWindow {0,lock}:End
                UnlockWindow {0,lock}
              Else End
              EndIf

          End Select

        Case $1                 ; Rollovers menu

          Select ItemHit

            Case $0             ; Create

              Gosub Create  ; GOSUBs?!! Are you the Devil-Child?!!

            Case $1             ; Save

              Gosub Save

          End Select

      End Select

; -------------------------------------------------------------------------

    Case $40                    ; gadget hit

      Select GadgetHit          ; but which one? do-de-do-do (Twilight Zone)

        Case $0 ; create new rollover

          Gosub Create

        Case $1 ; save sample HTML file with rollovers

          Gosub Save

        Case $2

          lock.l=LockWindow {0}
            a$="Rogobo:|"+Chr$(34)+"RollOver! GOod BOy!"+Chr$(34)+"||Javascript rollover generator"
            a$+"|by James L Boyd||Public Domain 2000."
            Request "Rogobo",a$,"OK"
          UnlockWindow {0,lock}

        Case $3

          If norolls>0
            lock.l=LockWindow {0}
            If Request ("Rogobo","Are you sure you want to quit?","Yes|No") Then UnlockWindow {0,lock}:End
            UnlockWindow {0,lock}
          Else End
          EndIf

      End Select
  End Select

; -------------------------------------------------------------------------

Forever

; -------------------------------------------------------------------------

; those GOSUBs in their true horror! look away! look away!

Create

  If norolls-1=127 Then MaxOut {}:Goto skiproll ; GOTO?!! AAAHH!! It is the Chosen One!!!
  ; ^^ "semi-hacky" way of keeping my byte :)
  ; basically, rollover limit reached...

    f$="" ; reset filename each time

    ; get rollover name:
    lock.l=LockWindow {0}
    a$=RTGetString {"Rogobo","Enter a name for this rollover:","Button"+Str$(norolls+1),30}
    UnlockWindow {0,lock}
    If a$="" Then Goto skiproll
    rolls (norolls)\name=a$     ; rollover name

    ; get first picture:
    lock.l=LockWindow {0}
    a$=ASLFileRequest$ ("Select main image:",p$,f$,"(#?.gif|#?.jp#?g|#?.png)")
    UnlockWindow {0,lock}
    If a$="" OR f$="" Then Goto skiproll
    a$="file://localhost/"+a$
    rolls (norolls)\firstpic=a$ ; main image name

    ; get second picture:
    lock.l=LockWindow {0}
    a$=ASLFileRequest$ ("Select alternative image:",p$,f$,"(#?.gif|#?.jp#?g|#?.png)")
    UnlockWindow {0,lock}
    If a$="" OR f$="" Then Goto skiproll
    a$="file://localhost/"+a$
    rolls (norolls)\secndpic=a$ ; alternative image name

    ; update window title:
    WTitle "Rogobo ["+Str$(norolls+1)+" created]","Rogobo"

    ; "Next!":
    norolls+1

    skiproll ; something was cancelled; keep norolls...

Return

Save


    If norolls=0
      lock.l=LockWindow {0}
      Request "Rogobo","You haven't created any rollovers!","Oh yeah..."
      UnlockWindow {0,lock}
      Goto skipsave
    EndIf

    overwriteloop ; back here in case of existing file

    f$="" ; let's not overwrite any image files :)

    lock.l=LockWindow {0} ; lock main window
    a$=ASLFileRequest$ ("Save HTML file as:",p$,f$,"#?")
    UnlockWindow {0,lock} ; unlock main window

    If a$="" OR f$="" Then Goto skipsave

    If Exist {a$}
      lock.l=LockWindow {0}
      If Request ("Rogobo","Overwrite existing file?","Yes|No")=0 Then UnlockWindow {0,lock}:Goto overwriteloop
      UnlockWindow {0,lock}
    EndIf

    If WriteFile (0,a$) ; let's output! rock 'n' roll! etc!

      lock.l=LockWindow {0} ; lock main window

      FileOutput 0      ; write to file 0

      ; HTML demo code:

; -------------------------------------------------------------------------
.
.HTMLDemo

      ; beginning of HTML file:

      NPrint ""
      NPrint "<!DOCTYPE HTML PUBLIC "+Chr$(34)+"-//W3C//DTD HTML 3.2//EN"+Chr$(34)+">"
      NPrint ""
      NPrint "<HTML>"
      NPrint ""
      NPrint "  <HEAD>"
      NPrint ""
      Print  "    <META HTTP-EQUIV="+Chr$(34)+"Content-Type"+Chr$(34)+" CONTENT="+Chr$(34); --> continued next line...
      NPrint "text/html;CHARSET=iso-8859-1"+Chr$(34)+">"
      NPrint ""
      NPrint "      <SCRIPT LANGUAGE="+Chr$(34)+"Javascript"+Chr$(34)+">"

; -------------------------------------------------------------------------

      ; list of images to use:

      For a.b=0 To norolls-1
        a$=rolls(a)\name
        NPrint ""
        NPrint "        "+a$+"up = new Image; "+a$+"up.src = "+Chr$(34)+rolls(a)\firstpic+Chr$(34)+";"
        NPrint "        "+a$+"dn = new Image; "+a$+"dn.src = "+Chr$(34)+rolls(a)\secndpic+Chr$(34)+";"
      Next a

; -------------------------------------------------------------------------

      ; mouseOut (main image - button up):

      NPrint ""
      NPrint "        function MyMouseOutRoutine(ButtonName)"
      NPrint "        {"

      For a.b=0 To norolls-1
        a$=rolls(a)\name
        NPrint "        if (ButtonName=="+Chr$(34)+a$+Chr$(34)+") {document."+a$+".src = "+a$+"up.src;}"
      Next a

      NPrint "        }"

; -------------------------------------------------------------------------

      ; mouseOver (alternative image - button down):

      NPrint ""
      NPrint "        function MyMouseOverRoutine(ButtonName)"
      NPrint "        {"

      For a.b=0 To norolls-1
        a$=rolls(a)\name
        NPrint "        if (ButtonName=="+Chr$(34)+a$+Chr$(34)+") {document."+a$+".src = "+a$+"dn.src;}"
      Next a

      NPrint "        }"

; -------------------------------------------------------------------------

      ; rest of <HEAD> section:

      NPrint ""
      NPrint "      </SCRIPT>"
      NPrint ""
      NPrint "      <TITLE>"
      NPrint "        Javascript rollover example page..."
      NPrint "      </TITLE>"
      NPrint ""
      NPrint "  </HEAD>"
      NPrint ""

; -------------------------------------------------------------------------

      ; <BODY> section:

      NPrint "  <BODY BGCOLOR="+Chr$(34)+"#FFFFFF"+Chr$(34)+" TEXT="+Chr$(34)+"#000000"+Chr$(34)+">"
      NPrint ""
      NPrint "    <CENTER>"
      NPrint ""

      ; column of rollovers:

      For a.b=0 To norolls-1
        a$=rolls(a)\name
        NPrint "        <!--"+a$+" rollover-->"
        NPrint "        <A HREF="+Chr$(34)+"index.html"+Chr$(34)
        NPrint "          onMouseOver="+Chr$(34)+"MyMouseOverRoutine('"+a$+"')"+Chr$(34)
        NPrint "          onMouseOut="+Chr$(34)+"MyMouseOutRoutine('"+a$+"')"+Chr$(34)+">"
        NPrint "          <IMG SRC="+Chr$(34)+rolls(a)\firstpic+Chr$(34)
        NPrint "          ALT="+Chr$(34)+a$+Chr$(34)+" NAME="+Chr$(34)+a$+Chr$(34)+">"
        NPrint "        </A><BR>"
        NPrint ""
      Next a

; -------------------------------------------------------------------------

      ; end of HTML file:

      NPrint "    </CENTER>"
      NPrint ""
      NPrint "  </BODY>"
      NPrint ""

      NPrint "</HTML>"
; -------------------------------------------------------------------------

      DefaultOutput:CloseFile 0
      UnlockWindow {0,lock} ; unlock main window

    EndIf

    skipsave

Return
