134. How to change cursor over clickable GtkLabel - Part 3 - using xpm data?

Problem

In Part 1, we changed the cursor using php-gtk's pre-defined cursors. In Part 2, we changed the cursor using standard image files.

Suppose you have a huge collection of icons in xpm format. You would like to set the cursor using these xpm images as shown below:

How to change cursor over clickable GtkLabel - Part 3 - using xpm data?


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   
45   
46   
48   
49   
50   
51   
52   
53   
54   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
79   
80   
81   
82   
83   
84   
85   
86   
87   
88   
89   
90   
91   
92   
93   
94   
95   
96   
97   
98   
99   
100   
101   
102   
103   
104   
105   
108   
109   
110   
113   
116   
118   
119   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 200);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// displays a title
$title = new GtkLabel("Change cursor over clickable link - Part 3\n".
"                       using xpm data");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);
$vbox->pack_start(new GtkLabel());

$link1 = create_link("manual",
    "http://gtk.php.net/manual/en/gtkclasses.php");
$link2 = create_link("mailing list",
    "http://www.nabble.com/Php---GTK---General-f171.html");

$hbox = new GtkHBox();
$hbox->pack_start(new GtkLabel("reference: php-gtk2 "), false);
$hbox->pack_start($link1, false);
$hbox->pack_start(new GtkLabel(" and "), false);
$hbox->pack_start($link2, false);
$vbox->pack_start($hbox, false);
$vbox->pack_start(new GtkLabel());

$status = new GtkLabel();
$status->set_alignment(0, 0);
$vbox->pack_start($status, 0, 0);

// function to setup the link
function create_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', 'on_click', $title, $url);
    $eventbox->connect('enter-notify-event', 'on_enter', $title, $url);
    $eventbox->connect('leave-notify-event', 'on_leave', $title, $url);

    return $eventbox;
}

function on_click($widget, $event, $title, $url) {
    $shell = new COM('WScript.Shell'); // note 6
    $shell->Run('cmd /c start "" "' . $url . '"', 0, FALSE); // note 6
    unset($shell); // note 6
}

function on_enter($widget, $event, $title, $url) {
    global $status;
    $status->set_text($url);
}

function on_leave($widget, $event, $title, $url) {
    global $status;
    $status->set_text('');
}

$window->show_all();

$xpm_data = array("22 18 17 1", // note 1
".     c None",
"+     c #3852A4",
"@     c #37569C",
"#     c #3753A1",
"$     c #37589A",
"%     c #37559F",
"&     c #35628C",
"*     c #365F91",
"=     c #346C7E",
"-     c #35608E",
";     c #346785",
">     c #346882",
",     c #356389",
"'     c #346A80",
")     c #365B95",
"!     c #365A98",
"~     c #356587",
" .....................",
".....++@++............",
"...++#++#+#+..........",
"..+++....+++..........",
"..#+.......#+.........",
".+#........+#.........",
".+$.........%+........",
".+%.........$+........",
".+#........+#.........",
".+#+.......#++........",
"..+#+.....+%%&$+......",
"...+#++.++#+*==-#.....",
"....+#%+#++.@;==>@+...",
".............#-===*+..",
"..............+$>==;@+",
"................#,==')",
".................+!'=~",
"..................+#*@");

$pixbuf = GdkPixbuf::new_from_xpm_data($xpm_data); // note 2
$display = $link1->get_display(); // note 3
$cursor = GdkCursor::new_from_pixbuf($display, $pixbuf, 7, 7); // note 4
$link1->window->set_cursor($cursor); // note 5
$link2->window->set_cursor($cursor); // note 5

Gtk::main();
?>

Output

As shown above.
 

Explanation

This example make use of the code in Part 1.

What's new here:

  1. Define the image in xpm format. Note how xpm data are defined in php as an array of strings.
  2. Create a new pixbuf from the xpm data.
  3. Get the GdkDisplay.
  4. Create the cursor from the pixbuf. Note that the third and fourth parameters are the horizontal and vertical offset of the 'hotspot' of the cursor. In this case we set it at (7,7) which is the center of the maginifying glass.
  5. Set the cursor for the two clickable links.
  6. Note that if you're running this example on linux, please comment out these 3 lines and replace with the system command to launch your favorite browser.

Note

  • Take note of where the three lines are located (note 1 and 2). If you set the cursor before the widgets are realized, you will get "Fatal error: Call to a member function set_cursor() on a non-object."

Related Links

Add comment


Security code
Refresh