CreateHyperLink suggested changes

BlitzMax Forums/BlitzMax GUI Programming/CreateHyperLink suggested changes

I just noticed this aspect of CreateHyperLink (taken from the help page...)

The optional customtext$ parameter allows you to set friendly text that is displayed instead of the URL in the label. If this is set then the label's tooltip is automatically generated with the URL the link refers to. Although this masking text can be changed at any time by calling SetGadgetText, the url$ that the hyperlink gadget opens cannot be altered once the gadget has been created.



As I don't like the last sentence (and I want to be free to change the URL without destroying and recreate a new gadget) I suggest this little change to the CreateHyperLink() code
	Method SetText(_url$) 
		url = _url
		Super.settext(_url) 
	End Method

	Method SetToolTip(_url$) 
		proxy.settooltip(_url) 
	End Method


This will allow to change the URL via a SetGadgetText command. The tooltip (if any) must be defined with SetGadgetToolTip()

Strict

Import MaxGUI.Drivers
Import MaxGUI.ProxyGadgets

AppTitle = "Hyperlink Test Window"

Global wndMain:TGadget = CreateWindow( AppTitle, 100, 100, 300, 59, Null, WINDOW_TITLEBAR|WINDOW_CLIENTCOORDS|WINDOW_STATUS )
	
	'Standard Hyperlink Gadget
	Global hypLeft:TGadget = CreateHyperlink( "http://www.blitzbasic.com/", 2, 2, ClientWidth(wndMain)-4, 15, wndMain, LABEL_LEFT )
	
	'Center Aligned Hyperlink Gadget with alternate text
	Global hypCenter:TGadget = CreateHyperlink( "http://www.blitzbasic.com/", 2, 21, ClientWidth(wndMain)-4, 17, wndMain, LABEL_CENTER|LABEL_FRAME, "Alternate Text" )
	
	'Right Aligned Sunken Hyperlink Gadget with custom rollover colors set
	Global hypRight:TGadget = CreateHyperlink( "http://www.blitzbasic.com/", 2, 42, ClientWidth(wndMain)-4, 15, wndMain, LABEL_RIGHT, "Custom Rollover Colors" )
		SetGadgetTextColor(hypRight,128,128,128)	'Set normal text color to grey.
		SetGadgetColor(hypRight,255,128,0)			'Set rollover color to orange.

	SetGadgetText hypCenter , "http://www.graphio.net" '<---- change the URL (keep the same initial tooltip)
	'SetGadgetToolTip hypCenter,"my site" ' <---- change the tooltip

Repeat
	
	WaitEvent()
	
	SetStatusText wndMain, CurrentEvent.ToString()
	
	Select EventID()
		Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE;End
	EndSelect
	
Forever



I think in this way the HyperLink gadget is more usable

ps: wow! I just noticed there is a new CreateSplitter gadget!

I just noticed that my suggested changes have a negative impact as you can not more use the 'alternate text' (if any).
So there is a complete rewrite of HyperLink.bmx
Strict

Import MaxGUI.MaxGUI

Rem
bbdoc: Creates a basic hyperlink gadget that opens the specified @url$ in the default browser when clicked.
about: The underlying gadget is a label, and so the @style parameter can take all the #CreateLabel flags apart from LABEL_SEPARATOR.

Both the default and roll-over text color can be set using #SetGadgetTextColor and #SetGadgetColor respectively.

The optional @customtext$ parameter allows you to set friendly text that is displayed instead of the URL in the label. If this is set
then the label's tooltip is automatically generated with the URL the link refers to. Although this masking text can be changed at any
time by calling #SetGadgetText, the @url$ that the hyperlink gadget opens cannot be altered once the gadget has been created.
End Rem
Function CreateHyperlink:TGadget( url$,x,y,w,h,group:TGadget,style=0,customtext$ = "" )
	
	Return New THyperlinkGadget.Create(url,x,y,w,h,group,style,customtext)
	
EndFunction

