Object Oriented PHP-GTK2 - Useful Link Buttons Object Oriented PHP-GTK2 - Useful Link Buttons
it's me, bob. lol. Object Oriented PHP-GTK2 OOPS

Useful Link Buttons

Help make GtkLinkButton a useful widget. It needs it.

Topic: GtkLinkButton (View All Tutorials)
Keywords: gtklinkbutton gtkframe static extend browser
Updated: 364 Days Ago, 2007/12/04 03:13

This widget, GtkLinkButton, will create a normal GtkButton, but with text that is styled to look like a basic hyperlink. When clicked, the colour will even change the standard 'visited link' colour. Unfortunately, it will not open the browser for you. To be honest this widget is no more useful than a normal GtkButton, other than for showing that clicking it should result in the user visiting an outside resource. To make this widget useful, we have to extend it so that it knows how to open a web browser. Yeah, I understand, I was a little annoyed at the misleadingness¹ of this widget too.

GtkLinkButton and Firefox

There is a little more bad news. We must detect what operating we are running so we know the proper command to launch. Thankfully it should not require any more in-depth detection other than “Windows” or “Not Windows”.

As I think about it more...

This might need a special case for Mac OS. If anyone is running PHP-GTK on Mac OS (or knows the command required) please contact me. Thanks!

 

Code Extending GtkLinkButton

#!/usr/bin/php -c/etc/gtk/php.ini
<?php

if(substr(strtolower(PHP_OS),0,3) == 'win') {
    
define('WINDOWS',true);
} else {
    
define('WINDOWS',false);
}

class 
bobLinkButton extends GtkLinkButton {

    public function 
__construct($uri,$title null) {
        if(
$title) { parent::__construct($uri,$title); }
        else { 
parent::__construct($uri); }
        
        
$this->connect_simple('clicked',array('bobLinkButton','go'),$uri);
        return;
    }
    
    
// note 1
    
static function go($uri null) {
        if(
$uri) {
            if(
WINDOWS) {
                
// note 2
                
system(sprintf('cmd /c start "" "%s"',$uri));
            }
            
            else {
                
// note 3
                
$try = array('gnome-open','xfbrowser4','firefox','mozilla');
                
$fail true;
                foreach(
$try as $which) {
                    if(
substr(shell_exec("which {$which}"),0,1) == '/') {
                        
system(sprintf('%s "%s" &> /dev/null &',$which,$uri));
                        
$fail false;
                        break;
                    }
                } unset(
$try,$which);
                
                if(
$fail) {
                    echo(
"Warning: unable to automatically open a browser.\n");
                }
            }
        }
        
        return;
    }
}

class 
bobWindow extends GtkWindow {

    public 
$vbox,$linkvbox,$bbox;
    public 
$linkframe;
    public 
$linkbutton = array();
    public 
$quitbutton;
    
    const 
TITLE 'Some of Bob\'s Favourite Links';

    public function 
__construct() {
        
parent::__construct();
    
        
$this->vbox = new GtkVBox;
        
$this->bbox = new GtkHBox;
        
$this->linkvbox = new GtkVBox;
        
$this->linkframe = new GtkFrame(self::TITLE);
        
$this->quitbutton = new GtkButton('Quit This');

        
$links = array(
            
'opsat' => array('Bob\'s Blog','http://www.opsat.net'),
            
'ems' => array('Elizabeth\'s Blog','http://elizabethmariesmith.com'),
            
'oops' => array('Object Oriented PHP-GTK2','http://oops.opsat.net'),
            
'kateos' => array('KateOS','http://www.kateos.org'),
            
'shr' => array('Squirrels Hate Robots','http://www.squirrelshaterobots.com'),
            
'fb' => array('Frozen Bubble 2','http://frozen-bubble.org')
        );        
        
        foreach(
$links as $key => $link) {
            
$this->linkbutton[$key] = new bobLinkButton($link[1],$link[0]);
        } unset(
$links);
        
        
$this->set_title(self::TITLE);
        
$this->set_position(Gtk::WIN_POS_CENTER);
        
$this->set_border_width(6);
        
$this->set_size_request(400,-1);
        
$this->connect_simple('delete-event',array($this,'on_quit'));
        
$this->quitbutton->connect_simple('clicked',array($this,'on_quit'));
    
        foreach(
$this->linkbutton as $btn) {
            
$this->linkvbox->pack_start($btn,true,true,2);
        }
        
        
$this->bbox->pack_end($this->quitbutton,false,false,0);
        
        
$this->linkframe->add($this->linkvbox);
        
$this->vbox->pack_start($this->linkframe,true,true,0);
        
$this->vbox->pack_start($this->bbox,false,false,0);
        
$this->add($this->vbox);    

        
$this->quitbutton->grab_focus();
        
$this->show_all();
        return;
    }
    
    public function 
on_quit() {
        
$this->hide();
        
Gtk::main_quit();
        return;
    }
}

$w = new bobWindow;

Gtk::main();

$w->destroy();
unset(
$w);
exit(
0);

?>

Note 1

To make the method which runs the web browser at least practical, I have declared it as a static method. Why? This way it can be used anywhere in the application, even when not connected to any instance of a Link Button.

<?php
bobLinkButton
::go('http://gtk.php.net');
?>

Note 2

Because Windows has a centralized configuration method (aka, the registry) if run on a Windows platform we simply execute the command to launch a web browser.

Note 3

Unlucky us on Unix systems though, because nobody can agree on anything there is no real good way to centralize configurations for what browser should be opened. This is why Pidgin and other applications have built-in options for you to select the browser to use.

To keep with the theme though, this method attempts to automatically 'detect' the browser by first looking for common Window Manager launchers of preferred applications (GNOME uses gnome-open, Xfce4 uses xfbrowser4, etc) and failing finding one of those, attempts to directly launch some common browsers. If you are determined to not annoy your user by accidentally opening the wrong browser, you might want to just build it an option for them to specify what command to use.

 

End Game

 

¹ If misleadingness is not a word, copyright © 2007 bob, it is now. pwnt.

Was this document helpful? I appreciate your feedback.
What are the rules about reusing this code?


i can has web two point ooh // copyright © 2007-2008 bob majdak jr
[ xhtml css | firefox ie7 opera ]