Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Widgets - General questions or Mixed problems » Creating a hyperlink label
Creating a hyperlink label [message #41218] Fri, 15 November 2013 08:52 Go to next message
crydev is currently offline  crydev
Messages: 151
Registered: October 2012
Location: Netherlands
Experienced Member
Hello,

I'm trying to create a hyperlink label. I have a label that displays an URL using QTF, but I cannot catch the mouse click event to it. This is the code I have now, I have tried multiple mouse events, but I cannot find it out. How can I catch the left mouse click on the label?

class HyperlinkLabel : public Label
{
private:
	virtual void LeftUp(Point p, dword keyflags)
	{
		this->WhenLeftUp();
		Label::LeftUp(p, keyflags);
	}

public:
	Callback WhenLeftUp;
};


Thanks in advance!
Re: Creating a hyperlink label [message #41219 is a reply to message #41218] Fri, 15 November 2013 15:43 Go to previous messageGo to next message
Klugier is currently offline  Klugier
Messages: 1077
Registered: September 2012
Location: Poland, Kraków
Senior Contributor
Hello crydev,

You should definitly use LeftDown method instead of LeftUp. Here is exemplary code from my private control (LinkCtrl):

void LinkCtrl::LeftDown (Point p, dword keyflags) {
	for (int i = 0; i < linkFields.GetCount (); i++) {
		if (p.x >= linkFields[i].left && p.x <= linkFields[i].right) {
			if (p.y >= linkFields[i].top && p.y <= linkFields[i].bottom) {
				if (!WhenLink.Empty ()) {
					overrideCursor = false;
					WhenLink ();
					overrideCursor = true;
				}
			}
		}
	}
	
	return;
}


I am not sure that Label supports such functionality.But, you can always implement HyperlinkCtrl by your own. If you want to open web browser with specific url try following function:
LaunchWebBrowser(const String& url)


Sincerely,
Klugier


U++ - one framework to rule them all.

[Updated on: Fri, 15 November 2013 16:44]

Report message to a moderator

Re: Creating a hyperlink label [message #41220 is a reply to message #41218] Fri, 15 November 2013 17:33 Go to previous messageGo to next message
crydev is currently offline  crydev
Messages: 151
Registered: October 2012
Location: Netherlands
Experienced Member
Thank Klugier for your answer.

I know I can use the LaunchWebBrowser function to open the browser. I implemented that behind the callback.

The point is, the mouse callbacks are never called. Therefore I have reasons to think it is not implemented in a Label, but I am not sure. If it is not implemented in a Label, is there something else I can use?

Thanks in advance!
Re: Creating a hyperlink label [message #41221 is a reply to message #41220] Fri, 15 November 2013 20:20 Go to previous messageGo to next message
Klugier is currently offline  Klugier
Messages: 1077
Registered: September 2012
Location: Poland, Kraków
Senior Contributor
Hello,

I think the problem is with controls that inherit from LabelBase. It seems that some events are locked for this widgets set.

I have made simply test:
class HyperlinkLabel : public Label {
	typedef HyperlinkLabel CLASSNAME;

public:
	void LeftDown(Point p, dword keyflags) {
		Cout() << "LeftDown!\n"; 
	}
};


It dosen't work, but

class HyperlinkLabel : public Ctrl {
	typedef HyperlinkLabel CLASSNAME;

public:
	void LeftDown(Point p, dword keyflags) {
		Cout() << "LeftDown!\n"; 
	}
};


works as excepted.

For me, you have two options. The first one is to implement control based on RichTextView (It supports hyperlink via Qtf, so implementation should be easy). The second way more complex is created your own hyperlink control based on bare "Ctrl" class. The second way is harder, but can give you additional customization options for instance link hoover.

Sincerely,
Klugier


U++ - one framework to rule them all.

[Updated on: Sat, 16 November 2013 00:32]

Report message to a moderator

Re: Creating a hyperlink label [message #41232 is a reply to message #41218] Sun, 17 November 2013 12:05 Go to previous messageGo to next message
crydev is currently offline  crydev
Messages: 151
Registered: October 2012
Location: Netherlands
Experienced Member
Thanks Klugier,

I now solved that problem the following way, allowing me to set the LaunchWebBrowser call as WhenLeftDown callback:

// Represents a label that is clickable, for example, to execute a hyperlink.
class HyperlinkLabel : public ParentCtrl
{
private:
	Label mLinkLabel;

	virtual void LeftDown(Point p, dword keyflags) { this->WhenLeftDown(); }
public:
	Callback WhenLeftDown;
	
	HyperlinkLabel() { *this << this->mLinkLabel.SizePos(); }
	
	HyperlinkLabel& SetLabel(const char* lbl) { this->mLinkLabel.SetLabel(lbl); return *this; }
};


This works, but one question remains. I have already tried a few things, for example, overriding MouseEvent, and using a RectTracker, but none really seemed to work for me. I would like to change the cursor to a hand while the mouse is on top of the control.

Thanks in advance!
Re: Creating a hyperlink label [message #41233 is a reply to message #41232] Sun, 17 November 2013 12:35 Go to previous messageGo to next message
Klugier is currently offline  Klugier
Messages: 1077
Registered: September 2012
Location: Poland, Kraków
Senior Contributor
Hello Crydev,

You should definitly override "CursorImage" method:
class MyLinkCtrl : public Ctrl {
public:
   virtual Image CursorImage(Point p, dowrd keyflags) {
      bool isLink = true;
      // Here you can write hyperlink detection code if you want to.
      if (isLink)
          return Image::Hand();
      else
          return Image::Arrow();
   }
}


Sincerely,
Klugier


U++ - one framework to rule them all.
Re: Creating a hyperlink label [message #41235 is a reply to message #41218] Sun, 17 November 2013 17:27 Go to previous messageGo to next message
crydev is currently offline  crydev
Messages: 151
Registered: October 2012
Location: Netherlands
Experienced Member
Thanks Klugier,

That is a very easy and effective way. Thank you for your help, the issue is resolved. For other users, the class' code is below. Smile

class HyperlinkLabel : public ParentCtrl
{
private:
	Label mLinkLabel;
	
	virtual Image CursorImage(Point p, dword keyflags) { return Image::Hand(); }
	
	virtual void LeftDown(Point p, dword keyflags) { this->WhenLeftDown(); }
public:
	Callback WhenLeftDown;
	
	HyperlinkLabel() { *this << this->mLinkLabel.SizePos(); }
	
	HyperlinkLabel& SetLabel(const char* lbl) { this->mLinkLabel.SetLabel(lbl); return *this; }
};
Re: Creating a hyperlink label [message #43755 is a reply to message #41235] Fri, 03 October 2014 09:31 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3374
Registered: August 2008
Senior Veteran
Hello all

If you do not mind I would like to include it in Controls4U.

.cpp/.h
class HyperlinkText : public ParentCtrl {
public:
	HyperlinkText() {
		label.SetInk(LtBlue());
		*this << label.SizePos();
	}
	HyperlinkText& SetText(const char* txt) {
		label.SetText(txt); 
		return *this;
	}
	HyperlinkText& SetHyperlink(const char* str) {
		hyperlink = str; 
		return *this; 
	}

private:
	StaticText label;
	String hyperlink;
	virtual Image CursorImage(Point p, dword keyflags) {
		return Image::Hand();
	}
	virtual void LeftDown(Point p, dword keyflags) {
		LaunchWebBrowser(hyperlink);
	}
};

.usc
ctrl HyperlinkText {
	group "Static";

	GetMinSize() { return XMinSize(); }
	GetStdSize() { sz = XMinSize(); sz.cy += 6; sz.cx *= 5; return sz; }

	Doc    SetText ? "Label of control" ;
	Doc    SetHyperlink ? "Hyperlink" ;
	Align  SetAlign = ALIGN_LEFT;
	Font   SetFont = StdFont();
	Frame  SetFrame @1;

	ViewRect(w) {
		r = GetRect();
		DrawCtrlFrame(w, r, .SetFrame);
		return r;
	}
	ViewSize(w) {
		r = ViewRect(w);
		return Size(r.right - r.left, r.bottom - r.top);
	}

	Paint(w) {
		sz = ViewSize(w);
		textsize = GetTextSize(.SetText, .SetFont);
		px = 0;
		if(.SetAlign == "ALIGN_CENTER")
			px = (sz.cx - textsize.cx) / 2;
		if(.SetAlign == "ALIGN_RIGHT")
			px = sz.cx - textsize.cx;
		w.DrawText(px, (sz.cy - textsize.cy) / 2, .SetText, .SetFont, :SLtBlue);
	}
	Sample() {
		.SetText = "Text";
		.SetFont = Arial(10).Bold().Italic();
	}
};


Best regards
Iñaki
Re: Creating a hyperlink label [message #43792 is a reply to message #43755] Mon, 13 October 2014 12:00 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13986
Registered: November 2005
Ultimate Member
Note: The reason why it does not work with Label is that Label (as most static widgets) have "IgnoreMouse" active. The reason for that is that labels (and especially LabelBox) often overlap with other widgets and they would be stealing the mouse input for these...

Calling NoIgnoreMouse in constructor would fix that.

Final note: If you need hyperlink, you might be happier using RichTextCtrl instead of Label...

[Updated on: Mon, 13 October 2014 12:02]

Report message to a moderator

Re: Creating a hyperlink label [message #43796 is a reply to message #43792] Tue, 14 October 2014 09:57 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3374
Registered: August 2008
Senior Veteran
Thank you Mirek, you are right. New implementation is:

class HyperlinkLabel : public Label {
public:
	HyperlinkLabel() {
		NoIgnoreMouse();
		SetInk(LtBlue());
	}
	HyperlinkLabel& SetHyperlink(const char* str) 		{hyperlink = str; return *this;}

private:
	String hyperlink;
	virtual Image CursorImage(Point p, dword keyflags) 	{return Image::Hand();}
	virtual void LeftDown(Point p, dword keyflags) 		{LaunchWebBrowser(hyperlink);}
};

Quote:
Final note: If you need hyperlink, you might be happier using RichTextCtrl instead of Label...
IMHO Rolling Eyes it is very handy to have an hyperlink in a Label, mainly included in .usc so it can be filled directly in the Layout editor with no code. It could be in the Label too Rolling Eyes Rolling Eyes


Best regards
Iñaki
Re: Creating a hyperlink label [message #44361 is a reply to message #41218] Sun, 01 March 2015 02:31 Go to previous messageGo to next message
Edward is currently offline  Edward
Messages: 34
Registered: February 2015
Location: United States
Member
Many years back when there was a handy little guy named HyperLabel.OCX. The URL was placed in a field, font style selected, then click, click.
I still have it in older projects I'm currently updating, and trying not to loose features.

Quote:
IMHO Rolling Eyes it is very handy to have an hyperlink in a Label, mainly included in .usc so it can be filled directly in the Layout editor with no code. It could be in the Label too Rolling Eyes Rolling Eyes


I second the motion Koldo!!
We're in the 2Ks now this could actually be a reality, like dropping an imageCTRL on a layout...


My mission is to find a powerful(Non MS) C++ IDE w/GUI to marry and spend the rest of my life with...

[Updated on: Sun, 01 March 2015 02:44]

Report message to a moderator

Re: Creating a hyperlink label [message #44362 is a reply to message #44361] Sun, 01 March 2015 08:12 Go to previous message
mirek is currently offline  mirek
Messages: 13986
Registered: November 2005
Ultimate Member
Edward wrote on Sun, 01 March 2015 02:31
Many years back when there was a handy little guy named HyperLabel.OCX. The URL was placed in a field, font style selected, then click, click.
I still have it in older projects I'm currently updating, and trying not to loose features.

Quote:
IMHO Rolling Eyes it is very handy to have an hyperlink in a Label, mainly included in .usc so it can be filled directly in the Layout editor with no code. It could be in the Label too Rolling Eyes Rolling Eyes


I second the motion Koldo!!
We're in the 2Ks now this could actually be a reality, like dropping an imageCTRL on a layout...


Ah, OK. The only thing that is missing is actually change in .usc that would allow the text of RichTextCtrl to be edited. Adding RM task.

The reason why it was not done yet is that a) it is not needed so often b) it is trivial to set text in code c) whenever I needed it, the text was variable and had to be created in the code anyway.
Previous Topic: Using a Button to load a window/form using Run. THISBACK syntax
Next Topic: Win32: custom WM_XXXXX message processing
Goto Forum:
  


Current Time: Fri Jun 21 23:50:29 CEST 2024

Total time taken to generate the page: 0.02003 seconds