Problem
You would like to set up a clickable link using the new widget GtkLinkButton available in gtk+2.10 and above as shown below:
Solution
- Instead of using the standard
connect('clicked', call_back_fn)
to capture button clicks on each of the GtkLinkButton, we can set up a global hook withGtkLinkButton::set_uri_hook()
that will capture button clicks for ALL GtkLinkButtons in an application. - The advantage of using this method is that you do not need to explicitly set up
connect('clicked')
for each of the link button. - Note that if you intend to use
GtkLinkButton::set_uri_hook()
, when creating the GtkLinkButton, you have to use another constructornew GtkLinkButton($url, $label)
where$url
is the url of the link, andlabel
is the label for the link button.
Important Note: This only works for PHP-GTK2 compliled with gtk+ v2.10 and above. If you are using an older version, for linux, you may follow the step-by-step instructions to recompile php-gtk2 with gtk+ v2.10. For windows, please refer to How to install php gtk2 on windows? You may also want to take a look here to see some of the new exciting PHP-GTK2 Functionalities.
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 38 39 40 41 42 43 | <?php $window = new GtkWindow(); $window->set_title($argv[0]); $window->set_size_request(400, 150); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox()); // display title $title = new GtkLabel("Set up clickable link using GtkLinkButton\n". "Part 2 - using set_uri_hook()"); $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); $title->set_justify(Gtk::JUSTIFY_CENTER); $alignment = new GtkAlignment(0.5, 0, 0, 0); $alignment->add($title); $vbox->pack_start($alignment, 0, 0); $vbox->pack_start(new GtkLabel(), 0, 0); // link button $hbox = new GtkHBox(); $vbox->pack_start($hbox, false, false); $link_button1 = new GtkLinkButton( // note 1 "http://gtk.php.net/manual/en/gtkclasses.php", "manual"); $link_button2 = new GtkLinkBUtton( "http://www.nabble.com/Php---GTK---General-f171.html", "mailing list"); $hbox->pack_start(new GtkLabel("Reference: php-gtk2"), false); $hbox->pack_start($link_button1, false); $hbox->pack_start($link_button2, false); GtkLinkButton::set_uri_hook('on_linkbutton'); // note 2 $window->show_all(); Gtk::main(); function on_linkbutton($linkbutton, $url) { $shell = new COM('WScript.Shell'); // note 3 $shell->Run('cmd /c start "" "' . $url . '"', 0, FALSE); // note 3 unset($shell); // note 3 } ?> |
Output
As shown above.Explanation
- Create the link button. Note that you have to use this alternative constructor to specify both the url and the label of the button. Compare this with Part 1.
- Set up the global hook.
- 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.
Related Links
- How to set up clickable link using GtkLinkButton - Part 1 - using signal clicked?
- How to change cursor over clickable GtkLabel - Part 1 - using pre defined cursors?
- How to change cursor over clickable GtkLabel - Part 2 - using image file?
- How to change cursor over clickable GtkLabel - Part 3 - using xpm data?
- How to launch external app in winxp without the flashing of cmd window?
- How to put a clickable link in GtkLabel - Part 1?
- How to put a clickable link in GtkLabel - Part 2?
Read more...