224. How to set the tab position for GtkTextView - Part 1 - in pixel unit?

Problem

By default, GtkTreeView sets the tab at every eighth character as shown below:

You would like to change the tab position of a GtkTextView. In this Part 1, we will show how to set the tab position using pixel value (e.g. every 100 pixels) as shown below:

How to set the tab position for GtkTextView - Part 1 - in pixel unit?


Solution


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
17   
18   
19   
20   
21   
22   
23   
24   
25   
30   
31   
32   
33   
34   
35   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
<?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("Set the Tab Position of GtkTextView\n".
"Part 1 - by pixel unit");
$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, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

// 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("Courier New"));
$view->set_wrap_mode(Gtk::WRAP_WORD);

$tabs = new PangoTabArray(0, true); // note 1
$tabs->set_tab(0, PANGO::TAB_LEFT, 100); // note 2
$tabs->set_tab(1, PANGO::TAB_LEFT, 200); // note 2
$tabs->set_tab(2, PANGO::TAB_LEFT, 300); // note 2
$view->set_tabs($tabs); // note 3

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

?>

Output

As shown above.

 

Explanation

  1. Create a new PangoTabArray. Note that for the second parameter, set the value to true to use pixel unit. Otherwise it will use pango unit.
  2. Set the tab positions. In this example, we set a tab for every 100 pixels.
  3. Set the tab on the treeview.

Related Links

Add comment


Security code
Refresh