-- Hyperlink Behavior for custom hyperlink colors in #text members
-- ©03 alex da franca -- alex@farbflash.de

property pSprite
property pLastHL, pLastPress
property pHLColorNormal, pHLColorVisited, pHLColorOver, pHLStyle, pHLCursor, pHLColorActive

property pXtras


-- PUBLIC
-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- Example:

-- paramlist = [:]
-- paramlist.setaprop(#link, rgb(0,0,150))
-- paramlist.setaprop(#hover, rgb(0,0,200))
-- paramlist.setaprop(#active, rgb(200,0,0))
-- paramlist.setaprop(#visited, rgb(200,0,100))
-- paramlist.setaprop(#fontstsyle, [#underline])
-- paramlist.setaprop(#cursor, 280)

-- sendSprite(x, #mSetHyperLinkStyles, paramlist)


on mSetHyperLinkStyles me, paramList
  
  if ilk(paramList) <> #proplist then exit
  
  val = paramList.getaprop(#link)
  if ilk(val) = #color then pHLColorNormal = val
  
  val = paramList.getaprop(#hover)
  if ilk(val) = #color then pHLColorOver = val
  
  val = paramList.getaprop(#active)
  if ilk(val) = #color then pHLColorActive = val
  
  val = paramList.getaprop(#visited)
  if ilk(val) = #color then pHLColorVisited = val
  
  val = paramList.getaprop(#fontstsyle)
  if ilk(val) = #list then pHLStyle = val
  
  val = paramList.getaprop(#cursor)
  if ilk(val) = #integer then pHLCursor = val
  
  mInitLinkStyles me, pHLColorNormal, pHLColorVisited, pHLStyle
  
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

-- Example:

-- sendSprite(x, #mClearHyperlinkState)

on mClearHyperlinkState me
  
  textmember = pSprite.member
  if textmember.type <> #text then exit
  
  hl = textmember.hyperlinks
  repeat with thisHL in hl
    chunkRef = textmember.char[thisHL[1] .. thisHL[2]].ref
    chunkRef.hyperlinkstate = #normal
    chunkRef.color = pHLColorNormal
  end repeat
  
end


-- CALLBACK
-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mHyperLinkClicked me, data, range, linktext
  --  put data
  --  put linktext
  
  if mCheckForXtra(me, "BudAPI") then
    -- give BudAPI precedence before gotonetpage
    theMail = baOpenURL(data, "normal")
  else
    gotonetpage(data, "_blank")
  end if
  
end


-- STANDARD
-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on beginsprite me
  pSprite = sprite(me.spritenum)
  mInitLinkStyles me, pHLColorNormal, pHLColorVisited, pHLStyle
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on getPropertyDescriptionList
  retliste = [:]
  retliste[#pHLColorNormal] = [#format:#color, #default:rgb(0,0,150), comment:"Hyperlink color:"]
  retliste[#pHLColorVisited] = [#format:#color, #default:rgb(100,0,100), comment:"Visited Hyperlink color:"]
  retliste[#pHLColorOver] = [#format:#color, #default:rgb(0,0,255), comment:"Hyperlink rollover color:"]
  retliste[#pHLColorActive] = [#format:#color, #default:rgb(200,0,0), comment:"Hyperlink active color:"]
  retliste[#pHLStyle] = [#format:#list, #default:[#underline], comment:"Hyperlink fontstyle:"]
  retliste[#pHLCursor] = [#format:#integer, #default:280, comment:"Hyperlink cursor:"]
  return retliste
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mousewithin me
  if pSprite.pointinhyperlink(the mouseloc) then
    hlrange = pSprite.pointToChar(the mouseloc)
    if hlrange > 0 then hlrange = pSprite.member.char[hlrange].hyperlinkrange
    if hlrange <> pLastHL then
      if listP(pLastHL) then
        chunkRef = pSprite.member.char[pLastHL[1] .. pLastHL[2]].ref
        if chunkRef.hyperlinkstate = #visited then chunkRef.color = pHLColorVisited
        else chunkRef.color = pHLColorNormal
      end if
      pLastHL = hlrange
      if listP(hlrange) then
        pSprite.member.char[hlrange[1] .. hlrange[2]].color = pHLColorOver
        cursor pHLCursor
      end if
    end if
    
  else
    if listP(pLastHL) then
      chunkRef = pSprite.member.char[pLastHL[1] .. pLastHL[2]].ref
      if chunkRef.hyperlinkstate = #visited then chunkRef.color = pHLColorVisited
      else chunkRef.color = pHLColorNormal
      pLastHL = 0
      cursor 0
    end if
  end if
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mouseLeave me
  if listP(pLastHL) then
    chunkRef = pSprite.member.char[pLastHL[1] .. pLastHL[2]].ref
    if chunkRef.hyperlinkstate = #visited then chunkRef.color = pHLColorVisited
    else chunkRef.color = pHLColorNormal
    pLastHL = 0
    cursor 0
  end if
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mouseDown me
  if pSprite.pointinhyperlink(the clickloc) then
    hlrange = pSprite.pointToChar(the clickloc)
    hlrange = pSprite.member.char[hlrange].hyperlinkrange
    pLastPress = hlrange
    pSprite.member.char[hlrange[1] .. hlrange[2]].color = pHLColorActive
  end if
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mouseUp me
  if listP(pLastPress) then
    hlrange = pSprite.pointToChar(the mouseloc)
    hlrange = pSprite.member.char[hlrange].hyperlinkrange
    
    chunkRef = pSprite.member.char[pLastPress[1] .. pLastPress[2]].ref
    if hlrange = pLastPress then
      chunkRef.color = pHLColorVisited
      chunkRef.hyperlinkstate = #visited
      mHyperLinkClicked me, chunkRef.hyperlink, pLastPress, chunkRef.text
    else
      if chunkRef.hyperlinkstate = #visited then chunkRef.color = pHLColorVisited
      else chunkRef.color = pHLColorNormal
    end if
    pLastPress = 0
  end if
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mouseUpOutside me
  if listP(pLastPress) then
    chunkRef = pSprite.member.char[pLastPress[1] .. pLastPress[2]].ref
    if chunkRef.hyperlinkstate = #visited then chunkRef.color = pHLColorVisited
    else chunkRef.color = pHLColorNormal
    pLastPress = 0
    cursor 0
  end if
end


-- PRIVATE:
-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mInitLinkStyles me, colorNormal, colorVisited, theFontstyle
  
  textmember = pSprite.member
  if textmember.type <> #text then exit
  
  -- funny bug. we must set it twice:
  textmember.usehypertextstyles = 1
  textmember.usehypertextstyles = 0
  
  hl = textmember.hyperlinks
  repeat with thisHL in hl
    chunkRef = textmember.char[thisHL[1] .. thisHL[2]].ref
    if chunkRef.hyperlinkstate = #visited then chunkRef.color = colorVisited
    else chunkRef.color = colorNormal
    chunkRef.fontstyle = theFontstyle
  end repeat
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mCheckForXtra me, whichXtra
  if ilk(pXtras) <> #proplist then pXtras = [:]
  xtraPresent = pXtras.getaprop(whichXtra)
  if voidP(xtraPresent) then
    xtraPresent = 0
    numX = the number of xtras
    repeat with n = 1 to numX
      if (the name of xtra n = whichXtra) then
        xtraPresent = 1
        exit repeat
      end if
    end repeat
    pXtras.setaprop(whichXtra, xtraPresent)
  end if
  return xtraPresent
end
