PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 165: How to tab out of GtkTextView - Part 2?
Written by kksou   
Thursday, 08 February 2007
Problem

In Part 1, we press Tab to move focus out of the textview.

Suppose you still want to be able to insert a tab key if necessary. If you're used to Microsoft Word, the convention used is to press Ctrl-tab to enter a tab. We'll adopt the same convention here, i.e. press Tab to move out of textview, and Ctrl-Tab to insert a tab as shown below:

How to tab out of GtkTextView - Part 2?


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   
<?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("Press tab to move out of TextView\n".
"Part 2 - Insert Tab");
$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, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

$hbox = new GtkHBox();
$hbox->pack_start(new GtkLabel("Search string: "), 0);
$search_box = new GtkEntry();
$hbox->pack_start($search_box, 0);
$hbox->pack_start($button_search = new GtkButton('Search'), 0);
$hbox->pack_start($button_search_next = new GtkButton('Next'), 0);
$vbox->pack_start($hbox, 0);
$button_search->connect('clicked', 'on_search_button', $search_box);
$button_search_next->connect('clicked', 'on_search_next_button', $search_box);

// Create a new buffer and a new view to show the buffer.
$buffer = new GtkTextBuffer();
$view = new GtkTextView();
$view->set_buffer($buffer);
$view->modify_font(new PangoFontDescription("Arial 12"));
$view->set_wrap_mode(Gtk::WRAP_WORD);
$view->connect('key-press-event', 'on_keypress', $search_box);

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

function on_search_button($button, $entry) {
    global $view, $buffer;
    $search_str = $entry->get_text();
    $start_iter = $buffer->get_start_iter();
    search($search_str, $start_iter);
}

function on_search_next_button($button, $entry) {
    global $view, $buffer;
    $search_str = $entry->get_text();
    $last_search_pos = $buffer->get_mark('last_search_pos');
    if ($last_search_pos==null) return;

    $last_search_iter = $buffer->get_iter_at_mark($last_search_pos);
    search($search_str, $last_search_iter);
}

function search($search_str, $start_iter) {
    global $view, $buffer;
  • 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 .

What's new here:

  1. Is it a ctrl-tab?
  2. Yes, insert a tab at the current cursor position.
  3. No, it's just a tab. Move the focus to the search box.

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