hyperlink.bmx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. Strict
  2. Import MaxGUI.MaxGUI
  3. ' THyperlinkGadget Proxy Gadget
  4. ' Author: Seb Hollington
  5. Rem
  6. bbdoc: Creates a basic hyperlink gadget that opens the specified @url$ in the default browser when clicked.
  7. about: The underlying gadget is a label, and so the @style parameter can take all the #CreateLabel flags apart from LABEL_SEPARATOR.
  8. The normal and roll-over text color can be set individually using #SetGadgetTextColor and #SetGadgetColor respectively.
  9. The optional @customtext$ parameter allows you to set user-friendly text that masks the URL in the label. If this is specified in #CreateHyperlink
  10. then the label's tooltip is automatically set to the URL the link refers to. This masking text can be changed at any time by calling #SetGadgetText.
  11. Finally, the @url$ that the hyperlink gadget opens can be modified/retrieved using #SetGadgetExtra and String( #GadgetExtra )
  12. respectively (see code example).
  13. End Rem
  14. Function CreateHyperlink:TGadget( url$,x,y,w,h,group:TGadget,style=0,customtext$ = "" )
  15. Return New THyperlinkGadget.Create(url,x,y,w,h,group,style,customtext)
  16. EndFunction
  17. Type THyperlinkGadget Extends TProxyGadget
  18. Global lstHyperlinkGadgets:TList
  19. Field fnt:TGuiFont, fntHover:TGuiFont
  20. Field hyperlinkstyle%
  21. Field colors[][] = [[0,0,255],[255,0,0]]
  22. Field lastclick[] = [-1, -1]
  23. Method New()
  24. If Not lstHyperlinkGadgets Then Initialize()
  25. Local r:Byte, g:Byte, b:Byte
  26. LookupGuiColor( GUICOLOR_LINKFG, r, g, b )
  27. colors[0] = [Int r, Int g, Int b]
  28. EndMethod
  29. Method Create:THyperlinkGadget(pUrl$,x,y,w,h,group:TGadget,style,customtext$)
  30. If Not customtext Then customtext = pUrl$
  31. If (style&LABEL_SEPARATOR) = LABEL_SEPARATOR Then style:&~LABEL_SEPARATOR
  32. Local tmpLabel:TGadget = CreateLabel( customtext, x, y, w, h, group, style&31 )
  33. If Not tmpLabel Then Return Null Else SetGadgetSensitivity(tmpLabel, SENSITIZE_MOUSE)
  34. fnt = LookupGuiFont( GUIFONT_SYSTEM, 0, 0 )
  35. fntHover = LookupGuiFont( GUIFONT_SYSTEM, 0, FONT_UNDERLINE )
  36. SetGadgetFont(tmpLabel,fnt)
  37. If customtext <> pUrl Then SetGadgetToolTip( tmpLabel, pUrl )
  38. SetProxy( tmpLabel );Super.SetTextColor(colors[0][0], colors[0][1], colors[0][2])
  39. hyperlinkstyle = style;extra = pUrl
  40. lstHyperlinkGadgets.AddLast Self
  41. Return Self
  42. EndMethod
  43. Method EventHook:TEvent( pEvent:TEvent )
  44. Select pEvent.id
  45. Case EVENT_MOUSEENTER
  46. Super.SetTextColor(colors[1][0], colors[1][1], colors[1][2]);SetPointer(POINTER_HAND)
  47. Super.SetFont( fntHover )
  48. Case EVENT_MOUSELEAVE
  49. Super.SetTextColor(colors[0][0], colors[0][1], colors[0][2]);SetPointer(POINTER_DEFAULT)
  50. Super.SetFont( fnt )
  51. Case EVENT_MOUSEDOWN;If lastclick[0] <> pEvent.x Or lastclick[1] <> pEvent.y Then lastclick = [pEvent.x,pEvent.y];OpenURL(String(extra))
  52. EndSelect
  53. Return Null
  54. EndMethod
  55. Method SetFont( font:TGuiFont )
  56. Super.SetFont(font)
  57. fnt = font
  58. fntHover = font
  59. EndMethod
  60. Method SetColor(r,g,b)
  61. colors[1][0] = r;colors[1][1] = g;colors[1][2] = b
  62. EndMethod
  63. Method SetTextColor(r,g,b)
  64. colors[0][0] = r;colors[0][1] = g;colors[0][2] = b
  65. Super.SetTextColor(colors[0][0], colors[0][1], colors[0][2])
  66. EndMethod
  67. Method CleanUp()
  68. lstHyperlinkGadgets.Remove(Self)
  69. Super.CleanUp()
  70. EndMethod
  71. Function Initialize()
  72. lstHyperlinkGadgets = New TList
  73. AddHook EmitEventHook, eventHandler, Null, -1
  74. EndFunction
  75. Function eventHandler:Object( pID%, pData:Object, pContext:Object )
  76. Local pEvent:TEvent = TEvent(pData)
  77. If pEvent Then
  78. For Local tmpHyperlinkGadget:THyperlinkGadget = EachIn lstHyperlinkGadgets
  79. If tmpHyperlinkGadget = pEvent.source Then Return tmpHyperlinkGadget.EventHook( pEvent )
  80. Next
  81. EndIf
  82. Return pData
  83. EndFunction
  84. EndType