PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 161: How to insert links in GtkTextView - Part 5 - Display Tooltip over Link?
Written by kksou   
Tuesday, 06 February 2007
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 as blue underlined text.
  • In Part 2, we launch the link in the default browser when the user clicks on the link.
  • In Part 3, we display the url address in the status bar when the user hovers the mouse over the link.
  • In Part 4, we change the cursor when the user hovers the mouse over the link in the textview.

In this article, we will display a tooltip when the user hovers the mouse over the link in the textview as shown below:

How to insert links in GtkTextView - Part 5 - Display Tooltip over Link?


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   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
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   
106   
107   
108   
109   
110   
111   
112   
113   
114   
115   
116   
117   
118   
119   
120   
121   
122   
123   
124   
125   
126   
127   
128   
129   
130   
131   
132   
133   
134   
135   
136   
137   
138   
139   
140   
141   
142   
143   
144   
145   
146   
147   
148   
149   
150   
151   
152   
153   
154   
155   
156   
<?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 5\n".
"Display Tooltip over Link");
$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.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');
$view->connect('motion-notify-event', 'on_motion_in_textview');
$view->set_events(Gdk::POINTER_MOTION_MASK);


$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);

$scrolled_win->add($view);
$vbox->pack_start($scrolled_win);

// setup status area
$status = new GtkStatusbar(); // note 1
$vbox->pack_start($status, 0);

$window->show_all();

$standard_cursor = new GdkCursor(Gdk::XTERM);
$link_cursor = new GdkCursor(Gdk::DRAFT_LARGE);

$tooltips = new TreeviewTooltips(); // note 1

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();
    $iter = $buffer->get_iter_at_mark($cursor_pos);

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

function on_motion_in_textview($view, $event) {
    global $standard_cursor, $link_cursor;
    $buffer_location = $view->window_to_buffer_coords
        (Gtk::TEXT_WINDOW_TEXT, $event->x, $event->y);
    $iter = $view->get_iter_at_location(
        $buffer_location[0], $buffer_location[1]);
    if ($iter==null) return;

    global $tag, $url, $tag_count, $status;
    for ($i=0; $i<$tag_count; ++$i) {
        $context_id = $status->get_context_id('msg1');
        if ($iter->has_tag($tag[$i])) {
            $status->pop($context_id);
            $status->push($context_id, $url[$i]);
            $view->get_window(Gtk::TEXT_WINDOW_TEXT)->set_cursor($link_cursor);

            global $tooltips;
            $tooltips->on_motion($event, $url[$i]); // note 2
            break;
        } else {
            $status->pop($context_id);
            $view->get_window(Gtk::TEXT_WINDOW_TEXT)->set_cursor($standard_cursor);

            global $tooltips;
            $tooltips->on_leave($event); // note 3
        }
    }
}

//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());
  • 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

We make use of the code in Part 4.

We also make use of the code in How to display tooltips in GtkTreeView - Part 1? to display the tooltips.

What's new here:

  1. Set up the tooltips.
  2. Display the tooltip.
  3. Hide the tooltip.

Note

This example works only on windows. It does not work on Linux. This is because for some reason, GtkTextView does not emit the signal motion-notify-event in linux. So there's no way you can know if the current mouse location is over a link.


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.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved