PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 154: How to highlight matching brackets in GtkTextView - Part 3?
Written by kksou   
Monday, 29 January 2007
Problem

In Part 1 and Part 2, you have highlighted enclosing curly brackets in a GtkTextView.

We will make one more improvement in this article. When the user moves the cursor with the mouse or arrow keys, the highlight will automatically disappear as shown below:

How to highlight matching brackets in GtkTextView - Part 3?


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   
<?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("Find matching curly bracket - Part 3\n".
"The highlight will disappear\n".
"when you move the cursor with mouse or keypress");
$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);

$left_start_iter = $left_end_iter = null;
$right_start_iter = $right_end_iter = null;

// Create a new buffer and a new view to show the buffer.
$buffer = new GtkTextBuffer();
$buffer->set_text('if ($i==1) {
    test();
} else {
    test2();
}');
$view = new GtkTextView();
$view->set_buffer($buffer);
$view->modify_font(new PangoFontDescription("Arial 10"));
$view->set_wrap_mode(Gtk::WRAP_WORD);
$view->connect('button-press-event', 'on_button_press_in_textview'); // note 1
$view->connect('key-press-event', 'on_key_press_in_textview'); // note 2

$hbox = new GtkHBox();
$hbox->pack_start($button = new GtkButton('Find enclosing bracket'), 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);

$tag_table = $buffer->get_tag_table();
$tag['highlight'] = new GtkTextTag();
$tag['highlight']->set_property('background', "#ffff00");
$tag_table->add($tag['highlight']);

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

function find_left_bracket($iter, $buffer) {
    global $tag;
    $iter2 = $iter->copy();

    $match_start = $iter->copy();
    $match_end = $iter->copy();

    // check left { first
    $found = $iter->backward_search('{', 0, $match_start,
        $match_end, null);
    if (!$found) return null;

    // check if there's a } on the left
    $match_start2 = $iter->copy();
    $match_end2 = $iter->copy();

    $found2 = $iter2->backward_search('}', 0, $match_start2,
        $match_end2, null);

    if ($found2 && $match_start->compare($match_start2)!=1) return null;

    $buffer->apply_tag($tag['highlight'], $match_start, $match_end);
    return array($match_start, $match_end);
}

function find_right_bracket($iter, $buffer) {
    global $tag;
    $iter2 = $iter->copy();
    $match_start = $iter->copy();
    $match_end = $iter->copy();

    // check right } first
    $found = $iter->forward_search('}', 0, $match_start,
        $match_end, null);
    if (!$found) return null;

    // check if there's a { on the left
    $match_start2 = $iter->copy();
    $match_end2 = $iter->copy();

    $found2 = $iter2->forward_search('{', 0, $match_start2,
        $match_end2, null);

    if ($found2 && $match_start->compare($match_start2)!=-1) return null;

    $buffer->apply_tag($tag['highlight'], $match_start, $match_end);
    return array($match_start, $match_end);
  • 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. Detect mouse movement.
  2. Detect keypress.
  3. Check if there's any existing highlight.
  4. If there is, remove the highlight!

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