PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 157: How to insert links in GtkTextView - Part 1 - Show Link?
Written by kksou   
Tuesday, 30 January 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 as shown below:

How to insert links in GtkTextView - Part 1 - Show 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   
<?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 1\n".
"Highlight some text and press the \"Insert Link\" button.\n".
"You will be prompted to enter the URL address.");
$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 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);

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

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

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

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

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

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

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

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

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

class Prompt{

  • 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
  1. Set up the "Insert Link" button.
  2. Extends GtkTextTag to form a new class LinkTag to display the link as blue underlined text.
  3. Get the current selection.
  4. Use a popup dialog box to get the URL address from user.
  5. Create a new tag and apply it.
  6. Pleaes refer to How to display a popup dialog to prompt for data? for detailed explanation of function prompt().

Note

In this article, we have displayed the link.

In the next article, we will show you how to launch the link in the default browser when the user clicks on the link in the textview.


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 >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved