Sample Code 16: How to put a clickable link in GtkLabel - Part 2?
Written by kksou   
Friday, 15 September 2006
Problem

You want to put a clickable link in GtkLabel. In Part 1, you have the clickable link displayed.

However, as shown in the figure below, you would like to have some message being displayed (e.g. the url) in a status area at the bottom of the window when the user hovers over the link (similar to a web browser).

How to put a clickable link in GtkLabel - Part 2?


Solution

Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 300);

$window->connect_simple('destroy', array('Gtk','main_quit'));

$window->add($vbox = new GtkVBox());

// displays a title
$title = new GtkLabel();
$title->set_markup('<span color="blue" font_desc="Times New Roman Italic 12">
    Clilckable Link</span>');
$title->set_size_request(-1, 80);
$title->set_alignment(0.5, 0.5);
$vbox->pack_start($title, 0, 0); // note 7

// create the clickable label
$clickable_label = new GtkHBox();
$clickable_label->set_size_request(-1, 24);
$vbox->pack_start($clickable_label,0 ,0);
$clickable_label->pack_start(new GtkLabel("reference: php-gtk2 "), 0, 0);
$clickable_label->pack_start(make_link("manual", "http://gtk.php.net/manual/en/gtkclasses.php"), 0, 0);
$clickable_label->pack_start(new GtkLabel(" and "), 0, 0);
$clickable_label->pack_start(make_link("mailing list", "http://www.nabble.com/Php---GTK---General-f171.html"), 0, 0);

$vbox->pack_start(new GtkLabel('')); // note 6

$status = new GtkLabel(''); // note 1
$status->set_alignment(0, 0);
$vbox->pack_start($status, 0, 0);

// function to setup the link
function make_link($title, $url) {
    $label = new GtkLabel($title);
    $label->set_markup('<span color="blue"><u>'.$title."</u></span>");
    $eventbox = new GtkEventBox;
    $eventbox->add($label);
    $eventbox->connect('button-press-event', 'link_clicked', $title, $url);
    $eventbox->connect('enter-notify-event', 'link_hover', $title, $url); // note 2
    $eventbox->connect('leave-notify-event', 'link_leave', $title, $url); // note 3
    return $eventbox;
}

// call-back function when user click on the link
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation

Please refer to Part 1 first which explains how to display a clickable link in GtkLabel.

What's new here in Part 2:

  1. Sets up the status area
  2. This basically says call the function "link_hover" when the mouse enters the link. Note that we also pass along two user-defined data ($title and $url) when the function is being called.
  3. This says call the function "link_leave" when the mouse leaves the link.
  4. This is the "link_hover" function, which simply displays the $url in the status area when the mouse hovers over the link.
  5. This is the "link_leave" function, which simply clears the status area when the mouse leaves the link.

Notes
Notes on layout:
  1. Try to enlarge the window. You can see that the status area will always stay at the bottom of the window. Observe how we achieve this with just this one statement:
  2. $vbox->pack_start(new GtkLabel(''));
    
  3. Also note how we "force" the title to stay up there with:
  4. $title->set_size_request(-1, 80);
    $title->set_alignment(0.5, 0.5);
    $vbox->pack_start($title, 0, 0);
    

    The set_alignment ensures that there's some vertical gap between the title and the top of the margin. Without this, you will find that the title will "stick" to the top margin.


Related Links

User reviews   Average user ratings:    0.0   (from 2 users)
  1. Marc from France
    March 05, 2007 6:56am

    There is a problem with link() function as it is a reserved function that make link. So just rename it to make_link() or some think like that.

    This tutorial is good. Thank.

  2. kksou
    March 05, 2007 7:48am

    Thanks for pointing this out, Marc! Have updated this example and the previous one (Part 1).

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved