158. How to insert links in GtkTextView - Part 2 - Activate Link?

Problem

You want to allow users to insert links in a GtkTextView. The user will highlight some text and press the "Insert Link" button. A popup dialog box will appear where the user enters the URL address. On pressing return, the URL address will be registered, and the link highlighted in the standard blue underlined text.

In Part 1, we have the link displayed. In this article, we will launch the link in the default browser when the user clicks on the link in the textview as shown below:

How to insert links in GtkTextView - Part 2 - Activate Link?


Solution


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
18   
21   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
57   
58   
59   
60   
67   
68   
69   
70   
71   
72   
73   
74   
76   
77   
78   
79   
80   
82   
84   
85   
86   
90   
91   
92   
94   
95   
96   
98   
99   
100   
101   
102   
103   
104   
106   
107   
109   
118   
119   
120   
121   
122   
123   
124   
125   
128   
129   
130   
131   
132   
133   
134   
135   
137   
138   
140   
143   
144   
145   
146   
148   
149   
151   
152   
153   
154   
155   
156   
157   
158   
159   
161   
165   
166   
167   
168   
170   
171   
172   
173   
174   
176   
177   
178   
179   
180   
181   
182   
184   
185   
186   
187   
188   
189   
190   
191   
192   
193   
194   
195   
196   
197   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Insert Links in GtkTextView - Part 2\n".
"Click on the link.\n".
"The link will be opened in your default browser.");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 60);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0.5, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment);

// Setup TextBuffer and TextView
$tag_count = 0;
$buffer = new GtkTextBuffer();
$buffer->set_text('PHP-GTK2 resources:
1) manual: http://gtk.php.net/manual/en/gtkclasses.php
2) mailing list: http://www.nabble.com/Php---GTK---General-f171.html
');
$view = new GtkTextView();
$view->set_buffer($buffer);
$view->modify_font(new PangoFontDescription("Arial 10"));
$view->set_wrap_mode(Gtk::WRAP_WORD);
$buffer->connect('mark-set', 'on_mark_set'); // note 1

$hbox = new GtkHBox();
$hbox->pack_start($button = new GtkButton('Insert Link'), 0);
$vbox->pack_start($hbox, 0);
$button->connect('clicked', 'on_button', $buffer);

$scrolled_win = new GtkScrolledWindow();
$scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
$vbox->pack_start($scrolled_win);
$scrolled_win->add($view);

$window->show_all();
Gtk::main();

// Setup highlight tag

class LinkTag extends GtkTextTag {
    function __construct($link) {
        global $buffer;
        parent::__construct();
        $tag_table = $buffer->get_tag_table();
        $this->set_property('foreground', "#0000ff");
        $this->set_property('underline', Pango::UNDERLINE_SINGLE);
        $tag_table->add($this);
    }
}

function on_button($button, $buffer) {
    global $view, $tag, $url, $tag_count;

    $cursor_pos = $buffer->get_mark('insert');
    $iter = $buffer->get_iter_at_mark($cursor_pos);

    list($start, $end) = $buffer->get_selection_bounds();
    if ($start==null || $end==null) return; // no selection

    $url[$tag_count] = prompt("Enter URL:");
    $tag[$tag_count] = new LinkTag($url[$tag_count]);
    $buffer->apply_tag($tag[$tag_count], $start, $end);
    ++$tag_count;

    $buffer->place_cursor($end);
    $view->grab_focus();
}

function on_mark_set($buffer, $textiter, $textmark) {

    global $tag, $url, $tag_count;

    if ($textmark->get_name()!='insert') return;

    $cursor_pos = $buffer->get_insert(); // note 2
    $iter = $buffer->get_iter_at_mark($cursor_pos); // note 2

    for ($i=0; $i<$tag_count; ++$i) {
        if ($iter->has_tag($tag[$i])) { // note 3
            if (PHP_SHLIB_SUFFIX=='dll') {
                $shell = new COM('WScript.Shell'); // note 4
                $shell->Run('cmd /c start "" "' . $url[$i] . '"', 0, FALSE); // note 4
                unset($shell); // note 4
            } else {
                exec("firefox $url[$i] > /dev/null &"); // note 5
            }
        } else {
        }
    }
    return true;
}

//function to prompt for user data
function prompt($str) {
    $prompt = new Prompt($str);
    $input = $prompt->entry->get_text();
    return $input;
}

class Prompt{

    var $entry; // the user input

    function Prompt($str) {
        $dialog = new GtkDialog('Prompt', null, Gtk::DIALOG_MODAL);
        $top_area = $dialog->vbox;
        $top_area->pack_start($hbox = new GtkHBox());
        $stock = GtkImage::new_from_stock(Gtk::STOCK_DIALOG_QUESTION,
            Gtk::ICON_SIZE_DIALOG);
        $hbox->pack_start($stock, 0, 0);
        $hbox->pack_start(new GtkLabel($str));
        $this->entry = new GtkEntry();
        $hbox->pack_start($this->entry, 0, 0);
        $hbox->pack_start(new GtkLabel(' '), 0, 0);

        $dialog->add_button(Gtk::STOCK_OK, Gtk::RESPONSE_OK);

        $buttons = $dialog->action_area->get_children();
        $button_ok = $buttons[0]; // get the ID of the OK button
        // simulate button click when user press enter
        $this->entry->connect('activate', array(&$this, 'on_enter'), $button_ok);

        $dialog->set_has_separator(false);
        $dialog->show_all();
        $dialog->run();
        $dialog->destroy();
    }

    // simulate button click when user press enter
    function on_enter($entry, $button) {
        $button->clicked();
    }

}

?>

Output

As shown above.
 

Explanation

We make use of the code in Part 1.

We also make use of the code in How to launch external app in winxp without the flashing of cmd window? to launch the url in external for windows platform.

What's new here:

  1. Connect the mark-set signal.
  2. Get the current cursor position.
  3. Check if there's any tag at the current cursor position.
  4. If there is, launch the url in the default browser for windows platform.
  5. For linux users, please replace this with your favorite browser.

Note

In the next article, we will show you how to display the url address in the status bar when the mouse hovers over the link.

Related Links

Add comment


Security code
Refresh