PHP-GTK2 Newsletter
PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou
Sample Code 254: How to set up clickable link using GtkLinkButton - Part 2 - using set_uri_hook? |
|
Written by kksou
|
|
Saturday, 09 June 2007 |
|
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 with GtkLinkButton::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 constructor new GtkLinkButton($url, $label) where $url is the url of the link, and label 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 17 18 19 20 21 22 23 24 25 26 27 28
| <?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);
|
- 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
- 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
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |
|