Type THyperlinkGadget Extends TProxyGadget
	
	Global lstHyperlinkGadgets:TList
	
	Global fntDefault:TGuiFont

	Field tmpLabel:tgadget ' --- now we keep the TmpLabel to further changes
	Field hyperlinkstyle%, url$
	Field colors[][] = [[0,0,255],[255,0,0]]
	
	Field lastclick[] = [-1, -1]
	
	Method New()
		If Not lstHyperlinkGadgets Then Initialize()
	EndMethod
	
	Method Create:THyperlinkGadget(pUrl$,x,y,w,h,group:TGadget,style,customtext$)
		
		If Not customtext Then customtext = pUrl$
		If (style&LABEL_SEPARATOR) = LABEL_SEPARATOR Then style:&~LABEL_SEPARATOR
		
		tmpLabel:TGadget = CreateLabel( customtext, x, y, w, h, group, style&31 )
		If Not tmpLabel Then Return Null Else SetGadgetSensitivity(tmpLabel, SENSITIZE_MOUSE)
		
		SetGadgetFont(tmpLabel,fntDefault)		'Let's underline our hyperlink
		
		If customtext <> pUrl Then SetGadgetToolTip( tmpLabel, pUrl )
		
		SetProxy( tmpLabel ) 
		Super.SetTextColor(colors[0][0], colors[0][1], colors[0][2])
		
		hyperlinkstyle = style
		url = pUrl
		lstHyperlinkGadgets.AddLast Self
		
		Return Self
		
	EndMethod
	
	Method EventHook:TEvent( pEvent:TEvent )
		
		Select pEvent.id
		
			Case EVENT_MOUSEENTER;Super.SetTextColor(colors[1][0], colors[1][1], colors[1][2]);SetPointer(POINTER_HAND)
			Case EVENT_MOUSELEAVE;Super.SetTextColor(colors[0][0], colors[0][1], colors[0][2]);SetPointer(POINTER_DEFAULT)
			Case EVENT_MOUSEDOWN;If lastclick[0] <> pEvent.x Or lastclick[1] <> pEvent.y Then lastclick = [pEvent.x,pEvent.y];OpenURL(url)
		
		EndSelect
		
		Return Null
	
	EndMethod
	
	Method SetText(_url$) 
		url = _url ' --- we change ONLY the url (not the text)
	End Method
	
	Method SetToolTip(_url$) 
		tmpLabel.settooltip(_url) '--- we refer to the label
	End Method

	Method SetColor(r,g,b)
		colors[1][0] = r;colors[1][1] = g;colors[1][2] = b
	EndMethod
	
	Method SetTextColor(r,g,b)
		colors[0][0] = r;colors[0][1] = g;colors[0][2] = b
		Super.SetTextColor(colors[0][0], colors[0][1], colors[0][2])
	EndMethod
	
	Method Free()
		lstHyperlinkGadgets.Remove(Self)
		Super.Free()
	EndMethod
	
	Function Initialize()
		lstHyperlinkGadgets = New TList
		AddHook EmitEventHook, eventHandler, Null, 1
		fntDefault = LookupGuiFont( GUIFONT_SYSTEM, 0, FONT_UNDERLINE )
	EndFunction
	
	Function eventHandler:Object( pID%, pData:Object, pContext:Object )
		Local pEvent:TEvent = TEvent(pData)
		
		If pEvent Then
			For Local tmpHyperlinkGadget:THyperlinkGadget = EachIn lstHyperlinkGadgets
				If tmpHyperlinkGadget.proxy = pEvent.source Then Return tmpHyperlinkGadget.EventHook( pEvent )
			Next
		EndIf
		
		Return pData
	EndFunction
	
EndType


With SetGadgetText() it is possibile to change ONLY the URL, the AlternateText still remain. (Unless we use SetGadgetToolTip to change both ToolTip and alternate_text...)

edit:

I'm playing with SetGadgetExtra(gadget,object). At the moment I can change everything (URL, tooltip, label) with standard MaxGUI commands.

edit2:

A third - not conventional & strange - method I tested

	Method SetText(_url$) 
		
		If _URL.find("||")<0
	             	url = _url
		        If String(Self.extra)<>"" Super.settext(String(Self.extra) ) 
		Else
		        Local turl$[2]
			turl= _url.split("||") 
			If turl[1] <> "" Super.settext turl[1]
			url=turl[0]
	        End If
	
	End Method


SetGadgetText hypCenter,"www.graphio.net||To my site"

If there is not "||" SetGadgetText changes the only the URL, else the string is splitted in two parts: the URL and the new Label/Text.

I just noticed that my suggested changes have a negative impact as you can not more use the 'alternate text' (if any).

Exactly the reason why I implemented that behaviour.

I like the SetGadgetExtra() idea though... I'll take a look at some point, but I think I might keep the current behaviour 'as is' (with SetGadgetText() affecting the displayed text), and use SetGadgetExtra() to set the URL.

Sounds fair enough?

Perfect.
What is important it's a way to allow the user to change the URL.

I've just committed a new version (rev. 84).

You should now be able to set the URL using SetGadgetExtra() and retrieve it using String( GadgetExtra() ).

Let me know if this is OK...

Great. It works perfectly.
Thanks.

ps: semi-related problem

I've tested on MacOS X (PPC - 10.3.9) the old and the new version of CreateHyperLink() gadget, and I get the same problem: OpenURL doesn't open the brower (the default one Firefox, neither Safari). If FF is already opened clicking on a HyperLink gadget works as expected.

I just checked with a simple program
OpenURL("http://www.google.com")

but neither this works.
So the problem is not in MaxGUI, but resides in the OpenURL command.